c++: inherited default constructor [CWG2799]
[official-gcc.git] / gcc / cp / class.cc
blob469e98ed8b7559cf7cfc0fc081565e983d1c625d
1 /* Functions related to building -*- C++ -*- classes and their related objects.
2 Copyright (C) 1987-2023 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_context);
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 (INDIRECT_REF_P (expr))
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,
782 fold_convert (TREE_TYPE (instance_ptr), idx));
784 return aref;
787 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
788 for the given TYPE. */
790 static tree
791 get_vtable_name (tree type)
793 return mangle_vtbl_for_type (type);
796 /* DECL is an entity associated with TYPE, like a virtual table or an
797 implicitly generated constructor. Determine whether or not DECL
798 should have external or internal linkage at the object file
799 level. This routine does not deal with COMDAT linkage and other
800 similar complexities; it simply sets TREE_PUBLIC if it possible for
801 entities in other translation units to contain copies of DECL, in
802 the abstract. */
804 void
805 set_linkage_according_to_type (tree /*type*/, tree decl)
807 TREE_PUBLIC (decl) = 1;
808 determine_visibility (decl);
811 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
812 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
813 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
815 static tree
816 build_vtable (tree class_type, tree name, tree vtable_type)
818 tree decl;
820 decl = build_lang_decl (VAR_DECL, name, vtable_type);
821 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
822 now to avoid confusion in mangle_decl. */
823 SET_DECL_ASSEMBLER_NAME (decl, name);
824 DECL_CONTEXT (decl) = class_type;
825 DECL_ARTIFICIAL (decl) = 1;
826 TREE_STATIC (decl) = 1;
827 TREE_READONLY (decl) = 1;
828 DECL_VIRTUAL_P (decl) = 1;
829 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
830 DECL_USER_ALIGN (decl) = true;
831 DECL_VTABLE_OR_VTT_P (decl) = 1;
832 set_linkage_according_to_type (class_type, decl);
833 /* The vtable has not been defined -- yet. */
834 DECL_EXTERNAL (decl) = 1;
835 DECL_NOT_REALLY_EXTERN (decl) = 1;
837 /* Mark the VAR_DECL node representing the vtable itself as a
838 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
839 is rather important that such things be ignored because any
840 effort to actually generate DWARF for them will run into
841 trouble when/if we encounter code like:
843 #pragma interface
844 struct S { virtual void member (); };
846 because the artificial declaration of the vtable itself (as
847 manufactured by the g++ front end) will say that the vtable is
848 a static member of `S' but only *after* the debug output for
849 the definition of `S' has already been output. This causes
850 grief because the DWARF entry for the definition of the vtable
851 will try to refer back to an earlier *declaration* of the
852 vtable as a static member of `S' and there won't be one. We
853 might be able to arrange to have the "vtable static member"
854 attached to the member list for `S' before the debug info for
855 `S' get written (which would solve the problem) but that would
856 require more intrusive changes to the g++ front end. */
857 DECL_IGNORED_P (decl) = 1;
859 return decl;
862 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
863 or even complete. If this does not exist, create it. If COMPLETE is
864 nonzero, then complete the definition of it -- that will render it
865 impossible to actually build the vtable, but is useful to get at those
866 which are known to exist in the runtime. */
868 tree
869 get_vtable_decl (tree type, int complete)
871 tree decl;
873 if (CLASSTYPE_VTABLES (type))
874 return CLASSTYPE_VTABLES (type);
876 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
877 CLASSTYPE_VTABLES (type) = decl;
879 if (complete)
881 DECL_EXTERNAL (decl) = 1;
882 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
885 return decl;
888 /* Build the primary virtual function table for TYPE. If BINFO is
889 non-NULL, build the vtable starting with the initial approximation
890 that it is the same as the one which is the head of the association
891 list. Returns a nonzero value if a new vtable is actually
892 created. */
894 static int
895 build_primary_vtable (tree binfo, tree type)
897 tree decl;
898 tree virtuals;
900 decl = get_vtable_decl (type, /*complete=*/0);
902 if (binfo)
904 if (BINFO_NEW_VTABLE_MARKED (binfo))
905 /* We have already created a vtable for this base, so there's
906 no need to do it again. */
907 return 0;
909 virtuals = copy_list (BINFO_VIRTUALS (binfo));
910 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
911 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
912 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
914 else
916 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
917 virtuals = NULL_TREE;
920 /* Initialize the association list for this type, based
921 on our first approximation. */
922 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
923 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
924 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
925 return 1;
928 /* Give BINFO a new virtual function table which is initialized
929 with a skeleton-copy of its original initialization. The only
930 entry that changes is the `delta' entry, so we can really
931 share a lot of structure.
933 FOR_TYPE is the most derived type which caused this table to
934 be needed.
936 Returns nonzero if we haven't met BINFO before.
938 The order in which vtables are built (by calling this function) for
939 an object must remain the same, otherwise a binary incompatibility
940 can result. */
942 static int
943 build_secondary_vtable (tree binfo)
945 if (BINFO_NEW_VTABLE_MARKED (binfo))
946 /* We already created a vtable for this base. There's no need to
947 do it again. */
948 return 0;
950 /* Remember that we've created a vtable for this BINFO, so that we
951 don't try to do so again. */
952 SET_BINFO_NEW_VTABLE_MARKED (binfo);
954 /* Make fresh virtual list, so we can smash it later. */
955 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
957 /* Secondary vtables are laid out as part of the same structure as
958 the primary vtable. */
959 BINFO_VTABLE (binfo) = NULL_TREE;
960 return 1;
963 /* Create a new vtable for BINFO which is the hierarchy dominated by
964 T. Return nonzero if we actually created a new vtable. */
966 static int
967 make_new_vtable (tree t, tree binfo)
969 if (binfo == TYPE_BINFO (t))
970 /* In this case, it is *type*'s vtable we are modifying. We start
971 with the approximation that its vtable is that of the
972 immediate base class. */
973 return build_primary_vtable (binfo, t);
974 else
975 /* This is our very own copy of `basetype' to play with. Later,
976 we will fill in all the virtual functions that override the
977 virtual functions in these base classes which are not defined
978 by the current type. */
979 return build_secondary_vtable (binfo);
982 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
983 (which is in the hierarchy dominated by T) list FNDECL as its
984 BV_FN. DELTA is the required constant adjustment from the `this'
985 pointer where the vtable entry appears to the `this' required when
986 the function is actually called. */
988 static void
989 modify_vtable_entry (tree t,
990 tree binfo,
991 tree fndecl,
992 tree delta,
993 tree *virtuals)
995 tree v;
997 v = *virtuals;
999 if (fndecl != BV_FN (v)
1000 || !tree_int_cst_equal (delta, BV_DELTA (v)))
1002 /* We need a new vtable for BINFO. */
1003 if (make_new_vtable (t, binfo))
1005 /* If we really did make a new vtable, we also made a copy
1006 of the BINFO_VIRTUALS list. Now, we have to find the
1007 corresponding entry in that list. */
1008 *virtuals = BINFO_VIRTUALS (binfo);
1009 while (BV_FN (*virtuals) != BV_FN (v))
1010 *virtuals = TREE_CHAIN (*virtuals);
1011 v = *virtuals;
1014 BV_DELTA (v) = delta;
1015 BV_VCALL_INDEX (v) = NULL_TREE;
1016 BV_FN (v) = fndecl;
1021 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1022 METHOD is being injected via a using_decl. Returns true if the
1023 method could be added to the method vec. */
1025 bool
1026 add_method (tree type, tree method, bool via_using)
1028 if (method == error_mark_node)
1029 return false;
1031 gcc_assert (!DECL_EXTERN_C_P (method));
1033 tree *slot = find_member_slot (type, DECL_NAME (method));
1034 tree current_fns = slot ? *slot : NULL_TREE;
1036 /* See below. */
1037 int losem = -1;
1039 /* Check to see if we've already got this method. */
1040 for (ovl_iterator iter (current_fns); iter; ++iter)
1042 tree fn = *iter;
1044 if (TREE_CODE (fn) != TREE_CODE (method))
1045 continue;
1047 /* Two using-declarations can coexist, we'll complain about ambiguity in
1048 overload resolution. */
1049 if (via_using && iter.using_p ()
1050 /* Except handle inherited constructors specially. */
1051 && ! DECL_CONSTRUCTOR_P (fn))
1053 if (fn == method)
1054 /* Don't add the same one twice. */
1055 return false;
1056 continue;
1059 /* [over.load] Member function declarations with the
1060 same name and the same parameter types cannot be
1061 overloaded if any of them is a static member
1062 function declaration.
1064 [over.load] Member function declarations with the same name and
1065 the same parameter-type-list as well as member function template
1066 declarations with the same name, the same parameter-type-list, and
1067 the same template parameter lists cannot be overloaded if any of
1068 them, but not all, have a ref-qualifier.
1070 [namespace.udecl] When a using-declaration brings names
1071 from a base class into a derived class scope, member
1072 functions in the derived class override and/or hide member
1073 functions with the same name and parameter types in a base
1074 class (rather than conflicting). */
1075 tree fn_type = TREE_TYPE (fn);
1076 tree method_type = TREE_TYPE (method);
1078 /* Compare the quals on the 'this' parm. Don't compare
1079 the whole types, as used functions are treated as
1080 coming from the using class in overload resolution. */
1081 if (! DECL_STATIC_FUNCTION_P (fn)
1082 && ! DECL_STATIC_FUNCTION_P (method)
1083 /* Either both or neither need to be ref-qualified for
1084 differing quals to allow overloading. */
1085 && (FUNCTION_REF_QUALIFIED (fn_type)
1086 == FUNCTION_REF_QUALIFIED (method_type))
1087 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1088 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1089 continue;
1091 tree real_fn = fn;
1092 tree real_method = method;
1094 /* Templates and conversion ops must match return types. */
1095 if ((DECL_CONV_FN_P (fn) || TREE_CODE (fn) == TEMPLATE_DECL)
1096 && !same_type_p (TREE_TYPE (fn_type), TREE_TYPE (method_type)))
1097 continue;
1099 /* For templates, the template parameters must be identical. */
1100 if (TREE_CODE (fn) == TEMPLATE_DECL)
1102 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1103 DECL_TEMPLATE_PARMS (method)))
1104 continue;
1106 real_fn = DECL_TEMPLATE_RESULT (fn);
1107 real_method = DECL_TEMPLATE_RESULT (method);
1110 tree parms1 = TYPE_ARG_TYPES (fn_type);
1111 tree parms2 = TYPE_ARG_TYPES (method_type);
1112 if (! DECL_STATIC_FUNCTION_P (real_fn))
1113 parms1 = TREE_CHAIN (parms1);
1114 if (! DECL_STATIC_FUNCTION_P (real_method))
1115 parms2 = TREE_CHAIN (parms2);
1117 /* Bring back parameters omitted from an inherited ctor. The
1118 method and the function can have different omittedness. */
1119 if (ctor_omit_inherited_parms (real_fn))
1120 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn));
1121 if (ctor_omit_inherited_parms (real_method))
1122 parms2 = (FUNCTION_FIRST_USER_PARMTYPE
1123 (DECL_CLONED_FUNCTION (real_method)));
1125 if (!compparms (parms1, parms2))
1126 continue;
1128 if (!equivalently_constrained (fn, method))
1130 if (processing_template_decl)
1131 /* We can't check satisfaction in dependent context, wait until
1132 the class is instantiated. */
1133 continue;
1135 special_function_kind sfk = special_memfn_p (method);
1137 if (sfk == sfk_none
1138 || DECL_INHERITED_CTOR (fn)
1139 || TREE_CODE (fn) == TEMPLATE_DECL)
1140 /* Member function templates and non-special member functions
1141 coexist if they are not equivalently constrained. A member
1142 function is not hidden by an inherited constructor. */
1143 continue;
1145 /* P0848: For special member functions, deleted, unsatisfied, or
1146 less constrained overloads are ineligible. We implement this
1147 by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1148 use the notion of eligibility, and the selected destructor can
1149 be deleted, but removing unsatisfied or less constrained
1150 overloads has the same effect as overload resolution. */
1151 bool dtor = (sfk == sfk_destructor);
1152 if (losem == -1)
1153 losem = ((!dtor && DECL_DELETED_FN (method))
1154 || !constraints_satisfied_p (method));
1155 bool losef = ((!dtor && DECL_DELETED_FN (fn))
1156 || !constraints_satisfied_p (fn));
1157 int win;
1158 if (losem || losef)
1159 win = losem - losef;
1160 else
1161 win = more_constrained (fn, method);
1162 if (win > 0)
1163 /* Leave FN in the method vec, discard METHOD. */
1164 return false;
1165 else if (win < 0)
1167 /* Remove FN, add METHOD. */
1168 current_fns = iter.remove_node (current_fns);
1169 continue;
1171 else
1172 /* Let them coexist for now. */
1173 continue;
1176 /* If these are versions of the same function, process and
1177 move on. */
1178 if (TREE_CODE (fn) == FUNCTION_DECL
1179 && maybe_version_functions (method, fn, true))
1180 continue;
1182 if (DECL_INHERITED_CTOR (method))
1184 if (!DECL_INHERITED_CTOR (fn))
1185 /* Defer to the other function. */
1186 return false;
1188 tree basem = DECL_INHERITED_CTOR_BASE (method);
1189 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1190 if (flag_new_inheriting_ctors)
1192 if (basem == basef)
1194 /* Inheriting the same constructor along different
1195 paths, combine them. */
1196 SET_DECL_INHERITED_CTOR
1197 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1198 DECL_INHERITED_CTOR (fn)));
1199 /* And discard the new one. */
1200 return false;
1202 else
1203 /* Inherited ctors can coexist until overload
1204 resolution. */
1205 continue;
1208 error_at (DECL_SOURCE_LOCATION (method),
1209 "%q#D conflicts with version inherited from %qT",
1210 method, basef);
1211 inform (DECL_SOURCE_LOCATION (fn),
1212 "version inherited from %qT declared here",
1213 basef);
1214 return false;
1217 if (via_using)
1218 /* Defer to the local function. */
1219 return false;
1220 else if (iter.using_p ()
1221 || (flag_new_inheriting_ctors
1222 && DECL_INHERITED_CTOR (fn)))
1224 /* Remove the inherited function. */
1225 current_fns = iter.remove_node (current_fns);
1226 continue;
1228 else
1230 error_at (DECL_SOURCE_LOCATION (method),
1231 "%q#D cannot be overloaded with %q#D", method, fn);
1232 inform (DECL_SOURCE_LOCATION (fn),
1233 "previous declaration %q#D", fn);
1234 return false;
1238 current_fns = ovl_insert (method, current_fns, via_using);
1240 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1241 && !push_class_level_binding (DECL_NAME (method), current_fns))
1242 return false;
1244 if (!slot)
1245 slot = add_member_slot (type, DECL_NAME (method));
1247 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1248 grok_special_member_properties (method);
1250 *slot = current_fns;
1252 return true;
1255 /* Subroutines of finish_struct. */
1257 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1258 legit, otherwise return 0. */
1260 static int
1261 alter_access (tree t, tree fdecl, tree access)
1263 tree elem;
1265 retrofit_lang_decl (fdecl);
1267 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1269 elem = purpose_member (t, DECL_ACCESS (fdecl));
1270 if (elem)
1272 if (TREE_VALUE (elem) != access)
1274 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1275 error ("conflicting access specifications for method"
1276 " %q+D, ignored", TREE_TYPE (fdecl));
1277 else
1278 error ("conflicting access specifications for field %qE, ignored",
1279 DECL_NAME (fdecl));
1281 else
1283 /* They're changing the access to the same thing they changed
1284 it to before. That's OK. */
1288 else
1290 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1291 tf_warning_or_error);
1292 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1293 return 1;
1295 return 0;
1298 /* Return the access node for DECL's access in its enclosing class. */
1300 tree
1301 declared_access (tree decl)
1303 return (TREE_PRIVATE (decl) ? access_private_node
1304 : TREE_PROTECTED (decl) ? access_protected_node
1305 : access_public_node);
1308 /* If DECL is a non-dependent using of non-ctor function members, push them
1309 and return true, otherwise return false. Called from
1310 finish_member_declaration. */
1312 bool
1313 maybe_push_used_methods (tree decl)
1315 if (TREE_CODE (decl) != USING_DECL)
1316 return false;
1317 tree used = strip_using_decl (decl);
1318 if (!used || !is_overloaded_fn (used))
1319 return false;
1321 /* Add the functions to CLASSTYPE_MEMBER_VEC so that overload resolution
1322 works within the class body. */
1323 for (tree f : ovl_range (used))
1325 if (DECL_CONSTRUCTOR_P (f))
1326 /* Inheriting constructors are handled separately. */
1327 return false;
1329 bool added = add_method (current_class_type, f, true);
1331 if (added)
1332 alter_access (current_class_type, f, current_access_specifier);
1334 /* If add_method returns false because f was already declared, look
1335 for a duplicate using-declaration. */
1336 else
1337 for (tree d = TYPE_FIELDS (current_class_type); d; d = DECL_CHAIN (d))
1338 if (TREE_CODE (d) == USING_DECL
1339 && DECL_NAME (d) == DECL_NAME (decl)
1340 && same_type_p (USING_DECL_SCOPE (d), USING_DECL_SCOPE (decl)))
1342 diagnose_name_conflict (decl, d);
1343 break;
1346 return true;
1349 /* Process the USING_DECL, which is a member of T. */
1351 static void
1352 handle_using_decl (tree using_decl, tree t)
1354 tree decl = USING_DECL_DECLS (using_decl);
1356 gcc_assert (!processing_template_decl && decl);
1358 cp_emit_debug_info_for_using (decl, t);
1360 if (is_overloaded_fn (decl))
1361 /* Handled in maybe_push_used_methods. */
1362 return;
1364 tree name = DECL_NAME (using_decl);
1365 tree old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1366 tf_warning_or_error);
1367 if (old_value)
1369 old_value = OVL_FIRST (old_value);
1371 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1372 /* OK */;
1373 else
1374 old_value = NULL_TREE;
1377 if (! old_value)
1379 else if (is_overloaded_fn (old_value))
1381 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1382 "because of local method %q#D with same name",
1383 using_decl, t, old_value);
1384 inform (DECL_SOURCE_LOCATION (old_value),
1385 "local method %q#D declared here", old_value);
1386 return;
1388 else if (!DECL_ARTIFICIAL (old_value))
1390 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1391 "because of local member %q#D with same name",
1392 using_decl, t, old_value);
1393 inform (DECL_SOURCE_LOCATION (old_value),
1394 "local member %q#D declared here", old_value);
1395 return;
1398 iloc_sentinel ils (DECL_SOURCE_LOCATION (using_decl));
1399 tree access = declared_access (using_decl);
1401 /* Make type T see field decl FDECL with access ACCESS. */
1402 if (USING_DECL_UNRELATED_P (using_decl))
1404 /* C++20 using enum can import non-inherited enumerators into class
1405 scope. We implement that by making a copy of the CONST_DECL for which
1406 CONST_DECL_USING_P is true. */
1407 gcc_assert (TREE_CODE (decl) == CONST_DECL);
1409 auto cas = make_temp_override (current_access_specifier, access);
1410 tree copy = copy_decl (decl);
1411 DECL_CONTEXT (copy) = t;
1412 DECL_ARTIFICIAL (copy) = true;
1413 /* We emitted debug info for the USING_DECL above; make sure we don't
1414 also emit anything for this clone. */
1415 DECL_IGNORED_P (copy) = true;
1416 DECL_SOURCE_LOCATION (copy) = DECL_SOURCE_LOCATION (using_decl);
1417 finish_member_declaration (copy);
1418 DECL_ABSTRACT_ORIGIN (copy) = decl;
1420 else
1421 alter_access (t, decl, access);
1424 /* Data structure for find_abi_tags_r, below. */
1426 struct abi_tag_data
1428 tree t; // The type that we're checking for missing tags.
1429 tree subob; // The subobject of T that we're getting tags from.
1430 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1433 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1434 in the context of P. TAG can be either an identifier (the DECL_NAME of
1435 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1437 static void
1438 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1440 if (!IDENTIFIER_MARKED (id))
1442 if (p->tags != error_mark_node)
1444 /* We're collecting tags from template arguments or from
1445 the type of a variable or function return type. */
1446 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1448 /* Don't inherit this tag multiple times. */
1449 IDENTIFIER_MARKED (id) = true;
1451 if (TYPE_P (p->t))
1453 /* Tags inherited from type template arguments are only used
1454 to avoid warnings. */
1455 ABI_TAG_IMPLICIT (p->tags) = true;
1456 return;
1458 /* For functions and variables we want to warn, too. */
1461 /* Otherwise we're diagnosing missing tags. */
1462 if (TREE_CODE (p->t) == FUNCTION_DECL)
1464 auto_diagnostic_group d;
1465 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1466 "that %qT (used in its return type) has",
1467 p->t, tag, *tp))
1468 inform (location_of (*tp), "%qT declared here", *tp);
1470 else if (VAR_P (p->t))
1472 auto_diagnostic_group d;
1473 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1474 "that %qT (used in its type) has", p->t, tag, *tp))
1475 inform (location_of (*tp), "%qT declared here", *tp);
1477 else if (TYPE_P (p->subob))
1479 auto_diagnostic_group d;
1480 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1481 "that base %qT has", p->t, tag, p->subob))
1482 inform (location_of (p->subob), "%qT declared here",
1483 p->subob);
1485 else
1487 auto_diagnostic_group d;
1488 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1489 "that %qT (used in the type of %qD) has",
1490 p->t, tag, *tp, p->subob))
1492 inform (location_of (p->subob), "%qD declared here",
1493 p->subob);
1494 inform (location_of (*tp), "%qT declared here", *tp);
1500 /* Find all the ABI tags in the attribute list ATTR and either call
1501 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1503 static void
1504 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1506 if (!attr)
1507 return;
1508 for (; (attr = lookup_attribute ("abi_tag", attr));
1509 attr = TREE_CHAIN (attr))
1510 for (tree list = TREE_VALUE (attr); list;
1511 list = TREE_CHAIN (list))
1513 tree tag = TREE_VALUE (list);
1514 tree id = get_identifier (TREE_STRING_POINTER (tag));
1515 if (tp)
1516 check_tag (tag, id, tp, p);
1517 else
1518 IDENTIFIER_MARKED (id) = val;
1522 /* Find all the ABI tags on T and its enclosing scopes and either call
1523 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1525 static void
1526 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1528 while (t != global_namespace)
1530 tree attr;
1531 if (TYPE_P (t))
1533 attr = TYPE_ATTRIBUTES (t);
1534 t = CP_TYPE_CONTEXT (t);
1536 else
1538 attr = DECL_ATTRIBUTES (t);
1539 t = CP_DECL_CONTEXT (t);
1541 mark_or_check_attr_tags (attr, tp, p, val);
1545 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1546 types with ABI tags, add the corresponding identifiers to the VEC in
1547 *DATA and set IDENTIFIER_MARKED. */
1549 static tree
1550 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1552 if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1553 /* Tell cp_walk_subtrees to look though typedefs. [PR98481] */
1554 *walk_subtrees = 2;
1556 if (!OVERLOAD_TYPE_P (*tp))
1557 return NULL_TREE;
1559 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1560 anyway, but let's make sure of it. */
1561 *walk_subtrees = false;
1563 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1565 mark_or_check_tags (*tp, tp, p, false);
1567 return NULL_TREE;
1570 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1571 IDENTIFIER_MARKED on its ABI tags. */
1573 static tree
1574 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1576 if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1577 /* Tell cp_walk_subtrees to look though typedefs. */
1578 *walk_subtrees = 2;
1580 if (!OVERLOAD_TYPE_P (*tp))
1581 return NULL_TREE;
1583 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1584 anyway, but let's make sure of it. */
1585 *walk_subtrees = false;
1587 bool *valp = static_cast<bool*>(data);
1589 mark_or_check_tags (*tp, NULL, NULL, *valp);
1591 return NULL_TREE;
1594 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1595 scopes. */
1597 static void
1598 mark_abi_tags (tree t, bool val)
1600 mark_or_check_tags (t, NULL, NULL, val);
1601 if (DECL_P (t))
1603 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1604 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1606 /* Template arguments are part of the signature. */
1607 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1608 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1610 tree arg = TREE_VEC_ELT (level, j);
1611 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1614 if (TREE_CODE (t) == FUNCTION_DECL)
1615 /* A function's parameter types are part of the signature, so
1616 we don't need to inherit any tags that are also in them. */
1617 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1618 arg = TREE_CHAIN (arg))
1619 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1620 mark_abi_tags_r, &val);
1624 /* Check that T has all the ABI tags that subobject SUBOB has, or
1625 warn if not. If T is a (variable or function) declaration, also
1626 return any missing tags, and add them to T if JUST_CHECKING is false. */
1628 static tree
1629 check_abi_tags (tree t, tree subob, bool just_checking = false)
1631 bool inherit = DECL_P (t);
1633 if (!inherit && !warn_abi_tag)
1634 return NULL_TREE;
1636 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1637 if (!TREE_PUBLIC (decl))
1638 /* No need to worry about things local to this TU. */
1639 return NULL_TREE;
1641 mark_abi_tags (t, true);
1643 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1644 struct abi_tag_data data = { t, subob, error_mark_node };
1645 if (inherit)
1646 data.tags = NULL_TREE;
1648 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1650 if (!(inherit && data.tags))
1651 /* We don't need to do anything with data.tags. */;
1652 else if (just_checking)
1653 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1655 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1656 IDENTIFIER_MARKED (id) = false;
1658 else
1660 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1661 if (attr)
1662 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1663 else
1664 DECL_ATTRIBUTES (t)
1665 = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1668 mark_abi_tags (t, false);
1670 return data.tags;
1673 /* Check that DECL has all the ABI tags that are used in parts of its type
1674 that are not reflected in its mangled name. */
1676 void
1677 check_abi_tags (tree decl)
1679 if (VAR_P (decl))
1680 check_abi_tags (decl, TREE_TYPE (decl));
1681 else if (TREE_CODE (decl) == FUNCTION_DECL
1682 && !DECL_CONV_FN_P (decl)
1683 && !mangle_return_type_p (decl))
1684 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1687 /* Return any ABI tags that are used in parts of the type of DECL
1688 that are not reflected in its mangled name. This function is only
1689 used in backward-compatible mangling for ABI <11. */
1691 tree
1692 missing_abi_tags (tree decl)
1694 if (VAR_P (decl))
1695 return check_abi_tags (decl, TREE_TYPE (decl), true);
1696 else if (TREE_CODE (decl) == FUNCTION_DECL
1697 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1698 that we can use this function for setting need_abi_warning
1699 regardless of the current flag_abi_version. */
1700 && !mangle_return_type_p (decl))
1701 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1702 else
1703 return NULL_TREE;
1706 void
1707 inherit_targ_abi_tags (tree t)
1709 if (!CLASS_TYPE_P (t)
1710 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1711 return;
1713 mark_abi_tags (t, true);
1715 tree args = CLASSTYPE_TI_ARGS (t);
1716 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1717 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1719 tree level = TMPL_ARGS_LEVEL (args, i+1);
1720 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1722 tree arg = TREE_VEC_ELT (level, j);
1723 data.subob = arg;
1724 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1728 // If we found some tags on our template arguments, add them to our
1729 // abi_tag attribute.
1730 if (data.tags)
1732 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1733 if (attr)
1734 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1735 else
1736 TYPE_ATTRIBUTES (t)
1737 = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
1740 mark_abi_tags (t, false);
1743 /* Return true, iff class T has a non-virtual destructor that is
1744 accessible from outside the class heirarchy (i.e. is public, or
1745 there's a suitable friend. */
1747 static bool
1748 accessible_nvdtor_p (tree t)
1750 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1752 /* An implicitly declared destructor is always public. And,
1753 if it were virtual, we would have created it by now. */
1754 if (!dtor)
1755 return true;
1757 if (DECL_VINDEX (dtor))
1758 return false; /* Virtual */
1760 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1761 return true; /* Public */
1763 if (CLASSTYPE_FRIEND_CLASSES (t)
1764 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1765 return true; /* Has friends */
1767 return false;
1770 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1771 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1772 properties of the bases. */
1774 static void
1775 check_bases (tree t,
1776 int* cant_have_const_ctor_p,
1777 int* no_const_asn_ref_p)
1779 int i;
1780 bool seen_non_virtual_nearly_empty_base_p = 0;
1781 int seen_tm_mask = 0;
1782 tree base_binfo;
1783 tree binfo;
1784 tree field = NULL_TREE;
1786 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1787 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1788 if (TREE_CODE (field) == FIELD_DECL)
1789 break;
1791 for (binfo = TYPE_BINFO (t), i = 0;
1792 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1794 tree basetype = TREE_TYPE (base_binfo);
1796 gcc_assert (COMPLETE_TYPE_P (basetype));
1798 if (CLASSTYPE_FINAL (basetype))
1799 error ("cannot derive from %<final%> base %qT in derived type %qT",
1800 basetype, t);
1802 /* If any base class is non-literal, so is the derived class. */
1803 if (!CLASSTYPE_LITERAL_P (basetype))
1804 CLASSTYPE_LITERAL_P (t) = false;
1806 /* If the base class doesn't have copy constructors or
1807 assignment operators that take const references, then the
1808 derived class cannot have such a member automatically
1809 generated. */
1810 if (TYPE_HAS_COPY_CTOR (basetype)
1811 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1812 *cant_have_const_ctor_p = 1;
1813 if (TYPE_HAS_COPY_ASSIGN (basetype)
1814 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1815 *no_const_asn_ref_p = 1;
1817 if (BINFO_VIRTUAL_P (base_binfo))
1818 /* A virtual base does not effect nearly emptiness. */
1820 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1822 if (seen_non_virtual_nearly_empty_base_p)
1823 /* And if there is more than one nearly empty base, then the
1824 derived class is not nearly empty either. */
1825 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1826 else
1827 /* Remember we've seen one. */
1828 seen_non_virtual_nearly_empty_base_p = 1;
1830 else if (!is_empty_class (basetype))
1831 /* If the base class is not empty or nearly empty, then this
1832 class cannot be nearly empty. */
1833 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1835 /* A lot of properties from the bases also apply to the derived
1836 class. */
1837 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1838 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1839 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1840 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1841 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1842 || !TYPE_HAS_COPY_ASSIGN (basetype));
1843 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1844 || !TYPE_HAS_COPY_CTOR (basetype));
1845 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1846 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1847 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1848 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1849 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1850 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1851 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1852 || TYPE_HAS_COMPLEX_DFLT (basetype));
1853 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1854 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1855 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1856 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1857 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1858 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1859 if (TYPE_HAS_MUTABLE_P (basetype))
1860 CLASSTYPE_HAS_MUTABLE (t) = 1;
1862 /* A standard-layout class is a class that:
1864 * has no non-standard-layout base classes, */
1865 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1866 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1868 tree basefield;
1869 /* ...has no base classes of the same type as the first non-static
1870 data member... */
1871 if (field && DECL_CONTEXT (field) == t
1872 && (same_type_ignoring_top_level_qualifiers_p
1873 (TREE_TYPE (field), basetype)))
1874 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1875 /* DR 1813:
1876 ...has at most one base class subobject of any given type... */
1877 else if (CLASSTYPE_REPEATED_BASE_P (t))
1878 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1879 else
1880 /* ...has all non-static data members and bit-fields in the class
1881 and its base classes first declared in the same class. */
1882 for (basefield = TYPE_FIELDS (basetype); basefield;
1883 basefield = DECL_CHAIN (basefield))
1884 if (TREE_CODE (basefield) == FIELD_DECL
1885 && !(DECL_FIELD_IS_BASE (basefield)
1886 && is_empty_field (basefield)))
1888 if (field)
1889 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1890 else
1891 field = basefield;
1892 break;
1896 /* Don't bother collecting tm attributes if transactional memory
1897 support is not enabled. */
1898 if (flag_tm)
1900 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1901 if (tm_attr)
1902 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1905 check_abi_tags (t, basetype);
1908 /* If one of the base classes had TM attributes, and the current class
1909 doesn't define its own, then the current class inherits one. */
1910 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1912 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1913 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1917 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1918 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1919 that have had a nearly-empty virtual primary base stolen by some
1920 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1921 T. */
1923 static void
1924 determine_primary_bases (tree t)
1926 unsigned i;
1927 tree primary = NULL_TREE;
1928 tree type_binfo = TYPE_BINFO (t);
1929 tree base_binfo;
1931 /* Determine the primary bases of our bases. */
1932 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1933 base_binfo = TREE_CHAIN (base_binfo))
1935 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1937 /* See if we're the non-virtual primary of our inheritance
1938 chain. */
1939 if (!BINFO_VIRTUAL_P (base_binfo))
1941 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1942 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1944 if (parent_primary
1945 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1946 BINFO_TYPE (parent_primary)))
1947 /* We are the primary binfo. */
1948 BINFO_PRIMARY_P (base_binfo) = 1;
1950 /* Determine if we have a virtual primary base, and mark it so.
1952 if (primary && BINFO_VIRTUAL_P (primary))
1954 tree this_primary = copied_binfo (primary, base_binfo);
1956 if (BINFO_PRIMARY_P (this_primary))
1957 /* Someone already claimed this base. */
1958 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1959 else
1961 tree delta;
1963 BINFO_PRIMARY_P (this_primary) = 1;
1964 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1966 /* A virtual binfo might have been copied from within
1967 another hierarchy. As we're about to use it as a
1968 primary base, make sure the offsets match. */
1969 delta = size_diffop_loc (input_location,
1970 fold_convert (ssizetype,
1971 BINFO_OFFSET (base_binfo)),
1972 fold_convert (ssizetype,
1973 BINFO_OFFSET (this_primary)));
1975 propagate_binfo_offsets (this_primary, delta);
1980 /* First look for a dynamic direct non-virtual base. */
1981 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1983 tree basetype = BINFO_TYPE (base_binfo);
1985 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1987 primary = base_binfo;
1988 goto found;
1992 /* A "nearly-empty" virtual base class can be the primary base
1993 class, if no non-virtual polymorphic base can be found. Look for
1994 a nearly-empty virtual dynamic base that is not already a primary
1995 base of something in the hierarchy. If there is no such base,
1996 just pick the first nearly-empty virtual base. */
1998 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1999 base_binfo = TREE_CHAIN (base_binfo))
2000 if (BINFO_VIRTUAL_P (base_binfo)
2001 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
2003 if (!BINFO_PRIMARY_P (base_binfo))
2005 /* Found one that is not primary. */
2006 primary = base_binfo;
2007 goto found;
2009 else if (!primary)
2010 /* Remember the first candidate. */
2011 primary = base_binfo;
2014 found:
2015 /* If we've got a primary base, use it. */
2016 if (primary)
2018 tree basetype = BINFO_TYPE (primary);
2020 CLASSTYPE_PRIMARY_BINFO (t) = primary;
2021 if (BINFO_PRIMARY_P (primary))
2022 /* We are stealing a primary base. */
2023 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
2024 BINFO_PRIMARY_P (primary) = 1;
2025 if (BINFO_VIRTUAL_P (primary))
2027 tree delta;
2029 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
2030 /* A virtual binfo might have been copied from within
2031 another hierarchy. As we're about to use it as a primary
2032 base, make sure the offsets match. */
2033 delta = size_diffop_loc (input_location, ssize_int (0),
2034 fold_convert (ssizetype, BINFO_OFFSET (primary)));
2036 propagate_binfo_offsets (primary, delta);
2039 primary = TYPE_BINFO (basetype);
2041 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2042 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2043 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2047 /* Update the variant types of T. */
2049 void
2050 fixup_type_variants (tree type)
2052 if (!type)
2053 return;
2055 for (tree variant = TYPE_NEXT_VARIANT (type);
2056 variant;
2057 variant = TYPE_NEXT_VARIANT (variant))
2059 /* These fields are in the _TYPE part of the node, not in
2060 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2061 TYPE_HAS_USER_CONSTRUCTOR (variant) = TYPE_HAS_USER_CONSTRUCTOR (type);
2062 TYPE_NEEDS_CONSTRUCTING (variant) = TYPE_NEEDS_CONSTRUCTING (type);
2063 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variant)
2064 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2066 TYPE_POLYMORPHIC_P (variant) = TYPE_POLYMORPHIC_P (type);
2067 CLASSTYPE_FINAL (variant) = CLASSTYPE_FINAL (type);
2069 TYPE_BINFO (variant) = TYPE_BINFO (type);
2071 /* Copy whatever these are holding today. */
2072 TYPE_VFIELD (variant) = TYPE_VFIELD (type);
2073 TYPE_FIELDS (variant) = TYPE_FIELDS (type);
2075 TYPE_SIZE (variant) = TYPE_SIZE (type);
2076 TYPE_SIZE_UNIT (variant) = TYPE_SIZE_UNIT (type);
2078 if (!TYPE_USER_ALIGN (variant)
2079 || TYPE_NAME (variant) == TYPE_NAME (type)
2080 || TYPE_ALIGN_RAW (variant) < TYPE_ALIGN_RAW (type))
2082 TYPE_ALIGN_RAW (variant) = TYPE_ALIGN_RAW (type);
2083 TYPE_USER_ALIGN (variant) = TYPE_USER_ALIGN (type);
2086 TYPE_PRECISION (variant) = TYPE_PRECISION (type);
2087 TYPE_MODE_RAW (variant) = TYPE_MODE_RAW (type);
2088 TYPE_EMPTY_P (variant) = TYPE_EMPTY_P (type);
2092 /* KLASS is a class that we're applying may_alias to after the body is
2093 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2094 canonical type(s) will be implicitly updated. */
2096 static void
2097 fixup_may_alias (tree klass)
2099 tree t, v;
2101 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2102 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2103 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2104 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2105 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2106 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2109 /* Early variant fixups: we apply attributes at the beginning of the class
2110 definition, and we need to fix up any variants that have already been
2111 made via elaborated-type-specifier so that check_qualified_type works. */
2113 void
2114 fixup_attribute_variants (tree t)
2116 tree variants;
2118 if (!t)
2119 return;
2121 tree attrs = TYPE_ATTRIBUTES (t);
2122 unsigned align = TYPE_ALIGN (t);
2123 bool user_align = TYPE_USER_ALIGN (t);
2124 bool may_alias = lookup_attribute ("may_alias", attrs);
2125 bool packed = TYPE_PACKED (t);
2127 if (may_alias)
2128 fixup_may_alias (t);
2130 for (variants = TYPE_NEXT_VARIANT (t);
2131 variants;
2132 variants = TYPE_NEXT_VARIANT (variants))
2134 /* These are the two fields that check_qualified_type looks at and
2135 are affected by attributes. */
2136 TYPE_ATTRIBUTES (variants) = attrs;
2137 unsigned valign = align;
2138 if (TYPE_USER_ALIGN (variants))
2139 valign = MAX (valign, TYPE_ALIGN (variants));
2140 else
2141 TYPE_USER_ALIGN (variants) = user_align;
2142 SET_TYPE_ALIGN (variants, valign);
2143 TYPE_PACKED (variants) = packed;
2144 if (may_alias)
2145 fixup_may_alias (variants);
2149 /* Set memoizing fields and bits of T (and its variants) for later
2150 use. */
2152 static void
2153 finish_struct_bits (tree t)
2155 /* Fix up variants (if any). */
2156 fixup_type_variants (t);
2158 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2159 /* For a class w/o baseclasses, 'finish_struct' has set
2160 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2161 Similarly for a class whose base classes do not have vtables.
2162 When neither of these is true, we might have removed abstract
2163 virtuals (by providing a definition), added some (by declaring
2164 new ones), or redeclared ones from a base class. We need to
2165 recalculate what's really an abstract virtual at this point (by
2166 looking in the vtables). */
2167 get_pure_virtuals (t);
2169 /* If this type has a copy constructor or a destructor, force its
2170 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2171 nonzero. This will cause it to be passed by invisible reference
2172 and prevent it from being returned in a register. */
2173 if (type_has_nontrivial_copy_init (t)
2174 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2176 tree variants;
2177 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2178 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2180 SET_TYPE_MODE (variants, BLKmode);
2181 TREE_ADDRESSABLE (variants) = 1;
2186 /* Issue warnings about T having private constructors, but no friends,
2187 and so forth.
2189 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2190 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2191 non-private static member functions. */
2193 static void
2194 maybe_warn_about_overly_private_class (tree t)
2196 int has_member_fn = 0;
2197 int has_nonprivate_method = 0;
2198 bool nonprivate_ctor = false;
2200 if (!warn_ctor_dtor_privacy
2201 /* If the class has friends, those entities might create and
2202 access instances, so we should not warn. */
2203 || (CLASSTYPE_FRIEND_CLASSES (t)
2204 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2205 /* We will have warned when the template was declared; there's
2206 no need to warn on every instantiation. */
2207 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2208 /* There's no reason to even consider warning about this
2209 class. */
2210 return;
2212 /* We only issue one warning, if more than one applies, because
2213 otherwise, on code like:
2215 class A {
2216 // Oops - forgot `public:'
2217 A();
2218 A(const A&);
2219 ~A();
2222 we warn several times about essentially the same problem. */
2224 /* Check to see if all (non-constructor, non-destructor) member
2225 functions are private. (Since there are no friends or
2226 non-private statics, we can't ever call any of the private member
2227 functions.) */
2228 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2229 if (TREE_CODE (fn) == USING_DECL
2230 && DECL_NAME (fn) == ctor_identifier
2231 && !TREE_PRIVATE (fn))
2232 nonprivate_ctor = true;
2233 else if (!DECL_DECLARES_FUNCTION_P (fn))
2234 /* Not a function. */;
2235 else if (DECL_ARTIFICIAL (fn))
2236 /* We're not interested in compiler-generated methods; they don't
2237 provide any way to call private members. */;
2238 else if (!TREE_PRIVATE (fn))
2240 if (DECL_STATIC_FUNCTION_P (fn))
2241 /* A non-private static member function is just like a
2242 friend; it can create and invoke private member
2243 functions, and be accessed without a class
2244 instance. */
2245 return;
2247 has_nonprivate_method = 1;
2248 /* Keep searching for a static member function. */
2250 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2251 has_member_fn = 1;
2253 if (!has_nonprivate_method && has_member_fn)
2255 /* There are no non-private methods, and there's at least one
2256 private member function that isn't a constructor or
2257 destructor. (If all the private members are
2258 constructors/destructors we want to use the code below that
2259 issues error messages specifically referring to
2260 constructors/destructors.) */
2261 unsigned i;
2262 tree binfo = TYPE_BINFO (t);
2264 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2265 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2267 has_nonprivate_method = 1;
2268 break;
2270 if (!has_nonprivate_method)
2272 warning (OPT_Wctor_dtor_privacy,
2273 "all member functions in class %qT are private", t);
2274 return;
2278 /* Even if some of the member functions are non-private, the class
2279 won't be useful for much if all the constructors or destructors
2280 are private: such an object can never be created or destroyed. */
2281 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2282 if (TREE_PRIVATE (dtor))
2284 warning (OPT_Wctor_dtor_privacy,
2285 "%q#T only defines a private destructor and has no friends",
2287 return;
2290 /* Warn about classes that have private constructors and no friends. */
2291 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2292 /* Implicitly generated constructors are always public. */
2293 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2295 tree copy_or_move = NULL_TREE;
2297 /* If a non-template class does not define a copy
2298 constructor, one is defined for it, enabling it to avoid
2299 this warning. For a template class, this does not
2300 happen, and so we would normally get a warning on:
2302 template <class T> class C { private: C(); };
2304 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2305 complete non-template or fully instantiated classes have this
2306 flag set. */
2307 if (!TYPE_HAS_COPY_CTOR (t))
2308 nonprivate_ctor = true;
2309 else
2310 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
2311 if (TREE_PRIVATE (fn))
2312 continue;
2313 else if (copy_fn_p (fn) || move_fn_p (fn))
2314 /* Ideally, we wouldn't count any constructor that takes
2315 an argument of the class type as a parameter, because
2316 such things cannot be used to construct an instance of
2317 the class unless you already have one. */
2318 copy_or_move = fn;
2319 else
2321 nonprivate_ctor = true;
2322 break;
2325 if (!nonprivate_ctor)
2327 bool w = warning (OPT_Wctor_dtor_privacy,
2328 "%q#T only defines private constructors and has "
2329 "no friends", t);
2330 if (w && copy_or_move)
2331 inform (DECL_SOURCE_LOCATION (copy_or_move),
2332 "%q#D is public, but requires an existing %q#T object",
2333 copy_or_move, t);
2334 return;
2339 /* Make BINFO's vtable have N entries, including RTTI entries,
2340 vbase and vcall offsets, etc. Set its type and call the back end
2341 to lay it out. */
2343 static void
2344 layout_vtable_decl (tree binfo, int n)
2346 tree atype;
2347 tree vtable;
2349 atype = build_array_of_n_type (vtable_entry_type, n);
2350 layout_type (atype);
2352 /* We may have to grow the vtable. */
2353 vtable = get_vtbl_decl_for_binfo (binfo);
2354 if (!same_type_p (TREE_TYPE (vtable), atype))
2356 TREE_TYPE (vtable) = atype;
2357 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2358 layout_decl (vtable, 0);
2362 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2363 have the same signature. */
2366 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2368 /* One destructor overrides another if they are the same kind of
2369 destructor. */
2370 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2371 && special_function_p (base_fndecl) == special_function_p (fndecl))
2372 return 1;
2373 /* But a non-destructor never overrides a destructor, nor vice
2374 versa, nor do different kinds of destructors override
2375 one-another. For example, a complete object destructor does not
2376 override a deleting destructor. */
2377 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2378 return 0;
2380 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2381 || (DECL_CONV_FN_P (fndecl)
2382 && DECL_CONV_FN_P (base_fndecl)
2383 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2384 DECL_CONV_FN_TYPE (base_fndecl))))
2386 tree fntype = TREE_TYPE (fndecl);
2387 tree base_fntype = TREE_TYPE (base_fndecl);
2388 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2389 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2390 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2391 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2392 return 1;
2394 return 0;
2397 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2398 subobject. */
2400 static bool
2401 base_derived_from (tree derived, tree base)
2403 tree probe;
2405 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2407 if (probe == derived)
2408 return true;
2409 else if (BINFO_VIRTUAL_P (probe))
2410 /* If we meet a virtual base, we can't follow the inheritance
2411 any more. See if the complete type of DERIVED contains
2412 such a virtual base. */
2413 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2414 != NULL_TREE);
2416 return false;
2419 struct find_final_overrider_data {
2420 /* The function for which we are trying to find a final overrider. */
2421 tree fn;
2422 /* The base class in which the function was declared. */
2423 tree declaring_base;
2424 /* The candidate overriders. */
2425 tree candidates;
2426 /* Path to most derived. */
2427 auto_vec<tree> path;
2430 /* Add the overrider along the current path to FFOD->CANDIDATES.
2431 Returns true if an overrider was found; false otherwise. */
2433 static bool
2434 dfs_find_final_overrider_1 (tree binfo,
2435 find_final_overrider_data *ffod,
2436 unsigned depth)
2438 tree method;
2440 /* If BINFO is not the most derived type, try a more derived class.
2441 A definition there will overrider a definition here. */
2442 if (depth)
2444 depth--;
2445 if (dfs_find_final_overrider_1
2446 (ffod->path[depth], ffod, depth))
2447 return true;
2450 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2451 if (method)
2453 tree *candidate = &ffod->candidates;
2455 /* Remove any candidates overridden by this new function. */
2456 while (*candidate)
2458 /* If *CANDIDATE overrides METHOD, then METHOD
2459 cannot override anything else on the list. */
2460 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2461 return true;
2462 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2463 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2464 *candidate = TREE_CHAIN (*candidate);
2465 else
2466 candidate = &TREE_CHAIN (*candidate);
2469 /* Add the new function. */
2470 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2471 return true;
2474 return false;
2477 /* Called from find_final_overrider via dfs_walk. */
2479 static tree
2480 dfs_find_final_overrider_pre (tree binfo, void *data)
2482 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2484 if (binfo == ffod->declaring_base)
2485 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2486 ffod->path.safe_push (binfo);
2488 return NULL_TREE;
2491 static tree
2492 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2494 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2495 ffod->path.pop ();
2497 return NULL_TREE;
2500 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2501 FN and whose TREE_VALUE is the binfo for the base where the
2502 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2503 DERIVED) is the base object in which FN is declared. */
2505 static tree
2506 find_final_overrider (tree derived, tree binfo, tree fn)
2508 find_final_overrider_data ffod;
2510 /* Getting this right is a little tricky. This is valid:
2512 struct S { virtual void f (); };
2513 struct T { virtual void f (); };
2514 struct U : public S, public T { };
2516 even though calling `f' in `U' is ambiguous. But,
2518 struct R { virtual void f(); };
2519 struct S : virtual public R { virtual void f (); };
2520 struct T : virtual public R { virtual void f (); };
2521 struct U : public S, public T { };
2523 is not -- there's no way to decide whether to put `S::f' or
2524 `T::f' in the vtable for `R'.
2526 The solution is to look at all paths to BINFO. If we find
2527 different overriders along any two, then there is a problem. */
2528 if (DECL_THUNK_P (fn))
2529 fn = THUNK_TARGET (fn);
2531 /* Determine the depth of the hierarchy. */
2532 ffod.fn = fn;
2533 ffod.declaring_base = binfo;
2534 ffod.candidates = NULL_TREE;
2535 ffod.path.create (30);
2537 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2538 dfs_find_final_overrider_post, &ffod);
2540 /* If there was no winner, issue an error message. */
2541 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2542 return error_mark_node;
2544 return ffod.candidates;
2547 /* Return the index of the vcall offset for FN when TYPE is used as a
2548 virtual base. */
2550 static tree
2551 get_vcall_index (tree fn, tree type)
2553 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2554 tree_pair_p p;
2555 unsigned ix;
2557 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2558 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2559 || same_signature_p (fn, p->purpose))
2560 return p->value;
2562 /* There should always be an appropriate index. */
2563 gcc_unreachable ();
2566 /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2567 overrider at that index in the vtable. This should only be used when we
2568 know that BINFO is correct for the dynamic type of the object. */
2570 tree
2571 lookup_vfn_in_binfo (tree idx, tree binfo)
2573 int ix = tree_to_shwi (idx);
2574 if (TARGET_VTABLE_USES_DESCRIPTORS)
2575 ix /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
2576 while (BINFO_PRIMARY_P (binfo))
2577 /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2578 class that actually owns the vtable. */
2579 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2580 tree virtuals = BINFO_VIRTUALS (binfo);
2581 return TREE_VALUE (chain_index (ix, virtuals));
2584 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2585 dominated by T. FN is the old function; VIRTUALS points to the
2586 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2587 of that entry in the list. */
2589 static void
2590 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2591 unsigned ix)
2593 tree b;
2594 tree overrider;
2595 tree delta;
2596 tree virtual_base;
2597 tree first_defn;
2598 tree overrider_fn, overrider_target;
2599 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2600 tree over_return, base_return;
2601 bool lost = false;
2603 /* Find the nearest primary base (possibly binfo itself) which defines
2604 this function; this is the class the caller will convert to when
2605 calling FN through BINFO. */
2606 for (b = binfo; ; b = get_primary_binfo (b))
2608 gcc_assert (b);
2609 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2610 break;
2612 /* The nearest definition is from a lost primary. */
2613 if (BINFO_LOST_PRIMARY_P (b))
2614 lost = true;
2616 first_defn = b;
2618 /* Find the final overrider. */
2619 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2620 if (overrider == error_mark_node)
2622 error ("no unique final overrider for %qD in %qT", target_fn, t);
2623 return;
2625 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2627 /* Check for adjusting covariant return types. */
2628 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2629 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2631 if (INDIRECT_TYPE_P (over_return)
2632 && TREE_CODE (over_return) == TREE_CODE (base_return)
2633 && CLASS_TYPE_P (TREE_TYPE (over_return))
2634 && CLASS_TYPE_P (TREE_TYPE (base_return))
2635 /* If the overrider is invalid, don't even try. */
2636 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2638 /* If FN is a covariant thunk, we must figure out the adjustment
2639 to the final base FN was converting to. As OVERRIDER_TARGET might
2640 also be converting to the return type of FN, we have to
2641 combine the two conversions here. */
2642 tree fixed_offset, virtual_offset;
2644 over_return = TREE_TYPE (over_return);
2645 base_return = TREE_TYPE (base_return);
2647 if (DECL_THUNK_P (fn))
2649 gcc_assert (DECL_RESULT_THUNK_P (fn));
2650 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2651 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2653 else
2654 fixed_offset = virtual_offset = NULL_TREE;
2656 if (virtual_offset)
2657 /* Find the equivalent binfo within the return type of the
2658 overriding function. We will want the vbase offset from
2659 there. */
2660 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2661 over_return);
2662 else if (!same_type_ignoring_top_level_qualifiers_p
2663 (over_return, base_return))
2665 /* There was no existing virtual thunk (which takes
2666 precedence). So find the binfo of the base function's
2667 return type within the overriding function's return type.
2668 Fortunately we know the covariancy is valid (it
2669 has already been checked), so we can just iterate along
2670 the binfos, which have been chained in inheritance graph
2671 order. Of course it is lame that we have to repeat the
2672 search here anyway -- we should really be caching pieces
2673 of the vtable and avoiding this repeated work. */
2674 tree thunk_binfo = NULL_TREE;
2675 tree base_binfo = TYPE_BINFO (base_return);
2677 /* Find the base binfo within the overriding function's
2678 return type. We will always find a thunk_binfo, except
2679 when the covariancy is invalid (which we will have
2680 already diagnosed). */
2681 if (base_binfo)
2682 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2683 thunk_binfo = TREE_CHAIN (thunk_binfo))
2684 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2685 BINFO_TYPE (base_binfo)))
2686 break;
2687 gcc_assert (thunk_binfo || errorcount);
2689 /* See if virtual inheritance is involved. */
2690 for (virtual_offset = thunk_binfo;
2691 virtual_offset;
2692 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2693 if (BINFO_VIRTUAL_P (virtual_offset))
2694 break;
2696 if (virtual_offset
2697 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2699 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2701 if (virtual_offset)
2703 /* We convert via virtual base. Adjust the fixed
2704 offset to be from there. */
2705 offset =
2706 size_diffop (offset,
2707 fold_convert (ssizetype,
2708 BINFO_OFFSET (virtual_offset)));
2710 if (fixed_offset)
2711 /* There was an existing fixed offset, this must be
2712 from the base just converted to, and the base the
2713 FN was thunking to. */
2714 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2715 else
2716 fixed_offset = offset;
2720 if (fixed_offset || virtual_offset)
2721 /* Replace the overriding function with a covariant thunk. We
2722 will emit the overriding function in its own slot as
2723 well. */
2724 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2725 fixed_offset, virtual_offset);
2727 else
2728 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2729 !DECL_THUNK_P (fn));
2731 /* If we need a covariant thunk, then we may need to adjust first_defn.
2732 The ABI specifies that the thunks emitted with a function are
2733 determined by which bases the function overrides, so we need to be
2734 sure that we're using a thunk for some overridden base; even if we
2735 know that the necessary this adjustment is zero, there may not be an
2736 appropriate zero-this-adjustment thunk for us to use since thunks for
2737 overriding virtual bases always use the vcall offset.
2739 Furthermore, just choosing any base that overrides this function isn't
2740 quite right, as this slot won't be used for calls through a type that
2741 puts a covariant thunk here. Calling the function through such a type
2742 will use a different slot, and that slot is the one that determines
2743 the thunk emitted for that base.
2745 So, keep looking until we find the base that we're really overriding
2746 in this slot: the nearest primary base that doesn't use a covariant
2747 thunk in this slot. */
2748 if (overrider_target != overrider_fn)
2750 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2751 /* We already know that the overrider needs a covariant thunk. */
2752 b = get_primary_binfo (b);
2753 for (; ; b = get_primary_binfo (b))
2755 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2756 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2757 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2758 break;
2759 if (BINFO_LOST_PRIMARY_P (b))
2760 lost = true;
2762 first_defn = b;
2765 /* Assume that we will produce a thunk that convert all the way to
2766 the final overrider, and not to an intermediate virtual base. */
2767 virtual_base = NULL_TREE;
2769 /* See if we can convert to an intermediate virtual base first, and then
2770 use the vcall offset located there to finish the conversion. */
2771 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2773 /* If we find the final overrider, then we can stop
2774 walking. */
2775 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2776 BINFO_TYPE (TREE_VALUE (overrider))))
2777 break;
2779 /* If we find a virtual base, and we haven't yet found the
2780 overrider, then there is a virtual base between the
2781 declaring base (first_defn) and the final overrider. */
2782 if (BINFO_VIRTUAL_P (b))
2784 virtual_base = b;
2785 break;
2789 /* Compute the constant adjustment to the `this' pointer. The
2790 `this' pointer, when this function is called, will point at BINFO
2791 (or one of its primary bases, which are at the same offset). */
2792 if (virtual_base)
2793 /* The `this' pointer needs to be adjusted from the declaration to
2794 the nearest virtual base. */
2795 delta = size_diffop_loc (input_location,
2796 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2797 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2798 else if (lost)
2799 /* If the nearest definition is in a lost primary, we don't need an
2800 entry in our vtable. Except possibly in a constructor vtable,
2801 if we happen to get our primary back. In that case, the offset
2802 will be zero, as it will be a primary base. */
2803 delta = size_zero_node;
2804 else
2805 /* The `this' pointer needs to be adjusted from pointing to
2806 BINFO to pointing at the base where the final overrider
2807 appears. */
2808 delta = size_diffop_loc (input_location,
2809 fold_convert (ssizetype,
2810 BINFO_OFFSET (TREE_VALUE (overrider))),
2811 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2813 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2815 if (virtual_base)
2816 BV_VCALL_INDEX (*virtuals)
2817 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2818 else
2819 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2821 BV_LOST_PRIMARY (*virtuals) = lost;
2824 /* Called from modify_all_vtables via dfs_walk. */
2826 static tree
2827 dfs_modify_vtables (tree binfo, void* data)
2829 tree t = (tree) data;
2830 tree virtuals;
2831 tree old_virtuals;
2832 unsigned ix;
2834 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2835 /* A base without a vtable needs no modification, and its bases
2836 are uninteresting. */
2837 return dfs_skip_bases;
2839 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2840 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2841 /* Don't do the primary vtable, if it's new. */
2842 return NULL_TREE;
2844 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2845 /* There's no need to modify the vtable for a non-virtual primary
2846 base; we're not going to use that vtable anyhow. We do still
2847 need to do this for virtual primary bases, as they could become
2848 non-primary in a construction vtable. */
2849 return NULL_TREE;
2851 make_new_vtable (t, binfo);
2853 /* Now, go through each of the virtual functions in the virtual
2854 function table for BINFO. Find the final overrider, and update
2855 the BINFO_VIRTUALS list appropriately. */
2856 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2857 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2858 virtuals;
2859 ix++, virtuals = TREE_CHAIN (virtuals),
2860 old_virtuals = TREE_CHAIN (old_virtuals))
2861 update_vtable_entry_for_fn (t,
2862 binfo,
2863 BV_FN (old_virtuals),
2864 &virtuals, ix);
2866 return NULL_TREE;
2869 /* Update all of the primary and secondary vtables for T. Create new
2870 vtables as required, and initialize their RTTI information. Each
2871 of the functions in VIRTUALS is declared in T and may override a
2872 virtual function from a base class; find and modify the appropriate
2873 entries to point to the overriding functions. Returns a list, in
2874 declaration order, of the virtual functions that are declared in T,
2875 but do not appear in the primary base class vtable, and which
2876 should therefore be appended to the end of the vtable for T. */
2878 static tree
2879 modify_all_vtables (tree t, tree virtuals)
2881 tree binfo = TYPE_BINFO (t);
2882 tree *fnsp;
2884 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2885 if (TYPE_CONTAINS_VPTR_P (t))
2886 get_vtable_decl (t, false);
2888 /* Update all of the vtables. */
2889 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2891 /* Add virtual functions not already in our primary vtable. These
2892 will be both those introduced by this class, and those overridden
2893 from secondary bases. It does not include virtuals merely
2894 inherited from secondary bases. */
2895 for (fnsp = &virtuals; *fnsp; )
2897 tree fn = TREE_VALUE (*fnsp);
2899 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2900 || DECL_VINDEX (fn) == error_mark_node)
2902 /* We don't need to adjust the `this' pointer when
2903 calling this function. */
2904 BV_DELTA (*fnsp) = integer_zero_node;
2905 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2907 /* This is a function not already in our vtable. Keep it. */
2908 fnsp = &TREE_CHAIN (*fnsp);
2910 else
2911 /* We've already got an entry for this function. Skip it. */
2912 *fnsp = TREE_CHAIN (*fnsp);
2915 return virtuals;
2918 /* Get the base virtual function declarations in T that have the
2919 indicated NAME. */
2921 static void
2922 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2924 bool found_decls = false;
2926 /* Find virtual functions in T with the indicated NAME. */
2927 for (tree method : ovl_range (get_class_binding (t, name)))
2929 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2931 base_fndecls->safe_push (method);
2932 found_decls = true;
2936 if (found_decls)
2937 return;
2939 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2940 for (int i = 0; i < n_baseclasses; i++)
2942 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2943 get_basefndecls (name, basetype, base_fndecls);
2947 /* If this method overrides a virtual method from a base, then mark
2948 this member function as being virtual as well. Do 'final' and
2949 'override' checks too. */
2951 void
2952 check_for_override (tree decl, tree ctype)
2954 if (TREE_CODE (decl) == TEMPLATE_DECL)
2955 /* In [temp.mem] we have:
2957 A specialization of a member function template does not
2958 override a virtual function from a base class. */
2959 return;
2961 /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
2962 used for a vfunc. That avoids the expensive look_for_overrides
2963 call that when we know there's nothing to find. As conversion
2964 operators for the same type can have distinct identifiers, we
2965 cannot optimize those in that way. */
2966 if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2967 || DECL_CONV_FN_P (decl))
2968 && look_for_overrides (ctype, decl)
2969 /* Check staticness after we've checked if we 'override'. */
2970 && !DECL_STATIC_FUNCTION_P (decl))
2972 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2973 the error_mark_node so that we know it is an overriding
2974 function. */
2975 DECL_VINDEX (decl) = decl;
2977 if (warn_override
2978 && !DECL_OVERRIDE_P (decl)
2979 && !DECL_FINAL_P (decl)
2980 && !DECL_DESTRUCTOR_P (decl))
2981 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2982 "%qD can be marked override", decl);
2984 else if (DECL_OVERRIDE_P (decl))
2985 error ("%q+#D marked %<override%>, but does not override", decl);
2987 if (DECL_VIRTUAL_P (decl))
2989 /* Remember this identifier is virtual name. */
2990 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
2992 if (!DECL_VINDEX (decl))
2993 /* It's a new vfunc. */
2994 DECL_VINDEX (decl) = error_mark_node;
2996 if (DECL_DESTRUCTOR_P (decl))
2997 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2999 else if (DECL_FINAL_P (decl))
3000 error ("%q+#D marked %<final%>, but is not virtual", decl);
3003 /* Warn about hidden virtual functions that are not overridden in t.
3004 We know that constructors and destructors don't apply. */
3006 static void
3007 warn_hidden (tree t)
3009 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
3010 for (unsigned ix = member_vec->length (); ix--;)
3012 tree fns = (*member_vec)[ix];
3014 if (!OVL_P (fns))
3015 continue;
3017 tree name = OVL_NAME (fns);
3018 auto_vec<tree, 20> base_fndecls;
3019 tree base_binfo;
3020 tree binfo;
3021 unsigned j;
3023 if (IDENTIFIER_CDTOR_P (name))
3024 continue;
3026 /* Iterate through all of the base classes looking for possibly
3027 hidden functions. */
3028 for (binfo = TYPE_BINFO (t), j = 0;
3029 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3031 tree basetype = BINFO_TYPE (base_binfo);
3032 get_basefndecls (name, basetype, &base_fndecls);
3035 /* If there are no functions to hide, continue. */
3036 if (base_fndecls.is_empty ())
3037 continue;
3039 /* Remove any overridden functions. */
3040 bool seen_non_override = false;
3041 for (tree fndecl : ovl_range (fns))
3043 bool any_override = false;
3044 if (TREE_CODE (fndecl) == FUNCTION_DECL
3045 && DECL_VINDEX (fndecl))
3047 /* If the method from the base class has the same
3048 signature as the method from the derived class, it
3049 has been overridden. Note that we can't move on
3050 after finding one match: fndecl might override
3051 multiple base fns. */
3052 for (size_t k = 0; k < base_fndecls.length (); k++)
3053 if (base_fndecls[k]
3054 && same_signature_p (fndecl, base_fndecls[k]))
3056 base_fndecls[k] = NULL_TREE;
3057 any_override = true;
3060 if (!any_override)
3061 seen_non_override = true;
3064 if (!seen_non_override && warn_overloaded_virtual == 1)
3065 /* All the derived fns override base virtuals. */
3066 return;
3068 /* Now give a warning for all base functions without overriders,
3069 as they are hidden. */
3070 for (tree base_fndecl : base_fndecls)
3071 if (base_fndecl)
3073 auto_diagnostic_group d;
3074 /* Here we know it is a hider, and no overrider exists. */
3075 if (warning_at (location_of (base_fndecl),
3076 OPT_Woverloaded_virtual_,
3077 "%qD was hidden", base_fndecl))
3078 inform (location_of (fns), " by %qD", fns);
3083 /* Recursive helper for finish_struct_anon. */
3085 static void
3086 finish_struct_anon_r (tree field)
3088 for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
3090 /* We're generally only interested in entities the user
3091 declared, but we also find nested classes by noticing
3092 the TYPE_DECL that we create implicitly. You're
3093 allowed to put one anonymous union inside another,
3094 though, so we explicitly tolerate that. We use
3095 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3096 we also allow unnamed types used for defining fields. */
3097 if (DECL_ARTIFICIAL (elt)
3098 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3099 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3100 continue;
3102 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3103 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3105 /* Recurse into the anonymous aggregates to correctly handle
3106 access control (c++/24926):
3108 class A {
3109 union {
3110 union {
3111 int i;
3116 int j=A().i; */
3117 if (DECL_NAME (elt) == NULL_TREE
3118 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3119 finish_struct_anon_r (elt);
3123 /* Fix up any anonymous union/struct members of T. */
3125 static void
3126 finish_struct_anon (tree t)
3128 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3130 if (TREE_STATIC (field))
3131 continue;
3132 if (TREE_CODE (field) != FIELD_DECL)
3133 continue;
3135 if (DECL_NAME (field) == NULL_TREE
3136 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3137 finish_struct_anon_r (field);
3141 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3142 will be used later during class template instantiation.
3143 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3144 a non-static member data (FIELD_DECL), a member function
3145 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3146 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3147 When FRIEND_P is nonzero, T is either a friend class
3148 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3149 (FUNCTION_DECL, TEMPLATE_DECL). */
3151 void
3152 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3154 if (CLASSTYPE_TEMPLATE_INFO (type)
3155 && TREE_CODE (t) != CONST_DECL)
3157 tree purpose = friend_p ? NULL_TREE : type;
3159 CLASSTYPE_DECL_LIST (type)
3160 = tree_cons (purpose, t, CLASSTYPE_DECL_LIST (type));
3164 /* This function is called from declare_virt_assop_and_dtor via
3165 dfs_walk_all.
3167 DATA is a type that direcly or indirectly inherits the base
3168 represented by BINFO. If BINFO contains a virtual assignment [copy
3169 assignment or move assigment] operator or a virtual constructor,
3170 declare that function in DATA if it hasn't been already declared. */
3172 static tree
3173 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3175 tree bv, fn, t = (tree)data;
3176 tree opname = assign_op_identifier;
3178 gcc_assert (t && CLASS_TYPE_P (t));
3179 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3181 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3182 /* A base without a vtable needs no modification, and its bases
3183 are uninteresting. */
3184 return dfs_skip_bases;
3186 if (BINFO_PRIMARY_P (binfo))
3187 /* If this is a primary base, then we have already looked at the
3188 virtual functions of its vtable. */
3189 return NULL_TREE;
3191 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3193 fn = BV_FN (bv);
3195 if (DECL_NAME (fn) == opname)
3197 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3198 lazily_declare_fn (sfk_copy_assignment, t);
3199 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3200 lazily_declare_fn (sfk_move_assignment, t);
3202 else if (DECL_DESTRUCTOR_P (fn)
3203 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3204 lazily_declare_fn (sfk_destructor, t);
3207 return NULL_TREE;
3210 /* If the class type T has a direct or indirect base that contains a
3211 virtual assignment operator or a virtual destructor, declare that
3212 function in T if it hasn't been already declared. */
3214 static void
3215 declare_virt_assop_and_dtor (tree t)
3217 if (!(TYPE_POLYMORPHIC_P (t)
3218 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3219 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3220 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3221 return;
3223 dfs_walk_all (TYPE_BINFO (t),
3224 dfs_declare_virt_assop_and_dtor,
3225 NULL, t);
3228 /* Declare the inheriting constructor for class T inherited from base
3229 constructor CTOR with the parameter array PARMS of size NPARMS. */
3231 static void
3232 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3234 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3236 /* We don't declare an inheriting ctor that would be a default,
3237 copy or move ctor for derived or base. */
3238 if (nparms == 0)
3239 return;
3240 if (nparms == 1
3241 && TYPE_REF_P (parms[0]))
3243 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3244 if (parm == t || parm == DECL_CONTEXT (ctor))
3245 return;
3248 tree parmlist = void_list_node;
3249 for (int i = nparms - 1; i >= 0; i--)
3250 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3251 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3252 t, false, ctor, parmlist);
3254 if (add_method (t, fn, false))
3256 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3257 TYPE_FIELDS (t) = fn;
3261 /* Declare all the inheriting constructors for class T inherited from base
3262 constructor CTOR. */
3264 static void
3265 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3267 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3269 if (flag_new_inheriting_ctors)
3271 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3272 t, /*const*/false, ctor, parms);
3273 add_method (t, ctor, using_decl != NULL_TREE);
3274 return;
3277 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3278 int i = 0;
3279 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3281 if (TREE_PURPOSE (parms))
3282 one_inheriting_sig (t, ctor, new_parms, i);
3283 new_parms[i++] = TREE_VALUE (parms);
3285 one_inheriting_sig (t, ctor, new_parms, i);
3286 if (parms == NULL_TREE)
3288 auto_diagnostic_group d;
3289 if (warning (OPT_Winherited_variadic_ctor,
3290 "the ellipsis in %qD is not inherited", ctor))
3291 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3295 /* Implicitly declare T(). */
3297 static void
3298 add_implicit_default_ctor (tree t)
3300 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3301 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3302 if (cxx_dialect >= cxx11)
3303 TYPE_HAS_CONSTEXPR_CTOR (t)
3304 /* Don't force the declaration to get a hard answer; if the
3305 definition would have made the class non-literal, it will still be
3306 non-literal because of the base or member in question, and that
3307 gives a better diagnostic. */
3308 = type_maybe_constexpr_default_constructor (t);
3311 /* Create default constructors, assignment operators, and so forth for
3312 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3313 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3314 the class cannot have a default constructor, copy constructor
3315 taking a const reference argument, or an assignment operator taking
3316 a const reference, respectively. */
3318 static void
3319 add_implicitly_declared_members (tree t, tree* access_decls,
3320 int cant_have_const_cctor,
3321 int cant_have_const_assignment)
3323 /* Destructor. */
3324 if (!CLASSTYPE_DESTRUCTOR (t))
3325 /* In general, we create destructors lazily. */
3326 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3328 bool move_ok = false;
3329 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3330 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3331 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3332 move_ok = true;
3334 /* [class.ctor]
3336 If there is no user-declared constructor for a class, a default
3337 constructor is implicitly declared. */
3338 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3339 add_implicit_default_ctor (t);
3341 /* [class.ctor]
3343 If a class definition does not explicitly declare a copy
3344 constructor, one is declared implicitly. */
3345 if (! TYPE_HAS_COPY_CTOR (t))
3347 TYPE_HAS_COPY_CTOR (t) = 1;
3348 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3349 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3350 if (move_ok)
3351 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3354 /* If there is no assignment operator, one will be created if and
3355 when it is needed. For now, just record whether or not the type
3356 of the parameter to the assignment operator will be a const or
3357 non-const reference. */
3358 if (!TYPE_HAS_COPY_ASSIGN (t))
3360 TYPE_HAS_COPY_ASSIGN (t) = 1;
3361 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3362 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3363 if (move_ok && !LAMBDA_TYPE_P (t))
3364 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3367 /* We can't be lazy about declaring functions that might override
3368 a virtual function from a base class. */
3369 declare_virt_assop_and_dtor (t);
3371 /* If the class definition does not explicitly declare an == operator
3372 function, but declares a defaulted three-way comparison operator function,
3373 an == operator function is declared implicitly. */
3374 if (!classtype_has_op (t, EQ_EXPR))
3375 if (tree space = classtype_has_defaulted_op (t, SPACESHIP_EXPR))
3377 tree eq = implicitly_declare_fn (sfk_comparison, t, false, space,
3378 NULL_TREE);
3379 bool is_friend = DECL_CONTEXT (space) != t;
3380 if (is_friend)
3381 do_friend (NULL_TREE, DECL_NAME (eq), eq,
3382 NO_SPECIAL, true);
3383 else
3385 add_method (t, eq, false);
3386 DECL_CHAIN (eq) = TYPE_FIELDS (t);
3387 TYPE_FIELDS (t) = eq;
3389 maybe_add_class_template_decl_list (t, eq, is_friend);
3392 while (*access_decls)
3394 tree using_decl = TREE_VALUE (*access_decls);
3395 tree decl = USING_DECL_DECLS (using_decl);
3396 if (DECL_NAME (using_decl) == ctor_identifier)
3398 /* declare, then remove the decl */
3399 tree ctor_list = decl;
3400 location_t loc = input_location;
3401 input_location = DECL_SOURCE_LOCATION (using_decl);
3402 for (tree fn : ovl_range (ctor_list))
3404 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t) && default_ctor_p (fn))
3405 /* CWG2799: Inheriting a default constructor gives us a default
3406 constructor, not just an inherited constructor. */
3407 add_implicit_default_ctor (t);
3408 one_inherited_ctor (fn, t, using_decl);
3410 *access_decls = TREE_CHAIN (*access_decls);
3411 input_location = loc;
3413 else
3414 access_decls = &TREE_CHAIN (*access_decls);
3418 /* Cache of enum_min_precision values. */
3419 static GTY((deletable)) hash_map<tree, int> *enum_to_min_precision;
3421 /* Return the minimum precision of a bit-field needed to store all
3422 enumerators of ENUMERAL_TYPE TYPE. */
3424 static int
3425 enum_min_precision (tree type)
3427 type = TYPE_MAIN_VARIANT (type);
3428 /* For unscoped enums without fixed underlying type and without mode
3429 attribute we can just use precision of the underlying type. */
3430 if (UNSCOPED_ENUM_P (type)
3431 && !ENUM_FIXED_UNDERLYING_TYPE_P (type)
3432 && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type)))
3433 return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3435 if (enum_to_min_precision == NULL)
3436 enum_to_min_precision = hash_map<tree, int>::create_ggc (37);
3438 bool existed;
3439 int &prec = enum_to_min_precision->get_or_insert (type, &existed);
3440 if (existed)
3441 return prec;
3443 tree minnode, maxnode;
3444 if (TYPE_VALUES (type))
3446 minnode = maxnode = NULL_TREE;
3447 for (tree values = TYPE_VALUES (type);
3448 values; values = TREE_CHAIN (values))
3450 tree decl = TREE_VALUE (values);
3451 tree value = DECL_INITIAL (decl);
3452 if (value == error_mark_node)
3453 value = integer_zero_node;
3454 if (!minnode)
3455 minnode = maxnode = value;
3456 else if (tree_int_cst_lt (maxnode, value))
3457 maxnode = value;
3458 else if (tree_int_cst_lt (value, minnode))
3459 minnode = value;
3462 else
3463 minnode = maxnode = integer_zero_node;
3465 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
3466 int lowprec = tree_int_cst_min_precision (minnode, sgn);
3467 int highprec = tree_int_cst_min_precision (maxnode, sgn);
3468 prec = MAX (lowprec, highprec);
3469 return prec;
3472 /* FIELD is a bit-field. We are finishing the processing for its
3473 enclosing type. Issue any appropriate messages and set appropriate
3474 flags. Returns false if an error has been diagnosed. */
3476 static bool
3477 check_bitfield_decl (tree field)
3479 tree type = TREE_TYPE (field);
3480 tree w;
3482 /* Extract the declared width of the bitfield, which has been
3483 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3484 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3485 gcc_assert (w != NULL_TREE);
3486 /* Remove the bit-field width indicator so that the rest of the
3487 compiler does not treat that value as a qualifier. */
3488 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3490 /* Detect invalid bit-field type. */
3491 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3493 error_at (DECL_SOURCE_LOCATION (field),
3494 "bit-field %q#D with non-integral type %qT", field, type);
3495 w = error_mark_node;
3497 else
3499 location_t loc = input_location;
3500 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3501 STRIP_NOPS (w);
3503 /* detect invalid field size. */
3504 input_location = DECL_SOURCE_LOCATION (field);
3505 w = cxx_constant_value (w);
3506 input_location = loc;
3508 if (TREE_CODE (w) != INTEGER_CST)
3510 error ("bit-field %q+D width not an integer constant", field);
3511 w = error_mark_node;
3513 else if (tree_int_cst_sgn (w) < 0)
3515 error ("negative width in bit-field %q+D", field);
3516 w = error_mark_node;
3518 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3520 error ("zero width for bit-field %q+D", field);
3521 w = error_mark_node;
3523 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3524 && TREE_CODE (type) != BOOLEAN_TYPE
3525 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3526 || ((TREE_CODE (type) == ENUMERAL_TYPE
3527 || TREE_CODE (type) == BOOLEAN_TYPE)
3528 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3529 warning_at (DECL_SOURCE_LOCATION (field), 0,
3530 "width of %qD exceeds its type", field);
3531 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3533 int prec = enum_min_precision (type);
3534 if (compare_tree_int (w, prec) < 0)
3535 warning_at (DECL_SOURCE_LOCATION (field), 0,
3536 "%qD is too small to hold all values of %q#T",
3537 field, type);
3541 if (w != error_mark_node)
3543 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3544 DECL_BIT_FIELD (field) = 1;
3545 return true;
3547 else
3549 /* Non-bit-fields are aligned for their type. */
3550 DECL_BIT_FIELD (field) = 0;
3551 CLEAR_DECL_C_BIT_FIELD (field);
3552 return false;
3556 /* FIELD is a non bit-field. We are finishing the processing for its
3557 enclosing type T. Issue any appropriate messages and set appropriate
3558 flags. */
3560 static bool
3561 check_field_decl (tree field,
3562 tree t,
3563 int* cant_have_const_ctor,
3564 int* no_const_asn_ref)
3566 tree type = strip_array_types (TREE_TYPE (field));
3567 bool any_default_members = false;
3569 /* In C++98 an anonymous union cannot contain any fields which would change
3570 the settings of CANT_HAVE_CONST_CTOR and friends. */
3571 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3573 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3574 structs. So, we recurse through their fields here. */
3575 else if (ANON_AGGR_TYPE_P (type))
3577 for (tree fields = TYPE_FIELDS (type); fields;
3578 fields = DECL_CHAIN (fields))
3579 if (TREE_CODE (fields) == FIELD_DECL)
3580 any_default_members |= check_field_decl (fields, t,
3581 cant_have_const_ctor,
3582 no_const_asn_ref);
3584 /* Check members with class type for constructors, destructors,
3585 etc. */
3586 else if (CLASS_TYPE_P (type))
3588 /* Never let anything with uninheritable virtuals
3589 make it through without complaint. */
3590 abstract_virtuals_error (field, type);
3592 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3594 static bool warned;
3595 int oldcount = errorcount;
3596 if (TYPE_NEEDS_CONSTRUCTING (type))
3597 error ("member %q+#D with constructor not allowed in union",
3598 field);
3599 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3600 error ("member %q+#D with destructor not allowed in union", field);
3601 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3602 error ("member %q+#D with copy assignment operator not allowed in union",
3603 field);
3604 if (!warned && errorcount > oldcount)
3606 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3607 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3608 warned = true;
3611 else
3613 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3614 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3615 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3616 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3617 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3618 || !TYPE_HAS_COPY_ASSIGN (type));
3619 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3620 || !TYPE_HAS_COPY_CTOR (type));
3621 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3622 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3623 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3624 || TYPE_HAS_COMPLEX_DFLT (type));
3627 if (TYPE_HAS_COPY_CTOR (type)
3628 && !TYPE_HAS_CONST_COPY_CTOR (type))
3629 *cant_have_const_ctor = 1;
3631 if (TYPE_HAS_COPY_ASSIGN (type)
3632 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3633 *no_const_asn_ref = 1;
3636 check_abi_tags (t, field);
3638 if (DECL_INITIAL (field) != NULL_TREE)
3639 /* `build_class_init_list' does not recognize
3640 non-FIELD_DECLs. */
3641 any_default_members = true;
3643 return any_default_members;
3646 /* Check the data members (both static and non-static), class-scoped
3647 typedefs, etc., appearing in the declaration of T. Issue
3648 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3649 declaration order) of access declarations; each TREE_VALUE in this
3650 list is a USING_DECL.
3652 In addition, set the following flags:
3654 EMPTY_P
3655 The class is empty, i.e., contains no non-static data members.
3657 CANT_HAVE_CONST_CTOR_P
3658 This class cannot have an implicitly generated copy constructor
3659 taking a const reference.
3661 CANT_HAVE_CONST_ASN_REF
3662 This class cannot have an implicitly generated assignment
3663 operator taking a const reference.
3665 All of these flags should be initialized before calling this
3666 function. */
3668 static void
3669 check_field_decls (tree t, tree *access_decls,
3670 int *cant_have_const_ctor_p,
3671 int *no_const_asn_ref_p)
3673 int cant_pack = 0;
3675 /* Assume there are no access declarations. */
3676 *access_decls = NULL_TREE;
3677 /* Effective C has things to say about classes with pointer members. */
3678 tree pointer_member = NULL_TREE;
3679 /* Default initialized members affect the whole class. */
3680 tree default_init_member = NULL_TREE;
3681 /* Lack of any non-static data member of non-volatile literal
3682 type affects a union. */
3683 bool found_nv_literal_p = false;
3684 /* Standard layout requires all FIELDS have same access. */
3685 int field_access = -1;
3687 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3689 tree type = TREE_TYPE (field);
3691 switch (TREE_CODE (field))
3693 default:
3694 gcc_unreachable ();
3696 case USING_DECL:
3697 /* Save the access declarations for our caller. */
3698 *access_decls = tree_cons (NULL_TREE, field, *access_decls);
3699 break;
3701 case TYPE_DECL:
3702 case TEMPLATE_DECL:
3703 break;
3705 case FUNCTION_DECL:
3706 /* FIXME: We should fold in the checking from check_methods. */
3707 break;
3709 case CONST_DECL:
3710 DECL_NONLOCAL (field) = 1;
3711 break;
3713 case VAR_DECL:
3714 if (TREE_CODE (t) == UNION_TYPE
3715 && cxx_dialect < cxx11)
3717 /* [class.union]
3719 (C++98) If a union contains a static data member,
3720 ... the program is ill-formed. */
3721 if (cxx_dialect < cxx11)
3722 error ("in C++98 %q+D may not be static because it is "
3723 "a member of a union", field);
3725 goto data_member;
3727 case FIELD_DECL:
3728 if (TREE_CODE (t) == UNION_TYPE)
3730 /* [class.union]
3732 If a union contains ... or a [non-static data] member
3733 of reference type, the program is ill-formed. */
3734 if (TYPE_REF_P (type))
3735 error ("non-static data member %q+D in a union may not "
3736 "have reference type %qT", field, type);
3739 data_member:
3740 /* Common VAR_DECL & FIELD_DECL processing. */
3741 DECL_CONTEXT (field) = t;
3742 DECL_NONLOCAL (field) = 1;
3744 /* Template instantiation can cause this. Perhaps this
3745 should be a specific instantiation check? */
3746 if (TREE_CODE (type) == FUNCTION_TYPE)
3748 error ("data member %q+D invalidly declared function type", field);
3749 type = build_pointer_type (type);
3750 TREE_TYPE (field) = type;
3752 else if (TREE_CODE (type) == METHOD_TYPE)
3754 error ("data member %q+D invalidly declared method type", field);
3755 type = build_pointer_type (type);
3756 TREE_TYPE (field) = type;
3759 break;
3762 if (TREE_CODE (field) != FIELD_DECL)
3763 continue;
3765 if (type == error_mark_node)
3766 continue;
3768 /* If it is not a union and at least one non-static data member is
3769 non-literal, the whole class becomes non-literal. Per Core/1453,
3770 volatile non-static data members and base classes are also not allowed.
3771 If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
3772 members.
3773 Note: if the type is incomplete we will complain later on. */
3774 if (COMPLETE_TYPE_P (type))
3776 if (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type))
3777 CLASSTYPE_LITERAL_P (t) = false;
3778 else
3779 found_nv_literal_p = true;
3782 int this_field_access = (TREE_PROTECTED (field) ? 1
3783 : TREE_PRIVATE (field) ? 2 : 0);
3784 if (field_access != this_field_access)
3786 /* A standard-layout class is a class that:
3788 ... has the same access control (Clause 11) for all
3789 non-static data members, */
3790 if (field_access < 0)
3791 field_access = this_field_access;
3792 else
3793 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3795 /* Aggregates must be public. */
3796 if (this_field_access)
3797 CLASSTYPE_NON_AGGREGATE (t) = 1;
3800 /* If this is of reference type, check if it needs an init. */
3801 if (TYPE_REF_P (type))
3803 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3804 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3805 if (DECL_INITIAL (field) == NULL_TREE)
3806 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3807 if (cxx_dialect < cxx11)
3809 /* ARM $12.6.2: [A member initializer list] (or, for an
3810 aggregate, initialization by a brace-enclosed list) is the
3811 only way to initialize non-static const and reference
3812 members. */
3813 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3814 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3818 type = strip_array_types (type);
3820 if (TYPE_PACKED (t))
3822 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3824 warning_at (DECL_SOURCE_LOCATION (field), 0,
3825 "ignoring packed attribute because of"
3826 " unpacked non-POD field %q#D", field);
3827 cant_pack = 1;
3829 else if (DECL_C_BIT_FIELD (field)
3830 || TYPE_ALIGN (TREE_TYPE (field)) > BITS_PER_UNIT)
3831 DECL_PACKED (field) = 1;
3834 if (DECL_C_BIT_FIELD (field)
3835 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field)))
3836 /* We don't treat zero-width bitfields as making a class
3837 non-empty. */
3839 else if (field_poverlapping_p (field)
3840 && is_empty_class (TREE_TYPE (field)))
3841 /* Empty data members also don't make a class non-empty. */
3842 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3843 else
3845 /* The class is non-empty. */
3846 CLASSTYPE_EMPTY_P (t) = 0;
3847 /* The class is not even nearly empty. */
3848 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3849 /* If one of the data members contains an empty class, so
3850 does T. */
3851 if (CLASS_TYPE_P (type)
3852 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3853 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3856 /* This is used by -Weffc++ (see below). Warn only for pointers
3857 to members which might hold dynamic memory. So do not warn
3858 for pointers to functions or pointers to members. */
3859 if (TYPE_PTR_P (type)
3860 && !TYPE_PTRFN_P (type))
3861 pointer_member = field;
3863 if (CLASS_TYPE_P (type))
3865 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3866 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3867 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3868 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3871 if (DECL_MUTABLE_P (field) || TYPE_HAS_MUTABLE_P (type))
3872 CLASSTYPE_HAS_MUTABLE (t) = 1;
3874 if (DECL_MUTABLE_P (field))
3876 if (TYPE_REF_P (type))
3877 error ("member %q+D cannot be declared as a %<mutable%> "
3878 "reference", field);
3879 else if (CP_TYPE_CONST_P (type))
3880 error ("member %q+D cannot be declared both %<const%> "
3881 "and %<mutable%>", field);
3884 if (! layout_pod_type_p (type))
3885 /* DR 148 now allows pointers to members (which are POD themselves),
3886 to be allowed in POD structs. */
3887 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3889 if (field_poverlapping_p (field))
3890 /* A potentially-overlapping non-static data member makes the class
3891 non-layout-POD. */
3892 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3894 if (!std_layout_type_p (type))
3895 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3897 if (! zero_init_p (type))
3898 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3900 /* We set DECL_C_BIT_FIELD in grokbitfield.
3901 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3902 if (DECL_C_BIT_FIELD (field))
3903 check_bitfield_decl (field);
3905 if (check_field_decl (field, t,
3906 cant_have_const_ctor_p, no_const_asn_ref_p))
3908 if (default_init_member
3909 && TREE_CODE (t) == UNION_TYPE)
3911 error ("multiple fields in union %qT initialized", t);
3912 inform (DECL_SOURCE_LOCATION (default_init_member),
3913 "initialized member %q+D declared here",
3914 default_init_member);
3916 default_init_member = field;
3919 /* Now that we've removed bit-field widths from DECL_INITIAL,
3920 anything left in DECL_INITIAL is an NSDMI that makes the class
3921 non-aggregate in C++11, and non-layout-POD always. */
3922 if (DECL_INITIAL (field))
3924 if (cxx_dialect < cxx14)
3925 CLASSTYPE_NON_AGGREGATE (t) = true;
3926 else
3927 CLASSTYPE_NON_POD_AGGREGATE (t) = true;
3930 if (CP_TYPE_CONST_P (type))
3932 /* If any field is const, the structure type is pseudo-const. */
3933 C_TYPE_FIELDS_READONLY (t) = 1;
3934 if (DECL_INITIAL (field) == NULL_TREE)
3935 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3936 if (cxx_dialect < cxx11)
3938 /* ARM $12.6.2: [A member initializer list] (or, for an
3939 aggregate, initialization by a brace-enclosed list) is the
3940 only way to initialize non-static const and reference
3941 members. */
3942 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3943 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3946 /* A field that is pseudo-const makes the structure likewise. */
3947 else if (CLASS_TYPE_P (type))
3949 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3950 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3951 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3952 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3955 /* Core issue 80: A non-static data member is required to have a
3956 different name from the class iff the class has a
3957 user-declared constructor. */
3958 if (constructor_name_p (DECL_NAME (field), t)
3959 && TYPE_HAS_USER_CONSTRUCTOR (t))
3960 permerror (DECL_SOURCE_LOCATION (field),
3961 "field %q#D with same name as class", field);
3964 /* Per CWG 2096, a type is a literal type if it is a union, and at least
3965 one of its non-static data members is of non-volatile literal type. */
3966 if (TREE_CODE (t) == UNION_TYPE && found_nv_literal_p)
3967 CLASSTYPE_LITERAL_P (t) = true;
3969 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3970 it should also define a copy constructor and an assignment operator to
3971 implement the correct copy semantic (deep vs shallow, etc.). As it is
3972 not feasible to check whether the constructors do allocate dynamic memory
3973 and store it within members, we approximate the warning like this:
3975 -- Warn only if there are members which are pointers
3976 -- Warn only if there is a non-trivial constructor (otherwise,
3977 there cannot be memory allocated).
3978 -- Warn only if there is a non-trivial destructor. We assume that the
3979 user at least implemented the cleanup correctly, and a destructor
3980 is needed to free dynamic memory.
3982 This seems enough for practical purposes. */
3983 if (warn_ecpp
3984 && pointer_member
3985 && TYPE_HAS_USER_CONSTRUCTOR (t)
3986 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3987 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3989 if (warning (OPT_Weffc__, "%q#T has pointer data members", t))
3991 if (! TYPE_HAS_COPY_CTOR (t))
3993 warning (OPT_Weffc__,
3994 " but does not declare %<%T(const %T&)%>", t, t);
3995 if (!TYPE_HAS_COPY_ASSIGN (t))
3996 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3998 else if (! TYPE_HAS_COPY_ASSIGN (t))
3999 warning (OPT_Weffc__,
4000 " but does not declare %<operator=(const %T&)%>", t);
4001 inform (DECL_SOURCE_LOCATION (pointer_member),
4002 "pointer member %q+D declared here", pointer_member);
4006 /* Non-static data member initializers make the default constructor
4007 non-trivial. */
4008 if (default_init_member)
4010 TYPE_NEEDS_CONSTRUCTING (t) = true;
4011 TYPE_HAS_COMPLEX_DFLT (t) = true;
4014 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4015 if (cant_pack)
4016 TYPE_PACKED (t) = 0;
4018 /* Check anonymous struct/anonymous union fields. */
4019 finish_struct_anon (t);
4021 /* We've built up the list of access declarations in reverse order.
4022 Fix that now. */
4023 *access_decls = nreverse (*access_decls);
4026 /* If TYPE is an empty class type, records its OFFSET in the table of
4027 OFFSETS. */
4029 static int
4030 record_subobject_offset (tree type, tree offset, splay_tree offsets)
4032 splay_tree_node n;
4034 if (!is_empty_class (type))
4035 return 0;
4037 /* Record the location of this empty object in OFFSETS. */
4038 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4039 if (!n)
4040 n = splay_tree_insert (offsets,
4041 (splay_tree_key) offset,
4042 (splay_tree_value) NULL_TREE);
4043 n->value = ((splay_tree_value)
4044 tree_cons (NULL_TREE,
4045 type,
4046 (tree) n->value));
4048 return 0;
4051 /* Returns nonzero if TYPE is an empty class type and there is
4052 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4054 static int
4055 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4057 splay_tree_node n;
4058 tree t;
4060 if (!is_empty_class (type))
4061 return 0;
4063 /* Record the location of this empty object in OFFSETS. */
4064 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4065 if (!n)
4066 return 0;
4068 enum { ignore, fast, slow, warn }
4069 cv_check = (abi_version_crosses (19) ? slow
4070 : abi_version_at_least (19) ? fast
4071 : ignore);
4072 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4074 tree elt = TREE_VALUE (t);
4076 if (same_type_p (elt, type))
4077 return 1;
4079 if (cv_check != ignore
4080 && similar_type_p (elt, type))
4082 if (cv_check == fast)
4083 return 1;
4084 cv_check = warn;
4088 if (cv_check == warn)
4090 warning (OPT_Wabi, "layout of %qs member of type %qT changes in %qs",
4091 "[[no_unique_address]]", type, "-fabi-version=19");
4092 if (abi_version_at_least (19))
4093 return 1;
4096 return 0;
4099 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4100 F for every subobject, passing it the type, offset, and table of
4101 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4102 be traversed.
4104 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4105 than MAX_OFFSET will not be walked.
4107 If F returns a nonzero value, the traversal ceases, and that value
4108 is returned. Otherwise, returns zero. */
4110 static int
4111 walk_subobject_offsets (tree type,
4112 subobject_offset_fn f,
4113 tree offset,
4114 splay_tree offsets,
4115 tree max_offset,
4116 int vbases_p)
4118 int r = 0;
4119 tree type_binfo = NULL_TREE;
4121 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4122 stop. */
4123 if (max_offset && tree_int_cst_lt (max_offset, offset))
4124 return 0;
4126 if (type == error_mark_node)
4127 return 0;
4129 if (!TYPE_P (type))
4131 type_binfo = type;
4132 type = BINFO_TYPE (type);
4135 if (CLASS_TYPE_P (type))
4137 tree field;
4138 tree binfo;
4139 int i;
4141 /* Avoid recursing into objects that are not interesting. */
4142 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4143 return 0;
4145 /* Record the location of TYPE. */
4146 r = (*f) (type, offset, offsets);
4147 if (r)
4148 return r;
4150 /* Iterate through the direct base classes of TYPE. */
4151 if (!type_binfo)
4152 type_binfo = TYPE_BINFO (type);
4153 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4155 tree binfo_offset;
4157 if (BINFO_VIRTUAL_P (binfo))
4158 continue;
4160 tree orig_binfo;
4161 /* We cannot rely on BINFO_OFFSET being set for the base
4162 class yet, but the offsets for direct non-virtual
4163 bases can be calculated by going back to the TYPE. */
4164 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4165 binfo_offset = size_binop (PLUS_EXPR,
4166 offset,
4167 BINFO_OFFSET (orig_binfo));
4169 r = walk_subobject_offsets (binfo,
4171 binfo_offset,
4172 offsets,
4173 max_offset,
4174 /*vbases_p=*/0);
4175 if (r)
4176 return r;
4179 if (CLASSTYPE_VBASECLASSES (type))
4181 unsigned ix;
4182 vec<tree, va_gc> *vbases;
4184 /* Iterate through the virtual base classes of TYPE. In G++
4185 3.2, we included virtual bases in the direct base class
4186 loop above, which results in incorrect results; the
4187 correct offsets for virtual bases are only known when
4188 working with the most derived type. */
4189 if (vbases_p)
4190 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4191 vec_safe_iterate (vbases, ix, &binfo); ix++)
4193 r = walk_subobject_offsets (binfo,
4195 size_binop (PLUS_EXPR,
4196 offset,
4197 BINFO_OFFSET (binfo)),
4198 offsets,
4199 max_offset,
4200 /*vbases_p=*/0);
4201 if (r)
4202 return r;
4204 else
4206 /* We still have to walk the primary base, if it is
4207 virtual. (If it is non-virtual, then it was walked
4208 above.) */
4209 tree vbase = get_primary_binfo (type_binfo);
4211 if (vbase && BINFO_VIRTUAL_P (vbase)
4212 && BINFO_PRIMARY_P (vbase)
4213 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4215 r = (walk_subobject_offsets
4216 (vbase, f, offset,
4217 offsets, max_offset, /*vbases_p=*/0));
4218 if (r)
4219 return r;
4224 /* Iterate through the fields of TYPE. */
4225 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4226 if (TREE_CODE (field) == FIELD_DECL
4227 && TREE_TYPE (field) != error_mark_node
4228 && !DECL_ARTIFICIAL (field))
4230 tree field_offset;
4232 field_offset = byte_position (field);
4234 r = walk_subobject_offsets (TREE_TYPE (field),
4236 size_binop (PLUS_EXPR,
4237 offset,
4238 field_offset),
4239 offsets,
4240 max_offset,
4241 /*vbases_p=*/1);
4242 if (r)
4243 return r;
4246 else if (TREE_CODE (type) == ARRAY_TYPE)
4248 tree element_type = strip_array_types (type);
4249 tree domain = TYPE_DOMAIN (type);
4250 tree index;
4252 /* Avoid recursing into objects that are not interesting. */
4253 if (!CLASS_TYPE_P (element_type)
4254 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4255 || !domain
4256 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4257 return 0;
4259 /* Step through each of the elements in the array. */
4260 for (index = size_zero_node;
4261 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4262 index = size_binop (PLUS_EXPR, index, size_one_node))
4264 r = walk_subobject_offsets (TREE_TYPE (type),
4266 offset,
4267 offsets,
4268 max_offset,
4269 /*vbases_p=*/1);
4270 if (r)
4271 return r;
4272 offset = size_binop (PLUS_EXPR, offset,
4273 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4274 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4275 there's no point in iterating through the remaining
4276 elements of the array. */
4277 if (max_offset && tree_int_cst_lt (max_offset, offset))
4278 break;
4282 return 0;
4285 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4287 static bool
4288 field_poverlapping_p (tree decl)
4290 /* Base fields are actually potentially overlapping, but C++ bases go through
4291 a different code path based on binfos, and ObjC++ base fields are laid out
4292 in objc-act, so we don't want layout_class_type to mess with them. */
4293 if (DECL_FIELD_IS_BASE (decl))
4295 gcc_checking_assert (c_dialect_objc ());
4296 return false;
4299 return lookup_attribute ("no_unique_address",
4300 DECL_ATTRIBUTES (decl));
4303 /* Return true iff DECL is an empty field, either for an empty base or a
4304 [[no_unique_address]] data member. */
4306 bool
4307 is_empty_field (tree decl)
4309 if (!decl || TREE_CODE (decl) != FIELD_DECL)
4310 return false;
4312 bool r = (is_empty_class (TREE_TYPE (decl))
4313 && (DECL_FIELD_IS_BASE (decl)
4314 || field_poverlapping_p (decl)));
4316 /* Empty fields should have size zero. */
4317 gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl)));
4319 return r;
4322 /* Record all of the empty subobjects of DECL_OR_BINFO. */
4324 static void
4325 record_subobject_offsets (tree decl_or_binfo,
4326 splay_tree offsets)
4328 tree type, offset;
4329 bool overlapping, vbases_p;
4331 if (DECL_P (decl_or_binfo))
4333 tree decl = decl_or_binfo;
4334 type = TREE_TYPE (decl);
4335 offset = byte_position (decl);
4336 overlapping = field_poverlapping_p (decl);
4337 vbases_p = true;
4339 else
4341 type = BINFO_TYPE (decl_or_binfo);
4342 offset = BINFO_OFFSET (decl_or_binfo);
4343 overlapping = true;
4344 vbases_p = false;
4347 tree max_offset;
4348 /* If recording subobjects for a non-static data member or a
4349 non-empty base class, we do not need to record offsets beyond
4350 the size of the biggest empty class. Additional data members
4351 will go at the end of the class. Additional base classes will go
4352 either at offset zero (if empty, in which case they cannot
4353 overlap with offsets past the size of the biggest empty class) or
4354 at the end of the class.
4356 However, if we are placing an empty base class, then we must record
4357 all offsets, as either the empty class is at offset zero (where
4358 other empty classes might later be placed) or at the end of the
4359 class (where other objects might then be placed, so other empty
4360 subobjects might later overlap). */
4361 if (!overlapping
4362 || !is_empty_class (type))
4363 max_offset = sizeof_biggest_empty_class;
4364 else
4365 max_offset = NULL_TREE;
4366 walk_subobject_offsets (type, record_subobject_offset, offset,
4367 offsets, max_offset, vbases_p);
4370 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4371 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4372 virtual bases of TYPE are examined. */
4374 static int
4375 layout_conflict_p (tree type,
4376 tree offset,
4377 splay_tree offsets,
4378 int vbases_p)
4380 splay_tree_node max_node;
4382 /* Get the node in OFFSETS that indicates the maximum offset where
4383 an empty subobject is located. */
4384 max_node = splay_tree_max (offsets);
4385 /* If there aren't any empty subobjects, then there's no point in
4386 performing this check. */
4387 if (!max_node)
4388 return 0;
4390 return walk_subobject_offsets (type, check_subobject_offset, offset,
4391 offsets, (tree) (max_node->key),
4392 vbases_p);
4395 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4396 non-static data member of the type indicated by RLI. BINFO is the
4397 binfo corresponding to the base subobject, OFFSETS maps offsets to
4398 types already located at those offsets. This function determines
4399 the position of the DECL. */
4401 static void
4402 layout_nonempty_base_or_field (record_layout_info rli,
4403 tree decl,
4404 tree binfo,
4405 splay_tree offsets)
4407 tree offset = NULL_TREE;
4408 bool field_p;
4409 tree type;
4411 if (binfo)
4413 /* For the purposes of determining layout conflicts, we want to
4414 use the class type of BINFO; TREE_TYPE (DECL) will be the
4415 CLASSTYPE_AS_BASE version, which does not contain entries for
4416 zero-sized bases. */
4417 type = TREE_TYPE (binfo);
4418 field_p = false;
4420 else
4422 type = TREE_TYPE (decl);
4423 field_p = true;
4426 /* Try to place the field. It may take more than one try if we have
4427 a hard time placing the field without putting two objects of the
4428 same type at the same address. */
4429 while (1)
4431 struct record_layout_info_s old_rli = *rli;
4433 /* Place this field. */
4434 place_field (rli, decl);
4435 offset = byte_position (decl);
4437 /* We have to check to see whether or not there is already
4438 something of the same type at the offset we're about to use.
4439 For example, consider:
4441 struct S {};
4442 struct T : public S { int i; };
4443 struct U : public S, public T {};
4445 Here, we put S at offset zero in U. Then, we can't put T at
4446 offset zero -- its S component would be at the same address
4447 as the S we already allocated. So, we have to skip ahead.
4448 Since all data members, including those whose type is an
4449 empty class, have nonzero size, any overlap can happen only
4450 with a direct or indirect base-class -- it can't happen with
4451 a data member. */
4452 /* In a union, overlap is permitted; all members are placed at
4453 offset zero. */
4454 if (TREE_CODE (rli->t) == UNION_TYPE)
4455 break;
4456 if (layout_conflict_p (field_p ? type : binfo, offset,
4457 offsets, field_p))
4459 /* Strip off the size allocated to this field. That puts us
4460 at the first place we could have put the field with
4461 proper alignment. */
4462 *rli = old_rli;
4464 /* Bump up by the alignment required for the type. */
4465 rli->bitpos
4466 = size_binop (PLUS_EXPR, rli->bitpos,
4467 bitsize_int (binfo
4468 ? CLASSTYPE_ALIGN (type)
4469 : TYPE_ALIGN (type)));
4470 normalize_rli (rli);
4472 else if (TREE_CODE (type) == NULLPTR_TYPE
4473 && warn_abi && abi_version_crosses (9))
4475 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4476 the offset wasn't aligned like a pointer when we started to
4477 layout this field, that affects its position. */
4478 tree pos = rli_size_unit_so_far (&old_rli);
4479 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4481 if (abi_version_at_least (9))
4482 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4483 "alignment of %qD increased in %<-fabi-version=9%> "
4484 "(GCC 5.2)", decl);
4485 else
4486 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4487 "of %qD will increase in %<-fabi-version=9%>",
4488 decl);
4490 break;
4492 else
4493 /* There was no conflict. We're done laying out this field. */
4494 break;
4497 /* Now that we know where it will be placed, update its
4498 BINFO_OFFSET. */
4499 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4500 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4501 this point because their BINFO_OFFSET is copied from another
4502 hierarchy. Therefore, we may not need to add the entire
4503 OFFSET. */
4504 propagate_binfo_offsets (binfo,
4505 size_diffop_loc (input_location,
4506 fold_convert (ssizetype, offset),
4507 fold_convert (ssizetype,
4508 BINFO_OFFSET (binfo))));
4511 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4513 static int
4514 empty_base_at_nonzero_offset_p (tree type,
4515 tree offset,
4516 splay_tree /*offsets*/)
4518 return is_empty_class (type) && !integer_zerop (offset);
4521 /* Layout the empty base BINFO. EOC indicates the byte currently just
4522 past the end of the class, and should be correctly aligned for a
4523 class of the type indicated by BINFO; OFFSETS gives the offsets of
4524 the empty bases allocated so far. T is the most derived
4525 type. Return nonzero iff we added it at the end. */
4527 static bool
4528 layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl,
4529 splay_tree offsets)
4531 tree alignment;
4532 bool atend = false;
4533 tree binfo = NULL_TREE;
4534 tree decl = NULL_TREE;
4535 tree type;
4536 if (TREE_CODE (binfo_or_decl) == TREE_BINFO)
4538 binfo = binfo_or_decl;
4539 type = BINFO_TYPE (binfo);
4541 else
4543 decl = binfo_or_decl;
4544 type = TREE_TYPE (decl);
4547 /* On some platforms (ARM), even empty classes will not be
4548 byte-aligned. */
4549 tree eoc = round_up_loc (input_location,
4550 rli_size_unit_so_far (rli),
4551 CLASSTYPE_ALIGN_UNIT (type));
4553 /* This routine should only be used for empty classes. */
4554 gcc_assert (is_empty_class (type));
4556 if (decl && DECL_USER_ALIGN (decl))
4557 alignment = size_int (DECL_ALIGN_UNIT (decl));
4558 else
4559 alignment = size_int (CLASSTYPE_ALIGN_UNIT (type));
4561 /* This is an empty base class. We first try to put it at offset
4562 zero. */
4563 tree offset = size_zero_node;
4564 if (TREE_CODE (rli->t) != UNION_TYPE
4565 && layout_conflict_p (type,
4566 offset,
4567 offsets,
4568 /*vbases_p=*/0))
4570 /* That didn't work. Now, we move forward from the next
4571 available spot in the class. */
4572 atend = true;
4573 offset = eoc;
4574 while (1)
4576 if (!layout_conflict_p (type,
4577 offset,
4578 offsets,
4579 /*vbases_p=*/0))
4580 /* We finally found a spot where there's no overlap. */
4581 break;
4583 /* There's overlap here, too. Bump along to the next spot. */
4584 offset = size_binop (PLUS_EXPR, offset, alignment);
4588 if (decl && DECL_USER_ALIGN (decl))
4590 rli->record_align = MAX (rli->record_align, DECL_ALIGN (decl));
4591 if (warn_packed)
4592 rli->unpacked_align = MAX (rli->unpacked_align, DECL_ALIGN (decl));
4593 TYPE_USER_ALIGN (rli->t) = 1;
4595 else if (CLASSTYPE_USER_ALIGN (type))
4597 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
4598 if (warn_packed)
4599 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
4600 TYPE_USER_ALIGN (rli->t) = 1;
4603 if (binfo)
4604 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4605 propagate_binfo_offsets (binfo,
4606 size_diffop (offset, BINFO_OFFSET (binfo)));
4607 else
4609 DECL_FIELD_OFFSET (decl) = offset;
4610 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4611 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4614 return atend;
4617 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4618 fields at NEXT_FIELD, and return it. */
4620 static tree
4621 build_base_field_1 (tree t, tree binfo, tree access, tree *&next_field)
4623 /* Create the FIELD_DECL. */
4624 tree basetype = BINFO_TYPE (binfo);
4625 tree as_base = CLASSTYPE_AS_BASE (basetype);
4626 gcc_assert (as_base);
4627 tree decl = build_decl (input_location, FIELD_DECL, NULL_TREE, as_base);
4629 DECL_ARTIFICIAL (decl) = 1;
4630 DECL_IGNORED_P (decl) = 1;
4631 DECL_FIELD_CONTEXT (decl) = t;
4632 if (is_empty_class (basetype))
4633 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4634 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4635 else
4637 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4638 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4640 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4641 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4642 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4643 DECL_FIELD_IS_BASE (decl) = 1;
4645 if (access == access_private_node)
4646 TREE_PRIVATE (decl) = true;
4647 else if (access == access_protected_node)
4648 TREE_PROTECTED (decl) = true;
4650 /* Add the new FIELD_DECL to the list of fields for T. */
4651 DECL_CHAIN (decl) = *next_field;
4652 *next_field = decl;
4653 next_field = &DECL_CHAIN (decl);
4655 return decl;
4658 /* Layout the base given by BINFO in the class indicated by RLI.
4659 *BASE_ALIGN is a running maximum of the alignments of
4660 any base class. OFFSETS gives the location of empty base
4661 subobjects. T is the most derived type. Return nonzero if the new
4662 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4663 *NEXT_FIELD, unless BINFO is for an empty base class.
4665 Returns the location at which the next field should be inserted. */
4667 static tree *
4668 build_base_field (record_layout_info rli, tree binfo, tree access,
4669 splay_tree offsets, tree *next_field)
4671 tree t = rli->t;
4672 tree basetype = BINFO_TYPE (binfo);
4674 if (!COMPLETE_TYPE_P (basetype))
4675 /* This error is now reported in xref_tag, thus giving better
4676 location information. */
4677 return next_field;
4679 /* Place the base class. */
4680 if (!is_empty_class (basetype))
4682 tree decl;
4684 /* The containing class is non-empty because it has a non-empty
4685 base class. */
4686 CLASSTYPE_EMPTY_P (t) = 0;
4688 /* Create the FIELD_DECL. */
4689 decl = build_base_field_1 (t, binfo, access, next_field);
4691 /* Try to place the field. It may take more than one try if we
4692 have a hard time placing the field without putting two
4693 objects of the same type at the same address. */
4694 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4696 else
4698 bool atend = layout_empty_base_or_field (rli, binfo, offsets);
4699 /* A nearly-empty class "has no proper base class that is empty,
4700 not morally virtual, and at an offset other than zero." */
4701 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4703 if (atend)
4704 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4705 /* The check above (used in G++ 3.2) is insufficient because
4706 an empty class placed at offset zero might itself have an
4707 empty base at a nonzero offset. */
4708 else if (walk_subobject_offsets (basetype,
4709 empty_base_at_nonzero_offset_p,
4710 size_zero_node,
4711 /*offsets=*/NULL,
4712 /*max_offset=*/NULL_TREE,
4713 /*vbases_p=*/true))
4714 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4717 /* We used to not create a FIELD_DECL for empty base classes because of
4718 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4719 be a problem anymore. We need them to handle initialization of C++17
4720 aggregate bases. */
4721 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4723 tree decl = build_base_field_1 (t, binfo, access, next_field);
4724 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4725 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4726 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4727 SET_DECL_FIELD_ABI_IGNORED (decl, 1);
4730 /* An empty virtual base causes a class to be non-empty
4731 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4732 here because that was already done when the virtual table
4733 pointer was created. */
4736 /* Record the offsets of BINFO and its base subobjects. */
4737 record_subobject_offsets (binfo, offsets);
4739 return next_field;
4742 /* Layout all of the non-virtual base classes. Record empty
4743 subobjects in OFFSETS. T is the most derived type. Return nonzero
4744 if the type cannot be nearly empty. The fields created
4745 corresponding to the base classes will be inserted at
4746 *NEXT_FIELD. */
4748 static void
4749 build_base_fields (record_layout_info rli,
4750 splay_tree offsets, tree *next_field)
4752 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4753 subobjects. */
4754 tree t = rli->t;
4755 tree binfo = TYPE_BINFO (t);
4756 int n_baseclasses = BINFO_N_BASE_BINFOS (binfo);
4758 /* The primary base class is always allocated first. */
4759 const tree primary_binfo = CLASSTYPE_PRIMARY_BINFO (t);
4760 if (primary_binfo)
4762 /* We need to walk BINFO_BASE_BINFO to find the access of the primary
4763 base, if it is direct. Indirect base fields are private. */
4764 tree primary_access = access_private_node;
4765 for (int i = 0; i < n_baseclasses; ++i)
4767 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
4768 if (base_binfo == primary_binfo)
4770 primary_access = BINFO_BASE_ACCESS (binfo, i);
4771 break;
4774 next_field = build_base_field (rli, primary_binfo,
4775 primary_access,
4776 offsets, next_field);
4779 /* Now allocate the rest of the bases. */
4780 for (int i = 0; i < n_baseclasses; ++i)
4782 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
4784 /* The primary base was already allocated above, so we don't
4785 need to allocate it again here. */
4786 if (base_binfo == primary_binfo)
4787 continue;
4789 /* Virtual bases are added at the end (a primary virtual base
4790 will have already been added). */
4791 if (BINFO_VIRTUAL_P (base_binfo))
4792 continue;
4794 next_field = build_base_field (rli, base_binfo,
4795 BINFO_BASE_ACCESS (binfo, i),
4796 offsets, next_field);
4800 /* Go through the TYPE_FIELDS of T issuing any appropriate
4801 diagnostics, figuring out which methods override which other
4802 methods, and so forth. */
4804 static void
4805 check_methods (tree t)
4807 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4808 if (DECL_DECLARES_FUNCTION_P (x))
4810 check_for_override (x, t);
4812 if (DECL_PURE_VIRTUAL_P (x)
4813 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4814 error ("initializer specified for non-virtual method %q+D", x);
4815 /* The name of the field is the original field name
4816 Save this in auxiliary field for later overloading. */
4817 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4819 TYPE_POLYMORPHIC_P (t) = 1;
4820 if (DECL_PURE_VIRTUAL_P (x))
4821 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4824 if (!DECL_VIRTUAL_P (x)
4825 && lookup_attribute ("transaction_safe_dynamic",
4826 DECL_ATTRIBUTES (x)))
4827 error_at (DECL_SOURCE_LOCATION (x),
4828 "%<transaction_safe_dynamic%> may only be specified for "
4829 "a virtual function");
4832 /* Check whether the eligible special member functions (P0848) are
4833 user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
4834 has the eligible ones, unless none are eligible; TYPE_FIELDS also contains
4835 ineligible overloads, which is why this needs to be separate from the loop
4836 above. */
4838 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4840 if (TREE_CODE (dtor) == OVERLOAD)
4842 /* P0848: At the end of the definition of a class, overload
4843 resolution is performed among the prospective destructors declared
4844 in that class with an empty argument list to select the destructor
4845 for the class, also known as the selected destructor. The program
4846 is ill-formed if overload resolution fails. */
4847 int viable = 0;
4848 for (tree fn : ovl_range (dtor))
4849 if (constraints_satisfied_p (fn))
4850 ++viable;
4851 gcc_checking_assert (viable != 1);
4853 auto_diagnostic_group d;
4854 if (viable == 0)
4855 error_at (location_of (t), "no viable destructor for %qT", t);
4856 else
4857 error_at (location_of (t), "destructor for %qT is ambiguous", t);
4858 print_candidates (dtor);
4860 /* Arbitrarily prune the overload set to a single function for
4861 sake of error recovery. */
4862 tree *slot = find_member_slot (t, dtor_identifier);
4863 *slot = get_first_fn (dtor);
4865 else if (user_provided_p (dtor))
4866 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
4869 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
4871 if (!user_provided_p (fn))
4872 /* Might be trivial. */;
4873 else if (TREE_CODE (fn) == TEMPLATE_DECL)
4874 /* Templates are never special members. */;
4875 else if (copy_fn_p (fn)
4876 && constraints_satisfied_p (fn))
4877 TYPE_HAS_COMPLEX_COPY_CTOR (t) = true;
4878 else if (move_fn_p (fn)
4879 && constraints_satisfied_p (fn))
4880 TYPE_HAS_COMPLEX_MOVE_CTOR (t) = true;
4883 for (tree fn : ovl_range (get_class_binding_direct (t, assign_op_identifier)))
4885 if (!user_provided_p (fn))
4886 /* Might be trivial. */;
4887 else if (TREE_CODE (fn) == TEMPLATE_DECL)
4888 /* Templates are never special members. */;
4889 else if (copy_fn_p (fn)
4890 && constraints_satisfied_p (fn))
4891 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true;
4892 else if (move_fn_p (fn)
4893 && constraints_satisfied_p (fn))
4894 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = true;
4898 /* FN is constructor, destructor or operator function. Clone the
4899 declaration to create a NAME'd variant. NEED_VTT_PARM_P and
4900 OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
4902 static tree
4903 copy_fndecl_with_name (tree fn, tree name, tree_code code,
4904 bool need_vtt_parm_p, bool omit_inherited_parms_p)
4906 /* Copy the function. */
4907 tree clone = copy_decl (fn);
4908 /* Reset the function name. */
4909 DECL_NAME (clone) = name;
4911 if (flag_concepts)
4912 /* Clone constraints. */
4913 if (tree ci = get_constraints (fn))
4914 set_constraints (clone, copy_node (ci));
4916 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4917 /* There's no pending inline data for this function. */
4918 DECL_PENDING_INLINE_INFO (clone) = NULL;
4919 DECL_PENDING_INLINE_P (clone) = 0;
4921 if (name == base_dtor_identifier)
4923 /* The base-class destructor is not virtual. */
4924 DECL_VIRTUAL_P (clone) = 0;
4925 DECL_VINDEX (clone) = NULL_TREE;
4927 else if (code != ERROR_MARK)
4929 /* Set the operator code. */
4930 const ovl_op_info_t *ovl_op = OVL_OP_INFO (false, code);
4931 DECL_OVERLOADED_OPERATOR_CODE_RAW (clone) = ovl_op->ovl_op_code;
4933 /* The operator could be virtual. */
4934 if (DECL_VIRTUAL_P (clone))
4935 IDENTIFIER_VIRTUAL_P (name) = true;
4938 if (omit_inherited_parms_p)
4939 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4941 /* If there was an in-charge parameter, drop it from the function
4942 type. */
4943 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4945 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4946 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4947 /* Skip the `this' parameter. */
4948 parmtypes = TREE_CHAIN (parmtypes);
4949 /* Skip the in-charge parameter. */
4950 parmtypes = TREE_CHAIN (parmtypes);
4951 /* And the VTT parm, in a complete [cd]tor. */
4952 if (DECL_HAS_VTT_PARM_P (fn) && !need_vtt_parm_p)
4953 parmtypes = TREE_CHAIN (parmtypes);
4954 if (omit_inherited_parms_p)
4956 /* If we're omitting inherited parms, that just leaves the VTT. */
4957 gcc_assert (need_vtt_parm_p);
4958 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4960 TREE_TYPE (clone)
4961 = build_method_type_directly (basetype,
4962 TREE_TYPE (TREE_TYPE (clone)),
4963 parmtypes);
4964 TREE_TYPE (clone)
4965 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4966 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4967 TREE_TYPE (clone)
4968 = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
4971 /* Copy the function parameters. */
4972 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4974 /* Remove the in-charge parameter. */
4975 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4977 DECL_CHAIN (DECL_ARGUMENTS (clone))
4978 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4979 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4982 /* And the VTT parm, in a complete [cd]tor. */
4983 if (DECL_HAS_VTT_PARM_P (fn))
4985 if (need_vtt_parm_p)
4986 DECL_HAS_VTT_PARM_P (clone) = 1;
4987 else
4989 DECL_CHAIN (DECL_ARGUMENTS (clone))
4990 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4991 DECL_HAS_VTT_PARM_P (clone) = 0;
4995 /* A base constructor inheriting from a virtual base doesn't get the
4996 arguments. */
4997 if (omit_inherited_parms_p)
4998 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
5000 for (tree parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
5002 DECL_CONTEXT (parms) = clone;
5003 cxx_dup_lang_specific_decl (parms);
5006 /* Create the RTL for this function. */
5007 SET_DECL_RTL (clone, NULL);
5009 /* Regardless of the current scope, this is a member function, so
5010 not at namespace scope. */
5011 rest_of_decl_compilation (clone, /*top_level=*/0, at_eof);
5013 return clone;
5016 /* FN is an operator function, create a variant for CODE. */
5018 tree
5019 copy_operator_fn (tree fn, tree_code code)
5021 return copy_fndecl_with_name (fn, ovl_op_identifier (code),
5022 code, false, false);
5025 /* FN is a constructor or destructor. Clone the declaration to create
5026 a specialized in-charge or not-in-charge version, as indicated by
5027 NAME. */
5029 static tree
5030 build_clone (tree fn, tree name, bool need_vtt_parm_p,
5031 bool omit_inherited_parms_p)
5033 tree clone;
5035 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
5036 if (TREE_CODE (fn) == TEMPLATE_DECL)
5038 clone = copy_decl (fn);
5039 DECL_NAME (clone) = name;
5041 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name,
5042 need_vtt_parm_p, omit_inherited_parms_p);
5043 DECL_TEMPLATE_RESULT (clone) = result;
5045 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
5046 DECL_TI_TEMPLATE (result) = clone;
5048 TREE_TYPE (clone) = TREE_TYPE (result);
5050 else
5052 clone = copy_fndecl_with_name (fn, name, ERROR_MARK,
5053 need_vtt_parm_p, omit_inherited_parms_p);
5054 DECL_CLONED_FUNCTION (clone) = fn;
5057 /* Remember where this function came from. */
5058 DECL_ABSTRACT_ORIGIN (clone) = fn;
5060 /* Make it easy to find the CLONE given the FN. Note the
5061 template_result of a template will be chained this way too. */
5062 DECL_CHAIN (clone) = DECL_CHAIN (fn);
5063 DECL_CHAIN (fn) = clone;
5065 return clone;
5068 /* Build the clones of FN, return the number of clones built. These
5069 will be inserted onto DECL_CHAIN of FN. */
5071 void
5072 build_cdtor_clones (tree fn, bool needs_vtt_p, bool base_omits_inherited_p,
5073 bool update_methods)
5075 unsigned count = 0;
5077 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
5079 /* For each constructor, we need two variants: an in-charge version
5080 and a not-in-charge version. */
5081 build_clone (fn, complete_ctor_identifier, false, false);
5082 build_clone (fn, base_ctor_identifier, needs_vtt_p,
5083 base_omits_inherited_p);
5084 count += 2;
5086 else
5088 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
5090 /* For each destructor, we need three variants: an in-charge
5091 version, a not-in-charge version, and an in-charge deleting
5092 version. We clone the deleting version first because that
5093 means it will go second on the TYPE_FIELDS list -- and that
5094 corresponds to the correct layout order in the virtual
5095 function table.
5097 For a non-virtual destructor, we do not build a deleting
5098 destructor. */
5099 if (DECL_VIRTUAL_P (fn))
5101 build_clone (fn, deleting_dtor_identifier, false, false);
5102 count++;
5104 build_clone (fn, complete_dtor_identifier, false, false);
5105 build_clone (fn, base_dtor_identifier, needs_vtt_p, false);
5106 count += 2;
5109 /* The original is now an abstract function that is never
5110 emitted. */
5111 DECL_ABSTRACT_P (fn) = true;
5113 if (update_methods)
5114 for (tree clone = fn; count--;)
5116 clone = DECL_CHAIN (clone);
5117 add_method (DECL_CONTEXT (clone), clone, false);
5121 /* Produce declarations for all appropriate clones of FN. If
5122 UPDATE_METHODS is true, the clones are added to the
5123 CLASSTYPE_MEMBER_VEC. */
5125 void
5126 clone_cdtor (tree fn, bool update_methods)
5128 /* Avoid inappropriate cloning. */
5129 if (DECL_CHAIN (fn)
5130 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
5131 return;
5133 /* Base cdtors need a vtt parm if there are virtual bases. */
5134 bool vtt = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn));
5136 /* Base ctor omits inherited parms it needs a vttparm and inherited
5137 from a virtual nase ctor. */
5138 bool base_omits_inherited = (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
5139 && base_ctor_omit_inherited_parms (fn));
5141 build_cdtor_clones (fn, vtt, base_omits_inherited, update_methods);
5144 /* DECL is an in charge constructor, which is being defined. This will
5145 have had an in class declaration, from whence clones were
5146 declared. An out-of-class definition can specify additional default
5147 arguments. As it is the clones that are involved in overload
5148 resolution, we must propagate the information from the DECL to its
5149 clones. */
5151 void
5152 adjust_clone_args (tree decl)
5154 tree clone;
5156 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
5157 clone = DECL_CHAIN (clone))
5159 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
5160 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
5161 tree decl_parms, clone_parms;
5163 /* Skip the 'this' parameter. */
5164 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
5165 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5167 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
5168 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5169 if (DECL_HAS_VTT_PARM_P (decl))
5170 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5172 clone_parms = orig_clone_parms;
5173 if (DECL_HAS_VTT_PARM_P (clone))
5174 clone_parms = TREE_CHAIN (clone_parms);
5176 for (decl_parms = orig_decl_parms; decl_parms;
5177 decl_parms = TREE_CHAIN (decl_parms),
5178 clone_parms = TREE_CHAIN (clone_parms))
5180 if (clone_parms == void_list_node)
5182 gcc_assert (decl_parms == clone_parms
5183 || ctor_omit_inherited_parms (clone));
5184 break;
5187 gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms),
5188 TREE_VALUE (clone_parms)));
5190 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
5192 /* A default parameter has been added. Adjust the
5193 clone's parameters. */
5194 clone_parms = orig_decl_parms;
5196 if (DECL_HAS_VTT_PARM_P (clone))
5198 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
5199 TREE_VALUE (orig_clone_parms),
5200 clone_parms);
5201 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
5204 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5205 tree type
5206 = build_method_type_directly (basetype,
5207 TREE_TYPE (TREE_TYPE (clone)),
5208 clone_parms);
5209 if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
5210 type = cp_build_type_attribute_variant (type, attrs);
5211 type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
5212 TREE_TYPE (clone) = type;
5214 clone_parms = NULL_TREE;
5215 break;
5218 gcc_assert (!clone_parms || clone_parms == void_list_node);
5222 /* For each of the constructors and destructors in T, create an
5223 in-charge and not-in-charge variant. */
5225 static void
5226 clone_constructors_and_destructors (tree t)
5228 /* We do not need to propagate the usingness to the clone, at this
5229 point that is not needed. */
5230 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5231 clone_cdtor (fn, /*update_methods=*/true);
5233 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5234 clone_cdtor (dtor, /*update_methods=*/true);
5237 /* Deduce noexcept for a destructor DTOR. */
5239 void
5240 deduce_noexcept_on_destructor (tree dtor)
5242 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5243 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5244 noexcept_deferred_spec);
5247 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5248 of TYPE for virtual functions which FNDECL overrides. Return a
5249 mask of the tm attributes found therein. */
5251 static int
5252 look_for_tm_attr_overrides (tree type, tree fndecl)
5254 tree binfo = TYPE_BINFO (type);
5255 tree base_binfo;
5256 int ix, found = 0;
5258 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5260 tree o, basetype = BINFO_TYPE (base_binfo);
5262 if (!TYPE_POLYMORPHIC_P (basetype))
5263 continue;
5265 o = look_for_overrides_here (basetype, fndecl);
5266 if (o)
5268 if (lookup_attribute ("transaction_safe_dynamic",
5269 DECL_ATTRIBUTES (o)))
5270 /* transaction_safe_dynamic is not inherited. */;
5271 else
5272 found |= tm_attr_to_mask (find_tm_attribute
5273 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5275 else
5276 found |= look_for_tm_attr_overrides (basetype, fndecl);
5279 return found;
5282 /* Subroutine of set_method_tm_attributes. Handle the checks and
5283 inheritance for one virtual method FNDECL. */
5285 static void
5286 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5288 tree tm_attr;
5289 int found, have;
5291 found = look_for_tm_attr_overrides (type, fndecl);
5293 /* If FNDECL doesn't actually override anything (i.e. T is the
5294 class that first declares FNDECL virtual), then we're done. */
5295 if (found == 0)
5296 return;
5298 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5299 have = tm_attr_to_mask (tm_attr);
5301 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5302 tm_pure must match exactly, otherwise no weakening of
5303 tm_safe > tm_callable > nothing. */
5304 /* ??? The tm_pure attribute didn't make the transition to the
5305 multivendor language spec. */
5306 if (have == TM_ATTR_PURE)
5308 if (found != TM_ATTR_PURE)
5310 found &= -found;
5311 goto err_override;
5314 /* If the overridden function is tm_pure, then FNDECL must be. */
5315 else if (found == TM_ATTR_PURE && tm_attr)
5316 goto err_override;
5317 /* Look for base class combinations that cannot be satisfied. */
5318 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5320 found &= ~TM_ATTR_PURE;
5321 found &= -found;
5322 error_at (DECL_SOURCE_LOCATION (fndecl),
5323 "method overrides both %<transaction_pure%> and %qE methods",
5324 tm_mask_to_attr (found));
5326 /* If FNDECL did not declare an attribute, then inherit the most
5327 restrictive one. */
5328 else if (tm_attr == NULL)
5330 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5332 /* Otherwise validate that we're not weaker than a function
5333 that is being overridden. */
5334 else
5336 found &= -found;
5337 if (found <= TM_ATTR_CALLABLE && have > found)
5338 goto err_override;
5340 return;
5342 err_override:
5343 error_at (DECL_SOURCE_LOCATION (fndecl),
5344 "method declared %qE overriding %qE method",
5345 tm_attr, tm_mask_to_attr (found));
5348 /* For each of the methods in T, propagate a class-level tm attribute. */
5350 static void
5351 set_method_tm_attributes (tree t)
5353 tree class_tm_attr, fndecl;
5355 /* Don't bother collecting tm attributes if transactional memory
5356 support is not enabled. */
5357 if (!flag_tm)
5358 return;
5360 /* Process virtual methods first, as they inherit directly from the
5361 base virtual function and also require validation of new attributes. */
5362 if (TYPE_CONTAINS_VPTR_P (t))
5364 tree vchain;
5365 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5366 vchain = TREE_CHAIN (vchain))
5368 fndecl = BV_FN (vchain);
5369 if (DECL_THUNK_P (fndecl))
5370 fndecl = THUNK_TARGET (fndecl);
5371 set_one_vmethod_tm_attributes (t, fndecl);
5375 /* If the class doesn't have an attribute, nothing more to do. */
5376 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5377 if (class_tm_attr == NULL)
5378 return;
5380 /* Any method that does not yet have a tm attribute inherits
5381 the one from the class. */
5382 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
5383 if (DECL_DECLARES_FUNCTION_P (fndecl)
5384 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5385 apply_tm_attr (fndecl, class_tm_attr);
5388 /* Returns true if FN is a default constructor. */
5390 bool
5391 default_ctor_p (const_tree fn)
5393 return (DECL_CONSTRUCTOR_P (fn)
5394 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5397 /* Returns true iff class T has a user-provided constructor that can be called
5398 with more than zero arguments. */
5400 bool
5401 type_has_user_nondefault_constructor (tree t)
5403 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5404 return false;
5406 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5408 if (user_provided_p (fn)
5409 && (TREE_CODE (fn) == TEMPLATE_DECL
5410 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5411 != NULL_TREE)))
5412 return true;
5415 return false;
5418 /* Returns the defaulted constructor if T has one. Otherwise, returns
5419 NULL_TREE. */
5421 tree
5422 in_class_defaulted_default_constructor (tree t)
5424 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5425 return NULL_TREE;
5427 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5429 tree fn = *iter;
5431 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5432 && default_ctor_p (fn))
5433 return fn;
5436 return NULL_TREE;
5439 /* Returns true iff FN is a user-provided function, i.e. user-declared
5440 and not defaulted at its first declaration. */
5442 bool
5443 user_provided_p (tree fn)
5445 fn = STRIP_TEMPLATE (fn);
5446 return (!DECL_ARTIFICIAL (fn)
5447 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5448 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5451 /* Returns true iff class T has a user-provided constructor. */
5453 bool
5454 type_has_user_provided_constructor (tree t)
5456 if (!CLASS_TYPE_P (t))
5457 return false;
5459 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5460 return false;
5462 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5463 if (user_provided_p (*iter))
5464 return true;
5466 return false;
5469 /* Returns true iff class T has a user-provided or explicit constructor. */
5471 bool
5472 type_has_user_provided_or_explicit_constructor (tree t)
5474 if (!CLASS_TYPE_P (t))
5475 return false;
5477 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5478 return false;
5480 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5482 tree fn = *iter;
5483 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5484 return true;
5487 return false;
5490 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5491 declared or explicitly defaulted in the class body) default
5492 constructor. */
5494 bool
5495 type_has_non_user_provided_default_constructor (tree t)
5497 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5498 return false;
5499 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5500 return true;
5502 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5504 tree fn = *iter;
5505 if (TREE_CODE (fn) == FUNCTION_DECL
5506 && default_ctor_p (fn)
5507 && !user_provided_p (fn))
5508 return true;
5511 return false;
5514 /* TYPE is being used as a virtual base, and has a non-trivial move
5515 assignment. Return true if this is due to there being a user-provided
5516 move assignment in TYPE or one of its subobjects; if there isn't, then
5517 multiple move assignment can't cause any harm. */
5519 bool
5520 vbase_has_user_provided_move_assign (tree type)
5522 /* Does the type itself have a user-provided move assignment operator? */
5523 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5524 for (ovl_iterator iter (get_class_binding_direct
5525 (type, assign_op_identifier));
5526 iter; ++iter)
5527 if (user_provided_p (*iter) && move_fn_p (*iter))
5528 return true;
5530 /* Do any of its bases? */
5531 tree binfo = TYPE_BINFO (type);
5532 tree base_binfo;
5533 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5534 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5535 return true;
5537 /* Or non-static data members? */
5538 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5540 if (TREE_CODE (field) == FIELD_DECL
5541 && CLASS_TYPE_P (TREE_TYPE (field))
5542 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5543 return true;
5546 /* Seems not. */
5547 return false;
5550 /* If default-initialization leaves part of TYPE uninitialized, returns
5551 a DECL for the field or TYPE itself (DR 253). */
5553 tree
5554 default_init_uninitialized_part (tree type)
5556 tree t, r, binfo;
5557 int i;
5559 type = strip_array_types (type);
5560 if (!CLASS_TYPE_P (type))
5561 return type;
5562 if (!type_has_non_user_provided_default_constructor (type))
5563 return NULL_TREE;
5564 for (binfo = TYPE_BINFO (type), i = 0;
5565 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5567 r = default_init_uninitialized_part (BINFO_TYPE (t));
5568 if (r)
5569 return r;
5571 for (t = next_aggregate_field (TYPE_FIELDS (type)); t;
5572 t = next_aggregate_field (DECL_CHAIN (t)))
5573 if (!DECL_INITIAL (t) && !DECL_ARTIFICIAL (t))
5575 r = default_init_uninitialized_part (TREE_TYPE (t));
5576 if (r)
5577 return DECL_P (r) ? r : t;
5580 return NULL_TREE;
5583 /* Returns true iff for class T, a trivial synthesized default constructor
5584 would be constexpr. */
5586 bool
5587 trivial_default_constructor_is_constexpr (tree t)
5589 /* A defaulted trivial default constructor is constexpr
5590 if there is nothing to initialize. */
5591 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5592 /* A class with a vptr doesn't have a trivial default ctor.
5593 In C++20, a class can have transient uninitialized members, e.g.:
5595 struct S { int i; constexpr S() = default; };
5597 should work. */
5598 return (cxx_dialect >= cxx20
5599 || is_really_empty_class (t, /*ignore_vptr*/true));
5602 /* Returns true iff class T has a constexpr default constructor. */
5604 bool
5605 type_has_constexpr_default_constructor (tree t)
5607 tree fns;
5609 if (!CLASS_TYPE_P (t))
5611 /* The caller should have stripped an enclosing array. */
5612 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5613 return false;
5615 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5617 if (!TYPE_HAS_COMPLEX_DFLT (t))
5618 return trivial_default_constructor_is_constexpr (t);
5619 /* Non-trivial, we need to check subobject constructors. */
5620 lazily_declare_fn (sfk_constructor, t);
5622 fns = locate_ctor (t);
5623 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5626 /* Returns true iff class T has a constexpr default constructor or has an
5627 implicitly declared default constructor that we can't tell if it's constexpr
5628 without forcing a lazy declaration (which might cause undesired
5629 instantiations). */
5631 static bool
5632 type_maybe_constexpr_default_constructor (tree t)
5634 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5635 && TYPE_HAS_COMPLEX_DFLT (t))
5636 /* Assume it's constexpr. */
5637 return true;
5638 return type_has_constexpr_default_constructor (t);
5641 /* Returns true iff class T has a constexpr destructor. */
5643 bool
5644 type_has_constexpr_destructor (tree t)
5646 tree fns;
5648 if (CLASSTYPE_LAZY_DESTRUCTOR (t))
5649 /* Non-trivial, we need to check subobject destructors. */
5650 lazily_declare_fn (sfk_destructor, t);
5651 fns = CLASSTYPE_DESTRUCTOR (t);
5652 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5655 /* Returns true iff class T has a constexpr destructor or has an
5656 implicitly declared destructor that we can't tell if it's constexpr
5657 without forcing a lazy declaration (which might cause undesired
5658 instantiations). */
5660 static bool
5661 type_maybe_constexpr_destructor (tree t)
5663 /* Until C++20, only trivial destruction is constexpr. */
5664 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (t))
5665 return true;
5666 if (cxx_dialect < cxx20)
5667 return false;
5668 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t))
5669 /* Assume it's constexpr. */
5670 return true;
5671 tree fn = CLASSTYPE_DESTRUCTOR (t);
5672 return (fn && maybe_constexpr_fn (fn));
5675 /* Returns true iff class TYPE has a virtual destructor. */
5677 bool
5678 type_has_virtual_destructor (tree type)
5680 tree dtor;
5682 if (!NON_UNION_CLASS_TYPE_P (type))
5683 return false;
5685 gcc_assert (COMPLETE_TYPE_P (type));
5686 dtor = CLASSTYPE_DESTRUCTOR (type);
5687 return (dtor && DECL_VIRTUAL_P (dtor));
5690 /* Returns true iff T, a class, has a move-assignment or
5691 move-constructor. Does not lazily declare either.
5692 If USER_P is false, any move function will do. If it is true, the
5693 move function must be user-declared.
5695 Note that user-declared here is different from "user-provided",
5696 which doesn't include functions that are defaulted in the
5697 class. */
5699 bool
5700 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5702 gcc_assert (user_p
5703 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5704 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5706 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5707 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5708 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5709 return true;
5711 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5712 for (ovl_iterator iter (get_class_binding_direct
5713 (t, assign_op_identifier));
5714 iter; ++iter)
5715 if ((!user_p || !DECL_ARTIFICIAL (*iter))
5716 && DECL_CONTEXT (*iter) == t
5717 && move_fn_p (*iter))
5718 return true;
5720 return false;
5723 /* True iff T has a move constructor that is not deleted. */
5725 bool
5726 classtype_has_non_deleted_move_ctor (tree t)
5728 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5729 lazily_declare_fn (sfk_move_constructor, t);
5730 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5731 if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5732 return true;
5733 return false;
5736 /* If T, a class, has a user-provided copy constructor, copy assignment
5737 operator, or destructor, returns that function. Otherwise, null. */
5739 tree
5740 classtype_has_depr_implicit_copy (tree t)
5742 if (!CLASSTYPE_LAZY_COPY_CTOR (t))
5743 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5745 tree fn = *iter;
5746 if (user_provided_p (fn) && copy_fn_p (fn))
5747 return fn;
5750 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
5751 for (ovl_iterator iter (get_class_binding_direct
5752 (t, assign_op_identifier));
5753 iter; ++iter)
5755 tree fn = *iter;
5756 if (DECL_CONTEXT (fn) == t
5757 && user_provided_p (fn) && copy_fn_p (fn))
5758 return fn;
5761 if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
5763 tree fn = CLASSTYPE_DESTRUCTOR (t);
5764 if (user_provided_p (fn))
5765 return fn;
5768 return NULL_TREE;
5771 /* True iff T has a member or friend declaration of operator OP. */
5773 bool
5774 classtype_has_op (tree t, tree_code op)
5776 tree name = ovl_op_identifier (op);
5777 if (get_class_binding (t, name))
5778 return true;
5779 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
5780 if (FRIEND_NAME (f) == name)
5781 return true;
5782 return false;
5786 /* If T has a defaulted member or friend declaration of OP, return it. */
5788 tree
5789 classtype_has_defaulted_op (tree t, tree_code op)
5791 tree name = ovl_op_identifier (op);
5792 for (ovl_iterator oi (get_class_binding (t, name)); oi; ++oi)
5794 tree fn = *oi;
5795 if (DECL_DEFAULTED_FN (fn))
5796 return fn;
5798 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
5799 if (FRIEND_NAME (f) == name)
5800 for (tree l = FRIEND_DECLS (f); l; l = TREE_CHAIN (l))
5802 tree fn = TREE_VALUE (l);
5803 if (DECL_DEFAULTED_FN (fn))
5804 return fn;
5806 return NULL_TREE;
5809 /* Nonzero if we need to build up a constructor call when initializing an
5810 object of this class, either because it has a user-declared constructor
5811 or because it doesn't have a default constructor (so we need to give an
5812 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5813 what you care about is whether or not an object can be produced by a
5814 constructor (e.g. so we don't set TREE_READONLY on const variables of
5815 such type); use this function when what you care about is whether or not
5816 to try to call a constructor to create an object. The latter case is
5817 the former plus some cases of constructors that cannot be called. */
5819 bool
5820 type_build_ctor_call (tree t)
5822 tree inner;
5823 if (TYPE_NEEDS_CONSTRUCTING (t))
5824 return true;
5825 inner = strip_array_types (t);
5826 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5827 return false;
5828 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5829 return true;
5830 if (cxx_dialect < cxx11)
5831 return false;
5832 /* A user-declared constructor might be private, and a constructor might
5833 be trivial but deleted. */
5834 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5835 iter; ++iter)
5837 tree fn = *iter;
5838 if (!DECL_ARTIFICIAL (fn)
5839 || TREE_DEPRECATED (fn)
5840 || TREE_UNAVAILABLE (fn)
5841 || DECL_DELETED_FN (fn))
5842 return true;
5844 return false;
5847 /* Like type_build_ctor_call, but for destructors. */
5849 bool
5850 type_build_dtor_call (tree t)
5852 tree inner;
5853 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5854 return true;
5855 inner = strip_array_types (t);
5856 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5857 || !COMPLETE_TYPE_P (inner))
5858 return false;
5859 if (cxx_dialect < cxx11)
5860 return false;
5861 /* A user-declared destructor might be private, and a destructor might
5862 be trivial but deleted. */
5863 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5864 iter; ++iter)
5866 tree fn = *iter;
5867 if (!DECL_ARTIFICIAL (fn)
5868 || TREE_DEPRECATED (fn)
5869 || TREE_UNAVAILABLE (fn)
5870 || DECL_DELETED_FN (fn))
5871 return true;
5873 return false;
5876 /* Returns TRUE iff we need a cookie when dynamically allocating an
5877 array whose elements have the indicated class TYPE. */
5879 static bool
5880 type_requires_array_cookie (tree type)
5882 tree fns;
5883 bool has_two_argument_delete_p = false;
5885 gcc_assert (CLASS_TYPE_P (type));
5887 /* If there's a non-trivial destructor, we need a cookie. In order
5888 to iterate through the array calling the destructor for each
5889 element, we'll have to know how many elements there are. */
5890 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5891 return true;
5893 /* If the usual deallocation function is a two-argument whose second
5894 argument is of type `size_t', then we have to pass the size of
5895 the array to the deallocation function, so we will need to store
5896 a cookie. */
5897 fns = lookup_fnfields (TYPE_BINFO (type),
5898 ovl_op_identifier (false, VEC_DELETE_EXPR),
5899 /*protect=*/0, tf_warning_or_error);
5900 /* If there are no `operator []' members, or the lookup is
5901 ambiguous, then we don't need a cookie. */
5902 if (!fns || fns == error_mark_node)
5903 return false;
5904 /* Loop through all of the functions. */
5905 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5907 tree fn = *iter;
5909 /* See if this function is a one-argument delete function. If
5910 it is, then it will be the usual deallocation function. */
5911 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5912 if (second_parm == void_list_node)
5913 return false;
5914 /* Do not consider this function if its second argument is an
5915 ellipsis. */
5916 if (!second_parm)
5917 continue;
5918 /* Otherwise, if we have a two-argument function and the second
5919 argument is `size_t', it will be the usual deallocation
5920 function -- unless there is one-argument function, too. */
5921 if (TREE_CHAIN (second_parm) == void_list_node
5922 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5923 has_two_argument_delete_p = true;
5926 return has_two_argument_delete_p;
5929 /* Finish computing the `literal type' property of class type T.
5931 At this point, we have already processed base classes and
5932 non-static data members. We need to check whether the copy
5933 constructor is trivial, the destructor is trivial, and there
5934 is a trivial default constructor or at least one constexpr
5935 constructor other than the copy constructor. */
5937 static void
5938 finalize_literal_type_property (tree t)
5940 tree fn;
5942 if (cxx_dialect < cxx11)
5943 CLASSTYPE_LITERAL_P (t) = false;
5944 else if (CLASSTYPE_LITERAL_P (t)
5945 && !type_maybe_constexpr_destructor (t))
5946 CLASSTYPE_LITERAL_P (t) = false;
5947 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5948 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5949 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5950 && CLASSTYPE_NON_AGGREGATE (t)
5951 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5952 CLASSTYPE_LITERAL_P (t) = false;
5954 /* C++14 DR 1684 removed this restriction. */
5955 if (cxx_dialect < cxx14
5956 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5957 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5958 if (TREE_CODE (fn) == FUNCTION_DECL
5959 && DECL_DECLARED_CONSTEXPR_P (fn)
5960 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5961 && !DECL_CONSTRUCTOR_P (fn))
5963 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5964 if (!DECL_GENERATED_P (fn))
5966 auto_diagnostic_group d;
5967 if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5968 "enclosing class of %<constexpr%> non-static "
5969 "member function %q+#D is not a literal type", fn))
5970 explain_non_literal_class (t);
5975 /* T is a non-literal type used in a context which requires a constant
5976 expression. Explain why it isn't literal. */
5978 void
5979 explain_non_literal_class (tree t)
5981 static hash_set<tree> *diagnosed;
5983 if (!CLASS_TYPE_P (t))
5984 return;
5985 t = TYPE_MAIN_VARIANT (t);
5987 if (diagnosed == NULL)
5988 diagnosed = new hash_set<tree>;
5989 if (diagnosed->add (t))
5990 /* Already explained. */
5991 return;
5993 auto_diagnostic_group d;
5994 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5995 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5996 inform (UNKNOWN_LOCATION,
5997 " %qT is a closure type, which is only literal in "
5998 "C++17 and later", t);
5999 else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6000 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
6001 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
6002 && !type_maybe_constexpr_destructor (t))
6003 inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
6005 else if (CLASSTYPE_NON_AGGREGATE (t)
6006 && !TYPE_HAS_TRIVIAL_DFLT (t)
6007 && !LAMBDA_TYPE_P (t)
6008 && !TYPE_HAS_CONSTEXPR_CTOR (t))
6010 inform (UNKNOWN_LOCATION,
6011 " %q+T is not an aggregate, does not have a trivial "
6012 "default constructor, and has no %<constexpr%> constructor that "
6013 "is not a copy or move constructor", t);
6014 if (type_has_non_user_provided_default_constructor (t))
6015 /* Note that we can't simply call locate_ctor because when the
6016 constructor is deleted it just returns NULL_TREE. */
6017 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6019 tree fn = *iter;
6020 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6022 parms = skip_artificial_parms_for (fn, parms);
6024 if (sufficient_parms_p (parms))
6026 if (DECL_DELETED_FN (fn))
6027 maybe_explain_implicit_delete (fn);
6028 else
6029 explain_invalid_constexpr_fn (fn);
6030 break;
6034 else
6036 tree binfo, base_binfo, field; int i;
6037 for (binfo = TYPE_BINFO (t), i = 0;
6038 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6040 tree basetype = TREE_TYPE (base_binfo);
6041 if (!CLASSTYPE_LITERAL_P (basetype))
6043 inform (UNKNOWN_LOCATION,
6044 " base class %qT of %q+T is non-literal",
6045 basetype, t);
6046 explain_non_literal_class (basetype);
6047 return;
6050 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6052 tree ftype;
6053 if (TREE_CODE (field) != FIELD_DECL)
6054 continue;
6055 ftype = TREE_TYPE (field);
6056 if (!literal_type_p (ftype))
6058 inform (DECL_SOURCE_LOCATION (field),
6059 " non-static data member %qD has non-literal type",
6060 field);
6061 if (CLASS_TYPE_P (ftype))
6062 explain_non_literal_class (ftype);
6064 if (CP_TYPE_VOLATILE_P (ftype))
6065 inform (DECL_SOURCE_LOCATION (field),
6066 " non-static data member %qD has volatile type", field);
6071 /* Check the validity of the bases and members declared in T. Add any
6072 implicitly-generated functions (like copy-constructors and
6073 assignment operators). Compute various flag bits (like
6074 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
6075 level: i.e., independently of the ABI in use. */
6077 static void
6078 check_bases_and_members (tree t)
6080 /* Nonzero if the implicitly generated copy constructor should take
6081 a non-const reference argument. */
6082 int cant_have_const_ctor;
6083 /* Nonzero if the implicitly generated assignment operator
6084 should take a non-const reference argument. */
6085 int no_const_asn_ref;
6086 tree access_decls;
6087 bool saved_complex_asn_ref;
6088 bool saved_nontrivial_dtor;
6089 tree fn;
6091 /* By default, we use const reference arguments and generate default
6092 constructors. */
6093 cant_have_const_ctor = 0;
6094 no_const_asn_ref = 0;
6096 /* Check all the base-classes and set FMEM members to point to arrays
6097 of potential interest. */
6098 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
6100 /* Deduce noexcept on destructor. This needs to happen after we've set
6101 triviality flags appropriately for our bases, and before checking
6102 overriden virtual functions via check_methods. */
6103 if (cxx_dialect >= cxx11)
6104 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
6105 for (tree fn : ovl_range (dtor))
6106 deduce_noexcept_on_destructor (fn);
6108 /* Check all the method declarations. */
6109 check_methods (t);
6111 /* Save the initial values of these flags which only indicate whether
6112 or not the class has user-provided functions. As we analyze the
6113 bases and members we can set these flags for other reasons. */
6114 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
6115 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
6117 /* Check all the data member declarations. We cannot call
6118 check_field_decls until we have called check_bases check_methods,
6119 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6120 being set appropriately. */
6121 check_field_decls (t, &access_decls,
6122 &cant_have_const_ctor,
6123 &no_const_asn_ref);
6125 /* A nearly-empty class has to be vptr-containing; a nearly empty
6126 class contains just a vptr. */
6127 if (!TYPE_CONTAINS_VPTR_P (t))
6128 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
6130 /* Do some bookkeeping that will guide the generation of implicitly
6131 declared member functions. */
6132 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6133 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6134 /* We need to call a constructor for this class if it has a
6135 user-provided constructor, or if the default constructor is going
6136 to initialize the vptr. (This is not an if-and-only-if;
6137 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
6138 themselves need constructing.) */
6139 TYPE_NEEDS_CONSTRUCTING (t)
6140 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
6141 /* [dcl.init.aggr]
6143 An aggregate is an array or a class with no user-provided
6144 constructors ... and no virtual functions.
6146 Again, other conditions for being an aggregate are checked
6147 elsewhere. */
6148 CLASSTYPE_NON_AGGREGATE (t)
6149 |= ((cxx_dialect < cxx20
6150 ? type_has_user_provided_or_explicit_constructor (t)
6151 : TYPE_HAS_USER_CONSTRUCTOR (t))
6152 || TYPE_POLYMORPHIC_P (t));
6153 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6154 retain the old definition internally for ABI reasons. */
6155 CLASSTYPE_NON_LAYOUT_POD_P (t)
6156 |= (CLASSTYPE_NON_AGGREGATE (t)
6157 || saved_nontrivial_dtor || saved_complex_asn_ref);
6158 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
6159 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6160 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6161 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
6163 /* Is this class non-layout-POD because it wasn't an aggregate in C++98? */
6164 if (CLASSTYPE_NON_POD_AGGREGATE (t))
6166 if (CLASSTYPE_NON_LAYOUT_POD_P (t))
6167 /* It's non-POD for another reason. */
6168 CLASSTYPE_NON_POD_AGGREGATE (t) = false;
6169 else if (abi_version_at_least (17))
6170 CLASSTYPE_NON_LAYOUT_POD_P (t) = true;
6173 /* If the only explicitly declared default constructor is user-provided,
6174 set TYPE_HAS_COMPLEX_DFLT. */
6175 if (!TYPE_HAS_COMPLEX_DFLT (t)
6176 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
6177 && !type_has_non_user_provided_default_constructor (t))
6178 TYPE_HAS_COMPLEX_DFLT (t) = true;
6180 /* Warn if a public base of a polymorphic type has an accessible
6181 non-virtual destructor. It is only now that we know the class is
6182 polymorphic. Although a polymorphic base will have a already
6183 been diagnosed during its definition, we warn on use too. */
6184 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
6186 tree binfo = TYPE_BINFO (t);
6187 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
6188 tree base_binfo;
6189 unsigned i;
6191 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6193 tree basetype = TREE_TYPE (base_binfo);
6195 if ((*accesses)[i] == access_public_node
6196 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
6197 && accessible_nvdtor_p (basetype))
6198 warning (OPT_Wnon_virtual_dtor,
6199 "base class %q#T has accessible non-virtual destructor",
6200 basetype);
6204 /* If the class has no user-declared constructor, but does have
6205 non-static const or reference data members that can never be
6206 initialized, issue a warning. */
6207 if (warn_uninitialized
6208 /* Classes with user-declared constructors are presumed to
6209 initialize these members. */
6210 && !TYPE_HAS_USER_CONSTRUCTOR (t)
6211 /* Aggregates can be initialized with brace-enclosed
6212 initializers. */
6213 && CLASSTYPE_NON_AGGREGATE (t))
6215 tree field;
6217 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6219 tree type;
6221 if (TREE_CODE (field) != FIELD_DECL
6222 || DECL_INITIAL (field) != NULL_TREE)
6223 continue;
6225 type = TREE_TYPE (field);
6226 if (TYPE_REF_P (type))
6227 warning_at (DECL_SOURCE_LOCATION (field),
6228 OPT_Wuninitialized, "non-static reference %q#D "
6229 "in class without a constructor", field);
6230 else if (CP_TYPE_CONST_P (type)
6231 && (!CLASS_TYPE_P (type)
6232 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6233 warning_at (DECL_SOURCE_LOCATION (field),
6234 OPT_Wuninitialized, "non-static const member %q#D "
6235 "in class without a constructor", field);
6239 /* Synthesize any needed methods. */
6240 add_implicitly_declared_members (t, &access_decls,
6241 cant_have_const_ctor,
6242 no_const_asn_ref);
6244 /* Check defaulted declarations here so we have cant_have_const_ctor
6245 and don't need to worry about clones. */
6246 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6247 if (DECL_DECLARES_FUNCTION_P (fn)
6248 && !DECL_ARTIFICIAL (fn)
6249 && DECL_DEFAULTED_IN_CLASS_P (fn))
6251 /* ...except handle comparisons later, in finish_struct_1. */
6252 if (special_function_p (fn) == sfk_comparison)
6253 continue;
6255 int copy = copy_fn_p (fn);
6256 if (copy > 0)
6258 bool imp_const_p
6259 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6260 : !no_const_asn_ref);
6261 bool fn_const_p = (copy == 2);
6263 if (fn_const_p && !imp_const_p)
6264 /* If the function is defaulted outside the class, we just
6265 give the synthesis error. Core Issue #1331 says this is
6266 no longer ill-formed, it is defined as deleted instead. */
6267 DECL_DELETED_FN (fn) = true;
6269 defaulted_late_check (fn);
6272 if (LAMBDA_TYPE_P (t))
6273 /* "This class type is not an aggregate." */
6274 CLASSTYPE_NON_AGGREGATE (t) = 1;
6276 /* Compute the 'literal type' property before we
6277 do anything with non-static member functions. */
6278 finalize_literal_type_property (t);
6280 /* Create the in-charge and not-in-charge variants of constructors
6281 and destructors. */
6282 clone_constructors_and_destructors (t);
6284 /* Process the using-declarations. */
6285 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6286 handle_using_decl (TREE_VALUE (access_decls), t);
6288 /* Figure out whether or not we will need a cookie when dynamically
6289 allocating an array of this type. */
6290 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6291 = type_requires_array_cookie (t);
6294 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6295 accordingly. If a new vfield was created (because T doesn't have a
6296 primary base class), then the newly created field is returned. It
6297 is not added to the TYPE_FIELDS list; it is the caller's
6298 responsibility to do that. Accumulate declared virtual functions
6299 on VIRTUALS_P. */
6301 static tree
6302 create_vtable_ptr (tree t, tree* virtuals_p)
6304 tree fn;
6306 /* Collect the virtual functions declared in T. */
6307 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6308 if (TREE_CODE (fn) == FUNCTION_DECL
6309 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6310 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6312 tree new_virtual = make_node (TREE_LIST);
6314 BV_FN (new_virtual) = fn;
6315 BV_DELTA (new_virtual) = integer_zero_node;
6316 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6318 TREE_CHAIN (new_virtual) = *virtuals_p;
6319 *virtuals_p = new_virtual;
6322 /* If we couldn't find an appropriate base class, create a new field
6323 here. Even if there weren't any new virtual functions, we might need a
6324 new virtual function table if we're supposed to include vptrs in
6325 all classes that need them. */
6326 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6328 /* We build this decl with vtbl_ptr_type_node, which is a
6329 `vtable_entry_type*'. It might seem more precise to use
6330 `vtable_entry_type (*)[N]' where N is the number of virtual
6331 functions. However, that would require the vtable pointer in
6332 base classes to have a different type than the vtable pointer
6333 in derived classes. We could make that happen, but that
6334 still wouldn't solve all the problems. In particular, the
6335 type-based alias analysis code would decide that assignments
6336 to the base class vtable pointer can't alias assignments to
6337 the derived class vtable pointer, since they have different
6338 types. Thus, in a derived class destructor, where the base
6339 class constructor was inlined, we could generate bad code for
6340 setting up the vtable pointer.
6342 Therefore, we use one type for all vtable pointers. We still
6343 use a type-correct type; it's just doesn't indicate the array
6344 bounds. That's better than using `void*' or some such; it's
6345 cleaner, and it let's the alias analysis code know that these
6346 stores cannot alias stores to void*! */
6347 tree field;
6349 field = build_decl (input_location,
6350 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6351 DECL_VIRTUAL_P (field) = 1;
6352 DECL_ARTIFICIAL (field) = 1;
6353 DECL_FIELD_CONTEXT (field) = t;
6354 DECL_FCONTEXT (field) = t;
6355 if (TYPE_PACKED (t))
6356 DECL_PACKED (field) = 1;
6358 TYPE_VFIELD (t) = field;
6360 /* This class is non-empty. */
6361 CLASSTYPE_EMPTY_P (t) = 0;
6363 return field;
6366 return NULL_TREE;
6369 /* Add OFFSET to all base types of BINFO which is a base in the
6370 hierarchy dominated by T.
6372 OFFSET, which is a type offset, is number of bytes. */
6374 static void
6375 propagate_binfo_offsets (tree binfo, tree offset)
6377 int i;
6378 tree primary_binfo;
6379 tree base_binfo;
6381 /* Update BINFO's offset. */
6382 BINFO_OFFSET (binfo)
6383 = fold_convert (sizetype,
6384 size_binop (PLUS_EXPR,
6385 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6386 offset));
6388 /* Find the primary base class. */
6389 primary_binfo = get_primary_binfo (binfo);
6391 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6392 propagate_binfo_offsets (primary_binfo, offset);
6394 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6395 downwards. */
6396 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6398 /* Don't do the primary base twice. */
6399 if (base_binfo == primary_binfo)
6400 continue;
6402 if (BINFO_VIRTUAL_P (base_binfo))
6403 continue;
6405 propagate_binfo_offsets (base_binfo, offset);
6409 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6410 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6411 empty subobjects of T. */
6413 static void
6414 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6416 tree vbase;
6417 tree t = rli->t;
6418 tree *next_field;
6420 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6421 return;
6423 /* Find the last field. The artificial fields created for virtual
6424 bases will go after the last extant field to date. */
6425 next_field = &TYPE_FIELDS (t);
6426 while (*next_field)
6427 next_field = &DECL_CHAIN (*next_field);
6429 /* Go through the virtual bases, allocating space for each virtual
6430 base that is not already a primary base class. These are
6431 allocated in inheritance graph order. */
6432 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6434 if (!BINFO_VIRTUAL_P (vbase))
6435 continue;
6437 if (!BINFO_PRIMARY_P (vbase))
6439 /* This virtual base is not a primary base of any class in the
6440 hierarchy, so we have to add space for it. */
6441 next_field = build_base_field (rli, vbase,
6442 access_private_node,
6443 offsets, next_field);
6448 /* Returns the offset of the byte just past the end of the base class
6449 BINFO. */
6451 static tree
6452 end_of_base (tree binfo)
6454 tree size;
6456 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6457 size = TYPE_SIZE_UNIT (char_type_node);
6458 else if (is_empty_class (BINFO_TYPE (binfo)))
6459 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6460 allocate some space for it. It cannot have virtual bases, so
6461 TYPE_SIZE_UNIT is fine. */
6462 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6463 else
6464 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6466 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6469 /* Returns one of three variations of the ending offset of T. If MODE is
6470 eoc_nvsize, the result is the ABI "nvsize" (i.e. sizeof before allocating
6471 vbases). If MODE is eoc_vsize, the result is the sizeof after allocating
6472 vbases but before rounding, which is not named in the ABI. If MODE is
6473 eoc_nv_or_dsize, the result is the greater of "nvsize" and "dsize" (the size
6474 of the actual data in the class, kinda), as used for allocation of
6475 potentially-overlapping fields. */
6477 enum eoc_mode { eoc_nvsize, eoc_vsize, eoc_nv_or_dsize };
6478 static tree
6479 end_of_class (tree t, eoc_mode mode)
6481 tree result = size_zero_node;
6482 vec<tree, va_gc> *vbases;
6483 tree binfo;
6484 tree base_binfo;
6485 tree offset;
6486 int i;
6488 for (binfo = TYPE_BINFO (t), i = 0;
6489 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6491 if (BINFO_VIRTUAL_P (base_binfo)
6492 && (!BINFO_PRIMARY_P (base_binfo)
6493 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6494 continue;
6496 offset = end_of_base (base_binfo);
6497 if (tree_int_cst_lt (result, offset))
6498 result = offset;
6501 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6502 if (TREE_CODE (field) == FIELD_DECL
6503 && !DECL_FIELD_IS_BASE (field))
6505 tree size = DECL_SIZE_UNIT (field);
6506 if (!size)
6507 /* DECL_SIZE_UNIT can be null for a flexible array. */
6508 continue;
6510 if (is_empty_field (field))
6511 /* For empty fields DECL_SIZE_UNIT is 0, but we want the
6512 size of the type (usually 1) for computing nvsize. */
6513 size = TYPE_SIZE_UNIT (TREE_TYPE (field));
6515 if (DECL_BIT_FIELD_TYPE (field))
6517 offset = size_binop (PLUS_EXPR, bit_position (field),
6518 DECL_SIZE (field));
6519 offset = size_binop (CEIL_DIV_EXPR, offset, bitsize_unit_node);
6520 offset = fold_convert (sizetype, offset);
6522 else
6523 offset = size_binop (PLUS_EXPR, byte_position (field), size);
6524 if (tree_int_cst_lt (result, offset))
6525 result = offset;
6528 if (mode != eoc_nvsize)
6529 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6530 vec_safe_iterate (vbases, i, &base_binfo); i++)
6532 if (mode == eoc_nv_or_dsize)
6533 /* For dsize, don't count trailing empty bases. */
6534 offset = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo),
6535 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
6536 else
6537 offset = end_of_base (base_binfo);
6538 if (tree_int_cst_lt (result, offset))
6539 result = offset;
6542 return result;
6545 /* Warn as appropriate about the change in whether we pack into the tail
6546 padding of FIELD, a base field which has a C++14 aggregate type with default
6547 member initializers. */
6549 static void
6550 check_non_pod_aggregate (tree field)
6552 if (!abi_version_crosses (17) || cxx_dialect < cxx14)
6553 return;
6554 if (TREE_CODE (field) != FIELD_DECL
6555 || (!DECL_FIELD_IS_BASE (field)
6556 && !field_poverlapping_p (field)))
6557 return;
6558 tree next = DECL_CHAIN (field);
6559 while (next && TREE_CODE (next) != FIELD_DECL) next = DECL_CHAIN (next);
6560 if (!next)
6561 return;
6562 tree type = TREE_TYPE (field);
6563 if (TYPE_IDENTIFIER (type) == as_base_identifier)
6564 type = TYPE_CONTEXT (type);
6565 if (!CLASS_TYPE_P (type) || !CLASSTYPE_NON_POD_AGGREGATE (type))
6566 return;
6567 tree size = end_of_class (type, (DECL_FIELD_IS_BASE (field)
6568 ? eoc_nvsize : eoc_nv_or_dsize));
6569 tree rounded = round_up_loc (input_location, size, DECL_ALIGN_UNIT (next));
6570 if (tree_int_cst_lt (rounded, TYPE_SIZE_UNIT (type)))
6572 location_t loc = DECL_SOURCE_LOCATION (next);
6573 if (DECL_FIELD_IS_BASE (next))
6574 warning_at (loc, OPT_Wabi,"offset of %qT base class for "
6575 "%<-std=c++14%> and up changes in "
6576 "%<-fabi-version=17%> (GCC 12)", TREE_TYPE (next));
6577 else
6578 warning_at (loc, OPT_Wabi, "offset of %qD for "
6579 "%<-std=c++14%> and up changes in "
6580 "%<-fabi-version=17%> (GCC 12)", next);
6584 /* Warn about bases of T that are inaccessible because they are
6585 ambiguous. For example:
6587 struct S {};
6588 struct T : public S {};
6589 struct U : public S, public T {};
6591 Here, `(S*) new U' is not allowed because there are two `S'
6592 subobjects of U. */
6594 static void
6595 maybe_warn_about_inaccessible_bases (tree t)
6597 int i;
6598 vec<tree, va_gc> *vbases;
6599 tree basetype;
6600 tree binfo;
6601 tree base_binfo;
6603 /* If not checking for warning then return early. */
6604 if (!warn_inaccessible_base)
6605 return;
6607 /* If there are no repeated bases, nothing can be ambiguous. */
6608 if (!CLASSTYPE_REPEATED_BASE_P (t))
6609 return;
6611 /* Check direct bases. */
6612 for (binfo = TYPE_BINFO (t), i = 0;
6613 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6615 basetype = BINFO_TYPE (base_binfo);
6617 if (!uniquely_derived_from_p (basetype, t))
6618 warning (OPT_Winaccessible_base, "direct base %qT inaccessible "
6619 "in %qT due to ambiguity", basetype, t);
6622 /* Check for ambiguous virtual bases. */
6623 if (extra_warnings)
6624 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6625 vec_safe_iterate (vbases, i, &binfo); i++)
6627 basetype = BINFO_TYPE (binfo);
6629 if (!uniquely_derived_from_p (basetype, t))
6630 warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in "
6631 "%qT due to ambiguity", basetype, t);
6635 /* Compare two INTEGER_CSTs K1 and K2. */
6637 static int
6638 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6640 return tree_int_cst_compare ((tree) k1, (tree) k2);
6643 /* Increase the size indicated in RLI to account for empty classes
6644 that are "off the end" of the class. */
6646 static void
6647 include_empty_classes (record_layout_info rli)
6649 tree eoc;
6650 tree rli_size;
6652 /* It might be the case that we grew the class to allocate a
6653 zero-sized base class. That won't be reflected in RLI, yet,
6654 because we are willing to overlay multiple bases at the same
6655 offset. However, now we need to make sure that RLI is big enough
6656 to reflect the entire class. */
6657 eoc = end_of_class (rli->t, eoc_vsize);
6658 rli_size = rli_size_unit_so_far (rli);
6659 if (TREE_CODE (rli_size) == INTEGER_CST
6660 && tree_int_cst_lt (rli_size, eoc))
6662 /* The size should have been rounded to a whole byte. */
6663 gcc_assert (tree_int_cst_equal
6664 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6665 rli->bitpos
6666 = size_binop (PLUS_EXPR,
6667 rli->bitpos,
6668 size_binop (MULT_EXPR,
6669 fold_convert (bitsizetype,
6670 size_binop (MINUS_EXPR,
6671 eoc, rli_size)),
6672 bitsize_int (BITS_PER_UNIT)));
6673 normalize_rli (rli);
6677 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6678 BINFO_OFFSETs for all of the base-classes. Position the vtable
6679 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6681 static void
6682 layout_class_type (tree t, tree *virtuals_p)
6684 tree non_static_data_members;
6685 tree field;
6686 tree vptr;
6687 record_layout_info rli;
6688 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6689 types that appear at that offset. */
6690 splay_tree empty_base_offsets;
6691 /* True if the last field laid out was a bit-field. */
6692 bool last_field_was_bitfield = false;
6693 /* The location at which the next field should be inserted. */
6694 tree *next_field;
6696 /* Keep track of the first non-static data member. */
6697 non_static_data_members = TYPE_FIELDS (t);
6699 /* Start laying out the record. */
6700 rli = start_record_layout (t);
6702 /* Mark all the primary bases in the hierarchy. */
6703 determine_primary_bases (t);
6705 /* Create a pointer to our virtual function table. */
6706 vptr = create_vtable_ptr (t, virtuals_p);
6708 /* The vptr is always the first thing in the class. */
6709 if (vptr)
6711 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6712 TYPE_FIELDS (t) = vptr;
6713 next_field = &DECL_CHAIN (vptr);
6714 place_field (rli, vptr);
6716 else
6717 next_field = &TYPE_FIELDS (t);
6719 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6720 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6721 NULL, NULL);
6722 build_base_fields (rli, empty_base_offsets, next_field);
6724 /* Layout the non-static data members. */
6725 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6727 tree type;
6728 tree padding;
6730 /* We still pass things that aren't non-static data members to
6731 the back end, in case it wants to do something with them. */
6732 if (TREE_CODE (field) != FIELD_DECL)
6734 place_field (rli, field);
6735 /* If the static data member has incomplete type, keep track
6736 of it so that it can be completed later. (The handling
6737 of pending statics in finish_record_layout is
6738 insufficient; consider:
6740 struct S1;
6741 struct S2 { static S1 s1; };
6743 At this point, finish_record_layout will be called, but
6744 S1 is still incomplete.) */
6745 if (VAR_P (field))
6747 maybe_register_incomplete_var (field);
6748 /* The visibility of static data members is determined
6749 at their point of declaration, not their point of
6750 definition. */
6751 determine_visibility (field);
6753 continue;
6756 type = TREE_TYPE (field);
6757 if (type == error_mark_node)
6758 continue;
6760 padding = NULL_TREE;
6762 bool might_overlap = field_poverlapping_p (field);
6764 if (might_overlap && CLASS_TYPE_P (type)
6765 && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type)))
6767 /* if D is a potentially-overlapping data member, update sizeof(C) to
6768 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6769 if (CLASSTYPE_EMPTY_P (type))
6770 DECL_SIZE (field) = DECL_SIZE_UNIT (field) = size_zero_node;
6771 else
6773 tree size = end_of_class (type, eoc_nv_or_dsize);
6774 DECL_SIZE_UNIT (field) = size;
6775 DECL_SIZE (field) = bit_from_pos (size, bitsize_zero_node);
6779 /* If this field is a bit-field whose width is greater than its
6780 type, then there are some special rules for allocating
6781 it. */
6782 if (DECL_C_BIT_FIELD (field)
6783 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6785 bool was_unnamed_p = false;
6786 /* We must allocate the bits as if suitably aligned for the
6787 longest integer type that fits in this many bits. Then,
6788 we are supposed to use the left over bits as additional
6789 padding. */
6791 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6792 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6793 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6794 limit = DECL_SIZE (field);
6796 tree integer_type = integer_types[itk_char];
6797 for (unsigned itk = itk_char; itk != itk_none; itk++)
6798 if (tree next = integer_types[itk])
6800 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6801 /* Too big, so our current guess is what we want. */
6802 break;
6803 /* Not bigger than limit, ok */
6804 integer_type = next;
6807 /* Figure out how much additional padding is required. */
6808 if (TREE_CODE (t) == UNION_TYPE)
6809 /* In a union, the padding field must have the full width
6810 of the bit-field; all fields start at offset zero. */
6811 padding = DECL_SIZE (field);
6812 else
6813 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6814 TYPE_SIZE (integer_type));
6816 if (integer_zerop (padding))
6817 padding = NULL_TREE;
6819 /* An unnamed bitfield does not normally affect the
6820 alignment of the containing class on a target where
6821 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6822 make any exceptions for unnamed bitfields when the
6823 bitfields are longer than their types. Therefore, we
6824 temporarily give the field a name. */
6825 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6827 was_unnamed_p = true;
6828 DECL_NAME (field) = make_anon_name ();
6831 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6832 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6833 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6834 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6835 empty_base_offsets);
6836 if (was_unnamed_p)
6837 DECL_NAME (field) = NULL_TREE;
6838 /* Now that layout has been performed, set the size of the
6839 field to the size of its declared type; the rest of the
6840 field is effectively invisible. */
6841 DECL_SIZE (field) = TYPE_SIZE (type);
6842 /* We must also reset the DECL_MODE of the field. */
6843 SET_DECL_MODE (field, TYPE_MODE (type));
6845 else if (might_overlap && is_empty_class (type))
6847 SET_DECL_FIELD_ABI_IGNORED (field, 1);
6848 layout_empty_base_or_field (rli, field, empty_base_offsets);
6850 else
6851 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6852 empty_base_offsets);
6854 /* Remember the location of any empty classes in FIELD. */
6855 record_subobject_offsets (field, empty_base_offsets);
6857 /* If a bit-field does not immediately follow another bit-field,
6858 and yet it starts in the middle of a byte, we have failed to
6859 comply with the ABI. */
6860 if (warn_abi
6861 && DECL_C_BIT_FIELD (field)
6862 /* The TREE_NO_WARNING flag gets set by Objective-C when
6863 laying out an Objective-C class. The ObjC ABI differs
6864 from the C++ ABI, and so we do not want a warning
6865 here. */
6866 && !warning_suppressed_p (field, OPT_Wabi)
6867 && !last_field_was_bitfield
6868 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6869 DECL_FIELD_BIT_OFFSET (field),
6870 bitsize_unit_node)))
6871 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6872 "offset of %qD is not ABI-compliant and may "
6873 "change in a future version of GCC", field);
6875 /* The middle end uses the type of expressions to determine the
6876 possible range of expression values. In order to optimize
6877 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6878 must be made aware of the width of "i", via its type.
6880 Because C++ does not have integer types of arbitrary width,
6881 we must (for the purposes of the front end) convert from the
6882 type assigned here to the declared type of the bitfield
6883 whenever a bitfield expression is used as an rvalue.
6884 Similarly, when assigning a value to a bitfield, the value
6885 must be converted to the type given the bitfield here. */
6886 if (DECL_C_BIT_FIELD (field))
6888 unsigned HOST_WIDE_INT width;
6889 tree ftype = TREE_TYPE (field);
6890 width = tree_to_uhwi (DECL_SIZE (field));
6891 if (width != TYPE_PRECISION (ftype))
6893 TREE_TYPE (field)
6894 = c_build_bitfield_integer_type (width,
6895 TYPE_UNSIGNED (ftype));
6896 TREE_TYPE (field)
6897 = cp_build_qualified_type (TREE_TYPE (field),
6898 cp_type_quals (ftype));
6902 /* If we needed additional padding after this field, add it
6903 now. */
6904 if (padding)
6906 tree padding_field;
6908 padding_field = build_decl (input_location,
6909 FIELD_DECL,
6910 NULL_TREE,
6911 char_type_node);
6912 DECL_BIT_FIELD (padding_field) = 1;
6913 DECL_SIZE (padding_field) = padding;
6914 DECL_CONTEXT (padding_field) = t;
6915 DECL_ARTIFICIAL (padding_field) = 1;
6916 DECL_IGNORED_P (padding_field) = 1;
6917 DECL_PADDING_P (padding_field) = 1;
6918 layout_nonempty_base_or_field (rli, padding_field,
6919 NULL_TREE,
6920 empty_base_offsets);
6923 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6926 if (!integer_zerop (rli->bitpos))
6928 /* Make sure that we are on a byte boundary so that the size of
6929 the class without virtual bases will always be a round number
6930 of bytes. */
6931 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6932 normalize_rli (rli);
6935 /* We used to remove zero width bitfields at this point since PR42217,
6936 while the C FE never did that. That caused ABI differences on various
6937 targets. Set the DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD flag on them
6938 instead, so that the backends can emit -Wpsabi warnings in the cases
6939 where the ABI changed. */
6940 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6942 if (TREE_CODE (field) == FIELD_DECL
6943 && DECL_C_BIT_FIELD (field)
6944 /* We should not be confused by the fact that grokbitfield
6945 temporarily sets the width of the bit field into
6946 DECL_BIT_FIELD_REPRESENTATIVE (field).
6947 check_bitfield_decl eventually sets DECL_SIZE (field)
6948 to that width. */
6949 && (DECL_SIZE (field) == NULL_TREE
6950 || integer_zerop (DECL_SIZE (field))))
6951 SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field, 1);
6952 check_non_pod_aggregate (field);
6955 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6957 /* T needs a different layout as a base (eliding virtual bases
6958 or whatever). Create that version. */
6959 tree base_t = make_node (TREE_CODE (t));
6960 tree base_d = create_implicit_typedef (as_base_identifier, base_t);
6962 TYPE_CONTEXT (base_t) = t;
6963 DECL_CONTEXT (base_d) = t;
6965 set_instantiating_module (base_d);
6967 /* If the ABI version is not at least two, and the last
6968 field was a bit-field, RLI may not be on a byte
6969 boundary. In particular, rli_size_unit_so_far might
6970 indicate the last complete byte, while rli_size_so_far
6971 indicates the total number of bits used. Therefore,
6972 rli_size_so_far, rather than rli_size_unit_so_far, is
6973 used to compute TYPE_SIZE_UNIT. */
6975 /* Set the size and alignment for the new type. */
6976 tree eoc = end_of_class (t, eoc_nvsize);
6977 TYPE_SIZE_UNIT (base_t)
6978 = size_binop (MAX_EXPR,
6979 fold_convert (sizetype,
6980 size_binop (CEIL_DIV_EXPR,
6981 rli_size_so_far (rli),
6982 bitsize_int (BITS_PER_UNIT))),
6983 eoc);
6984 TYPE_SIZE (base_t)
6985 = size_binop (MAX_EXPR,
6986 rli_size_so_far (rli),
6987 size_binop (MULT_EXPR,
6988 fold_convert (bitsizetype, eoc),
6989 bitsize_int (BITS_PER_UNIT)));
6990 SET_TYPE_ALIGN (base_t, rli->record_align);
6991 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6992 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
6993 TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t);
6995 /* Copy the non-static data members of T. This will include its
6996 direct non-virtual bases & vtable. */
6997 next_field = &TYPE_FIELDS (base_t);
6998 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6999 if (TREE_CODE (field) == FIELD_DECL)
7001 *next_field = copy_node (field);
7002 /* Zap any NSDMI, it's not needed and might be a deferred
7003 parse. */
7004 DECL_INITIAL (*next_field) = NULL_TREE;
7005 DECL_CONTEXT (*next_field) = base_t;
7006 next_field = &DECL_CHAIN (*next_field);
7008 *next_field = NULL_TREE;
7010 /* We use the base type for trivial assignments, and hence it
7011 needs a mode. */
7012 compute_record_mode (base_t);
7014 /* Record the base version of the type. */
7015 CLASSTYPE_AS_BASE (t) = base_t;
7017 else
7018 CLASSTYPE_AS_BASE (t) = t;
7020 /* Every empty class contains an empty class. */
7021 if (CLASSTYPE_EMPTY_P (t))
7022 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
7024 /* Set the TYPE_DECL for this type to contain the right
7025 value for DECL_OFFSET, so that we can use it as part
7026 of a COMPONENT_REF for multiple inheritance. */
7027 layout_decl (TYPE_MAIN_DECL (t), 0);
7029 /* Now fix up any virtual base class types that we left lying
7030 around. We must get these done before we try to lay out the
7031 virtual function table. As a side-effect, this will remove the
7032 base subobject fields. */
7033 layout_virtual_bases (rli, empty_base_offsets);
7035 /* Make sure that empty classes are reflected in RLI at this
7036 point. */
7037 include_empty_classes (rli);
7039 /* Make sure not to create any structures with zero size. */
7040 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
7041 place_field (rli,
7042 build_decl (input_location,
7043 FIELD_DECL, NULL_TREE, char_type_node));
7045 /* If this is a non-POD, declaring it packed makes a difference to how it
7046 can be used as a field; don't let finalize_record_size undo it. */
7047 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
7048 rli->packed_maybe_necessary = true;
7050 /* Let the back end lay out the type. */
7051 finish_record_layout (rli, /*free_p=*/true);
7053 /* If we didn't end up needing an as-base type, don't use it. */
7054 if (CLASSTYPE_AS_BASE (t) != t
7055 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
7056 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
7057 causing us to lose the user-specified alignment as in PR94050. */
7058 && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t))
7059 && tree_int_cst_equal (TYPE_SIZE (t),
7060 TYPE_SIZE (CLASSTYPE_AS_BASE (t))))
7061 CLASSTYPE_AS_BASE (t) = t;
7063 if (TYPE_SIZE_UNIT (t)
7064 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7065 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7066 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7067 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
7069 /* Warn about bases that can't be talked about due to ambiguity. */
7070 maybe_warn_about_inaccessible_bases (t);
7072 /* Now that we're done with layout, give the base fields the real types. */
7073 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7074 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
7075 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
7077 /* Clean up. */
7078 splay_tree_delete (empty_base_offsets);
7080 if (CLASSTYPE_EMPTY_P (t)
7081 && tree_int_cst_lt (sizeof_biggest_empty_class,
7082 TYPE_SIZE_UNIT (t)))
7083 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
7086 /* Determine the "key method" for the class type indicated by TYPE,
7087 and set CLASSTYPE_KEY_METHOD accordingly. */
7089 void
7090 determine_key_method (tree type)
7092 tree method;
7094 if (processing_template_decl
7095 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
7096 || CLASSTYPE_INTERFACE_KNOWN (type))
7097 return;
7099 /* The key method is the first non-pure virtual function that is not
7100 inline at the point of class definition. On some targets the
7101 key function may not be inline; those targets should not call
7102 this function until the end of the translation unit. */
7103 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
7104 if (TREE_CODE (method) == FUNCTION_DECL
7105 && DECL_VINDEX (method) != NULL_TREE
7106 && ! DECL_DECLARED_INLINE_P (method)
7107 && ! DECL_PURE_VIRTUAL_P (method))
7109 CLASSTYPE_KEY_METHOD (type) = method;
7110 break;
7113 return;
7116 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
7117 class data member of non-zero size, otherwise false. */
7119 static inline bool
7120 field_nonempty_p (const_tree fld)
7122 if (TREE_CODE (fld) == ERROR_MARK)
7123 return false;
7125 tree type = TREE_TYPE (fld);
7126 if (TREE_CODE (fld) == FIELD_DECL
7127 && TREE_CODE (type) != ERROR_MARK
7128 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
7130 return TYPE_SIZE (type)
7131 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
7132 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
7135 return false;
7138 /* Used by find_flexarrays and related functions. */
7140 struct flexmems_t
7142 /* The first flexible array member or non-zero array member found
7143 in the order of layout. */
7144 tree array;
7145 /* First non-static non-empty data member in the class or its bases. */
7146 tree first;
7147 /* The first non-static non-empty data member following either
7148 the flexible array member, if found, or the zero-length array member
7149 otherwise. AFTER[1] refers to the first such data member of a union
7150 of which the struct containing the flexible array member or zero-length
7151 array is a member, or NULL when no such union exists. This element is
7152 only used during searching, not for diagnosing problems. AFTER[0]
7153 refers to the first such data member that is not a member of such
7154 a union. */
7155 tree after[2];
7157 /* Refers to a struct (not union) in which the struct of which the flexible
7158 array is member is defined. Used to diagnose strictly (according to C)
7159 invalid uses of the latter structs. */
7160 tree enclosing;
7163 /* Find either the first flexible array member or the first zero-length
7164 array, in that order of preference, among members of class T (but not
7165 its base classes), and set members of FMEM accordingly.
7166 BASE_P is true if T is a base class of another class.
7167 PUN is set to the outermost union in which the flexible array member
7168 (or zero-length array) is defined if one such union exists, otherwise
7169 to NULL.
7170 Similarly, PSTR is set to a data member of the outermost struct of
7171 which the flexible array is a member if one such struct exists,
7172 otherwise to NULL. */
7174 static void
7175 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
7176 tree pun /* = NULL_TREE */,
7177 tree pstr /* = NULL_TREE */)
7179 /* Set the "pointer" to the outermost enclosing union if not set
7180 yet and maintain it for the remainder of the recursion. */
7181 if (!pun && TREE_CODE (t) == UNION_TYPE)
7182 pun = t;
7184 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
7186 if (fld == error_mark_node)
7187 return;
7189 /* Is FLD a typedef for an anonymous struct? */
7191 /* FIXME: Note that typedefs (as well as arrays) need to be fully
7192 handled elsewhere so that errors like the following are detected
7193 as well:
7194 typedef struct { int i, a[], j; } S; // bug c++/72753
7195 S s [2]; // bug c++/68489
7197 if (TREE_CODE (fld) == TYPE_DECL
7198 && DECL_IMPLICIT_TYPEDEF_P (fld)
7199 && CLASS_TYPE_P (TREE_TYPE (fld))
7200 && IDENTIFIER_ANON_P (DECL_NAME (fld)))
7202 /* Check the nested unnamed type referenced via a typedef
7203 independently of FMEM (since it's not a data member of
7204 the enclosing class). */
7205 check_flexarrays (TREE_TYPE (fld));
7206 continue;
7209 /* Skip anything that's GCC-generated or not a (non-static) data
7210 member. */
7211 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
7212 continue;
7214 /* Type of the member. */
7215 tree fldtype = TREE_TYPE (fld);
7216 if (fldtype == error_mark_node)
7217 return;
7219 /* Determine the type of the array element or object referenced
7220 by the member so that it can be checked for flexible array
7221 members if it hasn't been yet. */
7222 tree eltype = fldtype;
7223 while (TREE_CODE (eltype) == ARRAY_TYPE
7224 || INDIRECT_TYPE_P (eltype))
7225 eltype = TREE_TYPE (eltype);
7227 if (RECORD_OR_UNION_TYPE_P (eltype))
7229 if (fmem->array && !fmem->after[bool (pun)])
7231 /* Once the member after the flexible array has been found
7232 we're done. */
7233 fmem->after[bool (pun)] = fld;
7234 break;
7237 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
7239 /* Descend into the non-static member struct or union and try
7240 to find a flexible array member or zero-length array among
7241 its members. This is only necessary for anonymous types
7242 and types in whose context the current type T has not been
7243 defined (the latter must not be checked again because they
7244 are already in the process of being checked by one of the
7245 recursive calls). */
7247 tree first = fmem->first;
7248 tree array = fmem->array;
7250 /* If this member isn't anonymous and a prior non-flexible array
7251 member has been seen in one of the enclosing structs, clear
7252 the FIRST member since it doesn't contribute to the flexible
7253 array struct's members. */
7254 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7255 fmem->first = NULL_TREE;
7257 find_flexarrays (eltype, fmem, false, pun,
7258 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
7260 if (fmem->array != array)
7261 continue;
7263 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7265 /* Restore the FIRST member reset above if no flexible
7266 array member has been found in this member's struct. */
7267 fmem->first = first;
7270 /* If the member struct contains the first flexible array
7271 member, or if this member is a base class, continue to
7272 the next member and avoid setting the FMEM->NEXT pointer
7273 to point to it. */
7274 if (base_p)
7275 continue;
7279 if (field_nonempty_p (fld))
7281 /* Remember the first non-static data member. */
7282 if (!fmem->first)
7283 fmem->first = fld;
7285 /* Remember the first non-static data member after the flexible
7286 array member, if one has been found, or the zero-length array
7287 if it has been found. */
7288 if (fmem->array && !fmem->after[bool (pun)])
7289 fmem->after[bool (pun)] = fld;
7292 /* Skip non-arrays. */
7293 if (TREE_CODE (fldtype) != ARRAY_TYPE)
7294 continue;
7296 /* Determine the upper bound of the array if it has one. */
7297 if (TYPE_DOMAIN (fldtype))
7299 if (fmem->array)
7301 /* Make a record of the zero-length array if either one
7302 such field or a flexible array member has been seen to
7303 handle the pathological and unlikely case of multiple
7304 such members. */
7305 if (!fmem->after[bool (pun)])
7306 fmem->after[bool (pun)] = fld;
7308 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7310 /* Remember the first zero-length array unless a flexible array
7311 member has already been seen. */
7312 fmem->array = fld;
7313 fmem->enclosing = pstr;
7316 else
7318 /* Flexible array members have no upper bound. */
7319 if (fmem->array)
7321 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7323 /* Replace the zero-length array if it's been stored and
7324 reset the after pointer. */
7325 fmem->after[bool (pun)] = NULL_TREE;
7326 fmem->array = fld;
7327 fmem->enclosing = pstr;
7329 else if (!fmem->after[bool (pun)])
7330 /* Make a record of another flexible array member. */
7331 fmem->after[bool (pun)] = fld;
7333 else
7335 fmem->array = fld;
7336 fmem->enclosing = pstr;
7342 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7343 a flexible array member (or the zero-length array extension). */
7345 static void
7346 diagnose_invalid_flexarray (const flexmems_t *fmem)
7348 if (fmem->array && fmem->enclosing)
7350 auto_diagnostic_group d;
7351 if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
7352 TYPE_DOMAIN (TREE_TYPE (fmem->array))
7353 ? G_("invalid use of %q#T with a zero-size array "
7354 "in %q#D")
7355 : G_("invalid use of %q#T with a flexible array member "
7356 "in %q#T"),
7357 DECL_CONTEXT (fmem->array),
7358 DECL_CONTEXT (fmem->enclosing)))
7359 inform (DECL_SOURCE_LOCATION (fmem->array),
7360 "array member %q#D declared here", fmem->array);
7364 /* Issue diagnostics for invalid flexible array members or zero-length
7365 arrays that are not the last elements of the containing class or its
7366 base classes or that are its sole members. */
7368 static void
7369 diagnose_flexarrays (tree t, const flexmems_t *fmem)
7371 if (!fmem->array)
7372 return;
7374 if (fmem->first && !fmem->after[0])
7376 diagnose_invalid_flexarray (fmem);
7377 return;
7380 /* Has a diagnostic been issued? */
7381 bool diagd = false;
7383 const char *msg = 0;
7385 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7387 if (fmem->after[0])
7388 msg = G_("zero-size array member %qD not at end of %q#T");
7389 else if (!fmem->first)
7390 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7392 if (msg)
7394 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7396 auto_diagnostic_group d;
7397 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7399 inform (location_of (t), "in the definition of %q#T", t);
7400 diagd = true;
7404 else
7406 if (fmem->after[0])
7407 msg = G_("flexible array member %qD not at end of %q#T");
7408 else if (!fmem->first)
7409 msg = G_("flexible array member %qD in an otherwise empty %q#T");
7411 if (msg)
7413 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7414 diagd = true;
7416 auto_diagnostic_group d;
7417 error_at (loc, msg, fmem->array, t);
7419 /* In the unlikely event that the member following the flexible
7420 array member is declared in a different class, or the member
7421 overlaps another member of a common union, point to it.
7422 Otherwise it should be obvious. */
7423 if (fmem->after[0]
7424 && ((DECL_CONTEXT (fmem->after[0])
7425 != DECL_CONTEXT (fmem->array))))
7427 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7428 "next member %q#D declared here",
7429 fmem->after[0]);
7430 inform (location_of (t), "in the definition of %q#T", t);
7435 if (!diagd && fmem->array && fmem->enclosing)
7436 diagnose_invalid_flexarray (fmem);
7440 /* Recursively check to make sure that any flexible array or zero-length
7441 array members of class T or its bases are valid (i.e., not the sole
7442 non-static data member of T and, if one exists, that it is the last
7443 non-static data member of T and its base classes. FMEM is expected
7444 to be initially null and is used internally by recursive calls to
7445 the function. Issue the appropriate diagnostics for the array member
7446 that fails the checks. */
7448 static void
7449 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7450 bool base_p /* = false */)
7452 /* Initialize the result of a search for flexible array and zero-length
7453 array members. Avoid doing any work if the most interesting FMEM data
7454 have already been populated. */
7455 flexmems_t flexmems = flexmems_t ();
7456 if (!fmem)
7457 fmem = &flexmems;
7458 else if (fmem->array && fmem->first && fmem->after[0])
7459 return;
7461 tree fam = fmem->array;
7463 /* Recursively check the primary base class first. */
7464 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7466 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7467 check_flexarrays (basetype, fmem, true);
7470 /* Recursively check the base classes. */
7471 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7472 for (int i = 0; i < nbases; ++i)
7474 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7476 /* The primary base class was already checked above. */
7477 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7478 continue;
7480 /* Virtual base classes are at the end. */
7481 if (BINFO_VIRTUAL_P (base_binfo))
7482 continue;
7484 /* Check the base class. */
7485 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7488 if (fmem == &flexmems)
7490 /* Check virtual base classes only once per derived class.
7491 I.e., this check is not performed recursively for base
7492 classes. */
7493 int i;
7494 tree base_binfo;
7495 vec<tree, va_gc> *vbases;
7496 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7497 vec_safe_iterate (vbases, i, &base_binfo); i++)
7499 /* Check the virtual base class. */
7500 tree basetype = TREE_TYPE (base_binfo);
7502 check_flexarrays (basetype, fmem, /*base_p=*/true);
7506 /* Is the type unnamed (and therefore a member of it potentially
7507 an anonymous struct or union)? */
7508 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7509 if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE)
7510 maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7512 /* Search the members of the current (possibly derived) class, skipping
7513 unnamed structs and unions since those could be anonymous. */
7514 if (fmem != &flexmems || !maybe_anon_p)
7515 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7517 if (fmem == &flexmems && !maybe_anon_p)
7519 /* Issue diagnostics for invalid flexible and zero-length array
7520 members found in base classes or among the members of the current
7521 class. Ignore anonymous structs and unions whose members are
7522 considered to be members of the enclosing class and thus will
7523 be diagnosed when checking it. */
7524 diagnose_flexarrays (t, fmem);
7528 /* Perform processing required when the definition of T (a class type)
7529 is complete. Diagnose invalid definitions of flexible array members
7530 and zero-size arrays. */
7532 void
7533 finish_struct_1 (tree t)
7535 tree x;
7536 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7537 tree virtuals = NULL_TREE;
7539 if (COMPLETE_TYPE_P (t))
7541 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7542 error ("redefinition of %q#T", t);
7543 popclass ();
7544 return;
7547 /* If this type was previously laid out as a forward reference,
7548 make sure we lay it out again. */
7549 TYPE_SIZE (t) = NULL_TREE;
7550 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7552 /* Make assumptions about the class; we'll reset the flags if
7553 necessary. */
7554 CLASSTYPE_EMPTY_P (t) = 1;
7555 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7556 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7557 CLASSTYPE_LITERAL_P (t) = true;
7559 /* Do end-of-class semantic processing: checking the validity of the
7560 bases and members and add implicitly generated methods. */
7561 check_bases_and_members (t);
7563 /* Find the key method. */
7564 if (TYPE_CONTAINS_VPTR_P (t))
7566 /* The Itanium C++ ABI permits the key method to be chosen when
7567 the class is defined -- even though the key method so
7568 selected may later turn out to be an inline function. On
7569 some systems (such as ARM Symbian OS) the key method cannot
7570 be determined until the end of the translation unit. On such
7571 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7572 will cause the class to be added to KEYED_CLASSES. Then, in
7573 finish_file we will determine the key method. */
7574 if (targetm.cxx.key_method_may_be_inline ())
7575 determine_key_method (t);
7577 /* If a polymorphic class has no key method, we may emit the vtable
7578 in every translation unit where the class definition appears. If
7579 we're devirtualizing, we can look into the vtable even if we
7580 aren't emitting it. */
7581 if (!CLASSTYPE_KEY_METHOD (t))
7582 vec_safe_push (keyed_classes, t);
7585 /* Layout the class itself. */
7586 layout_class_type (t, &virtuals);
7587 /* COMPLETE_TYPE_P is now true. */
7589 set_class_bindings (t);
7591 /* With the layout complete, check for flexible array members and
7592 zero-length arrays that might overlap other members in the final
7593 layout. */
7594 check_flexarrays (t);
7596 virtuals = modify_all_vtables (t, nreverse (virtuals));
7598 /* If necessary, create the primary vtable for this class. */
7599 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7601 /* We must enter these virtuals into the table. */
7602 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7603 build_primary_vtable (NULL_TREE, t);
7604 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7605 /* Here we know enough to change the type of our virtual
7606 function table, but we will wait until later this function. */
7607 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7609 /* If we're warning about ABI tags, check the types of the new
7610 virtual functions. */
7611 if (warn_abi_tag)
7612 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7613 check_abi_tags (t, TREE_VALUE (v));
7616 if (TYPE_CONTAINS_VPTR_P (t))
7618 int vindex;
7619 tree fn;
7621 if (BINFO_VTABLE (TYPE_BINFO (t)))
7622 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7623 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7624 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7626 /* Add entries for virtual functions introduced by this class. */
7627 BINFO_VIRTUALS (TYPE_BINFO (t))
7628 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7630 /* Set DECL_VINDEX for all functions declared in this class. */
7631 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7633 fn = TREE_CHAIN (fn),
7634 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7635 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7637 tree fndecl = BV_FN (fn);
7639 if (DECL_THUNK_P (fndecl))
7640 /* A thunk. We should never be calling this entry directly
7641 from this vtable -- we'd use the entry for the non
7642 thunk base function. */
7643 DECL_VINDEX (fndecl) = NULL_TREE;
7644 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7645 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7649 finish_struct_bits (t);
7651 set_method_tm_attributes (t);
7652 if (flag_openmp || flag_openmp_simd)
7653 finish_omp_declare_simd_methods (t);
7655 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7656 for any static member objects of the type we're working on. */
7657 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7658 if (DECL_DECLARES_FUNCTION_P (x))
7660 /* Synthesize constexpr defaulted comparisons. */
7661 if (!DECL_ARTIFICIAL (x)
7662 && DECL_DEFAULTED_IN_CLASS_P (x)
7663 && special_function_p (x) == sfk_comparison)
7664 defaulted_late_check (x);
7665 DECL_IN_AGGR_P (x) = false;
7667 else if (VAR_P (x) && TREE_STATIC (x)
7668 && TREE_TYPE (x) != error_mark_node
7669 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7670 SET_DECL_MODE (x, TYPE_MODE (t));
7672 /* Complain if one of the field types requires lower visibility. */
7673 constrain_class_visibility (t);
7675 /* Make the rtl for any new vtables we have created, and unmark
7676 the base types we marked. */
7677 finish_vtbls (t);
7679 /* Build the VTT for T. */
7680 build_vtt (t);
7682 if (warn_nonvdtor
7683 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7684 && !CLASSTYPE_FINAL (t))
7685 warning (OPT_Wnon_virtual_dtor,
7686 "%q#T has virtual functions and accessible"
7687 " non-virtual destructor", t);
7689 complete_vars (t);
7691 if (warn_overloaded_virtual)
7692 warn_hidden (t);
7694 /* Class layout, assignment of virtual table slots, etc., is now
7695 complete. Give the back end a chance to tweak the visibility of
7696 the class or perform any other required target modifications. */
7697 targetm.cxx.adjust_class_at_definition (t);
7699 maybe_suppress_debug_info (t);
7701 if (flag_vtable_verify)
7702 vtv_save_class_info (t);
7704 dump_class_hierarchy (t);
7706 /* Finish debugging output for this type. */
7707 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7709 if (TYPE_TRANSPARENT_AGGR (t))
7711 tree field = first_field (t);
7712 if (field == NULL_TREE || error_operand_p (field))
7714 error ("type transparent %q#T does not have any fields", t);
7715 TYPE_TRANSPARENT_AGGR (t) = 0;
7717 else if (DECL_ARTIFICIAL (field))
7719 if (DECL_FIELD_IS_BASE (field))
7720 error ("type transparent class %qT has base classes", t);
7721 else
7723 gcc_checking_assert (DECL_VIRTUAL_P (field));
7724 error ("type transparent class %qT has virtual functions", t);
7726 TYPE_TRANSPARENT_AGGR (t) = 0;
7728 else if (TYPE_MODE (t) != DECL_MODE (field))
7730 error ("type transparent %q#T cannot be made transparent because "
7731 "the type of the first field has a different ABI from the "
7732 "class overall", t);
7733 TYPE_TRANSPARENT_AGGR (t) = 0;
7738 /* When T was built up, the member declarations were added in reverse
7739 order. Rearrange them to declaration order. */
7741 void
7742 unreverse_member_declarations (tree t)
7744 tree next;
7745 tree prev;
7746 tree x;
7748 /* The following lists are all in reverse order. Put them in
7749 declaration order now. */
7750 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7752 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7753 order, so we can't just use nreverse. Due to stat_hack
7754 chicanery in finish_member_declaration. */
7755 prev = NULL_TREE;
7756 for (x = TYPE_FIELDS (t);
7757 x && TREE_CODE (x) != TYPE_DECL;
7758 x = next)
7760 next = DECL_CHAIN (x);
7761 DECL_CHAIN (x) = prev;
7762 prev = x;
7765 if (prev)
7767 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7768 TYPE_FIELDS (t) = prev;
7772 tree
7773 finish_struct (tree t, tree attributes)
7775 location_t saved_loc = input_location;
7777 /* Now that we've got all the field declarations, reverse everything
7778 as necessary. */
7779 unreverse_member_declarations (t);
7781 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7782 fixup_attribute_variants (t);
7784 /* Nadger the current location so that diagnostics point to the start of
7785 the struct, not the end. */
7786 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7788 if (processing_template_decl)
7790 tree x;
7792 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7793 if (DECL_DECLARES_FUNCTION_P (x))
7795 DECL_IN_AGGR_P (x) = false;
7796 if (DECL_VIRTUAL_P (x))
7797 CLASSTYPE_NON_AGGREGATE (t) = true;
7799 else if (TREE_CODE (x) == FIELD_DECL)
7801 if (TREE_PROTECTED (x) || TREE_PRIVATE (x))
7802 CLASSTYPE_NON_AGGREGATE (t) = true;
7805 /* Also add a USING_DECL for operator=. We know there'll be (at
7806 least) one, but we don't know the signature(s). We want name
7807 lookup not to fail or recurse into bases. This isn't added
7808 to the template decl list so we drop this at instantiation
7809 time. */
7810 tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
7811 NULL_TREE);
7812 DECL_CONTEXT (ass_op) = t;
7813 USING_DECL_SCOPE (ass_op) = t;
7814 DECL_DEPENDENT_P (ass_op) = true;
7815 DECL_ARTIFICIAL (ass_op) = true;
7816 DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
7817 TYPE_FIELDS (t) = ass_op;
7819 TYPE_SIZE (t) = bitsize_zero_node;
7820 TYPE_SIZE_UNIT (t) = size_zero_node;
7821 /* COMPLETE_TYPE_P is now true. */
7823 set_class_bindings (t);
7825 /* We need to emit an error message if this type was used as a parameter
7826 and it is an abstract type, even if it is a template. We construct
7827 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7828 account and we call complete_vars with this type, which will check
7829 the PARM_DECLS. Note that while the type is being defined,
7830 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7831 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7832 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7833 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7834 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7835 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7836 complete_vars (t);
7838 /* Remember current #pragma pack value. */
7839 TYPE_PRECISION (t) = maximum_field_alignment;
7841 if (cxx_dialect < cxx20)
7843 if (!CLASSTYPE_NON_AGGREGATE (t)
7844 && type_has_user_provided_or_explicit_constructor (t))
7845 CLASSTYPE_NON_AGGREGATE (t) = 1;
7847 else if (TYPE_HAS_USER_CONSTRUCTOR (t))
7848 CLASSTYPE_NON_AGGREGATE (t) = 1;
7850 /* Fix up any variants we've already built. */
7851 fixup_type_variants (t);
7853 else
7854 finish_struct_1 (t);
7855 /* COMPLETE_TYPE_P is now true. */
7857 maybe_warn_about_overly_private_class (t);
7859 if (is_std_init_list (t))
7861 /* People keep complaining that the compiler crashes on an invalid
7862 definition of initializer_list, so I guess we should explicitly
7863 reject it. What the compiler internals care about is that it's a
7864 template and has a pointer field followed by size_type field. */
7865 bool ok = false;
7866 if (processing_template_decl)
7868 tree f = next_aggregate_field (TYPE_FIELDS (t));
7869 if (f && TYPE_PTR_P (TREE_TYPE (f)))
7871 f = next_aggregate_field (DECL_CHAIN (f));
7872 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7873 ok = true;
7876 /* It also cannot be a union. */
7877 ok &= NON_UNION_CLASS_TYPE_P (t);
7878 if (!ok)
7879 fatal_error (input_location, "definition of %qD does not match "
7880 "%<#include <initializer_list>%>", TYPE_NAME (t));
7883 input_location = saved_loc;
7885 TYPE_BEING_DEFINED (t) = 0;
7887 if (current_class_type)
7888 popclass ();
7889 else
7890 error ("trying to finish struct, but kicked out due to previous parse errors");
7892 if (flag_openmp)
7893 for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl))
7894 if (TREE_CODE (decl) == FUNCTION_DECL
7895 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
7896 if (tree attr = lookup_attribute ("omp declare variant base",
7897 DECL_ATTRIBUTES (decl)))
7898 omp_declare_variant_finalize (decl, attr);
7900 if (processing_template_decl && at_function_scope_p ()
7901 /* Lambdas are defined by the LAMBDA_EXPR. */
7902 && !LAMBDA_TYPE_P (t))
7903 add_stmt (build_min (TAG_DEFN, t));
7905 return t;
7908 /* Hash table to avoid endless recursion when handling references. */
7909 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7911 /* Return the dynamic type of INSTANCE, if known.
7912 Used to determine whether the virtual function table is needed
7913 or not.
7915 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7916 of our knowledge of its type. *NONNULL should be initialized
7917 before this function is called. */
7919 static tree
7920 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7922 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7924 switch (TREE_CODE (instance))
7926 case INDIRECT_REF:
7927 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
7928 return NULL_TREE;
7929 else
7930 return RECUR (TREE_OPERAND (instance, 0));
7932 case CALL_EXPR:
7933 /* This is a call to a constructor, hence it's never zero. */
7934 if (CALL_EXPR_FN (instance)
7935 && TREE_HAS_CONSTRUCTOR (instance))
7937 if (nonnull)
7938 *nonnull = 1;
7939 return TREE_TYPE (instance);
7941 return NULL_TREE;
7943 case SAVE_EXPR:
7944 /* This is a call to a constructor, hence it's never zero. */
7945 if (TREE_HAS_CONSTRUCTOR (instance))
7947 if (nonnull)
7948 *nonnull = 1;
7949 return TREE_TYPE (instance);
7951 return RECUR (TREE_OPERAND (instance, 0));
7953 case POINTER_PLUS_EXPR:
7954 case PLUS_EXPR:
7955 case MINUS_EXPR:
7956 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7957 return RECUR (TREE_OPERAND (instance, 0));
7958 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7959 /* Propagate nonnull. */
7960 return RECUR (TREE_OPERAND (instance, 0));
7962 return NULL_TREE;
7964 CASE_CONVERT:
7965 return RECUR (TREE_OPERAND (instance, 0));
7967 case ADDR_EXPR:
7968 instance = TREE_OPERAND (instance, 0);
7969 if (nonnull)
7971 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7972 with a real object -- given &p->f, p can still be null. */
7973 tree t = get_base_address (instance);
7974 /* ??? Probably should check DECL_WEAK here. */
7975 if (t && DECL_P (t))
7976 *nonnull = 1;
7978 return RECUR (instance);
7980 case COMPONENT_REF:
7981 /* If this component is really a base class reference, then the field
7982 itself isn't definitive. */
7983 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7984 return RECUR (TREE_OPERAND (instance, 0));
7985 return RECUR (TREE_OPERAND (instance, 1));
7987 case VAR_DECL:
7988 case FIELD_DECL:
7989 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7990 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7992 if (nonnull)
7993 *nonnull = 1;
7994 return TREE_TYPE (TREE_TYPE (instance));
7996 /* fall through. */
7997 case TARGET_EXPR:
7998 case PARM_DECL:
7999 case RESULT_DECL:
8000 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
8002 if (nonnull)
8003 *nonnull = 1;
8004 return TREE_TYPE (instance);
8006 else if (instance == current_class_ptr)
8008 if (nonnull)
8009 *nonnull = 1;
8011 /* if we're in a ctor or dtor, we know our type. If
8012 current_class_ptr is set but we aren't in a function, we're in
8013 an NSDMI (and therefore a constructor). */
8014 if (current_scope () != current_function_decl
8015 || (DECL_LANG_SPECIFIC (current_function_decl)
8016 && (DECL_CONSTRUCTOR_P (current_function_decl)
8017 || DECL_DESTRUCTOR_P (current_function_decl))))
8019 if (cdtorp)
8020 *cdtorp = 1;
8021 return TREE_TYPE (TREE_TYPE (instance));
8024 else if (TYPE_REF_P (TREE_TYPE (instance)))
8026 /* We only need one hash table because it is always left empty. */
8027 if (!fixed_type_or_null_ref_ht)
8028 fixed_type_or_null_ref_ht
8029 = new hash_table<nofree_ptr_hash<tree_node> > (37);
8031 /* Reference variables should be references to objects. */
8032 if (nonnull)
8033 *nonnull = 1;
8035 /* Enter the INSTANCE in a table to prevent recursion; a
8036 variable's initializer may refer to the variable
8037 itself. */
8038 if (VAR_P (instance)
8039 && DECL_INITIAL (instance)
8040 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
8041 && !fixed_type_or_null_ref_ht->find (instance))
8043 tree type;
8044 tree_node **slot;
8046 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
8047 *slot = instance;
8048 type = RECUR (DECL_INITIAL (instance));
8049 fixed_type_or_null_ref_ht->remove_elt (instance);
8051 return type;
8054 return NULL_TREE;
8056 case VIEW_CONVERT_EXPR:
8057 if (location_wrapper_p (instance))
8058 return RECUR (TREE_OPERAND (instance, 0));
8059 else
8060 /* TODO: Recursion may be correct for some non-location-wrapper
8061 uses of VIEW_CONVERT_EXPR. */
8062 return NULL_TREE;
8064 default:
8065 return NULL_TREE;
8067 #undef RECUR
8070 /* Return nonzero if the dynamic type of INSTANCE is known, and
8071 equivalent to the static type. We also handle the case where
8072 INSTANCE is really a pointer. Return negative if this is a
8073 ctor/dtor. There the dynamic type is known, but this might not be
8074 the most derived base of the original object, and hence virtual
8075 bases may not be laid out according to this type.
8077 Used to determine whether the virtual function table is needed
8078 or not.
8080 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8081 of our knowledge of its type. *NONNULL should be initialized
8082 before this function is called. */
8085 resolves_to_fixed_type_p (tree instance, int* nonnull)
8087 tree t = TREE_TYPE (instance);
8088 int cdtorp = 0;
8089 tree fixed;
8091 /* processing_template_decl can be false in a template if we're in
8092 instantiate_non_dependent_expr, but we still want to suppress
8093 this check. */
8094 if (in_template_context)
8096 /* In a template we only care about the type of the result. */
8097 if (nonnull)
8098 *nonnull = true;
8099 return true;
8102 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
8103 if (INDIRECT_TYPE_P (t))
8104 t = TREE_TYPE (t);
8105 if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t))
8106 return 1;
8107 if (fixed == NULL_TREE)
8108 return 0;
8109 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
8110 return 0;
8111 return cdtorp ? -1 : 1;
8115 void
8116 init_class_processing (void)
8118 current_class_depth = 0;
8119 current_class_stack_size = 10;
8120 current_class_stack
8121 = XNEWVEC (struct class_stack_node, current_class_stack_size);
8122 sizeof_biggest_empty_class = size_zero_node;
8124 ridpointers[(int) RID_PUBLIC] = access_public_node;
8125 ridpointers[(int) RID_PRIVATE] = access_private_node;
8126 ridpointers[(int) RID_PROTECTED] = access_protected_node;
8129 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
8131 static void
8132 restore_class_cache (void)
8134 tree type;
8136 /* We are re-entering the same class we just left, so we don't
8137 have to search the whole inheritance matrix to find all the
8138 decls to bind again. Instead, we install the cached
8139 class_shadowed list and walk through it binding names. */
8140 push_binding_level (previous_class_level);
8141 class_binding_level = previous_class_level;
8142 /* Restore IDENTIFIER_TYPE_VALUE. */
8143 for (type = class_binding_level->type_shadowed;
8144 type;
8145 type = TREE_CHAIN (type))
8146 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
8149 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
8150 appropriate for TYPE.
8152 So that we may avoid calls to lookup_name, we cache the _TYPE
8153 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
8155 For multiple inheritance, we perform a two-pass depth-first search
8156 of the type lattice. */
8158 void
8159 pushclass (tree type)
8161 class_stack_node_t csn;
8163 type = TYPE_MAIN_VARIANT (type);
8165 /* Make sure there is enough room for the new entry on the stack. */
8166 if (current_class_depth + 1 >= current_class_stack_size)
8168 current_class_stack_size *= 2;
8169 current_class_stack
8170 = XRESIZEVEC (struct class_stack_node, current_class_stack,
8171 current_class_stack_size);
8174 /* Insert a new entry on the class stack. */
8175 csn = current_class_stack + current_class_depth;
8176 csn->name = current_class_name;
8177 csn->type = current_class_type;
8178 csn->access = current_access_specifier;
8179 csn->names_used = 0;
8180 csn->hidden = 0;
8181 current_class_depth++;
8183 /* Now set up the new type. */
8184 current_class_name = TYPE_NAME (type);
8185 if (TREE_CODE (current_class_name) == TYPE_DECL)
8186 current_class_name = DECL_NAME (current_class_name);
8187 current_class_type = type;
8189 /* By default, things in classes are private, while things in
8190 structures or unions are public. */
8191 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
8192 ? access_private_node
8193 : access_public_node);
8195 if (previous_class_level
8196 && type != previous_class_level->this_entity
8197 && current_class_depth == 1)
8199 /* Forcibly remove any old class remnants. */
8200 invalidate_class_lookup_cache ();
8203 if (!previous_class_level
8204 || type != previous_class_level->this_entity
8205 || current_class_depth > 1)
8206 pushlevel_class ();
8207 else
8208 restore_class_cache ();
8211 /* Get out of the current class scope. If we were in a class scope
8212 previously, that is the one popped to. */
8214 void
8215 popclass (void)
8217 poplevel_class ();
8219 current_class_depth--;
8220 current_class_name = current_class_stack[current_class_depth].name;
8221 current_class_type = current_class_stack[current_class_depth].type;
8222 current_access_specifier = current_class_stack[current_class_depth].access;
8223 if (current_class_stack[current_class_depth].names_used)
8224 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8227 /* Mark the top of the class stack as hidden. */
8229 void
8230 push_class_stack (void)
8232 if (current_class_depth)
8233 ++current_class_stack[current_class_depth - 1].hidden;
8236 /* Mark the top of the class stack as un-hidden. */
8238 void
8239 pop_class_stack (void)
8241 if (current_class_depth)
8242 --current_class_stack[current_class_depth - 1].hidden;
8245 /* If the class type currently being defined is either T or
8246 a nested type of T, returns the type from the current_class_stack,
8247 which might be equivalent to but not equal to T in case of
8248 constrained partial specializations. */
8250 tree
8251 currently_open_class (tree t)
8253 int i;
8255 if (!CLASS_TYPE_P (t))
8256 return NULL_TREE;
8258 t = TYPE_MAIN_VARIANT (t);
8260 /* We start looking from 1 because entry 0 is from global scope,
8261 and has no type. */
8262 for (i = current_class_depth; i > 0; --i)
8264 tree c;
8265 if (i == current_class_depth)
8266 c = current_class_type;
8267 else
8269 if (current_class_stack[i].hidden)
8270 break;
8271 c = current_class_stack[i].type;
8273 if (!c)
8274 continue;
8275 if (same_type_p (c, t))
8276 return c;
8278 return NULL_TREE;
8281 /* If either current_class_type or one of its enclosing classes are derived
8282 from T, return the appropriate type. Used to determine how we found
8283 something via unqualified lookup. */
8285 tree
8286 currently_open_derived_class (tree t)
8288 int i;
8290 /* The bases of a dependent type are unknown. */
8291 if (dependent_type_p (t))
8292 return NULL_TREE;
8294 if (!current_class_type)
8295 return NULL_TREE;
8297 if (DERIVED_FROM_P (t, current_class_type))
8298 return current_class_type;
8300 for (i = current_class_depth - 1; i > 0; --i)
8302 if (current_class_stack[i].hidden)
8303 break;
8304 if (DERIVED_FROM_P (t, current_class_stack[i].type))
8305 return current_class_stack[i].type;
8308 return NULL_TREE;
8311 /* Return the outermost enclosing class type that is still open, or
8312 NULL_TREE. */
8314 tree
8315 outermost_open_class (void)
8317 if (!current_class_type)
8318 return NULL_TREE;
8319 tree r = NULL_TREE;
8320 if (TYPE_BEING_DEFINED (current_class_type))
8321 r = current_class_type;
8322 for (int i = current_class_depth - 1; i > 0; --i)
8324 if (current_class_stack[i].hidden)
8325 break;
8326 tree t = current_class_stack[i].type;
8327 if (!TYPE_BEING_DEFINED (t))
8328 break;
8329 r = t;
8331 return r;
8334 /* Returns the innermost class type which is not a lambda closure type. */
8336 tree
8337 current_nonlambda_class_type (void)
8339 tree type = current_class_type;
8340 while (type && LAMBDA_TYPE_P (type))
8341 type = decl_type_context (TYPE_NAME (type));
8342 return type;
8345 /* When entering a class scope, all enclosing class scopes' names with
8346 static meaning (static variables, static functions, types and
8347 enumerators) have to be visible. This recursive function calls
8348 pushclass for all enclosing class contexts until global or a local
8349 scope is reached. TYPE is the enclosed class. */
8351 void
8352 push_nested_class (tree type)
8354 /* A namespace might be passed in error cases, like A::B:C. */
8355 if (type == NULL_TREE
8356 || !CLASS_TYPE_P (type))
8357 return;
8359 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8361 pushclass (type);
8364 /* Undoes a push_nested_class call. */
8366 void
8367 pop_nested_class (void)
8369 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8371 popclass ();
8372 if (context && CLASS_TYPE_P (context))
8373 pop_nested_class ();
8376 /* Returns the number of extern "LANG" blocks we are nested within. */
8379 current_lang_depth (void)
8381 return vec_safe_length (current_lang_base);
8384 /* Set global variables CURRENT_LANG_NAME to appropriate value
8385 so that behavior of name-mangling machinery is correct. */
8387 void
8388 push_lang_context (tree name)
8390 vec_safe_push (current_lang_base, current_lang_name);
8392 if (name == lang_name_cplusplus)
8393 current_lang_name = name;
8394 else if (name == lang_name_c)
8395 current_lang_name = name;
8396 else
8397 error ("language string %<\"%E\"%> not recognized", name);
8400 /* Get out of the current language scope. */
8402 void
8403 pop_lang_context (void)
8405 current_lang_name = current_lang_base->pop ();
8408 /* Type instantiation routines. */
8410 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8411 matches the TARGET_TYPE. If there is no satisfactory match, return
8412 error_mark_node, and issue an error & warning messages under
8413 control of FLAGS. Permit pointers to member function if FLAGS
8414 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8415 a template-id, and EXPLICIT_TARGS are the explicitly provided
8416 template arguments.
8418 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8419 is the base path used to reference those member functions. If
8420 the address is resolved to a member function, access checks will be
8421 performed and errors issued if appropriate. */
8423 static tree
8424 resolve_address_of_overloaded_function (tree target_type,
8425 tree overload,
8426 tsubst_flags_t complain,
8427 bool template_only,
8428 tree explicit_targs,
8429 tree access_path)
8431 /* Here's what the standard says:
8433 [over.over]
8435 If the name is a function template, template argument deduction
8436 is done, and if the argument deduction succeeds, the deduced
8437 arguments are used to generate a single template function, which
8438 is added to the set of overloaded functions considered.
8440 Non-member functions and static member functions match targets of
8441 type "pointer-to-function" or "reference-to-function." Nonstatic
8442 member functions match targets of type "pointer-to-member
8443 function;" the function type of the pointer to member is used to
8444 select the member function from the set of overloaded member
8445 functions. If a non-static member function is selected, the
8446 reference to the overloaded function name is required to have the
8447 form of a pointer to member as described in 5.3.1.
8449 If more than one function is selected, any template functions in
8450 the set are eliminated if the set also contains a non-template
8451 function, and any given template function is eliminated if the
8452 set contains a second template function that is more specialized
8453 than the first according to the partial ordering rules 14.5.5.2.
8454 After such eliminations, if any, there shall remain exactly one
8455 selected function. */
8457 int is_ptrmem = 0;
8458 /* We store the matches in a TREE_LIST rooted here. The functions
8459 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8460 interoperability with most_specialized_instantiation. */
8461 tree matches = NULL_TREE;
8462 tree fn;
8463 tree target_fn_type;
8465 /* By the time we get here, we should be seeing only real
8466 pointer-to-member types, not the internal POINTER_TYPE to
8467 METHOD_TYPE representation. */
8468 gcc_assert (!TYPE_PTR_P (target_type)
8469 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8471 gcc_assert (is_overloaded_fn (overload));
8473 /* Check that the TARGET_TYPE is reasonable. */
8474 if (TYPE_PTRFN_P (target_type)
8475 || TYPE_REFFN_P (target_type))
8476 /* This is OK. */;
8477 else if (TYPE_PTRMEMFUNC_P (target_type))
8478 /* This is OK, too. */
8479 is_ptrmem = 1;
8480 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8481 /* This is OK, too. This comes from a conversion to reference
8482 type. */
8483 target_type = build_reference_type (target_type);
8484 else
8486 if (complain & tf_error)
8487 error ("cannot resolve overloaded function %qD based on"
8488 " conversion to type %qT",
8489 OVL_NAME (overload), target_type);
8490 return error_mark_node;
8493 /* Non-member functions and static member functions match targets of type
8494 "pointer-to-function" or "reference-to-function." Nonstatic member
8495 functions match targets of type "pointer-to-member-function;" the
8496 function type of the pointer to member is used to select the member
8497 function from the set of overloaded member functions.
8499 So figure out the FUNCTION_TYPE that we want to match against. */
8500 target_fn_type = static_fn_type (target_type);
8502 /* If we can find a non-template function that matches, we can just
8503 use it. There's no point in generating template instantiations
8504 if we're just going to throw them out anyhow. But, of course, we
8505 can only do this when we don't *need* a template function. */
8506 if (!template_only)
8507 for (lkp_iterator iter (overload); iter; ++iter)
8509 tree fn = *iter;
8511 if (TREE_CODE (fn) == TEMPLATE_DECL)
8512 /* We're not looking for templates just yet. */
8513 continue;
8515 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8516 /* We're looking for a non-static member, and this isn't
8517 one, or vice versa. */
8518 continue;
8520 /* Constraints must be satisfied. This is done before
8521 return type deduction since that instantiates the
8522 function. */
8523 if (!constraints_satisfied_p (fn))
8524 continue;
8526 if (undeduced_auto_decl (fn))
8528 /* Force instantiation to do return type deduction. */
8529 maybe_instantiate_decl (fn);
8530 require_deduced_type (fn);
8533 /* In C++17 we need the noexcept-qualifier to compare types. */
8534 if (flag_noexcept_type
8535 && !maybe_instantiate_noexcept (fn, complain))
8536 continue;
8538 /* See if there's a match. */
8539 tree fntype = static_fn_type (fn);
8540 if (same_type_p (target_fn_type, fntype)
8541 || fnptr_conv_p (target_fn_type, fntype))
8542 matches = tree_cons (fn, NULL_TREE, matches);
8545 /* Now, if we've already got a match (or matches), there's no need
8546 to proceed to the template functions. But, if we don't have a
8547 match we need to look at them, too. */
8548 if (!matches)
8550 tree target_arg_types;
8551 tree target_ret_type;
8552 tree *args;
8553 unsigned int nargs, ia;
8554 tree arg;
8556 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8557 target_ret_type = TREE_TYPE (target_fn_type);
8559 nargs = list_length (target_arg_types);
8560 args = XALLOCAVEC (tree, nargs);
8561 for (arg = target_arg_types, ia = 0;
8562 arg != NULL_TREE;
8563 arg = TREE_CHAIN (arg), ++ia)
8564 args[ia] = TREE_VALUE (arg);
8565 nargs = ia;
8567 for (lkp_iterator iter (overload); iter; ++iter)
8569 tree fn = *iter;
8570 tree instantiation;
8571 tree targs;
8573 if (TREE_CODE (fn) != TEMPLATE_DECL)
8574 /* We're only looking for templates. */
8575 continue;
8577 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8578 != is_ptrmem)
8579 /* We're not looking for a non-static member, and this is
8580 one, or vice versa. */
8581 continue;
8583 tree ret = target_ret_type;
8585 /* If the template has a deduced return type, don't expose it to
8586 template argument deduction. */
8587 if (undeduced_auto_decl (fn))
8588 ret = NULL_TREE;
8590 /* Try to do argument deduction. */
8591 targs = make_tree_vec (DECL_NTPARMS (fn));
8592 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8593 nargs, ret,
8594 DEDUCE_EXACT, LOOKUP_NORMAL,
8595 NULL, false, false);
8596 if (instantiation == error_mark_node)
8597 /* Instantiation failed. */
8598 continue;
8600 /* Constraints must be satisfied. This is done before
8601 return type deduction since that instantiates the
8602 function. */
8603 if (flag_concepts && !constraints_satisfied_p (instantiation))
8604 continue;
8606 /* And now force instantiation to do return type deduction. */
8607 if (undeduced_auto_decl (instantiation))
8609 ++function_depth;
8610 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8611 --function_depth;
8613 require_deduced_type (instantiation);
8616 /* In C++17 we need the noexcept-qualifier to compare types. */
8617 if (flag_noexcept_type)
8618 maybe_instantiate_noexcept (instantiation, complain);
8620 /* See if there's a match. */
8621 tree fntype = static_fn_type (instantiation);
8622 if (same_type_p (target_fn_type, fntype)
8623 || fnptr_conv_p (target_fn_type, fntype))
8624 matches = tree_cons (instantiation, fn, matches);
8627 /* Now, remove all but the most specialized of the matches. */
8628 if (matches)
8630 tree match = most_specialized_instantiation (matches);
8632 if (match != error_mark_node)
8633 matches = tree_cons (TREE_PURPOSE (match),
8634 NULL_TREE,
8635 NULL_TREE);
8639 /* Now we should have exactly one function in MATCHES. */
8640 if (matches == NULL_TREE)
8642 /* There were *no* matches. */
8643 if (complain & tf_error)
8645 error ("no matches converting function %qD to type %q#T",
8646 OVL_NAME (overload), target_type);
8648 print_candidates (overload);
8650 return error_mark_node;
8652 else if (TREE_CHAIN (matches))
8654 /* There were too many matches. First check if they're all
8655 the same function. */
8656 tree match = NULL_TREE;
8658 fn = TREE_PURPOSE (matches);
8660 /* For multi-versioned functions, more than one match is just fine and
8661 decls_match will return false as they are different. */
8662 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8663 if (!decls_match (fn, TREE_PURPOSE (match))
8664 && !targetm.target_option.function_versions
8665 (fn, TREE_PURPOSE (match)))
8666 break;
8668 if (match)
8670 if (complain & tf_error)
8672 error ("converting overloaded function %qD to type %q#T is ambiguous",
8673 OVL_NAME (overload), target_type);
8675 /* Since print_candidates expects the functions in the
8676 TREE_VALUE slot, we flip them here. */
8677 for (match = matches; match; match = TREE_CHAIN (match))
8678 TREE_VALUE (match) = TREE_PURPOSE (match);
8680 print_candidates (matches);
8683 return error_mark_node;
8687 /* Good, exactly one match. Now, convert it to the correct type. */
8688 fn = TREE_PURPOSE (matches);
8690 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8691 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8693 static int explained;
8695 if (!(complain & tf_error))
8696 return error_mark_node;
8698 auto_diagnostic_group d;
8699 if (permerror (input_location, "assuming pointer to member %qD", fn)
8700 && !explained)
8702 inform (input_location, "(a pointer to member can only be "
8703 "formed with %<&%E%>)", fn);
8704 explained = 1;
8708 /* If a pointer to a function that is multi-versioned is requested, the
8709 pointer to the dispatcher function is returned instead. This works
8710 well because indirectly calling the function will dispatch the right
8711 function version at run-time. */
8712 if (DECL_FUNCTION_VERSIONED (fn))
8714 fn = get_function_version_dispatcher (fn);
8715 if (fn == NULL)
8716 return error_mark_node;
8717 /* Mark all the versions corresponding to the dispatcher as used. */
8718 if (!(complain & tf_conv))
8719 mark_versions_used (fn);
8722 /* If we're doing overload resolution purely for the purpose of
8723 determining conversion sequences, we should not consider the
8724 function used. If this conversion sequence is selected, the
8725 function will be marked as used at this point. */
8726 if (!(complain & tf_conv))
8728 /* Make =delete work with SFINAE. */
8729 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8730 return error_mark_node;
8731 if (!mark_used (fn, complain) && !(complain & tf_error))
8732 return error_mark_node;
8735 /* We could not check access to member functions when this
8736 expression was originally created since we did not know at that
8737 time to which function the expression referred. */
8738 if (DECL_FUNCTION_MEMBER_P (fn))
8740 gcc_assert (access_path);
8741 perform_or_defer_access_check (access_path, fn, fn, complain);
8744 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8745 return cp_build_addr_expr (fn, complain);
8746 else
8748 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8749 will mark the function as addressed, but here we must do it
8750 explicitly. */
8751 cxx_mark_addressable (fn);
8753 return fn;
8757 /* This function will instantiate the type of the expression given in
8758 RHS to match the type of LHSTYPE. If errors exist, then return
8759 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8760 we complain on errors. If we are not complaining, never modify rhs,
8761 as overload resolution wants to try many possible instantiations, in
8762 the hope that at least one will work.
8764 For non-recursive calls, LHSTYPE should be a function, pointer to
8765 function, or a pointer to member function. */
8767 tree
8768 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8770 tsubst_flags_t complain_in = complain;
8771 tree access_path = NULL_TREE;
8773 complain &= ~tf_ptrmem_ok;
8775 STRIP_ANY_LOCATION_WRAPPER (rhs);
8777 if (lhstype == unknown_type_node)
8779 if (complain & tf_error)
8780 error ("not enough type information");
8781 return error_mark_node;
8784 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8786 tree fntype = non_reference (lhstype);
8787 if (same_type_p (fntype, TREE_TYPE (rhs)))
8788 return rhs;
8789 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8790 return rhs;
8791 if (flag_ms_extensions
8792 && TYPE_PTRMEMFUNC_P (fntype)
8793 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8794 /* Microsoft allows `A::f' to be resolved to a
8795 pointer-to-member. */
8797 else
8799 if (complain & tf_error)
8800 error ("cannot convert %qE from type %qT to type %qT",
8801 rhs, TREE_TYPE (rhs), fntype);
8802 return error_mark_node;
8806 /* If we instantiate a template, and it is a A ?: C expression
8807 with omitted B, look through the SAVE_EXPR. */
8808 if (TREE_CODE (rhs) == SAVE_EXPR)
8809 rhs = TREE_OPERAND (rhs, 0);
8811 if (BASELINK_P (rhs))
8813 access_path = BASELINK_ACCESS_BINFO (rhs);
8814 rhs = BASELINK_FUNCTIONS (rhs);
8817 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8818 deduce any type information. */
8819 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8821 if (complain & tf_error)
8822 error ("not enough type information");
8823 return error_mark_node;
8826 /* There are only a few kinds of expressions that may have a type
8827 dependent on overload resolution. */
8828 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8829 || TREE_CODE (rhs) == COMPONENT_REF
8830 || is_overloaded_fn (rhs)
8831 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8833 /* This should really only be used when attempting to distinguish
8834 what sort of a pointer to function we have. For now, any
8835 arithmetic operation which is not supported on pointers
8836 is rejected as an error. */
8838 switch (TREE_CODE (rhs))
8840 case COMPONENT_REF:
8842 tree member = TREE_OPERAND (rhs, 1);
8844 member = instantiate_type (lhstype, member, complain);
8845 if (member != error_mark_node
8846 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8847 /* Do not lose object's side effects. */
8848 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8849 TREE_OPERAND (rhs, 0), member);
8850 return member;
8853 case OFFSET_REF:
8854 rhs = TREE_OPERAND (rhs, 1);
8855 if (BASELINK_P (rhs))
8856 return instantiate_type (lhstype, rhs, complain_in);
8858 /* This can happen if we are forming a pointer-to-member for a
8859 member template. */
8860 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8862 /* Fall through. */
8864 case TEMPLATE_ID_EXPR:
8866 tree fns = TREE_OPERAND (rhs, 0);
8867 tree args = TREE_OPERAND (rhs, 1);
8869 return
8870 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8871 /*template_only=*/true,
8872 args, access_path);
8875 case OVERLOAD:
8876 case FUNCTION_DECL:
8877 return
8878 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8879 /*template_only=*/false,
8880 /*explicit_targs=*/NULL_TREE,
8881 access_path);
8883 case ADDR_EXPR:
8885 if (PTRMEM_OK_P (rhs))
8886 complain |= tf_ptrmem_ok;
8888 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8891 case ERROR_MARK:
8892 return error_mark_node;
8894 default:
8895 gcc_unreachable ();
8897 return error_mark_node;
8900 /* Return the name of the virtual function pointer field
8901 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8902 this may have to look back through base types to find the
8903 ultimate field name. (For single inheritance, these could
8904 all be the same name. Who knows for multiple inheritance). */
8906 static tree
8907 get_vfield_name (tree type)
8909 tree binfo, base_binfo;
8911 for (binfo = TYPE_BINFO (type);
8912 BINFO_N_BASE_BINFOS (binfo);
8913 binfo = base_binfo)
8915 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8917 if (BINFO_VIRTUAL_P (base_binfo)
8918 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8919 break;
8922 type = BINFO_TYPE (binfo);
8923 tree ctor_name = constructor_name (type);
8924 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8925 + IDENTIFIER_LENGTH (ctor_name) + 2);
8926 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8927 return get_identifier (buf);
8930 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8931 according to [class]:
8932 The class-name is also inserted
8933 into the scope of the class itself. For purposes of access checking,
8934 the inserted class name is treated as if it were a public member name. */
8936 void
8937 build_self_reference (void)
8939 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8940 tree decl = build_lang_decl (TYPE_DECL, name, current_class_type);
8942 DECL_NONLOCAL (decl) = 1;
8943 DECL_CONTEXT (decl) = current_class_type;
8944 DECL_ARTIFICIAL (decl) = 1;
8945 SET_DECL_SELF_REFERENCE_P (decl);
8946 set_underlying_type (decl);
8947 set_instantiating_module (decl);
8949 if (processing_template_decl)
8950 decl = push_template_decl (decl);
8952 tree saved_cas = current_access_specifier;
8953 current_access_specifier = access_public_node;
8954 finish_member_declaration (decl);
8955 current_access_specifier = saved_cas;
8958 /* Returns 1 if TYPE contains only padding bytes. */
8961 is_empty_class (tree type)
8963 if (type == error_mark_node)
8964 return 0;
8966 if (! CLASS_TYPE_P (type))
8967 return 0;
8969 return CLASSTYPE_EMPTY_P (type);
8972 /* Returns true if TYPE contains no actual data, just various
8973 possible combinations of empty classes. If IGNORE_VPTR is true,
8974 a vptr doesn't prevent the class from being considered empty. Typically
8975 we want to ignore the vptr on assignment, and not on initialization. */
8977 bool
8978 is_really_empty_class (tree type, bool ignore_vptr)
8980 if (CLASS_TYPE_P (type))
8982 tree field;
8983 tree binfo;
8984 tree base_binfo;
8985 int i;
8987 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8988 out, but we'd like to be able to check this before then. */
8989 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8990 return true;
8992 if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
8993 return false;
8995 for (binfo = TYPE_BINFO (type), i = 0;
8996 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8997 if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
8998 return false;
8999 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9000 if (TREE_CODE (field) == FIELD_DECL
9001 && !DECL_ARTIFICIAL (field)
9002 /* An unnamed bit-field is not a data member. */
9003 && !DECL_UNNAMED_BIT_FIELD (field)
9004 && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
9005 return false;
9006 return true;
9008 else if (TREE_CODE (type) == ARRAY_TYPE)
9009 return (integer_zerop (array_type_nelts_top (type))
9010 || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
9011 return false;
9014 /* Note that NAME was looked up while the current class was being
9015 defined and that the result of that lookup was DECL. */
9017 void
9018 maybe_note_name_used_in_class (tree name, tree decl)
9020 /* If we're not defining a class, there's nothing to do. */
9021 if (!(innermost_scope_kind() == sk_class
9022 && TYPE_BEING_DEFINED (current_class_type)
9023 && !LAMBDA_TYPE_P (current_class_type)))
9024 return;
9026 const cp_binding_level *blev = nullptr;
9027 if (const cxx_binding *binding = IDENTIFIER_BINDING (name))
9028 blev = binding->scope;
9029 const cp_binding_level *lev = current_binding_level;
9031 /* Record the binding in the names_used tables for classes inside blev. */
9032 for (int i = current_class_depth; i > 0; --i)
9034 tree type = (i == current_class_depth
9035 ? current_class_type
9036 : current_class_stack[i].type);
9038 for (; lev; lev = lev->level_chain)
9040 if (lev == blev)
9041 /* We found the declaration. */
9042 return;
9043 if (lev->kind == sk_class && lev->this_entity == type)
9044 /* This class is inside the declaration scope. */
9045 break;
9048 auto &names_used = current_class_stack[i-1].names_used;
9049 if (!names_used)
9050 names_used = splay_tree_new (splay_tree_compare_pointers, 0, 0);
9052 tree use = build1_loc (input_location, VIEW_CONVERT_EXPR,
9053 TREE_TYPE (decl), decl);
9054 EXPR_LOCATION_WRAPPER_P (use) = 1;
9055 splay_tree_insert (names_used,
9056 (splay_tree_key) name,
9057 (splay_tree_value) use);
9061 /* Note that NAME was declared (as DECL) in the current class. Check
9062 to see that the declaration is valid under [class.member.lookup]:
9064 If [the result of a search in T for N at point P] differs from the result of
9065 a search in T for N from immediately after the class-specifier of T, the
9066 program is ill-formed, no diagnostic required. */
9068 void
9069 note_name_declared_in_class (tree name, tree decl)
9071 splay_tree names_used;
9072 splay_tree_node n;
9074 /* Look to see if we ever used this name. */
9075 names_used
9076 = current_class_stack[current_class_depth - 1].names_used;
9077 if (!names_used)
9078 return;
9079 /* The C language allows members to be declared with a type of the same
9080 name, and the C++ standard says this diagnostic is not required. So
9081 allow it in extern "C" blocks unless pedantic is specified.
9082 Allow it in all cases if -ms-extensions is specified. */
9083 if ((!pedantic && current_lang_name == lang_name_c)
9084 || flag_ms_extensions)
9085 return;
9086 n = splay_tree_lookup (names_used, (splay_tree_key) name);
9087 if (n)
9089 tree use = (tree) n->value;
9090 location_t loc = EXPR_LOCATION (use);
9091 tree olddecl = OVL_FIRST (TREE_OPERAND (use, 0));
9092 /* [basic.scope.class]
9094 A name N used in a class S shall refer to the same declaration
9095 in its context and when re-evaluated in the completed scope of
9096 S. */
9097 auto ov = make_temp_override (global_dc->pedantic_errors);
9098 if (TREE_CODE (decl) == TYPE_DECL
9099 && TREE_CODE (olddecl) == TYPE_DECL
9100 && same_type_p (TREE_TYPE (decl), TREE_TYPE (olddecl)))
9101 /* Different declaration, but same meaning; just warn. */;
9102 else if (flag_permissive)
9103 /* Let -fpermissive make it a warning like past versions. */;
9104 else
9105 /* Make it an error. */
9106 global_dc->pedantic_errors = 1;
9107 if (pedwarn (location_of (decl), OPT_Wchanges_meaning,
9108 "declaration of %q#D changes meaning of %qD",
9109 decl, OVL_NAME (decl)))
9111 inform (loc, "used here to mean %q#D", olddecl);
9112 inform (location_of (olddecl), "declared here" );
9117 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
9118 Secondary vtables are merged with primary vtables; this function
9119 will return the VAR_DECL for the primary vtable. */
9121 tree
9122 get_vtbl_decl_for_binfo (tree binfo)
9124 tree decl;
9126 decl = BINFO_VTABLE (binfo);
9127 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
9129 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
9130 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
9132 if (decl)
9133 gcc_assert (VAR_P (decl));
9134 return decl;
9138 /* Returns the binfo for the primary base of BINFO. If the resulting
9139 BINFO is a virtual base, and it is inherited elsewhere in the
9140 hierarchy, then the returned binfo might not be the primary base of
9141 BINFO in the complete object. Check BINFO_PRIMARY_P or
9142 BINFO_LOST_PRIMARY_P to be sure. */
9144 static tree
9145 get_primary_binfo (tree binfo)
9147 tree primary_base;
9149 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
9150 if (!primary_base)
9151 return NULL_TREE;
9153 return copied_binfo (primary_base, binfo);
9156 /* As above, but iterate until we reach the binfo that actually provides the
9157 vptr for BINFO. */
9159 static tree
9160 most_primary_binfo (tree binfo)
9162 tree b = binfo;
9163 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9164 && !BINFO_LOST_PRIMARY_P (b))
9166 tree primary_base = get_primary_binfo (b);
9167 gcc_assert (BINFO_PRIMARY_P (primary_base)
9168 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9169 b = primary_base;
9171 return b;
9174 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
9175 type. Note that the virtual inheritance might be above or below BINFO in
9176 the hierarchy. */
9178 bool
9179 vptr_via_virtual_p (tree binfo)
9181 if (TYPE_P (binfo))
9182 binfo = TYPE_BINFO (binfo);
9183 tree primary = most_primary_binfo (binfo);
9184 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
9185 a morally virtual base. */
9186 tree virt = binfo_via_virtual (primary, NULL_TREE);
9187 return virt != NULL_TREE;
9190 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
9192 static int
9193 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
9195 if (!indented_p)
9196 fprintf (stream, "%*s", indent, "");
9197 return 1;
9200 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
9201 INDENT should be zero when called from the top level; it is
9202 incremented recursively. IGO indicates the next expected BINFO in
9203 inheritance graph ordering. */
9205 static tree
9206 dump_class_hierarchy_r (FILE *stream,
9207 dump_flags_t flags,
9208 tree binfo,
9209 tree igo,
9210 int indent)
9212 int indented = 0;
9213 tree base_binfo;
9214 int i;
9216 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
9217 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
9218 (HOST_WIDE_INT) (uintptr_t) binfo);
9219 if (binfo != igo)
9221 fprintf (stream, "alternative-path\n");
9222 return igo;
9224 igo = TREE_CHAIN (binfo);
9226 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
9227 tree_to_shwi (BINFO_OFFSET (binfo)));
9228 if (is_empty_class (BINFO_TYPE (binfo)))
9229 fprintf (stream, " empty");
9230 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
9231 fprintf (stream, " nearly-empty");
9232 if (BINFO_VIRTUAL_P (binfo))
9233 fprintf (stream, " virtual");
9234 fprintf (stream, "\n");
9236 if (BINFO_PRIMARY_P (binfo))
9238 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9239 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
9240 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
9241 TFF_PLAIN_IDENTIFIER),
9242 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
9244 if (BINFO_LOST_PRIMARY_P (binfo))
9246 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9247 fprintf (stream, " lost-primary");
9249 if (indented)
9250 fprintf (stream, "\n");
9252 if (!(flags & TDF_SLIM))
9254 int indented = 0;
9256 if (BINFO_SUBVTT_INDEX (binfo))
9258 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9259 fprintf (stream, " subvttidx=%s",
9260 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
9261 TFF_PLAIN_IDENTIFIER));
9263 if (BINFO_VPTR_INDEX (binfo))
9265 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9266 fprintf (stream, " vptridx=%s",
9267 expr_as_string (BINFO_VPTR_INDEX (binfo),
9268 TFF_PLAIN_IDENTIFIER));
9270 if (BINFO_VPTR_FIELD (binfo))
9272 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9273 fprintf (stream, " vbaseoffset=%s",
9274 expr_as_string (BINFO_VPTR_FIELD (binfo),
9275 TFF_PLAIN_IDENTIFIER));
9277 if (BINFO_VTABLE (binfo))
9279 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9280 fprintf (stream, " vptr=%s",
9281 expr_as_string (BINFO_VTABLE (binfo),
9282 TFF_PLAIN_IDENTIFIER));
9285 if (indented)
9286 fprintf (stream, "\n");
9289 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9290 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
9292 return igo;
9295 /* Dump the BINFO hierarchy for T. */
9297 static void
9298 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
9300 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9301 fprintf (stream, " size=%lu align=%lu\n",
9302 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
9303 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
9304 if (tree as_base = CLASSTYPE_AS_BASE (t))
9305 fprintf (stream, " base size=%lu base align=%lu\n",
9306 (unsigned long)(tree_to_shwi (TYPE_SIZE (as_base))
9307 / BITS_PER_UNIT),
9308 (unsigned long)(TYPE_ALIGN (as_base) / BITS_PER_UNIT));
9309 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
9310 fprintf (stream, "\n");
9313 /* Debug interface to hierarchy dumping. */
9315 void
9316 debug_class (tree t)
9318 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
9321 static void
9322 dump_class_hierarchy (tree t)
9324 dump_flags_t flags;
9325 if (FILE *stream = dump_begin (class_dump_id, &flags))
9327 dump_class_hierarchy_1 (stream, flags, t);
9328 dump_end (class_dump_id, stream);
9332 static void
9333 dump_array (FILE * stream, tree decl)
9335 tree value;
9336 unsigned HOST_WIDE_INT ix;
9337 HOST_WIDE_INT elt;
9338 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
9340 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
9341 / BITS_PER_UNIT);
9342 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
9343 fprintf (stream, " %s entries",
9344 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
9345 TFF_PLAIN_IDENTIFIER));
9346 fprintf (stream, "\n");
9348 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
9349 ix, value)
9350 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
9351 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
9354 static void
9355 dump_vtable (tree t, tree binfo, tree vtable)
9357 dump_flags_t flags;
9358 FILE *stream = dump_begin (class_dump_id, &flags);
9360 if (!stream)
9361 return;
9363 if (!(flags & TDF_SLIM))
9365 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9367 fprintf (stream, "%s for %s",
9368 ctor_vtbl_p ? "Construction vtable" : "Vtable",
9369 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
9370 if (ctor_vtbl_p)
9372 if (!BINFO_VIRTUAL_P (binfo))
9373 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
9374 (HOST_WIDE_INT) (uintptr_t) binfo);
9375 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9377 fprintf (stream, "\n");
9378 dump_array (stream, vtable);
9379 fprintf (stream, "\n");
9382 dump_end (class_dump_id, stream);
9385 static void
9386 dump_vtt (tree t, tree vtt)
9388 dump_flags_t flags;
9389 FILE *stream = dump_begin (class_dump_id, &flags);
9391 if (!stream)
9392 return;
9394 if (!(flags & TDF_SLIM))
9396 fprintf (stream, "VTT for %s\n",
9397 type_as_string (t, TFF_PLAIN_IDENTIFIER));
9398 dump_array (stream, vtt);
9399 fprintf (stream, "\n");
9402 dump_end (class_dump_id, stream);
9405 /* Dump a function or thunk and its thunkees. */
9407 static void
9408 dump_thunk (FILE *stream, int indent, tree thunk)
9410 static const char spaces[] = " ";
9411 tree name = DECL_NAME (thunk);
9412 tree thunks;
9414 fprintf (stream, "%.*s%p %s %s", indent, spaces,
9415 (void *)thunk,
9416 !DECL_THUNK_P (thunk) ? "function"
9417 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9418 name ? IDENTIFIER_POINTER (name) : "<unset>");
9419 if (DECL_THUNK_P (thunk))
9421 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9422 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9424 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9425 if (!virtual_adjust)
9426 /*NOP*/;
9427 else if (DECL_THIS_THUNK_P (thunk))
9428 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9429 tree_to_shwi (virtual_adjust));
9430 else
9431 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9432 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9433 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9434 if (THUNK_ALIAS (thunk))
9435 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9437 fprintf (stream, "\n");
9438 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9439 dump_thunk (stream, indent + 2, thunks);
9442 /* Dump the thunks for FN. */
9444 void
9445 debug_thunks (tree fn)
9447 dump_thunk (stderr, 0, fn);
9450 /* Virtual function table initialization. */
9452 /* Create all the necessary vtables for T and its base classes. */
9454 static void
9455 finish_vtbls (tree t)
9457 tree vbase;
9458 vec<constructor_elt, va_gc> *v = NULL;
9459 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9461 /* We lay out the primary and secondary vtables in one contiguous
9462 vtable. The primary vtable is first, followed by the non-virtual
9463 secondary vtables in inheritance graph order. */
9464 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9465 vtable, t, &v);
9467 /* Then come the virtual bases, also in inheritance graph order. */
9468 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9470 if (!BINFO_VIRTUAL_P (vbase))
9471 continue;
9472 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9475 if (BINFO_VTABLE (TYPE_BINFO (t)))
9476 initialize_vtable (TYPE_BINFO (t), v);
9479 /* Initialize the vtable for BINFO with the INITS. */
9481 static void
9482 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9484 tree decl;
9486 layout_vtable_decl (binfo, vec_safe_length (inits));
9487 decl = get_vtbl_decl_for_binfo (binfo);
9488 initialize_artificial_var (decl, inits);
9489 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9492 /* Build the VTT (virtual table table) for T.
9493 A class requires a VTT if it has virtual bases.
9495 This holds
9496 1 - primary virtual pointer for complete object T
9497 2 - secondary VTTs for each direct non-virtual base of T which requires a
9499 3 - secondary virtual pointers for each direct or indirect base of T which
9500 has virtual bases or is reachable via a virtual path from T.
9501 4 - secondary VTTs for each direct or indirect virtual base of T.
9503 Secondary VTTs look like complete object VTTs without part 4. */
9505 static void
9506 build_vtt (tree t)
9508 tree type;
9509 tree vtt;
9510 tree index;
9511 vec<constructor_elt, va_gc> *inits;
9513 /* Build up the initializers for the VTT. */
9514 inits = NULL;
9515 index = size_zero_node;
9516 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9518 /* If we didn't need a VTT, we're done. */
9519 if (!inits)
9520 return;
9522 /* Figure out the type of the VTT. */
9523 type = build_array_of_n_type (const_ptr_type_node,
9524 inits->length ());
9526 /* Now, build the VTT object itself. */
9527 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9528 initialize_artificial_var (vtt, inits);
9529 /* Add the VTT to the vtables list. */
9530 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9531 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9533 dump_vtt (t, vtt);
9536 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9537 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9538 and CHAIN the vtable pointer for this binfo after construction is
9539 complete. VALUE can also be another BINFO, in which case we recurse. */
9541 static tree
9542 binfo_ctor_vtable (tree binfo)
9544 tree vt;
9546 while (1)
9548 vt = BINFO_VTABLE (binfo);
9549 if (TREE_CODE (vt) == TREE_LIST)
9550 vt = TREE_VALUE (vt);
9551 if (TREE_CODE (vt) == TREE_BINFO)
9552 binfo = vt;
9553 else
9554 break;
9557 return vt;
9560 /* Data for secondary VTT initialization. */
9561 struct secondary_vptr_vtt_init_data
9563 /* Is this the primary VTT? */
9564 bool top_level_p;
9566 /* Current index into the VTT. */
9567 tree index;
9569 /* Vector of initializers built up. */
9570 vec<constructor_elt, va_gc> *inits;
9572 /* The type being constructed by this secondary VTT. */
9573 tree type_being_constructed;
9576 /* Recursively build the VTT-initializer for BINFO (which is in the
9577 hierarchy dominated by T). INITS points to the end of the initializer
9578 list to date. INDEX is the VTT index where the next element will be
9579 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9580 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9581 for virtual bases of T. When it is not so, we build the constructor
9582 vtables for the BINFO-in-T variant. */
9584 static void
9585 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9586 tree *index)
9588 int i;
9589 tree b;
9590 tree init;
9591 secondary_vptr_vtt_init_data data;
9592 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9594 /* We only need VTTs for subobjects with virtual bases. */
9595 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9596 return;
9598 /* We need to use a construction vtable if this is not the primary
9599 VTT. */
9600 if (!top_level_p)
9602 build_ctor_vtbl_group (binfo, t);
9604 /* Record the offset in the VTT where this sub-VTT can be found. */
9605 BINFO_SUBVTT_INDEX (binfo) = *index;
9608 /* Add the address of the primary vtable for the complete object. */
9609 init = binfo_ctor_vtable (binfo);
9610 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9611 if (top_level_p)
9613 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9614 BINFO_VPTR_INDEX (binfo) = *index;
9616 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9618 /* Recursively add the secondary VTTs for non-virtual bases. */
9619 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9620 if (!BINFO_VIRTUAL_P (b))
9621 build_vtt_inits (b, t, inits, index);
9623 /* Add secondary virtual pointers for all subobjects of BINFO with
9624 either virtual bases or reachable along a virtual path, except
9625 subobjects that are non-virtual primary bases. */
9626 data.top_level_p = top_level_p;
9627 data.index = *index;
9628 data.inits = *inits;
9629 data.type_being_constructed = BINFO_TYPE (binfo);
9631 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9633 *index = data.index;
9635 /* data.inits might have grown as we added secondary virtual pointers.
9636 Make sure our caller knows about the new vector. */
9637 *inits = data.inits;
9639 if (top_level_p)
9640 /* Add the secondary VTTs for virtual bases in inheritance graph
9641 order. */
9642 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9644 if (!BINFO_VIRTUAL_P (b))
9645 continue;
9647 build_vtt_inits (b, t, inits, index);
9649 else
9650 /* Remove the ctor vtables we created. */
9651 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9654 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9655 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9657 static tree
9658 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9660 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9662 /* We don't care about bases that don't have vtables. */
9663 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9664 return dfs_skip_bases;
9666 /* We're only interested in proper subobjects of the type being
9667 constructed. */
9668 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9669 return NULL_TREE;
9671 /* We're only interested in bases with virtual bases or reachable
9672 via a virtual path from the type being constructed. */
9673 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9674 || binfo_via_virtual (binfo, data->type_being_constructed)))
9675 return dfs_skip_bases;
9677 /* We're not interested in non-virtual primary bases. */
9678 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9679 return NULL_TREE;
9681 /* Record the index where this secondary vptr can be found. */
9682 if (data->top_level_p)
9684 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9685 BINFO_VPTR_INDEX (binfo) = data->index;
9687 if (BINFO_VIRTUAL_P (binfo))
9689 /* It's a primary virtual base, and this is not a
9690 construction vtable. Find the base this is primary of in
9691 the inheritance graph, and use that base's vtable
9692 now. */
9693 while (BINFO_PRIMARY_P (binfo))
9694 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9698 /* Add the initializer for the secondary vptr itself. */
9699 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9701 /* Advance the vtt index. */
9702 data->index = size_binop (PLUS_EXPR, data->index,
9703 TYPE_SIZE_UNIT (ptr_type_node));
9705 return NULL_TREE;
9708 /* Called from build_vtt_inits via dfs_walk. After building
9709 constructor vtables and generating the sub-vtt from them, we need
9710 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9711 binfo of the base whose sub vtt was generated. */
9713 static tree
9714 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9716 tree vtable = BINFO_VTABLE (binfo);
9718 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9719 /* If this class has no vtable, none of its bases do. */
9720 return dfs_skip_bases;
9722 if (!vtable)
9723 /* This might be a primary base, so have no vtable in this
9724 hierarchy. */
9725 return NULL_TREE;
9727 /* If we scribbled the construction vtable vptr into BINFO, clear it
9728 out now. */
9729 if (TREE_CODE (vtable) == TREE_LIST
9730 && (TREE_PURPOSE (vtable) == (tree) data))
9731 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9733 return NULL_TREE;
9736 /* Build the construction vtable group for BINFO which is in the
9737 hierarchy dominated by T. */
9739 static void
9740 build_ctor_vtbl_group (tree binfo, tree t)
9742 tree type;
9743 tree vtbl;
9744 tree id;
9745 tree vbase;
9746 vec<constructor_elt, va_gc> *v;
9748 /* See if we've already created this construction vtable group. */
9749 id = mangle_ctor_vtbl_for_type (t, binfo);
9750 if (get_global_binding (id))
9751 return;
9753 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9754 /* Build a version of VTBL (with the wrong type) for use in
9755 constructing the addresses of secondary vtables in the
9756 construction vtable group. */
9757 vtbl = build_vtable (t, id, ptr_type_node);
9759 /* Don't export construction vtables from shared libraries. Even on
9760 targets that don't support hidden visibility, this tells
9761 can_refer_decl_in_current_unit_p not to assume that it's safe to
9762 access from a different compilation unit (bz 54314). */
9763 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9764 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9766 v = NULL;
9767 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9768 binfo, vtbl, t, &v);
9770 /* Add the vtables for each of our virtual bases using the vbase in T
9771 binfo. */
9772 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9773 vbase;
9774 vbase = TREE_CHAIN (vbase))
9776 tree b;
9778 if (!BINFO_VIRTUAL_P (vbase))
9779 continue;
9780 b = copied_binfo (vbase, binfo);
9782 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9785 /* Figure out the type of the construction vtable. */
9786 type = build_array_of_n_type (vtable_entry_type, v->length ());
9787 layout_type (type);
9788 TREE_TYPE (vtbl) = type;
9789 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9790 layout_decl (vtbl, 0);
9792 /* Initialize the construction vtable. */
9793 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9794 initialize_artificial_var (vtbl, v);
9795 dump_vtable (t, binfo, vtbl);
9798 /* Add the vtbl initializers for BINFO (and its bases other than
9799 non-virtual primaries) to the list of INITS. BINFO is in the
9800 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9801 the constructor the vtbl inits should be accumulated for. (If this
9802 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9803 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9804 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9805 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9806 but are not necessarily the same in terms of layout. */
9808 static void
9809 accumulate_vtbl_inits (tree binfo,
9810 tree orig_binfo,
9811 tree rtti_binfo,
9812 tree vtbl,
9813 tree t,
9814 vec<constructor_elt, va_gc> **inits)
9816 int i;
9817 tree base_binfo;
9818 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9820 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9822 /* If it doesn't have a vptr, we don't do anything. */
9823 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9824 return;
9826 /* If we're building a construction vtable, we're not interested in
9827 subobjects that don't require construction vtables. */
9828 if (ctor_vtbl_p
9829 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9830 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9831 return;
9833 /* Build the initializers for the BINFO-in-T vtable. */
9834 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9836 /* Walk the BINFO and its bases. We walk in preorder so that as we
9837 initialize each vtable we can figure out at what offset the
9838 secondary vtable lies from the primary vtable. We can't use
9839 dfs_walk here because we need to iterate through bases of BINFO
9840 and RTTI_BINFO simultaneously. */
9841 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9843 /* Skip virtual bases. */
9844 if (BINFO_VIRTUAL_P (base_binfo))
9845 continue;
9846 accumulate_vtbl_inits (base_binfo,
9847 BINFO_BASE_BINFO (orig_binfo, i),
9848 rtti_binfo, vtbl, t,
9849 inits);
9853 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9854 BINFO vtable to L. */
9856 static void
9857 dfs_accumulate_vtbl_inits (tree binfo,
9858 tree orig_binfo,
9859 tree rtti_binfo,
9860 tree orig_vtbl,
9861 tree t,
9862 vec<constructor_elt, va_gc> **l)
9864 tree vtbl = NULL_TREE;
9865 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9866 int n_inits;
9868 if (ctor_vtbl_p
9869 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9871 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9872 primary virtual base. If it is not the same primary in
9873 the hierarchy of T, we'll need to generate a ctor vtable
9874 for it, to place at its location in T. If it is the same
9875 primary, we still need a VTT entry for the vtable, but it
9876 should point to the ctor vtable for the base it is a
9877 primary for within the sub-hierarchy of RTTI_BINFO.
9879 There are three possible cases:
9881 1) We are in the same place.
9882 2) We are a primary base within a lost primary virtual base of
9883 RTTI_BINFO.
9884 3) We are primary to something not a base of RTTI_BINFO. */
9886 tree b;
9887 tree last = NULL_TREE;
9889 /* First, look through the bases we are primary to for RTTI_BINFO
9890 or a virtual base. */
9891 b = binfo;
9892 while (BINFO_PRIMARY_P (b))
9894 b = BINFO_INHERITANCE_CHAIN (b);
9895 last = b;
9896 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9897 goto found;
9899 /* If we run out of primary links, keep looking down our
9900 inheritance chain; we might be an indirect primary. */
9901 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9902 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9903 break;
9904 found:
9906 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9907 base B and it is a base of RTTI_BINFO, this is case 2. In
9908 either case, we share our vtable with LAST, i.e. the
9909 derived-most base within B of which we are a primary. */
9910 if (b == rtti_binfo
9911 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9912 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9913 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9914 binfo_ctor_vtable after everything's been set up. */
9915 vtbl = last;
9917 /* Otherwise, this is case 3 and we get our own. */
9919 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9920 return;
9922 n_inits = vec_safe_length (*l);
9924 if (!vtbl)
9926 tree index;
9927 int non_fn_entries;
9929 /* Add the initializer for this vtable. */
9930 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9931 &non_fn_entries, l);
9933 /* Figure out the position to which the VPTR should point. */
9934 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9935 index = size_binop (MULT_EXPR,
9936 TYPE_SIZE_UNIT (vtable_entry_type),
9937 size_int (non_fn_entries + n_inits));
9938 vtbl = fold_build_pointer_plus (vtbl, index);
9941 if (ctor_vtbl_p)
9942 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9943 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9944 straighten this out. */
9945 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9946 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9947 /* Throw away any unneeded intializers. */
9948 (*l)->truncate (n_inits);
9949 else
9950 /* For an ordinary vtable, set BINFO_VTABLE. */
9951 BINFO_VTABLE (binfo) = vtbl;
9954 static GTY(()) tree abort_fndecl_addr;
9955 static GTY(()) tree dvirt_fn;
9957 /* Construct the initializer for BINFO's virtual function table. BINFO
9958 is part of the hierarchy dominated by T. If we're building a
9959 construction vtable, the ORIG_BINFO is the binfo we should use to
9960 find the actual function pointers to put in the vtable - but they
9961 can be overridden on the path to most-derived in the graph that
9962 ORIG_BINFO belongs. Otherwise,
9963 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9964 BINFO that should be indicated by the RTTI information in the
9965 vtable; it will be a base class of T, rather than T itself, if we
9966 are building a construction vtable.
9968 The value returned is a TREE_LIST suitable for wrapping in a
9969 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9970 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9971 number of non-function entries in the vtable.
9973 It might seem that this function should never be called with a
9974 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9975 base is always subsumed by a derived class vtable. However, when
9976 we are building construction vtables, we do build vtables for
9977 primary bases; we need these while the primary base is being
9978 constructed. */
9980 static void
9981 build_vtbl_initializer (tree binfo,
9982 tree orig_binfo,
9983 tree t,
9984 tree rtti_binfo,
9985 int* non_fn_entries_p,
9986 vec<constructor_elt, va_gc> **inits)
9988 tree v;
9989 vtbl_init_data vid;
9990 unsigned ix, jx;
9991 tree vbinfo;
9992 vec<tree, va_gc> *vbases;
9993 constructor_elt *e;
9995 /* Initialize VID. */
9996 memset (&vid, 0, sizeof (vid));
9997 vid.binfo = binfo;
9998 vid.derived = t;
9999 vid.rtti_binfo = rtti_binfo;
10000 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
10001 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10002 vid.generate_vcall_entries = true;
10003 /* The first vbase or vcall offset is at index -3 in the vtable. */
10004 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
10006 /* Add entries to the vtable for RTTI. */
10007 build_rtti_vtbl_entries (binfo, &vid);
10009 /* Create an array for keeping track of the functions we've
10010 processed. When we see multiple functions with the same
10011 signature, we share the vcall offsets. */
10012 vec_alloc (vid.fns, 32);
10013 /* Add the vcall and vbase offset entries. */
10014 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
10016 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
10017 build_vbase_offset_vtbl_entries. */
10018 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
10019 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
10020 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
10022 /* If the target requires padding between data entries, add that now. */
10023 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
10025 int n_entries = vec_safe_length (vid.inits);
10027 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries,
10028 true);
10030 /* Move data entries into their new positions and add padding
10031 after the new positions. Iterate backwards so we don't
10032 overwrite entries that we would need to process later. */
10033 for (ix = n_entries - 1;
10034 vid.inits->iterate (ix, &e);
10035 ix--)
10037 int j;
10038 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
10039 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
10041 (*vid.inits)[new_position] = *e;
10043 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
10045 constructor_elt *f = &(*vid.inits)[new_position - j];
10046 f->index = NULL_TREE;
10047 f->value = build1 (NOP_EXPR, vtable_entry_type,
10048 null_pointer_node);
10053 if (non_fn_entries_p)
10054 *non_fn_entries_p = vec_safe_length (vid.inits);
10056 /* The initializers for virtual functions were built up in reverse
10057 order. Straighten them out and add them to the running list in one
10058 step. */
10059 jx = vec_safe_length (*inits);
10060 vec_safe_grow (*inits, jx + vid.inits->length (), true);
10062 for (ix = vid.inits->length () - 1;
10063 vid.inits->iterate (ix, &e);
10064 ix--, jx++)
10065 (**inits)[jx] = *e;
10067 /* Go through all the ordinary virtual functions, building up
10068 initializers. */
10069 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
10071 tree delta;
10072 tree vcall_index;
10073 tree fn, fn_original;
10074 tree init = NULL_TREE;
10076 fn = BV_FN (v);
10077 fn_original = fn;
10078 if (DECL_THUNK_P (fn))
10080 if (!DECL_NAME (fn))
10081 finish_thunk (fn);
10082 if (THUNK_ALIAS (fn))
10084 fn = THUNK_ALIAS (fn);
10085 BV_FN (v) = fn;
10087 fn_original = THUNK_TARGET (fn);
10090 /* If the only definition of this function signature along our
10091 primary base chain is from a lost primary, this vtable slot will
10092 never be used, so just zero it out. This is important to avoid
10093 requiring extra thunks which cannot be generated with the function.
10095 We first check this in update_vtable_entry_for_fn, so we handle
10096 restored primary bases properly; we also need to do it here so we
10097 zero out unused slots in ctor vtables, rather than filling them
10098 with erroneous values (though harmless, apart from relocation
10099 costs). */
10100 if (BV_LOST_PRIMARY (v))
10101 init = size_zero_node;
10103 if (! init)
10105 /* Pull the offset for `this', and the function to call, out of
10106 the list. */
10107 delta = BV_DELTA (v);
10108 vcall_index = BV_VCALL_INDEX (v);
10110 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
10111 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10113 /* You can't call an abstract virtual function; it's abstract.
10114 So, we replace these functions with __pure_virtual. */
10115 if (DECL_PURE_VIRTUAL_P (fn_original))
10117 fn = abort_fndecl;
10118 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10120 if (abort_fndecl_addr == NULL)
10121 abort_fndecl_addr
10122 = fold_convert (vfunc_ptr_type_node,
10123 build_fold_addr_expr (fn));
10124 init = abort_fndecl_addr;
10127 /* Likewise for deleted virtuals. */
10128 else if (DECL_DELETED_FN (fn_original))
10130 if (!dvirt_fn)
10132 tree name = get_identifier ("__cxa_deleted_virtual");
10133 dvirt_fn = get_global_binding (name);
10134 if (!dvirt_fn)
10135 dvirt_fn = push_library_fn
10136 (name,
10137 build_function_type_list (void_type_node, NULL_TREE),
10138 NULL_TREE, ECF_NORETURN | ECF_COLD);
10140 fn = dvirt_fn;
10141 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10142 init = fold_convert (vfunc_ptr_type_node,
10143 build_fold_addr_expr (fn));
10145 else
10147 if (!integer_zerop (delta) || vcall_index)
10149 fn = make_thunk (fn, /*this_adjusting=*/1,
10150 delta, vcall_index);
10151 if (!DECL_NAME (fn))
10152 finish_thunk (fn);
10154 /* Take the address of the function, considering it to be of an
10155 appropriate generic type. */
10156 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10157 init = fold_convert (vfunc_ptr_type_node,
10158 build_fold_addr_expr (fn));
10159 /* Don't refer to a virtual destructor from a constructor
10160 vtable or a vtable for an abstract class, since destroying
10161 an object under construction is undefined behavior and we
10162 don't want it to be considered a candidate for speculative
10163 devirtualization. But do create the thunk for ABI
10164 compliance. */
10165 if (DECL_DESTRUCTOR_P (fn_original)
10166 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
10167 || orig_binfo != binfo))
10168 init = size_zero_node;
10172 /* And add it to the chain of initializers. */
10173 if (TARGET_VTABLE_USES_DESCRIPTORS)
10175 int i;
10176 if (init == size_zero_node)
10177 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10178 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10179 else
10180 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10182 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
10183 fn, build_int_cst (NULL_TREE, i));
10184 TREE_CONSTANT (fdesc) = 1;
10186 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), fdesc);
10189 else
10190 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10194 /* Adds to vid->inits the initializers for the vbase and vcall
10195 offsets in BINFO, which is in the hierarchy dominated by T. */
10197 static void
10198 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
10200 tree b;
10202 /* If this is a derived class, we must first create entries
10203 corresponding to the primary base class. */
10204 b = get_primary_binfo (binfo);
10205 if (b)
10206 build_vcall_and_vbase_vtbl_entries (b, vid);
10208 /* Add the vbase entries for this base. */
10209 build_vbase_offset_vtbl_entries (binfo, vid);
10210 /* Add the vcall entries for this base. */
10211 build_vcall_offset_vtbl_entries (binfo, vid);
10214 /* Returns the initializers for the vbase offset entries in the vtable
10215 for BINFO (which is part of the class hierarchy dominated by T), in
10216 reverse order. VBASE_OFFSET_INDEX gives the vtable index
10217 where the next vbase offset will go. */
10219 static void
10220 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10222 tree vbase;
10223 tree t;
10224 tree non_primary_binfo;
10226 /* If there are no virtual baseclasses, then there is nothing to
10227 do. */
10228 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10229 return;
10231 t = vid->derived;
10233 /* We might be a primary base class. Go up the inheritance hierarchy
10234 until we find the most derived class of which we are a primary base:
10235 it is the offset of that which we need to use. */
10236 non_primary_binfo = binfo;
10237 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
10239 tree b;
10241 /* If we have reached a virtual base, then it must be a primary
10242 base (possibly multi-level) of vid->binfo, or we wouldn't
10243 have called build_vcall_and_vbase_vtbl_entries for it. But it
10244 might be a lost primary, so just skip down to vid->binfo. */
10245 if (BINFO_VIRTUAL_P (non_primary_binfo))
10247 non_primary_binfo = vid->binfo;
10248 break;
10251 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
10252 if (get_primary_binfo (b) != non_primary_binfo)
10253 break;
10254 non_primary_binfo = b;
10257 /* Go through the virtual bases, adding the offsets. */
10258 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10259 vbase;
10260 vbase = TREE_CHAIN (vbase))
10262 tree b;
10263 tree delta;
10265 if (!BINFO_VIRTUAL_P (vbase))
10266 continue;
10268 /* Find the instance of this virtual base in the complete
10269 object. */
10270 b = copied_binfo (vbase, binfo);
10272 /* If we've already got an offset for this virtual base, we
10273 don't need another one. */
10274 if (BINFO_VTABLE_PATH_MARKED (b))
10275 continue;
10276 BINFO_VTABLE_PATH_MARKED (b) = 1;
10278 /* Figure out where we can find this vbase offset. */
10279 delta = size_binop (MULT_EXPR,
10280 vid->index,
10281 fold_convert (ssizetype,
10282 TYPE_SIZE_UNIT (vtable_entry_type)));
10283 if (vid->primary_vtbl_p)
10284 BINFO_VPTR_FIELD (b) = delta;
10286 if (binfo != TYPE_BINFO (t))
10287 /* The vbase offset had better be the same. */
10288 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
10290 /* The next vbase will come at a more negative offset. */
10291 vid->index = size_binop (MINUS_EXPR, vid->index,
10292 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10294 /* The initializer is the delta from BINFO to this virtual base.
10295 The vbase offsets go in reverse inheritance-graph order, and
10296 we are walking in inheritance graph order so these end up in
10297 the right order. */
10298 delta = size_diffop_loc (input_location,
10299 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
10301 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
10302 fold_build1_loc (input_location, NOP_EXPR,
10303 vtable_entry_type, delta));
10307 /* Adds the initializers for the vcall offset entries in the vtable
10308 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10309 to VID->INITS. */
10311 static void
10312 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10314 /* We only need these entries if this base is a virtual base. We
10315 compute the indices -- but do not add to the vtable -- when
10316 building the main vtable for a class. */
10317 if (binfo == TYPE_BINFO (vid->derived)
10318 || (BINFO_VIRTUAL_P (binfo)
10319 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10320 correspond to VID->DERIVED), we are building a primary
10321 construction virtual table. Since this is a primary
10322 virtual table, we do not need the vcall offsets for
10323 BINFO. */
10324 && binfo != vid->rtti_binfo))
10326 /* We need a vcall offset for each of the virtual functions in this
10327 vtable. For example:
10329 class A { virtual void f (); };
10330 class B1 : virtual public A { virtual void f (); };
10331 class B2 : virtual public A { virtual void f (); };
10332 class C: public B1, public B2 { virtual void f (); };
10334 A C object has a primary base of B1, which has a primary base of A. A
10335 C also has a secondary base of B2, which no longer has a primary base
10336 of A. So the B2-in-C construction vtable needs a secondary vtable for
10337 A, which will adjust the A* to a B2* to call f. We have no way of
10338 knowing what (or even whether) this offset will be when we define B2,
10339 so we store this "vcall offset" in the A sub-vtable and look it up in
10340 a "virtual thunk" for B2::f.
10342 We need entries for all the functions in our primary vtable and
10343 in our non-virtual bases' secondary vtables. */
10344 vid->vbase = binfo;
10345 /* If we are just computing the vcall indices -- but do not need
10346 the actual entries -- not that. */
10347 if (!BINFO_VIRTUAL_P (binfo))
10348 vid->generate_vcall_entries = false;
10349 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10350 add_vcall_offset_vtbl_entries_r (binfo, vid);
10354 /* Build vcall offsets, starting with those for BINFO. */
10356 static void
10357 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
10359 int i;
10360 tree primary_binfo;
10361 tree base_binfo;
10363 /* Don't walk into virtual bases -- except, of course, for the
10364 virtual base for which we are building vcall offsets. Any
10365 primary virtual base will have already had its offsets generated
10366 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10367 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
10368 return;
10370 /* If BINFO has a primary base, process it first. */
10371 primary_binfo = get_primary_binfo (binfo);
10372 if (primary_binfo)
10373 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
10375 /* Add BINFO itself to the list. */
10376 add_vcall_offset_vtbl_entries_1 (binfo, vid);
10378 /* Scan the non-primary bases of BINFO. */
10379 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10380 if (base_binfo != primary_binfo)
10381 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
10384 /* Called from build_vcall_offset_vtbl_entries_r. */
10386 static void
10387 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10389 /* Make entries for the rest of the virtuals. */
10390 tree orig_fn;
10392 /* The ABI requires that the methods be processed in declaration
10393 order. */
10394 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
10395 orig_fn;
10396 orig_fn = DECL_CHAIN (orig_fn))
10397 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10398 add_vcall_offset (orig_fn, binfo, vid);
10401 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10403 static void
10404 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10406 size_t i;
10407 tree vcall_offset;
10408 tree derived_entry;
10410 /* If there is already an entry for a function with the same
10411 signature as FN, then we do not need a second vcall offset.
10412 Check the list of functions already present in the derived
10413 class vtable. */
10414 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10416 if (same_signature_p (derived_entry, orig_fn)
10417 /* We only use one vcall offset for virtual destructors,
10418 even though there are two virtual table entries. */
10419 || (DECL_DESTRUCTOR_P (derived_entry)
10420 && DECL_DESTRUCTOR_P (orig_fn)))
10421 return;
10424 /* If we are building these vcall offsets as part of building
10425 the vtable for the most derived class, remember the vcall
10426 offset. */
10427 if (vid->binfo == TYPE_BINFO (vid->derived))
10429 tree_pair_s elt = {orig_fn, vid->index};
10430 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10433 /* The next vcall offset will be found at a more negative
10434 offset. */
10435 vid->index = size_binop (MINUS_EXPR, vid->index,
10436 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10438 /* Keep track of this function. */
10439 vec_safe_push (vid->fns, orig_fn);
10441 if (vid->generate_vcall_entries)
10443 tree base;
10444 tree fn;
10446 /* Find the overriding function. */
10447 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10448 if (fn == error_mark_node)
10449 vcall_offset = build_zero_cst (vtable_entry_type);
10450 else
10452 base = TREE_VALUE (fn);
10454 /* The vbase we're working on is a primary base of
10455 vid->binfo. But it might be a lost primary, so its
10456 BINFO_OFFSET might be wrong, so we just use the
10457 BINFO_OFFSET from vid->binfo. */
10458 vcall_offset = size_diffop_loc (input_location,
10459 BINFO_OFFSET (base),
10460 BINFO_OFFSET (vid->binfo));
10461 vcall_offset = fold_build1_loc (input_location,
10462 NOP_EXPR, vtable_entry_type,
10463 vcall_offset);
10465 /* Add the initializer to the vtable. */
10466 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10470 /* Return vtbl initializers for the RTTI entries corresponding to the
10471 BINFO's vtable. The RTTI entries should indicate the object given
10472 by VID->rtti_binfo. */
10474 static void
10475 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10477 tree b;
10478 tree t;
10479 tree offset;
10480 tree decl;
10481 tree init;
10483 t = BINFO_TYPE (vid->rtti_binfo);
10485 /* To find the complete object, we will first convert to our most
10486 primary base, and then add the offset in the vtbl to that value. */
10487 b = most_primary_binfo (binfo);
10488 offset = size_diffop_loc (input_location,
10489 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10491 /* The second entry is the address of the typeinfo object. */
10492 if (flag_rtti)
10493 decl = build_address (get_tinfo_decl (t));
10494 else
10495 decl = integer_zero_node;
10497 /* Convert the declaration to a type that can be stored in the
10498 vtable. */
10499 init = build_nop (vfunc_ptr_type_node, decl);
10500 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10502 /* Add the offset-to-top entry. It comes earlier in the vtable than
10503 the typeinfo entry. Convert the offset to look like a
10504 function pointer, so that we can put it in the vtable. */
10505 init = build_nop (vfunc_ptr_type_node, offset);
10506 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10509 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10510 accessibility. */
10512 bool
10513 uniquely_derived_from_p (tree parent, tree type)
10515 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10516 return base && base != error_mark_node;
10519 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10521 bool
10522 publicly_uniquely_derived_p (tree parent, tree type)
10524 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10525 NULL, tf_none);
10526 return base && base != error_mark_node;
10529 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10530 class between them, if any. */
10532 tree
10533 common_enclosing_class (tree ctx1, tree ctx2)
10535 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10536 return NULL_TREE;
10537 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10538 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10539 if (ctx1 == ctx2)
10540 return ctx1;
10541 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10542 TYPE_MARKED_P (t) = true;
10543 tree found = NULL_TREE;
10544 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10545 if (TYPE_MARKED_P (t))
10547 found = t;
10548 break;
10550 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10551 TYPE_MARKED_P (t) = false;
10552 return found;
10555 #include "gt-cp-class.h"