1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
26 #include "coretypes.h"
29 #include "stringpool.h"
31 #include "stor-layout.h"
41 /* Id for dumping the class hierarchy. */
44 /* The number of nested classes being processed. If we are not in the
45 scope of any class, this is zero. */
47 int current_class_depth;
49 /* In order to deal with nested classes, we keep a stack of classes.
50 The topmost entry is the innermost class, and is the entry at index
51 CURRENT_CLASS_DEPTH */
53 typedef struct class_stack_node {
54 /* The name of the class. */
57 /* The _TYPE node for the class. */
60 /* The access specifier pending for new declarations in the scope of
64 /* If were defining TYPE, the names used in this class. */
65 splay_tree names_used;
67 /* Nonzero if this class is no longer open, because of a call to
70 }* class_stack_node_t;
74 /* The base for which we're building initializers. */
76 /* The type of the most-derived type. */
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
81 /* The negative-index vtable initializers built up so far. These
82 are in order from least negative index to most negative index. */
83 vec<constructor_elt, va_gc> *inits;
84 /* The binfo for the virtual base for which we're building
85 vcall offset initializers. */
87 /* The functions in vbase for which we have already provided vcall
89 vec<tree, va_gc> *fns;
90 /* The vtable index of the next vcall or vbase offset. */
92 /* Nonzero if we are building the initializer for the primary
95 /* Nonzero if we are building the initializer for a construction
98 /* True when adding vcall offset entries to the vtable. False when
99 merely computing the indices. */
100 bool generate_vcall_entries;
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
106 /* The stack itself. This is a dynamically resized array. The
107 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 static int current_class_stack_size;
109 static class_stack_node_t current_class_stack;
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
114 /* An array of all local classes present in this translation unit, in
115 declaration order. */
116 vec<tree, va_gc> *local_classes;
118 static tree get_vfield_name (tree);
119 static void finish_struct_anon (tree);
120 static tree get_vtable_name (tree);
121 static void get_basefndecls (tree, tree, vec<tree> *);
122 static int build_primary_vtable (tree, tree);
123 static int build_secondary_vtable (tree);
124 static void finish_vtbls (tree);
125 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
126 static void finish_struct_bits (tree);
127 static int alter_access (tree, tree, tree);
128 static void handle_using_decl (tree, tree);
129 static tree dfs_modify_vtables (tree, void *);
130 static tree modify_all_vtables (tree, tree);
131 static void determine_primary_bases (tree);
132 static void maybe_warn_about_overly_private_class (tree);
133 static void add_implicitly_declared_members (tree, tree*, int, int);
134 static tree fixed_type_or_null (tree, int *, int *);
135 static tree build_simple_base_path (tree expr, tree binfo);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
138 vec<constructor_elt, va_gc> **);
139 static bool check_bitfield_decl (tree);
140 static bool check_field_decl (tree, tree, int *, int *);
141 static void check_field_decls (tree, tree *, int *, int *);
142 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
143 static void build_base_fields (record_layout_info, splay_tree, tree *);
144 static void check_methods (tree);
145 static void remove_zero_width_bit_fields (tree);
146 static bool accessible_nvdtor_p (tree);
148 /* Used by find_flexarrays and related functions. */
150 static void diagnose_flexarrays (tree, const flexmems_t *);
151 static void find_flexarrays (tree, flexmems_t *, bool = false,
152 tree = NULL_TREE, tree = NULL_TREE);
153 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
154 static void check_bases (tree, int *, int *);
155 static void check_bases_and_members (tree);
156 static tree create_vtable_ptr (tree, tree *);
157 static void include_empty_classes (record_layout_info);
158 static void layout_class_type (tree, tree *);
159 static void propagate_binfo_offsets (tree, tree);
160 static void layout_virtual_bases (record_layout_info, splay_tree);
161 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
164 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
165 static void add_vcall_offset (tree, tree, vtbl_init_data *);
166 static void layout_vtable_decl (tree, int);
167 static tree dfs_find_final_overrider_pre (tree, void *);
168 static tree dfs_find_final_overrider_post (tree, void *);
169 static tree find_final_overrider (tree, tree, tree);
170 static int make_new_vtable (tree, tree);
171 static tree get_primary_binfo (tree);
172 static int maybe_indent_hierarchy (FILE *, int, int);
173 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
174 static void dump_class_hierarchy (tree);
175 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
176 static void dump_array (FILE *, tree);
177 static void dump_vtable (tree, tree, tree);
178 static void dump_vtt (tree, tree);
179 static void dump_thunk (FILE *, int, tree);
180 static tree build_vtable (tree, tree, tree);
181 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
182 static void layout_nonempty_base_or_field (record_layout_info,
183 tree, tree, splay_tree);
184 static tree end_of_class (tree, int);
185 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
186 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
187 vec<constructor_elt, va_gc> **);
188 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
189 vec<constructor_elt, va_gc> **);
190 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
191 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
192 static void clone_constructors_and_destructors (tree);
193 static tree build_clone (tree, tree);
194 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
195 static void build_ctor_vtbl_group (tree, tree);
196 static void build_vtt (tree);
197 static tree binfo_ctor_vtable (tree);
198 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
200 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
201 static tree dfs_fixup_binfo_vtbls (tree, void *);
202 static int record_subobject_offset (tree, tree, splay_tree);
203 static int check_subobject_offset (tree, tree, splay_tree);
204 static int walk_subobject_offsets (tree, subobject_offset_fn,
205 tree, splay_tree, tree, int);
206 static void record_subobject_offsets (tree, tree, splay_tree, bool);
207 static int layout_conflict_p (tree, tree, splay_tree, int);
208 static int splay_tree_compare_integer_csts (splay_tree_key k1,
210 static void warn_about_ambiguous_bases (tree);
211 static bool type_requires_array_cookie (tree);
212 static bool base_derived_from (tree, tree);
213 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
214 static tree end_of_base (tree);
215 static tree get_vcall_index (tree, tree);
216 static bool type_maybe_constexpr_default_constructor (tree);
218 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
219 'structor is in charge of 'structing virtual bases, or FALSE_STMT
223 build_if_in_charge (tree true_stmt, tree false_stmt)
225 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
226 tree cmp = build2 (NE_EXPR, boolean_type_node,
227 current_in_charge_parm, integer_zero_node);
228 tree type = unlowered_expr_type (true_stmt);
229 if (VOID_TYPE_P (type))
230 type = unlowered_expr_type (false_stmt);
231 tree cond = build3 (COND_EXPR, type,
232 cmp, true_stmt, false_stmt);
236 /* Convert to or from a base subobject. EXPR is an expression of type
237 `A' or `A*', an expression of type `B' or `B*' is returned. To
238 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
239 the B base instance within A. To convert base A to derived B, CODE
240 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
241 In this latter case, A must not be a morally virtual base of B.
242 NONNULL is true if EXPR is known to be non-NULL (this is only
243 needed when EXPR is of pointer type). CV qualifiers are preserved
247 build_base_path (enum tree_code code,
251 tsubst_flags_t complain)
253 tree v_binfo = NULL_TREE;
254 tree d_binfo = NULL_TREE;
258 tree null_test = NULL;
259 tree ptr_target_type;
261 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
262 bool has_empty = false;
266 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
267 return error_mark_node;
269 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
272 if (is_empty_class (BINFO_TYPE (probe)))
274 if (!v_binfo && BINFO_VIRTUAL_P (probe))
278 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
280 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
281 if (dependent_type_p (probe))
282 if (tree open = currently_open_class (probe))
285 if (code == PLUS_EXPR
286 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
288 /* This can happen when adjust_result_of_qualified_name_lookup can't
289 find a unique base binfo in a call to a member function. We
290 couldn't give the diagnostic then since we might have been calling
291 a static member function, so we do it now. In other cases, eg.
292 during error recovery (c++/71979), we may not have a base at all. */
293 if (complain & tf_error)
295 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
296 ba_unique, NULL, complain);
297 gcc_assert (base == error_mark_node || !base);
299 return error_mark_node;
302 gcc_assert ((code == MINUS_EXPR
303 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
304 || code == PLUS_EXPR);
306 if (binfo == d_binfo)
310 if (code == MINUS_EXPR && v_binfo)
312 if (complain & tf_error)
314 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
317 error ("cannot convert from pointer to base class %qT to "
318 "pointer to derived class %qT because the base is "
319 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
321 error ("cannot convert from base class %qT to derived "
322 "class %qT because the base is virtual",
323 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
328 error ("cannot convert from pointer to base class %qT to "
329 "pointer to derived class %qT via virtual base %qT",
330 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
331 BINFO_TYPE (v_binfo));
333 error ("cannot convert from base class %qT to derived "
334 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
335 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
338 return error_mark_node;
343 rvalue = !lvalue_p (expr);
344 /* This must happen before the call to save_expr. */
345 expr = cp_build_addr_expr (expr, complain);
348 expr = mark_rvalue_use (expr);
350 offset = BINFO_OFFSET (binfo);
351 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
352 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
353 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
354 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
355 expression returned matches the input. */
356 target_type = cp_build_qualified_type
357 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
358 ptr_target_type = build_pointer_type (target_type);
360 /* Do we need to look in the vtable for the real offset? */
361 virtual_access = (v_binfo && fixed_type_p <= 0);
363 /* Don't bother with the calculations inside sizeof; they'll ICE if the
364 source type is incomplete and the pointer value doesn't matter. In a
365 template (even in instantiate_non_dependent_expr), we don't have vtables
366 set up properly yet, and the value doesn't matter there either; we're
367 just interested in the result of overload resolution. */
368 if (cp_unevaluated_operand != 0
369 || processing_template_decl
370 || in_template_function ())
372 expr = build_nop (ptr_target_type, expr);
376 /* If we're in an NSDMI, we don't have the full constructor context yet
377 that we need for converting to a virtual base, so just build a stub
378 CONVERT_EXPR and expand it later in bot_replace. */
379 if (virtual_access && fixed_type_p < 0
380 && current_scope () != current_function_decl)
382 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
383 CONVERT_EXPR_VBASE_PATH (expr) = true;
387 /* Do we need to check for a null pointer? */
388 if (want_pointer && !nonnull)
390 /* If we know the conversion will not actually change the value
391 of EXPR, then we can avoid testing the expression for NULL.
392 We have to avoid generating a COMPONENT_REF for a base class
393 field, because other parts of the compiler know that such
394 expressions are always non-NULL. */
395 if (!virtual_access && integer_zerop (offset))
396 return build_nop (ptr_target_type, expr);
397 null_test = error_mark_node;
400 /* Protect against multiple evaluation if necessary. */
401 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
402 expr = save_expr (expr);
404 /* Now that we've saved expr, build the real null test. */
407 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
408 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
410 /* This is a compiler generated comparison, don't emit
411 e.g. -Wnonnull-compare warning for it. */
412 TREE_NO_WARNING (null_test) = 1;
415 /* If this is a simple base reference, express it as a COMPONENT_REF. */
416 if (code == PLUS_EXPR && !virtual_access
417 /* We don't build base fields for empty bases, and they aren't very
418 interesting to the optimizers anyway. */
421 expr = cp_build_fold_indirect_ref (expr);
422 expr = build_simple_base_path (expr, binfo);
423 if (rvalue && lvalue_p (expr))
426 expr = build_address (expr);
427 target_type = TREE_TYPE (expr);
433 /* Going via virtual base V_BINFO. We need the static offset
434 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
435 V_BINFO. That offset is an entry in D_BINFO's vtable. */
438 if (fixed_type_p < 0 && in_base_initializer)
440 /* In a base member initializer, we cannot rely on the
441 vtable being set up. We have to indirect via the
445 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
446 t = build_pointer_type (t);
447 v_offset = fold_convert (t, current_vtt_parm);
448 v_offset = cp_build_fold_indirect_ref (v_offset);
453 if (sanitize_flags_p (SANITIZE_VPTR)
454 && fixed_type_p == 0)
456 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
461 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
462 TREE_TYPE (TREE_TYPE (expr)));
465 if (v_offset == error_mark_node)
466 return error_mark_node;
468 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
469 v_offset = build1 (NOP_EXPR,
470 build_pointer_type (ptrdiff_type_node),
472 v_offset = cp_build_fold_indirect_ref (v_offset);
473 TREE_CONSTANT (v_offset) = 1;
475 offset = convert_to_integer (ptrdiff_type_node,
476 size_diffop_loc (input_location, offset,
477 BINFO_OFFSET (v_binfo)));
479 if (!integer_zerop (offset))
480 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
482 if (fixed_type_p < 0)
483 /* Negative fixed_type_p means this is a constructor or destructor;
484 virtual base layout is fixed in in-charge [cd]tors, but not in
486 offset = build_if_in_charge
487 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
494 target_type = ptr_target_type;
496 expr = build1 (NOP_EXPR, ptr_target_type, expr);
498 if (!integer_zerop (offset))
500 offset = fold_convert (sizetype, offset);
501 if (code == MINUS_EXPR)
502 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
503 expr = fold_build_pointer_plus (expr, offset);
511 expr = cp_build_fold_indirect_ref (expr);
518 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
519 build_zero_cst (target_type));
524 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
525 Perform a derived-to-base conversion by recursively building up a
526 sequence of COMPONENT_REFs to the appropriate base fields. */
529 build_simple_base_path (tree expr, tree binfo)
531 tree type = BINFO_TYPE (binfo);
532 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
535 if (d_binfo == NULL_TREE)
539 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
541 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
542 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
543 an lvalue in the front end; only _DECLs and _REFs are lvalues
545 temp = unary_complex_lvalue (ADDR_EXPR, expr);
547 expr = cp_build_fold_indirect_ref (temp);
553 expr = build_simple_base_path (expr, d_binfo);
555 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
556 field; field = DECL_CHAIN (field))
557 /* Is this the base field created by build_base_field? */
558 if (TREE_CODE (field) == FIELD_DECL
559 && DECL_FIELD_IS_BASE (field)
560 && TREE_TYPE (field) == type
561 /* If we're looking for a field in the most-derived class,
562 also check the field offset; we can have two base fields
563 of the same type if one is an indirect virtual base and one
564 is a direct non-virtual base. */
565 && (BINFO_INHERITANCE_CHAIN (d_binfo)
566 || tree_int_cst_equal (byte_position (field),
567 BINFO_OFFSET (binfo))))
569 /* We don't use build_class_member_access_expr here, as that
570 has unnecessary checks, and more importantly results in
571 recursive calls to dfs_walk_once. */
572 int type_quals = cp_type_quals (TREE_TYPE (expr));
574 expr = build3 (COMPONENT_REF,
575 cp_build_qualified_type (type, type_quals),
576 expr, field, NULL_TREE);
577 /* Mark the expression const or volatile, as appropriate.
578 Even though we've dealt with the type above, we still have
579 to mark the expression itself. */
580 if (type_quals & TYPE_QUAL_CONST)
581 TREE_READONLY (expr) = 1;
582 if (type_quals & TYPE_QUAL_VOLATILE)
583 TREE_THIS_VOLATILE (expr) = 1;
588 /* Didn't find the base field?!? */
592 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
593 type is a class type or a pointer to a class type. In the former
594 case, TYPE is also a class type; in the latter it is another
595 pointer type. If CHECK_ACCESS is true, an error message is emitted
596 if TYPE is inaccessible. If OBJECT has pointer type, the value is
597 assumed to be non-NULL. */
600 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
601 tsubst_flags_t complain)
606 if (TYPE_PTR_P (TREE_TYPE (object)))
608 object_type = TREE_TYPE (TREE_TYPE (object));
609 type = TREE_TYPE (type);
612 object_type = TREE_TYPE (object);
614 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
616 if (!binfo || binfo == error_mark_node)
617 return error_mark_node;
619 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
622 /* EXPR is an expression with unqualified class type. BASE is a base
623 binfo of that class type. Returns EXPR, converted to the BASE
624 type. This function assumes that EXPR is the most derived class;
625 therefore virtual bases can be found at their static offsets. */
628 convert_to_base_statically (tree expr, tree base)
632 expr_type = TREE_TYPE (expr);
633 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
635 /* If this is a non-empty base, use a COMPONENT_REF. */
636 if (!is_empty_class (BINFO_TYPE (base)))
637 return build_simple_base_path (expr, base);
639 /* We use fold_build2 and fold_convert below to simplify the trees
640 provided to the optimizers. It is not safe to call these functions
641 when processing a template because they do not handle C++-specific
643 gcc_assert (!processing_template_decl);
644 expr = cp_build_addr_expr (expr, tf_warning_or_error);
645 if (!integer_zerop (BINFO_OFFSET (base)))
646 expr = fold_build_pointer_plus_loc (input_location,
647 expr, BINFO_OFFSET (base));
648 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
649 expr = build_fold_indirect_ref_loc (input_location, expr);
657 build_vfield_ref (tree datum, tree type)
659 tree vfield, vcontext;
661 if (datum == error_mark_node
662 /* Can happen in case of duplicate base types (c++/59082). */
663 || !TYPE_VFIELD (type))
664 return error_mark_node;
666 /* First, convert to the requested type. */
667 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
668 datum = convert_to_base (datum, type, /*check_access=*/false,
669 /*nonnull=*/true, tf_warning_or_error);
671 /* Second, the requested type may not be the owner of its own vptr.
672 If not, convert to the base class that owns it. We cannot use
673 convert_to_base here, because VCONTEXT may appear more than once
674 in the inheritance hierarchy of TYPE, and thus direct conversion
675 between the types may be ambiguous. Following the path back up
676 one step at a time via primary bases avoids the problem. */
677 vfield = TYPE_VFIELD (type);
678 vcontext = DECL_CONTEXT (vfield);
679 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
681 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
682 type = TREE_TYPE (datum);
685 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
688 /* Given an object INSTANCE, return an expression which yields the
689 vtable element corresponding to INDEX. There are many special
690 cases for INSTANCE which we take care of here, mainly to avoid
691 creating extra tree nodes when we don't have to. */
694 build_vtbl_ref_1 (tree instance, tree idx)
697 tree vtbl = NULL_TREE;
699 /* Try to figure out what a reference refers to, and
700 access its virtual function table directly. */
703 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
705 tree basetype = non_reference (TREE_TYPE (instance));
707 if (fixed_type && !cdtorp)
709 tree binfo = lookup_base (fixed_type, basetype,
710 ba_unique, NULL, tf_none);
711 if (binfo && binfo != error_mark_node)
712 vtbl = unshare_expr (BINFO_VTABLE (binfo));
716 vtbl = build_vfield_ref (instance, basetype);
718 aref = build_array_ref (input_location, vtbl, idx);
719 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
725 build_vtbl_ref (tree instance, tree idx)
727 tree aref = build_vtbl_ref_1 (instance, idx);
732 /* Given a stable object pointer INSTANCE_PTR, return an expression which
733 yields a function pointer corresponding to vtable element INDEX. */
736 build_vfn_ref (tree instance_ptr, tree idx)
740 aref = build_vtbl_ref_1 (cp_build_fold_indirect_ref (instance_ptr),
743 /* When using function descriptors, the address of the
744 vtable entry is treated as a function pointer. */
745 if (TARGET_VTABLE_USES_DESCRIPTORS)
746 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
747 cp_build_addr_expr (aref, tf_warning_or_error));
749 /* Remember this as a method reference, for later devirtualization. */
750 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
755 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
756 for the given TYPE. */
759 get_vtable_name (tree type)
761 return mangle_vtbl_for_type (type);
764 /* DECL is an entity associated with TYPE, like a virtual table or an
765 implicitly generated constructor. Determine whether or not DECL
766 should have external or internal linkage at the object file
767 level. This routine does not deal with COMDAT linkage and other
768 similar complexities; it simply sets TREE_PUBLIC if it possible for
769 entities in other translation units to contain copies of DECL, in
773 set_linkage_according_to_type (tree /*type*/, tree decl)
775 TREE_PUBLIC (decl) = 1;
776 determine_visibility (decl);
779 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
780 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
781 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
784 build_vtable (tree class_type, tree name, tree vtable_type)
788 decl = build_lang_decl (VAR_DECL, name, vtable_type);
789 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
790 now to avoid confusion in mangle_decl. */
791 SET_DECL_ASSEMBLER_NAME (decl, name);
792 DECL_CONTEXT (decl) = class_type;
793 DECL_ARTIFICIAL (decl) = 1;
794 TREE_STATIC (decl) = 1;
795 TREE_READONLY (decl) = 1;
796 DECL_VIRTUAL_P (decl) = 1;
797 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
798 DECL_USER_ALIGN (decl) = true;
799 DECL_VTABLE_OR_VTT_P (decl) = 1;
800 set_linkage_according_to_type (class_type, decl);
801 /* The vtable has not been defined -- yet. */
802 DECL_EXTERNAL (decl) = 1;
803 DECL_NOT_REALLY_EXTERN (decl) = 1;
805 /* Mark the VAR_DECL node representing the vtable itself as a
806 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
807 is rather important that such things be ignored because any
808 effort to actually generate DWARF for them will run into
809 trouble when/if we encounter code like:
812 struct S { virtual void member (); };
814 because the artificial declaration of the vtable itself (as
815 manufactured by the g++ front end) will say that the vtable is
816 a static member of `S' but only *after* the debug output for
817 the definition of `S' has already been output. This causes
818 grief because the DWARF entry for the definition of the vtable
819 will try to refer back to an earlier *declaration* of the
820 vtable as a static member of `S' and there won't be one. We
821 might be able to arrange to have the "vtable static member"
822 attached to the member list for `S' before the debug info for
823 `S' get written (which would solve the problem) but that would
824 require more intrusive changes to the g++ front end. */
825 DECL_IGNORED_P (decl) = 1;
830 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
831 or even complete. If this does not exist, create it. If COMPLETE is
832 nonzero, then complete the definition of it -- that will render it
833 impossible to actually build the vtable, but is useful to get at those
834 which are known to exist in the runtime. */
837 get_vtable_decl (tree type, int complete)
841 if (CLASSTYPE_VTABLES (type))
842 return CLASSTYPE_VTABLES (type);
844 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
845 CLASSTYPE_VTABLES (type) = decl;
849 DECL_EXTERNAL (decl) = 1;
850 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
856 /* Build the primary virtual function table for TYPE. If BINFO is
857 non-NULL, build the vtable starting with the initial approximation
858 that it is the same as the one which is the head of the association
859 list. Returns a nonzero value if a new vtable is actually
863 build_primary_vtable (tree binfo, tree type)
868 decl = get_vtable_decl (type, /*complete=*/0);
872 if (BINFO_NEW_VTABLE_MARKED (binfo))
873 /* We have already created a vtable for this base, so there's
874 no need to do it again. */
877 virtuals = copy_list (BINFO_VIRTUALS (binfo));
878 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
879 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
880 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
884 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
885 virtuals = NULL_TREE;
888 /* Initialize the association list for this type, based
889 on our first approximation. */
890 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
891 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
892 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
896 /* Give BINFO a new virtual function table which is initialized
897 with a skeleton-copy of its original initialization. The only
898 entry that changes is the `delta' entry, so we can really
899 share a lot of structure.
901 FOR_TYPE is the most derived type which caused this table to
904 Returns nonzero if we haven't met BINFO before.
906 The order in which vtables are built (by calling this function) for
907 an object must remain the same, otherwise a binary incompatibility
911 build_secondary_vtable (tree binfo)
913 if (BINFO_NEW_VTABLE_MARKED (binfo))
914 /* We already created a vtable for this base. There's no need to
918 /* Remember that we've created a vtable for this BINFO, so that we
919 don't try to do so again. */
920 SET_BINFO_NEW_VTABLE_MARKED (binfo);
922 /* Make fresh virtual list, so we can smash it later. */
923 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
925 /* Secondary vtables are laid out as part of the same structure as
926 the primary vtable. */
927 BINFO_VTABLE (binfo) = NULL_TREE;
931 /* Create a new vtable for BINFO which is the hierarchy dominated by
932 T. Return nonzero if we actually created a new vtable. */
935 make_new_vtable (tree t, tree binfo)
937 if (binfo == TYPE_BINFO (t))
938 /* In this case, it is *type*'s vtable we are modifying. We start
939 with the approximation that its vtable is that of the
940 immediate base class. */
941 return build_primary_vtable (binfo, t);
943 /* This is our very own copy of `basetype' to play with. Later,
944 we will fill in all the virtual functions that override the
945 virtual functions in these base classes which are not defined
946 by the current type. */
947 return build_secondary_vtable (binfo);
950 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
951 (which is in the hierarchy dominated by T) list FNDECL as its
952 BV_FN. DELTA is the required constant adjustment from the `this'
953 pointer where the vtable entry appears to the `this' required when
954 the function is actually called. */
957 modify_vtable_entry (tree t,
967 if (fndecl != BV_FN (v)
968 || !tree_int_cst_equal (delta, BV_DELTA (v)))
970 /* We need a new vtable for BINFO. */
971 if (make_new_vtable (t, binfo))
973 /* If we really did make a new vtable, we also made a copy
974 of the BINFO_VIRTUALS list. Now, we have to find the
975 corresponding entry in that list. */
976 *virtuals = BINFO_VIRTUALS (binfo);
977 while (BV_FN (*virtuals) != BV_FN (v))
978 *virtuals = TREE_CHAIN (*virtuals);
982 BV_DELTA (v) = delta;
983 BV_VCALL_INDEX (v) = NULL_TREE;
989 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
990 METHOD is being injected via a using_decl. Returns true if the
991 method could be added to the method vec. */
994 add_method (tree type, tree method, bool via_using)
996 if (method == error_mark_node)
999 gcc_assert (!DECL_EXTERN_C_P (method));
1001 tree *slot = find_member_slot (type, DECL_NAME (method));
1002 tree current_fns = slot ? *slot : NULL_TREE;
1004 /* Check to see if we've already got this method. */
1005 for (ovl_iterator iter (current_fns); iter; ++iter)
1013 if (TREE_CODE (fn) != TREE_CODE (method))
1016 /* Two using-declarations can coexist, we'll complain about ambiguity in
1017 overload resolution. */
1018 if (via_using && iter.using_p ()
1019 /* Except handle inherited constructors specially. */
1020 && ! DECL_CONSTRUCTOR_P (fn))
1023 /* [over.load] Member function declarations with the
1024 same name and the same parameter types cannot be
1025 overloaded if any of them is a static member
1026 function declaration.
1028 [over.load] Member function declarations with the same name and
1029 the same parameter-type-list as well as member function template
1030 declarations with the same name, the same parameter-type-list, and
1031 the same template parameter lists cannot be overloaded if any of
1032 them, but not all, have a ref-qualifier.
1034 [namespace.udecl] When a using-declaration brings names
1035 from a base class into a derived class scope, member
1036 functions in the derived class override and/or hide member
1037 functions with the same name and parameter types in a base
1038 class (rather than conflicting). */
1039 fn_type = TREE_TYPE (fn);
1040 method_type = TREE_TYPE (method);
1041 parms1 = TYPE_ARG_TYPES (fn_type);
1042 parms2 = TYPE_ARG_TYPES (method_type);
1044 /* Compare the quals on the 'this' parm. Don't compare
1045 the whole types, as used functions are treated as
1046 coming from the using class in overload resolution. */
1047 if (! DECL_STATIC_FUNCTION_P (fn)
1048 && ! DECL_STATIC_FUNCTION_P (method)
1049 /* Either both or neither need to be ref-qualified for
1050 differing quals to allow overloading. */
1051 && (FUNCTION_REF_QUALIFIED (fn_type)
1052 == FUNCTION_REF_QUALIFIED (method_type))
1053 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1054 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1057 /* For templates, the return type and template parameters
1058 must be identical. */
1059 if (TREE_CODE (fn) == TEMPLATE_DECL
1060 && (!same_type_p (TREE_TYPE (fn_type),
1061 TREE_TYPE (method_type))
1062 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1063 DECL_TEMPLATE_PARMS (method))))
1066 if (! DECL_STATIC_FUNCTION_P (fn))
1067 parms1 = TREE_CHAIN (parms1);
1068 if (! DECL_STATIC_FUNCTION_P (method))
1069 parms2 = TREE_CHAIN (parms2);
1071 /* Bring back parameters omitted from an inherited ctor. */
1072 if (ctor_omit_inherited_parms (fn))
1073 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1074 if (ctor_omit_inherited_parms (method))
1075 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1077 if (compparms (parms1, parms2)
1078 && (!DECL_CONV_FN_P (fn)
1079 || same_type_p (TREE_TYPE (fn_type),
1080 TREE_TYPE (method_type)))
1081 && equivalently_constrained (fn, method))
1083 /* If these are versions of the same function, process and
1085 if (TREE_CODE (fn) == FUNCTION_DECL
1086 && maybe_version_functions (method, fn, true))
1089 if (DECL_INHERITED_CTOR (method))
1091 if (DECL_INHERITED_CTOR (fn))
1093 tree basem = DECL_INHERITED_CTOR_BASE (method);
1094 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1095 if (flag_new_inheriting_ctors)
1099 /* Inheriting the same constructor along different
1100 paths, combine them. */
1101 SET_DECL_INHERITED_CTOR
1102 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1103 DECL_INHERITED_CTOR (fn)));
1104 /* And discard the new one. */
1108 /* Inherited ctors can coexist until overload
1112 error_at (DECL_SOURCE_LOCATION (method),
1113 "%q#D conflicts with version inherited from %qT",
1115 inform (DECL_SOURCE_LOCATION (fn),
1116 "version inherited from %qT declared here",
1119 /* Otherwise defer to the other function. */
1124 /* Defer to the local function. */
1126 else if (flag_new_inheriting_ctors
1127 && DECL_INHERITED_CTOR (fn))
1129 /* Remove the inherited constructor. */
1130 current_fns = iter.remove_node (current_fns);
1135 error_at (DECL_SOURCE_LOCATION (method),
1136 "%q#D cannot be overloaded with %q#D", method, fn);
1137 inform (DECL_SOURCE_LOCATION (fn),
1138 "previous declaration %q#D", fn);
1144 current_fns = ovl_insert (method, current_fns, via_using);
1146 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1147 && !push_class_level_binding (DECL_NAME (method), current_fns))
1151 slot = add_member_slot (type, DECL_NAME (method));
1153 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1154 grok_special_member_properties (method);
1156 *slot = current_fns;
1161 /* Subroutines of finish_struct. */
1163 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1164 legit, otherwise return 0. */
1167 alter_access (tree t, tree fdecl, tree access)
1171 retrofit_lang_decl (fdecl);
1173 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1175 elem = purpose_member (t, DECL_ACCESS (fdecl));
1178 if (TREE_VALUE (elem) != access)
1180 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1181 error ("conflicting access specifications for method"
1182 " %q+D, ignored", TREE_TYPE (fdecl));
1184 error ("conflicting access specifications for field %qE, ignored",
1189 /* They're changing the access to the same thing they changed
1190 it to before. That's OK. */
1196 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1197 tf_warning_or_error);
1198 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1204 /* Return the access node for DECL's access in its enclosing class. */
1207 declared_access (tree decl)
1209 return (TREE_PRIVATE (decl) ? access_private_node
1210 : TREE_PROTECTED (decl) ? access_protected_node
1211 : access_public_node);
1214 /* Process the USING_DECL, which is a member of T. */
1217 handle_using_decl (tree using_decl, tree t)
1219 tree decl = USING_DECL_DECLS (using_decl);
1220 tree name = DECL_NAME (using_decl);
1221 tree access = declared_access (using_decl);
1222 tree flist = NULL_TREE;
1225 gcc_assert (!processing_template_decl && decl);
1227 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1228 tf_warning_or_error);
1231 old_value = OVL_FIRST (old_value);
1233 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1236 old_value = NULL_TREE;
1239 cp_emit_debug_info_for_using (decl, t);
1241 if (is_overloaded_fn (decl))
1246 else if (is_overloaded_fn (old_value))
1249 /* It's OK to use functions from a base when there are functions with
1250 the same name already present in the current class. */;
1253 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1254 "because of local method %q#D with same name",
1255 using_decl, t, old_value);
1256 inform (DECL_SOURCE_LOCATION (old_value),
1257 "local method %q#D declared here", old_value);
1261 else if (!DECL_ARTIFICIAL (old_value))
1263 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1264 "because of local member %q#D with same name",
1265 using_decl, t, old_value);
1266 inform (DECL_SOURCE_LOCATION (old_value),
1267 "local member %q#D declared here", old_value);
1271 /* Make type T see field decl FDECL with access ACCESS. */
1273 for (ovl_iterator iter (flist); iter; ++iter)
1275 add_method (t, *iter, true);
1276 alter_access (t, *iter, access);
1279 alter_access (t, decl, access);
1282 /* Data structure for find_abi_tags_r, below. */
1286 tree t; // The type that we're checking for missing tags.
1287 tree subob; // The subobject of T that we're getting tags from.
1288 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1291 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1292 in the context of P. TAG can be either an identifier (the DECL_NAME of
1293 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1296 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1298 if (!IDENTIFIER_MARKED (id))
1300 if (p->tags != error_mark_node)
1302 /* We're collecting tags from template arguments or from
1303 the type of a variable or function return type. */
1304 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1306 /* Don't inherit this tag multiple times. */
1307 IDENTIFIER_MARKED (id) = true;
1311 /* Tags inherited from type template arguments are only used
1312 to avoid warnings. */
1313 ABI_TAG_IMPLICIT (p->tags) = true;
1316 /* For functions and variables we want to warn, too. */
1319 /* Otherwise we're diagnosing missing tags. */
1320 if (TREE_CODE (p->t) == FUNCTION_DECL)
1322 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1323 "that %qT (used in its return type) has",
1325 inform (location_of (*tp), "%qT declared here", *tp);
1327 else if (VAR_P (p->t))
1329 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1330 "that %qT (used in its type) has", p->t, tag, *tp))
1331 inform (location_of (*tp), "%qT declared here", *tp);
1333 else if (TYPE_P (p->subob))
1335 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1336 "that base %qT has", p->t, tag, p->subob))
1337 inform (location_of (p->subob), "%qT declared here",
1342 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1343 "that %qT (used in the type of %qD) has",
1344 p->t, tag, *tp, p->subob))
1346 inform (location_of (p->subob), "%qD declared here",
1348 inform (location_of (*tp), "%qT declared here", *tp);
1354 /* Find all the ABI tags in the attribute list ATTR and either call
1355 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1358 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1362 for (; (attr = lookup_attribute ("abi_tag", attr));
1363 attr = TREE_CHAIN (attr))
1364 for (tree list = TREE_VALUE (attr); list;
1365 list = TREE_CHAIN (list))
1367 tree tag = TREE_VALUE (list);
1368 tree id = get_identifier (TREE_STRING_POINTER (tag));
1370 check_tag (tag, id, tp, p);
1372 IDENTIFIER_MARKED (id) = val;
1376 /* Find all the ABI tags on T and its enclosing scopes and either call
1377 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1380 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1382 while (t != global_namespace)
1387 attr = TYPE_ATTRIBUTES (t);
1388 t = CP_TYPE_CONTEXT (t);
1392 attr = DECL_ATTRIBUTES (t);
1393 t = CP_DECL_CONTEXT (t);
1395 mark_or_check_attr_tags (attr, tp, p, val);
1399 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1400 types with ABI tags, add the corresponding identifiers to the VEC in
1401 *DATA and set IDENTIFIER_MARKED. */
1404 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1406 if (!OVERLOAD_TYPE_P (*tp))
1409 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1410 anyway, but let's make sure of it. */
1411 *walk_subtrees = false;
1413 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1415 mark_or_check_tags (*tp, tp, p, false);
1420 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1421 IDENTIFIER_MARKED on its ABI tags. */
1424 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1426 if (!OVERLOAD_TYPE_P (*tp))
1429 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1430 anyway, but let's make sure of it. */
1431 *walk_subtrees = false;
1433 bool *valp = static_cast<bool*>(data);
1435 mark_or_check_tags (*tp, NULL, NULL, *valp);
1440 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1444 mark_abi_tags (tree t, bool val)
1446 mark_or_check_tags (t, NULL, NULL, val);
1449 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1450 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1452 /* Template arguments are part of the signature. */
1453 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1454 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1456 tree arg = TREE_VEC_ELT (level, j);
1457 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1460 if (TREE_CODE (t) == FUNCTION_DECL)
1461 /* A function's parameter types are part of the signature, so
1462 we don't need to inherit any tags that are also in them. */
1463 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1464 arg = TREE_CHAIN (arg))
1465 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1466 mark_abi_tags_r, &val);
1470 /* Check that T has all the ABI tags that subobject SUBOB has, or
1471 warn if not. If T is a (variable or function) declaration, also
1472 return any missing tags, and add them to T if JUST_CHECKING is false. */
1475 check_abi_tags (tree t, tree subob, bool just_checking = false)
1477 bool inherit = DECL_P (t);
1479 if (!inherit && !warn_abi_tag)
1482 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1483 if (!TREE_PUBLIC (decl))
1484 /* No need to worry about things local to this TU. */
1487 mark_abi_tags (t, true);
1489 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1490 struct abi_tag_data data = { t, subob, error_mark_node };
1492 data.tags = NULL_TREE;
1494 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1496 if (!(inherit && data.tags))
1497 /* We don't need to do anything with data.tags. */;
1498 else if (just_checking)
1499 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1501 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1502 IDENTIFIER_MARKED (id) = false;
1506 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1508 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1511 = tree_cons (get_identifier ("abi_tag"), data.tags,
1512 DECL_ATTRIBUTES (t));
1515 mark_abi_tags (t, false);
1520 /* Check that DECL has all the ABI tags that are used in parts of its type
1521 that are not reflected in its mangled name. */
1524 check_abi_tags (tree decl)
1527 check_abi_tags (decl, TREE_TYPE (decl));
1528 else if (TREE_CODE (decl) == FUNCTION_DECL
1529 && !DECL_CONV_FN_P (decl)
1530 && !mangle_return_type_p (decl))
1531 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1534 /* Return any ABI tags that are used in parts of the type of DECL
1535 that are not reflected in its mangled name. This function is only
1536 used in backward-compatible mangling for ABI <11. */
1539 missing_abi_tags (tree decl)
1542 return check_abi_tags (decl, TREE_TYPE (decl), true);
1543 else if (TREE_CODE (decl) == FUNCTION_DECL
1544 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1545 that we can use this function for setting need_abi_warning
1546 regardless of the current flag_abi_version. */
1547 && !mangle_return_type_p (decl))
1548 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1554 inherit_targ_abi_tags (tree t)
1556 if (!CLASS_TYPE_P (t)
1557 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1560 mark_abi_tags (t, true);
1562 tree args = CLASSTYPE_TI_ARGS (t);
1563 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1564 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1566 tree level = TMPL_ARGS_LEVEL (args, i+1);
1567 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1569 tree arg = TREE_VEC_ELT (level, j);
1571 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1575 // If we found some tags on our template arguments, add them to our
1576 // abi_tag attribute.
1579 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1581 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1584 = tree_cons (get_identifier ("abi_tag"), data.tags,
1585 TYPE_ATTRIBUTES (t));
1588 mark_abi_tags (t, false);
1591 /* Return true, iff class T has a non-virtual destructor that is
1592 accessible from outside the class heirarchy (i.e. is public, or
1593 there's a suitable friend. */
1596 accessible_nvdtor_p (tree t)
1598 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1600 /* An implicitly declared destructor is always public. And,
1601 if it were virtual, we would have created it by now. */
1605 if (DECL_VINDEX (dtor))
1606 return false; /* Virtual */
1608 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1609 return true; /* Public */
1611 if (CLASSTYPE_FRIEND_CLASSES (t)
1612 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1613 return true; /* Has friends */
1618 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1619 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1620 properties of the bases. */
1623 check_bases (tree t,
1624 int* cant_have_const_ctor_p,
1625 int* no_const_asn_ref_p)
1628 bool seen_non_virtual_nearly_empty_base_p = 0;
1629 int seen_tm_mask = 0;
1632 tree field = NULL_TREE;
1634 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1635 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1636 if (TREE_CODE (field) == FIELD_DECL)
1639 for (binfo = TYPE_BINFO (t), i = 0;
1640 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1642 tree basetype = TREE_TYPE (base_binfo);
1644 gcc_assert (COMPLETE_TYPE_P (basetype));
1646 if (CLASSTYPE_FINAL (basetype))
1647 error ("cannot derive from %<final%> base %qT in derived type %qT",
1650 /* If any base class is non-literal, so is the derived class. */
1651 if (!CLASSTYPE_LITERAL_P (basetype))
1652 CLASSTYPE_LITERAL_P (t) = false;
1654 /* If the base class doesn't have copy constructors or
1655 assignment operators that take const references, then the
1656 derived class cannot have such a member automatically
1658 if (TYPE_HAS_COPY_CTOR (basetype)
1659 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1660 *cant_have_const_ctor_p = 1;
1661 if (TYPE_HAS_COPY_ASSIGN (basetype)
1662 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1663 *no_const_asn_ref_p = 1;
1665 if (BINFO_VIRTUAL_P (base_binfo))
1666 /* A virtual base does not effect nearly emptiness. */
1668 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1670 if (seen_non_virtual_nearly_empty_base_p)
1671 /* And if there is more than one nearly empty base, then the
1672 derived class is not nearly empty either. */
1673 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1675 /* Remember we've seen one. */
1676 seen_non_virtual_nearly_empty_base_p = 1;
1678 else if (!is_empty_class (basetype))
1679 /* If the base class is not empty or nearly empty, then this
1680 class cannot be nearly empty. */
1681 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1683 /* A lot of properties from the bases also apply to the derived
1685 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1686 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1687 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1688 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1689 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1690 || !TYPE_HAS_COPY_ASSIGN (basetype));
1691 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1692 || !TYPE_HAS_COPY_CTOR (basetype));
1693 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1694 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1695 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1696 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1697 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1698 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1699 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1700 || TYPE_HAS_COMPLEX_DFLT (basetype));
1701 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1702 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1703 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1704 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1705 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1706 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1707 if (TYPE_HAS_MUTABLE_P (basetype))
1708 CLASSTYPE_HAS_MUTABLE (t) = 1;
1710 /* A standard-layout class is a class that:
1712 * has no non-standard-layout base classes, */
1713 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1714 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1717 /* ...has no base classes of the same type as the first non-static
1719 if (field && DECL_CONTEXT (field) == t
1720 && (same_type_ignoring_top_level_qualifiers_p
1721 (TREE_TYPE (field), basetype)))
1722 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1724 /* ...either has no non-static data members in the most-derived
1725 class and at most one base class with non-static data
1726 members, or has no base classes with non-static data
1728 for (basefield = TYPE_FIELDS (basetype); basefield;
1729 basefield = DECL_CHAIN (basefield))
1730 if (TREE_CODE (basefield) == FIELD_DECL
1731 && !(DECL_FIELD_IS_BASE (basefield)
1732 && integer_zerop (DECL_SIZE (basefield))))
1735 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1742 /* Don't bother collecting tm attributes if transactional memory
1743 support is not enabled. */
1746 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1748 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1751 check_abi_tags (t, basetype);
1754 /* If one of the base classes had TM attributes, and the current class
1755 doesn't define its own, then the current class inherits one. */
1756 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1758 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1759 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1763 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1764 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1765 that have had a nearly-empty virtual primary base stolen by some
1766 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1770 determine_primary_bases (tree t)
1773 tree primary = NULL_TREE;
1774 tree type_binfo = TYPE_BINFO (t);
1777 /* Determine the primary bases of our bases. */
1778 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1779 base_binfo = TREE_CHAIN (base_binfo))
1781 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1783 /* See if we're the non-virtual primary of our inheritance
1785 if (!BINFO_VIRTUAL_P (base_binfo))
1787 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1788 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1791 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1792 BINFO_TYPE (parent_primary)))
1793 /* We are the primary binfo. */
1794 BINFO_PRIMARY_P (base_binfo) = 1;
1796 /* Determine if we have a virtual primary base, and mark it so.
1798 if (primary && BINFO_VIRTUAL_P (primary))
1800 tree this_primary = copied_binfo (primary, base_binfo);
1802 if (BINFO_PRIMARY_P (this_primary))
1803 /* Someone already claimed this base. */
1804 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1809 BINFO_PRIMARY_P (this_primary) = 1;
1810 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1812 /* A virtual binfo might have been copied from within
1813 another hierarchy. As we're about to use it as a
1814 primary base, make sure the offsets match. */
1815 delta = size_diffop_loc (input_location,
1816 fold_convert (ssizetype,
1817 BINFO_OFFSET (base_binfo)),
1818 fold_convert (ssizetype,
1819 BINFO_OFFSET (this_primary)));
1821 propagate_binfo_offsets (this_primary, delta);
1826 /* First look for a dynamic direct non-virtual base. */
1827 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1829 tree basetype = BINFO_TYPE (base_binfo);
1831 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1833 primary = base_binfo;
1838 /* A "nearly-empty" virtual base class can be the primary base
1839 class, if no non-virtual polymorphic base can be found. Look for
1840 a nearly-empty virtual dynamic base that is not already a primary
1841 base of something in the hierarchy. If there is no such base,
1842 just pick the first nearly-empty virtual base. */
1844 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1845 base_binfo = TREE_CHAIN (base_binfo))
1846 if (BINFO_VIRTUAL_P (base_binfo)
1847 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1849 if (!BINFO_PRIMARY_P (base_binfo))
1851 /* Found one that is not primary. */
1852 primary = base_binfo;
1856 /* Remember the first candidate. */
1857 primary = base_binfo;
1861 /* If we've got a primary base, use it. */
1864 tree basetype = BINFO_TYPE (primary);
1866 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1867 if (BINFO_PRIMARY_P (primary))
1868 /* We are stealing a primary base. */
1869 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1870 BINFO_PRIMARY_P (primary) = 1;
1871 if (BINFO_VIRTUAL_P (primary))
1875 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1876 /* A virtual binfo might have been copied from within
1877 another hierarchy. As we're about to use it as a primary
1878 base, make sure the offsets match. */
1879 delta = size_diffop_loc (input_location, ssize_int (0),
1880 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1882 propagate_binfo_offsets (primary, delta);
1885 primary = TYPE_BINFO (basetype);
1887 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1888 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1889 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1893 /* Update the variant types of T. */
1896 fixup_type_variants (tree t)
1903 for (variants = TYPE_NEXT_VARIANT (t);
1905 variants = TYPE_NEXT_VARIANT (variants))
1907 /* These fields are in the _TYPE part of the node, not in
1908 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1909 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1910 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1911 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1912 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1914 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1916 TYPE_BINFO (variants) = TYPE_BINFO (t);
1918 /* Copy whatever these are holding today. */
1919 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1920 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1924 /* KLASS is a class that we're applying may_alias to after the body is
1925 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1926 canonical type(s) will be implicitly updated. */
1929 fixup_may_alias (tree klass)
1933 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1934 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1935 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1936 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1937 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1938 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1941 /* Early variant fixups: we apply attributes at the beginning of the class
1942 definition, and we need to fix up any variants that have already been
1943 made via elaborated-type-specifier so that check_qualified_type works. */
1946 fixup_attribute_variants (tree t)
1953 tree attrs = TYPE_ATTRIBUTES (t);
1954 unsigned align = TYPE_ALIGN (t);
1955 bool user_align = TYPE_USER_ALIGN (t);
1956 bool may_alias = lookup_attribute ("may_alias", attrs);
1957 bool packed = TYPE_PACKED (t);
1960 fixup_may_alias (t);
1962 for (variants = TYPE_NEXT_VARIANT (t);
1964 variants = TYPE_NEXT_VARIANT (variants))
1966 /* These are the two fields that check_qualified_type looks at and
1967 are affected by attributes. */
1968 TYPE_ATTRIBUTES (variants) = attrs;
1969 unsigned valign = align;
1970 if (TYPE_USER_ALIGN (variants))
1971 valign = MAX (valign, TYPE_ALIGN (variants));
1973 TYPE_USER_ALIGN (variants) = user_align;
1974 SET_TYPE_ALIGN (variants, valign);
1975 TYPE_PACKED (variants) = packed;
1977 fixup_may_alias (variants);
1981 /* Set memoizing fields and bits of T (and its variants) for later
1985 finish_struct_bits (tree t)
1987 /* Fix up variants (if any). */
1988 fixup_type_variants (t);
1990 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1991 /* For a class w/o baseclasses, 'finish_struct' has set
1992 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1993 Similarly for a class whose base classes do not have vtables.
1994 When neither of these is true, we might have removed abstract
1995 virtuals (by providing a definition), added some (by declaring
1996 new ones), or redeclared ones from a base class. We need to
1997 recalculate what's really an abstract virtual at this point (by
1998 looking in the vtables). */
1999 get_pure_virtuals (t);
2001 /* If this type has a copy constructor or a destructor, force its
2002 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2003 nonzero. This will cause it to be passed by invisible reference
2004 and prevent it from being returned in a register. */
2005 if (type_has_nontrivial_copy_init (t)
2006 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2009 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2010 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2012 SET_TYPE_MODE (variants, BLKmode);
2013 TREE_ADDRESSABLE (variants) = 1;
2018 /* Issue warnings about T having private constructors, but no friends,
2021 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2022 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2023 non-private static member functions. */
2026 maybe_warn_about_overly_private_class (tree t)
2028 int has_member_fn = 0;
2029 int has_nonprivate_method = 0;
2030 bool nonprivate_ctor = false;
2032 if (!warn_ctor_dtor_privacy
2033 /* If the class has friends, those entities might create and
2034 access instances, so we should not warn. */
2035 || (CLASSTYPE_FRIEND_CLASSES (t)
2036 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2037 /* We will have warned when the template was declared; there's
2038 no need to warn on every instantiation. */
2039 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2040 /* There's no reason to even consider warning about this
2044 /* We only issue one warning, if more than one applies, because
2045 otherwise, on code like:
2048 // Oops - forgot `public:'
2054 we warn several times about essentially the same problem. */
2056 /* Check to see if all (non-constructor, non-destructor) member
2057 functions are private. (Since there are no friends or
2058 non-private statics, we can't ever call any of the private member
2060 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2061 if (TREE_CODE (fn) == USING_DECL
2062 && DECL_NAME (fn) == ctor_identifier
2063 && !TREE_PRIVATE (fn))
2064 nonprivate_ctor = true;
2065 else if (!DECL_DECLARES_FUNCTION_P (fn))
2066 /* Not a function. */;
2067 else if (DECL_ARTIFICIAL (fn))
2068 /* We're not interested in compiler-generated methods; they don't
2069 provide any way to call private members. */;
2070 else if (!TREE_PRIVATE (fn))
2072 if (DECL_STATIC_FUNCTION_P (fn))
2073 /* A non-private static member function is just like a
2074 friend; it can create and invoke private member
2075 functions, and be accessed without a class
2079 has_nonprivate_method = 1;
2080 /* Keep searching for a static member function. */
2082 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2085 if (!has_nonprivate_method && has_member_fn)
2087 /* There are no non-private methods, and there's at least one
2088 private member function that isn't a constructor or
2089 destructor. (If all the private members are
2090 constructors/destructors we want to use the code below that
2091 issues error messages specifically referring to
2092 constructors/destructors.) */
2094 tree binfo = TYPE_BINFO (t);
2096 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2097 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2099 has_nonprivate_method = 1;
2102 if (!has_nonprivate_method)
2104 warning (OPT_Wctor_dtor_privacy,
2105 "all member functions in class %qT are private", t);
2110 /* Even if some of the member functions are non-private, the class
2111 won't be useful for much if all the constructors or destructors
2112 are private: such an object can never be created or destroyed. */
2113 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2114 if (TREE_PRIVATE (dtor))
2116 warning (OPT_Wctor_dtor_privacy,
2117 "%q#T only defines a private destructor and has no friends",
2122 /* Warn about classes that have private constructors and no friends. */
2123 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2124 /* Implicitly generated constructors are always public. */
2125 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2127 tree copy_or_move = NULL_TREE;
2129 /* If a non-template class does not define a copy
2130 constructor, one is defined for it, enabling it to avoid
2131 this warning. For a template class, this does not
2132 happen, and so we would normally get a warning on:
2134 template <class T> class C { private: C(); };
2136 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2137 complete non-template or fully instantiated classes have this
2139 if (!TYPE_HAS_COPY_CTOR (t))
2140 nonprivate_ctor = true;
2142 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2143 !nonprivate_ctor && iter; ++iter)
2144 if (TREE_PRIVATE (*iter))
2146 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2147 /* Ideally, we wouldn't count any constructor that takes
2148 an argument of the class type as a parameter, because
2149 such things cannot be used to construct an instance of
2150 the class unless you already have one. */
2151 copy_or_move = *iter;
2153 nonprivate_ctor = true;
2155 if (!nonprivate_ctor)
2157 warning (OPT_Wctor_dtor_privacy,
2158 "%q#T only defines private constructors and has no friends",
2161 inform (DECL_SOURCE_LOCATION (copy_or_move),
2162 "%q#D is public, but requires an existing %q#T object",
2169 /* Make BINFO's vtable have N entries, including RTTI entries,
2170 vbase and vcall offsets, etc. Set its type and call the back end
2174 layout_vtable_decl (tree binfo, int n)
2179 atype = build_array_of_n_type (vtable_entry_type, n);
2180 layout_type (atype);
2182 /* We may have to grow the vtable. */
2183 vtable = get_vtbl_decl_for_binfo (binfo);
2184 if (!same_type_p (TREE_TYPE (vtable), atype))
2186 TREE_TYPE (vtable) = atype;
2187 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2188 layout_decl (vtable, 0);
2192 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2193 have the same signature. */
2196 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2198 /* One destructor overrides another if they are the same kind of
2200 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2201 && special_function_p (base_fndecl) == special_function_p (fndecl))
2203 /* But a non-destructor never overrides a destructor, nor vice
2204 versa, nor do different kinds of destructors override
2205 one-another. For example, a complete object destructor does not
2206 override a deleting destructor. */
2207 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2210 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2211 || (DECL_CONV_FN_P (fndecl)
2212 && DECL_CONV_FN_P (base_fndecl)
2213 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2214 DECL_CONV_FN_TYPE (base_fndecl))))
2216 tree fntype = TREE_TYPE (fndecl);
2217 tree base_fntype = TREE_TYPE (base_fndecl);
2218 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2219 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2220 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2221 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2227 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2231 base_derived_from (tree derived, tree base)
2235 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2237 if (probe == derived)
2239 else if (BINFO_VIRTUAL_P (probe))
2240 /* If we meet a virtual base, we can't follow the inheritance
2241 any more. See if the complete type of DERIVED contains
2242 such a virtual base. */
2243 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2249 struct find_final_overrider_data {
2250 /* The function for which we are trying to find a final overrider. */
2252 /* The base class in which the function was declared. */
2253 tree declaring_base;
2254 /* The candidate overriders. */
2256 /* Path to most derived. */
2260 /* Add the overrider along the current path to FFOD->CANDIDATES.
2261 Returns true if an overrider was found; false otherwise. */
2264 dfs_find_final_overrider_1 (tree binfo,
2265 find_final_overrider_data *ffod,
2270 /* If BINFO is not the most derived type, try a more derived class.
2271 A definition there will overrider a definition here. */
2275 if (dfs_find_final_overrider_1
2276 (ffod->path[depth], ffod, depth))
2280 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2283 tree *candidate = &ffod->candidates;
2285 /* Remove any candidates overridden by this new function. */
2288 /* If *CANDIDATE overrides METHOD, then METHOD
2289 cannot override anything else on the list. */
2290 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2292 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2293 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2294 *candidate = TREE_CHAIN (*candidate);
2296 candidate = &TREE_CHAIN (*candidate);
2299 /* Add the new function. */
2300 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2307 /* Called from find_final_overrider via dfs_walk. */
2310 dfs_find_final_overrider_pre (tree binfo, void *data)
2312 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2314 if (binfo == ffod->declaring_base)
2315 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2316 ffod->path.safe_push (binfo);
2322 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2324 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2330 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2331 FN and whose TREE_VALUE is the binfo for the base where the
2332 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2333 DERIVED) is the base object in which FN is declared. */
2336 find_final_overrider (tree derived, tree binfo, tree fn)
2338 find_final_overrider_data ffod;
2340 /* Getting this right is a little tricky. This is valid:
2342 struct S { virtual void f (); };
2343 struct T { virtual void f (); };
2344 struct U : public S, public T { };
2346 even though calling `f' in `U' is ambiguous. But,
2348 struct R { virtual void f(); };
2349 struct S : virtual public R { virtual void f (); };
2350 struct T : virtual public R { virtual void f (); };
2351 struct U : public S, public T { };
2353 is not -- there's no way to decide whether to put `S::f' or
2354 `T::f' in the vtable for `R'.
2356 The solution is to look at all paths to BINFO. If we find
2357 different overriders along any two, then there is a problem. */
2358 if (DECL_THUNK_P (fn))
2359 fn = THUNK_TARGET (fn);
2361 /* Determine the depth of the hierarchy. */
2363 ffod.declaring_base = binfo;
2364 ffod.candidates = NULL_TREE;
2365 ffod.path.create (30);
2367 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2368 dfs_find_final_overrider_post, &ffod);
2370 ffod.path.release ();
2372 /* If there was no winner, issue an error message. */
2373 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2374 return error_mark_node;
2376 return ffod.candidates;
2379 /* Return the index of the vcall offset for FN when TYPE is used as a
2383 get_vcall_index (tree fn, tree type)
2385 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2389 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2390 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2391 || same_signature_p (fn, p->purpose))
2394 /* There should always be an appropriate index. */
2398 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2399 dominated by T. FN is the old function; VIRTUALS points to the
2400 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2401 of that entry in the list. */
2404 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2412 tree overrider_fn, overrider_target;
2413 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2414 tree over_return, base_return;
2417 /* Find the nearest primary base (possibly binfo itself) which defines
2418 this function; this is the class the caller will convert to when
2419 calling FN through BINFO. */
2420 for (b = binfo; ; b = get_primary_binfo (b))
2423 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2426 /* The nearest definition is from a lost primary. */
2427 if (BINFO_LOST_PRIMARY_P (b))
2432 /* Find the final overrider. */
2433 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2434 if (overrider == error_mark_node)
2436 error ("no unique final overrider for %qD in %qT", target_fn, t);
2439 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2441 /* Check for adjusting covariant return types. */
2442 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2443 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2445 if (POINTER_TYPE_P (over_return)
2446 && TREE_CODE (over_return) == TREE_CODE (base_return)
2447 && CLASS_TYPE_P (TREE_TYPE (over_return))
2448 && CLASS_TYPE_P (TREE_TYPE (base_return))
2449 /* If the overrider is invalid, don't even try. */
2450 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2452 /* If FN is a covariant thunk, we must figure out the adjustment
2453 to the final base FN was converting to. As OVERRIDER_TARGET might
2454 also be converting to the return type of FN, we have to
2455 combine the two conversions here. */
2456 tree fixed_offset, virtual_offset;
2458 over_return = TREE_TYPE (over_return);
2459 base_return = TREE_TYPE (base_return);
2461 if (DECL_THUNK_P (fn))
2463 gcc_assert (DECL_RESULT_THUNK_P (fn));
2464 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2465 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2468 fixed_offset = virtual_offset = NULL_TREE;
2471 /* Find the equivalent binfo within the return type of the
2472 overriding function. We will want the vbase offset from
2474 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2476 else if (!same_type_ignoring_top_level_qualifiers_p
2477 (over_return, base_return))
2479 /* There was no existing virtual thunk (which takes
2480 precedence). So find the binfo of the base function's
2481 return type within the overriding function's return type.
2482 Fortunately we know the covariancy is valid (it
2483 has already been checked), so we can just iterate along
2484 the binfos, which have been chained in inheritance graph
2485 order. Of course it is lame that we have to repeat the
2486 search here anyway -- we should really be caching pieces
2487 of the vtable and avoiding this repeated work. */
2488 tree thunk_binfo = NULL_TREE;
2489 tree base_binfo = TYPE_BINFO (base_return);
2491 /* Find the base binfo within the overriding function's
2492 return type. We will always find a thunk_binfo, except
2493 when the covariancy is invalid (which we will have
2494 already diagnosed). */
2496 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2497 thunk_binfo = TREE_CHAIN (thunk_binfo))
2498 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2499 BINFO_TYPE (base_binfo)))
2501 gcc_assert (thunk_binfo || errorcount);
2503 /* See if virtual inheritance is involved. */
2504 for (virtual_offset = thunk_binfo;
2506 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2507 if (BINFO_VIRTUAL_P (virtual_offset))
2511 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2513 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2517 /* We convert via virtual base. Adjust the fixed
2518 offset to be from there. */
2520 size_diffop (offset,
2521 fold_convert (ssizetype,
2522 BINFO_OFFSET (virtual_offset)));
2525 /* There was an existing fixed offset, this must be
2526 from the base just converted to, and the base the
2527 FN was thunking to. */
2528 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2530 fixed_offset = offset;
2534 if (fixed_offset || virtual_offset)
2535 /* Replace the overriding function with a covariant thunk. We
2536 will emit the overriding function in its own slot as
2538 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2539 fixed_offset, virtual_offset);
2542 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2543 !DECL_THUNK_P (fn));
2545 /* If we need a covariant thunk, then we may need to adjust first_defn.
2546 The ABI specifies that the thunks emitted with a function are
2547 determined by which bases the function overrides, so we need to be
2548 sure that we're using a thunk for some overridden base; even if we
2549 know that the necessary this adjustment is zero, there may not be an
2550 appropriate zero-this-adjustment thunk for us to use since thunks for
2551 overriding virtual bases always use the vcall offset.
2553 Furthermore, just choosing any base that overrides this function isn't
2554 quite right, as this slot won't be used for calls through a type that
2555 puts a covariant thunk here. Calling the function through such a type
2556 will use a different slot, and that slot is the one that determines
2557 the thunk emitted for that base.
2559 So, keep looking until we find the base that we're really overriding
2560 in this slot: the nearest primary base that doesn't use a covariant
2561 thunk in this slot. */
2562 if (overrider_target != overrider_fn)
2564 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2565 /* We already know that the overrider needs a covariant thunk. */
2566 b = get_primary_binfo (b);
2567 for (; ; b = get_primary_binfo (b))
2569 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2570 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2571 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2573 if (BINFO_LOST_PRIMARY_P (b))
2579 /* Assume that we will produce a thunk that convert all the way to
2580 the final overrider, and not to an intermediate virtual base. */
2581 virtual_base = NULL_TREE;
2583 /* See if we can convert to an intermediate virtual base first, and then
2584 use the vcall offset located there to finish the conversion. */
2585 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2587 /* If we find the final overrider, then we can stop
2589 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2590 BINFO_TYPE (TREE_VALUE (overrider))))
2593 /* If we find a virtual base, and we haven't yet found the
2594 overrider, then there is a virtual base between the
2595 declaring base (first_defn) and the final overrider. */
2596 if (BINFO_VIRTUAL_P (b))
2603 /* Compute the constant adjustment to the `this' pointer. The
2604 `this' pointer, when this function is called, will point at BINFO
2605 (or one of its primary bases, which are at the same offset). */
2607 /* The `this' pointer needs to be adjusted from the declaration to
2608 the nearest virtual base. */
2609 delta = size_diffop_loc (input_location,
2610 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2611 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2613 /* If the nearest definition is in a lost primary, we don't need an
2614 entry in our vtable. Except possibly in a constructor vtable,
2615 if we happen to get our primary back. In that case, the offset
2616 will be zero, as it will be a primary base. */
2617 delta = size_zero_node;
2619 /* The `this' pointer needs to be adjusted from pointing to
2620 BINFO to pointing at the base where the final overrider
2622 delta = size_diffop_loc (input_location,
2623 fold_convert (ssizetype,
2624 BINFO_OFFSET (TREE_VALUE (overrider))),
2625 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2627 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2630 BV_VCALL_INDEX (*virtuals)
2631 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2633 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2635 BV_LOST_PRIMARY (*virtuals) = lost;
2638 /* Called from modify_all_vtables via dfs_walk. */
2641 dfs_modify_vtables (tree binfo, void* data)
2643 tree t = (tree) data;
2648 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2649 /* A base without a vtable needs no modification, and its bases
2650 are uninteresting. */
2651 return dfs_skip_bases;
2653 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2654 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2655 /* Don't do the primary vtable, if it's new. */
2658 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2659 /* There's no need to modify the vtable for a non-virtual primary
2660 base; we're not going to use that vtable anyhow. We do still
2661 need to do this for virtual primary bases, as they could become
2662 non-primary in a construction vtable. */
2665 make_new_vtable (t, binfo);
2667 /* Now, go through each of the virtual functions in the virtual
2668 function table for BINFO. Find the final overrider, and update
2669 the BINFO_VIRTUALS list appropriately. */
2670 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2671 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2673 ix++, virtuals = TREE_CHAIN (virtuals),
2674 old_virtuals = TREE_CHAIN (old_virtuals))
2675 update_vtable_entry_for_fn (t,
2677 BV_FN (old_virtuals),
2683 /* Update all of the primary and secondary vtables for T. Create new
2684 vtables as required, and initialize their RTTI information. Each
2685 of the functions in VIRTUALS is declared in T and may override a
2686 virtual function from a base class; find and modify the appropriate
2687 entries to point to the overriding functions. Returns a list, in
2688 declaration order, of the virtual functions that are declared in T,
2689 but do not appear in the primary base class vtable, and which
2690 should therefore be appended to the end of the vtable for T. */
2693 modify_all_vtables (tree t, tree virtuals)
2695 tree binfo = TYPE_BINFO (t);
2698 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2699 if (TYPE_CONTAINS_VPTR_P (t))
2700 get_vtable_decl (t, false);
2702 /* Update all of the vtables. */
2703 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2705 /* Add virtual functions not already in our primary vtable. These
2706 will be both those introduced by this class, and those overridden
2707 from secondary bases. It does not include virtuals merely
2708 inherited from secondary bases. */
2709 for (fnsp = &virtuals; *fnsp; )
2711 tree fn = TREE_VALUE (*fnsp);
2713 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2714 || DECL_VINDEX (fn) == error_mark_node)
2716 /* We don't need to adjust the `this' pointer when
2717 calling this function. */
2718 BV_DELTA (*fnsp) = integer_zero_node;
2719 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2721 /* This is a function not already in our vtable. Keep it. */
2722 fnsp = &TREE_CHAIN (*fnsp);
2725 /* We've already got an entry for this function. Skip it. */
2726 *fnsp = TREE_CHAIN (*fnsp);
2732 /* Get the base virtual function declarations in T that have the
2736 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2738 bool found_decls = false;
2740 /* Find virtual functions in T with the indicated NAME. */
2741 for (ovl_iterator iter (get_class_binding (t, name)); iter; ++iter)
2743 tree method = *iter;
2745 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2747 base_fndecls->safe_push (method);
2755 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2756 for (int i = 0; i < n_baseclasses; i++)
2758 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2759 get_basefndecls (name, basetype, base_fndecls);
2763 /* If this declaration supersedes the declaration of
2764 a method declared virtual in the base class, then
2765 mark this field as being virtual as well. */
2768 check_for_override (tree decl, tree ctype)
2770 bool overrides_found = false;
2771 if (TREE_CODE (decl) == TEMPLATE_DECL)
2772 /* In [temp.mem] we have:
2774 A specialization of a member function template does not
2775 override a virtual function from a base class. */
2777 if ((DECL_DESTRUCTOR_P (decl)
2778 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2779 || DECL_CONV_FN_P (decl))
2780 && look_for_overrides (ctype, decl)
2781 && !DECL_STATIC_FUNCTION_P (decl))
2782 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2783 the error_mark_node so that we know it is an overriding
2786 DECL_VINDEX (decl) = decl;
2787 overrides_found = true;
2788 if (warn_override && !DECL_OVERRIDE_P (decl)
2789 && !DECL_DESTRUCTOR_P (decl))
2790 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2791 "%qD can be marked override", decl);
2794 if (DECL_VIRTUAL_P (decl))
2796 if (!DECL_VINDEX (decl))
2797 DECL_VINDEX (decl) = error_mark_node;
2798 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2799 if (DECL_DESTRUCTOR_P (decl))
2800 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2802 else if (DECL_FINAL_P (decl))
2803 error ("%q+#D marked %<final%>, but is not virtual", decl);
2804 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2805 error ("%q+#D marked %<override%>, but does not override", decl);
2808 /* Warn about hidden virtual functions that are not overridden in t.
2809 We know that constructors and destructors don't apply. */
2812 warn_hidden (tree t)
2814 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2815 for (unsigned ix = member_vec->length (); ix--;)
2817 tree fns = (*member_vec)[ix];
2822 tree name = OVL_NAME (fns);
2823 auto_vec<tree, 20> base_fndecls;
2828 /* Iterate through all of the base classes looking for possibly
2829 hidden functions. */
2830 for (binfo = TYPE_BINFO (t), j = 0;
2831 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2833 tree basetype = BINFO_TYPE (base_binfo);
2834 get_basefndecls (name, basetype, &base_fndecls);
2837 /* If there are no functions to hide, continue. */
2838 if (base_fndecls.is_empty ())
2841 /* Remove any overridden functions. */
2842 for (ovl_iterator iter (fns); iter; ++iter)
2844 tree fndecl = *iter;
2845 if (TREE_CODE (fndecl) == FUNCTION_DECL
2846 && DECL_VINDEX (fndecl))
2848 /* If the method from the base class has the same
2849 signature as the method from the derived class, it
2850 has been overridden. */
2851 for (size_t k = 0; k < base_fndecls.length (); k++)
2853 && same_signature_p (fndecl, base_fndecls[k]))
2854 base_fndecls[k] = NULL_TREE;
2858 /* Now give a warning for all base functions without overriders,
2859 as they are hidden. */
2861 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
2864 /* Here we know it is a hider, and no overrider exists. */
2865 warning_at (location_of (base_fndecl),
2866 OPT_Woverloaded_virtual,
2867 "%qD was hidden", base_fndecl);
2868 warning_at (location_of (fns),
2869 OPT_Woverloaded_virtual, " by %qD", fns);
2874 /* Recursive helper for finish_struct_anon. */
2877 finish_struct_anon_r (tree field, bool complain)
2879 for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
2881 /* We're generally only interested in entities the user
2882 declared, but we also find nested classes by noticing
2883 the TYPE_DECL that we create implicitly. You're
2884 allowed to put one anonymous union inside another,
2885 though, so we explicitly tolerate that. We use
2886 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2887 we also allow unnamed types used for defining fields. */
2888 if (DECL_ARTIFICIAL (elt)
2889 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2890 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2894 && (TREE_CODE (elt) != FIELD_DECL
2895 || (TREE_PRIVATE (elt) || TREE_PROTECTED (elt))))
2897 /* We already complained about static data members in
2898 finish_static_data_member_decl. */
2900 && permerror (DECL_SOURCE_LOCATION (elt),
2901 TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
2902 ? "%q#D invalid; an anonymous union may "
2903 "only have public non-static data members"
2904 : "%q#D invalid; an anonymous struct may "
2905 "only have public non-static data members", elt))
2908 if (flag_permissive && !hint)
2911 inform (DECL_SOURCE_LOCATION (elt),
2912 "this flexibility is deprecated and will be removed");
2917 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2918 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2920 /* Recurse into the anonymous aggregates to correctly handle
2921 access control (c++/24926):
2932 if (DECL_NAME (elt) == NULL_TREE
2933 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2934 finish_struct_anon_r (elt, /*complain=*/false);
2938 /* Check for things that are invalid. There are probably plenty of other
2939 things we should check for also. */
2942 finish_struct_anon (tree t)
2944 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2946 if (TREE_STATIC (field))
2948 if (TREE_CODE (field) != FIELD_DECL)
2951 if (DECL_NAME (field) == NULL_TREE
2952 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2953 finish_struct_anon_r (field, /*complain=*/true);
2957 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2958 will be used later during class template instantiation.
2959 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2960 a non-static member data (FIELD_DECL), a member function
2961 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2962 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2963 When FRIEND_P is nonzero, T is either a friend class
2964 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2965 (FUNCTION_DECL, TEMPLATE_DECL). */
2968 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2970 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2971 if (CLASSTYPE_TEMPLATE_INFO (type))
2972 CLASSTYPE_DECL_LIST (type)
2973 = tree_cons (friend_p ? NULL_TREE : type,
2974 t, CLASSTYPE_DECL_LIST (type));
2977 /* This function is called from declare_virt_assop_and_dtor via
2980 DATA is a type that direcly or indirectly inherits the base
2981 represented by BINFO. If BINFO contains a virtual assignment [copy
2982 assignment or move assigment] operator or a virtual constructor,
2983 declare that function in DATA if it hasn't been already declared. */
2986 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2988 tree bv, fn, t = (tree)data;
2989 tree opname = assign_op_identifier;
2991 gcc_assert (t && CLASS_TYPE_P (t));
2992 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2994 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2995 /* A base without a vtable needs no modification, and its bases
2996 are uninteresting. */
2997 return dfs_skip_bases;
2999 if (BINFO_PRIMARY_P (binfo))
3000 /* If this is a primary base, then we have already looked at the
3001 virtual functions of its vtable. */
3004 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3008 if (DECL_NAME (fn) == opname)
3010 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3011 lazily_declare_fn (sfk_copy_assignment, t);
3012 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3013 lazily_declare_fn (sfk_move_assignment, t);
3015 else if (DECL_DESTRUCTOR_P (fn)
3016 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3017 lazily_declare_fn (sfk_destructor, t);
3023 /* If the class type T has a direct or indirect base that contains a
3024 virtual assignment operator or a virtual destructor, declare that
3025 function in T if it hasn't been already declared. */
3028 declare_virt_assop_and_dtor (tree t)
3030 if (!(TYPE_POLYMORPHIC_P (t)
3031 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3032 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3033 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3036 dfs_walk_all (TYPE_BINFO (t),
3037 dfs_declare_virt_assop_and_dtor,
3041 /* Declare the inheriting constructor for class T inherited from base
3042 constructor CTOR with the parameter array PARMS of size NPARMS. */
3045 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3047 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3049 /* We don't declare an inheriting ctor that would be a default,
3050 copy or move ctor for derived or base. */
3054 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3056 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3057 if (parm == t || parm == DECL_CONTEXT (ctor))
3061 tree parmlist = void_list_node;
3062 for (int i = nparms - 1; i >= 0; i--)
3063 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3064 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3065 t, false, ctor, parmlist);
3067 if (add_method (t, fn, false))
3069 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3070 TYPE_FIELDS (t) = fn;
3074 /* Declare all the inheriting constructors for class T inherited from base
3075 constructor CTOR. */
3078 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3080 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3082 if (flag_new_inheriting_ctors)
3084 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3085 t, /*const*/false, ctor, parms);
3086 add_method (t, ctor, using_decl != NULL_TREE);
3087 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3091 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3093 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3095 if (TREE_PURPOSE (parms))
3096 one_inheriting_sig (t, ctor, new_parms, i);
3097 new_parms[i++] = TREE_VALUE (parms);
3099 one_inheriting_sig (t, ctor, new_parms, i);
3100 if (parms == NULL_TREE)
3102 if (warning (OPT_Winherited_variadic_ctor,
3103 "the ellipsis in %qD is not inherited", ctor))
3104 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3108 /* Create default constructors, assignment operators, and so forth for
3109 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3110 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3111 the class cannot have a default constructor, copy constructor
3112 taking a const reference argument, or an assignment operator taking
3113 a const reference, respectively. */
3116 add_implicitly_declared_members (tree t, tree* access_decls,
3117 int cant_have_const_cctor,
3118 int cant_have_const_assignment)
3121 if (!CLASSTYPE_DESTRUCTOR (t))
3122 /* In general, we create destructors lazily. */
3123 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3125 bool move_ok = false;
3126 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3127 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3128 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3133 If there is no user-declared constructor for a class, a default
3134 constructor is implicitly declared. */
3135 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3137 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3138 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3139 if (cxx_dialect >= cxx11)
3140 TYPE_HAS_CONSTEXPR_CTOR (t)
3141 /* Don't force the declaration to get a hard answer; if the
3142 definition would have made the class non-literal, it will still be
3143 non-literal because of the base or member in question, and that
3144 gives a better diagnostic. */
3145 = type_maybe_constexpr_default_constructor (t);
3150 If a class definition does not explicitly declare a copy
3151 constructor, one is declared implicitly. */
3152 if (! TYPE_HAS_COPY_CTOR (t))
3154 TYPE_HAS_COPY_CTOR (t) = 1;
3155 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3156 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3158 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3161 /* If there is no assignment operator, one will be created if and
3162 when it is needed. For now, just record whether or not the type
3163 of the parameter to the assignment operator will be a const or
3164 non-const reference. */
3165 if (!TYPE_HAS_COPY_ASSIGN (t))
3167 TYPE_HAS_COPY_ASSIGN (t) = 1;
3168 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3169 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3170 if (move_ok && !LAMBDA_TYPE_P (t))
3171 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3174 /* We can't be lazy about declaring functions that might override
3175 a virtual function from a base class. */
3176 declare_virt_assop_and_dtor (t);
3178 while (*access_decls)
3180 tree using_decl = TREE_VALUE (*access_decls);
3181 tree decl = USING_DECL_DECLS (using_decl);
3182 if (DECL_NAME (using_decl) == ctor_identifier)
3184 /* declare, then remove the decl */
3185 tree ctor_list = decl;
3186 location_t loc = input_location;
3187 input_location = DECL_SOURCE_LOCATION (using_decl);
3188 for (ovl_iterator iter (ctor_list); iter; ++iter)
3189 one_inherited_ctor (*iter, t, using_decl);
3190 *access_decls = TREE_CHAIN (*access_decls);
3191 input_location = loc;
3194 access_decls = &TREE_CHAIN (*access_decls);
3198 /* FIELD is a bit-field. We are finishing the processing for its
3199 enclosing type. Issue any appropriate messages and set appropriate
3200 flags. Returns false if an error has been diagnosed. */
3203 check_bitfield_decl (tree field)
3205 tree type = TREE_TYPE (field);
3208 /* Extract the declared width of the bitfield, which has been
3209 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3210 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3211 gcc_assert (w != NULL_TREE);
3212 /* Remove the bit-field width indicator so that the rest of the
3213 compiler does not treat that value as a qualifier. */
3214 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3216 /* Detect invalid bit-field type. */
3217 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3219 error ("bit-field %q+#D with non-integral type", field);
3220 w = error_mark_node;
3224 location_t loc = input_location;
3225 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3228 /* detect invalid field size. */
3229 input_location = DECL_SOURCE_LOCATION (field);
3230 w = cxx_constant_value (w);
3231 input_location = loc;
3233 if (TREE_CODE (w) != INTEGER_CST)
3235 error ("bit-field %q+D width not an integer constant", field);
3236 w = error_mark_node;
3238 else if (tree_int_cst_sgn (w) < 0)
3240 error ("negative width in bit-field %q+D", field);
3241 w = error_mark_node;
3243 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3245 error ("zero width for bit-field %q+D", field);
3246 w = error_mark_node;
3248 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3249 && TREE_CODE (type) != BOOLEAN_TYPE
3250 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3251 || ((TREE_CODE (type) == ENUMERAL_TYPE
3252 || TREE_CODE (type) == BOOLEAN_TYPE)
3253 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3254 warning_at (DECL_SOURCE_LOCATION (field), 0,
3255 "width of %qD exceeds its type", field);
3256 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3258 int prec = TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3259 if (compare_tree_int (w, prec) < 0)
3260 warning_at (DECL_SOURCE_LOCATION (field), 0,
3261 "%qD is too small to hold all values of %q#T",
3266 if (w != error_mark_node)
3268 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3269 DECL_BIT_FIELD (field) = 1;
3274 /* Non-bit-fields are aligned for their type. */
3275 DECL_BIT_FIELD (field) = 0;
3276 CLEAR_DECL_C_BIT_FIELD (field);
3281 /* FIELD is a non bit-field. We are finishing the processing for its
3282 enclosing type T. Issue any appropriate messages and set appropriate
3286 check_field_decl (tree field,
3288 int* cant_have_const_ctor,
3289 int* no_const_asn_ref)
3291 tree type = strip_array_types (TREE_TYPE (field));
3292 bool any_default_members = false;
3294 /* In C++98 an anonymous union cannot contain any fields which would change
3295 the settings of CANT_HAVE_CONST_CTOR and friends. */
3296 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3298 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3299 structs. So, we recurse through their fields here. */
3300 else if (ANON_AGGR_TYPE_P (type))
3302 for (tree fields = TYPE_FIELDS (type); fields;
3303 fields = DECL_CHAIN (fields))
3304 if (TREE_CODE (fields) == FIELD_DECL)
3305 any_default_members |= check_field_decl (fields, t,
3306 cant_have_const_ctor,
3309 /* Check members with class type for constructors, destructors,
3311 else if (CLASS_TYPE_P (type))
3313 /* Never let anything with uninheritable virtuals
3314 make it through without complaint. */
3315 abstract_virtuals_error (field, type);
3317 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3320 int oldcount = errorcount;
3321 if (TYPE_NEEDS_CONSTRUCTING (type))
3322 error ("member %q+#D with constructor not allowed in union",
3324 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3325 error ("member %q+#D with destructor not allowed in union", field);
3326 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3327 error ("member %q+#D with copy assignment operator not allowed in union",
3329 if (!warned && errorcount > oldcount)
3331 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3332 "only available with -std=c++11 or -std=gnu++11");
3338 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3339 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3340 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3341 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3342 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3343 || !TYPE_HAS_COPY_ASSIGN (type));
3344 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3345 || !TYPE_HAS_COPY_CTOR (type));
3346 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3347 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3348 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3349 || TYPE_HAS_COMPLEX_DFLT (type));
3352 if (TYPE_HAS_COPY_CTOR (type)
3353 && !TYPE_HAS_CONST_COPY_CTOR (type))
3354 *cant_have_const_ctor = 1;
3356 if (TYPE_HAS_COPY_ASSIGN (type)
3357 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3358 *no_const_asn_ref = 1;
3361 check_abi_tags (t, field);
3363 if (DECL_INITIAL (field) != NULL_TREE)
3364 /* `build_class_init_list' does not recognize
3366 any_default_members = true;
3368 return any_default_members;
3371 /* Check the data members (both static and non-static), class-scoped
3372 typedefs, etc., appearing in the declaration of T. Issue
3373 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3374 declaration order) of access declarations; each TREE_VALUE in this
3375 list is a USING_DECL.
3377 In addition, set the following flags:
3380 The class is empty, i.e., contains no non-static data members.
3382 CANT_HAVE_CONST_CTOR_P
3383 This class cannot have an implicitly generated copy constructor
3384 taking a const reference.
3386 CANT_HAVE_CONST_ASN_REF
3387 This class cannot have an implicitly generated assignment
3388 operator taking a const reference.
3390 All of these flags should be initialized before calling this
3393 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3394 fields can be added by adding to this chain. */
3397 check_field_decls (tree t, tree *access_decls,
3398 int *cant_have_const_ctor_p,
3399 int *no_const_asn_ref_p)
3404 bool any_default_members;
3406 int field_access = -1;
3408 /* Assume there are no access declarations. */
3409 *access_decls = NULL_TREE;
3410 /* Assume this class has no pointer members. */
3411 has_pointers = false;
3412 /* Assume none of the members of this class have default
3414 any_default_members = false;
3416 for (field = &TYPE_FIELDS (t); *field; field = next)
3419 tree type = TREE_TYPE (x);
3420 int this_field_access;
3422 next = &DECL_CHAIN (x);
3424 if (TREE_CODE (x) == USING_DECL)
3426 /* Save the access declarations for our caller. */
3427 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3431 if (TREE_CODE (x) == TYPE_DECL
3432 || TREE_CODE (x) == TEMPLATE_DECL)
3435 if (TREE_CODE (x) == FUNCTION_DECL)
3436 /* FIXME: We should fold in the checking from check_methods. */
3439 /* If we've gotten this far, it's a data member, possibly static,
3440 or an enumerator. */
3441 if (TREE_CODE (x) != CONST_DECL)
3442 DECL_CONTEXT (x) = t;
3444 /* When this goes into scope, it will be a non-local reference. */
3445 DECL_NONLOCAL (x) = 1;
3447 if (TREE_CODE (t) == UNION_TYPE)
3449 /* [class.union] (C++98)
3451 If a union contains a static data member, or a member of
3452 reference type, the program is ill-formed.
3454 In C++11 [class.union] says:
3455 If a union contains a non-static data member of reference type
3456 the program is ill-formed. */
3457 if (VAR_P (x) && cxx_dialect < cxx11)
3459 error ("in C++98 %q+D may not be static because it is "
3460 "a member of a union", x);
3463 if (TREE_CODE (type) == REFERENCE_TYPE
3464 && TREE_CODE (x) == FIELD_DECL)
3466 error ("non-static data member %q+D in a union may not "
3467 "have reference type %qT", x, type);
3472 /* Perform error checking that did not get done in
3474 if (TREE_CODE (type) == FUNCTION_TYPE)
3476 error ("field %q+D invalidly declared function type", x);
3477 type = build_pointer_type (type);
3478 TREE_TYPE (x) = type;
3480 else if (TREE_CODE (type) == METHOD_TYPE)
3482 error ("field %q+D invalidly declared method type", x);
3483 type = build_pointer_type (type);
3484 TREE_TYPE (x) = type;
3487 if (type == error_mark_node)
3490 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3493 /* Now it can only be a FIELD_DECL. */
3495 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3496 CLASSTYPE_NON_AGGREGATE (t) = 1;
3498 /* If at least one non-static data member is non-literal, the whole
3499 class becomes non-literal. Per Core/1453, volatile non-static
3500 data members and base classes are also not allowed.
3501 Note: if the type is incomplete we will complain later on. */
3502 if (COMPLETE_TYPE_P (type)
3503 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3504 CLASSTYPE_LITERAL_P (t) = false;
3506 /* A standard-layout class is a class that:
3508 has the same access control (Clause 11) for all non-static data members,
3510 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3511 if (field_access == -1)
3512 field_access = this_field_access;
3513 else if (this_field_access != field_access)
3514 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3516 /* If this is of reference type, check if it needs an init. */
3517 if (TREE_CODE (type) == REFERENCE_TYPE)
3519 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3520 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3521 if (DECL_INITIAL (x) == NULL_TREE)
3522 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3523 if (cxx_dialect < cxx11)
3525 /* ARM $12.6.2: [A member initializer list] (or, for an
3526 aggregate, initialization by a brace-enclosed list) is the
3527 only way to initialize nonstatic const and reference
3529 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3530 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3534 type = strip_array_types (type);
3536 if (TYPE_PACKED (t))
3538 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3541 (DECL_SOURCE_LOCATION (x), 0,
3542 "ignoring packed attribute because of unpacked non-POD field %q#D",
3546 else if (DECL_C_BIT_FIELD (x)
3547 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3548 DECL_PACKED (x) = 1;
3551 if (DECL_C_BIT_FIELD (x)
3552 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
3553 /* We don't treat zero-width bitfields as making a class
3558 /* The class is non-empty. */
3559 CLASSTYPE_EMPTY_P (t) = 0;
3560 /* The class is not even nearly empty. */
3561 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3562 /* If one of the data members contains an empty class,
3564 if (CLASS_TYPE_P (type)
3565 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3566 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3569 /* This is used by -Weffc++ (see below). Warn only for pointers
3570 to members which might hold dynamic memory. So do not warn
3571 for pointers to functions or pointers to members. */
3572 if (TYPE_PTR_P (type)
3573 && !TYPE_PTRFN_P (type))
3574 has_pointers = true;
3576 if (CLASS_TYPE_P (type))
3578 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3579 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3580 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3581 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3584 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3585 CLASSTYPE_HAS_MUTABLE (t) = 1;
3587 if (DECL_MUTABLE_P (x))
3589 if (CP_TYPE_CONST_P (type))
3591 error ("member %q+D cannot be declared both %<const%> "
3592 "and %<mutable%>", x);
3595 if (TREE_CODE (type) == REFERENCE_TYPE)
3597 error ("member %q+D cannot be declared as a %<mutable%> "
3603 if (! layout_pod_type_p (type))
3604 /* DR 148 now allows pointers to members (which are POD themselves),
3605 to be allowed in POD structs. */
3606 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3608 if (!std_layout_type_p (type))
3609 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3611 if (! zero_init_p (type))
3612 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3614 /* We set DECL_C_BIT_FIELD in grokbitfield.
3615 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3616 if (DECL_C_BIT_FIELD (x))
3617 check_bitfield_decl (x);
3619 if (check_field_decl (x, t, cant_have_const_ctor_p, no_const_asn_ref_p))
3621 if (any_default_members
3622 && TREE_CODE (t) == UNION_TYPE)
3623 error ("multiple fields in union %qT initialized", t);
3624 any_default_members = true;
3627 /* Now that we've removed bit-field widths from DECL_INITIAL,
3628 anything left in DECL_INITIAL is an NSDMI that makes the class
3629 non-aggregate in C++11. */
3630 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3631 CLASSTYPE_NON_AGGREGATE (t) = true;
3633 /* If any field is const, the structure type is pseudo-const. */
3634 if (CP_TYPE_CONST_P (type))
3636 C_TYPE_FIELDS_READONLY (t) = 1;
3637 if (DECL_INITIAL (x) == NULL_TREE)
3638 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3639 if (cxx_dialect < cxx11)
3641 /* ARM $12.6.2: [A member initializer list] (or, for an
3642 aggregate, initialization by a brace-enclosed list) is the
3643 only way to initialize nonstatic const and reference
3645 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3646 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3649 /* A field that is pseudo-const makes the structure likewise. */
3650 else if (CLASS_TYPE_P (type))
3652 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3653 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3654 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3655 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3658 /* Core issue 80: A nonstatic data member is required to have a
3659 different name from the class iff the class has a
3660 user-declared constructor. */
3661 if (constructor_name_p (DECL_NAME (x), t)
3662 && TYPE_HAS_USER_CONSTRUCTOR (t))
3663 permerror (DECL_SOURCE_LOCATION (x),
3664 "field %q#D with same name as class", x);
3667 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3668 it should also define a copy constructor and an assignment operator to
3669 implement the correct copy semantic (deep vs shallow, etc.). As it is
3670 not feasible to check whether the constructors do allocate dynamic memory
3671 and store it within members, we approximate the warning like this:
3673 -- Warn only if there are members which are pointers
3674 -- Warn only if there is a non-trivial constructor (otherwise,
3675 there cannot be memory allocated).
3676 -- Warn only if there is a non-trivial destructor. We assume that the
3677 user at least implemented the cleanup correctly, and a destructor
3678 is needed to free dynamic memory.
3680 This seems enough for practical purposes. */
3683 && TYPE_HAS_USER_CONSTRUCTOR (t)
3684 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3685 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3687 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3689 if (! TYPE_HAS_COPY_CTOR (t))
3691 warning (OPT_Weffc__,
3692 " but does not override %<%T(const %T&)%>", t, t);
3693 if (!TYPE_HAS_COPY_ASSIGN (t))
3694 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3696 else if (! TYPE_HAS_COPY_ASSIGN (t))
3697 warning (OPT_Weffc__,
3698 " but does not override %<operator=(const %T&)%>", t);
3701 /* Non-static data member initializers make the default constructor
3703 if (any_default_members)
3705 TYPE_NEEDS_CONSTRUCTING (t) = true;
3706 TYPE_HAS_COMPLEX_DFLT (t) = true;
3709 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3711 TYPE_PACKED (t) = 0;
3713 /* Check anonymous struct/anonymous union fields. */
3714 finish_struct_anon (t);
3716 /* We've built up the list of access declarations in reverse order.
3718 *access_decls = nreverse (*access_decls);
3721 /* If TYPE is an empty class type, records its OFFSET in the table of
3725 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3729 if (!is_empty_class (type))
3732 /* Record the location of this empty object in OFFSETS. */
3733 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3735 n = splay_tree_insert (offsets,
3736 (splay_tree_key) offset,
3737 (splay_tree_value) NULL_TREE);
3738 n->value = ((splay_tree_value)
3739 tree_cons (NULL_TREE,
3746 /* Returns nonzero if TYPE is an empty class type and there is
3747 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3750 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3755 if (!is_empty_class (type))
3758 /* Record the location of this empty object in OFFSETS. */
3759 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3763 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3764 if (same_type_p (TREE_VALUE (t), type))
3770 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3771 F for every subobject, passing it the type, offset, and table of
3772 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3775 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3776 than MAX_OFFSET will not be walked.
3778 If F returns a nonzero value, the traversal ceases, and that value
3779 is returned. Otherwise, returns zero. */
3782 walk_subobject_offsets (tree type,
3783 subobject_offset_fn f,
3790 tree type_binfo = NULL_TREE;
3792 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3794 if (max_offset && tree_int_cst_lt (max_offset, offset))
3797 if (type == error_mark_node)
3803 type = BINFO_TYPE (type);
3806 if (CLASS_TYPE_P (type))
3812 /* Avoid recursing into objects that are not interesting. */
3813 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3816 /* Record the location of TYPE. */
3817 r = (*f) (type, offset, offsets);
3821 /* Iterate through the direct base classes of TYPE. */
3823 type_binfo = TYPE_BINFO (type);
3824 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3828 if (BINFO_VIRTUAL_P (binfo))
3832 /* We cannot rely on BINFO_OFFSET being set for the base
3833 class yet, but the offsets for direct non-virtual
3834 bases can be calculated by going back to the TYPE. */
3835 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3836 binfo_offset = size_binop (PLUS_EXPR,
3838 BINFO_OFFSET (orig_binfo));
3840 r = walk_subobject_offsets (binfo,
3850 if (CLASSTYPE_VBASECLASSES (type))
3853 vec<tree, va_gc> *vbases;
3855 /* Iterate through the virtual base classes of TYPE. In G++
3856 3.2, we included virtual bases in the direct base class
3857 loop above, which results in incorrect results; the
3858 correct offsets for virtual bases are only known when
3859 working with the most derived type. */
3861 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3862 vec_safe_iterate (vbases, ix, &binfo); ix++)
3864 r = walk_subobject_offsets (binfo,
3866 size_binop (PLUS_EXPR,
3868 BINFO_OFFSET (binfo)),
3877 /* We still have to walk the primary base, if it is
3878 virtual. (If it is non-virtual, then it was walked
3880 tree vbase = get_primary_binfo (type_binfo);
3882 if (vbase && BINFO_VIRTUAL_P (vbase)
3883 && BINFO_PRIMARY_P (vbase)
3884 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3886 r = (walk_subobject_offsets
3888 offsets, max_offset, /*vbases_p=*/0));
3895 /* Iterate through the fields of TYPE. */
3896 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3897 if (TREE_CODE (field) == FIELD_DECL
3898 && TREE_TYPE (field) != error_mark_node
3899 && !DECL_ARTIFICIAL (field))
3903 field_offset = byte_position (field);
3905 r = walk_subobject_offsets (TREE_TYPE (field),
3907 size_binop (PLUS_EXPR,
3917 else if (TREE_CODE (type) == ARRAY_TYPE)
3919 tree element_type = strip_array_types (type);
3920 tree domain = TYPE_DOMAIN (type);
3923 /* Avoid recursing into objects that are not interesting. */
3924 if (!CLASS_TYPE_P (element_type)
3925 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
3927 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
3930 /* Step through each of the elements in the array. */
3931 for (index = size_zero_node;
3932 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
3933 index = size_binop (PLUS_EXPR, index, size_one_node))
3935 r = walk_subobject_offsets (TREE_TYPE (type),
3943 offset = size_binop (PLUS_EXPR, offset,
3944 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3945 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3946 there's no point in iterating through the remaining
3947 elements of the array. */
3948 if (max_offset && tree_int_cst_lt (max_offset, offset))
3956 /* Record all of the empty subobjects of TYPE (either a type or a
3957 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3958 is being placed at OFFSET; otherwise, it is a base class that is
3959 being placed at OFFSET. */
3962 record_subobject_offsets (tree type,
3965 bool is_data_member)
3968 /* If recording subobjects for a non-static data member or a
3969 non-empty base class , we do not need to record offsets beyond
3970 the size of the biggest empty class. Additional data members
3971 will go at the end of the class. Additional base classes will go
3972 either at offset zero (if empty, in which case they cannot
3973 overlap with offsets past the size of the biggest empty class) or
3974 at the end of the class.
3976 However, if we are placing an empty base class, then we must record
3977 all offsets, as either the empty class is at offset zero (where
3978 other empty classes might later be placed) or at the end of the
3979 class (where other objects might then be placed, so other empty
3980 subobjects might later overlap). */
3982 || !is_empty_class (BINFO_TYPE (type)))
3983 max_offset = sizeof_biggest_empty_class;
3985 max_offset = NULL_TREE;
3986 walk_subobject_offsets (type, record_subobject_offset, offset,
3987 offsets, max_offset, is_data_member);
3990 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3991 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3992 virtual bases of TYPE are examined. */
3995 layout_conflict_p (tree type,
4000 splay_tree_node max_node;
4002 /* Get the node in OFFSETS that indicates the maximum offset where
4003 an empty subobject is located. */
4004 max_node = splay_tree_max (offsets);
4005 /* If there aren't any empty subobjects, then there's no point in
4006 performing this check. */
4010 return walk_subobject_offsets (type, check_subobject_offset, offset,
4011 offsets, (tree) (max_node->key),
4015 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4016 non-static data member of the type indicated by RLI. BINFO is the
4017 binfo corresponding to the base subobject, OFFSETS maps offsets to
4018 types already located at those offsets. This function determines
4019 the position of the DECL. */
4022 layout_nonempty_base_or_field (record_layout_info rli,
4027 tree offset = NULL_TREE;
4033 /* For the purposes of determining layout conflicts, we want to
4034 use the class type of BINFO; TREE_TYPE (DECL) will be the
4035 CLASSTYPE_AS_BASE version, which does not contain entries for
4036 zero-sized bases. */
4037 type = TREE_TYPE (binfo);
4042 type = TREE_TYPE (decl);
4046 /* Try to place the field. It may take more than one try if we have
4047 a hard time placing the field without putting two objects of the
4048 same type at the same address. */
4051 struct record_layout_info_s old_rli = *rli;
4053 /* Place this field. */
4054 place_field (rli, decl);
4055 offset = byte_position (decl);
4057 /* We have to check to see whether or not there is already
4058 something of the same type at the offset we're about to use.
4059 For example, consider:
4062 struct T : public S { int i; };
4063 struct U : public S, public T {};
4065 Here, we put S at offset zero in U. Then, we can't put T at
4066 offset zero -- its S component would be at the same address
4067 as the S we already allocated. So, we have to skip ahead.
4068 Since all data members, including those whose type is an
4069 empty class, have nonzero size, any overlap can happen only
4070 with a direct or indirect base-class -- it can't happen with
4072 /* In a union, overlap is permitted; all members are placed at
4074 if (TREE_CODE (rli->t) == UNION_TYPE)
4076 if (layout_conflict_p (field_p ? type : binfo, offset,
4079 /* Strip off the size allocated to this field. That puts us
4080 at the first place we could have put the field with
4081 proper alignment. */
4084 /* Bump up by the alignment required for the type. */
4086 = size_binop (PLUS_EXPR, rli->bitpos,
4088 ? CLASSTYPE_ALIGN (type)
4089 : TYPE_ALIGN (type)));
4090 normalize_rli (rli);
4092 else if (TREE_CODE (type) == NULLPTR_TYPE
4093 && warn_abi && abi_version_crosses (9))
4095 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4096 the offset wasn't aligned like a pointer when we started to
4097 layout this field, that affects its position. */
4098 tree pos = rli_size_unit_so_far (&old_rli);
4099 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4101 if (abi_version_at_least (9))
4102 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4103 "alignment of %qD increased in -fabi-version=9 "
4106 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4107 "of %qD will increase in -fabi-version=9", decl);
4112 /* There was no conflict. We're done laying out this field. */
4116 /* Now that we know where it will be placed, update its
4118 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4119 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4120 this point because their BINFO_OFFSET is copied from another
4121 hierarchy. Therefore, we may not need to add the entire
4123 propagate_binfo_offsets (binfo,
4124 size_diffop_loc (input_location,
4125 fold_convert (ssizetype, offset),
4126 fold_convert (ssizetype,
4127 BINFO_OFFSET (binfo))));
4130 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4133 empty_base_at_nonzero_offset_p (tree type,
4135 splay_tree /*offsets*/)
4137 return is_empty_class (type) && !integer_zerop (offset);
4140 /* Layout the empty base BINFO. EOC indicates the byte currently just
4141 past the end of the class, and should be correctly aligned for a
4142 class of the type indicated by BINFO; OFFSETS gives the offsets of
4143 the empty bases allocated so far. T is the most derived
4144 type. Return nonzero iff we added it at the end. */
4147 layout_empty_base (record_layout_info rli, tree binfo,
4148 tree eoc, splay_tree offsets)
4151 tree basetype = BINFO_TYPE (binfo);
4154 /* This routine should only be used for empty classes. */
4155 gcc_assert (is_empty_class (basetype));
4156 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4158 if (!integer_zerop (BINFO_OFFSET (binfo)))
4159 propagate_binfo_offsets
4160 (binfo, size_diffop_loc (input_location,
4161 size_zero_node, BINFO_OFFSET (binfo)));
4163 /* This is an empty base class. We first try to put it at offset
4165 if (layout_conflict_p (binfo,
4166 BINFO_OFFSET (binfo),
4170 /* That didn't work. Now, we move forward from the next
4171 available spot in the class. */
4173 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4176 if (!layout_conflict_p (binfo,
4177 BINFO_OFFSET (binfo),
4180 /* We finally found a spot where there's no overlap. */
4183 /* There's overlap here, too. Bump along to the next spot. */
4184 propagate_binfo_offsets (binfo, alignment);
4188 if (CLASSTYPE_USER_ALIGN (basetype))
4190 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4192 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4193 TYPE_USER_ALIGN (rli->t) = 1;
4199 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4200 fields at NEXT_FIELD, and return it. */
4203 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4205 /* Create the FIELD_DECL. */
4206 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4207 tree decl = build_decl (input_location,
4208 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4209 DECL_ARTIFICIAL (decl) = 1;
4210 DECL_IGNORED_P (decl) = 1;
4211 DECL_FIELD_CONTEXT (decl) = t;
4212 if (is_empty_class (basetype))
4213 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4214 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4217 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4218 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4220 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4221 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4222 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4223 DECL_FIELD_IS_BASE (decl) = 1;
4225 /* Add the new FIELD_DECL to the list of fields for T. */
4226 DECL_CHAIN (decl) = *next_field;
4228 next_field = &DECL_CHAIN (decl);
4233 /* Layout the base given by BINFO in the class indicated by RLI.
4234 *BASE_ALIGN is a running maximum of the alignments of
4235 any base class. OFFSETS gives the location of empty base
4236 subobjects. T is the most derived type. Return nonzero if the new
4237 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4238 *NEXT_FIELD, unless BINFO is for an empty base class.
4240 Returns the location at which the next field should be inserted. */
4243 build_base_field (record_layout_info rli, tree binfo,
4244 splay_tree offsets, tree *next_field)
4247 tree basetype = BINFO_TYPE (binfo);
4249 if (!COMPLETE_TYPE_P (basetype))
4250 /* This error is now reported in xref_tag, thus giving better
4251 location information. */
4254 /* Place the base class. */
4255 if (!is_empty_class (basetype))
4259 /* The containing class is non-empty because it has a non-empty
4261 CLASSTYPE_EMPTY_P (t) = 0;
4263 /* Create the FIELD_DECL. */
4264 decl = build_base_field_1 (t, basetype, next_field);
4266 /* Try to place the field. It may take more than one try if we
4267 have a hard time placing the field without putting two
4268 objects of the same type at the same address. */
4269 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4276 /* On some platforms (ARM), even empty classes will not be
4278 eoc = round_up_loc (input_location,
4279 rli_size_unit_so_far (rli),
4280 CLASSTYPE_ALIGN_UNIT (basetype));
4281 atend = layout_empty_base (rli, binfo, eoc, offsets);
4282 /* A nearly-empty class "has no proper base class that is empty,
4283 not morally virtual, and at an offset other than zero." */
4284 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4287 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4288 /* The check above (used in G++ 3.2) is insufficient because
4289 an empty class placed at offset zero might itself have an
4290 empty base at a nonzero offset. */
4291 else if (walk_subobject_offsets (basetype,
4292 empty_base_at_nonzero_offset_p,
4295 /*max_offset=*/NULL_TREE,
4297 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4300 /* We used to not create a FIELD_DECL for empty base classes because of
4301 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4302 be a problem anymore. We need them to handle initialization of C++17
4304 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4306 tree decl = build_base_field_1 (t, basetype, next_field);
4307 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4308 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4309 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4312 /* An empty virtual base causes a class to be non-empty
4313 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4314 here because that was already done when the virtual table
4315 pointer was created. */
4318 /* Record the offsets of BINFO and its base subobjects. */
4319 record_subobject_offsets (binfo,
4320 BINFO_OFFSET (binfo),
4322 /*is_data_member=*/false);
4327 /* Layout all of the non-virtual base classes. Record empty
4328 subobjects in OFFSETS. T is the most derived type. Return nonzero
4329 if the type cannot be nearly empty. The fields created
4330 corresponding to the base classes will be inserted at
4334 build_base_fields (record_layout_info rli,
4335 splay_tree offsets, tree *next_field)
4337 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4340 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4343 /* The primary base class is always allocated first. */
4344 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4345 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4346 offsets, next_field);
4348 /* Now allocate the rest of the bases. */
4349 for (i = 0; i < n_baseclasses; ++i)
4353 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4355 /* The primary base was already allocated above, so we don't
4356 need to allocate it again here. */
4357 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4360 /* Virtual bases are added at the end (a primary virtual base
4361 will have already been added). */
4362 if (BINFO_VIRTUAL_P (base_binfo))
4365 next_field = build_base_field (rli, base_binfo,
4366 offsets, next_field);
4370 /* Go through the TYPE_FIELDS of T issuing any appropriate
4371 diagnostics, figuring out which methods override which other
4372 methods, and so forth. */
4375 check_methods (tree t)
4377 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4378 if (DECL_DECLARES_FUNCTION_P (x))
4380 check_for_override (x, t);
4382 if (DECL_PURE_VIRTUAL_P (x)
4383 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4384 error ("initializer specified for non-virtual method %q+D", x);
4385 /* The name of the field is the original field name
4386 Save this in auxiliary field for later overloading. */
4387 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4389 TYPE_POLYMORPHIC_P (t) = 1;
4390 if (DECL_PURE_VIRTUAL_P (x))
4391 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4394 /* All user-provided destructors are non-trivial.
4395 Constructors and assignment ops are handled in
4396 grok_special_member_properties. */
4397 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4398 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4399 if (!DECL_VIRTUAL_P (x)
4400 && lookup_attribute ("transaction_safe_dynamic",
4401 DECL_ATTRIBUTES (x)))
4402 error_at (DECL_SOURCE_LOCATION (x),
4403 "%<transaction_safe_dynamic%> may only be specified for "
4404 "a virtual function");
4408 /* FN is a constructor or destructor. Clone the declaration to create
4409 a specialized in-charge or not-in-charge version, as indicated by
4413 build_clone (tree fn, tree name)
4418 /* Copy the function. */
4419 clone = copy_decl (fn);
4420 /* Reset the function name. */
4421 DECL_NAME (clone) = name;
4422 /* Remember where this function came from. */
4423 DECL_ABSTRACT_ORIGIN (clone) = fn;
4424 /* Make it easy to find the CLONE given the FN. */
4425 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4426 DECL_CHAIN (fn) = clone;
4428 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4429 if (TREE_CODE (clone) == TEMPLATE_DECL)
4431 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4432 DECL_TEMPLATE_RESULT (clone) = result;
4433 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4434 DECL_TI_TEMPLATE (result) = clone;
4435 TREE_TYPE (clone) = TREE_TYPE (result);
4440 // Clone constraints.
4442 if (tree ci = get_constraints (fn))
4443 set_constraints (clone, copy_node (ci));
4447 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4448 DECL_CLONED_FUNCTION (clone) = fn;
4449 /* There's no pending inline data for this function. */
4450 DECL_PENDING_INLINE_INFO (clone) = NULL;
4451 DECL_PENDING_INLINE_P (clone) = 0;
4453 /* The base-class destructor is not virtual. */
4454 if (name == base_dtor_identifier)
4456 DECL_VIRTUAL_P (clone) = 0;
4457 if (TREE_CODE (clone) != TEMPLATE_DECL)
4458 DECL_VINDEX (clone) = NULL_TREE;
4461 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4462 if (ctor_omit_inherited_parms_p)
4463 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4465 /* If there was an in-charge parameter, drop it from the function
4467 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4473 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4474 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4475 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4476 /* Skip the `this' parameter. */
4477 parmtypes = TREE_CHAIN (parmtypes);
4478 /* Skip the in-charge parameter. */
4479 parmtypes = TREE_CHAIN (parmtypes);
4480 /* And the VTT parm, in a complete [cd]tor. */
4481 if (DECL_HAS_VTT_PARM_P (fn)
4482 && ! DECL_NEEDS_VTT_PARM_P (clone))
4483 parmtypes = TREE_CHAIN (parmtypes);
4484 if (ctor_omit_inherited_parms_p)
4486 /* If we're omitting inherited parms, that just leaves the VTT. */
4487 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4488 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4491 = build_method_type_directly (basetype,
4492 TREE_TYPE (TREE_TYPE (clone)),
4495 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4498 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4499 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4502 /* Copy the function parameters. */
4503 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4504 /* Remove the in-charge parameter. */
4505 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4507 DECL_CHAIN (DECL_ARGUMENTS (clone))
4508 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4509 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4511 /* And the VTT parm, in a complete [cd]tor. */
4512 if (DECL_HAS_VTT_PARM_P (fn))
4514 if (DECL_NEEDS_VTT_PARM_P (clone))
4515 DECL_HAS_VTT_PARM_P (clone) = 1;
4518 DECL_CHAIN (DECL_ARGUMENTS (clone))
4519 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4520 DECL_HAS_VTT_PARM_P (clone) = 0;
4524 /* A base constructor inheriting from a virtual base doesn't get the
4526 if (ctor_omit_inherited_parms_p)
4527 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4529 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4531 DECL_CONTEXT (parms) = clone;
4532 cxx_dup_lang_specific_decl (parms);
4535 /* Create the RTL for this function. */
4536 SET_DECL_RTL (clone, NULL);
4537 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4542 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4543 not invoke this function directly.
4545 For a non-thunk function, returns the address of the slot for storing
4546 the function it is a clone of. Otherwise returns NULL_TREE.
4548 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4549 cloned_function is unset. This is to support the separate
4550 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4551 on a template makes sense, but not the former. */
4554 decl_cloned_function_p (const_tree decl, bool just_testing)
4558 decl = STRIP_TEMPLATE (decl);
4560 if (TREE_CODE (decl) != FUNCTION_DECL
4561 || !DECL_LANG_SPECIFIC (decl)
4562 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4564 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4566 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4572 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4573 if (just_testing && *ptr == NULL_TREE)
4579 /* Produce declarations for all appropriate clones of FN. If
4580 UPDATE_METHODS is true, the clones are added to the
4581 CLASSTYPE_MEMBER_VEC. */
4584 clone_function_decl (tree fn, bool update_methods)
4588 /* Avoid inappropriate cloning. */
4590 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4593 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4595 /* For each constructor, we need two variants: an in-charge version
4596 and a not-in-charge version. */
4597 clone = build_clone (fn, complete_ctor_identifier);
4599 add_method (DECL_CONTEXT (clone), clone, false);
4600 clone = build_clone (fn, base_ctor_identifier);
4602 add_method (DECL_CONTEXT (clone), clone, false);
4606 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4608 /* For each destructor, we need three variants: an in-charge
4609 version, a not-in-charge version, and an in-charge deleting
4610 version. We clone the deleting version first because that
4611 means it will go second on the TYPE_FIELDS list -- and that
4612 corresponds to the correct layout order in the virtual
4615 For a non-virtual destructor, we do not build a deleting
4617 if (DECL_VIRTUAL_P (fn))
4619 clone = build_clone (fn, deleting_dtor_identifier);
4621 add_method (DECL_CONTEXT (clone), clone, false);
4623 clone = build_clone (fn, complete_dtor_identifier);
4625 add_method (DECL_CONTEXT (clone), clone, false);
4626 clone = build_clone (fn, base_dtor_identifier);
4628 add_method (DECL_CONTEXT (clone), clone, false);
4631 /* Note that this is an abstract function that is never emitted. */
4632 DECL_ABSTRACT_P (fn) = true;
4635 /* DECL is an in charge constructor, which is being defined. This will
4636 have had an in class declaration, from whence clones were
4637 declared. An out-of-class definition can specify additional default
4638 arguments. As it is the clones that are involved in overload
4639 resolution, we must propagate the information from the DECL to its
4643 adjust_clone_args (tree decl)
4647 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4648 clone = DECL_CHAIN (clone))
4650 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4651 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4652 tree decl_parms, clone_parms;
4654 clone_parms = orig_clone_parms;
4656 /* Skip the 'this' parameter. */
4657 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4658 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4660 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4661 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4662 if (DECL_HAS_VTT_PARM_P (decl))
4663 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4665 clone_parms = orig_clone_parms;
4666 if (DECL_HAS_VTT_PARM_P (clone))
4667 clone_parms = TREE_CHAIN (clone_parms);
4669 for (decl_parms = orig_decl_parms; decl_parms;
4670 decl_parms = TREE_CHAIN (decl_parms),
4671 clone_parms = TREE_CHAIN (clone_parms))
4673 if (clone_parms == void_list_node)
4675 gcc_assert (decl_parms == clone_parms
4676 || ctor_omit_inherited_parms (clone));
4680 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4681 TREE_TYPE (clone_parms)));
4683 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4685 /* A default parameter has been added. Adjust the
4686 clone's parameters. */
4687 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4688 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4689 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4692 clone_parms = orig_decl_parms;
4694 if (DECL_HAS_VTT_PARM_P (clone))
4696 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4697 TREE_VALUE (orig_clone_parms),
4699 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4701 type = build_method_type_directly (basetype,
4702 TREE_TYPE (TREE_TYPE (clone)),
4705 type = build_exception_variant (type, exceptions);
4707 type = cp_build_type_attribute_variant (type, attrs);
4708 TREE_TYPE (clone) = type;
4710 clone_parms = NULL_TREE;
4714 gcc_assert (!clone_parms || clone_parms == void_list_node);
4718 /* For each of the constructors and destructors in T, create an
4719 in-charge and not-in-charge variant. */
4722 clone_constructors_and_destructors (tree t)
4724 /* While constructors can be via a using declaration, at this point
4725 we no longer need to know that. */
4726 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4727 clone_function_decl (*iter, /*update_methods=*/true);
4729 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4730 clone_function_decl (dtor, /*update_methods=*/true);
4733 /* Deduce noexcept for a destructor DTOR. */
4736 deduce_noexcept_on_destructor (tree dtor)
4738 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4739 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
4740 noexcept_deferred_spec);
4743 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4744 of TYPE for virtual functions which FNDECL overrides. Return a
4745 mask of the tm attributes found therein. */
4748 look_for_tm_attr_overrides (tree type, tree fndecl)
4750 tree binfo = TYPE_BINFO (type);
4754 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4756 tree o, basetype = BINFO_TYPE (base_binfo);
4758 if (!TYPE_POLYMORPHIC_P (basetype))
4761 o = look_for_overrides_here (basetype, fndecl);
4764 if (lookup_attribute ("transaction_safe_dynamic",
4765 DECL_ATTRIBUTES (o)))
4766 /* transaction_safe_dynamic is not inherited. */;
4768 found |= tm_attr_to_mask (find_tm_attribute
4769 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4772 found |= look_for_tm_attr_overrides (basetype, fndecl);
4778 /* Subroutine of set_method_tm_attributes. Handle the checks and
4779 inheritance for one virtual method FNDECL. */
4782 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4787 found = look_for_tm_attr_overrides (type, fndecl);
4789 /* If FNDECL doesn't actually override anything (i.e. T is the
4790 class that first declares FNDECL virtual), then we're done. */
4794 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4795 have = tm_attr_to_mask (tm_attr);
4797 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4798 tm_pure must match exactly, otherwise no weakening of
4799 tm_safe > tm_callable > nothing. */
4800 /* ??? The tm_pure attribute didn't make the transition to the
4801 multivendor language spec. */
4802 if (have == TM_ATTR_PURE)
4804 if (found != TM_ATTR_PURE)
4810 /* If the overridden function is tm_pure, then FNDECL must be. */
4811 else if (found == TM_ATTR_PURE && tm_attr)
4813 /* Look for base class combinations that cannot be satisfied. */
4814 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4816 found &= ~TM_ATTR_PURE;
4818 error_at (DECL_SOURCE_LOCATION (fndecl),
4819 "method overrides both %<transaction_pure%> and %qE methods",
4820 tm_mask_to_attr (found));
4822 /* If FNDECL did not declare an attribute, then inherit the most
4824 else if (tm_attr == NULL)
4826 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
4828 /* Otherwise validate that we're not weaker than a function
4829 that is being overridden. */
4833 if (found <= TM_ATTR_CALLABLE && have > found)
4839 error_at (DECL_SOURCE_LOCATION (fndecl),
4840 "method declared %qE overriding %qE method",
4841 tm_attr, tm_mask_to_attr (found));
4844 /* For each of the methods in T, propagate a class-level tm attribute. */
4847 set_method_tm_attributes (tree t)
4849 tree class_tm_attr, fndecl;
4851 /* Don't bother collecting tm attributes if transactional memory
4852 support is not enabled. */
4856 /* Process virtual methods first, as they inherit directly from the
4857 base virtual function and also require validation of new attributes. */
4858 if (TYPE_CONTAINS_VPTR_P (t))
4861 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4862 vchain = TREE_CHAIN (vchain))
4864 fndecl = BV_FN (vchain);
4865 if (DECL_THUNK_P (fndecl))
4866 fndecl = THUNK_TARGET (fndecl);
4867 set_one_vmethod_tm_attributes (t, fndecl);
4871 /* If the class doesn't have an attribute, nothing more to do. */
4872 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4873 if (class_tm_attr == NULL)
4876 /* Any method that does not yet have a tm attribute inherits
4877 the one from the class. */
4878 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
4879 if (DECL_DECLARES_FUNCTION_P (fndecl)
4880 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4881 apply_tm_attr (fndecl, class_tm_attr);
4884 /* Returns true if FN is a default constructor. */
4887 default_ctor_p (tree fn)
4889 return (DECL_CONSTRUCTOR_P (fn)
4890 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
4893 /* Returns true iff class T has a user-defined constructor that can be called
4894 with more than zero arguments. */
4897 type_has_user_nondefault_constructor (tree t)
4899 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4902 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4905 if (!DECL_ARTIFICIAL (fn)
4906 && (TREE_CODE (fn) == TEMPLATE_DECL
4907 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4915 /* Returns the defaulted constructor if T has one. Otherwise, returns
4919 in_class_defaulted_default_constructor (tree t)
4921 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4924 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4928 if (DECL_DEFAULTED_IN_CLASS_P (fn)
4929 && default_ctor_p (fn))
4936 /* Returns true iff FN is a user-provided function, i.e. user-declared
4937 and not defaulted at its first declaration. */
4940 user_provided_p (tree fn)
4942 if (TREE_CODE (fn) == TEMPLATE_DECL)
4945 return (!DECL_ARTIFICIAL (fn)
4946 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4947 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4950 /* Returns true iff class T has a user-provided constructor. */
4953 type_has_user_provided_constructor (tree t)
4955 if (!CLASS_TYPE_P (t))
4958 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4961 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4962 if (user_provided_p (*iter))
4968 /* Returns true iff class T has a user-provided or explicit constructor. */
4971 type_has_user_provided_or_explicit_constructor (tree t)
4973 if (!CLASS_TYPE_P (t))
4976 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4979 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4982 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
4989 /* Returns true iff class T has a non-user-provided (i.e. implicitly
4990 declared or explicitly defaulted in the class body) default
4994 type_has_non_user_provided_default_constructor (tree t)
4996 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
4998 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5001 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5004 if (TREE_CODE (fn) == FUNCTION_DECL
5005 && default_ctor_p (fn)
5006 && !user_provided_p (fn))
5013 /* TYPE is being used as a virtual base, and has a non-trivial move
5014 assignment. Return true if this is due to there being a user-provided
5015 move assignment in TYPE or one of its subobjects; if there isn't, then
5016 multiple move assignment can't cause any harm. */
5019 vbase_has_user_provided_move_assign (tree type)
5021 /* Does the type itself have a user-provided move assignment operator? */
5022 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5023 for (ovl_iterator iter (get_class_binding_direct
5024 (type, assign_op_identifier));
5026 if (!DECL_ARTIFICIAL (*iter) && move_fn_p (*iter))
5029 /* Do any of its bases? */
5030 tree binfo = TYPE_BINFO (type);
5032 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5033 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5036 /* Or non-static data members? */
5037 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5039 if (TREE_CODE (field) == FIELD_DECL
5040 && CLASS_TYPE_P (TREE_TYPE (field))
5041 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5049 /* If default-initialization leaves part of TYPE uninitialized, returns
5050 a DECL for the field or TYPE itself (DR 253). */
5053 default_init_uninitialized_part (tree type)
5058 type = strip_array_types (type);
5059 if (!CLASS_TYPE_P (type))
5061 if (!type_has_non_user_provided_default_constructor (type))
5063 for (binfo = TYPE_BINFO (type), i = 0;
5064 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5066 r = default_init_uninitialized_part (BINFO_TYPE (t));
5070 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5071 if (TREE_CODE (t) == FIELD_DECL
5072 && !DECL_ARTIFICIAL (t)
5073 && !DECL_INITIAL (t))
5075 r = default_init_uninitialized_part (TREE_TYPE (t));
5077 return DECL_P (r) ? r : t;
5083 /* Returns true iff for class T, a trivial synthesized default constructor
5084 would be constexpr. */
5087 trivial_default_constructor_is_constexpr (tree t)
5089 /* A defaulted trivial default constructor is constexpr
5090 if there is nothing to initialize. */
5091 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5092 return is_really_empty_class (t);
5095 /* Returns true iff class T has a constexpr default constructor. */
5098 type_has_constexpr_default_constructor (tree t)
5102 if (!CLASS_TYPE_P (t))
5104 /* The caller should have stripped an enclosing array. */
5105 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5108 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5110 if (!TYPE_HAS_COMPLEX_DFLT (t))
5111 return trivial_default_constructor_is_constexpr (t);
5112 /* Non-trivial, we need to check subobject constructors. */
5113 lazily_declare_fn (sfk_constructor, t);
5115 fns = locate_ctor (t);
5116 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5119 /* Returns true iff class T has a constexpr default constructor or has an
5120 implicitly declared default constructor that we can't tell if it's constexpr
5121 without forcing a lazy declaration (which might cause undesired
5125 type_maybe_constexpr_default_constructor (tree t)
5127 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5128 && TYPE_HAS_COMPLEX_DFLT (t))
5129 /* Assume it's constexpr. */
5131 return type_has_constexpr_default_constructor (t);
5134 /* Returns true iff class TYPE has a virtual destructor. */
5137 type_has_virtual_destructor (tree type)
5141 if (!CLASS_TYPE_P (type))
5144 gcc_assert (COMPLETE_TYPE_P (type));
5145 dtor = CLASSTYPE_DESTRUCTOR (type);
5146 return (dtor && DECL_VIRTUAL_P (dtor));
5149 /* Returns true iff T, a class, has a move-assignment or
5150 move-constructor. Does not lazily declare either.
5151 If USER_P is false, any move function will do. If it is true, the
5152 move function must be user-declared.
5154 Note that user-declared here is different from "user-provided",
5155 which doesn't include functions that are defaulted in the
5159 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5162 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5163 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5165 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5166 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5167 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5170 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5171 for (ovl_iterator iter (get_class_binding_direct
5172 (t, assign_op_identifier));
5174 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5180 /* True iff T has a move constructor that is not deleted. */
5183 classtype_has_non_deleted_move_ctor (tree t)
5185 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5186 lazily_declare_fn (sfk_move_constructor, t);
5187 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5188 if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5193 /* Nonzero if we need to build up a constructor call when initializing an
5194 object of this class, either because it has a user-declared constructor
5195 or because it doesn't have a default constructor (so we need to give an
5196 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5197 what you care about is whether or not an object can be produced by a
5198 constructor (e.g. so we don't set TREE_READONLY on const variables of
5199 such type); use this function when what you care about is whether or not
5200 to try to call a constructor to create an object. The latter case is
5201 the former plus some cases of constructors that cannot be called. */
5204 type_build_ctor_call (tree t)
5207 if (TYPE_NEEDS_CONSTRUCTING (t))
5209 inner = strip_array_types (t);
5210 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5212 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5214 if (cxx_dialect < cxx11)
5216 /* A user-declared constructor might be private, and a constructor might
5217 be trivial but deleted. */
5218 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5222 if (!DECL_ARTIFICIAL (fn)
5223 || DECL_DELETED_FN (fn))
5229 /* Like type_build_ctor_call, but for destructors. */
5232 type_build_dtor_call (tree t)
5235 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5237 inner = strip_array_types (t);
5238 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5239 || !COMPLETE_TYPE_P (inner))
5241 if (cxx_dialect < cxx11)
5243 /* A user-declared destructor might be private, and a destructor might
5244 be trivial but deleted. */
5245 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5249 if (!DECL_ARTIFICIAL (fn)
5250 || DECL_DELETED_FN (fn))
5256 /* Remove all zero-width bit-fields from T. */
5259 remove_zero_width_bit_fields (tree t)
5263 fieldsp = &TYPE_FIELDS (t);
5266 if (TREE_CODE (*fieldsp) == FIELD_DECL
5267 && DECL_C_BIT_FIELD (*fieldsp)
5268 /* We should not be confused by the fact that grokbitfield
5269 temporarily sets the width of the bit field into
5270 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5271 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5273 && (DECL_SIZE (*fieldsp) == NULL_TREE
5274 || integer_zerop (DECL_SIZE (*fieldsp))))
5275 *fieldsp = DECL_CHAIN (*fieldsp);
5277 fieldsp = &DECL_CHAIN (*fieldsp);
5281 /* Returns TRUE iff we need a cookie when dynamically allocating an
5282 array whose elements have the indicated class TYPE. */
5285 type_requires_array_cookie (tree type)
5288 bool has_two_argument_delete_p = false;
5290 gcc_assert (CLASS_TYPE_P (type));
5292 /* If there's a non-trivial destructor, we need a cookie. In order
5293 to iterate through the array calling the destructor for each
5294 element, we'll have to know how many elements there are. */
5295 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5298 /* If the usual deallocation function is a two-argument whose second
5299 argument is of type `size_t', then we have to pass the size of
5300 the array to the deallocation function, so we will need to store
5302 fns = lookup_fnfields (TYPE_BINFO (type),
5303 ovl_op_identifier (false, VEC_DELETE_EXPR),
5305 /* If there are no `operator []' members, or the lookup is
5306 ambiguous, then we don't need a cookie. */
5307 if (!fns || fns == error_mark_node)
5309 /* Loop through all of the functions. */
5310 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5314 /* See if this function is a one-argument delete function. If
5315 it is, then it will be the usual deallocation function. */
5316 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5317 if (second_parm == void_list_node)
5319 /* Do not consider this function if its second argument is an
5323 /* Otherwise, if we have a two-argument function and the second
5324 argument is `size_t', it will be the usual deallocation
5325 function -- unless there is one-argument function, too. */
5326 if (TREE_CHAIN (second_parm) == void_list_node
5327 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5328 has_two_argument_delete_p = true;
5331 return has_two_argument_delete_p;
5334 /* Finish computing the `literal type' property of class type T.
5336 At this point, we have already processed base classes and
5337 non-static data members. We need to check whether the copy
5338 constructor is trivial, the destructor is trivial, and there
5339 is a trivial default constructor or at least one constexpr
5340 constructor other than the copy constructor. */
5343 finalize_literal_type_property (tree t)
5347 if (cxx_dialect < cxx11
5348 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5349 CLASSTYPE_LITERAL_P (t) = false;
5350 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5351 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5352 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5353 && CLASSTYPE_NON_AGGREGATE (t)
5354 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5355 CLASSTYPE_LITERAL_P (t) = false;
5357 /* C++14 DR 1684 removed this restriction. */
5358 if (cxx_dialect < cxx14
5359 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5360 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5361 if (TREE_CODE (fn) == FUNCTION_DECL
5362 && DECL_DECLARED_CONSTEXPR_P (fn)
5363 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5364 && !DECL_CONSTRUCTOR_P (fn))
5366 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5367 if (!DECL_GENERATED_P (fn)
5368 && pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5369 "enclosing class of %<constexpr%> non-static member "
5370 "function %q+#D is not a literal type", fn))
5371 explain_non_literal_class (t);
5375 /* T is a non-literal type used in a context which requires a constant
5376 expression. Explain why it isn't literal. */
5379 explain_non_literal_class (tree t)
5381 static hash_set<tree> *diagnosed;
5383 if (!CLASS_TYPE_P (t))
5385 t = TYPE_MAIN_VARIANT (t);
5387 if (diagnosed == NULL)
5388 diagnosed = new hash_set<tree>;
5389 if (diagnosed->add (t))
5390 /* Already explained. */
5393 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5394 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5395 inform (UNKNOWN_LOCATION,
5396 " %qT is a closure type, which is only literal in "
5397 "C++17 and later", t);
5398 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5399 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5400 else if (CLASSTYPE_NON_AGGREGATE (t)
5401 && !TYPE_HAS_TRIVIAL_DFLT (t)
5402 && !LAMBDA_TYPE_P (t)
5403 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5405 inform (UNKNOWN_LOCATION,
5406 " %q+T is not an aggregate, does not have a trivial "
5407 "default constructor, and has no %<constexpr%> constructor that "
5408 "is not a copy or move constructor", t);
5409 if (type_has_non_user_provided_default_constructor (t))
5410 /* Note that we can't simply call locate_ctor because when the
5411 constructor is deleted it just returns NULL_TREE. */
5412 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5415 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5417 parms = skip_artificial_parms_for (fn, parms);
5419 if (sufficient_parms_p (parms))
5421 if (DECL_DELETED_FN (fn))
5422 maybe_explain_implicit_delete (fn);
5424 explain_invalid_constexpr_fn (fn);
5431 tree binfo, base_binfo, field; int i;
5432 for (binfo = TYPE_BINFO (t), i = 0;
5433 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5435 tree basetype = TREE_TYPE (base_binfo);
5436 if (!CLASSTYPE_LITERAL_P (basetype))
5438 inform (UNKNOWN_LOCATION,
5439 " base class %qT of %q+T is non-literal",
5441 explain_non_literal_class (basetype);
5445 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5448 if (TREE_CODE (field) != FIELD_DECL)
5450 ftype = TREE_TYPE (field);
5451 if (!literal_type_p (ftype))
5453 inform (DECL_SOURCE_LOCATION (field),
5454 " non-static data member %qD has non-literal type",
5456 if (CLASS_TYPE_P (ftype))
5457 explain_non_literal_class (ftype);
5459 if (CP_TYPE_VOLATILE_P (ftype))
5460 inform (DECL_SOURCE_LOCATION (field),
5461 " non-static data member %qD has volatile type", field);
5466 /* Check the validity of the bases and members declared in T. Add any
5467 implicitly-generated functions (like copy-constructors and
5468 assignment operators). Compute various flag bits (like
5469 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5470 level: i.e., independently of the ABI in use. */
5473 check_bases_and_members (tree t)
5475 /* Nonzero if the implicitly generated copy constructor should take
5476 a non-const reference argument. */
5477 int cant_have_const_ctor;
5478 /* Nonzero if the implicitly generated assignment operator
5479 should take a non-const reference argument. */
5480 int no_const_asn_ref;
5482 bool saved_complex_asn_ref;
5483 bool saved_nontrivial_dtor;
5486 /* By default, we use const reference arguments and generate default
5488 cant_have_const_ctor = 0;
5489 no_const_asn_ref = 0;
5491 /* Check all the base-classes and set FMEM members to point to arrays
5492 of potential interest. */
5493 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5495 /* Deduce noexcept on destructor. This needs to happen after we've set
5496 triviality flags appropriately for our bases. */
5497 if (cxx_dialect >= cxx11)
5498 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5499 deduce_noexcept_on_destructor (dtor);
5501 /* Check all the method declarations. */
5504 /* Save the initial values of these flags which only indicate whether
5505 or not the class has user-provided functions. As we analyze the
5506 bases and members we can set these flags for other reasons. */
5507 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5508 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5510 /* Check all the data member declarations. We cannot call
5511 check_field_decls until we have called check_bases check_methods,
5512 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5513 being set appropriately. */
5514 check_field_decls (t, &access_decls,
5515 &cant_have_const_ctor,
5518 /* A nearly-empty class has to be vptr-containing; a nearly empty
5519 class contains just a vptr. */
5520 if (!TYPE_CONTAINS_VPTR_P (t))
5521 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5523 /* Do some bookkeeping that will guide the generation of implicitly
5524 declared member functions. */
5525 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5526 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5527 /* We need to call a constructor for this class if it has a
5528 user-provided constructor, or if the default constructor is going
5529 to initialize the vptr. (This is not an if-and-only-if;
5530 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5531 themselves need constructing.) */
5532 TYPE_NEEDS_CONSTRUCTING (t)
5533 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5536 An aggregate is an array or a class with no user-provided
5537 constructors ... and no virtual functions.
5539 Again, other conditions for being an aggregate are checked
5541 CLASSTYPE_NON_AGGREGATE (t)
5542 |= (type_has_user_provided_or_explicit_constructor (t)
5543 || TYPE_POLYMORPHIC_P (t));
5544 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5545 retain the old definition internally for ABI reasons. */
5546 CLASSTYPE_NON_LAYOUT_POD_P (t)
5547 |= (CLASSTYPE_NON_AGGREGATE (t)
5548 || saved_nontrivial_dtor || saved_complex_asn_ref);
5549 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5550 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5551 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5552 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5554 /* If the only explicitly declared default constructor is user-provided,
5555 set TYPE_HAS_COMPLEX_DFLT. */
5556 if (!TYPE_HAS_COMPLEX_DFLT (t)
5557 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5558 && !type_has_non_user_provided_default_constructor (t))
5559 TYPE_HAS_COMPLEX_DFLT (t) = true;
5561 /* Warn if a public base of a polymorphic type has an accessible
5562 non-virtual destructor. It is only now that we know the class is
5563 polymorphic. Although a polymorphic base will have a already
5564 been diagnosed during its definition, we warn on use too. */
5565 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5567 tree binfo = TYPE_BINFO (t);
5568 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5572 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5574 tree basetype = TREE_TYPE (base_binfo);
5576 if ((*accesses)[i] == access_public_node
5577 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5578 && accessible_nvdtor_p (basetype))
5579 warning (OPT_Wnon_virtual_dtor,
5580 "base class %q#T has accessible non-virtual destructor",
5585 /* If the class has no user-declared constructor, but does have
5586 non-static const or reference data members that can never be
5587 initialized, issue a warning. */
5588 if (warn_uninitialized
5589 /* Classes with user-declared constructors are presumed to
5590 initialize these members. */
5591 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5592 /* Aggregates can be initialized with brace-enclosed
5594 && CLASSTYPE_NON_AGGREGATE (t))
5598 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5602 if (TREE_CODE (field) != FIELD_DECL
5603 || DECL_INITIAL (field) != NULL_TREE)
5606 type = TREE_TYPE (field);
5607 if (TREE_CODE (type) == REFERENCE_TYPE)
5608 warning_at (DECL_SOURCE_LOCATION (field),
5609 OPT_Wuninitialized, "non-static reference %q#D "
5610 "in class without a constructor", field);
5611 else if (CP_TYPE_CONST_P (type)
5612 && (!CLASS_TYPE_P (type)
5613 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5614 warning_at (DECL_SOURCE_LOCATION (field),
5615 OPT_Wuninitialized, "non-static const member %q#D "
5616 "in class without a constructor", field);
5620 /* Synthesize any needed methods. */
5621 add_implicitly_declared_members (t, &access_decls,
5622 cant_have_const_ctor,
5625 /* Check defaulted declarations here so we have cant_have_const_ctor
5626 and don't need to worry about clones. */
5627 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5628 if (DECL_DECLARES_FUNCTION_P (fn)
5629 && !DECL_ARTIFICIAL (fn)
5630 && DECL_DEFAULTED_IN_CLASS_P (fn))
5632 int copy = copy_fn_p (fn);
5636 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5637 : !no_const_asn_ref);
5638 bool fn_const_p = (copy == 2);
5640 if (fn_const_p && !imp_const_p)
5641 /* If the function is defaulted outside the class, we just
5642 give the synthesis error. */
5643 error ("%q+D declared to take const reference, but implicit "
5644 "declaration would take non-const", fn);
5646 defaulted_late_check (fn);
5649 if (LAMBDA_TYPE_P (t))
5651 /* "This class type is not an aggregate." */
5652 CLASSTYPE_NON_AGGREGATE (t) = 1;
5655 /* Compute the 'literal type' property before we
5656 do anything with non-static member functions. */
5657 finalize_literal_type_property (t);
5659 /* Create the in-charge and not-in-charge variants of constructors
5661 clone_constructors_and_destructors (t);
5663 /* Process the using-declarations. */
5664 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5665 handle_using_decl (TREE_VALUE (access_decls), t);
5667 /* Figure out whether or not we will need a cookie when dynamically
5668 allocating an array of this type. */
5669 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5670 = type_requires_array_cookie (t);
5673 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5674 accordingly. If a new vfield was created (because T doesn't have a
5675 primary base class), then the newly created field is returned. It
5676 is not added to the TYPE_FIELDS list; it is the caller's
5677 responsibility to do that. Accumulate declared virtual functions
5681 create_vtable_ptr (tree t, tree* virtuals_p)
5685 /* Collect the virtual functions declared in T. */
5686 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5687 if (TREE_CODE (fn) == FUNCTION_DECL
5688 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5689 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5691 tree new_virtual = make_node (TREE_LIST);
5693 BV_FN (new_virtual) = fn;
5694 BV_DELTA (new_virtual) = integer_zero_node;
5695 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5697 TREE_CHAIN (new_virtual) = *virtuals_p;
5698 *virtuals_p = new_virtual;
5701 /* If we couldn't find an appropriate base class, create a new field
5702 here. Even if there weren't any new virtual functions, we might need a
5703 new virtual function table if we're supposed to include vptrs in
5704 all classes that need them. */
5705 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5707 /* We build this decl with vtbl_ptr_type_node, which is a
5708 `vtable_entry_type*'. It might seem more precise to use
5709 `vtable_entry_type (*)[N]' where N is the number of virtual
5710 functions. However, that would require the vtable pointer in
5711 base classes to have a different type than the vtable pointer
5712 in derived classes. We could make that happen, but that
5713 still wouldn't solve all the problems. In particular, the
5714 type-based alias analysis code would decide that assignments
5715 to the base class vtable pointer can't alias assignments to
5716 the derived class vtable pointer, since they have different
5717 types. Thus, in a derived class destructor, where the base
5718 class constructor was inlined, we could generate bad code for
5719 setting up the vtable pointer.
5721 Therefore, we use one type for all vtable pointers. We still
5722 use a type-correct type; it's just doesn't indicate the array
5723 bounds. That's better than using `void*' or some such; it's
5724 cleaner, and it let's the alias analysis code know that these
5725 stores cannot alias stores to void*! */
5728 field = build_decl (input_location,
5729 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5730 DECL_VIRTUAL_P (field) = 1;
5731 DECL_ARTIFICIAL (field) = 1;
5732 DECL_FIELD_CONTEXT (field) = t;
5733 DECL_FCONTEXT (field) = t;
5734 if (TYPE_PACKED (t))
5735 DECL_PACKED (field) = 1;
5737 TYPE_VFIELD (t) = field;
5739 /* This class is non-empty. */
5740 CLASSTYPE_EMPTY_P (t) = 0;
5748 /* Add OFFSET to all base types of BINFO which is a base in the
5749 hierarchy dominated by T.
5751 OFFSET, which is a type offset, is number of bytes. */
5754 propagate_binfo_offsets (tree binfo, tree offset)
5760 /* Update BINFO's offset. */
5761 BINFO_OFFSET (binfo)
5762 = fold_convert (sizetype,
5763 size_binop (PLUS_EXPR,
5764 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5767 /* Find the primary base class. */
5768 primary_binfo = get_primary_binfo (binfo);
5770 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5771 propagate_binfo_offsets (primary_binfo, offset);
5773 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5775 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5777 /* Don't do the primary base twice. */
5778 if (base_binfo == primary_binfo)
5781 if (BINFO_VIRTUAL_P (base_binfo))
5784 propagate_binfo_offsets (base_binfo, offset);
5788 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5789 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5790 empty subobjects of T. */
5793 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5799 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5802 /* Find the last field. The artificial fields created for virtual
5803 bases will go after the last extant field to date. */
5804 next_field = &TYPE_FIELDS (t);
5806 next_field = &DECL_CHAIN (*next_field);
5808 /* Go through the virtual bases, allocating space for each virtual
5809 base that is not already a primary base class. These are
5810 allocated in inheritance graph order. */
5811 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5813 if (!BINFO_VIRTUAL_P (vbase))
5816 if (!BINFO_PRIMARY_P (vbase))
5818 /* This virtual base is not a primary base of any class in the
5819 hierarchy, so we have to add space for it. */
5820 next_field = build_base_field (rli, vbase,
5821 offsets, next_field);
5826 /* Returns the offset of the byte just past the end of the base class
5830 end_of_base (tree binfo)
5834 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5835 size = TYPE_SIZE_UNIT (char_type_node);
5836 else if (is_empty_class (BINFO_TYPE (binfo)))
5837 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5838 allocate some space for it. It cannot have virtual bases, so
5839 TYPE_SIZE_UNIT is fine. */
5840 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5842 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5844 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5847 /* Returns the offset of the byte just past the end of the base class
5848 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5849 only non-virtual bases are included. */
5852 end_of_class (tree t, int include_virtuals_p)
5854 tree result = size_zero_node;
5855 vec<tree, va_gc> *vbases;
5861 for (binfo = TYPE_BINFO (t), i = 0;
5862 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5864 if (!include_virtuals_p
5865 && BINFO_VIRTUAL_P (base_binfo)
5866 && (!BINFO_PRIMARY_P (base_binfo)
5867 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5870 offset = end_of_base (base_binfo);
5871 if (tree_int_cst_lt (result, offset))
5875 if (include_virtuals_p)
5876 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5877 vec_safe_iterate (vbases, i, &base_binfo); i++)
5879 offset = end_of_base (base_binfo);
5880 if (tree_int_cst_lt (result, offset))
5887 /* Warn about bases of T that are inaccessible because they are
5888 ambiguous. For example:
5891 struct T : public S {};
5892 struct U : public S, public T {};
5894 Here, `(S*) new U' is not allowed because there are two `S'
5898 warn_about_ambiguous_bases (tree t)
5901 vec<tree, va_gc> *vbases;
5906 /* If there are no repeated bases, nothing can be ambiguous. */
5907 if (!CLASSTYPE_REPEATED_BASE_P (t))
5910 /* Check direct bases. */
5911 for (binfo = TYPE_BINFO (t), i = 0;
5912 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5914 basetype = BINFO_TYPE (base_binfo);
5916 if (!uniquely_derived_from_p (basetype, t))
5917 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5921 /* Check for ambiguous virtual bases. */
5923 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5924 vec_safe_iterate (vbases, i, &binfo); i++)
5926 basetype = BINFO_TYPE (binfo);
5928 if (!uniquely_derived_from_p (basetype, t))
5929 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5930 "to ambiguity", basetype, t);
5934 /* Compare two INTEGER_CSTs K1 and K2. */
5937 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5939 return tree_int_cst_compare ((tree) k1, (tree) k2);
5942 /* Increase the size indicated in RLI to account for empty classes
5943 that are "off the end" of the class. */
5946 include_empty_classes (record_layout_info rli)
5951 /* It might be the case that we grew the class to allocate a
5952 zero-sized base class. That won't be reflected in RLI, yet,
5953 because we are willing to overlay multiple bases at the same
5954 offset. However, now we need to make sure that RLI is big enough
5955 to reflect the entire class. */
5956 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5957 rli_size = rli_size_unit_so_far (rli);
5958 if (TREE_CODE (rli_size) == INTEGER_CST
5959 && tree_int_cst_lt (rli_size, eoc))
5961 /* The size should have been rounded to a whole byte. */
5962 gcc_assert (tree_int_cst_equal
5963 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5965 = size_binop (PLUS_EXPR,
5967 size_binop (MULT_EXPR,
5968 fold_convert (bitsizetype,
5969 size_binop (MINUS_EXPR,
5971 bitsize_int (BITS_PER_UNIT)));
5972 normalize_rli (rli);
5976 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5977 BINFO_OFFSETs for all of the base-classes. Position the vtable
5978 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5981 layout_class_type (tree t, tree *virtuals_p)
5983 tree non_static_data_members;
5986 record_layout_info rli;
5987 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5988 types that appear at that offset. */
5989 splay_tree empty_base_offsets;
5990 /* True if the last field laid out was a bit-field. */
5991 bool last_field_was_bitfield = false;
5992 /* The location at which the next field should be inserted. */
5995 /* Keep track of the first non-static data member. */
5996 non_static_data_members = TYPE_FIELDS (t);
5998 /* Start laying out the record. */
5999 rli = start_record_layout (t);
6001 /* Mark all the primary bases in the hierarchy. */
6002 determine_primary_bases (t);
6004 /* Create a pointer to our virtual function table. */
6005 vptr = create_vtable_ptr (t, virtuals_p);
6007 /* The vptr is always the first thing in the class. */
6010 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6011 TYPE_FIELDS (t) = vptr;
6012 next_field = &DECL_CHAIN (vptr);
6013 place_field (rli, vptr);
6016 next_field = &TYPE_FIELDS (t);
6018 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6019 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6021 build_base_fields (rli, empty_base_offsets, next_field);
6023 /* Layout the non-static data members. */
6024 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6029 /* We still pass things that aren't non-static data members to
6030 the back end, in case it wants to do something with them. */
6031 if (TREE_CODE (field) != FIELD_DECL)
6033 place_field (rli, field);
6034 /* If the static data member has incomplete type, keep track
6035 of it so that it can be completed later. (The handling
6036 of pending statics in finish_record_layout is
6037 insufficient; consider:
6040 struct S2 { static S1 s1; };
6042 At this point, finish_record_layout will be called, but
6043 S1 is still incomplete.) */
6046 maybe_register_incomplete_var (field);
6047 /* The visibility of static data members is determined
6048 at their point of declaration, not their point of
6050 determine_visibility (field);
6055 type = TREE_TYPE (field);
6056 if (type == error_mark_node)
6059 padding = NULL_TREE;
6061 /* If this field is a bit-field whose width is greater than its
6062 type, then there are some special rules for allocating
6064 if (DECL_C_BIT_FIELD (field)
6065 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6067 bool was_unnamed_p = false;
6068 /* We must allocate the bits as if suitably aligned for the
6069 longest integer type that fits in this many bits. Then,
6070 we are supposed to use the left over bits as additional
6073 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6074 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6075 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6076 limit = DECL_SIZE (field);
6078 tree integer_type = integer_types[itk_char];
6079 for (unsigned itk = itk_char; itk != itk_none; itk++)
6080 if (tree next = integer_types[itk])
6082 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6083 /* Too big, so our current guess is what we want. */
6085 /* Not bigger than limit, ok */
6086 integer_type = next;
6089 /* Figure out how much additional padding is required. */
6090 if (TREE_CODE (t) == UNION_TYPE)
6091 /* In a union, the padding field must have the full width
6092 of the bit-field; all fields start at offset zero. */
6093 padding = DECL_SIZE (field);
6095 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6096 TYPE_SIZE (integer_type));
6098 if (integer_zerop (padding))
6099 padding = NULL_TREE;
6101 /* An unnamed bitfield does not normally affect the
6102 alignment of the containing class on a target where
6103 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6104 make any exceptions for unnamed bitfields when the
6105 bitfields are longer than their types. Therefore, we
6106 temporarily give the field a name. */
6107 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6109 was_unnamed_p = true;
6110 DECL_NAME (field) = make_anon_name ();
6113 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6114 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6115 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6116 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6117 empty_base_offsets);
6119 DECL_NAME (field) = NULL_TREE;
6120 /* Now that layout has been performed, set the size of the
6121 field to the size of its declared type; the rest of the
6122 field is effectively invisible. */
6123 DECL_SIZE (field) = TYPE_SIZE (type);
6124 /* We must also reset the DECL_MODE of the field. */
6125 SET_DECL_MODE (field, TYPE_MODE (type));
6128 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6129 empty_base_offsets);
6131 /* Remember the location of any empty classes in FIELD. */
6132 record_subobject_offsets (TREE_TYPE (field),
6133 byte_position(field),
6135 /*is_data_member=*/true);
6137 /* If a bit-field does not immediately follow another bit-field,
6138 and yet it starts in the middle of a byte, we have failed to
6139 comply with the ABI. */
6141 && DECL_C_BIT_FIELD (field)
6142 /* The TREE_NO_WARNING flag gets set by Objective-C when
6143 laying out an Objective-C class. The ObjC ABI differs
6144 from the C++ ABI, and so we do not want a warning
6146 && !TREE_NO_WARNING (field)
6147 && !last_field_was_bitfield
6148 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6149 DECL_FIELD_BIT_OFFSET (field),
6150 bitsize_unit_node)))
6151 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6152 "offset of %qD is not ABI-compliant and may "
6153 "change in a future version of GCC", field);
6155 /* The middle end uses the type of expressions to determine the
6156 possible range of expression values. In order to optimize
6157 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6158 must be made aware of the width of "i", via its type.
6160 Because C++ does not have integer types of arbitrary width,
6161 we must (for the purposes of the front end) convert from the
6162 type assigned here to the declared type of the bitfield
6163 whenever a bitfield expression is used as an rvalue.
6164 Similarly, when assigning a value to a bitfield, the value
6165 must be converted to the type given the bitfield here. */
6166 if (DECL_C_BIT_FIELD (field))
6168 unsigned HOST_WIDE_INT width;
6169 tree ftype = TREE_TYPE (field);
6170 width = tree_to_uhwi (DECL_SIZE (field));
6171 if (width != TYPE_PRECISION (ftype))
6174 = c_build_bitfield_integer_type (width,
6175 TYPE_UNSIGNED (ftype));
6177 = cp_build_qualified_type (TREE_TYPE (field),
6178 cp_type_quals (ftype));
6182 /* If we needed additional padding after this field, add it
6188 padding_field = build_decl (input_location,
6192 DECL_BIT_FIELD (padding_field) = 1;
6193 DECL_SIZE (padding_field) = padding;
6194 DECL_CONTEXT (padding_field) = t;
6195 DECL_ARTIFICIAL (padding_field) = 1;
6196 DECL_IGNORED_P (padding_field) = 1;
6197 DECL_PADDING_P (padding_field) = 1;
6198 layout_nonempty_base_or_field (rli, padding_field,
6200 empty_base_offsets);
6203 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6206 if (!integer_zerop (rli->bitpos))
6208 /* Make sure that we are on a byte boundary so that the size of
6209 the class without virtual bases will always be a round number
6211 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6212 normalize_rli (rli);
6215 /* Delete all zero-width bit-fields from the list of fields. Now
6216 that the type is laid out they are no longer important. */
6217 remove_zero_width_bit_fields (t);
6219 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6221 /* T needs a different layout as a base (eliding virtual bases
6222 or whatever). Create that version. */
6223 tree base_t = make_node (TREE_CODE (t));
6225 /* If the ABI version is not at least two, and the last
6226 field was a bit-field, RLI may not be on a byte
6227 boundary. In particular, rli_size_unit_so_far might
6228 indicate the last complete byte, while rli_size_so_far
6229 indicates the total number of bits used. Therefore,
6230 rli_size_so_far, rather than rli_size_unit_so_far, is
6231 used to compute TYPE_SIZE_UNIT. */
6232 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6233 TYPE_SIZE_UNIT (base_t)
6234 = size_binop (MAX_EXPR,
6235 fold_convert (sizetype,
6236 size_binop (CEIL_DIV_EXPR,
6237 rli_size_so_far (rli),
6238 bitsize_int (BITS_PER_UNIT))),
6241 = size_binop (MAX_EXPR,
6242 rli_size_so_far (rli),
6243 size_binop (MULT_EXPR,
6244 fold_convert (bitsizetype, eoc),
6245 bitsize_int (BITS_PER_UNIT)));
6246 SET_TYPE_ALIGN (base_t, rli->record_align);
6247 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6248 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
6250 /* Copy the non-static data members of T. This will include its
6251 direct non-virtual bases & vtable. */
6252 next_field = &TYPE_FIELDS (base_t);
6253 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6254 if (TREE_CODE (field) == FIELD_DECL)
6256 *next_field = copy_node (field);
6257 DECL_CONTEXT (*next_field) = base_t;
6258 next_field = &DECL_CHAIN (*next_field);
6260 *next_field = NULL_TREE;
6262 /* We use the base type for trivial assignments, and hence it
6264 compute_record_mode (base_t);
6266 TYPE_CONTEXT (base_t) = t;
6268 /* Record the base version of the type. */
6269 CLASSTYPE_AS_BASE (t) = base_t;
6272 CLASSTYPE_AS_BASE (t) = t;
6274 /* Every empty class contains an empty class. */
6275 if (CLASSTYPE_EMPTY_P (t))
6276 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6278 /* Set the TYPE_DECL for this type to contain the right
6279 value for DECL_OFFSET, so that we can use it as part
6280 of a COMPONENT_REF for multiple inheritance. */
6281 layout_decl (TYPE_MAIN_DECL (t), 0);
6283 /* Now fix up any virtual base class types that we left lying
6284 around. We must get these done before we try to lay out the
6285 virtual function table. As a side-effect, this will remove the
6286 base subobject fields. */
6287 layout_virtual_bases (rli, empty_base_offsets);
6289 /* Make sure that empty classes are reflected in RLI at this
6291 include_empty_classes (rli);
6293 /* Make sure not to create any structures with zero size. */
6294 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6296 build_decl (input_location,
6297 FIELD_DECL, NULL_TREE, char_type_node));
6299 /* If this is a non-POD, declaring it packed makes a difference to how it
6300 can be used as a field; don't let finalize_record_size undo it. */
6301 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6302 rli->packed_maybe_necessary = true;
6304 /* Let the back end lay out the type. */
6305 finish_record_layout (rli, /*free_p=*/true);
6307 if (TYPE_SIZE_UNIT (t)
6308 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6309 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6310 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6311 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6313 /* Warn about bases that can't be talked about due to ambiguity. */
6314 warn_about_ambiguous_bases (t);
6316 /* Now that we're done with layout, give the base fields the real types. */
6317 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6318 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6319 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6322 splay_tree_delete (empty_base_offsets);
6324 if (CLASSTYPE_EMPTY_P (t)
6325 && tree_int_cst_lt (sizeof_biggest_empty_class,
6326 TYPE_SIZE_UNIT (t)))
6327 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6330 /* Determine the "key method" for the class type indicated by TYPE,
6331 and set CLASSTYPE_KEY_METHOD accordingly. */
6334 determine_key_method (tree type)
6338 if (processing_template_decl
6339 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6340 || CLASSTYPE_INTERFACE_KNOWN (type))
6343 /* The key method is the first non-pure virtual function that is not
6344 inline at the point of class definition. On some targets the
6345 key function may not be inline; those targets should not call
6346 this function until the end of the translation unit. */
6347 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6348 if (TREE_CODE (method) == FUNCTION_DECL
6349 && DECL_VINDEX (method) != NULL_TREE
6350 && ! DECL_DECLARED_INLINE_P (method)
6351 && ! DECL_PURE_VIRTUAL_P (method))
6353 CLASSTYPE_KEY_METHOD (type) = method;
6360 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6361 class data member of non-zero size, otherwise false. */
6364 field_nonempty_p (const_tree fld)
6366 if (TREE_CODE (fld) == ERROR_MARK)
6369 tree type = TREE_TYPE (fld);
6370 if (TREE_CODE (fld) == FIELD_DECL
6371 && TREE_CODE (type) != ERROR_MARK
6372 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6374 return TYPE_SIZE (type)
6375 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6376 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6382 /* Used by find_flexarrays and related functions. */
6386 /* The first flexible array member or non-zero array member found
6387 in the order of layout. */
6389 /* First non-static non-empty data member in the class or its bases. */
6391 /* The first non-static non-empty data member following either
6392 the flexible array member, if found, or the zero-length array member
6393 otherwise. AFTER[1] refers to the first such data member of a union
6394 of which the struct containing the flexible array member or zero-length
6395 array is a member, or NULL when no such union exists. This element is
6396 only used during searching, not for diagnosing problems. AFTER[0]
6397 refers to the first such data member that is not a member of such
6401 /* Refers to a struct (not union) in which the struct of which the flexible
6402 array is member is defined. Used to diagnose strictly (according to C)
6403 invalid uses of the latter structs. */
6407 /* Find either the first flexible array member or the first zero-length
6408 array, in that order of preference, among members of class T (but not
6409 its base classes), and set members of FMEM accordingly.
6410 BASE_P is true if T is a base class of another class.
6411 PUN is set to the outermost union in which the flexible array member
6412 (or zero-length array) is defined if one such union exists, otherwise
6414 Similarly, PSTR is set to a data member of the outermost struct of
6415 which the flexible array is a member if one such struct exists,
6416 otherwise to NULL. */
6419 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6420 tree pun /* = NULL_TREE */,
6421 tree pstr /* = NULL_TREE */)
6423 /* Set the "pointer" to the outermost enclosing union if not set
6424 yet and maintain it for the remainder of the recursion. */
6425 if (!pun && TREE_CODE (t) == UNION_TYPE)
6428 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6430 if (fld == error_mark_node)
6433 /* Is FLD a typedef for an anonymous struct? */
6435 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6436 handled elsewhere so that errors like the following are detected
6438 typedef struct { int i, a[], j; } S; // bug c++/72753
6439 S s [2]; // bug c++/68489
6441 if (TREE_CODE (fld) == TYPE_DECL
6442 && DECL_IMPLICIT_TYPEDEF_P (fld)
6443 && CLASS_TYPE_P (TREE_TYPE (fld))
6444 && anon_aggrname_p (DECL_NAME (fld)))
6446 /* Check the nested unnamed type referenced via a typedef
6447 independently of FMEM (since it's not a data member of
6448 the enclosing class). */
6449 check_flexarrays (TREE_TYPE (fld));
6453 /* Skip anything that's GCC-generated or not a (non-static) data
6455 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6458 /* Type of the member. */
6459 tree fldtype = TREE_TYPE (fld);
6460 if (fldtype == error_mark_node)
6463 /* Determine the type of the array element or object referenced
6464 by the member so that it can be checked for flexible array
6465 members if it hasn't been yet. */
6466 tree eltype = fldtype;
6467 while (TREE_CODE (eltype) == ARRAY_TYPE
6468 || TREE_CODE (eltype) == POINTER_TYPE
6469 || TREE_CODE (eltype) == REFERENCE_TYPE)
6470 eltype = TREE_TYPE (eltype);
6472 if (RECORD_OR_UNION_TYPE_P (eltype))
6474 if (fmem->array && !fmem->after[bool (pun)])
6476 /* Once the member after the flexible array has been found
6478 fmem->after[bool (pun)] = fld;
6482 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6484 /* Descend into the non-static member struct or union and try
6485 to find a flexible array member or zero-length array among
6486 its members. This is only necessary for anonymous types
6487 and types in whose context the current type T has not been
6488 defined (the latter must not be checked again because they
6489 are already in the process of being checked by one of the
6490 recursive calls). */
6492 tree first = fmem->first;
6493 tree array = fmem->array;
6495 /* If this member isn't anonymous and a prior non-flexible array
6496 member has been seen in one of the enclosing structs, clear
6497 the FIRST member since it doesn't contribute to the flexible
6498 array struct's members. */
6499 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6500 fmem->first = NULL_TREE;
6502 find_flexarrays (eltype, fmem, false, pun,
6503 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6505 if (fmem->array != array)
6508 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6510 /* Restore the FIRST member reset above if no flexible
6511 array member has been found in this member's struct. */
6512 fmem->first = first;
6515 /* If the member struct contains the first flexible array
6516 member, or if this member is a base class, continue to
6517 the next member and avoid setting the FMEM->NEXT pointer
6524 if (field_nonempty_p (fld))
6526 /* Remember the first non-static data member. */
6530 /* Remember the first non-static data member after the flexible
6531 array member, if one has been found, or the zero-length array
6532 if it has been found. */
6533 if (fmem->array && !fmem->after[bool (pun)])
6534 fmem->after[bool (pun)] = fld;
6537 /* Skip non-arrays. */
6538 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6541 /* Determine the upper bound of the array if it has one. */
6542 if (TYPE_DOMAIN (fldtype))
6546 /* Make a record of the zero-length array if either one
6547 such field or a flexible array member has been seen to
6548 handle the pathological and unlikely case of multiple
6550 if (!fmem->after[bool (pun)])
6551 fmem->after[bool (pun)] = fld;
6553 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6555 /* Remember the first zero-length array unless a flexible array
6556 member has already been seen. */
6558 fmem->enclosing = pstr;
6563 /* Flexible array members have no upper bound. */
6566 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6568 /* Replace the zero-length array if it's been stored and
6569 reset the after pointer. */
6570 fmem->after[bool (pun)] = NULL_TREE;
6572 fmem->enclosing = pstr;
6574 else if (!fmem->after[bool (pun)])
6575 /* Make a record of another flexible array member. */
6576 fmem->after[bool (pun)] = fld;
6581 fmem->enclosing = pstr;
6587 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6588 a flexible array member (or the zero-length array extension). */
6591 diagnose_invalid_flexarray (const flexmems_t *fmem)
6593 if (fmem->array && fmem->enclosing
6594 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6595 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6596 ? G_("invalid use of %q#T with a zero-size array "
6598 : G_("invalid use of %q#T with a flexible array member "
6600 DECL_CONTEXT (fmem->array),
6601 DECL_CONTEXT (fmem->enclosing)))
6602 inform (DECL_SOURCE_LOCATION (fmem->array),
6603 "array member %q#D declared here", fmem->array);
6606 /* Issue diagnostics for invalid flexible array members or zero-length
6607 arrays that are not the last elements of the containing class or its
6608 base classes or that are its sole members. */
6611 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6616 if (fmem->first && !fmem->after[0])
6618 diagnose_invalid_flexarray (fmem);
6622 /* Has a diagnostic been issued? */
6625 const char *msg = 0;
6627 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6630 msg = G_("zero-size array member %qD not at end of %q#T");
6631 else if (!fmem->first)
6632 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6636 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6638 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6640 inform (location_of (t), "in the definition of %q#T", t);
6648 msg = G_("flexible array member %qD not at end of %q#T");
6649 else if (!fmem->first)
6650 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6654 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6657 error_at (loc, msg, fmem->array, t);
6659 /* In the unlikely event that the member following the flexible
6660 array member is declared in a different class, or the member
6661 overlaps another member of a common union, point to it.
6662 Otherwise it should be obvious. */
6664 && ((DECL_CONTEXT (fmem->after[0])
6665 != DECL_CONTEXT (fmem->array))))
6667 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6668 "next member %q#D declared here",
6670 inform (location_of (t), "in the definition of %q#T", t);
6675 if (!diagd && fmem->array && fmem->enclosing)
6676 diagnose_invalid_flexarray (fmem);
6680 /* Recursively check to make sure that any flexible array or zero-length
6681 array members of class T or its bases are valid (i.e., not the sole
6682 non-static data member of T and, if one exists, that it is the last
6683 non-static data member of T and its base classes. FMEM is expected
6684 to be initially null and is used internally by recursive calls to
6685 the function. Issue the appropriate diagnostics for the array member
6686 that fails the checks. */
6689 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
6690 bool base_p /* = false */)
6692 /* Initialize the result of a search for flexible array and zero-length
6693 array members. Avoid doing any work if the most interesting FMEM data
6694 have already been populated. */
6695 flexmems_t flexmems = flexmems_t ();
6698 else if (fmem->array && fmem->first && fmem->after[0])
6701 tree fam = fmem->array;
6703 /* Recursively check the primary base class first. */
6704 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6706 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6707 check_flexarrays (basetype, fmem, true);
6710 /* Recursively check the base classes. */
6711 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
6712 for (int i = 0; i < nbases; ++i)
6714 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6716 /* The primary base class was already checked above. */
6717 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6720 /* Virtual base classes are at the end. */
6721 if (BINFO_VIRTUAL_P (base_binfo))
6724 /* Check the base class. */
6725 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
6728 if (fmem == &flexmems)
6730 /* Check virtual base classes only once per derived class.
6731 I.e., this check is not performed recursively for base
6735 vec<tree, va_gc> *vbases;
6736 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6737 vec_safe_iterate (vbases, i, &base_binfo); i++)
6739 /* Check the virtual base class. */
6740 tree basetype = TREE_TYPE (base_binfo);
6742 check_flexarrays (basetype, fmem, /*base_p=*/true);
6746 /* Is the type unnamed (and therefore a member of it potentially
6747 an anonymous struct or union)? */
6748 bool maybe_anon_p = TYPE_UNNAMED_P (t);
6750 /* Search the members of the current (possibly derived) class, skipping
6751 unnamed structs and unions since those could be anonymous. */
6752 if (fmem != &flexmems || !maybe_anon_p)
6753 find_flexarrays (t, fmem, base_p || fam != fmem->array);
6755 if (fmem == &flexmems && !maybe_anon_p)
6757 /* Issue diagnostics for invalid flexible and zero-length array
6758 members found in base classes or among the members of the current
6759 class. Ignore anonymous structs and unions whose members are
6760 considered to be members of the enclosing class and thus will
6761 be diagnosed when checking it. */
6762 diagnose_flexarrays (t, fmem);
6766 /* Perform processing required when the definition of T (a class type)
6767 is complete. Diagnose invalid definitions of flexible array members
6768 and zero-size arrays. */
6771 finish_struct_1 (tree t)
6774 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6775 tree virtuals = NULL_TREE;
6777 if (COMPLETE_TYPE_P (t))
6779 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6780 error ("redefinition of %q#T", t);
6785 /* If this type was previously laid out as a forward reference,
6786 make sure we lay it out again. */
6787 TYPE_SIZE (t) = NULL_TREE;
6788 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6790 /* Make assumptions about the class; we'll reset the flags if
6792 CLASSTYPE_EMPTY_P (t) = 1;
6793 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6794 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6795 CLASSTYPE_LITERAL_P (t) = true;
6797 /* Do end-of-class semantic processing: checking the validity of the
6798 bases and members and add implicitly generated methods. */
6799 check_bases_and_members (t);
6801 /* Find the key method. */
6802 if (TYPE_CONTAINS_VPTR_P (t))
6804 /* The Itanium C++ ABI permits the key method to be chosen when
6805 the class is defined -- even though the key method so
6806 selected may later turn out to be an inline function. On
6807 some systems (such as ARM Symbian OS) the key method cannot
6808 be determined until the end of the translation unit. On such
6809 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6810 will cause the class to be added to KEYED_CLASSES. Then, in
6811 finish_file we will determine the key method. */
6812 if (targetm.cxx.key_method_may_be_inline ())
6813 determine_key_method (t);
6815 /* If a polymorphic class has no key method, we may emit the vtable
6816 in every translation unit where the class definition appears. If
6817 we're devirtualizing, we can look into the vtable even if we
6818 aren't emitting it. */
6819 if (!CLASSTYPE_KEY_METHOD (t))
6820 vec_safe_push (keyed_classes, t);
6823 /* Layout the class itself. */
6824 layout_class_type (t, &virtuals);
6825 /* COMPLETE_TYPE_P is now true. */
6827 set_class_bindings (t);
6829 /* With the layout complete, check for flexible array members and
6830 zero-length arrays that might overlap other members in the final
6832 check_flexarrays (t);
6834 virtuals = modify_all_vtables (t, nreverse (virtuals));
6836 /* If necessary, create the primary vtable for this class. */
6837 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6839 /* We must enter these virtuals into the table. */
6840 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6841 build_primary_vtable (NULL_TREE, t);
6842 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6843 /* Here we know enough to change the type of our virtual
6844 function table, but we will wait until later this function. */
6845 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6847 /* If we're warning about ABI tags, check the types of the new
6848 virtual functions. */
6850 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6851 check_abi_tags (t, TREE_VALUE (v));
6854 if (TYPE_CONTAINS_VPTR_P (t))
6859 if (BINFO_VTABLE (TYPE_BINFO (t)))
6860 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6861 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6862 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6864 /* Add entries for virtual functions introduced by this class. */
6865 BINFO_VIRTUALS (TYPE_BINFO (t))
6866 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6868 /* Set DECL_VINDEX for all functions declared in this class. */
6869 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6871 fn = TREE_CHAIN (fn),
6872 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6873 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6875 tree fndecl = BV_FN (fn);
6877 if (DECL_THUNK_P (fndecl))
6878 /* A thunk. We should never be calling this entry directly
6879 from this vtable -- we'd use the entry for the non
6880 thunk base function. */
6881 DECL_VINDEX (fndecl) = NULL_TREE;
6882 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6883 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6887 finish_struct_bits (t);
6889 set_method_tm_attributes (t);
6890 if (flag_openmp || flag_openmp_simd)
6891 finish_omp_declare_simd_methods (t);
6893 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
6894 for any static member objects of the type we're working on. */
6895 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6896 if (DECL_DECLARES_FUNCTION_P (x))
6897 DECL_IN_AGGR_P (x) = false;
6898 else if (VAR_P (x) && TREE_STATIC (x)
6899 && TREE_TYPE (x) != error_mark_node
6900 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6901 SET_DECL_MODE (x, TYPE_MODE (t));
6903 /* Complain if one of the field types requires lower visibility. */
6904 constrain_class_visibility (t);
6906 /* Make the rtl for any new vtables we have created, and unmark
6907 the base types we marked. */
6910 /* Build the VTT for T. */
6914 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6915 && !CLASSTYPE_FINAL (t))
6916 warning (OPT_Wnon_virtual_dtor,
6917 "%q#T has virtual functions and accessible"
6918 " non-virtual destructor", t);
6922 if (warn_overloaded_virtual)
6925 /* Class layout, assignment of virtual table slots, etc., is now
6926 complete. Give the back end a chance to tweak the visibility of
6927 the class or perform any other required target modifications. */
6928 targetm.cxx.adjust_class_at_definition (t);
6930 maybe_suppress_debug_info (t);
6932 if (flag_vtable_verify)
6933 vtv_save_class_info (t);
6935 dump_class_hierarchy (t);
6937 /* Finish debugging output for this type. */
6938 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6940 if (TYPE_TRANSPARENT_AGGR (t))
6942 tree field = first_field (t);
6943 if (field == NULL_TREE || error_operand_p (field))
6945 error ("type transparent %q#T does not have any fields", t);
6946 TYPE_TRANSPARENT_AGGR (t) = 0;
6948 else if (DECL_ARTIFICIAL (field))
6950 if (DECL_FIELD_IS_BASE (field))
6951 error ("type transparent class %qT has base classes", t);
6954 gcc_checking_assert (DECL_VIRTUAL_P (field));
6955 error ("type transparent class %qT has virtual functions", t);
6957 TYPE_TRANSPARENT_AGGR (t) = 0;
6959 else if (TYPE_MODE (t) != DECL_MODE (field))
6961 error ("type transparent %q#T cannot be made transparent because "
6962 "the type of the first field has a different ABI from the "
6963 "class overall", t);
6964 TYPE_TRANSPARENT_AGGR (t) = 0;
6969 /* When T was built up, the member declarations were added in reverse
6970 order. Rearrange them to declaration order. */
6973 unreverse_member_declarations (tree t)
6979 /* The following lists are all in reverse order. Put them in
6980 declaration order now. */
6981 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6983 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
6984 order, so we can't just use nreverse. Due to stat_hack
6985 chicanery in finish_member_declaration. */
6987 for (x = TYPE_FIELDS (t);
6988 x && TREE_CODE (x) != TYPE_DECL;
6991 next = DECL_CHAIN (x);
6992 DECL_CHAIN (x) = prev;
6998 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6999 TYPE_FIELDS (t) = prev;
7004 finish_struct (tree t, tree attributes)
7006 location_t saved_loc = input_location;
7008 /* Now that we've got all the field declarations, reverse everything
7010 unreverse_member_declarations (t);
7012 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7013 fixup_attribute_variants (t);
7015 /* Nadger the current location so that diagnostics point to the start of
7016 the struct, not the end. */
7017 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7019 if (processing_template_decl)
7023 /* We need to add the target functions of USING_DECLS, so that
7024 they can be found when the using declaration is not
7025 instantiated yet. */
7026 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7027 if (TREE_CODE (x) == USING_DECL)
7029 tree fn = strip_using_decl (x);
7031 for (lkp_iterator iter (fn); iter; ++iter)
7032 add_method (t, *iter, true);
7034 else if (DECL_DECLARES_FUNCTION_P (x))
7035 DECL_IN_AGGR_P (x) = false;
7037 /* Also add a USING_DECL for operator=. We know there'll be (at
7038 least) one, but we don't know the signature(s). We want name
7039 lookup not to fail or recurse into bases. This isn't added
7040 to the template decl list so we drop this at instantiation
7042 tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
7044 DECL_CONTEXT (ass_op) = t;
7045 USING_DECL_SCOPE (ass_op) = t;
7046 DECL_DEPENDENT_P (ass_op) = true;
7047 DECL_ARTIFICIAL (ass_op) = true;
7048 DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
7049 TYPE_FIELDS (t) = ass_op;
7051 TYPE_SIZE (t) = bitsize_zero_node;
7052 TYPE_SIZE_UNIT (t) = size_zero_node;
7053 /* COMPLETE_TYPE_P is now true. */
7055 set_class_bindings (t);
7057 /* We need to emit an error message if this type was used as a parameter
7058 and it is an abstract type, even if it is a template. We construct
7059 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7060 account and we call complete_vars with this type, which will check
7061 the PARM_DECLS. Note that while the type is being defined,
7062 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7063 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7064 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7065 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7066 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7067 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7070 /* Remember current #pragma pack value. */
7071 TYPE_PRECISION (t) = maximum_field_alignment;
7073 /* Fix up any variants we've already built. */
7074 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7076 TYPE_SIZE (x) = TYPE_SIZE (t);
7077 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7078 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7082 finish_struct_1 (t);
7083 /* COMPLETE_TYPE_P is now true. */
7085 maybe_warn_about_overly_private_class (t);
7087 if (is_std_init_list (t))
7089 /* People keep complaining that the compiler crashes on an invalid
7090 definition of initializer_list, so I guess we should explicitly
7091 reject it. What the compiler internals care about is that it's a
7092 template and has a pointer field followed by size_type field. */
7094 if (processing_template_decl)
7096 tree f = next_initializable_field (TYPE_FIELDS (t));
7097 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7099 f = next_initializable_field (DECL_CHAIN (f));
7100 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7105 fatal_error (input_location, "definition of %qD does not match "
7106 "%<#include <initializer_list>%>", TYPE_NAME (t));
7109 input_location = saved_loc;
7111 TYPE_BEING_DEFINED (t) = 0;
7113 if (current_class_type)
7116 error ("trying to finish struct, but kicked out due to previous parse errors");
7118 if (processing_template_decl && at_function_scope_p ()
7119 /* Lambdas are defined by the LAMBDA_EXPR. */
7120 && !LAMBDA_TYPE_P (t))
7121 add_stmt (build_min (TAG_DEFN, t));
7126 /* Hash table to avoid endless recursion when handling references. */
7127 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7129 /* Return the dynamic type of INSTANCE, if known.
7130 Used to determine whether the virtual function table is needed
7133 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7134 of our knowledge of its type. *NONNULL should be initialized
7135 before this function is called. */
7138 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7140 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7142 switch (TREE_CODE (instance))
7145 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7148 return RECUR (TREE_OPERAND (instance, 0));
7151 /* This is a call to a constructor, hence it's never zero. */
7152 if (CALL_EXPR_FN (instance)
7153 && TREE_HAS_CONSTRUCTOR (instance))
7157 return TREE_TYPE (instance);
7162 /* This is a call to a constructor, hence it's never zero. */
7163 if (TREE_HAS_CONSTRUCTOR (instance))
7167 return TREE_TYPE (instance);
7169 return RECUR (TREE_OPERAND (instance, 0));
7171 case POINTER_PLUS_EXPR:
7174 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7175 return RECUR (TREE_OPERAND (instance, 0));
7176 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7177 /* Propagate nonnull. */
7178 return RECUR (TREE_OPERAND (instance, 0));
7183 return RECUR (TREE_OPERAND (instance, 0));
7186 instance = TREE_OPERAND (instance, 0);
7189 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7190 with a real object -- given &p->f, p can still be null. */
7191 tree t = get_base_address (instance);
7192 /* ??? Probably should check DECL_WEAK here. */
7193 if (t && DECL_P (t))
7196 return RECUR (instance);
7199 /* If this component is really a base class reference, then the field
7200 itself isn't definitive. */
7201 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7202 return RECUR (TREE_OPERAND (instance, 0));
7203 return RECUR (TREE_OPERAND (instance, 1));
7207 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7208 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7212 return TREE_TYPE (TREE_TYPE (instance));
7218 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7222 return TREE_TYPE (instance);
7224 else if (instance == current_class_ptr)
7229 /* if we're in a ctor or dtor, we know our type. If
7230 current_class_ptr is set but we aren't in a function, we're in
7231 an NSDMI (and therefore a constructor). */
7232 if (current_scope () != current_function_decl
7233 || (DECL_LANG_SPECIFIC (current_function_decl)
7234 && (DECL_CONSTRUCTOR_P (current_function_decl)
7235 || DECL_DESTRUCTOR_P (current_function_decl))))
7239 return TREE_TYPE (TREE_TYPE (instance));
7242 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7244 /* We only need one hash table because it is always left empty. */
7245 if (!fixed_type_or_null_ref_ht)
7246 fixed_type_or_null_ref_ht
7247 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7249 /* Reference variables should be references to objects. */
7253 /* Enter the INSTANCE in a table to prevent recursion; a
7254 variable's initializer may refer to the variable
7256 if (VAR_P (instance)
7257 && DECL_INITIAL (instance)
7258 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7259 && !fixed_type_or_null_ref_ht->find (instance))
7264 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7266 type = RECUR (DECL_INITIAL (instance));
7267 fixed_type_or_null_ref_ht->remove_elt (instance);
7280 /* Return nonzero if the dynamic type of INSTANCE is known, and
7281 equivalent to the static type. We also handle the case where
7282 INSTANCE is really a pointer. Return negative if this is a
7283 ctor/dtor. There the dynamic type is known, but this might not be
7284 the most derived base of the original object, and hence virtual
7285 bases may not be laid out according to this type.
7287 Used to determine whether the virtual function table is needed
7290 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7291 of our knowledge of its type. *NONNULL should be initialized
7292 before this function is called. */
7295 resolves_to_fixed_type_p (tree instance, int* nonnull)
7297 tree t = TREE_TYPE (instance);
7301 /* processing_template_decl can be false in a template if we're in
7302 instantiate_non_dependent_expr, but we still want to suppress
7304 if (in_template_function ())
7306 /* In a template we only care about the type of the result. */
7312 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7313 if (fixed == NULL_TREE)
7315 if (POINTER_TYPE_P (t))
7317 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7319 return cdtorp ? -1 : 1;
7324 init_class_processing (void)
7326 current_class_depth = 0;
7327 current_class_stack_size = 10;
7329 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7330 vec_alloc (local_classes, 8);
7331 sizeof_biggest_empty_class = size_zero_node;
7333 ridpointers[(int) RID_PUBLIC] = access_public_node;
7334 ridpointers[(int) RID_PRIVATE] = access_private_node;
7335 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7338 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7341 restore_class_cache (void)
7345 /* We are re-entering the same class we just left, so we don't
7346 have to search the whole inheritance matrix to find all the
7347 decls to bind again. Instead, we install the cached
7348 class_shadowed list and walk through it binding names. */
7349 push_binding_level (previous_class_level);
7350 class_binding_level = previous_class_level;
7351 /* Restore IDENTIFIER_TYPE_VALUE. */
7352 for (type = class_binding_level->type_shadowed;
7354 type = TREE_CHAIN (type))
7355 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7358 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7359 appropriate for TYPE.
7361 So that we may avoid calls to lookup_name, we cache the _TYPE
7362 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7364 For multiple inheritance, we perform a two-pass depth-first search
7365 of the type lattice. */
7368 pushclass (tree type)
7370 class_stack_node_t csn;
7372 type = TYPE_MAIN_VARIANT (type);
7374 /* Make sure there is enough room for the new entry on the stack. */
7375 if (current_class_depth + 1 >= current_class_stack_size)
7377 current_class_stack_size *= 2;
7379 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7380 current_class_stack_size);
7383 /* Insert a new entry on the class stack. */
7384 csn = current_class_stack + current_class_depth;
7385 csn->name = current_class_name;
7386 csn->type = current_class_type;
7387 csn->access = current_access_specifier;
7388 csn->names_used = 0;
7390 current_class_depth++;
7392 /* Now set up the new type. */
7393 current_class_name = TYPE_NAME (type);
7394 if (TREE_CODE (current_class_name) == TYPE_DECL)
7395 current_class_name = DECL_NAME (current_class_name);
7396 current_class_type = type;
7398 /* By default, things in classes are private, while things in
7399 structures or unions are public. */
7400 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7401 ? access_private_node
7402 : access_public_node);
7404 if (previous_class_level
7405 && type != previous_class_level->this_entity
7406 && current_class_depth == 1)
7408 /* Forcibly remove any old class remnants. */
7409 invalidate_class_lookup_cache ();
7412 if (!previous_class_level
7413 || type != previous_class_level->this_entity
7414 || current_class_depth > 1)
7417 restore_class_cache ();
7420 /* When we exit a toplevel class scope, we save its binding level so
7421 that we can restore it quickly. Here, we've entered some other
7422 class, so we must invalidate our cache. */
7425 invalidate_class_lookup_cache (void)
7427 previous_class_level = NULL;
7430 /* Get out of the current class scope. If we were in a class scope
7431 previously, that is the one popped to. */
7438 current_class_depth--;
7439 current_class_name = current_class_stack[current_class_depth].name;
7440 current_class_type = current_class_stack[current_class_depth].type;
7441 current_access_specifier = current_class_stack[current_class_depth].access;
7442 if (current_class_stack[current_class_depth].names_used)
7443 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7446 /* Mark the top of the class stack as hidden. */
7449 push_class_stack (void)
7451 if (current_class_depth)
7452 ++current_class_stack[current_class_depth - 1].hidden;
7455 /* Mark the top of the class stack as un-hidden. */
7458 pop_class_stack (void)
7460 if (current_class_depth)
7461 --current_class_stack[current_class_depth - 1].hidden;
7464 /* If the class type currently being defined is either T or
7465 a nested type of T, returns the type from the current_class_stack,
7466 which might be equivalent to but not equal to T in case of
7467 constrained partial specializations. */
7470 currently_open_class (tree t)
7474 if (!CLASS_TYPE_P (t))
7477 t = TYPE_MAIN_VARIANT (t);
7479 /* We start looking from 1 because entry 0 is from global scope,
7481 for (i = current_class_depth; i > 0; --i)
7484 if (i == current_class_depth)
7485 c = current_class_type;
7488 if (current_class_stack[i].hidden)
7490 c = current_class_stack[i].type;
7494 if (same_type_p (c, t))
7500 /* If either current_class_type or one of its enclosing classes are derived
7501 from T, return the appropriate type. Used to determine how we found
7502 something via unqualified lookup. */
7505 currently_open_derived_class (tree t)
7509 /* The bases of a dependent type are unknown. */
7510 if (dependent_type_p (t))
7513 if (!current_class_type)
7516 if (DERIVED_FROM_P (t, current_class_type))
7517 return current_class_type;
7519 for (i = current_class_depth - 1; i > 0; --i)
7521 if (current_class_stack[i].hidden)
7523 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7524 return current_class_stack[i].type;
7530 /* Return the outermost enclosing class type that is still open, or
7534 outermost_open_class (void)
7536 if (!current_class_type)
7539 if (TYPE_BEING_DEFINED (current_class_type))
7540 r = current_class_type;
7541 for (int i = current_class_depth - 1; i > 0; --i)
7543 if (current_class_stack[i].hidden)
7545 tree t = current_class_stack[i].type;
7546 if (!TYPE_BEING_DEFINED (t))
7553 /* Returns the innermost class type which is not a lambda closure type. */
7556 current_nonlambda_class_type (void)
7558 tree type = current_class_type;
7559 while (type && LAMBDA_TYPE_P (type))
7560 type = decl_type_context (TYPE_NAME (type));
7564 /* When entering a class scope, all enclosing class scopes' names with
7565 static meaning (static variables, static functions, types and
7566 enumerators) have to be visible. This recursive function calls
7567 pushclass for all enclosing class contexts until global or a local
7568 scope is reached. TYPE is the enclosed class. */
7571 push_nested_class (tree type)
7573 /* A namespace might be passed in error cases, like A::B:C. */
7574 if (type == NULL_TREE
7575 || !CLASS_TYPE_P (type))
7578 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7583 /* Undoes a push_nested_class call. */
7586 pop_nested_class (void)
7588 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7591 if (context && CLASS_TYPE_P (context))
7592 pop_nested_class ();
7595 /* Returns the number of extern "LANG" blocks we are nested within. */
7598 current_lang_depth (void)
7600 return vec_safe_length (current_lang_base);
7603 /* Set global variables CURRENT_LANG_NAME to appropriate value
7604 so that behavior of name-mangling machinery is correct. */
7607 push_lang_context (tree name)
7609 vec_safe_push (current_lang_base, current_lang_name);
7611 if (name == lang_name_cplusplus)
7612 current_lang_name = name;
7613 else if (name == lang_name_c)
7614 current_lang_name = name;
7616 error ("language string %<\"%E\"%> not recognized", name);
7619 /* Get out of the current language scope. */
7622 pop_lang_context (void)
7624 current_lang_name = current_lang_base->pop ();
7627 /* Type instantiation routines. */
7629 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7630 matches the TARGET_TYPE. If there is no satisfactory match, return
7631 error_mark_node, and issue an error & warning messages under
7632 control of FLAGS. Permit pointers to member function if FLAGS
7633 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7634 a template-id, and EXPLICIT_TARGS are the explicitly provided
7637 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7638 is the base path used to reference those member functions. If
7639 the address is resolved to a member function, access checks will be
7640 performed and errors issued if appropriate. */
7643 resolve_address_of_overloaded_function (tree target_type,
7645 tsubst_flags_t complain,
7647 tree explicit_targs,
7650 /* Here's what the standard says:
7654 If the name is a function template, template argument deduction
7655 is done, and if the argument deduction succeeds, the deduced
7656 arguments are used to generate a single template function, which
7657 is added to the set of overloaded functions considered.
7659 Non-member functions and static member functions match targets of
7660 type "pointer-to-function" or "reference-to-function." Nonstatic
7661 member functions match targets of type "pointer-to-member
7662 function;" the function type of the pointer to member is used to
7663 select the member function from the set of overloaded member
7664 functions. If a nonstatic member function is selected, the
7665 reference to the overloaded function name is required to have the
7666 form of a pointer to member as described in 5.3.1.
7668 If more than one function is selected, any template functions in
7669 the set are eliminated if the set also contains a non-template
7670 function, and any given template function is eliminated if the
7671 set contains a second template function that is more specialized
7672 than the first according to the partial ordering rules 14.5.5.2.
7673 After such eliminations, if any, there shall remain exactly one
7674 selected function. */
7677 /* We store the matches in a TREE_LIST rooted here. The functions
7678 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7679 interoperability with most_specialized_instantiation. */
7680 tree matches = NULL_TREE;
7682 tree target_fn_type;
7684 /* By the time we get here, we should be seeing only real
7685 pointer-to-member types, not the internal POINTER_TYPE to
7686 METHOD_TYPE representation. */
7687 gcc_assert (!TYPE_PTR_P (target_type)
7688 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7690 gcc_assert (is_overloaded_fn (overload));
7692 /* Check that the TARGET_TYPE is reasonable. */
7693 if (TYPE_PTRFN_P (target_type)
7694 || TYPE_REFFN_P (target_type))
7696 else if (TYPE_PTRMEMFUNC_P (target_type))
7697 /* This is OK, too. */
7699 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7700 /* This is OK, too. This comes from a conversion to reference
7702 target_type = build_reference_type (target_type);
7705 if (complain & tf_error)
7706 error ("cannot resolve overloaded function %qD based on"
7707 " conversion to type %qT",
7708 OVL_NAME (overload), target_type);
7709 return error_mark_node;
7712 /* Non-member functions and static member functions match targets of type
7713 "pointer-to-function" or "reference-to-function." Nonstatic member
7714 functions match targets of type "pointer-to-member-function;" the
7715 function type of the pointer to member is used to select the member
7716 function from the set of overloaded member functions.
7718 So figure out the FUNCTION_TYPE that we want to match against. */
7719 target_fn_type = static_fn_type (target_type);
7721 /* If we can find a non-template function that matches, we can just
7722 use it. There's no point in generating template instantiations
7723 if we're just going to throw them out anyhow. But, of course, we
7724 can only do this when we don't *need* a template function. */
7726 for (lkp_iterator iter (overload); iter; ++iter)
7730 if (TREE_CODE (fn) == TEMPLATE_DECL)
7731 /* We're not looking for templates just yet. */
7734 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
7735 /* We're looking for a non-static member, and this isn't
7736 one, or vice versa. */
7739 /* In C++17 we need the noexcept-qualifier to compare types. */
7740 if (flag_noexcept_type
7741 && !maybe_instantiate_noexcept (fn, complain))
7744 /* See if there's a match. */
7745 tree fntype = static_fn_type (fn);
7746 if (same_type_p (target_fn_type, fntype)
7747 || fnptr_conv_p (target_fn_type, fntype))
7748 matches = tree_cons (fn, NULL_TREE, matches);
7751 /* Now, if we've already got a match (or matches), there's no need
7752 to proceed to the template functions. But, if we don't have a
7753 match we need to look at them, too. */
7756 tree target_arg_types;
7757 tree target_ret_type;
7759 unsigned int nargs, ia;
7762 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7763 target_ret_type = TREE_TYPE (target_fn_type);
7765 nargs = list_length (target_arg_types);
7766 args = XALLOCAVEC (tree, nargs);
7767 for (arg = target_arg_types, ia = 0;
7768 arg != NULL_TREE && arg != void_list_node;
7769 arg = TREE_CHAIN (arg), ++ia)
7770 args[ia] = TREE_VALUE (arg);
7773 for (lkp_iterator iter (overload); iter; ++iter)
7779 if (TREE_CODE (fn) != TEMPLATE_DECL)
7780 /* We're only looking for templates. */
7783 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7785 /* We're not looking for a non-static member, and this is
7786 one, or vice versa. */
7789 tree ret = target_ret_type;
7791 /* If the template has a deduced return type, don't expose it to
7792 template argument deduction. */
7793 if (undeduced_auto_decl (fn))
7796 /* Try to do argument deduction. */
7797 targs = make_tree_vec (DECL_NTPARMS (fn));
7798 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7800 DEDUCE_EXACT, LOOKUP_NORMAL,
7802 if (instantiation == error_mark_node)
7803 /* Instantiation failed. */
7806 /* Constraints must be satisfied. This is done before
7807 return type deduction since that instantiates the
7809 if (flag_concepts && !constraints_satisfied_p (instantiation))
7812 /* And now force instantiation to do return type deduction. */
7813 if (undeduced_auto_decl (instantiation))
7816 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7819 require_deduced_type (instantiation);
7822 /* In C++17 we need the noexcept-qualifier to compare types. */
7823 if (flag_noexcept_type)
7824 maybe_instantiate_noexcept (instantiation, complain);
7826 /* See if there's a match. */
7827 tree fntype = static_fn_type (instantiation);
7828 if (same_type_p (target_fn_type, fntype)
7829 || fnptr_conv_p (target_fn_type, fntype))
7830 matches = tree_cons (instantiation, fn, matches);
7833 /* Now, remove all but the most specialized of the matches. */
7836 tree match = most_specialized_instantiation (matches);
7838 if (match != error_mark_node)
7839 matches = tree_cons (TREE_PURPOSE (match),
7845 /* Now we should have exactly one function in MATCHES. */
7846 if (matches == NULL_TREE)
7848 /* There were *no* matches. */
7849 if (complain & tf_error)
7851 error ("no matches converting function %qD to type %q#T",
7852 OVL_NAME (overload), target_type);
7854 print_candidates (overload);
7856 return error_mark_node;
7858 else if (TREE_CHAIN (matches))
7860 /* There were too many matches. First check if they're all
7861 the same function. */
7862 tree match = NULL_TREE;
7864 fn = TREE_PURPOSE (matches);
7866 /* For multi-versioned functions, more than one match is just fine and
7867 decls_match will return false as they are different. */
7868 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7869 if (!decls_match (fn, TREE_PURPOSE (match))
7870 && !targetm.target_option.function_versions
7871 (fn, TREE_PURPOSE (match)))
7876 if (complain & tf_error)
7878 error ("converting overloaded function %qD to type %q#T is ambiguous",
7879 OVL_NAME (overload), target_type);
7881 /* Since print_candidates expects the functions in the
7882 TREE_VALUE slot, we flip them here. */
7883 for (match = matches; match; match = TREE_CHAIN (match))
7884 TREE_VALUE (match) = TREE_PURPOSE (match);
7886 print_candidates (matches);
7889 return error_mark_node;
7893 /* Good, exactly one match. Now, convert it to the correct type. */
7894 fn = TREE_PURPOSE (matches);
7896 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7897 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7899 static int explained;
7901 if (!(complain & tf_error))
7902 return error_mark_node;
7904 permerror (input_location, "assuming pointer to member %qD", fn);
7907 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7912 /* If a pointer to a function that is multi-versioned is requested, the
7913 pointer to the dispatcher function is returned instead. This works
7914 well because indirectly calling the function will dispatch the right
7915 function version at run-time. */
7916 if (DECL_FUNCTION_VERSIONED (fn))
7918 fn = get_function_version_dispatcher (fn);
7920 return error_mark_node;
7921 /* Mark all the versions corresponding to the dispatcher as used. */
7922 if (!(complain & tf_conv))
7923 mark_versions_used (fn);
7926 /* If we're doing overload resolution purely for the purpose of
7927 determining conversion sequences, we should not consider the
7928 function used. If this conversion sequence is selected, the
7929 function will be marked as used at this point. */
7930 if (!(complain & tf_conv))
7932 /* Make =delete work with SFINAE. */
7933 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7934 return error_mark_node;
7935 if (!mark_used (fn, complain) && !(complain & tf_error))
7936 return error_mark_node;
7939 /* We could not check access to member functions when this
7940 expression was originally created since we did not know at that
7941 time to which function the expression referred. */
7942 if (DECL_FUNCTION_MEMBER_P (fn))
7944 gcc_assert (access_path);
7945 perform_or_defer_access_check (access_path, fn, fn, complain);
7948 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7949 return cp_build_addr_expr (fn, complain);
7952 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7953 will mark the function as addressed, but here we must do it
7955 cxx_mark_addressable (fn);
7961 /* This function will instantiate the type of the expression given in
7962 RHS to match the type of LHSTYPE. If errors exist, then return
7963 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7964 we complain on errors. If we are not complaining, never modify rhs,
7965 as overload resolution wants to try many possible instantiations, in
7966 the hope that at least one will work.
7968 For non-recursive calls, LHSTYPE should be a function, pointer to
7969 function, or a pointer to member function. */
7972 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7974 tsubst_flags_t complain_in = complain;
7975 tree access_path = NULL_TREE;
7977 complain &= ~tf_ptrmem_ok;
7979 if (lhstype == unknown_type_node)
7981 if (complain & tf_error)
7982 error ("not enough type information");
7983 return error_mark_node;
7986 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7988 tree fntype = non_reference (lhstype);
7989 if (same_type_p (fntype, TREE_TYPE (rhs)))
7991 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
7993 if (flag_ms_extensions
7994 && TYPE_PTRMEMFUNC_P (fntype)
7995 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7996 /* Microsoft allows `A::f' to be resolved to a
7997 pointer-to-member. */
8001 if (complain & tf_error)
8002 error ("cannot convert %qE from type %qT to type %qT",
8003 rhs, TREE_TYPE (rhs), fntype);
8004 return error_mark_node;
8008 /* If we instantiate a template, and it is a A ?: C expression
8009 with omitted B, look through the SAVE_EXPR. */
8010 if (TREE_CODE (rhs) == SAVE_EXPR)
8011 rhs = TREE_OPERAND (rhs, 0);
8013 if (BASELINK_P (rhs))
8015 access_path = BASELINK_ACCESS_BINFO (rhs);
8016 rhs = BASELINK_FUNCTIONS (rhs);
8019 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8020 deduce any type information. */
8021 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8023 if (complain & tf_error)
8024 error ("not enough type information");
8025 return error_mark_node;
8028 /* There are only a few kinds of expressions that may have a type
8029 dependent on overload resolution. */
8030 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8031 || TREE_CODE (rhs) == COMPONENT_REF
8032 || is_overloaded_fn (rhs)
8033 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8035 /* This should really only be used when attempting to distinguish
8036 what sort of a pointer to function we have. For now, any
8037 arithmetic operation which is not supported on pointers
8038 is rejected as an error. */
8040 switch (TREE_CODE (rhs))
8044 tree member = TREE_OPERAND (rhs, 1);
8046 member = instantiate_type (lhstype, member, complain);
8047 if (member != error_mark_node
8048 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8049 /* Do not lose object's side effects. */
8050 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8051 TREE_OPERAND (rhs, 0), member);
8056 rhs = TREE_OPERAND (rhs, 1);
8057 if (BASELINK_P (rhs))
8058 return instantiate_type (lhstype, rhs, complain_in);
8060 /* This can happen if we are forming a pointer-to-member for a
8062 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8066 case TEMPLATE_ID_EXPR:
8068 tree fns = TREE_OPERAND (rhs, 0);
8069 tree args = TREE_OPERAND (rhs, 1);
8072 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8073 /*template_only=*/true,
8080 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8081 /*template_only=*/false,
8082 /*explicit_targs=*/NULL_TREE,
8087 if (PTRMEM_OK_P (rhs))
8088 complain |= tf_ptrmem_ok;
8090 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8094 return error_mark_node;
8099 return error_mark_node;
8102 /* Return the name of the virtual function pointer field
8103 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8104 this may have to look back through base types to find the
8105 ultimate field name. (For single inheritance, these could
8106 all be the same name. Who knows for multiple inheritance). */
8109 get_vfield_name (tree type)
8111 tree binfo, base_binfo;
8113 for (binfo = TYPE_BINFO (type);
8114 BINFO_N_BASE_BINFOS (binfo);
8117 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8119 if (BINFO_VIRTUAL_P (base_binfo)
8120 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8124 type = BINFO_TYPE (binfo);
8125 tree ctor_name = constructor_name (type);
8126 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8127 + IDENTIFIER_LENGTH (ctor_name) + 2);
8128 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8129 return get_identifier (buf);
8132 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8133 according to [class]:
8134 The class-name is also inserted
8135 into the scope of the class itself. For purposes of access checking,
8136 the inserted class name is treated as if it were a public member name. */
8139 build_self_reference (void)
8141 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8142 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8144 DECL_NONLOCAL (value) = 1;
8145 DECL_CONTEXT (value) = current_class_type;
8146 DECL_ARTIFICIAL (value) = 1;
8147 SET_DECL_SELF_REFERENCE_P (value);
8148 set_underlying_type (value);
8150 if (processing_template_decl)
8151 value = push_template_decl (value);
8153 tree saved_cas = current_access_specifier;
8154 current_access_specifier = access_public_node;
8155 finish_member_declaration (value);
8156 current_access_specifier = saved_cas;
8159 /* Returns 1 if TYPE contains only padding bytes. */
8162 is_empty_class (tree type)
8164 if (type == error_mark_node)
8167 if (! CLASS_TYPE_P (type))
8170 return CLASSTYPE_EMPTY_P (type);
8173 /* Returns true if TYPE contains no actual data, just various
8174 possible combinations of empty classes and possibly a vptr. */
8177 is_really_empty_class (tree type)
8179 if (CLASS_TYPE_P (type))
8186 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8187 out, but we'd like to be able to check this before then. */
8188 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8191 for (binfo = TYPE_BINFO (type), i = 0;
8192 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8193 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8195 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8196 if (TREE_CODE (field) == FIELD_DECL
8197 && !DECL_ARTIFICIAL (field)
8198 /* An unnamed bit-field is not a data member. */
8199 && !DECL_UNNAMED_BIT_FIELD (field)
8200 && !is_really_empty_class (TREE_TYPE (field)))
8204 else if (TREE_CODE (type) == ARRAY_TYPE)
8205 return (integer_zerop (array_type_nelts_top (type))
8206 || is_really_empty_class (TREE_TYPE (type)));
8210 /* Note that NAME was looked up while the current class was being
8211 defined and that the result of that lookup was DECL. */
8214 maybe_note_name_used_in_class (tree name, tree decl)
8216 splay_tree names_used;
8218 /* If we're not defining a class, there's nothing to do. */
8219 if (!(innermost_scope_kind() == sk_class
8220 && TYPE_BEING_DEFINED (current_class_type)
8221 && !LAMBDA_TYPE_P (current_class_type)))
8224 /* If there's already a binding for this NAME, then we don't have
8225 anything to worry about. */
8226 if (lookup_member (current_class_type, name,
8227 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8230 if (!current_class_stack[current_class_depth - 1].names_used)
8231 current_class_stack[current_class_depth - 1].names_used
8232 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8233 names_used = current_class_stack[current_class_depth - 1].names_used;
8235 splay_tree_insert (names_used,
8236 (splay_tree_key) name,
8237 (splay_tree_value) decl);
8240 /* Note that NAME was declared (as DECL) in the current class. Check
8241 to see that the declaration is valid. */
8244 note_name_declared_in_class (tree name, tree decl)
8246 splay_tree names_used;
8249 /* Look to see if we ever used this name. */
8251 = current_class_stack[current_class_depth - 1].names_used;
8254 /* The C language allows members to be declared with a type of the same
8255 name, and the C++ standard says this diagnostic is not required. So
8256 allow it in extern "C" blocks unless predantic is specified.
8257 Allow it in all cases if -ms-extensions is specified. */
8258 if ((!pedantic && current_lang_name == lang_name_c)
8259 || flag_ms_extensions)
8261 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8264 /* [basic.scope.class]
8266 A name N used in a class S shall refer to the same declaration
8267 in its context and when re-evaluated in the completed scope of
8269 permerror (input_location, "declaration of %q#D", decl);
8270 permerror (location_of ((tree) n->value),
8271 "changes meaning of %qD from %q#D",
8272 OVL_NAME (decl), (tree) n->value);
8276 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8277 Secondary vtables are merged with primary vtables; this function
8278 will return the VAR_DECL for the primary vtable. */
8281 get_vtbl_decl_for_binfo (tree binfo)
8285 decl = BINFO_VTABLE (binfo);
8286 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8288 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8289 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8292 gcc_assert (VAR_P (decl));
8297 /* Returns the binfo for the primary base of BINFO. If the resulting
8298 BINFO is a virtual base, and it is inherited elsewhere in the
8299 hierarchy, then the returned binfo might not be the primary base of
8300 BINFO in the complete object. Check BINFO_PRIMARY_P or
8301 BINFO_LOST_PRIMARY_P to be sure. */
8304 get_primary_binfo (tree binfo)
8308 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8312 return copied_binfo (primary_base, binfo);
8315 /* As above, but iterate until we reach the binfo that actually provides the
8319 most_primary_binfo (tree binfo)
8322 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8323 && !BINFO_LOST_PRIMARY_P (b))
8325 tree primary_base = get_primary_binfo (b);
8326 gcc_assert (BINFO_PRIMARY_P (primary_base)
8327 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8333 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8334 type. Note that the virtual inheritance might be above or below BINFO in
8338 vptr_via_virtual_p (tree binfo)
8341 binfo = TYPE_BINFO (binfo);
8342 tree primary = most_primary_binfo (binfo);
8343 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8344 a morally virtual base. */
8345 tree virt = binfo_via_virtual (primary, NULL_TREE);
8346 return virt != NULL_TREE;
8349 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8352 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8355 fprintf (stream, "%*s", indent, "");
8359 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8360 INDENT should be zero when called from the top level; it is
8361 incremented recursively. IGO indicates the next expected BINFO in
8362 inheritance graph ordering. */
8365 dump_class_hierarchy_r (FILE *stream,
8375 indented = maybe_indent_hierarchy (stream, indent, 0);
8376 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8377 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8378 (HOST_WIDE_INT) (uintptr_t) binfo);
8381 fprintf (stream, "alternative-path\n");
8384 igo = TREE_CHAIN (binfo);
8386 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8387 tree_to_shwi (BINFO_OFFSET (binfo)));
8388 if (is_empty_class (BINFO_TYPE (binfo)))
8389 fprintf (stream, " empty");
8390 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8391 fprintf (stream, " nearly-empty");
8392 if (BINFO_VIRTUAL_P (binfo))
8393 fprintf (stream, " virtual");
8394 fprintf (stream, "\n");
8397 if (BINFO_PRIMARY_P (binfo))
8399 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8400 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8401 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8402 TFF_PLAIN_IDENTIFIER),
8403 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8405 if (BINFO_LOST_PRIMARY_P (binfo))
8407 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8408 fprintf (stream, " lost-primary");
8411 fprintf (stream, "\n");
8413 if (!(flags & TDF_SLIM))
8417 if (BINFO_SUBVTT_INDEX (binfo))
8419 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8420 fprintf (stream, " subvttidx=%s",
8421 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8422 TFF_PLAIN_IDENTIFIER));
8424 if (BINFO_VPTR_INDEX (binfo))
8426 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8427 fprintf (stream, " vptridx=%s",
8428 expr_as_string (BINFO_VPTR_INDEX (binfo),
8429 TFF_PLAIN_IDENTIFIER));
8431 if (BINFO_VPTR_FIELD (binfo))
8433 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8434 fprintf (stream, " vbaseoffset=%s",
8435 expr_as_string (BINFO_VPTR_FIELD (binfo),
8436 TFF_PLAIN_IDENTIFIER));
8438 if (BINFO_VTABLE (binfo))
8440 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8441 fprintf (stream, " vptr=%s",
8442 expr_as_string (BINFO_VTABLE (binfo),
8443 TFF_PLAIN_IDENTIFIER));
8447 fprintf (stream, "\n");
8450 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8451 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8456 /* Dump the BINFO hierarchy for T. */
8459 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8461 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8462 fprintf (stream, " size=%lu align=%lu\n",
8463 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8464 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8465 fprintf (stream, " base size=%lu base align=%lu\n",
8466 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8468 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8470 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8471 fprintf (stream, "\n");
8474 /* Debug interface to hierarchy dumping. */
8477 debug_class (tree t)
8479 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8483 dump_class_hierarchy (tree t)
8486 if (FILE *stream = dump_begin (class_dump_id, &flags))
8488 dump_class_hierarchy_1 (stream, flags, t);
8489 dump_end (class_dump_id, stream);
8494 dump_array (FILE * stream, tree decl)
8497 unsigned HOST_WIDE_INT ix;
8499 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8501 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8503 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8504 fprintf (stream, " %s entries",
8505 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8506 TFF_PLAIN_IDENTIFIER));
8507 fprintf (stream, "\n");
8509 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8511 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8512 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8516 dump_vtable (tree t, tree binfo, tree vtable)
8519 FILE *stream = dump_begin (class_dump_id, &flags);
8524 if (!(flags & TDF_SLIM))
8526 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8528 fprintf (stream, "%s for %s",
8529 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8530 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8533 if (!BINFO_VIRTUAL_P (binfo))
8534 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8535 (HOST_WIDE_INT) (uintptr_t) binfo);
8536 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8538 fprintf (stream, "\n");
8539 dump_array (stream, vtable);
8540 fprintf (stream, "\n");
8543 dump_end (class_dump_id, stream);
8547 dump_vtt (tree t, tree vtt)
8550 FILE *stream = dump_begin (class_dump_id, &flags);
8555 if (!(flags & TDF_SLIM))
8557 fprintf (stream, "VTT for %s\n",
8558 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8559 dump_array (stream, vtt);
8560 fprintf (stream, "\n");
8563 dump_end (class_dump_id, stream);
8566 /* Dump a function or thunk and its thunkees. */
8569 dump_thunk (FILE *stream, int indent, tree thunk)
8571 static const char spaces[] = " ";
8572 tree name = DECL_NAME (thunk);
8575 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8577 !DECL_THUNK_P (thunk) ? "function"
8578 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8579 name ? IDENTIFIER_POINTER (name) : "<unset>");
8580 if (DECL_THUNK_P (thunk))
8582 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8583 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8585 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8586 if (!virtual_adjust)
8588 else if (DECL_THIS_THUNK_P (thunk))
8589 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8590 tree_to_shwi (virtual_adjust));
8592 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8593 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8594 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8595 if (THUNK_ALIAS (thunk))
8596 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8598 fprintf (stream, "\n");
8599 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8600 dump_thunk (stream, indent + 2, thunks);
8603 /* Dump the thunks for FN. */
8606 debug_thunks (tree fn)
8608 dump_thunk (stderr, 0, fn);
8611 /* Virtual function table initialization. */
8613 /* Create all the necessary vtables for T and its base classes. */
8616 finish_vtbls (tree t)
8619 vec<constructor_elt, va_gc> *v = NULL;
8620 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8622 /* We lay out the primary and secondary vtables in one contiguous
8623 vtable. The primary vtable is first, followed by the non-virtual
8624 secondary vtables in inheritance graph order. */
8625 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8628 /* Then come the virtual bases, also in inheritance graph order. */
8629 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8631 if (!BINFO_VIRTUAL_P (vbase))
8633 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8636 if (BINFO_VTABLE (TYPE_BINFO (t)))
8637 initialize_vtable (TYPE_BINFO (t), v);
8640 /* Initialize the vtable for BINFO with the INITS. */
8643 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8647 layout_vtable_decl (binfo, vec_safe_length (inits));
8648 decl = get_vtbl_decl_for_binfo (binfo);
8649 initialize_artificial_var (decl, inits);
8650 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8653 /* Build the VTT (virtual table table) for T.
8654 A class requires a VTT if it has virtual bases.
8657 1 - primary virtual pointer for complete object T
8658 2 - secondary VTTs for each direct non-virtual base of T which requires a
8660 3 - secondary virtual pointers for each direct or indirect base of T which
8661 has virtual bases or is reachable via a virtual path from T.
8662 4 - secondary VTTs for each direct or indirect virtual base of T.
8664 Secondary VTTs look like complete object VTTs without part 4. */
8672 vec<constructor_elt, va_gc> *inits;
8674 /* Build up the initializers for the VTT. */
8676 index = size_zero_node;
8677 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8679 /* If we didn't need a VTT, we're done. */
8683 /* Figure out the type of the VTT. */
8684 type = build_array_of_n_type (const_ptr_type_node,
8687 /* Now, build the VTT object itself. */
8688 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8689 initialize_artificial_var (vtt, inits);
8690 /* Add the VTT to the vtables list. */
8691 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8692 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8697 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8698 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8699 and CHAIN the vtable pointer for this binfo after construction is
8700 complete. VALUE can also be another BINFO, in which case we recurse. */
8703 binfo_ctor_vtable (tree binfo)
8709 vt = BINFO_VTABLE (binfo);
8710 if (TREE_CODE (vt) == TREE_LIST)
8711 vt = TREE_VALUE (vt);
8712 if (TREE_CODE (vt) == TREE_BINFO)
8721 /* Data for secondary VTT initialization. */
8722 struct secondary_vptr_vtt_init_data
8724 /* Is this the primary VTT? */
8727 /* Current index into the VTT. */
8730 /* Vector of initializers built up. */
8731 vec<constructor_elt, va_gc> *inits;
8733 /* The type being constructed by this secondary VTT. */
8734 tree type_being_constructed;
8737 /* Recursively build the VTT-initializer for BINFO (which is in the
8738 hierarchy dominated by T). INITS points to the end of the initializer
8739 list to date. INDEX is the VTT index where the next element will be
8740 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8741 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8742 for virtual bases of T. When it is not so, we build the constructor
8743 vtables for the BINFO-in-T variant. */
8746 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8752 secondary_vptr_vtt_init_data data;
8753 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8755 /* We only need VTTs for subobjects with virtual bases. */
8756 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8759 /* We need to use a construction vtable if this is not the primary
8763 build_ctor_vtbl_group (binfo, t);
8765 /* Record the offset in the VTT where this sub-VTT can be found. */
8766 BINFO_SUBVTT_INDEX (binfo) = *index;
8769 /* Add the address of the primary vtable for the complete object. */
8770 init = binfo_ctor_vtable (binfo);
8771 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8774 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8775 BINFO_VPTR_INDEX (binfo) = *index;
8777 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8779 /* Recursively add the secondary VTTs for non-virtual bases. */
8780 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8781 if (!BINFO_VIRTUAL_P (b))
8782 build_vtt_inits (b, t, inits, index);
8784 /* Add secondary virtual pointers for all subobjects of BINFO with
8785 either virtual bases or reachable along a virtual path, except
8786 subobjects that are non-virtual primary bases. */
8787 data.top_level_p = top_level_p;
8788 data.index = *index;
8789 data.inits = *inits;
8790 data.type_being_constructed = BINFO_TYPE (binfo);
8792 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8794 *index = data.index;
8796 /* data.inits might have grown as we added secondary virtual pointers.
8797 Make sure our caller knows about the new vector. */
8798 *inits = data.inits;
8801 /* Add the secondary VTTs for virtual bases in inheritance graph
8803 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8805 if (!BINFO_VIRTUAL_P (b))
8808 build_vtt_inits (b, t, inits, index);
8811 /* Remove the ctor vtables we created. */
8812 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8815 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8816 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8819 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8821 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8823 /* We don't care about bases that don't have vtables. */
8824 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8825 return dfs_skip_bases;
8827 /* We're only interested in proper subobjects of the type being
8829 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8832 /* We're only interested in bases with virtual bases or reachable
8833 via a virtual path from the type being constructed. */
8834 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8835 || binfo_via_virtual (binfo, data->type_being_constructed)))
8836 return dfs_skip_bases;
8838 /* We're not interested in non-virtual primary bases. */
8839 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8842 /* Record the index where this secondary vptr can be found. */
8843 if (data->top_level_p)
8845 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8846 BINFO_VPTR_INDEX (binfo) = data->index;
8848 if (BINFO_VIRTUAL_P (binfo))
8850 /* It's a primary virtual base, and this is not a
8851 construction vtable. Find the base this is primary of in
8852 the inheritance graph, and use that base's vtable
8854 while (BINFO_PRIMARY_P (binfo))
8855 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8859 /* Add the initializer for the secondary vptr itself. */
8860 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8862 /* Advance the vtt index. */
8863 data->index = size_binop (PLUS_EXPR, data->index,
8864 TYPE_SIZE_UNIT (ptr_type_node));
8869 /* Called from build_vtt_inits via dfs_walk. After building
8870 constructor vtables and generating the sub-vtt from them, we need
8871 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8872 binfo of the base whose sub vtt was generated. */
8875 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8877 tree vtable = BINFO_VTABLE (binfo);
8879 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8880 /* If this class has no vtable, none of its bases do. */
8881 return dfs_skip_bases;
8884 /* This might be a primary base, so have no vtable in this
8888 /* If we scribbled the construction vtable vptr into BINFO, clear it
8890 if (TREE_CODE (vtable) == TREE_LIST
8891 && (TREE_PURPOSE (vtable) == (tree) data))
8892 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8897 /* Build the construction vtable group for BINFO which is in the
8898 hierarchy dominated by T. */
8901 build_ctor_vtbl_group (tree binfo, tree t)
8907 vec<constructor_elt, va_gc> *v;
8909 /* See if we've already created this construction vtable group. */
8910 id = mangle_ctor_vtbl_for_type (t, binfo);
8911 if (get_global_binding (id))
8914 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8915 /* Build a version of VTBL (with the wrong type) for use in
8916 constructing the addresses of secondary vtables in the
8917 construction vtable group. */
8918 vtbl = build_vtable (t, id, ptr_type_node);
8919 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8920 /* Don't export construction vtables from shared libraries. Even on
8921 targets that don't support hidden visibility, this tells
8922 can_refer_decl_in_current_unit_p not to assume that it's safe to
8923 access from a different compilation unit (bz 54314). */
8924 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8925 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8928 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8929 binfo, vtbl, t, &v);
8931 /* Add the vtables for each of our virtual bases using the vbase in T
8933 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8935 vbase = TREE_CHAIN (vbase))
8939 if (!BINFO_VIRTUAL_P (vbase))
8941 b = copied_binfo (vbase, binfo);
8943 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8946 /* Figure out the type of the construction vtable. */
8947 type = build_array_of_n_type (vtable_entry_type, v->length ());
8949 TREE_TYPE (vtbl) = type;
8950 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8951 layout_decl (vtbl, 0);
8953 /* Initialize the construction vtable. */
8954 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8955 initialize_artificial_var (vtbl, v);
8956 dump_vtable (t, binfo, vtbl);
8959 /* Add the vtbl initializers for BINFO (and its bases other than
8960 non-virtual primaries) to the list of INITS. BINFO is in the
8961 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8962 the constructor the vtbl inits should be accumulated for. (If this
8963 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8964 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8965 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8966 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8967 but are not necessarily the same in terms of layout. */
8970 accumulate_vtbl_inits (tree binfo,
8975 vec<constructor_elt, va_gc> **inits)
8979 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8981 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8983 /* If it doesn't have a vptr, we don't do anything. */
8984 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8987 /* If we're building a construction vtable, we're not interested in
8988 subobjects that don't require construction vtables. */
8990 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8991 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8994 /* Build the initializers for the BINFO-in-T vtable. */
8995 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8997 /* Walk the BINFO and its bases. We walk in preorder so that as we
8998 initialize each vtable we can figure out at what offset the
8999 secondary vtable lies from the primary vtable. We can't use
9000 dfs_walk here because we need to iterate through bases of BINFO
9001 and RTTI_BINFO simultaneously. */
9002 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9004 /* Skip virtual bases. */
9005 if (BINFO_VIRTUAL_P (base_binfo))
9007 accumulate_vtbl_inits (base_binfo,
9008 BINFO_BASE_BINFO (orig_binfo, i),
9009 rtti_binfo, vtbl, t,
9014 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9015 BINFO vtable to L. */
9018 dfs_accumulate_vtbl_inits (tree binfo,
9023 vec<constructor_elt, va_gc> **l)
9025 tree vtbl = NULL_TREE;
9026 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9030 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9032 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9033 primary virtual base. If it is not the same primary in
9034 the hierarchy of T, we'll need to generate a ctor vtable
9035 for it, to place at its location in T. If it is the same
9036 primary, we still need a VTT entry for the vtable, but it
9037 should point to the ctor vtable for the base it is a
9038 primary for within the sub-hierarchy of RTTI_BINFO.
9040 There are three possible cases:
9042 1) We are in the same place.
9043 2) We are a primary base within a lost primary virtual base of
9045 3) We are primary to something not a base of RTTI_BINFO. */
9048 tree last = NULL_TREE;
9050 /* First, look through the bases we are primary to for RTTI_BINFO
9051 or a virtual base. */
9053 while (BINFO_PRIMARY_P (b))
9055 b = BINFO_INHERITANCE_CHAIN (b);
9057 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9060 /* If we run out of primary links, keep looking down our
9061 inheritance chain; we might be an indirect primary. */
9062 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9063 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9067 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9068 base B and it is a base of RTTI_BINFO, this is case 2. In
9069 either case, we share our vtable with LAST, i.e. the
9070 derived-most base within B of which we are a primary. */
9072 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9073 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9074 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9075 binfo_ctor_vtable after everything's been set up. */
9078 /* Otherwise, this is case 3 and we get our own. */
9080 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9083 n_inits = vec_safe_length (*l);
9090 /* Add the initializer for this vtable. */
9091 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9092 &non_fn_entries, l);
9094 /* Figure out the position to which the VPTR should point. */
9095 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9096 index = size_binop (MULT_EXPR,
9097 TYPE_SIZE_UNIT (vtable_entry_type),
9098 size_int (non_fn_entries + n_inits));
9099 vtbl = fold_build_pointer_plus (vtbl, index);
9103 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9104 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9105 straighten this out. */
9106 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9107 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9108 /* Throw away any unneeded intializers. */
9109 (*l)->truncate (n_inits);
9111 /* For an ordinary vtable, set BINFO_VTABLE. */
9112 BINFO_VTABLE (binfo) = vtbl;
9115 static GTY(()) tree abort_fndecl_addr;
9116 static GTY(()) tree dvirt_fn;
9118 /* Construct the initializer for BINFO's virtual function table. BINFO
9119 is part of the hierarchy dominated by T. If we're building a
9120 construction vtable, the ORIG_BINFO is the binfo we should use to
9121 find the actual function pointers to put in the vtable - but they
9122 can be overridden on the path to most-derived in the graph that
9123 ORIG_BINFO belongs. Otherwise,
9124 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9125 BINFO that should be indicated by the RTTI information in the
9126 vtable; it will be a base class of T, rather than T itself, if we
9127 are building a construction vtable.
9129 The value returned is a TREE_LIST suitable for wrapping in a
9130 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9131 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9132 number of non-function entries in the vtable.
9134 It might seem that this function should never be called with a
9135 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9136 base is always subsumed by a derived class vtable. However, when
9137 we are building construction vtables, we do build vtables for
9138 primary bases; we need these while the primary base is being
9142 build_vtbl_initializer (tree binfo,
9146 int* non_fn_entries_p,
9147 vec<constructor_elt, va_gc> **inits)
9153 vec<tree, va_gc> *vbases;
9156 /* Initialize VID. */
9157 memset (&vid, 0, sizeof (vid));
9160 vid.rtti_binfo = rtti_binfo;
9161 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9162 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9163 vid.generate_vcall_entries = true;
9164 /* The first vbase or vcall offset is at index -3 in the vtable. */
9165 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9167 /* Add entries to the vtable for RTTI. */
9168 build_rtti_vtbl_entries (binfo, &vid);
9170 /* Create an array for keeping track of the functions we've
9171 processed. When we see multiple functions with the same
9172 signature, we share the vcall offsets. */
9173 vec_alloc (vid.fns, 32);
9174 /* Add the vcall and vbase offset entries. */
9175 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9177 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9178 build_vbase_offset_vtbl_entries. */
9179 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9180 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9181 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9183 /* If the target requires padding between data entries, add that now. */
9184 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9186 int n_entries = vec_safe_length (vid.inits);
9188 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9190 /* Move data entries into their new positions and add padding
9191 after the new positions. Iterate backwards so we don't
9192 overwrite entries that we would need to process later. */
9193 for (ix = n_entries - 1;
9194 vid.inits->iterate (ix, &e);
9198 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9199 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9201 (*vid.inits)[new_position] = *e;
9203 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9205 constructor_elt *f = &(*vid.inits)[new_position - j];
9206 f->index = NULL_TREE;
9207 f->value = build1 (NOP_EXPR, vtable_entry_type,
9213 if (non_fn_entries_p)
9214 *non_fn_entries_p = vec_safe_length (vid.inits);
9216 /* The initializers for virtual functions were built up in reverse
9217 order. Straighten them out and add them to the running list in one
9219 jx = vec_safe_length (*inits);
9220 vec_safe_grow (*inits, jx + vid.inits->length ());
9222 for (ix = vid.inits->length () - 1;
9223 vid.inits->iterate (ix, &e);
9227 /* Go through all the ordinary virtual functions, building up
9229 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9233 tree fn, fn_original;
9234 tree init = NULL_TREE;
9238 if (DECL_THUNK_P (fn))
9240 if (!DECL_NAME (fn))
9242 if (THUNK_ALIAS (fn))
9244 fn = THUNK_ALIAS (fn);
9247 fn_original = THUNK_TARGET (fn);
9250 /* If the only definition of this function signature along our
9251 primary base chain is from a lost primary, this vtable slot will
9252 never be used, so just zero it out. This is important to avoid
9253 requiring extra thunks which cannot be generated with the function.
9255 We first check this in update_vtable_entry_for_fn, so we handle
9256 restored primary bases properly; we also need to do it here so we
9257 zero out unused slots in ctor vtables, rather than filling them
9258 with erroneous values (though harmless, apart from relocation
9260 if (BV_LOST_PRIMARY (v))
9261 init = size_zero_node;
9265 /* Pull the offset for `this', and the function to call, out of
9267 delta = BV_DELTA (v);
9268 vcall_index = BV_VCALL_INDEX (v);
9270 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9271 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9273 /* You can't call an abstract virtual function; it's abstract.
9274 So, we replace these functions with __pure_virtual. */
9275 if (DECL_PURE_VIRTUAL_P (fn_original))
9278 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9280 if (abort_fndecl_addr == NULL)
9282 = fold_convert (vfunc_ptr_type_node,
9283 build_fold_addr_expr (fn));
9284 init = abort_fndecl_addr;
9287 /* Likewise for deleted virtuals. */
9288 else if (DECL_DELETED_FN (fn_original))
9292 tree name = get_identifier ("__cxa_deleted_virtual");
9293 dvirt_fn = get_global_binding (name);
9295 dvirt_fn = push_library_fn
9297 build_function_type_list (void_type_node, NULL_TREE),
9298 NULL_TREE, ECF_NORETURN | ECF_COLD);
9301 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9302 init = fold_convert (vfunc_ptr_type_node,
9303 build_fold_addr_expr (fn));
9307 if (!integer_zerop (delta) || vcall_index)
9309 fn = make_thunk (fn, /*this_adjusting=*/1,
9310 delta, vcall_index);
9311 if (!DECL_NAME (fn))
9314 /* Take the address of the function, considering it to be of an
9315 appropriate generic type. */
9316 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9317 init = fold_convert (vfunc_ptr_type_node,
9318 build_fold_addr_expr (fn));
9319 /* Don't refer to a virtual destructor from a constructor
9320 vtable or a vtable for an abstract class, since destroying
9321 an object under construction is undefined behavior and we
9322 don't want it to be considered a candidate for speculative
9323 devirtualization. But do create the thunk for ABI
9325 if (DECL_DESTRUCTOR_P (fn_original)
9326 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9327 || orig_binfo != binfo))
9328 init = size_zero_node;
9332 /* And add it to the chain of initializers. */
9333 if (TARGET_VTABLE_USES_DESCRIPTORS)
9336 if (init == size_zero_node)
9337 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9338 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9340 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9342 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9343 fn, build_int_cst (NULL_TREE, i));
9344 TREE_CONSTANT (fdesc) = 1;
9346 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9350 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9354 /* Adds to vid->inits the initializers for the vbase and vcall
9355 offsets in BINFO, which is in the hierarchy dominated by T. */
9358 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9362 /* If this is a derived class, we must first create entries
9363 corresponding to the primary base class. */
9364 b = get_primary_binfo (binfo);
9366 build_vcall_and_vbase_vtbl_entries (b, vid);
9368 /* Add the vbase entries for this base. */
9369 build_vbase_offset_vtbl_entries (binfo, vid);
9370 /* Add the vcall entries for this base. */
9371 build_vcall_offset_vtbl_entries (binfo, vid);
9374 /* Returns the initializers for the vbase offset entries in the vtable
9375 for BINFO (which is part of the class hierarchy dominated by T), in
9376 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9377 where the next vbase offset will go. */
9380 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9384 tree non_primary_binfo;
9386 /* If there are no virtual baseclasses, then there is nothing to
9388 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9393 /* We might be a primary base class. Go up the inheritance hierarchy
9394 until we find the most derived class of which we are a primary base:
9395 it is the offset of that which we need to use. */
9396 non_primary_binfo = binfo;
9397 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9401 /* If we have reached a virtual base, then it must be a primary
9402 base (possibly multi-level) of vid->binfo, or we wouldn't
9403 have called build_vcall_and_vbase_vtbl_entries for it. But it
9404 might be a lost primary, so just skip down to vid->binfo. */
9405 if (BINFO_VIRTUAL_P (non_primary_binfo))
9407 non_primary_binfo = vid->binfo;
9411 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9412 if (get_primary_binfo (b) != non_primary_binfo)
9414 non_primary_binfo = b;
9417 /* Go through the virtual bases, adding the offsets. */
9418 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9420 vbase = TREE_CHAIN (vbase))
9425 if (!BINFO_VIRTUAL_P (vbase))
9428 /* Find the instance of this virtual base in the complete
9430 b = copied_binfo (vbase, binfo);
9432 /* If we've already got an offset for this virtual base, we
9433 don't need another one. */
9434 if (BINFO_VTABLE_PATH_MARKED (b))
9436 BINFO_VTABLE_PATH_MARKED (b) = 1;
9438 /* Figure out where we can find this vbase offset. */
9439 delta = size_binop (MULT_EXPR,
9441 fold_convert (ssizetype,
9442 TYPE_SIZE_UNIT (vtable_entry_type)));
9443 if (vid->primary_vtbl_p)
9444 BINFO_VPTR_FIELD (b) = delta;
9446 if (binfo != TYPE_BINFO (t))
9447 /* The vbase offset had better be the same. */
9448 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9450 /* The next vbase will come at a more negative offset. */
9451 vid->index = size_binop (MINUS_EXPR, vid->index,
9452 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9454 /* The initializer is the delta from BINFO to this virtual base.
9455 The vbase offsets go in reverse inheritance-graph order, and
9456 we are walking in inheritance graph order so these end up in
9458 delta = size_diffop_loc (input_location,
9459 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9461 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9462 fold_build1_loc (input_location, NOP_EXPR,
9463 vtable_entry_type, delta));
9467 /* Adds the initializers for the vcall offset entries in the vtable
9468 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9472 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9474 /* We only need these entries if this base is a virtual base. We
9475 compute the indices -- but do not add to the vtable -- when
9476 building the main vtable for a class. */
9477 if (binfo == TYPE_BINFO (vid->derived)
9478 || (BINFO_VIRTUAL_P (binfo)
9479 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9480 correspond to VID->DERIVED), we are building a primary
9481 construction virtual table. Since this is a primary
9482 virtual table, we do not need the vcall offsets for
9484 && binfo != vid->rtti_binfo))
9486 /* We need a vcall offset for each of the virtual functions in this
9487 vtable. For example:
9489 class A { virtual void f (); };
9490 class B1 : virtual public A { virtual void f (); };
9491 class B2 : virtual public A { virtual void f (); };
9492 class C: public B1, public B2 { virtual void f (); };
9494 A C object has a primary base of B1, which has a primary base of A. A
9495 C also has a secondary base of B2, which no longer has a primary base
9496 of A. So the B2-in-C construction vtable needs a secondary vtable for
9497 A, which will adjust the A* to a B2* to call f. We have no way of
9498 knowing what (or even whether) this offset will be when we define B2,
9499 so we store this "vcall offset" in the A sub-vtable and look it up in
9500 a "virtual thunk" for B2::f.
9502 We need entries for all the functions in our primary vtable and
9503 in our non-virtual bases' secondary vtables. */
9505 /* If we are just computing the vcall indices -- but do not need
9506 the actual entries -- not that. */
9507 if (!BINFO_VIRTUAL_P (binfo))
9508 vid->generate_vcall_entries = false;
9509 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9510 add_vcall_offset_vtbl_entries_r (binfo, vid);
9514 /* Build vcall offsets, starting with those for BINFO. */
9517 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9523 /* Don't walk into virtual bases -- except, of course, for the
9524 virtual base for which we are building vcall offsets. Any
9525 primary virtual base will have already had its offsets generated
9526 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9527 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9530 /* If BINFO has a primary base, process it first. */
9531 primary_binfo = get_primary_binfo (binfo);
9533 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9535 /* Add BINFO itself to the list. */
9536 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9538 /* Scan the non-primary bases of BINFO. */
9539 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9540 if (base_binfo != primary_binfo)
9541 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9544 /* Called from build_vcall_offset_vtbl_entries_r. */
9547 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9549 /* Make entries for the rest of the virtuals. */
9552 /* The ABI requires that the methods be processed in declaration
9554 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9556 orig_fn = DECL_CHAIN (orig_fn))
9557 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9558 add_vcall_offset (orig_fn, binfo, vid);
9561 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9564 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9570 /* If there is already an entry for a function with the same
9571 signature as FN, then we do not need a second vcall offset.
9572 Check the list of functions already present in the derived
9574 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9576 if (same_signature_p (derived_entry, orig_fn)
9577 /* We only use one vcall offset for virtual destructors,
9578 even though there are two virtual table entries. */
9579 || (DECL_DESTRUCTOR_P (derived_entry)
9580 && DECL_DESTRUCTOR_P (orig_fn)))
9584 /* If we are building these vcall offsets as part of building
9585 the vtable for the most derived class, remember the vcall
9587 if (vid->binfo == TYPE_BINFO (vid->derived))
9589 tree_pair_s elt = {orig_fn, vid->index};
9590 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9593 /* The next vcall offset will be found at a more negative
9595 vid->index = size_binop (MINUS_EXPR, vid->index,
9596 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9598 /* Keep track of this function. */
9599 vec_safe_push (vid->fns, orig_fn);
9601 if (vid->generate_vcall_entries)
9606 /* Find the overriding function. */
9607 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9608 if (fn == error_mark_node)
9609 vcall_offset = build_zero_cst (vtable_entry_type);
9612 base = TREE_VALUE (fn);
9614 /* The vbase we're working on is a primary base of
9615 vid->binfo. But it might be a lost primary, so its
9616 BINFO_OFFSET might be wrong, so we just use the
9617 BINFO_OFFSET from vid->binfo. */
9618 vcall_offset = size_diffop_loc (input_location,
9619 BINFO_OFFSET (base),
9620 BINFO_OFFSET (vid->binfo));
9621 vcall_offset = fold_build1_loc (input_location,
9622 NOP_EXPR, vtable_entry_type,
9625 /* Add the initializer to the vtable. */
9626 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9630 /* Return vtbl initializers for the RTTI entries corresponding to the
9631 BINFO's vtable. The RTTI entries should indicate the object given
9632 by VID->rtti_binfo. */
9635 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9643 t = BINFO_TYPE (vid->rtti_binfo);
9645 /* To find the complete object, we will first convert to our most
9646 primary base, and then add the offset in the vtbl to that value. */
9647 b = most_primary_binfo (binfo);
9648 offset = size_diffop_loc (input_location,
9649 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9651 /* The second entry is the address of the typeinfo object. */
9653 decl = build_address (get_tinfo_decl (t));
9655 decl = integer_zero_node;
9657 /* Convert the declaration to a type that can be stored in the
9659 init = build_nop (vfunc_ptr_type_node, decl);
9660 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9662 /* Add the offset-to-top entry. It comes earlier in the vtable than
9663 the typeinfo entry. Convert the offset to look like a
9664 function pointer, so that we can put it in the vtable. */
9665 init = build_nop (vfunc_ptr_type_node, offset);
9666 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9669 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9673 uniquely_derived_from_p (tree parent, tree type)
9675 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9676 return base && base != error_mark_node;
9679 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9682 publicly_uniquely_derived_p (tree parent, tree type)
9684 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9686 return base && base != error_mark_node;
9689 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9690 class between them, if any. */
9693 common_enclosing_class (tree ctx1, tree ctx2)
9695 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9697 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9698 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9701 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9702 TYPE_MARKED_P (t) = true;
9703 tree found = NULL_TREE;
9704 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9705 if (TYPE_MARKED_P (t))
9710 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9711 TYPE_MARKED_P (t) = false;
9715 #include "gt-cp-class.h"