Daily bump.
[official-gcc.git] / gcc / cp / class.c
blobf16e50b9de9d7dba8ebfffbdaf69365cbb09ff2c
1 /* Functions related to building -*- C++ -*- classes and their related objects.
2 Copyright (C) 1987-2021 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)
10 any later version.
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. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "convert.h"
36 #include "dumpfile.h"
37 #include "gimplify.h"
38 #include "intl.h"
39 #include "asan.h"
41 /* Id for dumping the class hierarchy. */
42 int class_dump_id;
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. */
55 tree name;
57 /* The _TYPE node for the class. */
58 tree type;
60 /* The access specifier pending for new declarations in the scope of
61 this class. */
62 tree access;
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
68 push_to_top_level. */
69 size_t hidden;
70 }* class_stack_node_t;
72 struct vtbl_init_data
74 /* The base for which we're building initializers. */
75 tree binfo;
76 /* The type of the most-derived type. */
77 tree derived;
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
80 tree rtti_binfo;
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. */
86 tree vbase;
87 /* The functions in vbase for which we have already provided vcall
88 offsets. */
89 vec<tree, va_gc> *fns;
90 /* The vtable index of the next vcall or vbase offset. */
91 tree index;
92 /* Nonzero if we are building the initializer for the primary
93 vtable. */
94 int primary_vtbl_p;
95 /* Nonzero if we are building the initializer for a construction
96 vtable. */
97 int ctor_vtbl_p;
98 /* True when adding vcall offset entries to the vtable. False when
99 merely computing the indices. */
100 bool generate_vcall_entries;
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
106 /* The stack itself. This is a dynamically resized array. The
107 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 static int current_class_stack_size;
109 static class_stack_node_t current_class_stack;
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
114 static tree get_vfield_name (tree);
115 static void finish_struct_anon (tree);
116 static tree get_vtable_name (tree);
117 static void get_basefndecls (tree, tree, vec<tree> *);
118 static int build_primary_vtable (tree, tree);
119 static int build_secondary_vtable (tree);
120 static void finish_vtbls (tree);
121 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
122 static void finish_struct_bits (tree);
123 static int alter_access (tree, tree, tree);
124 static void handle_using_decl (tree, tree);
125 static tree dfs_modify_vtables (tree, void *);
126 static tree modify_all_vtables (tree, tree);
127 static void determine_primary_bases (tree);
128 static void maybe_warn_about_overly_private_class (tree);
129 static void add_implicitly_declared_members (tree, tree*, int, int);
130 static tree fixed_type_or_null (tree, int *, int *);
131 static tree build_simple_base_path (tree expr, tree binfo);
132 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
133 vec<constructor_elt, va_gc> **);
134 static bool check_bitfield_decl (tree);
135 static bool check_field_decl (tree, tree, int *, int *);
136 static void check_field_decls (tree, tree *, int *, int *);
137 static void build_base_fields (record_layout_info, splay_tree, tree *);
138 static void check_methods (tree);
139 static bool accessible_nvdtor_p (tree);
141 /* Used by find_flexarrays and related functions. */
142 struct flexmems_t;
143 static void diagnose_flexarrays (tree, const flexmems_t *);
144 static void find_flexarrays (tree, flexmems_t *, bool = false,
145 tree = NULL_TREE, tree = NULL_TREE);
146 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
147 static void check_bases (tree, int *, int *);
148 static void check_bases_and_members (tree);
149 static tree create_vtable_ptr (tree, tree *);
150 static void include_empty_classes (record_layout_info);
151 static void layout_class_type (tree, tree *);
152 static void propagate_binfo_offsets (tree, tree);
153 static void layout_virtual_bases (record_layout_info, splay_tree);
154 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
155 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
156 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
157 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
158 static void add_vcall_offset (tree, tree, vtbl_init_data *);
159 static void layout_vtable_decl (tree, int);
160 static tree dfs_find_final_overrider_pre (tree, void *);
161 static tree dfs_find_final_overrider_post (tree, void *);
162 static tree find_final_overrider (tree, tree, tree);
163 static int make_new_vtable (tree, tree);
164 static tree get_primary_binfo (tree);
165 static int maybe_indent_hierarchy (FILE *, int, int);
166 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
167 static void dump_class_hierarchy (tree);
168 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
169 static void dump_array (FILE *, tree);
170 static void dump_vtable (tree, tree, tree);
171 static void dump_vtt (tree, tree);
172 static void dump_thunk (FILE *, int, tree);
173 static tree build_vtable (tree, tree, tree);
174 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
175 static void layout_nonempty_base_or_field (record_layout_info,
176 tree, tree, splay_tree);
177 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
178 vec<constructor_elt, va_gc> **);
179 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
180 vec<constructor_elt, va_gc> **);
181 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
182 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
183 static void clone_constructors_and_destructors (tree);
184 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
185 static void build_ctor_vtbl_group (tree, tree);
186 static void build_vtt (tree);
187 static tree binfo_ctor_vtable (tree);
188 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
189 tree *);
190 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
191 static tree dfs_fixup_binfo_vtbls (tree, void *);
192 static int record_subobject_offset (tree, tree, splay_tree);
193 static int check_subobject_offset (tree, tree, splay_tree);
194 static int walk_subobject_offsets (tree, subobject_offset_fn,
195 tree, splay_tree, tree, int);
196 static int layout_conflict_p (tree, tree, splay_tree, int);
197 static int splay_tree_compare_integer_csts (splay_tree_key k1,
198 splay_tree_key k2);
199 static void maybe_warn_about_inaccessible_bases (tree);
200 static bool type_requires_array_cookie (tree);
201 static bool base_derived_from (tree, tree);
202 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
203 static tree end_of_base (tree);
204 static tree get_vcall_index (tree, tree);
205 static bool type_maybe_constexpr_default_constructor (tree);
206 static bool type_maybe_constexpr_destructor (tree);
207 static bool field_poverlapping_p (tree);
209 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
211 void
212 set_current_access_from_decl (tree decl)
214 if (TREE_PRIVATE (decl))
215 current_access_specifier = access_private_node;
216 else if (TREE_PROTECTED (decl))
217 current_access_specifier = access_protected_node;
218 else
219 current_access_specifier = access_public_node;
222 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
223 'structor is in charge of 'structing virtual bases, or FALSE_STMT
224 otherwise. */
226 tree
227 build_if_in_charge (tree true_stmt, tree false_stmt)
229 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
230 tree cmp = build2 (NE_EXPR, boolean_type_node,
231 current_in_charge_parm, integer_zero_node);
232 tree type = unlowered_expr_type (true_stmt);
233 if (VOID_TYPE_P (type))
234 type = unlowered_expr_type (false_stmt);
235 tree cond = build3 (COND_EXPR, type,
236 cmp, true_stmt, false_stmt);
237 return cond;
240 /* Convert to or from a base subobject. EXPR is an expression of type
241 `A' or `A*', an expression of type `B' or `B*' is returned. To
242 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
243 the B base instance within A. To convert base A to derived B, CODE
244 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
245 In this latter case, A must not be a morally virtual base of B.
246 NONNULL is true if EXPR is known to be non-NULL (this is only
247 needed when EXPR is of pointer type). CV qualifiers are preserved
248 from EXPR. */
250 tree
251 build_base_path (enum tree_code code,
252 tree expr,
253 tree binfo,
254 int nonnull,
255 tsubst_flags_t complain)
257 tree v_binfo = NULL_TREE;
258 tree d_binfo = NULL_TREE;
259 tree probe;
260 tree offset;
261 tree target_type;
262 tree null_test = NULL;
263 tree ptr_target_type;
264 int fixed_type_p;
265 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
266 bool has_empty = false;
267 bool virtual_access;
268 bool rvalue = false;
270 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
271 return error_mark_node;
273 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
275 d_binfo = probe;
276 if (is_empty_class (BINFO_TYPE (probe)))
277 has_empty = true;
278 if (!v_binfo && BINFO_VIRTUAL_P (probe))
279 v_binfo = probe;
282 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
283 if (want_pointer)
284 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
285 if (dependent_type_p (probe))
286 if (tree open = currently_open_class (probe))
287 probe = open;
289 if (code == PLUS_EXPR
290 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
292 /* This can happen when adjust_result_of_qualified_name_lookup can't
293 find a unique base binfo in a call to a member function. We
294 couldn't give the diagnostic then since we might have been calling
295 a static member function, so we do it now. In other cases, eg.
296 during error recovery (c++/71979), we may not have a base at all. */
297 if (complain & tf_error)
299 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
300 ba_unique, NULL, complain);
301 gcc_assert (base == error_mark_node || !base);
303 return error_mark_node;
306 gcc_assert ((code == MINUS_EXPR
307 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
308 || code == PLUS_EXPR);
310 if (binfo == d_binfo)
311 /* Nothing to do. */
312 return expr;
314 if (code == MINUS_EXPR && v_binfo)
316 if (complain & tf_error)
318 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
320 if (want_pointer)
321 error ("cannot convert from pointer to base class %qT to "
322 "pointer to derived class %qT because the base is "
323 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
324 else
325 error ("cannot convert from base class %qT to derived "
326 "class %qT because the base is virtual",
327 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
329 else
331 if (want_pointer)
332 error ("cannot convert from pointer to base class %qT to "
333 "pointer to derived class %qT via virtual base %qT",
334 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
335 BINFO_TYPE (v_binfo));
336 else
337 error ("cannot convert from base class %qT to derived "
338 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
339 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
342 return error_mark_node;
345 bool uneval = (cp_unevaluated_operand != 0
346 || processing_template_decl
347 || in_template_function ());
349 /* For a non-pointer simple base reference, express it as a COMPONENT_REF
350 without taking its address (and so causing lambda capture, 91933). */
351 if (code == PLUS_EXPR && !v_binfo && !want_pointer && !has_empty && !uneval)
352 return build_simple_base_path (expr, binfo);
354 if (!want_pointer)
356 rvalue = !lvalue_p (expr);
357 /* This must happen before the call to save_expr. */
358 expr = cp_build_addr_expr (expr, complain);
360 else
361 expr = mark_rvalue_use (expr);
363 offset = BINFO_OFFSET (binfo);
364 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
365 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
366 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
367 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
368 expression returned matches the input. */
369 target_type = cp_build_qualified_type
370 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
371 ptr_target_type = build_pointer_type (target_type);
373 /* Do we need to look in the vtable for the real offset? */
374 virtual_access = (v_binfo && fixed_type_p <= 0);
376 /* Don't bother with the calculations inside sizeof; they'll ICE if the
377 source type is incomplete and the pointer value doesn't matter. In a
378 template (even in instantiate_non_dependent_expr), we don't have vtables
379 set up properly yet, and the value doesn't matter there either; we're
380 just interested in the result of overload resolution. */
381 if (uneval)
383 expr = build_nop (ptr_target_type, expr);
384 goto indout;
387 if (!COMPLETE_TYPE_P (probe))
389 if (complain & tf_error)
390 error ("cannot convert from %qT to base class %qT because %qT is "
391 "incomplete", BINFO_TYPE (d_binfo), BINFO_TYPE (binfo),
392 BINFO_TYPE (d_binfo));
393 return error_mark_node;
396 /* If we're in an NSDMI, we don't have the full constructor context yet
397 that we need for converting to a virtual base, so just build a stub
398 CONVERT_EXPR and expand it later in bot_replace. */
399 if (virtual_access && fixed_type_p < 0
400 && current_scope () != current_function_decl)
402 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
403 CONVERT_EXPR_VBASE_PATH (expr) = true;
404 goto indout;
407 /* Do we need to check for a null pointer? */
408 if (want_pointer && !nonnull)
410 /* If we know the conversion will not actually change the value
411 of EXPR, then we can avoid testing the expression for NULL.
412 We have to avoid generating a COMPONENT_REF for a base class
413 field, because other parts of the compiler know that such
414 expressions are always non-NULL. */
415 if (!virtual_access && integer_zerop (offset))
416 return build_nop (ptr_target_type, expr);
417 null_test = error_mark_node;
420 /* Protect against multiple evaluation if necessary. */
421 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
422 expr = save_expr (expr);
424 /* Store EXPR and build the real null test just before returning. */
425 if (null_test)
426 null_test = expr;
428 /* If this is a simple base reference, express it as a COMPONENT_REF. */
429 if (code == PLUS_EXPR && !virtual_access
430 /* We don't build base fields for empty bases, and they aren't very
431 interesting to the optimizers anyway. */
432 && !has_empty)
434 expr = cp_build_fold_indirect_ref (expr);
435 expr = build_simple_base_path (expr, binfo);
436 if (rvalue && lvalue_p (expr))
437 expr = move (expr);
438 if (want_pointer)
439 expr = build_address (expr);
440 target_type = TREE_TYPE (expr);
441 goto out;
444 if (virtual_access)
446 /* Going via virtual base V_BINFO. We need the static offset
447 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
448 V_BINFO. That offset is an entry in D_BINFO's vtable. */
449 tree v_offset;
451 if (fixed_type_p < 0 && in_base_initializer)
453 /* In a base member initializer, we cannot rely on the
454 vtable being set up. We have to indirect via the
455 vtt_parm. */
456 tree t;
458 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
459 t = build_pointer_type (t);
460 v_offset = fold_convert (t, current_vtt_parm);
461 v_offset = cp_build_fold_indirect_ref (v_offset);
463 else
465 tree t = expr;
466 if (sanitize_flags_p (SANITIZE_VPTR)
467 && fixed_type_p == 0)
469 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
470 probe, expr);
471 if (t == NULL_TREE)
472 t = expr;
474 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
475 TREE_TYPE (TREE_TYPE (expr)));
478 if (v_offset == error_mark_node)
479 return error_mark_node;
481 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
482 v_offset = build1 (NOP_EXPR,
483 build_pointer_type (ptrdiff_type_node),
484 v_offset);
485 v_offset = cp_build_fold_indirect_ref (v_offset);
486 TREE_CONSTANT (v_offset) = 1;
488 offset = convert_to_integer (ptrdiff_type_node,
489 size_diffop_loc (input_location, offset,
490 BINFO_OFFSET (v_binfo)));
492 if (!integer_zerop (offset))
493 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
495 if (fixed_type_p < 0)
496 /* Negative fixed_type_p means this is a constructor or destructor;
497 virtual base layout is fixed in in-charge [cd]tors, but not in
498 base [cd]tors. */
499 offset = build_if_in_charge
500 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
501 v_offset);
502 else
503 offset = v_offset;
506 if (want_pointer)
507 target_type = ptr_target_type;
509 if (!integer_zerop (offset))
511 offset = fold_convert (sizetype, offset);
512 if (code == MINUS_EXPR)
513 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
514 expr = fold_build_pointer_plus (expr, offset);
516 else
517 null_test = NULL;
519 expr = build1 (NOP_EXPR, ptr_target_type, expr);
521 indout:
522 if (!want_pointer)
524 expr = cp_build_fold_indirect_ref (expr);
525 if (rvalue)
526 expr = move (expr);
529 out:
530 if (null_test)
531 /* Wrap EXPR in a null test. */
532 expr = build_if_nonnull (null_test, expr, complain);
534 return expr;
537 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
538 Perform a derived-to-base conversion by recursively building up a
539 sequence of COMPONENT_REFs to the appropriate base fields. */
541 static tree
542 build_simple_base_path (tree expr, tree binfo)
544 tree type = BINFO_TYPE (binfo);
545 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
546 tree field;
548 if (d_binfo == NULL_TREE)
550 tree temp;
552 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
554 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
555 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
556 an lvalue in the front end; only _DECLs and _REFs are lvalues
557 in the back end. */
558 temp = unary_complex_lvalue (ADDR_EXPR, expr);
559 if (temp)
560 expr = cp_build_fold_indirect_ref (temp);
562 return expr;
565 /* Recurse. */
566 expr = build_simple_base_path (expr, d_binfo);
568 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
569 field; field = DECL_CHAIN (field))
570 /* Is this the base field created by build_base_field? */
571 if (TREE_CODE (field) == FIELD_DECL
572 && DECL_FIELD_IS_BASE (field)
573 && TREE_TYPE (field) == type
574 /* If we're looking for a field in the most-derived class,
575 also check the field offset; we can have two base fields
576 of the same type if one is an indirect virtual base and one
577 is a direct non-virtual base. */
578 && (BINFO_INHERITANCE_CHAIN (d_binfo)
579 || tree_int_cst_equal (byte_position (field),
580 BINFO_OFFSET (binfo))))
582 /* We don't use build_class_member_access_expr here, as that
583 has unnecessary checks, and more importantly results in
584 recursive calls to dfs_walk_once. */
585 int type_quals = cp_type_quals (TREE_TYPE (expr));
587 expr = build3 (COMPONENT_REF,
588 cp_build_qualified_type (type, type_quals),
589 expr, field, NULL_TREE);
590 /* Mark the expression const or volatile, as appropriate.
591 Even though we've dealt with the type above, we still have
592 to mark the expression itself. */
593 if (type_quals & TYPE_QUAL_CONST)
594 TREE_READONLY (expr) = 1;
595 if (type_quals & TYPE_QUAL_VOLATILE)
596 TREE_THIS_VOLATILE (expr) = 1;
598 return expr;
601 /* Didn't find the base field?!? */
602 gcc_unreachable ();
605 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
606 type is a class type or a pointer to a class type. In the former
607 case, TYPE is also a class type; in the latter it is another
608 pointer type. If CHECK_ACCESS is true, an error message is emitted
609 if TYPE is inaccessible. If OBJECT has pointer type, the value is
610 assumed to be non-NULL. */
612 tree
613 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
614 tsubst_flags_t complain)
616 tree binfo;
617 tree object_type;
619 if (TYPE_PTR_P (TREE_TYPE (object)))
621 object_type = TREE_TYPE (TREE_TYPE (object));
622 type = TREE_TYPE (type);
624 else
625 object_type = TREE_TYPE (object);
627 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
628 NULL, complain);
629 if (!binfo || binfo == error_mark_node)
630 return error_mark_node;
632 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
635 /* EXPR is an expression with unqualified class type. BASE is a base
636 binfo of that class type. Returns EXPR, converted to the BASE
637 type. This function assumes that EXPR is the most derived class;
638 therefore virtual bases can be found at their static offsets. */
640 tree
641 convert_to_base_statically (tree expr, tree base)
643 tree expr_type;
645 expr_type = TREE_TYPE (expr);
646 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
648 /* If this is a non-empty base, use a COMPONENT_REF. */
649 if (!is_empty_class (BINFO_TYPE (base)))
650 return build_simple_base_path (expr, base);
652 /* We use fold_build2 and fold_convert below to simplify the trees
653 provided to the optimizers. It is not safe to call these functions
654 when processing a template because they do not handle C++-specific
655 trees. */
656 gcc_assert (!processing_template_decl);
657 expr = cp_build_addr_expr (expr, tf_warning_or_error);
658 if (!integer_zerop (BINFO_OFFSET (base)))
659 expr = fold_build_pointer_plus_loc (input_location,
660 expr, BINFO_OFFSET (base));
661 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
662 expr = build_fold_indirect_ref_loc (input_location, expr);
665 return expr;
668 /* True IFF EXPR is a reference to an empty base class "subobject", as built in
669 convert_to_base_statically. We look for the result of the fold_convert
670 call, a NOP_EXPR from one pointer type to another, where the target is an
671 empty base of the original type. */
673 bool
674 is_empty_base_ref (tree expr)
676 if (TREE_CODE (expr) == INDIRECT_REF)
677 expr = TREE_OPERAND (expr, 0);
678 if (TREE_CODE (expr) != NOP_EXPR)
679 return false;
680 tree type = TREE_TYPE (expr);
681 if (!POINTER_TYPE_P (type))
682 return false;
683 type = TREE_TYPE (type);
684 if (!is_empty_class (type))
685 return false;
686 STRIP_NOPS (expr);
687 tree fromtype = TREE_TYPE (expr);
688 if (!POINTER_TYPE_P (fromtype))
689 return false;
690 fromtype = TREE_TYPE (fromtype);
691 return (CLASS_TYPE_P (fromtype)
692 && !same_type_ignoring_top_level_qualifiers_p (fromtype, type)
693 && DERIVED_FROM_P (type, fromtype));
696 tree
697 build_vfield_ref (tree datum, tree type)
699 tree vfield, vcontext;
701 if (datum == error_mark_node
702 /* Can happen in case of duplicate base types (c++/59082). */
703 || !TYPE_VFIELD (type))
704 return error_mark_node;
706 /* First, convert to the requested type. */
707 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
708 datum = convert_to_base (datum, type, /*check_access=*/false,
709 /*nonnull=*/true, tf_warning_or_error);
711 /* Second, the requested type may not be the owner of its own vptr.
712 If not, convert to the base class that owns it. We cannot use
713 convert_to_base here, because VCONTEXT may appear more than once
714 in the inheritance hierarchy of TYPE, and thus direct conversion
715 between the types may be ambiguous. Following the path back up
716 one step at a time via primary bases avoids the problem. */
717 vfield = TYPE_VFIELD (type);
718 vcontext = DECL_CONTEXT (vfield);
719 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
721 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
722 type = TREE_TYPE (datum);
725 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
728 /* Given an object INSTANCE, return an expression which yields the
729 vtable element corresponding to INDEX. There are many special
730 cases for INSTANCE which we take care of here, mainly to avoid
731 creating extra tree nodes when we don't have to. */
733 tree
734 build_vtbl_ref (tree instance, tree idx)
736 tree aref;
737 tree vtbl = NULL_TREE;
739 /* Try to figure out what a reference refers to, and
740 access its virtual function table directly. */
742 int cdtorp = 0;
743 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
745 tree basetype = non_reference (TREE_TYPE (instance));
747 if (fixed_type && !cdtorp)
749 tree binfo = lookup_base (fixed_type, basetype,
750 ba_unique, NULL, tf_none);
751 if (binfo && binfo != error_mark_node)
752 vtbl = unshare_expr (BINFO_VTABLE (binfo));
755 if (!vtbl)
756 vtbl = build_vfield_ref (instance, basetype);
758 aref = build_array_ref (input_location, vtbl, idx);
759 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
761 return aref;
764 /* Given a stable object pointer INSTANCE_PTR, return an expression which
765 yields a function pointer corresponding to vtable element INDEX. */
767 tree
768 build_vfn_ref (tree instance_ptr, tree idx)
770 tree aref;
772 aref = build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr), idx);
774 /* When using function descriptors, the address of the
775 vtable entry is treated as a function pointer. */
776 if (TARGET_VTABLE_USES_DESCRIPTORS)
777 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
778 cp_build_addr_expr (aref, tf_warning_or_error));
780 /* Remember this as a method reference, for later devirtualization. */
781 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
783 return aref;
786 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
787 for the given TYPE. */
789 static tree
790 get_vtable_name (tree type)
792 return mangle_vtbl_for_type (type);
795 /* DECL is an entity associated with TYPE, like a virtual table or an
796 implicitly generated constructor. Determine whether or not DECL
797 should have external or internal linkage at the object file
798 level. This routine does not deal with COMDAT linkage and other
799 similar complexities; it simply sets TREE_PUBLIC if it possible for
800 entities in other translation units to contain copies of DECL, in
801 the abstract. */
803 void
804 set_linkage_according_to_type (tree /*type*/, tree decl)
806 TREE_PUBLIC (decl) = 1;
807 determine_visibility (decl);
810 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
811 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
812 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
814 static tree
815 build_vtable (tree class_type, tree name, tree vtable_type)
817 tree decl;
819 decl = build_lang_decl (VAR_DECL, name, vtable_type);
820 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
821 now to avoid confusion in mangle_decl. */
822 SET_DECL_ASSEMBLER_NAME (decl, name);
823 DECL_CONTEXT (decl) = class_type;
824 DECL_ARTIFICIAL (decl) = 1;
825 TREE_STATIC (decl) = 1;
826 TREE_READONLY (decl) = 1;
827 DECL_VIRTUAL_P (decl) = 1;
828 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
829 DECL_USER_ALIGN (decl) = true;
830 DECL_VTABLE_OR_VTT_P (decl) = 1;
831 set_linkage_according_to_type (class_type, decl);
832 /* The vtable has not been defined -- yet. */
833 DECL_EXTERNAL (decl) = 1;
834 DECL_NOT_REALLY_EXTERN (decl) = 1;
836 /* Mark the VAR_DECL node representing the vtable itself as a
837 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
838 is rather important that such things be ignored because any
839 effort to actually generate DWARF for them will run into
840 trouble when/if we encounter code like:
842 #pragma interface
843 struct S { virtual void member (); };
845 because the artificial declaration of the vtable itself (as
846 manufactured by the g++ front end) will say that the vtable is
847 a static member of `S' but only *after* the debug output for
848 the definition of `S' has already been output. This causes
849 grief because the DWARF entry for the definition of the vtable
850 will try to refer back to an earlier *declaration* of the
851 vtable as a static member of `S' and there won't be one. We
852 might be able to arrange to have the "vtable static member"
853 attached to the member list for `S' before the debug info for
854 `S' get written (which would solve the problem) but that would
855 require more intrusive changes to the g++ front end. */
856 DECL_IGNORED_P (decl) = 1;
858 return decl;
861 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
862 or even complete. If this does not exist, create it. If COMPLETE is
863 nonzero, then complete the definition of it -- that will render it
864 impossible to actually build the vtable, but is useful to get at those
865 which are known to exist in the runtime. */
867 tree
868 get_vtable_decl (tree type, int complete)
870 tree decl;
872 if (CLASSTYPE_VTABLES (type))
873 return CLASSTYPE_VTABLES (type);
875 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
876 CLASSTYPE_VTABLES (type) = decl;
878 if (complete)
880 DECL_EXTERNAL (decl) = 1;
881 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
884 return decl;
887 /* Build the primary virtual function table for TYPE. If BINFO is
888 non-NULL, build the vtable starting with the initial approximation
889 that it is the same as the one which is the head of the association
890 list. Returns a nonzero value if a new vtable is actually
891 created. */
893 static int
894 build_primary_vtable (tree binfo, tree type)
896 tree decl;
897 tree virtuals;
899 decl = get_vtable_decl (type, /*complete=*/0);
901 if (binfo)
903 if (BINFO_NEW_VTABLE_MARKED (binfo))
904 /* We have already created a vtable for this base, so there's
905 no need to do it again. */
906 return 0;
908 virtuals = copy_list (BINFO_VIRTUALS (binfo));
909 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
910 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
911 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
913 else
915 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
916 virtuals = NULL_TREE;
919 /* Initialize the association list for this type, based
920 on our first approximation. */
921 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
922 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
923 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
924 return 1;
927 /* Give BINFO a new virtual function table which is initialized
928 with a skeleton-copy of its original initialization. The only
929 entry that changes is the `delta' entry, so we can really
930 share a lot of structure.
932 FOR_TYPE is the most derived type which caused this table to
933 be needed.
935 Returns nonzero if we haven't met BINFO before.
937 The order in which vtables are built (by calling this function) for
938 an object must remain the same, otherwise a binary incompatibility
939 can result. */
941 static int
942 build_secondary_vtable (tree binfo)
944 if (BINFO_NEW_VTABLE_MARKED (binfo))
945 /* We already created a vtable for this base. There's no need to
946 do it again. */
947 return 0;
949 /* Remember that we've created a vtable for this BINFO, so that we
950 don't try to do so again. */
951 SET_BINFO_NEW_VTABLE_MARKED (binfo);
953 /* Make fresh virtual list, so we can smash it later. */
954 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
956 /* Secondary vtables are laid out as part of the same structure as
957 the primary vtable. */
958 BINFO_VTABLE (binfo) = NULL_TREE;
959 return 1;
962 /* Create a new vtable for BINFO which is the hierarchy dominated by
963 T. Return nonzero if we actually created a new vtable. */
965 static int
966 make_new_vtable (tree t, tree binfo)
968 if (binfo == TYPE_BINFO (t))
969 /* In this case, it is *type*'s vtable we are modifying. We start
970 with the approximation that its vtable is that of the
971 immediate base class. */
972 return build_primary_vtable (binfo, t);
973 else
974 /* This is our very own copy of `basetype' to play with. Later,
975 we will fill in all the virtual functions that override the
976 virtual functions in these base classes which are not defined
977 by the current type. */
978 return build_secondary_vtable (binfo);
981 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
982 (which is in the hierarchy dominated by T) list FNDECL as its
983 BV_FN. DELTA is the required constant adjustment from the `this'
984 pointer where the vtable entry appears to the `this' required when
985 the function is actually called. */
987 static void
988 modify_vtable_entry (tree t,
989 tree binfo,
990 tree fndecl,
991 tree delta,
992 tree *virtuals)
994 tree v;
996 v = *virtuals;
998 if (fndecl != BV_FN (v)
999 || !tree_int_cst_equal (delta, BV_DELTA (v)))
1001 /* We need a new vtable for BINFO. */
1002 if (make_new_vtable (t, binfo))
1004 /* If we really did make a new vtable, we also made a copy
1005 of the BINFO_VIRTUALS list. Now, we have to find the
1006 corresponding entry in that list. */
1007 *virtuals = BINFO_VIRTUALS (binfo);
1008 while (BV_FN (*virtuals) != BV_FN (v))
1009 *virtuals = TREE_CHAIN (*virtuals);
1010 v = *virtuals;
1013 BV_DELTA (v) = delta;
1014 BV_VCALL_INDEX (v) = NULL_TREE;
1015 BV_FN (v) = fndecl;
1020 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1021 METHOD is being injected via a using_decl. Returns true if the
1022 method could be added to the method vec. */
1024 bool
1025 add_method (tree type, tree method, bool via_using)
1027 if (method == error_mark_node)
1028 return false;
1030 gcc_assert (!DECL_EXTERN_C_P (method));
1032 tree *slot = find_member_slot (type, DECL_NAME (method));
1033 tree current_fns = slot ? *slot : NULL_TREE;
1035 /* See below. */
1036 int losem = -1;
1038 /* Check to see if we've already got this method. */
1039 for (ovl_iterator iter (current_fns); iter; ++iter)
1041 tree fn = *iter;
1043 if (TREE_CODE (fn) != TREE_CODE (method))
1044 continue;
1046 /* Two using-declarations can coexist, we'll complain about ambiguity in
1047 overload resolution. */
1048 if (via_using && iter.using_p ()
1049 /* Except handle inherited constructors specially. */
1050 && ! DECL_CONSTRUCTOR_P (fn))
1051 continue;
1053 /* [over.load] Member function declarations with the
1054 same name and the same parameter types cannot be
1055 overloaded if any of them is a static member
1056 function declaration.
1058 [over.load] Member function declarations with the same name and
1059 the same parameter-type-list as well as member function template
1060 declarations with the same name, the same parameter-type-list, and
1061 the same template parameter lists cannot be overloaded if any of
1062 them, but not all, have a ref-qualifier.
1064 [namespace.udecl] When a using-declaration brings names
1065 from a base class into a derived class scope, member
1066 functions in the derived class override and/or hide member
1067 functions with the same name and parameter types in a base
1068 class (rather than conflicting). */
1069 tree fn_type = TREE_TYPE (fn);
1070 tree method_type = TREE_TYPE (method);
1072 /* Compare the quals on the 'this' parm. Don't compare
1073 the whole types, as used functions are treated as
1074 coming from the using class in overload resolution. */
1075 if (! DECL_STATIC_FUNCTION_P (fn)
1076 && ! DECL_STATIC_FUNCTION_P (method)
1077 /* Either both or neither need to be ref-qualified for
1078 differing quals to allow overloading. */
1079 && (FUNCTION_REF_QUALIFIED (fn_type)
1080 == FUNCTION_REF_QUALIFIED (method_type))
1081 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1082 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1083 continue;
1085 tree real_fn = fn;
1086 tree real_method = method;
1088 /* Templates and conversion ops must match return types. */
1089 if ((DECL_CONV_FN_P (fn) || TREE_CODE (fn) == TEMPLATE_DECL)
1090 && !same_type_p (TREE_TYPE (fn_type), TREE_TYPE (method_type)))
1091 continue;
1093 /* For templates, the template parameters must be identical. */
1094 if (TREE_CODE (fn) == TEMPLATE_DECL)
1096 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1097 DECL_TEMPLATE_PARMS (method)))
1098 continue;
1100 real_fn = DECL_TEMPLATE_RESULT (fn);
1101 real_method = DECL_TEMPLATE_RESULT (method);
1104 tree parms1 = TYPE_ARG_TYPES (fn_type);
1105 tree parms2 = TYPE_ARG_TYPES (method_type);
1106 if (! DECL_STATIC_FUNCTION_P (real_fn))
1107 parms1 = TREE_CHAIN (parms1);
1108 if (! DECL_STATIC_FUNCTION_P (real_method))
1109 parms2 = TREE_CHAIN (parms2);
1111 /* Bring back parameters omitted from an inherited ctor. The
1112 method and the function can have different omittedness. */
1113 if (ctor_omit_inherited_parms (real_fn))
1114 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn));
1115 if (ctor_omit_inherited_parms (real_method))
1116 parms2 = (FUNCTION_FIRST_USER_PARMTYPE
1117 (DECL_CLONED_FUNCTION (real_method)));
1119 if (!compparms (parms1, parms2))
1120 continue;
1122 if (!equivalently_constrained (fn, method))
1124 if (processing_template_decl)
1125 /* We can't check satisfaction in dependent context, wait until
1126 the class is instantiated. */
1127 continue;
1129 special_function_kind sfk = special_memfn_p (method);
1131 if (sfk == sfk_none
1132 || DECL_INHERITED_CTOR (fn)
1133 || TREE_CODE (fn) == TEMPLATE_DECL)
1134 /* Member function templates and non-special member functions
1135 coexist if they are not equivalently constrained. A member
1136 function is not hidden by an inherited constructor. */
1137 continue;
1139 /* P0848: For special member functions, deleted, unsatisfied, or
1140 less constrained overloads are ineligible. We implement this
1141 by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1142 use the notion of eligibility, and the selected destructor can
1143 be deleted, but removing unsatisfied or less constrained
1144 overloads has the same effect as overload resolution. */
1145 bool dtor = (sfk == sfk_destructor);
1146 if (losem == -1)
1147 losem = ((!dtor && DECL_DELETED_FN (method))
1148 || !constraints_satisfied_p (method));
1149 bool losef = ((!dtor && DECL_DELETED_FN (fn))
1150 || !constraints_satisfied_p (fn));
1151 int win;
1152 if (losem || losef)
1153 win = losem - losef;
1154 else
1155 win = more_constrained (fn, method);
1156 if (win > 0)
1157 /* Leave FN in the method vec, discard METHOD. */
1158 return false;
1159 else if (win < 0)
1161 /* Remove FN, add METHOD. */
1162 current_fns = iter.remove_node (current_fns);
1163 continue;
1165 else
1166 /* Let them coexist for now. */
1167 continue;
1170 /* If these are versions of the same function, process and
1171 move on. */
1172 if (TREE_CODE (fn) == FUNCTION_DECL
1173 && maybe_version_functions (method, fn, true))
1174 continue;
1176 if (DECL_INHERITED_CTOR (method))
1178 if (!DECL_INHERITED_CTOR (fn))
1179 /* Defer to the other function. */
1180 return false;
1182 tree basem = DECL_INHERITED_CTOR_BASE (method);
1183 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1184 if (flag_new_inheriting_ctors)
1186 if (basem == basef)
1188 /* Inheriting the same constructor along different
1189 paths, combine them. */
1190 SET_DECL_INHERITED_CTOR
1191 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1192 DECL_INHERITED_CTOR (fn)));
1193 /* And discard the new one. */
1194 return false;
1196 else
1197 /* Inherited ctors can coexist until overload
1198 resolution. */
1199 continue;
1202 error_at (DECL_SOURCE_LOCATION (method),
1203 "%q#D conflicts with version inherited from %qT",
1204 method, basef);
1205 inform (DECL_SOURCE_LOCATION (fn),
1206 "version inherited from %qT declared here",
1207 basef);
1208 return false;
1211 if (via_using)
1212 /* Defer to the local function. */
1213 return false;
1214 else if (flag_new_inheriting_ctors
1215 && DECL_INHERITED_CTOR (fn))
1217 /* Remove the inherited constructor. */
1218 current_fns = iter.remove_node (current_fns);
1219 continue;
1221 else
1223 error_at (DECL_SOURCE_LOCATION (method),
1224 "%q#D cannot be overloaded with %q#D", method, fn);
1225 inform (DECL_SOURCE_LOCATION (fn),
1226 "previous declaration %q#D", fn);
1227 return false;
1231 current_fns = ovl_insert (method, current_fns, via_using);
1233 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1234 && !push_class_level_binding (DECL_NAME (method), current_fns))
1235 return false;
1237 if (!slot)
1238 slot = add_member_slot (type, DECL_NAME (method));
1240 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1241 grok_special_member_properties (method);
1243 *slot = current_fns;
1245 return true;
1248 /* Subroutines of finish_struct. */
1250 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1251 legit, otherwise return 0. */
1253 static int
1254 alter_access (tree t, tree fdecl, tree access)
1256 tree elem;
1258 retrofit_lang_decl (fdecl);
1260 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1262 elem = purpose_member (t, DECL_ACCESS (fdecl));
1263 if (elem)
1265 if (TREE_VALUE (elem) != access)
1267 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1268 error ("conflicting access specifications for method"
1269 " %q+D, ignored", TREE_TYPE (fdecl));
1270 else
1271 error ("conflicting access specifications for field %qE, ignored",
1272 DECL_NAME (fdecl));
1274 else
1276 /* They're changing the access to the same thing they changed
1277 it to before. That's OK. */
1281 else
1283 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1284 tf_warning_or_error);
1285 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1286 return 1;
1288 return 0;
1291 /* Return the access node for DECL's access in its enclosing class. */
1293 tree
1294 declared_access (tree decl)
1296 return (TREE_PRIVATE (decl) ? access_private_node
1297 : TREE_PROTECTED (decl) ? access_protected_node
1298 : access_public_node);
1301 /* Process the USING_DECL, which is a member of T. */
1303 static void
1304 handle_using_decl (tree using_decl, tree t)
1306 tree decl = USING_DECL_DECLS (using_decl);
1307 tree name = DECL_NAME (using_decl);
1308 tree access = declared_access (using_decl);
1309 tree flist = NULL_TREE;
1310 tree old_value;
1312 gcc_assert (!processing_template_decl && decl);
1314 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1315 tf_warning_or_error);
1316 if (old_value)
1318 old_value = OVL_FIRST (old_value);
1320 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1321 /* OK */;
1322 else
1323 old_value = NULL_TREE;
1326 cp_emit_debug_info_for_using (decl, t);
1328 if (is_overloaded_fn (decl))
1329 flist = decl;
1331 if (! old_value)
1333 else if (is_overloaded_fn (old_value))
1335 if (flist)
1336 /* It's OK to use functions from a base when there are functions with
1337 the same name already present in the current class. */;
1338 else
1340 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1341 "because of local method %q#D with same name",
1342 using_decl, t, old_value);
1343 inform (DECL_SOURCE_LOCATION (old_value),
1344 "local method %q#D declared here", old_value);
1345 return;
1348 else if (!DECL_ARTIFICIAL (old_value))
1350 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1351 "because of local member %q#D with same name",
1352 using_decl, t, old_value);
1353 inform (DECL_SOURCE_LOCATION (old_value),
1354 "local member %q#D declared here", old_value);
1355 return;
1358 iloc_sentinel ils (DECL_SOURCE_LOCATION (using_decl));
1360 /* Make type T see field decl FDECL with access ACCESS. */
1361 if (flist)
1362 for (tree f : ovl_range (flist))
1364 add_method (t, f, true);
1365 alter_access (t, f, access);
1367 else if (USING_DECL_UNRELATED_P (using_decl))
1369 /* C++20 using enum can import non-inherited enumerators into class
1370 scope. We implement that by making a copy of the CONST_DECL for which
1371 CONST_DECL_USING_P is true. */
1372 gcc_assert (TREE_CODE (decl) == CONST_DECL);
1374 auto cas = make_temp_override (current_access_specifier);
1375 set_current_access_from_decl (using_decl);
1376 tree copy = copy_decl (decl);
1377 DECL_CONTEXT (copy) = t;
1378 DECL_ARTIFICIAL (copy) = true;
1379 /* We emitted debug info for the USING_DECL above; make sure we don't
1380 also emit anything for this clone. */
1381 DECL_IGNORED_P (copy) = true;
1382 DECL_SOURCE_LOCATION (copy) = DECL_SOURCE_LOCATION (using_decl);
1383 finish_member_declaration (copy);
1384 DECL_ABSTRACT_ORIGIN (copy) = decl;
1386 else
1387 alter_access (t, decl, access);
1390 /* Data structure for find_abi_tags_r, below. */
1392 struct abi_tag_data
1394 tree t; // The type that we're checking for missing tags.
1395 tree subob; // The subobject of T that we're getting tags from.
1396 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1399 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1400 in the context of P. TAG can be either an identifier (the DECL_NAME of
1401 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1403 static void
1404 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1406 if (!IDENTIFIER_MARKED (id))
1408 if (p->tags != error_mark_node)
1410 /* We're collecting tags from template arguments or from
1411 the type of a variable or function return type. */
1412 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1414 /* Don't inherit this tag multiple times. */
1415 IDENTIFIER_MARKED (id) = true;
1417 if (TYPE_P (p->t))
1419 /* Tags inherited from type template arguments are only used
1420 to avoid warnings. */
1421 ABI_TAG_IMPLICIT (p->tags) = true;
1422 return;
1424 /* For functions and variables we want to warn, too. */
1427 /* Otherwise we're diagnosing missing tags. */
1428 if (TREE_CODE (p->t) == FUNCTION_DECL)
1430 auto_diagnostic_group d;
1431 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1432 "that %qT (used in its return type) has",
1433 p->t, tag, *tp))
1434 inform (location_of (*tp), "%qT declared here", *tp);
1436 else if (VAR_P (p->t))
1438 auto_diagnostic_group d;
1439 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1440 "that %qT (used in its type) has", p->t, tag, *tp))
1441 inform (location_of (*tp), "%qT declared here", *tp);
1443 else if (TYPE_P (p->subob))
1445 auto_diagnostic_group d;
1446 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1447 "that base %qT has", p->t, tag, p->subob))
1448 inform (location_of (p->subob), "%qT declared here",
1449 p->subob);
1451 else
1453 auto_diagnostic_group d;
1454 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1455 "that %qT (used in the type of %qD) has",
1456 p->t, tag, *tp, p->subob))
1458 inform (location_of (p->subob), "%qD declared here",
1459 p->subob);
1460 inform (location_of (*tp), "%qT declared here", *tp);
1466 /* Find all the ABI tags in the attribute list ATTR and either call
1467 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1469 static void
1470 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1472 if (!attr)
1473 return;
1474 for (; (attr = lookup_attribute ("abi_tag", attr));
1475 attr = TREE_CHAIN (attr))
1476 for (tree list = TREE_VALUE (attr); list;
1477 list = TREE_CHAIN (list))
1479 tree tag = TREE_VALUE (list);
1480 tree id = get_identifier (TREE_STRING_POINTER (tag));
1481 if (tp)
1482 check_tag (tag, id, tp, p);
1483 else
1484 IDENTIFIER_MARKED (id) = val;
1488 /* Find all the ABI tags on T and its enclosing scopes and either call
1489 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1491 static void
1492 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1494 while (t != global_namespace)
1496 tree attr;
1497 if (TYPE_P (t))
1499 attr = TYPE_ATTRIBUTES (t);
1500 t = CP_TYPE_CONTEXT (t);
1502 else
1504 attr = DECL_ATTRIBUTES (t);
1505 t = CP_DECL_CONTEXT (t);
1507 mark_or_check_attr_tags (attr, tp, p, val);
1511 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1512 types with ABI tags, add the corresponding identifiers to the VEC in
1513 *DATA and set IDENTIFIER_MARKED. */
1515 static tree
1516 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1518 if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1519 /* Tell cp_walk_subtrees to look though typedefs. [PR98481] */
1520 *walk_subtrees = 2;
1522 if (!OVERLOAD_TYPE_P (*tp))
1523 return NULL_TREE;
1525 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1526 anyway, but let's make sure of it. */
1527 *walk_subtrees = false;
1529 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1531 mark_or_check_tags (*tp, tp, p, false);
1533 return NULL_TREE;
1536 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1537 IDENTIFIER_MARKED on its ABI tags. */
1539 static tree
1540 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1542 if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1543 /* Tell cp_walk_subtrees to look though typedefs. */
1544 *walk_subtrees = 2;
1546 if (!OVERLOAD_TYPE_P (*tp))
1547 return NULL_TREE;
1549 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1550 anyway, but let's make sure of it. */
1551 *walk_subtrees = false;
1553 bool *valp = static_cast<bool*>(data);
1555 mark_or_check_tags (*tp, NULL, NULL, *valp);
1557 return NULL_TREE;
1560 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1561 scopes. */
1563 static void
1564 mark_abi_tags (tree t, bool val)
1566 mark_or_check_tags (t, NULL, NULL, val);
1567 if (DECL_P (t))
1569 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1570 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1572 /* Template arguments are part of the signature. */
1573 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1574 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1576 tree arg = TREE_VEC_ELT (level, j);
1577 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1580 if (TREE_CODE (t) == FUNCTION_DECL)
1581 /* A function's parameter types are part of the signature, so
1582 we don't need to inherit any tags that are also in them. */
1583 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1584 arg = TREE_CHAIN (arg))
1585 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1586 mark_abi_tags_r, &val);
1590 /* Check that T has all the ABI tags that subobject SUBOB has, or
1591 warn if not. If T is a (variable or function) declaration, also
1592 return any missing tags, and add them to T if JUST_CHECKING is false. */
1594 static tree
1595 check_abi_tags (tree t, tree subob, bool just_checking = false)
1597 bool inherit = DECL_P (t);
1599 if (!inherit && !warn_abi_tag)
1600 return NULL_TREE;
1602 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1603 if (!TREE_PUBLIC (decl))
1604 /* No need to worry about things local to this TU. */
1605 return NULL_TREE;
1607 mark_abi_tags (t, true);
1609 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1610 struct abi_tag_data data = { t, subob, error_mark_node };
1611 if (inherit)
1612 data.tags = NULL_TREE;
1614 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1616 if (!(inherit && data.tags))
1617 /* We don't need to do anything with data.tags. */;
1618 else if (just_checking)
1619 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1621 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1622 IDENTIFIER_MARKED (id) = false;
1624 else
1626 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1627 if (attr)
1628 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1629 else
1630 DECL_ATTRIBUTES (t)
1631 = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1634 mark_abi_tags (t, false);
1636 return data.tags;
1639 /* Check that DECL has all the ABI tags that are used in parts of its type
1640 that are not reflected in its mangled name. */
1642 void
1643 check_abi_tags (tree decl)
1645 if (VAR_P (decl))
1646 check_abi_tags (decl, TREE_TYPE (decl));
1647 else if (TREE_CODE (decl) == FUNCTION_DECL
1648 && !DECL_CONV_FN_P (decl)
1649 && !mangle_return_type_p (decl))
1650 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1653 /* Return any ABI tags that are used in parts of the type of DECL
1654 that are not reflected in its mangled name. This function is only
1655 used in backward-compatible mangling for ABI <11. */
1657 tree
1658 missing_abi_tags (tree decl)
1660 if (VAR_P (decl))
1661 return check_abi_tags (decl, TREE_TYPE (decl), true);
1662 else if (TREE_CODE (decl) == FUNCTION_DECL
1663 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1664 that we can use this function for setting need_abi_warning
1665 regardless of the current flag_abi_version. */
1666 && !mangle_return_type_p (decl))
1667 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1668 else
1669 return NULL_TREE;
1672 void
1673 inherit_targ_abi_tags (tree t)
1675 if (!CLASS_TYPE_P (t)
1676 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1677 return;
1679 mark_abi_tags (t, true);
1681 tree args = CLASSTYPE_TI_ARGS (t);
1682 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1683 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1685 tree level = TMPL_ARGS_LEVEL (args, i+1);
1686 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1688 tree arg = TREE_VEC_ELT (level, j);
1689 data.subob = arg;
1690 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1694 // If we found some tags on our template arguments, add them to our
1695 // abi_tag attribute.
1696 if (data.tags)
1698 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1699 if (attr)
1700 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1701 else
1702 TYPE_ATTRIBUTES (t)
1703 = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
1706 mark_abi_tags (t, false);
1709 /* Return true, iff class T has a non-virtual destructor that is
1710 accessible from outside the class heirarchy (i.e. is public, or
1711 there's a suitable friend. */
1713 static bool
1714 accessible_nvdtor_p (tree t)
1716 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1718 /* An implicitly declared destructor is always public. And,
1719 if it were virtual, we would have created it by now. */
1720 if (!dtor)
1721 return true;
1723 if (DECL_VINDEX (dtor))
1724 return false; /* Virtual */
1726 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1727 return true; /* Public */
1729 if (CLASSTYPE_FRIEND_CLASSES (t)
1730 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1731 return true; /* Has friends */
1733 return false;
1736 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1737 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1738 properties of the bases. */
1740 static void
1741 check_bases (tree t,
1742 int* cant_have_const_ctor_p,
1743 int* no_const_asn_ref_p)
1745 int i;
1746 bool seen_non_virtual_nearly_empty_base_p = 0;
1747 int seen_tm_mask = 0;
1748 tree base_binfo;
1749 tree binfo;
1750 tree field = NULL_TREE;
1752 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1753 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1754 if (TREE_CODE (field) == FIELD_DECL)
1755 break;
1757 for (binfo = TYPE_BINFO (t), i = 0;
1758 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1760 tree basetype = TREE_TYPE (base_binfo);
1762 gcc_assert (COMPLETE_TYPE_P (basetype));
1764 if (CLASSTYPE_FINAL (basetype))
1765 error ("cannot derive from %<final%> base %qT in derived type %qT",
1766 basetype, t);
1768 /* If any base class is non-literal, so is the derived class. */
1769 if (!CLASSTYPE_LITERAL_P (basetype))
1770 CLASSTYPE_LITERAL_P (t) = false;
1772 /* If the base class doesn't have copy constructors or
1773 assignment operators that take const references, then the
1774 derived class cannot have such a member automatically
1775 generated. */
1776 if (TYPE_HAS_COPY_CTOR (basetype)
1777 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1778 *cant_have_const_ctor_p = 1;
1779 if (TYPE_HAS_COPY_ASSIGN (basetype)
1780 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1781 *no_const_asn_ref_p = 1;
1783 if (BINFO_VIRTUAL_P (base_binfo))
1784 /* A virtual base does not effect nearly emptiness. */
1786 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1788 if (seen_non_virtual_nearly_empty_base_p)
1789 /* And if there is more than one nearly empty base, then the
1790 derived class is not nearly empty either. */
1791 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1792 else
1793 /* Remember we've seen one. */
1794 seen_non_virtual_nearly_empty_base_p = 1;
1796 else if (!is_empty_class (basetype))
1797 /* If the base class is not empty or nearly empty, then this
1798 class cannot be nearly empty. */
1799 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1801 /* A lot of properties from the bases also apply to the derived
1802 class. */
1803 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1804 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1805 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1806 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1807 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1808 || !TYPE_HAS_COPY_ASSIGN (basetype));
1809 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1810 || !TYPE_HAS_COPY_CTOR (basetype));
1811 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1812 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1813 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1814 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1815 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1816 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1817 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1818 || TYPE_HAS_COMPLEX_DFLT (basetype));
1819 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1820 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1821 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1822 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1823 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1824 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1825 if (TYPE_HAS_MUTABLE_P (basetype))
1826 CLASSTYPE_HAS_MUTABLE (t) = 1;
1828 /* A standard-layout class is a class that:
1830 * has no non-standard-layout base classes, */
1831 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1832 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1834 tree basefield;
1835 /* ...has no base classes of the same type as the first non-static
1836 data member... */
1837 if (field && DECL_CONTEXT (field) == t
1838 && (same_type_ignoring_top_level_qualifiers_p
1839 (TREE_TYPE (field), basetype)))
1840 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1841 /* DR 1813:
1842 ...has at most one base class subobject of any given type... */
1843 else if (CLASSTYPE_REPEATED_BASE_P (t))
1844 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1845 else
1846 /* ...has all non-static data members and bit-fields in the class
1847 and its base classes first declared in the same class. */
1848 for (basefield = TYPE_FIELDS (basetype); basefield;
1849 basefield = DECL_CHAIN (basefield))
1850 if (TREE_CODE (basefield) == FIELD_DECL
1851 && !(DECL_FIELD_IS_BASE (basefield)
1852 && is_empty_field (basefield)))
1854 if (field)
1855 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1856 else
1857 field = basefield;
1858 break;
1862 /* Don't bother collecting tm attributes if transactional memory
1863 support is not enabled. */
1864 if (flag_tm)
1866 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1867 if (tm_attr)
1868 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1871 check_abi_tags (t, basetype);
1874 /* If one of the base classes had TM attributes, and the current class
1875 doesn't define its own, then the current class inherits one. */
1876 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1878 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1879 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1883 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1884 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1885 that have had a nearly-empty virtual primary base stolen by some
1886 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1887 T. */
1889 static void
1890 determine_primary_bases (tree t)
1892 unsigned i;
1893 tree primary = NULL_TREE;
1894 tree type_binfo = TYPE_BINFO (t);
1895 tree base_binfo;
1897 /* Determine the primary bases of our bases. */
1898 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1899 base_binfo = TREE_CHAIN (base_binfo))
1901 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1903 /* See if we're the non-virtual primary of our inheritance
1904 chain. */
1905 if (!BINFO_VIRTUAL_P (base_binfo))
1907 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1908 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1910 if (parent_primary
1911 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1912 BINFO_TYPE (parent_primary)))
1913 /* We are the primary binfo. */
1914 BINFO_PRIMARY_P (base_binfo) = 1;
1916 /* Determine if we have a virtual primary base, and mark it so.
1918 if (primary && BINFO_VIRTUAL_P (primary))
1920 tree this_primary = copied_binfo (primary, base_binfo);
1922 if (BINFO_PRIMARY_P (this_primary))
1923 /* Someone already claimed this base. */
1924 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1925 else
1927 tree delta;
1929 BINFO_PRIMARY_P (this_primary) = 1;
1930 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1932 /* A virtual binfo might have been copied from within
1933 another hierarchy. As we're about to use it as a
1934 primary base, make sure the offsets match. */
1935 delta = size_diffop_loc (input_location,
1936 fold_convert (ssizetype,
1937 BINFO_OFFSET (base_binfo)),
1938 fold_convert (ssizetype,
1939 BINFO_OFFSET (this_primary)));
1941 propagate_binfo_offsets (this_primary, delta);
1946 /* First look for a dynamic direct non-virtual base. */
1947 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1949 tree basetype = BINFO_TYPE (base_binfo);
1951 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1953 primary = base_binfo;
1954 goto found;
1958 /* A "nearly-empty" virtual base class can be the primary base
1959 class, if no non-virtual polymorphic base can be found. Look for
1960 a nearly-empty virtual dynamic base that is not already a primary
1961 base of something in the hierarchy. If there is no such base,
1962 just pick the first nearly-empty virtual base. */
1964 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1965 base_binfo = TREE_CHAIN (base_binfo))
1966 if (BINFO_VIRTUAL_P (base_binfo)
1967 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1969 if (!BINFO_PRIMARY_P (base_binfo))
1971 /* Found one that is not primary. */
1972 primary = base_binfo;
1973 goto found;
1975 else if (!primary)
1976 /* Remember the first candidate. */
1977 primary = base_binfo;
1980 found:
1981 /* If we've got a primary base, use it. */
1982 if (primary)
1984 tree basetype = BINFO_TYPE (primary);
1986 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1987 if (BINFO_PRIMARY_P (primary))
1988 /* We are stealing a primary base. */
1989 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1990 BINFO_PRIMARY_P (primary) = 1;
1991 if (BINFO_VIRTUAL_P (primary))
1993 tree delta;
1995 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1996 /* A virtual binfo might have been copied from within
1997 another hierarchy. As we're about to use it as a primary
1998 base, make sure the offsets match. */
1999 delta = size_diffop_loc (input_location, ssize_int (0),
2000 fold_convert (ssizetype, BINFO_OFFSET (primary)));
2002 propagate_binfo_offsets (primary, delta);
2005 primary = TYPE_BINFO (basetype);
2007 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2008 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2009 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2013 /* Update the variant types of T. */
2015 void
2016 fixup_type_variants (tree type)
2018 if (!type)
2019 return;
2021 for (tree variant = TYPE_NEXT_VARIANT (type);
2022 variant;
2023 variant = TYPE_NEXT_VARIANT (variant))
2025 /* These fields are in the _TYPE part of the node, not in
2026 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2027 TYPE_HAS_USER_CONSTRUCTOR (variant) = TYPE_HAS_USER_CONSTRUCTOR (type);
2028 TYPE_NEEDS_CONSTRUCTING (variant) = TYPE_NEEDS_CONSTRUCTING (type);
2029 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variant)
2030 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2032 TYPE_POLYMORPHIC_P (variant) = TYPE_POLYMORPHIC_P (type);
2033 CLASSTYPE_FINAL (variant) = CLASSTYPE_FINAL (type);
2035 TYPE_BINFO (variant) = TYPE_BINFO (type);
2037 /* Copy whatever these are holding today. */
2038 TYPE_VFIELD (variant) = TYPE_VFIELD (type);
2039 TYPE_FIELDS (variant) = TYPE_FIELDS (type);
2041 TYPE_SIZE (variant) = TYPE_SIZE (type);
2042 TYPE_SIZE_UNIT (variant) = TYPE_SIZE_UNIT (type);
2044 if (!TYPE_USER_ALIGN (variant)
2045 || TYPE_NAME (variant) == TYPE_NAME (type)
2046 || TYPE_ALIGN_RAW (variant) < TYPE_ALIGN_RAW (type))
2048 TYPE_ALIGN_RAW (variant) = TYPE_ALIGN_RAW (type);
2049 TYPE_USER_ALIGN (variant) = TYPE_USER_ALIGN (type);
2052 TYPE_PRECISION (variant) = TYPE_PRECISION (type);
2053 TYPE_MODE_RAW (variant) = TYPE_MODE_RAW (type);
2054 TYPE_EMPTY_P (variant) = TYPE_EMPTY_P (type);
2058 /* KLASS is a class that we're applying may_alias to after the body is
2059 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2060 canonical type(s) will be implicitly updated. */
2062 static void
2063 fixup_may_alias (tree klass)
2065 tree t, v;
2067 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2068 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2069 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2070 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2071 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2072 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2075 /* Early variant fixups: we apply attributes at the beginning of the class
2076 definition, and we need to fix up any variants that have already been
2077 made via elaborated-type-specifier so that check_qualified_type works. */
2079 void
2080 fixup_attribute_variants (tree t)
2082 tree variants;
2084 if (!t)
2085 return;
2087 tree attrs = TYPE_ATTRIBUTES (t);
2088 unsigned align = TYPE_ALIGN (t);
2089 bool user_align = TYPE_USER_ALIGN (t);
2090 bool may_alias = lookup_attribute ("may_alias", attrs);
2091 bool packed = TYPE_PACKED (t);
2093 if (may_alias)
2094 fixup_may_alias (t);
2096 for (variants = TYPE_NEXT_VARIANT (t);
2097 variants;
2098 variants = TYPE_NEXT_VARIANT (variants))
2100 /* These are the two fields that check_qualified_type looks at and
2101 are affected by attributes. */
2102 TYPE_ATTRIBUTES (variants) = attrs;
2103 unsigned valign = align;
2104 if (TYPE_USER_ALIGN (variants))
2105 valign = MAX (valign, TYPE_ALIGN (variants));
2106 else
2107 TYPE_USER_ALIGN (variants) = user_align;
2108 SET_TYPE_ALIGN (variants, valign);
2109 TYPE_PACKED (variants) = packed;
2110 if (may_alias)
2111 fixup_may_alias (variants);
2115 /* Set memoizing fields and bits of T (and its variants) for later
2116 use. */
2118 static void
2119 finish_struct_bits (tree t)
2121 /* Fix up variants (if any). */
2122 fixup_type_variants (t);
2124 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2125 /* For a class w/o baseclasses, 'finish_struct' has set
2126 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2127 Similarly for a class whose base classes do not have vtables.
2128 When neither of these is true, we might have removed abstract
2129 virtuals (by providing a definition), added some (by declaring
2130 new ones), or redeclared ones from a base class. We need to
2131 recalculate what's really an abstract virtual at this point (by
2132 looking in the vtables). */
2133 get_pure_virtuals (t);
2135 /* If this type has a copy constructor or a destructor, force its
2136 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2137 nonzero. This will cause it to be passed by invisible reference
2138 and prevent it from being returned in a register. */
2139 if (type_has_nontrivial_copy_init (t)
2140 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2142 tree variants;
2143 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2144 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2146 SET_TYPE_MODE (variants, BLKmode);
2147 TREE_ADDRESSABLE (variants) = 1;
2152 /* Issue warnings about T having private constructors, but no friends,
2153 and so forth.
2155 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2156 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2157 non-private static member functions. */
2159 static void
2160 maybe_warn_about_overly_private_class (tree t)
2162 int has_member_fn = 0;
2163 int has_nonprivate_method = 0;
2164 bool nonprivate_ctor = false;
2166 if (!warn_ctor_dtor_privacy
2167 /* If the class has friends, those entities might create and
2168 access instances, so we should not warn. */
2169 || (CLASSTYPE_FRIEND_CLASSES (t)
2170 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2171 /* We will have warned when the template was declared; there's
2172 no need to warn on every instantiation. */
2173 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2174 /* There's no reason to even consider warning about this
2175 class. */
2176 return;
2178 /* We only issue one warning, if more than one applies, because
2179 otherwise, on code like:
2181 class A {
2182 // Oops - forgot `public:'
2183 A();
2184 A(const A&);
2185 ~A();
2188 we warn several times about essentially the same problem. */
2190 /* Check to see if all (non-constructor, non-destructor) member
2191 functions are private. (Since there are no friends or
2192 non-private statics, we can't ever call any of the private member
2193 functions.) */
2194 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2195 if (TREE_CODE (fn) == USING_DECL
2196 && DECL_NAME (fn) == ctor_identifier
2197 && !TREE_PRIVATE (fn))
2198 nonprivate_ctor = true;
2199 else if (!DECL_DECLARES_FUNCTION_P (fn))
2200 /* Not a function. */;
2201 else if (DECL_ARTIFICIAL (fn))
2202 /* We're not interested in compiler-generated methods; they don't
2203 provide any way to call private members. */;
2204 else if (!TREE_PRIVATE (fn))
2206 if (DECL_STATIC_FUNCTION_P (fn))
2207 /* A non-private static member function is just like a
2208 friend; it can create and invoke private member
2209 functions, and be accessed without a class
2210 instance. */
2211 return;
2213 has_nonprivate_method = 1;
2214 /* Keep searching for a static member function. */
2216 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2217 has_member_fn = 1;
2219 if (!has_nonprivate_method && has_member_fn)
2221 /* There are no non-private methods, and there's at least one
2222 private member function that isn't a constructor or
2223 destructor. (If all the private members are
2224 constructors/destructors we want to use the code below that
2225 issues error messages specifically referring to
2226 constructors/destructors.) */
2227 unsigned i;
2228 tree binfo = TYPE_BINFO (t);
2230 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2231 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2233 has_nonprivate_method = 1;
2234 break;
2236 if (!has_nonprivate_method)
2238 warning (OPT_Wctor_dtor_privacy,
2239 "all member functions in class %qT are private", t);
2240 return;
2244 /* Even if some of the member functions are non-private, the class
2245 won't be useful for much if all the constructors or destructors
2246 are private: such an object can never be created or destroyed. */
2247 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2248 if (TREE_PRIVATE (dtor))
2250 warning (OPT_Wctor_dtor_privacy,
2251 "%q#T only defines a private destructor and has no friends",
2253 return;
2256 /* Warn about classes that have private constructors and no friends. */
2257 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2258 /* Implicitly generated constructors are always public. */
2259 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2261 tree copy_or_move = NULL_TREE;
2263 /* If a non-template class does not define a copy
2264 constructor, one is defined for it, enabling it to avoid
2265 this warning. For a template class, this does not
2266 happen, and so we would normally get a warning on:
2268 template <class T> class C { private: C(); };
2270 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2271 complete non-template or fully instantiated classes have this
2272 flag set. */
2273 if (!TYPE_HAS_COPY_CTOR (t))
2274 nonprivate_ctor = true;
2275 else
2276 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
2277 if (TREE_PRIVATE (fn))
2278 continue;
2279 else if (copy_fn_p (fn) || move_fn_p (fn))
2280 /* Ideally, we wouldn't count any constructor that takes
2281 an argument of the class type as a parameter, because
2282 such things cannot be used to construct an instance of
2283 the class unless you already have one. */
2284 copy_or_move = fn;
2285 else
2287 nonprivate_ctor = true;
2288 break;
2291 if (!nonprivate_ctor)
2293 bool w = warning (OPT_Wctor_dtor_privacy,
2294 "%q#T only defines private constructors and has "
2295 "no friends", t);
2296 if (w && copy_or_move)
2297 inform (DECL_SOURCE_LOCATION (copy_or_move),
2298 "%q#D is public, but requires an existing %q#T object",
2299 copy_or_move, t);
2300 return;
2305 /* Make BINFO's vtable have N entries, including RTTI entries,
2306 vbase and vcall offsets, etc. Set its type and call the back end
2307 to lay it out. */
2309 static void
2310 layout_vtable_decl (tree binfo, int n)
2312 tree atype;
2313 tree vtable;
2315 atype = build_array_of_n_type (vtable_entry_type, n);
2316 layout_type (atype);
2318 /* We may have to grow the vtable. */
2319 vtable = get_vtbl_decl_for_binfo (binfo);
2320 if (!same_type_p (TREE_TYPE (vtable), atype))
2322 TREE_TYPE (vtable) = atype;
2323 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2324 layout_decl (vtable, 0);
2328 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2329 have the same signature. */
2332 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2334 /* One destructor overrides another if they are the same kind of
2335 destructor. */
2336 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2337 && special_function_p (base_fndecl) == special_function_p (fndecl))
2338 return 1;
2339 /* But a non-destructor never overrides a destructor, nor vice
2340 versa, nor do different kinds of destructors override
2341 one-another. For example, a complete object destructor does not
2342 override a deleting destructor. */
2343 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2344 return 0;
2346 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2347 || (DECL_CONV_FN_P (fndecl)
2348 && DECL_CONV_FN_P (base_fndecl)
2349 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2350 DECL_CONV_FN_TYPE (base_fndecl))))
2352 tree fntype = TREE_TYPE (fndecl);
2353 tree base_fntype = TREE_TYPE (base_fndecl);
2354 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2355 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2356 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2357 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2358 return 1;
2360 return 0;
2363 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2364 subobject. */
2366 static bool
2367 base_derived_from (tree derived, tree base)
2369 tree probe;
2371 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2373 if (probe == derived)
2374 return true;
2375 else if (BINFO_VIRTUAL_P (probe))
2376 /* If we meet a virtual base, we can't follow the inheritance
2377 any more. See if the complete type of DERIVED contains
2378 such a virtual base. */
2379 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2380 != NULL_TREE);
2382 return false;
2385 struct find_final_overrider_data {
2386 /* The function for which we are trying to find a final overrider. */
2387 tree fn;
2388 /* The base class in which the function was declared. */
2389 tree declaring_base;
2390 /* The candidate overriders. */
2391 tree candidates;
2392 /* Path to most derived. */
2393 auto_vec<tree> path;
2396 /* Add the overrider along the current path to FFOD->CANDIDATES.
2397 Returns true if an overrider was found; false otherwise. */
2399 static bool
2400 dfs_find_final_overrider_1 (tree binfo,
2401 find_final_overrider_data *ffod,
2402 unsigned depth)
2404 tree method;
2406 /* If BINFO is not the most derived type, try a more derived class.
2407 A definition there will overrider a definition here. */
2408 if (depth)
2410 depth--;
2411 if (dfs_find_final_overrider_1
2412 (ffod->path[depth], ffod, depth))
2413 return true;
2416 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2417 if (method)
2419 tree *candidate = &ffod->candidates;
2421 /* Remove any candidates overridden by this new function. */
2422 while (*candidate)
2424 /* If *CANDIDATE overrides METHOD, then METHOD
2425 cannot override anything else on the list. */
2426 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2427 return true;
2428 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2429 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2430 *candidate = TREE_CHAIN (*candidate);
2431 else
2432 candidate = &TREE_CHAIN (*candidate);
2435 /* Add the new function. */
2436 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2437 return true;
2440 return false;
2443 /* Called from find_final_overrider via dfs_walk. */
2445 static tree
2446 dfs_find_final_overrider_pre (tree binfo, void *data)
2448 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2450 if (binfo == ffod->declaring_base)
2451 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2452 ffod->path.safe_push (binfo);
2454 return NULL_TREE;
2457 static tree
2458 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2460 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2461 ffod->path.pop ();
2463 return NULL_TREE;
2466 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2467 FN and whose TREE_VALUE is the binfo for the base where the
2468 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2469 DERIVED) is the base object in which FN is declared. */
2471 static tree
2472 find_final_overrider (tree derived, tree binfo, tree fn)
2474 find_final_overrider_data ffod;
2476 /* Getting this right is a little tricky. This is valid:
2478 struct S { virtual void f (); };
2479 struct T { virtual void f (); };
2480 struct U : public S, public T { };
2482 even though calling `f' in `U' is ambiguous. But,
2484 struct R { virtual void f(); };
2485 struct S : virtual public R { virtual void f (); };
2486 struct T : virtual public R { virtual void f (); };
2487 struct U : public S, public T { };
2489 is not -- there's no way to decide whether to put `S::f' or
2490 `T::f' in the vtable for `R'.
2492 The solution is to look at all paths to BINFO. If we find
2493 different overriders along any two, then there is a problem. */
2494 if (DECL_THUNK_P (fn))
2495 fn = THUNK_TARGET (fn);
2497 /* Determine the depth of the hierarchy. */
2498 ffod.fn = fn;
2499 ffod.declaring_base = binfo;
2500 ffod.candidates = NULL_TREE;
2501 ffod.path.create (30);
2503 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2504 dfs_find_final_overrider_post, &ffod);
2506 /* If there was no winner, issue an error message. */
2507 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2508 return error_mark_node;
2510 return ffod.candidates;
2513 /* Return the index of the vcall offset for FN when TYPE is used as a
2514 virtual base. */
2516 static tree
2517 get_vcall_index (tree fn, tree type)
2519 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2520 tree_pair_p p;
2521 unsigned ix;
2523 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2524 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2525 || same_signature_p (fn, p->purpose))
2526 return p->value;
2528 /* There should always be an appropriate index. */
2529 gcc_unreachable ();
2532 /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2533 overrider at that index in the vtable. This should only be used when we
2534 know that BINFO is correct for the dynamic type of the object. */
2536 tree
2537 lookup_vfn_in_binfo (tree idx, tree binfo)
2539 int ix = tree_to_shwi (idx);
2540 if (TARGET_VTABLE_USES_DESCRIPTORS)
2541 ix /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
2542 while (BINFO_PRIMARY_P (binfo))
2543 /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2544 class that actually owns the vtable. */
2545 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2546 tree virtuals = BINFO_VIRTUALS (binfo);
2547 return TREE_VALUE (chain_index (ix, virtuals));
2550 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2551 dominated by T. FN is the old function; VIRTUALS points to the
2552 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2553 of that entry in the list. */
2555 static void
2556 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2557 unsigned ix)
2559 tree b;
2560 tree overrider;
2561 tree delta;
2562 tree virtual_base;
2563 tree first_defn;
2564 tree overrider_fn, overrider_target;
2565 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2566 tree over_return, base_return;
2567 bool lost = false;
2569 /* Find the nearest primary base (possibly binfo itself) which defines
2570 this function; this is the class the caller will convert to when
2571 calling FN through BINFO. */
2572 for (b = binfo; ; b = get_primary_binfo (b))
2574 gcc_assert (b);
2575 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2576 break;
2578 /* The nearest definition is from a lost primary. */
2579 if (BINFO_LOST_PRIMARY_P (b))
2580 lost = true;
2582 first_defn = b;
2584 /* Find the final overrider. */
2585 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2586 if (overrider == error_mark_node)
2588 error ("no unique final overrider for %qD in %qT", target_fn, t);
2589 return;
2591 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2593 /* Check for adjusting covariant return types. */
2594 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2595 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2597 if (INDIRECT_TYPE_P (over_return)
2598 && TREE_CODE (over_return) == TREE_CODE (base_return)
2599 && CLASS_TYPE_P (TREE_TYPE (over_return))
2600 && CLASS_TYPE_P (TREE_TYPE (base_return))
2601 /* If the overrider is invalid, don't even try. */
2602 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2604 /* If FN is a covariant thunk, we must figure out the adjustment
2605 to the final base FN was converting to. As OVERRIDER_TARGET might
2606 also be converting to the return type of FN, we have to
2607 combine the two conversions here. */
2608 tree fixed_offset, virtual_offset;
2610 over_return = TREE_TYPE (over_return);
2611 base_return = TREE_TYPE (base_return);
2613 if (DECL_THUNK_P (fn))
2615 gcc_assert (DECL_RESULT_THUNK_P (fn));
2616 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2617 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2619 else
2620 fixed_offset = virtual_offset = NULL_TREE;
2622 if (virtual_offset)
2623 /* Find the equivalent binfo within the return type of the
2624 overriding function. We will want the vbase offset from
2625 there. */
2626 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2627 over_return);
2628 else if (!same_type_ignoring_top_level_qualifiers_p
2629 (over_return, base_return))
2631 /* There was no existing virtual thunk (which takes
2632 precedence). So find the binfo of the base function's
2633 return type within the overriding function's return type.
2634 Fortunately we know the covariancy is valid (it
2635 has already been checked), so we can just iterate along
2636 the binfos, which have been chained in inheritance graph
2637 order. Of course it is lame that we have to repeat the
2638 search here anyway -- we should really be caching pieces
2639 of the vtable and avoiding this repeated work. */
2640 tree thunk_binfo = NULL_TREE;
2641 tree base_binfo = TYPE_BINFO (base_return);
2643 /* Find the base binfo within the overriding function's
2644 return type. We will always find a thunk_binfo, except
2645 when the covariancy is invalid (which we will have
2646 already diagnosed). */
2647 if (base_binfo)
2648 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2649 thunk_binfo = TREE_CHAIN (thunk_binfo))
2650 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2651 BINFO_TYPE (base_binfo)))
2652 break;
2653 gcc_assert (thunk_binfo || errorcount);
2655 /* See if virtual inheritance is involved. */
2656 for (virtual_offset = thunk_binfo;
2657 virtual_offset;
2658 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2659 if (BINFO_VIRTUAL_P (virtual_offset))
2660 break;
2662 if (virtual_offset
2663 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2665 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2667 if (virtual_offset)
2669 /* We convert via virtual base. Adjust the fixed
2670 offset to be from there. */
2671 offset =
2672 size_diffop (offset,
2673 fold_convert (ssizetype,
2674 BINFO_OFFSET (virtual_offset)));
2676 if (fixed_offset)
2677 /* There was an existing fixed offset, this must be
2678 from the base just converted to, and the base the
2679 FN was thunking to. */
2680 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2681 else
2682 fixed_offset = offset;
2686 if (fixed_offset || virtual_offset)
2687 /* Replace the overriding function with a covariant thunk. We
2688 will emit the overriding function in its own slot as
2689 well. */
2690 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2691 fixed_offset, virtual_offset);
2693 else
2694 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2695 !DECL_THUNK_P (fn));
2697 /* If we need a covariant thunk, then we may need to adjust first_defn.
2698 The ABI specifies that the thunks emitted with a function are
2699 determined by which bases the function overrides, so we need to be
2700 sure that we're using a thunk for some overridden base; even if we
2701 know that the necessary this adjustment is zero, there may not be an
2702 appropriate zero-this-adjustment thunk for us to use since thunks for
2703 overriding virtual bases always use the vcall offset.
2705 Furthermore, just choosing any base that overrides this function isn't
2706 quite right, as this slot won't be used for calls through a type that
2707 puts a covariant thunk here. Calling the function through such a type
2708 will use a different slot, and that slot is the one that determines
2709 the thunk emitted for that base.
2711 So, keep looking until we find the base that we're really overriding
2712 in this slot: the nearest primary base that doesn't use a covariant
2713 thunk in this slot. */
2714 if (overrider_target != overrider_fn)
2716 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2717 /* We already know that the overrider needs a covariant thunk. */
2718 b = get_primary_binfo (b);
2719 for (; ; b = get_primary_binfo (b))
2721 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2722 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2723 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2724 break;
2725 if (BINFO_LOST_PRIMARY_P (b))
2726 lost = true;
2728 first_defn = b;
2731 /* Assume that we will produce a thunk that convert all the way to
2732 the final overrider, and not to an intermediate virtual base. */
2733 virtual_base = NULL_TREE;
2735 /* See if we can convert to an intermediate virtual base first, and then
2736 use the vcall offset located there to finish the conversion. */
2737 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2739 /* If we find the final overrider, then we can stop
2740 walking. */
2741 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2742 BINFO_TYPE (TREE_VALUE (overrider))))
2743 break;
2745 /* If we find a virtual base, and we haven't yet found the
2746 overrider, then there is a virtual base between the
2747 declaring base (first_defn) and the final overrider. */
2748 if (BINFO_VIRTUAL_P (b))
2750 virtual_base = b;
2751 break;
2755 /* Compute the constant adjustment to the `this' pointer. The
2756 `this' pointer, when this function is called, will point at BINFO
2757 (or one of its primary bases, which are at the same offset). */
2758 if (virtual_base)
2759 /* The `this' pointer needs to be adjusted from the declaration to
2760 the nearest virtual base. */
2761 delta = size_diffop_loc (input_location,
2762 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2763 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2764 else if (lost)
2765 /* If the nearest definition is in a lost primary, we don't need an
2766 entry in our vtable. Except possibly in a constructor vtable,
2767 if we happen to get our primary back. In that case, the offset
2768 will be zero, as it will be a primary base. */
2769 delta = size_zero_node;
2770 else
2771 /* The `this' pointer needs to be adjusted from pointing to
2772 BINFO to pointing at the base where the final overrider
2773 appears. */
2774 delta = size_diffop_loc (input_location,
2775 fold_convert (ssizetype,
2776 BINFO_OFFSET (TREE_VALUE (overrider))),
2777 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2779 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2781 if (virtual_base)
2782 BV_VCALL_INDEX (*virtuals)
2783 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2784 else
2785 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2787 BV_LOST_PRIMARY (*virtuals) = lost;
2790 /* Called from modify_all_vtables via dfs_walk. */
2792 static tree
2793 dfs_modify_vtables (tree binfo, void* data)
2795 tree t = (tree) data;
2796 tree virtuals;
2797 tree old_virtuals;
2798 unsigned ix;
2800 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2801 /* A base without a vtable needs no modification, and its bases
2802 are uninteresting. */
2803 return dfs_skip_bases;
2805 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2806 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2807 /* Don't do the primary vtable, if it's new. */
2808 return NULL_TREE;
2810 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2811 /* There's no need to modify the vtable for a non-virtual primary
2812 base; we're not going to use that vtable anyhow. We do still
2813 need to do this for virtual primary bases, as they could become
2814 non-primary in a construction vtable. */
2815 return NULL_TREE;
2817 make_new_vtable (t, binfo);
2819 /* Now, go through each of the virtual functions in the virtual
2820 function table for BINFO. Find the final overrider, and update
2821 the BINFO_VIRTUALS list appropriately. */
2822 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2823 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2824 virtuals;
2825 ix++, virtuals = TREE_CHAIN (virtuals),
2826 old_virtuals = TREE_CHAIN (old_virtuals))
2827 update_vtable_entry_for_fn (t,
2828 binfo,
2829 BV_FN (old_virtuals),
2830 &virtuals, ix);
2832 return NULL_TREE;
2835 /* Update all of the primary and secondary vtables for T. Create new
2836 vtables as required, and initialize their RTTI information. Each
2837 of the functions in VIRTUALS is declared in T and may override a
2838 virtual function from a base class; find and modify the appropriate
2839 entries to point to the overriding functions. Returns a list, in
2840 declaration order, of the virtual functions that are declared in T,
2841 but do not appear in the primary base class vtable, and which
2842 should therefore be appended to the end of the vtable for T. */
2844 static tree
2845 modify_all_vtables (tree t, tree virtuals)
2847 tree binfo = TYPE_BINFO (t);
2848 tree *fnsp;
2850 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2851 if (TYPE_CONTAINS_VPTR_P (t))
2852 get_vtable_decl (t, false);
2854 /* Update all of the vtables. */
2855 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2857 /* Add virtual functions not already in our primary vtable. These
2858 will be both those introduced by this class, and those overridden
2859 from secondary bases. It does not include virtuals merely
2860 inherited from secondary bases. */
2861 for (fnsp = &virtuals; *fnsp; )
2863 tree fn = TREE_VALUE (*fnsp);
2865 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2866 || DECL_VINDEX (fn) == error_mark_node)
2868 /* We don't need to adjust the `this' pointer when
2869 calling this function. */
2870 BV_DELTA (*fnsp) = integer_zero_node;
2871 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2873 /* This is a function not already in our vtable. Keep it. */
2874 fnsp = &TREE_CHAIN (*fnsp);
2876 else
2877 /* We've already got an entry for this function. Skip it. */
2878 *fnsp = TREE_CHAIN (*fnsp);
2881 return virtuals;
2884 /* Get the base virtual function declarations in T that have the
2885 indicated NAME. */
2887 static void
2888 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2890 bool found_decls = false;
2892 /* Find virtual functions in T with the indicated NAME. */
2893 for (tree method : ovl_range (get_class_binding (t, name)))
2895 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2897 base_fndecls->safe_push (method);
2898 found_decls = true;
2902 if (found_decls)
2903 return;
2905 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2906 for (int i = 0; i < n_baseclasses; i++)
2908 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2909 get_basefndecls (name, basetype, base_fndecls);
2913 /* If this method overrides a virtual method from a base, then mark
2914 this member function as being virtual as well. Do 'final' and
2915 'override' checks too. */
2917 void
2918 check_for_override (tree decl, tree ctype)
2920 if (TREE_CODE (decl) == TEMPLATE_DECL)
2921 /* In [temp.mem] we have:
2923 A specialization of a member function template does not
2924 override a virtual function from a base class. */
2925 return;
2927 /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
2928 used for a vfunc. That avoids the expensive look_for_overrides
2929 call that when we know there's nothing to find. As conversion
2930 operators for the same type can have distinct identifiers, we
2931 cannot optimize those in that way. */
2932 if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2933 || DECL_CONV_FN_P (decl))
2934 && look_for_overrides (ctype, decl)
2935 /* Check staticness after we've checked if we 'override'. */
2936 && !DECL_STATIC_FUNCTION_P (decl))
2938 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2939 the error_mark_node so that we know it is an overriding
2940 function. */
2941 DECL_VINDEX (decl) = decl;
2943 if (warn_override
2944 && !DECL_OVERRIDE_P (decl)
2945 && !DECL_FINAL_P (decl)
2946 && !DECL_DESTRUCTOR_P (decl))
2947 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2948 "%qD can be marked override", decl);
2950 else if (DECL_OVERRIDE_P (decl))
2951 error ("%q+#D marked %<override%>, but does not override", decl);
2953 if (DECL_VIRTUAL_P (decl))
2955 /* Remember this identifier is virtual name. */
2956 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
2958 if (!DECL_VINDEX (decl))
2959 /* It's a new vfunc. */
2960 DECL_VINDEX (decl) = error_mark_node;
2962 if (DECL_DESTRUCTOR_P (decl))
2963 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2965 else if (DECL_FINAL_P (decl))
2966 error ("%q+#D marked %<final%>, but is not virtual", decl);
2969 /* Warn about hidden virtual functions that are not overridden in t.
2970 We know that constructors and destructors don't apply. */
2972 static void
2973 warn_hidden (tree t)
2975 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2976 for (unsigned ix = member_vec->length (); ix--;)
2978 tree fns = (*member_vec)[ix];
2980 if (!OVL_P (fns))
2981 continue;
2983 tree name = OVL_NAME (fns);
2984 auto_vec<tree, 20> base_fndecls;
2985 tree base_binfo;
2986 tree binfo;
2987 unsigned j;
2989 /* Iterate through all of the base classes looking for possibly
2990 hidden functions. */
2991 for (binfo = TYPE_BINFO (t), j = 0;
2992 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2994 tree basetype = BINFO_TYPE (base_binfo);
2995 get_basefndecls (name, basetype, &base_fndecls);
2998 /* If there are no functions to hide, continue. */
2999 if (base_fndecls.is_empty ())
3000 continue;
3002 /* Remove any overridden functions. */
3003 for (tree fndecl : ovl_range (fns))
3005 if (TREE_CODE (fndecl) == FUNCTION_DECL
3006 && DECL_VINDEX (fndecl))
3008 /* If the method from the base class has the same
3009 signature as the method from the derived class, it
3010 has been overridden. */
3011 for (size_t k = 0; k < base_fndecls.length (); k++)
3012 if (base_fndecls[k]
3013 && same_signature_p (fndecl, base_fndecls[k]))
3014 base_fndecls[k] = NULL_TREE;
3018 /* Now give a warning for all base functions without overriders,
3019 as they are hidden. */
3020 tree base_fndecl;
3021 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
3022 if (base_fndecl)
3024 auto_diagnostic_group d;
3025 /* Here we know it is a hider, and no overrider exists. */
3026 if (warning_at (location_of (base_fndecl),
3027 OPT_Woverloaded_virtual,
3028 "%qD was hidden", base_fndecl))
3029 inform (location_of (fns), " by %qD", fns);
3034 /* Recursive helper for finish_struct_anon. */
3036 static void
3037 finish_struct_anon_r (tree field)
3039 for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
3041 /* We're generally only interested in entities the user
3042 declared, but we also find nested classes by noticing
3043 the TYPE_DECL that we create implicitly. You're
3044 allowed to put one anonymous union inside another,
3045 though, so we explicitly tolerate that. We use
3046 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3047 we also allow unnamed types used for defining fields. */
3048 if (DECL_ARTIFICIAL (elt)
3049 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3050 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3051 continue;
3053 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3054 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3056 /* Recurse into the anonymous aggregates to correctly handle
3057 access control (c++/24926):
3059 class A {
3060 union {
3061 union {
3062 int i;
3067 int j=A().i; */
3068 if (DECL_NAME (elt) == NULL_TREE
3069 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3070 finish_struct_anon_r (elt);
3074 /* Fix up any anonymous union/struct members of T. */
3076 static void
3077 finish_struct_anon (tree t)
3079 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3081 if (TREE_STATIC (field))
3082 continue;
3083 if (TREE_CODE (field) != FIELD_DECL)
3084 continue;
3086 if (DECL_NAME (field) == NULL_TREE
3087 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3088 finish_struct_anon_r (field);
3092 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3093 will be used later during class template instantiation.
3094 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3095 a non-static member data (FIELD_DECL), a member function
3096 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3097 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3098 When FRIEND_P is nonzero, T is either a friend class
3099 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3100 (FUNCTION_DECL, TEMPLATE_DECL). */
3102 void
3103 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3105 if (CLASSTYPE_TEMPLATE_INFO (type)
3106 && TREE_CODE (t) != CONST_DECL)
3108 tree purpose = friend_p ? NULL_TREE : type;
3110 CLASSTYPE_DECL_LIST (type)
3111 = tree_cons (purpose, t, CLASSTYPE_DECL_LIST (type));
3115 /* This function is called from declare_virt_assop_and_dtor via
3116 dfs_walk_all.
3118 DATA is a type that direcly or indirectly inherits the base
3119 represented by BINFO. If BINFO contains a virtual assignment [copy
3120 assignment or move assigment] operator or a virtual constructor,
3121 declare that function in DATA if it hasn't been already declared. */
3123 static tree
3124 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3126 tree bv, fn, t = (tree)data;
3127 tree opname = assign_op_identifier;
3129 gcc_assert (t && CLASS_TYPE_P (t));
3130 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3132 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3133 /* A base without a vtable needs no modification, and its bases
3134 are uninteresting. */
3135 return dfs_skip_bases;
3137 if (BINFO_PRIMARY_P (binfo))
3138 /* If this is a primary base, then we have already looked at the
3139 virtual functions of its vtable. */
3140 return NULL_TREE;
3142 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3144 fn = BV_FN (bv);
3146 if (DECL_NAME (fn) == opname)
3148 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3149 lazily_declare_fn (sfk_copy_assignment, t);
3150 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3151 lazily_declare_fn (sfk_move_assignment, t);
3153 else if (DECL_DESTRUCTOR_P (fn)
3154 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3155 lazily_declare_fn (sfk_destructor, t);
3158 return NULL_TREE;
3161 /* If the class type T has a direct or indirect base that contains a
3162 virtual assignment operator or a virtual destructor, declare that
3163 function in T if it hasn't been already declared. */
3165 static void
3166 declare_virt_assop_and_dtor (tree t)
3168 if (!(TYPE_POLYMORPHIC_P (t)
3169 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3170 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3171 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3172 return;
3174 dfs_walk_all (TYPE_BINFO (t),
3175 dfs_declare_virt_assop_and_dtor,
3176 NULL, t);
3179 /* Declare the inheriting constructor for class T inherited from base
3180 constructor CTOR with the parameter array PARMS of size NPARMS. */
3182 static void
3183 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3185 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3187 /* We don't declare an inheriting ctor that would be a default,
3188 copy or move ctor for derived or base. */
3189 if (nparms == 0)
3190 return;
3191 if (nparms == 1
3192 && TYPE_REF_P (parms[0]))
3194 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3195 if (parm == t || parm == DECL_CONTEXT (ctor))
3196 return;
3199 tree parmlist = void_list_node;
3200 for (int i = nparms - 1; i >= 0; i--)
3201 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3202 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3203 t, false, ctor, parmlist);
3205 if (add_method (t, fn, false))
3207 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3208 TYPE_FIELDS (t) = fn;
3212 /* Declare all the inheriting constructors for class T inherited from base
3213 constructor CTOR. */
3215 static void
3216 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3218 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3220 if (flag_new_inheriting_ctors)
3222 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3223 t, /*const*/false, ctor, parms);
3224 add_method (t, ctor, using_decl != NULL_TREE);
3225 return;
3228 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3229 int i = 0;
3230 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3232 if (TREE_PURPOSE (parms))
3233 one_inheriting_sig (t, ctor, new_parms, i);
3234 new_parms[i++] = TREE_VALUE (parms);
3236 one_inheriting_sig (t, ctor, new_parms, i);
3237 if (parms == NULL_TREE)
3239 auto_diagnostic_group d;
3240 if (warning (OPT_Winherited_variadic_ctor,
3241 "the ellipsis in %qD is not inherited", ctor))
3242 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3246 /* Create default constructors, assignment operators, and so forth for
3247 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3248 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3249 the class cannot have a default constructor, copy constructor
3250 taking a const reference argument, or an assignment operator taking
3251 a const reference, respectively. */
3253 static void
3254 add_implicitly_declared_members (tree t, tree* access_decls,
3255 int cant_have_const_cctor,
3256 int cant_have_const_assignment)
3258 /* Destructor. */
3259 if (!CLASSTYPE_DESTRUCTOR (t))
3260 /* In general, we create destructors lazily. */
3261 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3263 bool move_ok = false;
3264 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3265 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3266 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3267 move_ok = true;
3269 /* [class.ctor]
3271 If there is no user-declared constructor for a class, a default
3272 constructor is implicitly declared. */
3273 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3275 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3276 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3277 if (cxx_dialect >= cxx11)
3278 TYPE_HAS_CONSTEXPR_CTOR (t)
3279 /* Don't force the declaration to get a hard answer; if the
3280 definition would have made the class non-literal, it will still be
3281 non-literal because of the base or member in question, and that
3282 gives a better diagnostic. */
3283 = type_maybe_constexpr_default_constructor (t);
3286 /* [class.ctor]
3288 If a class definition does not explicitly declare a copy
3289 constructor, one is declared implicitly. */
3290 if (! TYPE_HAS_COPY_CTOR (t))
3292 TYPE_HAS_COPY_CTOR (t) = 1;
3293 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3294 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3295 if (move_ok)
3296 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3299 /* If there is no assignment operator, one will be created if and
3300 when it is needed. For now, just record whether or not the type
3301 of the parameter to the assignment operator will be a const or
3302 non-const reference. */
3303 if (!TYPE_HAS_COPY_ASSIGN (t))
3305 TYPE_HAS_COPY_ASSIGN (t) = 1;
3306 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3307 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3308 if (move_ok && !LAMBDA_TYPE_P (t))
3309 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3312 /* We can't be lazy about declaring functions that might override
3313 a virtual function from a base class. */
3314 declare_virt_assop_and_dtor (t);
3316 /* If the class definition does not explicitly declare an == operator
3317 function, but declares a defaulted three-way comparison operator function,
3318 an == operator function is declared implicitly. */
3319 if (!classtype_has_op (t, EQ_EXPR))
3320 if (tree space = classtype_has_defaulted_op (t, SPACESHIP_EXPR))
3322 tree eq = implicitly_declare_fn (sfk_comparison, t, false, space,
3323 NULL_TREE);
3324 bool is_friend = DECL_CONTEXT (space) != t;
3325 if (is_friend)
3326 do_friend (NULL_TREE, DECL_NAME (eq), eq,
3327 NO_SPECIAL, true);
3328 else
3330 add_method (t, eq, false);
3331 DECL_CHAIN (eq) = TYPE_FIELDS (t);
3332 TYPE_FIELDS (t) = eq;
3334 maybe_add_class_template_decl_list (t, eq, is_friend);
3337 while (*access_decls)
3339 tree using_decl = TREE_VALUE (*access_decls);
3340 tree decl = USING_DECL_DECLS (using_decl);
3341 if (DECL_NAME (using_decl) == ctor_identifier)
3343 /* declare, then remove the decl */
3344 tree ctor_list = decl;
3345 location_t loc = input_location;
3346 input_location = DECL_SOURCE_LOCATION (using_decl);
3347 for (tree fn : ovl_range (ctor_list))
3348 one_inherited_ctor (fn, t, using_decl);
3349 *access_decls = TREE_CHAIN (*access_decls);
3350 input_location = loc;
3352 else
3353 access_decls = &TREE_CHAIN (*access_decls);
3357 /* Cache of enum_min_precision values. */
3358 static GTY((deletable)) hash_map<tree, int> *enum_to_min_precision;
3360 /* Return the minimum precision of a bit-field needed to store all
3361 enumerators of ENUMERAL_TYPE TYPE. */
3363 static int
3364 enum_min_precision (tree type)
3366 type = TYPE_MAIN_VARIANT (type);
3367 /* For unscoped enums without fixed underlying type and without mode
3368 attribute we can just use precision of the underlying type. */
3369 if (UNSCOPED_ENUM_P (type)
3370 && !ENUM_FIXED_UNDERLYING_TYPE_P (type)
3371 && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type)))
3372 return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3374 if (enum_to_min_precision == NULL)
3375 enum_to_min_precision = hash_map<tree, int>::create_ggc (37);
3377 bool existed;
3378 int &prec = enum_to_min_precision->get_or_insert (type, &existed);
3379 if (existed)
3380 return prec;
3382 tree minnode, maxnode;
3383 if (TYPE_VALUES (type))
3385 minnode = maxnode = NULL_TREE;
3386 for (tree values = TYPE_VALUES (type);
3387 values; values = TREE_CHAIN (values))
3389 tree decl = TREE_VALUE (values);
3390 tree value = DECL_INITIAL (decl);
3391 if (value == error_mark_node)
3392 value = integer_zero_node;
3393 if (!minnode)
3394 minnode = maxnode = value;
3395 else if (tree_int_cst_lt (maxnode, value))
3396 maxnode = value;
3397 else if (tree_int_cst_lt (value, minnode))
3398 minnode = value;
3401 else
3402 minnode = maxnode = integer_zero_node;
3404 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
3405 int lowprec = tree_int_cst_min_precision (minnode, sgn);
3406 int highprec = tree_int_cst_min_precision (maxnode, sgn);
3407 prec = MAX (lowprec, highprec);
3408 return prec;
3411 /* FIELD is a bit-field. We are finishing the processing for its
3412 enclosing type. Issue any appropriate messages and set appropriate
3413 flags. Returns false if an error has been diagnosed. */
3415 static bool
3416 check_bitfield_decl (tree field)
3418 tree type = TREE_TYPE (field);
3419 tree w;
3421 /* Extract the declared width of the bitfield, which has been
3422 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3423 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3424 gcc_assert (w != NULL_TREE);
3425 /* Remove the bit-field width indicator so that the rest of the
3426 compiler does not treat that value as a qualifier. */
3427 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3429 /* Detect invalid bit-field type. */
3430 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3432 error_at (DECL_SOURCE_LOCATION (field),
3433 "bit-field %q#D with non-integral type %qT", field, type);
3434 w = error_mark_node;
3436 else
3438 location_t loc = input_location;
3439 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3440 STRIP_NOPS (w);
3442 /* detect invalid field size. */
3443 input_location = DECL_SOURCE_LOCATION (field);
3444 w = cxx_constant_value (w);
3445 input_location = loc;
3447 if (TREE_CODE (w) != INTEGER_CST)
3449 error ("bit-field %q+D width not an integer constant", field);
3450 w = error_mark_node;
3452 else if (tree_int_cst_sgn (w) < 0)
3454 error ("negative width in bit-field %q+D", field);
3455 w = error_mark_node;
3457 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3459 error ("zero width for bit-field %q+D", field);
3460 w = error_mark_node;
3462 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3463 && TREE_CODE (type) != BOOLEAN_TYPE
3464 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3465 || ((TREE_CODE (type) == ENUMERAL_TYPE
3466 || TREE_CODE (type) == BOOLEAN_TYPE)
3467 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3468 warning_at (DECL_SOURCE_LOCATION (field), 0,
3469 "width of %qD exceeds its type", field);
3470 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3472 int prec = enum_min_precision (type);
3473 if (compare_tree_int (w, prec) < 0)
3474 warning_at (DECL_SOURCE_LOCATION (field), 0,
3475 "%qD is too small to hold all values of %q#T",
3476 field, type);
3480 if (w != error_mark_node)
3482 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3483 DECL_BIT_FIELD (field) = 1;
3484 return true;
3486 else
3488 /* Non-bit-fields are aligned for their type. */
3489 DECL_BIT_FIELD (field) = 0;
3490 CLEAR_DECL_C_BIT_FIELD (field);
3491 return false;
3495 /* FIELD is a non bit-field. We are finishing the processing for its
3496 enclosing type T. Issue any appropriate messages and set appropriate
3497 flags. */
3499 static bool
3500 check_field_decl (tree field,
3501 tree t,
3502 int* cant_have_const_ctor,
3503 int* no_const_asn_ref)
3505 tree type = strip_array_types (TREE_TYPE (field));
3506 bool any_default_members = false;
3508 /* In C++98 an anonymous union cannot contain any fields which would change
3509 the settings of CANT_HAVE_CONST_CTOR and friends. */
3510 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3512 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3513 structs. So, we recurse through their fields here. */
3514 else if (ANON_AGGR_TYPE_P (type))
3516 for (tree fields = TYPE_FIELDS (type); fields;
3517 fields = DECL_CHAIN (fields))
3518 if (TREE_CODE (fields) == FIELD_DECL)
3519 any_default_members |= check_field_decl (fields, t,
3520 cant_have_const_ctor,
3521 no_const_asn_ref);
3523 /* Check members with class type for constructors, destructors,
3524 etc. */
3525 else if (CLASS_TYPE_P (type))
3527 /* Never let anything with uninheritable virtuals
3528 make it through without complaint. */
3529 abstract_virtuals_error (field, type);
3531 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3533 static bool warned;
3534 int oldcount = errorcount;
3535 if (TYPE_NEEDS_CONSTRUCTING (type))
3536 error ("member %q+#D with constructor not allowed in union",
3537 field);
3538 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3539 error ("member %q+#D with destructor not allowed in union", field);
3540 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3541 error ("member %q+#D with copy assignment operator not allowed in union",
3542 field);
3543 if (!warned && errorcount > oldcount)
3545 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3546 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3547 warned = true;
3550 else
3552 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3553 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3554 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3555 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3556 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3557 || !TYPE_HAS_COPY_ASSIGN (type));
3558 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3559 || !TYPE_HAS_COPY_CTOR (type));
3560 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3561 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3562 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3563 || TYPE_HAS_COMPLEX_DFLT (type));
3566 if (TYPE_HAS_COPY_CTOR (type)
3567 && !TYPE_HAS_CONST_COPY_CTOR (type))
3568 *cant_have_const_ctor = 1;
3570 if (TYPE_HAS_COPY_ASSIGN (type)
3571 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3572 *no_const_asn_ref = 1;
3575 check_abi_tags (t, field);
3577 if (DECL_INITIAL (field) != NULL_TREE)
3578 /* `build_class_init_list' does not recognize
3579 non-FIELD_DECLs. */
3580 any_default_members = true;
3582 return any_default_members;
3585 /* Check the data members (both static and non-static), class-scoped
3586 typedefs, etc., appearing in the declaration of T. Issue
3587 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3588 declaration order) of access declarations; each TREE_VALUE in this
3589 list is a USING_DECL.
3591 In addition, set the following flags:
3593 EMPTY_P
3594 The class is empty, i.e., contains no non-static data members.
3596 CANT_HAVE_CONST_CTOR_P
3597 This class cannot have an implicitly generated copy constructor
3598 taking a const reference.
3600 CANT_HAVE_CONST_ASN_REF
3601 This class cannot have an implicitly generated assignment
3602 operator taking a const reference.
3604 All of these flags should be initialized before calling this
3605 function. */
3607 static void
3608 check_field_decls (tree t, tree *access_decls,
3609 int *cant_have_const_ctor_p,
3610 int *no_const_asn_ref_p)
3612 int cant_pack = 0;
3614 /* Assume there are no access declarations. */
3615 *access_decls = NULL_TREE;
3616 /* Effective C has things to say about classes with pointer members. */
3617 tree pointer_member = NULL_TREE;
3618 /* Default initialized members affect the whole class. */
3619 tree default_init_member = NULL_TREE;
3620 /* Lack of any non-static data member of non-volatile literal
3621 type affects a union. */
3622 bool found_nv_literal_p = false;
3623 /* Standard layout requires all FIELDS have same access. */
3624 int field_access = -1;
3626 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3628 tree type = TREE_TYPE (field);
3630 switch (TREE_CODE (field))
3632 default:
3633 gcc_unreachable ();
3635 case USING_DECL:
3636 /* Save the access declarations for our caller. */
3637 *access_decls = tree_cons (NULL_TREE, field, *access_decls);
3638 break;
3640 case TYPE_DECL:
3641 case TEMPLATE_DECL:
3642 break;
3644 case FUNCTION_DECL:
3645 /* FIXME: We should fold in the checking from check_methods. */
3646 break;
3648 case CONST_DECL:
3649 DECL_NONLOCAL (field) = 1;
3650 break;
3652 case VAR_DECL:
3653 if (TREE_CODE (t) == UNION_TYPE
3654 && cxx_dialect < cxx11)
3656 /* [class.union]
3658 (C++98) If a union contains a static data member,
3659 ... the program is ill-formed. */
3660 if (cxx_dialect < cxx11)
3661 error ("in C++98 %q+D may not be static because it is "
3662 "a member of a union", field);
3664 goto data_member;
3666 case FIELD_DECL:
3667 if (TREE_CODE (t) == UNION_TYPE)
3669 /* [class.union]
3671 If a union contains ... or a [non-static data] member
3672 of reference type, the program is ill-formed. */
3673 if (TYPE_REF_P (type))
3674 error ("non-static data member %q+D in a union may not "
3675 "have reference type %qT", field, type);
3678 data_member:
3679 /* Common VAR_DECL & FIELD_DECL processing. */
3680 DECL_CONTEXT (field) = t;
3681 DECL_NONLOCAL (field) = 1;
3683 /* Template instantiation can cause this. Perhaps this
3684 should be a specific instantiation check? */
3685 if (TREE_CODE (type) == FUNCTION_TYPE)
3687 error ("data member %q+D invalidly declared function type", field);
3688 type = build_pointer_type (type);
3689 TREE_TYPE (field) = type;
3691 else if (TREE_CODE (type) == METHOD_TYPE)
3693 error ("data member %q+D invalidly declared method type", field);
3694 type = build_pointer_type (type);
3695 TREE_TYPE (field) = type;
3698 break;
3701 if (TREE_CODE (field) != FIELD_DECL)
3702 continue;
3704 if (type == error_mark_node)
3705 continue;
3707 /* If it is not a union and at least one non-static data member is
3708 non-literal, the whole class becomes non-literal. Per Core/1453,
3709 volatile non-static data members and base classes are also not allowed.
3710 If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
3711 members.
3712 Note: if the type is incomplete we will complain later on. */
3713 if (COMPLETE_TYPE_P (type))
3715 if (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type))
3716 CLASSTYPE_LITERAL_P (t) = false;
3717 else
3718 found_nv_literal_p = true;
3721 int this_field_access = (TREE_PROTECTED (field) ? 1
3722 : TREE_PRIVATE (field) ? 2 : 0);
3723 if (field_access != this_field_access)
3725 /* A standard-layout class is a class that:
3727 ... has the same access control (Clause 11) for all
3728 non-static data members, */
3729 if (field_access < 0)
3730 field_access = this_field_access;
3731 else
3732 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3734 /* Aggregates must be public. */
3735 if (this_field_access)
3736 CLASSTYPE_NON_AGGREGATE (t) = 1;
3739 /* If this is of reference type, check if it needs an init. */
3740 if (TYPE_REF_P (type))
3742 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3743 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3744 if (DECL_INITIAL (field) == NULL_TREE)
3745 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3746 if (cxx_dialect < cxx11)
3748 /* ARM $12.6.2: [A member initializer list] (or, for an
3749 aggregate, initialization by a brace-enclosed list) is the
3750 only way to initialize non-static const and reference
3751 members. */
3752 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3753 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3757 type = strip_array_types (type);
3759 if (TYPE_PACKED (t))
3761 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3763 warning_at (DECL_SOURCE_LOCATION (field), 0,
3764 "ignoring packed attribute because of"
3765 " unpacked non-POD field %q#D", field);
3766 cant_pack = 1;
3768 else if (DECL_C_BIT_FIELD (field)
3769 || TYPE_ALIGN (TREE_TYPE (field)) > BITS_PER_UNIT)
3770 DECL_PACKED (field) = 1;
3773 if (DECL_C_BIT_FIELD (field)
3774 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field)))
3775 /* We don't treat zero-width bitfields as making a class
3776 non-empty. */
3778 else if (field_poverlapping_p (field)
3779 && is_empty_class (TREE_TYPE (field)))
3780 /* Empty data members also don't make a class non-empty. */
3781 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3782 else
3784 /* The class is non-empty. */
3785 CLASSTYPE_EMPTY_P (t) = 0;
3786 /* The class is not even nearly empty. */
3787 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3788 /* If one of the data members contains an empty class, so
3789 does T. */
3790 if (CLASS_TYPE_P (type)
3791 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3792 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3795 /* This is used by -Weffc++ (see below). Warn only for pointers
3796 to members which might hold dynamic memory. So do not warn
3797 for pointers to functions or pointers to members. */
3798 if (TYPE_PTR_P (type)
3799 && !TYPE_PTRFN_P (type))
3800 pointer_member = field;
3802 if (CLASS_TYPE_P (type))
3804 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3805 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3806 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3807 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3810 if (DECL_MUTABLE_P (field) || TYPE_HAS_MUTABLE_P (type))
3811 CLASSTYPE_HAS_MUTABLE (t) = 1;
3813 if (DECL_MUTABLE_P (field))
3815 if (TYPE_REF_P (type))
3816 error ("member %q+D cannot be declared as a %<mutable%> "
3817 "reference", field);
3818 else if (CP_TYPE_CONST_P (type))
3819 error ("member %q+D cannot be declared both %<const%> "
3820 "and %<mutable%>", field);
3823 if (! layout_pod_type_p (type))
3824 /* DR 148 now allows pointers to members (which are POD themselves),
3825 to be allowed in POD structs. */
3826 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3828 if (field_poverlapping_p (field))
3829 /* A potentially-overlapping non-static data member makes the class
3830 non-layout-POD. */
3831 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3833 if (!std_layout_type_p (type))
3834 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3836 if (! zero_init_p (type))
3837 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3839 /* We set DECL_C_BIT_FIELD in grokbitfield.
3840 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3841 if (DECL_C_BIT_FIELD (field))
3842 check_bitfield_decl (field);
3844 if (check_field_decl (field, t,
3845 cant_have_const_ctor_p, no_const_asn_ref_p))
3847 if (default_init_member
3848 && TREE_CODE (t) == UNION_TYPE)
3850 error ("multiple fields in union %qT initialized", t);
3851 inform (DECL_SOURCE_LOCATION (default_init_member),
3852 "initialized member %q+D declared here",
3853 default_init_member);
3855 default_init_member = field;
3858 /* Now that we've removed bit-field widths from DECL_INITIAL,
3859 anything left in DECL_INITIAL is an NSDMI that makes the class
3860 non-aggregate in C++11. */
3861 if (DECL_INITIAL (field) && cxx_dialect < cxx14)
3862 CLASSTYPE_NON_AGGREGATE (t) = true;
3864 if (CP_TYPE_CONST_P (type))
3866 /* If any field is const, the structure type is pseudo-const. */
3867 C_TYPE_FIELDS_READONLY (t) = 1;
3868 if (DECL_INITIAL (field) == NULL_TREE)
3869 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3870 if (cxx_dialect < cxx11)
3872 /* ARM $12.6.2: [A member initializer list] (or, for an
3873 aggregate, initialization by a brace-enclosed list) is the
3874 only way to initialize non-static const and reference
3875 members. */
3876 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3877 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3880 /* A field that is pseudo-const makes the structure likewise. */
3881 else if (CLASS_TYPE_P (type))
3883 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3884 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3885 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3886 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3889 /* Core issue 80: A non-static data member is required to have a
3890 different name from the class iff the class has a
3891 user-declared constructor. */
3892 if (constructor_name_p (DECL_NAME (field), t)
3893 && TYPE_HAS_USER_CONSTRUCTOR (t))
3894 permerror (DECL_SOURCE_LOCATION (field),
3895 "field %q#D with same name as class", field);
3898 /* Per CWG 2096, a type is a literal type if it is a union, and at least
3899 one of its non-static data members is of non-volatile literal type. */
3900 if (TREE_CODE (t) == UNION_TYPE && found_nv_literal_p)
3901 CLASSTYPE_LITERAL_P (t) = true;
3903 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3904 it should also define a copy constructor and an assignment operator to
3905 implement the correct copy semantic (deep vs shallow, etc.). As it is
3906 not feasible to check whether the constructors do allocate dynamic memory
3907 and store it within members, we approximate the warning like this:
3909 -- Warn only if there are members which are pointers
3910 -- Warn only if there is a non-trivial constructor (otherwise,
3911 there cannot be memory allocated).
3912 -- Warn only if there is a non-trivial destructor. We assume that the
3913 user at least implemented the cleanup correctly, and a destructor
3914 is needed to free dynamic memory.
3916 This seems enough for practical purposes. */
3917 if (warn_ecpp
3918 && pointer_member
3919 && TYPE_HAS_USER_CONSTRUCTOR (t)
3920 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3921 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3923 if (warning (OPT_Weffc__, "%q#T has pointer data members", t))
3925 if (! TYPE_HAS_COPY_CTOR (t))
3927 warning (OPT_Weffc__,
3928 " but does not declare %<%T(const %T&)%>", t, t);
3929 if (!TYPE_HAS_COPY_ASSIGN (t))
3930 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3932 else if (! TYPE_HAS_COPY_ASSIGN (t))
3933 warning (OPT_Weffc__,
3934 " but does not declare %<operator=(const %T&)%>", t);
3935 inform (DECL_SOURCE_LOCATION (pointer_member),
3936 "pointer member %q+D declared here", pointer_member);
3940 /* Non-static data member initializers make the default constructor
3941 non-trivial. */
3942 if (default_init_member)
3944 TYPE_NEEDS_CONSTRUCTING (t) = true;
3945 TYPE_HAS_COMPLEX_DFLT (t) = true;
3948 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3949 if (cant_pack)
3950 TYPE_PACKED (t) = 0;
3952 /* Check anonymous struct/anonymous union fields. */
3953 finish_struct_anon (t);
3955 /* We've built up the list of access declarations in reverse order.
3956 Fix that now. */
3957 *access_decls = nreverse (*access_decls);
3960 /* If TYPE is an empty class type, records its OFFSET in the table of
3961 OFFSETS. */
3963 static int
3964 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3966 splay_tree_node n;
3968 if (!is_empty_class (type))
3969 return 0;
3971 /* Record the location of this empty object in OFFSETS. */
3972 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3973 if (!n)
3974 n = splay_tree_insert (offsets,
3975 (splay_tree_key) offset,
3976 (splay_tree_value) NULL_TREE);
3977 n->value = ((splay_tree_value)
3978 tree_cons (NULL_TREE,
3979 type,
3980 (tree) n->value));
3982 return 0;
3985 /* Returns nonzero if TYPE is an empty class type and there is
3986 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3988 static int
3989 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3991 splay_tree_node n;
3992 tree t;
3994 if (!is_empty_class (type))
3995 return 0;
3997 /* Record the location of this empty object in OFFSETS. */
3998 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3999 if (!n)
4000 return 0;
4002 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4003 if (same_type_p (TREE_VALUE (t), type))
4004 return 1;
4006 return 0;
4009 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4010 F for every subobject, passing it the type, offset, and table of
4011 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4012 be traversed.
4014 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4015 than MAX_OFFSET will not be walked.
4017 If F returns a nonzero value, the traversal ceases, and that value
4018 is returned. Otherwise, returns zero. */
4020 static int
4021 walk_subobject_offsets (tree type,
4022 subobject_offset_fn f,
4023 tree offset,
4024 splay_tree offsets,
4025 tree max_offset,
4026 int vbases_p)
4028 int r = 0;
4029 tree type_binfo = NULL_TREE;
4031 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4032 stop. */
4033 if (max_offset && tree_int_cst_lt (max_offset, offset))
4034 return 0;
4036 if (type == error_mark_node)
4037 return 0;
4039 if (!TYPE_P (type))
4041 type_binfo = type;
4042 type = BINFO_TYPE (type);
4045 if (CLASS_TYPE_P (type))
4047 tree field;
4048 tree binfo;
4049 int i;
4051 /* Avoid recursing into objects that are not interesting. */
4052 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4053 return 0;
4055 /* Record the location of TYPE. */
4056 r = (*f) (type, offset, offsets);
4057 if (r)
4058 return r;
4060 /* Iterate through the direct base classes of TYPE. */
4061 if (!type_binfo)
4062 type_binfo = TYPE_BINFO (type);
4063 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4065 tree binfo_offset;
4067 if (BINFO_VIRTUAL_P (binfo))
4068 continue;
4070 tree orig_binfo;
4071 /* We cannot rely on BINFO_OFFSET being set for the base
4072 class yet, but the offsets for direct non-virtual
4073 bases can be calculated by going back to the TYPE. */
4074 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4075 binfo_offset = size_binop (PLUS_EXPR,
4076 offset,
4077 BINFO_OFFSET (orig_binfo));
4079 r = walk_subobject_offsets (binfo,
4081 binfo_offset,
4082 offsets,
4083 max_offset,
4084 /*vbases_p=*/0);
4085 if (r)
4086 return r;
4089 if (CLASSTYPE_VBASECLASSES (type))
4091 unsigned ix;
4092 vec<tree, va_gc> *vbases;
4094 /* Iterate through the virtual base classes of TYPE. In G++
4095 3.2, we included virtual bases in the direct base class
4096 loop above, which results in incorrect results; the
4097 correct offsets for virtual bases are only known when
4098 working with the most derived type. */
4099 if (vbases_p)
4100 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4101 vec_safe_iterate (vbases, ix, &binfo); ix++)
4103 r = walk_subobject_offsets (binfo,
4105 size_binop (PLUS_EXPR,
4106 offset,
4107 BINFO_OFFSET (binfo)),
4108 offsets,
4109 max_offset,
4110 /*vbases_p=*/0);
4111 if (r)
4112 return r;
4114 else
4116 /* We still have to walk the primary base, if it is
4117 virtual. (If it is non-virtual, then it was walked
4118 above.) */
4119 tree vbase = get_primary_binfo (type_binfo);
4121 if (vbase && BINFO_VIRTUAL_P (vbase)
4122 && BINFO_PRIMARY_P (vbase)
4123 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4125 r = (walk_subobject_offsets
4126 (vbase, f, offset,
4127 offsets, max_offset, /*vbases_p=*/0));
4128 if (r)
4129 return r;
4134 /* Iterate through the fields of TYPE. */
4135 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4136 if (TREE_CODE (field) == FIELD_DECL
4137 && TREE_TYPE (field) != error_mark_node
4138 && !DECL_ARTIFICIAL (field))
4140 tree field_offset;
4142 field_offset = byte_position (field);
4144 r = walk_subobject_offsets (TREE_TYPE (field),
4146 size_binop (PLUS_EXPR,
4147 offset,
4148 field_offset),
4149 offsets,
4150 max_offset,
4151 /*vbases_p=*/1);
4152 if (r)
4153 return r;
4156 else if (TREE_CODE (type) == ARRAY_TYPE)
4158 tree element_type = strip_array_types (type);
4159 tree domain = TYPE_DOMAIN (type);
4160 tree index;
4162 /* Avoid recursing into objects that are not interesting. */
4163 if (!CLASS_TYPE_P (element_type)
4164 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4165 || !domain
4166 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4167 return 0;
4169 /* Step through each of the elements in the array. */
4170 for (index = size_zero_node;
4171 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4172 index = size_binop (PLUS_EXPR, index, size_one_node))
4174 r = walk_subobject_offsets (TREE_TYPE (type),
4176 offset,
4177 offsets,
4178 max_offset,
4179 /*vbases_p=*/1);
4180 if (r)
4181 return r;
4182 offset = size_binop (PLUS_EXPR, offset,
4183 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4184 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4185 there's no point in iterating through the remaining
4186 elements of the array. */
4187 if (max_offset && tree_int_cst_lt (max_offset, offset))
4188 break;
4192 return 0;
4195 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4197 static bool
4198 field_poverlapping_p (tree decl)
4200 /* Base fields are actually potentially overlapping, but C++ bases go through
4201 a different code path based on binfos, and ObjC++ base fields are laid out
4202 in objc-act, so we don't want layout_class_type to mess with them. */
4203 if (DECL_FIELD_IS_BASE (decl))
4205 gcc_checking_assert (c_dialect_objc ());
4206 return false;
4209 return lookup_attribute ("no_unique_address",
4210 DECL_ATTRIBUTES (decl));
4213 /* Return true iff DECL is an empty field, either for an empty base or a
4214 [[no_unique_address]] data member. */
4216 bool
4217 is_empty_field (tree decl)
4219 if (!decl || TREE_CODE (decl) != FIELD_DECL)
4220 return false;
4222 bool r = (is_empty_class (TREE_TYPE (decl))
4223 && (DECL_FIELD_IS_BASE (decl)
4224 || field_poverlapping_p (decl)));
4226 /* Empty fields should have size zero. */
4227 gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl)));
4229 return r;
4232 /* Record all of the empty subobjects of DECL_OR_BINFO. */
4234 static void
4235 record_subobject_offsets (tree decl_or_binfo,
4236 splay_tree offsets)
4238 tree type, offset;
4239 bool overlapping, vbases_p;
4241 if (DECL_P (decl_or_binfo))
4243 tree decl = decl_or_binfo;
4244 type = TREE_TYPE (decl);
4245 offset = byte_position (decl);
4246 overlapping = field_poverlapping_p (decl);
4247 vbases_p = true;
4249 else
4251 type = BINFO_TYPE (decl_or_binfo);
4252 offset = BINFO_OFFSET (decl_or_binfo);
4253 overlapping = true;
4254 vbases_p = false;
4257 tree max_offset;
4258 /* If recording subobjects for a non-static data member or a
4259 non-empty base class, we do not need to record offsets beyond
4260 the size of the biggest empty class. Additional data members
4261 will go at the end of the class. Additional base classes will go
4262 either at offset zero (if empty, in which case they cannot
4263 overlap with offsets past the size of the biggest empty class) or
4264 at the end of the class.
4266 However, if we are placing an empty base class, then we must record
4267 all offsets, as either the empty class is at offset zero (where
4268 other empty classes might later be placed) or at the end of the
4269 class (where other objects might then be placed, so other empty
4270 subobjects might later overlap). */
4271 if (!overlapping
4272 || !is_empty_class (type))
4273 max_offset = sizeof_biggest_empty_class;
4274 else
4275 max_offset = NULL_TREE;
4276 walk_subobject_offsets (type, record_subobject_offset, offset,
4277 offsets, max_offset, vbases_p);
4280 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4281 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4282 virtual bases of TYPE are examined. */
4284 static int
4285 layout_conflict_p (tree type,
4286 tree offset,
4287 splay_tree offsets,
4288 int vbases_p)
4290 splay_tree_node max_node;
4292 /* Get the node in OFFSETS that indicates the maximum offset where
4293 an empty subobject is located. */
4294 max_node = splay_tree_max (offsets);
4295 /* If there aren't any empty subobjects, then there's no point in
4296 performing this check. */
4297 if (!max_node)
4298 return 0;
4300 return walk_subobject_offsets (type, check_subobject_offset, offset,
4301 offsets, (tree) (max_node->key),
4302 vbases_p);
4305 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4306 non-static data member of the type indicated by RLI. BINFO is the
4307 binfo corresponding to the base subobject, OFFSETS maps offsets to
4308 types already located at those offsets. This function determines
4309 the position of the DECL. */
4311 static void
4312 layout_nonempty_base_or_field (record_layout_info rli,
4313 tree decl,
4314 tree binfo,
4315 splay_tree offsets)
4317 tree offset = NULL_TREE;
4318 bool field_p;
4319 tree type;
4321 if (binfo)
4323 /* For the purposes of determining layout conflicts, we want to
4324 use the class type of BINFO; TREE_TYPE (DECL) will be the
4325 CLASSTYPE_AS_BASE version, which does not contain entries for
4326 zero-sized bases. */
4327 type = TREE_TYPE (binfo);
4328 field_p = false;
4330 else
4332 type = TREE_TYPE (decl);
4333 field_p = true;
4336 /* Try to place the field. It may take more than one try if we have
4337 a hard time placing the field without putting two objects of the
4338 same type at the same address. */
4339 while (1)
4341 struct record_layout_info_s old_rli = *rli;
4343 /* Place this field. */
4344 place_field (rli, decl);
4345 offset = byte_position (decl);
4347 /* We have to check to see whether or not there is already
4348 something of the same type at the offset we're about to use.
4349 For example, consider:
4351 struct S {};
4352 struct T : public S { int i; };
4353 struct U : public S, public T {};
4355 Here, we put S at offset zero in U. Then, we can't put T at
4356 offset zero -- its S component would be at the same address
4357 as the S we already allocated. So, we have to skip ahead.
4358 Since all data members, including those whose type is an
4359 empty class, have nonzero size, any overlap can happen only
4360 with a direct or indirect base-class -- it can't happen with
4361 a data member. */
4362 /* In a union, overlap is permitted; all members are placed at
4363 offset zero. */
4364 if (TREE_CODE (rli->t) == UNION_TYPE)
4365 break;
4366 if (layout_conflict_p (field_p ? type : binfo, offset,
4367 offsets, field_p))
4369 /* Strip off the size allocated to this field. That puts us
4370 at the first place we could have put the field with
4371 proper alignment. */
4372 *rli = old_rli;
4374 /* Bump up by the alignment required for the type. */
4375 rli->bitpos
4376 = size_binop (PLUS_EXPR, rli->bitpos,
4377 bitsize_int (binfo
4378 ? CLASSTYPE_ALIGN (type)
4379 : TYPE_ALIGN (type)));
4380 normalize_rli (rli);
4382 else if (TREE_CODE (type) == NULLPTR_TYPE
4383 && warn_abi && abi_version_crosses (9))
4385 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4386 the offset wasn't aligned like a pointer when we started to
4387 layout this field, that affects its position. */
4388 tree pos = rli_size_unit_so_far (&old_rli);
4389 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4391 if (abi_version_at_least (9))
4392 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4393 "alignment of %qD increased in %<-fabi-version=9%> "
4394 "(GCC 5.2)", decl);
4395 else
4396 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4397 "of %qD will increase in %<-fabi-version=9%>",
4398 decl);
4400 break;
4402 else
4403 /* There was no conflict. We're done laying out this field. */
4404 break;
4407 /* Now that we know where it will be placed, update its
4408 BINFO_OFFSET. */
4409 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4410 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4411 this point because their BINFO_OFFSET is copied from another
4412 hierarchy. Therefore, we may not need to add the entire
4413 OFFSET. */
4414 propagate_binfo_offsets (binfo,
4415 size_diffop_loc (input_location,
4416 fold_convert (ssizetype, offset),
4417 fold_convert (ssizetype,
4418 BINFO_OFFSET (binfo))));
4421 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4423 static int
4424 empty_base_at_nonzero_offset_p (tree type,
4425 tree offset,
4426 splay_tree /*offsets*/)
4428 return is_empty_class (type) && !integer_zerop (offset);
4431 /* Layout the empty base BINFO. EOC indicates the byte currently just
4432 past the end of the class, and should be correctly aligned for a
4433 class of the type indicated by BINFO; OFFSETS gives the offsets of
4434 the empty bases allocated so far. T is the most derived
4435 type. Return nonzero iff we added it at the end. */
4437 static bool
4438 layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl,
4439 splay_tree offsets)
4441 tree alignment;
4442 bool atend = false;
4443 tree binfo = NULL_TREE;
4444 tree decl = NULL_TREE;
4445 tree type;
4446 if (TREE_CODE (binfo_or_decl) == TREE_BINFO)
4448 binfo = binfo_or_decl;
4449 type = BINFO_TYPE (binfo);
4451 else
4453 decl = binfo_or_decl;
4454 type = TREE_TYPE (decl);
4457 /* On some platforms (ARM), even empty classes will not be
4458 byte-aligned. */
4459 tree eoc = round_up_loc (input_location,
4460 rli_size_unit_so_far (rli),
4461 CLASSTYPE_ALIGN_UNIT (type));
4463 /* This routine should only be used for empty classes. */
4464 gcc_assert (is_empty_class (type));
4466 if (decl && DECL_USER_ALIGN (decl))
4467 alignment = size_int (DECL_ALIGN_UNIT (decl));
4468 else
4469 alignment = size_int (CLASSTYPE_ALIGN_UNIT (type));
4471 /* This is an empty base class. We first try to put it at offset
4472 zero. */
4473 tree offset = size_zero_node;
4474 if (TREE_CODE (rli->t) != UNION_TYPE
4475 && layout_conflict_p (type,
4476 offset,
4477 offsets,
4478 /*vbases_p=*/0))
4480 /* That didn't work. Now, we move forward from the next
4481 available spot in the class. */
4482 atend = true;
4483 offset = eoc;
4484 while (1)
4486 if (!layout_conflict_p (type,
4487 offset,
4488 offsets,
4489 /*vbases_p=*/0))
4490 /* We finally found a spot where there's no overlap. */
4491 break;
4493 /* There's overlap here, too. Bump along to the next spot. */
4494 offset = size_binop (PLUS_EXPR, offset, alignment);
4498 if (decl && DECL_USER_ALIGN (decl))
4500 rli->record_align = MAX (rli->record_align, DECL_ALIGN (decl));
4501 if (warn_packed)
4502 rli->unpacked_align = MAX (rli->unpacked_align, DECL_ALIGN (decl));
4503 TYPE_USER_ALIGN (rli->t) = 1;
4505 else if (CLASSTYPE_USER_ALIGN (type))
4507 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
4508 if (warn_packed)
4509 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
4510 TYPE_USER_ALIGN (rli->t) = 1;
4513 if (binfo)
4514 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4515 propagate_binfo_offsets (binfo,
4516 size_diffop (offset, BINFO_OFFSET (binfo)));
4517 else
4519 DECL_FIELD_OFFSET (decl) = offset;
4520 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4521 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4524 return atend;
4527 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4528 fields at NEXT_FIELD, and return it. */
4530 static tree
4531 build_base_field_1 (tree t, tree binfo, tree access, tree *&next_field)
4533 /* Create the FIELD_DECL. */
4534 tree basetype = BINFO_TYPE (binfo);
4535 tree as_base = CLASSTYPE_AS_BASE (basetype);
4536 gcc_assert (as_base);
4537 tree decl = build_decl (input_location, FIELD_DECL, NULL_TREE, as_base);
4539 DECL_ARTIFICIAL (decl) = 1;
4540 DECL_IGNORED_P (decl) = 1;
4541 DECL_FIELD_CONTEXT (decl) = t;
4542 if (is_empty_class (basetype))
4543 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4544 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4545 else
4547 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4548 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4550 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4551 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4552 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4553 DECL_FIELD_IS_BASE (decl) = 1;
4555 if (access == access_private_node)
4556 TREE_PRIVATE (decl) = true;
4557 else if (access == access_protected_node)
4558 TREE_PROTECTED (decl) = true;
4560 /* Add the new FIELD_DECL to the list of fields for T. */
4561 DECL_CHAIN (decl) = *next_field;
4562 *next_field = decl;
4563 next_field = &DECL_CHAIN (decl);
4565 return decl;
4568 /* Layout the base given by BINFO in the class indicated by RLI.
4569 *BASE_ALIGN is a running maximum of the alignments of
4570 any base class. OFFSETS gives the location of empty base
4571 subobjects. T is the most derived type. Return nonzero if the new
4572 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4573 *NEXT_FIELD, unless BINFO is for an empty base class.
4575 Returns the location at which the next field should be inserted. */
4577 static tree *
4578 build_base_field (record_layout_info rli, tree binfo, tree access,
4579 splay_tree offsets, tree *next_field)
4581 tree t = rli->t;
4582 tree basetype = BINFO_TYPE (binfo);
4584 if (!COMPLETE_TYPE_P (basetype))
4585 /* This error is now reported in xref_tag, thus giving better
4586 location information. */
4587 return next_field;
4589 /* Place the base class. */
4590 if (!is_empty_class (basetype))
4592 tree decl;
4594 /* The containing class is non-empty because it has a non-empty
4595 base class. */
4596 CLASSTYPE_EMPTY_P (t) = 0;
4598 /* Create the FIELD_DECL. */
4599 decl = build_base_field_1 (t, binfo, access, next_field);
4601 /* Try to place the field. It may take more than one try if we
4602 have a hard time placing the field without putting two
4603 objects of the same type at the same address. */
4604 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4606 else
4608 bool atend = layout_empty_base_or_field (rli, binfo, offsets);
4609 /* A nearly-empty class "has no proper base class that is empty,
4610 not morally virtual, and at an offset other than zero." */
4611 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4613 if (atend)
4614 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4615 /* The check above (used in G++ 3.2) is insufficient because
4616 an empty class placed at offset zero might itself have an
4617 empty base at a nonzero offset. */
4618 else if (walk_subobject_offsets (basetype,
4619 empty_base_at_nonzero_offset_p,
4620 size_zero_node,
4621 /*offsets=*/NULL,
4622 /*max_offset=*/NULL_TREE,
4623 /*vbases_p=*/true))
4624 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4627 /* We used to not create a FIELD_DECL for empty base classes because of
4628 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4629 be a problem anymore. We need them to handle initialization of C++17
4630 aggregate bases. */
4631 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4633 tree decl = build_base_field_1 (t, binfo, access, next_field);
4634 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4635 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4636 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4637 SET_DECL_FIELD_ABI_IGNORED (decl, 1);
4640 /* An empty virtual base causes a class to be non-empty
4641 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4642 here because that was already done when the virtual table
4643 pointer was created. */
4646 /* Record the offsets of BINFO and its base subobjects. */
4647 record_subobject_offsets (binfo, offsets);
4649 return next_field;
4652 /* Layout all of the non-virtual base classes. Record empty
4653 subobjects in OFFSETS. T is the most derived type. Return nonzero
4654 if the type cannot be nearly empty. The fields created
4655 corresponding to the base classes will be inserted at
4656 *NEXT_FIELD. */
4658 static void
4659 build_base_fields (record_layout_info rli,
4660 splay_tree offsets, tree *next_field)
4662 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4663 subobjects. */
4664 tree t = rli->t;
4665 tree binfo = TYPE_BINFO (t);
4666 int n_baseclasses = BINFO_N_BASE_BINFOS (binfo);
4668 /* The primary base class is always allocated first. */
4669 const tree primary_binfo = CLASSTYPE_PRIMARY_BINFO (t);
4670 if (primary_binfo)
4672 /* We need to walk BINFO_BASE_BINFO to find the access of the primary
4673 base, if it is direct. Indirect base fields are private. */
4674 tree primary_access = access_private_node;
4675 for (int i = 0; i < n_baseclasses; ++i)
4677 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
4678 if (base_binfo == primary_binfo)
4680 primary_access = BINFO_BASE_ACCESS (binfo, i);
4681 break;
4684 next_field = build_base_field (rli, primary_binfo,
4685 primary_access,
4686 offsets, next_field);
4689 /* Now allocate the rest of the bases. */
4690 for (int i = 0; i < n_baseclasses; ++i)
4692 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
4694 /* The primary base was already allocated above, so we don't
4695 need to allocate it again here. */
4696 if (base_binfo == primary_binfo)
4697 continue;
4699 /* Virtual bases are added at the end (a primary virtual base
4700 will have already been added). */
4701 if (BINFO_VIRTUAL_P (base_binfo))
4702 continue;
4704 next_field = build_base_field (rli, base_binfo,
4705 BINFO_BASE_ACCESS (binfo, i),
4706 offsets, next_field);
4710 /* Go through the TYPE_FIELDS of T issuing any appropriate
4711 diagnostics, figuring out which methods override which other
4712 methods, and so forth. */
4714 static void
4715 check_methods (tree t)
4717 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4718 if (DECL_DECLARES_FUNCTION_P (x))
4720 check_for_override (x, t);
4722 if (DECL_PURE_VIRTUAL_P (x)
4723 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4724 error ("initializer specified for non-virtual method %q+D", x);
4725 /* The name of the field is the original field name
4726 Save this in auxiliary field for later overloading. */
4727 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4729 TYPE_POLYMORPHIC_P (t) = 1;
4730 if (DECL_PURE_VIRTUAL_P (x))
4731 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4734 if (!DECL_VIRTUAL_P (x)
4735 && lookup_attribute ("transaction_safe_dynamic",
4736 DECL_ATTRIBUTES (x)))
4737 error_at (DECL_SOURCE_LOCATION (x),
4738 "%<transaction_safe_dynamic%> may only be specified for "
4739 "a virtual function");
4742 /* Check whether the eligible special member functions (P0848) are
4743 user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
4744 has the eligible ones; TYPE_FIELDS also contains ineligible overloads,
4745 which is why this needs to be separate from the loop above. */
4747 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4749 if (TREE_CODE (dtor) == OVERLOAD)
4751 /* P0848: At the end of the definition of a class, overload
4752 resolution is performed among the prospective destructors declared
4753 in that class with an empty argument list to select the destructor
4754 for the class, also known as the selected destructor. The program
4755 is ill-formed if overload resolution fails. */
4756 auto_diagnostic_group d;
4757 error_at (location_of (t), "destructor for %qT is ambiguous", t);
4758 print_candidates (dtor);
4760 else if (user_provided_p (dtor))
4761 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
4764 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
4766 if (!user_provided_p (fn))
4767 /* Might be trivial. */;
4768 else if (copy_fn_p (fn))
4769 TYPE_HAS_COMPLEX_COPY_CTOR (t) = true;
4770 else if (move_fn_p (fn))
4771 TYPE_HAS_COMPLEX_MOVE_CTOR (t) = true;
4774 for (tree fn : ovl_range (get_class_binding_direct (t, assign_op_identifier)))
4776 if (!user_provided_p (fn))
4777 /* Might be trivial. */;
4778 else if (copy_fn_p (fn))
4779 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true;
4780 else if (move_fn_p (fn))
4781 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = true;
4785 /* FN is constructor, destructor or operator function. Clone the
4786 declaration to create a NAME'd variant. NEED_VTT_PARM_P and
4787 OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
4789 static tree
4790 copy_fndecl_with_name (tree fn, tree name, tree_code code,
4791 bool need_vtt_parm_p, bool omit_inherited_parms_p)
4793 /* Copy the function. */
4794 tree clone = copy_decl (fn);
4795 /* Reset the function name. */
4796 DECL_NAME (clone) = name;
4798 if (flag_concepts)
4799 /* Clone constraints. */
4800 if (tree ci = get_constraints (fn))
4801 set_constraints (clone, copy_node (ci));
4803 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4804 /* There's no pending inline data for this function. */
4805 DECL_PENDING_INLINE_INFO (clone) = NULL;
4806 DECL_PENDING_INLINE_P (clone) = 0;
4808 if (name == base_dtor_identifier)
4810 /* The base-class destructor is not virtual. */
4811 DECL_VIRTUAL_P (clone) = 0;
4812 DECL_VINDEX (clone) = NULL_TREE;
4814 else if (code != ERROR_MARK)
4816 /* Set the operator code. */
4817 const ovl_op_info_t *ovl_op = OVL_OP_INFO (false, code);
4818 DECL_OVERLOADED_OPERATOR_CODE_RAW (clone) = ovl_op->ovl_op_code;
4820 /* The operator could be virtual. */
4821 if (DECL_VIRTUAL_P (clone))
4822 IDENTIFIER_VIRTUAL_P (name) = true;
4825 if (omit_inherited_parms_p)
4826 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4828 /* If there was an in-charge parameter, drop it from the function
4829 type. */
4830 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4832 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4833 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4834 /* Skip the `this' parameter. */
4835 parmtypes = TREE_CHAIN (parmtypes);
4836 /* Skip the in-charge parameter. */
4837 parmtypes = TREE_CHAIN (parmtypes);
4838 /* And the VTT parm, in a complete [cd]tor. */
4839 if (DECL_HAS_VTT_PARM_P (fn) && !need_vtt_parm_p)
4840 parmtypes = TREE_CHAIN (parmtypes);
4841 if (omit_inherited_parms_p)
4843 /* If we're omitting inherited parms, that just leaves the VTT. */
4844 gcc_assert (need_vtt_parm_p);
4845 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4847 TREE_TYPE (clone)
4848 = build_method_type_directly (basetype,
4849 TREE_TYPE (TREE_TYPE (clone)),
4850 parmtypes);
4851 TREE_TYPE (clone)
4852 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4853 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4854 TREE_TYPE (clone)
4855 = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
4858 /* Copy the function parameters. */
4859 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4861 /* Remove the in-charge parameter. */
4862 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4864 DECL_CHAIN (DECL_ARGUMENTS (clone))
4865 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4866 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4869 /* And the VTT parm, in a complete [cd]tor. */
4870 if (DECL_HAS_VTT_PARM_P (fn))
4872 if (need_vtt_parm_p)
4873 DECL_HAS_VTT_PARM_P (clone) = 1;
4874 else
4876 DECL_CHAIN (DECL_ARGUMENTS (clone))
4877 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4878 DECL_HAS_VTT_PARM_P (clone) = 0;
4882 /* A base constructor inheriting from a virtual base doesn't get the
4883 arguments. */
4884 if (omit_inherited_parms_p)
4885 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4887 for (tree parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4889 DECL_CONTEXT (parms) = clone;
4890 cxx_dup_lang_specific_decl (parms);
4893 /* Create the RTL for this function. */
4894 SET_DECL_RTL (clone, NULL);
4896 /* Regardless of the current scope, this is a member function, so
4897 not at namespace scope. */
4898 rest_of_decl_compilation (clone, /*top_level=*/0, at_eof);
4900 return clone;
4903 /* FN is an operator function, create a variant for CODE. */
4905 tree
4906 copy_operator_fn (tree fn, tree_code code)
4908 return copy_fndecl_with_name (fn, ovl_op_identifier (code),
4909 code, false, false);
4912 /* FN is a constructor or destructor. Clone the declaration to create
4913 a specialized in-charge or not-in-charge version, as indicated by
4914 NAME. */
4916 static tree
4917 build_clone (tree fn, tree name, bool need_vtt_parm_p,
4918 bool omit_inherited_parms_p)
4920 tree clone;
4922 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4923 if (TREE_CODE (fn) == TEMPLATE_DECL)
4925 clone = copy_decl (fn);
4926 DECL_NAME (clone) = name;
4928 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name,
4929 need_vtt_parm_p, omit_inherited_parms_p);
4930 DECL_TEMPLATE_RESULT (clone) = result;
4932 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4933 DECL_TI_TEMPLATE (result) = clone;
4935 TREE_TYPE (clone) = TREE_TYPE (result);
4937 else
4939 clone = copy_fndecl_with_name (fn, name, ERROR_MARK,
4940 need_vtt_parm_p, omit_inherited_parms_p);
4941 DECL_CLONED_FUNCTION (clone) = fn;
4944 /* Remember where this function came from. */
4945 DECL_ABSTRACT_ORIGIN (clone) = fn;
4947 /* Make it easy to find the CLONE given the FN. Note the
4948 template_result of a template will be chained this way too. */
4949 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4950 DECL_CHAIN (fn) = clone;
4952 return clone;
4955 /* Build the clones of FN, return the number of clones built. These
4956 will be inserted onto DECL_CHAIN of FN. */
4958 void
4959 build_cdtor_clones (tree fn, bool needs_vtt_p, bool base_omits_inherited_p,
4960 bool update_methods)
4962 unsigned count = 0;
4964 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4966 /* For each constructor, we need two variants: an in-charge version
4967 and a not-in-charge version. */
4968 build_clone (fn, complete_ctor_identifier, false, false);
4969 build_clone (fn, base_ctor_identifier, needs_vtt_p,
4970 base_omits_inherited_p);
4971 count += 2;
4973 else
4975 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4977 /* For each destructor, we need three variants: an in-charge
4978 version, a not-in-charge version, and an in-charge deleting
4979 version. We clone the deleting version first because that
4980 means it will go second on the TYPE_FIELDS list -- and that
4981 corresponds to the correct layout order in the virtual
4982 function table.
4984 For a non-virtual destructor, we do not build a deleting
4985 destructor. */
4986 if (DECL_VIRTUAL_P (fn))
4988 build_clone (fn, deleting_dtor_identifier, false, false);
4989 count++;
4991 build_clone (fn, complete_dtor_identifier, false, false);
4992 build_clone (fn, base_dtor_identifier, needs_vtt_p, false);
4993 count += 2;
4996 /* The original is now an abstract function that is never
4997 emitted. */
4998 DECL_ABSTRACT_P (fn) = true;
5000 if (update_methods)
5001 for (tree clone = fn; count--;)
5003 clone = DECL_CHAIN (clone);
5004 add_method (DECL_CONTEXT (clone), clone, false);
5008 /* Produce declarations for all appropriate clones of FN. If
5009 UPDATE_METHODS is true, the clones are added to the
5010 CLASSTYPE_MEMBER_VEC. */
5012 void
5013 clone_cdtor (tree fn, bool update_methods)
5015 /* Avoid inappropriate cloning. */
5016 if (DECL_CHAIN (fn)
5017 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
5018 return;
5020 /* Base cdtors need a vtt parm if there are virtual bases. */
5021 bool vtt = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn));
5023 /* Base ctor omits inherited parms it needs a vttparm and inherited
5024 from a virtual nase ctor. */
5025 bool base_omits_inherited = (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
5026 && base_ctor_omit_inherited_parms (fn));
5028 build_cdtor_clones (fn, vtt, base_omits_inherited, update_methods);
5031 /* DECL is an in charge constructor, which is being defined. This will
5032 have had an in class declaration, from whence clones were
5033 declared. An out-of-class definition can specify additional default
5034 arguments. As it is the clones that are involved in overload
5035 resolution, we must propagate the information from the DECL to its
5036 clones. */
5038 void
5039 adjust_clone_args (tree decl)
5041 tree clone;
5043 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
5044 clone = DECL_CHAIN (clone))
5046 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
5047 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
5048 tree decl_parms, clone_parms;
5050 /* Skip the 'this' parameter. */
5051 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
5052 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5054 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
5055 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5056 if (DECL_HAS_VTT_PARM_P (decl))
5057 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5059 clone_parms = orig_clone_parms;
5060 if (DECL_HAS_VTT_PARM_P (clone))
5061 clone_parms = TREE_CHAIN (clone_parms);
5063 for (decl_parms = orig_decl_parms; decl_parms;
5064 decl_parms = TREE_CHAIN (decl_parms),
5065 clone_parms = TREE_CHAIN (clone_parms))
5067 if (clone_parms == void_list_node)
5069 gcc_assert (decl_parms == clone_parms
5070 || ctor_omit_inherited_parms (clone));
5071 break;
5074 gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms),
5075 TREE_VALUE (clone_parms)));
5077 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
5079 /* A default parameter has been added. Adjust the
5080 clone's parameters. */
5081 clone_parms = orig_decl_parms;
5083 if (DECL_HAS_VTT_PARM_P (clone))
5085 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
5086 TREE_VALUE (orig_clone_parms),
5087 clone_parms);
5088 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
5091 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5092 tree type
5093 = build_method_type_directly (basetype,
5094 TREE_TYPE (TREE_TYPE (clone)),
5095 clone_parms);
5096 if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
5097 type = cp_build_type_attribute_variant (type, attrs);
5098 type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
5099 TREE_TYPE (clone) = type;
5101 clone_parms = NULL_TREE;
5102 break;
5105 gcc_assert (!clone_parms || clone_parms == void_list_node);
5109 /* For each of the constructors and destructors in T, create an
5110 in-charge and not-in-charge variant. */
5112 static void
5113 clone_constructors_and_destructors (tree t)
5115 /* We do not need to propagate the usingness to the clone, at this
5116 point that is not needed. */
5117 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5118 clone_cdtor (fn, /*update_methods=*/true);
5120 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5121 clone_cdtor (dtor, /*update_methods=*/true);
5124 /* Deduce noexcept for a destructor DTOR. */
5126 void
5127 deduce_noexcept_on_destructor (tree dtor)
5129 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5130 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5131 noexcept_deferred_spec);
5134 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5135 of TYPE for virtual functions which FNDECL overrides. Return a
5136 mask of the tm attributes found therein. */
5138 static int
5139 look_for_tm_attr_overrides (tree type, tree fndecl)
5141 tree binfo = TYPE_BINFO (type);
5142 tree base_binfo;
5143 int ix, found = 0;
5145 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5147 tree o, basetype = BINFO_TYPE (base_binfo);
5149 if (!TYPE_POLYMORPHIC_P (basetype))
5150 continue;
5152 o = look_for_overrides_here (basetype, fndecl);
5153 if (o)
5155 if (lookup_attribute ("transaction_safe_dynamic",
5156 DECL_ATTRIBUTES (o)))
5157 /* transaction_safe_dynamic is not inherited. */;
5158 else
5159 found |= tm_attr_to_mask (find_tm_attribute
5160 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5162 else
5163 found |= look_for_tm_attr_overrides (basetype, fndecl);
5166 return found;
5169 /* Subroutine of set_method_tm_attributes. Handle the checks and
5170 inheritance for one virtual method FNDECL. */
5172 static void
5173 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5175 tree tm_attr;
5176 int found, have;
5178 found = look_for_tm_attr_overrides (type, fndecl);
5180 /* If FNDECL doesn't actually override anything (i.e. T is the
5181 class that first declares FNDECL virtual), then we're done. */
5182 if (found == 0)
5183 return;
5185 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5186 have = tm_attr_to_mask (tm_attr);
5188 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5189 tm_pure must match exactly, otherwise no weakening of
5190 tm_safe > tm_callable > nothing. */
5191 /* ??? The tm_pure attribute didn't make the transition to the
5192 multivendor language spec. */
5193 if (have == TM_ATTR_PURE)
5195 if (found != TM_ATTR_PURE)
5197 found &= -found;
5198 goto err_override;
5201 /* If the overridden function is tm_pure, then FNDECL must be. */
5202 else if (found == TM_ATTR_PURE && tm_attr)
5203 goto err_override;
5204 /* Look for base class combinations that cannot be satisfied. */
5205 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5207 found &= ~TM_ATTR_PURE;
5208 found &= -found;
5209 error_at (DECL_SOURCE_LOCATION (fndecl),
5210 "method overrides both %<transaction_pure%> and %qE methods",
5211 tm_mask_to_attr (found));
5213 /* If FNDECL did not declare an attribute, then inherit the most
5214 restrictive one. */
5215 else if (tm_attr == NULL)
5217 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5219 /* Otherwise validate that we're not weaker than a function
5220 that is being overridden. */
5221 else
5223 found &= -found;
5224 if (found <= TM_ATTR_CALLABLE && have > found)
5225 goto err_override;
5227 return;
5229 err_override:
5230 error_at (DECL_SOURCE_LOCATION (fndecl),
5231 "method declared %qE overriding %qE method",
5232 tm_attr, tm_mask_to_attr (found));
5235 /* For each of the methods in T, propagate a class-level tm attribute. */
5237 static void
5238 set_method_tm_attributes (tree t)
5240 tree class_tm_attr, fndecl;
5242 /* Don't bother collecting tm attributes if transactional memory
5243 support is not enabled. */
5244 if (!flag_tm)
5245 return;
5247 /* Process virtual methods first, as they inherit directly from the
5248 base virtual function and also require validation of new attributes. */
5249 if (TYPE_CONTAINS_VPTR_P (t))
5251 tree vchain;
5252 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5253 vchain = TREE_CHAIN (vchain))
5255 fndecl = BV_FN (vchain);
5256 if (DECL_THUNK_P (fndecl))
5257 fndecl = THUNK_TARGET (fndecl);
5258 set_one_vmethod_tm_attributes (t, fndecl);
5262 /* If the class doesn't have an attribute, nothing more to do. */
5263 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5264 if (class_tm_attr == NULL)
5265 return;
5267 /* Any method that does not yet have a tm attribute inherits
5268 the one from the class. */
5269 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
5270 if (DECL_DECLARES_FUNCTION_P (fndecl)
5271 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5272 apply_tm_attr (fndecl, class_tm_attr);
5275 /* Returns true if FN is a default constructor. */
5277 bool
5278 default_ctor_p (const_tree fn)
5280 return (DECL_CONSTRUCTOR_P (fn)
5281 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5284 /* Returns true iff class T has a user-provided constructor that can be called
5285 with more than zero arguments. */
5287 bool
5288 type_has_user_nondefault_constructor (tree t)
5290 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5291 return false;
5293 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5295 if (user_provided_p (fn)
5296 && (TREE_CODE (fn) == TEMPLATE_DECL
5297 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5298 != NULL_TREE)))
5299 return true;
5302 return false;
5305 /* Returns the defaulted constructor if T has one. Otherwise, returns
5306 NULL_TREE. */
5308 tree
5309 in_class_defaulted_default_constructor (tree t)
5311 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5312 return NULL_TREE;
5314 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5316 tree fn = *iter;
5318 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5319 && default_ctor_p (fn))
5320 return fn;
5323 return NULL_TREE;
5326 /* Returns true iff FN is a user-provided function, i.e. user-declared
5327 and not defaulted at its first declaration. */
5329 bool
5330 user_provided_p (tree fn)
5332 fn = STRIP_TEMPLATE (fn);
5333 return (!DECL_ARTIFICIAL (fn)
5334 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5335 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5338 /* Returns true iff class T has a user-provided constructor. */
5340 bool
5341 type_has_user_provided_constructor (tree t)
5343 if (!CLASS_TYPE_P (t))
5344 return false;
5346 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5347 return false;
5349 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5350 if (user_provided_p (*iter))
5351 return true;
5353 return false;
5356 /* Returns true iff class T has a user-provided or explicit constructor. */
5358 bool
5359 type_has_user_provided_or_explicit_constructor (tree t)
5361 if (!CLASS_TYPE_P (t))
5362 return false;
5364 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5365 return false;
5367 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5369 tree fn = *iter;
5370 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5371 return true;
5374 return false;
5377 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5378 declared or explicitly defaulted in the class body) default
5379 constructor. */
5381 bool
5382 type_has_non_user_provided_default_constructor (tree t)
5384 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5385 return false;
5386 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5387 return true;
5389 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5391 tree fn = *iter;
5392 if (TREE_CODE (fn) == FUNCTION_DECL
5393 && default_ctor_p (fn)
5394 && !user_provided_p (fn))
5395 return true;
5398 return false;
5401 /* TYPE is being used as a virtual base, and has a non-trivial move
5402 assignment. Return true if this is due to there being a user-provided
5403 move assignment in TYPE or one of its subobjects; if there isn't, then
5404 multiple move assignment can't cause any harm. */
5406 bool
5407 vbase_has_user_provided_move_assign (tree type)
5409 /* Does the type itself have a user-provided move assignment operator? */
5410 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5411 for (ovl_iterator iter (get_class_binding_direct
5412 (type, assign_op_identifier));
5413 iter; ++iter)
5414 if (user_provided_p (*iter) && move_fn_p (*iter))
5415 return true;
5417 /* Do any of its bases? */
5418 tree binfo = TYPE_BINFO (type);
5419 tree base_binfo;
5420 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5421 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5422 return true;
5424 /* Or non-static data members? */
5425 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5427 if (TREE_CODE (field) == FIELD_DECL
5428 && CLASS_TYPE_P (TREE_TYPE (field))
5429 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5430 return true;
5433 /* Seems not. */
5434 return false;
5437 /* If default-initialization leaves part of TYPE uninitialized, returns
5438 a DECL for the field or TYPE itself (DR 253). */
5440 tree
5441 default_init_uninitialized_part (tree type)
5443 tree t, r, binfo;
5444 int i;
5446 type = strip_array_types (type);
5447 if (!CLASS_TYPE_P (type))
5448 return type;
5449 if (!type_has_non_user_provided_default_constructor (type))
5450 return NULL_TREE;
5451 for (binfo = TYPE_BINFO (type), i = 0;
5452 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5454 r = default_init_uninitialized_part (BINFO_TYPE (t));
5455 if (r)
5456 return r;
5458 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5459 if (TREE_CODE (t) == FIELD_DECL
5460 && !DECL_ARTIFICIAL (t)
5461 && !DECL_INITIAL (t))
5463 r = default_init_uninitialized_part (TREE_TYPE (t));
5464 if (r)
5465 return DECL_P (r) ? r : t;
5468 return NULL_TREE;
5471 /* Returns true iff for class T, a trivial synthesized default constructor
5472 would be constexpr. */
5474 bool
5475 trivial_default_constructor_is_constexpr (tree t)
5477 /* A defaulted trivial default constructor is constexpr
5478 if there is nothing to initialize. */
5479 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5480 /* A class with a vptr doesn't have a trivial default ctor.
5481 In C++20, a class can have transient uninitialized members, e.g.:
5483 struct S { int i; constexpr S() = default; };
5485 should work. */
5486 return (cxx_dialect >= cxx20
5487 || is_really_empty_class (t, /*ignore_vptr*/true));
5490 /* Returns true iff class T has a constexpr default constructor. */
5492 bool
5493 type_has_constexpr_default_constructor (tree t)
5495 tree fns;
5497 if (!CLASS_TYPE_P (t))
5499 /* The caller should have stripped an enclosing array. */
5500 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5501 return false;
5503 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5505 if (!TYPE_HAS_COMPLEX_DFLT (t))
5506 return trivial_default_constructor_is_constexpr (t);
5507 /* Non-trivial, we need to check subobject constructors. */
5508 lazily_declare_fn (sfk_constructor, t);
5510 fns = locate_ctor (t);
5511 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5514 /* Returns true iff class T has a constexpr default constructor or has an
5515 implicitly declared default constructor that we can't tell if it's constexpr
5516 without forcing a lazy declaration (which might cause undesired
5517 instantiations). */
5519 static bool
5520 type_maybe_constexpr_default_constructor (tree t)
5522 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5523 && TYPE_HAS_COMPLEX_DFLT (t))
5524 /* Assume it's constexpr. */
5525 return true;
5526 return type_has_constexpr_default_constructor (t);
5529 /* Returns true iff class T has a constexpr destructor. */
5531 bool
5532 type_has_constexpr_destructor (tree t)
5534 tree fns;
5536 if (CLASSTYPE_LAZY_DESTRUCTOR (t))
5537 /* Non-trivial, we need to check subobject destructors. */
5538 lazily_declare_fn (sfk_destructor, t);
5539 fns = CLASSTYPE_DESTRUCTOR (t);
5540 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5543 /* Returns true iff class T has a constexpr destructor or has an
5544 implicitly declared destructor that we can't tell if it's constexpr
5545 without forcing a lazy declaration (which might cause undesired
5546 instantiations). */
5548 static bool
5549 type_maybe_constexpr_destructor (tree t)
5551 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t))
5552 /* Assume it's constexpr. */
5553 return true;
5554 return type_has_constexpr_destructor (t);
5557 /* Returns true iff class TYPE has a virtual destructor. */
5559 bool
5560 type_has_virtual_destructor (tree type)
5562 tree dtor;
5564 if (!CLASS_TYPE_P (type))
5565 return false;
5567 gcc_assert (COMPLETE_TYPE_P (type));
5568 dtor = CLASSTYPE_DESTRUCTOR (type);
5569 return (dtor && DECL_VIRTUAL_P (dtor));
5572 /* Returns true iff T, a class, has a move-assignment or
5573 move-constructor. Does not lazily declare either.
5574 If USER_P is false, any move function will do. If it is true, the
5575 move function must be user-declared.
5577 Note that user-declared here is different from "user-provided",
5578 which doesn't include functions that are defaulted in the
5579 class. */
5581 bool
5582 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5584 gcc_assert (user_p
5585 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5586 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5588 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5589 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5590 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5591 return true;
5593 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5594 for (ovl_iterator iter (get_class_binding_direct
5595 (t, assign_op_identifier));
5596 iter; ++iter)
5597 if ((!user_p || !DECL_ARTIFICIAL (*iter))
5598 && DECL_CONTEXT (*iter) == t
5599 && move_fn_p (*iter))
5600 return true;
5602 return false;
5605 /* True iff T has a move constructor that is not deleted. */
5607 bool
5608 classtype_has_non_deleted_move_ctor (tree t)
5610 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5611 lazily_declare_fn (sfk_move_constructor, t);
5612 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5613 if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5614 return true;
5615 return false;
5618 /* If T, a class, has a user-provided copy constructor, copy assignment
5619 operator, or destructor, returns that function. Otherwise, null. */
5621 tree
5622 classtype_has_depr_implicit_copy (tree t)
5624 if (!CLASSTYPE_LAZY_COPY_CTOR (t))
5625 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5627 tree fn = *iter;
5628 if (user_provided_p (fn) && copy_fn_p (fn))
5629 return fn;
5632 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
5633 for (ovl_iterator iter (get_class_binding_direct
5634 (t, assign_op_identifier));
5635 iter; ++iter)
5637 tree fn = *iter;
5638 if (DECL_CONTEXT (fn) == t
5639 && user_provided_p (fn) && copy_fn_p (fn))
5640 return fn;
5643 if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
5645 tree fn = CLASSTYPE_DESTRUCTOR (t);
5646 if (user_provided_p (fn))
5647 return fn;
5650 return NULL_TREE;
5653 /* True iff T has a member or friend declaration of operator OP. */
5655 bool
5656 classtype_has_op (tree t, tree_code op)
5658 tree name = ovl_op_identifier (op);
5659 if (get_class_binding (t, name))
5660 return true;
5661 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
5662 if (FRIEND_NAME (f) == name)
5663 return true;
5664 return false;
5668 /* If T has a defaulted member or friend declaration of OP, return it. */
5670 tree
5671 classtype_has_defaulted_op (tree t, tree_code op)
5673 tree name = ovl_op_identifier (op);
5674 for (ovl_iterator oi (get_class_binding (t, name)); oi; ++oi)
5676 tree fn = *oi;
5677 if (DECL_DEFAULTED_FN (fn))
5678 return fn;
5680 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
5681 if (FRIEND_NAME (f) == name)
5682 for (tree l = FRIEND_DECLS (f); l; l = TREE_CHAIN (l))
5684 tree fn = TREE_VALUE (l);
5685 if (DECL_DEFAULTED_FN (fn))
5686 return fn;
5688 return NULL_TREE;
5691 /* Nonzero if we need to build up a constructor call when initializing an
5692 object of this class, either because it has a user-declared constructor
5693 or because it doesn't have a default constructor (so we need to give an
5694 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5695 what you care about is whether or not an object can be produced by a
5696 constructor (e.g. so we don't set TREE_READONLY on const variables of
5697 such type); use this function when what you care about is whether or not
5698 to try to call a constructor to create an object. The latter case is
5699 the former plus some cases of constructors that cannot be called. */
5701 bool
5702 type_build_ctor_call (tree t)
5704 tree inner;
5705 if (TYPE_NEEDS_CONSTRUCTING (t))
5706 return true;
5707 inner = strip_array_types (t);
5708 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5709 return false;
5710 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5711 return true;
5712 if (cxx_dialect < cxx11)
5713 return false;
5714 /* A user-declared constructor might be private, and a constructor might
5715 be trivial but deleted. */
5716 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5717 iter; ++iter)
5719 tree fn = *iter;
5720 if (!DECL_ARTIFICIAL (fn)
5721 || TREE_DEPRECATED (fn)
5722 || TREE_UNAVAILABLE (fn)
5723 || DECL_DELETED_FN (fn))
5724 return true;
5726 return false;
5729 /* Like type_build_ctor_call, but for destructors. */
5731 bool
5732 type_build_dtor_call (tree t)
5734 tree inner;
5735 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5736 return true;
5737 inner = strip_array_types (t);
5738 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5739 || !COMPLETE_TYPE_P (inner))
5740 return false;
5741 if (cxx_dialect < cxx11)
5742 return false;
5743 /* A user-declared destructor might be private, and a destructor might
5744 be trivial but deleted. */
5745 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5746 iter; ++iter)
5748 tree fn = *iter;
5749 if (!DECL_ARTIFICIAL (fn)
5750 || TREE_DEPRECATED (fn)
5751 || TREE_UNAVAILABLE (fn)
5752 || DECL_DELETED_FN (fn))
5753 return true;
5755 return false;
5758 /* Returns TRUE iff we need a cookie when dynamically allocating an
5759 array whose elements have the indicated class TYPE. */
5761 static bool
5762 type_requires_array_cookie (tree type)
5764 tree fns;
5765 bool has_two_argument_delete_p = false;
5767 gcc_assert (CLASS_TYPE_P (type));
5769 /* If there's a non-trivial destructor, we need a cookie. In order
5770 to iterate through the array calling the destructor for each
5771 element, we'll have to know how many elements there are. */
5772 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5773 return true;
5775 /* If the usual deallocation function is a two-argument whose second
5776 argument is of type `size_t', then we have to pass the size of
5777 the array to the deallocation function, so we will need to store
5778 a cookie. */
5779 fns = lookup_fnfields (TYPE_BINFO (type),
5780 ovl_op_identifier (false, VEC_DELETE_EXPR),
5781 /*protect=*/0, tf_warning_or_error);
5782 /* If there are no `operator []' members, or the lookup is
5783 ambiguous, then we don't need a cookie. */
5784 if (!fns || fns == error_mark_node)
5785 return false;
5786 /* Loop through all of the functions. */
5787 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5789 tree fn = *iter;
5791 /* See if this function is a one-argument delete function. If
5792 it is, then it will be the usual deallocation function. */
5793 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5794 if (second_parm == void_list_node)
5795 return false;
5796 /* Do not consider this function if its second argument is an
5797 ellipsis. */
5798 if (!second_parm)
5799 continue;
5800 /* Otherwise, if we have a two-argument function and the second
5801 argument is `size_t', it will be the usual deallocation
5802 function -- unless there is one-argument function, too. */
5803 if (TREE_CHAIN (second_parm) == void_list_node
5804 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5805 has_two_argument_delete_p = true;
5808 return has_two_argument_delete_p;
5811 /* Finish computing the `literal type' property of class type T.
5813 At this point, we have already processed base classes and
5814 non-static data members. We need to check whether the copy
5815 constructor is trivial, the destructor is trivial, and there
5816 is a trivial default constructor or at least one constexpr
5817 constructor other than the copy constructor. */
5819 static void
5820 finalize_literal_type_property (tree t)
5822 tree fn;
5824 if (cxx_dialect < cxx11)
5825 CLASSTYPE_LITERAL_P (t) = false;
5826 else if (CLASSTYPE_LITERAL_P (t)
5827 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
5828 && (cxx_dialect < cxx20 || !type_maybe_constexpr_destructor (t)))
5829 CLASSTYPE_LITERAL_P (t) = false;
5830 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5831 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5832 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5833 && CLASSTYPE_NON_AGGREGATE (t)
5834 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5835 CLASSTYPE_LITERAL_P (t) = false;
5837 /* C++14 DR 1684 removed this restriction. */
5838 if (cxx_dialect < cxx14
5839 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5840 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5841 if (TREE_CODE (fn) == FUNCTION_DECL
5842 && DECL_DECLARED_CONSTEXPR_P (fn)
5843 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5844 && !DECL_CONSTRUCTOR_P (fn))
5846 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5847 if (!DECL_GENERATED_P (fn))
5849 auto_diagnostic_group d;
5850 if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5851 "enclosing class of %<constexpr%> non-static "
5852 "member function %q+#D is not a literal type", fn))
5853 explain_non_literal_class (t);
5858 /* T is a non-literal type used in a context which requires a constant
5859 expression. Explain why it isn't literal. */
5861 void
5862 explain_non_literal_class (tree t)
5864 static hash_set<tree> *diagnosed;
5866 if (!CLASS_TYPE_P (t))
5867 return;
5868 t = TYPE_MAIN_VARIANT (t);
5870 if (diagnosed == NULL)
5871 diagnosed = new hash_set<tree>;
5872 if (diagnosed->add (t))
5873 /* Already explained. */
5874 return;
5876 auto_diagnostic_group d;
5877 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5878 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5879 inform (UNKNOWN_LOCATION,
5880 " %qT is a closure type, which is only literal in "
5881 "C++17 and later", t);
5882 else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5883 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5884 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
5885 && !type_maybe_constexpr_destructor (t))
5886 inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
5888 else if (CLASSTYPE_NON_AGGREGATE (t)
5889 && !TYPE_HAS_TRIVIAL_DFLT (t)
5890 && !LAMBDA_TYPE_P (t)
5891 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5893 inform (UNKNOWN_LOCATION,
5894 " %q+T is not an aggregate, does not have a trivial "
5895 "default constructor, and has no %<constexpr%> constructor that "
5896 "is not a copy or move constructor", t);
5897 if (type_has_non_user_provided_default_constructor (t))
5898 /* Note that we can't simply call locate_ctor because when the
5899 constructor is deleted it just returns NULL_TREE. */
5900 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5902 tree fn = *iter;
5903 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5905 parms = skip_artificial_parms_for (fn, parms);
5907 if (sufficient_parms_p (parms))
5909 if (DECL_DELETED_FN (fn))
5910 maybe_explain_implicit_delete (fn);
5911 else
5912 explain_invalid_constexpr_fn (fn);
5913 break;
5917 else
5919 tree binfo, base_binfo, field; int i;
5920 for (binfo = TYPE_BINFO (t), i = 0;
5921 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5923 tree basetype = TREE_TYPE (base_binfo);
5924 if (!CLASSTYPE_LITERAL_P (basetype))
5926 inform (UNKNOWN_LOCATION,
5927 " base class %qT of %q+T is non-literal",
5928 basetype, t);
5929 explain_non_literal_class (basetype);
5930 return;
5933 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5935 tree ftype;
5936 if (TREE_CODE (field) != FIELD_DECL)
5937 continue;
5938 ftype = TREE_TYPE (field);
5939 if (!literal_type_p (ftype))
5941 inform (DECL_SOURCE_LOCATION (field),
5942 " non-static data member %qD has non-literal type",
5943 field);
5944 if (CLASS_TYPE_P (ftype))
5945 explain_non_literal_class (ftype);
5947 if (CP_TYPE_VOLATILE_P (ftype))
5948 inform (DECL_SOURCE_LOCATION (field),
5949 " non-static data member %qD has volatile type", field);
5954 /* Check the validity of the bases and members declared in T. Add any
5955 implicitly-generated functions (like copy-constructors and
5956 assignment operators). Compute various flag bits (like
5957 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5958 level: i.e., independently of the ABI in use. */
5960 static void
5961 check_bases_and_members (tree t)
5963 /* Nonzero if the implicitly generated copy constructor should take
5964 a non-const reference argument. */
5965 int cant_have_const_ctor;
5966 /* Nonzero if the implicitly generated assignment operator
5967 should take a non-const reference argument. */
5968 int no_const_asn_ref;
5969 tree access_decls;
5970 bool saved_complex_asn_ref;
5971 bool saved_nontrivial_dtor;
5972 tree fn;
5974 /* By default, we use const reference arguments and generate default
5975 constructors. */
5976 cant_have_const_ctor = 0;
5977 no_const_asn_ref = 0;
5979 /* Check all the base-classes and set FMEM members to point to arrays
5980 of potential interest. */
5981 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5983 /* Deduce noexcept on destructor. This needs to happen after we've set
5984 triviality flags appropriately for our bases. */
5985 if (cxx_dialect >= cxx11)
5986 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5987 deduce_noexcept_on_destructor (dtor);
5989 /* Check all the method declarations. */
5990 check_methods (t);
5992 /* Save the initial values of these flags which only indicate whether
5993 or not the class has user-provided functions. As we analyze the
5994 bases and members we can set these flags for other reasons. */
5995 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5996 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5998 /* Check all the data member declarations. We cannot call
5999 check_field_decls until we have called check_bases check_methods,
6000 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6001 being set appropriately. */
6002 check_field_decls (t, &access_decls,
6003 &cant_have_const_ctor,
6004 &no_const_asn_ref);
6006 /* A nearly-empty class has to be vptr-containing; a nearly empty
6007 class contains just a vptr. */
6008 if (!TYPE_CONTAINS_VPTR_P (t))
6009 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
6011 /* Do some bookkeeping that will guide the generation of implicitly
6012 declared member functions. */
6013 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6014 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6015 /* We need to call a constructor for this class if it has a
6016 user-provided constructor, or if the default constructor is going
6017 to initialize the vptr. (This is not an if-and-only-if;
6018 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
6019 themselves need constructing.) */
6020 TYPE_NEEDS_CONSTRUCTING (t)
6021 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
6022 /* [dcl.init.aggr]
6024 An aggregate is an array or a class with no user-provided
6025 constructors ... and no virtual functions.
6027 Again, other conditions for being an aggregate are checked
6028 elsewhere. */
6029 CLASSTYPE_NON_AGGREGATE (t)
6030 |= ((cxx_dialect < cxx20
6031 ? type_has_user_provided_or_explicit_constructor (t)
6032 : TYPE_HAS_USER_CONSTRUCTOR (t))
6033 || TYPE_POLYMORPHIC_P (t));
6034 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6035 retain the old definition internally for ABI reasons. */
6036 CLASSTYPE_NON_LAYOUT_POD_P (t)
6037 |= (CLASSTYPE_NON_AGGREGATE (t)
6038 || saved_nontrivial_dtor || saved_complex_asn_ref);
6039 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
6040 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6041 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6042 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
6044 /* If the only explicitly declared default constructor is user-provided,
6045 set TYPE_HAS_COMPLEX_DFLT. */
6046 if (!TYPE_HAS_COMPLEX_DFLT (t)
6047 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
6048 && !type_has_non_user_provided_default_constructor (t))
6049 TYPE_HAS_COMPLEX_DFLT (t) = true;
6051 /* Warn if a public base of a polymorphic type has an accessible
6052 non-virtual destructor. It is only now that we know the class is
6053 polymorphic. Although a polymorphic base will have a already
6054 been diagnosed during its definition, we warn on use too. */
6055 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
6057 tree binfo = TYPE_BINFO (t);
6058 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
6059 tree base_binfo;
6060 unsigned i;
6062 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6064 tree basetype = TREE_TYPE (base_binfo);
6066 if ((*accesses)[i] == access_public_node
6067 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
6068 && accessible_nvdtor_p (basetype))
6069 warning (OPT_Wnon_virtual_dtor,
6070 "base class %q#T has accessible non-virtual destructor",
6071 basetype);
6075 /* If the class has no user-declared constructor, but does have
6076 non-static const or reference data members that can never be
6077 initialized, issue a warning. */
6078 if (warn_uninitialized
6079 /* Classes with user-declared constructors are presumed to
6080 initialize these members. */
6081 && !TYPE_HAS_USER_CONSTRUCTOR (t)
6082 /* Aggregates can be initialized with brace-enclosed
6083 initializers. */
6084 && CLASSTYPE_NON_AGGREGATE (t))
6086 tree field;
6088 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6090 tree type;
6092 if (TREE_CODE (field) != FIELD_DECL
6093 || DECL_INITIAL (field) != NULL_TREE)
6094 continue;
6096 type = TREE_TYPE (field);
6097 if (TYPE_REF_P (type))
6098 warning_at (DECL_SOURCE_LOCATION (field),
6099 OPT_Wuninitialized, "non-static reference %q#D "
6100 "in class without a constructor", field);
6101 else if (CP_TYPE_CONST_P (type)
6102 && (!CLASS_TYPE_P (type)
6103 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6104 warning_at (DECL_SOURCE_LOCATION (field),
6105 OPT_Wuninitialized, "non-static const member %q#D "
6106 "in class without a constructor", field);
6110 /* Synthesize any needed methods. */
6111 add_implicitly_declared_members (t, &access_decls,
6112 cant_have_const_ctor,
6113 no_const_asn_ref);
6115 /* Check defaulted declarations here so we have cant_have_const_ctor
6116 and don't need to worry about clones. */
6117 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6118 if (DECL_DECLARES_FUNCTION_P (fn)
6119 && !DECL_ARTIFICIAL (fn)
6120 && DECL_DEFAULTED_IN_CLASS_P (fn))
6122 /* ...except handle comparisons later, in finish_struct_1. */
6123 if (special_function_p (fn) == sfk_comparison)
6124 continue;
6126 int copy = copy_fn_p (fn);
6127 if (copy > 0)
6129 bool imp_const_p
6130 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6131 : !no_const_asn_ref);
6132 bool fn_const_p = (copy == 2);
6134 if (fn_const_p && !imp_const_p)
6135 /* If the function is defaulted outside the class, we just
6136 give the synthesis error. Core Issue #1331 says this is
6137 no longer ill-formed, it is defined as deleted instead. */
6138 DECL_DELETED_FN (fn) = true;
6140 defaulted_late_check (fn);
6143 if (LAMBDA_TYPE_P (t))
6144 /* "This class type is not an aggregate." */
6145 CLASSTYPE_NON_AGGREGATE (t) = 1;
6147 /* Compute the 'literal type' property before we
6148 do anything with non-static member functions. */
6149 finalize_literal_type_property (t);
6151 /* Create the in-charge and not-in-charge variants of constructors
6152 and destructors. */
6153 clone_constructors_and_destructors (t);
6155 /* Process the using-declarations. */
6156 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6157 handle_using_decl (TREE_VALUE (access_decls), t);
6159 /* Figure out whether or not we will need a cookie when dynamically
6160 allocating an array of this type. */
6161 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6162 = type_requires_array_cookie (t);
6165 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6166 accordingly. If a new vfield was created (because T doesn't have a
6167 primary base class), then the newly created field is returned. It
6168 is not added to the TYPE_FIELDS list; it is the caller's
6169 responsibility to do that. Accumulate declared virtual functions
6170 on VIRTUALS_P. */
6172 static tree
6173 create_vtable_ptr (tree t, tree* virtuals_p)
6175 tree fn;
6177 /* Collect the virtual functions declared in T. */
6178 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6179 if (TREE_CODE (fn) == FUNCTION_DECL
6180 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6181 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6183 tree new_virtual = make_node (TREE_LIST);
6185 BV_FN (new_virtual) = fn;
6186 BV_DELTA (new_virtual) = integer_zero_node;
6187 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6189 TREE_CHAIN (new_virtual) = *virtuals_p;
6190 *virtuals_p = new_virtual;
6193 /* If we couldn't find an appropriate base class, create a new field
6194 here. Even if there weren't any new virtual functions, we might need a
6195 new virtual function table if we're supposed to include vptrs in
6196 all classes that need them. */
6197 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6199 /* We build this decl with vtbl_ptr_type_node, which is a
6200 `vtable_entry_type*'. It might seem more precise to use
6201 `vtable_entry_type (*)[N]' where N is the number of virtual
6202 functions. However, that would require the vtable pointer in
6203 base classes to have a different type than the vtable pointer
6204 in derived classes. We could make that happen, but that
6205 still wouldn't solve all the problems. In particular, the
6206 type-based alias analysis code would decide that assignments
6207 to the base class vtable pointer can't alias assignments to
6208 the derived class vtable pointer, since they have different
6209 types. Thus, in a derived class destructor, where the base
6210 class constructor was inlined, we could generate bad code for
6211 setting up the vtable pointer.
6213 Therefore, we use one type for all vtable pointers. We still
6214 use a type-correct type; it's just doesn't indicate the array
6215 bounds. That's better than using `void*' or some such; it's
6216 cleaner, and it let's the alias analysis code know that these
6217 stores cannot alias stores to void*! */
6218 tree field;
6220 field = build_decl (input_location,
6221 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6222 DECL_VIRTUAL_P (field) = 1;
6223 DECL_ARTIFICIAL (field) = 1;
6224 DECL_FIELD_CONTEXT (field) = t;
6225 DECL_FCONTEXT (field) = t;
6226 if (TYPE_PACKED (t))
6227 DECL_PACKED (field) = 1;
6229 TYPE_VFIELD (t) = field;
6231 /* This class is non-empty. */
6232 CLASSTYPE_EMPTY_P (t) = 0;
6234 return field;
6237 return NULL_TREE;
6240 /* Add OFFSET to all base types of BINFO which is a base in the
6241 hierarchy dominated by T.
6243 OFFSET, which is a type offset, is number of bytes. */
6245 static void
6246 propagate_binfo_offsets (tree binfo, tree offset)
6248 int i;
6249 tree primary_binfo;
6250 tree base_binfo;
6252 /* Update BINFO's offset. */
6253 BINFO_OFFSET (binfo)
6254 = fold_convert (sizetype,
6255 size_binop (PLUS_EXPR,
6256 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6257 offset));
6259 /* Find the primary base class. */
6260 primary_binfo = get_primary_binfo (binfo);
6262 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6263 propagate_binfo_offsets (primary_binfo, offset);
6265 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6266 downwards. */
6267 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6269 /* Don't do the primary base twice. */
6270 if (base_binfo == primary_binfo)
6271 continue;
6273 if (BINFO_VIRTUAL_P (base_binfo))
6274 continue;
6276 propagate_binfo_offsets (base_binfo, offset);
6280 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6281 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6282 empty subobjects of T. */
6284 static void
6285 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6287 tree vbase;
6288 tree t = rli->t;
6289 tree *next_field;
6291 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6292 return;
6294 /* Find the last field. The artificial fields created for virtual
6295 bases will go after the last extant field to date. */
6296 next_field = &TYPE_FIELDS (t);
6297 while (*next_field)
6298 next_field = &DECL_CHAIN (*next_field);
6300 /* Go through the virtual bases, allocating space for each virtual
6301 base that is not already a primary base class. These are
6302 allocated in inheritance graph order. */
6303 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6305 if (!BINFO_VIRTUAL_P (vbase))
6306 continue;
6308 if (!BINFO_PRIMARY_P (vbase))
6310 /* This virtual base is not a primary base of any class in the
6311 hierarchy, so we have to add space for it. */
6312 next_field = build_base_field (rli, vbase,
6313 access_private_node,
6314 offsets, next_field);
6319 /* Returns the offset of the byte just past the end of the base class
6320 BINFO. */
6322 static tree
6323 end_of_base (tree binfo)
6325 tree size;
6327 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6328 size = TYPE_SIZE_UNIT (char_type_node);
6329 else if (is_empty_class (BINFO_TYPE (binfo)))
6330 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6331 allocate some space for it. It cannot have virtual bases, so
6332 TYPE_SIZE_UNIT is fine. */
6333 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6334 else
6335 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6337 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6340 /* Returns the offset of the byte just past the end of the base class or empty
6341 data member with the highest offset in T. If INCLUDE_VIRTUALS_P is zero,
6342 then only non-virtual bases are included. */
6344 static tree
6345 end_of_class (tree t, bool include_virtuals_p)
6347 tree result = size_zero_node;
6348 vec<tree, va_gc> *vbases;
6349 tree binfo;
6350 tree base_binfo;
6351 tree offset;
6352 int i;
6354 for (binfo = TYPE_BINFO (t), i = 0;
6355 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6357 if (!include_virtuals_p
6358 && BINFO_VIRTUAL_P (base_binfo)
6359 && (!BINFO_PRIMARY_P (base_binfo)
6360 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6361 continue;
6363 offset = end_of_base (base_binfo);
6364 if (tree_int_cst_lt (result, offset))
6365 result = offset;
6368 /* Also consider empty data members. */
6369 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6370 if (TREE_CODE (field) == FIELD_DECL
6371 && !DECL_ARTIFICIAL (field)
6372 && field_poverlapping_p (field)
6373 && is_empty_class (TREE_TYPE (field)))
6375 /* Update sizeof(C) to max (sizeof(C), offset(D)+sizeof(D)) */
6376 offset = size_binop (PLUS_EXPR, DECL_FIELD_OFFSET (field),
6377 TYPE_SIZE_UNIT (TREE_TYPE (field)));
6378 if (tree_int_cst_lt (result, offset))
6379 result = offset;
6382 if (include_virtuals_p)
6383 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6384 vec_safe_iterate (vbases, i, &base_binfo); i++)
6386 offset = end_of_base (base_binfo);
6387 if (tree_int_cst_lt (result, offset))
6388 result = offset;
6391 return result;
6394 /* Warn about bases of T that are inaccessible because they are
6395 ambiguous. For example:
6397 struct S {};
6398 struct T : public S {};
6399 struct U : public S, public T {};
6401 Here, `(S*) new U' is not allowed because there are two `S'
6402 subobjects of U. */
6404 static void
6405 maybe_warn_about_inaccessible_bases (tree t)
6407 int i;
6408 vec<tree, va_gc> *vbases;
6409 tree basetype;
6410 tree binfo;
6411 tree base_binfo;
6413 /* If not checking for warning then return early. */
6414 if (!warn_inaccessible_base)
6415 return;
6417 /* If there are no repeated bases, nothing can be ambiguous. */
6418 if (!CLASSTYPE_REPEATED_BASE_P (t))
6419 return;
6421 /* Check direct bases. */
6422 for (binfo = TYPE_BINFO (t), i = 0;
6423 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6425 basetype = BINFO_TYPE (base_binfo);
6427 if (!uniquely_derived_from_p (basetype, t))
6428 warning (OPT_Winaccessible_base, "direct base %qT inaccessible "
6429 "in %qT due to ambiguity", basetype, t);
6432 /* Check for ambiguous virtual bases. */
6433 if (extra_warnings)
6434 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6435 vec_safe_iterate (vbases, i, &binfo); i++)
6437 basetype = BINFO_TYPE (binfo);
6439 if (!uniquely_derived_from_p (basetype, t))
6440 warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in "
6441 "%qT due to ambiguity", basetype, t);
6445 /* Compare two INTEGER_CSTs K1 and K2. */
6447 static int
6448 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6450 return tree_int_cst_compare ((tree) k1, (tree) k2);
6453 /* Increase the size indicated in RLI to account for empty classes
6454 that are "off the end" of the class. */
6456 static void
6457 include_empty_classes (record_layout_info rli)
6459 tree eoc;
6460 tree rli_size;
6462 /* It might be the case that we grew the class to allocate a
6463 zero-sized base class. That won't be reflected in RLI, yet,
6464 because we are willing to overlay multiple bases at the same
6465 offset. However, now we need to make sure that RLI is big enough
6466 to reflect the entire class. */
6467 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6468 rli_size = rli_size_unit_so_far (rli);
6469 if (TREE_CODE (rli_size) == INTEGER_CST
6470 && tree_int_cst_lt (rli_size, eoc))
6472 /* The size should have been rounded to a whole byte. */
6473 gcc_assert (tree_int_cst_equal
6474 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6475 rli->bitpos
6476 = size_binop (PLUS_EXPR,
6477 rli->bitpos,
6478 size_binop (MULT_EXPR,
6479 fold_convert (bitsizetype,
6480 size_binop (MINUS_EXPR,
6481 eoc, rli_size)),
6482 bitsize_int (BITS_PER_UNIT)));
6483 normalize_rli (rli);
6487 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6488 BINFO_OFFSETs for all of the base-classes. Position the vtable
6489 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6491 static void
6492 layout_class_type (tree t, tree *virtuals_p)
6494 tree non_static_data_members;
6495 tree field;
6496 tree vptr;
6497 record_layout_info rli;
6498 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6499 types that appear at that offset. */
6500 splay_tree empty_base_offsets;
6501 /* True if the last field laid out was a bit-field. */
6502 bool last_field_was_bitfield = false;
6503 /* The location at which the next field should be inserted. */
6504 tree *next_field;
6506 /* Keep track of the first non-static data member. */
6507 non_static_data_members = TYPE_FIELDS (t);
6509 /* Start laying out the record. */
6510 rli = start_record_layout (t);
6512 /* Mark all the primary bases in the hierarchy. */
6513 determine_primary_bases (t);
6515 /* Create a pointer to our virtual function table. */
6516 vptr = create_vtable_ptr (t, virtuals_p);
6518 /* The vptr is always the first thing in the class. */
6519 if (vptr)
6521 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6522 TYPE_FIELDS (t) = vptr;
6523 next_field = &DECL_CHAIN (vptr);
6524 place_field (rli, vptr);
6526 else
6527 next_field = &TYPE_FIELDS (t);
6529 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6530 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6531 NULL, NULL);
6532 build_base_fields (rli, empty_base_offsets, next_field);
6534 /* Layout the non-static data members. */
6535 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6537 tree type;
6538 tree padding;
6540 /* We still pass things that aren't non-static data members to
6541 the back end, in case it wants to do something with them. */
6542 if (TREE_CODE (field) != FIELD_DECL)
6544 place_field (rli, field);
6545 /* If the static data member has incomplete type, keep track
6546 of it so that it can be completed later. (The handling
6547 of pending statics in finish_record_layout is
6548 insufficient; consider:
6550 struct S1;
6551 struct S2 { static S1 s1; };
6553 At this point, finish_record_layout will be called, but
6554 S1 is still incomplete.) */
6555 if (VAR_P (field))
6557 maybe_register_incomplete_var (field);
6558 /* The visibility of static data members is determined
6559 at their point of declaration, not their point of
6560 definition. */
6561 determine_visibility (field);
6563 continue;
6566 type = TREE_TYPE (field);
6567 if (type == error_mark_node)
6568 continue;
6570 padding = NULL_TREE;
6572 bool might_overlap = field_poverlapping_p (field);
6574 if (might_overlap && CLASS_TYPE_P (type)
6575 && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type)))
6577 /* if D is a potentially-overlapping data member, update sizeof(C) to
6578 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6579 tree nvsize = CLASSTYPE_SIZE_UNIT (type);
6580 /* end_of_class doesn't always give dsize, but it does in the case of
6581 a class with virtual bases, which is when dsize > nvsize. */
6582 tree dsize = end_of_class (type, /*vbases*/true);
6583 if (CLASSTYPE_EMPTY_P (type))
6584 DECL_SIZE (field) = DECL_SIZE_UNIT (field) = size_zero_node;
6585 else if (tree_int_cst_le (dsize, nvsize))
6587 DECL_SIZE_UNIT (field) = nvsize;
6588 DECL_SIZE (field) = CLASSTYPE_SIZE (type);
6590 else
6592 DECL_SIZE_UNIT (field) = dsize;
6593 DECL_SIZE (field) = bit_from_pos (dsize, bitsize_zero_node);
6597 /* If this field is a bit-field whose width is greater than its
6598 type, then there are some special rules for allocating
6599 it. */
6600 if (DECL_C_BIT_FIELD (field)
6601 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6603 bool was_unnamed_p = false;
6604 /* We must allocate the bits as if suitably aligned for the
6605 longest integer type that fits in this many bits. Then,
6606 we are supposed to use the left over bits as additional
6607 padding. */
6609 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6610 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6611 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6612 limit = DECL_SIZE (field);
6614 tree integer_type = integer_types[itk_char];
6615 for (unsigned itk = itk_char; itk != itk_none; itk++)
6616 if (tree next = integer_types[itk])
6618 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6619 /* Too big, so our current guess is what we want. */
6620 break;
6621 /* Not bigger than limit, ok */
6622 integer_type = next;
6625 /* Figure out how much additional padding is required. */
6626 if (TREE_CODE (t) == UNION_TYPE)
6627 /* In a union, the padding field must have the full width
6628 of the bit-field; all fields start at offset zero. */
6629 padding = DECL_SIZE (field);
6630 else
6631 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6632 TYPE_SIZE (integer_type));
6634 if (integer_zerop (padding))
6635 padding = NULL_TREE;
6637 /* An unnamed bitfield does not normally affect the
6638 alignment of the containing class on a target where
6639 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6640 make any exceptions for unnamed bitfields when the
6641 bitfields are longer than their types. Therefore, we
6642 temporarily give the field a name. */
6643 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6645 was_unnamed_p = true;
6646 DECL_NAME (field) = make_anon_name ();
6649 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6650 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6651 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6652 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6653 empty_base_offsets);
6654 if (was_unnamed_p)
6655 DECL_NAME (field) = NULL_TREE;
6656 /* Now that layout has been performed, set the size of the
6657 field to the size of its declared type; the rest of the
6658 field is effectively invisible. */
6659 DECL_SIZE (field) = TYPE_SIZE (type);
6660 /* We must also reset the DECL_MODE of the field. */
6661 SET_DECL_MODE (field, TYPE_MODE (type));
6663 else if (might_overlap && is_empty_class (type))
6665 SET_DECL_FIELD_ABI_IGNORED (field, 1);
6666 layout_empty_base_or_field (rli, field, empty_base_offsets);
6668 else
6669 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6670 empty_base_offsets);
6672 /* Remember the location of any empty classes in FIELD. */
6673 record_subobject_offsets (field, empty_base_offsets);
6675 /* If a bit-field does not immediately follow another bit-field,
6676 and yet it starts in the middle of a byte, we have failed to
6677 comply with the ABI. */
6678 if (warn_abi
6679 && DECL_C_BIT_FIELD (field)
6680 /* The TREE_NO_WARNING flag gets set by Objective-C when
6681 laying out an Objective-C class. The ObjC ABI differs
6682 from the C++ ABI, and so we do not want a warning
6683 here. */
6684 && !warning_suppressed_p (field, OPT_Wabi)
6685 && !last_field_was_bitfield
6686 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6687 DECL_FIELD_BIT_OFFSET (field),
6688 bitsize_unit_node)))
6689 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6690 "offset of %qD is not ABI-compliant and may "
6691 "change in a future version of GCC", field);
6693 /* The middle end uses the type of expressions to determine the
6694 possible range of expression values. In order to optimize
6695 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6696 must be made aware of the width of "i", via its type.
6698 Because C++ does not have integer types of arbitrary width,
6699 we must (for the purposes of the front end) convert from the
6700 type assigned here to the declared type of the bitfield
6701 whenever a bitfield expression is used as an rvalue.
6702 Similarly, when assigning a value to a bitfield, the value
6703 must be converted to the type given the bitfield here. */
6704 if (DECL_C_BIT_FIELD (field))
6706 unsigned HOST_WIDE_INT width;
6707 tree ftype = TREE_TYPE (field);
6708 width = tree_to_uhwi (DECL_SIZE (field));
6709 if (width != TYPE_PRECISION (ftype))
6711 TREE_TYPE (field)
6712 = c_build_bitfield_integer_type (width,
6713 TYPE_UNSIGNED (ftype));
6714 TREE_TYPE (field)
6715 = cp_build_qualified_type (TREE_TYPE (field),
6716 cp_type_quals (ftype));
6720 /* If we needed additional padding after this field, add it
6721 now. */
6722 if (padding)
6724 tree padding_field;
6726 padding_field = build_decl (input_location,
6727 FIELD_DECL,
6728 NULL_TREE,
6729 char_type_node);
6730 DECL_BIT_FIELD (padding_field) = 1;
6731 DECL_SIZE (padding_field) = padding;
6732 DECL_CONTEXT (padding_field) = t;
6733 DECL_ARTIFICIAL (padding_field) = 1;
6734 DECL_IGNORED_P (padding_field) = 1;
6735 DECL_PADDING_P (padding_field) = 1;
6736 layout_nonempty_base_or_field (rli, padding_field,
6737 NULL_TREE,
6738 empty_base_offsets);
6741 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6744 if (!integer_zerop (rli->bitpos))
6746 /* Make sure that we are on a byte boundary so that the size of
6747 the class without virtual bases will always be a round number
6748 of bytes. */
6749 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6750 normalize_rli (rli);
6753 /* We used to remove zero width bitfields at this point since PR42217,
6754 while the C FE never did that. That caused ABI differences on various
6755 targets. Set the DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD flag on them
6756 instead, so that the backends can emit -Wpsabi warnings in the cases
6757 where the ABI changed. */
6758 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6759 if (TREE_CODE (field) == FIELD_DECL
6760 && DECL_C_BIT_FIELD (field)
6761 /* We should not be confused by the fact that grokbitfield
6762 temporarily sets the width of the bit field into
6763 DECL_BIT_FIELD_REPRESENTATIVE (field).
6764 check_bitfield_decl eventually sets DECL_SIZE (field)
6765 to that width. */
6766 && (DECL_SIZE (field) == NULL_TREE
6767 || integer_zerop (DECL_SIZE (field))))
6768 SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field, 1);
6770 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6772 /* T needs a different layout as a base (eliding virtual bases
6773 or whatever). Create that version. */
6774 tree base_t = make_node (TREE_CODE (t));
6775 tree base_d = create_implicit_typedef (as_base_identifier, base_t);
6777 TYPE_CONTEXT (base_t) = t;
6778 DECL_CONTEXT (base_d) = t;
6780 set_instantiating_module (base_d);
6782 /* If the ABI version is not at least two, and the last
6783 field was a bit-field, RLI may not be on a byte
6784 boundary. In particular, rli_size_unit_so_far might
6785 indicate the last complete byte, while rli_size_so_far
6786 indicates the total number of bits used. Therefore,
6787 rli_size_so_far, rather than rli_size_unit_so_far, is
6788 used to compute TYPE_SIZE_UNIT. */
6790 /* Set the size and alignment for the new type. */
6791 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6792 TYPE_SIZE_UNIT (base_t)
6793 = size_binop (MAX_EXPR,
6794 fold_convert (sizetype,
6795 size_binop (CEIL_DIV_EXPR,
6796 rli_size_so_far (rli),
6797 bitsize_int (BITS_PER_UNIT))),
6798 eoc);
6799 TYPE_SIZE (base_t)
6800 = size_binop (MAX_EXPR,
6801 rli_size_so_far (rli),
6802 size_binop (MULT_EXPR,
6803 fold_convert (bitsizetype, eoc),
6804 bitsize_int (BITS_PER_UNIT)));
6805 SET_TYPE_ALIGN (base_t, rli->record_align);
6806 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6807 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
6808 TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t);
6810 /* Copy the non-static data members of T. This will include its
6811 direct non-virtual bases & vtable. */
6812 next_field = &TYPE_FIELDS (base_t);
6813 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6814 if (TREE_CODE (field) == FIELD_DECL)
6816 *next_field = copy_node (field);
6817 /* Zap any NSDMI, it's not needed and might be a deferred
6818 parse. */
6819 DECL_INITIAL (*next_field) = NULL_TREE;
6820 DECL_CONTEXT (*next_field) = base_t;
6821 next_field = &DECL_CHAIN (*next_field);
6823 *next_field = NULL_TREE;
6825 /* We use the base type for trivial assignments, and hence it
6826 needs a mode. */
6827 compute_record_mode (base_t);
6829 /* Record the base version of the type. */
6830 CLASSTYPE_AS_BASE (t) = base_t;
6832 else
6833 CLASSTYPE_AS_BASE (t) = t;
6835 /* Every empty class contains an empty class. */
6836 if (CLASSTYPE_EMPTY_P (t))
6837 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6839 /* Set the TYPE_DECL for this type to contain the right
6840 value for DECL_OFFSET, so that we can use it as part
6841 of a COMPONENT_REF for multiple inheritance. */
6842 layout_decl (TYPE_MAIN_DECL (t), 0);
6844 /* Now fix up any virtual base class types that we left lying
6845 around. We must get these done before we try to lay out the
6846 virtual function table. As a side-effect, this will remove the
6847 base subobject fields. */
6848 layout_virtual_bases (rli, empty_base_offsets);
6850 /* Make sure that empty classes are reflected in RLI at this
6851 point. */
6852 include_empty_classes (rli);
6854 /* Make sure not to create any structures with zero size. */
6855 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6856 place_field (rli,
6857 build_decl (input_location,
6858 FIELD_DECL, NULL_TREE, char_type_node));
6860 /* If this is a non-POD, declaring it packed makes a difference to how it
6861 can be used as a field; don't let finalize_record_size undo it. */
6862 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6863 rli->packed_maybe_necessary = true;
6865 /* Let the back end lay out the type. */
6866 finish_record_layout (rli, /*free_p=*/true);
6868 /* If we didn't end up needing an as-base type, don't use it. */
6869 if (CLASSTYPE_AS_BASE (t) != t
6870 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
6871 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
6872 causing us to lose the user-specified alignment as in PR94050. */
6873 && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t))
6874 && tree_int_cst_equal (TYPE_SIZE (t),
6875 TYPE_SIZE (CLASSTYPE_AS_BASE (t))))
6876 CLASSTYPE_AS_BASE (t) = t;
6878 if (TYPE_SIZE_UNIT (t)
6879 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6880 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6881 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6882 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6884 /* Warn about bases that can't be talked about due to ambiguity. */
6885 maybe_warn_about_inaccessible_bases (t);
6887 /* Now that we're done with layout, give the base fields the real types. */
6888 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6889 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6890 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6892 /* Clean up. */
6893 splay_tree_delete (empty_base_offsets);
6895 if (CLASSTYPE_EMPTY_P (t)
6896 && tree_int_cst_lt (sizeof_biggest_empty_class,
6897 TYPE_SIZE_UNIT (t)))
6898 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6901 /* Determine the "key method" for the class type indicated by TYPE,
6902 and set CLASSTYPE_KEY_METHOD accordingly. */
6904 void
6905 determine_key_method (tree type)
6907 tree method;
6909 if (processing_template_decl
6910 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6911 || CLASSTYPE_INTERFACE_KNOWN (type))
6912 return;
6914 /* The key method is the first non-pure virtual function that is not
6915 inline at the point of class definition. On some targets the
6916 key function may not be inline; those targets should not call
6917 this function until the end of the translation unit. */
6918 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6919 if (TREE_CODE (method) == FUNCTION_DECL
6920 && DECL_VINDEX (method) != NULL_TREE
6921 && ! DECL_DECLARED_INLINE_P (method)
6922 && ! DECL_PURE_VIRTUAL_P (method))
6924 CLASSTYPE_KEY_METHOD (type) = method;
6925 break;
6928 return;
6931 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6932 class data member of non-zero size, otherwise false. */
6934 static inline bool
6935 field_nonempty_p (const_tree fld)
6937 if (TREE_CODE (fld) == ERROR_MARK)
6938 return false;
6940 tree type = TREE_TYPE (fld);
6941 if (TREE_CODE (fld) == FIELD_DECL
6942 && TREE_CODE (type) != ERROR_MARK
6943 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6945 return TYPE_SIZE (type)
6946 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6947 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6950 return false;
6953 /* Used by find_flexarrays and related functions. */
6955 struct flexmems_t
6957 /* The first flexible array member or non-zero array member found
6958 in the order of layout. */
6959 tree array;
6960 /* First non-static non-empty data member in the class or its bases. */
6961 tree first;
6962 /* The first non-static non-empty data member following either
6963 the flexible array member, if found, or the zero-length array member
6964 otherwise. AFTER[1] refers to the first such data member of a union
6965 of which the struct containing the flexible array member or zero-length
6966 array is a member, or NULL when no such union exists. This element is
6967 only used during searching, not for diagnosing problems. AFTER[0]
6968 refers to the first such data member that is not a member of such
6969 a union. */
6970 tree after[2];
6972 /* Refers to a struct (not union) in which the struct of which the flexible
6973 array is member is defined. Used to diagnose strictly (according to C)
6974 invalid uses of the latter structs. */
6975 tree enclosing;
6978 /* Find either the first flexible array member or the first zero-length
6979 array, in that order of preference, among members of class T (but not
6980 its base classes), and set members of FMEM accordingly.
6981 BASE_P is true if T is a base class of another class.
6982 PUN is set to the outermost union in which the flexible array member
6983 (or zero-length array) is defined if one such union exists, otherwise
6984 to NULL.
6985 Similarly, PSTR is set to a data member of the outermost struct of
6986 which the flexible array is a member if one such struct exists,
6987 otherwise to NULL. */
6989 static void
6990 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6991 tree pun /* = NULL_TREE */,
6992 tree pstr /* = NULL_TREE */)
6994 /* Set the "pointer" to the outermost enclosing union if not set
6995 yet and maintain it for the remainder of the recursion. */
6996 if (!pun && TREE_CODE (t) == UNION_TYPE)
6997 pun = t;
6999 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
7001 if (fld == error_mark_node)
7002 return;
7004 /* Is FLD a typedef for an anonymous struct? */
7006 /* FIXME: Note that typedefs (as well as arrays) need to be fully
7007 handled elsewhere so that errors like the following are detected
7008 as well:
7009 typedef struct { int i, a[], j; } S; // bug c++/72753
7010 S s [2]; // bug c++/68489
7012 if (TREE_CODE (fld) == TYPE_DECL
7013 && DECL_IMPLICIT_TYPEDEF_P (fld)
7014 && CLASS_TYPE_P (TREE_TYPE (fld))
7015 && IDENTIFIER_ANON_P (DECL_NAME (fld)))
7017 /* Check the nested unnamed type referenced via a typedef
7018 independently of FMEM (since it's not a data member of
7019 the enclosing class). */
7020 check_flexarrays (TREE_TYPE (fld));
7021 continue;
7024 /* Skip anything that's GCC-generated or not a (non-static) data
7025 member. */
7026 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
7027 continue;
7029 /* Type of the member. */
7030 tree fldtype = TREE_TYPE (fld);
7031 if (fldtype == error_mark_node)
7032 return;
7034 /* Determine the type of the array element or object referenced
7035 by the member so that it can be checked for flexible array
7036 members if it hasn't been yet. */
7037 tree eltype = fldtype;
7038 while (TREE_CODE (eltype) == ARRAY_TYPE
7039 || INDIRECT_TYPE_P (eltype))
7040 eltype = TREE_TYPE (eltype);
7042 if (RECORD_OR_UNION_TYPE_P (eltype))
7044 if (fmem->array && !fmem->after[bool (pun)])
7046 /* Once the member after the flexible array has been found
7047 we're done. */
7048 fmem->after[bool (pun)] = fld;
7049 break;
7052 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
7054 /* Descend into the non-static member struct or union and try
7055 to find a flexible array member or zero-length array among
7056 its members. This is only necessary for anonymous types
7057 and types in whose context the current type T has not been
7058 defined (the latter must not be checked again because they
7059 are already in the process of being checked by one of the
7060 recursive calls). */
7062 tree first = fmem->first;
7063 tree array = fmem->array;
7065 /* If this member isn't anonymous and a prior non-flexible array
7066 member has been seen in one of the enclosing structs, clear
7067 the FIRST member since it doesn't contribute to the flexible
7068 array struct's members. */
7069 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7070 fmem->first = NULL_TREE;
7072 find_flexarrays (eltype, fmem, false, pun,
7073 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
7075 if (fmem->array != array)
7076 continue;
7078 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7080 /* Restore the FIRST member reset above if no flexible
7081 array member has been found in this member's struct. */
7082 fmem->first = first;
7085 /* If the member struct contains the first flexible array
7086 member, or if this member is a base class, continue to
7087 the next member and avoid setting the FMEM->NEXT pointer
7088 to point to it. */
7089 if (base_p)
7090 continue;
7094 if (field_nonempty_p (fld))
7096 /* Remember the first non-static data member. */
7097 if (!fmem->first)
7098 fmem->first = fld;
7100 /* Remember the first non-static data member after the flexible
7101 array member, if one has been found, or the zero-length array
7102 if it has been found. */
7103 if (fmem->array && !fmem->after[bool (pun)])
7104 fmem->after[bool (pun)] = fld;
7107 /* Skip non-arrays. */
7108 if (TREE_CODE (fldtype) != ARRAY_TYPE)
7109 continue;
7111 /* Determine the upper bound of the array if it has one. */
7112 if (TYPE_DOMAIN (fldtype))
7114 if (fmem->array)
7116 /* Make a record of the zero-length array if either one
7117 such field or a flexible array member has been seen to
7118 handle the pathological and unlikely case of multiple
7119 such members. */
7120 if (!fmem->after[bool (pun)])
7121 fmem->after[bool (pun)] = fld;
7123 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7125 /* Remember the first zero-length array unless a flexible array
7126 member has already been seen. */
7127 fmem->array = fld;
7128 fmem->enclosing = pstr;
7131 else
7133 /* Flexible array members have no upper bound. */
7134 if (fmem->array)
7136 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7138 /* Replace the zero-length array if it's been stored and
7139 reset the after pointer. */
7140 fmem->after[bool (pun)] = NULL_TREE;
7141 fmem->array = fld;
7142 fmem->enclosing = pstr;
7144 else if (!fmem->after[bool (pun)])
7145 /* Make a record of another flexible array member. */
7146 fmem->after[bool (pun)] = fld;
7148 else
7150 fmem->array = fld;
7151 fmem->enclosing = pstr;
7157 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7158 a flexible array member (or the zero-length array extension). */
7160 static void
7161 diagnose_invalid_flexarray (const flexmems_t *fmem)
7163 if (fmem->array && fmem->enclosing)
7165 auto_diagnostic_group d;
7166 if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
7167 TYPE_DOMAIN (TREE_TYPE (fmem->array))
7168 ? G_("invalid use of %q#T with a zero-size array "
7169 "in %q#D")
7170 : G_("invalid use of %q#T with a flexible array member "
7171 "in %q#T"),
7172 DECL_CONTEXT (fmem->array),
7173 DECL_CONTEXT (fmem->enclosing)))
7174 inform (DECL_SOURCE_LOCATION (fmem->array),
7175 "array member %q#D declared here", fmem->array);
7179 /* Issue diagnostics for invalid flexible array members or zero-length
7180 arrays that are not the last elements of the containing class or its
7181 base classes or that are its sole members. */
7183 static void
7184 diagnose_flexarrays (tree t, const flexmems_t *fmem)
7186 if (!fmem->array)
7187 return;
7189 if (fmem->first && !fmem->after[0])
7191 diagnose_invalid_flexarray (fmem);
7192 return;
7195 /* Has a diagnostic been issued? */
7196 bool diagd = false;
7198 const char *msg = 0;
7200 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7202 if (fmem->after[0])
7203 msg = G_("zero-size array member %qD not at end of %q#T");
7204 else if (!fmem->first)
7205 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7207 if (msg)
7209 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7211 auto_diagnostic_group d;
7212 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7214 inform (location_of (t), "in the definition of %q#T", t);
7215 diagd = true;
7219 else
7221 if (fmem->after[0])
7222 msg = G_("flexible array member %qD not at end of %q#T");
7223 else if (!fmem->first)
7224 msg = G_("flexible array member %qD in an otherwise empty %q#T");
7226 if (msg)
7228 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7229 diagd = true;
7231 auto_diagnostic_group d;
7232 error_at (loc, msg, fmem->array, t);
7234 /* In the unlikely event that the member following the flexible
7235 array member is declared in a different class, or the member
7236 overlaps another member of a common union, point to it.
7237 Otherwise it should be obvious. */
7238 if (fmem->after[0]
7239 && ((DECL_CONTEXT (fmem->after[0])
7240 != DECL_CONTEXT (fmem->array))))
7242 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7243 "next member %q#D declared here",
7244 fmem->after[0]);
7245 inform (location_of (t), "in the definition of %q#T", t);
7250 if (!diagd && fmem->array && fmem->enclosing)
7251 diagnose_invalid_flexarray (fmem);
7255 /* Recursively check to make sure that any flexible array or zero-length
7256 array members of class T or its bases are valid (i.e., not the sole
7257 non-static data member of T and, if one exists, that it is the last
7258 non-static data member of T and its base classes. FMEM is expected
7259 to be initially null and is used internally by recursive calls to
7260 the function. Issue the appropriate diagnostics for the array member
7261 that fails the checks. */
7263 static void
7264 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7265 bool base_p /* = false */)
7267 /* Initialize the result of a search for flexible array and zero-length
7268 array members. Avoid doing any work if the most interesting FMEM data
7269 have already been populated. */
7270 flexmems_t flexmems = flexmems_t ();
7271 if (!fmem)
7272 fmem = &flexmems;
7273 else if (fmem->array && fmem->first && fmem->after[0])
7274 return;
7276 tree fam = fmem->array;
7278 /* Recursively check the primary base class first. */
7279 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7281 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7282 check_flexarrays (basetype, fmem, true);
7285 /* Recursively check the base classes. */
7286 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7287 for (int i = 0; i < nbases; ++i)
7289 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7291 /* The primary base class was already checked above. */
7292 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7293 continue;
7295 /* Virtual base classes are at the end. */
7296 if (BINFO_VIRTUAL_P (base_binfo))
7297 continue;
7299 /* Check the base class. */
7300 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7303 if (fmem == &flexmems)
7305 /* Check virtual base classes only once per derived class.
7306 I.e., this check is not performed recursively for base
7307 classes. */
7308 int i;
7309 tree base_binfo;
7310 vec<tree, va_gc> *vbases;
7311 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7312 vec_safe_iterate (vbases, i, &base_binfo); i++)
7314 /* Check the virtual base class. */
7315 tree basetype = TREE_TYPE (base_binfo);
7317 check_flexarrays (basetype, fmem, /*base_p=*/true);
7321 /* Is the type unnamed (and therefore a member of it potentially
7322 an anonymous struct or union)? */
7323 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7324 if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE)
7325 maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7327 /* Search the members of the current (possibly derived) class, skipping
7328 unnamed structs and unions since those could be anonymous. */
7329 if (fmem != &flexmems || !maybe_anon_p)
7330 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7332 if (fmem == &flexmems && !maybe_anon_p)
7334 /* Issue diagnostics for invalid flexible and zero-length array
7335 members found in base classes or among the members of the current
7336 class. Ignore anonymous structs and unions whose members are
7337 considered to be members of the enclosing class and thus will
7338 be diagnosed when checking it. */
7339 diagnose_flexarrays (t, fmem);
7343 /* Perform processing required when the definition of T (a class type)
7344 is complete. Diagnose invalid definitions of flexible array members
7345 and zero-size arrays. */
7347 void
7348 finish_struct_1 (tree t)
7350 tree x;
7351 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7352 tree virtuals = NULL_TREE;
7354 if (COMPLETE_TYPE_P (t))
7356 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7357 error ("redefinition of %q#T", t);
7358 popclass ();
7359 return;
7362 /* If this type was previously laid out as a forward reference,
7363 make sure we lay it out again. */
7364 TYPE_SIZE (t) = NULL_TREE;
7365 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7367 /* Make assumptions about the class; we'll reset the flags if
7368 necessary. */
7369 CLASSTYPE_EMPTY_P (t) = 1;
7370 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7371 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7372 CLASSTYPE_LITERAL_P (t) = true;
7374 /* Do end-of-class semantic processing: checking the validity of the
7375 bases and members and add implicitly generated methods. */
7376 check_bases_and_members (t);
7378 /* Find the key method. */
7379 if (TYPE_CONTAINS_VPTR_P (t))
7381 /* The Itanium C++ ABI permits the key method to be chosen when
7382 the class is defined -- even though the key method so
7383 selected may later turn out to be an inline function. On
7384 some systems (such as ARM Symbian OS) the key method cannot
7385 be determined until the end of the translation unit. On such
7386 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7387 will cause the class to be added to KEYED_CLASSES. Then, in
7388 finish_file we will determine the key method. */
7389 if (targetm.cxx.key_method_may_be_inline ())
7390 determine_key_method (t);
7392 /* If a polymorphic class has no key method, we may emit the vtable
7393 in every translation unit where the class definition appears. If
7394 we're devirtualizing, we can look into the vtable even if we
7395 aren't emitting it. */
7396 if (!CLASSTYPE_KEY_METHOD (t))
7397 vec_safe_push (keyed_classes, t);
7400 /* Layout the class itself. */
7401 layout_class_type (t, &virtuals);
7402 /* COMPLETE_TYPE_P is now true. */
7404 set_class_bindings (t);
7406 /* With the layout complete, check for flexible array members and
7407 zero-length arrays that might overlap other members in the final
7408 layout. */
7409 check_flexarrays (t);
7411 virtuals = modify_all_vtables (t, nreverse (virtuals));
7413 /* If necessary, create the primary vtable for this class. */
7414 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7416 /* We must enter these virtuals into the table. */
7417 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7418 build_primary_vtable (NULL_TREE, t);
7419 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7420 /* Here we know enough to change the type of our virtual
7421 function table, but we will wait until later this function. */
7422 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7424 /* If we're warning about ABI tags, check the types of the new
7425 virtual functions. */
7426 if (warn_abi_tag)
7427 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7428 check_abi_tags (t, TREE_VALUE (v));
7431 if (TYPE_CONTAINS_VPTR_P (t))
7433 int vindex;
7434 tree fn;
7436 if (BINFO_VTABLE (TYPE_BINFO (t)))
7437 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7438 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7439 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7441 /* Add entries for virtual functions introduced by this class. */
7442 BINFO_VIRTUALS (TYPE_BINFO (t))
7443 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7445 /* Set DECL_VINDEX for all functions declared in this class. */
7446 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7448 fn = TREE_CHAIN (fn),
7449 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7450 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7452 tree fndecl = BV_FN (fn);
7454 if (DECL_THUNK_P (fndecl))
7455 /* A thunk. We should never be calling this entry directly
7456 from this vtable -- we'd use the entry for the non
7457 thunk base function. */
7458 DECL_VINDEX (fndecl) = NULL_TREE;
7459 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7460 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7464 finish_struct_bits (t);
7466 set_method_tm_attributes (t);
7467 if (flag_openmp || flag_openmp_simd)
7468 finish_omp_declare_simd_methods (t);
7470 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7471 for any static member objects of the type we're working on. */
7472 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7473 if (DECL_DECLARES_FUNCTION_P (x))
7475 /* Synthesize constexpr defaulted comparisons. */
7476 if (!DECL_ARTIFICIAL (x)
7477 && DECL_DEFAULTED_IN_CLASS_P (x)
7478 && special_function_p (x) == sfk_comparison)
7479 defaulted_late_check (x);
7480 DECL_IN_AGGR_P (x) = false;
7482 else if (VAR_P (x) && TREE_STATIC (x)
7483 && TREE_TYPE (x) != error_mark_node
7484 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7485 SET_DECL_MODE (x, TYPE_MODE (t));
7487 /* Complain if one of the field types requires lower visibility. */
7488 constrain_class_visibility (t);
7490 /* Make the rtl for any new vtables we have created, and unmark
7491 the base types we marked. */
7492 finish_vtbls (t);
7494 /* Build the VTT for T. */
7495 build_vtt (t);
7497 if (warn_nonvdtor
7498 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7499 && !CLASSTYPE_FINAL (t))
7500 warning (OPT_Wnon_virtual_dtor,
7501 "%q#T has virtual functions and accessible"
7502 " non-virtual destructor", t);
7504 complete_vars (t);
7506 if (warn_overloaded_virtual)
7507 warn_hidden (t);
7509 /* Class layout, assignment of virtual table slots, etc., is now
7510 complete. Give the back end a chance to tweak the visibility of
7511 the class or perform any other required target modifications. */
7512 targetm.cxx.adjust_class_at_definition (t);
7514 maybe_suppress_debug_info (t);
7516 if (flag_vtable_verify)
7517 vtv_save_class_info (t);
7519 dump_class_hierarchy (t);
7521 /* Finish debugging output for this type. */
7522 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7524 if (TYPE_TRANSPARENT_AGGR (t))
7526 tree field = first_field (t);
7527 if (field == NULL_TREE || error_operand_p (field))
7529 error ("type transparent %q#T does not have any fields", t);
7530 TYPE_TRANSPARENT_AGGR (t) = 0;
7532 else if (DECL_ARTIFICIAL (field))
7534 if (DECL_FIELD_IS_BASE (field))
7535 error ("type transparent class %qT has base classes", t);
7536 else
7538 gcc_checking_assert (DECL_VIRTUAL_P (field));
7539 error ("type transparent class %qT has virtual functions", t);
7541 TYPE_TRANSPARENT_AGGR (t) = 0;
7543 else if (TYPE_MODE (t) != DECL_MODE (field))
7545 error ("type transparent %q#T cannot be made transparent because "
7546 "the type of the first field has a different ABI from the "
7547 "class overall", t);
7548 TYPE_TRANSPARENT_AGGR (t) = 0;
7553 /* When T was built up, the member declarations were added in reverse
7554 order. Rearrange them to declaration order. */
7556 void
7557 unreverse_member_declarations (tree t)
7559 tree next;
7560 tree prev;
7561 tree x;
7563 /* The following lists are all in reverse order. Put them in
7564 declaration order now. */
7565 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7567 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7568 order, so we can't just use nreverse. Due to stat_hack
7569 chicanery in finish_member_declaration. */
7570 prev = NULL_TREE;
7571 for (x = TYPE_FIELDS (t);
7572 x && TREE_CODE (x) != TYPE_DECL;
7573 x = next)
7575 next = DECL_CHAIN (x);
7576 DECL_CHAIN (x) = prev;
7577 prev = x;
7580 if (prev)
7582 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7583 TYPE_FIELDS (t) = prev;
7587 tree
7588 finish_struct (tree t, tree attributes)
7590 location_t saved_loc = input_location;
7592 /* Now that we've got all the field declarations, reverse everything
7593 as necessary. */
7594 unreverse_member_declarations (t);
7596 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7597 fixup_attribute_variants (t);
7599 /* Nadger the current location so that diagnostics point to the start of
7600 the struct, not the end. */
7601 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7603 if (processing_template_decl)
7605 tree x;
7607 /* We need to add the target functions of USING_DECLS, so that
7608 they can be found when the using declaration is not
7609 instantiated yet. */
7610 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7611 if (TREE_CODE (x) == USING_DECL)
7613 tree fn = strip_using_decl (x);
7614 if (OVL_P (fn))
7615 for (lkp_iterator iter (fn); iter; ++iter)
7616 add_method (t, *iter, true);
7618 else if (DECL_DECLARES_FUNCTION_P (x))
7620 DECL_IN_AGGR_P (x) = false;
7621 if (DECL_VIRTUAL_P (x))
7622 CLASSTYPE_NON_AGGREGATE (t) = true;
7624 else if (TREE_CODE (x) == FIELD_DECL)
7626 if (TREE_PROTECTED (x) || TREE_PRIVATE (x))
7627 CLASSTYPE_NON_AGGREGATE (t) = true;
7630 /* Also add a USING_DECL for operator=. We know there'll be (at
7631 least) one, but we don't know the signature(s). We want name
7632 lookup not to fail or recurse into bases. This isn't added
7633 to the template decl list so we drop this at instantiation
7634 time. */
7635 tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
7636 NULL_TREE);
7637 DECL_CONTEXT (ass_op) = t;
7638 USING_DECL_SCOPE (ass_op) = t;
7639 DECL_DEPENDENT_P (ass_op) = true;
7640 DECL_ARTIFICIAL (ass_op) = true;
7641 DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
7642 TYPE_FIELDS (t) = ass_op;
7644 TYPE_SIZE (t) = bitsize_zero_node;
7645 TYPE_SIZE_UNIT (t) = size_zero_node;
7646 /* COMPLETE_TYPE_P is now true. */
7648 set_class_bindings (t);
7650 /* We need to emit an error message if this type was used as a parameter
7651 and it is an abstract type, even if it is a template. We construct
7652 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7653 account and we call complete_vars with this type, which will check
7654 the PARM_DECLS. Note that while the type is being defined,
7655 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7656 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7657 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7658 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7659 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7660 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7661 complete_vars (t);
7663 /* Remember current #pragma pack value. */
7664 TYPE_PRECISION (t) = maximum_field_alignment;
7666 if (cxx_dialect < cxx20)
7668 if (!CLASSTYPE_NON_AGGREGATE (t)
7669 && type_has_user_provided_or_explicit_constructor (t))
7670 CLASSTYPE_NON_AGGREGATE (t) = 1;
7672 else if (TYPE_HAS_USER_CONSTRUCTOR (t))
7673 CLASSTYPE_NON_AGGREGATE (t) = 1;
7675 /* Fix up any variants we've already built. */
7676 fixup_type_variants (t);
7678 else
7679 finish_struct_1 (t);
7680 /* COMPLETE_TYPE_P is now true. */
7682 maybe_warn_about_overly_private_class (t);
7684 if (is_std_init_list (t))
7686 /* People keep complaining that the compiler crashes on an invalid
7687 definition of initializer_list, so I guess we should explicitly
7688 reject it. What the compiler internals care about is that it's a
7689 template and has a pointer field followed by size_type field. */
7690 bool ok = false;
7691 if (processing_template_decl)
7693 tree f = next_initializable_field (TYPE_FIELDS (t));
7694 if (f && TYPE_PTR_P (TREE_TYPE (f)))
7696 f = next_initializable_field (DECL_CHAIN (f));
7697 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7698 ok = true;
7701 if (!ok)
7702 fatal_error (input_location, "definition of %qD does not match "
7703 "%<#include <initializer_list>%>", TYPE_NAME (t));
7706 input_location = saved_loc;
7708 TYPE_BEING_DEFINED (t) = 0;
7710 if (current_class_type)
7711 popclass ();
7712 else
7713 error ("trying to finish struct, but kicked out due to previous parse errors");
7715 if (flag_openmp)
7716 for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl))
7717 if (TREE_CODE (decl) == FUNCTION_DECL
7718 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
7719 if (tree attr = lookup_attribute ("omp declare variant base",
7720 DECL_ATTRIBUTES (decl)))
7721 omp_declare_variant_finalize (decl, attr);
7723 if (processing_template_decl && at_function_scope_p ()
7724 /* Lambdas are defined by the LAMBDA_EXPR. */
7725 && !LAMBDA_TYPE_P (t))
7726 add_stmt (build_min (TAG_DEFN, t));
7728 return t;
7731 /* Hash table to avoid endless recursion when handling references. */
7732 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7734 /* Return the dynamic type of INSTANCE, if known.
7735 Used to determine whether the virtual function table is needed
7736 or not.
7738 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7739 of our knowledge of its type. *NONNULL should be initialized
7740 before this function is called. */
7742 static tree
7743 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7745 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7747 switch (TREE_CODE (instance))
7749 case INDIRECT_REF:
7750 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
7751 return NULL_TREE;
7752 else
7753 return RECUR (TREE_OPERAND (instance, 0));
7755 case CALL_EXPR:
7756 /* This is a call to a constructor, hence it's never zero. */
7757 if (CALL_EXPR_FN (instance)
7758 && TREE_HAS_CONSTRUCTOR (instance))
7760 if (nonnull)
7761 *nonnull = 1;
7762 return TREE_TYPE (instance);
7764 return NULL_TREE;
7766 case SAVE_EXPR:
7767 /* This is a call to a constructor, hence it's never zero. */
7768 if (TREE_HAS_CONSTRUCTOR (instance))
7770 if (nonnull)
7771 *nonnull = 1;
7772 return TREE_TYPE (instance);
7774 return RECUR (TREE_OPERAND (instance, 0));
7776 case POINTER_PLUS_EXPR:
7777 case PLUS_EXPR:
7778 case MINUS_EXPR:
7779 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7780 return RECUR (TREE_OPERAND (instance, 0));
7781 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7782 /* Propagate nonnull. */
7783 return RECUR (TREE_OPERAND (instance, 0));
7785 return NULL_TREE;
7787 CASE_CONVERT:
7788 return RECUR (TREE_OPERAND (instance, 0));
7790 case ADDR_EXPR:
7791 instance = TREE_OPERAND (instance, 0);
7792 if (nonnull)
7794 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7795 with a real object -- given &p->f, p can still be null. */
7796 tree t = get_base_address (instance);
7797 /* ??? Probably should check DECL_WEAK here. */
7798 if (t && DECL_P (t))
7799 *nonnull = 1;
7801 return RECUR (instance);
7803 case COMPONENT_REF:
7804 /* If this component is really a base class reference, then the field
7805 itself isn't definitive. */
7806 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7807 return RECUR (TREE_OPERAND (instance, 0));
7808 return RECUR (TREE_OPERAND (instance, 1));
7810 case VAR_DECL:
7811 case FIELD_DECL:
7812 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7813 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7815 if (nonnull)
7816 *nonnull = 1;
7817 return TREE_TYPE (TREE_TYPE (instance));
7819 /* fall through. */
7820 case TARGET_EXPR:
7821 case PARM_DECL:
7822 case RESULT_DECL:
7823 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7825 if (nonnull)
7826 *nonnull = 1;
7827 return TREE_TYPE (instance);
7829 else if (instance == current_class_ptr)
7831 if (nonnull)
7832 *nonnull = 1;
7834 /* if we're in a ctor or dtor, we know our type. If
7835 current_class_ptr is set but we aren't in a function, we're in
7836 an NSDMI (and therefore a constructor). */
7837 if (current_scope () != current_function_decl
7838 || (DECL_LANG_SPECIFIC (current_function_decl)
7839 && (DECL_CONSTRUCTOR_P (current_function_decl)
7840 || DECL_DESTRUCTOR_P (current_function_decl))))
7842 if (cdtorp)
7843 *cdtorp = 1;
7844 return TREE_TYPE (TREE_TYPE (instance));
7847 else if (TYPE_REF_P (TREE_TYPE (instance)))
7849 /* We only need one hash table because it is always left empty. */
7850 if (!fixed_type_or_null_ref_ht)
7851 fixed_type_or_null_ref_ht
7852 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7854 /* Reference variables should be references to objects. */
7855 if (nonnull)
7856 *nonnull = 1;
7858 /* Enter the INSTANCE in a table to prevent recursion; a
7859 variable's initializer may refer to the variable
7860 itself. */
7861 if (VAR_P (instance)
7862 && DECL_INITIAL (instance)
7863 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7864 && !fixed_type_or_null_ref_ht->find (instance))
7866 tree type;
7867 tree_node **slot;
7869 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7870 *slot = instance;
7871 type = RECUR (DECL_INITIAL (instance));
7872 fixed_type_or_null_ref_ht->remove_elt (instance);
7874 return type;
7877 return NULL_TREE;
7879 case VIEW_CONVERT_EXPR:
7880 if (location_wrapper_p (instance))
7881 return RECUR (TREE_OPERAND (instance, 0));
7882 else
7883 /* TODO: Recursion may be correct for some non-location-wrapper
7884 uses of VIEW_CONVERT_EXPR. */
7885 return NULL_TREE;
7887 default:
7888 return NULL_TREE;
7890 #undef RECUR
7893 /* Return nonzero if the dynamic type of INSTANCE is known, and
7894 equivalent to the static type. We also handle the case where
7895 INSTANCE is really a pointer. Return negative if this is a
7896 ctor/dtor. There the dynamic type is known, but this might not be
7897 the most derived base of the original object, and hence virtual
7898 bases may not be laid out according to this type.
7900 Used to determine whether the virtual function table is needed
7901 or not.
7903 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7904 of our knowledge of its type. *NONNULL should be initialized
7905 before this function is called. */
7908 resolves_to_fixed_type_p (tree instance, int* nonnull)
7910 tree t = TREE_TYPE (instance);
7911 int cdtorp = 0;
7912 tree fixed;
7914 /* processing_template_decl can be false in a template if we're in
7915 instantiate_non_dependent_expr, but we still want to suppress
7916 this check. */
7917 if (in_template_function ())
7919 /* In a template we only care about the type of the result. */
7920 if (nonnull)
7921 *nonnull = true;
7922 return true;
7925 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7926 if (INDIRECT_TYPE_P (t))
7927 t = TREE_TYPE (t);
7928 if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t))
7929 return 1;
7930 if (fixed == NULL_TREE)
7931 return 0;
7932 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7933 return 0;
7934 return cdtorp ? -1 : 1;
7938 void
7939 init_class_processing (void)
7941 current_class_depth = 0;
7942 current_class_stack_size = 10;
7943 current_class_stack
7944 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7945 sizeof_biggest_empty_class = size_zero_node;
7947 ridpointers[(int) RID_PUBLIC] = access_public_node;
7948 ridpointers[(int) RID_PRIVATE] = access_private_node;
7949 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7952 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7954 static void
7955 restore_class_cache (void)
7957 tree type;
7959 /* We are re-entering the same class we just left, so we don't
7960 have to search the whole inheritance matrix to find all the
7961 decls to bind again. Instead, we install the cached
7962 class_shadowed list and walk through it binding names. */
7963 push_binding_level (previous_class_level);
7964 class_binding_level = previous_class_level;
7965 /* Restore IDENTIFIER_TYPE_VALUE. */
7966 for (type = class_binding_level->type_shadowed;
7967 type;
7968 type = TREE_CHAIN (type))
7969 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7972 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7973 appropriate for TYPE.
7975 So that we may avoid calls to lookup_name, we cache the _TYPE
7976 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7978 For multiple inheritance, we perform a two-pass depth-first search
7979 of the type lattice. */
7981 void
7982 pushclass (tree type)
7984 class_stack_node_t csn;
7986 type = TYPE_MAIN_VARIANT (type);
7988 /* Make sure there is enough room for the new entry on the stack. */
7989 if (current_class_depth + 1 >= current_class_stack_size)
7991 current_class_stack_size *= 2;
7992 current_class_stack
7993 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7994 current_class_stack_size);
7997 /* Insert a new entry on the class stack. */
7998 csn = current_class_stack + current_class_depth;
7999 csn->name = current_class_name;
8000 csn->type = current_class_type;
8001 csn->access = current_access_specifier;
8002 csn->names_used = 0;
8003 csn->hidden = 0;
8004 current_class_depth++;
8006 /* Now set up the new type. */
8007 current_class_name = TYPE_NAME (type);
8008 if (TREE_CODE (current_class_name) == TYPE_DECL)
8009 current_class_name = DECL_NAME (current_class_name);
8010 current_class_type = type;
8012 /* By default, things in classes are private, while things in
8013 structures or unions are public. */
8014 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
8015 ? access_private_node
8016 : access_public_node);
8018 if (previous_class_level
8019 && type != previous_class_level->this_entity
8020 && current_class_depth == 1)
8022 /* Forcibly remove any old class remnants. */
8023 invalidate_class_lookup_cache ();
8026 if (!previous_class_level
8027 || type != previous_class_level->this_entity
8028 || current_class_depth > 1)
8029 pushlevel_class ();
8030 else
8031 restore_class_cache ();
8034 /* Get out of the current class scope. If we were in a class scope
8035 previously, that is the one popped to. */
8037 void
8038 popclass (void)
8040 poplevel_class ();
8042 current_class_depth--;
8043 current_class_name = current_class_stack[current_class_depth].name;
8044 current_class_type = current_class_stack[current_class_depth].type;
8045 current_access_specifier = current_class_stack[current_class_depth].access;
8046 if (current_class_stack[current_class_depth].names_used)
8047 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8050 /* Mark the top of the class stack as hidden. */
8052 void
8053 push_class_stack (void)
8055 if (current_class_depth)
8056 ++current_class_stack[current_class_depth - 1].hidden;
8059 /* Mark the top of the class stack as un-hidden. */
8061 void
8062 pop_class_stack (void)
8064 if (current_class_depth)
8065 --current_class_stack[current_class_depth - 1].hidden;
8068 /* If the class type currently being defined is either T or
8069 a nested type of T, returns the type from the current_class_stack,
8070 which might be equivalent to but not equal to T in case of
8071 constrained partial specializations. */
8073 tree
8074 currently_open_class (tree t)
8076 int i;
8078 if (!CLASS_TYPE_P (t))
8079 return NULL_TREE;
8081 t = TYPE_MAIN_VARIANT (t);
8083 /* We start looking from 1 because entry 0 is from global scope,
8084 and has no type. */
8085 for (i = current_class_depth; i > 0; --i)
8087 tree c;
8088 if (i == current_class_depth)
8089 c = current_class_type;
8090 else
8092 if (current_class_stack[i].hidden)
8093 break;
8094 c = current_class_stack[i].type;
8096 if (!c)
8097 continue;
8098 if (same_type_p (c, t))
8099 return c;
8101 return NULL_TREE;
8104 /* If either current_class_type or one of its enclosing classes are derived
8105 from T, return the appropriate type. Used to determine how we found
8106 something via unqualified lookup. */
8108 tree
8109 currently_open_derived_class (tree t)
8111 int i;
8113 /* The bases of a dependent type are unknown. */
8114 if (dependent_type_p (t))
8115 return NULL_TREE;
8117 if (!current_class_type)
8118 return NULL_TREE;
8120 if (DERIVED_FROM_P (t, current_class_type))
8121 return current_class_type;
8123 for (i = current_class_depth - 1; i > 0; --i)
8125 if (current_class_stack[i].hidden)
8126 break;
8127 if (DERIVED_FROM_P (t, current_class_stack[i].type))
8128 return current_class_stack[i].type;
8131 return NULL_TREE;
8134 /* Return the outermost enclosing class type that is still open, or
8135 NULL_TREE. */
8137 tree
8138 outermost_open_class (void)
8140 if (!current_class_type)
8141 return NULL_TREE;
8142 tree r = NULL_TREE;
8143 if (TYPE_BEING_DEFINED (current_class_type))
8144 r = current_class_type;
8145 for (int i = current_class_depth - 1; i > 0; --i)
8147 if (current_class_stack[i].hidden)
8148 break;
8149 tree t = current_class_stack[i].type;
8150 if (!TYPE_BEING_DEFINED (t))
8151 break;
8152 r = t;
8154 return r;
8157 /* Returns the innermost class type which is not a lambda closure type. */
8159 tree
8160 current_nonlambda_class_type (void)
8162 tree type = current_class_type;
8163 while (type && LAMBDA_TYPE_P (type))
8164 type = decl_type_context (TYPE_NAME (type));
8165 return type;
8168 /* When entering a class scope, all enclosing class scopes' names with
8169 static meaning (static variables, static functions, types and
8170 enumerators) have to be visible. This recursive function calls
8171 pushclass for all enclosing class contexts until global or a local
8172 scope is reached. TYPE is the enclosed class. */
8174 void
8175 push_nested_class (tree type)
8177 /* A namespace might be passed in error cases, like A::B:C. */
8178 if (type == NULL_TREE
8179 || !CLASS_TYPE_P (type))
8180 return;
8182 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8184 pushclass (type);
8187 /* Undoes a push_nested_class call. */
8189 void
8190 pop_nested_class (void)
8192 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8194 popclass ();
8195 if (context && CLASS_TYPE_P (context))
8196 pop_nested_class ();
8199 /* Returns the number of extern "LANG" blocks we are nested within. */
8202 current_lang_depth (void)
8204 return vec_safe_length (current_lang_base);
8207 /* Set global variables CURRENT_LANG_NAME to appropriate value
8208 so that behavior of name-mangling machinery is correct. */
8210 void
8211 push_lang_context (tree name)
8213 vec_safe_push (current_lang_base, current_lang_name);
8215 if (name == lang_name_cplusplus)
8216 current_lang_name = name;
8217 else if (name == lang_name_c)
8218 current_lang_name = name;
8219 else
8220 error ("language string %<\"%E\"%> not recognized", name);
8223 /* Get out of the current language scope. */
8225 void
8226 pop_lang_context (void)
8228 current_lang_name = current_lang_base->pop ();
8231 /* Type instantiation routines. */
8233 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8234 matches the TARGET_TYPE. If there is no satisfactory match, return
8235 error_mark_node, and issue an error & warning messages under
8236 control of FLAGS. Permit pointers to member function if FLAGS
8237 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8238 a template-id, and EXPLICIT_TARGS are the explicitly provided
8239 template arguments.
8241 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8242 is the base path used to reference those member functions. If
8243 the address is resolved to a member function, access checks will be
8244 performed and errors issued if appropriate. */
8246 static tree
8247 resolve_address_of_overloaded_function (tree target_type,
8248 tree overload,
8249 tsubst_flags_t complain,
8250 bool template_only,
8251 tree explicit_targs,
8252 tree access_path)
8254 /* Here's what the standard says:
8256 [over.over]
8258 If the name is a function template, template argument deduction
8259 is done, and if the argument deduction succeeds, the deduced
8260 arguments are used to generate a single template function, which
8261 is added to the set of overloaded functions considered.
8263 Non-member functions and static member functions match targets of
8264 type "pointer-to-function" or "reference-to-function." Nonstatic
8265 member functions match targets of type "pointer-to-member
8266 function;" the function type of the pointer to member is used to
8267 select the member function from the set of overloaded member
8268 functions. If a non-static member function is selected, the
8269 reference to the overloaded function name is required to have the
8270 form of a pointer to member as described in 5.3.1.
8272 If more than one function is selected, any template functions in
8273 the set are eliminated if the set also contains a non-template
8274 function, and any given template function is eliminated if the
8275 set contains a second template function that is more specialized
8276 than the first according to the partial ordering rules 14.5.5.2.
8277 After such eliminations, if any, there shall remain exactly one
8278 selected function. */
8280 int is_ptrmem = 0;
8281 /* We store the matches in a TREE_LIST rooted here. The functions
8282 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8283 interoperability with most_specialized_instantiation. */
8284 tree matches = NULL_TREE;
8285 tree fn;
8286 tree target_fn_type;
8288 /* By the time we get here, we should be seeing only real
8289 pointer-to-member types, not the internal POINTER_TYPE to
8290 METHOD_TYPE representation. */
8291 gcc_assert (!TYPE_PTR_P (target_type)
8292 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8294 gcc_assert (is_overloaded_fn (overload));
8296 /* Check that the TARGET_TYPE is reasonable. */
8297 if (TYPE_PTRFN_P (target_type)
8298 || TYPE_REFFN_P (target_type))
8299 /* This is OK. */;
8300 else if (TYPE_PTRMEMFUNC_P (target_type))
8301 /* This is OK, too. */
8302 is_ptrmem = 1;
8303 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8304 /* This is OK, too. This comes from a conversion to reference
8305 type. */
8306 target_type = build_reference_type (target_type);
8307 else
8309 if (complain & tf_error)
8310 error ("cannot resolve overloaded function %qD based on"
8311 " conversion to type %qT",
8312 OVL_NAME (overload), target_type);
8313 return error_mark_node;
8316 /* Non-member functions and static member functions match targets of type
8317 "pointer-to-function" or "reference-to-function." Nonstatic member
8318 functions match targets of type "pointer-to-member-function;" the
8319 function type of the pointer to member is used to select the member
8320 function from the set of overloaded member functions.
8322 So figure out the FUNCTION_TYPE that we want to match against. */
8323 target_fn_type = static_fn_type (target_type);
8325 /* If we can find a non-template function that matches, we can just
8326 use it. There's no point in generating template instantiations
8327 if we're just going to throw them out anyhow. But, of course, we
8328 can only do this when we don't *need* a template function. */
8329 if (!template_only)
8330 for (lkp_iterator iter (overload); iter; ++iter)
8332 tree fn = *iter;
8334 if (TREE_CODE (fn) == TEMPLATE_DECL)
8335 /* We're not looking for templates just yet. */
8336 continue;
8338 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8339 /* We're looking for a non-static member, and this isn't
8340 one, or vice versa. */
8341 continue;
8343 /* Constraints must be satisfied. This is done before
8344 return type deduction since that instantiates the
8345 function. */
8346 if (!constraints_satisfied_p (fn))
8347 continue;
8349 if (undeduced_auto_decl (fn))
8351 /* Force instantiation to do return type deduction. */
8352 maybe_instantiate_decl (fn);
8353 require_deduced_type (fn);
8356 /* In C++17 we need the noexcept-qualifier to compare types. */
8357 if (flag_noexcept_type
8358 && !maybe_instantiate_noexcept (fn, complain))
8359 continue;
8361 /* See if there's a match. */
8362 tree fntype = static_fn_type (fn);
8363 if (same_type_p (target_fn_type, fntype)
8364 || fnptr_conv_p (target_fn_type, fntype))
8365 matches = tree_cons (fn, NULL_TREE, matches);
8368 /* Now, if we've already got a match (or matches), there's no need
8369 to proceed to the template functions. But, if we don't have a
8370 match we need to look at them, too. */
8371 if (!matches)
8373 tree target_arg_types;
8374 tree target_ret_type;
8375 tree *args;
8376 unsigned int nargs, ia;
8377 tree arg;
8379 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8380 target_ret_type = TREE_TYPE (target_fn_type);
8382 nargs = list_length (target_arg_types);
8383 args = XALLOCAVEC (tree, nargs);
8384 for (arg = target_arg_types, ia = 0;
8385 arg != NULL_TREE;
8386 arg = TREE_CHAIN (arg), ++ia)
8387 args[ia] = TREE_VALUE (arg);
8388 nargs = ia;
8390 for (lkp_iterator iter (overload); iter; ++iter)
8392 tree fn = *iter;
8393 tree instantiation;
8394 tree targs;
8396 if (TREE_CODE (fn) != TEMPLATE_DECL)
8397 /* We're only looking for templates. */
8398 continue;
8400 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8401 != is_ptrmem)
8402 /* We're not looking for a non-static member, and this is
8403 one, or vice versa. */
8404 continue;
8406 tree ret = target_ret_type;
8408 /* If the template has a deduced return type, don't expose it to
8409 template argument deduction. */
8410 if (undeduced_auto_decl (fn))
8411 ret = NULL_TREE;
8413 /* Try to do argument deduction. */
8414 targs = make_tree_vec (DECL_NTPARMS (fn));
8415 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8416 nargs, ret,
8417 DEDUCE_EXACT, LOOKUP_NORMAL,
8418 NULL, false, false);
8419 if (instantiation == error_mark_node)
8420 /* Instantiation failed. */
8421 continue;
8423 /* Constraints must be satisfied. This is done before
8424 return type deduction since that instantiates the
8425 function. */
8426 if (flag_concepts && !constraints_satisfied_p (instantiation))
8427 continue;
8429 /* And now force instantiation to do return type deduction. */
8430 if (undeduced_auto_decl (instantiation))
8432 ++function_depth;
8433 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8434 --function_depth;
8436 require_deduced_type (instantiation);
8439 /* In C++17 we need the noexcept-qualifier to compare types. */
8440 if (flag_noexcept_type)
8441 maybe_instantiate_noexcept (instantiation, complain);
8443 /* See if there's a match. */
8444 tree fntype = static_fn_type (instantiation);
8445 if (same_type_p (target_fn_type, fntype)
8446 || fnptr_conv_p (target_fn_type, fntype))
8447 matches = tree_cons (instantiation, fn, matches);
8450 /* Now, remove all but the most specialized of the matches. */
8451 if (matches)
8453 tree match = most_specialized_instantiation (matches);
8455 if (match != error_mark_node)
8456 matches = tree_cons (TREE_PURPOSE (match),
8457 NULL_TREE,
8458 NULL_TREE);
8462 /* Now we should have exactly one function in MATCHES. */
8463 if (matches == NULL_TREE)
8465 /* There were *no* matches. */
8466 if (complain & tf_error)
8468 error ("no matches converting function %qD to type %q#T",
8469 OVL_NAME (overload), target_type);
8471 print_candidates (overload);
8473 return error_mark_node;
8475 else if (TREE_CHAIN (matches))
8477 /* There were too many matches. First check if they're all
8478 the same function. */
8479 tree match = NULL_TREE;
8481 fn = TREE_PURPOSE (matches);
8483 /* For multi-versioned functions, more than one match is just fine and
8484 decls_match will return false as they are different. */
8485 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8486 if (!decls_match (fn, TREE_PURPOSE (match))
8487 && !targetm.target_option.function_versions
8488 (fn, TREE_PURPOSE (match)))
8489 break;
8491 if (match)
8493 if (complain & tf_error)
8495 error ("converting overloaded function %qD to type %q#T is ambiguous",
8496 OVL_NAME (overload), target_type);
8498 /* Since print_candidates expects the functions in the
8499 TREE_VALUE slot, we flip them here. */
8500 for (match = matches; match; match = TREE_CHAIN (match))
8501 TREE_VALUE (match) = TREE_PURPOSE (match);
8503 print_candidates (matches);
8506 return error_mark_node;
8510 /* Good, exactly one match. Now, convert it to the correct type. */
8511 fn = TREE_PURPOSE (matches);
8513 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8514 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8516 static int explained;
8518 if (!(complain & tf_error))
8519 return error_mark_node;
8521 auto_diagnostic_group d;
8522 if (permerror (input_location, "assuming pointer to member %qD", fn)
8523 && !explained)
8525 inform (input_location, "(a pointer to member can only be "
8526 "formed with %<&%E%>)", fn);
8527 explained = 1;
8531 /* If a pointer to a function that is multi-versioned is requested, the
8532 pointer to the dispatcher function is returned instead. This works
8533 well because indirectly calling the function will dispatch the right
8534 function version at run-time. */
8535 if (DECL_FUNCTION_VERSIONED (fn))
8537 fn = get_function_version_dispatcher (fn);
8538 if (fn == NULL)
8539 return error_mark_node;
8540 /* Mark all the versions corresponding to the dispatcher as used. */
8541 if (!(complain & tf_conv))
8542 mark_versions_used (fn);
8545 /* If we're doing overload resolution purely for the purpose of
8546 determining conversion sequences, we should not consider the
8547 function used. If this conversion sequence is selected, the
8548 function will be marked as used at this point. */
8549 if (!(complain & tf_conv))
8551 /* Make =delete work with SFINAE. */
8552 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8553 return error_mark_node;
8554 if (!mark_used (fn, complain) && !(complain & tf_error))
8555 return error_mark_node;
8558 /* We could not check access to member functions when this
8559 expression was originally created since we did not know at that
8560 time to which function the expression referred. */
8561 if (DECL_FUNCTION_MEMBER_P (fn))
8563 gcc_assert (access_path);
8564 perform_or_defer_access_check (access_path, fn, fn, complain);
8567 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8568 return cp_build_addr_expr (fn, complain);
8569 else
8571 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8572 will mark the function as addressed, but here we must do it
8573 explicitly. */
8574 cxx_mark_addressable (fn);
8576 return fn;
8580 /* This function will instantiate the type of the expression given in
8581 RHS to match the type of LHSTYPE. If errors exist, then return
8582 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8583 we complain on errors. If we are not complaining, never modify rhs,
8584 as overload resolution wants to try many possible instantiations, in
8585 the hope that at least one will work.
8587 For non-recursive calls, LHSTYPE should be a function, pointer to
8588 function, or a pointer to member function. */
8590 tree
8591 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8593 tsubst_flags_t complain_in = complain;
8594 tree access_path = NULL_TREE;
8596 complain &= ~tf_ptrmem_ok;
8598 if (lhstype == unknown_type_node)
8600 if (complain & tf_error)
8601 error ("not enough type information");
8602 return error_mark_node;
8605 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8607 tree fntype = non_reference (lhstype);
8608 if (same_type_p (fntype, TREE_TYPE (rhs)))
8609 return rhs;
8610 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8611 return rhs;
8612 if (flag_ms_extensions
8613 && TYPE_PTRMEMFUNC_P (fntype)
8614 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8615 /* Microsoft allows `A::f' to be resolved to a
8616 pointer-to-member. */
8618 else
8620 if (complain & tf_error)
8621 error ("cannot convert %qE from type %qT to type %qT",
8622 rhs, TREE_TYPE (rhs), fntype);
8623 return error_mark_node;
8627 /* If we instantiate a template, and it is a A ?: C expression
8628 with omitted B, look through the SAVE_EXPR. */
8629 if (TREE_CODE (rhs) == SAVE_EXPR)
8630 rhs = TREE_OPERAND (rhs, 0);
8632 if (BASELINK_P (rhs))
8634 access_path = BASELINK_ACCESS_BINFO (rhs);
8635 rhs = BASELINK_FUNCTIONS (rhs);
8638 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8639 deduce any type information. */
8640 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8642 if (complain & tf_error)
8643 error ("not enough type information");
8644 return error_mark_node;
8647 /* There are only a few kinds of expressions that may have a type
8648 dependent on overload resolution. */
8649 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8650 || TREE_CODE (rhs) == COMPONENT_REF
8651 || is_overloaded_fn (rhs)
8652 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8654 /* This should really only be used when attempting to distinguish
8655 what sort of a pointer to function we have. For now, any
8656 arithmetic operation which is not supported on pointers
8657 is rejected as an error. */
8659 switch (TREE_CODE (rhs))
8661 case COMPONENT_REF:
8663 tree member = TREE_OPERAND (rhs, 1);
8665 member = instantiate_type (lhstype, member, complain);
8666 if (member != error_mark_node
8667 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8668 /* Do not lose object's side effects. */
8669 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8670 TREE_OPERAND (rhs, 0), member);
8671 return member;
8674 case OFFSET_REF:
8675 rhs = TREE_OPERAND (rhs, 1);
8676 if (BASELINK_P (rhs))
8677 return instantiate_type (lhstype, rhs, complain_in);
8679 /* This can happen if we are forming a pointer-to-member for a
8680 member template. */
8681 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8683 /* Fall through. */
8685 case TEMPLATE_ID_EXPR:
8687 tree fns = TREE_OPERAND (rhs, 0);
8688 tree args = TREE_OPERAND (rhs, 1);
8690 return
8691 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8692 /*template_only=*/true,
8693 args, access_path);
8696 case OVERLOAD:
8697 case FUNCTION_DECL:
8698 return
8699 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8700 /*template_only=*/false,
8701 /*explicit_targs=*/NULL_TREE,
8702 access_path);
8704 case ADDR_EXPR:
8706 if (PTRMEM_OK_P (rhs))
8707 complain |= tf_ptrmem_ok;
8709 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8712 case ERROR_MARK:
8713 return error_mark_node;
8715 default:
8716 gcc_unreachable ();
8718 return error_mark_node;
8721 /* Return the name of the virtual function pointer field
8722 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8723 this may have to look back through base types to find the
8724 ultimate field name. (For single inheritance, these could
8725 all be the same name. Who knows for multiple inheritance). */
8727 static tree
8728 get_vfield_name (tree type)
8730 tree binfo, base_binfo;
8732 for (binfo = TYPE_BINFO (type);
8733 BINFO_N_BASE_BINFOS (binfo);
8734 binfo = base_binfo)
8736 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8738 if (BINFO_VIRTUAL_P (base_binfo)
8739 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8740 break;
8743 type = BINFO_TYPE (binfo);
8744 tree ctor_name = constructor_name (type);
8745 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8746 + IDENTIFIER_LENGTH (ctor_name) + 2);
8747 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8748 return get_identifier (buf);
8751 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8752 according to [class]:
8753 The class-name is also inserted
8754 into the scope of the class itself. For purposes of access checking,
8755 the inserted class name is treated as if it were a public member name. */
8757 void
8758 build_self_reference (void)
8760 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8761 tree decl = build_lang_decl (TYPE_DECL, name, current_class_type);
8763 DECL_NONLOCAL (decl) = 1;
8764 DECL_CONTEXT (decl) = current_class_type;
8765 DECL_ARTIFICIAL (decl) = 1;
8766 SET_DECL_SELF_REFERENCE_P (decl);
8767 set_underlying_type (decl);
8768 set_instantiating_module (decl);
8770 if (processing_template_decl)
8771 decl = push_template_decl (decl);
8773 tree saved_cas = current_access_specifier;
8774 current_access_specifier = access_public_node;
8775 finish_member_declaration (decl);
8776 current_access_specifier = saved_cas;
8779 /* Returns 1 if TYPE contains only padding bytes. */
8782 is_empty_class (tree type)
8784 if (type == error_mark_node)
8785 return 0;
8787 if (! CLASS_TYPE_P (type))
8788 return 0;
8790 return CLASSTYPE_EMPTY_P (type);
8793 /* Returns true if TYPE contains no actual data, just various
8794 possible combinations of empty classes. If IGNORE_VPTR is true,
8795 a vptr doesn't prevent the class from being considered empty. Typically
8796 we want to ignore the vptr on assignment, and not on initialization. */
8798 bool
8799 is_really_empty_class (tree type, bool ignore_vptr)
8801 if (CLASS_TYPE_P (type))
8803 tree field;
8804 tree binfo;
8805 tree base_binfo;
8806 int i;
8808 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8809 out, but we'd like to be able to check this before then. */
8810 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8811 return true;
8813 if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
8814 return false;
8816 for (binfo = TYPE_BINFO (type), i = 0;
8817 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8818 if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
8819 return false;
8820 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8821 if (TREE_CODE (field) == FIELD_DECL
8822 && !DECL_ARTIFICIAL (field)
8823 /* An unnamed bit-field is not a data member. */
8824 && !DECL_UNNAMED_BIT_FIELD (field)
8825 && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
8826 return false;
8827 return true;
8829 else if (TREE_CODE (type) == ARRAY_TYPE)
8830 return (integer_zerop (array_type_nelts_top (type))
8831 || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
8832 return false;
8835 /* Note that NAME was looked up while the current class was being
8836 defined and that the result of that lookup was DECL. */
8838 void
8839 maybe_note_name_used_in_class (tree name, tree decl)
8841 splay_tree names_used;
8843 /* If we're not defining a class, there's nothing to do. */
8844 if (!(innermost_scope_kind() == sk_class
8845 && TYPE_BEING_DEFINED (current_class_type)
8846 && !LAMBDA_TYPE_P (current_class_type)))
8847 return;
8849 /* If there's already a binding for this NAME, then we don't have
8850 anything to worry about. */
8851 if (lookup_member (current_class_type, name,
8852 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8853 return;
8855 if (!current_class_stack[current_class_depth - 1].names_used)
8856 current_class_stack[current_class_depth - 1].names_used
8857 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8858 names_used = current_class_stack[current_class_depth - 1].names_used;
8860 splay_tree_insert (names_used,
8861 (splay_tree_key) name,
8862 (splay_tree_value) decl);
8865 /* Note that NAME was declared (as DECL) in the current class. Check
8866 to see that the declaration is valid. */
8868 void
8869 note_name_declared_in_class (tree name, tree decl)
8871 splay_tree names_used;
8872 splay_tree_node n;
8874 /* Look to see if we ever used this name. */
8875 names_used
8876 = current_class_stack[current_class_depth - 1].names_used;
8877 if (!names_used)
8878 return;
8879 /* The C language allows members to be declared with a type of the same
8880 name, and the C++ standard says this diagnostic is not required. So
8881 allow it in extern "C" blocks unless predantic is specified.
8882 Allow it in all cases if -ms-extensions is specified. */
8883 if ((!pedantic && current_lang_name == lang_name_c)
8884 || flag_ms_extensions)
8885 return;
8886 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8887 if (n)
8889 /* [basic.scope.class]
8891 A name N used in a class S shall refer to the same declaration
8892 in its context and when re-evaluated in the completed scope of
8893 S. */
8894 if (permerror (location_of (decl),
8895 "declaration of %q#D changes meaning of %qD",
8896 decl, OVL_NAME (decl)))
8897 inform (location_of ((tree) n->value),
8898 "%qD declared here as %q#D",
8899 OVL_NAME (decl), (tree) n->value);
8903 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8904 Secondary vtables are merged with primary vtables; this function
8905 will return the VAR_DECL for the primary vtable. */
8907 tree
8908 get_vtbl_decl_for_binfo (tree binfo)
8910 tree decl;
8912 decl = BINFO_VTABLE (binfo);
8913 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8915 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8916 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8918 if (decl)
8919 gcc_assert (VAR_P (decl));
8920 return decl;
8924 /* Returns the binfo for the primary base of BINFO. If the resulting
8925 BINFO is a virtual base, and it is inherited elsewhere in the
8926 hierarchy, then the returned binfo might not be the primary base of
8927 BINFO in the complete object. Check BINFO_PRIMARY_P or
8928 BINFO_LOST_PRIMARY_P to be sure. */
8930 static tree
8931 get_primary_binfo (tree binfo)
8933 tree primary_base;
8935 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8936 if (!primary_base)
8937 return NULL_TREE;
8939 return copied_binfo (primary_base, binfo);
8942 /* As above, but iterate until we reach the binfo that actually provides the
8943 vptr for BINFO. */
8945 static tree
8946 most_primary_binfo (tree binfo)
8948 tree b = binfo;
8949 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8950 && !BINFO_LOST_PRIMARY_P (b))
8952 tree primary_base = get_primary_binfo (b);
8953 gcc_assert (BINFO_PRIMARY_P (primary_base)
8954 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8955 b = primary_base;
8957 return b;
8960 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8961 type. Note that the virtual inheritance might be above or below BINFO in
8962 the hierarchy. */
8964 bool
8965 vptr_via_virtual_p (tree binfo)
8967 if (TYPE_P (binfo))
8968 binfo = TYPE_BINFO (binfo);
8969 tree primary = most_primary_binfo (binfo);
8970 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8971 a morally virtual base. */
8972 tree virt = binfo_via_virtual (primary, NULL_TREE);
8973 return virt != NULL_TREE;
8976 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8978 static int
8979 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8981 if (!indented_p)
8982 fprintf (stream, "%*s", indent, "");
8983 return 1;
8986 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8987 INDENT should be zero when called from the top level; it is
8988 incremented recursively. IGO indicates the next expected BINFO in
8989 inheritance graph ordering. */
8991 static tree
8992 dump_class_hierarchy_r (FILE *stream,
8993 dump_flags_t flags,
8994 tree binfo,
8995 tree igo,
8996 int indent)
8998 int indented = 0;
8999 tree base_binfo;
9000 int i;
9002 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
9003 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
9004 (HOST_WIDE_INT) (uintptr_t) binfo);
9005 if (binfo != igo)
9007 fprintf (stream, "alternative-path\n");
9008 return igo;
9010 igo = TREE_CHAIN (binfo);
9012 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
9013 tree_to_shwi (BINFO_OFFSET (binfo)));
9014 if (is_empty_class (BINFO_TYPE (binfo)))
9015 fprintf (stream, " empty");
9016 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
9017 fprintf (stream, " nearly-empty");
9018 if (BINFO_VIRTUAL_P (binfo))
9019 fprintf (stream, " virtual");
9020 fprintf (stream, "\n");
9022 if (BINFO_PRIMARY_P (binfo))
9024 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9025 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
9026 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
9027 TFF_PLAIN_IDENTIFIER),
9028 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
9030 if (BINFO_LOST_PRIMARY_P (binfo))
9032 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9033 fprintf (stream, " lost-primary");
9035 if (indented)
9036 fprintf (stream, "\n");
9038 if (!(flags & TDF_SLIM))
9040 int indented = 0;
9042 if (BINFO_SUBVTT_INDEX (binfo))
9044 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9045 fprintf (stream, " subvttidx=%s",
9046 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
9047 TFF_PLAIN_IDENTIFIER));
9049 if (BINFO_VPTR_INDEX (binfo))
9051 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9052 fprintf (stream, " vptridx=%s",
9053 expr_as_string (BINFO_VPTR_INDEX (binfo),
9054 TFF_PLAIN_IDENTIFIER));
9056 if (BINFO_VPTR_FIELD (binfo))
9058 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9059 fprintf (stream, " vbaseoffset=%s",
9060 expr_as_string (BINFO_VPTR_FIELD (binfo),
9061 TFF_PLAIN_IDENTIFIER));
9063 if (BINFO_VTABLE (binfo))
9065 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9066 fprintf (stream, " vptr=%s",
9067 expr_as_string (BINFO_VTABLE (binfo),
9068 TFF_PLAIN_IDENTIFIER));
9071 if (indented)
9072 fprintf (stream, "\n");
9075 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9076 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
9078 return igo;
9081 /* Dump the BINFO hierarchy for T. */
9083 static void
9084 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
9086 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9087 fprintf (stream, " size=%lu align=%lu\n",
9088 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
9089 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
9090 if (tree as_base = CLASSTYPE_AS_BASE (t))
9091 fprintf (stream, " base size=%lu base align=%lu\n",
9092 (unsigned long)(tree_to_shwi (TYPE_SIZE (as_base))
9093 / BITS_PER_UNIT),
9094 (unsigned long)(TYPE_ALIGN (as_base) / BITS_PER_UNIT));
9095 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
9096 fprintf (stream, "\n");
9099 /* Debug interface to hierarchy dumping. */
9101 void
9102 debug_class (tree t)
9104 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
9107 static void
9108 dump_class_hierarchy (tree t)
9110 dump_flags_t flags;
9111 if (FILE *stream = dump_begin (class_dump_id, &flags))
9113 dump_class_hierarchy_1 (stream, flags, t);
9114 dump_end (class_dump_id, stream);
9118 static void
9119 dump_array (FILE * stream, tree decl)
9121 tree value;
9122 unsigned HOST_WIDE_INT ix;
9123 HOST_WIDE_INT elt;
9124 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
9126 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
9127 / BITS_PER_UNIT);
9128 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
9129 fprintf (stream, " %s entries",
9130 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
9131 TFF_PLAIN_IDENTIFIER));
9132 fprintf (stream, "\n");
9134 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
9135 ix, value)
9136 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
9137 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
9140 static void
9141 dump_vtable (tree t, tree binfo, tree vtable)
9143 dump_flags_t flags;
9144 FILE *stream = dump_begin (class_dump_id, &flags);
9146 if (!stream)
9147 return;
9149 if (!(flags & TDF_SLIM))
9151 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9153 fprintf (stream, "%s for %s",
9154 ctor_vtbl_p ? "Construction vtable" : "Vtable",
9155 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
9156 if (ctor_vtbl_p)
9158 if (!BINFO_VIRTUAL_P (binfo))
9159 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
9160 (HOST_WIDE_INT) (uintptr_t) binfo);
9161 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9163 fprintf (stream, "\n");
9164 dump_array (stream, vtable);
9165 fprintf (stream, "\n");
9168 dump_end (class_dump_id, stream);
9171 static void
9172 dump_vtt (tree t, tree vtt)
9174 dump_flags_t flags;
9175 FILE *stream = dump_begin (class_dump_id, &flags);
9177 if (!stream)
9178 return;
9180 if (!(flags & TDF_SLIM))
9182 fprintf (stream, "VTT for %s\n",
9183 type_as_string (t, TFF_PLAIN_IDENTIFIER));
9184 dump_array (stream, vtt);
9185 fprintf (stream, "\n");
9188 dump_end (class_dump_id, stream);
9191 /* Dump a function or thunk and its thunkees. */
9193 static void
9194 dump_thunk (FILE *stream, int indent, tree thunk)
9196 static const char spaces[] = " ";
9197 tree name = DECL_NAME (thunk);
9198 tree thunks;
9200 fprintf (stream, "%.*s%p %s %s", indent, spaces,
9201 (void *)thunk,
9202 !DECL_THUNK_P (thunk) ? "function"
9203 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9204 name ? IDENTIFIER_POINTER (name) : "<unset>");
9205 if (DECL_THUNK_P (thunk))
9207 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9208 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9210 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9211 if (!virtual_adjust)
9212 /*NOP*/;
9213 else if (DECL_THIS_THUNK_P (thunk))
9214 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9215 tree_to_shwi (virtual_adjust));
9216 else
9217 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9218 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9219 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9220 if (THUNK_ALIAS (thunk))
9221 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9223 fprintf (stream, "\n");
9224 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9225 dump_thunk (stream, indent + 2, thunks);
9228 /* Dump the thunks for FN. */
9230 void
9231 debug_thunks (tree fn)
9233 dump_thunk (stderr, 0, fn);
9236 /* Virtual function table initialization. */
9238 /* Create all the necessary vtables for T and its base classes. */
9240 static void
9241 finish_vtbls (tree t)
9243 tree vbase;
9244 vec<constructor_elt, va_gc> *v = NULL;
9245 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9247 /* We lay out the primary and secondary vtables in one contiguous
9248 vtable. The primary vtable is first, followed by the non-virtual
9249 secondary vtables in inheritance graph order. */
9250 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9251 vtable, t, &v);
9253 /* Then come the virtual bases, also in inheritance graph order. */
9254 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9256 if (!BINFO_VIRTUAL_P (vbase))
9257 continue;
9258 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9261 if (BINFO_VTABLE (TYPE_BINFO (t)))
9262 initialize_vtable (TYPE_BINFO (t), v);
9265 /* Initialize the vtable for BINFO with the INITS. */
9267 static void
9268 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9270 tree decl;
9272 layout_vtable_decl (binfo, vec_safe_length (inits));
9273 decl = get_vtbl_decl_for_binfo (binfo);
9274 initialize_artificial_var (decl, inits);
9275 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9278 /* Build the VTT (virtual table table) for T.
9279 A class requires a VTT if it has virtual bases.
9281 This holds
9282 1 - primary virtual pointer for complete object T
9283 2 - secondary VTTs for each direct non-virtual base of T which requires a
9285 3 - secondary virtual pointers for each direct or indirect base of T which
9286 has virtual bases or is reachable via a virtual path from T.
9287 4 - secondary VTTs for each direct or indirect virtual base of T.
9289 Secondary VTTs look like complete object VTTs without part 4. */
9291 static void
9292 build_vtt (tree t)
9294 tree type;
9295 tree vtt;
9296 tree index;
9297 vec<constructor_elt, va_gc> *inits;
9299 /* Build up the initializers for the VTT. */
9300 inits = NULL;
9301 index = size_zero_node;
9302 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9304 /* If we didn't need a VTT, we're done. */
9305 if (!inits)
9306 return;
9308 /* Figure out the type of the VTT. */
9309 type = build_array_of_n_type (const_ptr_type_node,
9310 inits->length ());
9312 /* Now, build the VTT object itself. */
9313 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9314 initialize_artificial_var (vtt, inits);
9315 /* Add the VTT to the vtables list. */
9316 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9317 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9319 dump_vtt (t, vtt);
9322 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9323 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9324 and CHAIN the vtable pointer for this binfo after construction is
9325 complete. VALUE can also be another BINFO, in which case we recurse. */
9327 static tree
9328 binfo_ctor_vtable (tree binfo)
9330 tree vt;
9332 while (1)
9334 vt = BINFO_VTABLE (binfo);
9335 if (TREE_CODE (vt) == TREE_LIST)
9336 vt = TREE_VALUE (vt);
9337 if (TREE_CODE (vt) == TREE_BINFO)
9338 binfo = vt;
9339 else
9340 break;
9343 return vt;
9346 /* Data for secondary VTT initialization. */
9347 struct secondary_vptr_vtt_init_data
9349 /* Is this the primary VTT? */
9350 bool top_level_p;
9352 /* Current index into the VTT. */
9353 tree index;
9355 /* Vector of initializers built up. */
9356 vec<constructor_elt, va_gc> *inits;
9358 /* The type being constructed by this secondary VTT. */
9359 tree type_being_constructed;
9362 /* Recursively build the VTT-initializer for BINFO (which is in the
9363 hierarchy dominated by T). INITS points to the end of the initializer
9364 list to date. INDEX is the VTT index where the next element will be
9365 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9366 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9367 for virtual bases of T. When it is not so, we build the constructor
9368 vtables for the BINFO-in-T variant. */
9370 static void
9371 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9372 tree *index)
9374 int i;
9375 tree b;
9376 tree init;
9377 secondary_vptr_vtt_init_data data;
9378 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9380 /* We only need VTTs for subobjects with virtual bases. */
9381 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9382 return;
9384 /* We need to use a construction vtable if this is not the primary
9385 VTT. */
9386 if (!top_level_p)
9388 build_ctor_vtbl_group (binfo, t);
9390 /* Record the offset in the VTT where this sub-VTT can be found. */
9391 BINFO_SUBVTT_INDEX (binfo) = *index;
9394 /* Add the address of the primary vtable for the complete object. */
9395 init = binfo_ctor_vtable (binfo);
9396 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9397 if (top_level_p)
9399 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9400 BINFO_VPTR_INDEX (binfo) = *index;
9402 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9404 /* Recursively add the secondary VTTs for non-virtual bases. */
9405 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9406 if (!BINFO_VIRTUAL_P (b))
9407 build_vtt_inits (b, t, inits, index);
9409 /* Add secondary virtual pointers for all subobjects of BINFO with
9410 either virtual bases or reachable along a virtual path, except
9411 subobjects that are non-virtual primary bases. */
9412 data.top_level_p = top_level_p;
9413 data.index = *index;
9414 data.inits = *inits;
9415 data.type_being_constructed = BINFO_TYPE (binfo);
9417 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9419 *index = data.index;
9421 /* data.inits might have grown as we added secondary virtual pointers.
9422 Make sure our caller knows about the new vector. */
9423 *inits = data.inits;
9425 if (top_level_p)
9426 /* Add the secondary VTTs for virtual bases in inheritance graph
9427 order. */
9428 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9430 if (!BINFO_VIRTUAL_P (b))
9431 continue;
9433 build_vtt_inits (b, t, inits, index);
9435 else
9436 /* Remove the ctor vtables we created. */
9437 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9440 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9441 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9443 static tree
9444 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9446 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9448 /* We don't care about bases that don't have vtables. */
9449 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9450 return dfs_skip_bases;
9452 /* We're only interested in proper subobjects of the type being
9453 constructed. */
9454 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9455 return NULL_TREE;
9457 /* We're only interested in bases with virtual bases or reachable
9458 via a virtual path from the type being constructed. */
9459 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9460 || binfo_via_virtual (binfo, data->type_being_constructed)))
9461 return dfs_skip_bases;
9463 /* We're not interested in non-virtual primary bases. */
9464 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9465 return NULL_TREE;
9467 /* Record the index where this secondary vptr can be found. */
9468 if (data->top_level_p)
9470 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9471 BINFO_VPTR_INDEX (binfo) = data->index;
9473 if (BINFO_VIRTUAL_P (binfo))
9475 /* It's a primary virtual base, and this is not a
9476 construction vtable. Find the base this is primary of in
9477 the inheritance graph, and use that base's vtable
9478 now. */
9479 while (BINFO_PRIMARY_P (binfo))
9480 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9484 /* Add the initializer for the secondary vptr itself. */
9485 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9487 /* Advance the vtt index. */
9488 data->index = size_binop (PLUS_EXPR, data->index,
9489 TYPE_SIZE_UNIT (ptr_type_node));
9491 return NULL_TREE;
9494 /* Called from build_vtt_inits via dfs_walk. After building
9495 constructor vtables and generating the sub-vtt from them, we need
9496 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9497 binfo of the base whose sub vtt was generated. */
9499 static tree
9500 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9502 tree vtable = BINFO_VTABLE (binfo);
9504 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9505 /* If this class has no vtable, none of its bases do. */
9506 return dfs_skip_bases;
9508 if (!vtable)
9509 /* This might be a primary base, so have no vtable in this
9510 hierarchy. */
9511 return NULL_TREE;
9513 /* If we scribbled the construction vtable vptr into BINFO, clear it
9514 out now. */
9515 if (TREE_CODE (vtable) == TREE_LIST
9516 && (TREE_PURPOSE (vtable) == (tree) data))
9517 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9519 return NULL_TREE;
9522 /* Build the construction vtable group for BINFO which is in the
9523 hierarchy dominated by T. */
9525 static void
9526 build_ctor_vtbl_group (tree binfo, tree t)
9528 tree type;
9529 tree vtbl;
9530 tree id;
9531 tree vbase;
9532 vec<constructor_elt, va_gc> *v;
9534 /* See if we've already created this construction vtable group. */
9535 id = mangle_ctor_vtbl_for_type (t, binfo);
9536 if (get_global_binding (id))
9537 return;
9539 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9540 /* Build a version of VTBL (with the wrong type) for use in
9541 constructing the addresses of secondary vtables in the
9542 construction vtable group. */
9543 vtbl = build_vtable (t, id, ptr_type_node);
9545 /* Don't export construction vtables from shared libraries. Even on
9546 targets that don't support hidden visibility, this tells
9547 can_refer_decl_in_current_unit_p not to assume that it's safe to
9548 access from a different compilation unit (bz 54314). */
9549 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9550 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9552 v = NULL;
9553 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9554 binfo, vtbl, t, &v);
9556 /* Add the vtables for each of our virtual bases using the vbase in T
9557 binfo. */
9558 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9559 vbase;
9560 vbase = TREE_CHAIN (vbase))
9562 tree b;
9564 if (!BINFO_VIRTUAL_P (vbase))
9565 continue;
9566 b = copied_binfo (vbase, binfo);
9568 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9571 /* Figure out the type of the construction vtable. */
9572 type = build_array_of_n_type (vtable_entry_type, v->length ());
9573 layout_type (type);
9574 TREE_TYPE (vtbl) = type;
9575 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9576 layout_decl (vtbl, 0);
9578 /* Initialize the construction vtable. */
9579 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9580 initialize_artificial_var (vtbl, v);
9581 dump_vtable (t, binfo, vtbl);
9584 /* Add the vtbl initializers for BINFO (and its bases other than
9585 non-virtual primaries) to the list of INITS. BINFO is in the
9586 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9587 the constructor the vtbl inits should be accumulated for. (If this
9588 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9589 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9590 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9591 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9592 but are not necessarily the same in terms of layout. */
9594 static void
9595 accumulate_vtbl_inits (tree binfo,
9596 tree orig_binfo,
9597 tree rtti_binfo,
9598 tree vtbl,
9599 tree t,
9600 vec<constructor_elt, va_gc> **inits)
9602 int i;
9603 tree base_binfo;
9604 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9606 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9608 /* If it doesn't have a vptr, we don't do anything. */
9609 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9610 return;
9612 /* If we're building a construction vtable, we're not interested in
9613 subobjects that don't require construction vtables. */
9614 if (ctor_vtbl_p
9615 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9616 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9617 return;
9619 /* Build the initializers for the BINFO-in-T vtable. */
9620 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9622 /* Walk the BINFO and its bases. We walk in preorder so that as we
9623 initialize each vtable we can figure out at what offset the
9624 secondary vtable lies from the primary vtable. We can't use
9625 dfs_walk here because we need to iterate through bases of BINFO
9626 and RTTI_BINFO simultaneously. */
9627 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9629 /* Skip virtual bases. */
9630 if (BINFO_VIRTUAL_P (base_binfo))
9631 continue;
9632 accumulate_vtbl_inits (base_binfo,
9633 BINFO_BASE_BINFO (orig_binfo, i),
9634 rtti_binfo, vtbl, t,
9635 inits);
9639 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9640 BINFO vtable to L. */
9642 static void
9643 dfs_accumulate_vtbl_inits (tree binfo,
9644 tree orig_binfo,
9645 tree rtti_binfo,
9646 tree orig_vtbl,
9647 tree t,
9648 vec<constructor_elt, va_gc> **l)
9650 tree vtbl = NULL_TREE;
9651 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9652 int n_inits;
9654 if (ctor_vtbl_p
9655 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9657 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9658 primary virtual base. If it is not the same primary in
9659 the hierarchy of T, we'll need to generate a ctor vtable
9660 for it, to place at its location in T. If it is the same
9661 primary, we still need a VTT entry for the vtable, but it
9662 should point to the ctor vtable for the base it is a
9663 primary for within the sub-hierarchy of RTTI_BINFO.
9665 There are three possible cases:
9667 1) We are in the same place.
9668 2) We are a primary base within a lost primary virtual base of
9669 RTTI_BINFO.
9670 3) We are primary to something not a base of RTTI_BINFO. */
9672 tree b;
9673 tree last = NULL_TREE;
9675 /* First, look through the bases we are primary to for RTTI_BINFO
9676 or a virtual base. */
9677 b = binfo;
9678 while (BINFO_PRIMARY_P (b))
9680 b = BINFO_INHERITANCE_CHAIN (b);
9681 last = b;
9682 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9683 goto found;
9685 /* If we run out of primary links, keep looking down our
9686 inheritance chain; we might be an indirect primary. */
9687 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9688 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9689 break;
9690 found:
9692 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9693 base B and it is a base of RTTI_BINFO, this is case 2. In
9694 either case, we share our vtable with LAST, i.e. the
9695 derived-most base within B of which we are a primary. */
9696 if (b == rtti_binfo
9697 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9698 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9699 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9700 binfo_ctor_vtable after everything's been set up. */
9701 vtbl = last;
9703 /* Otherwise, this is case 3 and we get our own. */
9705 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9706 return;
9708 n_inits = vec_safe_length (*l);
9710 if (!vtbl)
9712 tree index;
9713 int non_fn_entries;
9715 /* Add the initializer for this vtable. */
9716 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9717 &non_fn_entries, l);
9719 /* Figure out the position to which the VPTR should point. */
9720 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9721 index = size_binop (MULT_EXPR,
9722 TYPE_SIZE_UNIT (vtable_entry_type),
9723 size_int (non_fn_entries + n_inits));
9724 vtbl = fold_build_pointer_plus (vtbl, index);
9727 if (ctor_vtbl_p)
9728 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9729 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9730 straighten this out. */
9731 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9732 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9733 /* Throw away any unneeded intializers. */
9734 (*l)->truncate (n_inits);
9735 else
9736 /* For an ordinary vtable, set BINFO_VTABLE. */
9737 BINFO_VTABLE (binfo) = vtbl;
9740 static GTY(()) tree abort_fndecl_addr;
9741 static GTY(()) tree dvirt_fn;
9743 /* Construct the initializer for BINFO's virtual function table. BINFO
9744 is part of the hierarchy dominated by T. If we're building a
9745 construction vtable, the ORIG_BINFO is the binfo we should use to
9746 find the actual function pointers to put in the vtable - but they
9747 can be overridden on the path to most-derived in the graph that
9748 ORIG_BINFO belongs. Otherwise,
9749 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9750 BINFO that should be indicated by the RTTI information in the
9751 vtable; it will be a base class of T, rather than T itself, if we
9752 are building a construction vtable.
9754 The value returned is a TREE_LIST suitable for wrapping in a
9755 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9756 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9757 number of non-function entries in the vtable.
9759 It might seem that this function should never be called with a
9760 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9761 base is always subsumed by a derived class vtable. However, when
9762 we are building construction vtables, we do build vtables for
9763 primary bases; we need these while the primary base is being
9764 constructed. */
9766 static void
9767 build_vtbl_initializer (tree binfo,
9768 tree orig_binfo,
9769 tree t,
9770 tree rtti_binfo,
9771 int* non_fn_entries_p,
9772 vec<constructor_elt, va_gc> **inits)
9774 tree v;
9775 vtbl_init_data vid;
9776 unsigned ix, jx;
9777 tree vbinfo;
9778 vec<tree, va_gc> *vbases;
9779 constructor_elt *e;
9781 /* Initialize VID. */
9782 memset (&vid, 0, sizeof (vid));
9783 vid.binfo = binfo;
9784 vid.derived = t;
9785 vid.rtti_binfo = rtti_binfo;
9786 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9787 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9788 vid.generate_vcall_entries = true;
9789 /* The first vbase or vcall offset is at index -3 in the vtable. */
9790 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9792 /* Add entries to the vtable for RTTI. */
9793 build_rtti_vtbl_entries (binfo, &vid);
9795 /* Create an array for keeping track of the functions we've
9796 processed. When we see multiple functions with the same
9797 signature, we share the vcall offsets. */
9798 vec_alloc (vid.fns, 32);
9799 /* Add the vcall and vbase offset entries. */
9800 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9802 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9803 build_vbase_offset_vtbl_entries. */
9804 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9805 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9806 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9808 /* If the target requires padding between data entries, add that now. */
9809 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9811 int n_entries = vec_safe_length (vid.inits);
9813 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries,
9814 true);
9816 /* Move data entries into their new positions and add padding
9817 after the new positions. Iterate backwards so we don't
9818 overwrite entries that we would need to process later. */
9819 for (ix = n_entries - 1;
9820 vid.inits->iterate (ix, &e);
9821 ix--)
9823 int j;
9824 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9825 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9827 (*vid.inits)[new_position] = *e;
9829 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9831 constructor_elt *f = &(*vid.inits)[new_position - j];
9832 f->index = NULL_TREE;
9833 f->value = build1 (NOP_EXPR, vtable_entry_type,
9834 null_pointer_node);
9839 if (non_fn_entries_p)
9840 *non_fn_entries_p = vec_safe_length (vid.inits);
9842 /* The initializers for virtual functions were built up in reverse
9843 order. Straighten them out and add them to the running list in one
9844 step. */
9845 jx = vec_safe_length (*inits);
9846 vec_safe_grow (*inits, jx + vid.inits->length (), true);
9848 for (ix = vid.inits->length () - 1;
9849 vid.inits->iterate (ix, &e);
9850 ix--, jx++)
9851 (**inits)[jx] = *e;
9853 /* Go through all the ordinary virtual functions, building up
9854 initializers. */
9855 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9857 tree delta;
9858 tree vcall_index;
9859 tree fn, fn_original;
9860 tree init = NULL_TREE;
9862 fn = BV_FN (v);
9863 fn_original = fn;
9864 if (DECL_THUNK_P (fn))
9866 if (!DECL_NAME (fn))
9867 finish_thunk (fn);
9868 if (THUNK_ALIAS (fn))
9870 fn = THUNK_ALIAS (fn);
9871 BV_FN (v) = fn;
9873 fn_original = THUNK_TARGET (fn);
9876 /* If the only definition of this function signature along our
9877 primary base chain is from a lost primary, this vtable slot will
9878 never be used, so just zero it out. This is important to avoid
9879 requiring extra thunks which cannot be generated with the function.
9881 We first check this in update_vtable_entry_for_fn, so we handle
9882 restored primary bases properly; we also need to do it here so we
9883 zero out unused slots in ctor vtables, rather than filling them
9884 with erroneous values (though harmless, apart from relocation
9885 costs). */
9886 if (BV_LOST_PRIMARY (v))
9887 init = size_zero_node;
9889 if (! init)
9891 /* Pull the offset for `this', and the function to call, out of
9892 the list. */
9893 delta = BV_DELTA (v);
9894 vcall_index = BV_VCALL_INDEX (v);
9896 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9897 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9899 /* You can't call an abstract virtual function; it's abstract.
9900 So, we replace these functions with __pure_virtual. */
9901 if (DECL_PURE_VIRTUAL_P (fn_original))
9903 fn = abort_fndecl;
9904 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9906 if (abort_fndecl_addr == NULL)
9907 abort_fndecl_addr
9908 = fold_convert (vfunc_ptr_type_node,
9909 build_fold_addr_expr (fn));
9910 init = abort_fndecl_addr;
9913 /* Likewise for deleted virtuals. */
9914 else if (DECL_DELETED_FN (fn_original))
9916 if (!dvirt_fn)
9918 tree name = get_identifier ("__cxa_deleted_virtual");
9919 dvirt_fn = get_global_binding (name);
9920 if (!dvirt_fn)
9921 dvirt_fn = push_library_fn
9922 (name,
9923 build_function_type_list (void_type_node, NULL_TREE),
9924 NULL_TREE, ECF_NORETURN | ECF_COLD);
9926 fn = dvirt_fn;
9927 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9928 init = fold_convert (vfunc_ptr_type_node,
9929 build_fold_addr_expr (fn));
9931 else
9933 if (!integer_zerop (delta) || vcall_index)
9935 fn = make_thunk (fn, /*this_adjusting=*/1,
9936 delta, vcall_index);
9937 if (!DECL_NAME (fn))
9938 finish_thunk (fn);
9940 /* Take the address of the function, considering it to be of an
9941 appropriate generic type. */
9942 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9943 init = fold_convert (vfunc_ptr_type_node,
9944 build_fold_addr_expr (fn));
9945 /* Don't refer to a virtual destructor from a constructor
9946 vtable or a vtable for an abstract class, since destroying
9947 an object under construction is undefined behavior and we
9948 don't want it to be considered a candidate for speculative
9949 devirtualization. But do create the thunk for ABI
9950 compliance. */
9951 if (DECL_DESTRUCTOR_P (fn_original)
9952 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9953 || orig_binfo != binfo))
9954 init = size_zero_node;
9958 /* And add it to the chain of initializers. */
9959 if (TARGET_VTABLE_USES_DESCRIPTORS)
9961 int i;
9962 if (init == size_zero_node)
9963 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9964 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
9965 else
9966 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9968 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9969 fn, build_int_cst (NULL_TREE, i));
9970 TREE_CONSTANT (fdesc) = 1;
9972 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), fdesc);
9975 else
9976 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
9980 /* Adds to vid->inits the initializers for the vbase and vcall
9981 offsets in BINFO, which is in the hierarchy dominated by T. */
9983 static void
9984 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9986 tree b;
9988 /* If this is a derived class, we must first create entries
9989 corresponding to the primary base class. */
9990 b = get_primary_binfo (binfo);
9991 if (b)
9992 build_vcall_and_vbase_vtbl_entries (b, vid);
9994 /* Add the vbase entries for this base. */
9995 build_vbase_offset_vtbl_entries (binfo, vid);
9996 /* Add the vcall entries for this base. */
9997 build_vcall_offset_vtbl_entries (binfo, vid);
10000 /* Returns the initializers for the vbase offset entries in the vtable
10001 for BINFO (which is part of the class hierarchy dominated by T), in
10002 reverse order. VBASE_OFFSET_INDEX gives the vtable index
10003 where the next vbase offset will go. */
10005 static void
10006 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10008 tree vbase;
10009 tree t;
10010 tree non_primary_binfo;
10012 /* If there are no virtual baseclasses, then there is nothing to
10013 do. */
10014 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10015 return;
10017 t = vid->derived;
10019 /* We might be a primary base class. Go up the inheritance hierarchy
10020 until we find the most derived class of which we are a primary base:
10021 it is the offset of that which we need to use. */
10022 non_primary_binfo = binfo;
10023 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
10025 tree b;
10027 /* If we have reached a virtual base, then it must be a primary
10028 base (possibly multi-level) of vid->binfo, or we wouldn't
10029 have called build_vcall_and_vbase_vtbl_entries for it. But it
10030 might be a lost primary, so just skip down to vid->binfo. */
10031 if (BINFO_VIRTUAL_P (non_primary_binfo))
10033 non_primary_binfo = vid->binfo;
10034 break;
10037 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
10038 if (get_primary_binfo (b) != non_primary_binfo)
10039 break;
10040 non_primary_binfo = b;
10043 /* Go through the virtual bases, adding the offsets. */
10044 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10045 vbase;
10046 vbase = TREE_CHAIN (vbase))
10048 tree b;
10049 tree delta;
10051 if (!BINFO_VIRTUAL_P (vbase))
10052 continue;
10054 /* Find the instance of this virtual base in the complete
10055 object. */
10056 b = copied_binfo (vbase, binfo);
10058 /* If we've already got an offset for this virtual base, we
10059 don't need another one. */
10060 if (BINFO_VTABLE_PATH_MARKED (b))
10061 continue;
10062 BINFO_VTABLE_PATH_MARKED (b) = 1;
10064 /* Figure out where we can find this vbase offset. */
10065 delta = size_binop (MULT_EXPR,
10066 vid->index,
10067 fold_convert (ssizetype,
10068 TYPE_SIZE_UNIT (vtable_entry_type)));
10069 if (vid->primary_vtbl_p)
10070 BINFO_VPTR_FIELD (b) = delta;
10072 if (binfo != TYPE_BINFO (t))
10073 /* The vbase offset had better be the same. */
10074 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
10076 /* The next vbase will come at a more negative offset. */
10077 vid->index = size_binop (MINUS_EXPR, vid->index,
10078 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10080 /* The initializer is the delta from BINFO to this virtual base.
10081 The vbase offsets go in reverse inheritance-graph order, and
10082 we are walking in inheritance graph order so these end up in
10083 the right order. */
10084 delta = size_diffop_loc (input_location,
10085 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
10087 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
10088 fold_build1_loc (input_location, NOP_EXPR,
10089 vtable_entry_type, delta));
10093 /* Adds the initializers for the vcall offset entries in the vtable
10094 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10095 to VID->INITS. */
10097 static void
10098 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10100 /* We only need these entries if this base is a virtual base. We
10101 compute the indices -- but do not add to the vtable -- when
10102 building the main vtable for a class. */
10103 if (binfo == TYPE_BINFO (vid->derived)
10104 || (BINFO_VIRTUAL_P (binfo)
10105 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10106 correspond to VID->DERIVED), we are building a primary
10107 construction virtual table. Since this is a primary
10108 virtual table, we do not need the vcall offsets for
10109 BINFO. */
10110 && binfo != vid->rtti_binfo))
10112 /* We need a vcall offset for each of the virtual functions in this
10113 vtable. For example:
10115 class A { virtual void f (); };
10116 class B1 : virtual public A { virtual void f (); };
10117 class B2 : virtual public A { virtual void f (); };
10118 class C: public B1, public B2 { virtual void f (); };
10120 A C object has a primary base of B1, which has a primary base of A. A
10121 C also has a secondary base of B2, which no longer has a primary base
10122 of A. So the B2-in-C construction vtable needs a secondary vtable for
10123 A, which will adjust the A* to a B2* to call f. We have no way of
10124 knowing what (or even whether) this offset will be when we define B2,
10125 so we store this "vcall offset" in the A sub-vtable and look it up in
10126 a "virtual thunk" for B2::f.
10128 We need entries for all the functions in our primary vtable and
10129 in our non-virtual bases' secondary vtables. */
10130 vid->vbase = binfo;
10131 /* If we are just computing the vcall indices -- but do not need
10132 the actual entries -- not that. */
10133 if (!BINFO_VIRTUAL_P (binfo))
10134 vid->generate_vcall_entries = false;
10135 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10136 add_vcall_offset_vtbl_entries_r (binfo, vid);
10140 /* Build vcall offsets, starting with those for BINFO. */
10142 static void
10143 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
10145 int i;
10146 tree primary_binfo;
10147 tree base_binfo;
10149 /* Don't walk into virtual bases -- except, of course, for the
10150 virtual base for which we are building vcall offsets. Any
10151 primary virtual base will have already had its offsets generated
10152 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10153 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
10154 return;
10156 /* If BINFO has a primary base, process it first. */
10157 primary_binfo = get_primary_binfo (binfo);
10158 if (primary_binfo)
10159 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
10161 /* Add BINFO itself to the list. */
10162 add_vcall_offset_vtbl_entries_1 (binfo, vid);
10164 /* Scan the non-primary bases of BINFO. */
10165 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10166 if (base_binfo != primary_binfo)
10167 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
10170 /* Called from build_vcall_offset_vtbl_entries_r. */
10172 static void
10173 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10175 /* Make entries for the rest of the virtuals. */
10176 tree orig_fn;
10178 /* The ABI requires that the methods be processed in declaration
10179 order. */
10180 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
10181 orig_fn;
10182 orig_fn = DECL_CHAIN (orig_fn))
10183 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10184 add_vcall_offset (orig_fn, binfo, vid);
10187 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10189 static void
10190 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10192 size_t i;
10193 tree vcall_offset;
10194 tree derived_entry;
10196 /* If there is already an entry for a function with the same
10197 signature as FN, then we do not need a second vcall offset.
10198 Check the list of functions already present in the derived
10199 class vtable. */
10200 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10202 if (same_signature_p (derived_entry, orig_fn)
10203 /* We only use one vcall offset for virtual destructors,
10204 even though there are two virtual table entries. */
10205 || (DECL_DESTRUCTOR_P (derived_entry)
10206 && DECL_DESTRUCTOR_P (orig_fn)))
10207 return;
10210 /* If we are building these vcall offsets as part of building
10211 the vtable for the most derived class, remember the vcall
10212 offset. */
10213 if (vid->binfo == TYPE_BINFO (vid->derived))
10215 tree_pair_s elt = {orig_fn, vid->index};
10216 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10219 /* The next vcall offset will be found at a more negative
10220 offset. */
10221 vid->index = size_binop (MINUS_EXPR, vid->index,
10222 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10224 /* Keep track of this function. */
10225 vec_safe_push (vid->fns, orig_fn);
10227 if (vid->generate_vcall_entries)
10229 tree base;
10230 tree fn;
10232 /* Find the overriding function. */
10233 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10234 if (fn == error_mark_node)
10235 vcall_offset = build_zero_cst (vtable_entry_type);
10236 else
10238 base = TREE_VALUE (fn);
10240 /* The vbase we're working on is a primary base of
10241 vid->binfo. But it might be a lost primary, so its
10242 BINFO_OFFSET might be wrong, so we just use the
10243 BINFO_OFFSET from vid->binfo. */
10244 vcall_offset = size_diffop_loc (input_location,
10245 BINFO_OFFSET (base),
10246 BINFO_OFFSET (vid->binfo));
10247 vcall_offset = fold_build1_loc (input_location,
10248 NOP_EXPR, vtable_entry_type,
10249 vcall_offset);
10251 /* Add the initializer to the vtable. */
10252 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10256 /* Return vtbl initializers for the RTTI entries corresponding to the
10257 BINFO's vtable. The RTTI entries should indicate the object given
10258 by VID->rtti_binfo. */
10260 static void
10261 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10263 tree b;
10264 tree t;
10265 tree offset;
10266 tree decl;
10267 tree init;
10269 t = BINFO_TYPE (vid->rtti_binfo);
10271 /* To find the complete object, we will first convert to our most
10272 primary base, and then add the offset in the vtbl to that value. */
10273 b = most_primary_binfo (binfo);
10274 offset = size_diffop_loc (input_location,
10275 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10277 /* The second entry is the address of the typeinfo object. */
10278 if (flag_rtti)
10279 decl = build_address (get_tinfo_decl (t));
10280 else
10281 decl = integer_zero_node;
10283 /* Convert the declaration to a type that can be stored in the
10284 vtable. */
10285 init = build_nop (vfunc_ptr_type_node, decl);
10286 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10288 /* Add the offset-to-top entry. It comes earlier in the vtable than
10289 the typeinfo entry. Convert the offset to look like a
10290 function pointer, so that we can put it in the vtable. */
10291 init = build_nop (vfunc_ptr_type_node, offset);
10292 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10295 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10296 accessibility. */
10298 bool
10299 uniquely_derived_from_p (tree parent, tree type)
10301 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10302 return base && base != error_mark_node;
10305 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10307 bool
10308 publicly_uniquely_derived_p (tree parent, tree type)
10310 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10311 NULL, tf_none);
10312 return base && base != error_mark_node;
10315 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10316 class between them, if any. */
10318 tree
10319 common_enclosing_class (tree ctx1, tree ctx2)
10321 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10322 return NULL_TREE;
10323 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10324 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10325 if (ctx1 == ctx2)
10326 return ctx1;
10327 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10328 TYPE_MARKED_P (t) = true;
10329 tree found = NULL_TREE;
10330 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10331 if (TYPE_MARKED_P (t))
10333 found = t;
10334 break;
10336 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10337 TYPE_MARKED_P (t) = false;
10338 return found;
10341 #include "gt-cp-class.h"