Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / cp / class.c
blob7138e304937631c5b858066d5b88e0f87ea1eeb6
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 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 || DECL_DELETED_FN (fn))
5723 return true;
5725 return false;
5728 /* Like type_build_ctor_call, but for destructors. */
5730 bool
5731 type_build_dtor_call (tree t)
5733 tree inner;
5734 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5735 return true;
5736 inner = strip_array_types (t);
5737 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5738 || !COMPLETE_TYPE_P (inner))
5739 return false;
5740 if (cxx_dialect < cxx11)
5741 return false;
5742 /* A user-declared destructor might be private, and a destructor might
5743 be trivial but deleted. */
5744 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5745 iter; ++iter)
5747 tree fn = *iter;
5748 if (!DECL_ARTIFICIAL (fn)
5749 || TREE_DEPRECATED (fn)
5750 || DECL_DELETED_FN (fn))
5751 return true;
5753 return false;
5756 /* Returns TRUE iff we need a cookie when dynamically allocating an
5757 array whose elements have the indicated class TYPE. */
5759 static bool
5760 type_requires_array_cookie (tree type)
5762 tree fns;
5763 bool has_two_argument_delete_p = false;
5765 gcc_assert (CLASS_TYPE_P (type));
5767 /* If there's a non-trivial destructor, we need a cookie. In order
5768 to iterate through the array calling the destructor for each
5769 element, we'll have to know how many elements there are. */
5770 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5771 return true;
5773 /* If the usual deallocation function is a two-argument whose second
5774 argument is of type `size_t', then we have to pass the size of
5775 the array to the deallocation function, so we will need to store
5776 a cookie. */
5777 fns = lookup_fnfields (TYPE_BINFO (type),
5778 ovl_op_identifier (false, VEC_DELETE_EXPR),
5779 /*protect=*/0, tf_warning_or_error);
5780 /* If there are no `operator []' members, or the lookup is
5781 ambiguous, then we don't need a cookie. */
5782 if (!fns || fns == error_mark_node)
5783 return false;
5784 /* Loop through all of the functions. */
5785 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5787 tree fn = *iter;
5789 /* See if this function is a one-argument delete function. If
5790 it is, then it will be the usual deallocation function. */
5791 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5792 if (second_parm == void_list_node)
5793 return false;
5794 /* Do not consider this function if its second argument is an
5795 ellipsis. */
5796 if (!second_parm)
5797 continue;
5798 /* Otherwise, if we have a two-argument function and the second
5799 argument is `size_t', it will be the usual deallocation
5800 function -- unless there is one-argument function, too. */
5801 if (TREE_CHAIN (second_parm) == void_list_node
5802 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5803 has_two_argument_delete_p = true;
5806 return has_two_argument_delete_p;
5809 /* Finish computing the `literal type' property of class type T.
5811 At this point, we have already processed base classes and
5812 non-static data members. We need to check whether the copy
5813 constructor is trivial, the destructor is trivial, and there
5814 is a trivial default constructor or at least one constexpr
5815 constructor other than the copy constructor. */
5817 static void
5818 finalize_literal_type_property (tree t)
5820 tree fn;
5822 if (cxx_dialect < cxx11)
5823 CLASSTYPE_LITERAL_P (t) = false;
5824 else if (CLASSTYPE_LITERAL_P (t)
5825 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
5826 && (cxx_dialect < cxx20 || !type_maybe_constexpr_destructor (t)))
5827 CLASSTYPE_LITERAL_P (t) = false;
5828 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5829 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5830 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5831 && CLASSTYPE_NON_AGGREGATE (t)
5832 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5833 CLASSTYPE_LITERAL_P (t) = false;
5835 /* C++14 DR 1684 removed this restriction. */
5836 if (cxx_dialect < cxx14
5837 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5838 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5839 if (TREE_CODE (fn) == FUNCTION_DECL
5840 && DECL_DECLARED_CONSTEXPR_P (fn)
5841 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5842 && !DECL_CONSTRUCTOR_P (fn))
5844 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5845 if (!DECL_GENERATED_P (fn))
5847 auto_diagnostic_group d;
5848 if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5849 "enclosing class of %<constexpr%> non-static "
5850 "member function %q+#D is not a literal type", fn))
5851 explain_non_literal_class (t);
5856 /* T is a non-literal type used in a context which requires a constant
5857 expression. Explain why it isn't literal. */
5859 void
5860 explain_non_literal_class (tree t)
5862 static hash_set<tree> *diagnosed;
5864 if (!CLASS_TYPE_P (t))
5865 return;
5866 t = TYPE_MAIN_VARIANT (t);
5868 if (diagnosed == NULL)
5869 diagnosed = new hash_set<tree>;
5870 if (diagnosed->add (t))
5871 /* Already explained. */
5872 return;
5874 auto_diagnostic_group d;
5875 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5876 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5877 inform (UNKNOWN_LOCATION,
5878 " %qT is a closure type, which is only literal in "
5879 "C++17 and later", t);
5880 else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5881 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5882 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
5883 && !type_maybe_constexpr_destructor (t))
5884 inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
5886 else if (CLASSTYPE_NON_AGGREGATE (t)
5887 && !TYPE_HAS_TRIVIAL_DFLT (t)
5888 && !LAMBDA_TYPE_P (t)
5889 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5891 inform (UNKNOWN_LOCATION,
5892 " %q+T is not an aggregate, does not have a trivial "
5893 "default constructor, and has no %<constexpr%> constructor that "
5894 "is not a copy or move constructor", t);
5895 if (type_has_non_user_provided_default_constructor (t))
5896 /* Note that we can't simply call locate_ctor because when the
5897 constructor is deleted it just returns NULL_TREE. */
5898 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5900 tree fn = *iter;
5901 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5903 parms = skip_artificial_parms_for (fn, parms);
5905 if (sufficient_parms_p (parms))
5907 if (DECL_DELETED_FN (fn))
5908 maybe_explain_implicit_delete (fn);
5909 else
5910 explain_invalid_constexpr_fn (fn);
5911 break;
5915 else
5917 tree binfo, base_binfo, field; int i;
5918 for (binfo = TYPE_BINFO (t), i = 0;
5919 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5921 tree basetype = TREE_TYPE (base_binfo);
5922 if (!CLASSTYPE_LITERAL_P (basetype))
5924 inform (UNKNOWN_LOCATION,
5925 " base class %qT of %q+T is non-literal",
5926 basetype, t);
5927 explain_non_literal_class (basetype);
5928 return;
5931 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5933 tree ftype;
5934 if (TREE_CODE (field) != FIELD_DECL)
5935 continue;
5936 ftype = TREE_TYPE (field);
5937 if (!literal_type_p (ftype))
5939 inform (DECL_SOURCE_LOCATION (field),
5940 " non-static data member %qD has non-literal type",
5941 field);
5942 if (CLASS_TYPE_P (ftype))
5943 explain_non_literal_class (ftype);
5945 if (CP_TYPE_VOLATILE_P (ftype))
5946 inform (DECL_SOURCE_LOCATION (field),
5947 " non-static data member %qD has volatile type", field);
5952 /* Check the validity of the bases and members declared in T. Add any
5953 implicitly-generated functions (like copy-constructors and
5954 assignment operators). Compute various flag bits (like
5955 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5956 level: i.e., independently of the ABI in use. */
5958 static void
5959 check_bases_and_members (tree t)
5961 /* Nonzero if the implicitly generated copy constructor should take
5962 a non-const reference argument. */
5963 int cant_have_const_ctor;
5964 /* Nonzero if the implicitly generated assignment operator
5965 should take a non-const reference argument. */
5966 int no_const_asn_ref;
5967 tree access_decls;
5968 bool saved_complex_asn_ref;
5969 bool saved_nontrivial_dtor;
5970 tree fn;
5972 /* By default, we use const reference arguments and generate default
5973 constructors. */
5974 cant_have_const_ctor = 0;
5975 no_const_asn_ref = 0;
5977 /* Check all the base-classes and set FMEM members to point to arrays
5978 of potential interest. */
5979 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5981 /* Deduce noexcept on destructor. This needs to happen after we've set
5982 triviality flags appropriately for our bases. */
5983 if (cxx_dialect >= cxx11)
5984 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5985 deduce_noexcept_on_destructor (dtor);
5987 /* Check all the method declarations. */
5988 check_methods (t);
5990 /* Save the initial values of these flags which only indicate whether
5991 or not the class has user-provided functions. As we analyze the
5992 bases and members we can set these flags for other reasons. */
5993 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5994 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5996 /* Check all the data member declarations. We cannot call
5997 check_field_decls until we have called check_bases check_methods,
5998 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5999 being set appropriately. */
6000 check_field_decls (t, &access_decls,
6001 &cant_have_const_ctor,
6002 &no_const_asn_ref);
6004 /* A nearly-empty class has to be vptr-containing; a nearly empty
6005 class contains just a vptr. */
6006 if (!TYPE_CONTAINS_VPTR_P (t))
6007 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
6009 /* Do some bookkeeping that will guide the generation of implicitly
6010 declared member functions. */
6011 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6012 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6013 /* We need to call a constructor for this class if it has a
6014 user-provided constructor, or if the default constructor is going
6015 to initialize the vptr. (This is not an if-and-only-if;
6016 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
6017 themselves need constructing.) */
6018 TYPE_NEEDS_CONSTRUCTING (t)
6019 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
6020 /* [dcl.init.aggr]
6022 An aggregate is an array or a class with no user-provided
6023 constructors ... and no virtual functions.
6025 Again, other conditions for being an aggregate are checked
6026 elsewhere. */
6027 CLASSTYPE_NON_AGGREGATE (t)
6028 |= ((cxx_dialect < cxx20
6029 ? type_has_user_provided_or_explicit_constructor (t)
6030 : TYPE_HAS_USER_CONSTRUCTOR (t))
6031 || TYPE_POLYMORPHIC_P (t));
6032 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6033 retain the old definition internally for ABI reasons. */
6034 CLASSTYPE_NON_LAYOUT_POD_P (t)
6035 |= (CLASSTYPE_NON_AGGREGATE (t)
6036 || saved_nontrivial_dtor || saved_complex_asn_ref);
6037 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
6038 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6039 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6040 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
6042 /* If the only explicitly declared default constructor is user-provided,
6043 set TYPE_HAS_COMPLEX_DFLT. */
6044 if (!TYPE_HAS_COMPLEX_DFLT (t)
6045 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
6046 && !type_has_non_user_provided_default_constructor (t))
6047 TYPE_HAS_COMPLEX_DFLT (t) = true;
6049 /* Warn if a public base of a polymorphic type has an accessible
6050 non-virtual destructor. It is only now that we know the class is
6051 polymorphic. Although a polymorphic base will have a already
6052 been diagnosed during its definition, we warn on use too. */
6053 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
6055 tree binfo = TYPE_BINFO (t);
6056 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
6057 tree base_binfo;
6058 unsigned i;
6060 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6062 tree basetype = TREE_TYPE (base_binfo);
6064 if ((*accesses)[i] == access_public_node
6065 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
6066 && accessible_nvdtor_p (basetype))
6067 warning (OPT_Wnon_virtual_dtor,
6068 "base class %q#T has accessible non-virtual destructor",
6069 basetype);
6073 /* If the class has no user-declared constructor, but does have
6074 non-static const or reference data members that can never be
6075 initialized, issue a warning. */
6076 if (warn_uninitialized
6077 /* Classes with user-declared constructors are presumed to
6078 initialize these members. */
6079 && !TYPE_HAS_USER_CONSTRUCTOR (t)
6080 /* Aggregates can be initialized with brace-enclosed
6081 initializers. */
6082 && CLASSTYPE_NON_AGGREGATE (t))
6084 tree field;
6086 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6088 tree type;
6090 if (TREE_CODE (field) != FIELD_DECL
6091 || DECL_INITIAL (field) != NULL_TREE)
6092 continue;
6094 type = TREE_TYPE (field);
6095 if (TYPE_REF_P (type))
6096 warning_at (DECL_SOURCE_LOCATION (field),
6097 OPT_Wuninitialized, "non-static reference %q#D "
6098 "in class without a constructor", field);
6099 else if (CP_TYPE_CONST_P (type)
6100 && (!CLASS_TYPE_P (type)
6101 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6102 warning_at (DECL_SOURCE_LOCATION (field),
6103 OPT_Wuninitialized, "non-static const member %q#D "
6104 "in class without a constructor", field);
6108 /* Synthesize any needed methods. */
6109 add_implicitly_declared_members (t, &access_decls,
6110 cant_have_const_ctor,
6111 no_const_asn_ref);
6113 /* Check defaulted declarations here so we have cant_have_const_ctor
6114 and don't need to worry about clones. */
6115 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6116 if (DECL_DECLARES_FUNCTION_P (fn)
6117 && !DECL_ARTIFICIAL (fn)
6118 && DECL_DEFAULTED_IN_CLASS_P (fn))
6120 int copy = copy_fn_p (fn);
6121 if (copy > 0)
6123 bool imp_const_p
6124 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6125 : !no_const_asn_ref);
6126 bool fn_const_p = (copy == 2);
6128 if (fn_const_p && !imp_const_p)
6129 /* If the function is defaulted outside the class, we just
6130 give the synthesis error. Core Issue #1331 says this is
6131 no longer ill-formed, it is defined as deleted instead. */
6132 DECL_DELETED_FN (fn) = true;
6134 defaulted_late_check (fn);
6137 if (LAMBDA_TYPE_P (t))
6138 /* "This class type is not an aggregate." */
6139 CLASSTYPE_NON_AGGREGATE (t) = 1;
6141 /* Compute the 'literal type' property before we
6142 do anything with non-static member functions. */
6143 finalize_literal_type_property (t);
6145 /* Create the in-charge and not-in-charge variants of constructors
6146 and destructors. */
6147 clone_constructors_and_destructors (t);
6149 /* Process the using-declarations. */
6150 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6151 handle_using_decl (TREE_VALUE (access_decls), t);
6153 /* Figure out whether or not we will need a cookie when dynamically
6154 allocating an array of this type. */
6155 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6156 = type_requires_array_cookie (t);
6159 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6160 accordingly. If a new vfield was created (because T doesn't have a
6161 primary base class), then the newly created field is returned. It
6162 is not added to the TYPE_FIELDS list; it is the caller's
6163 responsibility to do that. Accumulate declared virtual functions
6164 on VIRTUALS_P. */
6166 static tree
6167 create_vtable_ptr (tree t, tree* virtuals_p)
6169 tree fn;
6171 /* Collect the virtual functions declared in T. */
6172 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6173 if (TREE_CODE (fn) == FUNCTION_DECL
6174 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6175 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6177 tree new_virtual = make_node (TREE_LIST);
6179 BV_FN (new_virtual) = fn;
6180 BV_DELTA (new_virtual) = integer_zero_node;
6181 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6183 TREE_CHAIN (new_virtual) = *virtuals_p;
6184 *virtuals_p = new_virtual;
6187 /* If we couldn't find an appropriate base class, create a new field
6188 here. Even if there weren't any new virtual functions, we might need a
6189 new virtual function table if we're supposed to include vptrs in
6190 all classes that need them. */
6191 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6193 /* We build this decl with vtbl_ptr_type_node, which is a
6194 `vtable_entry_type*'. It might seem more precise to use
6195 `vtable_entry_type (*)[N]' where N is the number of virtual
6196 functions. However, that would require the vtable pointer in
6197 base classes to have a different type than the vtable pointer
6198 in derived classes. We could make that happen, but that
6199 still wouldn't solve all the problems. In particular, the
6200 type-based alias analysis code would decide that assignments
6201 to the base class vtable pointer can't alias assignments to
6202 the derived class vtable pointer, since they have different
6203 types. Thus, in a derived class destructor, where the base
6204 class constructor was inlined, we could generate bad code for
6205 setting up the vtable pointer.
6207 Therefore, we use one type for all vtable pointers. We still
6208 use a type-correct type; it's just doesn't indicate the array
6209 bounds. That's better than using `void*' or some such; it's
6210 cleaner, and it let's the alias analysis code know that these
6211 stores cannot alias stores to void*! */
6212 tree field;
6214 field = build_decl (input_location,
6215 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6216 DECL_VIRTUAL_P (field) = 1;
6217 DECL_ARTIFICIAL (field) = 1;
6218 DECL_FIELD_CONTEXT (field) = t;
6219 DECL_FCONTEXT (field) = t;
6220 if (TYPE_PACKED (t))
6221 DECL_PACKED (field) = 1;
6223 TYPE_VFIELD (t) = field;
6225 /* This class is non-empty. */
6226 CLASSTYPE_EMPTY_P (t) = 0;
6228 return field;
6231 return NULL_TREE;
6234 /* Add OFFSET to all base types of BINFO which is a base in the
6235 hierarchy dominated by T.
6237 OFFSET, which is a type offset, is number of bytes. */
6239 static void
6240 propagate_binfo_offsets (tree binfo, tree offset)
6242 int i;
6243 tree primary_binfo;
6244 tree base_binfo;
6246 /* Update BINFO's offset. */
6247 BINFO_OFFSET (binfo)
6248 = fold_convert (sizetype,
6249 size_binop (PLUS_EXPR,
6250 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6251 offset));
6253 /* Find the primary base class. */
6254 primary_binfo = get_primary_binfo (binfo);
6256 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6257 propagate_binfo_offsets (primary_binfo, offset);
6259 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6260 downwards. */
6261 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6263 /* Don't do the primary base twice. */
6264 if (base_binfo == primary_binfo)
6265 continue;
6267 if (BINFO_VIRTUAL_P (base_binfo))
6268 continue;
6270 propagate_binfo_offsets (base_binfo, offset);
6274 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6275 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6276 empty subobjects of T. */
6278 static void
6279 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6281 tree vbase;
6282 tree t = rli->t;
6283 tree *next_field;
6285 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6286 return;
6288 /* Find the last field. The artificial fields created for virtual
6289 bases will go after the last extant field to date. */
6290 next_field = &TYPE_FIELDS (t);
6291 while (*next_field)
6292 next_field = &DECL_CHAIN (*next_field);
6294 /* Go through the virtual bases, allocating space for each virtual
6295 base that is not already a primary base class. These are
6296 allocated in inheritance graph order. */
6297 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6299 if (!BINFO_VIRTUAL_P (vbase))
6300 continue;
6302 if (!BINFO_PRIMARY_P (vbase))
6304 /* This virtual base is not a primary base of any class in the
6305 hierarchy, so we have to add space for it. */
6306 next_field = build_base_field (rli, vbase,
6307 access_private_node,
6308 offsets, next_field);
6313 /* Returns the offset of the byte just past the end of the base class
6314 BINFO. */
6316 static tree
6317 end_of_base (tree binfo)
6319 tree size;
6321 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6322 size = TYPE_SIZE_UNIT (char_type_node);
6323 else if (is_empty_class (BINFO_TYPE (binfo)))
6324 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6325 allocate some space for it. It cannot have virtual bases, so
6326 TYPE_SIZE_UNIT is fine. */
6327 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6328 else
6329 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6331 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6334 /* Returns the offset of the byte just past the end of the base class or empty
6335 data member with the highest offset in T. If INCLUDE_VIRTUALS_P is zero,
6336 then only non-virtual bases are included. */
6338 static tree
6339 end_of_class (tree t, bool include_virtuals_p)
6341 tree result = size_zero_node;
6342 vec<tree, va_gc> *vbases;
6343 tree binfo;
6344 tree base_binfo;
6345 tree offset;
6346 int i;
6348 for (binfo = TYPE_BINFO (t), i = 0;
6349 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6351 if (!include_virtuals_p
6352 && BINFO_VIRTUAL_P (base_binfo)
6353 && (!BINFO_PRIMARY_P (base_binfo)
6354 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6355 continue;
6357 offset = end_of_base (base_binfo);
6358 if (tree_int_cst_lt (result, offset))
6359 result = offset;
6362 /* Also consider empty data members. */
6363 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6364 if (TREE_CODE (field) == FIELD_DECL
6365 && !DECL_ARTIFICIAL (field)
6366 && field_poverlapping_p (field)
6367 && is_empty_class (TREE_TYPE (field)))
6369 /* Update sizeof(C) to max (sizeof(C), offset(D)+sizeof(D)) */
6370 offset = size_binop (PLUS_EXPR, DECL_FIELD_OFFSET (field),
6371 TYPE_SIZE_UNIT (TREE_TYPE (field)));
6372 if (tree_int_cst_lt (result, offset))
6373 result = offset;
6376 if (include_virtuals_p)
6377 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6378 vec_safe_iterate (vbases, i, &base_binfo); i++)
6380 offset = end_of_base (base_binfo);
6381 if (tree_int_cst_lt (result, offset))
6382 result = offset;
6385 return result;
6388 /* Warn about bases of T that are inaccessible because they are
6389 ambiguous. For example:
6391 struct S {};
6392 struct T : public S {};
6393 struct U : public S, public T {};
6395 Here, `(S*) new U' is not allowed because there are two `S'
6396 subobjects of U. */
6398 static void
6399 maybe_warn_about_inaccessible_bases (tree t)
6401 int i;
6402 vec<tree, va_gc> *vbases;
6403 tree basetype;
6404 tree binfo;
6405 tree base_binfo;
6407 /* If not checking for warning then return early. */
6408 if (!warn_inaccessible_base)
6409 return;
6411 /* If there are no repeated bases, nothing can be ambiguous. */
6412 if (!CLASSTYPE_REPEATED_BASE_P (t))
6413 return;
6415 /* Check direct bases. */
6416 for (binfo = TYPE_BINFO (t), i = 0;
6417 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6419 basetype = BINFO_TYPE (base_binfo);
6421 if (!uniquely_derived_from_p (basetype, t))
6422 warning (OPT_Winaccessible_base, "direct base %qT inaccessible "
6423 "in %qT due to ambiguity", basetype, t);
6426 /* Check for ambiguous virtual bases. */
6427 if (extra_warnings)
6428 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6429 vec_safe_iterate (vbases, i, &binfo); i++)
6431 basetype = BINFO_TYPE (binfo);
6433 if (!uniquely_derived_from_p (basetype, t))
6434 warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in "
6435 "%qT due to ambiguity", basetype, t);
6439 /* Compare two INTEGER_CSTs K1 and K2. */
6441 static int
6442 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6444 return tree_int_cst_compare ((tree) k1, (tree) k2);
6447 /* Increase the size indicated in RLI to account for empty classes
6448 that are "off the end" of the class. */
6450 static void
6451 include_empty_classes (record_layout_info rli)
6453 tree eoc;
6454 tree rli_size;
6456 /* It might be the case that we grew the class to allocate a
6457 zero-sized base class. That won't be reflected in RLI, yet,
6458 because we are willing to overlay multiple bases at the same
6459 offset. However, now we need to make sure that RLI is big enough
6460 to reflect the entire class. */
6461 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6462 rli_size = rli_size_unit_so_far (rli);
6463 if (TREE_CODE (rli_size) == INTEGER_CST
6464 && tree_int_cst_lt (rli_size, eoc))
6466 /* The size should have been rounded to a whole byte. */
6467 gcc_assert (tree_int_cst_equal
6468 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6469 rli->bitpos
6470 = size_binop (PLUS_EXPR,
6471 rli->bitpos,
6472 size_binop (MULT_EXPR,
6473 fold_convert (bitsizetype,
6474 size_binop (MINUS_EXPR,
6475 eoc, rli_size)),
6476 bitsize_int (BITS_PER_UNIT)));
6477 normalize_rli (rli);
6481 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6482 BINFO_OFFSETs for all of the base-classes. Position the vtable
6483 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6485 static void
6486 layout_class_type (tree t, tree *virtuals_p)
6488 tree non_static_data_members;
6489 tree field;
6490 tree vptr;
6491 record_layout_info rli;
6492 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6493 types that appear at that offset. */
6494 splay_tree empty_base_offsets;
6495 /* True if the last field laid out was a bit-field. */
6496 bool last_field_was_bitfield = false;
6497 /* The location at which the next field should be inserted. */
6498 tree *next_field;
6500 /* Keep track of the first non-static data member. */
6501 non_static_data_members = TYPE_FIELDS (t);
6503 /* Start laying out the record. */
6504 rli = start_record_layout (t);
6506 /* Mark all the primary bases in the hierarchy. */
6507 determine_primary_bases (t);
6509 /* Create a pointer to our virtual function table. */
6510 vptr = create_vtable_ptr (t, virtuals_p);
6512 /* The vptr is always the first thing in the class. */
6513 if (vptr)
6515 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6516 TYPE_FIELDS (t) = vptr;
6517 next_field = &DECL_CHAIN (vptr);
6518 place_field (rli, vptr);
6520 else
6521 next_field = &TYPE_FIELDS (t);
6523 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6524 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6525 NULL, NULL);
6526 build_base_fields (rli, empty_base_offsets, next_field);
6528 /* Layout the non-static data members. */
6529 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6531 tree type;
6532 tree padding;
6534 /* We still pass things that aren't non-static data members to
6535 the back end, in case it wants to do something with them. */
6536 if (TREE_CODE (field) != FIELD_DECL)
6538 place_field (rli, field);
6539 /* If the static data member has incomplete type, keep track
6540 of it so that it can be completed later. (The handling
6541 of pending statics in finish_record_layout is
6542 insufficient; consider:
6544 struct S1;
6545 struct S2 { static S1 s1; };
6547 At this point, finish_record_layout will be called, but
6548 S1 is still incomplete.) */
6549 if (VAR_P (field))
6551 maybe_register_incomplete_var (field);
6552 /* The visibility of static data members is determined
6553 at their point of declaration, not their point of
6554 definition. */
6555 determine_visibility (field);
6557 continue;
6560 type = TREE_TYPE (field);
6561 if (type == error_mark_node)
6562 continue;
6564 padding = NULL_TREE;
6566 bool might_overlap = field_poverlapping_p (field);
6568 if (might_overlap && CLASS_TYPE_P (type)
6569 && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type)))
6571 /* if D is a potentially-overlapping data member, update sizeof(C) to
6572 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6573 tree nvsize = CLASSTYPE_SIZE_UNIT (type);
6574 /* end_of_class doesn't always give dsize, but it does in the case of
6575 a class with virtual bases, which is when dsize > nvsize. */
6576 tree dsize = end_of_class (type, /*vbases*/true);
6577 if (CLASSTYPE_EMPTY_P (type))
6578 DECL_SIZE (field) = DECL_SIZE_UNIT (field) = size_zero_node;
6579 else if (tree_int_cst_le (dsize, nvsize))
6581 DECL_SIZE_UNIT (field) = nvsize;
6582 DECL_SIZE (field) = CLASSTYPE_SIZE (type);
6584 else
6586 DECL_SIZE_UNIT (field) = dsize;
6587 DECL_SIZE (field) = bit_from_pos (dsize, bitsize_zero_node);
6591 /* If this field is a bit-field whose width is greater than its
6592 type, then there are some special rules for allocating
6593 it. */
6594 if (DECL_C_BIT_FIELD (field)
6595 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6597 bool was_unnamed_p = false;
6598 /* We must allocate the bits as if suitably aligned for the
6599 longest integer type that fits in this many bits. Then,
6600 we are supposed to use the left over bits as additional
6601 padding. */
6603 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6604 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6605 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6606 limit = DECL_SIZE (field);
6608 tree integer_type = integer_types[itk_char];
6609 for (unsigned itk = itk_char; itk != itk_none; itk++)
6610 if (tree next = integer_types[itk])
6612 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6613 /* Too big, so our current guess is what we want. */
6614 break;
6615 /* Not bigger than limit, ok */
6616 integer_type = next;
6619 /* Figure out how much additional padding is required. */
6620 if (TREE_CODE (t) == UNION_TYPE)
6621 /* In a union, the padding field must have the full width
6622 of the bit-field; all fields start at offset zero. */
6623 padding = DECL_SIZE (field);
6624 else
6625 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6626 TYPE_SIZE (integer_type));
6628 if (integer_zerop (padding))
6629 padding = NULL_TREE;
6631 /* An unnamed bitfield does not normally affect the
6632 alignment of the containing class on a target where
6633 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6634 make any exceptions for unnamed bitfields when the
6635 bitfields are longer than their types. Therefore, we
6636 temporarily give the field a name. */
6637 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6639 was_unnamed_p = true;
6640 DECL_NAME (field) = make_anon_name ();
6643 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6644 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6645 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6646 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6647 empty_base_offsets);
6648 if (was_unnamed_p)
6649 DECL_NAME (field) = NULL_TREE;
6650 /* Now that layout has been performed, set the size of the
6651 field to the size of its declared type; the rest of the
6652 field is effectively invisible. */
6653 DECL_SIZE (field) = TYPE_SIZE (type);
6654 /* We must also reset the DECL_MODE of the field. */
6655 SET_DECL_MODE (field, TYPE_MODE (type));
6657 else if (might_overlap && is_empty_class (type))
6659 DECL_FIELD_ABI_IGNORED (field) = 1;
6660 layout_empty_base_or_field (rli, field, empty_base_offsets);
6662 else
6663 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6664 empty_base_offsets);
6666 /* Remember the location of any empty classes in FIELD. */
6667 record_subobject_offsets (field, empty_base_offsets);
6669 /* If a bit-field does not immediately follow another bit-field,
6670 and yet it starts in the middle of a byte, we have failed to
6671 comply with the ABI. */
6672 if (warn_abi
6673 && DECL_C_BIT_FIELD (field)
6674 /* The TREE_NO_WARNING flag gets set by Objective-C when
6675 laying out an Objective-C class. The ObjC ABI differs
6676 from the C++ ABI, and so we do not want a warning
6677 here. */
6678 && !warning_suppressed_p (field, OPT_Wabi)
6679 && !last_field_was_bitfield
6680 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6681 DECL_FIELD_BIT_OFFSET (field),
6682 bitsize_unit_node)))
6683 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6684 "offset of %qD is not ABI-compliant and may "
6685 "change in a future version of GCC", field);
6687 /* The middle end uses the type of expressions to determine the
6688 possible range of expression values. In order to optimize
6689 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6690 must be made aware of the width of "i", via its type.
6692 Because C++ does not have integer types of arbitrary width,
6693 we must (for the purposes of the front end) convert from the
6694 type assigned here to the declared type of the bitfield
6695 whenever a bitfield expression is used as an rvalue.
6696 Similarly, when assigning a value to a bitfield, the value
6697 must be converted to the type given the bitfield here. */
6698 if (DECL_C_BIT_FIELD (field))
6700 unsigned HOST_WIDE_INT width;
6701 tree ftype = TREE_TYPE (field);
6702 width = tree_to_uhwi (DECL_SIZE (field));
6703 if (width != TYPE_PRECISION (ftype))
6705 TREE_TYPE (field)
6706 = c_build_bitfield_integer_type (width,
6707 TYPE_UNSIGNED (ftype));
6708 TREE_TYPE (field)
6709 = cp_build_qualified_type (TREE_TYPE (field),
6710 cp_type_quals (ftype));
6714 /* If we needed additional padding after this field, add it
6715 now. */
6716 if (padding)
6718 tree padding_field;
6720 padding_field = build_decl (input_location,
6721 FIELD_DECL,
6722 NULL_TREE,
6723 char_type_node);
6724 DECL_BIT_FIELD (padding_field) = 1;
6725 DECL_SIZE (padding_field) = padding;
6726 DECL_CONTEXT (padding_field) = t;
6727 DECL_ARTIFICIAL (padding_field) = 1;
6728 DECL_IGNORED_P (padding_field) = 1;
6729 DECL_PADDING_P (padding_field) = 1;
6730 layout_nonempty_base_or_field (rli, padding_field,
6731 NULL_TREE,
6732 empty_base_offsets);
6735 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6738 if (!integer_zerop (rli->bitpos))
6740 /* Make sure that we are on a byte boundary so that the size of
6741 the class without virtual bases will always be a round number
6742 of bytes. */
6743 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6744 normalize_rli (rli);
6747 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6749 /* T needs a different layout as a base (eliding virtual bases
6750 or whatever). Create that version. */
6751 tree base_t = make_node (TREE_CODE (t));
6752 tree base_d = create_implicit_typedef (as_base_identifier, base_t);
6754 TYPE_CONTEXT (base_t) = t;
6755 DECL_CONTEXT (base_d) = t;
6757 set_instantiating_module (base_d);
6759 /* If the ABI version is not at least two, and the last
6760 field was a bit-field, RLI may not be on a byte
6761 boundary. In particular, rli_size_unit_so_far might
6762 indicate the last complete byte, while rli_size_so_far
6763 indicates the total number of bits used. Therefore,
6764 rli_size_so_far, rather than rli_size_unit_so_far, is
6765 used to compute TYPE_SIZE_UNIT. */
6767 /* Set the size and alignment for the new type. */
6768 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6769 TYPE_SIZE_UNIT (base_t)
6770 = size_binop (MAX_EXPR,
6771 fold_convert (sizetype,
6772 size_binop (CEIL_DIV_EXPR,
6773 rli_size_so_far (rli),
6774 bitsize_int (BITS_PER_UNIT))),
6775 eoc);
6776 TYPE_SIZE (base_t)
6777 = size_binop (MAX_EXPR,
6778 rli_size_so_far (rli),
6779 size_binop (MULT_EXPR,
6780 fold_convert (bitsizetype, eoc),
6781 bitsize_int (BITS_PER_UNIT)));
6782 SET_TYPE_ALIGN (base_t, rli->record_align);
6783 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6784 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
6785 TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t);
6787 /* Copy the non-static data members of T. This will include its
6788 direct non-virtual bases & vtable. */
6789 next_field = &TYPE_FIELDS (base_t);
6790 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6791 if (TREE_CODE (field) == FIELD_DECL)
6793 *next_field = copy_node (field);
6794 /* Zap any NSDMI, it's not needed and might be a deferred
6795 parse. */
6796 DECL_INITIAL (*next_field) = NULL_TREE;
6797 DECL_CONTEXT (*next_field) = base_t;
6798 next_field = &DECL_CHAIN (*next_field);
6800 *next_field = NULL_TREE;
6802 /* We use the base type for trivial assignments, and hence it
6803 needs a mode. */
6804 compute_record_mode (base_t);
6806 /* Record the base version of the type. */
6807 CLASSTYPE_AS_BASE (t) = base_t;
6809 else
6810 CLASSTYPE_AS_BASE (t) = t;
6812 /* Every empty class contains an empty class. */
6813 if (CLASSTYPE_EMPTY_P (t))
6814 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6816 /* Set the TYPE_DECL for this type to contain the right
6817 value for DECL_OFFSET, so that we can use it as part
6818 of a COMPONENT_REF for multiple inheritance. */
6819 layout_decl (TYPE_MAIN_DECL (t), 0);
6821 /* Now fix up any virtual base class types that we left lying
6822 around. We must get these done before we try to lay out the
6823 virtual function table. As a side-effect, this will remove the
6824 base subobject fields. */
6825 layout_virtual_bases (rli, empty_base_offsets);
6827 /* Make sure that empty classes are reflected in RLI at this
6828 point. */
6829 include_empty_classes (rli);
6831 /* Make sure not to create any structures with zero size. */
6832 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6833 place_field (rli,
6834 build_decl (input_location,
6835 FIELD_DECL, NULL_TREE, char_type_node));
6837 /* If this is a non-POD, declaring it packed makes a difference to how it
6838 can be used as a field; don't let finalize_record_size undo it. */
6839 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6840 rli->packed_maybe_necessary = true;
6842 /* Let the back end lay out the type. */
6843 finish_record_layout (rli, /*free_p=*/true);
6845 /* If we didn't end up needing an as-base type, don't use it. */
6846 if (CLASSTYPE_AS_BASE (t) != t
6847 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
6848 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
6849 causing us to lose the user-specified alignment as in PR94050. */
6850 && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t))
6851 && tree_int_cst_equal (TYPE_SIZE (t),
6852 TYPE_SIZE (CLASSTYPE_AS_BASE (t))))
6853 CLASSTYPE_AS_BASE (t) = t;
6855 if (TYPE_SIZE_UNIT (t)
6856 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6857 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6858 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6859 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6861 /* Warn about bases that can't be talked about due to ambiguity. */
6862 maybe_warn_about_inaccessible_bases (t);
6864 /* Now that we're done with layout, give the base fields the real types. */
6865 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6866 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6867 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6869 /* Clean up. */
6870 splay_tree_delete (empty_base_offsets);
6872 if (CLASSTYPE_EMPTY_P (t)
6873 && tree_int_cst_lt (sizeof_biggest_empty_class,
6874 TYPE_SIZE_UNIT (t)))
6875 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6878 /* Determine the "key method" for the class type indicated by TYPE,
6879 and set CLASSTYPE_KEY_METHOD accordingly. */
6881 void
6882 determine_key_method (tree type)
6884 tree method;
6886 if (processing_template_decl
6887 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6888 || CLASSTYPE_INTERFACE_KNOWN (type))
6889 return;
6891 /* The key method is the first non-pure virtual function that is not
6892 inline at the point of class definition. On some targets the
6893 key function may not be inline; those targets should not call
6894 this function until the end of the translation unit. */
6895 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6896 if (TREE_CODE (method) == FUNCTION_DECL
6897 && DECL_VINDEX (method) != NULL_TREE
6898 && ! DECL_DECLARED_INLINE_P (method)
6899 && ! DECL_PURE_VIRTUAL_P (method))
6901 CLASSTYPE_KEY_METHOD (type) = method;
6902 break;
6905 return;
6908 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6909 class data member of non-zero size, otherwise false. */
6911 static inline bool
6912 field_nonempty_p (const_tree fld)
6914 if (TREE_CODE (fld) == ERROR_MARK)
6915 return false;
6917 tree type = TREE_TYPE (fld);
6918 if (TREE_CODE (fld) == FIELD_DECL
6919 && TREE_CODE (type) != ERROR_MARK
6920 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6922 return TYPE_SIZE (type)
6923 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6924 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6927 return false;
6930 /* Used by find_flexarrays and related functions. */
6932 struct flexmems_t
6934 /* The first flexible array member or non-zero array member found
6935 in the order of layout. */
6936 tree array;
6937 /* First non-static non-empty data member in the class or its bases. */
6938 tree first;
6939 /* The first non-static non-empty data member following either
6940 the flexible array member, if found, or the zero-length array member
6941 otherwise. AFTER[1] refers to the first such data member of a union
6942 of which the struct containing the flexible array member or zero-length
6943 array is a member, or NULL when no such union exists. This element is
6944 only used during searching, not for diagnosing problems. AFTER[0]
6945 refers to the first such data member that is not a member of such
6946 a union. */
6947 tree after[2];
6949 /* Refers to a struct (not union) in which the struct of which the flexible
6950 array is member is defined. Used to diagnose strictly (according to C)
6951 invalid uses of the latter structs. */
6952 tree enclosing;
6955 /* Find either the first flexible array member or the first zero-length
6956 array, in that order of preference, among members of class T (but not
6957 its base classes), and set members of FMEM accordingly.
6958 BASE_P is true if T is a base class of another class.
6959 PUN is set to the outermost union in which the flexible array member
6960 (or zero-length array) is defined if one such union exists, otherwise
6961 to NULL.
6962 Similarly, PSTR is set to a data member of the outermost struct of
6963 which the flexible array is a member if one such struct exists,
6964 otherwise to NULL. */
6966 static void
6967 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6968 tree pun /* = NULL_TREE */,
6969 tree pstr /* = NULL_TREE */)
6971 /* Set the "pointer" to the outermost enclosing union if not set
6972 yet and maintain it for the remainder of the recursion. */
6973 if (!pun && TREE_CODE (t) == UNION_TYPE)
6974 pun = t;
6976 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6978 if (fld == error_mark_node)
6979 return;
6981 /* Is FLD a typedef for an anonymous struct? */
6983 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6984 handled elsewhere so that errors like the following are detected
6985 as well:
6986 typedef struct { int i, a[], j; } S; // bug c++/72753
6987 S s [2]; // bug c++/68489
6989 if (TREE_CODE (fld) == TYPE_DECL
6990 && DECL_IMPLICIT_TYPEDEF_P (fld)
6991 && CLASS_TYPE_P (TREE_TYPE (fld))
6992 && IDENTIFIER_ANON_P (DECL_NAME (fld)))
6994 /* Check the nested unnamed type referenced via a typedef
6995 independently of FMEM (since it's not a data member of
6996 the enclosing class). */
6997 check_flexarrays (TREE_TYPE (fld));
6998 continue;
7001 /* Skip anything that's GCC-generated or not a (non-static) data
7002 member. */
7003 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
7004 continue;
7006 /* Type of the member. */
7007 tree fldtype = TREE_TYPE (fld);
7008 if (fldtype == error_mark_node)
7009 return;
7011 /* Determine the type of the array element or object referenced
7012 by the member so that it can be checked for flexible array
7013 members if it hasn't been yet. */
7014 tree eltype = fldtype;
7015 while (TREE_CODE (eltype) == ARRAY_TYPE
7016 || INDIRECT_TYPE_P (eltype))
7017 eltype = TREE_TYPE (eltype);
7019 if (RECORD_OR_UNION_TYPE_P (eltype))
7021 if (fmem->array && !fmem->after[bool (pun)])
7023 /* Once the member after the flexible array has been found
7024 we're done. */
7025 fmem->after[bool (pun)] = fld;
7026 break;
7029 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
7031 /* Descend into the non-static member struct or union and try
7032 to find a flexible array member or zero-length array among
7033 its members. This is only necessary for anonymous types
7034 and types in whose context the current type T has not been
7035 defined (the latter must not be checked again because they
7036 are already in the process of being checked by one of the
7037 recursive calls). */
7039 tree first = fmem->first;
7040 tree array = fmem->array;
7042 /* If this member isn't anonymous and a prior non-flexible array
7043 member has been seen in one of the enclosing structs, clear
7044 the FIRST member since it doesn't contribute to the flexible
7045 array struct's members. */
7046 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7047 fmem->first = NULL_TREE;
7049 find_flexarrays (eltype, fmem, false, pun,
7050 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
7052 if (fmem->array != array)
7053 continue;
7055 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7057 /* Restore the FIRST member reset above if no flexible
7058 array member has been found in this member's struct. */
7059 fmem->first = first;
7062 /* If the member struct contains the first flexible array
7063 member, or if this member is a base class, continue to
7064 the next member and avoid setting the FMEM->NEXT pointer
7065 to point to it. */
7066 if (base_p)
7067 continue;
7071 if (field_nonempty_p (fld))
7073 /* Remember the first non-static data member. */
7074 if (!fmem->first)
7075 fmem->first = fld;
7077 /* Remember the first non-static data member after the flexible
7078 array member, if one has been found, or the zero-length array
7079 if it has been found. */
7080 if (fmem->array && !fmem->after[bool (pun)])
7081 fmem->after[bool (pun)] = fld;
7084 /* Skip non-arrays. */
7085 if (TREE_CODE (fldtype) != ARRAY_TYPE)
7086 continue;
7088 /* Determine the upper bound of the array if it has one. */
7089 if (TYPE_DOMAIN (fldtype))
7091 if (fmem->array)
7093 /* Make a record of the zero-length array if either one
7094 such field or a flexible array member has been seen to
7095 handle the pathological and unlikely case of multiple
7096 such members. */
7097 if (!fmem->after[bool (pun)])
7098 fmem->after[bool (pun)] = fld;
7100 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7102 /* Remember the first zero-length array unless a flexible array
7103 member has already been seen. */
7104 fmem->array = fld;
7105 fmem->enclosing = pstr;
7108 else
7110 /* Flexible array members have no upper bound. */
7111 if (fmem->array)
7113 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7115 /* Replace the zero-length array if it's been stored and
7116 reset the after pointer. */
7117 fmem->after[bool (pun)] = NULL_TREE;
7118 fmem->array = fld;
7119 fmem->enclosing = pstr;
7121 else if (!fmem->after[bool (pun)])
7122 /* Make a record of another flexible array member. */
7123 fmem->after[bool (pun)] = fld;
7125 else
7127 fmem->array = fld;
7128 fmem->enclosing = pstr;
7134 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7135 a flexible array member (or the zero-length array extension). */
7137 static void
7138 diagnose_invalid_flexarray (const flexmems_t *fmem)
7140 if (fmem->array && fmem->enclosing)
7142 auto_diagnostic_group d;
7143 if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
7144 TYPE_DOMAIN (TREE_TYPE (fmem->array))
7145 ? G_("invalid use of %q#T with a zero-size array "
7146 "in %q#D")
7147 : G_("invalid use of %q#T with a flexible array member "
7148 "in %q#T"),
7149 DECL_CONTEXT (fmem->array),
7150 DECL_CONTEXT (fmem->enclosing)))
7151 inform (DECL_SOURCE_LOCATION (fmem->array),
7152 "array member %q#D declared here", fmem->array);
7156 /* Issue diagnostics for invalid flexible array members or zero-length
7157 arrays that are not the last elements of the containing class or its
7158 base classes or that are its sole members. */
7160 static void
7161 diagnose_flexarrays (tree t, const flexmems_t *fmem)
7163 if (!fmem->array)
7164 return;
7166 if (fmem->first && !fmem->after[0])
7168 diagnose_invalid_flexarray (fmem);
7169 return;
7172 /* Has a diagnostic been issued? */
7173 bool diagd = false;
7175 const char *msg = 0;
7177 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7179 if (fmem->after[0])
7180 msg = G_("zero-size array member %qD not at end of %q#T");
7181 else if (!fmem->first)
7182 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7184 if (msg)
7186 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7188 auto_diagnostic_group d;
7189 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7191 inform (location_of (t), "in the definition of %q#T", t);
7192 diagd = true;
7196 else
7198 if (fmem->after[0])
7199 msg = G_("flexible array member %qD not at end of %q#T");
7200 else if (!fmem->first)
7201 msg = G_("flexible array member %qD in an otherwise empty %q#T");
7203 if (msg)
7205 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7206 diagd = true;
7208 auto_diagnostic_group d;
7209 error_at (loc, msg, fmem->array, t);
7211 /* In the unlikely event that the member following the flexible
7212 array member is declared in a different class, or the member
7213 overlaps another member of a common union, point to it.
7214 Otherwise it should be obvious. */
7215 if (fmem->after[0]
7216 && ((DECL_CONTEXT (fmem->after[0])
7217 != DECL_CONTEXT (fmem->array))))
7219 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7220 "next member %q#D declared here",
7221 fmem->after[0]);
7222 inform (location_of (t), "in the definition of %q#T", t);
7227 if (!diagd && fmem->array && fmem->enclosing)
7228 diagnose_invalid_flexarray (fmem);
7232 /* Recursively check to make sure that any flexible array or zero-length
7233 array members of class T or its bases are valid (i.e., not the sole
7234 non-static data member of T and, if one exists, that it is the last
7235 non-static data member of T and its base classes. FMEM is expected
7236 to be initially null and is used internally by recursive calls to
7237 the function. Issue the appropriate diagnostics for the array member
7238 that fails the checks. */
7240 static void
7241 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7242 bool base_p /* = false */)
7244 /* Initialize the result of a search for flexible array and zero-length
7245 array members. Avoid doing any work if the most interesting FMEM data
7246 have already been populated. */
7247 flexmems_t flexmems = flexmems_t ();
7248 if (!fmem)
7249 fmem = &flexmems;
7250 else if (fmem->array && fmem->first && fmem->after[0])
7251 return;
7253 tree fam = fmem->array;
7255 /* Recursively check the primary base class first. */
7256 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7258 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7259 check_flexarrays (basetype, fmem, true);
7262 /* Recursively check the base classes. */
7263 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7264 for (int i = 0; i < nbases; ++i)
7266 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7268 /* The primary base class was already checked above. */
7269 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7270 continue;
7272 /* Virtual base classes are at the end. */
7273 if (BINFO_VIRTUAL_P (base_binfo))
7274 continue;
7276 /* Check the base class. */
7277 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7280 if (fmem == &flexmems)
7282 /* Check virtual base classes only once per derived class.
7283 I.e., this check is not performed recursively for base
7284 classes. */
7285 int i;
7286 tree base_binfo;
7287 vec<tree, va_gc> *vbases;
7288 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7289 vec_safe_iterate (vbases, i, &base_binfo); i++)
7291 /* Check the virtual base class. */
7292 tree basetype = TREE_TYPE (base_binfo);
7294 check_flexarrays (basetype, fmem, /*base_p=*/true);
7298 /* Is the type unnamed (and therefore a member of it potentially
7299 an anonymous struct or union)? */
7300 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7301 if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE)
7302 maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7304 /* Search the members of the current (possibly derived) class, skipping
7305 unnamed structs and unions since those could be anonymous. */
7306 if (fmem != &flexmems || !maybe_anon_p)
7307 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7309 if (fmem == &flexmems && !maybe_anon_p)
7311 /* Issue diagnostics for invalid flexible and zero-length array
7312 members found in base classes or among the members of the current
7313 class. Ignore anonymous structs and unions whose members are
7314 considered to be members of the enclosing class and thus will
7315 be diagnosed when checking it. */
7316 diagnose_flexarrays (t, fmem);
7320 /* Perform processing required when the definition of T (a class type)
7321 is complete. Diagnose invalid definitions of flexible array members
7322 and zero-size arrays. */
7324 void
7325 finish_struct_1 (tree t)
7327 tree x;
7328 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7329 tree virtuals = NULL_TREE;
7331 if (COMPLETE_TYPE_P (t))
7333 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7334 error ("redefinition of %q#T", t);
7335 popclass ();
7336 return;
7339 /* If this type was previously laid out as a forward reference,
7340 make sure we lay it out again. */
7341 TYPE_SIZE (t) = NULL_TREE;
7342 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7344 /* Make assumptions about the class; we'll reset the flags if
7345 necessary. */
7346 CLASSTYPE_EMPTY_P (t) = 1;
7347 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7348 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7349 CLASSTYPE_LITERAL_P (t) = true;
7351 /* Do end-of-class semantic processing: checking the validity of the
7352 bases and members and add implicitly generated methods. */
7353 check_bases_and_members (t);
7355 /* Find the key method. */
7356 if (TYPE_CONTAINS_VPTR_P (t))
7358 /* The Itanium C++ ABI permits the key method to be chosen when
7359 the class is defined -- even though the key method so
7360 selected may later turn out to be an inline function. On
7361 some systems (such as ARM Symbian OS) the key method cannot
7362 be determined until the end of the translation unit. On such
7363 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7364 will cause the class to be added to KEYED_CLASSES. Then, in
7365 finish_file we will determine the key method. */
7366 if (targetm.cxx.key_method_may_be_inline ())
7367 determine_key_method (t);
7369 /* If a polymorphic class has no key method, we may emit the vtable
7370 in every translation unit where the class definition appears. If
7371 we're devirtualizing, we can look into the vtable even if we
7372 aren't emitting it. */
7373 if (!CLASSTYPE_KEY_METHOD (t))
7374 vec_safe_push (keyed_classes, t);
7377 /* Layout the class itself. */
7378 layout_class_type (t, &virtuals);
7379 /* COMPLETE_TYPE_P is now true. */
7381 set_class_bindings (t);
7383 /* With the layout complete, check for flexible array members and
7384 zero-length arrays that might overlap other members in the final
7385 layout. */
7386 check_flexarrays (t);
7388 virtuals = modify_all_vtables (t, nreverse (virtuals));
7390 /* If necessary, create the primary vtable for this class. */
7391 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7393 /* We must enter these virtuals into the table. */
7394 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7395 build_primary_vtable (NULL_TREE, t);
7396 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7397 /* Here we know enough to change the type of our virtual
7398 function table, but we will wait until later this function. */
7399 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7401 /* If we're warning about ABI tags, check the types of the new
7402 virtual functions. */
7403 if (warn_abi_tag)
7404 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7405 check_abi_tags (t, TREE_VALUE (v));
7408 if (TYPE_CONTAINS_VPTR_P (t))
7410 int vindex;
7411 tree fn;
7413 if (BINFO_VTABLE (TYPE_BINFO (t)))
7414 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7415 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7416 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7418 /* Add entries for virtual functions introduced by this class. */
7419 BINFO_VIRTUALS (TYPE_BINFO (t))
7420 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7422 /* Set DECL_VINDEX for all functions declared in this class. */
7423 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7425 fn = TREE_CHAIN (fn),
7426 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7427 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7429 tree fndecl = BV_FN (fn);
7431 if (DECL_THUNK_P (fndecl))
7432 /* A thunk. We should never be calling this entry directly
7433 from this vtable -- we'd use the entry for the non
7434 thunk base function. */
7435 DECL_VINDEX (fndecl) = NULL_TREE;
7436 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7437 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7441 finish_struct_bits (t);
7443 set_method_tm_attributes (t);
7444 if (flag_openmp || flag_openmp_simd)
7445 finish_omp_declare_simd_methods (t);
7447 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7448 for any static member objects of the type we're working on. */
7449 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7450 if (DECL_DECLARES_FUNCTION_P (x))
7451 DECL_IN_AGGR_P (x) = false;
7452 else if (VAR_P (x) && TREE_STATIC (x)
7453 && TREE_TYPE (x) != error_mark_node
7454 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7455 SET_DECL_MODE (x, TYPE_MODE (t));
7457 /* Complain if one of the field types requires lower visibility. */
7458 constrain_class_visibility (t);
7460 /* Make the rtl for any new vtables we have created, and unmark
7461 the base types we marked. */
7462 finish_vtbls (t);
7464 /* Build the VTT for T. */
7465 build_vtt (t);
7467 if (warn_nonvdtor
7468 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7469 && !CLASSTYPE_FINAL (t))
7470 warning (OPT_Wnon_virtual_dtor,
7471 "%q#T has virtual functions and accessible"
7472 " non-virtual destructor", t);
7474 complete_vars (t);
7476 if (warn_overloaded_virtual)
7477 warn_hidden (t);
7479 /* Class layout, assignment of virtual table slots, etc., is now
7480 complete. Give the back end a chance to tweak the visibility of
7481 the class or perform any other required target modifications. */
7482 targetm.cxx.adjust_class_at_definition (t);
7484 maybe_suppress_debug_info (t);
7486 if (flag_vtable_verify)
7487 vtv_save_class_info (t);
7489 dump_class_hierarchy (t);
7491 /* Finish debugging output for this type. */
7492 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7494 if (TYPE_TRANSPARENT_AGGR (t))
7496 tree field = first_field (t);
7497 if (field == NULL_TREE || error_operand_p (field))
7499 error ("type transparent %q#T does not have any fields", t);
7500 TYPE_TRANSPARENT_AGGR (t) = 0;
7502 else if (DECL_ARTIFICIAL (field))
7504 if (DECL_FIELD_IS_BASE (field))
7505 error ("type transparent class %qT has base classes", t);
7506 else
7508 gcc_checking_assert (DECL_VIRTUAL_P (field));
7509 error ("type transparent class %qT has virtual functions", t);
7511 TYPE_TRANSPARENT_AGGR (t) = 0;
7513 else if (TYPE_MODE (t) != DECL_MODE (field))
7515 error ("type transparent %q#T cannot be made transparent because "
7516 "the type of the first field has a different ABI from the "
7517 "class overall", t);
7518 TYPE_TRANSPARENT_AGGR (t) = 0;
7523 /* When T was built up, the member declarations were added in reverse
7524 order. Rearrange them to declaration order. */
7526 void
7527 unreverse_member_declarations (tree t)
7529 tree next;
7530 tree prev;
7531 tree x;
7533 /* The following lists are all in reverse order. Put them in
7534 declaration order now. */
7535 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7537 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7538 order, so we can't just use nreverse. Due to stat_hack
7539 chicanery in finish_member_declaration. */
7540 prev = NULL_TREE;
7541 for (x = TYPE_FIELDS (t);
7542 x && TREE_CODE (x) != TYPE_DECL;
7543 x = next)
7545 next = DECL_CHAIN (x);
7546 DECL_CHAIN (x) = prev;
7547 prev = x;
7550 if (prev)
7552 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7553 TYPE_FIELDS (t) = prev;
7557 tree
7558 finish_struct (tree t, tree attributes)
7560 location_t saved_loc = input_location;
7562 /* Now that we've got all the field declarations, reverse everything
7563 as necessary. */
7564 unreverse_member_declarations (t);
7566 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7567 fixup_attribute_variants (t);
7569 /* Nadger the current location so that diagnostics point to the start of
7570 the struct, not the end. */
7571 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7573 if (processing_template_decl)
7575 tree x;
7577 /* We need to add the target functions of USING_DECLS, so that
7578 they can be found when the using declaration is not
7579 instantiated yet. */
7580 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7581 if (TREE_CODE (x) == USING_DECL)
7583 tree fn = strip_using_decl (x);
7584 if (OVL_P (fn))
7585 for (lkp_iterator iter (fn); iter; ++iter)
7586 add_method (t, *iter, true);
7588 else if (DECL_DECLARES_FUNCTION_P (x))
7590 DECL_IN_AGGR_P (x) = false;
7591 if (DECL_VIRTUAL_P (x))
7592 CLASSTYPE_NON_AGGREGATE (t) = true;
7594 else if (TREE_CODE (x) == FIELD_DECL)
7596 if (TREE_PROTECTED (x) || TREE_PRIVATE (x))
7597 CLASSTYPE_NON_AGGREGATE (t) = true;
7600 /* Also add a USING_DECL for operator=. We know there'll be (at
7601 least) one, but we don't know the signature(s). We want name
7602 lookup not to fail or recurse into bases. This isn't added
7603 to the template decl list so we drop this at instantiation
7604 time. */
7605 tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
7606 NULL_TREE);
7607 DECL_CONTEXT (ass_op) = t;
7608 USING_DECL_SCOPE (ass_op) = t;
7609 DECL_DEPENDENT_P (ass_op) = true;
7610 DECL_ARTIFICIAL (ass_op) = true;
7611 DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
7612 TYPE_FIELDS (t) = ass_op;
7614 TYPE_SIZE (t) = bitsize_zero_node;
7615 TYPE_SIZE_UNIT (t) = size_zero_node;
7616 /* COMPLETE_TYPE_P is now true. */
7618 set_class_bindings (t);
7620 /* We need to emit an error message if this type was used as a parameter
7621 and it is an abstract type, even if it is a template. We construct
7622 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7623 account and we call complete_vars with this type, which will check
7624 the PARM_DECLS. Note that while the type is being defined,
7625 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7626 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7627 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7628 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7629 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7630 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7631 complete_vars (t);
7633 /* Remember current #pragma pack value. */
7634 TYPE_PRECISION (t) = maximum_field_alignment;
7636 if (cxx_dialect < cxx20)
7638 if (!CLASSTYPE_NON_AGGREGATE (t)
7639 && type_has_user_provided_or_explicit_constructor (t))
7640 CLASSTYPE_NON_AGGREGATE (t) = 1;
7642 else if (TYPE_HAS_USER_CONSTRUCTOR (t))
7643 CLASSTYPE_NON_AGGREGATE (t) = 1;
7645 /* Fix up any variants we've already built. */
7646 fixup_type_variants (t);
7648 else
7649 finish_struct_1 (t);
7650 /* COMPLETE_TYPE_P is now true. */
7652 maybe_warn_about_overly_private_class (t);
7654 if (is_std_init_list (t))
7656 /* People keep complaining that the compiler crashes on an invalid
7657 definition of initializer_list, so I guess we should explicitly
7658 reject it. What the compiler internals care about is that it's a
7659 template and has a pointer field followed by size_type field. */
7660 bool ok = false;
7661 if (processing_template_decl)
7663 tree f = next_initializable_field (TYPE_FIELDS (t));
7664 if (f && TYPE_PTR_P (TREE_TYPE (f)))
7666 f = next_initializable_field (DECL_CHAIN (f));
7667 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7668 ok = true;
7671 if (!ok)
7672 fatal_error (input_location, "definition of %qD does not match "
7673 "%<#include <initializer_list>%>", TYPE_NAME (t));
7676 input_location = saved_loc;
7678 TYPE_BEING_DEFINED (t) = 0;
7680 if (current_class_type)
7681 popclass ();
7682 else
7683 error ("trying to finish struct, but kicked out due to previous parse errors");
7685 if (flag_openmp)
7686 for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl))
7687 if (TREE_CODE (decl) == FUNCTION_DECL
7688 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
7689 if (tree attr = lookup_attribute ("omp declare variant base",
7690 DECL_ATTRIBUTES (decl)))
7691 omp_declare_variant_finalize (decl, attr);
7693 if (processing_template_decl && at_function_scope_p ()
7694 /* Lambdas are defined by the LAMBDA_EXPR. */
7695 && !LAMBDA_TYPE_P (t))
7696 add_stmt (build_min (TAG_DEFN, t));
7698 return t;
7701 /* Hash table to avoid endless recursion when handling references. */
7702 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7704 /* Return the dynamic type of INSTANCE, if known.
7705 Used to determine whether the virtual function table is needed
7706 or not.
7708 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7709 of our knowledge of its type. *NONNULL should be initialized
7710 before this function is called. */
7712 static tree
7713 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7715 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7717 switch (TREE_CODE (instance))
7719 case INDIRECT_REF:
7720 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
7721 return NULL_TREE;
7722 else
7723 return RECUR (TREE_OPERAND (instance, 0));
7725 case CALL_EXPR:
7726 /* This is a call to a constructor, hence it's never zero. */
7727 if (CALL_EXPR_FN (instance)
7728 && TREE_HAS_CONSTRUCTOR (instance))
7730 if (nonnull)
7731 *nonnull = 1;
7732 return TREE_TYPE (instance);
7734 return NULL_TREE;
7736 case SAVE_EXPR:
7737 /* This is a call to a constructor, hence it's never zero. */
7738 if (TREE_HAS_CONSTRUCTOR (instance))
7740 if (nonnull)
7741 *nonnull = 1;
7742 return TREE_TYPE (instance);
7744 return RECUR (TREE_OPERAND (instance, 0));
7746 case POINTER_PLUS_EXPR:
7747 case PLUS_EXPR:
7748 case MINUS_EXPR:
7749 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7750 return RECUR (TREE_OPERAND (instance, 0));
7751 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7752 /* Propagate nonnull. */
7753 return RECUR (TREE_OPERAND (instance, 0));
7755 return NULL_TREE;
7757 CASE_CONVERT:
7758 return RECUR (TREE_OPERAND (instance, 0));
7760 case ADDR_EXPR:
7761 instance = TREE_OPERAND (instance, 0);
7762 if (nonnull)
7764 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7765 with a real object -- given &p->f, p can still be null. */
7766 tree t = get_base_address (instance);
7767 /* ??? Probably should check DECL_WEAK here. */
7768 if (t && DECL_P (t))
7769 *nonnull = 1;
7771 return RECUR (instance);
7773 case COMPONENT_REF:
7774 /* If this component is really a base class reference, then the field
7775 itself isn't definitive. */
7776 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7777 return RECUR (TREE_OPERAND (instance, 0));
7778 return RECUR (TREE_OPERAND (instance, 1));
7780 case VAR_DECL:
7781 case FIELD_DECL:
7782 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7783 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7785 if (nonnull)
7786 *nonnull = 1;
7787 return TREE_TYPE (TREE_TYPE (instance));
7789 /* fall through. */
7790 case TARGET_EXPR:
7791 case PARM_DECL:
7792 case RESULT_DECL:
7793 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7795 if (nonnull)
7796 *nonnull = 1;
7797 return TREE_TYPE (instance);
7799 else if (instance == current_class_ptr)
7801 if (nonnull)
7802 *nonnull = 1;
7804 /* if we're in a ctor or dtor, we know our type. If
7805 current_class_ptr is set but we aren't in a function, we're in
7806 an NSDMI (and therefore a constructor). */
7807 if (current_scope () != current_function_decl
7808 || (DECL_LANG_SPECIFIC (current_function_decl)
7809 && (DECL_CONSTRUCTOR_P (current_function_decl)
7810 || DECL_DESTRUCTOR_P (current_function_decl))))
7812 if (cdtorp)
7813 *cdtorp = 1;
7814 return TREE_TYPE (TREE_TYPE (instance));
7817 else if (TYPE_REF_P (TREE_TYPE (instance)))
7819 /* We only need one hash table because it is always left empty. */
7820 if (!fixed_type_or_null_ref_ht)
7821 fixed_type_or_null_ref_ht
7822 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7824 /* Reference variables should be references to objects. */
7825 if (nonnull)
7826 *nonnull = 1;
7828 /* Enter the INSTANCE in a table to prevent recursion; a
7829 variable's initializer may refer to the variable
7830 itself. */
7831 if (VAR_P (instance)
7832 && DECL_INITIAL (instance)
7833 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7834 && !fixed_type_or_null_ref_ht->find (instance))
7836 tree type;
7837 tree_node **slot;
7839 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7840 *slot = instance;
7841 type = RECUR (DECL_INITIAL (instance));
7842 fixed_type_or_null_ref_ht->remove_elt (instance);
7844 return type;
7847 return NULL_TREE;
7849 case VIEW_CONVERT_EXPR:
7850 if (location_wrapper_p (instance))
7851 return RECUR (TREE_OPERAND (instance, 0));
7852 else
7853 /* TODO: Recursion may be correct for some non-location-wrapper
7854 uses of VIEW_CONVERT_EXPR. */
7855 return NULL_TREE;
7857 default:
7858 return NULL_TREE;
7860 #undef RECUR
7863 /* Return nonzero if the dynamic type of INSTANCE is known, and
7864 equivalent to the static type. We also handle the case where
7865 INSTANCE is really a pointer. Return negative if this is a
7866 ctor/dtor. There the dynamic type is known, but this might not be
7867 the most derived base of the original object, and hence virtual
7868 bases may not be laid out according to this type.
7870 Used to determine whether the virtual function table is needed
7871 or not.
7873 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7874 of our knowledge of its type. *NONNULL should be initialized
7875 before this function is called. */
7878 resolves_to_fixed_type_p (tree instance, int* nonnull)
7880 tree t = TREE_TYPE (instance);
7881 int cdtorp = 0;
7882 tree fixed;
7884 /* processing_template_decl can be false in a template if we're in
7885 instantiate_non_dependent_expr, but we still want to suppress
7886 this check. */
7887 if (in_template_function ())
7889 /* In a template we only care about the type of the result. */
7890 if (nonnull)
7891 *nonnull = true;
7892 return true;
7895 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7896 if (INDIRECT_TYPE_P (t))
7897 t = TREE_TYPE (t);
7898 if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t))
7899 return 1;
7900 if (fixed == NULL_TREE)
7901 return 0;
7902 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7903 return 0;
7904 return cdtorp ? -1 : 1;
7908 void
7909 init_class_processing (void)
7911 current_class_depth = 0;
7912 current_class_stack_size = 10;
7913 current_class_stack
7914 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7915 sizeof_biggest_empty_class = size_zero_node;
7917 ridpointers[(int) RID_PUBLIC] = access_public_node;
7918 ridpointers[(int) RID_PRIVATE] = access_private_node;
7919 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7922 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7924 static void
7925 restore_class_cache (void)
7927 tree type;
7929 /* We are re-entering the same class we just left, so we don't
7930 have to search the whole inheritance matrix to find all the
7931 decls to bind again. Instead, we install the cached
7932 class_shadowed list and walk through it binding names. */
7933 push_binding_level (previous_class_level);
7934 class_binding_level = previous_class_level;
7935 /* Restore IDENTIFIER_TYPE_VALUE. */
7936 for (type = class_binding_level->type_shadowed;
7937 type;
7938 type = TREE_CHAIN (type))
7939 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7942 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7943 appropriate for TYPE.
7945 So that we may avoid calls to lookup_name, we cache the _TYPE
7946 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7948 For multiple inheritance, we perform a two-pass depth-first search
7949 of the type lattice. */
7951 void
7952 pushclass (tree type)
7954 class_stack_node_t csn;
7956 type = TYPE_MAIN_VARIANT (type);
7958 /* Make sure there is enough room for the new entry on the stack. */
7959 if (current_class_depth + 1 >= current_class_stack_size)
7961 current_class_stack_size *= 2;
7962 current_class_stack
7963 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7964 current_class_stack_size);
7967 /* Insert a new entry on the class stack. */
7968 csn = current_class_stack + current_class_depth;
7969 csn->name = current_class_name;
7970 csn->type = current_class_type;
7971 csn->access = current_access_specifier;
7972 csn->names_used = 0;
7973 csn->hidden = 0;
7974 current_class_depth++;
7976 /* Now set up the new type. */
7977 current_class_name = TYPE_NAME (type);
7978 if (TREE_CODE (current_class_name) == TYPE_DECL)
7979 current_class_name = DECL_NAME (current_class_name);
7980 current_class_type = type;
7982 /* By default, things in classes are private, while things in
7983 structures or unions are public. */
7984 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7985 ? access_private_node
7986 : access_public_node);
7988 if (previous_class_level
7989 && type != previous_class_level->this_entity
7990 && current_class_depth == 1)
7992 /* Forcibly remove any old class remnants. */
7993 invalidate_class_lookup_cache ();
7996 if (!previous_class_level
7997 || type != previous_class_level->this_entity
7998 || current_class_depth > 1)
7999 pushlevel_class ();
8000 else
8001 restore_class_cache ();
8004 /* Get out of the current class scope. If we were in a class scope
8005 previously, that is the one popped to. */
8007 void
8008 popclass (void)
8010 poplevel_class ();
8012 current_class_depth--;
8013 current_class_name = current_class_stack[current_class_depth].name;
8014 current_class_type = current_class_stack[current_class_depth].type;
8015 current_access_specifier = current_class_stack[current_class_depth].access;
8016 if (current_class_stack[current_class_depth].names_used)
8017 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8020 /* Mark the top of the class stack as hidden. */
8022 void
8023 push_class_stack (void)
8025 if (current_class_depth)
8026 ++current_class_stack[current_class_depth - 1].hidden;
8029 /* Mark the top of the class stack as un-hidden. */
8031 void
8032 pop_class_stack (void)
8034 if (current_class_depth)
8035 --current_class_stack[current_class_depth - 1].hidden;
8038 /* If the class type currently being defined is either T or
8039 a nested type of T, returns the type from the current_class_stack,
8040 which might be equivalent to but not equal to T in case of
8041 constrained partial specializations. */
8043 tree
8044 currently_open_class (tree t)
8046 int i;
8048 if (!CLASS_TYPE_P (t))
8049 return NULL_TREE;
8051 t = TYPE_MAIN_VARIANT (t);
8053 /* We start looking from 1 because entry 0 is from global scope,
8054 and has no type. */
8055 for (i = current_class_depth; i > 0; --i)
8057 tree c;
8058 if (i == current_class_depth)
8059 c = current_class_type;
8060 else
8062 if (current_class_stack[i].hidden)
8063 break;
8064 c = current_class_stack[i].type;
8066 if (!c)
8067 continue;
8068 if (same_type_p (c, t))
8069 return c;
8071 return NULL_TREE;
8074 /* If either current_class_type or one of its enclosing classes are derived
8075 from T, return the appropriate type. Used to determine how we found
8076 something via unqualified lookup. */
8078 tree
8079 currently_open_derived_class (tree t)
8081 int i;
8083 /* The bases of a dependent type are unknown. */
8084 if (dependent_type_p (t))
8085 return NULL_TREE;
8087 if (!current_class_type)
8088 return NULL_TREE;
8090 if (DERIVED_FROM_P (t, current_class_type))
8091 return current_class_type;
8093 for (i = current_class_depth - 1; i > 0; --i)
8095 if (current_class_stack[i].hidden)
8096 break;
8097 if (DERIVED_FROM_P (t, current_class_stack[i].type))
8098 return current_class_stack[i].type;
8101 return NULL_TREE;
8104 /* Return the outermost enclosing class type that is still open, or
8105 NULL_TREE. */
8107 tree
8108 outermost_open_class (void)
8110 if (!current_class_type)
8111 return NULL_TREE;
8112 tree r = NULL_TREE;
8113 if (TYPE_BEING_DEFINED (current_class_type))
8114 r = current_class_type;
8115 for (int i = current_class_depth - 1; i > 0; --i)
8117 if (current_class_stack[i].hidden)
8118 break;
8119 tree t = current_class_stack[i].type;
8120 if (!TYPE_BEING_DEFINED (t))
8121 break;
8122 r = t;
8124 return r;
8127 /* Returns the innermost class type which is not a lambda closure type. */
8129 tree
8130 current_nonlambda_class_type (void)
8132 tree type = current_class_type;
8133 while (type && LAMBDA_TYPE_P (type))
8134 type = decl_type_context (TYPE_NAME (type));
8135 return type;
8138 /* When entering a class scope, all enclosing class scopes' names with
8139 static meaning (static variables, static functions, types and
8140 enumerators) have to be visible. This recursive function calls
8141 pushclass for all enclosing class contexts until global or a local
8142 scope is reached. TYPE is the enclosed class. */
8144 void
8145 push_nested_class (tree type)
8147 /* A namespace might be passed in error cases, like A::B:C. */
8148 if (type == NULL_TREE
8149 || !CLASS_TYPE_P (type))
8150 return;
8152 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8154 pushclass (type);
8157 /* Undoes a push_nested_class call. */
8159 void
8160 pop_nested_class (void)
8162 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8164 popclass ();
8165 if (context && CLASS_TYPE_P (context))
8166 pop_nested_class ();
8169 /* Returns the number of extern "LANG" blocks we are nested within. */
8172 current_lang_depth (void)
8174 return vec_safe_length (current_lang_base);
8177 /* Set global variables CURRENT_LANG_NAME to appropriate value
8178 so that behavior of name-mangling machinery is correct. */
8180 void
8181 push_lang_context (tree name)
8183 vec_safe_push (current_lang_base, current_lang_name);
8185 if (name == lang_name_cplusplus)
8186 current_lang_name = name;
8187 else if (name == lang_name_c)
8188 current_lang_name = name;
8189 else
8190 error ("language string %<\"%E\"%> not recognized", name);
8193 /* Get out of the current language scope. */
8195 void
8196 pop_lang_context (void)
8198 current_lang_name = current_lang_base->pop ();
8201 /* Type instantiation routines. */
8203 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8204 matches the TARGET_TYPE. If there is no satisfactory match, return
8205 error_mark_node, and issue an error & warning messages under
8206 control of FLAGS. Permit pointers to member function if FLAGS
8207 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8208 a template-id, and EXPLICIT_TARGS are the explicitly provided
8209 template arguments.
8211 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8212 is the base path used to reference those member functions. If
8213 the address is resolved to a member function, access checks will be
8214 performed and errors issued if appropriate. */
8216 static tree
8217 resolve_address_of_overloaded_function (tree target_type,
8218 tree overload,
8219 tsubst_flags_t complain,
8220 bool template_only,
8221 tree explicit_targs,
8222 tree access_path)
8224 /* Here's what the standard says:
8226 [over.over]
8228 If the name is a function template, template argument deduction
8229 is done, and if the argument deduction succeeds, the deduced
8230 arguments are used to generate a single template function, which
8231 is added to the set of overloaded functions considered.
8233 Non-member functions and static member functions match targets of
8234 type "pointer-to-function" or "reference-to-function." Nonstatic
8235 member functions match targets of type "pointer-to-member
8236 function;" the function type of the pointer to member is used to
8237 select the member function from the set of overloaded member
8238 functions. If a non-static member function is selected, the
8239 reference to the overloaded function name is required to have the
8240 form of a pointer to member as described in 5.3.1.
8242 If more than one function is selected, any template functions in
8243 the set are eliminated if the set also contains a non-template
8244 function, and any given template function is eliminated if the
8245 set contains a second template function that is more specialized
8246 than the first according to the partial ordering rules 14.5.5.2.
8247 After such eliminations, if any, there shall remain exactly one
8248 selected function. */
8250 int is_ptrmem = 0;
8251 /* We store the matches in a TREE_LIST rooted here. The functions
8252 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8253 interoperability with most_specialized_instantiation. */
8254 tree matches = NULL_TREE;
8255 tree fn;
8256 tree target_fn_type;
8258 /* By the time we get here, we should be seeing only real
8259 pointer-to-member types, not the internal POINTER_TYPE to
8260 METHOD_TYPE representation. */
8261 gcc_assert (!TYPE_PTR_P (target_type)
8262 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8264 gcc_assert (is_overloaded_fn (overload));
8266 /* Check that the TARGET_TYPE is reasonable. */
8267 if (TYPE_PTRFN_P (target_type)
8268 || TYPE_REFFN_P (target_type))
8269 /* This is OK. */;
8270 else if (TYPE_PTRMEMFUNC_P (target_type))
8271 /* This is OK, too. */
8272 is_ptrmem = 1;
8273 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8274 /* This is OK, too. This comes from a conversion to reference
8275 type. */
8276 target_type = build_reference_type (target_type);
8277 else
8279 if (complain & tf_error)
8280 error ("cannot resolve overloaded function %qD based on"
8281 " conversion to type %qT",
8282 OVL_NAME (overload), target_type);
8283 return error_mark_node;
8286 /* Non-member functions and static member functions match targets of type
8287 "pointer-to-function" or "reference-to-function." Nonstatic member
8288 functions match targets of type "pointer-to-member-function;" the
8289 function type of the pointer to member is used to select the member
8290 function from the set of overloaded member functions.
8292 So figure out the FUNCTION_TYPE that we want to match against. */
8293 target_fn_type = static_fn_type (target_type);
8295 /* If we can find a non-template function that matches, we can just
8296 use it. There's no point in generating template instantiations
8297 if we're just going to throw them out anyhow. But, of course, we
8298 can only do this when we don't *need* a template function. */
8299 if (!template_only)
8300 for (lkp_iterator iter (overload); iter; ++iter)
8302 tree fn = *iter;
8304 if (TREE_CODE (fn) == TEMPLATE_DECL)
8305 /* We're not looking for templates just yet. */
8306 continue;
8308 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8309 /* We're looking for a non-static member, and this isn't
8310 one, or vice versa. */
8311 continue;
8313 /* Constraints must be satisfied. This is done before
8314 return type deduction since that instantiates the
8315 function. */
8316 if (!constraints_satisfied_p (fn))
8317 continue;
8319 if (undeduced_auto_decl (fn))
8321 /* Force instantiation to do return type deduction. */
8322 maybe_instantiate_decl (fn);
8323 require_deduced_type (fn);
8326 /* In C++17 we need the noexcept-qualifier to compare types. */
8327 if (flag_noexcept_type
8328 && !maybe_instantiate_noexcept (fn, complain))
8329 continue;
8331 /* See if there's a match. */
8332 tree fntype = static_fn_type (fn);
8333 if (same_type_p (target_fn_type, fntype)
8334 || fnptr_conv_p (target_fn_type, fntype))
8335 matches = tree_cons (fn, NULL_TREE, matches);
8338 /* Now, if we've already got a match (or matches), there's no need
8339 to proceed to the template functions. But, if we don't have a
8340 match we need to look at them, too. */
8341 if (!matches)
8343 tree target_arg_types;
8344 tree target_ret_type;
8345 tree *args;
8346 unsigned int nargs, ia;
8347 tree arg;
8349 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8350 target_ret_type = TREE_TYPE (target_fn_type);
8352 nargs = list_length (target_arg_types);
8353 args = XALLOCAVEC (tree, nargs);
8354 for (arg = target_arg_types, ia = 0;
8355 arg != NULL_TREE && arg != void_list_node;
8356 arg = TREE_CHAIN (arg), ++ia)
8357 args[ia] = TREE_VALUE (arg);
8358 nargs = ia;
8360 for (lkp_iterator iter (overload); iter; ++iter)
8362 tree fn = *iter;
8363 tree instantiation;
8364 tree targs;
8366 if (TREE_CODE (fn) != TEMPLATE_DECL)
8367 /* We're only looking for templates. */
8368 continue;
8370 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8371 != is_ptrmem)
8372 /* We're not looking for a non-static member, and this is
8373 one, or vice versa. */
8374 continue;
8376 tree ret = target_ret_type;
8378 /* If the template has a deduced return type, don't expose it to
8379 template argument deduction. */
8380 if (undeduced_auto_decl (fn))
8381 ret = NULL_TREE;
8383 /* Try to do argument deduction. */
8384 targs = make_tree_vec (DECL_NTPARMS (fn));
8385 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8386 nargs, ret,
8387 DEDUCE_EXACT, LOOKUP_NORMAL,
8388 NULL, false, false);
8389 if (instantiation == error_mark_node)
8390 /* Instantiation failed. */
8391 continue;
8393 /* Constraints must be satisfied. This is done before
8394 return type deduction since that instantiates the
8395 function. */
8396 if (flag_concepts && !constraints_satisfied_p (instantiation))
8397 continue;
8399 /* And now force instantiation to do return type deduction. */
8400 if (undeduced_auto_decl (instantiation))
8402 ++function_depth;
8403 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8404 --function_depth;
8406 require_deduced_type (instantiation);
8409 /* In C++17 we need the noexcept-qualifier to compare types. */
8410 if (flag_noexcept_type)
8411 maybe_instantiate_noexcept (instantiation, complain);
8413 /* See if there's a match. */
8414 tree fntype = static_fn_type (instantiation);
8415 if (same_type_p (target_fn_type, fntype)
8416 || fnptr_conv_p (target_fn_type, fntype))
8417 matches = tree_cons (instantiation, fn, matches);
8420 /* Now, remove all but the most specialized of the matches. */
8421 if (matches)
8423 tree match = most_specialized_instantiation (matches);
8425 if (match != error_mark_node)
8426 matches = tree_cons (TREE_PURPOSE (match),
8427 NULL_TREE,
8428 NULL_TREE);
8432 /* Now we should have exactly one function in MATCHES. */
8433 if (matches == NULL_TREE)
8435 /* There were *no* matches. */
8436 if (complain & tf_error)
8438 error ("no matches converting function %qD to type %q#T",
8439 OVL_NAME (overload), target_type);
8441 print_candidates (overload);
8443 return error_mark_node;
8445 else if (TREE_CHAIN (matches))
8447 /* There were too many matches. First check if they're all
8448 the same function. */
8449 tree match = NULL_TREE;
8451 fn = TREE_PURPOSE (matches);
8453 /* For multi-versioned functions, more than one match is just fine and
8454 decls_match will return false as they are different. */
8455 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8456 if (!decls_match (fn, TREE_PURPOSE (match))
8457 && !targetm.target_option.function_versions
8458 (fn, TREE_PURPOSE (match)))
8459 break;
8461 if (match)
8463 if (complain & tf_error)
8465 error ("converting overloaded function %qD to type %q#T is ambiguous",
8466 OVL_NAME (overload), target_type);
8468 /* Since print_candidates expects the functions in the
8469 TREE_VALUE slot, we flip them here. */
8470 for (match = matches; match; match = TREE_CHAIN (match))
8471 TREE_VALUE (match) = TREE_PURPOSE (match);
8473 print_candidates (matches);
8476 return error_mark_node;
8480 /* Good, exactly one match. Now, convert it to the correct type. */
8481 fn = TREE_PURPOSE (matches);
8483 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8484 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8486 static int explained;
8488 if (!(complain & tf_error))
8489 return error_mark_node;
8491 auto_diagnostic_group d;
8492 if (permerror (input_location, "assuming pointer to member %qD", fn)
8493 && !explained)
8495 inform (input_location, "(a pointer to member can only be "
8496 "formed with %<&%E%>)", fn);
8497 explained = 1;
8501 /* If a pointer to a function that is multi-versioned is requested, the
8502 pointer to the dispatcher function is returned instead. This works
8503 well because indirectly calling the function will dispatch the right
8504 function version at run-time. */
8505 if (DECL_FUNCTION_VERSIONED (fn))
8507 fn = get_function_version_dispatcher (fn);
8508 if (fn == NULL)
8509 return error_mark_node;
8510 /* Mark all the versions corresponding to the dispatcher as used. */
8511 if (!(complain & tf_conv))
8512 mark_versions_used (fn);
8515 /* If we're doing overload resolution purely for the purpose of
8516 determining conversion sequences, we should not consider the
8517 function used. If this conversion sequence is selected, the
8518 function will be marked as used at this point. */
8519 if (!(complain & tf_conv))
8521 /* Make =delete work with SFINAE. */
8522 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8523 return error_mark_node;
8524 if (!mark_used (fn, complain) && !(complain & tf_error))
8525 return error_mark_node;
8528 /* We could not check access to member functions when this
8529 expression was originally created since we did not know at that
8530 time to which function the expression referred. */
8531 if (DECL_FUNCTION_MEMBER_P (fn))
8533 gcc_assert (access_path);
8534 perform_or_defer_access_check (access_path, fn, fn, complain);
8537 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8538 return cp_build_addr_expr (fn, complain);
8539 else
8541 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8542 will mark the function as addressed, but here we must do it
8543 explicitly. */
8544 cxx_mark_addressable (fn);
8546 return fn;
8550 /* This function will instantiate the type of the expression given in
8551 RHS to match the type of LHSTYPE. If errors exist, then return
8552 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8553 we complain on errors. If we are not complaining, never modify rhs,
8554 as overload resolution wants to try many possible instantiations, in
8555 the hope that at least one will work.
8557 For non-recursive calls, LHSTYPE should be a function, pointer to
8558 function, or a pointer to member function. */
8560 tree
8561 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8563 tsubst_flags_t complain_in = complain;
8564 tree access_path = NULL_TREE;
8566 complain &= ~tf_ptrmem_ok;
8568 if (lhstype == unknown_type_node)
8570 if (complain & tf_error)
8571 error ("not enough type information");
8572 return error_mark_node;
8575 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8577 tree fntype = non_reference (lhstype);
8578 if (same_type_p (fntype, TREE_TYPE (rhs)))
8579 return rhs;
8580 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8581 return rhs;
8582 if (flag_ms_extensions
8583 && TYPE_PTRMEMFUNC_P (fntype)
8584 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8585 /* Microsoft allows `A::f' to be resolved to a
8586 pointer-to-member. */
8588 else
8590 if (complain & tf_error)
8591 error ("cannot convert %qE from type %qT to type %qT",
8592 rhs, TREE_TYPE (rhs), fntype);
8593 return error_mark_node;
8597 /* If we instantiate a template, and it is a A ?: C expression
8598 with omitted B, look through the SAVE_EXPR. */
8599 if (TREE_CODE (rhs) == SAVE_EXPR)
8600 rhs = TREE_OPERAND (rhs, 0);
8602 if (BASELINK_P (rhs))
8604 access_path = BASELINK_ACCESS_BINFO (rhs);
8605 rhs = BASELINK_FUNCTIONS (rhs);
8608 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8609 deduce any type information. */
8610 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8612 if (complain & tf_error)
8613 error ("not enough type information");
8614 return error_mark_node;
8617 /* There are only a few kinds of expressions that may have a type
8618 dependent on overload resolution. */
8619 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8620 || TREE_CODE (rhs) == COMPONENT_REF
8621 || is_overloaded_fn (rhs)
8622 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8624 /* This should really only be used when attempting to distinguish
8625 what sort of a pointer to function we have. For now, any
8626 arithmetic operation which is not supported on pointers
8627 is rejected as an error. */
8629 switch (TREE_CODE (rhs))
8631 case COMPONENT_REF:
8633 tree member = TREE_OPERAND (rhs, 1);
8635 member = instantiate_type (lhstype, member, complain);
8636 if (member != error_mark_node
8637 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8638 /* Do not lose object's side effects. */
8639 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8640 TREE_OPERAND (rhs, 0), member);
8641 return member;
8644 case OFFSET_REF:
8645 rhs = TREE_OPERAND (rhs, 1);
8646 if (BASELINK_P (rhs))
8647 return instantiate_type (lhstype, rhs, complain_in);
8649 /* This can happen if we are forming a pointer-to-member for a
8650 member template. */
8651 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8653 /* Fall through. */
8655 case TEMPLATE_ID_EXPR:
8657 tree fns = TREE_OPERAND (rhs, 0);
8658 tree args = TREE_OPERAND (rhs, 1);
8660 return
8661 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8662 /*template_only=*/true,
8663 args, access_path);
8666 case OVERLOAD:
8667 case FUNCTION_DECL:
8668 return
8669 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8670 /*template_only=*/false,
8671 /*explicit_targs=*/NULL_TREE,
8672 access_path);
8674 case ADDR_EXPR:
8676 if (PTRMEM_OK_P (rhs))
8677 complain |= tf_ptrmem_ok;
8679 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8682 case ERROR_MARK:
8683 return error_mark_node;
8685 default:
8686 gcc_unreachable ();
8688 return error_mark_node;
8691 /* Return the name of the virtual function pointer field
8692 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8693 this may have to look back through base types to find the
8694 ultimate field name. (For single inheritance, these could
8695 all be the same name. Who knows for multiple inheritance). */
8697 static tree
8698 get_vfield_name (tree type)
8700 tree binfo, base_binfo;
8702 for (binfo = TYPE_BINFO (type);
8703 BINFO_N_BASE_BINFOS (binfo);
8704 binfo = base_binfo)
8706 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8708 if (BINFO_VIRTUAL_P (base_binfo)
8709 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8710 break;
8713 type = BINFO_TYPE (binfo);
8714 tree ctor_name = constructor_name (type);
8715 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8716 + IDENTIFIER_LENGTH (ctor_name) + 2);
8717 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8718 return get_identifier (buf);
8721 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8722 according to [class]:
8723 The class-name is also inserted
8724 into the scope of the class itself. For purposes of access checking,
8725 the inserted class name is treated as if it were a public member name. */
8727 void
8728 build_self_reference (void)
8730 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8731 tree decl = build_lang_decl (TYPE_DECL, name, current_class_type);
8733 DECL_NONLOCAL (decl) = 1;
8734 DECL_CONTEXT (decl) = current_class_type;
8735 DECL_ARTIFICIAL (decl) = 1;
8736 SET_DECL_SELF_REFERENCE_P (decl);
8737 set_underlying_type (decl);
8738 set_instantiating_module (decl);
8740 if (processing_template_decl)
8741 decl = push_template_decl (decl);
8743 tree saved_cas = current_access_specifier;
8744 current_access_specifier = access_public_node;
8745 finish_member_declaration (decl);
8746 current_access_specifier = saved_cas;
8749 /* Returns 1 if TYPE contains only padding bytes. */
8752 is_empty_class (tree type)
8754 if (type == error_mark_node)
8755 return 0;
8757 if (! CLASS_TYPE_P (type))
8758 return 0;
8760 return CLASSTYPE_EMPTY_P (type);
8763 /* Returns true if TYPE contains no actual data, just various
8764 possible combinations of empty classes. If IGNORE_VPTR is true,
8765 a vptr doesn't prevent the class from being considered empty. Typically
8766 we want to ignore the vptr on assignment, and not on initialization. */
8768 bool
8769 is_really_empty_class (tree type, bool ignore_vptr)
8771 if (CLASS_TYPE_P (type))
8773 tree field;
8774 tree binfo;
8775 tree base_binfo;
8776 int i;
8778 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8779 out, but we'd like to be able to check this before then. */
8780 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8781 return true;
8783 if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
8784 return false;
8786 for (binfo = TYPE_BINFO (type), i = 0;
8787 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8788 if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
8789 return false;
8790 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8791 if (TREE_CODE (field) == FIELD_DECL
8792 && !DECL_ARTIFICIAL (field)
8793 /* An unnamed bit-field is not a data member. */
8794 && !DECL_UNNAMED_BIT_FIELD (field)
8795 && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
8796 return false;
8797 return true;
8799 else if (TREE_CODE (type) == ARRAY_TYPE)
8800 return (integer_zerop (array_type_nelts_top (type))
8801 || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
8802 return false;
8805 /* Note that NAME was looked up while the current class was being
8806 defined and that the result of that lookup was DECL. */
8808 void
8809 maybe_note_name_used_in_class (tree name, tree decl)
8811 splay_tree names_used;
8813 /* If we're not defining a class, there's nothing to do. */
8814 if (!(innermost_scope_kind() == sk_class
8815 && TYPE_BEING_DEFINED (current_class_type)
8816 && !LAMBDA_TYPE_P (current_class_type)))
8817 return;
8819 /* If there's already a binding for this NAME, then we don't have
8820 anything to worry about. */
8821 if (lookup_member (current_class_type, name,
8822 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8823 return;
8825 if (!current_class_stack[current_class_depth - 1].names_used)
8826 current_class_stack[current_class_depth - 1].names_used
8827 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8828 names_used = current_class_stack[current_class_depth - 1].names_used;
8830 splay_tree_insert (names_used,
8831 (splay_tree_key) name,
8832 (splay_tree_value) decl);
8835 /* Note that NAME was declared (as DECL) in the current class. Check
8836 to see that the declaration is valid. */
8838 void
8839 note_name_declared_in_class (tree name, tree decl)
8841 splay_tree names_used;
8842 splay_tree_node n;
8844 /* Look to see if we ever used this name. */
8845 names_used
8846 = current_class_stack[current_class_depth - 1].names_used;
8847 if (!names_used)
8848 return;
8849 /* The C language allows members to be declared with a type of the same
8850 name, and the C++ standard says this diagnostic is not required. So
8851 allow it in extern "C" blocks unless predantic is specified.
8852 Allow it in all cases if -ms-extensions is specified. */
8853 if ((!pedantic && current_lang_name == lang_name_c)
8854 || flag_ms_extensions)
8855 return;
8856 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8857 if (n)
8859 /* [basic.scope.class]
8861 A name N used in a class S shall refer to the same declaration
8862 in its context and when re-evaluated in the completed scope of
8863 S. */
8864 if (permerror (location_of (decl),
8865 "declaration of %q#D changes meaning of %qD",
8866 decl, OVL_NAME (decl)))
8867 inform (location_of ((tree) n->value),
8868 "%qD declared here as %q#D",
8869 OVL_NAME (decl), (tree) n->value);
8873 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8874 Secondary vtables are merged with primary vtables; this function
8875 will return the VAR_DECL for the primary vtable. */
8877 tree
8878 get_vtbl_decl_for_binfo (tree binfo)
8880 tree decl;
8882 decl = BINFO_VTABLE (binfo);
8883 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8885 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8886 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8888 if (decl)
8889 gcc_assert (VAR_P (decl));
8890 return decl;
8894 /* Returns the binfo for the primary base of BINFO. If the resulting
8895 BINFO is a virtual base, and it is inherited elsewhere in the
8896 hierarchy, then the returned binfo might not be the primary base of
8897 BINFO in the complete object. Check BINFO_PRIMARY_P or
8898 BINFO_LOST_PRIMARY_P to be sure. */
8900 static tree
8901 get_primary_binfo (tree binfo)
8903 tree primary_base;
8905 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8906 if (!primary_base)
8907 return NULL_TREE;
8909 return copied_binfo (primary_base, binfo);
8912 /* As above, but iterate until we reach the binfo that actually provides the
8913 vptr for BINFO. */
8915 static tree
8916 most_primary_binfo (tree binfo)
8918 tree b = binfo;
8919 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8920 && !BINFO_LOST_PRIMARY_P (b))
8922 tree primary_base = get_primary_binfo (b);
8923 gcc_assert (BINFO_PRIMARY_P (primary_base)
8924 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8925 b = primary_base;
8927 return b;
8930 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8931 type. Note that the virtual inheritance might be above or below BINFO in
8932 the hierarchy. */
8934 bool
8935 vptr_via_virtual_p (tree binfo)
8937 if (TYPE_P (binfo))
8938 binfo = TYPE_BINFO (binfo);
8939 tree primary = most_primary_binfo (binfo);
8940 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8941 a morally virtual base. */
8942 tree virt = binfo_via_virtual (primary, NULL_TREE);
8943 return virt != NULL_TREE;
8946 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8948 static int
8949 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8951 if (!indented_p)
8952 fprintf (stream, "%*s", indent, "");
8953 return 1;
8956 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8957 INDENT should be zero when called from the top level; it is
8958 incremented recursively. IGO indicates the next expected BINFO in
8959 inheritance graph ordering. */
8961 static tree
8962 dump_class_hierarchy_r (FILE *stream,
8963 dump_flags_t flags,
8964 tree binfo,
8965 tree igo,
8966 int indent)
8968 int indented = 0;
8969 tree base_binfo;
8970 int i;
8972 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8973 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8974 (HOST_WIDE_INT) (uintptr_t) binfo);
8975 if (binfo != igo)
8977 fprintf (stream, "alternative-path\n");
8978 return igo;
8980 igo = TREE_CHAIN (binfo);
8982 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8983 tree_to_shwi (BINFO_OFFSET (binfo)));
8984 if (is_empty_class (BINFO_TYPE (binfo)))
8985 fprintf (stream, " empty");
8986 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8987 fprintf (stream, " nearly-empty");
8988 if (BINFO_VIRTUAL_P (binfo))
8989 fprintf (stream, " virtual");
8990 fprintf (stream, "\n");
8992 if (BINFO_PRIMARY_P (binfo))
8994 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8995 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8996 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8997 TFF_PLAIN_IDENTIFIER),
8998 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
9000 if (BINFO_LOST_PRIMARY_P (binfo))
9002 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9003 fprintf (stream, " lost-primary");
9005 if (indented)
9006 fprintf (stream, "\n");
9008 if (!(flags & TDF_SLIM))
9010 int indented = 0;
9012 if (BINFO_SUBVTT_INDEX (binfo))
9014 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9015 fprintf (stream, " subvttidx=%s",
9016 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
9017 TFF_PLAIN_IDENTIFIER));
9019 if (BINFO_VPTR_INDEX (binfo))
9021 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9022 fprintf (stream, " vptridx=%s",
9023 expr_as_string (BINFO_VPTR_INDEX (binfo),
9024 TFF_PLAIN_IDENTIFIER));
9026 if (BINFO_VPTR_FIELD (binfo))
9028 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9029 fprintf (stream, " vbaseoffset=%s",
9030 expr_as_string (BINFO_VPTR_FIELD (binfo),
9031 TFF_PLAIN_IDENTIFIER));
9033 if (BINFO_VTABLE (binfo))
9035 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9036 fprintf (stream, " vptr=%s",
9037 expr_as_string (BINFO_VTABLE (binfo),
9038 TFF_PLAIN_IDENTIFIER));
9041 if (indented)
9042 fprintf (stream, "\n");
9045 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9046 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
9048 return igo;
9051 /* Dump the BINFO hierarchy for T. */
9053 static void
9054 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
9056 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9057 fprintf (stream, " size=%lu align=%lu\n",
9058 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
9059 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
9060 if (tree as_base = CLASSTYPE_AS_BASE (t))
9061 fprintf (stream, " base size=%lu base align=%lu\n",
9062 (unsigned long)(tree_to_shwi (TYPE_SIZE (as_base))
9063 / BITS_PER_UNIT),
9064 (unsigned long)(TYPE_ALIGN (as_base) / BITS_PER_UNIT));
9065 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
9066 fprintf (stream, "\n");
9069 /* Debug interface to hierarchy dumping. */
9071 void
9072 debug_class (tree t)
9074 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
9077 static void
9078 dump_class_hierarchy (tree t)
9080 dump_flags_t flags;
9081 if (FILE *stream = dump_begin (class_dump_id, &flags))
9083 dump_class_hierarchy_1 (stream, flags, t);
9084 dump_end (class_dump_id, stream);
9088 static void
9089 dump_array (FILE * stream, tree decl)
9091 tree value;
9092 unsigned HOST_WIDE_INT ix;
9093 HOST_WIDE_INT elt;
9094 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
9096 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
9097 / BITS_PER_UNIT);
9098 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
9099 fprintf (stream, " %s entries",
9100 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
9101 TFF_PLAIN_IDENTIFIER));
9102 fprintf (stream, "\n");
9104 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
9105 ix, value)
9106 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
9107 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
9110 static void
9111 dump_vtable (tree t, tree binfo, tree vtable)
9113 dump_flags_t flags;
9114 FILE *stream = dump_begin (class_dump_id, &flags);
9116 if (!stream)
9117 return;
9119 if (!(flags & TDF_SLIM))
9121 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9123 fprintf (stream, "%s for %s",
9124 ctor_vtbl_p ? "Construction vtable" : "Vtable",
9125 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
9126 if (ctor_vtbl_p)
9128 if (!BINFO_VIRTUAL_P (binfo))
9129 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
9130 (HOST_WIDE_INT) (uintptr_t) binfo);
9131 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9133 fprintf (stream, "\n");
9134 dump_array (stream, vtable);
9135 fprintf (stream, "\n");
9138 dump_end (class_dump_id, stream);
9141 static void
9142 dump_vtt (tree t, tree vtt)
9144 dump_flags_t flags;
9145 FILE *stream = dump_begin (class_dump_id, &flags);
9147 if (!stream)
9148 return;
9150 if (!(flags & TDF_SLIM))
9152 fprintf (stream, "VTT for %s\n",
9153 type_as_string (t, TFF_PLAIN_IDENTIFIER));
9154 dump_array (stream, vtt);
9155 fprintf (stream, "\n");
9158 dump_end (class_dump_id, stream);
9161 /* Dump a function or thunk and its thunkees. */
9163 static void
9164 dump_thunk (FILE *stream, int indent, tree thunk)
9166 static const char spaces[] = " ";
9167 tree name = DECL_NAME (thunk);
9168 tree thunks;
9170 fprintf (stream, "%.*s%p %s %s", indent, spaces,
9171 (void *)thunk,
9172 !DECL_THUNK_P (thunk) ? "function"
9173 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9174 name ? IDENTIFIER_POINTER (name) : "<unset>");
9175 if (DECL_THUNK_P (thunk))
9177 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9178 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9180 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9181 if (!virtual_adjust)
9182 /*NOP*/;
9183 else if (DECL_THIS_THUNK_P (thunk))
9184 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9185 tree_to_shwi (virtual_adjust));
9186 else
9187 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9188 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9189 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9190 if (THUNK_ALIAS (thunk))
9191 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9193 fprintf (stream, "\n");
9194 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9195 dump_thunk (stream, indent + 2, thunks);
9198 /* Dump the thunks for FN. */
9200 void
9201 debug_thunks (tree fn)
9203 dump_thunk (stderr, 0, fn);
9206 /* Virtual function table initialization. */
9208 /* Create all the necessary vtables for T and its base classes. */
9210 static void
9211 finish_vtbls (tree t)
9213 tree vbase;
9214 vec<constructor_elt, va_gc> *v = NULL;
9215 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9217 /* We lay out the primary and secondary vtables in one contiguous
9218 vtable. The primary vtable is first, followed by the non-virtual
9219 secondary vtables in inheritance graph order. */
9220 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9221 vtable, t, &v);
9223 /* Then come the virtual bases, also in inheritance graph order. */
9224 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9226 if (!BINFO_VIRTUAL_P (vbase))
9227 continue;
9228 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9231 if (BINFO_VTABLE (TYPE_BINFO (t)))
9232 initialize_vtable (TYPE_BINFO (t), v);
9235 /* Initialize the vtable for BINFO with the INITS. */
9237 static void
9238 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9240 tree decl;
9242 layout_vtable_decl (binfo, vec_safe_length (inits));
9243 decl = get_vtbl_decl_for_binfo (binfo);
9244 initialize_artificial_var (decl, inits);
9245 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9248 /* Build the VTT (virtual table table) for T.
9249 A class requires a VTT if it has virtual bases.
9251 This holds
9252 1 - primary virtual pointer for complete object T
9253 2 - secondary VTTs for each direct non-virtual base of T which requires a
9255 3 - secondary virtual pointers for each direct or indirect base of T which
9256 has virtual bases or is reachable via a virtual path from T.
9257 4 - secondary VTTs for each direct or indirect virtual base of T.
9259 Secondary VTTs look like complete object VTTs without part 4. */
9261 static void
9262 build_vtt (tree t)
9264 tree type;
9265 tree vtt;
9266 tree index;
9267 vec<constructor_elt, va_gc> *inits;
9269 /* Build up the initializers for the VTT. */
9270 inits = NULL;
9271 index = size_zero_node;
9272 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9274 /* If we didn't need a VTT, we're done. */
9275 if (!inits)
9276 return;
9278 /* Figure out the type of the VTT. */
9279 type = build_array_of_n_type (const_ptr_type_node,
9280 inits->length ());
9282 /* Now, build the VTT object itself. */
9283 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9284 initialize_artificial_var (vtt, inits);
9285 /* Add the VTT to the vtables list. */
9286 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9287 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9289 dump_vtt (t, vtt);
9292 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9293 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9294 and CHAIN the vtable pointer for this binfo after construction is
9295 complete. VALUE can also be another BINFO, in which case we recurse. */
9297 static tree
9298 binfo_ctor_vtable (tree binfo)
9300 tree vt;
9302 while (1)
9304 vt = BINFO_VTABLE (binfo);
9305 if (TREE_CODE (vt) == TREE_LIST)
9306 vt = TREE_VALUE (vt);
9307 if (TREE_CODE (vt) == TREE_BINFO)
9308 binfo = vt;
9309 else
9310 break;
9313 return vt;
9316 /* Data for secondary VTT initialization. */
9317 struct secondary_vptr_vtt_init_data
9319 /* Is this the primary VTT? */
9320 bool top_level_p;
9322 /* Current index into the VTT. */
9323 tree index;
9325 /* Vector of initializers built up. */
9326 vec<constructor_elt, va_gc> *inits;
9328 /* The type being constructed by this secondary VTT. */
9329 tree type_being_constructed;
9332 /* Recursively build the VTT-initializer for BINFO (which is in the
9333 hierarchy dominated by T). INITS points to the end of the initializer
9334 list to date. INDEX is the VTT index where the next element will be
9335 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9336 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9337 for virtual bases of T. When it is not so, we build the constructor
9338 vtables for the BINFO-in-T variant. */
9340 static void
9341 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9342 tree *index)
9344 int i;
9345 tree b;
9346 tree init;
9347 secondary_vptr_vtt_init_data data;
9348 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9350 /* We only need VTTs for subobjects with virtual bases. */
9351 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9352 return;
9354 /* We need to use a construction vtable if this is not the primary
9355 VTT. */
9356 if (!top_level_p)
9358 build_ctor_vtbl_group (binfo, t);
9360 /* Record the offset in the VTT where this sub-VTT can be found. */
9361 BINFO_SUBVTT_INDEX (binfo) = *index;
9364 /* Add the address of the primary vtable for the complete object. */
9365 init = binfo_ctor_vtable (binfo);
9366 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9367 if (top_level_p)
9369 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9370 BINFO_VPTR_INDEX (binfo) = *index;
9372 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9374 /* Recursively add the secondary VTTs for non-virtual bases. */
9375 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9376 if (!BINFO_VIRTUAL_P (b))
9377 build_vtt_inits (b, t, inits, index);
9379 /* Add secondary virtual pointers for all subobjects of BINFO with
9380 either virtual bases or reachable along a virtual path, except
9381 subobjects that are non-virtual primary bases. */
9382 data.top_level_p = top_level_p;
9383 data.index = *index;
9384 data.inits = *inits;
9385 data.type_being_constructed = BINFO_TYPE (binfo);
9387 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9389 *index = data.index;
9391 /* data.inits might have grown as we added secondary virtual pointers.
9392 Make sure our caller knows about the new vector. */
9393 *inits = data.inits;
9395 if (top_level_p)
9396 /* Add the secondary VTTs for virtual bases in inheritance graph
9397 order. */
9398 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9400 if (!BINFO_VIRTUAL_P (b))
9401 continue;
9403 build_vtt_inits (b, t, inits, index);
9405 else
9406 /* Remove the ctor vtables we created. */
9407 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9410 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9411 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9413 static tree
9414 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9416 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9418 /* We don't care about bases that don't have vtables. */
9419 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9420 return dfs_skip_bases;
9422 /* We're only interested in proper subobjects of the type being
9423 constructed. */
9424 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9425 return NULL_TREE;
9427 /* We're only interested in bases with virtual bases or reachable
9428 via a virtual path from the type being constructed. */
9429 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9430 || binfo_via_virtual (binfo, data->type_being_constructed)))
9431 return dfs_skip_bases;
9433 /* We're not interested in non-virtual primary bases. */
9434 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9435 return NULL_TREE;
9437 /* Record the index where this secondary vptr can be found. */
9438 if (data->top_level_p)
9440 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9441 BINFO_VPTR_INDEX (binfo) = data->index;
9443 if (BINFO_VIRTUAL_P (binfo))
9445 /* It's a primary virtual base, and this is not a
9446 construction vtable. Find the base this is primary of in
9447 the inheritance graph, and use that base's vtable
9448 now. */
9449 while (BINFO_PRIMARY_P (binfo))
9450 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9454 /* Add the initializer for the secondary vptr itself. */
9455 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9457 /* Advance the vtt index. */
9458 data->index = size_binop (PLUS_EXPR, data->index,
9459 TYPE_SIZE_UNIT (ptr_type_node));
9461 return NULL_TREE;
9464 /* Called from build_vtt_inits via dfs_walk. After building
9465 constructor vtables and generating the sub-vtt from them, we need
9466 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9467 binfo of the base whose sub vtt was generated. */
9469 static tree
9470 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9472 tree vtable = BINFO_VTABLE (binfo);
9474 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9475 /* If this class has no vtable, none of its bases do. */
9476 return dfs_skip_bases;
9478 if (!vtable)
9479 /* This might be a primary base, so have no vtable in this
9480 hierarchy. */
9481 return NULL_TREE;
9483 /* If we scribbled the construction vtable vptr into BINFO, clear it
9484 out now. */
9485 if (TREE_CODE (vtable) == TREE_LIST
9486 && (TREE_PURPOSE (vtable) == (tree) data))
9487 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9489 return NULL_TREE;
9492 /* Build the construction vtable group for BINFO which is in the
9493 hierarchy dominated by T. */
9495 static void
9496 build_ctor_vtbl_group (tree binfo, tree t)
9498 tree type;
9499 tree vtbl;
9500 tree id;
9501 tree vbase;
9502 vec<constructor_elt, va_gc> *v;
9504 /* See if we've already created this construction vtable group. */
9505 id = mangle_ctor_vtbl_for_type (t, binfo);
9506 if (get_global_binding (id))
9507 return;
9509 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9510 /* Build a version of VTBL (with the wrong type) for use in
9511 constructing the addresses of secondary vtables in the
9512 construction vtable group. */
9513 vtbl = build_vtable (t, id, ptr_type_node);
9515 /* Don't export construction vtables from shared libraries. Even on
9516 targets that don't support hidden visibility, this tells
9517 can_refer_decl_in_current_unit_p not to assume that it's safe to
9518 access from a different compilation unit (bz 54314). */
9519 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9520 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9522 v = NULL;
9523 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9524 binfo, vtbl, t, &v);
9526 /* Add the vtables for each of our virtual bases using the vbase in T
9527 binfo. */
9528 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9529 vbase;
9530 vbase = TREE_CHAIN (vbase))
9532 tree b;
9534 if (!BINFO_VIRTUAL_P (vbase))
9535 continue;
9536 b = copied_binfo (vbase, binfo);
9538 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9541 /* Figure out the type of the construction vtable. */
9542 type = build_array_of_n_type (vtable_entry_type, v->length ());
9543 layout_type (type);
9544 TREE_TYPE (vtbl) = type;
9545 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9546 layout_decl (vtbl, 0);
9548 /* Initialize the construction vtable. */
9549 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9550 initialize_artificial_var (vtbl, v);
9551 dump_vtable (t, binfo, vtbl);
9554 /* Add the vtbl initializers for BINFO (and its bases other than
9555 non-virtual primaries) to the list of INITS. BINFO is in the
9556 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9557 the constructor the vtbl inits should be accumulated for. (If this
9558 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9559 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9560 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9561 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9562 but are not necessarily the same in terms of layout. */
9564 static void
9565 accumulate_vtbl_inits (tree binfo,
9566 tree orig_binfo,
9567 tree rtti_binfo,
9568 tree vtbl,
9569 tree t,
9570 vec<constructor_elt, va_gc> **inits)
9572 int i;
9573 tree base_binfo;
9574 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9576 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9578 /* If it doesn't have a vptr, we don't do anything. */
9579 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9580 return;
9582 /* If we're building a construction vtable, we're not interested in
9583 subobjects that don't require construction vtables. */
9584 if (ctor_vtbl_p
9585 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9586 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9587 return;
9589 /* Build the initializers for the BINFO-in-T vtable. */
9590 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9592 /* Walk the BINFO and its bases. We walk in preorder so that as we
9593 initialize each vtable we can figure out at what offset the
9594 secondary vtable lies from the primary vtable. We can't use
9595 dfs_walk here because we need to iterate through bases of BINFO
9596 and RTTI_BINFO simultaneously. */
9597 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9599 /* Skip virtual bases. */
9600 if (BINFO_VIRTUAL_P (base_binfo))
9601 continue;
9602 accumulate_vtbl_inits (base_binfo,
9603 BINFO_BASE_BINFO (orig_binfo, i),
9604 rtti_binfo, vtbl, t,
9605 inits);
9609 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9610 BINFO vtable to L. */
9612 static void
9613 dfs_accumulate_vtbl_inits (tree binfo,
9614 tree orig_binfo,
9615 tree rtti_binfo,
9616 tree orig_vtbl,
9617 tree t,
9618 vec<constructor_elt, va_gc> **l)
9620 tree vtbl = NULL_TREE;
9621 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9622 int n_inits;
9624 if (ctor_vtbl_p
9625 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9627 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9628 primary virtual base. If it is not the same primary in
9629 the hierarchy of T, we'll need to generate a ctor vtable
9630 for it, to place at its location in T. If it is the same
9631 primary, we still need a VTT entry for the vtable, but it
9632 should point to the ctor vtable for the base it is a
9633 primary for within the sub-hierarchy of RTTI_BINFO.
9635 There are three possible cases:
9637 1) We are in the same place.
9638 2) We are a primary base within a lost primary virtual base of
9639 RTTI_BINFO.
9640 3) We are primary to something not a base of RTTI_BINFO. */
9642 tree b;
9643 tree last = NULL_TREE;
9645 /* First, look through the bases we are primary to for RTTI_BINFO
9646 or a virtual base. */
9647 b = binfo;
9648 while (BINFO_PRIMARY_P (b))
9650 b = BINFO_INHERITANCE_CHAIN (b);
9651 last = b;
9652 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9653 goto found;
9655 /* If we run out of primary links, keep looking down our
9656 inheritance chain; we might be an indirect primary. */
9657 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9658 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9659 break;
9660 found:
9662 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9663 base B and it is a base of RTTI_BINFO, this is case 2. In
9664 either case, we share our vtable with LAST, i.e. the
9665 derived-most base within B of which we are a primary. */
9666 if (b == rtti_binfo
9667 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9668 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9669 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9670 binfo_ctor_vtable after everything's been set up. */
9671 vtbl = last;
9673 /* Otherwise, this is case 3 and we get our own. */
9675 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9676 return;
9678 n_inits = vec_safe_length (*l);
9680 if (!vtbl)
9682 tree index;
9683 int non_fn_entries;
9685 /* Add the initializer for this vtable. */
9686 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9687 &non_fn_entries, l);
9689 /* Figure out the position to which the VPTR should point. */
9690 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9691 index = size_binop (MULT_EXPR,
9692 TYPE_SIZE_UNIT (vtable_entry_type),
9693 size_int (non_fn_entries + n_inits));
9694 vtbl = fold_build_pointer_plus (vtbl, index);
9697 if (ctor_vtbl_p)
9698 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9699 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9700 straighten this out. */
9701 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9702 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9703 /* Throw away any unneeded intializers. */
9704 (*l)->truncate (n_inits);
9705 else
9706 /* For an ordinary vtable, set BINFO_VTABLE. */
9707 BINFO_VTABLE (binfo) = vtbl;
9710 static GTY(()) tree abort_fndecl_addr;
9711 static GTY(()) tree dvirt_fn;
9713 /* Construct the initializer for BINFO's virtual function table. BINFO
9714 is part of the hierarchy dominated by T. If we're building a
9715 construction vtable, the ORIG_BINFO is the binfo we should use to
9716 find the actual function pointers to put in the vtable - but they
9717 can be overridden on the path to most-derived in the graph that
9718 ORIG_BINFO belongs. Otherwise,
9719 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9720 BINFO that should be indicated by the RTTI information in the
9721 vtable; it will be a base class of T, rather than T itself, if we
9722 are building a construction vtable.
9724 The value returned is a TREE_LIST suitable for wrapping in a
9725 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9726 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9727 number of non-function entries in the vtable.
9729 It might seem that this function should never be called with a
9730 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9731 base is always subsumed by a derived class vtable. However, when
9732 we are building construction vtables, we do build vtables for
9733 primary bases; we need these while the primary base is being
9734 constructed. */
9736 static void
9737 build_vtbl_initializer (tree binfo,
9738 tree orig_binfo,
9739 tree t,
9740 tree rtti_binfo,
9741 int* non_fn_entries_p,
9742 vec<constructor_elt, va_gc> **inits)
9744 tree v;
9745 vtbl_init_data vid;
9746 unsigned ix, jx;
9747 tree vbinfo;
9748 vec<tree, va_gc> *vbases;
9749 constructor_elt *e;
9751 /* Initialize VID. */
9752 memset (&vid, 0, sizeof (vid));
9753 vid.binfo = binfo;
9754 vid.derived = t;
9755 vid.rtti_binfo = rtti_binfo;
9756 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9757 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9758 vid.generate_vcall_entries = true;
9759 /* The first vbase or vcall offset is at index -3 in the vtable. */
9760 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9762 /* Add entries to the vtable for RTTI. */
9763 build_rtti_vtbl_entries (binfo, &vid);
9765 /* Create an array for keeping track of the functions we've
9766 processed. When we see multiple functions with the same
9767 signature, we share the vcall offsets. */
9768 vec_alloc (vid.fns, 32);
9769 /* Add the vcall and vbase offset entries. */
9770 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9772 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9773 build_vbase_offset_vtbl_entries. */
9774 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9775 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9776 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9778 /* If the target requires padding between data entries, add that now. */
9779 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9781 int n_entries = vec_safe_length (vid.inits);
9783 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries,
9784 true);
9786 /* Move data entries into their new positions and add padding
9787 after the new positions. Iterate backwards so we don't
9788 overwrite entries that we would need to process later. */
9789 for (ix = n_entries - 1;
9790 vid.inits->iterate (ix, &e);
9791 ix--)
9793 int j;
9794 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9795 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9797 (*vid.inits)[new_position] = *e;
9799 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9801 constructor_elt *f = &(*vid.inits)[new_position - j];
9802 f->index = NULL_TREE;
9803 f->value = build1 (NOP_EXPR, vtable_entry_type,
9804 null_pointer_node);
9809 if (non_fn_entries_p)
9810 *non_fn_entries_p = vec_safe_length (vid.inits);
9812 /* The initializers for virtual functions were built up in reverse
9813 order. Straighten them out and add them to the running list in one
9814 step. */
9815 jx = vec_safe_length (*inits);
9816 vec_safe_grow (*inits, jx + vid.inits->length (), true);
9818 for (ix = vid.inits->length () - 1;
9819 vid.inits->iterate (ix, &e);
9820 ix--, jx++)
9821 (**inits)[jx] = *e;
9823 /* Go through all the ordinary virtual functions, building up
9824 initializers. */
9825 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9827 tree delta;
9828 tree vcall_index;
9829 tree fn, fn_original;
9830 tree init = NULL_TREE;
9832 fn = BV_FN (v);
9833 fn_original = fn;
9834 if (DECL_THUNK_P (fn))
9836 if (!DECL_NAME (fn))
9837 finish_thunk (fn);
9838 if (THUNK_ALIAS (fn))
9840 fn = THUNK_ALIAS (fn);
9841 BV_FN (v) = fn;
9843 fn_original = THUNK_TARGET (fn);
9846 /* If the only definition of this function signature along our
9847 primary base chain is from a lost primary, this vtable slot will
9848 never be used, so just zero it out. This is important to avoid
9849 requiring extra thunks which cannot be generated with the function.
9851 We first check this in update_vtable_entry_for_fn, so we handle
9852 restored primary bases properly; we also need to do it here so we
9853 zero out unused slots in ctor vtables, rather than filling them
9854 with erroneous values (though harmless, apart from relocation
9855 costs). */
9856 if (BV_LOST_PRIMARY (v))
9857 init = size_zero_node;
9859 if (! init)
9861 /* Pull the offset for `this', and the function to call, out of
9862 the list. */
9863 delta = BV_DELTA (v);
9864 vcall_index = BV_VCALL_INDEX (v);
9866 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9867 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9869 /* You can't call an abstract virtual function; it's abstract.
9870 So, we replace these functions with __pure_virtual. */
9871 if (DECL_PURE_VIRTUAL_P (fn_original))
9873 fn = abort_fndecl;
9874 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9876 if (abort_fndecl_addr == NULL)
9877 abort_fndecl_addr
9878 = fold_convert (vfunc_ptr_type_node,
9879 build_fold_addr_expr (fn));
9880 init = abort_fndecl_addr;
9883 /* Likewise for deleted virtuals. */
9884 else if (DECL_DELETED_FN (fn_original))
9886 if (!dvirt_fn)
9888 tree name = get_identifier ("__cxa_deleted_virtual");
9889 dvirt_fn = get_global_binding (name);
9890 if (!dvirt_fn)
9891 dvirt_fn = push_library_fn
9892 (name,
9893 build_function_type_list (void_type_node, NULL_TREE),
9894 NULL_TREE, ECF_NORETURN | ECF_COLD);
9896 fn = dvirt_fn;
9897 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9898 init = fold_convert (vfunc_ptr_type_node,
9899 build_fold_addr_expr (fn));
9901 else
9903 if (!integer_zerop (delta) || vcall_index)
9905 fn = make_thunk (fn, /*this_adjusting=*/1,
9906 delta, vcall_index);
9907 if (!DECL_NAME (fn))
9908 finish_thunk (fn);
9910 /* Take the address of the function, considering it to be of an
9911 appropriate generic type. */
9912 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9913 init = fold_convert (vfunc_ptr_type_node,
9914 build_fold_addr_expr (fn));
9915 /* Don't refer to a virtual destructor from a constructor
9916 vtable or a vtable for an abstract class, since destroying
9917 an object under construction is undefined behavior and we
9918 don't want it to be considered a candidate for speculative
9919 devirtualization. But do create the thunk for ABI
9920 compliance. */
9921 if (DECL_DESTRUCTOR_P (fn_original)
9922 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9923 || orig_binfo != binfo))
9924 init = size_zero_node;
9928 /* And add it to the chain of initializers. */
9929 if (TARGET_VTABLE_USES_DESCRIPTORS)
9931 int i;
9932 if (init == size_zero_node)
9933 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9934 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
9935 else
9936 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9938 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9939 fn, build_int_cst (NULL_TREE, i));
9940 TREE_CONSTANT (fdesc) = 1;
9942 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), fdesc);
9945 else
9946 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
9950 /* Adds to vid->inits the initializers for the vbase and vcall
9951 offsets in BINFO, which is in the hierarchy dominated by T. */
9953 static void
9954 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9956 tree b;
9958 /* If this is a derived class, we must first create entries
9959 corresponding to the primary base class. */
9960 b = get_primary_binfo (binfo);
9961 if (b)
9962 build_vcall_and_vbase_vtbl_entries (b, vid);
9964 /* Add the vbase entries for this base. */
9965 build_vbase_offset_vtbl_entries (binfo, vid);
9966 /* Add the vcall entries for this base. */
9967 build_vcall_offset_vtbl_entries (binfo, vid);
9970 /* Returns the initializers for the vbase offset entries in the vtable
9971 for BINFO (which is part of the class hierarchy dominated by T), in
9972 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9973 where the next vbase offset will go. */
9975 static void
9976 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9978 tree vbase;
9979 tree t;
9980 tree non_primary_binfo;
9982 /* If there are no virtual baseclasses, then there is nothing to
9983 do. */
9984 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9985 return;
9987 t = vid->derived;
9989 /* We might be a primary base class. Go up the inheritance hierarchy
9990 until we find the most derived class of which we are a primary base:
9991 it is the offset of that which we need to use. */
9992 non_primary_binfo = binfo;
9993 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9995 tree b;
9997 /* If we have reached a virtual base, then it must be a primary
9998 base (possibly multi-level) of vid->binfo, or we wouldn't
9999 have called build_vcall_and_vbase_vtbl_entries for it. But it
10000 might be a lost primary, so just skip down to vid->binfo. */
10001 if (BINFO_VIRTUAL_P (non_primary_binfo))
10003 non_primary_binfo = vid->binfo;
10004 break;
10007 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
10008 if (get_primary_binfo (b) != non_primary_binfo)
10009 break;
10010 non_primary_binfo = b;
10013 /* Go through the virtual bases, adding the offsets. */
10014 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10015 vbase;
10016 vbase = TREE_CHAIN (vbase))
10018 tree b;
10019 tree delta;
10021 if (!BINFO_VIRTUAL_P (vbase))
10022 continue;
10024 /* Find the instance of this virtual base in the complete
10025 object. */
10026 b = copied_binfo (vbase, binfo);
10028 /* If we've already got an offset for this virtual base, we
10029 don't need another one. */
10030 if (BINFO_VTABLE_PATH_MARKED (b))
10031 continue;
10032 BINFO_VTABLE_PATH_MARKED (b) = 1;
10034 /* Figure out where we can find this vbase offset. */
10035 delta = size_binop (MULT_EXPR,
10036 vid->index,
10037 fold_convert (ssizetype,
10038 TYPE_SIZE_UNIT (vtable_entry_type)));
10039 if (vid->primary_vtbl_p)
10040 BINFO_VPTR_FIELD (b) = delta;
10042 if (binfo != TYPE_BINFO (t))
10043 /* The vbase offset had better be the same. */
10044 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
10046 /* The next vbase will come at a more negative offset. */
10047 vid->index = size_binop (MINUS_EXPR, vid->index,
10048 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10050 /* The initializer is the delta from BINFO to this virtual base.
10051 The vbase offsets go in reverse inheritance-graph order, and
10052 we are walking in inheritance graph order so these end up in
10053 the right order. */
10054 delta = size_diffop_loc (input_location,
10055 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
10057 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
10058 fold_build1_loc (input_location, NOP_EXPR,
10059 vtable_entry_type, delta));
10063 /* Adds the initializers for the vcall offset entries in the vtable
10064 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10065 to VID->INITS. */
10067 static void
10068 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10070 /* We only need these entries if this base is a virtual base. We
10071 compute the indices -- but do not add to the vtable -- when
10072 building the main vtable for a class. */
10073 if (binfo == TYPE_BINFO (vid->derived)
10074 || (BINFO_VIRTUAL_P (binfo)
10075 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10076 correspond to VID->DERIVED), we are building a primary
10077 construction virtual table. Since this is a primary
10078 virtual table, we do not need the vcall offsets for
10079 BINFO. */
10080 && binfo != vid->rtti_binfo))
10082 /* We need a vcall offset for each of the virtual functions in this
10083 vtable. For example:
10085 class A { virtual void f (); };
10086 class B1 : virtual public A { virtual void f (); };
10087 class B2 : virtual public A { virtual void f (); };
10088 class C: public B1, public B2 { virtual void f (); };
10090 A C object has a primary base of B1, which has a primary base of A. A
10091 C also has a secondary base of B2, which no longer has a primary base
10092 of A. So the B2-in-C construction vtable needs a secondary vtable for
10093 A, which will adjust the A* to a B2* to call f. We have no way of
10094 knowing what (or even whether) this offset will be when we define B2,
10095 so we store this "vcall offset" in the A sub-vtable and look it up in
10096 a "virtual thunk" for B2::f.
10098 We need entries for all the functions in our primary vtable and
10099 in our non-virtual bases' secondary vtables. */
10100 vid->vbase = binfo;
10101 /* If we are just computing the vcall indices -- but do not need
10102 the actual entries -- not that. */
10103 if (!BINFO_VIRTUAL_P (binfo))
10104 vid->generate_vcall_entries = false;
10105 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10106 add_vcall_offset_vtbl_entries_r (binfo, vid);
10110 /* Build vcall offsets, starting with those for BINFO. */
10112 static void
10113 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
10115 int i;
10116 tree primary_binfo;
10117 tree base_binfo;
10119 /* Don't walk into virtual bases -- except, of course, for the
10120 virtual base for which we are building vcall offsets. Any
10121 primary virtual base will have already had its offsets generated
10122 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10123 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
10124 return;
10126 /* If BINFO has a primary base, process it first. */
10127 primary_binfo = get_primary_binfo (binfo);
10128 if (primary_binfo)
10129 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
10131 /* Add BINFO itself to the list. */
10132 add_vcall_offset_vtbl_entries_1 (binfo, vid);
10134 /* Scan the non-primary bases of BINFO. */
10135 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10136 if (base_binfo != primary_binfo)
10137 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
10140 /* Called from build_vcall_offset_vtbl_entries_r. */
10142 static void
10143 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10145 /* Make entries for the rest of the virtuals. */
10146 tree orig_fn;
10148 /* The ABI requires that the methods be processed in declaration
10149 order. */
10150 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
10151 orig_fn;
10152 orig_fn = DECL_CHAIN (orig_fn))
10153 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10154 add_vcall_offset (orig_fn, binfo, vid);
10157 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10159 static void
10160 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10162 size_t i;
10163 tree vcall_offset;
10164 tree derived_entry;
10166 /* If there is already an entry for a function with the same
10167 signature as FN, then we do not need a second vcall offset.
10168 Check the list of functions already present in the derived
10169 class vtable. */
10170 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10172 if (same_signature_p (derived_entry, orig_fn)
10173 /* We only use one vcall offset for virtual destructors,
10174 even though there are two virtual table entries. */
10175 || (DECL_DESTRUCTOR_P (derived_entry)
10176 && DECL_DESTRUCTOR_P (orig_fn)))
10177 return;
10180 /* If we are building these vcall offsets as part of building
10181 the vtable for the most derived class, remember the vcall
10182 offset. */
10183 if (vid->binfo == TYPE_BINFO (vid->derived))
10185 tree_pair_s elt = {orig_fn, vid->index};
10186 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10189 /* The next vcall offset will be found at a more negative
10190 offset. */
10191 vid->index = size_binop (MINUS_EXPR, vid->index,
10192 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10194 /* Keep track of this function. */
10195 vec_safe_push (vid->fns, orig_fn);
10197 if (vid->generate_vcall_entries)
10199 tree base;
10200 tree fn;
10202 /* Find the overriding function. */
10203 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10204 if (fn == error_mark_node)
10205 vcall_offset = build_zero_cst (vtable_entry_type);
10206 else
10208 base = TREE_VALUE (fn);
10210 /* The vbase we're working on is a primary base of
10211 vid->binfo. But it might be a lost primary, so its
10212 BINFO_OFFSET might be wrong, so we just use the
10213 BINFO_OFFSET from vid->binfo. */
10214 vcall_offset = size_diffop_loc (input_location,
10215 BINFO_OFFSET (base),
10216 BINFO_OFFSET (vid->binfo));
10217 vcall_offset = fold_build1_loc (input_location,
10218 NOP_EXPR, vtable_entry_type,
10219 vcall_offset);
10221 /* Add the initializer to the vtable. */
10222 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10226 /* Return vtbl initializers for the RTTI entries corresponding to the
10227 BINFO's vtable. The RTTI entries should indicate the object given
10228 by VID->rtti_binfo. */
10230 static void
10231 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10233 tree b;
10234 tree t;
10235 tree offset;
10236 tree decl;
10237 tree init;
10239 t = BINFO_TYPE (vid->rtti_binfo);
10241 /* To find the complete object, we will first convert to our most
10242 primary base, and then add the offset in the vtbl to that value. */
10243 b = most_primary_binfo (binfo);
10244 offset = size_diffop_loc (input_location,
10245 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10247 /* The second entry is the address of the typeinfo object. */
10248 if (flag_rtti)
10249 decl = build_address (get_tinfo_decl (t));
10250 else
10251 decl = integer_zero_node;
10253 /* Convert the declaration to a type that can be stored in the
10254 vtable. */
10255 init = build_nop (vfunc_ptr_type_node, decl);
10256 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10258 /* Add the offset-to-top entry. It comes earlier in the vtable than
10259 the typeinfo entry. Convert the offset to look like a
10260 function pointer, so that we can put it in the vtable. */
10261 init = build_nop (vfunc_ptr_type_node, offset);
10262 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10265 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10266 accessibility. */
10268 bool
10269 uniquely_derived_from_p (tree parent, tree type)
10271 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10272 return base && base != error_mark_node;
10275 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10277 bool
10278 publicly_uniquely_derived_p (tree parent, tree type)
10280 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10281 NULL, tf_none);
10282 return base && base != error_mark_node;
10285 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10286 class between them, if any. */
10288 tree
10289 common_enclosing_class (tree ctx1, tree ctx2)
10291 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10292 return NULL_TREE;
10293 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10294 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10295 if (ctx1 == ctx2)
10296 return ctx1;
10297 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10298 TYPE_MARKED_P (t) = true;
10299 tree found = NULL_TREE;
10300 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10301 if (TYPE_MARKED_P (t))
10303 found = t;
10304 break;
10306 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10307 TYPE_MARKED_P (t) = false;
10308 return found;
10311 #include "gt-cp-class.h"