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