compiler: add missing Slice_info_expression::do_traverse
[official-gcc.git] / gcc / cp / class.cc
blob68b62086340bcd1ab357907308b52b64c680b994
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_function ());
349 /* For a non-pointer simple base reference, express it as a COMPONENT_REF
350 without taking its address (and so causing lambda capture, 91933). */
351 if (code == PLUS_EXPR && !v_binfo && !want_pointer && !has_empty && !uneval)
352 return build_simple_base_path (expr, binfo);
354 if (!want_pointer)
356 rvalue = !lvalue_p (expr);
357 /* This must happen before the call to save_expr. */
358 expr = cp_build_addr_expr (expr, complain);
360 else
361 expr = mark_rvalue_use (expr);
363 offset = BINFO_OFFSET (binfo);
364 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
365 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
366 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
367 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
368 expression returned matches the input. */
369 target_type = cp_build_qualified_type
370 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
371 ptr_target_type = build_pointer_type (target_type);
373 /* Do we need to look in the vtable for the real offset? */
374 virtual_access = (v_binfo && fixed_type_p <= 0);
376 /* Don't bother with the calculations inside sizeof; they'll ICE if the
377 source type is incomplete and the pointer value doesn't matter. In a
378 template (even in instantiate_non_dependent_expr), we don't have vtables
379 set up properly yet, and the value doesn't matter there either; we're
380 just interested in the result of overload resolution. */
381 if (uneval)
383 expr = build_nop (ptr_target_type, expr);
384 goto indout;
387 if (!COMPLETE_TYPE_P (probe))
389 if (complain & tf_error)
390 error ("cannot convert from %qT to base class %qT because %qT is "
391 "incomplete", BINFO_TYPE (d_binfo), BINFO_TYPE (binfo),
392 BINFO_TYPE (d_binfo));
393 return error_mark_node;
396 /* If we're in an NSDMI, we don't have the full constructor context yet
397 that we need for converting to a virtual base, so just build a stub
398 CONVERT_EXPR and expand it later in bot_replace. */
399 if (virtual_access && fixed_type_p < 0
400 && current_scope () != current_function_decl)
402 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
403 CONVERT_EXPR_VBASE_PATH (expr) = true;
404 goto indout;
407 /* Do we need to check for a null pointer? */
408 if (want_pointer && !nonnull)
410 /* If we know the conversion will not actually change the value
411 of EXPR, then we can avoid testing the expression for NULL.
412 We have to avoid generating a COMPONENT_REF for a base class
413 field, because other parts of the compiler know that such
414 expressions are always non-NULL. */
415 if (!virtual_access && integer_zerop (offset))
416 return build_nop (ptr_target_type, expr);
417 null_test = error_mark_node;
420 /* Protect against multiple evaluation if necessary. */
421 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
422 expr = save_expr (expr);
424 /* Store EXPR and build the real null test just before returning. */
425 if (null_test)
426 null_test = expr;
428 /* If this is a simple base reference, express it as a COMPONENT_REF. */
429 if (code == PLUS_EXPR && !virtual_access
430 /* We don't build base fields for empty bases, and they aren't very
431 interesting to the optimizers anyway. */
432 && !has_empty)
434 expr = cp_build_fold_indirect_ref (expr);
435 expr = build_simple_base_path (expr, binfo);
436 if (rvalue && lvalue_p (expr))
437 expr = move (expr);
438 if (want_pointer)
439 expr = build_address (expr);
440 target_type = TREE_TYPE (expr);
441 goto out;
444 if (virtual_access)
446 /* Going via virtual base V_BINFO. We need the static offset
447 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
448 V_BINFO. That offset is an entry in D_BINFO's vtable. */
449 tree v_offset;
451 if (fixed_type_p < 0 && in_base_initializer)
453 /* In a base member initializer, we cannot rely on the
454 vtable being set up. We have to indirect via the
455 vtt_parm. */
456 tree t;
458 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
459 t = build_pointer_type (t);
460 v_offset = fold_convert (t, current_vtt_parm);
461 v_offset = cp_build_fold_indirect_ref (v_offset);
463 else
465 tree t = expr;
466 if (sanitize_flags_p (SANITIZE_VPTR)
467 && fixed_type_p == 0)
469 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
470 probe, expr);
471 if (t == NULL_TREE)
472 t = expr;
474 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
475 TREE_TYPE (TREE_TYPE (expr)));
478 if (v_offset == error_mark_node)
479 return error_mark_node;
481 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
482 v_offset = build1 (NOP_EXPR,
483 build_pointer_type (ptrdiff_type_node),
484 v_offset);
485 v_offset = cp_build_fold_indirect_ref (v_offset);
486 TREE_CONSTANT (v_offset) = 1;
488 offset = convert_to_integer (ptrdiff_type_node,
489 size_diffop_loc (input_location, offset,
490 BINFO_OFFSET (v_binfo)));
492 if (!integer_zerop (offset))
493 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
495 if (fixed_type_p < 0)
496 /* Negative fixed_type_p means this is a constructor or destructor;
497 virtual base layout is fixed in in-charge [cd]tors, but not in
498 base [cd]tors. */
499 offset = build_if_in_charge
500 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
501 v_offset);
502 else
503 offset = v_offset;
506 if (want_pointer)
507 target_type = ptr_target_type;
509 if (!integer_zerop (offset))
511 offset = fold_convert (sizetype, offset);
512 if (code == MINUS_EXPR)
513 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
514 expr = fold_build_pointer_plus (expr, offset);
516 else
517 null_test = NULL;
519 expr = build1 (NOP_EXPR, ptr_target_type, expr);
521 indout:
522 if (!want_pointer)
524 expr = cp_build_fold_indirect_ref (expr);
525 if (rvalue)
526 expr = move (expr);
529 out:
530 if (null_test)
531 /* Wrap EXPR in a null test. */
532 expr = build_if_nonnull (null_test, expr, complain);
534 return expr;
537 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
538 Perform a derived-to-base conversion by recursively building up a
539 sequence of COMPONENT_REFs to the appropriate base fields. */
541 static tree
542 build_simple_base_path (tree expr, tree binfo)
544 tree type = BINFO_TYPE (binfo);
545 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
546 tree field;
548 if (d_binfo == NULL_TREE)
550 tree temp;
552 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
554 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
555 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
556 an lvalue in the front end; only _DECLs and _REFs are lvalues
557 in the back end. */
558 temp = unary_complex_lvalue (ADDR_EXPR, expr);
559 if (temp)
560 expr = cp_build_fold_indirect_ref (temp);
562 return expr;
565 /* Recurse. */
566 expr = build_simple_base_path (expr, d_binfo);
568 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
569 field; field = DECL_CHAIN (field))
570 /* Is this the base field created by build_base_field? */
571 if (TREE_CODE (field) == FIELD_DECL
572 && DECL_FIELD_IS_BASE (field)
573 && TREE_TYPE (field) == type
574 /* If we're looking for a field in the most-derived class,
575 also check the field offset; we can have two base fields
576 of the same type if one is an indirect virtual base and one
577 is a direct non-virtual base. */
578 && (BINFO_INHERITANCE_CHAIN (d_binfo)
579 || tree_int_cst_equal (byte_position (field),
580 BINFO_OFFSET (binfo))))
582 /* We don't use build_class_member_access_expr here, as that
583 has unnecessary checks, and more importantly results in
584 recursive calls to dfs_walk_once. */
585 int type_quals = cp_type_quals (TREE_TYPE (expr));
587 expr = build3 (COMPONENT_REF,
588 cp_build_qualified_type (type, type_quals),
589 expr, field, NULL_TREE);
590 /* Mark the expression const or volatile, as appropriate.
591 Even though we've dealt with the type above, we still have
592 to mark the expression itself. */
593 if (type_quals & TYPE_QUAL_CONST)
594 TREE_READONLY (expr) = 1;
595 if (type_quals & TYPE_QUAL_VOLATILE)
596 TREE_THIS_VOLATILE (expr) = 1;
598 return expr;
601 /* Didn't find the base field?!? */
602 gcc_unreachable ();
605 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
606 type is a class type or a pointer to a class type. In the former
607 case, TYPE is also a class type; in the latter it is another
608 pointer type. If CHECK_ACCESS is true, an error message is emitted
609 if TYPE is inaccessible. If OBJECT has pointer type, the value is
610 assumed to be non-NULL. */
612 tree
613 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
614 tsubst_flags_t complain)
616 tree binfo;
617 tree object_type;
619 if (TYPE_PTR_P (TREE_TYPE (object)))
621 object_type = TREE_TYPE (TREE_TYPE (object));
622 type = TREE_TYPE (type);
624 else
625 object_type = TREE_TYPE (object);
627 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
628 NULL, complain);
629 if (!binfo || binfo == error_mark_node)
630 return error_mark_node;
632 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
635 /* EXPR is an expression with unqualified class type. BASE is a base
636 binfo of that class type. Returns EXPR, converted to the BASE
637 type. This function assumes that EXPR is the most derived class;
638 therefore virtual bases can be found at their static offsets. */
640 tree
641 convert_to_base_statically (tree expr, tree base)
643 tree expr_type;
645 expr_type = TREE_TYPE (expr);
646 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
648 /* If this is a non-empty base, use a COMPONENT_REF. */
649 if (!is_empty_class (BINFO_TYPE (base)))
650 return build_simple_base_path (expr, base);
652 /* We use fold_build2 and fold_convert below to simplify the trees
653 provided to the optimizers. It is not safe to call these functions
654 when processing a template because they do not handle C++-specific
655 trees. */
656 gcc_assert (!processing_template_decl);
657 expr = cp_build_addr_expr (expr, tf_warning_or_error);
658 if (!integer_zerop (BINFO_OFFSET (base)))
659 expr = fold_build_pointer_plus_loc (input_location,
660 expr, BINFO_OFFSET (base));
661 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
662 expr = build_fold_indirect_ref_loc (input_location, expr);
665 return expr;
668 /* True IFF EXPR is a reference to an empty base class "subobject", as built in
669 convert_to_base_statically. We look for the result of the fold_convert
670 call, a NOP_EXPR from one pointer type to another, where the target is an
671 empty base of the original type. */
673 bool
674 is_empty_base_ref (tree expr)
676 if (TREE_CODE (expr) == INDIRECT_REF)
677 expr = TREE_OPERAND (expr, 0);
678 if (TREE_CODE (expr) != NOP_EXPR)
679 return false;
680 tree type = TREE_TYPE (expr);
681 if (!POINTER_TYPE_P (type))
682 return false;
683 type = TREE_TYPE (type);
684 if (!is_empty_class (type))
685 return false;
686 STRIP_NOPS (expr);
687 tree fromtype = TREE_TYPE (expr);
688 if (!POINTER_TYPE_P (fromtype))
689 return false;
690 fromtype = TREE_TYPE (fromtype);
691 return (CLASS_TYPE_P (fromtype)
692 && !same_type_ignoring_top_level_qualifiers_p (fromtype, type)
693 && DERIVED_FROM_P (type, fromtype));
696 tree
697 build_vfield_ref (tree datum, tree type)
699 tree vfield, vcontext;
701 if (datum == error_mark_node
702 /* Can happen in case of duplicate base types (c++/59082). */
703 || !TYPE_VFIELD (type))
704 return error_mark_node;
706 /* First, convert to the requested type. */
707 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
708 datum = convert_to_base (datum, type, /*check_access=*/false,
709 /*nonnull=*/true, tf_warning_or_error);
711 /* Second, the requested type may not be the owner of its own vptr.
712 If not, convert to the base class that owns it. We cannot use
713 convert_to_base here, because VCONTEXT may appear more than once
714 in the inheritance hierarchy of TYPE, and thus direct conversion
715 between the types may be ambiguous. Following the path back up
716 one step at a time via primary bases avoids the problem. */
717 vfield = TYPE_VFIELD (type);
718 vcontext = DECL_CONTEXT (vfield);
719 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
721 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
722 type = TREE_TYPE (datum);
725 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
728 /* Given an object INSTANCE, return an expression which yields the
729 vtable element corresponding to INDEX. There are many special
730 cases for INSTANCE which we take care of here, mainly to avoid
731 creating extra tree nodes when we don't have to. */
733 tree
734 build_vtbl_ref (tree instance, tree idx)
736 tree aref;
737 tree vtbl = NULL_TREE;
739 /* Try to figure out what a reference refers to, and
740 access its virtual function table directly. */
742 int cdtorp = 0;
743 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
745 tree basetype = non_reference (TREE_TYPE (instance));
747 if (fixed_type && !cdtorp)
749 tree binfo = lookup_base (fixed_type, basetype,
750 ba_unique, NULL, tf_none);
751 if (binfo && binfo != error_mark_node)
752 vtbl = unshare_expr (BINFO_VTABLE (binfo));
755 if (!vtbl)
756 vtbl = build_vfield_ref (instance, basetype);
758 aref = build_array_ref (input_location, vtbl, idx);
759 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
761 return aref;
764 /* Given a stable object pointer INSTANCE_PTR, return an expression which
765 yields a function pointer corresponding to vtable element INDEX. */
767 tree
768 build_vfn_ref (tree instance_ptr, tree idx)
770 tree aref;
772 aref = build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr), idx);
774 /* When using function descriptors, the address of the
775 vtable entry is treated as a function pointer. */
776 if (TARGET_VTABLE_USES_DESCRIPTORS)
777 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
778 cp_build_addr_expr (aref, tf_warning_or_error));
780 /* Remember this as a method reference, for later devirtualization. */
781 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr,
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 /* Create default constructors, assignment operators, and so forth for
3296 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3297 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3298 the class cannot have a default constructor, copy constructor
3299 taking a const reference argument, or an assignment operator taking
3300 a const reference, respectively. */
3302 static void
3303 add_implicitly_declared_members (tree t, tree* access_decls,
3304 int cant_have_const_cctor,
3305 int cant_have_const_assignment)
3307 /* Destructor. */
3308 if (!CLASSTYPE_DESTRUCTOR (t))
3309 /* In general, we create destructors lazily. */
3310 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3312 bool move_ok = false;
3313 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3314 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3315 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3316 move_ok = true;
3318 /* [class.ctor]
3320 If there is no user-declared constructor for a class, a default
3321 constructor is implicitly declared. */
3322 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3324 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3325 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3326 if (cxx_dialect >= cxx11)
3327 TYPE_HAS_CONSTEXPR_CTOR (t)
3328 /* Don't force the declaration to get a hard answer; if the
3329 definition would have made the class non-literal, it will still be
3330 non-literal because of the base or member in question, and that
3331 gives a better diagnostic. */
3332 = type_maybe_constexpr_default_constructor (t);
3335 /* [class.ctor]
3337 If a class definition does not explicitly declare a copy
3338 constructor, one is declared implicitly. */
3339 if (! TYPE_HAS_COPY_CTOR (t))
3341 TYPE_HAS_COPY_CTOR (t) = 1;
3342 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3343 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3344 if (move_ok)
3345 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3348 /* If there is no assignment operator, one will be created if and
3349 when it is needed. For now, just record whether or not the type
3350 of the parameter to the assignment operator will be a const or
3351 non-const reference. */
3352 if (!TYPE_HAS_COPY_ASSIGN (t))
3354 TYPE_HAS_COPY_ASSIGN (t) = 1;
3355 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3356 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3357 if (move_ok && !LAMBDA_TYPE_P (t))
3358 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3361 /* We can't be lazy about declaring functions that might override
3362 a virtual function from a base class. */
3363 declare_virt_assop_and_dtor (t);
3365 /* If the class definition does not explicitly declare an == operator
3366 function, but declares a defaulted three-way comparison operator function,
3367 an == operator function is declared implicitly. */
3368 if (!classtype_has_op (t, EQ_EXPR))
3369 if (tree space = classtype_has_defaulted_op (t, SPACESHIP_EXPR))
3371 tree eq = implicitly_declare_fn (sfk_comparison, t, false, space,
3372 NULL_TREE);
3373 bool is_friend = DECL_CONTEXT (space) != t;
3374 if (is_friend)
3375 do_friend (NULL_TREE, DECL_NAME (eq), eq,
3376 NO_SPECIAL, true);
3377 else
3379 add_method (t, eq, false);
3380 DECL_CHAIN (eq) = TYPE_FIELDS (t);
3381 TYPE_FIELDS (t) = eq;
3383 maybe_add_class_template_decl_list (t, eq, is_friend);
3386 while (*access_decls)
3388 tree using_decl = TREE_VALUE (*access_decls);
3389 tree decl = USING_DECL_DECLS (using_decl);
3390 if (DECL_NAME (using_decl) == ctor_identifier)
3392 /* declare, then remove the decl */
3393 tree ctor_list = decl;
3394 location_t loc = input_location;
3395 input_location = DECL_SOURCE_LOCATION (using_decl);
3396 for (tree fn : ovl_range (ctor_list))
3397 one_inherited_ctor (fn, t, using_decl);
3398 *access_decls = TREE_CHAIN (*access_decls);
3399 input_location = loc;
3401 else
3402 access_decls = &TREE_CHAIN (*access_decls);
3406 /* Cache of enum_min_precision values. */
3407 static GTY((deletable)) hash_map<tree, int> *enum_to_min_precision;
3409 /* Return the minimum precision of a bit-field needed to store all
3410 enumerators of ENUMERAL_TYPE TYPE. */
3412 static int
3413 enum_min_precision (tree type)
3415 type = TYPE_MAIN_VARIANT (type);
3416 /* For unscoped enums without fixed underlying type and without mode
3417 attribute we can just use precision of the underlying type. */
3418 if (UNSCOPED_ENUM_P (type)
3419 && !ENUM_FIXED_UNDERLYING_TYPE_P (type)
3420 && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type)))
3421 return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3423 if (enum_to_min_precision == NULL)
3424 enum_to_min_precision = hash_map<tree, int>::create_ggc (37);
3426 bool existed;
3427 int &prec = enum_to_min_precision->get_or_insert (type, &existed);
3428 if (existed)
3429 return prec;
3431 tree minnode, maxnode;
3432 if (TYPE_VALUES (type))
3434 minnode = maxnode = NULL_TREE;
3435 for (tree values = TYPE_VALUES (type);
3436 values; values = TREE_CHAIN (values))
3438 tree decl = TREE_VALUE (values);
3439 tree value = DECL_INITIAL (decl);
3440 if (value == error_mark_node)
3441 value = integer_zero_node;
3442 if (!minnode)
3443 minnode = maxnode = value;
3444 else if (tree_int_cst_lt (maxnode, value))
3445 maxnode = value;
3446 else if (tree_int_cst_lt (value, minnode))
3447 minnode = value;
3450 else
3451 minnode = maxnode = integer_zero_node;
3453 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
3454 int lowprec = tree_int_cst_min_precision (minnode, sgn);
3455 int highprec = tree_int_cst_min_precision (maxnode, sgn);
3456 prec = MAX (lowprec, highprec);
3457 return prec;
3460 /* FIELD is a bit-field. We are finishing the processing for its
3461 enclosing type. Issue any appropriate messages and set appropriate
3462 flags. Returns false if an error has been diagnosed. */
3464 static bool
3465 check_bitfield_decl (tree field)
3467 tree type = TREE_TYPE (field);
3468 tree w;
3470 /* Extract the declared width of the bitfield, which has been
3471 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3472 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3473 gcc_assert (w != NULL_TREE);
3474 /* Remove the bit-field width indicator so that the rest of the
3475 compiler does not treat that value as a qualifier. */
3476 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3478 /* Detect invalid bit-field type. */
3479 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3481 error_at (DECL_SOURCE_LOCATION (field),
3482 "bit-field %q#D with non-integral type %qT", field, type);
3483 w = error_mark_node;
3485 else
3487 location_t loc = input_location;
3488 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3489 STRIP_NOPS (w);
3491 /* detect invalid field size. */
3492 input_location = DECL_SOURCE_LOCATION (field);
3493 w = cxx_constant_value (w);
3494 input_location = loc;
3496 if (TREE_CODE (w) != INTEGER_CST)
3498 error ("bit-field %q+D width not an integer constant", field);
3499 w = error_mark_node;
3501 else if (tree_int_cst_sgn (w) < 0)
3503 error ("negative width in bit-field %q+D", field);
3504 w = error_mark_node;
3506 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3508 error ("zero width for bit-field %q+D", field);
3509 w = error_mark_node;
3511 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3512 && TREE_CODE (type) != BOOLEAN_TYPE
3513 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3514 || ((TREE_CODE (type) == ENUMERAL_TYPE
3515 || TREE_CODE (type) == BOOLEAN_TYPE)
3516 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3517 warning_at (DECL_SOURCE_LOCATION (field), 0,
3518 "width of %qD exceeds its type", field);
3519 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3521 int prec = enum_min_precision (type);
3522 if (compare_tree_int (w, prec) < 0)
3523 warning_at (DECL_SOURCE_LOCATION (field), 0,
3524 "%qD is too small to hold all values of %q#T",
3525 field, type);
3529 if (w != error_mark_node)
3531 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3532 DECL_BIT_FIELD (field) = 1;
3533 return true;
3535 else
3537 /* Non-bit-fields are aligned for their type. */
3538 DECL_BIT_FIELD (field) = 0;
3539 CLEAR_DECL_C_BIT_FIELD (field);
3540 return false;
3544 /* FIELD is a non bit-field. We are finishing the processing for its
3545 enclosing type T. Issue any appropriate messages and set appropriate
3546 flags. */
3548 static bool
3549 check_field_decl (tree field,
3550 tree t,
3551 int* cant_have_const_ctor,
3552 int* no_const_asn_ref)
3554 tree type = strip_array_types (TREE_TYPE (field));
3555 bool any_default_members = false;
3557 /* In C++98 an anonymous union cannot contain any fields which would change
3558 the settings of CANT_HAVE_CONST_CTOR and friends. */
3559 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3561 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3562 structs. So, we recurse through their fields here. */
3563 else if (ANON_AGGR_TYPE_P (type))
3565 for (tree fields = TYPE_FIELDS (type); fields;
3566 fields = DECL_CHAIN (fields))
3567 if (TREE_CODE (fields) == FIELD_DECL)
3568 any_default_members |= check_field_decl (fields, t,
3569 cant_have_const_ctor,
3570 no_const_asn_ref);
3572 /* Check members with class type for constructors, destructors,
3573 etc. */
3574 else if (CLASS_TYPE_P (type))
3576 /* Never let anything with uninheritable virtuals
3577 make it through without complaint. */
3578 abstract_virtuals_error (field, type);
3580 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3582 static bool warned;
3583 int oldcount = errorcount;
3584 if (TYPE_NEEDS_CONSTRUCTING (type))
3585 error ("member %q+#D with constructor not allowed in union",
3586 field);
3587 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3588 error ("member %q+#D with destructor not allowed in union", field);
3589 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3590 error ("member %q+#D with copy assignment operator not allowed in union",
3591 field);
3592 if (!warned && errorcount > oldcount)
3594 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3595 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3596 warned = true;
3599 else
3601 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3602 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3603 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3604 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3605 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3606 || !TYPE_HAS_COPY_ASSIGN (type));
3607 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3608 || !TYPE_HAS_COPY_CTOR (type));
3609 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3610 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3611 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3612 || TYPE_HAS_COMPLEX_DFLT (type));
3615 if (TYPE_HAS_COPY_CTOR (type)
3616 && !TYPE_HAS_CONST_COPY_CTOR (type))
3617 *cant_have_const_ctor = 1;
3619 if (TYPE_HAS_COPY_ASSIGN (type)
3620 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3621 *no_const_asn_ref = 1;
3624 check_abi_tags (t, field);
3626 if (DECL_INITIAL (field) != NULL_TREE)
3627 /* `build_class_init_list' does not recognize
3628 non-FIELD_DECLs. */
3629 any_default_members = true;
3631 return any_default_members;
3634 /* Check the data members (both static and non-static), class-scoped
3635 typedefs, etc., appearing in the declaration of T. Issue
3636 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3637 declaration order) of access declarations; each TREE_VALUE in this
3638 list is a USING_DECL.
3640 In addition, set the following flags:
3642 EMPTY_P
3643 The class is empty, i.e., contains no non-static data members.
3645 CANT_HAVE_CONST_CTOR_P
3646 This class cannot have an implicitly generated copy constructor
3647 taking a const reference.
3649 CANT_HAVE_CONST_ASN_REF
3650 This class cannot have an implicitly generated assignment
3651 operator taking a const reference.
3653 All of these flags should be initialized before calling this
3654 function. */
3656 static void
3657 check_field_decls (tree t, tree *access_decls,
3658 int *cant_have_const_ctor_p,
3659 int *no_const_asn_ref_p)
3661 int cant_pack = 0;
3663 /* Assume there are no access declarations. */
3664 *access_decls = NULL_TREE;
3665 /* Effective C has things to say about classes with pointer members. */
3666 tree pointer_member = NULL_TREE;
3667 /* Default initialized members affect the whole class. */
3668 tree default_init_member = NULL_TREE;
3669 /* Lack of any non-static data member of non-volatile literal
3670 type affects a union. */
3671 bool found_nv_literal_p = false;
3672 /* Standard layout requires all FIELDS have same access. */
3673 int field_access = -1;
3675 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3677 tree type = TREE_TYPE (field);
3679 switch (TREE_CODE (field))
3681 default:
3682 gcc_unreachable ();
3684 case USING_DECL:
3685 /* Save the access declarations for our caller. */
3686 *access_decls = tree_cons (NULL_TREE, field, *access_decls);
3687 break;
3689 case TYPE_DECL:
3690 case TEMPLATE_DECL:
3691 break;
3693 case FUNCTION_DECL:
3694 /* FIXME: We should fold in the checking from check_methods. */
3695 break;
3697 case CONST_DECL:
3698 DECL_NONLOCAL (field) = 1;
3699 break;
3701 case VAR_DECL:
3702 if (TREE_CODE (t) == UNION_TYPE
3703 && cxx_dialect < cxx11)
3705 /* [class.union]
3707 (C++98) If a union contains a static data member,
3708 ... the program is ill-formed. */
3709 if (cxx_dialect < cxx11)
3710 error ("in C++98 %q+D may not be static because it is "
3711 "a member of a union", field);
3713 goto data_member;
3715 case FIELD_DECL:
3716 if (TREE_CODE (t) == UNION_TYPE)
3718 /* [class.union]
3720 If a union contains ... or a [non-static data] member
3721 of reference type, the program is ill-formed. */
3722 if (TYPE_REF_P (type))
3723 error ("non-static data member %q+D in a union may not "
3724 "have reference type %qT", field, type);
3727 data_member:
3728 /* Common VAR_DECL & FIELD_DECL processing. */
3729 DECL_CONTEXT (field) = t;
3730 DECL_NONLOCAL (field) = 1;
3732 /* Template instantiation can cause this. Perhaps this
3733 should be a specific instantiation check? */
3734 if (TREE_CODE (type) == FUNCTION_TYPE)
3736 error ("data member %q+D invalidly declared function type", field);
3737 type = build_pointer_type (type);
3738 TREE_TYPE (field) = type;
3740 else if (TREE_CODE (type) == METHOD_TYPE)
3742 error ("data member %q+D invalidly declared method type", field);
3743 type = build_pointer_type (type);
3744 TREE_TYPE (field) = type;
3747 break;
3750 if (TREE_CODE (field) != FIELD_DECL)
3751 continue;
3753 if (type == error_mark_node)
3754 continue;
3756 /* If it is not a union and at least one non-static data member is
3757 non-literal, the whole class becomes non-literal. Per Core/1453,
3758 volatile non-static data members and base classes are also not allowed.
3759 If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
3760 members.
3761 Note: if the type is incomplete we will complain later on. */
3762 if (COMPLETE_TYPE_P (type))
3764 if (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type))
3765 CLASSTYPE_LITERAL_P (t) = false;
3766 else
3767 found_nv_literal_p = true;
3770 int this_field_access = (TREE_PROTECTED (field) ? 1
3771 : TREE_PRIVATE (field) ? 2 : 0);
3772 if (field_access != this_field_access)
3774 /* A standard-layout class is a class that:
3776 ... has the same access control (Clause 11) for all
3777 non-static data members, */
3778 if (field_access < 0)
3779 field_access = this_field_access;
3780 else
3781 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3783 /* Aggregates must be public. */
3784 if (this_field_access)
3785 CLASSTYPE_NON_AGGREGATE (t) = 1;
3788 /* If this is of reference type, check if it needs an init. */
3789 if (TYPE_REF_P (type))
3791 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3792 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3793 if (DECL_INITIAL (field) == NULL_TREE)
3794 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3795 if (cxx_dialect < cxx11)
3797 /* ARM $12.6.2: [A member initializer list] (or, for an
3798 aggregate, initialization by a brace-enclosed list) is the
3799 only way to initialize non-static const and reference
3800 members. */
3801 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3802 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3806 type = strip_array_types (type);
3808 if (TYPE_PACKED (t))
3810 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3812 warning_at (DECL_SOURCE_LOCATION (field), 0,
3813 "ignoring packed attribute because of"
3814 " unpacked non-POD field %q#D", field);
3815 cant_pack = 1;
3817 else if (DECL_C_BIT_FIELD (field)
3818 || TYPE_ALIGN (TREE_TYPE (field)) > BITS_PER_UNIT)
3819 DECL_PACKED (field) = 1;
3822 if (DECL_C_BIT_FIELD (field)
3823 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field)))
3824 /* We don't treat zero-width bitfields as making a class
3825 non-empty. */
3827 else if (field_poverlapping_p (field)
3828 && is_empty_class (TREE_TYPE (field)))
3829 /* Empty data members also don't make a class non-empty. */
3830 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3831 else
3833 /* The class is non-empty. */
3834 CLASSTYPE_EMPTY_P (t) = 0;
3835 /* The class is not even nearly empty. */
3836 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3837 /* If one of the data members contains an empty class, so
3838 does T. */
3839 if (CLASS_TYPE_P (type)
3840 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3841 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3844 /* This is used by -Weffc++ (see below). Warn only for pointers
3845 to members which might hold dynamic memory. So do not warn
3846 for pointers to functions or pointers to members. */
3847 if (TYPE_PTR_P (type)
3848 && !TYPE_PTRFN_P (type))
3849 pointer_member = field;
3851 if (CLASS_TYPE_P (type))
3853 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3854 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3855 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3856 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3859 if (DECL_MUTABLE_P (field) || TYPE_HAS_MUTABLE_P (type))
3860 CLASSTYPE_HAS_MUTABLE (t) = 1;
3862 if (DECL_MUTABLE_P (field))
3864 if (TYPE_REF_P (type))
3865 error ("member %q+D cannot be declared as a %<mutable%> "
3866 "reference", field);
3867 else if (CP_TYPE_CONST_P (type))
3868 error ("member %q+D cannot be declared both %<const%> "
3869 "and %<mutable%>", field);
3872 if (! layout_pod_type_p (type))
3873 /* DR 148 now allows pointers to members (which are POD themselves),
3874 to be allowed in POD structs. */
3875 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3877 if (field_poverlapping_p (field))
3878 /* A potentially-overlapping non-static data member makes the class
3879 non-layout-POD. */
3880 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3882 if (!std_layout_type_p (type))
3883 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3885 if (! zero_init_p (type))
3886 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3888 /* We set DECL_C_BIT_FIELD in grokbitfield.
3889 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3890 if (DECL_C_BIT_FIELD (field))
3891 check_bitfield_decl (field);
3893 if (check_field_decl (field, t,
3894 cant_have_const_ctor_p, no_const_asn_ref_p))
3896 if (default_init_member
3897 && TREE_CODE (t) == UNION_TYPE)
3899 error ("multiple fields in union %qT initialized", t);
3900 inform (DECL_SOURCE_LOCATION (default_init_member),
3901 "initialized member %q+D declared here",
3902 default_init_member);
3904 default_init_member = field;
3907 /* Now that we've removed bit-field widths from DECL_INITIAL,
3908 anything left in DECL_INITIAL is an NSDMI that makes the class
3909 non-aggregate in C++11, and non-layout-POD always. */
3910 if (DECL_INITIAL (field))
3912 if (cxx_dialect < cxx14)
3913 CLASSTYPE_NON_AGGREGATE (t) = true;
3914 else
3915 CLASSTYPE_NON_POD_AGGREGATE (t) = true;
3918 if (CP_TYPE_CONST_P (type))
3920 /* If any field is const, the structure type is pseudo-const. */
3921 C_TYPE_FIELDS_READONLY (t) = 1;
3922 if (DECL_INITIAL (field) == NULL_TREE)
3923 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3924 if (cxx_dialect < cxx11)
3926 /* ARM $12.6.2: [A member initializer list] (or, for an
3927 aggregate, initialization by a brace-enclosed list) is the
3928 only way to initialize non-static const and reference
3929 members. */
3930 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3931 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3934 /* A field that is pseudo-const makes the structure likewise. */
3935 else if (CLASS_TYPE_P (type))
3937 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3938 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3939 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3940 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3943 /* Core issue 80: A non-static data member is required to have a
3944 different name from the class iff the class has a
3945 user-declared constructor. */
3946 if (constructor_name_p (DECL_NAME (field), t)
3947 && TYPE_HAS_USER_CONSTRUCTOR (t))
3948 permerror (DECL_SOURCE_LOCATION (field),
3949 "field %q#D with same name as class", field);
3952 /* Per CWG 2096, a type is a literal type if it is a union, and at least
3953 one of its non-static data members is of non-volatile literal type. */
3954 if (TREE_CODE (t) == UNION_TYPE && found_nv_literal_p)
3955 CLASSTYPE_LITERAL_P (t) = true;
3957 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3958 it should also define a copy constructor and an assignment operator to
3959 implement the correct copy semantic (deep vs shallow, etc.). As it is
3960 not feasible to check whether the constructors do allocate dynamic memory
3961 and store it within members, we approximate the warning like this:
3963 -- Warn only if there are members which are pointers
3964 -- Warn only if there is a non-trivial constructor (otherwise,
3965 there cannot be memory allocated).
3966 -- Warn only if there is a non-trivial destructor. We assume that the
3967 user at least implemented the cleanup correctly, and a destructor
3968 is needed to free dynamic memory.
3970 This seems enough for practical purposes. */
3971 if (warn_ecpp
3972 && pointer_member
3973 && TYPE_HAS_USER_CONSTRUCTOR (t)
3974 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3975 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3977 if (warning (OPT_Weffc__, "%q#T has pointer data members", t))
3979 if (! TYPE_HAS_COPY_CTOR (t))
3981 warning (OPT_Weffc__,
3982 " but does not declare %<%T(const %T&)%>", t, t);
3983 if (!TYPE_HAS_COPY_ASSIGN (t))
3984 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3986 else if (! TYPE_HAS_COPY_ASSIGN (t))
3987 warning (OPT_Weffc__,
3988 " but does not declare %<operator=(const %T&)%>", t);
3989 inform (DECL_SOURCE_LOCATION (pointer_member),
3990 "pointer member %q+D declared here", pointer_member);
3994 /* Non-static data member initializers make the default constructor
3995 non-trivial. */
3996 if (default_init_member)
3998 TYPE_NEEDS_CONSTRUCTING (t) = true;
3999 TYPE_HAS_COMPLEX_DFLT (t) = true;
4002 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4003 if (cant_pack)
4004 TYPE_PACKED (t) = 0;
4006 /* Check anonymous struct/anonymous union fields. */
4007 finish_struct_anon (t);
4009 /* We've built up the list of access declarations in reverse order.
4010 Fix that now. */
4011 *access_decls = nreverse (*access_decls);
4014 /* If TYPE is an empty class type, records its OFFSET in the table of
4015 OFFSETS. */
4017 static int
4018 record_subobject_offset (tree type, tree offset, splay_tree offsets)
4020 splay_tree_node n;
4022 if (!is_empty_class (type))
4023 return 0;
4025 /* Record the location of this empty object in OFFSETS. */
4026 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4027 if (!n)
4028 n = splay_tree_insert (offsets,
4029 (splay_tree_key) offset,
4030 (splay_tree_value) NULL_TREE);
4031 n->value = ((splay_tree_value)
4032 tree_cons (NULL_TREE,
4033 type,
4034 (tree) n->value));
4036 return 0;
4039 /* Returns nonzero if TYPE is an empty class type and there is
4040 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4042 static int
4043 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4045 splay_tree_node n;
4046 tree t;
4048 if (!is_empty_class (type))
4049 return 0;
4051 /* Record the location of this empty object in OFFSETS. */
4052 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4053 if (!n)
4054 return 0;
4056 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4057 if (same_type_p (TREE_VALUE (t), type))
4058 return 1;
4060 return 0;
4063 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4064 F for every subobject, passing it the type, offset, and table of
4065 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4066 be traversed.
4068 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4069 than MAX_OFFSET will not be walked.
4071 If F returns a nonzero value, the traversal ceases, and that value
4072 is returned. Otherwise, returns zero. */
4074 static int
4075 walk_subobject_offsets (tree type,
4076 subobject_offset_fn f,
4077 tree offset,
4078 splay_tree offsets,
4079 tree max_offset,
4080 int vbases_p)
4082 int r = 0;
4083 tree type_binfo = NULL_TREE;
4085 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4086 stop. */
4087 if (max_offset && tree_int_cst_lt (max_offset, offset))
4088 return 0;
4090 if (type == error_mark_node)
4091 return 0;
4093 if (!TYPE_P (type))
4095 type_binfo = type;
4096 type = BINFO_TYPE (type);
4099 if (CLASS_TYPE_P (type))
4101 tree field;
4102 tree binfo;
4103 int i;
4105 /* Avoid recursing into objects that are not interesting. */
4106 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4107 return 0;
4109 /* Record the location of TYPE. */
4110 r = (*f) (type, offset, offsets);
4111 if (r)
4112 return r;
4114 /* Iterate through the direct base classes of TYPE. */
4115 if (!type_binfo)
4116 type_binfo = TYPE_BINFO (type);
4117 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4119 tree binfo_offset;
4121 if (BINFO_VIRTUAL_P (binfo))
4122 continue;
4124 tree orig_binfo;
4125 /* We cannot rely on BINFO_OFFSET being set for the base
4126 class yet, but the offsets for direct non-virtual
4127 bases can be calculated by going back to the TYPE. */
4128 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4129 binfo_offset = size_binop (PLUS_EXPR,
4130 offset,
4131 BINFO_OFFSET (orig_binfo));
4133 r = walk_subobject_offsets (binfo,
4135 binfo_offset,
4136 offsets,
4137 max_offset,
4138 /*vbases_p=*/0);
4139 if (r)
4140 return r;
4143 if (CLASSTYPE_VBASECLASSES (type))
4145 unsigned ix;
4146 vec<tree, va_gc> *vbases;
4148 /* Iterate through the virtual base classes of TYPE. In G++
4149 3.2, we included virtual bases in the direct base class
4150 loop above, which results in incorrect results; the
4151 correct offsets for virtual bases are only known when
4152 working with the most derived type. */
4153 if (vbases_p)
4154 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4155 vec_safe_iterate (vbases, ix, &binfo); ix++)
4157 r = walk_subobject_offsets (binfo,
4159 size_binop (PLUS_EXPR,
4160 offset,
4161 BINFO_OFFSET (binfo)),
4162 offsets,
4163 max_offset,
4164 /*vbases_p=*/0);
4165 if (r)
4166 return r;
4168 else
4170 /* We still have to walk the primary base, if it is
4171 virtual. (If it is non-virtual, then it was walked
4172 above.) */
4173 tree vbase = get_primary_binfo (type_binfo);
4175 if (vbase && BINFO_VIRTUAL_P (vbase)
4176 && BINFO_PRIMARY_P (vbase)
4177 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4179 r = (walk_subobject_offsets
4180 (vbase, f, offset,
4181 offsets, max_offset, /*vbases_p=*/0));
4182 if (r)
4183 return r;
4188 /* Iterate through the fields of TYPE. */
4189 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4190 if (TREE_CODE (field) == FIELD_DECL
4191 && TREE_TYPE (field) != error_mark_node
4192 && !DECL_ARTIFICIAL (field))
4194 tree field_offset;
4196 field_offset = byte_position (field);
4198 r = walk_subobject_offsets (TREE_TYPE (field),
4200 size_binop (PLUS_EXPR,
4201 offset,
4202 field_offset),
4203 offsets,
4204 max_offset,
4205 /*vbases_p=*/1);
4206 if (r)
4207 return r;
4210 else if (TREE_CODE (type) == ARRAY_TYPE)
4212 tree element_type = strip_array_types (type);
4213 tree domain = TYPE_DOMAIN (type);
4214 tree index;
4216 /* Avoid recursing into objects that are not interesting. */
4217 if (!CLASS_TYPE_P (element_type)
4218 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4219 || !domain
4220 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4221 return 0;
4223 /* Step through each of the elements in the array. */
4224 for (index = size_zero_node;
4225 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4226 index = size_binop (PLUS_EXPR, index, size_one_node))
4228 r = walk_subobject_offsets (TREE_TYPE (type),
4230 offset,
4231 offsets,
4232 max_offset,
4233 /*vbases_p=*/1);
4234 if (r)
4235 return r;
4236 offset = size_binop (PLUS_EXPR, offset,
4237 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4238 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4239 there's no point in iterating through the remaining
4240 elements of the array. */
4241 if (max_offset && tree_int_cst_lt (max_offset, offset))
4242 break;
4246 return 0;
4249 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4251 static bool
4252 field_poverlapping_p (tree decl)
4254 /* Base fields are actually potentially overlapping, but C++ bases go through
4255 a different code path based on binfos, and ObjC++ base fields are laid out
4256 in objc-act, so we don't want layout_class_type to mess with them. */
4257 if (DECL_FIELD_IS_BASE (decl))
4259 gcc_checking_assert (c_dialect_objc ());
4260 return false;
4263 return lookup_attribute ("no_unique_address",
4264 DECL_ATTRIBUTES (decl));
4267 /* Return true iff DECL is an empty field, either for an empty base or a
4268 [[no_unique_address]] data member. */
4270 bool
4271 is_empty_field (tree decl)
4273 if (!decl || TREE_CODE (decl) != FIELD_DECL)
4274 return false;
4276 bool r = (is_empty_class (TREE_TYPE (decl))
4277 && (DECL_FIELD_IS_BASE (decl)
4278 || field_poverlapping_p (decl)));
4280 /* Empty fields should have size zero. */
4281 gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl)));
4283 return r;
4286 /* Record all of the empty subobjects of DECL_OR_BINFO. */
4288 static void
4289 record_subobject_offsets (tree decl_or_binfo,
4290 splay_tree offsets)
4292 tree type, offset;
4293 bool overlapping, vbases_p;
4295 if (DECL_P (decl_or_binfo))
4297 tree decl = decl_or_binfo;
4298 type = TREE_TYPE (decl);
4299 offset = byte_position (decl);
4300 overlapping = field_poverlapping_p (decl);
4301 vbases_p = true;
4303 else
4305 type = BINFO_TYPE (decl_or_binfo);
4306 offset = BINFO_OFFSET (decl_or_binfo);
4307 overlapping = true;
4308 vbases_p = false;
4311 tree max_offset;
4312 /* If recording subobjects for a non-static data member or a
4313 non-empty base class, we do not need to record offsets beyond
4314 the size of the biggest empty class. Additional data members
4315 will go at the end of the class. Additional base classes will go
4316 either at offset zero (if empty, in which case they cannot
4317 overlap with offsets past the size of the biggest empty class) or
4318 at the end of the class.
4320 However, if we are placing an empty base class, then we must record
4321 all offsets, as either the empty class is at offset zero (where
4322 other empty classes might later be placed) or at the end of the
4323 class (where other objects might then be placed, so other empty
4324 subobjects might later overlap). */
4325 if (!overlapping
4326 || !is_empty_class (type))
4327 max_offset = sizeof_biggest_empty_class;
4328 else
4329 max_offset = NULL_TREE;
4330 walk_subobject_offsets (type, record_subobject_offset, offset,
4331 offsets, max_offset, vbases_p);
4334 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4335 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4336 virtual bases of TYPE are examined. */
4338 static int
4339 layout_conflict_p (tree type,
4340 tree offset,
4341 splay_tree offsets,
4342 int vbases_p)
4344 splay_tree_node max_node;
4346 /* Get the node in OFFSETS that indicates the maximum offset where
4347 an empty subobject is located. */
4348 max_node = splay_tree_max (offsets);
4349 /* If there aren't any empty subobjects, then there's no point in
4350 performing this check. */
4351 if (!max_node)
4352 return 0;
4354 return walk_subobject_offsets (type, check_subobject_offset, offset,
4355 offsets, (tree) (max_node->key),
4356 vbases_p);
4359 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4360 non-static data member of the type indicated by RLI. BINFO is the
4361 binfo corresponding to the base subobject, OFFSETS maps offsets to
4362 types already located at those offsets. This function determines
4363 the position of the DECL. */
4365 static void
4366 layout_nonempty_base_or_field (record_layout_info rli,
4367 tree decl,
4368 tree binfo,
4369 splay_tree offsets)
4371 tree offset = NULL_TREE;
4372 bool field_p;
4373 tree type;
4375 if (binfo)
4377 /* For the purposes of determining layout conflicts, we want to
4378 use the class type of BINFO; TREE_TYPE (DECL) will be the
4379 CLASSTYPE_AS_BASE version, which does not contain entries for
4380 zero-sized bases. */
4381 type = TREE_TYPE (binfo);
4382 field_p = false;
4384 else
4386 type = TREE_TYPE (decl);
4387 field_p = true;
4390 /* Try to place the field. It may take more than one try if we have
4391 a hard time placing the field without putting two objects of the
4392 same type at the same address. */
4393 while (1)
4395 struct record_layout_info_s old_rli = *rli;
4397 /* Place this field. */
4398 place_field (rli, decl);
4399 offset = byte_position (decl);
4401 /* We have to check to see whether or not there is already
4402 something of the same type at the offset we're about to use.
4403 For example, consider:
4405 struct S {};
4406 struct T : public S { int i; };
4407 struct U : public S, public T {};
4409 Here, we put S at offset zero in U. Then, we can't put T at
4410 offset zero -- its S component would be at the same address
4411 as the S we already allocated. So, we have to skip ahead.
4412 Since all data members, including those whose type is an
4413 empty class, have nonzero size, any overlap can happen only
4414 with a direct or indirect base-class -- it can't happen with
4415 a data member. */
4416 /* In a union, overlap is permitted; all members are placed at
4417 offset zero. */
4418 if (TREE_CODE (rli->t) == UNION_TYPE)
4419 break;
4420 if (layout_conflict_p (field_p ? type : binfo, offset,
4421 offsets, field_p))
4423 /* Strip off the size allocated to this field. That puts us
4424 at the first place we could have put the field with
4425 proper alignment. */
4426 *rli = old_rli;
4428 /* Bump up by the alignment required for the type. */
4429 rli->bitpos
4430 = size_binop (PLUS_EXPR, rli->bitpos,
4431 bitsize_int (binfo
4432 ? CLASSTYPE_ALIGN (type)
4433 : TYPE_ALIGN (type)));
4434 normalize_rli (rli);
4436 else if (TREE_CODE (type) == NULLPTR_TYPE
4437 && warn_abi && abi_version_crosses (9))
4439 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4440 the offset wasn't aligned like a pointer when we started to
4441 layout this field, that affects its position. */
4442 tree pos = rli_size_unit_so_far (&old_rli);
4443 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4445 if (abi_version_at_least (9))
4446 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4447 "alignment of %qD increased in %<-fabi-version=9%> "
4448 "(GCC 5.2)", decl);
4449 else
4450 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4451 "of %qD will increase in %<-fabi-version=9%>",
4452 decl);
4454 break;
4456 else
4457 /* There was no conflict. We're done laying out this field. */
4458 break;
4461 /* Now that we know where it will be placed, update its
4462 BINFO_OFFSET. */
4463 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4464 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4465 this point because their BINFO_OFFSET is copied from another
4466 hierarchy. Therefore, we may not need to add the entire
4467 OFFSET. */
4468 propagate_binfo_offsets (binfo,
4469 size_diffop_loc (input_location,
4470 fold_convert (ssizetype, offset),
4471 fold_convert (ssizetype,
4472 BINFO_OFFSET (binfo))));
4475 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4477 static int
4478 empty_base_at_nonzero_offset_p (tree type,
4479 tree offset,
4480 splay_tree /*offsets*/)
4482 return is_empty_class (type) && !integer_zerop (offset);
4485 /* Layout the empty base BINFO. EOC indicates the byte currently just
4486 past the end of the class, and should be correctly aligned for a
4487 class of the type indicated by BINFO; OFFSETS gives the offsets of
4488 the empty bases allocated so far. T is the most derived
4489 type. Return nonzero iff we added it at the end. */
4491 static bool
4492 layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl,
4493 splay_tree offsets)
4495 tree alignment;
4496 bool atend = false;
4497 tree binfo = NULL_TREE;
4498 tree decl = NULL_TREE;
4499 tree type;
4500 if (TREE_CODE (binfo_or_decl) == TREE_BINFO)
4502 binfo = binfo_or_decl;
4503 type = BINFO_TYPE (binfo);
4505 else
4507 decl = binfo_or_decl;
4508 type = TREE_TYPE (decl);
4511 /* On some platforms (ARM), even empty classes will not be
4512 byte-aligned. */
4513 tree eoc = round_up_loc (input_location,
4514 rli_size_unit_so_far (rli),
4515 CLASSTYPE_ALIGN_UNIT (type));
4517 /* This routine should only be used for empty classes. */
4518 gcc_assert (is_empty_class (type));
4520 if (decl && DECL_USER_ALIGN (decl))
4521 alignment = size_int (DECL_ALIGN_UNIT (decl));
4522 else
4523 alignment = size_int (CLASSTYPE_ALIGN_UNIT (type));
4525 /* This is an empty base class. We first try to put it at offset
4526 zero. */
4527 tree offset = size_zero_node;
4528 if (TREE_CODE (rli->t) != UNION_TYPE
4529 && layout_conflict_p (type,
4530 offset,
4531 offsets,
4532 /*vbases_p=*/0))
4534 /* That didn't work. Now, we move forward from the next
4535 available spot in the class. */
4536 atend = true;
4537 offset = eoc;
4538 while (1)
4540 if (!layout_conflict_p (type,
4541 offset,
4542 offsets,
4543 /*vbases_p=*/0))
4544 /* We finally found a spot where there's no overlap. */
4545 break;
4547 /* There's overlap here, too. Bump along to the next spot. */
4548 offset = size_binop (PLUS_EXPR, offset, alignment);
4552 if (decl && DECL_USER_ALIGN (decl))
4554 rli->record_align = MAX (rli->record_align, DECL_ALIGN (decl));
4555 if (warn_packed)
4556 rli->unpacked_align = MAX (rli->unpacked_align, DECL_ALIGN (decl));
4557 TYPE_USER_ALIGN (rli->t) = 1;
4559 else if (CLASSTYPE_USER_ALIGN (type))
4561 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
4562 if (warn_packed)
4563 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
4564 TYPE_USER_ALIGN (rli->t) = 1;
4567 if (binfo)
4568 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4569 propagate_binfo_offsets (binfo,
4570 size_diffop (offset, BINFO_OFFSET (binfo)));
4571 else
4573 DECL_FIELD_OFFSET (decl) = offset;
4574 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4575 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4578 return atend;
4581 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4582 fields at NEXT_FIELD, and return it. */
4584 static tree
4585 build_base_field_1 (tree t, tree binfo, tree access, tree *&next_field)
4587 /* Create the FIELD_DECL. */
4588 tree basetype = BINFO_TYPE (binfo);
4589 tree as_base = CLASSTYPE_AS_BASE (basetype);
4590 gcc_assert (as_base);
4591 tree decl = build_decl (input_location, FIELD_DECL, NULL_TREE, as_base);
4593 DECL_ARTIFICIAL (decl) = 1;
4594 DECL_IGNORED_P (decl) = 1;
4595 DECL_FIELD_CONTEXT (decl) = t;
4596 if (is_empty_class (basetype))
4597 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4598 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4599 else
4601 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4602 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4604 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4605 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4606 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4607 DECL_FIELD_IS_BASE (decl) = 1;
4609 if (access == access_private_node)
4610 TREE_PRIVATE (decl) = true;
4611 else if (access == access_protected_node)
4612 TREE_PROTECTED (decl) = true;
4614 /* Add the new FIELD_DECL to the list of fields for T. */
4615 DECL_CHAIN (decl) = *next_field;
4616 *next_field = decl;
4617 next_field = &DECL_CHAIN (decl);
4619 return decl;
4622 /* Layout the base given by BINFO in the class indicated by RLI.
4623 *BASE_ALIGN is a running maximum of the alignments of
4624 any base class. OFFSETS gives the location of empty base
4625 subobjects. T is the most derived type. Return nonzero if the new
4626 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4627 *NEXT_FIELD, unless BINFO is for an empty base class.
4629 Returns the location at which the next field should be inserted. */
4631 static tree *
4632 build_base_field (record_layout_info rli, tree binfo, tree access,
4633 splay_tree offsets, tree *next_field)
4635 tree t = rli->t;
4636 tree basetype = BINFO_TYPE (binfo);
4638 if (!COMPLETE_TYPE_P (basetype))
4639 /* This error is now reported in xref_tag, thus giving better
4640 location information. */
4641 return next_field;
4643 /* Place the base class. */
4644 if (!is_empty_class (basetype))
4646 tree decl;
4648 /* The containing class is non-empty because it has a non-empty
4649 base class. */
4650 CLASSTYPE_EMPTY_P (t) = 0;
4652 /* Create the FIELD_DECL. */
4653 decl = build_base_field_1 (t, binfo, access, next_field);
4655 /* Try to place the field. It may take more than one try if we
4656 have a hard time placing the field without putting two
4657 objects of the same type at the same address. */
4658 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4660 else
4662 bool atend = layout_empty_base_or_field (rli, binfo, offsets);
4663 /* A nearly-empty class "has no proper base class that is empty,
4664 not morally virtual, and at an offset other than zero." */
4665 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4667 if (atend)
4668 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4669 /* The check above (used in G++ 3.2) is insufficient because
4670 an empty class placed at offset zero might itself have an
4671 empty base at a nonzero offset. */
4672 else if (walk_subobject_offsets (basetype,
4673 empty_base_at_nonzero_offset_p,
4674 size_zero_node,
4675 /*offsets=*/NULL,
4676 /*max_offset=*/NULL_TREE,
4677 /*vbases_p=*/true))
4678 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4681 /* We used to not create a FIELD_DECL for empty base classes because of
4682 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4683 be a problem anymore. We need them to handle initialization of C++17
4684 aggregate bases. */
4685 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4687 tree decl = build_base_field_1 (t, binfo, access, next_field);
4688 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4689 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4690 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4691 SET_DECL_FIELD_ABI_IGNORED (decl, 1);
4694 /* An empty virtual base causes a class to be non-empty
4695 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4696 here because that was already done when the virtual table
4697 pointer was created. */
4700 /* Record the offsets of BINFO and its base subobjects. */
4701 record_subobject_offsets (binfo, offsets);
4703 return next_field;
4706 /* Layout all of the non-virtual base classes. Record empty
4707 subobjects in OFFSETS. T is the most derived type. Return nonzero
4708 if the type cannot be nearly empty. The fields created
4709 corresponding to the base classes will be inserted at
4710 *NEXT_FIELD. */
4712 static void
4713 build_base_fields (record_layout_info rli,
4714 splay_tree offsets, tree *next_field)
4716 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4717 subobjects. */
4718 tree t = rli->t;
4719 tree binfo = TYPE_BINFO (t);
4720 int n_baseclasses = BINFO_N_BASE_BINFOS (binfo);
4722 /* The primary base class is always allocated first. */
4723 const tree primary_binfo = CLASSTYPE_PRIMARY_BINFO (t);
4724 if (primary_binfo)
4726 /* We need to walk BINFO_BASE_BINFO to find the access of the primary
4727 base, if it is direct. Indirect base fields are private. */
4728 tree primary_access = access_private_node;
4729 for (int i = 0; i < n_baseclasses; ++i)
4731 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
4732 if (base_binfo == primary_binfo)
4734 primary_access = BINFO_BASE_ACCESS (binfo, i);
4735 break;
4738 next_field = build_base_field (rli, primary_binfo,
4739 primary_access,
4740 offsets, next_field);
4743 /* Now allocate the rest of the bases. */
4744 for (int i = 0; i < n_baseclasses; ++i)
4746 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
4748 /* The primary base was already allocated above, so we don't
4749 need to allocate it again here. */
4750 if (base_binfo == primary_binfo)
4751 continue;
4753 /* Virtual bases are added at the end (a primary virtual base
4754 will have already been added). */
4755 if (BINFO_VIRTUAL_P (base_binfo))
4756 continue;
4758 next_field = build_base_field (rli, base_binfo,
4759 BINFO_BASE_ACCESS (binfo, i),
4760 offsets, next_field);
4764 /* Go through the TYPE_FIELDS of T issuing any appropriate
4765 diagnostics, figuring out which methods override which other
4766 methods, and so forth. */
4768 static void
4769 check_methods (tree t)
4771 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4772 if (DECL_DECLARES_FUNCTION_P (x))
4774 check_for_override (x, t);
4776 if (DECL_PURE_VIRTUAL_P (x)
4777 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4778 error ("initializer specified for non-virtual method %q+D", x);
4779 /* The name of the field is the original field name
4780 Save this in auxiliary field for later overloading. */
4781 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4783 TYPE_POLYMORPHIC_P (t) = 1;
4784 if (DECL_PURE_VIRTUAL_P (x))
4785 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4788 if (!DECL_VIRTUAL_P (x)
4789 && lookup_attribute ("transaction_safe_dynamic",
4790 DECL_ATTRIBUTES (x)))
4791 error_at (DECL_SOURCE_LOCATION (x),
4792 "%<transaction_safe_dynamic%> may only be specified for "
4793 "a virtual function");
4796 /* Check whether the eligible special member functions (P0848) are
4797 user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
4798 has the eligible ones, unless none are eligible; TYPE_FIELDS also contains
4799 ineligible overloads, which is why this needs to be separate from the loop
4800 above. */
4802 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4804 if (TREE_CODE (dtor) == OVERLOAD)
4806 /* P0848: At the end of the definition of a class, overload
4807 resolution is performed among the prospective destructors declared
4808 in that class with an empty argument list to select the destructor
4809 for the class, also known as the selected destructor. The program
4810 is ill-formed if overload resolution fails. */
4811 int viable = 0;
4812 for (tree fn : ovl_range (dtor))
4813 if (constraints_satisfied_p (fn))
4814 ++viable;
4815 gcc_checking_assert (viable != 1);
4817 auto_diagnostic_group d;
4818 if (viable == 0)
4819 error_at (location_of (t), "no viable destructor for %qT", t);
4820 else
4821 error_at (location_of (t), "destructor for %qT is ambiguous", t);
4822 print_candidates (dtor);
4824 /* Arbitrarily prune the overload set to a single function for
4825 sake of error recovery. */
4826 tree *slot = find_member_slot (t, dtor_identifier);
4827 *slot = get_first_fn (dtor);
4829 else if (user_provided_p (dtor))
4830 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
4833 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
4835 if (!user_provided_p (fn))
4836 /* Might be trivial. */;
4837 else if (TREE_CODE (fn) == TEMPLATE_DECL)
4838 /* Templates are never special members. */;
4839 else if (copy_fn_p (fn)
4840 && constraints_satisfied_p (fn))
4841 TYPE_HAS_COMPLEX_COPY_CTOR (t) = true;
4842 else if (move_fn_p (fn)
4843 && constraints_satisfied_p (fn))
4844 TYPE_HAS_COMPLEX_MOVE_CTOR (t) = true;
4847 for (tree fn : ovl_range (get_class_binding_direct (t, assign_op_identifier)))
4849 if (!user_provided_p (fn))
4850 /* Might be trivial. */;
4851 else if (TREE_CODE (fn) == TEMPLATE_DECL)
4852 /* Templates are never special members. */;
4853 else if (copy_fn_p (fn)
4854 && constraints_satisfied_p (fn))
4855 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true;
4856 else if (move_fn_p (fn)
4857 && constraints_satisfied_p (fn))
4858 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = true;
4862 /* FN is constructor, destructor or operator function. Clone the
4863 declaration to create a NAME'd variant. NEED_VTT_PARM_P and
4864 OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
4866 static tree
4867 copy_fndecl_with_name (tree fn, tree name, tree_code code,
4868 bool need_vtt_parm_p, bool omit_inherited_parms_p)
4870 /* Copy the function. */
4871 tree clone = copy_decl (fn);
4872 /* Reset the function name. */
4873 DECL_NAME (clone) = name;
4875 if (flag_concepts)
4876 /* Clone constraints. */
4877 if (tree ci = get_constraints (fn))
4878 set_constraints (clone, copy_node (ci));
4880 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4881 /* There's no pending inline data for this function. */
4882 DECL_PENDING_INLINE_INFO (clone) = NULL;
4883 DECL_PENDING_INLINE_P (clone) = 0;
4885 if (name == base_dtor_identifier)
4887 /* The base-class destructor is not virtual. */
4888 DECL_VIRTUAL_P (clone) = 0;
4889 DECL_VINDEX (clone) = NULL_TREE;
4891 else if (code != ERROR_MARK)
4893 /* Set the operator code. */
4894 const ovl_op_info_t *ovl_op = OVL_OP_INFO (false, code);
4895 DECL_OVERLOADED_OPERATOR_CODE_RAW (clone) = ovl_op->ovl_op_code;
4897 /* The operator could be virtual. */
4898 if (DECL_VIRTUAL_P (clone))
4899 IDENTIFIER_VIRTUAL_P (name) = true;
4902 if (omit_inherited_parms_p)
4903 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4905 /* If there was an in-charge parameter, drop it from the function
4906 type. */
4907 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4909 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4910 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4911 /* Skip the `this' parameter. */
4912 parmtypes = TREE_CHAIN (parmtypes);
4913 /* Skip the in-charge parameter. */
4914 parmtypes = TREE_CHAIN (parmtypes);
4915 /* And the VTT parm, in a complete [cd]tor. */
4916 if (DECL_HAS_VTT_PARM_P (fn) && !need_vtt_parm_p)
4917 parmtypes = TREE_CHAIN (parmtypes);
4918 if (omit_inherited_parms_p)
4920 /* If we're omitting inherited parms, that just leaves the VTT. */
4921 gcc_assert (need_vtt_parm_p);
4922 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4924 TREE_TYPE (clone)
4925 = build_method_type_directly (basetype,
4926 TREE_TYPE (TREE_TYPE (clone)),
4927 parmtypes);
4928 TREE_TYPE (clone)
4929 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4930 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4931 TREE_TYPE (clone)
4932 = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
4935 /* Copy the function parameters. */
4936 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4938 /* Remove the in-charge parameter. */
4939 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4941 DECL_CHAIN (DECL_ARGUMENTS (clone))
4942 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4943 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4946 /* And the VTT parm, in a complete [cd]tor. */
4947 if (DECL_HAS_VTT_PARM_P (fn))
4949 if (need_vtt_parm_p)
4950 DECL_HAS_VTT_PARM_P (clone) = 1;
4951 else
4953 DECL_CHAIN (DECL_ARGUMENTS (clone))
4954 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4955 DECL_HAS_VTT_PARM_P (clone) = 0;
4959 /* A base constructor inheriting from a virtual base doesn't get the
4960 arguments. */
4961 if (omit_inherited_parms_p)
4962 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4964 for (tree parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4966 DECL_CONTEXT (parms) = clone;
4967 cxx_dup_lang_specific_decl (parms);
4970 /* Create the RTL for this function. */
4971 SET_DECL_RTL (clone, NULL);
4973 /* Regardless of the current scope, this is a member function, so
4974 not at namespace scope. */
4975 rest_of_decl_compilation (clone, /*top_level=*/0, at_eof);
4977 return clone;
4980 /* FN is an operator function, create a variant for CODE. */
4982 tree
4983 copy_operator_fn (tree fn, tree_code code)
4985 return copy_fndecl_with_name (fn, ovl_op_identifier (code),
4986 code, false, false);
4989 /* FN is a constructor or destructor. Clone the declaration to create
4990 a specialized in-charge or not-in-charge version, as indicated by
4991 NAME. */
4993 static tree
4994 build_clone (tree fn, tree name, bool need_vtt_parm_p,
4995 bool omit_inherited_parms_p)
4997 tree clone;
4999 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
5000 if (TREE_CODE (fn) == TEMPLATE_DECL)
5002 clone = copy_decl (fn);
5003 DECL_NAME (clone) = name;
5005 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name,
5006 need_vtt_parm_p, omit_inherited_parms_p);
5007 DECL_TEMPLATE_RESULT (clone) = result;
5009 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
5010 DECL_TI_TEMPLATE (result) = clone;
5012 TREE_TYPE (clone) = TREE_TYPE (result);
5014 else
5016 clone = copy_fndecl_with_name (fn, name, ERROR_MARK,
5017 need_vtt_parm_p, omit_inherited_parms_p);
5018 DECL_CLONED_FUNCTION (clone) = fn;
5021 /* Remember where this function came from. */
5022 DECL_ABSTRACT_ORIGIN (clone) = fn;
5024 /* Make it easy to find the CLONE given the FN. Note the
5025 template_result of a template will be chained this way too. */
5026 DECL_CHAIN (clone) = DECL_CHAIN (fn);
5027 DECL_CHAIN (fn) = clone;
5029 return clone;
5032 /* Build the clones of FN, return the number of clones built. These
5033 will be inserted onto DECL_CHAIN of FN. */
5035 void
5036 build_cdtor_clones (tree fn, bool needs_vtt_p, bool base_omits_inherited_p,
5037 bool update_methods)
5039 unsigned count = 0;
5041 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
5043 /* For each constructor, we need two variants: an in-charge version
5044 and a not-in-charge version. */
5045 build_clone (fn, complete_ctor_identifier, false, false);
5046 build_clone (fn, base_ctor_identifier, needs_vtt_p,
5047 base_omits_inherited_p);
5048 count += 2;
5050 else
5052 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
5054 /* For each destructor, we need three variants: an in-charge
5055 version, a not-in-charge version, and an in-charge deleting
5056 version. We clone the deleting version first because that
5057 means it will go second on the TYPE_FIELDS list -- and that
5058 corresponds to the correct layout order in the virtual
5059 function table.
5061 For a non-virtual destructor, we do not build a deleting
5062 destructor. */
5063 if (DECL_VIRTUAL_P (fn))
5065 build_clone (fn, deleting_dtor_identifier, false, false);
5066 count++;
5068 build_clone (fn, complete_dtor_identifier, false, false);
5069 build_clone (fn, base_dtor_identifier, needs_vtt_p, false);
5070 count += 2;
5073 /* The original is now an abstract function that is never
5074 emitted. */
5075 DECL_ABSTRACT_P (fn) = true;
5077 if (update_methods)
5078 for (tree clone = fn; count--;)
5080 clone = DECL_CHAIN (clone);
5081 add_method (DECL_CONTEXT (clone), clone, false);
5085 /* Produce declarations for all appropriate clones of FN. If
5086 UPDATE_METHODS is true, the clones are added to the
5087 CLASSTYPE_MEMBER_VEC. */
5089 void
5090 clone_cdtor (tree fn, bool update_methods)
5092 /* Avoid inappropriate cloning. */
5093 if (DECL_CHAIN (fn)
5094 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
5095 return;
5097 /* Base cdtors need a vtt parm if there are virtual bases. */
5098 bool vtt = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn));
5100 /* Base ctor omits inherited parms it needs a vttparm and inherited
5101 from a virtual nase ctor. */
5102 bool base_omits_inherited = (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
5103 && base_ctor_omit_inherited_parms (fn));
5105 build_cdtor_clones (fn, vtt, base_omits_inherited, update_methods);
5108 /* DECL is an in charge constructor, which is being defined. This will
5109 have had an in class declaration, from whence clones were
5110 declared. An out-of-class definition can specify additional default
5111 arguments. As it is the clones that are involved in overload
5112 resolution, we must propagate the information from the DECL to its
5113 clones. */
5115 void
5116 adjust_clone_args (tree decl)
5118 tree clone;
5120 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
5121 clone = DECL_CHAIN (clone))
5123 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
5124 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
5125 tree decl_parms, clone_parms;
5127 /* Skip the 'this' parameter. */
5128 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
5129 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5131 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
5132 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5133 if (DECL_HAS_VTT_PARM_P (decl))
5134 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5136 clone_parms = orig_clone_parms;
5137 if (DECL_HAS_VTT_PARM_P (clone))
5138 clone_parms = TREE_CHAIN (clone_parms);
5140 for (decl_parms = orig_decl_parms; decl_parms;
5141 decl_parms = TREE_CHAIN (decl_parms),
5142 clone_parms = TREE_CHAIN (clone_parms))
5144 if (clone_parms == void_list_node)
5146 gcc_assert (decl_parms == clone_parms
5147 || ctor_omit_inherited_parms (clone));
5148 break;
5151 gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms),
5152 TREE_VALUE (clone_parms)));
5154 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
5156 /* A default parameter has been added. Adjust the
5157 clone's parameters. */
5158 clone_parms = orig_decl_parms;
5160 if (DECL_HAS_VTT_PARM_P (clone))
5162 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
5163 TREE_VALUE (orig_clone_parms),
5164 clone_parms);
5165 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
5168 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5169 tree type
5170 = build_method_type_directly (basetype,
5171 TREE_TYPE (TREE_TYPE (clone)),
5172 clone_parms);
5173 if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
5174 type = cp_build_type_attribute_variant (type, attrs);
5175 type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
5176 TREE_TYPE (clone) = type;
5178 clone_parms = NULL_TREE;
5179 break;
5182 gcc_assert (!clone_parms || clone_parms == void_list_node);
5186 /* For each of the constructors and destructors in T, create an
5187 in-charge and not-in-charge variant. */
5189 static void
5190 clone_constructors_and_destructors (tree t)
5192 /* We do not need to propagate the usingness to the clone, at this
5193 point that is not needed. */
5194 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5195 clone_cdtor (fn, /*update_methods=*/true);
5197 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5198 clone_cdtor (dtor, /*update_methods=*/true);
5201 /* Deduce noexcept for a destructor DTOR. */
5203 void
5204 deduce_noexcept_on_destructor (tree dtor)
5206 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5207 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5208 noexcept_deferred_spec);
5211 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5212 of TYPE for virtual functions which FNDECL overrides. Return a
5213 mask of the tm attributes found therein. */
5215 static int
5216 look_for_tm_attr_overrides (tree type, tree fndecl)
5218 tree binfo = TYPE_BINFO (type);
5219 tree base_binfo;
5220 int ix, found = 0;
5222 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5224 tree o, basetype = BINFO_TYPE (base_binfo);
5226 if (!TYPE_POLYMORPHIC_P (basetype))
5227 continue;
5229 o = look_for_overrides_here (basetype, fndecl);
5230 if (o)
5232 if (lookup_attribute ("transaction_safe_dynamic",
5233 DECL_ATTRIBUTES (o)))
5234 /* transaction_safe_dynamic is not inherited. */;
5235 else
5236 found |= tm_attr_to_mask (find_tm_attribute
5237 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5239 else
5240 found |= look_for_tm_attr_overrides (basetype, fndecl);
5243 return found;
5246 /* Subroutine of set_method_tm_attributes. Handle the checks and
5247 inheritance for one virtual method FNDECL. */
5249 static void
5250 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5252 tree tm_attr;
5253 int found, have;
5255 found = look_for_tm_attr_overrides (type, fndecl);
5257 /* If FNDECL doesn't actually override anything (i.e. T is the
5258 class that first declares FNDECL virtual), then we're done. */
5259 if (found == 0)
5260 return;
5262 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5263 have = tm_attr_to_mask (tm_attr);
5265 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5266 tm_pure must match exactly, otherwise no weakening of
5267 tm_safe > tm_callable > nothing. */
5268 /* ??? The tm_pure attribute didn't make the transition to the
5269 multivendor language spec. */
5270 if (have == TM_ATTR_PURE)
5272 if (found != TM_ATTR_PURE)
5274 found &= -found;
5275 goto err_override;
5278 /* If the overridden function is tm_pure, then FNDECL must be. */
5279 else if (found == TM_ATTR_PURE && tm_attr)
5280 goto err_override;
5281 /* Look for base class combinations that cannot be satisfied. */
5282 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5284 found &= ~TM_ATTR_PURE;
5285 found &= -found;
5286 error_at (DECL_SOURCE_LOCATION (fndecl),
5287 "method overrides both %<transaction_pure%> and %qE methods",
5288 tm_mask_to_attr (found));
5290 /* If FNDECL did not declare an attribute, then inherit the most
5291 restrictive one. */
5292 else if (tm_attr == NULL)
5294 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5296 /* Otherwise validate that we're not weaker than a function
5297 that is being overridden. */
5298 else
5300 found &= -found;
5301 if (found <= TM_ATTR_CALLABLE && have > found)
5302 goto err_override;
5304 return;
5306 err_override:
5307 error_at (DECL_SOURCE_LOCATION (fndecl),
5308 "method declared %qE overriding %qE method",
5309 tm_attr, tm_mask_to_attr (found));
5312 /* For each of the methods in T, propagate a class-level tm attribute. */
5314 static void
5315 set_method_tm_attributes (tree t)
5317 tree class_tm_attr, fndecl;
5319 /* Don't bother collecting tm attributes if transactional memory
5320 support is not enabled. */
5321 if (!flag_tm)
5322 return;
5324 /* Process virtual methods first, as they inherit directly from the
5325 base virtual function and also require validation of new attributes. */
5326 if (TYPE_CONTAINS_VPTR_P (t))
5328 tree vchain;
5329 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5330 vchain = TREE_CHAIN (vchain))
5332 fndecl = BV_FN (vchain);
5333 if (DECL_THUNK_P (fndecl))
5334 fndecl = THUNK_TARGET (fndecl);
5335 set_one_vmethod_tm_attributes (t, fndecl);
5339 /* If the class doesn't have an attribute, nothing more to do. */
5340 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5341 if (class_tm_attr == NULL)
5342 return;
5344 /* Any method that does not yet have a tm attribute inherits
5345 the one from the class. */
5346 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
5347 if (DECL_DECLARES_FUNCTION_P (fndecl)
5348 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5349 apply_tm_attr (fndecl, class_tm_attr);
5352 /* Returns true if FN is a default constructor. */
5354 bool
5355 default_ctor_p (const_tree fn)
5357 return (DECL_CONSTRUCTOR_P (fn)
5358 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5361 /* Returns true iff class T has a user-provided constructor that can be called
5362 with more than zero arguments. */
5364 bool
5365 type_has_user_nondefault_constructor (tree t)
5367 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5368 return false;
5370 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5372 if (user_provided_p (fn)
5373 && (TREE_CODE (fn) == TEMPLATE_DECL
5374 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5375 != NULL_TREE)))
5376 return true;
5379 return false;
5382 /* Returns the defaulted constructor if T has one. Otherwise, returns
5383 NULL_TREE. */
5385 tree
5386 in_class_defaulted_default_constructor (tree t)
5388 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5389 return NULL_TREE;
5391 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5393 tree fn = *iter;
5395 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5396 && default_ctor_p (fn))
5397 return fn;
5400 return NULL_TREE;
5403 /* Returns true iff FN is a user-provided function, i.e. user-declared
5404 and not defaulted at its first declaration. */
5406 bool
5407 user_provided_p (tree fn)
5409 fn = STRIP_TEMPLATE (fn);
5410 return (!DECL_ARTIFICIAL (fn)
5411 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5412 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5415 /* Returns true iff class T has a user-provided constructor. */
5417 bool
5418 type_has_user_provided_constructor (tree t)
5420 if (!CLASS_TYPE_P (t))
5421 return false;
5423 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5424 return false;
5426 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5427 if (user_provided_p (*iter))
5428 return true;
5430 return false;
5433 /* Returns true iff class T has a user-provided or explicit constructor. */
5435 bool
5436 type_has_user_provided_or_explicit_constructor (tree t)
5438 if (!CLASS_TYPE_P (t))
5439 return false;
5441 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5442 return false;
5444 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5446 tree fn = *iter;
5447 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5448 return true;
5451 return false;
5454 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5455 declared or explicitly defaulted in the class body) default
5456 constructor. */
5458 bool
5459 type_has_non_user_provided_default_constructor (tree t)
5461 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5462 return false;
5463 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5464 return true;
5466 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5468 tree fn = *iter;
5469 if (TREE_CODE (fn) == FUNCTION_DECL
5470 && default_ctor_p (fn)
5471 && !user_provided_p (fn))
5472 return true;
5475 return false;
5478 /* TYPE is being used as a virtual base, and has a non-trivial move
5479 assignment. Return true if this is due to there being a user-provided
5480 move assignment in TYPE or one of its subobjects; if there isn't, then
5481 multiple move assignment can't cause any harm. */
5483 bool
5484 vbase_has_user_provided_move_assign (tree type)
5486 /* Does the type itself have a user-provided move assignment operator? */
5487 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5488 for (ovl_iterator iter (get_class_binding_direct
5489 (type, assign_op_identifier));
5490 iter; ++iter)
5491 if (user_provided_p (*iter) && move_fn_p (*iter))
5492 return true;
5494 /* Do any of its bases? */
5495 tree binfo = TYPE_BINFO (type);
5496 tree base_binfo;
5497 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5498 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5499 return true;
5501 /* Or non-static data members? */
5502 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5504 if (TREE_CODE (field) == FIELD_DECL
5505 && CLASS_TYPE_P (TREE_TYPE (field))
5506 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5507 return true;
5510 /* Seems not. */
5511 return false;
5514 /* If default-initialization leaves part of TYPE uninitialized, returns
5515 a DECL for the field or TYPE itself (DR 253). */
5517 tree
5518 default_init_uninitialized_part (tree type)
5520 tree t, r, binfo;
5521 int i;
5523 type = strip_array_types (type);
5524 if (!CLASS_TYPE_P (type))
5525 return type;
5526 if (!type_has_non_user_provided_default_constructor (type))
5527 return NULL_TREE;
5528 for (binfo = TYPE_BINFO (type), i = 0;
5529 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5531 r = default_init_uninitialized_part (BINFO_TYPE (t));
5532 if (r)
5533 return r;
5535 for (t = next_aggregate_field (TYPE_FIELDS (type)); t;
5536 t = next_aggregate_field (DECL_CHAIN (t)))
5537 if (!DECL_INITIAL (t) && !DECL_ARTIFICIAL (t))
5539 r = default_init_uninitialized_part (TREE_TYPE (t));
5540 if (r)
5541 return DECL_P (r) ? r : t;
5544 return NULL_TREE;
5547 /* Returns true iff for class T, a trivial synthesized default constructor
5548 would be constexpr. */
5550 bool
5551 trivial_default_constructor_is_constexpr (tree t)
5553 /* A defaulted trivial default constructor is constexpr
5554 if there is nothing to initialize. */
5555 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5556 /* A class with a vptr doesn't have a trivial default ctor.
5557 In C++20, a class can have transient uninitialized members, e.g.:
5559 struct S { int i; constexpr S() = default; };
5561 should work. */
5562 return (cxx_dialect >= cxx20
5563 || is_really_empty_class (t, /*ignore_vptr*/true));
5566 /* Returns true iff class T has a constexpr default constructor. */
5568 bool
5569 type_has_constexpr_default_constructor (tree t)
5571 tree fns;
5573 if (!CLASS_TYPE_P (t))
5575 /* The caller should have stripped an enclosing array. */
5576 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5577 return false;
5579 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5581 if (!TYPE_HAS_COMPLEX_DFLT (t))
5582 return trivial_default_constructor_is_constexpr (t);
5583 /* Non-trivial, we need to check subobject constructors. */
5584 lazily_declare_fn (sfk_constructor, t);
5586 fns = locate_ctor (t);
5587 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5590 /* Returns true iff class T has a constexpr default constructor or has an
5591 implicitly declared default constructor that we can't tell if it's constexpr
5592 without forcing a lazy declaration (which might cause undesired
5593 instantiations). */
5595 static bool
5596 type_maybe_constexpr_default_constructor (tree t)
5598 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5599 && TYPE_HAS_COMPLEX_DFLT (t))
5600 /* Assume it's constexpr. */
5601 return true;
5602 return type_has_constexpr_default_constructor (t);
5605 /* Returns true iff class T has a constexpr destructor. */
5607 bool
5608 type_has_constexpr_destructor (tree t)
5610 tree fns;
5612 if (CLASSTYPE_LAZY_DESTRUCTOR (t))
5613 /* Non-trivial, we need to check subobject destructors. */
5614 lazily_declare_fn (sfk_destructor, t);
5615 fns = CLASSTYPE_DESTRUCTOR (t);
5616 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5619 /* Returns true iff class T has a constexpr destructor or has an
5620 implicitly declared destructor that we can't tell if it's constexpr
5621 without forcing a lazy declaration (which might cause undesired
5622 instantiations). */
5624 static bool
5625 type_maybe_constexpr_destructor (tree t)
5627 /* Until C++20, only trivial destruction is constexpr. */
5628 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (t))
5629 return true;
5630 if (cxx_dialect < cxx20)
5631 return false;
5632 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t))
5633 /* Assume it's constexpr. */
5634 return true;
5635 tree fn = CLASSTYPE_DESTRUCTOR (t);
5636 return (fn && maybe_constexpr_fn (fn));
5639 /* Returns true iff class TYPE has a virtual destructor. */
5641 bool
5642 type_has_virtual_destructor (tree type)
5644 tree dtor;
5646 if (!NON_UNION_CLASS_TYPE_P (type))
5647 return false;
5649 gcc_assert (COMPLETE_TYPE_P (type));
5650 dtor = CLASSTYPE_DESTRUCTOR (type);
5651 return (dtor && DECL_VIRTUAL_P (dtor));
5654 /* Returns true iff T, a class, has a move-assignment or
5655 move-constructor. Does not lazily declare either.
5656 If USER_P is false, any move function will do. If it is true, the
5657 move function must be user-declared.
5659 Note that user-declared here is different from "user-provided",
5660 which doesn't include functions that are defaulted in the
5661 class. */
5663 bool
5664 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5666 gcc_assert (user_p
5667 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5668 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5670 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5671 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5672 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5673 return true;
5675 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5676 for (ovl_iterator iter (get_class_binding_direct
5677 (t, assign_op_identifier));
5678 iter; ++iter)
5679 if ((!user_p || !DECL_ARTIFICIAL (*iter))
5680 && DECL_CONTEXT (*iter) == t
5681 && move_fn_p (*iter))
5682 return true;
5684 return false;
5687 /* True iff T has a move constructor that is not deleted. */
5689 bool
5690 classtype_has_non_deleted_move_ctor (tree t)
5692 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5693 lazily_declare_fn (sfk_move_constructor, t);
5694 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5695 if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5696 return true;
5697 return false;
5700 /* If T, a class, has a user-provided copy constructor, copy assignment
5701 operator, or destructor, returns that function. Otherwise, null. */
5703 tree
5704 classtype_has_depr_implicit_copy (tree t)
5706 if (!CLASSTYPE_LAZY_COPY_CTOR (t))
5707 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5709 tree fn = *iter;
5710 if (user_provided_p (fn) && copy_fn_p (fn))
5711 return fn;
5714 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
5715 for (ovl_iterator iter (get_class_binding_direct
5716 (t, assign_op_identifier));
5717 iter; ++iter)
5719 tree fn = *iter;
5720 if (DECL_CONTEXT (fn) == t
5721 && user_provided_p (fn) && copy_fn_p (fn))
5722 return fn;
5725 if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
5727 tree fn = CLASSTYPE_DESTRUCTOR (t);
5728 if (user_provided_p (fn))
5729 return fn;
5732 return NULL_TREE;
5735 /* True iff T has a member or friend declaration of operator OP. */
5737 bool
5738 classtype_has_op (tree t, tree_code op)
5740 tree name = ovl_op_identifier (op);
5741 if (get_class_binding (t, name))
5742 return true;
5743 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
5744 if (FRIEND_NAME (f) == name)
5745 return true;
5746 return false;
5750 /* If T has a defaulted member or friend declaration of OP, return it. */
5752 tree
5753 classtype_has_defaulted_op (tree t, tree_code op)
5755 tree name = ovl_op_identifier (op);
5756 for (ovl_iterator oi (get_class_binding (t, name)); oi; ++oi)
5758 tree fn = *oi;
5759 if (DECL_DEFAULTED_FN (fn))
5760 return fn;
5762 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
5763 if (FRIEND_NAME (f) == name)
5764 for (tree l = FRIEND_DECLS (f); l; l = TREE_CHAIN (l))
5766 tree fn = TREE_VALUE (l);
5767 if (DECL_DEFAULTED_FN (fn))
5768 return fn;
5770 return NULL_TREE;
5773 /* Nonzero if we need to build up a constructor call when initializing an
5774 object of this class, either because it has a user-declared constructor
5775 or because it doesn't have a default constructor (so we need to give an
5776 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5777 what you care about is whether or not an object can be produced by a
5778 constructor (e.g. so we don't set TREE_READONLY on const variables of
5779 such type); use this function when what you care about is whether or not
5780 to try to call a constructor to create an object. The latter case is
5781 the former plus some cases of constructors that cannot be called. */
5783 bool
5784 type_build_ctor_call (tree t)
5786 tree inner;
5787 if (TYPE_NEEDS_CONSTRUCTING (t))
5788 return true;
5789 inner = strip_array_types (t);
5790 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5791 return false;
5792 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5793 return true;
5794 if (cxx_dialect < cxx11)
5795 return false;
5796 /* A user-declared constructor might be private, and a constructor might
5797 be trivial but deleted. */
5798 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5799 iter; ++iter)
5801 tree fn = *iter;
5802 if (!DECL_ARTIFICIAL (fn)
5803 || TREE_DEPRECATED (fn)
5804 || TREE_UNAVAILABLE (fn)
5805 || DECL_DELETED_FN (fn))
5806 return true;
5808 return false;
5811 /* Like type_build_ctor_call, but for destructors. */
5813 bool
5814 type_build_dtor_call (tree t)
5816 tree inner;
5817 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5818 return true;
5819 inner = strip_array_types (t);
5820 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5821 || !COMPLETE_TYPE_P (inner))
5822 return false;
5823 if (cxx_dialect < cxx11)
5824 return false;
5825 /* A user-declared destructor might be private, and a destructor might
5826 be trivial but deleted. */
5827 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5828 iter; ++iter)
5830 tree fn = *iter;
5831 if (!DECL_ARTIFICIAL (fn)
5832 || TREE_DEPRECATED (fn)
5833 || TREE_UNAVAILABLE (fn)
5834 || DECL_DELETED_FN (fn))
5835 return true;
5837 return false;
5840 /* Returns TRUE iff we need a cookie when dynamically allocating an
5841 array whose elements have the indicated class TYPE. */
5843 static bool
5844 type_requires_array_cookie (tree type)
5846 tree fns;
5847 bool has_two_argument_delete_p = false;
5849 gcc_assert (CLASS_TYPE_P (type));
5851 /* If there's a non-trivial destructor, we need a cookie. In order
5852 to iterate through the array calling the destructor for each
5853 element, we'll have to know how many elements there are. */
5854 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5855 return true;
5857 /* If the usual deallocation function is a two-argument whose second
5858 argument is of type `size_t', then we have to pass the size of
5859 the array to the deallocation function, so we will need to store
5860 a cookie. */
5861 fns = lookup_fnfields (TYPE_BINFO (type),
5862 ovl_op_identifier (false, VEC_DELETE_EXPR),
5863 /*protect=*/0, tf_warning_or_error);
5864 /* If there are no `operator []' members, or the lookup is
5865 ambiguous, then we don't need a cookie. */
5866 if (!fns || fns == error_mark_node)
5867 return false;
5868 /* Loop through all of the functions. */
5869 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5871 tree fn = *iter;
5873 /* See if this function is a one-argument delete function. If
5874 it is, then it will be the usual deallocation function. */
5875 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5876 if (second_parm == void_list_node)
5877 return false;
5878 /* Do not consider this function if its second argument is an
5879 ellipsis. */
5880 if (!second_parm)
5881 continue;
5882 /* Otherwise, if we have a two-argument function and the second
5883 argument is `size_t', it will be the usual deallocation
5884 function -- unless there is one-argument function, too. */
5885 if (TREE_CHAIN (second_parm) == void_list_node
5886 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5887 has_two_argument_delete_p = true;
5890 return has_two_argument_delete_p;
5893 /* Finish computing the `literal type' property of class type T.
5895 At this point, we have already processed base classes and
5896 non-static data members. We need to check whether the copy
5897 constructor is trivial, the destructor is trivial, and there
5898 is a trivial default constructor or at least one constexpr
5899 constructor other than the copy constructor. */
5901 static void
5902 finalize_literal_type_property (tree t)
5904 tree fn;
5906 if (cxx_dialect < cxx11)
5907 CLASSTYPE_LITERAL_P (t) = false;
5908 else if (CLASSTYPE_LITERAL_P (t)
5909 && !type_maybe_constexpr_destructor (t))
5910 CLASSTYPE_LITERAL_P (t) = false;
5911 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5912 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5913 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5914 && CLASSTYPE_NON_AGGREGATE (t)
5915 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5916 CLASSTYPE_LITERAL_P (t) = false;
5918 /* C++14 DR 1684 removed this restriction. */
5919 if (cxx_dialect < cxx14
5920 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5921 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5922 if (TREE_CODE (fn) == FUNCTION_DECL
5923 && DECL_DECLARED_CONSTEXPR_P (fn)
5924 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5925 && !DECL_CONSTRUCTOR_P (fn))
5927 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5928 if (!DECL_GENERATED_P (fn))
5930 auto_diagnostic_group d;
5931 if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5932 "enclosing class of %<constexpr%> non-static "
5933 "member function %q+#D is not a literal type", fn))
5934 explain_non_literal_class (t);
5939 /* T is a non-literal type used in a context which requires a constant
5940 expression. Explain why it isn't literal. */
5942 void
5943 explain_non_literal_class (tree t)
5945 static hash_set<tree> *diagnosed;
5947 if (!CLASS_TYPE_P (t))
5948 return;
5949 t = TYPE_MAIN_VARIANT (t);
5951 if (diagnosed == NULL)
5952 diagnosed = new hash_set<tree>;
5953 if (diagnosed->add (t))
5954 /* Already explained. */
5955 return;
5957 auto_diagnostic_group d;
5958 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5959 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5960 inform (UNKNOWN_LOCATION,
5961 " %qT is a closure type, which is only literal in "
5962 "C++17 and later", t);
5963 else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5964 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5965 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
5966 && !type_maybe_constexpr_destructor (t))
5967 inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
5969 else if (CLASSTYPE_NON_AGGREGATE (t)
5970 && !TYPE_HAS_TRIVIAL_DFLT (t)
5971 && !LAMBDA_TYPE_P (t)
5972 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5974 inform (UNKNOWN_LOCATION,
5975 " %q+T is not an aggregate, does not have a trivial "
5976 "default constructor, and has no %<constexpr%> constructor that "
5977 "is not a copy or move constructor", t);
5978 if (type_has_non_user_provided_default_constructor (t))
5979 /* Note that we can't simply call locate_ctor because when the
5980 constructor is deleted it just returns NULL_TREE. */
5981 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5983 tree fn = *iter;
5984 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5986 parms = skip_artificial_parms_for (fn, parms);
5988 if (sufficient_parms_p (parms))
5990 if (DECL_DELETED_FN (fn))
5991 maybe_explain_implicit_delete (fn);
5992 else
5993 explain_invalid_constexpr_fn (fn);
5994 break;
5998 else
6000 tree binfo, base_binfo, field; int i;
6001 for (binfo = TYPE_BINFO (t), i = 0;
6002 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6004 tree basetype = TREE_TYPE (base_binfo);
6005 if (!CLASSTYPE_LITERAL_P (basetype))
6007 inform (UNKNOWN_LOCATION,
6008 " base class %qT of %q+T is non-literal",
6009 basetype, t);
6010 explain_non_literal_class (basetype);
6011 return;
6014 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6016 tree ftype;
6017 if (TREE_CODE (field) != FIELD_DECL)
6018 continue;
6019 ftype = TREE_TYPE (field);
6020 if (!literal_type_p (ftype))
6022 inform (DECL_SOURCE_LOCATION (field),
6023 " non-static data member %qD has non-literal type",
6024 field);
6025 if (CLASS_TYPE_P (ftype))
6026 explain_non_literal_class (ftype);
6028 if (CP_TYPE_VOLATILE_P (ftype))
6029 inform (DECL_SOURCE_LOCATION (field),
6030 " non-static data member %qD has volatile type", field);
6035 /* Check the validity of the bases and members declared in T. Add any
6036 implicitly-generated functions (like copy-constructors and
6037 assignment operators). Compute various flag bits (like
6038 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
6039 level: i.e., independently of the ABI in use. */
6041 static void
6042 check_bases_and_members (tree t)
6044 /* Nonzero if the implicitly generated copy constructor should take
6045 a non-const reference argument. */
6046 int cant_have_const_ctor;
6047 /* Nonzero if the implicitly generated assignment operator
6048 should take a non-const reference argument. */
6049 int no_const_asn_ref;
6050 tree access_decls;
6051 bool saved_complex_asn_ref;
6052 bool saved_nontrivial_dtor;
6053 tree fn;
6055 /* By default, we use const reference arguments and generate default
6056 constructors. */
6057 cant_have_const_ctor = 0;
6058 no_const_asn_ref = 0;
6060 /* Check all the base-classes and set FMEM members to point to arrays
6061 of potential interest. */
6062 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
6064 /* Deduce noexcept on destructor. This needs to happen after we've set
6065 triviality flags appropriately for our bases, and before checking
6066 overriden virtual functions via check_methods. */
6067 if (cxx_dialect >= cxx11)
6068 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
6069 for (tree fn : ovl_range (dtor))
6070 deduce_noexcept_on_destructor (fn);
6072 /* Check all the method declarations. */
6073 check_methods (t);
6075 /* Save the initial values of these flags which only indicate whether
6076 or not the class has user-provided functions. As we analyze the
6077 bases and members we can set these flags for other reasons. */
6078 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
6079 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
6081 /* Check all the data member declarations. We cannot call
6082 check_field_decls until we have called check_bases check_methods,
6083 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6084 being set appropriately. */
6085 check_field_decls (t, &access_decls,
6086 &cant_have_const_ctor,
6087 &no_const_asn_ref);
6089 /* A nearly-empty class has to be vptr-containing; a nearly empty
6090 class contains just a vptr. */
6091 if (!TYPE_CONTAINS_VPTR_P (t))
6092 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
6094 /* Do some bookkeeping that will guide the generation of implicitly
6095 declared member functions. */
6096 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6097 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6098 /* We need to call a constructor for this class if it has a
6099 user-provided constructor, or if the default constructor is going
6100 to initialize the vptr. (This is not an if-and-only-if;
6101 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
6102 themselves need constructing.) */
6103 TYPE_NEEDS_CONSTRUCTING (t)
6104 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
6105 /* [dcl.init.aggr]
6107 An aggregate is an array or a class with no user-provided
6108 constructors ... and no virtual functions.
6110 Again, other conditions for being an aggregate are checked
6111 elsewhere. */
6112 CLASSTYPE_NON_AGGREGATE (t)
6113 |= ((cxx_dialect < cxx20
6114 ? type_has_user_provided_or_explicit_constructor (t)
6115 : TYPE_HAS_USER_CONSTRUCTOR (t))
6116 || TYPE_POLYMORPHIC_P (t));
6117 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6118 retain the old definition internally for ABI reasons. */
6119 CLASSTYPE_NON_LAYOUT_POD_P (t)
6120 |= (CLASSTYPE_NON_AGGREGATE (t)
6121 || saved_nontrivial_dtor || saved_complex_asn_ref);
6122 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
6123 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6124 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6125 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
6127 /* Is this class non-layout-POD because it wasn't an aggregate in C++98? */
6128 if (CLASSTYPE_NON_POD_AGGREGATE (t))
6130 if (CLASSTYPE_NON_LAYOUT_POD_P (t))
6131 /* It's non-POD for another reason. */
6132 CLASSTYPE_NON_POD_AGGREGATE (t) = false;
6133 else if (abi_version_at_least (17))
6134 CLASSTYPE_NON_LAYOUT_POD_P (t) = true;
6137 /* If the only explicitly declared default constructor is user-provided,
6138 set TYPE_HAS_COMPLEX_DFLT. */
6139 if (!TYPE_HAS_COMPLEX_DFLT (t)
6140 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
6141 && !type_has_non_user_provided_default_constructor (t))
6142 TYPE_HAS_COMPLEX_DFLT (t) = true;
6144 /* Warn if a public base of a polymorphic type has an accessible
6145 non-virtual destructor. It is only now that we know the class is
6146 polymorphic. Although a polymorphic base will have a already
6147 been diagnosed during its definition, we warn on use too. */
6148 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
6150 tree binfo = TYPE_BINFO (t);
6151 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
6152 tree base_binfo;
6153 unsigned i;
6155 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6157 tree basetype = TREE_TYPE (base_binfo);
6159 if ((*accesses)[i] == access_public_node
6160 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
6161 && accessible_nvdtor_p (basetype))
6162 warning (OPT_Wnon_virtual_dtor,
6163 "base class %q#T has accessible non-virtual destructor",
6164 basetype);
6168 /* If the class has no user-declared constructor, but does have
6169 non-static const or reference data members that can never be
6170 initialized, issue a warning. */
6171 if (warn_uninitialized
6172 /* Classes with user-declared constructors are presumed to
6173 initialize these members. */
6174 && !TYPE_HAS_USER_CONSTRUCTOR (t)
6175 /* Aggregates can be initialized with brace-enclosed
6176 initializers. */
6177 && CLASSTYPE_NON_AGGREGATE (t))
6179 tree field;
6181 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6183 tree type;
6185 if (TREE_CODE (field) != FIELD_DECL
6186 || DECL_INITIAL (field) != NULL_TREE)
6187 continue;
6189 type = TREE_TYPE (field);
6190 if (TYPE_REF_P (type))
6191 warning_at (DECL_SOURCE_LOCATION (field),
6192 OPT_Wuninitialized, "non-static reference %q#D "
6193 "in class without a constructor", field);
6194 else if (CP_TYPE_CONST_P (type)
6195 && (!CLASS_TYPE_P (type)
6196 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6197 warning_at (DECL_SOURCE_LOCATION (field),
6198 OPT_Wuninitialized, "non-static const member %q#D "
6199 "in class without a constructor", field);
6203 /* Synthesize any needed methods. */
6204 add_implicitly_declared_members (t, &access_decls,
6205 cant_have_const_ctor,
6206 no_const_asn_ref);
6208 /* Check defaulted declarations here so we have cant_have_const_ctor
6209 and don't need to worry about clones. */
6210 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6211 if (DECL_DECLARES_FUNCTION_P (fn)
6212 && !DECL_ARTIFICIAL (fn)
6213 && DECL_DEFAULTED_IN_CLASS_P (fn))
6215 /* ...except handle comparisons later, in finish_struct_1. */
6216 if (special_function_p (fn) == sfk_comparison)
6217 continue;
6219 int copy = copy_fn_p (fn);
6220 if (copy > 0)
6222 bool imp_const_p
6223 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6224 : !no_const_asn_ref);
6225 bool fn_const_p = (copy == 2);
6227 if (fn_const_p && !imp_const_p)
6228 /* If the function is defaulted outside the class, we just
6229 give the synthesis error. Core Issue #1331 says this is
6230 no longer ill-formed, it is defined as deleted instead. */
6231 DECL_DELETED_FN (fn) = true;
6233 defaulted_late_check (fn);
6236 if (LAMBDA_TYPE_P (t))
6237 /* "This class type is not an aggregate." */
6238 CLASSTYPE_NON_AGGREGATE (t) = 1;
6240 /* Compute the 'literal type' property before we
6241 do anything with non-static member functions. */
6242 finalize_literal_type_property (t);
6244 /* Create the in-charge and not-in-charge variants of constructors
6245 and destructors. */
6246 clone_constructors_and_destructors (t);
6248 /* Process the using-declarations. */
6249 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6250 handle_using_decl (TREE_VALUE (access_decls), t);
6252 /* Figure out whether or not we will need a cookie when dynamically
6253 allocating an array of this type. */
6254 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6255 = type_requires_array_cookie (t);
6258 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6259 accordingly. If a new vfield was created (because T doesn't have a
6260 primary base class), then the newly created field is returned. It
6261 is not added to the TYPE_FIELDS list; it is the caller's
6262 responsibility to do that. Accumulate declared virtual functions
6263 on VIRTUALS_P. */
6265 static tree
6266 create_vtable_ptr (tree t, tree* virtuals_p)
6268 tree fn;
6270 /* Collect the virtual functions declared in T. */
6271 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6272 if (TREE_CODE (fn) == FUNCTION_DECL
6273 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6274 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6276 tree new_virtual = make_node (TREE_LIST);
6278 BV_FN (new_virtual) = fn;
6279 BV_DELTA (new_virtual) = integer_zero_node;
6280 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6282 TREE_CHAIN (new_virtual) = *virtuals_p;
6283 *virtuals_p = new_virtual;
6286 /* If we couldn't find an appropriate base class, create a new field
6287 here. Even if there weren't any new virtual functions, we might need a
6288 new virtual function table if we're supposed to include vptrs in
6289 all classes that need them. */
6290 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6292 /* We build this decl with vtbl_ptr_type_node, which is a
6293 `vtable_entry_type*'. It might seem more precise to use
6294 `vtable_entry_type (*)[N]' where N is the number of virtual
6295 functions. However, that would require the vtable pointer in
6296 base classes to have a different type than the vtable pointer
6297 in derived classes. We could make that happen, but that
6298 still wouldn't solve all the problems. In particular, the
6299 type-based alias analysis code would decide that assignments
6300 to the base class vtable pointer can't alias assignments to
6301 the derived class vtable pointer, since they have different
6302 types. Thus, in a derived class destructor, where the base
6303 class constructor was inlined, we could generate bad code for
6304 setting up the vtable pointer.
6306 Therefore, we use one type for all vtable pointers. We still
6307 use a type-correct type; it's just doesn't indicate the array
6308 bounds. That's better than using `void*' or some such; it's
6309 cleaner, and it let's the alias analysis code know that these
6310 stores cannot alias stores to void*! */
6311 tree field;
6313 field = build_decl (input_location,
6314 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6315 DECL_VIRTUAL_P (field) = 1;
6316 DECL_ARTIFICIAL (field) = 1;
6317 DECL_FIELD_CONTEXT (field) = t;
6318 DECL_FCONTEXT (field) = t;
6319 if (TYPE_PACKED (t))
6320 DECL_PACKED (field) = 1;
6322 TYPE_VFIELD (t) = field;
6324 /* This class is non-empty. */
6325 CLASSTYPE_EMPTY_P (t) = 0;
6327 return field;
6330 return NULL_TREE;
6333 /* Add OFFSET to all base types of BINFO which is a base in the
6334 hierarchy dominated by T.
6336 OFFSET, which is a type offset, is number of bytes. */
6338 static void
6339 propagate_binfo_offsets (tree binfo, tree offset)
6341 int i;
6342 tree primary_binfo;
6343 tree base_binfo;
6345 /* Update BINFO's offset. */
6346 BINFO_OFFSET (binfo)
6347 = fold_convert (sizetype,
6348 size_binop (PLUS_EXPR,
6349 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6350 offset));
6352 /* Find the primary base class. */
6353 primary_binfo = get_primary_binfo (binfo);
6355 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6356 propagate_binfo_offsets (primary_binfo, offset);
6358 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6359 downwards. */
6360 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6362 /* Don't do the primary base twice. */
6363 if (base_binfo == primary_binfo)
6364 continue;
6366 if (BINFO_VIRTUAL_P (base_binfo))
6367 continue;
6369 propagate_binfo_offsets (base_binfo, offset);
6373 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6374 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6375 empty subobjects of T. */
6377 static void
6378 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6380 tree vbase;
6381 tree t = rli->t;
6382 tree *next_field;
6384 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6385 return;
6387 /* Find the last field. The artificial fields created for virtual
6388 bases will go after the last extant field to date. */
6389 next_field = &TYPE_FIELDS (t);
6390 while (*next_field)
6391 next_field = &DECL_CHAIN (*next_field);
6393 /* Go through the virtual bases, allocating space for each virtual
6394 base that is not already a primary base class. These are
6395 allocated in inheritance graph order. */
6396 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6398 if (!BINFO_VIRTUAL_P (vbase))
6399 continue;
6401 if (!BINFO_PRIMARY_P (vbase))
6403 /* This virtual base is not a primary base of any class in the
6404 hierarchy, so we have to add space for it. */
6405 next_field = build_base_field (rli, vbase,
6406 access_private_node,
6407 offsets, next_field);
6412 /* Returns the offset of the byte just past the end of the base class
6413 BINFO. */
6415 static tree
6416 end_of_base (tree binfo)
6418 tree size;
6420 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6421 size = TYPE_SIZE_UNIT (char_type_node);
6422 else if (is_empty_class (BINFO_TYPE (binfo)))
6423 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6424 allocate some space for it. It cannot have virtual bases, so
6425 TYPE_SIZE_UNIT is fine. */
6426 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6427 else
6428 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6430 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6433 /* Returns one of three variations of the ending offset of T. If MODE is
6434 eoc_nvsize, the result is the ABI "nvsize" (i.e. sizeof before allocating
6435 vbases). If MODE is eoc_vsize, the result is the sizeof after allocating
6436 vbases but before rounding, which is not named in the ABI. If MODE is
6437 eoc_nv_or_dsize, the result is the greater of "nvsize" and "dsize" (the size
6438 of the actual data in the class, kinda), as used for allocation of
6439 potentially-overlapping fields. */
6441 enum eoc_mode { eoc_nvsize, eoc_vsize, eoc_nv_or_dsize };
6442 static tree
6443 end_of_class (tree t, eoc_mode mode)
6445 tree result = size_zero_node;
6446 vec<tree, va_gc> *vbases;
6447 tree binfo;
6448 tree base_binfo;
6449 tree offset;
6450 int i;
6452 for (binfo = TYPE_BINFO (t), i = 0;
6453 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6455 if (BINFO_VIRTUAL_P (base_binfo)
6456 && (!BINFO_PRIMARY_P (base_binfo)
6457 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6458 continue;
6460 offset = end_of_base (base_binfo);
6461 if (tree_int_cst_lt (result, offset))
6462 result = offset;
6465 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6466 if (TREE_CODE (field) == FIELD_DECL
6467 && !DECL_FIELD_IS_BASE (field))
6469 tree size = DECL_SIZE_UNIT (field);
6470 if (!size)
6471 /* DECL_SIZE_UNIT can be null for a flexible array. */
6472 continue;
6474 if (is_empty_field (field))
6475 /* For empty fields DECL_SIZE_UNIT is 0, but we want the
6476 size of the type (usually 1) for computing nvsize. */
6477 size = TYPE_SIZE_UNIT (TREE_TYPE (field));
6479 if (DECL_BIT_FIELD_TYPE (field))
6481 offset = size_binop (PLUS_EXPR, bit_position (field),
6482 DECL_SIZE (field));
6483 offset = size_binop (CEIL_DIV_EXPR, offset, bitsize_unit_node);
6484 offset = fold_convert (sizetype, offset);
6486 else
6487 offset = size_binop (PLUS_EXPR, byte_position (field), size);
6488 if (tree_int_cst_lt (result, offset))
6489 result = offset;
6492 if (mode != eoc_nvsize)
6493 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6494 vec_safe_iterate (vbases, i, &base_binfo); i++)
6496 if (mode == eoc_nv_or_dsize)
6497 /* For dsize, don't count trailing empty bases. */
6498 offset = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo),
6499 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
6500 else
6501 offset = end_of_base (base_binfo);
6502 if (tree_int_cst_lt (result, offset))
6503 result = offset;
6506 return result;
6509 /* Warn as appropriate about the change in whether we pack into the tail
6510 padding of FIELD, a base field which has a C++14 aggregate type with default
6511 member initializers. */
6513 static void
6514 check_non_pod_aggregate (tree field)
6516 if (!abi_version_crosses (17) || cxx_dialect < cxx14)
6517 return;
6518 if (TREE_CODE (field) != FIELD_DECL
6519 || (!DECL_FIELD_IS_BASE (field)
6520 && !field_poverlapping_p (field)))
6521 return;
6522 tree next = DECL_CHAIN (field);
6523 while (next && TREE_CODE (next) != FIELD_DECL) next = DECL_CHAIN (next);
6524 if (!next)
6525 return;
6526 tree type = TREE_TYPE (field);
6527 if (TYPE_IDENTIFIER (type) == as_base_identifier)
6528 type = TYPE_CONTEXT (type);
6529 if (!CLASS_TYPE_P (type) || !CLASSTYPE_NON_POD_AGGREGATE (type))
6530 return;
6531 tree size = end_of_class (type, (DECL_FIELD_IS_BASE (field)
6532 ? eoc_nvsize : eoc_nv_or_dsize));
6533 tree rounded = round_up_loc (input_location, size, DECL_ALIGN_UNIT (next));
6534 if (tree_int_cst_lt (rounded, TYPE_SIZE_UNIT (type)))
6536 location_t loc = DECL_SOURCE_LOCATION (next);
6537 if (DECL_FIELD_IS_BASE (next))
6538 warning_at (loc, OPT_Wabi,"offset of %qT base class for "
6539 "%<-std=c++14%> and up changes in "
6540 "%<-fabi-version=17%> (GCC 12)", TREE_TYPE (next));
6541 else
6542 warning_at (loc, OPT_Wabi, "offset of %qD for "
6543 "%<-std=c++14%> and up changes in "
6544 "%<-fabi-version=17%> (GCC 12)", next);
6548 /* Warn about bases of T that are inaccessible because they are
6549 ambiguous. For example:
6551 struct S {};
6552 struct T : public S {};
6553 struct U : public S, public T {};
6555 Here, `(S*) new U' is not allowed because there are two `S'
6556 subobjects of U. */
6558 static void
6559 maybe_warn_about_inaccessible_bases (tree t)
6561 int i;
6562 vec<tree, va_gc> *vbases;
6563 tree basetype;
6564 tree binfo;
6565 tree base_binfo;
6567 /* If not checking for warning then return early. */
6568 if (!warn_inaccessible_base)
6569 return;
6571 /* If there are no repeated bases, nothing can be ambiguous. */
6572 if (!CLASSTYPE_REPEATED_BASE_P (t))
6573 return;
6575 /* Check direct bases. */
6576 for (binfo = TYPE_BINFO (t), i = 0;
6577 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6579 basetype = BINFO_TYPE (base_binfo);
6581 if (!uniquely_derived_from_p (basetype, t))
6582 warning (OPT_Winaccessible_base, "direct base %qT inaccessible "
6583 "in %qT due to ambiguity", basetype, t);
6586 /* Check for ambiguous virtual bases. */
6587 if (extra_warnings)
6588 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6589 vec_safe_iterate (vbases, i, &binfo); i++)
6591 basetype = BINFO_TYPE (binfo);
6593 if (!uniquely_derived_from_p (basetype, t))
6594 warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in "
6595 "%qT due to ambiguity", basetype, t);
6599 /* Compare two INTEGER_CSTs K1 and K2. */
6601 static int
6602 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6604 return tree_int_cst_compare ((tree) k1, (tree) k2);
6607 /* Increase the size indicated in RLI to account for empty classes
6608 that are "off the end" of the class. */
6610 static void
6611 include_empty_classes (record_layout_info rli)
6613 tree eoc;
6614 tree rli_size;
6616 /* It might be the case that we grew the class to allocate a
6617 zero-sized base class. That won't be reflected in RLI, yet,
6618 because we are willing to overlay multiple bases at the same
6619 offset. However, now we need to make sure that RLI is big enough
6620 to reflect the entire class. */
6621 eoc = end_of_class (rli->t, eoc_vsize);
6622 rli_size = rli_size_unit_so_far (rli);
6623 if (TREE_CODE (rli_size) == INTEGER_CST
6624 && tree_int_cst_lt (rli_size, eoc))
6626 /* The size should have been rounded to a whole byte. */
6627 gcc_assert (tree_int_cst_equal
6628 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6629 rli->bitpos
6630 = size_binop (PLUS_EXPR,
6631 rli->bitpos,
6632 size_binop (MULT_EXPR,
6633 fold_convert (bitsizetype,
6634 size_binop (MINUS_EXPR,
6635 eoc, rli_size)),
6636 bitsize_int (BITS_PER_UNIT)));
6637 normalize_rli (rli);
6641 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6642 BINFO_OFFSETs for all of the base-classes. Position the vtable
6643 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6645 static void
6646 layout_class_type (tree t, tree *virtuals_p)
6648 tree non_static_data_members;
6649 tree field;
6650 tree vptr;
6651 record_layout_info rli;
6652 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6653 types that appear at that offset. */
6654 splay_tree empty_base_offsets;
6655 /* True if the last field laid out was a bit-field. */
6656 bool last_field_was_bitfield = false;
6657 /* The location at which the next field should be inserted. */
6658 tree *next_field;
6660 /* Keep track of the first non-static data member. */
6661 non_static_data_members = TYPE_FIELDS (t);
6663 /* Start laying out the record. */
6664 rli = start_record_layout (t);
6666 /* Mark all the primary bases in the hierarchy. */
6667 determine_primary_bases (t);
6669 /* Create a pointer to our virtual function table. */
6670 vptr = create_vtable_ptr (t, virtuals_p);
6672 /* The vptr is always the first thing in the class. */
6673 if (vptr)
6675 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6676 TYPE_FIELDS (t) = vptr;
6677 next_field = &DECL_CHAIN (vptr);
6678 place_field (rli, vptr);
6680 else
6681 next_field = &TYPE_FIELDS (t);
6683 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6684 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6685 NULL, NULL);
6686 build_base_fields (rli, empty_base_offsets, next_field);
6688 /* Layout the non-static data members. */
6689 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6691 tree type;
6692 tree padding;
6694 /* We still pass things that aren't non-static data members to
6695 the back end, in case it wants to do something with them. */
6696 if (TREE_CODE (field) != FIELD_DECL)
6698 place_field (rli, field);
6699 /* If the static data member has incomplete type, keep track
6700 of it so that it can be completed later. (The handling
6701 of pending statics in finish_record_layout is
6702 insufficient; consider:
6704 struct S1;
6705 struct S2 { static S1 s1; };
6707 At this point, finish_record_layout will be called, but
6708 S1 is still incomplete.) */
6709 if (VAR_P (field))
6711 maybe_register_incomplete_var (field);
6712 /* The visibility of static data members is determined
6713 at their point of declaration, not their point of
6714 definition. */
6715 determine_visibility (field);
6717 continue;
6720 type = TREE_TYPE (field);
6721 if (type == error_mark_node)
6722 continue;
6724 padding = NULL_TREE;
6726 bool might_overlap = field_poverlapping_p (field);
6728 if (might_overlap && CLASS_TYPE_P (type)
6729 && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type)))
6731 /* if D is a potentially-overlapping data member, update sizeof(C) to
6732 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
6733 if (CLASSTYPE_EMPTY_P (type))
6734 DECL_SIZE (field) = DECL_SIZE_UNIT (field) = size_zero_node;
6735 else
6737 tree size = end_of_class (type, eoc_nv_or_dsize);
6738 DECL_SIZE_UNIT (field) = size;
6739 DECL_SIZE (field) = bit_from_pos (size, bitsize_zero_node);
6743 /* If this field is a bit-field whose width is greater than its
6744 type, then there are some special rules for allocating
6745 it. */
6746 if (DECL_C_BIT_FIELD (field)
6747 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6749 bool was_unnamed_p = false;
6750 /* We must allocate the bits as if suitably aligned for the
6751 longest integer type that fits in this many bits. Then,
6752 we are supposed to use the left over bits as additional
6753 padding. */
6755 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6756 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6757 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6758 limit = DECL_SIZE (field);
6760 tree integer_type = integer_types[itk_char];
6761 for (unsigned itk = itk_char; itk != itk_none; itk++)
6762 if (tree next = integer_types[itk])
6764 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6765 /* Too big, so our current guess is what we want. */
6766 break;
6767 /* Not bigger than limit, ok */
6768 integer_type = next;
6771 /* Figure out how much additional padding is required. */
6772 if (TREE_CODE (t) == UNION_TYPE)
6773 /* In a union, the padding field must have the full width
6774 of the bit-field; all fields start at offset zero. */
6775 padding = DECL_SIZE (field);
6776 else
6777 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6778 TYPE_SIZE (integer_type));
6780 if (integer_zerop (padding))
6781 padding = NULL_TREE;
6783 /* An unnamed bitfield does not normally affect the
6784 alignment of the containing class on a target where
6785 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6786 make any exceptions for unnamed bitfields when the
6787 bitfields are longer than their types. Therefore, we
6788 temporarily give the field a name. */
6789 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6791 was_unnamed_p = true;
6792 DECL_NAME (field) = make_anon_name ();
6795 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6796 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6797 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6798 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6799 empty_base_offsets);
6800 if (was_unnamed_p)
6801 DECL_NAME (field) = NULL_TREE;
6802 /* Now that layout has been performed, set the size of the
6803 field to the size of its declared type; the rest of the
6804 field is effectively invisible. */
6805 DECL_SIZE (field) = TYPE_SIZE (type);
6806 /* We must also reset the DECL_MODE of the field. */
6807 SET_DECL_MODE (field, TYPE_MODE (type));
6809 else if (might_overlap && is_empty_class (type))
6811 SET_DECL_FIELD_ABI_IGNORED (field, 1);
6812 layout_empty_base_or_field (rli, field, empty_base_offsets);
6814 else
6815 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6816 empty_base_offsets);
6818 /* Remember the location of any empty classes in FIELD. */
6819 record_subobject_offsets (field, empty_base_offsets);
6821 /* If a bit-field does not immediately follow another bit-field,
6822 and yet it starts in the middle of a byte, we have failed to
6823 comply with the ABI. */
6824 if (warn_abi
6825 && DECL_C_BIT_FIELD (field)
6826 /* The TREE_NO_WARNING flag gets set by Objective-C when
6827 laying out an Objective-C class. The ObjC ABI differs
6828 from the C++ ABI, and so we do not want a warning
6829 here. */
6830 && !warning_suppressed_p (field, OPT_Wabi)
6831 && !last_field_was_bitfield
6832 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6833 DECL_FIELD_BIT_OFFSET (field),
6834 bitsize_unit_node)))
6835 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6836 "offset of %qD is not ABI-compliant and may "
6837 "change in a future version of GCC", field);
6839 /* The middle end uses the type of expressions to determine the
6840 possible range of expression values. In order to optimize
6841 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6842 must be made aware of the width of "i", via its type.
6844 Because C++ does not have integer types of arbitrary width,
6845 we must (for the purposes of the front end) convert from the
6846 type assigned here to the declared type of the bitfield
6847 whenever a bitfield expression is used as an rvalue.
6848 Similarly, when assigning a value to a bitfield, the value
6849 must be converted to the type given the bitfield here. */
6850 if (DECL_C_BIT_FIELD (field))
6852 unsigned HOST_WIDE_INT width;
6853 tree ftype = TREE_TYPE (field);
6854 width = tree_to_uhwi (DECL_SIZE (field));
6855 if (width != TYPE_PRECISION (ftype))
6857 TREE_TYPE (field)
6858 = c_build_bitfield_integer_type (width,
6859 TYPE_UNSIGNED (ftype));
6860 TREE_TYPE (field)
6861 = cp_build_qualified_type (TREE_TYPE (field),
6862 cp_type_quals (ftype));
6866 /* If we needed additional padding after this field, add it
6867 now. */
6868 if (padding)
6870 tree padding_field;
6872 padding_field = build_decl (input_location,
6873 FIELD_DECL,
6874 NULL_TREE,
6875 char_type_node);
6876 DECL_BIT_FIELD (padding_field) = 1;
6877 DECL_SIZE (padding_field) = padding;
6878 DECL_CONTEXT (padding_field) = t;
6879 DECL_ARTIFICIAL (padding_field) = 1;
6880 DECL_IGNORED_P (padding_field) = 1;
6881 DECL_PADDING_P (padding_field) = 1;
6882 layout_nonempty_base_or_field (rli, padding_field,
6883 NULL_TREE,
6884 empty_base_offsets);
6887 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6890 if (!integer_zerop (rli->bitpos))
6892 /* Make sure that we are on a byte boundary so that the size of
6893 the class without virtual bases will always be a round number
6894 of bytes. */
6895 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6896 normalize_rli (rli);
6899 /* We used to remove zero width bitfields at this point since PR42217,
6900 while the C FE never did that. That caused ABI differences on various
6901 targets. Set the DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD flag on them
6902 instead, so that the backends can emit -Wpsabi warnings in the cases
6903 where the ABI changed. */
6904 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6906 if (TREE_CODE (field) == FIELD_DECL
6907 && DECL_C_BIT_FIELD (field)
6908 /* We should not be confused by the fact that grokbitfield
6909 temporarily sets the width of the bit field into
6910 DECL_BIT_FIELD_REPRESENTATIVE (field).
6911 check_bitfield_decl eventually sets DECL_SIZE (field)
6912 to that width. */
6913 && (DECL_SIZE (field) == NULL_TREE
6914 || integer_zerop (DECL_SIZE (field))))
6915 SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field, 1);
6916 check_non_pod_aggregate (field);
6919 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6921 /* T needs a different layout as a base (eliding virtual bases
6922 or whatever). Create that version. */
6923 tree base_t = make_node (TREE_CODE (t));
6924 tree base_d = create_implicit_typedef (as_base_identifier, base_t);
6926 TYPE_CONTEXT (base_t) = t;
6927 DECL_CONTEXT (base_d) = t;
6929 set_instantiating_module (base_d);
6931 /* If the ABI version is not at least two, and the last
6932 field was a bit-field, RLI may not be on a byte
6933 boundary. In particular, rli_size_unit_so_far might
6934 indicate the last complete byte, while rli_size_so_far
6935 indicates the total number of bits used. Therefore,
6936 rli_size_so_far, rather than rli_size_unit_so_far, is
6937 used to compute TYPE_SIZE_UNIT. */
6939 /* Set the size and alignment for the new type. */
6940 tree eoc = end_of_class (t, eoc_nvsize);
6941 TYPE_SIZE_UNIT (base_t)
6942 = size_binop (MAX_EXPR,
6943 fold_convert (sizetype,
6944 size_binop (CEIL_DIV_EXPR,
6945 rli_size_so_far (rli),
6946 bitsize_int (BITS_PER_UNIT))),
6947 eoc);
6948 TYPE_SIZE (base_t)
6949 = size_binop (MAX_EXPR,
6950 rli_size_so_far (rli),
6951 size_binop (MULT_EXPR,
6952 fold_convert (bitsizetype, eoc),
6953 bitsize_int (BITS_PER_UNIT)));
6954 SET_TYPE_ALIGN (base_t, rli->record_align);
6955 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6956 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
6957 TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t);
6959 /* Copy the non-static data members of T. This will include its
6960 direct non-virtual bases & vtable. */
6961 next_field = &TYPE_FIELDS (base_t);
6962 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6963 if (TREE_CODE (field) == FIELD_DECL)
6965 *next_field = copy_node (field);
6966 /* Zap any NSDMI, it's not needed and might be a deferred
6967 parse. */
6968 DECL_INITIAL (*next_field) = NULL_TREE;
6969 DECL_CONTEXT (*next_field) = base_t;
6970 next_field = &DECL_CHAIN (*next_field);
6972 *next_field = NULL_TREE;
6974 /* We use the base type for trivial assignments, and hence it
6975 needs a mode. */
6976 compute_record_mode (base_t);
6978 /* Record the base version of the type. */
6979 CLASSTYPE_AS_BASE (t) = base_t;
6981 else
6982 CLASSTYPE_AS_BASE (t) = t;
6984 /* Every empty class contains an empty class. */
6985 if (CLASSTYPE_EMPTY_P (t))
6986 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6988 /* Set the TYPE_DECL for this type to contain the right
6989 value for DECL_OFFSET, so that we can use it as part
6990 of a COMPONENT_REF for multiple inheritance. */
6991 layout_decl (TYPE_MAIN_DECL (t), 0);
6993 /* Now fix up any virtual base class types that we left lying
6994 around. We must get these done before we try to lay out the
6995 virtual function table. As a side-effect, this will remove the
6996 base subobject fields. */
6997 layout_virtual_bases (rli, empty_base_offsets);
6999 /* Make sure that empty classes are reflected in RLI at this
7000 point. */
7001 include_empty_classes (rli);
7003 /* Make sure not to create any structures with zero size. */
7004 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
7005 place_field (rli,
7006 build_decl (input_location,
7007 FIELD_DECL, NULL_TREE, char_type_node));
7009 /* If this is a non-POD, declaring it packed makes a difference to how it
7010 can be used as a field; don't let finalize_record_size undo it. */
7011 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
7012 rli->packed_maybe_necessary = true;
7014 /* Let the back end lay out the type. */
7015 finish_record_layout (rli, /*free_p=*/true);
7017 /* If we didn't end up needing an as-base type, don't use it. */
7018 if (CLASSTYPE_AS_BASE (t) != t
7019 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
7020 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
7021 causing us to lose the user-specified alignment as in PR94050. */
7022 && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t))
7023 && tree_int_cst_equal (TYPE_SIZE (t),
7024 TYPE_SIZE (CLASSTYPE_AS_BASE (t))))
7025 CLASSTYPE_AS_BASE (t) = t;
7027 if (TYPE_SIZE_UNIT (t)
7028 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7029 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7030 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7031 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
7033 /* Warn about bases that can't be talked about due to ambiguity. */
7034 maybe_warn_about_inaccessible_bases (t);
7036 /* Now that we're done with layout, give the base fields the real types. */
7037 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7038 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
7039 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
7041 /* Clean up. */
7042 splay_tree_delete (empty_base_offsets);
7044 if (CLASSTYPE_EMPTY_P (t)
7045 && tree_int_cst_lt (sizeof_biggest_empty_class,
7046 TYPE_SIZE_UNIT (t)))
7047 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
7050 /* Determine the "key method" for the class type indicated by TYPE,
7051 and set CLASSTYPE_KEY_METHOD accordingly. */
7053 void
7054 determine_key_method (tree type)
7056 tree method;
7058 if (processing_template_decl
7059 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
7060 || CLASSTYPE_INTERFACE_KNOWN (type))
7061 return;
7063 /* The key method is the first non-pure virtual function that is not
7064 inline at the point of class definition. On some targets the
7065 key function may not be inline; those targets should not call
7066 this function until the end of the translation unit. */
7067 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
7068 if (TREE_CODE (method) == FUNCTION_DECL
7069 && DECL_VINDEX (method) != NULL_TREE
7070 && ! DECL_DECLARED_INLINE_P (method)
7071 && ! DECL_PURE_VIRTUAL_P (method))
7073 CLASSTYPE_KEY_METHOD (type) = method;
7074 break;
7077 return;
7080 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
7081 class data member of non-zero size, otherwise false. */
7083 static inline bool
7084 field_nonempty_p (const_tree fld)
7086 if (TREE_CODE (fld) == ERROR_MARK)
7087 return false;
7089 tree type = TREE_TYPE (fld);
7090 if (TREE_CODE (fld) == FIELD_DECL
7091 && TREE_CODE (type) != ERROR_MARK
7092 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
7094 return TYPE_SIZE (type)
7095 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
7096 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
7099 return false;
7102 /* Used by find_flexarrays and related functions. */
7104 struct flexmems_t
7106 /* The first flexible array member or non-zero array member found
7107 in the order of layout. */
7108 tree array;
7109 /* First non-static non-empty data member in the class or its bases. */
7110 tree first;
7111 /* The first non-static non-empty data member following either
7112 the flexible array member, if found, or the zero-length array member
7113 otherwise. AFTER[1] refers to the first such data member of a union
7114 of which the struct containing the flexible array member or zero-length
7115 array is a member, or NULL when no such union exists. This element is
7116 only used during searching, not for diagnosing problems. AFTER[0]
7117 refers to the first such data member that is not a member of such
7118 a union. */
7119 tree after[2];
7121 /* Refers to a struct (not union) in which the struct of which the flexible
7122 array is member is defined. Used to diagnose strictly (according to C)
7123 invalid uses of the latter structs. */
7124 tree enclosing;
7127 /* Find either the first flexible array member or the first zero-length
7128 array, in that order of preference, among members of class T (but not
7129 its base classes), and set members of FMEM accordingly.
7130 BASE_P is true if T is a base class of another class.
7131 PUN is set to the outermost union in which the flexible array member
7132 (or zero-length array) is defined if one such union exists, otherwise
7133 to NULL.
7134 Similarly, PSTR is set to a data member of the outermost struct of
7135 which the flexible array is a member if one such struct exists,
7136 otherwise to NULL. */
7138 static void
7139 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
7140 tree pun /* = NULL_TREE */,
7141 tree pstr /* = NULL_TREE */)
7143 /* Set the "pointer" to the outermost enclosing union if not set
7144 yet and maintain it for the remainder of the recursion. */
7145 if (!pun && TREE_CODE (t) == UNION_TYPE)
7146 pun = t;
7148 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
7150 if (fld == error_mark_node)
7151 return;
7153 /* Is FLD a typedef for an anonymous struct? */
7155 /* FIXME: Note that typedefs (as well as arrays) need to be fully
7156 handled elsewhere so that errors like the following are detected
7157 as well:
7158 typedef struct { int i, a[], j; } S; // bug c++/72753
7159 S s [2]; // bug c++/68489
7161 if (TREE_CODE (fld) == TYPE_DECL
7162 && DECL_IMPLICIT_TYPEDEF_P (fld)
7163 && CLASS_TYPE_P (TREE_TYPE (fld))
7164 && IDENTIFIER_ANON_P (DECL_NAME (fld)))
7166 /* Check the nested unnamed type referenced via a typedef
7167 independently of FMEM (since it's not a data member of
7168 the enclosing class). */
7169 check_flexarrays (TREE_TYPE (fld));
7170 continue;
7173 /* Skip anything that's GCC-generated or not a (non-static) data
7174 member. */
7175 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
7176 continue;
7178 /* Type of the member. */
7179 tree fldtype = TREE_TYPE (fld);
7180 if (fldtype == error_mark_node)
7181 return;
7183 /* Determine the type of the array element or object referenced
7184 by the member so that it can be checked for flexible array
7185 members if it hasn't been yet. */
7186 tree eltype = fldtype;
7187 while (TREE_CODE (eltype) == ARRAY_TYPE
7188 || INDIRECT_TYPE_P (eltype))
7189 eltype = TREE_TYPE (eltype);
7191 if (RECORD_OR_UNION_TYPE_P (eltype))
7193 if (fmem->array && !fmem->after[bool (pun)])
7195 /* Once the member after the flexible array has been found
7196 we're done. */
7197 fmem->after[bool (pun)] = fld;
7198 break;
7201 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
7203 /* Descend into the non-static member struct or union and try
7204 to find a flexible array member or zero-length array among
7205 its members. This is only necessary for anonymous types
7206 and types in whose context the current type T has not been
7207 defined (the latter must not be checked again because they
7208 are already in the process of being checked by one of the
7209 recursive calls). */
7211 tree first = fmem->first;
7212 tree array = fmem->array;
7214 /* If this member isn't anonymous and a prior non-flexible array
7215 member has been seen in one of the enclosing structs, clear
7216 the FIRST member since it doesn't contribute to the flexible
7217 array struct's members. */
7218 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7219 fmem->first = NULL_TREE;
7221 find_flexarrays (eltype, fmem, false, pun,
7222 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
7224 if (fmem->array != array)
7225 continue;
7227 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7229 /* Restore the FIRST member reset above if no flexible
7230 array member has been found in this member's struct. */
7231 fmem->first = first;
7234 /* If the member struct contains the first flexible array
7235 member, or if this member is a base class, continue to
7236 the next member and avoid setting the FMEM->NEXT pointer
7237 to point to it. */
7238 if (base_p)
7239 continue;
7243 if (field_nonempty_p (fld))
7245 /* Remember the first non-static data member. */
7246 if (!fmem->first)
7247 fmem->first = fld;
7249 /* Remember the first non-static data member after the flexible
7250 array member, if one has been found, or the zero-length array
7251 if it has been found. */
7252 if (fmem->array && !fmem->after[bool (pun)])
7253 fmem->after[bool (pun)] = fld;
7256 /* Skip non-arrays. */
7257 if (TREE_CODE (fldtype) != ARRAY_TYPE)
7258 continue;
7260 /* Determine the upper bound of the array if it has one. */
7261 if (TYPE_DOMAIN (fldtype))
7263 if (fmem->array)
7265 /* Make a record of the zero-length array if either one
7266 such field or a flexible array member has been seen to
7267 handle the pathological and unlikely case of multiple
7268 such members. */
7269 if (!fmem->after[bool (pun)])
7270 fmem->after[bool (pun)] = fld;
7272 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7274 /* Remember the first zero-length array unless a flexible array
7275 member has already been seen. */
7276 fmem->array = fld;
7277 fmem->enclosing = pstr;
7280 else
7282 /* Flexible array members have no upper bound. */
7283 if (fmem->array)
7285 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7287 /* Replace the zero-length array if it's been stored and
7288 reset the after pointer. */
7289 fmem->after[bool (pun)] = NULL_TREE;
7290 fmem->array = fld;
7291 fmem->enclosing = pstr;
7293 else if (!fmem->after[bool (pun)])
7294 /* Make a record of another flexible array member. */
7295 fmem->after[bool (pun)] = fld;
7297 else
7299 fmem->array = fld;
7300 fmem->enclosing = pstr;
7306 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7307 a flexible array member (or the zero-length array extension). */
7309 static void
7310 diagnose_invalid_flexarray (const flexmems_t *fmem)
7312 if (fmem->array && fmem->enclosing)
7314 auto_diagnostic_group d;
7315 if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
7316 TYPE_DOMAIN (TREE_TYPE (fmem->array))
7317 ? G_("invalid use of %q#T with a zero-size array "
7318 "in %q#D")
7319 : G_("invalid use of %q#T with a flexible array member "
7320 "in %q#T"),
7321 DECL_CONTEXT (fmem->array),
7322 DECL_CONTEXT (fmem->enclosing)))
7323 inform (DECL_SOURCE_LOCATION (fmem->array),
7324 "array member %q#D declared here", fmem->array);
7328 /* Issue diagnostics for invalid flexible array members or zero-length
7329 arrays that are not the last elements of the containing class or its
7330 base classes or that are its sole members. */
7332 static void
7333 diagnose_flexarrays (tree t, const flexmems_t *fmem)
7335 if (!fmem->array)
7336 return;
7338 if (fmem->first && !fmem->after[0])
7340 diagnose_invalid_flexarray (fmem);
7341 return;
7344 /* Has a diagnostic been issued? */
7345 bool diagd = false;
7347 const char *msg = 0;
7349 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7351 if (fmem->after[0])
7352 msg = G_("zero-size array member %qD not at end of %q#T");
7353 else if (!fmem->first)
7354 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7356 if (msg)
7358 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7360 auto_diagnostic_group d;
7361 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7363 inform (location_of (t), "in the definition of %q#T", t);
7364 diagd = true;
7368 else
7370 if (fmem->after[0])
7371 msg = G_("flexible array member %qD not at end of %q#T");
7372 else if (!fmem->first)
7373 msg = G_("flexible array member %qD in an otherwise empty %q#T");
7375 if (msg)
7377 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7378 diagd = true;
7380 auto_diagnostic_group d;
7381 error_at (loc, msg, fmem->array, t);
7383 /* In the unlikely event that the member following the flexible
7384 array member is declared in a different class, or the member
7385 overlaps another member of a common union, point to it.
7386 Otherwise it should be obvious. */
7387 if (fmem->after[0]
7388 && ((DECL_CONTEXT (fmem->after[0])
7389 != DECL_CONTEXT (fmem->array))))
7391 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7392 "next member %q#D declared here",
7393 fmem->after[0]);
7394 inform (location_of (t), "in the definition of %q#T", t);
7399 if (!diagd && fmem->array && fmem->enclosing)
7400 diagnose_invalid_flexarray (fmem);
7404 /* Recursively check to make sure that any flexible array or zero-length
7405 array members of class T or its bases are valid (i.e., not the sole
7406 non-static data member of T and, if one exists, that it is the last
7407 non-static data member of T and its base classes. FMEM is expected
7408 to be initially null and is used internally by recursive calls to
7409 the function. Issue the appropriate diagnostics for the array member
7410 that fails the checks. */
7412 static void
7413 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7414 bool base_p /* = false */)
7416 /* Initialize the result of a search for flexible array and zero-length
7417 array members. Avoid doing any work if the most interesting FMEM data
7418 have already been populated. */
7419 flexmems_t flexmems = flexmems_t ();
7420 if (!fmem)
7421 fmem = &flexmems;
7422 else if (fmem->array && fmem->first && fmem->after[0])
7423 return;
7425 tree fam = fmem->array;
7427 /* Recursively check the primary base class first. */
7428 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7430 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7431 check_flexarrays (basetype, fmem, true);
7434 /* Recursively check the base classes. */
7435 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7436 for (int i = 0; i < nbases; ++i)
7438 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7440 /* The primary base class was already checked above. */
7441 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7442 continue;
7444 /* Virtual base classes are at the end. */
7445 if (BINFO_VIRTUAL_P (base_binfo))
7446 continue;
7448 /* Check the base class. */
7449 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7452 if (fmem == &flexmems)
7454 /* Check virtual base classes only once per derived class.
7455 I.e., this check is not performed recursively for base
7456 classes. */
7457 int i;
7458 tree base_binfo;
7459 vec<tree, va_gc> *vbases;
7460 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7461 vec_safe_iterate (vbases, i, &base_binfo); i++)
7463 /* Check the virtual base class. */
7464 tree basetype = TREE_TYPE (base_binfo);
7466 check_flexarrays (basetype, fmem, /*base_p=*/true);
7470 /* Is the type unnamed (and therefore a member of it potentially
7471 an anonymous struct or union)? */
7472 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7473 if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE)
7474 maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7476 /* Search the members of the current (possibly derived) class, skipping
7477 unnamed structs and unions since those could be anonymous. */
7478 if (fmem != &flexmems || !maybe_anon_p)
7479 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7481 if (fmem == &flexmems && !maybe_anon_p)
7483 /* Issue diagnostics for invalid flexible and zero-length array
7484 members found in base classes or among the members of the current
7485 class. Ignore anonymous structs and unions whose members are
7486 considered to be members of the enclosing class and thus will
7487 be diagnosed when checking it. */
7488 diagnose_flexarrays (t, fmem);
7492 /* Perform processing required when the definition of T (a class type)
7493 is complete. Diagnose invalid definitions of flexible array members
7494 and zero-size arrays. */
7496 void
7497 finish_struct_1 (tree t)
7499 tree x;
7500 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7501 tree virtuals = NULL_TREE;
7503 if (COMPLETE_TYPE_P (t))
7505 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7506 error ("redefinition of %q#T", t);
7507 popclass ();
7508 return;
7511 /* If this type was previously laid out as a forward reference,
7512 make sure we lay it out again. */
7513 TYPE_SIZE (t) = NULL_TREE;
7514 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7516 /* Make assumptions about the class; we'll reset the flags if
7517 necessary. */
7518 CLASSTYPE_EMPTY_P (t) = 1;
7519 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7520 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7521 CLASSTYPE_LITERAL_P (t) = true;
7523 /* Do end-of-class semantic processing: checking the validity of the
7524 bases and members and add implicitly generated methods. */
7525 check_bases_and_members (t);
7527 /* Find the key method. */
7528 if (TYPE_CONTAINS_VPTR_P (t))
7530 /* The Itanium C++ ABI permits the key method to be chosen when
7531 the class is defined -- even though the key method so
7532 selected may later turn out to be an inline function. On
7533 some systems (such as ARM Symbian OS) the key method cannot
7534 be determined until the end of the translation unit. On such
7535 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7536 will cause the class to be added to KEYED_CLASSES. Then, in
7537 finish_file we will determine the key method. */
7538 if (targetm.cxx.key_method_may_be_inline ())
7539 determine_key_method (t);
7541 /* If a polymorphic class has no key method, we may emit the vtable
7542 in every translation unit where the class definition appears. If
7543 we're devirtualizing, we can look into the vtable even if we
7544 aren't emitting it. */
7545 if (!CLASSTYPE_KEY_METHOD (t))
7546 vec_safe_push (keyed_classes, t);
7549 /* Layout the class itself. */
7550 layout_class_type (t, &virtuals);
7551 /* COMPLETE_TYPE_P is now true. */
7553 set_class_bindings (t);
7555 /* With the layout complete, check for flexible array members and
7556 zero-length arrays that might overlap other members in the final
7557 layout. */
7558 check_flexarrays (t);
7560 virtuals = modify_all_vtables (t, nreverse (virtuals));
7562 /* If necessary, create the primary vtable for this class. */
7563 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7565 /* We must enter these virtuals into the table. */
7566 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7567 build_primary_vtable (NULL_TREE, t);
7568 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7569 /* Here we know enough to change the type of our virtual
7570 function table, but we will wait until later this function. */
7571 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7573 /* If we're warning about ABI tags, check the types of the new
7574 virtual functions. */
7575 if (warn_abi_tag)
7576 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7577 check_abi_tags (t, TREE_VALUE (v));
7580 if (TYPE_CONTAINS_VPTR_P (t))
7582 int vindex;
7583 tree fn;
7585 if (BINFO_VTABLE (TYPE_BINFO (t)))
7586 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7587 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7588 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7590 /* Add entries for virtual functions introduced by this class. */
7591 BINFO_VIRTUALS (TYPE_BINFO (t))
7592 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7594 /* Set DECL_VINDEX for all functions declared in this class. */
7595 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7597 fn = TREE_CHAIN (fn),
7598 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7599 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7601 tree fndecl = BV_FN (fn);
7603 if (DECL_THUNK_P (fndecl))
7604 /* A thunk. We should never be calling this entry directly
7605 from this vtable -- we'd use the entry for the non
7606 thunk base function. */
7607 DECL_VINDEX (fndecl) = NULL_TREE;
7608 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7609 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7613 finish_struct_bits (t);
7615 set_method_tm_attributes (t);
7616 if (flag_openmp || flag_openmp_simd)
7617 finish_omp_declare_simd_methods (t);
7619 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
7620 for any static member objects of the type we're working on. */
7621 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7622 if (DECL_DECLARES_FUNCTION_P (x))
7624 /* Synthesize constexpr defaulted comparisons. */
7625 if (!DECL_ARTIFICIAL (x)
7626 && DECL_DEFAULTED_IN_CLASS_P (x)
7627 && special_function_p (x) == sfk_comparison)
7628 defaulted_late_check (x);
7629 DECL_IN_AGGR_P (x) = false;
7631 else if (VAR_P (x) && TREE_STATIC (x)
7632 && TREE_TYPE (x) != error_mark_node
7633 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7634 SET_DECL_MODE (x, TYPE_MODE (t));
7636 /* Complain if one of the field types requires lower visibility. */
7637 constrain_class_visibility (t);
7639 /* Make the rtl for any new vtables we have created, and unmark
7640 the base types we marked. */
7641 finish_vtbls (t);
7643 /* Build the VTT for T. */
7644 build_vtt (t);
7646 if (warn_nonvdtor
7647 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7648 && !CLASSTYPE_FINAL (t))
7649 warning (OPT_Wnon_virtual_dtor,
7650 "%q#T has virtual functions and accessible"
7651 " non-virtual destructor", t);
7653 complete_vars (t);
7655 if (warn_overloaded_virtual)
7656 warn_hidden (t);
7658 /* Class layout, assignment of virtual table slots, etc., is now
7659 complete. Give the back end a chance to tweak the visibility of
7660 the class or perform any other required target modifications. */
7661 targetm.cxx.adjust_class_at_definition (t);
7663 maybe_suppress_debug_info (t);
7665 if (flag_vtable_verify)
7666 vtv_save_class_info (t);
7668 dump_class_hierarchy (t);
7670 /* Finish debugging output for this type. */
7671 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7673 if (TYPE_TRANSPARENT_AGGR (t))
7675 tree field = first_field (t);
7676 if (field == NULL_TREE || error_operand_p (field))
7678 error ("type transparent %q#T does not have any fields", t);
7679 TYPE_TRANSPARENT_AGGR (t) = 0;
7681 else if (DECL_ARTIFICIAL (field))
7683 if (DECL_FIELD_IS_BASE (field))
7684 error ("type transparent class %qT has base classes", t);
7685 else
7687 gcc_checking_assert (DECL_VIRTUAL_P (field));
7688 error ("type transparent class %qT has virtual functions", t);
7690 TYPE_TRANSPARENT_AGGR (t) = 0;
7692 else if (TYPE_MODE (t) != DECL_MODE (field))
7694 error ("type transparent %q#T cannot be made transparent because "
7695 "the type of the first field has a different ABI from the "
7696 "class overall", t);
7697 TYPE_TRANSPARENT_AGGR (t) = 0;
7702 /* When T was built up, the member declarations were added in reverse
7703 order. Rearrange them to declaration order. */
7705 void
7706 unreverse_member_declarations (tree t)
7708 tree next;
7709 tree prev;
7710 tree x;
7712 /* The following lists are all in reverse order. Put them in
7713 declaration order now. */
7714 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7716 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7717 order, so we can't just use nreverse. Due to stat_hack
7718 chicanery in finish_member_declaration. */
7719 prev = NULL_TREE;
7720 for (x = TYPE_FIELDS (t);
7721 x && TREE_CODE (x) != TYPE_DECL;
7722 x = next)
7724 next = DECL_CHAIN (x);
7725 DECL_CHAIN (x) = prev;
7726 prev = x;
7729 if (prev)
7731 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7732 TYPE_FIELDS (t) = prev;
7736 tree
7737 finish_struct (tree t, tree attributes)
7739 location_t saved_loc = input_location;
7741 /* Now that we've got all the field declarations, reverse everything
7742 as necessary. */
7743 unreverse_member_declarations (t);
7745 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7746 fixup_attribute_variants (t);
7748 /* Nadger the current location so that diagnostics point to the start of
7749 the struct, not the end. */
7750 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7752 if (processing_template_decl)
7754 tree x;
7756 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7757 if (DECL_DECLARES_FUNCTION_P (x))
7759 DECL_IN_AGGR_P (x) = false;
7760 if (DECL_VIRTUAL_P (x))
7761 CLASSTYPE_NON_AGGREGATE (t) = true;
7763 else if (TREE_CODE (x) == FIELD_DECL)
7765 if (TREE_PROTECTED (x) || TREE_PRIVATE (x))
7766 CLASSTYPE_NON_AGGREGATE (t) = true;
7769 /* Also add a USING_DECL for operator=. We know there'll be (at
7770 least) one, but we don't know the signature(s). We want name
7771 lookup not to fail or recurse into bases. This isn't added
7772 to the template decl list so we drop this at instantiation
7773 time. */
7774 tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
7775 NULL_TREE);
7776 DECL_CONTEXT (ass_op) = t;
7777 USING_DECL_SCOPE (ass_op) = t;
7778 DECL_DEPENDENT_P (ass_op) = true;
7779 DECL_ARTIFICIAL (ass_op) = true;
7780 DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
7781 TYPE_FIELDS (t) = ass_op;
7783 TYPE_SIZE (t) = bitsize_zero_node;
7784 TYPE_SIZE_UNIT (t) = size_zero_node;
7785 /* COMPLETE_TYPE_P is now true. */
7787 set_class_bindings (t);
7789 /* We need to emit an error message if this type was used as a parameter
7790 and it is an abstract type, even if it is a template. We construct
7791 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7792 account and we call complete_vars with this type, which will check
7793 the PARM_DECLS. Note that while the type is being defined,
7794 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7795 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7796 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7797 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7798 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7799 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7800 complete_vars (t);
7802 /* Remember current #pragma pack value. */
7803 TYPE_PRECISION (t) = maximum_field_alignment;
7805 if (cxx_dialect < cxx20)
7807 if (!CLASSTYPE_NON_AGGREGATE (t)
7808 && type_has_user_provided_or_explicit_constructor (t))
7809 CLASSTYPE_NON_AGGREGATE (t) = 1;
7811 else if (TYPE_HAS_USER_CONSTRUCTOR (t))
7812 CLASSTYPE_NON_AGGREGATE (t) = 1;
7814 /* Fix up any variants we've already built. */
7815 fixup_type_variants (t);
7817 else
7818 finish_struct_1 (t);
7819 /* COMPLETE_TYPE_P is now true. */
7821 maybe_warn_about_overly_private_class (t);
7823 if (is_std_init_list (t))
7825 /* People keep complaining that the compiler crashes on an invalid
7826 definition of initializer_list, so I guess we should explicitly
7827 reject it. What the compiler internals care about is that it's a
7828 template and has a pointer field followed by size_type field. */
7829 bool ok = false;
7830 if (processing_template_decl)
7832 tree f = next_aggregate_field (TYPE_FIELDS (t));
7833 if (f && TYPE_PTR_P (TREE_TYPE (f)))
7835 f = next_aggregate_field (DECL_CHAIN (f));
7836 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7837 ok = true;
7840 /* It also cannot be a union. */
7841 ok &= NON_UNION_CLASS_TYPE_P (t);
7842 if (!ok)
7843 fatal_error (input_location, "definition of %qD does not match "
7844 "%<#include <initializer_list>%>", TYPE_NAME (t));
7847 input_location = saved_loc;
7849 TYPE_BEING_DEFINED (t) = 0;
7851 if (current_class_type)
7852 popclass ();
7853 else
7854 error ("trying to finish struct, but kicked out due to previous parse errors");
7856 if (flag_openmp)
7857 for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl))
7858 if (TREE_CODE (decl) == FUNCTION_DECL
7859 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
7860 if (tree attr = lookup_attribute ("omp declare variant base",
7861 DECL_ATTRIBUTES (decl)))
7862 omp_declare_variant_finalize (decl, attr);
7864 if (processing_template_decl && at_function_scope_p ()
7865 /* Lambdas are defined by the LAMBDA_EXPR. */
7866 && !LAMBDA_TYPE_P (t))
7867 add_stmt (build_min (TAG_DEFN, t));
7869 return t;
7872 /* Hash table to avoid endless recursion when handling references. */
7873 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7875 /* Return the dynamic type of INSTANCE, if known.
7876 Used to determine whether the virtual function table is needed
7877 or not.
7879 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7880 of our knowledge of its type. *NONNULL should be initialized
7881 before this function is called. */
7883 static tree
7884 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7886 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7888 switch (TREE_CODE (instance))
7890 case INDIRECT_REF:
7891 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
7892 return NULL_TREE;
7893 else
7894 return RECUR (TREE_OPERAND (instance, 0));
7896 case CALL_EXPR:
7897 /* This is a call to a constructor, hence it's never zero. */
7898 if (CALL_EXPR_FN (instance)
7899 && TREE_HAS_CONSTRUCTOR (instance))
7901 if (nonnull)
7902 *nonnull = 1;
7903 return TREE_TYPE (instance);
7905 return NULL_TREE;
7907 case SAVE_EXPR:
7908 /* This is a call to a constructor, hence it's never zero. */
7909 if (TREE_HAS_CONSTRUCTOR (instance))
7911 if (nonnull)
7912 *nonnull = 1;
7913 return TREE_TYPE (instance);
7915 return RECUR (TREE_OPERAND (instance, 0));
7917 case POINTER_PLUS_EXPR:
7918 case PLUS_EXPR:
7919 case MINUS_EXPR:
7920 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7921 return RECUR (TREE_OPERAND (instance, 0));
7922 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7923 /* Propagate nonnull. */
7924 return RECUR (TREE_OPERAND (instance, 0));
7926 return NULL_TREE;
7928 CASE_CONVERT:
7929 return RECUR (TREE_OPERAND (instance, 0));
7931 case ADDR_EXPR:
7932 instance = TREE_OPERAND (instance, 0);
7933 if (nonnull)
7935 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7936 with a real object -- given &p->f, p can still be null. */
7937 tree t = get_base_address (instance);
7938 /* ??? Probably should check DECL_WEAK here. */
7939 if (t && DECL_P (t))
7940 *nonnull = 1;
7942 return RECUR (instance);
7944 case COMPONENT_REF:
7945 /* If this component is really a base class reference, then the field
7946 itself isn't definitive. */
7947 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7948 return RECUR (TREE_OPERAND (instance, 0));
7949 return RECUR (TREE_OPERAND (instance, 1));
7951 case VAR_DECL:
7952 case FIELD_DECL:
7953 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7954 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7956 if (nonnull)
7957 *nonnull = 1;
7958 return TREE_TYPE (TREE_TYPE (instance));
7960 /* fall through. */
7961 case TARGET_EXPR:
7962 case PARM_DECL:
7963 case RESULT_DECL:
7964 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7966 if (nonnull)
7967 *nonnull = 1;
7968 return TREE_TYPE (instance);
7970 else if (instance == current_class_ptr)
7972 if (nonnull)
7973 *nonnull = 1;
7975 /* if we're in a ctor or dtor, we know our type. If
7976 current_class_ptr is set but we aren't in a function, we're in
7977 an NSDMI (and therefore a constructor). */
7978 if (current_scope () != current_function_decl
7979 || (DECL_LANG_SPECIFIC (current_function_decl)
7980 && (DECL_CONSTRUCTOR_P (current_function_decl)
7981 || DECL_DESTRUCTOR_P (current_function_decl))))
7983 if (cdtorp)
7984 *cdtorp = 1;
7985 return TREE_TYPE (TREE_TYPE (instance));
7988 else if (TYPE_REF_P (TREE_TYPE (instance)))
7990 /* We only need one hash table because it is always left empty. */
7991 if (!fixed_type_or_null_ref_ht)
7992 fixed_type_or_null_ref_ht
7993 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7995 /* Reference variables should be references to objects. */
7996 if (nonnull)
7997 *nonnull = 1;
7999 /* Enter the INSTANCE in a table to prevent recursion; a
8000 variable's initializer may refer to the variable
8001 itself. */
8002 if (VAR_P (instance)
8003 && DECL_INITIAL (instance)
8004 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
8005 && !fixed_type_or_null_ref_ht->find (instance))
8007 tree type;
8008 tree_node **slot;
8010 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
8011 *slot = instance;
8012 type = RECUR (DECL_INITIAL (instance));
8013 fixed_type_or_null_ref_ht->remove_elt (instance);
8015 return type;
8018 return NULL_TREE;
8020 case VIEW_CONVERT_EXPR:
8021 if (location_wrapper_p (instance))
8022 return RECUR (TREE_OPERAND (instance, 0));
8023 else
8024 /* TODO: Recursion may be correct for some non-location-wrapper
8025 uses of VIEW_CONVERT_EXPR. */
8026 return NULL_TREE;
8028 default:
8029 return NULL_TREE;
8031 #undef RECUR
8034 /* Return nonzero if the dynamic type of INSTANCE is known, and
8035 equivalent to the static type. We also handle the case where
8036 INSTANCE is really a pointer. Return negative if this is a
8037 ctor/dtor. There the dynamic type is known, but this might not be
8038 the most derived base of the original object, and hence virtual
8039 bases may not be laid out according to this type.
8041 Used to determine whether the virtual function table is needed
8042 or not.
8044 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8045 of our knowledge of its type. *NONNULL should be initialized
8046 before this function is called. */
8049 resolves_to_fixed_type_p (tree instance, int* nonnull)
8051 tree t = TREE_TYPE (instance);
8052 int cdtorp = 0;
8053 tree fixed;
8055 /* processing_template_decl can be false in a template if we're in
8056 instantiate_non_dependent_expr, but we still want to suppress
8057 this check. */
8058 if (in_template_function ())
8060 /* In a template we only care about the type of the result. */
8061 if (nonnull)
8062 *nonnull = true;
8063 return true;
8066 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
8067 if (INDIRECT_TYPE_P (t))
8068 t = TREE_TYPE (t);
8069 if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t))
8070 return 1;
8071 if (fixed == NULL_TREE)
8072 return 0;
8073 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
8074 return 0;
8075 return cdtorp ? -1 : 1;
8079 void
8080 init_class_processing (void)
8082 current_class_depth = 0;
8083 current_class_stack_size = 10;
8084 current_class_stack
8085 = XNEWVEC (struct class_stack_node, current_class_stack_size);
8086 sizeof_biggest_empty_class = size_zero_node;
8088 ridpointers[(int) RID_PUBLIC] = access_public_node;
8089 ridpointers[(int) RID_PRIVATE] = access_private_node;
8090 ridpointers[(int) RID_PROTECTED] = access_protected_node;
8093 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
8095 static void
8096 restore_class_cache (void)
8098 tree type;
8100 /* We are re-entering the same class we just left, so we don't
8101 have to search the whole inheritance matrix to find all the
8102 decls to bind again. Instead, we install the cached
8103 class_shadowed list and walk through it binding names. */
8104 push_binding_level (previous_class_level);
8105 class_binding_level = previous_class_level;
8106 /* Restore IDENTIFIER_TYPE_VALUE. */
8107 for (type = class_binding_level->type_shadowed;
8108 type;
8109 type = TREE_CHAIN (type))
8110 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
8113 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
8114 appropriate for TYPE.
8116 So that we may avoid calls to lookup_name, we cache the _TYPE
8117 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
8119 For multiple inheritance, we perform a two-pass depth-first search
8120 of the type lattice. */
8122 void
8123 pushclass (tree type)
8125 class_stack_node_t csn;
8127 type = TYPE_MAIN_VARIANT (type);
8129 /* Make sure there is enough room for the new entry on the stack. */
8130 if (current_class_depth + 1 >= current_class_stack_size)
8132 current_class_stack_size *= 2;
8133 current_class_stack
8134 = XRESIZEVEC (struct class_stack_node, current_class_stack,
8135 current_class_stack_size);
8138 /* Insert a new entry on the class stack. */
8139 csn = current_class_stack + current_class_depth;
8140 csn->name = current_class_name;
8141 csn->type = current_class_type;
8142 csn->access = current_access_specifier;
8143 csn->names_used = 0;
8144 csn->hidden = 0;
8145 current_class_depth++;
8147 /* Now set up the new type. */
8148 current_class_name = TYPE_NAME (type);
8149 if (TREE_CODE (current_class_name) == TYPE_DECL)
8150 current_class_name = DECL_NAME (current_class_name);
8151 current_class_type = type;
8153 /* By default, things in classes are private, while things in
8154 structures or unions are public. */
8155 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
8156 ? access_private_node
8157 : access_public_node);
8159 if (previous_class_level
8160 && type != previous_class_level->this_entity
8161 && current_class_depth == 1)
8163 /* Forcibly remove any old class remnants. */
8164 invalidate_class_lookup_cache ();
8167 if (!previous_class_level
8168 || type != previous_class_level->this_entity
8169 || current_class_depth > 1)
8170 pushlevel_class ();
8171 else
8172 restore_class_cache ();
8175 /* Get out of the current class scope. If we were in a class scope
8176 previously, that is the one popped to. */
8178 void
8179 popclass (void)
8181 poplevel_class ();
8183 current_class_depth--;
8184 current_class_name = current_class_stack[current_class_depth].name;
8185 current_class_type = current_class_stack[current_class_depth].type;
8186 current_access_specifier = current_class_stack[current_class_depth].access;
8187 if (current_class_stack[current_class_depth].names_used)
8188 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8191 /* Mark the top of the class stack as hidden. */
8193 void
8194 push_class_stack (void)
8196 if (current_class_depth)
8197 ++current_class_stack[current_class_depth - 1].hidden;
8200 /* Mark the top of the class stack as un-hidden. */
8202 void
8203 pop_class_stack (void)
8205 if (current_class_depth)
8206 --current_class_stack[current_class_depth - 1].hidden;
8209 /* If the class type currently being defined is either T or
8210 a nested type of T, returns the type from the current_class_stack,
8211 which might be equivalent to but not equal to T in case of
8212 constrained partial specializations. */
8214 tree
8215 currently_open_class (tree t)
8217 int i;
8219 if (!CLASS_TYPE_P (t))
8220 return NULL_TREE;
8222 t = TYPE_MAIN_VARIANT (t);
8224 /* We start looking from 1 because entry 0 is from global scope,
8225 and has no type. */
8226 for (i = current_class_depth; i > 0; --i)
8228 tree c;
8229 if (i == current_class_depth)
8230 c = current_class_type;
8231 else
8233 if (current_class_stack[i].hidden)
8234 break;
8235 c = current_class_stack[i].type;
8237 if (!c)
8238 continue;
8239 if (same_type_p (c, t))
8240 return c;
8242 return NULL_TREE;
8245 /* If either current_class_type or one of its enclosing classes are derived
8246 from T, return the appropriate type. Used to determine how we found
8247 something via unqualified lookup. */
8249 tree
8250 currently_open_derived_class (tree t)
8252 int i;
8254 /* The bases of a dependent type are unknown. */
8255 if (dependent_type_p (t))
8256 return NULL_TREE;
8258 if (!current_class_type)
8259 return NULL_TREE;
8261 if (DERIVED_FROM_P (t, current_class_type))
8262 return current_class_type;
8264 for (i = current_class_depth - 1; i > 0; --i)
8266 if (current_class_stack[i].hidden)
8267 break;
8268 if (DERIVED_FROM_P (t, current_class_stack[i].type))
8269 return current_class_stack[i].type;
8272 return NULL_TREE;
8275 /* Return the outermost enclosing class type that is still open, or
8276 NULL_TREE. */
8278 tree
8279 outermost_open_class (void)
8281 if (!current_class_type)
8282 return NULL_TREE;
8283 tree r = NULL_TREE;
8284 if (TYPE_BEING_DEFINED (current_class_type))
8285 r = current_class_type;
8286 for (int i = current_class_depth - 1; i > 0; --i)
8288 if (current_class_stack[i].hidden)
8289 break;
8290 tree t = current_class_stack[i].type;
8291 if (!TYPE_BEING_DEFINED (t))
8292 break;
8293 r = t;
8295 return r;
8298 /* Returns the innermost class type which is not a lambda closure type. */
8300 tree
8301 current_nonlambda_class_type (void)
8303 tree type = current_class_type;
8304 while (type && LAMBDA_TYPE_P (type))
8305 type = decl_type_context (TYPE_NAME (type));
8306 return type;
8309 /* When entering a class scope, all enclosing class scopes' names with
8310 static meaning (static variables, static functions, types and
8311 enumerators) have to be visible. This recursive function calls
8312 pushclass for all enclosing class contexts until global or a local
8313 scope is reached. TYPE is the enclosed class. */
8315 void
8316 push_nested_class (tree type)
8318 /* A namespace might be passed in error cases, like A::B:C. */
8319 if (type == NULL_TREE
8320 || !CLASS_TYPE_P (type))
8321 return;
8323 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8325 pushclass (type);
8328 /* Undoes a push_nested_class call. */
8330 void
8331 pop_nested_class (void)
8333 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8335 popclass ();
8336 if (context && CLASS_TYPE_P (context))
8337 pop_nested_class ();
8340 /* Returns the number of extern "LANG" blocks we are nested within. */
8343 current_lang_depth (void)
8345 return vec_safe_length (current_lang_base);
8348 /* Set global variables CURRENT_LANG_NAME to appropriate value
8349 so that behavior of name-mangling machinery is correct. */
8351 void
8352 push_lang_context (tree name)
8354 vec_safe_push (current_lang_base, current_lang_name);
8356 if (name == lang_name_cplusplus)
8357 current_lang_name = name;
8358 else if (name == lang_name_c)
8359 current_lang_name = name;
8360 else
8361 error ("language string %<\"%E\"%> not recognized", name);
8364 /* Get out of the current language scope. */
8366 void
8367 pop_lang_context (void)
8369 current_lang_name = current_lang_base->pop ();
8372 /* Type instantiation routines. */
8374 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8375 matches the TARGET_TYPE. If there is no satisfactory match, return
8376 error_mark_node, and issue an error & warning messages under
8377 control of FLAGS. Permit pointers to member function if FLAGS
8378 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8379 a template-id, and EXPLICIT_TARGS are the explicitly provided
8380 template arguments.
8382 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8383 is the base path used to reference those member functions. If
8384 the address is resolved to a member function, access checks will be
8385 performed and errors issued if appropriate. */
8387 static tree
8388 resolve_address_of_overloaded_function (tree target_type,
8389 tree overload,
8390 tsubst_flags_t complain,
8391 bool template_only,
8392 tree explicit_targs,
8393 tree access_path)
8395 /* Here's what the standard says:
8397 [over.over]
8399 If the name is a function template, template argument deduction
8400 is done, and if the argument deduction succeeds, the deduced
8401 arguments are used to generate a single template function, which
8402 is added to the set of overloaded functions considered.
8404 Non-member functions and static member functions match targets of
8405 type "pointer-to-function" or "reference-to-function." Nonstatic
8406 member functions match targets of type "pointer-to-member
8407 function;" the function type of the pointer to member is used to
8408 select the member function from the set of overloaded member
8409 functions. If a non-static member function is selected, the
8410 reference to the overloaded function name is required to have the
8411 form of a pointer to member as described in 5.3.1.
8413 If more than one function is selected, any template functions in
8414 the set are eliminated if the set also contains a non-template
8415 function, and any given template function is eliminated if the
8416 set contains a second template function that is more specialized
8417 than the first according to the partial ordering rules 14.5.5.2.
8418 After such eliminations, if any, there shall remain exactly one
8419 selected function. */
8421 int is_ptrmem = 0;
8422 /* We store the matches in a TREE_LIST rooted here. The functions
8423 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8424 interoperability with most_specialized_instantiation. */
8425 tree matches = NULL_TREE;
8426 tree fn;
8427 tree target_fn_type;
8429 /* By the time we get here, we should be seeing only real
8430 pointer-to-member types, not the internal POINTER_TYPE to
8431 METHOD_TYPE representation. */
8432 gcc_assert (!TYPE_PTR_P (target_type)
8433 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8435 gcc_assert (is_overloaded_fn (overload));
8437 /* Check that the TARGET_TYPE is reasonable. */
8438 if (TYPE_PTRFN_P (target_type)
8439 || TYPE_REFFN_P (target_type))
8440 /* This is OK. */;
8441 else if (TYPE_PTRMEMFUNC_P (target_type))
8442 /* This is OK, too. */
8443 is_ptrmem = 1;
8444 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8445 /* This is OK, too. This comes from a conversion to reference
8446 type. */
8447 target_type = build_reference_type (target_type);
8448 else
8450 if (complain & tf_error)
8451 error ("cannot resolve overloaded function %qD based on"
8452 " conversion to type %qT",
8453 OVL_NAME (overload), target_type);
8454 return error_mark_node;
8457 /* Non-member functions and static member functions match targets of type
8458 "pointer-to-function" or "reference-to-function." Nonstatic member
8459 functions match targets of type "pointer-to-member-function;" the
8460 function type of the pointer to member is used to select the member
8461 function from the set of overloaded member functions.
8463 So figure out the FUNCTION_TYPE that we want to match against. */
8464 target_fn_type = static_fn_type (target_type);
8466 /* If we can find a non-template function that matches, we can just
8467 use it. There's no point in generating template instantiations
8468 if we're just going to throw them out anyhow. But, of course, we
8469 can only do this when we don't *need* a template function. */
8470 if (!template_only)
8471 for (lkp_iterator iter (overload); iter; ++iter)
8473 tree fn = *iter;
8475 if (TREE_CODE (fn) == TEMPLATE_DECL)
8476 /* We're not looking for templates just yet. */
8477 continue;
8479 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8480 /* We're looking for a non-static member, and this isn't
8481 one, or vice versa. */
8482 continue;
8484 /* Constraints must be satisfied. This is done before
8485 return type deduction since that instantiates the
8486 function. */
8487 if (!constraints_satisfied_p (fn))
8488 continue;
8490 if (undeduced_auto_decl (fn))
8492 /* Force instantiation to do return type deduction. */
8493 maybe_instantiate_decl (fn);
8494 require_deduced_type (fn);
8497 /* In C++17 we need the noexcept-qualifier to compare types. */
8498 if (flag_noexcept_type
8499 && !maybe_instantiate_noexcept (fn, complain))
8500 continue;
8502 /* See if there's a match. */
8503 tree fntype = static_fn_type (fn);
8504 if (same_type_p (target_fn_type, fntype)
8505 || fnptr_conv_p (target_fn_type, fntype))
8506 matches = tree_cons (fn, NULL_TREE, matches);
8509 /* Now, if we've already got a match (or matches), there's no need
8510 to proceed to the template functions. But, if we don't have a
8511 match we need to look at them, too. */
8512 if (!matches)
8514 tree target_arg_types;
8515 tree target_ret_type;
8516 tree *args;
8517 unsigned int nargs, ia;
8518 tree arg;
8520 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8521 target_ret_type = TREE_TYPE (target_fn_type);
8523 nargs = list_length (target_arg_types);
8524 args = XALLOCAVEC (tree, nargs);
8525 for (arg = target_arg_types, ia = 0;
8526 arg != NULL_TREE;
8527 arg = TREE_CHAIN (arg), ++ia)
8528 args[ia] = TREE_VALUE (arg);
8529 nargs = ia;
8531 for (lkp_iterator iter (overload); iter; ++iter)
8533 tree fn = *iter;
8534 tree instantiation;
8535 tree targs;
8537 if (TREE_CODE (fn) != TEMPLATE_DECL)
8538 /* We're only looking for templates. */
8539 continue;
8541 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8542 != is_ptrmem)
8543 /* We're not looking for a non-static member, and this is
8544 one, or vice versa. */
8545 continue;
8547 tree ret = target_ret_type;
8549 /* If the template has a deduced return type, don't expose it to
8550 template argument deduction. */
8551 if (undeduced_auto_decl (fn))
8552 ret = NULL_TREE;
8554 /* Try to do argument deduction. */
8555 targs = make_tree_vec (DECL_NTPARMS (fn));
8556 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8557 nargs, ret,
8558 DEDUCE_EXACT, LOOKUP_NORMAL,
8559 NULL, false, false);
8560 if (instantiation == error_mark_node)
8561 /* Instantiation failed. */
8562 continue;
8564 /* Constraints must be satisfied. This is done before
8565 return type deduction since that instantiates the
8566 function. */
8567 if (flag_concepts && !constraints_satisfied_p (instantiation))
8568 continue;
8570 /* And now force instantiation to do return type deduction. */
8571 if (undeduced_auto_decl (instantiation))
8573 ++function_depth;
8574 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8575 --function_depth;
8577 require_deduced_type (instantiation);
8580 /* In C++17 we need the noexcept-qualifier to compare types. */
8581 if (flag_noexcept_type)
8582 maybe_instantiate_noexcept (instantiation, complain);
8584 /* See if there's a match. */
8585 tree fntype = static_fn_type (instantiation);
8586 if (same_type_p (target_fn_type, fntype)
8587 || fnptr_conv_p (target_fn_type, fntype))
8588 matches = tree_cons (instantiation, fn, matches);
8591 /* Now, remove all but the most specialized of the matches. */
8592 if (matches)
8594 tree match = most_specialized_instantiation (matches);
8596 if (match != error_mark_node)
8597 matches = tree_cons (TREE_PURPOSE (match),
8598 NULL_TREE,
8599 NULL_TREE);
8603 /* Now we should have exactly one function in MATCHES. */
8604 if (matches == NULL_TREE)
8606 /* There were *no* matches. */
8607 if (complain & tf_error)
8609 error ("no matches converting function %qD to type %q#T",
8610 OVL_NAME (overload), target_type);
8612 print_candidates (overload);
8614 return error_mark_node;
8616 else if (TREE_CHAIN (matches))
8618 /* There were too many matches. First check if they're all
8619 the same function. */
8620 tree match = NULL_TREE;
8622 fn = TREE_PURPOSE (matches);
8624 /* For multi-versioned functions, more than one match is just fine and
8625 decls_match will return false as they are different. */
8626 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8627 if (!decls_match (fn, TREE_PURPOSE (match))
8628 && !targetm.target_option.function_versions
8629 (fn, TREE_PURPOSE (match)))
8630 break;
8632 if (match)
8634 if (complain & tf_error)
8636 error ("converting overloaded function %qD to type %q#T is ambiguous",
8637 OVL_NAME (overload), target_type);
8639 /* Since print_candidates expects the functions in the
8640 TREE_VALUE slot, we flip them here. */
8641 for (match = matches; match; match = TREE_CHAIN (match))
8642 TREE_VALUE (match) = TREE_PURPOSE (match);
8644 print_candidates (matches);
8647 return error_mark_node;
8651 /* Good, exactly one match. Now, convert it to the correct type. */
8652 fn = TREE_PURPOSE (matches);
8654 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8655 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8657 static int explained;
8659 if (!(complain & tf_error))
8660 return error_mark_node;
8662 auto_diagnostic_group d;
8663 if (permerror (input_location, "assuming pointer to member %qD", fn)
8664 && !explained)
8666 inform (input_location, "(a pointer to member can only be "
8667 "formed with %<&%E%>)", fn);
8668 explained = 1;
8672 /* If a pointer to a function that is multi-versioned is requested, the
8673 pointer to the dispatcher function is returned instead. This works
8674 well because indirectly calling the function will dispatch the right
8675 function version at run-time. */
8676 if (DECL_FUNCTION_VERSIONED (fn))
8678 fn = get_function_version_dispatcher (fn);
8679 if (fn == NULL)
8680 return error_mark_node;
8681 /* Mark all the versions corresponding to the dispatcher as used. */
8682 if (!(complain & tf_conv))
8683 mark_versions_used (fn);
8686 /* If we're doing overload resolution purely for the purpose of
8687 determining conversion sequences, we should not consider the
8688 function used. If this conversion sequence is selected, the
8689 function will be marked as used at this point. */
8690 if (!(complain & tf_conv))
8692 /* Make =delete work with SFINAE. */
8693 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8694 return error_mark_node;
8695 if (!mark_used (fn, complain) && !(complain & tf_error))
8696 return error_mark_node;
8699 /* We could not check access to member functions when this
8700 expression was originally created since we did not know at that
8701 time to which function the expression referred. */
8702 if (DECL_FUNCTION_MEMBER_P (fn))
8704 gcc_assert (access_path);
8705 perform_or_defer_access_check (access_path, fn, fn, complain);
8708 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8709 return cp_build_addr_expr (fn, complain);
8710 else
8712 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8713 will mark the function as addressed, but here we must do it
8714 explicitly. */
8715 cxx_mark_addressable (fn);
8717 return fn;
8721 /* This function will instantiate the type of the expression given in
8722 RHS to match the type of LHSTYPE. If errors exist, then return
8723 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8724 we complain on errors. If we are not complaining, never modify rhs,
8725 as overload resolution wants to try many possible instantiations, in
8726 the hope that at least one will work.
8728 For non-recursive calls, LHSTYPE should be a function, pointer to
8729 function, or a pointer to member function. */
8731 tree
8732 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8734 tsubst_flags_t complain_in = complain;
8735 tree access_path = NULL_TREE;
8737 complain &= ~tf_ptrmem_ok;
8739 STRIP_ANY_LOCATION_WRAPPER (rhs);
8741 if (lhstype == unknown_type_node)
8743 if (complain & tf_error)
8744 error ("not enough type information");
8745 return error_mark_node;
8748 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8750 tree fntype = non_reference (lhstype);
8751 if (same_type_p (fntype, TREE_TYPE (rhs)))
8752 return rhs;
8753 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8754 return rhs;
8755 if (flag_ms_extensions
8756 && TYPE_PTRMEMFUNC_P (fntype)
8757 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8758 /* Microsoft allows `A::f' to be resolved to a
8759 pointer-to-member. */
8761 else
8763 if (complain & tf_error)
8764 error ("cannot convert %qE from type %qT to type %qT",
8765 rhs, TREE_TYPE (rhs), fntype);
8766 return error_mark_node;
8770 /* If we instantiate a template, and it is a A ?: C expression
8771 with omitted B, look through the SAVE_EXPR. */
8772 if (TREE_CODE (rhs) == SAVE_EXPR)
8773 rhs = TREE_OPERAND (rhs, 0);
8775 if (BASELINK_P (rhs))
8777 access_path = BASELINK_ACCESS_BINFO (rhs);
8778 rhs = BASELINK_FUNCTIONS (rhs);
8781 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8782 deduce any type information. */
8783 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8785 if (complain & tf_error)
8786 error ("not enough type information");
8787 return error_mark_node;
8790 /* There are only a few kinds of expressions that may have a type
8791 dependent on overload resolution. */
8792 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8793 || TREE_CODE (rhs) == COMPONENT_REF
8794 || is_overloaded_fn (rhs)
8795 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8797 /* This should really only be used when attempting to distinguish
8798 what sort of a pointer to function we have. For now, any
8799 arithmetic operation which is not supported on pointers
8800 is rejected as an error. */
8802 switch (TREE_CODE (rhs))
8804 case COMPONENT_REF:
8806 tree member = TREE_OPERAND (rhs, 1);
8808 member = instantiate_type (lhstype, member, complain);
8809 if (member != error_mark_node
8810 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8811 /* Do not lose object's side effects. */
8812 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8813 TREE_OPERAND (rhs, 0), member);
8814 return member;
8817 case OFFSET_REF:
8818 rhs = TREE_OPERAND (rhs, 1);
8819 if (BASELINK_P (rhs))
8820 return instantiate_type (lhstype, rhs, complain_in);
8822 /* This can happen if we are forming a pointer-to-member for a
8823 member template. */
8824 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8826 /* Fall through. */
8828 case TEMPLATE_ID_EXPR:
8830 tree fns = TREE_OPERAND (rhs, 0);
8831 tree args = TREE_OPERAND (rhs, 1);
8833 return
8834 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8835 /*template_only=*/true,
8836 args, access_path);
8839 case OVERLOAD:
8840 case FUNCTION_DECL:
8841 return
8842 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8843 /*template_only=*/false,
8844 /*explicit_targs=*/NULL_TREE,
8845 access_path);
8847 case ADDR_EXPR:
8849 if (PTRMEM_OK_P (rhs))
8850 complain |= tf_ptrmem_ok;
8852 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8855 case ERROR_MARK:
8856 return error_mark_node;
8858 default:
8859 gcc_unreachable ();
8861 return error_mark_node;
8864 /* Return the name of the virtual function pointer field
8865 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8866 this may have to look back through base types to find the
8867 ultimate field name. (For single inheritance, these could
8868 all be the same name. Who knows for multiple inheritance). */
8870 static tree
8871 get_vfield_name (tree type)
8873 tree binfo, base_binfo;
8875 for (binfo = TYPE_BINFO (type);
8876 BINFO_N_BASE_BINFOS (binfo);
8877 binfo = base_binfo)
8879 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8881 if (BINFO_VIRTUAL_P (base_binfo)
8882 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8883 break;
8886 type = BINFO_TYPE (binfo);
8887 tree ctor_name = constructor_name (type);
8888 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8889 + IDENTIFIER_LENGTH (ctor_name) + 2);
8890 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8891 return get_identifier (buf);
8894 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8895 according to [class]:
8896 The class-name is also inserted
8897 into the scope of the class itself. For purposes of access checking,
8898 the inserted class name is treated as if it were a public member name. */
8900 void
8901 build_self_reference (void)
8903 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8904 tree decl = build_lang_decl (TYPE_DECL, name, current_class_type);
8906 DECL_NONLOCAL (decl) = 1;
8907 DECL_CONTEXT (decl) = current_class_type;
8908 DECL_ARTIFICIAL (decl) = 1;
8909 SET_DECL_SELF_REFERENCE_P (decl);
8910 set_underlying_type (decl);
8911 set_instantiating_module (decl);
8913 if (processing_template_decl)
8914 decl = push_template_decl (decl);
8916 tree saved_cas = current_access_specifier;
8917 current_access_specifier = access_public_node;
8918 finish_member_declaration (decl);
8919 current_access_specifier = saved_cas;
8922 /* Returns 1 if TYPE contains only padding bytes. */
8925 is_empty_class (tree type)
8927 if (type == error_mark_node)
8928 return 0;
8930 if (! CLASS_TYPE_P (type))
8931 return 0;
8933 return CLASSTYPE_EMPTY_P (type);
8936 /* Returns true if TYPE contains no actual data, just various
8937 possible combinations of empty classes. If IGNORE_VPTR is true,
8938 a vptr doesn't prevent the class from being considered empty. Typically
8939 we want to ignore the vptr on assignment, and not on initialization. */
8941 bool
8942 is_really_empty_class (tree type, bool ignore_vptr)
8944 if (CLASS_TYPE_P (type))
8946 tree field;
8947 tree binfo;
8948 tree base_binfo;
8949 int i;
8951 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8952 out, but we'd like to be able to check this before then. */
8953 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8954 return true;
8956 if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
8957 return false;
8959 for (binfo = TYPE_BINFO (type), i = 0;
8960 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8961 if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
8962 return false;
8963 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8964 if (TREE_CODE (field) == FIELD_DECL
8965 && !DECL_ARTIFICIAL (field)
8966 /* An unnamed bit-field is not a data member. */
8967 && !DECL_UNNAMED_BIT_FIELD (field)
8968 && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
8969 return false;
8970 return true;
8972 else if (TREE_CODE (type) == ARRAY_TYPE)
8973 return (integer_zerop (array_type_nelts_top (type))
8974 || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
8975 return false;
8978 /* Note that NAME was looked up while the current class was being
8979 defined and that the result of that lookup was DECL. */
8981 void
8982 maybe_note_name_used_in_class (tree name, tree decl)
8984 /* If we're not defining a class, there's nothing to do. */
8985 if (!(innermost_scope_kind() == sk_class
8986 && TYPE_BEING_DEFINED (current_class_type)
8987 && !LAMBDA_TYPE_P (current_class_type)))
8988 return;
8990 const cp_binding_level *blev = nullptr;
8991 if (const cxx_binding *binding = IDENTIFIER_BINDING (name))
8992 blev = binding->scope;
8993 const cp_binding_level *lev = current_binding_level;
8995 /* Record the binding in the names_used tables for classes inside blev. */
8996 for (int i = current_class_depth; i > 0; --i)
8998 tree type = (i == current_class_depth
8999 ? current_class_type
9000 : current_class_stack[i].type);
9002 for (; lev; lev = lev->level_chain)
9004 if (lev == blev)
9005 /* We found the declaration. */
9006 return;
9007 if (lev->kind == sk_class && lev->this_entity == type)
9008 /* This class is inside the declaration scope. */
9009 break;
9012 auto &names_used = current_class_stack[i-1].names_used;
9013 if (!names_used)
9014 names_used = splay_tree_new (splay_tree_compare_pointers, 0, 0);
9016 tree use = build1_loc (input_location, VIEW_CONVERT_EXPR,
9017 TREE_TYPE (decl), decl);
9018 EXPR_LOCATION_WRAPPER_P (use) = 1;
9019 splay_tree_insert (names_used,
9020 (splay_tree_key) name,
9021 (splay_tree_value) use);
9025 /* Note that NAME was declared (as DECL) in the current class. Check
9026 to see that the declaration is valid under [class.member.lookup]:
9028 If [the result of a search in T for N at point P] differs from the result of
9029 a search in T for N from immediately after the class-specifier of T, the
9030 program is ill-formed, no diagnostic required. */
9032 void
9033 note_name_declared_in_class (tree name, tree decl)
9035 splay_tree names_used;
9036 splay_tree_node n;
9038 /* Look to see if we ever used this name. */
9039 names_used
9040 = current_class_stack[current_class_depth - 1].names_used;
9041 if (!names_used)
9042 return;
9043 /* The C language allows members to be declared with a type of the same
9044 name, and the C++ standard says this diagnostic is not required. So
9045 allow it in extern "C" blocks unless pedantic is specified.
9046 Allow it in all cases if -ms-extensions is specified. */
9047 if ((!pedantic && current_lang_name == lang_name_c)
9048 || flag_ms_extensions)
9049 return;
9050 n = splay_tree_lookup (names_used, (splay_tree_key) name);
9051 if (n)
9053 tree use = (tree) n->value;
9054 location_t loc = EXPR_LOCATION (use);
9055 tree olddecl = OVL_FIRST (TREE_OPERAND (use, 0));
9056 /* [basic.scope.class]
9058 A name N used in a class S shall refer to the same declaration
9059 in its context and when re-evaluated in the completed scope of
9060 S. */
9061 auto ov = make_temp_override (global_dc->pedantic_errors);
9062 if (TREE_CODE (decl) == TYPE_DECL
9063 && TREE_CODE (olddecl) == TYPE_DECL
9064 && same_type_p (TREE_TYPE (decl), TREE_TYPE (olddecl)))
9065 /* Different declaration, but same meaning; just warn. */;
9066 else if (flag_permissive)
9067 /* Let -fpermissive make it a warning like past versions. */;
9068 else
9069 /* Make it an error. */
9070 global_dc->pedantic_errors = 1;
9071 if (pedwarn (location_of (decl), OPT_Wchanges_meaning,
9072 "declaration of %q#D changes meaning of %qD",
9073 decl, OVL_NAME (decl)))
9075 inform (loc, "used here to mean %q#D", olddecl);
9076 inform (location_of (olddecl), "declared here" );
9081 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
9082 Secondary vtables are merged with primary vtables; this function
9083 will return the VAR_DECL for the primary vtable. */
9085 tree
9086 get_vtbl_decl_for_binfo (tree binfo)
9088 tree decl;
9090 decl = BINFO_VTABLE (binfo);
9091 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
9093 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
9094 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
9096 if (decl)
9097 gcc_assert (VAR_P (decl));
9098 return decl;
9102 /* Returns the binfo for the primary base of BINFO. If the resulting
9103 BINFO is a virtual base, and it is inherited elsewhere in the
9104 hierarchy, then the returned binfo might not be the primary base of
9105 BINFO in the complete object. Check BINFO_PRIMARY_P or
9106 BINFO_LOST_PRIMARY_P to be sure. */
9108 static tree
9109 get_primary_binfo (tree binfo)
9111 tree primary_base;
9113 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
9114 if (!primary_base)
9115 return NULL_TREE;
9117 return copied_binfo (primary_base, binfo);
9120 /* As above, but iterate until we reach the binfo that actually provides the
9121 vptr for BINFO. */
9123 static tree
9124 most_primary_binfo (tree binfo)
9126 tree b = binfo;
9127 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9128 && !BINFO_LOST_PRIMARY_P (b))
9130 tree primary_base = get_primary_binfo (b);
9131 gcc_assert (BINFO_PRIMARY_P (primary_base)
9132 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9133 b = primary_base;
9135 return b;
9138 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
9139 type. Note that the virtual inheritance might be above or below BINFO in
9140 the hierarchy. */
9142 bool
9143 vptr_via_virtual_p (tree binfo)
9145 if (TYPE_P (binfo))
9146 binfo = TYPE_BINFO (binfo);
9147 tree primary = most_primary_binfo (binfo);
9148 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
9149 a morally virtual base. */
9150 tree virt = binfo_via_virtual (primary, NULL_TREE);
9151 return virt != NULL_TREE;
9154 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
9156 static int
9157 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
9159 if (!indented_p)
9160 fprintf (stream, "%*s", indent, "");
9161 return 1;
9164 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
9165 INDENT should be zero when called from the top level; it is
9166 incremented recursively. IGO indicates the next expected BINFO in
9167 inheritance graph ordering. */
9169 static tree
9170 dump_class_hierarchy_r (FILE *stream,
9171 dump_flags_t flags,
9172 tree binfo,
9173 tree igo,
9174 int indent)
9176 int indented = 0;
9177 tree base_binfo;
9178 int i;
9180 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
9181 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
9182 (HOST_WIDE_INT) (uintptr_t) binfo);
9183 if (binfo != igo)
9185 fprintf (stream, "alternative-path\n");
9186 return igo;
9188 igo = TREE_CHAIN (binfo);
9190 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
9191 tree_to_shwi (BINFO_OFFSET (binfo)));
9192 if (is_empty_class (BINFO_TYPE (binfo)))
9193 fprintf (stream, " empty");
9194 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
9195 fprintf (stream, " nearly-empty");
9196 if (BINFO_VIRTUAL_P (binfo))
9197 fprintf (stream, " virtual");
9198 fprintf (stream, "\n");
9200 if (BINFO_PRIMARY_P (binfo))
9202 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9203 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
9204 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
9205 TFF_PLAIN_IDENTIFIER),
9206 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
9208 if (BINFO_LOST_PRIMARY_P (binfo))
9210 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9211 fprintf (stream, " lost-primary");
9213 if (indented)
9214 fprintf (stream, "\n");
9216 if (!(flags & TDF_SLIM))
9218 int indented = 0;
9220 if (BINFO_SUBVTT_INDEX (binfo))
9222 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9223 fprintf (stream, " subvttidx=%s",
9224 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
9225 TFF_PLAIN_IDENTIFIER));
9227 if (BINFO_VPTR_INDEX (binfo))
9229 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9230 fprintf (stream, " vptridx=%s",
9231 expr_as_string (BINFO_VPTR_INDEX (binfo),
9232 TFF_PLAIN_IDENTIFIER));
9234 if (BINFO_VPTR_FIELD (binfo))
9236 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9237 fprintf (stream, " vbaseoffset=%s",
9238 expr_as_string (BINFO_VPTR_FIELD (binfo),
9239 TFF_PLAIN_IDENTIFIER));
9241 if (BINFO_VTABLE (binfo))
9243 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9244 fprintf (stream, " vptr=%s",
9245 expr_as_string (BINFO_VTABLE (binfo),
9246 TFF_PLAIN_IDENTIFIER));
9249 if (indented)
9250 fprintf (stream, "\n");
9253 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9254 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
9256 return igo;
9259 /* Dump the BINFO hierarchy for T. */
9261 static void
9262 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
9264 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9265 fprintf (stream, " size=%lu align=%lu\n",
9266 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
9267 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
9268 if (tree as_base = CLASSTYPE_AS_BASE (t))
9269 fprintf (stream, " base size=%lu base align=%lu\n",
9270 (unsigned long)(tree_to_shwi (TYPE_SIZE (as_base))
9271 / BITS_PER_UNIT),
9272 (unsigned long)(TYPE_ALIGN (as_base) / BITS_PER_UNIT));
9273 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
9274 fprintf (stream, "\n");
9277 /* Debug interface to hierarchy dumping. */
9279 void
9280 debug_class (tree t)
9282 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
9285 static void
9286 dump_class_hierarchy (tree t)
9288 dump_flags_t flags;
9289 if (FILE *stream = dump_begin (class_dump_id, &flags))
9291 dump_class_hierarchy_1 (stream, flags, t);
9292 dump_end (class_dump_id, stream);
9296 static void
9297 dump_array (FILE * stream, tree decl)
9299 tree value;
9300 unsigned HOST_WIDE_INT ix;
9301 HOST_WIDE_INT elt;
9302 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
9304 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
9305 / BITS_PER_UNIT);
9306 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
9307 fprintf (stream, " %s entries",
9308 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
9309 TFF_PLAIN_IDENTIFIER));
9310 fprintf (stream, "\n");
9312 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
9313 ix, value)
9314 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
9315 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
9318 static void
9319 dump_vtable (tree t, tree binfo, tree vtable)
9321 dump_flags_t flags;
9322 FILE *stream = dump_begin (class_dump_id, &flags);
9324 if (!stream)
9325 return;
9327 if (!(flags & TDF_SLIM))
9329 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9331 fprintf (stream, "%s for %s",
9332 ctor_vtbl_p ? "Construction vtable" : "Vtable",
9333 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
9334 if (ctor_vtbl_p)
9336 if (!BINFO_VIRTUAL_P (binfo))
9337 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
9338 (HOST_WIDE_INT) (uintptr_t) binfo);
9339 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9341 fprintf (stream, "\n");
9342 dump_array (stream, vtable);
9343 fprintf (stream, "\n");
9346 dump_end (class_dump_id, stream);
9349 static void
9350 dump_vtt (tree t, tree vtt)
9352 dump_flags_t flags;
9353 FILE *stream = dump_begin (class_dump_id, &flags);
9355 if (!stream)
9356 return;
9358 if (!(flags & TDF_SLIM))
9360 fprintf (stream, "VTT for %s\n",
9361 type_as_string (t, TFF_PLAIN_IDENTIFIER));
9362 dump_array (stream, vtt);
9363 fprintf (stream, "\n");
9366 dump_end (class_dump_id, stream);
9369 /* Dump a function or thunk and its thunkees. */
9371 static void
9372 dump_thunk (FILE *stream, int indent, tree thunk)
9374 static const char spaces[] = " ";
9375 tree name = DECL_NAME (thunk);
9376 tree thunks;
9378 fprintf (stream, "%.*s%p %s %s", indent, spaces,
9379 (void *)thunk,
9380 !DECL_THUNK_P (thunk) ? "function"
9381 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9382 name ? IDENTIFIER_POINTER (name) : "<unset>");
9383 if (DECL_THUNK_P (thunk))
9385 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9386 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9388 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9389 if (!virtual_adjust)
9390 /*NOP*/;
9391 else if (DECL_THIS_THUNK_P (thunk))
9392 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9393 tree_to_shwi (virtual_adjust));
9394 else
9395 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9396 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9397 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9398 if (THUNK_ALIAS (thunk))
9399 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9401 fprintf (stream, "\n");
9402 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9403 dump_thunk (stream, indent + 2, thunks);
9406 /* Dump the thunks for FN. */
9408 void
9409 debug_thunks (tree fn)
9411 dump_thunk (stderr, 0, fn);
9414 /* Virtual function table initialization. */
9416 /* Create all the necessary vtables for T and its base classes. */
9418 static void
9419 finish_vtbls (tree t)
9421 tree vbase;
9422 vec<constructor_elt, va_gc> *v = NULL;
9423 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9425 /* We lay out the primary and secondary vtables in one contiguous
9426 vtable. The primary vtable is first, followed by the non-virtual
9427 secondary vtables in inheritance graph order. */
9428 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9429 vtable, t, &v);
9431 /* Then come the virtual bases, also in inheritance graph order. */
9432 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9434 if (!BINFO_VIRTUAL_P (vbase))
9435 continue;
9436 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9439 if (BINFO_VTABLE (TYPE_BINFO (t)))
9440 initialize_vtable (TYPE_BINFO (t), v);
9443 /* Initialize the vtable for BINFO with the INITS. */
9445 static void
9446 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9448 tree decl;
9450 layout_vtable_decl (binfo, vec_safe_length (inits));
9451 decl = get_vtbl_decl_for_binfo (binfo);
9452 initialize_artificial_var (decl, inits);
9453 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9456 /* Build the VTT (virtual table table) for T.
9457 A class requires a VTT if it has virtual bases.
9459 This holds
9460 1 - primary virtual pointer for complete object T
9461 2 - secondary VTTs for each direct non-virtual base of T which requires a
9463 3 - secondary virtual pointers for each direct or indirect base of T which
9464 has virtual bases or is reachable via a virtual path from T.
9465 4 - secondary VTTs for each direct or indirect virtual base of T.
9467 Secondary VTTs look like complete object VTTs without part 4. */
9469 static void
9470 build_vtt (tree t)
9472 tree type;
9473 tree vtt;
9474 tree index;
9475 vec<constructor_elt, va_gc> *inits;
9477 /* Build up the initializers for the VTT. */
9478 inits = NULL;
9479 index = size_zero_node;
9480 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9482 /* If we didn't need a VTT, we're done. */
9483 if (!inits)
9484 return;
9486 /* Figure out the type of the VTT. */
9487 type = build_array_of_n_type (const_ptr_type_node,
9488 inits->length ());
9490 /* Now, build the VTT object itself. */
9491 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9492 initialize_artificial_var (vtt, inits);
9493 /* Add the VTT to the vtables list. */
9494 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9495 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9497 dump_vtt (t, vtt);
9500 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9501 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9502 and CHAIN the vtable pointer for this binfo after construction is
9503 complete. VALUE can also be another BINFO, in which case we recurse. */
9505 static tree
9506 binfo_ctor_vtable (tree binfo)
9508 tree vt;
9510 while (1)
9512 vt = BINFO_VTABLE (binfo);
9513 if (TREE_CODE (vt) == TREE_LIST)
9514 vt = TREE_VALUE (vt);
9515 if (TREE_CODE (vt) == TREE_BINFO)
9516 binfo = vt;
9517 else
9518 break;
9521 return vt;
9524 /* Data for secondary VTT initialization. */
9525 struct secondary_vptr_vtt_init_data
9527 /* Is this the primary VTT? */
9528 bool top_level_p;
9530 /* Current index into the VTT. */
9531 tree index;
9533 /* Vector of initializers built up. */
9534 vec<constructor_elt, va_gc> *inits;
9536 /* The type being constructed by this secondary VTT. */
9537 tree type_being_constructed;
9540 /* Recursively build the VTT-initializer for BINFO (which is in the
9541 hierarchy dominated by T). INITS points to the end of the initializer
9542 list to date. INDEX is the VTT index where the next element will be
9543 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9544 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9545 for virtual bases of T. When it is not so, we build the constructor
9546 vtables for the BINFO-in-T variant. */
9548 static void
9549 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9550 tree *index)
9552 int i;
9553 tree b;
9554 tree init;
9555 secondary_vptr_vtt_init_data data;
9556 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9558 /* We only need VTTs for subobjects with virtual bases. */
9559 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9560 return;
9562 /* We need to use a construction vtable if this is not the primary
9563 VTT. */
9564 if (!top_level_p)
9566 build_ctor_vtbl_group (binfo, t);
9568 /* Record the offset in the VTT where this sub-VTT can be found. */
9569 BINFO_SUBVTT_INDEX (binfo) = *index;
9572 /* Add the address of the primary vtable for the complete object. */
9573 init = binfo_ctor_vtable (binfo);
9574 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9575 if (top_level_p)
9577 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9578 BINFO_VPTR_INDEX (binfo) = *index;
9580 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9582 /* Recursively add the secondary VTTs for non-virtual bases. */
9583 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9584 if (!BINFO_VIRTUAL_P (b))
9585 build_vtt_inits (b, t, inits, index);
9587 /* Add secondary virtual pointers for all subobjects of BINFO with
9588 either virtual bases or reachable along a virtual path, except
9589 subobjects that are non-virtual primary bases. */
9590 data.top_level_p = top_level_p;
9591 data.index = *index;
9592 data.inits = *inits;
9593 data.type_being_constructed = BINFO_TYPE (binfo);
9595 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9597 *index = data.index;
9599 /* data.inits might have grown as we added secondary virtual pointers.
9600 Make sure our caller knows about the new vector. */
9601 *inits = data.inits;
9603 if (top_level_p)
9604 /* Add the secondary VTTs for virtual bases in inheritance graph
9605 order. */
9606 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9608 if (!BINFO_VIRTUAL_P (b))
9609 continue;
9611 build_vtt_inits (b, t, inits, index);
9613 else
9614 /* Remove the ctor vtables we created. */
9615 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9618 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9619 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9621 static tree
9622 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9624 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9626 /* We don't care about bases that don't have vtables. */
9627 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9628 return dfs_skip_bases;
9630 /* We're only interested in proper subobjects of the type being
9631 constructed. */
9632 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9633 return NULL_TREE;
9635 /* We're only interested in bases with virtual bases or reachable
9636 via a virtual path from the type being constructed. */
9637 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9638 || binfo_via_virtual (binfo, data->type_being_constructed)))
9639 return dfs_skip_bases;
9641 /* We're not interested in non-virtual primary bases. */
9642 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9643 return NULL_TREE;
9645 /* Record the index where this secondary vptr can be found. */
9646 if (data->top_level_p)
9648 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9649 BINFO_VPTR_INDEX (binfo) = data->index;
9651 if (BINFO_VIRTUAL_P (binfo))
9653 /* It's a primary virtual base, and this is not a
9654 construction vtable. Find the base this is primary of in
9655 the inheritance graph, and use that base's vtable
9656 now. */
9657 while (BINFO_PRIMARY_P (binfo))
9658 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9662 /* Add the initializer for the secondary vptr itself. */
9663 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9665 /* Advance the vtt index. */
9666 data->index = size_binop (PLUS_EXPR, data->index,
9667 TYPE_SIZE_UNIT (ptr_type_node));
9669 return NULL_TREE;
9672 /* Called from build_vtt_inits via dfs_walk. After building
9673 constructor vtables and generating the sub-vtt from them, we need
9674 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9675 binfo of the base whose sub vtt was generated. */
9677 static tree
9678 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9680 tree vtable = BINFO_VTABLE (binfo);
9682 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9683 /* If this class has no vtable, none of its bases do. */
9684 return dfs_skip_bases;
9686 if (!vtable)
9687 /* This might be a primary base, so have no vtable in this
9688 hierarchy. */
9689 return NULL_TREE;
9691 /* If we scribbled the construction vtable vptr into BINFO, clear it
9692 out now. */
9693 if (TREE_CODE (vtable) == TREE_LIST
9694 && (TREE_PURPOSE (vtable) == (tree) data))
9695 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9697 return NULL_TREE;
9700 /* Build the construction vtable group for BINFO which is in the
9701 hierarchy dominated by T. */
9703 static void
9704 build_ctor_vtbl_group (tree binfo, tree t)
9706 tree type;
9707 tree vtbl;
9708 tree id;
9709 tree vbase;
9710 vec<constructor_elt, va_gc> *v;
9712 /* See if we've already created this construction vtable group. */
9713 id = mangle_ctor_vtbl_for_type (t, binfo);
9714 if (get_global_binding (id))
9715 return;
9717 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9718 /* Build a version of VTBL (with the wrong type) for use in
9719 constructing the addresses of secondary vtables in the
9720 construction vtable group. */
9721 vtbl = build_vtable (t, id, ptr_type_node);
9723 /* Don't export construction vtables from shared libraries. Even on
9724 targets that don't support hidden visibility, this tells
9725 can_refer_decl_in_current_unit_p not to assume that it's safe to
9726 access from a different compilation unit (bz 54314). */
9727 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9728 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9730 v = NULL;
9731 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9732 binfo, vtbl, t, &v);
9734 /* Add the vtables for each of our virtual bases using the vbase in T
9735 binfo. */
9736 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9737 vbase;
9738 vbase = TREE_CHAIN (vbase))
9740 tree b;
9742 if (!BINFO_VIRTUAL_P (vbase))
9743 continue;
9744 b = copied_binfo (vbase, binfo);
9746 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9749 /* Figure out the type of the construction vtable. */
9750 type = build_array_of_n_type (vtable_entry_type, v->length ());
9751 layout_type (type);
9752 TREE_TYPE (vtbl) = type;
9753 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9754 layout_decl (vtbl, 0);
9756 /* Initialize the construction vtable. */
9757 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9758 initialize_artificial_var (vtbl, v);
9759 dump_vtable (t, binfo, vtbl);
9762 /* Add the vtbl initializers for BINFO (and its bases other than
9763 non-virtual primaries) to the list of INITS. BINFO is in the
9764 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9765 the constructor the vtbl inits should be accumulated for. (If this
9766 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9767 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9768 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9769 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9770 but are not necessarily the same in terms of layout. */
9772 static void
9773 accumulate_vtbl_inits (tree binfo,
9774 tree orig_binfo,
9775 tree rtti_binfo,
9776 tree vtbl,
9777 tree t,
9778 vec<constructor_elt, va_gc> **inits)
9780 int i;
9781 tree base_binfo;
9782 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9784 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9786 /* If it doesn't have a vptr, we don't do anything. */
9787 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9788 return;
9790 /* If we're building a construction vtable, we're not interested in
9791 subobjects that don't require construction vtables. */
9792 if (ctor_vtbl_p
9793 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9794 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9795 return;
9797 /* Build the initializers for the BINFO-in-T vtable. */
9798 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9800 /* Walk the BINFO and its bases. We walk in preorder so that as we
9801 initialize each vtable we can figure out at what offset the
9802 secondary vtable lies from the primary vtable. We can't use
9803 dfs_walk here because we need to iterate through bases of BINFO
9804 and RTTI_BINFO simultaneously. */
9805 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9807 /* Skip virtual bases. */
9808 if (BINFO_VIRTUAL_P (base_binfo))
9809 continue;
9810 accumulate_vtbl_inits (base_binfo,
9811 BINFO_BASE_BINFO (orig_binfo, i),
9812 rtti_binfo, vtbl, t,
9813 inits);
9817 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9818 BINFO vtable to L. */
9820 static void
9821 dfs_accumulate_vtbl_inits (tree binfo,
9822 tree orig_binfo,
9823 tree rtti_binfo,
9824 tree orig_vtbl,
9825 tree t,
9826 vec<constructor_elt, va_gc> **l)
9828 tree vtbl = NULL_TREE;
9829 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9830 int n_inits;
9832 if (ctor_vtbl_p
9833 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9835 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9836 primary virtual base. If it is not the same primary in
9837 the hierarchy of T, we'll need to generate a ctor vtable
9838 for it, to place at its location in T. If it is the same
9839 primary, we still need a VTT entry for the vtable, but it
9840 should point to the ctor vtable for the base it is a
9841 primary for within the sub-hierarchy of RTTI_BINFO.
9843 There are three possible cases:
9845 1) We are in the same place.
9846 2) We are a primary base within a lost primary virtual base of
9847 RTTI_BINFO.
9848 3) We are primary to something not a base of RTTI_BINFO. */
9850 tree b;
9851 tree last = NULL_TREE;
9853 /* First, look through the bases we are primary to for RTTI_BINFO
9854 or a virtual base. */
9855 b = binfo;
9856 while (BINFO_PRIMARY_P (b))
9858 b = BINFO_INHERITANCE_CHAIN (b);
9859 last = b;
9860 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9861 goto found;
9863 /* If we run out of primary links, keep looking down our
9864 inheritance chain; we might be an indirect primary. */
9865 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9866 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9867 break;
9868 found:
9870 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9871 base B and it is a base of RTTI_BINFO, this is case 2. In
9872 either case, we share our vtable with LAST, i.e. the
9873 derived-most base within B of which we are a primary. */
9874 if (b == rtti_binfo
9875 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9876 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9877 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9878 binfo_ctor_vtable after everything's been set up. */
9879 vtbl = last;
9881 /* Otherwise, this is case 3 and we get our own. */
9883 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9884 return;
9886 n_inits = vec_safe_length (*l);
9888 if (!vtbl)
9890 tree index;
9891 int non_fn_entries;
9893 /* Add the initializer for this vtable. */
9894 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9895 &non_fn_entries, l);
9897 /* Figure out the position to which the VPTR should point. */
9898 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9899 index = size_binop (MULT_EXPR,
9900 TYPE_SIZE_UNIT (vtable_entry_type),
9901 size_int (non_fn_entries + n_inits));
9902 vtbl = fold_build_pointer_plus (vtbl, index);
9905 if (ctor_vtbl_p)
9906 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9907 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9908 straighten this out. */
9909 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9910 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9911 /* Throw away any unneeded intializers. */
9912 (*l)->truncate (n_inits);
9913 else
9914 /* For an ordinary vtable, set BINFO_VTABLE. */
9915 BINFO_VTABLE (binfo) = vtbl;
9918 static GTY(()) tree abort_fndecl_addr;
9919 static GTY(()) tree dvirt_fn;
9921 /* Construct the initializer for BINFO's virtual function table. BINFO
9922 is part of the hierarchy dominated by T. If we're building a
9923 construction vtable, the ORIG_BINFO is the binfo we should use to
9924 find the actual function pointers to put in the vtable - but they
9925 can be overridden on the path to most-derived in the graph that
9926 ORIG_BINFO belongs. Otherwise,
9927 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9928 BINFO that should be indicated by the RTTI information in the
9929 vtable; it will be a base class of T, rather than T itself, if we
9930 are building a construction vtable.
9932 The value returned is a TREE_LIST suitable for wrapping in a
9933 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9934 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9935 number of non-function entries in the vtable.
9937 It might seem that this function should never be called with a
9938 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9939 base is always subsumed by a derived class vtable. However, when
9940 we are building construction vtables, we do build vtables for
9941 primary bases; we need these while the primary base is being
9942 constructed. */
9944 static void
9945 build_vtbl_initializer (tree binfo,
9946 tree orig_binfo,
9947 tree t,
9948 tree rtti_binfo,
9949 int* non_fn_entries_p,
9950 vec<constructor_elt, va_gc> **inits)
9952 tree v;
9953 vtbl_init_data vid;
9954 unsigned ix, jx;
9955 tree vbinfo;
9956 vec<tree, va_gc> *vbases;
9957 constructor_elt *e;
9959 /* Initialize VID. */
9960 memset (&vid, 0, sizeof (vid));
9961 vid.binfo = binfo;
9962 vid.derived = t;
9963 vid.rtti_binfo = rtti_binfo;
9964 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9965 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9966 vid.generate_vcall_entries = true;
9967 /* The first vbase or vcall offset is at index -3 in the vtable. */
9968 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9970 /* Add entries to the vtable for RTTI. */
9971 build_rtti_vtbl_entries (binfo, &vid);
9973 /* Create an array for keeping track of the functions we've
9974 processed. When we see multiple functions with the same
9975 signature, we share the vcall offsets. */
9976 vec_alloc (vid.fns, 32);
9977 /* Add the vcall and vbase offset entries. */
9978 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9980 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9981 build_vbase_offset_vtbl_entries. */
9982 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9983 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9984 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9986 /* If the target requires padding between data entries, add that now. */
9987 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9989 int n_entries = vec_safe_length (vid.inits);
9991 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries,
9992 true);
9994 /* Move data entries into their new positions and add padding
9995 after the new positions. Iterate backwards so we don't
9996 overwrite entries that we would need to process later. */
9997 for (ix = n_entries - 1;
9998 vid.inits->iterate (ix, &e);
9999 ix--)
10001 int j;
10002 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
10003 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
10005 (*vid.inits)[new_position] = *e;
10007 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
10009 constructor_elt *f = &(*vid.inits)[new_position - j];
10010 f->index = NULL_TREE;
10011 f->value = build1 (NOP_EXPR, vtable_entry_type,
10012 null_pointer_node);
10017 if (non_fn_entries_p)
10018 *non_fn_entries_p = vec_safe_length (vid.inits);
10020 /* The initializers for virtual functions were built up in reverse
10021 order. Straighten them out and add them to the running list in one
10022 step. */
10023 jx = vec_safe_length (*inits);
10024 vec_safe_grow (*inits, jx + vid.inits->length (), true);
10026 for (ix = vid.inits->length () - 1;
10027 vid.inits->iterate (ix, &e);
10028 ix--, jx++)
10029 (**inits)[jx] = *e;
10031 /* Go through all the ordinary virtual functions, building up
10032 initializers. */
10033 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
10035 tree delta;
10036 tree vcall_index;
10037 tree fn, fn_original;
10038 tree init = NULL_TREE;
10040 fn = BV_FN (v);
10041 fn_original = fn;
10042 if (DECL_THUNK_P (fn))
10044 if (!DECL_NAME (fn))
10045 finish_thunk (fn);
10046 if (THUNK_ALIAS (fn))
10048 fn = THUNK_ALIAS (fn);
10049 BV_FN (v) = fn;
10051 fn_original = THUNK_TARGET (fn);
10054 /* If the only definition of this function signature along our
10055 primary base chain is from a lost primary, this vtable slot will
10056 never be used, so just zero it out. This is important to avoid
10057 requiring extra thunks which cannot be generated with the function.
10059 We first check this in update_vtable_entry_for_fn, so we handle
10060 restored primary bases properly; we also need to do it here so we
10061 zero out unused slots in ctor vtables, rather than filling them
10062 with erroneous values (though harmless, apart from relocation
10063 costs). */
10064 if (BV_LOST_PRIMARY (v))
10065 init = size_zero_node;
10067 if (! init)
10069 /* Pull the offset for `this', and the function to call, out of
10070 the list. */
10071 delta = BV_DELTA (v);
10072 vcall_index = BV_VCALL_INDEX (v);
10074 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
10075 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10077 /* You can't call an abstract virtual function; it's abstract.
10078 So, we replace these functions with __pure_virtual. */
10079 if (DECL_PURE_VIRTUAL_P (fn_original))
10081 fn = abort_fndecl;
10082 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10084 if (abort_fndecl_addr == NULL)
10085 abort_fndecl_addr
10086 = fold_convert (vfunc_ptr_type_node,
10087 build_fold_addr_expr (fn));
10088 init = abort_fndecl_addr;
10091 /* Likewise for deleted virtuals. */
10092 else if (DECL_DELETED_FN (fn_original))
10094 if (!dvirt_fn)
10096 tree name = get_identifier ("__cxa_deleted_virtual");
10097 dvirt_fn = get_global_binding (name);
10098 if (!dvirt_fn)
10099 dvirt_fn = push_library_fn
10100 (name,
10101 build_function_type_list (void_type_node, NULL_TREE),
10102 NULL_TREE, ECF_NORETURN | ECF_COLD);
10104 fn = dvirt_fn;
10105 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10106 init = fold_convert (vfunc_ptr_type_node,
10107 build_fold_addr_expr (fn));
10109 else
10111 if (!integer_zerop (delta) || vcall_index)
10113 fn = make_thunk (fn, /*this_adjusting=*/1,
10114 delta, vcall_index);
10115 if (!DECL_NAME (fn))
10116 finish_thunk (fn);
10118 /* Take the address of the function, considering it to be of an
10119 appropriate generic type. */
10120 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10121 init = fold_convert (vfunc_ptr_type_node,
10122 build_fold_addr_expr (fn));
10123 /* Don't refer to a virtual destructor from a constructor
10124 vtable or a vtable for an abstract class, since destroying
10125 an object under construction is undefined behavior and we
10126 don't want it to be considered a candidate for speculative
10127 devirtualization. But do create the thunk for ABI
10128 compliance. */
10129 if (DECL_DESTRUCTOR_P (fn_original)
10130 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
10131 || orig_binfo != binfo))
10132 init = size_zero_node;
10136 /* And add it to the chain of initializers. */
10137 if (TARGET_VTABLE_USES_DESCRIPTORS)
10139 int i;
10140 if (init == size_zero_node)
10141 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10142 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10143 else
10144 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10146 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
10147 fn, build_int_cst (NULL_TREE, i));
10148 TREE_CONSTANT (fdesc) = 1;
10150 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), fdesc);
10153 else
10154 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10158 /* Adds to vid->inits the initializers for the vbase and vcall
10159 offsets in BINFO, which is in the hierarchy dominated by T. */
10161 static void
10162 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
10164 tree b;
10166 /* If this is a derived class, we must first create entries
10167 corresponding to the primary base class. */
10168 b = get_primary_binfo (binfo);
10169 if (b)
10170 build_vcall_and_vbase_vtbl_entries (b, vid);
10172 /* Add the vbase entries for this base. */
10173 build_vbase_offset_vtbl_entries (binfo, vid);
10174 /* Add the vcall entries for this base. */
10175 build_vcall_offset_vtbl_entries (binfo, vid);
10178 /* Returns the initializers for the vbase offset entries in the vtable
10179 for BINFO (which is part of the class hierarchy dominated by T), in
10180 reverse order. VBASE_OFFSET_INDEX gives the vtable index
10181 where the next vbase offset will go. */
10183 static void
10184 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10186 tree vbase;
10187 tree t;
10188 tree non_primary_binfo;
10190 /* If there are no virtual baseclasses, then there is nothing to
10191 do. */
10192 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10193 return;
10195 t = vid->derived;
10197 /* We might be a primary base class. Go up the inheritance hierarchy
10198 until we find the most derived class of which we are a primary base:
10199 it is the offset of that which we need to use. */
10200 non_primary_binfo = binfo;
10201 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
10203 tree b;
10205 /* If we have reached a virtual base, then it must be a primary
10206 base (possibly multi-level) of vid->binfo, or we wouldn't
10207 have called build_vcall_and_vbase_vtbl_entries for it. But it
10208 might be a lost primary, so just skip down to vid->binfo. */
10209 if (BINFO_VIRTUAL_P (non_primary_binfo))
10211 non_primary_binfo = vid->binfo;
10212 break;
10215 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
10216 if (get_primary_binfo (b) != non_primary_binfo)
10217 break;
10218 non_primary_binfo = b;
10221 /* Go through the virtual bases, adding the offsets. */
10222 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10223 vbase;
10224 vbase = TREE_CHAIN (vbase))
10226 tree b;
10227 tree delta;
10229 if (!BINFO_VIRTUAL_P (vbase))
10230 continue;
10232 /* Find the instance of this virtual base in the complete
10233 object. */
10234 b = copied_binfo (vbase, binfo);
10236 /* If we've already got an offset for this virtual base, we
10237 don't need another one. */
10238 if (BINFO_VTABLE_PATH_MARKED (b))
10239 continue;
10240 BINFO_VTABLE_PATH_MARKED (b) = 1;
10242 /* Figure out where we can find this vbase offset. */
10243 delta = size_binop (MULT_EXPR,
10244 vid->index,
10245 fold_convert (ssizetype,
10246 TYPE_SIZE_UNIT (vtable_entry_type)));
10247 if (vid->primary_vtbl_p)
10248 BINFO_VPTR_FIELD (b) = delta;
10250 if (binfo != TYPE_BINFO (t))
10251 /* The vbase offset had better be the same. */
10252 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
10254 /* The next vbase will come at a more negative offset. */
10255 vid->index = size_binop (MINUS_EXPR, vid->index,
10256 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10258 /* The initializer is the delta from BINFO to this virtual base.
10259 The vbase offsets go in reverse inheritance-graph order, and
10260 we are walking in inheritance graph order so these end up in
10261 the right order. */
10262 delta = size_diffop_loc (input_location,
10263 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
10265 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
10266 fold_build1_loc (input_location, NOP_EXPR,
10267 vtable_entry_type, delta));
10271 /* Adds the initializers for the vcall offset entries in the vtable
10272 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10273 to VID->INITS. */
10275 static void
10276 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10278 /* We only need these entries if this base is a virtual base. We
10279 compute the indices -- but do not add to the vtable -- when
10280 building the main vtable for a class. */
10281 if (binfo == TYPE_BINFO (vid->derived)
10282 || (BINFO_VIRTUAL_P (binfo)
10283 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10284 correspond to VID->DERIVED), we are building a primary
10285 construction virtual table. Since this is a primary
10286 virtual table, we do not need the vcall offsets for
10287 BINFO. */
10288 && binfo != vid->rtti_binfo))
10290 /* We need a vcall offset for each of the virtual functions in this
10291 vtable. For example:
10293 class A { virtual void f (); };
10294 class B1 : virtual public A { virtual void f (); };
10295 class B2 : virtual public A { virtual void f (); };
10296 class C: public B1, public B2 { virtual void f (); };
10298 A C object has a primary base of B1, which has a primary base of A. A
10299 C also has a secondary base of B2, which no longer has a primary base
10300 of A. So the B2-in-C construction vtable needs a secondary vtable for
10301 A, which will adjust the A* to a B2* to call f. We have no way of
10302 knowing what (or even whether) this offset will be when we define B2,
10303 so we store this "vcall offset" in the A sub-vtable and look it up in
10304 a "virtual thunk" for B2::f.
10306 We need entries for all the functions in our primary vtable and
10307 in our non-virtual bases' secondary vtables. */
10308 vid->vbase = binfo;
10309 /* If we are just computing the vcall indices -- but do not need
10310 the actual entries -- not that. */
10311 if (!BINFO_VIRTUAL_P (binfo))
10312 vid->generate_vcall_entries = false;
10313 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10314 add_vcall_offset_vtbl_entries_r (binfo, vid);
10318 /* Build vcall offsets, starting with those for BINFO. */
10320 static void
10321 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
10323 int i;
10324 tree primary_binfo;
10325 tree base_binfo;
10327 /* Don't walk into virtual bases -- except, of course, for the
10328 virtual base for which we are building vcall offsets. Any
10329 primary virtual base will have already had its offsets generated
10330 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10331 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
10332 return;
10334 /* If BINFO has a primary base, process it first. */
10335 primary_binfo = get_primary_binfo (binfo);
10336 if (primary_binfo)
10337 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
10339 /* Add BINFO itself to the list. */
10340 add_vcall_offset_vtbl_entries_1 (binfo, vid);
10342 /* Scan the non-primary bases of BINFO. */
10343 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10344 if (base_binfo != primary_binfo)
10345 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
10348 /* Called from build_vcall_offset_vtbl_entries_r. */
10350 static void
10351 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10353 /* Make entries for the rest of the virtuals. */
10354 tree orig_fn;
10356 /* The ABI requires that the methods be processed in declaration
10357 order. */
10358 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
10359 orig_fn;
10360 orig_fn = DECL_CHAIN (orig_fn))
10361 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10362 add_vcall_offset (orig_fn, binfo, vid);
10365 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10367 static void
10368 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10370 size_t i;
10371 tree vcall_offset;
10372 tree derived_entry;
10374 /* If there is already an entry for a function with the same
10375 signature as FN, then we do not need a second vcall offset.
10376 Check the list of functions already present in the derived
10377 class vtable. */
10378 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10380 if (same_signature_p (derived_entry, orig_fn)
10381 /* We only use one vcall offset for virtual destructors,
10382 even though there are two virtual table entries. */
10383 || (DECL_DESTRUCTOR_P (derived_entry)
10384 && DECL_DESTRUCTOR_P (orig_fn)))
10385 return;
10388 /* If we are building these vcall offsets as part of building
10389 the vtable for the most derived class, remember the vcall
10390 offset. */
10391 if (vid->binfo == TYPE_BINFO (vid->derived))
10393 tree_pair_s elt = {orig_fn, vid->index};
10394 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10397 /* The next vcall offset will be found at a more negative
10398 offset. */
10399 vid->index = size_binop (MINUS_EXPR, vid->index,
10400 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10402 /* Keep track of this function. */
10403 vec_safe_push (vid->fns, orig_fn);
10405 if (vid->generate_vcall_entries)
10407 tree base;
10408 tree fn;
10410 /* Find the overriding function. */
10411 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10412 if (fn == error_mark_node)
10413 vcall_offset = build_zero_cst (vtable_entry_type);
10414 else
10416 base = TREE_VALUE (fn);
10418 /* The vbase we're working on is a primary base of
10419 vid->binfo. But it might be a lost primary, so its
10420 BINFO_OFFSET might be wrong, so we just use the
10421 BINFO_OFFSET from vid->binfo. */
10422 vcall_offset = size_diffop_loc (input_location,
10423 BINFO_OFFSET (base),
10424 BINFO_OFFSET (vid->binfo));
10425 vcall_offset = fold_build1_loc (input_location,
10426 NOP_EXPR, vtable_entry_type,
10427 vcall_offset);
10429 /* Add the initializer to the vtable. */
10430 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10434 /* Return vtbl initializers for the RTTI entries corresponding to the
10435 BINFO's vtable. The RTTI entries should indicate the object given
10436 by VID->rtti_binfo. */
10438 static void
10439 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10441 tree b;
10442 tree t;
10443 tree offset;
10444 tree decl;
10445 tree init;
10447 t = BINFO_TYPE (vid->rtti_binfo);
10449 /* To find the complete object, we will first convert to our most
10450 primary base, and then add the offset in the vtbl to that value. */
10451 b = most_primary_binfo (binfo);
10452 offset = size_diffop_loc (input_location,
10453 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10455 /* The second entry is the address of the typeinfo object. */
10456 if (flag_rtti)
10457 decl = build_address (get_tinfo_decl (t));
10458 else
10459 decl = integer_zero_node;
10461 /* Convert the declaration to a type that can be stored in the
10462 vtable. */
10463 init = build_nop (vfunc_ptr_type_node, decl);
10464 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10466 /* Add the offset-to-top entry. It comes earlier in the vtable than
10467 the typeinfo entry. Convert the offset to look like a
10468 function pointer, so that we can put it in the vtable. */
10469 init = build_nop (vfunc_ptr_type_node, offset);
10470 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10473 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10474 accessibility. */
10476 bool
10477 uniquely_derived_from_p (tree parent, tree type)
10479 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10480 return base && base != error_mark_node;
10483 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10485 bool
10486 publicly_uniquely_derived_p (tree parent, tree type)
10488 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10489 NULL, tf_none);
10490 return base && base != error_mark_node;
10493 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10494 class between them, if any. */
10496 tree
10497 common_enclosing_class (tree ctx1, tree ctx2)
10499 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10500 return NULL_TREE;
10501 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10502 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10503 if (ctx1 == ctx2)
10504 return ctx1;
10505 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10506 TYPE_MARKED_P (t) = true;
10507 tree found = NULL_TREE;
10508 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10509 if (TYPE_MARKED_P (t))
10511 found = t;
10512 break;
10514 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10515 TYPE_MARKED_P (t) = false;
10516 return found;
10519 #include "gt-cp-class.h"