Debian Codegraph @

gcc/cp/class.cc

raw
1 /* Functions related to building -*- C++ -*- classes and their related objects.
2 Copyright (C) 1987-2025 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann ([email protected])
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21
22 /* High-level class interface. */
23
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"
40
41 /* Id for dumping the class hierarchy. */
42 int class_dump_id;
43
44 /* The number of nested classes being processed. If we are not in the
45 scope of any class, this is zero. */
46
47 int current_class_depth;
48
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 */
52
53 typedef struct class_stack_node {
54 /* The name of the class. */
55 tree name;
56
57 /* The _TYPE node for the class. */
58 tree type;
59
60 /* The access specifier pending for new declarations in the scope of
61 this class. */
62 tree access;
63
64 /* If were defining TYPE, the names used in this class. */
65 splay_tree names_used;
66
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;
71
72 struct vtbl_init_data
73 {
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;
101 };
102
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
105
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;
110
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
113
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);
140
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, bool = false,
145 bool = false, 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);
208 static void propagate_class_warmth_attribute (tree);
209
210 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
211
212 void
213 set_current_access_from_decl (tree decl)
214 {
215 if (TREE_PRIVATE (decl))
216 current_access_specifier = access_private_node;
217 else if (TREE_PROTECTED (decl))
218 current_access_specifier = access_protected_node;
219 else
220 current_access_specifier = access_public_node;
221 }
222
223 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
224 'structor is in charge of 'structing virtual bases, or FALSE_STMT
225 otherwise. */
226
227 tree
228 build_if_in_charge (tree true_stmt, tree false_stmt)
229 {
230 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
231 tree cmp = build2 (NE_EXPR, boolean_type_node,
232 current_in_charge_parm, integer_zero_node);
233 tree type = unlowered_expr_type (true_stmt);
234 if (VOID_TYPE_P (type))
235 type = unlowered_expr_type (false_stmt);
236 tree cond = build3 (COND_EXPR, type,
237 cmp, true_stmt, false_stmt);
238 return cond;
239 }
240
241 /* Convert to or from a base subobject. EXPR is an expression of type
242 `A' or `A*', an expression of type `B' or `B*' is returned. To
243 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
244 the B base instance within A. To convert base A to derived B, CODE
245 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
246 In this latter case, A must not be a morally virtual base of B.
247 NONNULL is true if EXPR is known to be non-NULL (this is only
248 needed when EXPR is of pointer type). CV qualifiers are preserved
249 from EXPR. */
250
251 tree
252 build_base_path (enum tree_code code,
253 tree expr,
254 tree binfo,
255 int nonnull,
256 tsubst_flags_t complain)
257 {
258 tree v_binfo = NULL_TREE;
259 tree d_binfo = NULL_TREE;
260 tree probe;
261 tree offset;
262 tree target_type;
263 tree null_test = NULL;
264 tree ptr_target_type;
265 int fixed_type_p;
266 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
267 bool has_empty = false;
268 bool virtual_access;
269 bool rvalue = false;
270
271 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
272 return error_mark_node;
273
274 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
275 {
276 d_binfo = probe;
277 if (is_empty_class (BINFO_TYPE (probe)))
278 has_empty = true;
279 if (!v_binfo && BINFO_VIRTUAL_P (probe))
280 v_binfo = probe;
281 }
282
283 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
284 if (want_pointer)
285 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
286 if (dependent_type_p (probe))
287 if (tree open = currently_open_class (probe))
288 probe = open;
289
290 if (code == PLUS_EXPR
291 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
292 {
293 /* This can happen when adjust_result_of_qualified_name_lookup can't
294 find a unique base binfo in a call to a member function. We
295 couldn't give the diagnostic then since we might have been calling
296 a static member function, so we do it now. In other cases, eg.
297 during error recovery (c++/71979), we may not have a base at all. */
298 if (complain & tf_error)
299 {
300 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
301 ba_unique, NULL, complain);
302 gcc_assert (base == error_mark_node || !base);
303 }
304 return error_mark_node;
305 }
306
307 gcc_assert ((code == MINUS_EXPR
308 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
309 || code == PLUS_EXPR);
310
311 if (binfo == d_binfo)
312 /* Nothing to do. */
313 return expr;
314
315 if (code == MINUS_EXPR && v_binfo)
316 {
317 if (complain & tf_error)
318 {
319 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
320 {
321 if (want_pointer)
322 error ("cannot convert from pointer to base class %qT to "
323 "pointer to derived class %qT because the base is "
324 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
325 else
326 error ("cannot convert from base class %qT to derived "
327 "class %qT because the base is virtual",
328 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
329 }
330 else
331 {
332 if (want_pointer)
333 error ("cannot convert from pointer to base class %qT to "
334 "pointer to derived class %qT via virtual base %qT",
335 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
336 BINFO_TYPE (v_binfo));
337 else
338 error ("cannot convert from base class %qT to derived "
339 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
340 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
341 }
342 }
343 return error_mark_node;
344 }
345
346 bool uneval = (cp_unevaluated_operand != 0
347 || processing_template_decl
348 || in_template_context);
349
350 /* For a non-pointer simple base reference, express it as a COMPONENT_REF
351 without taking its address (and so causing lambda capture, 91933). */
352 if (code == PLUS_EXPR && !v_binfo && !want_pointer && !has_empty && !uneval)
353 return build_simple_base_path (expr, binfo);
354
355 if (!want_pointer)
356 {
357 rvalue = !lvalue_p (expr);
358 /* This must happen before the call to save_expr. */
359 expr = cp_build_addr_expr (expr, complain);
360 }
361 else
362 expr = mark_rvalue_use (expr);
363
364 offset = BINFO_OFFSET (binfo);
365 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
366 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
367 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
368 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
369 expression returned matches the input. */
370 target_type = cp_build_qualified_type
371 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
372 ptr_target_type = build_pointer_type (target_type);
373
374 /* Do we need to look in the vtable for the real offset? */
375 virtual_access = (v_binfo && fixed_type_p <= 0);
376
377 /* Don't bother with the calculations inside sizeof; they'll ICE if the
378 source type is incomplete and the pointer value doesn't matter. In a
379 template (even in instantiate_non_dependent_expr), we don't have vtables
380 set up properly yet, and the value doesn't matter there either; we're
381 just interested in the result of overload resolution. */
382 if (uneval)
383 {
384 expr = build_nop (ptr_target_type, expr);
385 goto indout;
386 }
387
388 if (!COMPLETE_TYPE_P (probe))
389 {
390 if (complain & tf_error)
391 error ("cannot convert from %qT to base class %qT because %qT is "
392 "incomplete", BINFO_TYPE (d_binfo), BINFO_TYPE (binfo),
393 BINFO_TYPE (d_binfo));
394 return error_mark_node;
395 }
396
397 /* If we're in an NSDMI, we don't have the full constructor context yet
398 that we need for converting to a virtual base, so just build a stub
399 CONVERT_EXPR and expand it later in bot_replace. */
400 if (virtual_access && fixed_type_p < 0
401 && current_scope () != current_function_decl)
402 {
403 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
404 CONVERT_EXPR_VBASE_PATH (expr) = true;
405 goto indout;
406 }
407
408 /* Do we need to check for a null pointer? */
409 if (want_pointer && !nonnull)
410 {
411 /* If we know the conversion will not actually change the value
412 of EXPR, then we can avoid testing the expression for NULL.
413 We have to avoid generating a COMPONENT_REF for a base class
414 field, because other parts of the compiler know that such
415 expressions are always non-NULL. */
416 if (!virtual_access && integer_zerop (offset))
417 return build_nop (ptr_target_type, expr);
418 null_test = error_mark_node;
419 }
420
421 /* Protect against multiple evaluation if necessary. */
422 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
423 expr = save_expr (expr);
424
425 /* Store EXPR and build the real null test just before returning. */
426 if (null_test)
427 null_test = expr;
428
429 /* If this is a simple base reference, express it as a COMPONENT_REF. */
430 if (code == PLUS_EXPR && !virtual_access
431 /* We don't build base fields for empty bases, and they aren't very
432 interesting to the optimizers anyway. */
433 && !has_empty)
434 {
435 expr = cp_build_fold_indirect_ref (expr);
436 expr = build_simple_base_path (expr, binfo);
437 if (rvalue && lvalue_p (expr))
438 expr = move (expr);
439 if (want_pointer)
440 expr = build_address (expr);
441 target_type = TREE_TYPE (expr);
442 goto out;
443 }
444
445 if (virtual_access)
446 {
447 /* Going via virtual base V_BINFO. We need the static offset
448 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
449 V_BINFO. That offset is an entry in D_BINFO's vtable. */
450 tree v_offset;
451
452 if (fixed_type_p < 0 && in_base_initializer)
453 {
454 /* In a base member initializer, we cannot rely on the
455 vtable being set up. We have to indirect via the
456 vtt_parm. */
457 tree t;
458
459 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
460 t = build_pointer_type (t);
461 v_offset = fold_convert (t, current_vtt_parm);
462 v_offset = cp_build_fold_indirect_ref (v_offset);
463 }
464 else
465 {
466 tree t = expr;
467 if (sanitize_flags_p (SANITIZE_VPTR)
468 && fixed_type_p == 0)
469 {
470 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
471 probe, expr);
472 if (t == NULL_TREE)
473 t = expr;
474 }
475 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
476 TREE_TYPE (TREE_TYPE (expr)));
477 }
478
479 if (v_offset == error_mark_node)
480 return error_mark_node;
481
482 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
483 v_offset = build1 (NOP_EXPR,
484 build_pointer_type (ptrdiff_type_node),
485 v_offset);
486 v_offset = cp_build_fold_indirect_ref (v_offset);
487 TREE_CONSTANT (v_offset) = 1;
488
489 offset = convert_to_integer (ptrdiff_type_node,
490 size_diffop_loc (input_location, offset,
491 BINFO_OFFSET (v_binfo)));
492
493 if (!integer_zerop (offset))
494 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
495
496 if (fixed_type_p < 0)
497 /* Negative fixed_type_p means this is a constructor or destructor;
498 virtual base layout is fixed in in-charge [cd]tors, but not in
499 base [cd]tors. */
500 offset = build_if_in_charge
501 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
502 v_offset);
503 else
504 offset = v_offset;
505 }
506
507 if (want_pointer)
508 target_type = ptr_target_type;
509
510 if (!integer_zerop (offset))
511 {
512 offset = fold_convert (sizetype, offset);
513 if (code == MINUS_EXPR)
514 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
515 expr = fold_build_pointer_plus (expr, offset);
516 }
517 else
518 null_test = NULL;
519
520 expr = build1 (NOP_EXPR, ptr_target_type, expr);
521
522 indout:
523 if (!want_pointer)
524 {
525 expr = cp_build_fold_indirect_ref (expr);
526 if (rvalue)
527 expr = move (expr);
528 }
529
530 out:
531 if (null_test)
532 /* Wrap EXPR in a null test. */
533 expr = build_if_nonnull (null_test, expr, complain);
534
535 return expr;
536 }
537
538 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
539 Perform a derived-to-base conversion by recursively building up a
540 sequence of COMPONENT_REFs to the appropriate base fields. */
541
542 static tree
543 build_simple_base_path (tree expr, tree binfo)
544 {
545 tree type = BINFO_TYPE (binfo);
546 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
547 tree field;
548
549 if (d_binfo == NULL_TREE)
550 {
551 tree temp;
552
553 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
554
555 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
556 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
557 an lvalue in the front end; only _DECLs and _REFs are lvalues
558 in the back end. */
559 temp = unary_complex_lvalue (ADDR_EXPR, expr);
560 if (temp)
561 expr = cp_build_fold_indirect_ref (temp);
562
563 return expr;
564 }
565
566 /* Recurse. */
567 expr = build_simple_base_path (expr, d_binfo);
568
569 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
570 field; field = DECL_CHAIN (field))
571 /* Is this the base field created by build_base_field? */
572 if (TREE_CODE (field) == FIELD_DECL
573 && DECL_FIELD_IS_BASE (field)
574 && TREE_TYPE (field) == type
575 /* If we're looking for a field in the most-derived class,
576 also check the field offset; we can have two base fields
577 of the same type if one is an indirect virtual base and one
578 is a direct non-virtual base. */
579 && (BINFO_INHERITANCE_CHAIN (d_binfo)
580 || tree_int_cst_equal (byte_position (field),
581 BINFO_OFFSET (binfo))))
582 {
583 /* We don't use build_class_member_access_expr here, as that
584 has unnecessary checks, and more importantly results in
585 recursive calls to dfs_walk_once. */
586 int type_quals = cp_type_quals (TREE_TYPE (expr));
587
588 expr = build3 (COMPONENT_REF,
589 cp_build_qualified_type (type, type_quals),
590 expr, field, NULL_TREE);
591 /* Mark the expression const or volatile, as appropriate.
592 Even though we've dealt with the type above, we still have
593 to mark the expression itself. */
594 if (type_quals & TYPE_QUAL_CONST)
595 TREE_READONLY (expr) = 1;
596 if (type_quals & TYPE_QUAL_VOLATILE)
597 TREE_THIS_VOLATILE (expr) = 1;
598
599 return expr;
600 }
601
602 /* Didn't find the base field?!? */
603 gcc_unreachable ();
604 }
605
606 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
607 type is a class type or a pointer to a class type. In the former
608 case, TYPE is also a class type; in the latter it is another
609 pointer type. If CHECK_ACCESS is true, an error message is emitted
610 if TYPE is inaccessible. If OBJECT has pointer type, the value is
611 assumed to be non-NULL. */
612
613 tree
614 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
615 tsubst_flags_t complain)
616 {
617 tree binfo;
618 tree object_type;
619
620 if (TYPE_PTR_P (TREE_TYPE (object)))
621 {
622 object_type = TREE_TYPE (TREE_TYPE (object));
623 type = TREE_TYPE (type);
624 }
625 else
626 object_type = TREE_TYPE (object);
627
628 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
629 NULL, complain);
630 if (!binfo || binfo == error_mark_node)
631 return error_mark_node;
632
633 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
634 }
635
636 /* EXPR is an expression with unqualified class type. BASE is a base
637 binfo of that class type. Returns EXPR, converted to the BASE
638 type. This function assumes that EXPR is the most derived class;
639 therefore virtual bases can be found at their static offsets. */
640
641 tree
642 convert_to_base_statically (tree expr, tree base)
643 {
644 tree expr_type;
645
646 expr_type = TREE_TYPE (expr);
647 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
648 {
649 /* If this is a non-empty base, use a COMPONENT_REF. */
650 if (!is_empty_class (BINFO_TYPE (base)))
651 return build_simple_base_path (expr, base);
652
653 /* We use fold_build2 and fold_convert below to simplify the trees
654 provided to the optimizers. It is not safe to call these functions
655 when processing a template because they do not handle C++-specific
656 trees. */
657 gcc_assert (!processing_template_decl);
658 expr = cp_build_addr_expr (expr, tf_warning_or_error);
659 if (!integer_zerop (BINFO_OFFSET (base)))
660 expr = fold_build_pointer_plus_loc (input_location,
661 expr, BINFO_OFFSET (base));
662 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
663 expr = build_fold_indirect_ref_loc (input_location, expr);
664 }
665
666 return expr;
667 }
668
669 /* True IFF EXPR is a reference to an empty base class "subobject", as built in
670 convert_to_base_statically. We look for the result of the fold_convert
671 call, a NOP_EXPR from one pointer type to another, where the target is an
672 empty base of the original type. */
673
674 bool
675 is_empty_base_ref (tree expr)
676 {
677 if (INDIRECT_REF_P (expr))
678 expr = TREE_OPERAND (expr, 0);
679 if (TREE_CODE (expr) != NOP_EXPR)
680 return false;
681 tree type = TREE_TYPE (expr);
682 if (!POINTER_TYPE_P (type))
683 return false;
684 type = TREE_TYPE (type);
685 if (!is_empty_class (type))
686 return false;
687 STRIP_NOPS (expr);
688 tree fromtype = TREE_TYPE (expr);
689 if (!POINTER_TYPE_P (fromtype))
690 return false;
691 fromtype = TREE_TYPE (fromtype);
692 return (CLASS_TYPE_P (fromtype)
693 && !same_type_ignoring_top_level_qualifiers_p (fromtype, type)
694 && DERIVED_FROM_P (type, fromtype));
695 }
696
697 tree
698 build_vfield_ref (tree datum, tree type)
699 {
700 tree vfield, vcontext;
701
702 if (datum == error_mark_node
703 /* Can happen in case of duplicate base types (c++/59082). */
704 || !TYPE_VFIELD (type))
705 return error_mark_node;
706
707 /* First, convert to the requested type. */
708 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
709 datum = convert_to_base (datum, type, /*check_access=*/false,
710 /*nonnull=*/true, tf_warning_or_error);
711
712 /* Second, the requested type may not be the owner of its own vptr.
713 If not, convert to the base class that owns it. We cannot use
714 convert_to_base here, because VCONTEXT may appear more than once
715 in the inheritance hierarchy of TYPE, and thus direct conversion
716 between the types may be ambiguous. Following the path back up
717 one step at a time via primary bases avoids the problem. */
718 vfield = TYPE_VFIELD (type);
719 vcontext = DECL_CONTEXT (vfield);
720 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
721 {
722 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
723 type = TREE_TYPE (datum);
724 }
725
726 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
727 }
728
729 /* Given an object INSTANCE, return an expression which yields the
730 vtable element corresponding to INDEX. There are many special
731 cases for INSTANCE which we take care of here, mainly to avoid
732 creating extra tree nodes when we don't have to. */
733
734 tree
735 build_vtbl_ref (tree instance, tree idx)
736 {
737 tree aref;
738 tree vtbl = NULL_TREE;
739
740 /* Try to figure out what a reference refers to, and
741 access its virtual function table directly. */
742
743 int cdtorp = 0;
744 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
745
746 tree basetype = non_reference (TREE_TYPE (instance));
747
748 if (fixed_type && !cdtorp)
749 {
750 tree binfo = lookup_base (fixed_type, basetype,
751 ba_unique, NULL, tf_none);
752 if (binfo && binfo != error_mark_node)
753 vtbl = unshare_expr (BINFO_VTABLE (binfo));
754 }
755
756 if (!vtbl)
757 vtbl = build_vfield_ref (instance, basetype);
758
759 aref = build_array_ref (input_location, vtbl, idx);
760 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
761
762 return aref;
763 }
764
765 /* Given a stable object pointer INSTANCE_PTR, return an expression which
766 yields a function pointer corresponding to vtable element INDEX. */
767
768 tree
769 build_vfn_ref (tree instance_ptr, tree idx)
770 {
771 tree aref;
772
773 aref = build_vtbl_ref (cp_build_fold_indirect_ref (instance_ptr), idx);
774
775 /* When using function descriptors, the address of the
776 vtable entry is treated as a function pointer. */
777 if (TARGET_VTABLE_USES_DESCRIPTORS)
778 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
779 cp_build_addr_expr (aref, tf_warning_or_error));
780
781 /* Remember this as a method reference, for later devirtualization. */
782 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr,
783 fold_convert (TREE_TYPE (instance_ptr), idx));
784
785 return aref;
786 }
787
788 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
789 for the given TYPE. */
790
791 static tree
792 get_vtable_name (tree type)
793 {
794 return mangle_vtbl_for_type (type);
795 }
796
797 /* DECL is an entity associated with TYPE, like a virtual table or an
798 implicitly generated constructor. Determine whether or not DECL
799 should have external or internal linkage at the object file
800 level. This routine does not deal with COMDAT linkage and other
801 similar complexities; it simply sets TREE_PUBLIC if it possible for
802 entities in other translation units to contain copies of DECL, in
803 the abstract. */
804
805 void
806 set_linkage_according_to_type (tree /*type*/, tree decl)
807 {
808 TREE_PUBLIC (decl) = 1;
809 determine_visibility (decl);
810 }
811
812 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
813 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
814 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
815
816 static tree
817 build_vtable (tree class_type, tree name, tree vtable_type)
818 {
819 tree decl;
820
821 decl = build_lang_decl (VAR_DECL, name, vtable_type);
822 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
823 now to avoid confusion in mangle_decl. */
824 SET_DECL_ASSEMBLER_NAME (decl, name);
825 DECL_CONTEXT (decl) = class_type;
826 DECL_ARTIFICIAL (decl) = 1;
827 TREE_STATIC (decl) = 1;
828 TREE_READONLY (decl) = 1;
829 DECL_VIRTUAL_P (decl) = 1;
830 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
831 DECL_USER_ALIGN (decl) = true;
832 DECL_VTABLE_OR_VTT_P (decl) = 1;
833 set_linkage_according_to_type (class_type, decl);
834 /* The vtable has not been defined -- yet. */
835 DECL_EXTERNAL (decl) = 1;
836 DECL_NOT_REALLY_EXTERN (decl) = 1;
837
838 /* Mark the VAR_DECL node representing the vtable itself as a
839 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
840 is rather important that such things be ignored because any
841 effort to actually generate DWARF for them will run into
842 trouble when/if we encounter code like:
843
844 #pragma interface
845 struct S { virtual void member (); };
846
847 because the artificial declaration of the vtable itself (as
848 manufactured by the g++ front end) will say that the vtable is
849 a static member of `S' but only *after* the debug output for
850 the definition of `S' has already been output. This causes
851 grief because the DWARF entry for the definition of the vtable
852 will try to refer back to an earlier *declaration* of the
853 vtable as a static member of `S' and there won't be one. We
854 might be able to arrange to have the "vtable static member"
855 attached to the member list for `S' before the debug info for
856 `S' get written (which would solve the problem) but that would
857 require more intrusive changes to the g++ front end. */
858 DECL_IGNORED_P (decl) = 1;
859
860 return decl;
861 }
862
863 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
864 or even complete. If this does not exist, create it. If COMPLETE is
865 nonzero, then complete the definition of it -- that will render it
866 impossible to actually build the vtable, but is useful to get at those
867 which are known to exist in the runtime. */
868
869 tree
870 get_vtable_decl (tree type, int complete)
871 {
872 tree decl;
873
874 if (CLASSTYPE_VTABLES (type))
875 return CLASSTYPE_VTABLES (type);
876
877 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
878 CLASSTYPE_VTABLES (type) = decl;
879
880 if (complete)
881 {
882 DECL_EXTERNAL (decl) = 1;
883 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
884 }
885
886 return decl;
887 }
888
889 /* Build the primary virtual function table for TYPE. If BINFO is
890 non-NULL, build the vtable starting with the initial approximation
891 that it is the same as the one which is the head of the association
892 list. Returns a nonzero value if a new vtable is actually
893 created. */
894
895 static int
896 build_primary_vtable (tree binfo, tree type)
897 {
898 tree decl;
899 tree virtuals;
900
901 decl = get_vtable_decl (type, /*complete=*/0);
902
903 if (binfo)
904 {
905 if (BINFO_NEW_VTABLE_MARKED (binfo))
906 /* We have already created a vtable for this base, so there's
907 no need to do it again. */
908 return 0;
909
910 virtuals = copy_list (BINFO_VIRTUALS (binfo));
911 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
912 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
913 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
914 }
915 else
916 {
917 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
918 virtuals = NULL_TREE;
919 }
920
921 /* Initialize the association list for this type, based
922 on our first approximation. */
923 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
924 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
925 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
926 return 1;
927 }
928
929 /* Give BINFO a new virtual function table which is initialized
930 with a skeleton-copy of its original initialization. The only
931 entry that changes is the `delta' entry, so we can really
932 share a lot of structure.
933
934 FOR_TYPE is the most derived type which caused this table to
935 be needed.
936
937 Returns nonzero if we haven't met BINFO before.
938
939 The order in which vtables are built (by calling this function) for
940 an object must remain the same, otherwise a binary incompatibility
941 can result. */
942
943 static int
944 build_secondary_vtable (tree binfo)
945 {
946 if (BINFO_NEW_VTABLE_MARKED (binfo))
947 /* We already created a vtable for this base. There's no need to
948 do it again. */
949 return 0;
950
951 /* Remember that we've created a vtable for this BINFO, so that we
952 don't try to do so again. */
953 SET_BINFO_NEW_VTABLE_MARKED (binfo);
954
955 /* Make fresh virtual list, so we can smash it later. */
956 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
957
958 /* Secondary vtables are laid out as part of the same structure as
959 the primary vtable. */
960 BINFO_VTABLE (binfo) = NULL_TREE;
961 return 1;
962 }
963
964 /* Create a new vtable for BINFO which is the hierarchy dominated by
965 T. Return nonzero if we actually created a new vtable. */
966
967 static int
968 make_new_vtable (tree t, tree binfo)
969 {
970 if (binfo == TYPE_BINFO (t))
971 /* In this case, it is *type*'s vtable we are modifying. We start
972 with the approximation that its vtable is that of the
973 immediate base class. */
974 return build_primary_vtable (binfo, t);
975 else
976 /* This is our very own copy of `basetype' to play with. Later,
977 we will fill in all the virtual functions that override the
978 virtual functions in these base classes which are not defined
979 by the current type. */
980 return build_secondary_vtable (binfo);
981 }
982
983 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
984 (which is in the hierarchy dominated by T) list FNDECL as its
985 BV_FN. DELTA is the required constant adjustment from the `this'
986 pointer where the vtable entry appears to the `this' required when
987 the function is actually called. */
988
989 static void
990 modify_vtable_entry (tree t,
991 tree binfo,
992 tree fndecl,
993 tree delta,
994 tree *virtuals)
995 {
996 tree v;
997
998 v = *virtuals;
999
1000 if (fndecl != BV_FN (v)
1001 || !tree_int_cst_equal (delta, BV_DELTA (v)))
1002 {
1003 /* We need a new vtable for BINFO. */
1004 if (make_new_vtable (t, binfo))
1005 {
1006 /* If we really did make a new vtable, we also made a copy
1007 of the BINFO_VIRTUALS list. Now, we have to find the
1008 corresponding entry in that list. */
1009 *virtuals = BINFO_VIRTUALS (binfo);
1010 while (BV_FN (*virtuals) != BV_FN (v))
1011 *virtuals = TREE_CHAIN (*virtuals);
1012 v = *virtuals;
1013 }
1014
1015 BV_DELTA (v) = delta;
1016 BV_VCALL_INDEX (v) = NULL_TREE;
1017 BV_FN (v) = fndecl;
1018 }
1019 }
1020
1021
1022 /* Check if the object parameter of an iobj member function corresponds to
1023 another parameter type. CONTEXT is the class that the implicit object
1024 parameter is considered to refer to. */
1025
1026 bool
1027 iobj_parm_corresponds_to (tree iobj_fn, tree xobj_param, tree context)
1028 {
1029 tree iobj_fn_type = TREE_TYPE (iobj_fn);
1030
1031 /* If the iobj member function was introduced with a using declaration, the
1032 type of its object parameter is considered to be that of the class it was
1033 introduced into.
1034
1035 [over.match.funcs.general.4]
1036 For non-conversion functions that are implicit object member
1037 functions nominated by a using-declaration in a derived class, the
1038 function is considered to be a member of the derived class for the purpose
1039 of defining the type of the implicit object parameter.
1040
1041 Unfortunately, because of this rule, we can't just compare the xobj member
1042 function's DECL_CONTEXT to its object parameter.
1043
1044 struct S;
1045
1046 struct B {
1047 int f(this S&) { return 5; }
1048 };
1049
1050 struct S : B {
1051 using B::f;
1052 int f() { return 10; }
1053 };
1054
1055 The using declaration does not change the object parameter of B::f as it
1056 is an xobj member function. However, its object parameter still
1057 corresponds to S::f as it was declared with an object parameter of type
1058 S const&. The DECL_CONTEXT of B::f is B, so if we compare the type of the
1059 object parameter to that, it will not match. If we naively assume a
1060 different type from the DECL_CONTEXT for an xobj parameter means that the
1061 object parameters do not correspond, then the object parameters in the
1062 above example will be considered non-corresponding.
1063
1064 As a result of this, B::f would incorrectly not be discarded, causing an
1065 ambiguity when f is called on an object of type S.
1066
1067 This also impacts member functions with constraints as in the following
1068 example.
1069
1070 template<typename = void>
1071 struct S;
1072
1073 template<typename = void>
1074 struct B {
1075 int f(this S<>&) requires true { return 5; }
1076 };
1077
1078 template<typename>
1079 struct S : B<> {
1080 using B<>::f;
1081 int f() { return 10; }
1082 };
1083
1084 Once again, if we compare the DECL_CONTEXT of B<>::f to it's xobj
1085 parameter, it would not match. If the object parameters do not
1086 correspond, constraints are not taken into account, so in this example we
1087 would (probably) get an ambiguous lookup instead of correctly picking
1088 B<>::f.
1089
1090 Because of this caveat, we must actually compare the type of the iobj
1091 parameter to the type of the xobj parameter, shortcuts will have these
1092 edge cases.
1093
1094 Aside from the more complex reasons above, this logic also implicitly
1095 handles xobj parameters of pointer type, we don't have to explicitly
1096 check for that case. */
1097
1098 if (!same_type_ignoring_top_level_qualifiers_p
1099 (context, non_reference (xobj_param)))
1100 return false;
1101
1102 /* We don't get to bail yet even if we have a by-value xobj parameter,
1103 a by-value xobj parameter can correspond to an iobj parameter provided the
1104 iobj member function is not declared with a reference qualifier.
1105
1106 From this point on, we know we are dealing with an xobj parameter that has
1107 an object parameter of the same type as the class it was declared in.
1108 We still don't know if we have a reference or by-value parameter yet
1109 though. */
1110
1111 cp_ref_qualifier const iobj_ref_qual = type_memfn_rqual (iobj_fn_type);
1112 /* We only care about cv qualifiers when determining correspondence. */
1113 static constexpr cp_cv_quals cv_bits = TYPE_QUAL_VOLATILE
1114 | TYPE_QUAL_CONST;
1115 cp_cv_quals const iobj_cv_quals = type_memfn_quals (iobj_fn_type) & cv_bits;
1116 /* We need to ignore the ref qualifier of the xobj parameter if the iobj
1117 member function lacks a ref qualifier.
1118
1119 [basic.scope.scope.3]
1120 Two non-static member functions have corresponding object parameters if:
1121 -- exactly one is an implicit object member function with no ref-qualifier
1122 and the types of their object parameters ([dcl.fct]), after removing
1123 top-level references, are the same, or
1124 -- their object parameters have the same type.
1125
1126 The cv qualifiers of a by-value parameter are supposed to be discarded, so
1127 we ignore them.
1128
1129 [dcl.fct.5]
1130 After producing the list of parameter types, any top-level cv-qualifiers
1131 modifying a parameter type are deleted when forming the function type.
1132
1133 However, they still need to be taken into account when our xobj parameter
1134 is a reference that is being ignored (according to [basic.scope.scope.3]
1135 quoted above), but when we are actually dealing with a by-value xobj
1136 parameter we can proceed following this table.
1137 | iobj | xobj | equal |
1138 | none | none | X |
1139 | none | c | X |
1140 | none | v | X |
1141 | none | cv | X |
1142 | c | none | O |
1143 | c | c | O |
1144 | c | v | O |
1145 | c | cv | O |
1146 | v | none | O |
1147 | v | c | O |
1148 | v | v | O |
1149 | v | cv | O |
1150 | cv | none | O |
1151 | cv | c | O |
1152 | cv | v | O |
1153 | cv | cv | O |
1154
1155 Additionally, if the iobj member function is ref qualified, we aren't
1156 ignoring the ref qualifier of the iobj parameter, so we can't be dealing
1157 with correspondence in that case either.
1158
1159 So to recap, if we have a by-value xobj parameter, we know for sure that
1160 we aren't dealing with corresponding object parameters if the iobj member
1161 function has any cv-ref qualifiers. The only case where we might still be
1162 dealing with corresponding object parameters is when the iobj member
1163 function lacks any cv-ref qualification. */
1164 if (!TYPE_REF_P (xobj_param))
1165 {
1166 if (iobj_ref_qual || iobj_cv_quals)
1167 return false;
1168 }
1169 else
1170 {
1171 /* We are dealing with an xobj parameter that is a reference now, but due
1172 to [basic.scope.scope.3] we need to ignore its ref qual. */
1173 cp_ref_qualifier const xobj_ref_qual = [&](){
1174 if (!TYPE_REF_P (xobj_param) || !iobj_ref_qual)
1175 return REF_QUAL_NONE;
1176 return TYPE_REF_IS_RVALUE (xobj_param) ? REF_QUAL_RVALUE
1177 : REF_QUAL_LVALUE;
1178 }(); /* IILE. */
1179
1180 /* Even if we are ignoring the reference qualifier, the xobj parameter
1181 was still a reference so we still take the cv qualifiers into
1182 account. */
1183 cp_cv_quals const xobj_cv_quals
1184 = cp_type_quals (TREE_TYPE (xobj_param)) & cv_bits;
1185
1186 /* Finally, if the qualifications don't match exactly, the object
1187 parameters don't correspond. */
1188 if (iobj_ref_qual != xobj_ref_qual
1189 || iobj_cv_quals != xobj_cv_quals)
1190 return false;
1191 }
1192 /* If we got past everything else, the object parameters of fn1 and fn2
1193 definitely correspond. */
1194 return true;
1195 }
1196
1197 /* True if FN and METHOD have corresponding object parms per
1198 [basic.scope.scope], or if one of them is a static member function (which
1199 are considered to have an object parm that corresponds to any other).
1200 CONTEXT is the class that an implicit object member function is considered
1201 to be a member of for the purpose of this comparison, per
1202 [over.match.funcs]. */
1203
1204 bool
1205 object_parms_correspond (tree fn, tree method, tree context)
1206 {
1207 tree fn_type = TREE_TYPE (fn);
1208 tree method_type = TREE_TYPE (method);
1209
1210 /* Compare the quals on the 'this' parm. Don't compare
1211 the whole types, as used functions are treated as
1212 coming from the using class in overload resolution. */
1213 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)
1214 && DECL_IOBJ_MEMBER_FUNCTION_P (method))
1215 {
1216 /* Either both or neither need to be ref-qualified for
1217 differing quals to allow overloading. */
1218 if ((FUNCTION_REF_QUALIFIED (fn_type)
1219 == FUNCTION_REF_QUALIFIED (method_type))
1220 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1221 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1222 return false;
1223 return true;
1224 }
1225 /* Treat a static member function as corresponding to any object parm. */
1226 else if (DECL_STATIC_FUNCTION_P (fn) || DECL_STATIC_FUNCTION_P (method))
1227 return true;
1228 /* Handle special correspondence rules for xobj vs xobj and xobj vs iobj
1229 member function declarations.
1230 We don't worry about static member functions here. */
1231 else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
1232 && DECL_XOBJ_MEMBER_FUNCTION_P (method))
1233 {
1234 auto get_object_param = [] (tree fn)
1235 {
1236 return TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)));
1237 };
1238 /* We skip the object parameter below, check it here instead of
1239 making changes to that code. */
1240 tree fn_param = get_object_param (fn);
1241 tree method_param = get_object_param (method);
1242 if (!same_type_p (fn_param, method_param))
1243 return false;
1244 }
1245 else
1246 {
1247 tree xobj_fn = DECL_XOBJ_MEMBER_FUNCTION_P (fn) ? fn : method;
1248 tree iobj_fn = xobj_fn != fn ? fn : method;
1249 tree xobj_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (xobj_fn)));
1250
1251 return iobj_parm_corresponds_to (iobj_fn, xobj_param, context);
1252 }
1253
1254 return true;
1255 }
1256
1257 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1258 METHOD is being injected via a using_decl. Returns true if the
1259 method could be added to the method vec. */
1260
1261 bool
1262 add_method (tree type, tree method, bool via_using)
1263 {
1264 if (method == error_mark_node)
1265 return false;
1266
1267 gcc_assert (!DECL_EXTERN_C_P (method));
1268
1269 tree *slot = find_member_slot (type, DECL_NAME (method));
1270 tree current_fns = slot ? *slot : NULL_TREE;
1271
1272 /* See below. */
1273 int losem = -1;
1274
1275 /* Check to see if we've already got this method. */
1276 for (ovl_iterator iter (current_fns); iter; ++iter)
1277 {
1278 tree fn = *iter;
1279
1280 if (TREE_CODE (fn) != TREE_CODE (method))
1281 continue;
1282
1283 /* Two using-declarations can coexist, we'll complain about ambiguity in
1284 overload resolution. */
1285 if (via_using && iter.using_p ()
1286 /* Except handle inherited constructors specially. */
1287 && ! DECL_CONSTRUCTOR_P (fn))
1288 {
1289 if (fn == method)
1290 /* Don't add the same one twice. */
1291 return false;
1292 continue;
1293 }
1294
1295 /* [over.load] Member function declarations with the
1296 same name and the same parameter types cannot be
1297 overloaded if any of them is a static member
1298 function declaration.
1299
1300 [over.load] Member function declarations with the same name and
1301 the same parameter-type-list as well as member function template
1302 declarations with the same name, the same parameter-type-list, and
1303 the same template parameter lists cannot be overloaded if any of
1304 them, but not all, have a ref-qualifier.
1305
1306 [namespace.udecl] When a using-declaration brings names
1307 from a base class into a derived class scope, member
1308 functions in the derived class override and/or hide member
1309 functions with the same name and parameter types in a base
1310 class (rather than conflicting). */
1311 if (!object_parms_correspond (fn, method, type))
1312 continue;
1313
1314 tree fn_type = TREE_TYPE (fn);
1315 tree method_type = TREE_TYPE (method);
1316
1317 tree real_fn = fn;
1318 tree real_method = method;
1319
1320 /* Templates and conversion ops must match return types. */
1321 if ((DECL_CONV_FN_P (fn) || TREE_CODE (fn) == TEMPLATE_DECL)
1322 && !same_type_p (TREE_TYPE (fn_type), TREE_TYPE (method_type)))
1323 continue;
1324
1325 /* For templates, the template parameters must be identical. */
1326 if (TREE_CODE (fn) == TEMPLATE_DECL)
1327 {
1328 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1329 DECL_TEMPLATE_PARMS (method)))
1330 continue;
1331
1332 real_fn = DECL_TEMPLATE_RESULT (fn);
1333 real_method = DECL_TEMPLATE_RESULT (method);
1334 }
1335
1336 tree parms1 = TYPE_ARG_TYPES (fn_type);
1337 tree parms2 = TYPE_ARG_TYPES (method_type);
1338 if (! DECL_STATIC_FUNCTION_P (real_fn))
1339 parms1 = TREE_CHAIN (parms1);
1340 if (! DECL_STATIC_FUNCTION_P (real_method))
1341 parms2 = TREE_CHAIN (parms2);
1342
1343 /* Bring back parameters omitted from an inherited ctor. The
1344 method and the function can have different omittedness. */
1345 if (ctor_omit_inherited_parms (real_fn))
1346 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_CLONED_FUNCTION (real_fn));
1347 if (ctor_omit_inherited_parms (real_method))
1348 parms2 = (FUNCTION_FIRST_USER_PARMTYPE
1349 (DECL_CLONED_FUNCTION (real_method)));
1350
1351 if (!compparms (parms1, parms2))
1352 continue;
1353
1354 tree fn_constraints = get_constraints (fn);
1355 tree method_constraints = get_constraints (method);
1356
1357 if (fn_constraints && method_constraints
1358 && DECL_CONTEXT (fn) != type
1359 && !processing_template_decl)
1360 {
1361 if (TREE_CODE (fn) == TEMPLATE_DECL)
1362 ++processing_template_decl;
1363 if (tree outer_args = outer_template_args (fn))
1364 fn_constraints = tsubst_constraint_info (fn_constraints,
1365 outer_args,
1366 tf_warning_or_error,
1367 fn);
1368 if (tree outer_args = outer_template_args (method))
1369 method_constraints = tsubst_constraint_info (method_constraints,
1370 outer_args,
1371 tf_warning_or_error,
1372 method);
1373 if (TREE_CODE (fn) == TEMPLATE_DECL)
1374 --processing_template_decl;
1375 }
1376
1377 if (!equivalent_constraints (fn_constraints, method_constraints))
1378 {
1379 if (processing_template_decl)
1380 /* We can't check satisfaction in dependent context, wait until
1381 the class is instantiated. */
1382 continue;
1383
1384 special_function_kind sfk = special_memfn_p (method);
1385
1386 if (sfk == sfk_none
1387 || DECL_INHERITED_CTOR (fn)
1388 || TREE_CODE (fn) == TEMPLATE_DECL)
1389 /* Member function templates and non-special member functions
1390 coexist if they are not equivalently constrained. A member
1391 function is not hidden by an inherited constructor. */
1392 continue;
1393
1394 /* P0848: For special member functions, deleted, unsatisfied, or
1395 less constrained overloads are ineligible. We implement this
1396 by removing them from CLASSTYPE_MEMBER_VEC. Destructors don't
1397 use the notion of eligibility, and the selected destructor can
1398 be deleted, but removing unsatisfied or less constrained
1399 overloads has the same effect as overload resolution. */
1400 bool dtor = (sfk == sfk_destructor);
1401 if (losem == -1)
1402 losem = ((!dtor && DECL_DELETED_FN (method))
1403 || !constraints_satisfied_p (method));
1404 bool losef = ((!dtor && DECL_DELETED_FN (fn))
1405 || !constraints_satisfied_p (fn));
1406 int win;
1407 if (losem || losef)
1408 win = losem - losef;
1409 else
1410 win = more_constrained (fn, method);
1411 if (win > 0)
1412 /* Leave FN in the method vec, discard METHOD. */
1413 return false;
1414 else if (win < 0)
1415 {
1416 /* Remove FN, add METHOD. */
1417 current_fns = iter.remove_node (current_fns);
1418 continue;
1419 }
1420 else
1421 /* Let them coexist for now. */
1422 continue;
1423 }
1424
1425 /* If these are versions of the same function, process and
1426 move on. */
1427 if (TREE_CODE (fn) == FUNCTION_DECL
1428 && maybe_version_functions (method, fn, true))
1429 continue;
1430
1431 if (DECL_INHERITED_CTOR (method))
1432 {
1433 if (!DECL_INHERITED_CTOR (fn))
1434 /* Defer to the other function. */
1435 return false;
1436
1437 tree basem = DECL_INHERITED_CTOR_BASE (method);
1438 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1439 if (flag_new_inheriting_ctors)
1440 {
1441 if (basem == basef)
1442 {
1443 /* Inheriting the same constructor along different
1444 paths, combine them. */
1445 SET_DECL_INHERITED_CTOR
1446 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1447 DECL_INHERITED_CTOR (fn)));
1448 /* And discard the new one. */
1449 return false;
1450 }
1451 else
1452 /* Inherited ctors can coexist until overload
1453 resolution. */
1454 continue;
1455 }
1456
1457 auto_diagnostic_group d;
1458 error_at (DECL_SOURCE_LOCATION (method),
1459 "%q#D conflicts with version inherited from %qT",
1460 method, basef);
1461 inform (DECL_SOURCE_LOCATION (fn),
1462 "version inherited from %qT declared here",
1463 basef);
1464 return false;
1465 }
1466
1467 if (via_using)
1468 /* Defer to the local function. */
1469 return false;
1470 else if (iter.using_p ()
1471 || (flag_new_inheriting_ctors
1472 && DECL_INHERITED_CTOR (fn)))
1473 {
1474 /* Remove the inherited function. */
1475 current_fns = iter.remove_node (current_fns);
1476 continue;
1477 }
1478 else
1479 {
1480 auto_diagnostic_group d;
1481 error_at (DECL_SOURCE_LOCATION (method),
1482 "%q#D cannot be overloaded with %q#D", method, fn);
1483 inform (DECL_SOURCE_LOCATION (fn),
1484 "previous declaration %q#D", fn);
1485 return false;
1486 }
1487 }
1488
1489 current_fns = ovl_insert (method, current_fns, via_using);
1490
1491 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1492 && !push_class_level_binding (DECL_NAME (method), current_fns))
1493 return false;
1494
1495 if (!slot)
1496 slot = add_member_slot (type, DECL_NAME (method));
1497
1498 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1499 grok_special_member_properties (method);
1500
1501 *slot = current_fns;
1502
1503 return true;
1504 }
1505
1506 /* Subroutines of finish_struct. */
1507
1508 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1509 legit, otherwise return 0. */
1510
1511 static int
1512 alter_access (tree t, tree fdecl, tree access)
1513 {
1514 tree elem;
1515
1516 retrofit_lang_decl (fdecl);
1517
1518 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1519
1520 elem = purpose_member (t, DECL_ACCESS (fdecl));
1521 if (elem)
1522 {
1523 if (TREE_VALUE (elem) != access)
1524 {
1525 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1526 error ("conflicting access specifications for method"
1527 " %q+D, ignored", TREE_TYPE (fdecl));
1528 else
1529 error ("conflicting access specifications for field %qE, ignored",
1530 DECL_NAME (fdecl));
1531 }
1532 else
1533 {
1534 /* They're changing the access to the same thing they changed
1535 it to before. That's OK. */
1536 ;
1537 }
1538 }
1539 else
1540 {
1541 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1542 tf_warning_or_error);
1543 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1544 return 1;
1545 }
1546 return 0;
1547 }
1548
1549 /* Return the access node for DECL's access in its enclosing class. */
1550
1551 tree
1552 declared_access (tree decl)
1553 {
1554 return (TREE_PRIVATE (decl) ? access_private_node
1555 : TREE_PROTECTED (decl) ? access_protected_node
1556 : access_public_node);
1557 }
1558
1559 /* If DECL is a non-dependent using of non-ctor function members, push them
1560 and return true, otherwise return false. Called from
1561 finish_member_declaration. */
1562
1563 bool
1564 maybe_push_used_methods (tree decl)
1565 {
1566 if (TREE_CODE (decl) != USING_DECL)
1567 return false;
1568 tree used = strip_using_decl (decl);
1569 if (!used || !is_overloaded_fn (used))
1570 return false;
1571
1572 /* Add the functions to CLASSTYPE_MEMBER_VEC so that overload resolution
1573 works within the class body. */
1574 for (tree f : ovl_range (used))
1575 {
1576 if (DECL_CONSTRUCTOR_P (f))
1577 /* Inheriting constructors are handled separately. */
1578 return false;
1579
1580 bool added = add_method (current_class_type, f, true);
1581
1582 if (added)
1583 alter_access (current_class_type, f, current_access_specifier);
1584
1585 /* If add_method returns false because f was already declared, look
1586 for a duplicate using-declaration. */
1587 else
1588 for (tree d = TYPE_FIELDS (current_class_type); d; d = DECL_CHAIN (d))
1589 if (TREE_CODE (d) == USING_DECL
1590 && DECL_NAME (d) == DECL_NAME (decl)
1591 && same_type_p (USING_DECL_SCOPE (d), USING_DECL_SCOPE (decl)))
1592 {
1593 diagnose_name_conflict (decl, d);
1594 break;
1595 }
1596 }
1597 return true;
1598 }
1599
1600 /* Process the USING_DECL, which is a member of T. */
1601
1602 static void
1603 handle_using_decl (tree using_decl, tree t)
1604 {
1605 tree decl = USING_DECL_DECLS (using_decl);
1606
1607 gcc_assert (!processing_template_decl && decl);
1608
1609 cp_emit_debug_info_for_using (decl, t);
1610
1611 if (is_overloaded_fn (decl))
1612 /* Handled in maybe_push_used_methods. */
1613 return;
1614
1615 tree name = DECL_NAME (using_decl);
1616 tree old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1617 tf_warning_or_error);
1618 if (old_value)
1619 {
1620 old_value = OVL_FIRST (old_value);
1621
1622 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1623 /* OK */;
1624 else
1625 old_value = NULL_TREE;
1626 }
1627
1628 if (! old_value)
1629 ;
1630 else if (is_overloaded_fn (old_value))
1631 {
1632 auto_diagnostic_group d;
1633 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1634 "because of local method %q#D with same name",
1635 using_decl, t, old_value);
1636 inform (DECL_SOURCE_LOCATION (old_value),
1637 "local method %q#D declared here", old_value);
1638 return;
1639 }
1640 else if (!DECL_ARTIFICIAL (old_value))
1641 {
1642 auto_diagnostic_group d;
1643 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1644 "because of local member %q#D with same name",
1645 using_decl, t, old_value);
1646 inform (DECL_SOURCE_LOCATION (old_value),
1647 "local member %q#D declared here", old_value);
1648 return;
1649 }
1650
1651 iloc_sentinel ils (DECL_SOURCE_LOCATION (using_decl));
1652 tree access = declared_access (using_decl);
1653
1654 /* Make type T see field decl FDECL with access ACCESS. */
1655 if (USING_DECL_UNRELATED_P (using_decl))
1656 {
1657 /* C++20 using enum can import non-inherited enumerators into class
1658 scope. We implement that by making a copy of the CONST_DECL for which
1659 CONST_DECL_USING_P is true. */
1660 gcc_assert (TREE_CODE (decl) == CONST_DECL);
1661
1662 auto cas = make_temp_override (current_access_specifier, access);
1663 tree copy = copy_decl (decl);
1664 DECL_CONTEXT (copy) = t;
1665 DECL_ARTIFICIAL (copy) = true;
1666 /* We emitted debug info for the USING_DECL above; make sure we don't
1667 also emit anything for this clone. */
1668 DECL_IGNORED_P (copy) = true;
1669 DECL_SOURCE_LOCATION (copy) = DECL_SOURCE_LOCATION (using_decl);
1670 finish_member_declaration (copy);
1671 DECL_ABSTRACT_ORIGIN (copy) = decl;
1672 }
1673 else
1674 alter_access (t, decl, access);
1675 }
1676
1677 /* Data structure for find_abi_tags_r, below. */
1678
1679 struct abi_tag_data
1680 {
1681 tree t; // The type that we're checking for missing tags.
1682 tree subob; // The subobject of T that we're getting tags from.
1683 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1684 };
1685
1686 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1687 in the context of P. TAG can be either an identifier (the DECL_NAME of
1688 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1689
1690 static void
1691 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1692 {
1693 if (!IDENTIFIER_MARKED (id))
1694 {
1695 if (p->tags != error_mark_node)
1696 {
1697 /* We're collecting tags from template arguments or from
1698 the type of a variable or function return type. */
1699 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1700
1701 /* Don't inherit this tag multiple times. */
1702 IDENTIFIER_MARKED (id) = true;
1703
1704 if (TYPE_P (p->t))
1705 {
1706 /* Tags inherited from type template arguments are only used
1707 to avoid warnings. */
1708 ABI_TAG_IMPLICIT (p->tags) = true;
1709 return;
1710 }
1711 /* For functions and variables we want to warn, too. */
1712 }
1713
1714 /* Otherwise we're diagnosing missing tags. */
1715 if (TREE_CODE (p->t) == FUNCTION_DECL)
1716 {
1717 auto_diagnostic_group d;
1718 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1719 "that %qT (used in its return type) has",
1720 p->t, tag, *tp))
1721 inform (location_of (*tp), "%qT declared here", *tp);
1722 }
1723 else if (VAR_P (p->t))
1724 {
1725 auto_diagnostic_group d;
1726 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1727 "that %qT (used in its type) has", p->t, tag, *tp))
1728 inform (location_of (*tp), "%qT declared here", *tp);
1729 }
1730 else if (TYPE_P (p->subob))
1731 {
1732 auto_diagnostic_group d;
1733 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1734 "that base %qT has", p->t, tag, p->subob))
1735 inform (location_of (p->subob), "%qT declared here",
1736 p->subob);
1737 }
1738 else
1739 {
1740 auto_diagnostic_group d;
1741 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1742 "that %qT (used in the type of %qD) has",
1743 p->t, tag, *tp, p->subob))
1744 {
1745 inform (location_of (p->subob), "%qD declared here",
1746 p->subob);
1747 inform (location_of (*tp), "%qT declared here", *tp);
1748 }
1749 }
1750 }
1751 }
1752
1753 /* Find all the ABI tags in the attribute list ATTR and either call
1754 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1755
1756 static void
1757 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1758 {
1759 if (!attr)
1760 return;
1761 for (; (attr = lookup_attribute ("abi_tag", attr));
1762 attr = TREE_CHAIN (attr))
1763 for (tree list = TREE_VALUE (attr); list;
1764 list = TREE_CHAIN (list))
1765 {
1766 tree tag = TREE_VALUE (list);
1767 tree id = get_identifier (TREE_STRING_POINTER (tag));
1768 if (tp)
1769 check_tag (tag, id, tp, p);
1770 else
1771 IDENTIFIER_MARKED (id) = val;
1772 }
1773 }
1774
1775 /* Find all the ABI tags on T and its enclosing scopes and either call
1776 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1777
1778 static void
1779 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1780 {
1781 while (t != global_namespace)
1782 {
1783 tree attr;
1784 if (TYPE_P (t))
1785 {
1786 attr = TYPE_ATTRIBUTES (t);
1787 t = CP_TYPE_CONTEXT (t);
1788 }
1789 else
1790 {
1791 attr = DECL_ATTRIBUTES (t);
1792 t = CP_DECL_CONTEXT (t);
1793 }
1794 mark_or_check_attr_tags (attr, tp, p, val);
1795 }
1796 }
1797
1798 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1799 types with ABI tags, add the corresponding identifiers to the VEC in
1800 *DATA and set IDENTIFIER_MARKED. */
1801
1802 static tree
1803 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1804 {
1805 if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1806 /* Tell cp_walk_subtrees to look though typedefs. [PR98481] */
1807 *walk_subtrees = 2;
1808
1809 if (!OVERLOAD_TYPE_P (*tp))
1810 return NULL_TREE;
1811
1812 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1813 anyway, but let's make sure of it. */
1814 *walk_subtrees = false;
1815
1816 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1817
1818 mark_or_check_tags (*tp, tp, p, false);
1819
1820 return NULL_TREE;
1821 }
1822
1823 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1824 IDENTIFIER_MARKED on its ABI tags. */
1825
1826 static tree
1827 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1828 {
1829 if (TYPE_P (*tp) && *walk_subtrees == 1 && flag_abi_version != 14)
1830 /* Tell cp_walk_subtrees to look though typedefs. */
1831 *walk_subtrees = 2;
1832
1833 if (!OVERLOAD_TYPE_P (*tp))
1834 return NULL_TREE;
1835
1836 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1837 anyway, but let's make sure of it. */
1838 *walk_subtrees = false;
1839
1840 bool *valp = static_cast<bool*>(data);
1841
1842 mark_or_check_tags (*tp, NULL, NULL, *valp);
1843
1844 return NULL_TREE;
1845 }
1846
1847 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1848 scopes. */
1849
1850 static void
1851 mark_abi_tags (tree t, bool val)
1852 {
1853 mark_or_check_tags (t, NULL, NULL, val);
1854 if (DECL_P (t))
1855 {
1856 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1857 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1858 {
1859 /* Template arguments are part of the signature. */
1860 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1861 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1862 {
1863 tree arg = TREE_VEC_ELT (level, j);
1864 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1865 }
1866 }
1867 if (TREE_CODE (t) == FUNCTION_DECL)
1868 /* A function's parameter types are part of the signature, so
1869 we don't need to inherit any tags that are also in them. */
1870 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1871 arg = TREE_CHAIN (arg))
1872 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1873 mark_abi_tags_r, &val);
1874 }
1875 }
1876
1877 /* Check that T has all the ABI tags that subobject SUBOB has, or
1878 warn if not. If T is a (variable or function) declaration, also
1879 return any missing tags, and add them to T if JUST_CHECKING is false. */
1880
1881 static tree
1882 check_abi_tags (tree t, tree subob, bool just_checking = false)
1883 {
1884 bool inherit = DECL_P (t);
1885
1886 if (!inherit && !warn_abi_tag)
1887 return NULL_TREE;
1888
1889 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1890 if (!TREE_PUBLIC (decl))
1891 /* No need to worry about things local to this TU. */
1892 return NULL_TREE;
1893
1894 mark_abi_tags (t, true);
1895
1896 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1897 struct abi_tag_data data = { t, subob, error_mark_node };
1898 if (inherit)
1899 data.tags = NULL_TREE;
1900
1901 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1902
1903 if (!(inherit && data.tags))
1904 /* We don't need to do anything with data.tags. */;
1905 else if (just_checking)
1906 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1907 {
1908 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1909 IDENTIFIER_MARKED (id) = false;
1910 }
1911 else
1912 {
1913 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1914 if (attr)
1915 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1916 else
1917 DECL_ATTRIBUTES (t)
1918 = tree_cons (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1919 }
1920
1921 mark_abi_tags (t, false);
1922
1923 return data.tags;
1924 }
1925
1926 /* Check that DECL has all the ABI tags that are used in parts of its type
1927 that are not reflected in its mangled name. */
1928
1929 void
1930 check_abi_tags (tree decl)
1931 {
1932 if (VAR_P (decl))
1933 check_abi_tags (decl, TREE_TYPE (decl));
1934 else if (TREE_CODE (decl) == FUNCTION_DECL
1935 && !DECL_CONV_FN_P (decl)
1936 && !mangle_return_type_p (decl))
1937 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1938 }
1939
1940 /* Return any ABI tags that are used in parts of the type of DECL
1941 that are not reflected in its mangled name. This function is only
1942 used in backward-compatible mangling for ABI <11. */
1943
1944 tree
1945 missing_abi_tags (tree decl)
1946 {
1947 if (VAR_P (decl))
1948 return check_abi_tags (decl, TREE_TYPE (decl), true);
1949 else if (TREE_CODE (decl) == FUNCTION_DECL
1950 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1951 that we can use this function for setting need_abi_warning
1952 regardless of the current flag_abi_version. */
1953 && !mangle_return_type_p (decl))
1954 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1955 else
1956 return NULL_TREE;
1957 }
1958
1959 void
1960 inherit_targ_abi_tags (tree t)
1961 {
1962 if (!CLASS_TYPE_P (t)
1963 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1964 return;
1965
1966 mark_abi_tags (t, true);
1967
1968 tree args = CLASSTYPE_TI_ARGS (t);
1969 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1970 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1971 {
1972 tree level = TMPL_ARGS_LEVEL (args, i+1);
1973 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1974 {
1975 tree arg = TREE_VEC_ELT (level, j);
1976 data.subob = arg;
1977 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1978 }
1979 }
1980
1981 // If we found some tags on our template arguments, add them to our
1982 // abi_tag attribute.
1983 if (data.tags)
1984 {
1985 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1986 if (attr)
1987 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1988 else
1989 TYPE_ATTRIBUTES (t)
1990 = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
1991 }
1992
1993 mark_abi_tags (t, false);
1994 }
1995
1996 /* Return true, iff class T has a non-virtual destructor that is
1997 accessible from outside the class heirarchy (i.e. is public, or
1998 there's a suitable friend. */
1999
2000 static bool
2001 accessible_nvdtor_p (tree t)
2002 {
2003 tree dtor = CLASSTYPE_DESTRUCTOR (t);
2004
2005 /* An implicitly declared destructor is always public. And,
2006 if it were virtual, we would have created it by now. */
2007 if (!dtor)
2008 return true;
2009
2010 if (DECL_VINDEX (dtor))
2011 return false; /* Virtual */
2012
2013 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
2014 return true; /* Public */
2015
2016 if (CLASSTYPE_FRIEND_CLASSES (t)
2017 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2018 return true; /* Has friends */
2019
2020 return false;
2021 }
2022
2023 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
2024 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
2025 properties of the bases. */
2026
2027 static void
2028 check_bases (tree t,
2029 int* cant_have_const_ctor_p,
2030 int* no_const_asn_ref_p)
2031 {
2032 int i;
2033 bool seen_non_virtual_nearly_empty_base_p = 0;
2034 int seen_tm_mask = 0;
2035 tree base_binfo;
2036 tree binfo;
2037 tree field = NULL_TREE;
2038
2039 if (!CLASSTYPE_NON_STD_LAYOUT (t))
2040 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2041 if (TREE_CODE (field) == FIELD_DECL)
2042 break;
2043
2044 for (binfo = TYPE_BINFO (t), i = 0;
2045 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2046 {
2047 tree basetype = TREE_TYPE (base_binfo);
2048
2049 gcc_assert (COMPLETE_TYPE_P (basetype));
2050
2051 if (CLASSTYPE_FINAL (basetype))
2052 error ("cannot derive from %<final%> base %qT in derived type %qT",
2053 basetype, t);
2054
2055 /* If any base class is non-literal, so is the derived class. */
2056 if (!CLASSTYPE_LITERAL_P (basetype))
2057 CLASSTYPE_LITERAL_P (t) = false;
2058
2059 /* If the base class doesn't have copy constructors or
2060 assignment operators that take const references, then the
2061 derived class cannot have such a member automatically
2062 generated. */
2063 if (TYPE_HAS_COPY_CTOR (basetype)
2064 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
2065 *cant_have_const_ctor_p = 1;
2066 if (TYPE_HAS_COPY_ASSIGN (basetype)
2067 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
2068 *no_const_asn_ref_p = 1;
2069
2070 if (BINFO_VIRTUAL_P (base_binfo))
2071 /* A virtual base does not effect nearly emptiness. */
2072 ;
2073 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
2074 {
2075 if (seen_non_virtual_nearly_empty_base_p)
2076 /* And if there is more than one nearly empty base, then the
2077 derived class is not nearly empty either. */
2078 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2079 else
2080 /* Remember we've seen one. */
2081 seen_non_virtual_nearly_empty_base_p = 1;
2082 }
2083 else if (!is_empty_class (basetype))
2084 /* If the base class is not empty or nearly empty, then this
2085 class cannot be nearly empty. */
2086 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
2087
2088 /* A lot of properties from the bases also apply to the derived
2089 class. */
2090 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
2091 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2092 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
2093 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
2094 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
2095 || !TYPE_HAS_COPY_ASSIGN (basetype));
2096 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
2097 || !TYPE_HAS_COPY_CTOR (basetype));
2098 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
2099 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
2100 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
2101 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
2102 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
2103 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
2104 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
2105 || TYPE_HAS_COMPLEX_DFLT (basetype));
2106 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
2107 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
2108 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
2109 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
2110 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
2111 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
2112 if (TYPE_HAS_MUTABLE_P (basetype))
2113 CLASSTYPE_HAS_MUTABLE (t) = 1;
2114
2115 /* A standard-layout class is a class that:
2116 ...
2117 * has no non-standard-layout base classes, */
2118 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
2119 if (!CLASSTYPE_NON_STD_LAYOUT (t))
2120 {
2121 tree basefield;
2122 /* ...has no base classes of the same type as the first non-static
2123 data member... */
2124 if (field && DECL_CONTEXT (field) == t
2125 && (same_type_ignoring_top_level_qualifiers_p
2126 (TREE_TYPE (field), basetype)))
2127 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2128 /* DR 1813:
2129 ...has at most one base class subobject of any given type... */
2130 else if (CLASSTYPE_REPEATED_BASE_P (t))
2131 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2132 else
2133 /* ...has all non-static data members and bit-fields in the class
2134 and its base classes first declared in the same class. */
2135 for (basefield = TYPE_FIELDS (basetype); basefield;
2136 basefield = DECL_CHAIN (basefield))
2137 if (TREE_CODE (basefield) == FIELD_DECL
2138 && !(DECL_FIELD_IS_BASE (basefield)
2139 && is_empty_field (basefield)))
2140 {
2141 if (field)
2142 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
2143 else
2144 field = basefield;
2145 break;
2146 }
2147 }
2148
2149 /* Don't bother collecting tm attributes if transactional memory
2150 support is not enabled. */
2151 if (flag_tm)
2152 {
2153 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
2154 if (tm_attr)
2155 seen_tm_mask |= tm_attr_to_mask (tm_attr);
2156 }
2157
2158 check_abi_tags (t, basetype);
2159 }
2160
2161 /* If one of the base classes had TM attributes, and the current class
2162 doesn't define its own, then the current class inherits one. */
2163 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
2164 {
2165 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
2166 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
2167 }
2168 }
2169
2170 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
2171 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
2172 that have had a nearly-empty virtual primary base stolen by some
2173 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
2174 T. */
2175
2176 static void
2177 determine_primary_bases (tree t)
2178 {
2179 unsigned i;
2180 tree primary = NULL_TREE;
2181 tree type_binfo = TYPE_BINFO (t);
2182 tree base_binfo;
2183
2184 /* Determine the primary bases of our bases. */
2185 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2186 base_binfo = TREE_CHAIN (base_binfo))
2187 {
2188 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
2189
2190 /* See if we're the non-virtual primary of our inheritance
2191 chain. */
2192 if (!BINFO_VIRTUAL_P (base_binfo))
2193 {
2194 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
2195 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
2196
2197 if (parent_primary
2198 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
2199 BINFO_TYPE (parent_primary)))
2200 /* We are the primary binfo. */
2201 BINFO_PRIMARY_P (base_binfo) = 1;
2202 }
2203 /* Determine if we have a virtual primary base, and mark it so.
2204 */
2205 if (primary && BINFO_VIRTUAL_P (primary))
2206 {
2207 tree this_primary = copied_binfo (primary, base_binfo);
2208
2209 if (BINFO_PRIMARY_P (this_primary))
2210 /* Someone already claimed this base. */
2211 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
2212 else
2213 {
2214 tree delta;
2215
2216 BINFO_PRIMARY_P (this_primary) = 1;
2217 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
2218
2219 /* A virtual binfo might have been copied from within
2220 another hierarchy. As we're about to use it as a
2221 primary base, make sure the offsets match. */
2222 delta = size_diffop_loc (input_location,
2223 fold_convert (ssizetype,
2224 BINFO_OFFSET (base_binfo)),
2225 fold_convert (ssizetype,
2226 BINFO_OFFSET (this_primary)));
2227
2228 propagate_binfo_offsets (this_primary, delta);
2229 }
2230 }
2231 }
2232
2233 /* First look for a dynamic direct non-virtual base. */
2234 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
2235 {
2236 tree basetype = BINFO_TYPE (base_binfo);
2237
2238 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
2239 {
2240 primary = base_binfo;
2241 goto found;
2242 }
2243 }
2244
2245 /* A "nearly-empty" virtual base class can be the primary base
2246 class, if no non-virtual polymorphic base can be found. Look for
2247 a nearly-empty virtual dynamic base that is not already a primary
2248 base of something in the hierarchy. If there is no such base,
2249 just pick the first nearly-empty virtual base. */
2250
2251 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
2252 base_binfo = TREE_CHAIN (base_binfo))
2253 if (BINFO_VIRTUAL_P (base_binfo)
2254 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
2255 {
2256 if (!BINFO_PRIMARY_P (base_binfo))
2257 {
2258 /* Found one that is not primary. */
2259 primary = base_binfo;
2260 goto found;
2261 }
2262 else if (!primary)
2263 /* Remember the first candidate. */
2264 primary = base_binfo;
2265 }
2266
2267 found:
2268 /* If we've got a primary base, use it. */
2269 if (primary)
2270 {
2271 tree basetype = BINFO_TYPE (primary);
2272
2273 CLASSTYPE_PRIMARY_BINFO (t) = primary;
2274 if (BINFO_PRIMARY_P (primary))
2275 /* We are stealing a primary base. */
2276 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
2277 BINFO_PRIMARY_P (primary) = 1;
2278 if (BINFO_VIRTUAL_P (primary))
2279 {
2280 tree delta;
2281
2282 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
2283 /* A virtual binfo might have been copied from within
2284 another hierarchy. As we're about to use it as a primary
2285 base, make sure the offsets match. */
2286 delta = size_diffop_loc (input_location, ssize_int (0),
2287 fold_convert (ssizetype, BINFO_OFFSET (primary)));
2288
2289 propagate_binfo_offsets (primary, delta);
2290 }
2291
2292 primary = TYPE_BINFO (basetype);
2293
2294 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2295 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2296 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2297 }
2298 }
2299
2300 /* Update the variant types of T. */
2301
2302 void
2303 fixup_type_variants (tree type)
2304 {
2305 if (!type)
2306 return;
2307
2308 for (tree variant = TYPE_NEXT_VARIANT (type);
2309 variant;
2310 variant = TYPE_NEXT_VARIANT (variant))
2311 {
2312 /* These fields are in the _TYPE part of the node, not in
2313 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2314 TYPE_HAS_USER_CONSTRUCTOR (variant) = TYPE_HAS_USER_CONSTRUCTOR (type);
2315 TYPE_NEEDS_CONSTRUCTING (variant) = TYPE_NEEDS_CONSTRUCTING (type);
2316 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variant)
2317 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2318
2319 TYPE_POLYMORPHIC_P (variant) = TYPE_POLYMORPHIC_P (type);
2320 CLASSTYPE_FINAL (variant) = CLASSTYPE_FINAL (type);
2321
2322 TYPE_BINFO (variant) = TYPE_BINFO (type);
2323
2324 /* Copy whatever these are holding today. */
2325 TYPE_VFIELD (variant) = TYPE_VFIELD (type);
2326 TYPE_FIELDS (variant) = TYPE_FIELDS (type);
2327
2328 TYPE_SIZE (variant) = TYPE_SIZE (type);
2329 TYPE_SIZE_UNIT (variant) = TYPE_SIZE_UNIT (type);
2330
2331 if (!TYPE_USER_ALIGN (variant)
2332 || TYPE_NAME (variant) == TYPE_NAME (type)
2333 || TYPE_ALIGN_RAW (variant) < TYPE_ALIGN_RAW (type))
2334 {
2335 TYPE_ALIGN_RAW (variant) = TYPE_ALIGN_RAW (type);
2336 TYPE_USER_ALIGN (variant) = TYPE_USER_ALIGN (type);
2337 }
2338
2339 TYPE_PRECISION (variant) = TYPE_PRECISION (type);
2340 TYPE_MODE_RAW (variant) = TYPE_MODE_RAW (type);
2341 TYPE_EMPTY_P (variant) = TYPE_EMPTY_P (type);
2342 TREE_ADDRESSABLE (variant) = TREE_ADDRESSABLE (type);
2343 }
2344 }
2345
2346 /* KLASS is a class that we're applying may_alias to after the body is
2347 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2348 canonical type(s) will be implicitly updated. */
2349
2350 static void
2351 fixup_may_alias (tree klass)
2352 {
2353 tree t, v;
2354
2355 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2356 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2357 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2358 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2359 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2360 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2361 }
2362
2363 /* Early variant fixups: we apply attributes at the beginning of the class
2364 definition, and we need to fix up any variants that have already been
2365 made via elaborated-type-specifier so that check_qualified_type works. */
2366
2367 void
2368 fixup_attribute_variants (tree t)
2369 {
2370 tree variants;
2371
2372 if (!t)
2373 return;
2374
2375 tree attrs = TYPE_ATTRIBUTES (t);
2376 unsigned align = TYPE_ALIGN (t);
2377 bool user_align = TYPE_USER_ALIGN (t);
2378 bool may_alias = lookup_attribute ("may_alias", attrs);
2379 bool packed = TYPE_PACKED (t);
2380
2381 if (may_alias)
2382 fixup_may_alias (t);
2383
2384 for (variants = TYPE_NEXT_VARIANT (t);
2385 variants;
2386 variants = TYPE_NEXT_VARIANT (variants))
2387 {
2388 /* These are the two fields that check_qualified_type looks at and
2389 are affected by attributes. */
2390 TYPE_ATTRIBUTES (variants) = attrs;
2391 unsigned valign = align;
2392 if (TYPE_USER_ALIGN (variants))
2393 valign = MAX (valign, TYPE_ALIGN (variants));
2394 else
2395 TYPE_USER_ALIGN (variants) = user_align;
2396 SET_TYPE_ALIGN (variants, valign);
2397 TYPE_PACKED (variants) = packed;
2398 if (may_alias)
2399 fixup_may_alias (variants);
2400 }
2401 }
2402
2403 /* Set memoizing fields and bits of T (and its variants) for later
2404 use. */
2405
2406 static void
2407 finish_struct_bits (tree t)
2408 {
2409 /* If this type has a copy constructor or a destructor, force its
2410 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2411 nonzero. This will cause it to be passed by invisible reference
2412 and prevent it from being returned in a register. */
2413 if (type_has_nontrivial_copy_init (t)
2414 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2415 {
2416 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2417 SET_TYPE_MODE (t, BLKmode);
2418 TREE_ADDRESSABLE (t) = 1;
2419 }
2420
2421 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2422 /* For a class w/o baseclasses, 'finish_struct' has set
2423 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2424 Similarly for a class whose base classes do not have vtables.
2425 When neither of these is true, we might have removed abstract
2426 virtuals (by providing a definition), added some (by declaring
2427 new ones), or redeclared ones from a base class. We need to
2428 recalculate what's really an abstract virtual at this point (by
2429 looking in the vtables). */
2430 get_pure_virtuals (t);
2431
2432 /* Fix up variants (if any). */
2433 fixup_type_variants (t);
2434 }
2435
2436 /* Issue warnings about T having private constructors, but no friends,
2437 and so forth.
2438
2439 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2440 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2441 non-private static member functions. */
2442
2443 static void
2444 maybe_warn_about_overly_private_class (tree t)
2445 {
2446 int has_member_fn = 0;
2447 int has_nonprivate_method = 0;
2448 bool nonprivate_ctor = false;
2449
2450 if (!warn_ctor_dtor_privacy
2451 /* If the class has friends, those entities might create and
2452 access instances, so we should not warn. */
2453 || (CLASSTYPE_FRIEND_CLASSES (t)
2454 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2455 /* We will have warned when the template was declared; there's
2456 no need to warn on every instantiation. */
2457 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2458 /* There's no reason to even consider warning about this
2459 class. */
2460 return;
2461
2462 /* We only issue one warning, if more than one applies, because
2463 otherwise, on code like:
2464
2465 class A {
2466 // Oops - forgot `public:'
2467 A();
2468 A(const A&);
2469 ~A();
2470 };
2471
2472 we warn several times about essentially the same problem. */
2473
2474 /* Check to see if all (non-constructor, non-destructor) member
2475 functions are private. (Since there are no friends or
2476 non-private statics, we can't ever call any of the private member
2477 functions.) */
2478 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2479 if (TREE_CODE (fn) == USING_DECL
2480 && DECL_NAME (fn) == ctor_identifier
2481 && !TREE_PRIVATE (fn))
2482 nonprivate_ctor = true;
2483 else if (!DECL_DECLARES_FUNCTION_P (fn))
2484 /* Not a function. */;
2485 else if (DECL_ARTIFICIAL (fn))
2486 /* We're not interested in compiler-generated methods; they don't
2487 provide any way to call private members. */;
2488 else if (!TREE_PRIVATE (fn))
2489 {
2490 if (DECL_STATIC_FUNCTION_P (fn))
2491 /* A non-private static member function is just like a
2492 friend; it can create and invoke private member
2493 functions, and be accessed without a class
2494 instance. */
2495 return;
2496
2497 has_nonprivate_method = 1;
2498 /* Keep searching for a static member function. */
2499 }
2500 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2501 has_member_fn = 1;
2502
2503 if (!has_nonprivate_method && has_member_fn)
2504 {
2505 /* There are no non-private methods, and there's at least one
2506 private member function that isn't a constructor or
2507 destructor. (If all the private members are
2508 constructors/destructors we want to use the code below that
2509 issues error messages specifically referring to
2510 constructors/destructors.) */
2511 unsigned i;
2512 tree binfo = TYPE_BINFO (t);
2513
2514 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2515 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2516 {
2517 has_nonprivate_method = 1;
2518 break;
2519 }
2520 if (!has_nonprivate_method)
2521 {
2522 warning (OPT_Wctor_dtor_privacy,
2523 "all member functions in class %qT are private", t);
2524 return;
2525 }
2526 }
2527
2528 /* Even if some of the member functions are non-private, the class
2529 won't be useful for much if all the constructors or destructors
2530 are private: such an object can never be created or destroyed. */
2531 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2532 if (TREE_PRIVATE (dtor))
2533 {
2534 warning (OPT_Wctor_dtor_privacy,
2535 "%q#T only defines a private destructor and has no friends",
2536 t);
2537 return;
2538 }
2539
2540 /* Warn about classes that have private constructors and no friends. */
2541 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2542 /* Implicitly generated constructors are always public. */
2543 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2544 {
2545 tree copy_or_move = NULL_TREE;
2546
2547 /* If a non-template class does not define a copy
2548 constructor, one is defined for it, enabling it to avoid
2549 this warning. For a template class, this does not
2550 happen, and so we would normally get a warning on:
2551
2552 template <class T> class C { private: C(); };
2553
2554 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2555 complete non-template or fully instantiated classes have this
2556 flag set. */
2557 if (!TYPE_HAS_COPY_CTOR (t))
2558 nonprivate_ctor = true;
2559 else
2560 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
2561 if (TREE_PRIVATE (fn))
2562 continue;
2563 else if (copy_fn_p (fn) || move_fn_p (fn))
2564 /* Ideally, we wouldn't count any constructor that takes
2565 an argument of the class type as a parameter, because
2566 such things cannot be used to construct an instance of
2567 the class unless you already have one. */
2568 copy_or_move = fn;
2569 else
2570 {
2571 nonprivate_ctor = true;
2572 break;
2573 }
2574
2575 if (!nonprivate_ctor)
2576 {
2577 auto_diagnostic_group d;
2578 bool w = warning (OPT_Wctor_dtor_privacy,
2579 "%q#T only defines private constructors and has "
2580 "no friends", t);
2581 if (w && copy_or_move)
2582 inform (DECL_SOURCE_LOCATION (copy_or_move),
2583 "%q#D is public, but requires an existing %q#T object",
2584 copy_or_move, t);
2585 return;
2586 }
2587 }
2588 }
2589
2590 /* Make BINFO's vtable have N entries, including RTTI entries,
2591 vbase and vcall offsets, etc. Set its type and call the back end
2592 to lay it out. */
2593
2594 static void
2595 layout_vtable_decl (tree binfo, int n)
2596 {
2597 tree atype;
2598 tree vtable;
2599
2600 atype = build_array_of_n_type (vtable_entry_type, n);
2601 layout_type (atype);
2602
2603 /* We may have to grow the vtable. */
2604 vtable = get_vtbl_decl_for_binfo (binfo);
2605 if (!same_type_p (TREE_TYPE (vtable), atype))
2606 {
2607 TREE_TYPE (vtable) = atype;
2608 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2609 layout_decl (vtable, 0);
2610 }
2611 }
2612
2613 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2614 have the same signature. */
2615
2616 int
2617 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2618 {
2619 /* One destructor overrides another if they are the same kind of
2620 destructor. */
2621 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2622 && special_function_p (base_fndecl) == special_function_p (fndecl))
2623 return 1;
2624 /* But a non-destructor never overrides a destructor, nor vice
2625 versa, nor do different kinds of destructors override
2626 one-another. For example, a complete object destructor does not
2627 override a deleting destructor. */
2628 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2629 return 0;
2630
2631 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2632 || (DECL_CONV_FN_P (fndecl)
2633 && DECL_CONV_FN_P (base_fndecl)
2634 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2635 DECL_CONV_FN_TYPE (base_fndecl))))
2636 {
2637 tree fntype = TREE_TYPE (fndecl);
2638 tree base_fntype = TREE_TYPE (base_fndecl);
2639 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2640 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2641 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2642 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2643 return 1;
2644 }
2645 return 0;
2646 }
2647
2648 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2649 subobject. */
2650
2651 static bool
2652 base_derived_from (tree derived, tree base)
2653 {
2654 tree probe;
2655
2656 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2657 {
2658 if (probe == derived)
2659 return true;
2660 else if (BINFO_VIRTUAL_P (probe))
2661 /* If we meet a virtual base, we can't follow the inheritance
2662 any more. See if the complete type of DERIVED contains
2663 such a virtual base. */
2664 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2665 != NULL_TREE);
2666 }
2667 return false;
2668 }
2669
2670 struct find_final_overrider_data {
2671 /* The function for which we are trying to find a final overrider. */
2672 tree fn;
2673 /* The base class in which the function was declared. */
2674 tree declaring_base;
2675 /* The candidate overriders. */
2676 tree candidates;
2677 /* Path to most derived. */
2678 auto_vec<tree> path;
2679 };
2680
2681 /* Add the overrider along the current path to FFOD->CANDIDATES.
2682 Returns true if an overrider was found; false otherwise. */
2683
2684 static bool
2685 dfs_find_final_overrider_1 (tree binfo,
2686 find_final_overrider_data *ffod,
2687 unsigned depth)
2688 {
2689 tree method;
2690
2691 /* If BINFO is not the most derived type, try a more derived class.
2692 A definition there will overrider a definition here. */
2693 if (depth)
2694 {
2695 depth--;
2696 if (dfs_find_final_overrider_1
2697 (ffod->path[depth], ffod, depth))
2698 return true;
2699 }
2700
2701 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2702 if (method)
2703 {
2704 tree *candidate = &ffod->candidates;
2705
2706 /* Remove any candidates overridden by this new function. */
2707 while (*candidate)
2708 {
2709 /* If *CANDIDATE overrides METHOD, then METHOD
2710 cannot override anything else on the list. */
2711 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2712 return true;
2713 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2714 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2715 *candidate = TREE_CHAIN (*candidate);
2716 else
2717 candidate = &TREE_CHAIN (*candidate);
2718 }
2719
2720 /* Add the new function. */
2721 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2722 return true;
2723 }
2724
2725 return false;
2726 }
2727
2728 /* Called from find_final_overrider via dfs_walk. */
2729
2730 static tree
2731 dfs_find_final_overrider_pre (tree binfo, void *data)
2732 {
2733 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2734
2735 if (binfo == ffod->declaring_base)
2736 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2737 ffod->path.safe_push (binfo);
2738
2739 return NULL_TREE;
2740 }
2741
2742 static tree
2743 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2744 {
2745 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2746 ffod->path.pop ();
2747
2748 return NULL_TREE;
2749 }
2750
2751 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2752 FN and whose TREE_VALUE is the binfo for the base where the
2753 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2754 DERIVED) is the base object in which FN is declared. */
2755
2756 static tree
2757 find_final_overrider (tree derived, tree binfo, tree fn)
2758 {
2759 find_final_overrider_data ffod;
2760
2761 /* Getting this right is a little tricky. This is valid:
2762
2763 struct S { virtual void f (); };
2764 struct T { virtual void f (); };
2765 struct U : public S, public T { };
2766
2767 even though calling `f' in `U' is ambiguous. But,
2768
2769 struct R { virtual void f(); };
2770 struct S : virtual public R { virtual void f (); };
2771 struct T : virtual public R { virtual void f (); };
2772 struct U : public S, public T { };
2773
2774 is not -- there's no way to decide whether to put `S::f' or
2775 `T::f' in the vtable for `R'.
2776
2777 The solution is to look at all paths to BINFO. If we find
2778 different overriders along any two, then there is a problem. */
2779 if (DECL_THUNK_P (fn))
2780 fn = THUNK_TARGET (fn);
2781
2782 /* Determine the depth of the hierarchy. */
2783 ffod.fn = fn;
2784 ffod.declaring_base = binfo;
2785 ffod.candidates = NULL_TREE;
2786 ffod.path.create (30);
2787
2788 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2789 dfs_find_final_overrider_post, &ffod);
2790
2791 /* If there was no winner, issue an error message. */
2792 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2793 return error_mark_node;
2794
2795 return ffod.candidates;
2796 }
2797
2798 /* Return the index of the vcall offset for FN when TYPE is used as a
2799 virtual base. */
2800
2801 static tree
2802 get_vcall_index (tree fn, tree type)
2803 {
2804 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2805 tree_pair_p p;
2806 unsigned ix;
2807
2808 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2809 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2810 || same_signature_p (fn, p->purpose))
2811 return p->value;
2812
2813 /* There should always be an appropriate index. */
2814 gcc_unreachable ();
2815 }
2816
2817 /* Given a DECL_VINDEX of a virtual function found in BINFO, return the final
2818 overrider at that index in the vtable. This should only be used when we
2819 know that BINFO is correct for the dynamic type of the object. */
2820
2821 tree
2822 lookup_vfn_in_binfo (tree idx, tree binfo)
2823 {
2824 int ix = tree_to_shwi (idx);
2825 if (TARGET_VTABLE_USES_DESCRIPTORS)
2826 ix /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
2827 while (BINFO_PRIMARY_P (binfo))
2828 /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
2829 class that actually owns the vtable. */
2830 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2831 tree virtuals = BINFO_VIRTUALS (binfo);
2832 return TREE_VALUE (chain_index (ix, virtuals));
2833 }
2834
2835 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2836 dominated by T. FN is the old function; VIRTUALS points to the
2837 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2838 of that entry in the list. */
2839
2840 static void
2841 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2842 unsigned ix)
2843 {
2844 tree b;
2845 tree overrider;
2846 tree delta;
2847 tree virtual_base;
2848 tree first_defn;
2849 tree overrider_fn, overrider_target;
2850 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2851 tree over_return, base_return;
2852 bool lost = false;
2853
2854 /* Find the nearest primary base (possibly binfo itself) which defines
2855 this function; this is the class the caller will convert to when
2856 calling FN through BINFO. */
2857 for (b = binfo; ; b = get_primary_binfo (b))
2858 {
2859 gcc_assert (b);
2860 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2861 break;
2862
2863 /* The nearest definition is from a lost primary. */
2864 if (BINFO_LOST_PRIMARY_P (b))
2865 lost = true;
2866 }
2867 first_defn = b;
2868
2869 /* Find the final overrider. */
2870 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2871 if (overrider == error_mark_node)
2872 {
2873 error ("no unique final overrider for %qD in %qT", target_fn, t);
2874 return;
2875 }
2876 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2877
2878 /* Check for adjusting covariant return types. */
2879 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2880 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2881
2882 if (INDIRECT_TYPE_P (over_return)
2883 && TREE_CODE (over_return) == TREE_CODE (base_return)
2884 && CLASS_TYPE_P (TREE_TYPE (over_return))
2885 && CLASS_TYPE_P (TREE_TYPE (base_return))
2886 /* If the overrider is invalid, don't even try. */
2887 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2888 {
2889 /* If FN is a covariant thunk, we must figure out the adjustment
2890 to the final base FN was converting to. As OVERRIDER_TARGET might
2891 also be converting to the return type of FN, we have to
2892 combine the two conversions here. */
2893 tree fixed_offset, virtual_offset;
2894
2895 over_return = TREE_TYPE (over_return);
2896 base_return = TREE_TYPE (base_return);
2897
2898 if (DECL_THUNK_P (fn))
2899 {
2900 gcc_assert (DECL_RESULT_THUNK_P (fn));
2901 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2902 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2903 }
2904 else
2905 fixed_offset = virtual_offset = NULL_TREE;
2906
2907 if (virtual_offset)
2908 /* Find the equivalent binfo within the return type of the
2909 overriding function. We will want the vbase offset from
2910 there. */
2911 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2912 over_return);
2913 else if (!same_type_ignoring_top_level_qualifiers_p
2914 (over_return, base_return))
2915 {
2916 /* There was no existing virtual thunk (which takes
2917 precedence). So find the binfo of the base function's
2918 return type within the overriding function's return type.
2919 Fortunately we know the covariancy is valid (it
2920 has already been checked), so we can just iterate along
2921 the binfos, which have been chained in inheritance graph
2922 order. Of course it is lame that we have to repeat the
2923 search here anyway -- we should really be caching pieces
2924 of the vtable and avoiding this repeated work. */
2925 tree thunk_binfo = NULL_TREE;
2926 tree base_binfo = TYPE_BINFO (base_return);
2927
2928 /* Find the base binfo within the overriding function's
2929 return type. We will always find a thunk_binfo, except
2930 when the covariancy is invalid (which we will have
2931 already diagnosed). */
2932 if (base_binfo)
2933 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2934 thunk_binfo = TREE_CHAIN (thunk_binfo))
2935 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2936 BINFO_TYPE (base_binfo)))
2937 break;
2938 gcc_assert (thunk_binfo || errorcount);
2939
2940 /* See if virtual inheritance is involved. */
2941 for (virtual_offset = thunk_binfo;
2942 virtual_offset;
2943 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2944 if (BINFO_VIRTUAL_P (virtual_offset))
2945 break;
2946
2947 if (virtual_offset
2948 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2949 {
2950 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2951
2952 if (virtual_offset)
2953 {
2954 /* We convert via virtual base. Adjust the fixed
2955 offset to be from there. */
2956 offset =
2957 size_diffop (offset,
2958 fold_convert (ssizetype,
2959 BINFO_OFFSET (virtual_offset)));
2960 }
2961 if (fixed_offset)
2962 /* There was an existing fixed offset, this must be
2963 from the base just converted to, and the base the
2964 FN was thunking to. */
2965 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2966 else
2967 fixed_offset = offset;
2968 }
2969 }
2970
2971 if (fixed_offset || virtual_offset)
2972 /* Replace the overriding function with a covariant thunk. We
2973 will emit the overriding function in its own slot as
2974 well. */
2975 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2976 fixed_offset, virtual_offset);
2977 }
2978 else
2979 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2980 !DECL_THUNK_P (fn));
2981
2982 /* If we need a covariant thunk, then we may need to adjust first_defn.
2983 The ABI specifies that the thunks emitted with a function are
2984 determined by which bases the function overrides, so we need to be
2985 sure that we're using a thunk for some overridden base; even if we
2986 know that the necessary this adjustment is zero, there may not be an
2987 appropriate zero-this-adjustment thunk for us to use since thunks for
2988 overriding virtual bases always use the vcall offset.
2989
2990 Furthermore, just choosing any base that overrides this function isn't
2991 quite right, as this slot won't be used for calls through a type that
2992 puts a covariant thunk here. Calling the function through such a type
2993 will use a different slot, and that slot is the one that determines
2994 the thunk emitted for that base.
2995
2996 So, keep looking until we find the base that we're really overriding
2997 in this slot: the nearest primary base that doesn't use a covariant
2998 thunk in this slot. */
2999 if (overrider_target != overrider_fn)
3000 {
3001 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
3002 /* We already know that the overrider needs a covariant thunk. */
3003 b = get_primary_binfo (b);
3004 for (; ; b = get_primary_binfo (b))
3005 {
3006 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
3007 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
3008 if (!DECL_THUNK_P (TREE_VALUE (bv)))
3009 break;
3010 if (BINFO_LOST_PRIMARY_P (b))
3011 lost = true;
3012 }
3013 first_defn = b;
3014 }
3015
3016 /* Assume that we will produce a thunk that convert all the way to
3017 the final overrider, and not to an intermediate virtual base. */
3018 virtual_base = NULL_TREE;
3019
3020 /* See if we can convert to an intermediate virtual base first, and then
3021 use the vcall offset located there to finish the conversion. */
3022 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
3023 {
3024 /* If we find the final overrider, then we can stop
3025 walking. */
3026 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
3027 BINFO_TYPE (TREE_VALUE (overrider))))
3028 break;
3029
3030 /* If we find a virtual base, and we haven't yet found the
3031 overrider, then there is a virtual base between the
3032 declaring base (first_defn) and the final overrider. */
3033 if (BINFO_VIRTUAL_P (b))
3034 {
3035 virtual_base = b;
3036 break;
3037 }
3038 }
3039
3040 /* Compute the constant adjustment to the `this' pointer. The
3041 `this' pointer, when this function is called, will point at BINFO
3042 (or one of its primary bases, which are at the same offset). */
3043 if (virtual_base)
3044 /* The `this' pointer needs to be adjusted from the declaration to
3045 the nearest virtual base. */
3046 delta = size_diffop_loc (input_location,
3047 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
3048 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
3049 else if (lost)
3050 /* If the nearest definition is in a lost primary, we don't need an
3051 entry in our vtable. Except possibly in a constructor vtable,
3052 if we happen to get our primary back. In that case, the offset
3053 will be zero, as it will be a primary base. */
3054 delta = size_zero_node;
3055 else
3056 /* The `this' pointer needs to be adjusted from pointing to
3057 BINFO to pointing at the base where the final overrider
3058 appears. */
3059 delta = size_diffop_loc (input_location,
3060 fold_convert (ssizetype,
3061 BINFO_OFFSET (TREE_VALUE (overrider))),
3062 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
3063
3064 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
3065
3066 if (virtual_base)
3067 BV_VCALL_INDEX (*virtuals)
3068 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
3069 else
3070 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
3071
3072 BV_LOST_PRIMARY (*virtuals) = lost;
3073 }
3074
3075 /* Called from modify_all_vtables via dfs_walk. */
3076
3077 static tree
3078 dfs_modify_vtables (tree binfo, void* data)
3079 {
3080 tree t = (tree) data;
3081 tree virtuals;
3082 tree old_virtuals;
3083 unsigned ix;
3084
3085 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3086 /* A base without a vtable needs no modification, and its bases
3087 are uninteresting. */
3088 return dfs_skip_bases;
3089
3090 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
3091 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3092 /* Don't do the primary vtable, if it's new. */
3093 return NULL_TREE;
3094
3095 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
3096 /* There's no need to modify the vtable for a non-virtual primary
3097 base; we're not going to use that vtable anyhow. We do still
3098 need to do this for virtual primary bases, as they could become
3099 non-primary in a construction vtable. */
3100 return NULL_TREE;
3101
3102 make_new_vtable (t, binfo);
3103
3104 /* Now, go through each of the virtual functions in the virtual
3105 function table for BINFO. Find the final overrider, and update
3106 the BINFO_VIRTUALS list appropriately. */
3107 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
3108 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
3109 virtuals;
3110 ix++, virtuals = TREE_CHAIN (virtuals),
3111 old_virtuals = TREE_CHAIN (old_virtuals))
3112 update_vtable_entry_for_fn (t,
3113 binfo,
3114 BV_FN (old_virtuals),
3115 &virtuals, ix);
3116
3117 return NULL_TREE;
3118 }
3119
3120 /* Update all of the primary and secondary vtables for T. Create new
3121 vtables as required, and initialize their RTTI information. Each
3122 of the functions in VIRTUALS is declared in T and may override a
3123 virtual function from a base class; find and modify the appropriate
3124 entries to point to the overriding functions. Returns a list, in
3125 declaration order, of the virtual functions that are declared in T,
3126 but do not appear in the primary base class vtable, and which
3127 should therefore be appended to the end of the vtable for T. */
3128
3129 static tree
3130 modify_all_vtables (tree t, tree virtuals)
3131 {
3132 tree binfo = TYPE_BINFO (t);
3133 tree *fnsp;
3134
3135 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
3136 if (TYPE_CONTAINS_VPTR_P (t))
3137 get_vtable_decl (t, false);
3138
3139 /* Update all of the vtables. */
3140 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
3141
3142 /* Add virtual functions not already in our primary vtable. These
3143 will be both those introduced by this class, and those overridden
3144 from secondary bases. It does not include virtuals merely
3145 inherited from secondary bases. */
3146 for (fnsp = &virtuals; *fnsp; )
3147 {
3148 tree fn = TREE_VALUE (*fnsp);
3149
3150 if (!value_member (fn, BINFO_VIRTUALS (binfo))
3151 || DECL_VINDEX (fn) == error_mark_node)
3152 {
3153 /* We don't need to adjust the `this' pointer when
3154 calling this function. */
3155 BV_DELTA (*fnsp) = integer_zero_node;
3156 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
3157
3158 /* This is a function not already in our vtable. Keep it. */
3159 fnsp = &TREE_CHAIN (*fnsp);
3160 }
3161 else
3162 /* We've already got an entry for this function. Skip it. */
3163 *fnsp = TREE_CHAIN (*fnsp);
3164 }
3165
3166 return virtuals;
3167 }
3168
3169 /* Get the base virtual function declarations in T that have the
3170 indicated NAME. */
3171
3172 static void
3173 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
3174 {
3175 bool found_decls = false;
3176
3177 /* Find virtual functions in T with the indicated NAME. */
3178 for (tree method : ovl_range (get_class_binding (t, name)))
3179 {
3180 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
3181 {
3182 base_fndecls->safe_push (method);
3183 found_decls = true;
3184 }
3185 }
3186
3187 if (found_decls)
3188 return;
3189
3190 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3191 for (int i = 0; i < n_baseclasses; i++)
3192 {
3193 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
3194 get_basefndecls (name, basetype, base_fndecls);
3195 }
3196 }
3197
3198 /* If this method overrides a virtual method from a base, then mark
3199 this member function as being virtual as well. Do 'final' and
3200 'override' checks too. */
3201
3202 void
3203 check_for_override (tree decl, tree ctype)
3204 {
3205 if (TREE_CODE (decl) == TEMPLATE_DECL)
3206 /* In [temp.mem] we have:
3207
3208 A specialization of a member function template does not
3209 override a virtual function from a base class. */
3210 return;
3211
3212 /* IDENTIFIER_VIRTUAL_P indicates whether the name has ever been
3213 used for a vfunc. That avoids the expensive look_for_overrides
3214 call that when we know there's nothing to find. As conversion
3215 operators for the same type can have distinct identifiers, we
3216 cannot optimize those in that way. */
3217 if ((IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
3218 || DECL_CONV_FN_P (decl))
3219 && look_for_overrides (ctype, decl)
3220 /* Check staticness after we've checked if we 'override'. */
3221 && !DECL_STATIC_FUNCTION_P (decl))
3222 {
3223 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3224 the error_mark_node so that we know it is an overriding
3225 function. */
3226 DECL_VINDEX (decl) = decl;
3227
3228 if (warn_override
3229 && !DECL_OVERRIDE_P (decl)
3230 && !DECL_FINAL_P (decl)
3231 && !DECL_DESTRUCTOR_P (decl))
3232 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
3233 "%qD can be marked override", decl);
3234 }
3235 else if (DECL_OVERRIDE_P (decl))
3236 error ("%q+#D marked %<override%>, but does not override", decl);
3237
3238 if (DECL_VIRTUAL_P (decl))
3239 {
3240 /* Remember this identifier is virtual name. */
3241 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
3242
3243 if (!DECL_VINDEX (decl))
3244 /* It's a new vfunc. */
3245 DECL_VINDEX (decl) = error_mark_node;
3246
3247 if (DECL_DESTRUCTOR_P (decl))
3248 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3249 }
3250 else if (DECL_FINAL_P (decl))
3251 error ("%q+#D marked %<final%>, but is not virtual", decl);
3252 }
3253
3254 /* Warn about hidden virtual functions that are not overridden in t.
3255 We know that constructors and destructors don't apply. */
3256
3257 static void
3258 warn_hidden (tree t)
3259 {
3260 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
3261 for (unsigned ix = member_vec->length (); ix--;)
3262 {
3263 tree fns = (*member_vec)[ix];
3264
3265 if (!OVL_P (fns))
3266 continue;
3267
3268 tree name = OVL_NAME (fns);
3269 size_t num_fns = 0; /* The number of fndecls in fns. */
3270 auto_vec<tree, 20> base_fndecls;
3271 tree base_binfo;
3272 tree binfo;
3273 unsigned j;
3274 size_t num_overriders = 0;
3275 hash_set<tree> overriden_base_fndecls;
3276 /* base_fndecls that are hidden but not overriden. The "value"
3277 contains the last fndecl we saw that hides the "key". */
3278 hash_map<tree, tree> hidden_base_fndecls;
3279
3280 if (IDENTIFIER_CDTOR_P (name))
3281 continue;
3282
3283 /* Iterate through all of the base classes looking for possibly
3284 hidden functions. */
3285 for (binfo = TYPE_BINFO (t), j = 0;
3286 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3287 {
3288 tree basetype = BINFO_TYPE (base_binfo);
3289 get_basefndecls (name, basetype, &base_fndecls);
3290 }
3291
3292 /* If there are no functions to hide, continue. */
3293 if (base_fndecls.is_empty ())
3294 continue;
3295
3296 /* Find all the base_fndecls that are overridden, as well as those
3297 that are hidden, in T. */
3298 for (tree fndecl : ovl_range (fns))
3299 {
3300 bool template_p = TREE_CODE (fndecl) == TEMPLATE_DECL;
3301 bool fndecl_overrides_p = false;
3302 fndecl = STRIP_TEMPLATE (fndecl);
3303 if (TREE_CODE (fndecl) != FUNCTION_DECL
3304 || fndecl == conv_op_marker)
3305 continue;
3306 num_fns++;
3307 for (size_t k = 0; k < base_fndecls.length (); k++)
3308 {
3309 if (!base_fndecls[k] || !DECL_VINDEX (base_fndecls[k]))
3310 continue;
3311 if (IDENTIFIER_CONV_OP_P (name)
3312 && !same_type_p (DECL_CONV_FN_TYPE (fndecl),
3313 DECL_CONV_FN_TYPE (base_fndecls[k])))
3314 /* If base_fndecl[k] and fndecl are conversion operators
3315 to different types, they're unrelated. */
3316 ;
3317 else if (!template_p /* Template methods don't override. */
3318 && same_signature_p (fndecl, base_fndecls[k]))
3319 {
3320 overriden_base_fndecls.add (base_fndecls[k]);
3321 fndecl_overrides_p = true;
3322 }
3323 else
3324 {
3325 /* fndecl hides base_fndecls[k]. */
3326 hidden_base_fndecls.put (base_fndecls[k], fndecl);
3327 }
3328 }
3329 if (fndecl_overrides_p)
3330 ++num_overriders;
3331 }
3332
3333 if (warn_overloaded_virtual == 1 && num_overriders == num_fns)
3334 /* All the fns override a base virtual. */
3335 continue;
3336
3337 /* Now give a warning for all hidden methods. Note that a method that
3338 is both in hidden_base_fndecls and overriden_base_fndecls is not
3339 hidden. */
3340 for (auto hidden_base_fndecl : hidden_base_fndecls)
3341 {
3342 tree hidden_fndecl = hidden_base_fndecl.first;
3343 if (!hidden_fndecl
3344 || overriden_base_fndecls.contains (hidden_fndecl))
3345 continue;
3346 auto_diagnostic_group d;
3347 if (warning_at (location_of (hidden_fndecl),
3348 OPT_Woverloaded_virtual_,
3349 "%qD was hidden", hidden_fndecl))
3350 {
3351 tree hider = hidden_base_fndecl.second;
3352 inform (location_of (hider), " by %qD", hider);
3353 }
3354 }
3355 }
3356 }
3357
3358 /* Recursive helper for finish_struct_anon. */
3359
3360 static void
3361 finish_struct_anon_r (tree field)
3362 {
3363 for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
3364 {
3365 /* We're generally only interested in entities the user
3366 declared, but we also find nested classes by noticing
3367 the TYPE_DECL that we create implicitly. You're
3368 allowed to put one anonymous union inside another,
3369 though, so we explicitly tolerate that. We use
3370 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3371 we also allow unnamed types used for defining fields. */
3372 if (DECL_ARTIFICIAL (elt)
3373 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3374 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3375 continue;
3376
3377 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3378 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3379
3380 /* Recurse into the anonymous aggregates to correctly handle
3381 access control (c++/24926):
3382
3383 class A {
3384 union {
3385 union {
3386 int i;
3387 };
3388 };
3389 };
3390
3391 int j=A().i; */
3392 if (DECL_NAME (elt) == NULL_TREE
3393 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3394 finish_struct_anon_r (elt);
3395 }
3396 }
3397
3398 /* Fix up any anonymous union/struct members of T. */
3399
3400 static void
3401 finish_struct_anon (tree t)
3402 {
3403 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3404 {
3405 if (TREE_STATIC (field))
3406 continue;
3407 if (TREE_CODE (field) != FIELD_DECL)
3408 continue;
3409
3410 if (DECL_NAME (field) == NULL_TREE
3411 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3412 finish_struct_anon_r (field);
3413 }
3414 }
3415
3416 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3417 will be used later during class template instantiation.
3418 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3419 a non-static member data (FIELD_DECL), a member function
3420 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3421 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3422 When FRIEND_P is nonzero, T is either a friend class
3423 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3424 (FUNCTION_DECL, TEMPLATE_DECL). */
3425
3426 void
3427 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3428 {
3429 if (CLASSTYPE_TEMPLATE_INFO (type)
3430 && TREE_CODE (t) != CONST_DECL)
3431 {
3432 tree purpose = friend_p ? NULL_TREE : type;
3433
3434 CLASSTYPE_DECL_LIST (type)
3435 = tree_cons (purpose, t, CLASSTYPE_DECL_LIST (type));
3436 }
3437 }
3438
3439 /* This function is called from declare_virt_assop_and_dtor via
3440 dfs_walk_all.
3441
3442 DATA is a type that direcly or indirectly inherits the base
3443 represented by BINFO. If BINFO contains a virtual assignment [copy
3444 assignment or move assigment] operator or a virtual constructor,
3445 declare that function in DATA if it hasn't been already declared. */
3446
3447 static tree
3448 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3449 {
3450 tree bv, fn, t = (tree)data;
3451 tree opname = assign_op_identifier;
3452
3453 gcc_assert (t && CLASS_TYPE_P (t));
3454 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3455
3456 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3457 /* A base without a vtable needs no modification, and its bases
3458 are uninteresting. */
3459 return dfs_skip_bases;
3460
3461 if (BINFO_PRIMARY_P (binfo))
3462 /* If this is a primary base, then we have already looked at the
3463 virtual functions of its vtable. */
3464 return NULL_TREE;
3465
3466 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3467 {
3468 fn = BV_FN (bv);
3469
3470 if (DECL_NAME (fn) == opname)
3471 {
3472 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3473 lazily_declare_fn (sfk_copy_assignment, t);
3474 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3475 lazily_declare_fn (sfk_move_assignment, t);
3476 }
3477 else if (DECL_DESTRUCTOR_P (fn)
3478 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3479 lazily_declare_fn (sfk_destructor, t);
3480 }
3481
3482 return NULL_TREE;
3483 }
3484
3485 /* If the class type T has a direct or indirect base that contains a
3486 virtual assignment operator or a virtual destructor, declare that
3487 function in T if it hasn't been already declared. */
3488
3489 static void
3490 declare_virt_assop_and_dtor (tree t)
3491 {
3492 if (!(TYPE_POLYMORPHIC_P (t)
3493 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3494 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3495 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3496 return;
3497
3498 dfs_walk_all (TYPE_BINFO (t),
3499 dfs_declare_virt_assop_and_dtor,
3500 NULL, t);
3501 }
3502
3503 /* Declare the inheriting constructor for class T inherited from base
3504 constructor CTOR with the parameter array PARMS of size NPARMS. */
3505
3506 static void
3507 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3508 {
3509 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3510
3511 /* We don't declare an inheriting ctor that would be a default,
3512 copy or move ctor for derived or base. */
3513 if (nparms == 0)
3514 return;
3515 if (nparms == 1
3516 && TYPE_REF_P (parms[0]))
3517 {
3518 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3519 if (parm == t || parm == DECL_CONTEXT (ctor))
3520 return;
3521 }
3522
3523 tree parmlist = void_list_node;
3524 for (int i = nparms - 1; i >= 0; i--)
3525 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3526 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3527 t, false, ctor, parmlist);
3528
3529 if (add_method (t, fn, false))
3530 {
3531 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3532 TYPE_FIELDS (t) = fn;
3533 }
3534 }
3535
3536 /* Declare all the inheriting constructors for class T inherited from base
3537 constructor CTOR. */
3538
3539 static void
3540 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3541 {
3542 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3543
3544 if (flag_new_inheriting_ctors)
3545 {
3546 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3547 t, /*const*/false, ctor, parms);
3548 add_method (t, ctor, using_decl != NULL_TREE);
3549 return;
3550 }
3551
3552 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3553 int i = 0;
3554 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3555 {
3556 if (TREE_PURPOSE (parms))
3557 one_inheriting_sig (t, ctor, new_parms, i);
3558 new_parms[i++] = TREE_VALUE (parms);
3559 }
3560 one_inheriting_sig (t, ctor, new_parms, i);
3561 if (parms == NULL_TREE)
3562 {
3563 auto_diagnostic_group d;
3564 if (warning (OPT_Winherited_variadic_ctor,
3565 "the ellipsis in %qD is not inherited", ctor))
3566 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3567 }
3568 }
3569
3570 /* Implicitly declare T(). */
3571
3572 static void
3573 add_implicit_default_ctor (tree t)
3574 {
3575 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3576 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3577 if (cxx_dialect >= cxx11)
3578 TYPE_HAS_CONSTEXPR_CTOR (t)
3579 /* Don't force the declaration to get a hard answer; if the
3580 definition would have made the class non-literal, it will still be
3581 non-literal because of the base or member in question, and that
3582 gives a better diagnostic. */
3583 = type_maybe_constexpr_default_constructor (t);
3584 }
3585
3586 /* Create default constructors, assignment operators, and so forth for
3587 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3588 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3589 the class cannot have a default constructor, copy constructor
3590 taking a const reference argument, or an assignment operator taking
3591 a const reference, respectively. */
3592
3593 static void
3594 add_implicitly_declared_members (tree t, tree* access_decls,
3595 int cant_have_const_cctor,
3596 int cant_have_const_assignment)
3597 {
3598 /* Destructor. */
3599 if (!CLASSTYPE_DESTRUCTOR (t))
3600 /* In general, we create destructors lazily. */
3601 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3602
3603 bool move_ok = false;
3604 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3605 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3606 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3607 move_ok = true;
3608
3609 /* [class.ctor]
3610
3611 If there is no user-declared constructor for a class, a default
3612 constructor is implicitly declared. */
3613 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3614 add_implicit_default_ctor (t);
3615
3616 /* [class.ctor]
3617
3618 If a class definition does not explicitly declare a copy
3619 constructor, one is declared implicitly. */
3620 if (! TYPE_HAS_COPY_CTOR (t))
3621 {
3622 TYPE_HAS_COPY_CTOR (t) = 1;
3623 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3624 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3625 if (move_ok)
3626 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3627 }
3628
3629 /* If there is no assignment operator, one will be created if and
3630 when it is needed. For now, just record whether or not the type
3631 of the parameter to the assignment operator will be a const or
3632 non-const reference. */
3633 if (!TYPE_HAS_COPY_ASSIGN (t))
3634 {
3635 TYPE_HAS_COPY_ASSIGN (t) = 1;
3636 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3637 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3638 if (move_ok && !LAMBDA_TYPE_P (t))
3639 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3640 }
3641
3642 /* We can't be lazy about declaring functions that might override
3643 a virtual function from a base class. */
3644 declare_virt_assop_and_dtor (t);
3645
3646 /* If the class definition does not explicitly declare an == operator
3647 function, but declares a defaulted three-way comparison operator function,
3648 an == operator function is declared implicitly. */
3649 if (!classtype_has_op (t, EQ_EXPR))
3650 if (tree space = classtype_has_defaulted_op (t, SPACESHIP_EXPR))
3651 {
3652 tree eq = implicitly_declare_fn (sfk_comparison, t, false, space,
3653 NULL_TREE);
3654 bool is_friend = DECL_CONTEXT (space) != t;
3655 if (is_friend)
3656 do_friend (NULL_TREE, DECL_NAME (eq), eq,
3657 NO_SPECIAL, true);
3658 else
3659 {
3660 add_method (t, eq, false);
3661 DECL_CHAIN (eq) = TYPE_FIELDS (t);
3662 TYPE_FIELDS (t) = eq;
3663 }
3664 maybe_add_class_template_decl_list (t, eq, is_friend);
3665 }
3666
3667 while (*access_decls)
3668 {
3669 tree using_decl = TREE_VALUE (*access_decls);
3670 tree decl = USING_DECL_DECLS (using_decl);
3671 if (DECL_NAME (using_decl) == ctor_identifier)
3672 {
3673 /* declare, then remove the decl */
3674 tree ctor_list = decl;
3675 location_t loc = input_location;
3676 input_location = DECL_SOURCE_LOCATION (using_decl);
3677 for (tree fn : ovl_range (ctor_list))
3678 {
3679 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t) && default_ctor_p (fn))
3680 /* CWG2799: Inheriting a default constructor gives us a default
3681 constructor, not just an inherited constructor. */
3682 add_implicit_default_ctor (t);
3683 one_inherited_ctor (fn, t, using_decl);
3684 }
3685 *access_decls = TREE_CHAIN (*access_decls);
3686 input_location = loc;
3687 }
3688 else
3689 access_decls = &TREE_CHAIN (*access_decls);
3690 }
3691 }
3692
3693 /* Cache of enum_min_precision values. */
3694 static GTY((deletable)) hash_map<tree, int> *enum_to_min_precision;
3695
3696 /* Return the minimum precision of a bit-field needed to store all
3697 enumerators of ENUMERAL_TYPE TYPE. */
3698
3699 static int
3700 enum_min_precision (tree type)
3701 {
3702 type = TYPE_MAIN_VARIANT (type);
3703 /* For unscoped enums without fixed underlying type and without mode
3704 attribute we can just use precision of the underlying type. */
3705 if (UNSCOPED_ENUM_P (type)
3706 && !ENUM_FIXED_UNDERLYING_TYPE_P (type)
3707 && !lookup_attribute ("mode", TYPE_ATTRIBUTES (type)))
3708 return TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3709
3710 if (enum_to_min_precision == NULL)
3711 enum_to_min_precision = hash_map<tree, int>::create_ggc (37);
3712
3713 bool existed;
3714 int &prec = enum_to_min_precision->get_or_insert (type, &existed);
3715 if (existed)
3716 return prec;
3717
3718 tree minnode, maxnode;
3719 if (TYPE_VALUES (type))
3720 {
3721 minnode = maxnode = NULL_TREE;
3722 for (tree values = TYPE_VALUES (type);
3723 values; values = TREE_CHAIN (values))
3724 {
3725 tree decl = TREE_VALUE (values);
3726 tree value = DECL_INITIAL (decl);
3727 if (value == error_mark_node)
3728 value = integer_zero_node;
3729 if (!minnode)
3730 minnode = maxnode = value;
3731 else if (tree_int_cst_lt (maxnode, value))
3732 maxnode = value;
3733 else if (tree_int_cst_lt (value, minnode))
3734 minnode = value;
3735 }
3736 }
3737 else
3738 minnode = maxnode = integer_zero_node;
3739
3740 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
3741 int lowprec = tree_int_cst_min_precision (minnode, sgn);
3742 int highprec = tree_int_cst_min_precision (maxnode, sgn);
3743 prec = MAX (lowprec, highprec);
3744 return prec;
3745 }
3746
3747 /* FIELD is a bit-field. We are finishing the processing for its
3748 enclosing type. Issue any appropriate messages and set appropriate
3749 flags. Returns false if an error has been diagnosed. */
3750
3751 static bool
3752 check_bitfield_decl (tree field)
3753 {
3754 tree type = TREE_TYPE (field);
3755 tree w;
3756
3757 /* Extract the declared width of the bitfield, which has been
3758 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3759 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3760 gcc_assert (w != NULL_TREE);
3761 /* Remove the bit-field width indicator so that the rest of the
3762 compiler does not treat that value as a qualifier. */
3763 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3764
3765 /* Detect invalid bit-field type. */
3766 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3767 {
3768 error_at (DECL_SOURCE_LOCATION (field),
3769 "bit-field %q#D with non-integral type %qT", field, type);
3770 w = error_mark_node;
3771 }
3772 else
3773 {
3774 location_t loc = input_location;
3775 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3776 STRIP_NOPS (w);
3777
3778 /* detect invalid field size. */
3779 input_location = DECL_SOURCE_LOCATION (field);
3780 w = cxx_constant_value (w);
3781 input_location = loc;
3782
3783 if (TREE_CODE (w) != INTEGER_CST)
3784 {
3785 error ("bit-field %q+D width not an integer constant", field);
3786 w = error_mark_node;
3787 }
3788 else if (tree_int_cst_sgn (w) < 0)
3789 {
3790 error ("negative width in bit-field %q+D", field);
3791 w = error_mark_node;
3792 }
3793 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3794 {
3795 error ("zero width for bit-field %q+D", field);
3796 w = error_mark_node;
3797 }
3798 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3799 && TREE_CODE (type) != BOOLEAN_TYPE
3800 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3801 || ((TREE_CODE (type) == ENUMERAL_TYPE
3802 || TREE_CODE (type) == BOOLEAN_TYPE)
3803 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3804 warning_at (DECL_SOURCE_LOCATION (field), 0,
3805 "width of %qD exceeds its type", field);
3806 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3807 {
3808 int prec = enum_min_precision (type);
3809 if (compare_tree_int (w, prec) < 0)
3810 warning_at (DECL_SOURCE_LOCATION (field), 0,
3811 "%qD is too small to hold all values of %q#T",
3812 field, type);
3813 }
3814 }
3815
3816 if (w != error_mark_node)
3817 {
3818 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3819 DECL_BIT_FIELD (field) = 1;
3820 return true;
3821 }
3822 else
3823 {
3824 /* Non-bit-fields are aligned for their type. */
3825 DECL_BIT_FIELD (field) = 0;
3826 CLEAR_DECL_C_BIT_FIELD (field);
3827 return false;
3828 }
3829 }
3830
3831 /* FIELD is a non bit-field. We are finishing the processing for its
3832 enclosing type T. Issue any appropriate messages and set appropriate
3833 flags. */
3834
3835 static bool
3836 check_field_decl (tree field,
3837 tree t,
3838 int* cant_have_const_ctor,
3839 int* no_const_asn_ref)
3840 {
3841 tree type = strip_array_types (TREE_TYPE (field));
3842 bool any_default_members = false;
3843
3844 /* In C++98 an anonymous union cannot contain any fields which would change
3845 the settings of CANT_HAVE_CONST_CTOR and friends. */
3846 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3847 ;
3848 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3849 structs. So, we recurse through their fields here. */
3850 else if (ANON_AGGR_TYPE_P (type))
3851 {
3852 for (tree fields = TYPE_FIELDS (type); fields;
3853 fields = DECL_CHAIN (fields))
3854 if (TREE_CODE (fields) == FIELD_DECL)
3855 any_default_members |= check_field_decl (fields, t,
3856 cant_have_const_ctor,
3857 no_const_asn_ref);
3858 }
3859 /* Check members with class type for constructors, destructors,
3860 etc. */
3861 else if (CLASS_TYPE_P (type))
3862 {
3863 /* Never let anything with uninheritable virtuals
3864 make it through without complaint. */
3865 abstract_virtuals_error (field, type);
3866
3867 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3868 {
3869 static bool warned;
3870 auto_diagnostic_group d;
3871 int oldcount = errorcount;
3872 if (TYPE_NEEDS_CONSTRUCTING (type))
3873 error ("member %q+#D with constructor not allowed in union",
3874 field);
3875 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3876 error ("member %q+#D with destructor not allowed in union", field);
3877 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3878 error ("member %q+#D with copy assignment operator not allowed in union",
3879 field);
3880 if (!warned && errorcount > oldcount)
3881 {
3882 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3883 "only available with %<-std=c++11%> or %<-std=gnu++11%>");
3884 warned = true;
3885 }
3886 }
3887 else
3888 {
3889 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3890 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3891 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3892 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3893 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3894 || !TYPE_HAS_COPY_ASSIGN (type));
3895 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3896 || !TYPE_HAS_COPY_CTOR (type));
3897 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3898 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3899 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3900 || TYPE_HAS_COMPLEX_DFLT (type));
3901 }
3902
3903 if (TYPE_HAS_COPY_CTOR (type)
3904 && !TYPE_HAS_CONST_COPY_CTOR (type))
3905 *cant_have_const_ctor = 1;
3906
3907 if (TYPE_HAS_COPY_ASSIGN (type)
3908 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3909 *no_const_asn_ref = 1;
3910 }
3911
3912 check_abi_tags (t, field);
3913
3914 if (DECL_INITIAL (field) != NULL_TREE)
3915 /* `build_class_init_list' does not recognize
3916 non-FIELD_DECLs. */
3917 any_default_members = true;
3918
3919 return any_default_members;
3920 }
3921
3922 /* Check the data members (both static and non-static), class-scoped
3923 typedefs, etc., appearing in the declaration of T. Issue
3924 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3925 declaration order) of access declarations; each TREE_VALUE in this
3926 list is a USING_DECL.
3927
3928 In addition, set the following flags:
3929
3930 EMPTY_P
3931 The class is empty, i.e., contains no non-static data members.
3932
3933 CANT_HAVE_CONST_CTOR_P
3934 This class cannot have an implicitly generated copy constructor
3935 taking a const reference.
3936
3937 CANT_HAVE_CONST_ASN_REF
3938 This class cannot have an implicitly generated assignment
3939 operator taking a const reference.
3940
3941 All of these flags should be initialized before calling this
3942 function. */
3943
3944 static void
3945 check_field_decls (tree t, tree *access_decls,
3946 int *cant_have_const_ctor_p,
3947 int *no_const_asn_ref_p)
3948 {
3949 int cant_pack = 0;
3950
3951 /* Assume there are no access declarations. */
3952 *access_decls = NULL_TREE;
3953 /* Effective C has things to say about classes with pointer members. */
3954 tree pointer_member = NULL_TREE;
3955 /* Default initialized members affect the whole class. */
3956 tree default_init_member = NULL_TREE;
3957 /* Lack of any non-static data member of non-volatile literal
3958 type affects a union. */
3959 bool found_nv_literal_p = false;
3960 /* Standard layout requires all FIELDS have same access. */
3961 int field_access = -1;
3962
3963 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3964 {
3965 tree type = TREE_TYPE (field);
3966
3967 switch (TREE_CODE (field))
3968 {
3969 default:
3970 gcc_unreachable ();
3971
3972 case USING_DECL:
3973 /* Save the access declarations for our caller. */
3974 *access_decls = tree_cons (NULL_TREE, field, *access_decls);
3975 break;
3976
3977 case TYPE_DECL:
3978 case TEMPLATE_DECL:
3979 break;
3980
3981 case FUNCTION_DECL:
3982 /* FIXME: We should fold in the checking from check_methods. */
3983 break;
3984
3985 case CONST_DECL:
3986 DECL_NONLOCAL (field) = 1;
3987 break;
3988
3989 case VAR_DECL:
3990 if (TREE_CODE (t) == UNION_TYPE
3991 && cxx_dialect < cxx11)
3992 {
3993 /* [class.union]
3994
3995 (C++98) If a union contains a static data member,
3996 ... the program is ill-formed. */
3997 if (cxx_dialect < cxx11)
3998 error ("in C++98 %q+D may not be static because it is "
3999 "a member of a union", field);
4000 }
4001 goto data_member;
4002
4003 case FIELD_DECL:
4004 if (TREE_CODE (t) == UNION_TYPE)
4005 {
4006 /* [class.union]
4007
4008 If a union contains ... or a [non-static data] member
4009 of reference type, the program is ill-formed. */
4010 if (TYPE_REF_P (type))
4011 error ("non-static data member %q+D in a union may not "
4012 "have reference type %qT", field, type);
4013 }
4014
4015 data_member:
4016 /* Common VAR_DECL & FIELD_DECL processing. */
4017 DECL_CONTEXT (field) = t;
4018 DECL_NONLOCAL (field) = 1;
4019
4020 /* Template instantiation can cause this. Perhaps this
4021 should be a specific instantiation check? */
4022 if (TREE_CODE (type) == FUNCTION_TYPE)
4023 {
4024 error ("data member %q+D invalidly declared function type", field);
4025 type = build_pointer_type (type);
4026 TREE_TYPE (field) = type;
4027 }
4028 else if (TREE_CODE (type) == METHOD_TYPE)
4029 {
4030 error ("data member %q+D invalidly declared method type", field);
4031 type = build_pointer_type (type);
4032 TREE_TYPE (field) = type;
4033 }
4034
4035 break;
4036 }
4037
4038 if (TREE_CODE (field) != FIELD_DECL)
4039 continue;
4040
4041 if (type == error_mark_node)
4042 continue;
4043
4044 /* If it is not a union and at least one non-static data member is
4045 non-literal, the whole class becomes non-literal. Per Core/1453,
4046 volatile non-static data members and base classes are also not allowed.
4047 If it is a union, we might set CLASSTYPE_LITERAL_P after we've seen all
4048 members.
4049 Note: if the type is incomplete we will complain later on. */
4050 if (COMPLETE_TYPE_P (type))
4051 {
4052 if (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type))
4053 CLASSTYPE_LITERAL_P (t) = false;
4054 else
4055 found_nv_literal_p = true;
4056 }
4057
4058 int this_field_access = (TREE_PROTECTED (field) ? 1
4059 : TREE_PRIVATE (field) ? 2 : 0);
4060 if (field_access != this_field_access)
4061 {
4062 /* A standard-layout class is a class that:
4063
4064 ... has the same access control (Clause 11) for all
4065 non-static data members, */
4066 if (field_access < 0)
4067 field_access = this_field_access;
4068 else
4069 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4070
4071 /* Aggregates must be public. */
4072 if (this_field_access)
4073 CLASSTYPE_NON_AGGREGATE (t) = 1;
4074 }
4075
4076 /* If this is of reference type, check if it needs an init. */
4077 if (TYPE_REF_P (type))
4078 {
4079 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4080 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4081 if (DECL_INITIAL (field) == NULL_TREE)
4082 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4083 if (cxx_dialect < cxx11)
4084 {
4085 /* ARM $12.6.2: [A member initializer list] (or, for an
4086 aggregate, initialization by a brace-enclosed list) is the
4087 only way to initialize non-static const and reference
4088 members. */
4089 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4090 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4091 }
4092 }
4093
4094 type = strip_array_types (type);
4095
4096 if (TYPE_PACKED (t))
4097 {
4098 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
4099 {
4100 warning_at (DECL_SOURCE_LOCATION (field), 0,
4101 "ignoring packed attribute because of"
4102 " unpacked non-POD field %q#D", field);
4103 cant_pack = 1;
4104 }
4105 else if (DECL_C_BIT_FIELD (field)
4106 || TYPE_ALIGN (TREE_TYPE (field)) > BITS_PER_UNIT)
4107 DECL_PACKED (field) = 1;
4108 }
4109
4110 if (DECL_C_BIT_FIELD (field)
4111 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (field)))
4112 /* We don't treat zero-width bitfields as making a class
4113 non-empty. */
4114 ;
4115 else if (field_poverlapping_p (field)
4116 && is_empty_class (TREE_TYPE (field)))
4117 /* Empty data members also don't make a class non-empty. */
4118 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4119 else
4120 {
4121 /* The class is non-empty. */
4122 CLASSTYPE_EMPTY_P (t) = 0;
4123 /* The class is not even nearly empty. */
4124 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4125 /* If one of the data members contains an empty class, so
4126 does T. */
4127 if (CLASS_TYPE_P (type)
4128 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4129 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4130 }
4131
4132 /* This is used by -Weffc++ (see below). Warn only for pointers
4133 to members which might hold dynamic memory. So do not warn
4134 for pointers to functions or pointers to members. */
4135 if (TYPE_PTR_P (type)
4136 && !TYPE_PTRFN_P (type))
4137 pointer_member = field;
4138
4139 if (CLASS_TYPE_P (type))
4140 {
4141 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
4142 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
4143 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
4144 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4145 }
4146
4147 if (DECL_MUTABLE_P (field) || TYPE_HAS_MUTABLE_P (type))
4148 CLASSTYPE_HAS_MUTABLE (t) = 1;
4149
4150 if (DECL_MUTABLE_P (field))
4151 {
4152 if (TYPE_REF_P (type))
4153 error ("member %q+D cannot be declared as a %<mutable%> "
4154 "reference", field);
4155 else if (CP_TYPE_CONST_P (type))
4156 error ("member %q+D cannot be declared both %<const%> "
4157 "and %<mutable%>", field);
4158 }
4159
4160 if (! layout_pod_type_p (type))
4161 /* DR 148 now allows pointers to members (which are POD themselves),
4162 to be allowed in POD structs. */
4163 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4164
4165 if (field_poverlapping_p (field))
4166 /* A potentially-overlapping non-static data member makes the class
4167 non-layout-POD. */
4168 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
4169
4170 if (!std_layout_type_p (type))
4171 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
4172
4173 if (! zero_init_p (type))
4174 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
4175
4176 /* We set DECL_C_BIT_FIELD in grokbitfield.
4177 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4178 if (DECL_C_BIT_FIELD (field))
4179 check_bitfield_decl (field);
4180
4181 if (check_field_decl (field, t,
4182 cant_have_const_ctor_p, no_const_asn_ref_p))
4183 {
4184 if (default_init_member
4185 && TREE_CODE (t) == UNION_TYPE)
4186 {
4187 auto_diagnostic_group d;
4188 error ("multiple fields in union %qT initialized", t);
4189 inform (DECL_SOURCE_LOCATION (default_init_member),
4190 "initialized member %q+D declared here",
4191 default_init_member);
4192 }
4193 default_init_member = field;
4194 }
4195
4196 /* Now that we've removed bit-field widths from DECL_INITIAL,
4197 anything left in DECL_INITIAL is an NSDMI that makes the class
4198 non-aggregate in C++11, and non-layout-POD always. */
4199 if (DECL_INITIAL (field))
4200 {
4201 if (cxx_dialect < cxx14)
4202 CLASSTYPE_NON_AGGREGATE (t) = true;
4203 else
4204 CLASSTYPE_NON_POD_AGGREGATE (t) = true;
4205 }
4206
4207 if (CP_TYPE_CONST_P (type))
4208 {
4209 /* If any field is const, the structure type is pseudo-const. */
4210 C_TYPE_FIELDS_READONLY (t) = 1;
4211 if (DECL_INITIAL (field) == NULL_TREE)
4212 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
4213 if (cxx_dialect < cxx11)
4214 {
4215 /* ARM $12.6.2: [A member initializer list] (or, for an
4216 aggregate, initialization by a brace-enclosed list) is the
4217 only way to initialize non-static const and reference
4218 members. */
4219 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4220 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
4221 }
4222 }
4223 /* A field that is pseudo-const makes the structure likewise. */
4224 else if (CLASS_TYPE_P (type))
4225 {
4226 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4227 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
4228 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4229 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
4230 }
4231
4232 /* Core issue 80: A non-static data member is required to have a
4233 different name from the class iff the class has a
4234 user-declared constructor. */
4235 if (constructor_name_p (DECL_NAME (field), t)
4236 && TYPE_HAS_USER_CONSTRUCTOR (t))
4237 permerror (DECL_SOURCE_LOCATION (field),
4238 "field %q#D with same name as class", field);
4239 }
4240
4241 /* Per CWG 2096, a type is a literal type if it is a union, and at least
4242 one of its non-static data members is of non-volatile literal type. */
4243 if (TREE_CODE (t) == UNION_TYPE && found_nv_literal_p)
4244 CLASSTYPE_LITERAL_P (t) = true;
4245
4246 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
4247 it should also define a copy constructor and an assignment operator to
4248 implement the correct copy semantic (deep vs shallow, etc.). As it is
4249 not feasible to check whether the constructors do allocate dynamic memory
4250 and store it within members, we approximate the warning like this:
4251
4252 -- Warn only if there are members which are pointers
4253 -- Warn only if there is a non-trivial constructor (otherwise,
4254 there cannot be memory allocated).
4255 -- Warn only if there is a non-trivial destructor. We assume that the
4256 user at least implemented the cleanup correctly, and a destructor
4257 is needed to free dynamic memory.
4258
4259 This seems enough for practical purposes. */
4260 if (warn_ecpp
4261 && pointer_member
4262 && TYPE_HAS_USER_CONSTRUCTOR (t)
4263 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
4264 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
4265 {
4266 auto_diagnostic_group d;
4267 if (warning (OPT_Weffc__, "%q#T has pointer data members", t))
4268 {
4269 if (! TYPE_HAS_COPY_CTOR (t))
4270 {
4271 warning (OPT_Weffc__,
4272 " but does not declare %<%T(const %T&)%>", t, t);
4273 if (!TYPE_HAS_COPY_ASSIGN (t))
4274 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
4275 }
4276 else if (! TYPE_HAS_COPY_ASSIGN (t))
4277 warning (OPT_Weffc__,
4278 " but does not declare %<operator=(const %T&)%>", t);
4279 inform (DECL_SOURCE_LOCATION (pointer_member),
4280 "pointer member %q+D declared here", pointer_member);
4281 }
4282 }
4283
4284 /* Non-static data member initializers make the default constructor
4285 non-trivial. */
4286 if (default_init_member)
4287 {
4288 TYPE_NEEDS_CONSTRUCTING (t) = true;
4289 TYPE_HAS_COMPLEX_DFLT (t) = true;
4290 }
4291
4292 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4293 if (cant_pack)
4294 TYPE_PACKED (t) = 0;
4295
4296 /* Check anonymous struct/anonymous union fields. */
4297 finish_struct_anon (t);
4298
4299 /* We've built up the list of access declarations in reverse order.
4300 Fix that now. */
4301 *access_decls = nreverse (*access_decls);
4302 }
4303
4304 /* If TYPE is an empty class type, records its OFFSET in the table of
4305 OFFSETS. */
4306
4307 static int
4308 record_subobject_offset (tree type, tree offset, splay_tree offsets)
4309 {
4310 splay_tree_node n;
4311
4312 if (!is_empty_class (type))
4313 return 0;
4314
4315 /* Record the location of this empty object in OFFSETS. */
4316 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4317 if (!n)
4318 n = splay_tree_insert (offsets,
4319 (splay_tree_key) offset,
4320 (splay_tree_value) NULL_TREE);
4321 n->value = ((splay_tree_value)
4322 tree_cons (NULL_TREE,
4323 type,
4324 (tree) n->value));
4325
4326 return 0;
4327 }
4328
4329 /* Returns nonzero if TYPE is an empty class type and there is
4330 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4331
4332 static int
4333 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4334 {
4335 splay_tree_node n;
4336 tree t;
4337
4338 if (!is_empty_class (type))
4339 return 0;
4340
4341 /* Record the location of this empty object in OFFSETS. */
4342 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4343 if (!n)
4344 return 0;
4345
4346 enum { ignore, fast, slow, warn }
4347 cv_check = (abi_version_crosses (19) ? slow
4348 : abi_version_at_least (19) ? fast
4349 : ignore);
4350 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4351 {
4352 tree elt = TREE_VALUE (t);
4353
4354 if (same_type_p (elt, type))
4355 return 1;
4356
4357 if (cv_check != ignore
4358 && similar_type_p (elt, type))
4359 {
4360 if (cv_check == fast)
4361 return 1;
4362 cv_check = warn;
4363 }
4364 }
4365
4366 if (cv_check == warn)
4367 {
4368 warning (OPT_Wabi, "layout of %qs member of type %qT changes in %qs",
4369 "[[no_unique_address]]", type, "-fabi-version=19");
4370 if (abi_version_at_least (19))
4371 return 1;
4372 }
4373
4374 return 0;
4375 }
4376
4377 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4378 F for every subobject, passing it the type, offset, and table of
4379 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4380 be traversed.
4381
4382 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4383 than MAX_OFFSET will not be walked.
4384
4385 If F returns a nonzero value, the traversal ceases, and that value
4386 is returned. Otherwise, returns zero. */
4387
4388 static int
4389 walk_subobject_offsets (tree type,
4390 subobject_offset_fn f,
4391 tree offset,
4392 splay_tree offsets,
4393 tree max_offset,
4394 int vbases_p)
4395 {
4396 int r = 0;
4397 tree type_binfo = NULL_TREE;
4398
4399 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4400 stop. */
4401 if (max_offset && tree_int_cst_lt (max_offset, offset))
4402 return 0;
4403
4404 if (type == error_mark_node)
4405 return 0;
4406
4407 if (!TYPE_P (type))
4408 {
4409 type_binfo = type;
4410 type = BINFO_TYPE (type);
4411 }
4412
4413 if (CLASS_TYPE_P (type))
4414 {
4415 tree field;
4416 tree binfo;
4417 int i;
4418
4419 /* Avoid recursing into objects that are not interesting. */
4420 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4421 return 0;
4422
4423 /* Record the location of TYPE. */
4424 r = (*f) (type, offset, offsets);
4425 if (r)
4426 return r;
4427
4428 /* Iterate through the direct base classes of TYPE. */
4429 if (!type_binfo)
4430 type_binfo = TYPE_BINFO (type);
4431 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4432 {
4433 tree binfo_offset;
4434
4435 if (BINFO_VIRTUAL_P (binfo))
4436 continue;
4437
4438 tree orig_binfo;
4439 /* We cannot rely on BINFO_OFFSET being set for the base
4440 class yet, but the offsets for direct non-virtual
4441 bases can be calculated by going back to the TYPE. */
4442 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4443 binfo_offset = size_binop (PLUS_EXPR,
4444 offset,
4445 BINFO_OFFSET (orig_binfo));
4446
4447 r = walk_subobject_offsets (binfo,
4448 f,
4449 binfo_offset,
4450 offsets,
4451 max_offset,
4452 /*vbases_p=*/0);
4453 if (r)
4454 return r;
4455 }
4456
4457 if (CLASSTYPE_VBASECLASSES (type))
4458 {
4459 unsigned ix;
4460 vec<tree, va_gc> *vbases;
4461
4462 /* Iterate through the virtual base classes of TYPE. In G++
4463 3.2, we included virtual bases in the direct base class
4464 loop above, which results in incorrect results; the
4465 correct offsets for virtual bases are only known when
4466 working with the most derived type. */
4467 if (vbases_p)
4468 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4469 vec_safe_iterate (vbases, ix, &binfo); ix++)
4470 {
4471 r = walk_subobject_offsets (binfo,
4472 f,
4473 size_binop (PLUS_EXPR,
4474 offset,
4475 BINFO_OFFSET (binfo)),
4476 offsets,
4477 max_offset,
4478 /*vbases_p=*/0);
4479 if (r)
4480 return r;
4481 }
4482 else
4483 {
4484 /* We still have to walk the primary base, if it is
4485 virtual. (If it is non-virtual, then it was walked
4486 above.) */
4487 tree vbase = get_primary_binfo (type_binfo);
4488
4489 if (vbase && BINFO_VIRTUAL_P (vbase)
4490 && BINFO_PRIMARY_P (vbase)
4491 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4492 {
4493 r = (walk_subobject_offsets
4494 (vbase, f, offset,
4495 offsets, max_offset, /*vbases_p=*/0));
4496 if (r)
4497 return r;
4498 }
4499 }
4500 }
4501
4502 /* Iterate through the fields of TYPE. */
4503 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4504 if (TREE_CODE (field) == FIELD_DECL
4505 && TREE_TYPE (field) != error_mark_node
4506 && !DECL_ARTIFICIAL (field))
4507 {
4508 tree field_offset;
4509
4510 field_offset = byte_position (field);
4511
4512 r = walk_subobject_offsets (TREE_TYPE (field),
4513 f,
4514 size_binop (PLUS_EXPR,
4515 offset,
4516 field_offset),
4517 offsets,
4518 max_offset,
4519 /*vbases_p=*/1);
4520 if (r)
4521 return r;
4522 }
4523 }
4524 else if (TREE_CODE (type) == ARRAY_TYPE)
4525 {
4526 tree element_type = strip_array_types (type);
4527 tree domain = TYPE_DOMAIN (type);
4528 tree index;
4529
4530 /* Avoid recursing into objects that are not interesting. */
4531 if (!CLASS_TYPE_P (element_type)
4532 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4533 || !domain
4534 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4535 return 0;
4536
4537 /* Step through each of the elements in the array. */
4538 for (index = size_zero_node;
4539 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4540 index = size_binop (PLUS_EXPR, index, size_one_node))
4541 {
4542 r = walk_subobject_offsets (TREE_TYPE (type),
4543 f,
4544 offset,
4545 offsets,
4546 max_offset,
4547 /*vbases_p=*/1);
4548 if (r)
4549 return r;
4550 offset = size_binop (PLUS_EXPR, offset,
4551 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4552 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4553 there's no point in iterating through the remaining
4554 elements of the array. */
4555 if (max_offset && tree_int_cst_lt (max_offset, offset))
4556 break;
4557 }
4558 }
4559
4560 return 0;
4561 }
4562
4563 /* Return true iff FIELD_DECL DECL is potentially overlapping. */
4564
4565 static bool
4566 field_poverlapping_p (tree decl)
4567 {
4568 /* Base fields are actually potentially overlapping, but C++ bases go through
4569 a different code path based on binfos, and ObjC++ base fields are laid out
4570 in objc-act, so we don't want layout_class_type to mess with them. */
4571 if (DECL_FIELD_IS_BASE (decl))
4572 {
4573 gcc_checking_assert (c_dialect_objc ());
4574 return false;
4575 }
4576
4577 return lookup_attribute ("no_unique_address",
4578 DECL_ATTRIBUTES (decl));
4579 }
4580
4581 /* Return true iff DECL is an empty field, either for an empty base or a
4582 [[no_unique_address]] data member. */
4583
4584 bool
4585 is_empty_field (tree decl)
4586 {
4587 if (!decl || TREE_CODE (decl) != FIELD_DECL)
4588 return false;
4589
4590 bool r = (is_empty_class (TREE_TYPE (decl))
4591 && (DECL_FIELD_IS_BASE (decl)
4592 || field_poverlapping_p (decl)));
4593
4594 /* Empty fields should have size zero. */
4595 gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl)));
4596
4597 return r;
4598 }
4599
4600 /* Record all of the empty subobjects of DECL_OR_BINFO. */
4601
4602 static void
4603 record_subobject_offsets (tree decl_or_binfo,
4604 splay_tree offsets)
4605 {
4606 tree type, offset;
4607 bool overlapping, vbases_p;
4608
4609 if (DECL_P (decl_or_binfo))
4610 {
4611 tree decl = decl_or_binfo;
4612 type = TREE_TYPE (decl);
4613 offset = byte_position (decl);
4614 overlapping = field_poverlapping_p (decl);
4615 vbases_p = true;
4616 }
4617 else
4618 {
4619 type = BINFO_TYPE (decl_or_binfo);
4620 offset = BINFO_OFFSET (decl_or_binfo);
4621 overlapping = true;
4622 vbases_p = false;
4623 }
4624
4625 tree max_offset;
4626 /* If recording subobjects for a non-static data member or a
4627 non-empty base class, we do not need to record offsets beyond
4628 the size of the biggest empty class. Additional data members
4629 will go at the end of the class. Additional base classes will go
4630 either at offset zero (if empty, in which case they cannot
4631 overlap with offsets past the size of the biggest empty class) or
4632 at the end of the class.
4633
4634 However, if we are placing an empty base class, then we must record
4635 all offsets, as either the empty class is at offset zero (where
4636 other empty classes might later be placed) or at the end of the
4637 class (where other objects might then be placed, so other empty
4638 subobjects might later overlap). */
4639 if (!overlapping
4640 || !is_empty_class (type))
4641 max_offset = sizeof_biggest_empty_class;
4642 else
4643 max_offset = NULL_TREE;
4644 walk_subobject_offsets (type, record_subobject_offset, offset,
4645 offsets, max_offset, vbases_p);
4646 }
4647
4648 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4649 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4650 virtual bases of TYPE are examined. */
4651
4652 static int
4653 layout_conflict_p (tree type,
4654 tree offset,
4655 splay_tree offsets,
4656 int vbases_p)
4657 {
4658 splay_tree_node max_node;
4659
4660 /* Get the node in OFFSETS that indicates the maximum offset where
4661 an empty subobject is located. */
4662 max_node = splay_tree_max (offsets);
4663 /* If there aren't any empty subobjects, then there's no point in
4664 performing this check. */
4665 if (!max_node)
4666 return 0;
4667
4668 return walk_subobject_offsets (type, check_subobject_offset, offset,
4669 offsets, (tree) (max_node->key),
4670 vbases_p);
4671 }
4672
4673 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4674 non-static data member of the type indicated by RLI. BINFO is the
4675 binfo corresponding to the base subobject, OFFSETS maps offsets to
4676 types already located at those offsets. This function determines
4677 the position of the DECL. */
4678
4679 static void
4680 layout_nonempty_base_or_field (record_layout_info rli,
4681 tree decl,
4682 tree binfo,
4683 splay_tree offsets)
4684 {
4685 tree offset = NULL_TREE;
4686 bool field_p;
4687 tree type;
4688
4689 if (binfo)
4690 {
4691 /* For the purposes of determining layout conflicts, we want to
4692 use the class type of BINFO; TREE_TYPE (DECL) will be the
4693 CLASSTYPE_AS_BASE version, which does not contain entries for
4694 zero-sized bases. */
4695 type = TREE_TYPE (binfo);
4696 field_p = false;
4697 }
4698 else
4699 {
4700 type = TREE_TYPE (decl);
4701 field_p = true;
4702 }
4703
4704 /* Try to place the field. It may take more than one try if we have
4705 a hard time placing the field without putting two objects of the
4706 same type at the same address. */
4707 while (1)
4708 {
4709 struct record_layout_info_s old_rli = *rli;
4710
4711 /* Place this field. */
4712 place_field (rli, decl);
4713 offset = byte_position (decl);
4714
4715 /* We have to check to see whether or not there is already
4716 something of the same type at the offset we're about to use.
4717 For example, consider:
4718
4719 struct S {};
4720 struct T : public S { int i; };
4721 struct U : public S, public T {};
4722
4723 Here, we put S at offset zero in U. Then, we can't put T at
4724 offset zero -- its S component would be at the same address
4725 as the S we already allocated. So, we have to skip ahead.
4726 Since all data members, including those whose type is an
4727 empty class, have nonzero size, any overlap can happen only
4728 with a direct or indirect base-class -- it can't happen with
4729 a data member. */
4730 /* In a union, overlap is permitted; all members are placed at
4731 offset zero. */
4732 if (TREE_CODE (rli->t) == UNION_TYPE)
4733 break;
4734 if (layout_conflict_p (field_p ? type : binfo, offset,
4735 offsets, field_p))
4736 {
4737 /* Strip off the size allocated to this field. That puts us
4738 at the first place we could have put the field with
4739 proper alignment. */
4740 *rli = old_rli;
4741
4742 /* Bump up by the alignment required for the type. */
4743 rli->bitpos
4744 = size_binop (PLUS_EXPR, rli->bitpos,
4745 bitsize_int (binfo
4746 ? CLASSTYPE_ALIGN (type)
4747 : TYPE_ALIGN (type)));
4748 normalize_rli (rli);
4749 }
4750 else if (TREE_CODE (type) == NULLPTR_TYPE
4751 && warn_abi && abi_version_crosses (9))
4752 {
4753 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4754 the offset wasn't aligned like a pointer when we started to
4755 layout this field, that affects its position. */
4756 tree pos = rli_size_unit_so_far (&old_rli);
4757 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4758 {
4759 if (abi_version_at_least (9))
4760 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4761 "alignment of %qD increased in %<-fabi-version=9%> "
4762 "(GCC 5.2)", decl);
4763 else
4764 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4765 "of %qD will increase in %<-fabi-version=9%>",
4766 decl);
4767 }
4768 break;
4769 }
4770 else
4771 /* There was no conflict. We're done laying out this field. */
4772 break;
4773 }
4774
4775 /* Now that we know where it will be placed, update its
4776 BINFO_OFFSET. */
4777 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4778 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4779 this point because their BINFO_OFFSET is copied from another
4780 hierarchy. Therefore, we may not need to add the entire
4781 OFFSET. */
4782 propagate_binfo_offsets (binfo,
4783 size_diffop_loc (input_location,
4784 fold_convert (ssizetype, offset),
4785 fold_convert (ssizetype,
4786 BINFO_OFFSET (binfo))));
4787 }
4788
4789 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4790
4791 static int
4792 empty_base_at_nonzero_offset_p (tree type,
4793 tree offset,
4794 splay_tree /*offsets*/)
4795 {
4796 return is_empty_class (type) && !integer_zerop (offset);
4797 }
4798
4799 /* Layout the empty base BINFO. EOC indicates the byte currently just
4800 past the end of the class, and should be correctly aligned for a
4801 class of the type indicated by BINFO; OFFSETS gives the offsets of
4802 the empty bases allocated so far. T is the most derived
4803 type. Return nonzero iff we added it at the end. */
4804
4805 static bool
4806 layout_empty_base_or_field (record_layout_info rli, tree binfo_or_decl,
4807 splay_tree offsets)
4808 {
4809 tree alignment;
4810 bool atend = false;
4811 tree binfo = NULL_TREE;
4812 tree decl = NULL_TREE;
4813 tree type;
4814 if (TREE_CODE (binfo_or_decl) == TREE_BINFO)
4815 {
4816 binfo = binfo_or_decl;
4817 type = BINFO_TYPE (binfo);
4818 }
4819 else
4820 {
4821 decl = binfo_or_decl;
4822 type = TREE_TYPE (decl);
4823 }
4824
4825 /* On some platforms (ARM), even empty classes will not be
4826 byte-aligned. */
4827 tree eoc = round_up_loc (input_location,
4828 rli_size_unit_so_far (rli),
4829 CLASSTYPE_ALIGN_UNIT (type));
4830
4831 /* This routine should only be used for empty classes. */
4832 gcc_assert (is_empty_class (type));
4833
4834 if (decl && DECL_USER_ALIGN (decl))
4835 alignment = size_int (DECL_ALIGN_UNIT (decl));
4836 else
4837 alignment = size_int (CLASSTYPE_ALIGN_UNIT (type));
4838
4839 /* This is an empty base class. We first try to put it at offset
4840 zero. */
4841 tree offset = size_zero_node;
4842 if (TREE_CODE (rli->t) != UNION_TYPE
4843 && layout_conflict_p (type,
4844 offset,
4845 offsets,
4846 /*vbases_p=*/0))
4847 {
4848 /* That didn't work. Now, we move forward from the next
4849 available spot in the class. */
4850 atend = true;
4851 offset = eoc;
4852 while (1)
4853 {
4854 if (!layout_conflict_p (type,
4855 offset,
4856 offsets,
4857 /*vbases_p=*/0))
4858 /* We finally found a spot where there's no overlap. */
4859 break;
4860
4861 /* There's overlap here, too. Bump along to the next spot. */
4862 offset = size_binop (PLUS_EXPR, offset, alignment);
4863 }
4864 }
4865
4866 if (decl && DECL_USER_ALIGN (decl))
4867 {
4868 rli->record_align = MAX (rli->record_align, DECL_ALIGN (decl));
4869 if (warn_packed)
4870 rli->unpacked_align = MAX (rli->unpacked_align, DECL_ALIGN (decl));
4871 TYPE_USER_ALIGN (rli->t) = 1;
4872 }
4873 else if (CLASSTYPE_USER_ALIGN (type))
4874 {
4875 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (type));
4876 if (warn_packed)
4877 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (type));
4878 TYPE_USER_ALIGN (rli->t) = 1;
4879 }
4880
4881 if (binfo)
4882 /* Adjust BINFO_OFFSET (binfo) to be exactly OFFSET. */
4883 propagate_binfo_offsets (binfo,
4884 size_diffop (offset, BINFO_OFFSET (binfo)));
4885 else
4886 {
4887 DECL_FIELD_OFFSET (decl) = offset;
4888 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4889 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4890 }
4891
4892 return atend;
4893 }
4894
4895 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4896 fields at NEXT_FIELD, and return it. */
4897
4898 static tree
4899 build_base_field_1 (tree t, tree binfo, tree access, tree *&next_field)
4900 {
4901 /* Create the FIELD_DECL. */
4902 tree basetype = BINFO_TYPE (binfo);
4903 tree as_base = CLASSTYPE_AS_BASE (basetype);
4904 gcc_assert (as_base);
4905 tree decl = build_decl (input_location, FIELD_DECL, NULL_TREE, as_base);
4906
4907 DECL_ARTIFICIAL (decl) = 1;
4908 DECL_IGNORED_P (decl) = 1;
4909 DECL_FIELD_CONTEXT (decl) = t;
4910 if (is_empty_class (basetype))
4911 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4912 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4913 else
4914 {
4915 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4916 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4917 }
4918 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4919 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4920 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4921 DECL_FIELD_IS_BASE (decl) = 1;
4922
4923 if (access == access_private_node)
4924 TREE_PRIVATE (decl) = true;
4925 else if (access == access_protected_node)
4926 TREE_PROTECTED (decl) = true;
4927
4928 /* Add the new FIELD_DECL to the list of fields for T. */
4929 DECL_CHAIN (decl) = *next_field;
4930 *next_field = decl;
4931 next_field = &DECL_CHAIN (decl);
4932
4933 return decl;
4934 }
4935
4936 /* Layout the base given by BINFO in the class indicated by RLI.
4937 *BASE_ALIGN is a running maximum of the alignments of
4938 any base class. OFFSETS gives the location of empty base
4939 subobjects. T is the most derived type. Return nonzero if the new
4940 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4941 *NEXT_FIELD, unless BINFO is for an empty base class.
4942
4943 Returns the location at which the next field should be inserted. */
4944
4945 static tree *
4946 build_base_field (record_layout_info rli, tree binfo, tree access,
4947 splay_tree offsets, tree *next_field)
4948 {
4949 tree t = rli->t;
4950 tree basetype = BINFO_TYPE (binfo);
4951
4952 if (!COMPLETE_TYPE_P (basetype))
4953 /* This error is now reported in xref_tag, thus giving better
4954 location information. */
4955 return next_field;
4956
4957 /* Place the base class. */
4958 if (!is_empty_class (basetype))
4959 {
4960 tree decl;
4961
4962 /* The containing class is non-empty because it has a non-empty
4963 base class. */
4964 CLASSTYPE_EMPTY_P (t) = 0;
4965
4966 /* Create the FIELD_DECL. */
4967 decl = build_base_field_1 (t, binfo, access, next_field);
4968
4969 /* Try to place the field. It may take more than one try if we
4970 have a hard time placing the field without putting two
4971 objects of the same type at the same address. */
4972 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4973 }
4974 else
4975 {
4976 bool atend = layout_empty_base_or_field (rli, binfo, offsets);
4977 /* A nearly-empty class "has no proper base class that is empty,
4978 not morally virtual, and at an offset other than zero." */
4979 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4980 {
4981 if (atend)
4982 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4983 /* The check above (used in G++ 3.2) is insufficient because
4984 an empty class placed at offset zero might itself have an
4985 empty base at a nonzero offset. */
4986 else if (walk_subobject_offsets (basetype,
4987 empty_base_at_nonzero_offset_p,
4988 size_zero_node,
4989 /*offsets=*/NULL,
4990 /*max_offset=*/NULL_TREE,
4991 /*vbases_p=*/true))
4992 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4993 }
4994
4995 /* We used to not create a FIELD_DECL for empty base classes because of
4996 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4997 be a problem anymore. We need them to handle initialization of C++17
4998 aggregate bases. */
4999 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
5000 {
5001 tree decl = build_base_field_1 (t, binfo, access, next_field);
5002 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
5003 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
5004 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
5005 SET_DECL_FIELD_ABI_IGNORED (decl, 1);
5006 }
5007
5008 /* An empty virtual base causes a class to be non-empty
5009 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
5010 here because that was already done when the virtual table
5011 pointer was created. */
5012 }
5013
5014 /* Record the offsets of BINFO and its base subobjects. */
5015 record_subobject_offsets (binfo, offsets);
5016
5017 return next_field;
5018 }
5019
5020 /* Layout all of the non-virtual base classes. Record empty
5021 subobjects in OFFSETS. T is the most derived type. Return nonzero
5022 if the type cannot be nearly empty. The fields created
5023 corresponding to the base classes will be inserted at
5024 *NEXT_FIELD. */
5025
5026 static void
5027 build_base_fields (record_layout_info rli,
5028 splay_tree offsets, tree *next_field)
5029 {
5030 /* Chain to hold all the new FIELD_DECLs which stand in for base class
5031 subobjects. */
5032 tree t = rli->t;
5033 tree binfo = TYPE_BINFO (t);
5034 int n_baseclasses = BINFO_N_BASE_BINFOS (binfo);
5035
5036 /* The primary base class is always allocated first. */
5037 const tree primary_binfo = CLASSTYPE_PRIMARY_BINFO (t);
5038 if (primary_binfo)
5039 {
5040 /* We need to walk BINFO_BASE_BINFO to find the access of the primary
5041 base, if it is direct. Indirect base fields are private. */
5042 tree primary_access = access_private_node;
5043 for (int i = 0; i < n_baseclasses; ++i)
5044 {
5045 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
5046 if (base_binfo == primary_binfo)
5047 {
5048 primary_access = BINFO_BASE_ACCESS (binfo, i);
5049 break;
5050 }
5051 }
5052 next_field = build_base_field (rli, primary_binfo,
5053 primary_access,
5054 offsets, next_field);
5055 }
5056
5057 /* Now allocate the rest of the bases. */
5058 for (int i = 0; i < n_baseclasses; ++i)
5059 {
5060 tree base_binfo = BINFO_BASE_BINFO (binfo, i);
5061
5062 /* The primary base was already allocated above, so we don't
5063 need to allocate it again here. */
5064 if (base_binfo == primary_binfo)
5065 continue;
5066
5067 /* Virtual bases are added at the end (a primary virtual base
5068 will have already been added). */
5069 if (BINFO_VIRTUAL_P (base_binfo))
5070 continue;
5071
5072 next_field = build_base_field (rli, base_binfo,
5073 BINFO_BASE_ACCESS (binfo, i),
5074 offsets, next_field);
5075 }
5076 }
5077
5078 /* Go through the TYPE_FIELDS of T issuing any appropriate
5079 diagnostics, figuring out which methods override which other
5080 methods, and so forth. */
5081
5082 static void
5083 check_methods (tree t)
5084 {
5085 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5086 if (DECL_DECLARES_FUNCTION_P (x))
5087 {
5088 check_for_override (x, t);
5089
5090 if (DECL_PURE_VIRTUAL_P (x)
5091 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
5092 error ("initializer specified for non-virtual method %q+D", x);
5093 /* The name of the field is the original field name
5094 Save this in auxiliary field for later overloading. */
5095 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
5096 {
5097 TYPE_POLYMORPHIC_P (t) = 1;
5098 if (DECL_PURE_VIRTUAL_P (x))
5099 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
5100 }
5101
5102 if (!DECL_VIRTUAL_P (x)
5103 && lookup_attribute ("transaction_safe_dynamic",
5104 DECL_ATTRIBUTES (x)))
5105 error_at (DECL_SOURCE_LOCATION (x),
5106 "%<transaction_safe_dynamic%> may only be specified for "
5107 "a virtual function");
5108 }
5109
5110 /* Check whether the eligible special member functions (P0848) are
5111 user-provided. add_method arranged that the CLASSTYPE_MEMBER_VEC only
5112 has the eligible ones, unless none are eligible; TYPE_FIELDS also contains
5113 ineligible overloads, which is why this needs to be separate from the loop
5114 above. */
5115
5116 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5117 {
5118 if (TREE_CODE (dtor) == OVERLOAD)
5119 {
5120 /* P0848: At the end of the definition of a class, overload
5121 resolution is performed among the prospective destructors declared
5122 in that class with an empty argument list to select the destructor
5123 for the class, also known as the selected destructor. The program
5124 is ill-formed if overload resolution fails. */
5125 int viable = 0;
5126 for (tree fn : ovl_range (dtor))
5127 if (constraints_satisfied_p (fn))
5128 ++viable;
5129 gcc_checking_assert (viable != 1);
5130
5131 auto_diagnostic_group d;
5132 if (viable == 0)
5133 error_at (location_of (t), "no viable destructor for %qT", t);
5134 else
5135 error_at (location_of (t), "destructor for %qT is ambiguous", t);
5136 print_candidates (dtor);
5137
5138 /* Arbitrarily prune the overload set to a single function for
5139 sake of error recovery. */
5140 tree *slot = find_member_slot (t, dtor_identifier);
5141 *slot = get_first_fn (dtor);
5142 }
5143 else if (user_provided_p (dtor))
5144 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = true;
5145 }
5146
5147 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5148 {
5149 if (!user_provided_p (fn))
5150 /* Might be trivial. */;
5151 else if (TREE_CODE (fn) == TEMPLATE_DECL)
5152 /* Templates are never special members. */;
5153 else if (copy_fn_p (fn)
5154 && constraints_satisfied_p (fn))
5155 TYPE_HAS_COMPLEX_COPY_CTOR (t) = true;
5156 else if (move_fn_p (fn)
5157 && constraints_satisfied_p (fn))
5158 TYPE_HAS_COMPLEX_MOVE_CTOR (t) = true;
5159 }
5160
5161 for (tree fn : ovl_range (get_class_binding_direct (t, assign_op_identifier)))
5162 {
5163 if (!user_provided_p (fn))
5164 /* Might be trivial. */;
5165 else if (TREE_CODE (fn) == TEMPLATE_DECL)
5166 /* Templates are never special members. */;
5167 else if (copy_fn_p (fn)
5168 && constraints_satisfied_p (fn))
5169 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = true;
5170 else if (move_fn_p (fn)
5171 && constraints_satisfied_p (fn))
5172 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = true;
5173 }
5174 }
5175
5176 /* FN is constructor, destructor or operator function. Clone the
5177 declaration to create a NAME'd variant. NEED_VTT_PARM_P and
5178 OMIT_INHERITED_PARMS_P are relevant if it's a cdtor. */
5179
5180 static tree
5181 copy_fndecl_with_name (tree fn, tree name, tree_code code,
5182 bool need_vtt_parm_p, bool omit_inherited_parms_p)
5183 {
5184 /* Copy the function. */
5185 tree clone = copy_decl (fn);
5186 /* Reset the function name. */
5187 DECL_NAME (clone) = name;
5188
5189 if (flag_concepts)
5190 /* Clone constraints. */
5191 if (tree ci = get_constraints (fn))
5192 set_constraints (clone, copy_node (ci));
5193
5194 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
5195 DECL_ABSTRACT_P (clone) = false;
5196 /* There's no pending inline data for this function. */
5197 DECL_PENDING_INLINE_INFO (clone) = NULL;
5198 DECL_PENDING_INLINE_P (clone) = 0;
5199
5200 if (name == base_dtor_identifier)
5201 {
5202 /* The base-class destructor is not virtual. */
5203 DECL_VIRTUAL_P (clone) = 0;
5204 DECL_VINDEX (clone) = NULL_TREE;
5205 }
5206 else if (code != ERROR_MARK)
5207 {
5208 /* Set the operator code. */
5209 const ovl_op_info_t *ovl_op = OVL_OP_INFO (false, code);
5210 DECL_OVERLOADED_OPERATOR_CODE_RAW (clone) = ovl_op->ovl_op_code;
5211
5212 /* The operator could be virtual. */
5213 if (DECL_VIRTUAL_P (clone))
5214 IDENTIFIER_VIRTUAL_P (name) = true;
5215 }
5216
5217 if (omit_inherited_parms_p)
5218 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
5219
5220 /* If there was an in-charge parameter, drop it from the function
5221 type. */
5222 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5223 {
5224 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5225 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
5226 /* Skip the `this' parameter. */
5227 parmtypes = TREE_CHAIN (parmtypes);
5228 /* Skip the in-charge parameter. */
5229 parmtypes = TREE_CHAIN (parmtypes);
5230 /* And the VTT parm, in a complete [cd]tor. */
5231 if (DECL_HAS_VTT_PARM_P (fn) && !need_vtt_parm_p)
5232 parmtypes = TREE_CHAIN (parmtypes);
5233 if (omit_inherited_parms_p)
5234 {
5235 /* If we're omitting inherited parms, that just leaves the VTT. */
5236 gcc_assert (need_vtt_parm_p);
5237 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
5238 }
5239 TREE_TYPE (clone)
5240 = build_method_type_directly (basetype,
5241 TREE_TYPE (TREE_TYPE (clone)),
5242 parmtypes);
5243 TREE_TYPE (clone)
5244 = cp_build_type_attribute_variant (TREE_TYPE (clone),
5245 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
5246 TREE_TYPE (clone)
5247 = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
5248 }
5249
5250 /* Copy the function parameters. */
5251 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
5252
5253 /* Remove the in-charge parameter. */
5254 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
5255 {
5256 DECL_CHAIN (DECL_ARGUMENTS (clone))
5257 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5258 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
5259 }
5260
5261 /* And the VTT parm, in a complete [cd]tor. */
5262 if (DECL_HAS_VTT_PARM_P (fn))
5263 {
5264 if (need_vtt_parm_p)
5265 DECL_HAS_VTT_PARM_P (clone) = 1;
5266 else
5267 {
5268 DECL_CHAIN (DECL_ARGUMENTS (clone))
5269 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
5270 DECL_HAS_VTT_PARM_P (clone) = 0;
5271 }
5272 }
5273
5274 /* A base constructor inheriting from a virtual base doesn't get the
5275 arguments. */
5276 if (omit_inherited_parms_p)
5277 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
5278
5279 for (tree parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
5280 {
5281 DECL_CONTEXT (parms) = clone;
5282 cxx_dup_lang_specific_decl (parms);
5283 }
5284
5285 /* Create the RTL for this function. */
5286 SET_DECL_RTL (clone, NULL);
5287
5288 /* Regardless of the current scope, this is a member function, so
5289 not at namespace scope. */
5290 rest_of_decl_compilation (clone, /*top_level=*/0, at_eof);
5291
5292 return clone;
5293 }
5294
5295 /* FN is an operator function, create a variant for CODE. */
5296
5297 tree
5298 copy_operator_fn (tree fn, tree_code code)
5299 {
5300 return copy_fndecl_with_name (fn, ovl_op_identifier (code),
5301 code, false, false);
5302 }
5303
5304 /* FN is a constructor or destructor. Clone the declaration to create
5305 a specialized in-charge or not-in-charge version, as indicated by
5306 NAME. */
5307
5308 static tree
5309 build_clone (tree fn, tree name, bool need_vtt_parm_p,
5310 bool omit_inherited_parms_p)
5311 {
5312 tree clone;
5313
5314 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
5315 if (TREE_CODE (fn) == TEMPLATE_DECL)
5316 {
5317 clone = copy_decl (fn);
5318 DECL_NAME (clone) = name;
5319
5320 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name,
5321 need_vtt_parm_p, omit_inherited_parms_p);
5322 DECL_TEMPLATE_RESULT (clone) = result;
5323
5324 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
5325 DECL_TI_TEMPLATE (result) = clone;
5326
5327 TREE_TYPE (clone) = TREE_TYPE (result);
5328 }
5329 else
5330 {
5331 clone = copy_fndecl_with_name (fn, name, ERROR_MARK,
5332 need_vtt_parm_p, omit_inherited_parms_p);
5333 DECL_CLONED_FUNCTION (clone) = fn;
5334
5335 maybe_prepare_return_this (clone);
5336 }
5337
5338 /* Remember where this function came from. */
5339 DECL_ABSTRACT_ORIGIN (clone) = fn;
5340
5341 /* Make it easy to find the CLONE given the FN. Note the
5342 template_result of a template will be chained this way too. */
5343 DECL_CHAIN (clone) = DECL_CHAIN (fn);
5344 DECL_CHAIN (fn) = clone;
5345
5346 return clone;
5347 }
5348
5349 /* Build the clones of FN, return the number of clones built. These
5350 will be inserted onto DECL_CHAIN of FN. */
5351
5352 void
5353 build_cdtor_clones (tree fn, bool needs_vtt_p, bool base_omits_inherited_p,
5354 bool update_methods)
5355 {
5356 unsigned count = 0;
5357
5358 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
5359 {
5360 /* For each constructor, we need two variants: an in-charge version
5361 and a not-in-charge version. */
5362 build_clone (fn, complete_ctor_identifier, false, false);
5363 build_clone (fn, base_ctor_identifier, needs_vtt_p,
5364 base_omits_inherited_p);
5365 count += 2;
5366 }
5367 else
5368 {
5369 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
5370
5371 /* For each destructor, we need three variants: an in-charge
5372 version, a not-in-charge version, and an in-charge deleting
5373 version. We clone the deleting version first because that
5374 means it will go second on the TYPE_FIELDS list -- and that
5375 corresponds to the correct layout order in the virtual
5376 function table.
5377
5378 For a non-virtual destructor, we do not build a deleting
5379 destructor. */
5380 if (DECL_VIRTUAL_P (fn))
5381 {
5382 build_clone (fn, deleting_dtor_identifier, false, false);
5383 count++;
5384 }
5385 build_clone (fn, complete_dtor_identifier, false, false);
5386 build_clone (fn, base_dtor_identifier, needs_vtt_p, false);
5387 count += 2;
5388 }
5389
5390 /* The original is now an abstract function that is never
5391 emitted. */
5392 DECL_ABSTRACT_P (fn) = true;
5393
5394 if (update_methods)
5395 for (tree clone = fn; count--;)
5396 {
5397 clone = DECL_CHAIN (clone);
5398 add_method (DECL_CONTEXT (clone), clone, false);
5399 }
5400 }
5401
5402 /* Produce declarations for all appropriate clones of FN. If
5403 UPDATE_METHODS is true, the clones are added to the
5404 CLASSTYPE_MEMBER_VEC. */
5405
5406 void
5407 clone_cdtor (tree fn, bool update_methods)
5408 {
5409 /* Avoid inappropriate cloning. */
5410 if (DECL_CHAIN (fn)
5411 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
5412 return;
5413
5414 /* Base cdtors need a vtt parm if there are virtual bases. */
5415 bool vtt = CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn));
5416
5417 /* Base ctor omits inherited parms it needs a vttparm and inherited
5418 from a virtual nase ctor. */
5419 bool base_omits_inherited = (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
5420 && base_ctor_omit_inherited_parms (fn));
5421
5422 build_cdtor_clones (fn, vtt, base_omits_inherited, update_methods);
5423 }
5424
5425 /* DECL is an in charge constructor, which is being defined. This will
5426 have had an in class declaration, from whence clones were
5427 declared. An out-of-class definition can specify additional default
5428 arguments. As it is the clones that are involved in overload
5429 resolution, we must propagate the information from the DECL to its
5430 clones. */
5431
5432 void
5433 adjust_clone_args (tree decl)
5434 {
5435 tree clone;
5436
5437 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
5438 clone = DECL_CHAIN (clone))
5439 {
5440 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
5441 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
5442 tree decl_parms, clone_parms;
5443
5444 /* Skip the 'this' parameter. */
5445 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
5446 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5447
5448 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
5449 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5450 if (DECL_HAS_VTT_PARM_P (decl))
5451 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
5452
5453 clone_parms = orig_clone_parms;
5454 if (DECL_HAS_VTT_PARM_P (clone))
5455 clone_parms = TREE_CHAIN (clone_parms);
5456
5457 for (decl_parms = orig_decl_parms; decl_parms;
5458 decl_parms = TREE_CHAIN (decl_parms),
5459 clone_parms = TREE_CHAIN (clone_parms))
5460 {
5461 if (clone_parms == void_list_node)
5462 {
5463 gcc_assert (decl_parms == clone_parms
5464 || ctor_omit_inherited_parms (clone));
5465 break;
5466 }
5467
5468 gcc_checking_assert (same_type_p (TREE_VALUE (decl_parms),
5469 TREE_VALUE (clone_parms)));
5470
5471 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
5472 {
5473 /* A default parameter has been added. Adjust the
5474 clone's parameters. */
5475 clone_parms = orig_decl_parms;
5476
5477 if (DECL_HAS_VTT_PARM_P (clone))
5478 {
5479 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
5480 TREE_VALUE (orig_clone_parms),
5481 clone_parms);
5482 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
5483 }
5484
5485 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
5486 tree type
5487 = build_method_type_directly (basetype,
5488 TREE_TYPE (TREE_TYPE (clone)),
5489 clone_parms);
5490 if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
5491 type = cp_build_type_attribute_variant (type, attrs);
5492 type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
5493 TREE_TYPE (clone) = type;
5494
5495 clone_parms = NULL_TREE;
5496 break;
5497 }
5498 }
5499 gcc_assert (!clone_parms || clone_parms == void_list_node);
5500 }
5501 }
5502
5503 /* For each of the constructors and destructors in T, create an
5504 in-charge and not-in-charge variant. */
5505
5506 static void
5507 clone_constructors_and_destructors (tree t)
5508 {
5509 /* We do not need to propagate the usingness to the clone, at this
5510 point that is not needed. */
5511 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5512 clone_cdtor (fn, /*update_methods=*/true);
5513
5514 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5515 clone_cdtor (dtor, /*update_methods=*/true);
5516 }
5517
5518 /* Deduce noexcept for a destructor DTOR. */
5519
5520 void
5521 deduce_noexcept_on_destructor (tree dtor)
5522 {
5523 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5524 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5525 noexcept_deferred_spec);
5526 }
5527
5528 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5529 of TYPE for virtual functions which FNDECL overrides. Return a
5530 mask of the tm attributes found therein. */
5531
5532 static int
5533 look_for_tm_attr_overrides (tree type, tree fndecl)
5534 {
5535 tree binfo = TYPE_BINFO (type);
5536 tree base_binfo;
5537 int ix, found = 0;
5538
5539 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5540 {
5541 tree o, basetype = BINFO_TYPE (base_binfo);
5542
5543 if (!TYPE_POLYMORPHIC_P (basetype))
5544 continue;
5545
5546 o = look_for_overrides_here (basetype, fndecl);
5547 if (o)
5548 {
5549 if (lookup_attribute ("transaction_safe_dynamic",
5550 DECL_ATTRIBUTES (o)))
5551 /* transaction_safe_dynamic is not inherited. */;
5552 else
5553 found |= tm_attr_to_mask (find_tm_attribute
5554 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5555 }
5556 else
5557 found |= look_for_tm_attr_overrides (basetype, fndecl);
5558 }
5559
5560 return found;
5561 }
5562
5563 /* Subroutine of set_method_tm_attributes. Handle the checks and
5564 inheritance for one virtual method FNDECL. */
5565
5566 static void
5567 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5568 {
5569 tree tm_attr;
5570 int found, have;
5571
5572 found = look_for_tm_attr_overrides (type, fndecl);
5573
5574 /* If FNDECL doesn't actually override anything (i.e. T is the
5575 class that first declares FNDECL virtual), then we're done. */
5576 if (found == 0)
5577 return;
5578
5579 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5580 have = tm_attr_to_mask (tm_attr);
5581
5582 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5583 tm_pure must match exactly, otherwise no weakening of
5584 tm_safe > tm_callable > nothing. */
5585 /* ??? The tm_pure attribute didn't make the transition to the
5586 multivendor language spec. */
5587 if (have == TM_ATTR_PURE)
5588 {
5589 if (found != TM_ATTR_PURE)
5590 {
5591 found &= -found;
5592 goto err_override;
5593 }
5594 }
5595 /* If the overridden function is tm_pure, then FNDECL must be. */
5596 else if (found == TM_ATTR_PURE && tm_attr)
5597 goto err_override;
5598 /* Look for base class combinations that cannot be satisfied. */
5599 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5600 {
5601 found &= ~TM_ATTR_PURE;
5602 found &= -found;
5603 error_at (DECL_SOURCE_LOCATION (fndecl),
5604 "method overrides both %<transaction_pure%> and %qE methods",
5605 tm_mask_to_attr (found));
5606 }
5607 /* If FNDECL did not declare an attribute, then inherit the most
5608 restrictive one. */
5609 else if (tm_attr == NULL)
5610 {
5611 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5612 }
5613 /* Otherwise validate that we're not weaker than a function
5614 that is being overridden. */
5615 else
5616 {
5617 found &= -found;
5618 if (found <= TM_ATTR_CALLABLE && have > found)
5619 goto err_override;
5620 }
5621 return;
5622
5623 err_override:
5624 error_at (DECL_SOURCE_LOCATION (fndecl),
5625 "method declared %qE overriding %qE method",
5626 tm_attr, tm_mask_to_attr (found));
5627 }
5628
5629 /* For each of the methods in T, propagate a class-level tm attribute. */
5630
5631 static void
5632 set_method_tm_attributes (tree t)
5633 {
5634 tree class_tm_attr, fndecl;
5635
5636 /* Don't bother collecting tm attributes if transactional memory
5637 support is not enabled. */
5638 if (!flag_tm)
5639 return;
5640
5641 /* Process virtual methods first, as they inherit directly from the
5642 base virtual function and also require validation of new attributes. */
5643 if (TYPE_CONTAINS_VPTR_P (t))
5644 {
5645 tree vchain;
5646 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5647 vchain = TREE_CHAIN (vchain))
5648 {
5649 fndecl = BV_FN (vchain);
5650 if (DECL_THUNK_P (fndecl))
5651 fndecl = THUNK_TARGET (fndecl);
5652 set_one_vmethod_tm_attributes (t, fndecl);
5653 }
5654 }
5655
5656 /* If the class doesn't have an attribute, nothing more to do. */
5657 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5658 if (class_tm_attr == NULL)
5659 return;
5660
5661 /* Any method that does not yet have a tm attribute inherits
5662 the one from the class. */
5663 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
5664 if (DECL_DECLARES_FUNCTION_P (fndecl)
5665 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5666 apply_tm_attr (fndecl, class_tm_attr);
5667 }
5668
5669 /* Returns true if FN is a default constructor. */
5670
5671 bool
5672 default_ctor_p (const_tree fn)
5673 {
5674 return (DECL_CONSTRUCTOR_P (fn)
5675 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5676 }
5677
5678 /* Returns true iff class T has a user-provided constructor that can be called
5679 with more than zero arguments. */
5680
5681 bool
5682 type_has_user_nondefault_constructor (tree t)
5683 {
5684 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5685 return false;
5686
5687 for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (t)))
5688 {
5689 if (user_provided_p (fn)
5690 && (TREE_CODE (fn) == TEMPLATE_DECL
5691 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5692 != NULL_TREE)))
5693 return true;
5694 }
5695
5696 return false;
5697 }
5698
5699 /* Returns the defaulted constructor if T has one. Otherwise, returns
5700 NULL_TREE. */
5701
5702 tree
5703 in_class_defaulted_default_constructor (tree t)
5704 {
5705 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5706 return NULL_TREE;
5707
5708 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5709 {
5710 tree fn = *iter;
5711
5712 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5713 && default_ctor_p (fn))
5714 return fn;
5715 }
5716
5717 return NULL_TREE;
5718 }
5719
5720 /* Returns true iff FN is a user-provided function, i.e. user-declared
5721 and not defaulted at its first declaration. */
5722
5723 bool
5724 user_provided_p (tree fn)
5725 {
5726 fn = STRIP_TEMPLATE (fn);
5727 return (!DECL_ARTIFICIAL (fn)
5728 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5729 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5730 }
5731
5732 /* Returns true iff class T has a user-provided constructor. */
5733
5734 bool
5735 type_has_user_provided_constructor (tree t)
5736 {
5737 if (!CLASS_TYPE_P (t))
5738 return false;
5739
5740 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5741 return false;
5742
5743 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5744 if (user_provided_p (*iter))
5745 return true;
5746
5747 return false;
5748 }
5749
5750 /* Returns true iff class T has a user-provided or explicit constructor. */
5751
5752 bool
5753 type_has_user_provided_or_explicit_constructor (tree t)
5754 {
5755 if (!CLASS_TYPE_P (t))
5756 return false;
5757
5758 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5759 return false;
5760
5761 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5762 {
5763 tree fn = *iter;
5764 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5765 return true;
5766 }
5767
5768 return false;
5769 }
5770
5771 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5772 declared or explicitly defaulted in the class body) default
5773 constructor. */
5774
5775 bool
5776 type_has_non_user_provided_default_constructor (tree t)
5777 {
5778 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5779 return false;
5780 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5781 return true;
5782
5783 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5784 {
5785 tree fn = *iter;
5786 if (TREE_CODE (fn) == FUNCTION_DECL
5787 && default_ctor_p (fn)
5788 && !user_provided_p (fn))
5789 return true;
5790 }
5791
5792 return false;
5793 }
5794
5795 /* TYPE is being used as a virtual base, and has a non-trivial move
5796 assignment. Return true if this is due to there being a user-provided
5797 move assignment in TYPE or one of its subobjects; if there isn't, then
5798 multiple move assignment can't cause any harm. */
5799
5800 bool
5801 vbase_has_user_provided_move_assign (tree type)
5802 {
5803 /* Does the type itself have a user-provided move assignment operator? */
5804 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5805 for (ovl_iterator iter (get_class_binding_direct
5806 (type, assign_op_identifier));
5807 iter; ++iter)
5808 if (user_provided_p (*iter) && move_fn_p (*iter))
5809 return true;
5810
5811 /* Do any of its bases? */
5812 tree binfo = TYPE_BINFO (type);
5813 tree base_binfo;
5814 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5815 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5816 return true;
5817
5818 /* Or non-static data members? */
5819 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5820 {
5821 if (TREE_CODE (field) == FIELD_DECL
5822 && CLASS_TYPE_P (TREE_TYPE (field))
5823 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5824 return true;
5825 }
5826
5827 /* Seems not. */
5828 return false;
5829 }
5830
5831 /* If default-initialization leaves part of TYPE uninitialized, returns
5832 a DECL for the field or TYPE itself (DR 253). */
5833
5834 tree
5835 default_init_uninitialized_part (tree type)
5836 {
5837 tree t, r, binfo;
5838 int i;
5839
5840 type = strip_array_types (type);
5841 if (!CLASS_TYPE_P (type))
5842 return type;
5843 if (!type_has_non_user_provided_default_constructor (type))
5844 return NULL_TREE;
5845 for (binfo = TYPE_BINFO (type), i = 0;
5846 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5847 {
5848 r = default_init_uninitialized_part (BINFO_TYPE (t));
5849 if (r)
5850 return r;
5851 }
5852 for (t = next_aggregate_field (TYPE_FIELDS (type)); t;
5853 t = next_aggregate_field (DECL_CHAIN (t)))
5854 if (!DECL_INITIAL (t) && !DECL_ARTIFICIAL (t))
5855 {
5856 r = default_init_uninitialized_part (TREE_TYPE (t));
5857 if (r)
5858 return DECL_P (r) ? r : t;
5859 }
5860
5861 return NULL_TREE;
5862 }
5863
5864 /* Returns true iff for class T, a trivial synthesized default constructor
5865 would be constexpr. */
5866
5867 bool
5868 trivial_default_constructor_is_constexpr (tree t)
5869 {
5870 /* A defaulted trivial default constructor is constexpr
5871 if there is nothing to initialize. */
5872 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5873 /* A class with a vptr doesn't have a trivial default ctor.
5874 In C++20, a class can have transient uninitialized members, e.g.:
5875
5876 struct S { int i; constexpr S() = default; };
5877
5878 should work. */
5879 return (cxx_dialect >= cxx20
5880 || is_really_empty_class (t, /*ignore_vptr*/true));
5881 }
5882
5883 /* Returns true iff class T has a constexpr default constructor. */
5884
5885 bool
5886 type_has_constexpr_default_constructor (tree t)
5887 {
5888 tree fns;
5889
5890 if (!CLASS_TYPE_P (t))
5891 {
5892 /* The caller should have stripped an enclosing array. */
5893 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5894 return false;
5895 }
5896 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5897 {
5898 if (!TYPE_HAS_COMPLEX_DFLT (t))
5899 return trivial_default_constructor_is_constexpr (t);
5900 /* Non-trivial, we need to check subobject constructors. */
5901 lazily_declare_fn (sfk_constructor, t);
5902 }
5903 fns = locate_ctor (t);
5904 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5905 }
5906
5907 /* Returns true iff class T has a constexpr default constructor or has an
5908 implicitly declared default constructor that we can't tell if it's constexpr
5909 without forcing a lazy declaration (which might cause undesired
5910 instantiations). */
5911
5912 static bool
5913 type_maybe_constexpr_default_constructor (tree t)
5914 {
5915 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5916 && TYPE_HAS_COMPLEX_DFLT (t))
5917 /* Assume it's constexpr. */
5918 return true;
5919 return type_has_constexpr_default_constructor (t);
5920 }
5921
5922 /* Returns true iff class T has a constexpr destructor. */
5923
5924 bool
5925 type_has_constexpr_destructor (tree t)
5926 {
5927 tree fns;
5928
5929 if (CLASSTYPE_LAZY_DESTRUCTOR (t))
5930 /* Non-trivial, we need to check subobject destructors. */
5931 lazily_declare_fn (sfk_destructor, t);
5932 fns = CLASSTYPE_DESTRUCTOR (t);
5933 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5934 }
5935
5936 /* Returns true iff class T has a constexpr destructor or has an
5937 implicitly declared destructor that we can't tell if it's constexpr
5938 without forcing a lazy declaration (which might cause undesired
5939 instantiations). */
5940
5941 static bool
5942 type_maybe_constexpr_destructor (tree t)
5943 {
5944 /* Until C++20, only trivial destruction is constexpr. */
5945 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (t))
5946 return true;
5947 if (cxx_dialect < cxx20)
5948 return false;
5949 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DESTRUCTOR (t))
5950 /* Assume it's constexpr. */
5951 return true;
5952 tree fn = CLASSTYPE_DESTRUCTOR (t);
5953 return (fn && maybe_constexpr_fn (fn));
5954 }
5955
5956 /* Returns true iff class TYPE has a virtual destructor. */
5957
5958 bool
5959 type_has_virtual_destructor (tree type)
5960 {
5961 tree dtor;
5962
5963 if (!NON_UNION_CLASS_TYPE_P (type))
5964 return false;
5965
5966 gcc_assert (COMPLETE_TYPE_P (type));
5967 dtor = CLASSTYPE_DESTRUCTOR (type);
5968 return (dtor && DECL_VIRTUAL_P (dtor));
5969 }
5970
5971 /* True iff class TYPE has a non-deleted trivial default
5972 constructor. */
5973
5974 bool
5975 type_has_non_deleted_trivial_default_ctor (tree type)
5976 {
5977 return TYPE_HAS_TRIVIAL_DFLT (type) && locate_ctor (type);
5978 }
5979
5980 /* Returns true iff T, a class, has a move-assignment or
5981 move-constructor. Does not lazily declare either.
5982 If USER_P is false, any move function will do. If it is true, the
5983 move function must be user-declared.
5984
5985 Note that user-declared here is different from "user-provided",
5986 which doesn't include functions that are defaulted in the
5987 class. */
5988
5989 bool
5990 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5991 {
5992 gcc_assert (user_p
5993 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5994 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5995
5996 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5997 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5998 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5999 return true;
6000
6001 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
6002 for (ovl_iterator iter (get_class_binding_direct
6003 (t, assign_op_identifier));
6004 iter; ++iter)
6005 if ((!user_p || !DECL_ARTIFICIAL (*iter))
6006 && DECL_CONTEXT (*iter) == t
6007 && move_fn_p (*iter))
6008 return true;
6009
6010 return false;
6011 }
6012
6013 /* True iff T has a move constructor that is not deleted. */
6014
6015 bool
6016 classtype_has_non_deleted_move_ctor (tree t)
6017 {
6018 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
6019 lazily_declare_fn (sfk_move_constructor, t);
6020 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6021 if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
6022 return true;
6023 return false;
6024 }
6025
6026 /* If T, a class, has a user-provided copy constructor, copy assignment
6027 operator, or destructor, returns that function. Otherwise, null. */
6028
6029 tree
6030 classtype_has_depr_implicit_copy (tree t)
6031 {
6032 if (!CLASSTYPE_LAZY_COPY_CTOR (t))
6033 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6034 {
6035 tree fn = *iter;
6036 if (user_provided_p (fn) && copy_fn_p (fn))
6037 return fn;
6038 }
6039
6040 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
6041 for (ovl_iterator iter (get_class_binding_direct
6042 (t, assign_op_identifier));
6043 iter; ++iter)
6044 {
6045 tree fn = *iter;
6046 if (DECL_CONTEXT (fn) == t
6047 && user_provided_p (fn) && copy_fn_p (fn))
6048 return fn;
6049 }
6050
6051 if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
6052 {
6053 tree fn = CLASSTYPE_DESTRUCTOR (t);
6054 if (user_provided_p (fn))
6055 return fn;
6056 }
6057
6058 return NULL_TREE;
6059 }
6060
6061 /* True iff T has a member or friend declaration of operator OP. */
6062
6063 bool
6064 classtype_has_op (tree t, tree_code op)
6065 {
6066 tree name = ovl_op_identifier (op);
6067 if (get_class_binding (t, name))
6068 return true;
6069 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6070 if (FRIEND_NAME (f) == name)
6071 return true;
6072 return false;
6073 }
6074
6075
6076 /* If T has a defaulted member or friend declaration of OP, return it. */
6077
6078 tree
6079 classtype_has_defaulted_op (tree t, tree_code op)
6080 {
6081 tree name = ovl_op_identifier (op);
6082 for (ovl_iterator oi (get_class_binding (t, name)); oi; ++oi)
6083 {
6084 tree fn = *oi;
6085 if (DECL_DEFAULTED_FN (fn))
6086 return fn;
6087 }
6088 for (tree f = DECL_FRIENDLIST (TYPE_MAIN_DECL (t)); f; f = TREE_CHAIN (f))
6089 if (FRIEND_NAME (f) == name)
6090 for (tree l = FRIEND_DECLS (f); l; l = TREE_CHAIN (l))
6091 {
6092 tree fn = TREE_VALUE (l);
6093 if (DECL_DEFAULTED_FN (fn))
6094 return fn;
6095 }
6096 return NULL_TREE;
6097 }
6098
6099 /* Nonzero if we need to build up a constructor call when initializing an
6100 object of this class, either because it has a user-declared constructor
6101 or because it doesn't have a default constructor (so we need to give an
6102 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
6103 what you care about is whether or not an object can be produced by a
6104 constructor (e.g. so we don't set TREE_READONLY on const variables of
6105 such type); use this function when what you care about is whether or not
6106 to try to call a constructor to create an object. The latter case is
6107 the former plus some cases of constructors that cannot be called. */
6108
6109 bool
6110 type_build_ctor_call (tree t)
6111 {
6112 tree inner;
6113 if (TYPE_NEEDS_CONSTRUCTING (t))
6114 return true;
6115 inner = strip_array_types (t);
6116 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
6117 return false;
6118 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
6119 return true;
6120 if (cxx_dialect < cxx11)
6121 return false;
6122 /* A user-declared constructor might be private, and a constructor might
6123 be trivial but deleted. */
6124 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
6125 iter; ++iter)
6126 {
6127 tree fn = *iter;
6128 if (!DECL_ARTIFICIAL (fn)
6129 || TREE_DEPRECATED (fn)
6130 || TREE_UNAVAILABLE (fn)
6131 || DECL_DELETED_FN (fn))
6132 return true;
6133 }
6134 return false;
6135 }
6136
6137 /* Like type_build_ctor_call, but for destructors. */
6138
6139 bool
6140 type_build_dtor_call (tree t)
6141 {
6142 tree inner;
6143 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6144 return true;
6145 inner = strip_array_types (t);
6146 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
6147 || !COMPLETE_TYPE_P (inner))
6148 return false;
6149 if (cxx_dialect < cxx11)
6150 return false;
6151 /* A user-declared destructor might be private, and a destructor might
6152 be trivial but deleted. */
6153 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
6154 iter; ++iter)
6155 {
6156 tree fn = *iter;
6157 if (!DECL_ARTIFICIAL (fn)
6158 || TREE_DEPRECATED (fn)
6159 || TREE_UNAVAILABLE (fn)
6160 || DECL_DELETED_FN (fn))
6161 return true;
6162 }
6163 return false;
6164 }
6165
6166 /* Returns TRUE iff we need a cookie when dynamically allocating an
6167 array whose elements have the indicated class TYPE. */
6168
6169 static bool
6170 type_requires_array_cookie (tree type)
6171 {
6172 tree fns;
6173 bool has_two_argument_delete_p = false;
6174
6175 gcc_assert (CLASS_TYPE_P (type));
6176
6177 /* If there's a non-trivial destructor, we need a cookie. In order
6178 to iterate through the array calling the destructor for each
6179 element, we'll have to know how many elements there are. */
6180 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6181 return true;
6182
6183 /* If the usual deallocation function is a two-argument whose second
6184 argument is of type `size_t', then we have to pass the size of
6185 the array to the deallocation function, so we will need to store
6186 a cookie. */
6187 fns = lookup_fnfields (TYPE_BINFO (type),
6188 ovl_op_identifier (false, VEC_DELETE_EXPR),
6189 /*protect=*/0, tf_warning_or_error);
6190 /* If there are no `operator []' members, or the lookup is
6191 ambiguous, then we don't need a cookie. */
6192 if (!fns || fns == error_mark_node)
6193 return false;
6194 /* Loop through all of the functions. */
6195 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
6196 {
6197 tree fn = *iter;
6198
6199 /* See if this function is a one-argument delete function. If
6200 it is, then it will be the usual deallocation function. */
6201 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
6202 if (second_parm == void_list_node)
6203 return false;
6204 /* Do not consider this function if its second argument is an
6205 ellipsis. */
6206 if (!second_parm)
6207 continue;
6208 /* Otherwise, if we have a two-argument function and the second
6209 argument is `size_t', it will be the usual deallocation
6210 function -- unless there is one-argument function, too. */
6211 if (TREE_CHAIN (second_parm) == void_list_node
6212 && same_type_p (TREE_VALUE (second_parm), size_type_node))
6213 has_two_argument_delete_p = true;
6214 }
6215
6216 return has_two_argument_delete_p;
6217 }
6218
6219 /* Finish computing the `literal type' property of class type T.
6220
6221 At this point, we have already processed base classes and
6222 non-static data members. We need to check whether the copy
6223 constructor is trivial, the destructor is trivial, and there
6224 is a trivial default constructor or at least one constexpr
6225 constructor other than the copy constructor. */
6226
6227 static void
6228 finalize_literal_type_property (tree t)
6229 {
6230 tree fn;
6231
6232 if (cxx_dialect < cxx11)
6233 CLASSTYPE_LITERAL_P (t) = false;
6234 else if (CLASSTYPE_LITERAL_P (t)
6235 && !type_maybe_constexpr_destructor (t))
6236 CLASSTYPE_LITERAL_P (t) = false;
6237 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
6238 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
6239 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
6240 && CLASSTYPE_NON_AGGREGATE (t)
6241 && !TYPE_HAS_CONSTEXPR_CTOR (t))
6242 CLASSTYPE_LITERAL_P (t) = false;
6243
6244 /* C++14 DR 1684 removed this restriction. */
6245 if (cxx_dialect < cxx14
6246 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
6247 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6248 if (TREE_CODE (fn) == FUNCTION_DECL
6249 && DECL_DECLARED_CONSTEXPR_P (fn)
6250 && DECL_IOBJ_MEMBER_FUNCTION_P (fn)
6251 && !DECL_CONSTRUCTOR_P (fn))
6252 {
6253 DECL_DECLARED_CONSTEXPR_P (fn) = false;
6254 if (!DECL_GENERATED_P (fn))
6255 {
6256 auto_diagnostic_group d;
6257 if (pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
6258 "enclosing class of %<constexpr%> non-static "
6259 "member function %q+#D is not a literal type", fn))
6260 explain_non_literal_class (t);
6261 }
6262 }
6263 }
6264
6265 /* T is a non-literal type used in a context which requires a constant
6266 expression. Explain why it isn't literal. */
6267
6268 void
6269 explain_non_literal_class (tree t)
6270 {
6271 static hash_set<tree> *diagnosed;
6272
6273 if (!CLASS_TYPE_P (t))
6274 return;
6275 t = TYPE_MAIN_VARIANT (t);
6276
6277 if (diagnosed == NULL)
6278 diagnosed = new hash_set<tree>;
6279 if (diagnosed->add (t))
6280 /* Already explained. */
6281 return;
6282
6283 auto_diagnostic_group d;
6284 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
6285 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
6286 inform (UNKNOWN_LOCATION,
6287 " %qT is a closure type, which is only literal in "
6288 "C++17 and later", t);
6289 else if (cxx_dialect < cxx20 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
6290 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
6291 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
6292 && !type_maybe_constexpr_destructor (t))
6293 inform (UNKNOWN_LOCATION, " %q+T does not have %<constexpr%> destructor",
6294 t);
6295 else if (CLASSTYPE_NON_AGGREGATE (t)
6296 && !TYPE_HAS_TRIVIAL_DFLT (t)
6297 && !LAMBDA_TYPE_P (t)
6298 && !TYPE_HAS_CONSTEXPR_CTOR (t))
6299 {
6300 inform (UNKNOWN_LOCATION,
6301 " %q+T is not an aggregate, does not have a trivial "
6302 "default constructor, and has no %<constexpr%> constructor that "
6303 "is not a copy or move constructor", t);
6304 if (type_has_non_user_provided_default_constructor (t))
6305 /* Note that we can't simply call locate_ctor because when the
6306 constructor is deleted it just returns NULL_TREE. */
6307 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
6308 {
6309 tree fn = *iter;
6310 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6311
6312 parms = skip_artificial_parms_for (fn, parms);
6313
6314 if (sufficient_parms_p (parms))
6315 {
6316 if (DECL_DELETED_FN (fn))
6317 maybe_explain_implicit_delete (fn);
6318 else
6319 explain_invalid_constexpr_fn (fn);
6320 break;
6321 }
6322 }
6323 }
6324 else
6325 {
6326 tree binfo, base_binfo, field; int i;
6327 for (binfo = TYPE_BINFO (t), i = 0;
6328 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6329 {
6330 tree basetype = TREE_TYPE (base_binfo);
6331 if (!CLASSTYPE_LITERAL_P (basetype))
6332 {
6333 inform (UNKNOWN_LOCATION,
6334 " base class %qT of %q+T is non-literal",
6335 basetype, t);
6336 explain_non_literal_class (basetype);
6337 return;
6338 }
6339 }
6340 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6341 {
6342 tree ftype;
6343 if (TREE_CODE (field) != FIELD_DECL)
6344 continue;
6345 ftype = TREE_TYPE (field);
6346 if (!literal_type_p (ftype))
6347 {
6348 inform (DECL_SOURCE_LOCATION (field),
6349 " non-static data member %qD has non-literal type",
6350 field);
6351 if (CLASS_TYPE_P (ftype))
6352 explain_non_literal_class (ftype);
6353 }
6354 if (CP_TYPE_VOLATILE_P (ftype))
6355 inform (DECL_SOURCE_LOCATION (field),
6356 " non-static data member %qD has volatile type", field);
6357 }
6358 }
6359 }
6360
6361 /* Check the validity of the bases and members declared in T. Add any
6362 implicitly-generated functions (like copy-constructors and
6363 assignment operators). Compute various flag bits (like
6364 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
6365 level: i.e., independently of the ABI in use. */
6366
6367 static void
6368 check_bases_and_members (tree t)
6369 {
6370 /* Nonzero if the implicitly generated copy constructor should take
6371 a non-const reference argument. */
6372 int cant_have_const_ctor;
6373 /* Nonzero if the implicitly generated assignment operator
6374 should take a non-const reference argument. */
6375 int no_const_asn_ref;
6376 tree access_decls;
6377 bool saved_complex_asn_ref;
6378 bool saved_nontrivial_dtor;
6379 tree fn;
6380
6381 /* By default, we use const reference arguments and generate default
6382 constructors. */
6383 cant_have_const_ctor = 0;
6384 no_const_asn_ref = 0;
6385
6386 /* Check all the base-classes and set FMEM members to point to arrays
6387 of potential interest. */
6388 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
6389
6390 /* Deduce noexcept on destructor. This needs to happen after we've set
6391 triviality flags appropriately for our bases, and before checking
6392 overriden virtual functions via check_methods. */
6393 if (cxx_dialect >= cxx11)
6394 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
6395 for (tree fn : ovl_range (dtor))
6396 deduce_noexcept_on_destructor (fn);
6397
6398 /* Check all the method declarations. */
6399 check_methods (t);
6400
6401 /* Save the initial values of these flags which only indicate whether
6402 or not the class has user-provided functions. As we analyze the
6403 bases and members we can set these flags for other reasons. */
6404 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
6405 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
6406
6407 /* Check all the data member declarations. We cannot call
6408 check_field_decls until we have called check_bases check_methods,
6409 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
6410 being set appropriately. */
6411 check_field_decls (t, &access_decls,
6412 &cant_have_const_ctor,
6413 &no_const_asn_ref);
6414
6415 /* A nearly-empty class has to be vptr-containing; a nearly empty
6416 class contains just a vptr. */
6417 if (!TYPE_CONTAINS_VPTR_P (t))
6418 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
6419
6420 /* Do some bookkeeping that will guide the generation of implicitly
6421 declared member functions. */
6422 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6423 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
6424 /* We need to call a constructor for this class if it has a
6425 user-provided constructor, or if the default constructor is going
6426 to initialize the vptr. (This is not an if-and-only-if;
6427 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
6428 themselves need constructing.) */
6429 TYPE_NEEDS_CONSTRUCTING (t)
6430 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
6431 /* [dcl.init.aggr]
6432
6433 An aggregate is an array or a class with no user-provided
6434 constructors ... and no virtual functions.
6435
6436 Again, other conditions for being an aggregate are checked
6437 elsewhere. */
6438 CLASSTYPE_NON_AGGREGATE (t)
6439 |= (type_has_user_provided_or_explicit_constructor (t)
6440 || TYPE_POLYMORPHIC_P (t));
6441 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
6442 retain the old definition internally for ABI reasons. */
6443 CLASSTYPE_NON_LAYOUT_POD_P (t)
6444 |= (CLASSTYPE_NON_AGGREGATE (t)
6445 || saved_nontrivial_dtor || saved_complex_asn_ref);
6446 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
6447 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6448 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
6449 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
6450
6451 /* Is this class non-layout-POD because it wasn't an aggregate in C++98? */
6452 if (CLASSTYPE_NON_POD_AGGREGATE (t))
6453 {
6454 if (CLASSTYPE_NON_LAYOUT_POD_P (t))
6455 /* It's non-POD for another reason. */
6456 CLASSTYPE_NON_POD_AGGREGATE (t) = false;
6457 else if (abi_version_at_least (17))
6458 CLASSTYPE_NON_LAYOUT_POD_P (t) = true;
6459 }
6460
6461 /* P1008: Prohibit aggregates with user-declared constructors. */
6462 if (cxx_dialect >= cxx20 && TYPE_HAS_USER_CONSTRUCTOR (t))
6463 {
6464 CLASSTYPE_NON_AGGREGATE (t) = true;
6465 if (!CLASSTYPE_NON_LAYOUT_POD_P (t))
6466 {
6467 /* c++/120012: The C++20 aggregate change affected layout. */
6468 if (!abi_version_at_least (21))
6469 CLASSTYPE_NON_LAYOUT_POD_P (t) = true;
6470 if (abi_version_crosses (21))
6471 CLASSTYPE_NON_AGGREGATE_POD (t) = true;
6472 }
6473 }
6474
6475 /* If the only explicitly declared default constructor is user-provided,
6476 set TYPE_HAS_COMPLEX_DFLT. */
6477 if (!TYPE_HAS_COMPLEX_DFLT (t)
6478 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
6479 && !type_has_non_user_provided_default_constructor (t))
6480 TYPE_HAS_COMPLEX_DFLT (t) = true;
6481
6482 /* Warn if a public base of a polymorphic type has an accessible
6483 non-virtual destructor. It is only now that we know the class is
6484 polymorphic. Although a polymorphic base will have a already
6485 been diagnosed during its definition, we warn on use too. */
6486 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
6487 {
6488 tree binfo = TYPE_BINFO (t);
6489 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
6490 tree base_binfo;
6491 unsigned i;
6492
6493 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6494 {
6495 tree basetype = TREE_TYPE (base_binfo);
6496
6497 if ((*accesses)[i] == access_public_node
6498 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
6499 && accessible_nvdtor_p (basetype))
6500 warning (OPT_Wnon_virtual_dtor,
6501 "base class %q#T has accessible non-virtual destructor",
6502 basetype);
6503 }
6504 }
6505
6506 /* If the class has no user-declared constructor, but does have
6507 non-static const or reference data members that can never be
6508 initialized, issue a warning. */
6509 if (warn_uninitialized
6510 /* Classes with user-declared constructors are presumed to
6511 initialize these members. */
6512 && !TYPE_HAS_USER_CONSTRUCTOR (t)
6513 /* Aggregates can be initialized with brace-enclosed
6514 initializers. */
6515 && CLASSTYPE_NON_AGGREGATE (t))
6516 {
6517 tree field;
6518
6519 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6520 {
6521 tree type;
6522
6523 if (TREE_CODE (field) != FIELD_DECL
6524 || DECL_INITIAL (field) != NULL_TREE)
6525 continue;
6526
6527 type = TREE_TYPE (field);
6528 if (TYPE_REF_P (type))
6529 warning_at (DECL_SOURCE_LOCATION (field),
6530 OPT_Wuninitialized, "non-static reference %q#D "
6531 "in class without a constructor", field);
6532 else if (CP_TYPE_CONST_P (type)
6533 && (!CLASS_TYPE_P (type)
6534 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
6535 warning_at (DECL_SOURCE_LOCATION (field),
6536 OPT_Wuninitialized, "non-static const member %q#D "
6537 "in class without a constructor", field);
6538 }
6539 }
6540
6541 /* Synthesize any needed methods. */
6542 add_implicitly_declared_members (t, &access_decls,
6543 cant_have_const_ctor,
6544 no_const_asn_ref);
6545
6546 /* Check defaulted declarations here so we have cant_have_const_ctor
6547 and don't need to worry about clones. */
6548 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6549 if (DECL_DECLARES_FUNCTION_P (fn)
6550 && !DECL_ARTIFICIAL (fn)
6551 && DECL_DEFAULTED_IN_CLASS_P (fn)
6552 /* ...except handle comparisons later, in finish_struct_1. */
6553 && special_function_p (fn) != sfk_comparison)
6554 {
6555 bool imp_const_p
6556 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
6557 : !no_const_asn_ref);
6558 defaulted_late_check (fn, imp_const_p);
6559 }
6560
6561 if (LAMBDA_TYPE_P (t))
6562 /* "This class type is not an aggregate." */
6563 CLASSTYPE_NON_AGGREGATE (t) = 1;
6564
6565 /* Compute the 'literal type' property before we
6566 do anything with non-static member functions. */
6567 finalize_literal_type_property (t);
6568
6569 /* Create the in-charge and not-in-charge variants of constructors
6570 and destructors. */
6571 clone_constructors_and_destructors (t);
6572
6573 /* Process the using-declarations. */
6574 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6575 handle_using_decl (TREE_VALUE (access_decls), t);
6576
6577 /* Figure out whether or not we will need a cookie when dynamically
6578 allocating an array of this type. */
6579 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6580 = type_requires_array_cookie (t);
6581
6582 /* Classes marked hot or cold propagate the attribute to all members. We
6583 may do this now that methods are declared. This does miss some lazily
6584 declared special member functions (CLASSTYPE_LAZY_*), which are handled
6585 in lazily_declare_fn later on. */
6586 propagate_class_warmth_attribute (t);
6587 }
6588
6589 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6590 accordingly. If a new vfield was created (because T doesn't have a
6591 primary base class), then the newly created field is returned. It
6592 is not added to the TYPE_FIELDS list; it is the caller's
6593 responsibility to do that. Accumulate declared virtual functions
6594 on VIRTUALS_P. */
6595
6596 static tree
6597 create_vtable_ptr (tree t, tree* virtuals_p)
6598 {
6599 tree fn;
6600
6601 /* Collect the virtual functions declared in T. */
6602 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
6603 if (TREE_CODE (fn) == FUNCTION_DECL
6604 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6605 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6606 {
6607 tree new_virtual = make_node (TREE_LIST);
6608
6609 BV_FN (new_virtual) = fn;
6610 BV_DELTA (new_virtual) = integer_zero_node;
6611 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6612
6613 TREE_CHAIN (new_virtual) = *virtuals_p;
6614 *virtuals_p = new_virtual;
6615 }
6616
6617 /* If we couldn't find an appropriate base class, create a new field
6618 here. Even if there weren't any new virtual functions, we might need a
6619 new virtual function table if we're supposed to include vptrs in
6620 all classes that need them. */
6621 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6622 {
6623 /* We build this decl with vtbl_ptr_type_node, which is a
6624 `vtable_entry_type*'. It might seem more precise to use
6625 `vtable_entry_type (*)[N]' where N is the number of virtual
6626 functions. However, that would require the vtable pointer in
6627 base classes to have a different type than the vtable pointer
6628 in derived classes. We could make that happen, but that
6629 still wouldn't solve all the problems. In particular, the
6630 type-based alias analysis code would decide that assignments
6631 to the base class vtable pointer can't alias assignments to
6632 the derived class vtable pointer, since they have different
6633 types. Thus, in a derived class destructor, where the base
6634 class constructor was inlined, we could generate bad code for
6635 setting up the vtable pointer.
6636
6637 Therefore, we use one type for all vtable pointers. We still
6638 use a type-correct type; it's just doesn't indicate the array
6639 bounds. That's better than using `void*' or some such; it's
6640 cleaner, and it let's the alias analysis code know that these
6641 stores cannot alias stores to void*! */
6642 tree field;
6643
6644 field = build_decl (input_location,
6645 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6646 DECL_VIRTUAL_P (field) = 1;
6647 DECL_ARTIFICIAL (field) = 1;
6648 DECL_FIELD_CONTEXT (field) = t;
6649 DECL_FCONTEXT (field) = t;
6650 if (TYPE_PACKED (t))
6651 DECL_PACKED (field) = 1;
6652
6653 TYPE_VFIELD (t) = field;
6654
6655 /* This class is non-empty. */
6656 CLASSTYPE_EMPTY_P (t) = 0;
6657
6658 return field;
6659 }
6660
6661 return NULL_TREE;
6662 }
6663
6664 /* Add OFFSET to all base types of BINFO which is a base in the
6665 hierarchy dominated by T.
6666
6667 OFFSET, which is a type offset, is number of bytes. */
6668
6669 static void
6670 propagate_binfo_offsets (tree binfo, tree offset)
6671 {
6672 int i;
6673 tree primary_binfo;
6674 tree base_binfo;
6675
6676 /* Update BINFO's offset. */
6677 BINFO_OFFSET (binfo)
6678 = fold_convert (sizetype,
6679 size_binop (PLUS_EXPR,
6680 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6681 offset));
6682
6683 /* Find the primary base class. */
6684 primary_binfo = get_primary_binfo (binfo);
6685
6686 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6687 propagate_binfo_offsets (primary_binfo, offset);
6688
6689 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6690 downwards. */
6691 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6692 {
6693 /* Don't do the primary base twice. */
6694 if (base_binfo == primary_binfo)
6695 continue;
6696
6697 if (BINFO_VIRTUAL_P (base_binfo))
6698 continue;
6699
6700 propagate_binfo_offsets (base_binfo, offset);
6701 }
6702 }
6703
6704 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6705 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6706 empty subobjects of T. */
6707
6708 static void
6709 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6710 {
6711 tree vbase;
6712 tree t = rli->t;
6713 tree *next_field;
6714
6715 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6716 return;
6717
6718 /* Find the last field. The artificial fields created for virtual
6719 bases will go after the last extant field to date. */
6720 next_field = &TYPE_FIELDS (t);
6721 while (*next_field)
6722 next_field = &DECL_CHAIN (*next_field);
6723
6724 /* Go through the virtual bases, allocating space for each virtual
6725 base that is not already a primary base class. These are
6726 allocated in inheritance graph order. */
6727 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6728 {
6729 if (!BINFO_VIRTUAL_P (vbase))
6730 continue;
6731
6732 if (!BINFO_PRIMARY_P (vbase))
6733 {
6734 /* This virtual base is not a primary base of any class in the
6735 hierarchy, so we have to add space for it. */
6736 next_field = build_base_field (rli, vbase,
6737 access_private_node,
6738 offsets, next_field);
6739 }
6740 }
6741 }
6742
6743 /* Returns the offset of the byte just past the end of the base class
6744 BINFO. */
6745
6746 static tree
6747 end_of_base (tree binfo)
6748 {
6749 tree size;
6750
6751 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6752 size = TYPE_SIZE_UNIT (char_type_node);
6753 else if (is_empty_class (BINFO_TYPE (binfo)))
6754 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6755 allocate some space for it. It cannot have virtual bases, so
6756 TYPE_SIZE_UNIT is fine. */
6757 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6758 else
6759 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6760
6761 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6762 }
6763
6764 /* Returns one of three variations of the ending offset of T. If MODE is
6765 eoc_nvsize, the result is the ABI "nvsize" (i.e. sizeof before allocating
6766 vbases). If MODE is eoc_vsize, the result is the sizeof after allocating
6767 vbases but before rounding, which is not named in the ABI. If MODE is
6768 eoc_nv_or_dsize, the result is the greater of "nvsize" and "dsize" (the size
6769 of the actual data in the class, kinda), as used for allocation of
6770 potentially-overlapping fields. */
6771
6772 enum eoc_mode { eoc_nvsize, eoc_vsize, eoc_nv_or_dsize };
6773 static tree
6774 end_of_class (tree t, eoc_mode mode)
6775 {
6776 tree result = size_zero_node;
6777 vec<tree, va_gc> *vbases;
6778 tree binfo;
6779 tree base_binfo;
6780 tree offset;
6781 int i;
6782
6783 for (binfo = TYPE_BINFO (t), i = 0;
6784 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6785 {
6786 if (BINFO_VIRTUAL_P (base_binfo)
6787 && (!BINFO_PRIMARY_P (base_binfo)
6788 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6789 continue;
6790
6791 offset = end_of_base (base_binfo);
6792 if (tree_int_cst_lt (result, offset))
6793 result = offset;
6794 }
6795
6796 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6797 if (TREE_CODE (field) == FIELD_DECL
6798 && !DECL_FIELD_IS_BASE (field))
6799 {
6800 tree size = DECL_SIZE_UNIT (field);
6801 if (!size)
6802 /* DECL_SIZE_UNIT can be null for a flexible array. */
6803 continue;
6804
6805 if (is_empty_field (field))
6806 /* For empty fields DECL_SIZE_UNIT is 0, but we want the
6807 size of the type (usually 1) for computing nvsize. */
6808 size = TYPE_SIZE_UNIT (TREE_TYPE (field));
6809
6810 if (DECL_BIT_FIELD_TYPE (field))
6811 {
6812 offset = size_binop (PLUS_EXPR, bit_position (field),
6813 DECL_SIZE (field));
6814 offset = size_binop (CEIL_DIV_EXPR, offset, bitsize_unit_node);
6815 offset = fold_convert (sizetype, offset);
6816 }
6817 else
6818 offset = size_binop (PLUS_EXPR, byte_position (field), size);
6819 if (tree_int_cst_lt (result, offset))
6820 result = offset;
6821 }
6822
6823 if (mode != eoc_nvsize)
6824 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6825 vec_safe_iterate (vbases, i, &base_binfo); i++)
6826 {
6827 if (mode == eoc_nv_or_dsize)
6828 /* For dsize, don't count trailing empty bases. */
6829 offset = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo),
6830 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
6831 else
6832 offset = end_of_base (base_binfo);
6833 if (tree_int_cst_lt (result, offset))
6834 result = offset;
6835 }
6836
6837 return result;
6838 }
6839
6840 /* Warn as appropriate about the change in whether we pack into the tail
6841 padding of FIELD, a base field which has a C++14 aggregate type with default
6842 member initializers. */
6843
6844 static void
6845 check_non_pod_aggregate (tree field)
6846 {
6847 if ((!abi_version_crosses (17) || cxx_dialect < cxx14)
6848 && (!abi_version_crosses (21) || cxx_dialect < cxx20))
6849 return;
6850 if (TREE_CODE (field) != FIELD_DECL
6851 || (!DECL_FIELD_IS_BASE (field)
6852 && !field_poverlapping_p (field)))
6853 return;
6854 tree next = DECL_CHAIN (field);
6855 while (next && TREE_CODE (next) != FIELD_DECL) next = DECL_CHAIN (next);
6856 if (!next)
6857 return;
6858 tree type = TREE_TYPE (field);
6859 if (TYPE_IDENTIFIER (type) == as_base_identifier)
6860 type = TYPE_CONTEXT (type);
6861 if (!CLASS_TYPE_P (type)
6862 || is_empty_class (type)
6863 || (!CLASSTYPE_NON_POD_AGGREGATE (type)
6864 && !CLASSTYPE_NON_AGGREGATE_POD (type)))
6865 return;
6866 tree size = end_of_class (type, (DECL_FIELD_IS_BASE (field)
6867 ? eoc_nvsize : eoc_nv_or_dsize));
6868 tree rounded = round_up_loc (input_location, size, DECL_ALIGN_UNIT (next));
6869 if (tree_int_cst_lt (rounded, TYPE_SIZE_UNIT (type)))
6870 {
6871 location_t loc = DECL_SOURCE_LOCATION (next);
6872 if (DECL_FIELD_IS_BASE (next))
6873 {
6874 if (abi_version_crosses (17)
6875 && CLASSTYPE_NON_POD_AGGREGATE (type))
6876 warning_at (loc, OPT_Wabi,"offset of %qT base class for "
6877 "%<-std=c++14%> and up changes in "
6878 "%<-fabi-version=17%> (GCC 12)", TREE_TYPE (next));
6879 else if (abi_version_crosses (21)
6880 && CLASSTYPE_NON_AGGREGATE_POD (type))
6881 warning_at (loc, OPT_Wabi,"offset of %qT base class for "
6882 "%<-std=c++20%> and up changes in "
6883 "%<-fabi-version=21%> (GCC 16)", TREE_TYPE (next));
6884 }
6885 else
6886 {
6887 if (abi_version_crosses (17)
6888 && CLASSTYPE_NON_POD_AGGREGATE (type))
6889 warning_at (loc, OPT_Wabi, "offset of %qD for "
6890 "%<-std=c++14%> and up changes in "
6891 "%<-fabi-version=17%> (GCC 12)", next);
6892 else if (abi_version_crosses (21)
6893 && CLASSTYPE_NON_AGGREGATE_POD (type))
6894 warning_at (loc, OPT_Wabi, "offset of %qD for "
6895 "%<-std=c++20%> and up changes in "
6896 "%<-fabi-version=21%> (GCC 16)", next);
6897 }
6898 }
6899 }
6900
6901 /* Warn about bases of T that are inaccessible because they are
6902 ambiguous. For example:
6903
6904 struct S {};
6905 struct T : public S {};
6906 struct U : public S, public T {};
6907
6908 Here, `(S*) new U' is not allowed because there are two `S'
6909 subobjects of U. */
6910
6911 static void
6912 maybe_warn_about_inaccessible_bases (tree t)
6913 {
6914 int i;
6915 vec<tree, va_gc> *vbases;
6916 tree basetype;
6917 tree binfo;
6918 tree base_binfo;
6919
6920 /* If not checking for warning then return early. */
6921 if (!warn_inaccessible_base)
6922 return;
6923
6924 /* If there are no repeated bases, nothing can be ambiguous. */
6925 if (!CLASSTYPE_REPEATED_BASE_P (t))
6926 return;
6927
6928 /* Check direct bases. */
6929 for (binfo = TYPE_BINFO (t), i = 0;
6930 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6931 {
6932 basetype = BINFO_TYPE (base_binfo);
6933
6934 if (!uniquely_derived_from_p (basetype, t))
6935 warning (OPT_Winaccessible_base, "direct base %qT inaccessible "
6936 "in %qT due to ambiguity", basetype, t);
6937 }
6938
6939 /* Check for ambiguous virtual bases. */
6940 if (extra_warnings)
6941 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6942 vec_safe_iterate (vbases, i, &binfo); i++)
6943 {
6944 basetype = BINFO_TYPE (binfo);
6945
6946 if (!uniquely_derived_from_p (basetype, t))
6947 warning (OPT_Winaccessible_base, "virtual base %qT inaccessible in "
6948 "%qT due to ambiguity", basetype, t);
6949 }
6950 }
6951
6952 /* Compare two INTEGER_CSTs K1 and K2. */
6953
6954 static int
6955 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6956 {
6957 return tree_int_cst_compare ((tree) k1, (tree) k2);
6958 }
6959
6960 /* Increase the size indicated in RLI to account for empty classes
6961 that are "off the end" of the class. */
6962
6963 static void
6964 include_empty_classes (record_layout_info rli)
6965 {
6966 tree eoc;
6967 tree rli_size;
6968
6969 /* It might be the case that we grew the class to allocate a
6970 zero-sized base class. That won't be reflected in RLI, yet,
6971 because we are willing to overlay multiple bases at the same
6972 offset. However, now we need to make sure that RLI is big enough
6973 to reflect the entire class. */
6974 eoc = end_of_class (rli->t, eoc_vsize);
6975 rli_size = rli_size_unit_so_far (rli);
6976 if (TREE_CODE (rli_size) == INTEGER_CST
6977 && tree_int_cst_lt (rli_size, eoc))
6978 {
6979 /* The size should have been rounded to a whole byte. */
6980 gcc_assert (tree_int_cst_equal
6981 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6982 rli->bitpos
6983 = size_binop (PLUS_EXPR,
6984 rli->bitpos,
6985 size_binop (MULT_EXPR,
6986 fold_convert (bitsizetype,
6987 size_binop (MINUS_EXPR,
6988 eoc, rli_size)),
6989 bitsize_int (BITS_PER_UNIT)));
6990 normalize_rli (rli);
6991 }
6992 }
6993
6994 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6995 BINFO_OFFSETs for all of the base-classes. Position the vtable
6996 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6997
6998 static void
6999 layout_class_type (tree t, tree *virtuals_p)
7000 {
7001 tree non_static_data_members;
7002 tree field;
7003 tree vptr;
7004 record_layout_info rli;
7005 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
7006 types that appear at that offset. */
7007 splay_tree empty_base_offsets;
7008 /* True if the last field laid out was a bit-field. */
7009 bool last_field_was_bitfield = false;
7010 /* The location at which the next field should be inserted. */
7011 tree *next_field;
7012
7013 /* Keep track of the first non-static data member. */
7014 non_static_data_members = TYPE_FIELDS (t);
7015
7016 /* Start laying out the record. */
7017 rli = start_record_layout (t);
7018
7019 /* Mark all the primary bases in the hierarchy. */
7020 determine_primary_bases (t);
7021
7022 /* Create a pointer to our virtual function table. */
7023 vptr = create_vtable_ptr (t, virtuals_p);
7024
7025 /* The vptr is always the first thing in the class. */
7026 if (vptr)
7027 {
7028 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
7029 TYPE_FIELDS (t) = vptr;
7030 next_field = &DECL_CHAIN (vptr);
7031 place_field (rli, vptr);
7032 }
7033 else
7034 next_field = &TYPE_FIELDS (t);
7035
7036 /* Build FIELD_DECLs for all of the non-virtual base-types. */
7037 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
7038 NULL, NULL);
7039 build_base_fields (rli, empty_base_offsets, next_field);
7040
7041 /* Layout the non-static data members. */
7042 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
7043 {
7044 tree type;
7045 tree padding;
7046
7047 /* We still pass things that aren't non-static data members to
7048 the back end, in case it wants to do something with them. */
7049 if (TREE_CODE (field) != FIELD_DECL)
7050 {
7051 place_field (rli, field);
7052 /* If the static data member has incomplete type, keep track
7053 of it so that it can be completed later. (The handling
7054 of pending statics in finish_record_layout is
7055 insufficient; consider:
7056
7057 struct S1;
7058 struct S2 { static S1 s1; };
7059
7060 At this point, finish_record_layout will be called, but
7061 S1 is still incomplete.) */
7062 if (VAR_P (field))
7063 {
7064 maybe_register_incomplete_var (field);
7065 /* The visibility of static data members is determined
7066 at their point of declaration, not their point of
7067 definition. */
7068 determine_visibility (field);
7069 }
7070 continue;
7071 }
7072
7073 type = TREE_TYPE (field);
7074 if (type == error_mark_node)
7075 continue;
7076
7077 padding = NULL_TREE;
7078
7079 bool might_overlap = field_poverlapping_p (field);
7080
7081 if (might_overlap && CLASS_TYPE_P (type)
7082 && (CLASSTYPE_NON_LAYOUT_POD_P (type) || CLASSTYPE_EMPTY_P (type)))
7083 {
7084 /* if D is a potentially-overlapping data member, update sizeof(C) to
7085 max (sizeof(C), offset(D)+max (nvsize(D), dsize(D))). */
7086 if (CLASSTYPE_EMPTY_P (type))
7087 DECL_SIZE (field) = DECL_SIZE_UNIT (field) = size_zero_node;
7088 else
7089 {
7090 tree size = end_of_class (type, eoc_nv_or_dsize);
7091 DECL_SIZE_UNIT (field) = size;
7092 DECL_SIZE (field) = bit_from_pos (size, bitsize_zero_node);
7093 }
7094 }
7095
7096 /* If this field is a bit-field whose width is greater than its
7097 type, then there are some special rules for allocating
7098 it. */
7099 if (DECL_C_BIT_FIELD (field)
7100 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
7101 {
7102 bool was_unnamed_p = false;
7103 /* We must allocate the bits as if suitably aligned for the
7104 longest integer type that fits in this many bits. Then,
7105 we are supposed to use the left over bits as additional
7106 padding. */
7107
7108 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
7109 tree limit = size_int (MAX_FIXED_MODE_SIZE);
7110 if (tree_int_cst_lt (DECL_SIZE (field), limit))
7111 limit = DECL_SIZE (field);
7112
7113 tree integer_type = integer_types[itk_char];
7114 for (unsigned itk = itk_char; itk != itk_none; itk++)
7115 if (tree next = integer_types[itk])
7116 {
7117 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
7118 /* Too big, so our current guess is what we want. */
7119 break;
7120 /* Not bigger than limit, ok */
7121 integer_type = next;
7122 }
7123
7124 /* Figure out how much additional padding is required. */
7125 if (TREE_CODE (t) == UNION_TYPE)
7126 /* In a union, the padding field must have the full width
7127 of the bit-field; all fields start at offset zero. */
7128 padding = DECL_SIZE (field);
7129 else
7130 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
7131 TYPE_SIZE (integer_type));
7132
7133 if (integer_zerop (padding))
7134 padding = NULL_TREE;
7135
7136 /* An unnamed bitfield does not normally affect the
7137 alignment of the containing class on a target where
7138 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
7139 make any exceptions for unnamed bitfields when the
7140 bitfields are longer than their types. Therefore, we
7141 temporarily give the field a name. */
7142 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
7143 {
7144 was_unnamed_p = true;
7145 DECL_NAME (field) = make_anon_name ();
7146 }
7147
7148 DECL_SIZE (field) = TYPE_SIZE (integer_type);
7149 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
7150 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
7151 layout_nonempty_base_or_field (rli, field, NULL_TREE,
7152 empty_base_offsets);
7153 if (was_unnamed_p)
7154 DECL_NAME (field) = NULL_TREE;
7155 /* Now that layout has been performed, set the size of the
7156 field to the size of its declared type; the rest of the
7157 field is effectively invisible. */
7158 DECL_SIZE (field) = TYPE_SIZE (type);
7159 /* We must also reset the DECL_MODE of the field. */
7160 SET_DECL_MODE (field, TYPE_MODE (type));
7161 }
7162 else if (might_overlap && is_empty_class (type))
7163 {
7164 SET_DECL_FIELD_ABI_IGNORED (field, 1);
7165 layout_empty_base_or_field (rli, field, empty_base_offsets);
7166 }
7167 else
7168 layout_nonempty_base_or_field (rli, field, NULL_TREE,
7169 empty_base_offsets);
7170
7171 /* Remember the location of any empty classes in FIELD. */
7172 record_subobject_offsets (field, empty_base_offsets);
7173
7174 /* If a bit-field does not immediately follow another bit-field,
7175 and yet it starts in the middle of a byte, we have failed to
7176 comply with the ABI. */
7177 if (warn_abi
7178 && DECL_C_BIT_FIELD (field)
7179 /* The TREE_NO_WARNING flag gets set by Objective-C when
7180 laying out an Objective-C class. The ObjC ABI differs
7181 from the C++ ABI, and so we do not want a warning
7182 here. */
7183 && !warning_suppressed_p (field, OPT_Wabi)
7184 && !last_field_was_bitfield
7185 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
7186 DECL_FIELD_BIT_OFFSET (field),
7187 bitsize_unit_node)))
7188 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
7189 "offset of %qD is not ABI-compliant and may "
7190 "change in a future version of GCC", field);
7191
7192 /* The middle end uses the type of expressions to determine the
7193 possible range of expression values. In order to optimize
7194 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
7195 must be made aware of the width of "i", via its type.
7196
7197 Because C++ does not have integer types of arbitrary width,
7198 we must (for the purposes of the front end) convert from the
7199 type assigned here to the declared type of the bitfield
7200 whenever a bitfield expression is used as an rvalue.
7201 Similarly, when assigning a value to a bitfield, the value
7202 must be converted to the type given the bitfield here. */
7203 if (DECL_C_BIT_FIELD (field))
7204 {
7205 unsigned HOST_WIDE_INT width;
7206 tree ftype = TREE_TYPE (field);
7207 width = tree_to_uhwi (DECL_SIZE (field));
7208 if (width != TYPE_PRECISION (ftype))
7209 {
7210 TREE_TYPE (field)
7211 = c_build_bitfield_integer_type (width,
7212 TYPE_UNSIGNED (ftype));
7213 TREE_TYPE (field)
7214 = cp_build_qualified_type (TREE_TYPE (field),
7215 cp_type_quals (ftype));
7216 }
7217 }
7218
7219 /* If we needed additional padding after this field, add it
7220 now. */
7221 if (padding)
7222 {
7223 tree padding_field;
7224
7225 padding_field = build_decl (input_location,
7226 FIELD_DECL,
7227 NULL_TREE,
7228 char_type_node);
7229 DECL_BIT_FIELD (padding_field) = 1;
7230 DECL_SIZE (padding_field) = padding;
7231 DECL_CONTEXT (padding_field) = t;
7232 DECL_ARTIFICIAL (padding_field) = 1;
7233 DECL_IGNORED_P (padding_field) = 1;
7234 DECL_PADDING_P (padding_field) = 1;
7235 layout_nonempty_base_or_field (rli, padding_field,
7236 NULL_TREE,
7237 empty_base_offsets);
7238 }
7239
7240 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
7241 }
7242
7243 if (!integer_zerop (rli->bitpos))
7244 {
7245 /* Make sure that we are on a byte boundary so that the size of
7246 the class without virtual bases will always be a round number
7247 of bytes. */
7248 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
7249 normalize_rli (rli);
7250 }
7251
7252 /* We used to remove zero width bitfields at this point since PR42217,
7253 while the C FE never did that. That caused ABI differences on various
7254 targets. Set the DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD flag on them
7255 instead, so that the backends can emit -Wpsabi warnings in the cases
7256 where the ABI changed. */
7257 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7258 {
7259 if (TREE_CODE (field) == FIELD_DECL
7260 && DECL_C_BIT_FIELD (field)
7261 /* We should not be confused by the fact that grokbitfield
7262 temporarily sets the width of the bit field into
7263 DECL_BIT_FIELD_REPRESENTATIVE (field).
7264 check_bitfield_decl eventually sets DECL_SIZE (field)
7265 to that width. */
7266 && (DECL_SIZE (field) == NULL_TREE
7267 || integer_zerop (DECL_SIZE (field)))
7268 && TREE_TYPE (field) != error_mark_node)
7269 SET_DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field, 1);
7270 check_non_pod_aggregate (field);
7271 }
7272
7273 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
7274 {
7275 /* T needs a different layout as a base (eliding virtual bases
7276 or whatever). Create that version. */
7277 tree base_t = make_node (TREE_CODE (t));
7278 tree base_d = create_implicit_typedef (as_base_identifier, base_t);
7279
7280 TYPE_CONTEXT (base_t) = t;
7281 DECL_CONTEXT (base_d) = t;
7282
7283 set_instantiating_module (base_d);
7284
7285 /* If the ABI version is not at least two, and the last
7286 field was a bit-field, RLI may not be on a byte
7287 boundary. In particular, rli_size_unit_so_far might
7288 indicate the last complete byte, while rli_size_so_far
7289 indicates the total number of bits used. Therefore,
7290 rli_size_so_far, rather than rli_size_unit_so_far, is
7291 used to compute TYPE_SIZE_UNIT. */
7292
7293 /* Set the size and alignment for the new type. */
7294 tree eoc = end_of_class (t, eoc_nvsize);
7295 TYPE_SIZE_UNIT (base_t)
7296 = size_binop (MAX_EXPR,
7297 fold_convert (sizetype,
7298 size_binop (CEIL_DIV_EXPR,
7299 rli_size_so_far (rli),
7300 bitsize_int (BITS_PER_UNIT))),
7301 eoc);
7302 TYPE_SIZE (base_t)
7303 = size_binop (MAX_EXPR,
7304 rli_size_so_far (rli),
7305 size_binop (MULT_EXPR,
7306 fold_convert (bitsizetype, eoc),
7307 bitsize_int (BITS_PER_UNIT)));
7308 SET_TYPE_ALIGN (base_t, rli->record_align);
7309 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
7310 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
7311 TYPE_CXX_ODR_P (base_t) = TYPE_CXX_ODR_P (t);
7312
7313 /* Copy the non-static data members of T. This will include its
7314 direct non-virtual bases & vtable. */
7315 next_field = &TYPE_FIELDS (base_t);
7316 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7317 if (TREE_CODE (field) == FIELD_DECL)
7318 {
7319 *next_field = copy_node (field);
7320 /* Zap any NSDMI, it's not needed and might be a deferred
7321 parse. */
7322 DECL_INITIAL (*next_field) = NULL_TREE;
7323 DECL_CONTEXT (*next_field) = base_t;
7324 next_field = &DECL_CHAIN (*next_field);
7325 }
7326 *next_field = NULL_TREE;
7327
7328 /* We use the base type for trivial assignments, and hence it
7329 needs a mode. */
7330 compute_record_mode (base_t);
7331
7332 /* Record the base version of the type. */
7333 CLASSTYPE_AS_BASE (t) = base_t;
7334 }
7335 else
7336 CLASSTYPE_AS_BASE (t) = t;
7337
7338 /* Every empty class contains an empty class. */
7339 if (CLASSTYPE_EMPTY_P (t))
7340 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
7341
7342 /* Set the TYPE_DECL for this type to contain the right
7343 value for DECL_OFFSET, so that we can use it as part
7344 of a COMPONENT_REF for multiple inheritance. */
7345 layout_decl (TYPE_MAIN_DECL (t), 0);
7346
7347 /* Now fix up any virtual base class types that we left lying
7348 around. We must get these done before we try to lay out the
7349 virtual function table. As a side-effect, this will remove the
7350 base subobject fields. */
7351 layout_virtual_bases (rli, empty_base_offsets);
7352
7353 /* Make sure that empty classes are reflected in RLI at this
7354 point. */
7355 include_empty_classes (rli);
7356
7357 /* Make sure not to create any structures with zero size. */
7358 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
7359 place_field (rli,
7360 build_decl (input_location,
7361 FIELD_DECL, NULL_TREE, char_type_node));
7362
7363 /* If this is a non-POD, declaring it packed makes a difference to how it
7364 can be used as a field; don't let finalize_record_size undo it. */
7365 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
7366 rli->packed_maybe_necessary = true;
7367
7368 /* Let the back end lay out the type. */
7369 finish_record_layout (rli, /*free_p=*/true);
7370
7371 /* If we didn't end up needing an as-base type, don't use it. */
7372 if (CLASSTYPE_AS_BASE (t) != t
7373 /* If T's CLASSTYPE_AS_BASE is TYPE_USER_ALIGN, but T is not,
7374 replacing the as-base type would change CLASSTYPE_USER_ALIGN,
7375 causing us to lose the user-specified alignment as in PR94050. */
7376 && TYPE_USER_ALIGN (t) == TYPE_USER_ALIGN (CLASSTYPE_AS_BASE (t))
7377 && tree_int_cst_equal (TYPE_SIZE (t),
7378 TYPE_SIZE (CLASSTYPE_AS_BASE (t))))
7379 CLASSTYPE_AS_BASE (t) = t;
7380
7381 if (TYPE_SIZE_UNIT (t)
7382 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7383 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7384 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7385 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
7386
7387 /* Warn about bases that can't be talked about due to ambiguity. */
7388 maybe_warn_about_inaccessible_bases (t);
7389
7390 /* Now that we're done with layout, give the base fields the real types. */
7391 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
7392 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
7393 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
7394
7395 /* Clean up. */
7396 splay_tree_delete (empty_base_offsets);
7397
7398 if (CLASSTYPE_EMPTY_P (t)
7399 && tree_int_cst_lt (sizeof_biggest_empty_class,
7400 TYPE_SIZE_UNIT (t)))
7401 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
7402 }
7403
7404 /* Determine the "key method" for the class type indicated by TYPE,
7405 and set CLASSTYPE_KEY_METHOD accordingly. */
7406
7407 void
7408 determine_key_method (tree type)
7409 {
7410 tree method;
7411
7412 if (processing_template_decl
7413 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
7414 || CLASSTYPE_INTERFACE_KNOWN (type))
7415 return;
7416
7417 /* The key method is the first non-pure virtual function that is not
7418 inline at the point of class definition. On some targets the
7419 key function may not be inline; those targets should not call
7420 this function until the end of the translation unit. */
7421 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
7422 if (TREE_CODE (method) == FUNCTION_DECL
7423 && DECL_VINDEX (method) != NULL_TREE
7424 && ! DECL_DECLARED_INLINE_P (method)
7425 && ! DECL_PURE_VIRTUAL_P (method))
7426 {
7427 CLASSTYPE_KEY_METHOD (type) = method;
7428 break;
7429 }
7430
7431 return;
7432 }
7433
7434 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
7435 class data member of non-zero size, otherwise false. */
7436
7437 static inline bool
7438 field_nonempty_p (const_tree fld)
7439 {
7440 if (TREE_CODE (fld) == ERROR_MARK)
7441 return false;
7442
7443 tree type = TREE_TYPE (fld);
7444 if (TREE_CODE (fld) == FIELD_DECL
7445 && TREE_CODE (type) != ERROR_MARK
7446 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
7447 return TYPE_SIZE (type) && !integer_zerop (TYPE_SIZE (type));
7448
7449 return false;
7450 }
7451
7452 /* Used by find_flexarrays and related functions. */
7453
7454 struct flexmems_t
7455 {
7456 /* The first flexible array member or non-zero array member found
7457 in the order of layout. */
7458 tree array;
7459 /* True if there is a non-static non-empty data member in the class or
7460 its bases. */
7461 bool first;
7462 /* The first non-static non-empty data member following either
7463 the flexible array member, if found, or the zero-length array member
7464 otherwise. AFTER[1] refers to the first such data member of a union
7465 of which the struct containing the flexible array member or zero-length
7466 array is a member, or NULL when no such union exists. This element is
7467 only used during searching, not for diagnosing problems. AFTER[0]
7468 refers to the first such data member that is not a member of such
7469 a union. */
7470 tree after[2];
7471
7472 /* Refers to a struct (not union) in which the struct of which the flexible
7473 array is member is defined. Used to diagnose strictly (according to C)
7474 invalid uses of the latter structs. */
7475 tree enclosing;
7476 };
7477
7478 /* Find either the first flexible array member or the first zero-length
7479 array, in that order of preference, among members of class T (but not
7480 its base classes), and set members of FMEM accordingly.
7481 BASE_P is true if T is a base class of another class.
7482 PUN is true when inside of a union (perhaps recursively).
7483 PSTR is set to a data member of the outermost struct of
7484 which the flexible array is a member if one such struct exists,
7485 otherwise to NULL. NESTED_P is true for recursive calls except ones
7486 handling anonymous aggregates. Those types are expected to be diagnosed
7487 on its own already and so only the last member is checked vs. what
7488 follows it in the outer type. */
7489
7490 static void
7491 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
7492 bool nested_p /* = false */, bool pun /* = false */,
7493 tree pstr /* = NULL_TREE */)
7494 {
7495 if (TREE_CODE (t) == UNION_TYPE)
7496 pun = true;
7497
7498 tree fld = TYPE_FIELDS (t);
7499 if ((base_p || nested_p) && TREE_CODE (t) == RECORD_TYPE)
7500 {
7501 /* In bases or in nested structures, only process the last
7502 non-static data member. If we have say
7503 struct A { int a; int b[]; int c; };
7504 struct B { int d; int e[]; int f; };
7505 struct C : A { int g; B h, i; int j; };
7506 then the A::b followed by A::c should have been diagnosed
7507 already when completing struct A, and B::e followed by B::f
7508 when completing struct B, so no need to repeat that when completing
7509 struct C. So, only look at the last member so we cover e.g.
7510 struct D { int k; int l[]; };
7511 struct E : D { int m; };
7512 struct F { D n; int o; };
7513 where flexible array member at the end of D is fine, but it isn't
7514 correct that E::m in E or F::o in F follows it. */
7515 tree last_fld = NULL_TREE;
7516 for (; (fld = next_subobject_field (fld)); fld = DECL_CHAIN (fld))
7517 if (DECL_ARTIFICIAL (fld))
7518 continue;
7519 else if (TREE_TYPE (fld) == error_mark_node)
7520 return;
7521 else if (TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE
7522 || field_nonempty_p (fld))
7523 last_fld = fld;
7524 fld = last_fld;
7525 }
7526 for (; fld; fld = DECL_CHAIN (fld))
7527 {
7528 if (fld == error_mark_node)
7529 return;
7530
7531 /* Is FLD a typedef for an anonymous struct? */
7532
7533 /* FIXME: Note that typedefs (as well as arrays) need to be fully
7534 handled elsewhere so that errors like the following are detected
7535 as well:
7536 typedef struct { int i, a[], j; } S; // bug c++/72753
7537 S s [2]; // bug c++/68489
7538 */
7539 if (TREE_CODE (fld) == TYPE_DECL
7540 && DECL_IMPLICIT_TYPEDEF_P (fld)
7541 && CLASS_TYPE_P (TREE_TYPE (fld))
7542 && IDENTIFIER_ANON_P (DECL_NAME (fld)))
7543 {
7544 /* Check the nested unnamed type referenced via a typedef
7545 independently of FMEM (since it's not a data member of
7546 the enclosing class). */
7547 check_flexarrays (TREE_TYPE (fld));
7548 continue;
7549 }
7550
7551 /* Skip anything that's GCC-generated or not a (non-static) data
7552 member. */
7553 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
7554 continue;
7555
7556 /* Type of the member. */
7557 tree fldtype = TREE_TYPE (fld);
7558 if (fldtype == error_mark_node)
7559 return;
7560
7561 /* Determine the type of the array element or object referenced
7562 by the member so that it can be checked for flexible array
7563 members if it hasn't been yet. */
7564 tree eltype = fldtype;
7565 while (TREE_CODE (eltype) == ARRAY_TYPE
7566 || INDIRECT_TYPE_P (eltype))
7567 eltype = TREE_TYPE (eltype);
7568
7569 if (RECORD_OR_UNION_TYPE_P (eltype))
7570 {
7571 if (fmem->array && !fmem->after[pun])
7572 {
7573 /* Once the member after the flexible array has been found
7574 we're done. */
7575 fmem->after[pun] = fld;
7576 break;
7577 }
7578
7579 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
7580 {
7581 /* Descend into the non-static member struct or union and try
7582 to find a flexible array member or zero-length array among
7583 its members. This is only necessary for anonymous types
7584 and types in whose context the current type T has not been
7585 defined (the latter must not be checked again because they
7586 are already in the process of being checked by one of the
7587 recursive calls). */
7588
7589 bool first = fmem->first;
7590 tree array = fmem->array;
7591 bool maybe_anon_p = TYPE_UNNAMED_P (eltype);
7592 if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (eltype) : NULL_TREE)
7593 maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7594
7595 /* If this member isn't anonymous and a prior non-flexible array
7596 member has been seen in one of the enclosing structs, clear
7597 the FIRST member since it doesn't contribute to the flexible
7598 array struct's members. */
7599 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7600 fmem->first = false;
7601
7602 find_flexarrays (eltype, fmem, false, !maybe_anon_p, pun,
7603 !pstr && TREE_CODE (t) == RECORD_TYPE
7604 ? fld : pstr);
7605
7606 if (fmem->array != array)
7607 {
7608 /* If the recursive call passed true to nested_p,
7609 it only looked at the last field and we do not
7610 want to diagnose in that case the "in otherwise empty"
7611 case, just if it is followed by some other non-empty
7612 member. So set fmem->first. */
7613 if (!maybe_anon_p)
7614 fmem->first = true;
7615 continue;
7616 }
7617
7618 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
7619 /* Restore the FIRST member reset above if no flexible
7620 array member has been found in this member's struct. */
7621 fmem->first = first;
7622
7623 /* If the member struct contains the first flexible array
7624 member, or if this member is a base class, continue to
7625 the next member and avoid setting the FMEM->AFTER pointer
7626 to point to it. */
7627 if (base_p)
7628 continue;
7629 }
7630 }
7631
7632 if (field_nonempty_p (fld))
7633 {
7634 /* Remember the first non-static data member. */
7635 if (!fmem->first)
7636 fmem->first = true;
7637
7638 /* Remember the first non-static data member after the flexible
7639 array member, if one has been found, or the zero-length array
7640 if it has been found. */
7641 if (fmem->array && !fmem->after[pun])
7642 fmem->after[pun] = fld;
7643 }
7644
7645 /* Skip non-arrays. */
7646 if (TREE_CODE (fldtype) != ARRAY_TYPE)
7647 continue;
7648
7649 /* Determine the upper bound of the array if it has one. */
7650 if (TYPE_DOMAIN (fldtype))
7651 {
7652 if (fmem->array)
7653 {
7654 /* Make a record of the zero-length array if either one
7655 such field or a flexible array member has been seen to
7656 handle the pathological and unlikely case of multiple
7657 such members. */
7658 if (!fmem->after[pun])
7659 fmem->after[pun] = fld;
7660 }
7661 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7662 {
7663 /* Remember the first zero-length array unless a flexible array
7664 member has already been seen. */
7665 fmem->array = fld;
7666 fmem->enclosing = pstr;
7667 }
7668 }
7669 else
7670 {
7671 /* Flexible array members have no upper bound. */
7672 if (fmem->array)
7673 {
7674 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7675 {
7676 /* Replace the zero-length array if it's been stored and
7677 reset the after pointer. */
7678 fmem->after[pun] = NULL_TREE;
7679 fmem->array = fld;
7680 fmem->enclosing = pstr;
7681 }
7682 else if (!fmem->after[pun])
7683 /* Make a record of another flexible array member. */
7684 fmem->after[pun] = fld;
7685 }
7686 else
7687 {
7688 fmem->array = fld;
7689 fmem->enclosing = pstr;
7690 }
7691 }
7692 }
7693 }
7694
7695 /* Diagnose a strictly (by the C standard) invalid use of a struct with
7696 a flexible array member (or the zero-length array extension). */
7697
7698 static void
7699 diagnose_invalid_flexarray (const flexmems_t *fmem)
7700 {
7701 if (fmem->array && fmem->enclosing)
7702 {
7703 auto_diagnostic_group d;
7704 if (pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
7705 TYPE_DOMAIN (TREE_TYPE (fmem->array))
7706 ? G_("invalid use of %q#T with a zero-size array in %q#D")
7707 : G_("invalid use of %q#T with a flexible array member "
7708 "in %q#T"),
7709 DECL_CONTEXT (fmem->array),
7710 DECL_CONTEXT (fmem->enclosing)))
7711 inform (DECL_SOURCE_LOCATION (fmem->array),
7712 "array member %q#D declared here", fmem->array);
7713 }
7714 }
7715
7716 /* Issue diagnostics for invalid flexible array members or zero-length
7717 arrays that are not the last elements of the containing class or its
7718 base classes or that are its sole members. */
7719
7720 static void
7721 diagnose_flexarrays (tree t, const flexmems_t *fmem)
7722 {
7723 if (!fmem->array)
7724 return;
7725
7726 if (fmem->first && !fmem->after[0])
7727 {
7728 diagnose_invalid_flexarray (fmem);
7729 return;
7730 }
7731
7732 /* Has a diagnostic been issued? */
7733 bool diagd = false;
7734
7735 const char *msg = 0;
7736 const char *msg_fam = 0;
7737
7738 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7739 {
7740 if (fmem->after[0])
7741 msg = G_("zero-size array member %qD not at end of %q#T");
7742 else if (!fmem->first)
7743 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7744
7745 if (msg)
7746 {
7747 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7748
7749 auto_diagnostic_group d;
7750 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7751 {
7752 inform (location_of (t), "in the definition of %q#T", t);
7753 diagd = true;
7754 }
7755 }
7756 }
7757 else
7758 {
7759 if (fmem->after[0])
7760 msg = G_("flexible array member %qD not at end of %q#T");
7761 else if (!fmem->first)
7762 msg_fam = G_("flexible array member %qD in an otherwise"
7763 " empty %q#T is a GCC extension");
7764
7765 if (msg || msg_fam)
7766 {
7767 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7768 diagd = true;
7769
7770 auto_diagnostic_group d;
7771 if (msg)
7772 error_at (loc, msg, fmem->array, t);
7773 else
7774 pedwarn (loc, OPT_Wpedantic, msg_fam, fmem->array, t);
7775
7776 /* In the unlikely event that the member following the flexible
7777 array member is declared in a different class, or the member
7778 overlaps another member of a common union, point to it.
7779 Otherwise it should be obvious. */
7780 if (fmem->after[0]
7781 && (DECL_CONTEXT (fmem->after[0])
7782 != DECL_CONTEXT (fmem->array)))
7783 {
7784 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7785 "next member %q#D declared here",
7786 fmem->after[0]);
7787 inform (location_of (t), "in the definition of %q#T", t);
7788 }
7789 }
7790 }
7791
7792 if (!diagd && fmem->array && fmem->enclosing)
7793 diagnose_invalid_flexarray (fmem);
7794 }
7795
7796
7797 /* Recursively check to make sure that any flexible array or zero-length
7798 array members of class T or its bases are valid (i.e., not the sole
7799 non-static data member of T and, if one exists, that it is the last
7800 non-static data member of T and its base classes. FMEM is expected
7801 to be initially null and is used internally by recursive calls to
7802 the function. Issue the appropriate diagnostics for the array member
7803 that fails the checks. */
7804
7805 static void
7806 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7807 bool base_p /* = false */)
7808 {
7809 /* Initialize the result of a search for flexible array and zero-length
7810 array members. Avoid doing any work if the most interesting FMEM data
7811 have already been populated. */
7812 flexmems_t flexmems = flexmems_t ();
7813 if (!fmem)
7814 fmem = &flexmems;
7815 else if (fmem->array && fmem->first && fmem->after[0])
7816 return;
7817
7818 tree fam = fmem->array;
7819
7820 /* Recursively check the primary base class first. */
7821 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7822 {
7823 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7824 check_flexarrays (basetype, fmem, true);
7825 }
7826
7827 /* Recursively check the base classes. */
7828 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7829 for (int i = 0; i < nbases; ++i)
7830 {
7831 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7832
7833 /* The primary base class was already checked above. */
7834 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7835 continue;
7836
7837 /* Virtual base classes are at the end. */
7838 if (BINFO_VIRTUAL_P (base_binfo))
7839 continue;
7840
7841 /* Check the base class. */
7842 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7843 }
7844
7845 if (fmem == &flexmems)
7846 {
7847 /* Check virtual base classes only once per derived class.
7848 I.e., this check is not performed recursively for base
7849 classes. */
7850 int i;
7851 tree base_binfo;
7852 vec<tree, va_gc> *vbases;
7853 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7854 vec_safe_iterate (vbases, i, &base_binfo); i++)
7855 {
7856 /* Check the virtual base class. */
7857 tree basetype = TREE_TYPE (base_binfo);
7858
7859 check_flexarrays (basetype, fmem, /*base_p=*/true);
7860 }
7861 }
7862
7863 /* Is the type unnamed (and therefore a member of it potentially
7864 an anonymous struct or union)? */
7865 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7866 if (tree ctx = maybe_anon_p ? TYPE_CONTEXT (t) : NULL_TREE)
7867 maybe_anon_p = RECORD_OR_UNION_TYPE_P (ctx);
7868
7869 /* Search the members of the current (possibly derived) class, skipping
7870 unnamed structs and unions since those could be anonymous. */
7871 if (fmem != &flexmems || !maybe_anon_p)
7872 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7873
7874 if (fmem == &flexmems && !maybe_anon_p)
7875 /* Issue diagnostics for invalid flexible and zero-length array
7876 members found in base classes or among the members of the current
7877 class. Ignore anonymous structs and unions whose members are
7878 considered to be members of the enclosing class and thus will
7879 be diagnosed when checking it. */
7880 diagnose_flexarrays (t, fmem);
7881 }
7882
7883 /* Perform processing required when the definition of T (a class type)
7884 is complete. Diagnose invalid definitions of flexible array members
7885 and zero-size arrays. */
7886
7887 void
7888 finish_struct_1 (tree t)
7889 {
7890 tree x;
7891 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7892 tree virtuals = NULL_TREE;
7893
7894 if (COMPLETE_TYPE_P (t))
7895 {
7896 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7897 error ("redefinition of %q#T", t);
7898 popclass ();
7899 return;
7900 }
7901
7902 /* If this type was previously laid out as a forward reference,
7903 make sure we lay it out again. */
7904 TYPE_SIZE (t) = NULL_TREE;
7905 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7906
7907 /* Make assumptions about the class; we'll reset the flags if
7908 necessary. */
7909 CLASSTYPE_EMPTY_P (t) = 1;
7910 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7911 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7912 CLASSTYPE_LITERAL_P (t) = true;
7913
7914 /* Do end-of-class semantic processing: checking the validity of the
7915 bases and members and add implicitly generated methods. */
7916 check_bases_and_members (t);
7917
7918 /* Find the key method. */
7919 if (TYPE_CONTAINS_VPTR_P (t))
7920 {
7921 /* The Itanium C++ ABI permits the key method to be chosen when
7922 the class is defined -- even though the key method so
7923 selected may later turn out to be an inline function. On
7924 some systems (such as ARM Symbian OS) the key method cannot
7925 be determined until the end of the translation unit. On such
7926 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7927 will cause the class to be added to KEYED_CLASSES. Then, in
7928 finish_file we will determine the key method. */
7929 if (targetm.cxx.key_method_may_be_inline ())
7930 determine_key_method (t);
7931
7932 /* If a polymorphic class has no key method, we may emit the vtable
7933 in every translation unit where the class definition appears. If
7934 we're devirtualizing, we can look into the vtable even if we
7935 aren't emitting it.
7936
7937 Additionally, if the class is attached to a named module, make sure
7938 to always emit the vtable in this TU. */
7939 if (!CLASSTYPE_KEY_METHOD (t) || module_attach_p ())
7940 vec_safe_push (keyed_classes, t);
7941 }
7942
7943 /* Layout the class itself. */
7944 layout_class_type (t, &virtuals);
7945 /* COMPLETE_TYPE_P is now true. */
7946
7947 set_class_bindings (t);
7948
7949 /* With the layout complete, check for flexible array members and
7950 zero-length arrays that might overlap other members in the final
7951 layout. */
7952 check_flexarrays (t);
7953
7954 virtuals = modify_all_vtables (t, nreverse (virtuals));
7955
7956 /* If necessary, create the primary vtable for this class. */
7957 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7958 {
7959 /* We must enter these virtuals into the table. */
7960 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7961 build_primary_vtable (NULL_TREE, t);
7962 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7963 /* Here we know enough to change the type of our virtual
7964 function table, but we will wait until later this function. */
7965 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7966
7967 /* If we're warning about ABI tags, check the types of the new
7968 virtual functions. */
7969 if (warn_abi_tag)
7970 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7971 check_abi_tags (t, TREE_VALUE (v));
7972 }
7973
7974 if (TYPE_CONTAINS_VPTR_P (t))
7975 {
7976 int vindex;
7977 tree fn;
7978
7979 if (BINFO_VTABLE (TYPE_BINFO (t)))
7980 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7981 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7982 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7983
7984 /* Add entries for virtual functions introduced by this class. */
7985 BINFO_VIRTUALS (TYPE_BINFO (t))
7986 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7987
7988 /* Set DECL_VINDEX for all functions declared in this class. */
7989 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7990 fn;
7991 fn = TREE_CHAIN (fn),
7992 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7993 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7994 {
7995 tree fndecl = BV_FN (fn);
7996
7997 if (DECL_THUNK_P (fndecl))
7998 /* A thunk. We should never be calling this entry directly
7999 from this vtable -- we'd use the entry for the non
8000 thunk base function. */
8001 DECL_VINDEX (fndecl) = NULL_TREE;
8002 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
8003 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
8004 }
8005 }
8006
8007 finish_struct_bits (t);
8008
8009 set_method_tm_attributes (t);
8010 if (flag_openmp || flag_openmp_simd)
8011 finish_omp_declare_simd_methods (t);
8012
8013 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
8014 for any static member objects of the type we're working on. */
8015 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8016 if (DECL_DECLARES_FUNCTION_P (x))
8017 {
8018 /* Synthesize constexpr defaulted comparisons. */
8019 if (!DECL_ARTIFICIAL (x)
8020 && DECL_DEFAULTED_IN_CLASS_P (x)
8021 && special_function_p (x) == sfk_comparison)
8022 defaulted_late_check (x);
8023 DECL_IN_AGGR_P (x) = false;
8024 }
8025 else if (VAR_P (x) && TREE_STATIC (x)
8026 && TREE_TYPE (x) != error_mark_node
8027 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
8028 SET_DECL_MODE (x, TYPE_MODE (t));
8029
8030 /* Complain if one of the field types requires lower visibility. */
8031 constrain_class_visibility (t);
8032
8033 /* Make the rtl for any new vtables we have created, and unmark
8034 the base types we marked. */
8035 finish_vtbls (t);
8036
8037 /* Build the VTT for T. */
8038 build_vtt (t);
8039
8040 if (warn_nonvdtor
8041 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
8042 && !CLASSTYPE_FINAL (t))
8043 warning (OPT_Wnon_virtual_dtor,
8044 "%q#T has virtual functions and accessible"
8045 " non-virtual destructor", t);
8046
8047 complete_vars (t);
8048
8049 if (warn_overloaded_virtual)
8050 warn_hidden (t);
8051
8052 /* Class layout, assignment of virtual table slots, etc., is now
8053 complete. Give the back end a chance to tweak the visibility of
8054 the class or perform any other required target modifications. */
8055 targetm.cxx.adjust_class_at_definition (t);
8056
8057 maybe_suppress_debug_info (t);
8058
8059 if (flag_vtable_verify)
8060 vtv_save_class_info (t);
8061
8062 dump_class_hierarchy (t);
8063
8064 /* Finish debugging output for this type. */
8065 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
8066
8067 if (TYPE_TRANSPARENT_AGGR (t))
8068 {
8069 tree field = first_field (t);
8070 if (field == NULL_TREE || error_operand_p (field))
8071 {
8072 error ("type transparent %q#T does not have any fields", t);
8073 TYPE_TRANSPARENT_AGGR (t) = 0;
8074 }
8075 else if (DECL_ARTIFICIAL (field))
8076 {
8077 if (DECL_FIELD_IS_BASE (field))
8078 error ("type transparent class %qT has base classes", t);
8079 else
8080 {
8081 gcc_checking_assert (DECL_VIRTUAL_P (field));
8082 error ("type transparent class %qT has virtual functions", t);
8083 }
8084 TYPE_TRANSPARENT_AGGR (t) = 0;
8085 }
8086 else if (TYPE_MODE (t) != DECL_MODE (field))
8087 {
8088 error ("type transparent %q#T cannot be made transparent because "
8089 "the type of the first field has a different ABI from the "
8090 "class overall", t);
8091 TYPE_TRANSPARENT_AGGR (t) = 0;
8092 }
8093 }
8094 }
8095
8096 /* When T was built up, the member declarations were added in reverse
8097 order. Rearrange them to declaration order. */
8098
8099 void
8100 unreverse_member_declarations (tree t)
8101 {
8102 tree next;
8103 tree prev;
8104 tree x;
8105
8106 /* The following lists are all in reverse order. Put them in
8107 declaration order now. */
8108 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
8109
8110 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
8111 order, so we can't just use nreverse. Due to stat_hack
8112 chicanery in finish_member_declaration. */
8113 prev = NULL_TREE;
8114 for (x = TYPE_FIELDS (t);
8115 x && TREE_CODE (x) != TYPE_DECL;
8116 x = next)
8117 {
8118 next = DECL_CHAIN (x);
8119 DECL_CHAIN (x) = prev;
8120 prev = x;
8121 }
8122
8123 if (prev)
8124 {
8125 DECL_CHAIN (TYPE_FIELDS (t)) = x;
8126 TYPE_FIELDS (t) = prev;
8127 }
8128 }
8129
8130 /* Classes, structs or unions T marked with hotness attributes propagate
8131 the attribute to all methods. */
8132
8133 void
8134 propagate_class_warmth_attribute (tree t)
8135 {
8136 if (t == NULL_TREE
8137 || !(TREE_CODE (t) == RECORD_TYPE
8138 || TREE_CODE (t) == UNION_TYPE))
8139 return;
8140
8141 tree class_has_cold_attr
8142 = lookup_attribute ("cold", TYPE_ATTRIBUTES (t));
8143 tree class_has_hot_attr
8144 = lookup_attribute ("hot", TYPE_ATTRIBUTES (t));
8145
8146 if (class_has_cold_attr || class_has_hot_attr)
8147 for (tree f = TYPE_FIELDS (t); f; f = DECL_CHAIN (f))
8148 if (DECL_DECLARES_FUNCTION_P (f))
8149 maybe_propagate_warmth_attributes (STRIP_TEMPLATE (f), t);
8150 }
8151
8152 tree
8153 finish_struct (tree t, tree attributes)
8154 {
8155 location_t saved_loc = input_location;
8156
8157 /* Now that we've got all the field declarations, reverse everything
8158 as necessary. */
8159 unreverse_member_declarations (t);
8160
8161 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8162 fixup_attribute_variants (t);
8163
8164 /* Nadger the current location so that diagnostics point to the start of
8165 the struct, not the end. */
8166 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
8167
8168 if (processing_template_decl)
8169 {
8170 tree x;
8171
8172 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8173 if (DECL_DECLARES_FUNCTION_P (x))
8174 {
8175 DECL_IN_AGGR_P (x) = false;
8176 if (DECL_VIRTUAL_P (x))
8177 CLASSTYPE_NON_AGGREGATE (t) = true;
8178 }
8179 else if (TREE_CODE (x) == FIELD_DECL)
8180 {
8181 if (TREE_PROTECTED (x) || TREE_PRIVATE (x))
8182 CLASSTYPE_NON_AGGREGATE (t) = true;
8183 }
8184
8185 /* Also add a USING_DECL for operator=. We know there'll be (at
8186 least) one, but we don't know the signature(s). We want name
8187 lookup not to fail or recurse into bases. This isn't added
8188 to the template decl list so we drop this at instantiation
8189 time. */
8190 tree ass_op = build_lang_decl (USING_DECL, assign_op_identifier,
8191 NULL_TREE);
8192 DECL_CONTEXT (ass_op) = t;
8193 USING_DECL_SCOPE (ass_op) = t;
8194 DECL_DEPENDENT_P (ass_op) = true;
8195 DECL_ARTIFICIAL (ass_op) = true;
8196 DECL_CHAIN (ass_op) = TYPE_FIELDS (t);
8197 TYPE_FIELDS (t) = ass_op;
8198
8199 TYPE_SIZE (t) = bitsize_zero_node;
8200 TYPE_SIZE_UNIT (t) = size_zero_node;
8201 /* COMPLETE_TYPE_P is now true. */
8202
8203 set_class_bindings (t);
8204
8205 /* We need to emit an error message if this type was used as a parameter
8206 and it is an abstract type, even if it is a template. We construct
8207 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
8208 account and we call complete_vars with this type, which will check
8209 the PARM_DECLS. Note that while the type is being defined,
8210 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
8211 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
8212 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
8213 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
8214 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
8215 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
8216 complete_vars (t);
8217
8218 /* Remember current #pragma pack value. */
8219 TYPE_PRECISION (t) = maximum_field_alignment;
8220
8221 if (cxx_dialect < cxx20)
8222 {
8223 if (!CLASSTYPE_NON_AGGREGATE (t)
8224 && type_has_user_provided_or_explicit_constructor (t))
8225 CLASSTYPE_NON_AGGREGATE (t) = 1;
8226 }
8227 else if (TYPE_HAS_USER_CONSTRUCTOR (t))
8228 CLASSTYPE_NON_AGGREGATE (t) = 1;
8229
8230 /* Fix up any variants we've already built. */
8231 fixup_type_variants (t);
8232 }
8233 else
8234 finish_struct_1 (t);
8235 /* COMPLETE_TYPE_P is now true. */
8236
8237 maybe_warn_about_overly_private_class (t);
8238
8239 if (is_std_init_list (t))
8240 {
8241 /* People keep complaining that the compiler crashes on an invalid
8242 definition of initializer_list, so I guess we should explicitly
8243 reject it. What the compiler internals care about is that it's a
8244 template and has a pointer field followed by size_type field. */
8245 bool ok = false;
8246 if (processing_template_decl)
8247 {
8248 tree f = next_aggregate_field (TYPE_FIELDS (t));
8249 if (f && TYPE_PTR_P (TREE_TYPE (f)))
8250 {
8251 f = next_aggregate_field (DECL_CHAIN (f));
8252 if (f && same_type_p (TREE_TYPE (f), size_type_node))
8253 ok = true;
8254 }
8255 }
8256 /* It also cannot be a union. */
8257 ok &= NON_UNION_CLASS_TYPE_P (t);
8258 if (!ok)
8259 fatal_error (input_location, "definition of %qD does not match "
8260 "%<#include <initializer_list>%>", TYPE_NAME (t));
8261 }
8262
8263 input_location = saved_loc;
8264
8265 TYPE_BEING_DEFINED (t) = 0;
8266
8267 if (current_class_type)
8268 popclass ();
8269 else
8270 error ("trying to finish struct, but kicked out due to previous parse errors");
8271
8272 if (flag_openmp)
8273 for (tree decl = TYPE_FIELDS (t); decl; decl = DECL_CHAIN (decl))
8274 if (TREE_CODE (decl) == FUNCTION_DECL
8275 && DECL_OBJECT_MEMBER_FUNCTION_P (decl))
8276 if (tree attr = lookup_attribute ("omp declare variant base",
8277 DECL_ATTRIBUTES (decl)))
8278 omp_declare_variant_finalize (decl, attr);
8279
8280 if (processing_template_decl && at_function_scope_p ()
8281 /* Lambdas are defined by the LAMBDA_EXPR. */
8282 && !LAMBDA_TYPE_P (t))
8283 add_stmt (build_min (TAG_DEFN, t));
8284
8285 return t;
8286 }
8287
8288 /* Hash table to avoid endless recursion when handling references. */
8289 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
8290
8291 /* Return the dynamic type of INSTANCE, if known.
8292 Used to determine whether the virtual function table is needed
8293 or not.
8294
8295 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8296 of our knowledge of its type. *NONNULL should be initialized
8297 before this function is called. */
8298
8299 static tree
8300 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
8301 {
8302 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
8303
8304 switch (TREE_CODE (instance))
8305 {
8306 case INDIRECT_REF:
8307 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
8308 return NULL_TREE;
8309 else
8310 return RECUR (TREE_OPERAND (instance, 0));
8311
8312 case CALL_EXPR:
8313 /* This is a call to a constructor, hence it's never zero. */
8314 if (CALL_EXPR_FN (instance)
8315 && TREE_HAS_CONSTRUCTOR (instance))
8316 {
8317 if (nonnull)
8318 *nonnull = 1;
8319 return TREE_TYPE (instance);
8320 }
8321 return NULL_TREE;
8322
8323 case SAVE_EXPR:
8324 /* This is a call to a constructor, hence it's never zero. */
8325 if (TREE_HAS_CONSTRUCTOR (instance))
8326 {
8327 if (nonnull)
8328 *nonnull = 1;
8329 return TREE_TYPE (instance);
8330 }
8331 return RECUR (TREE_OPERAND (instance, 0));
8332
8333 case POINTER_PLUS_EXPR:
8334 case PLUS_EXPR:
8335 case MINUS_EXPR:
8336 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
8337 return RECUR (TREE_OPERAND (instance, 0));
8338 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
8339 /* Propagate nonnull. */
8340 return RECUR (TREE_OPERAND (instance, 0));
8341
8342 return NULL_TREE;
8343
8344 CASE_CONVERT:
8345 return RECUR (TREE_OPERAND (instance, 0));
8346
8347 case ADDR_EXPR:
8348 instance = TREE_OPERAND (instance, 0);
8349 if (nonnull)
8350 {
8351 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
8352 with a real object -- given &p->f, p can still be null. */
8353 tree t = get_base_address (instance);
8354 /* ??? Probably should check DECL_WEAK here. */
8355 if (t && DECL_P (t))
8356 *nonnull = 1;
8357 }
8358 return RECUR (instance);
8359
8360 case COMPONENT_REF:
8361 /* If this component is really a base class reference, then the field
8362 itself isn't definitive. */
8363 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
8364 return RECUR (TREE_OPERAND (instance, 0));
8365 return RECUR (TREE_OPERAND (instance, 1));
8366
8367 case VAR_DECL:
8368 case FIELD_DECL:
8369 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
8370 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
8371 {
8372 if (nonnull)
8373 *nonnull = 1;
8374 return TREE_TYPE (TREE_TYPE (instance));
8375 }
8376 /* fall through. */
8377 case TARGET_EXPR:
8378 case PARM_DECL:
8379 case RESULT_DECL:
8380 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
8381 {
8382 if (nonnull)
8383 *nonnull = 1;
8384 return TREE_TYPE (instance);
8385 }
8386 else if (instance == current_class_ptr)
8387 {
8388 if (nonnull)
8389 *nonnull = 1;
8390
8391 /* if we're in a ctor or dtor, we know our type. If
8392 current_class_ptr is set but we aren't in a function, we're in
8393 an NSDMI (and therefore a constructor). */
8394 if (current_scope () != current_function_decl
8395 || (DECL_LANG_SPECIFIC (current_function_decl)
8396 && (DECL_CONSTRUCTOR_P (current_function_decl)
8397 || DECL_DESTRUCTOR_P (current_function_decl))))
8398 {
8399 if (cdtorp)
8400 *cdtorp = 1;
8401 return TREE_TYPE (TREE_TYPE (instance));
8402 }
8403 }
8404 else if (TYPE_REF_P (TREE_TYPE (instance)))
8405 {
8406 /* We only need one hash table because it is always left empty. */
8407 if (!fixed_type_or_null_ref_ht)
8408 fixed_type_or_null_ref_ht
8409 = new hash_table<nofree_ptr_hash<tree_node> > (37);
8410
8411 /* Reference variables should be references to objects. */
8412 if (nonnull)
8413 *nonnull = 1;
8414
8415 /* Enter the INSTANCE in a table to prevent recursion; a
8416 variable's initializer may refer to the variable
8417 itself. */
8418 if (VAR_P (instance)
8419 && DECL_INITIAL (instance)
8420 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
8421 && !fixed_type_or_null_ref_ht->find (instance))
8422 {
8423 tree type;
8424 tree_node **slot;
8425
8426 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
8427 *slot = instance;
8428 type = RECUR (DECL_INITIAL (instance));
8429 fixed_type_or_null_ref_ht->remove_elt (instance);
8430
8431 return type;
8432 }
8433 }
8434 return NULL_TREE;
8435
8436 case VIEW_CONVERT_EXPR:
8437 if (location_wrapper_p (instance))
8438 return RECUR (TREE_OPERAND (instance, 0));
8439 else
8440 /* TODO: Recursion may be correct for some non-location-wrapper
8441 uses of VIEW_CONVERT_EXPR. */
8442 return NULL_TREE;
8443
8444 default:
8445 return NULL_TREE;
8446 }
8447 #undef RECUR
8448 }
8449
8450 /* Return nonzero if the dynamic type of INSTANCE is known, and
8451 equivalent to the static type. We also handle the case where
8452 INSTANCE is really a pointer. Return negative if this is a
8453 ctor/dtor. There the dynamic type is known, but this might not be
8454 the most derived base of the original object, and hence virtual
8455 bases may not be laid out according to this type.
8456
8457 Used to determine whether the virtual function table is needed
8458 or not.
8459
8460 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
8461 of our knowledge of its type. *NONNULL should be initialized
8462 before this function is called. */
8463
8464 int
8465 resolves_to_fixed_type_p (tree instance, int* nonnull)
8466 {
8467 tree t = TREE_TYPE (instance);
8468 int cdtorp = 0;
8469 tree fixed;
8470
8471 /* processing_template_decl can be false in a template if we're in
8472 instantiate_non_dependent_expr, but we still want to suppress
8473 this check. */
8474 if (in_template_context)
8475 {
8476 /* In a template we only care about the type of the result. */
8477 if (nonnull)
8478 *nonnull = true;
8479 return true;
8480 }
8481
8482 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
8483 if (INDIRECT_TYPE_P (t))
8484 t = TREE_TYPE (t);
8485 if (CLASS_TYPE_P (t) && CLASSTYPE_FINAL (t))
8486 return 1;
8487 if (fixed == NULL_TREE)
8488 return 0;
8489 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
8490 return 0;
8491 return cdtorp ? -1 : 1;
8492 }
8493
8494
8495 void
8496 init_class_processing (void)
8497 {
8498 current_class_depth = 0;
8499 current_class_stack_size = 10;
8500 current_class_stack
8501 = XNEWVEC (struct class_stack_node, current_class_stack_size);
8502 sizeof_biggest_empty_class = size_zero_node;
8503
8504 ridpointers[(int) RID_PUBLIC] = access_public_node;
8505 ridpointers[(int) RID_PRIVATE] = access_private_node;
8506 ridpointers[(int) RID_PROTECTED] = access_protected_node;
8507 }
8508
8509 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
8510
8511 static void
8512 restore_class_cache (void)
8513 {
8514 tree type;
8515
8516 /* We are re-entering the same class we just left, so we don't
8517 have to search the whole inheritance matrix to find all the
8518 decls to bind again. Instead, we install the cached
8519 class_shadowed list and walk through it binding names. */
8520 push_binding_level (previous_class_level);
8521 class_binding_level = previous_class_level;
8522 /* Restore IDENTIFIER_TYPE_VALUE. */
8523 for (type = class_binding_level->type_shadowed;
8524 type;
8525 type = TREE_CHAIN (type))
8526 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
8527 }
8528
8529 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
8530 appropriate for TYPE.
8531
8532 So that we may avoid calls to lookup_name, we cache the _TYPE
8533 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
8534
8535 For multiple inheritance, we perform a two-pass depth-first search
8536 of the type lattice. */
8537
8538 void
8539 pushclass (tree type)
8540 {
8541 class_stack_node_t csn;
8542
8543 type = TYPE_MAIN_VARIANT (type);
8544
8545 /* Make sure there is enough room for the new entry on the stack. */
8546 if (current_class_depth + 1 >= current_class_stack_size)
8547 {
8548 current_class_stack_size *= 2;
8549 current_class_stack
8550 = XRESIZEVEC (struct class_stack_node, current_class_stack,
8551 current_class_stack_size);
8552 }
8553
8554 /* Insert a new entry on the class stack. */
8555 csn = current_class_stack + current_class_depth;
8556 csn->name = current_class_name;
8557 csn->type = current_class_type;
8558 csn->access = current_access_specifier;
8559 csn->names_used = 0;
8560 csn->hidden = 0;
8561 current_class_depth++;
8562
8563 /* Now set up the new type. */
8564 current_class_name = TYPE_NAME (type);
8565 if (TREE_CODE (current_class_name) == TYPE_DECL)
8566 current_class_name = DECL_NAME (current_class_name);
8567 current_class_type = type;
8568
8569 /* By default, things in classes are private, while things in
8570 structures or unions are public. */
8571 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
8572 ? access_private_node
8573 : access_public_node);
8574
8575 if (previous_class_level
8576 && type != previous_class_level->this_entity
8577 && current_class_depth == 1)
8578 {
8579 /* Forcibly remove any old class remnants. */
8580 invalidate_class_lookup_cache ();
8581 }
8582
8583 if (!previous_class_level
8584 || type != previous_class_level->this_entity
8585 || current_class_depth > 1)
8586 pushlevel_class ();
8587 else
8588 restore_class_cache ();
8589 }
8590
8591 /* Get out of the current class scope. If we were in a class scope
8592 previously, that is the one popped to. */
8593
8594 void
8595 popclass (void)
8596 {
8597 poplevel_class ();
8598
8599 current_class_depth--;
8600 current_class_name = current_class_stack[current_class_depth].name;
8601 current_class_type = current_class_stack[current_class_depth].type;
8602 current_access_specifier = current_class_stack[current_class_depth].access;
8603 if (current_class_stack[current_class_depth].names_used)
8604 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8605 }
8606
8607 /* Mark the top of the class stack as hidden. */
8608
8609 void
8610 push_class_stack (void)
8611 {
8612 if (current_class_depth)
8613 ++current_class_stack[current_class_depth - 1].hidden;
8614 }
8615
8616 /* Mark the top of the class stack as un-hidden. */
8617
8618 void
8619 pop_class_stack (void)
8620 {
8621 if (current_class_depth)
8622 --current_class_stack[current_class_depth - 1].hidden;
8623 }
8624
8625 /* If the class type currently being defined is either T or
8626 a nested type of T, returns the type from the current_class_stack,
8627 which might be equivalent to but not equal to T in case of
8628 constrained partial specializations. */
8629
8630 tree
8631 currently_open_class (tree t)
8632 {
8633 int i;
8634
8635 if (!CLASS_TYPE_P (t))
8636 return NULL_TREE;
8637
8638 t = TYPE_MAIN_VARIANT (t);
8639
8640 /* We start looking from 1 because entry 0 is from global scope,
8641 and has no type. */
8642 for (i = current_class_depth; i > 0; --i)
8643 {
8644 tree c;
8645 if (i == current_class_depth)
8646 c = current_class_type;
8647 else
8648 {
8649 if (current_class_stack[i].hidden)
8650 break;
8651 c = current_class_stack[i].type;
8652 }
8653 if (!c)
8654 continue;
8655 if (same_type_p (c, t))
8656 return c;
8657 }
8658 return NULL_TREE;
8659 }
8660
8661 /* If either current_class_type or one of its enclosing classes are derived
8662 from T, return the appropriate type. Used to determine how we found
8663 something via unqualified lookup. */
8664
8665 tree
8666 currently_open_derived_class (tree t)
8667 {
8668 int i;
8669
8670 /* The bases of a dependent type are unknown. */
8671 if (dependent_type_p (t))
8672 return NULL_TREE;
8673
8674 if (!current_class_type)
8675 return NULL_TREE;
8676
8677 if (DERIVED_FROM_P (t, current_class_type))
8678 return current_class_type;
8679
8680 for (i = current_class_depth - 1; i > 0; --i)
8681 {
8682 if (current_class_stack[i].hidden)
8683 break;
8684 if (DERIVED_FROM_P (t, current_class_stack[i].type))
8685 return current_class_stack[i].type;
8686 }
8687
8688 return NULL_TREE;
8689 }
8690
8691 /* Return the outermost enclosing class type that is still open, or
8692 NULL_TREE. */
8693
8694 tree
8695 outermost_open_class (void)
8696 {
8697 if (!current_class_type)
8698 return NULL_TREE;
8699 tree r = NULL_TREE;
8700 if (TYPE_BEING_DEFINED (current_class_type))
8701 r = current_class_type;
8702 for (int i = current_class_depth - 1; i > 0; --i)
8703 {
8704 if (current_class_stack[i].hidden)
8705 break;
8706 tree t = current_class_stack[i].type;
8707 if (!TYPE_BEING_DEFINED (t))
8708 break;
8709 r = t;
8710 }
8711 return r;
8712 }
8713
8714 /* Returns the innermost class type which is not a lambda closure type. */
8715
8716 tree
8717 current_nonlambda_class_type (void)
8718 {
8719 tree type = current_class_type;
8720 while (type && LAMBDA_TYPE_P (type))
8721 type = decl_type_context (TYPE_NAME (type));
8722 return type;
8723 }
8724
8725 /* When entering a class scope, all enclosing class scopes' names with
8726 static meaning (static variables, static functions, types and
8727 enumerators) have to be visible. This recursive function calls
8728 pushclass for all enclosing class contexts until global or a local
8729 scope is reached. TYPE is the enclosed class. */
8730
8731 void
8732 push_nested_class (tree type)
8733 {
8734 /* A namespace might be passed in error cases, like A::B:C. */
8735 if (type == NULL_TREE
8736 || !CLASS_TYPE_P (type))
8737 return;
8738
8739 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8740
8741 pushclass (type);
8742 }
8743
8744 /* Undoes a push_nested_class call. */
8745
8746 void
8747 pop_nested_class (void)
8748 {
8749 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8750
8751 popclass ();
8752 if (context && CLASS_TYPE_P (context))
8753 pop_nested_class ();
8754 }
8755
8756 /* Returns the number of extern "LANG" blocks we are nested within. */
8757
8758 int
8759 current_lang_depth (void)
8760 {
8761 return vec_safe_length (current_lang_base);
8762 }
8763
8764 /* Set global variables CURRENT_LANG_NAME to appropriate value
8765 so that behavior of name-mangling machinery is correct. */
8766
8767 void
8768 push_lang_context (tree name)
8769 {
8770 vec_safe_push (current_lang_base, current_lang_name);
8771
8772 if (name == lang_name_cplusplus)
8773 current_lang_name = name;
8774 else if (name == lang_name_c)
8775 current_lang_name = name;
8776 else
8777 error ("language string %<\"%E\"%> not recognized", name);
8778 }
8779
8780 /* Get out of the current language scope. */
8781
8782 void
8783 pop_lang_context (void)
8784 {
8785 current_lang_name = current_lang_base->pop ();
8786 }
8787
8788 /* Type instantiation routines. */
8789
8790 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8791 matches the TARGET_TYPE. If there is no satisfactory match, return
8792 error_mark_node, and issue an error & warning messages under
8793 control of FLAGS. Permit pointers to member function if FLAGS
8794 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8795 a template-id, and EXPLICIT_TARGS are the explicitly provided
8796 template arguments.
8797
8798 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8799 is the base path used to reference those member functions. If
8800 the address is resolved to a member function, access checks will be
8801 performed and errors issued if appropriate. */
8802
8803 static tree
8804 resolve_address_of_overloaded_function (tree target_type,
8805 tree overload,
8806 tsubst_flags_t complain,
8807 bool template_only,
8808 tree explicit_targs,
8809 tree access_path)
8810 {
8811 /* Here's what the standard says:
8812
8813 [over.over]
8814
8815 If the name is a function template, template argument deduction
8816 is done, and if the argument deduction succeeds, the deduced
8817 arguments are used to generate a single template function, which
8818 is added to the set of overloaded functions considered.
8819
8820 Non-member functions and static member functions match targets of
8821 type "pointer-to-function" or "reference-to-function." Nonstatic
8822 member functions match targets of type "pointer-to-member
8823 function;" the function type of the pointer to member is used to
8824 select the member function from the set of overloaded member
8825 functions. If a non-static member function is selected, the
8826 reference to the overloaded function name is required to have the
8827 form of a pointer to member as described in 5.3.1.
8828
8829 If more than one function is selected, any template functions in
8830 the set are eliminated if the set also contains a non-template
8831 function, and any given template function is eliminated if the
8832 set contains a second template function that is more specialized
8833 than the first according to the partial ordering rules 14.5.5.2.
8834 After such eliminations, if any, there shall remain exactly one
8835 selected function. */
8836
8837 int is_ptrmem = 0;
8838 /* We store the matches in a TREE_LIST rooted here. The functions
8839 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8840 interoperability with most_specialized_instantiation. */
8841 tree matches = NULL_TREE;
8842 tree fn;
8843 tree target_fn_type;
8844
8845 /* By the time we get here, we should be seeing only real
8846 pointer-to-member types, not the internal POINTER_TYPE to
8847 METHOD_TYPE representation. */
8848 gcc_assert (!TYPE_PTR_P (target_type)
8849 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8850
8851 gcc_assert (is_overloaded_fn (overload));
8852
8853 /* Check that the TARGET_TYPE is reasonable. */
8854 if (TYPE_PTRFN_P (target_type)
8855 || TYPE_REFFN_P (target_type))
8856 /* This is OK. */;
8857 else if (TYPE_PTRMEMFUNC_P (target_type))
8858 /* This is OK, too. */
8859 is_ptrmem = 1;
8860 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8861 /* This is OK, too. This comes from a conversion to reference
8862 type. */
8863 target_type = build_reference_type (target_type);
8864 else
8865 {
8866 if (complain & tf_error)
8867 error ("cannot resolve overloaded function %qD based on"
8868 " conversion to type %qT",
8869 OVL_NAME (overload), target_type);
8870 return error_mark_node;
8871 }
8872
8873 /* Non-member functions and static member functions match targets of type
8874 "pointer-to-function" or "reference-to-function." Nonstatic member
8875 functions match targets of type "pointer-to-member-function;" the
8876 function type of the pointer to member is used to select the member
8877 function from the set of overloaded member functions.
8878
8879 So figure out the FUNCTION_TYPE that we want to match against. */
8880 target_fn_type = static_fn_type (target_type);
8881
8882 /* If we can find a non-template function that matches, we can just
8883 use it. There's no point in generating template instantiations
8884 if we're just going to throw them out anyhow. But, of course, we
8885 can only do this when we don't *need* a template function. */
8886 if (!template_only)
8887 for (lkp_iterator iter (overload); iter; ++iter)
8888 {
8889 tree fn = *iter;
8890
8891 if (TREE_CODE (fn) == TEMPLATE_DECL)
8892 /* We're not looking for templates just yet. */
8893 continue;
8894
8895 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8896 /* We're looking for a non-static member, and this isn't
8897 one, or vice versa. */
8898 continue;
8899
8900 /* Constraints must be satisfied. This is done before
8901 return type deduction since that instantiates the
8902 function. */
8903 if (!constraints_satisfied_p (fn))
8904 continue;
8905
8906 if (undeduced_auto_decl (fn))
8907 {
8908 /* Force instantiation to do return type deduction. */
8909 maybe_instantiate_decl (fn);
8910 require_deduced_type (fn);
8911 }
8912
8913 /* In C++17 we need the noexcept-qualifier to compare types. */
8914 if (flag_noexcept_type
8915 && !maybe_instantiate_noexcept (fn, complain))
8916 continue;
8917
8918 /* See if there's a match. */
8919 tree fntype = static_fn_type (fn);
8920 if (same_type_p (target_fn_type, fntype)
8921 || fnptr_conv_p (target_fn_type, fntype))
8922 matches = tree_cons (fn, NULL_TREE, matches);
8923 }
8924
8925 /* Now, if we've already got a match (or matches), there's no need
8926 to proceed to the template functions. But, if we don't have a
8927 match we need to look at them, too. */
8928 if (!matches)
8929 {
8930 tree target_arg_types;
8931 tree target_ret_type;
8932 tree *args;
8933 unsigned int nargs, ia;
8934 tree arg;
8935
8936 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8937 target_ret_type = TREE_TYPE (target_fn_type);
8938
8939 nargs = list_length (target_arg_types);
8940 args = XALLOCAVEC (tree, nargs);
8941 for (arg = target_arg_types, ia = 0;
8942 arg != NULL_TREE;
8943 arg = TREE_CHAIN (arg), ++ia)
8944 args[ia] = TREE_VALUE (arg);
8945 nargs = ia;
8946
8947 for (lkp_iterator iter (overload); iter; ++iter)
8948 {
8949 tree fn = *iter;
8950 tree instantiation;
8951 tree targs;
8952
8953 if (TREE_CODE (fn) != TEMPLATE_DECL)
8954 /* We're only looking for templates. */
8955 continue;
8956
8957 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8958 != is_ptrmem)
8959 /* We're not looking for a non-static member, and this is
8960 one, or vice versa. */
8961 continue;
8962
8963 tree ret = target_ret_type;
8964
8965 /* If the template has a deduced return type, don't expose it to
8966 template argument deduction. */
8967 if (undeduced_auto_decl (fn))
8968 ret = NULL_TREE;
8969
8970 /* Try to do argument deduction. */
8971 targs = make_tree_vec (DECL_NTPARMS (fn));
8972 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8973 nargs, ret,
8974 DEDUCE_EXACT, LOOKUP_NORMAL,
8975 NULL, false, false);
8976 if (instantiation == error_mark_node)
8977 /* Instantiation failed. */
8978 continue;
8979
8980 /* Constraints must be satisfied. This is done before
8981 return type deduction since that instantiates the
8982 function. */
8983 if (flag_concepts && !constraints_satisfied_p (instantiation))
8984 continue;
8985
8986 /* And now force instantiation to do return type deduction. */
8987 if (undeduced_auto_decl (instantiation))
8988 {
8989 ++function_depth;
8990 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8991 --function_depth;
8992
8993 require_deduced_type (instantiation);
8994 }
8995
8996 /* In C++17 we need the noexcept-qualifier to compare types. */
8997 if (flag_noexcept_type)
8998 maybe_instantiate_noexcept (instantiation, complain);
8999
9000 /* See if there's a match. */
9001 tree fntype = static_fn_type (instantiation);
9002 if (same_type_p (target_fn_type, fntype)
9003 || fnptr_conv_p (target_fn_type, fntype))
9004 matches = tree_cons (instantiation, fn, matches);
9005 }
9006
9007 /* Now, remove all but the most specialized of the matches. */
9008 if (matches)
9009 {
9010 tree match = most_specialized_instantiation (matches);
9011
9012 if (match != error_mark_node)
9013 {
9014 matches = match;
9015 TREE_CHAIN (match) = NULL_TREE;
9016 }
9017 }
9018 }
9019 else if (flag_concepts && TREE_CHAIN (matches))
9020 if (tree match = most_constrained_function (matches))
9021 {
9022 matches = match;
9023 TREE_CHAIN (match) = NULL_TREE;
9024 }
9025
9026 /* Now we should have exactly one function in MATCHES. */
9027 if (matches == NULL_TREE)
9028 {
9029 /* There were *no* matches. */
9030 if (complain & tf_error)
9031 {
9032 auto_diagnostic_group d;
9033 error ("no matches converting function %qD to type %q#T",
9034 OVL_NAME (overload), target_type);
9035
9036 print_candidates (overload);
9037 }
9038 return error_mark_node;
9039 }
9040 else if (TREE_CHAIN (matches))
9041 {
9042 /* There were too many matches. First check if they're all
9043 the same function. */
9044 tree match = NULL_TREE;
9045
9046 fn = TREE_PURPOSE (matches);
9047
9048 /* For multi-versioned functions, more than one match is just fine and
9049 decls_match will return false as they are different. */
9050 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
9051 if (!decls_match (fn, TREE_PURPOSE (match))
9052 && !targetm.target_option.function_versions
9053 (fn, TREE_PURPOSE (match)))
9054 break;
9055
9056 if (match)
9057 {
9058 if (complain & tf_error)
9059 {
9060 auto_diagnostic_group d;
9061 error ("converting overloaded function %qD to type %q#T is ambiguous",
9062 OVL_NAME (overload), target_type);
9063
9064 /* Since print_candidates expects the functions in the
9065 TREE_VALUE slot, we flip them here. */
9066 for (match = matches; match; match = TREE_CHAIN (match))
9067 TREE_VALUE (match) = TREE_PURPOSE (match);
9068
9069 print_candidates (matches);
9070 }
9071
9072 return error_mark_node;
9073 }
9074 }
9075
9076 /* Good, exactly one match. Now, convert it to the correct type. */
9077 fn = TREE_PURPOSE (matches);
9078
9079 if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)
9080 && !(complain & tf_ptrmem_ok))
9081 {
9082 /* Previously we allowed this behavior for iobj member functions when the
9083 -fms-extensions flag is passed as MSVC allows this as a language
9084 extension. MSVC also allows this for xobj member functions, but the
9085 documentation for -fms-extensions states it's purpose is to support
9086 the use of microsoft headers. Until otherwise demonstrated, we should
9087 assume xobj member functions are not used in this manner in microsoft
9088 headers and forbid the incorrect syntax instead of supporting it for
9089 non-legacy uses. This should hopefully encourage conformance going
9090 forward.
9091 This comment is referred to in typeck.cc:cp_build_addr_expr_1. */
9092 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && flag_ms_extensions)
9093 /* Early escape. */;
9094 else if (!(complain & tf_error))
9095 return error_mark_node;
9096 else if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
9097 {
9098 auto_diagnostic_group d;
9099 /* Should match the error in typeck.cc:cp_build_addr_expr_1.
9100 We seem to lack the details here to match that diagnostic exactly,
9101 perhaps this could be fixed in the future? See PR113075 bug 2. */
9102 error_at (input_location,
9103 "ISO C++ forbids taking the address of an unqualified"
9104 " or parenthesized non-static member function to form"
9105 " a pointer to explicit object member function.");
9106 /* This is incorrect, see PR113075 bug 3. */
9107 inform (input_location,
9108 "a pointer to explicit object member function can only be "
9109 "formed with %<&%E%>", fn);
9110 }
9111 else
9112 {
9113 static int explained;
9114 gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn) && !flag_ms_extensions);
9115 /* Is there a reason this error message doesn't match the one in
9116 typeck.cc:cp_build_addr_expr_1? */
9117 auto_diagnostic_group d;
9118 if (permerror (input_location, "assuming pointer to member %qD", fn)
9119 && !explained)
9120 {
9121 inform (input_location, "(a pointer to member can only be "
9122 "formed with %<&%E%>)", fn);
9123 explained = 1;
9124 }
9125 }
9126 }
9127
9128 /* If a pointer to a function that is multi-versioned is requested, the
9129 pointer to the dispatcher function is returned instead. This works
9130 well because indirectly calling the function will dispatch the right
9131 function version at run-time. */
9132 if (DECL_FUNCTION_VERSIONED (fn))
9133 {
9134 fn = get_function_version_dispatcher (fn);
9135 if (fn == NULL)
9136 return error_mark_node;
9137 /* Mark all the versions corresponding to the dispatcher as used. */
9138 if (!(complain & tf_conv))
9139 mark_versions_used (fn);
9140 }
9141
9142 /* If we're doing overload resolution purely for the purpose of
9143 determining conversion sequences, we should not consider the
9144 function used. If this conversion sequence is selected, the
9145 function will be marked as used at this point. */
9146 if (!(complain & tf_conv))
9147 {
9148 /* Make =delete work with SFINAE. */
9149 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
9150 return error_mark_node;
9151 if (!mark_used (fn, complain) && !(complain & tf_error))
9152 return error_mark_node;
9153 }
9154
9155 /* We could not check access to member functions when this
9156 expression was originally created since we did not know at that
9157 time to which function the expression referred. */
9158 if (DECL_FUNCTION_MEMBER_P (fn))
9159 {
9160 gcc_assert (access_path);
9161 perform_or_defer_access_check (access_path, fn, fn, complain);
9162 }
9163
9164 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
9165 return cp_build_addr_expr (fn, complain);
9166 else
9167 {
9168 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
9169 will mark the function as addressed, but here we must do it
9170 explicitly. */
9171 cxx_mark_addressable (fn);
9172
9173 return fn;
9174 }
9175 }
9176
9177 /* This function will instantiate the type of the expression given in
9178 RHS to match the type of LHSTYPE. If errors exist, then return
9179 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
9180 we complain on errors. If we are not complaining, never modify rhs,
9181 as overload resolution wants to try many possible instantiations, in
9182 the hope that at least one will work.
9183
9184 For non-recursive calls, LHSTYPE should be a function, pointer to
9185 function, or a pointer to member function. */
9186
9187 tree
9188 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
9189 {
9190 tsubst_flags_t complain_in = complain;
9191 tree access_path = NULL_TREE;
9192
9193 complain &= ~tf_ptrmem_ok;
9194
9195 STRIP_ANY_LOCATION_WRAPPER (rhs);
9196
9197 if (lhstype == unknown_type_node)
9198 {
9199 if (complain & tf_error)
9200 error ("not enough type information");
9201 return error_mark_node;
9202 }
9203
9204 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
9205 {
9206 tree fntype = non_reference (lhstype);
9207 if (same_type_p (fntype, TREE_TYPE (rhs)))
9208 return rhs;
9209 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
9210 return rhs;
9211 if (flag_ms_extensions
9212 && TYPE_PTRMEMFUNC_P (fntype)
9213 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
9214 /* Microsoft allows `A::f' to be resolved to a
9215 pointer-to-member. */
9216 ;
9217 else
9218 {
9219 if (complain & tf_error)
9220 error ("cannot convert %qE from type %qT to type %qT",
9221 rhs, TREE_TYPE (rhs), fntype);
9222 return error_mark_node;
9223 }
9224 }
9225
9226 /* If we instantiate a template, and it is a A ?: C expression
9227 with omitted B, look through the SAVE_EXPR. */
9228 if (TREE_CODE (rhs) == SAVE_EXPR)
9229 rhs = TREE_OPERAND (rhs, 0);
9230
9231 if (BASELINK_P (rhs))
9232 {
9233 access_path = BASELINK_ACCESS_BINFO (rhs);
9234 rhs = BASELINK_FUNCTIONS (rhs);
9235 }
9236
9237 /* There are only a few kinds of expressions that may have a type
9238 dependent on overload resolution. */
9239 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
9240 || TREE_CODE (rhs) == COMPONENT_REF
9241 || is_overloaded_fn (rhs)
9242 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
9243
9244 /* This should really only be used when attempting to distinguish
9245 what sort of a pointer to function we have. For now, any
9246 arithmetic operation which is not supported on pointers
9247 is rejected as an error. */
9248
9249 switch (TREE_CODE (rhs))
9250 {
9251 case COMPONENT_REF:
9252 {
9253 tree member = TREE_OPERAND (rhs, 1);
9254
9255 member = instantiate_type (lhstype, member, complain);
9256 if (member != error_mark_node
9257 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
9258 /* Do not lose object's side effects. */
9259 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
9260 TREE_OPERAND (rhs, 0), member);
9261 return member;
9262 }
9263
9264 case OFFSET_REF:
9265 rhs = TREE_OPERAND (rhs, 1);
9266 if (BASELINK_P (rhs))
9267 return instantiate_type (lhstype, rhs, complain_in);
9268
9269 /* This can happen if we are forming a pointer-to-member for a
9270 member template. */
9271 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
9272
9273 /* Fall through. */
9274
9275 case TEMPLATE_ID_EXPR:
9276 {
9277 tree fns = TREE_OPERAND (rhs, 0);
9278 tree args = TREE_OPERAND (rhs, 1);
9279
9280 return
9281 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
9282 /*template_only=*/true,
9283 args, access_path);
9284 }
9285
9286 case OVERLOAD:
9287 case FUNCTION_DECL:
9288 return
9289 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
9290 /*template_only=*/false,
9291 /*explicit_targs=*/NULL_TREE,
9292 access_path);
9293
9294 case ADDR_EXPR:
9295 {
9296 if (PTRMEM_OK_P (rhs))
9297 complain |= tf_ptrmem_ok;
9298
9299 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
9300 }
9301
9302 case ERROR_MARK:
9303 return error_mark_node;
9304
9305 default:
9306 gcc_unreachable ();
9307 }
9308 return error_mark_node;
9309 }
9310
9311 /* Return the name of the virtual function pointer field
9312 (as an IDENTIFIER_NODE) for the given TYPE. Note that
9313 this may have to look back through base types to find the
9314 ultimate field name. (For single inheritance, these could
9315 all be the same name. Who knows for multiple inheritance). */
9316
9317 static tree
9318 get_vfield_name (tree type)
9319 {
9320 tree binfo, base_binfo;
9321
9322 for (binfo = TYPE_BINFO (type);
9323 BINFO_N_BASE_BINFOS (binfo);
9324 binfo = base_binfo)
9325 {
9326 base_binfo = BINFO_BASE_BINFO (binfo, 0);
9327
9328 if (BINFO_VIRTUAL_P (base_binfo)
9329 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
9330 break;
9331 }
9332
9333 type = BINFO_TYPE (binfo);
9334 tree ctor_name = constructor_name (type);
9335 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
9336 + IDENTIFIER_LENGTH (ctor_name) + 2);
9337 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
9338 return get_identifier (buf);
9339 }
9340
9341 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
9342 according to [class]:
9343 The class-name is also inserted
9344 into the scope of the class itself. For purposes of access checking,
9345 the inserted class name is treated as if it were a public member name. */
9346
9347 void
9348 build_self_reference (void)
9349 {
9350 tree name = DECL_NAME (TYPE_NAME (current_class_type));
9351 tree decl = build_lang_decl (TYPE_DECL, name, current_class_type);
9352
9353 DECL_NONLOCAL (decl) = 1;
9354 DECL_CONTEXT (decl) = current_class_type;
9355 DECL_ARTIFICIAL (decl) = 1;
9356 SET_DECL_SELF_REFERENCE_P (decl);
9357 set_underlying_type (decl);
9358 set_instantiating_module (decl);
9359
9360 if (processing_template_decl)
9361 decl = push_template_decl (decl);
9362
9363 tree saved_cas = current_access_specifier;
9364 current_access_specifier = access_public_node;
9365 finish_member_declaration (decl);
9366 current_access_specifier = saved_cas;
9367 }
9368
9369 /* Returns 1 if TYPE contains only padding bytes. */
9370
9371 int
9372 is_empty_class (tree type)
9373 {
9374 if (type == error_mark_node)
9375 return 0;
9376
9377 if (! CLASS_TYPE_P (type))
9378 return 0;
9379
9380 return CLASSTYPE_EMPTY_P (type);
9381 }
9382
9383 /* Returns true if TYPE contains no actual data, just various
9384 possible combinations of empty classes. If IGNORE_VPTR is true,
9385 a vptr doesn't prevent the class from being considered empty. Typically
9386 we want to ignore the vptr on assignment, and not on initialization. */
9387
9388 bool
9389 is_really_empty_class (tree type, bool ignore_vptr)
9390 {
9391 if (CLASS_TYPE_P (type))
9392 {
9393 tree field;
9394 tree binfo;
9395 tree base_binfo;
9396 int i;
9397
9398 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
9399 out, but we'd like to be able to check this before then. */
9400 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
9401 return true;
9402
9403 if (!ignore_vptr && TYPE_CONTAINS_VPTR_P (type))
9404 return false;
9405
9406 for (binfo = TYPE_BINFO (type), i = 0;
9407 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9408 if (!is_really_empty_class (BINFO_TYPE (base_binfo), ignore_vptr))
9409 return false;
9410 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
9411 if (TREE_CODE (field) == FIELD_DECL
9412 && !DECL_ARTIFICIAL (field)
9413 /* An unnamed bit-field is not a data member. */
9414 && !DECL_UNNAMED_BIT_FIELD (field)
9415 && !is_really_empty_class (TREE_TYPE (field), ignore_vptr))
9416 return false;
9417 return true;
9418 }
9419 else if (TREE_CODE (type) == ARRAY_TYPE)
9420 return (integer_zerop (array_type_nelts_top (type))
9421 || is_really_empty_class (TREE_TYPE (type), ignore_vptr));
9422 return false;
9423 }
9424
9425 /* Note that NAME was looked up while the current class was being
9426 defined and that the result of that lookup was DECL. */
9427
9428 void
9429 maybe_note_name_used_in_class (tree name, tree decl)
9430 {
9431 /* If we're not defining a class, there's nothing to do. */
9432 if (!(innermost_scope_kind() == sk_class
9433 && TYPE_BEING_DEFINED (current_class_type)
9434 && !LAMBDA_TYPE_P (current_class_type)))
9435 return;
9436
9437 const cp_binding_level *blev = nullptr;
9438 if (const cxx_binding *binding = IDENTIFIER_BINDING (name))
9439 blev = binding->scope;
9440 const cp_binding_level *lev = current_binding_level;
9441
9442 /* Record the binding in the names_used tables for classes inside blev. */
9443 for (int i = current_class_depth; i > 0; --i)
9444 {
9445 tree type = (i == current_class_depth
9446 ? current_class_type
9447 : current_class_stack[i].type);
9448
9449 for (; lev; lev = lev->level_chain)
9450 {
9451 if (lev == blev)
9452 /* We found the declaration. */
9453 return;
9454 if (lev->kind == sk_class && lev->this_entity == type)
9455 /* This class is inside the declaration scope. */
9456 break;
9457 }
9458
9459 auto &names_used = current_class_stack[i-1].names_used;
9460 if (!names_used)
9461 names_used = splay_tree_new (splay_tree_compare_pointers, 0, 0);
9462
9463 tree use = build1_loc (input_location, VIEW_CONVERT_EXPR,
9464 TREE_TYPE (decl), decl);
9465 EXPR_LOCATION_WRAPPER_P (use) = 1;
9466 splay_tree_insert (names_used,
9467 (splay_tree_key) name,
9468 (splay_tree_value) use);
9469 }
9470 }
9471
9472 /* Note that NAME was declared (as DECL) in the current class. Check
9473 to see that the declaration is valid under [class.member.lookup]:
9474
9475 If [the result of a search in T for N at point P] differs from the result of
9476 a search in T for N from immediately after the class-specifier of T, the
9477 program is ill-formed, no diagnostic required. */
9478
9479 void
9480 note_name_declared_in_class (tree name, tree decl)
9481 {
9482 splay_tree names_used;
9483 splay_tree_node n;
9484
9485 /* Look to see if we ever used this name. */
9486 names_used
9487 = current_class_stack[current_class_depth - 1].names_used;
9488 if (!names_used)
9489 return;
9490 /* The C language allows members to be declared with a type of the same
9491 name, and the C++ standard says this diagnostic is not required. So
9492 allow it in extern "C" blocks unless pedantic is specified.
9493 Allow it in all cases if -ms-extensions is specified. */
9494 if ((!pedantic && current_lang_name == lang_name_c)
9495 || flag_ms_extensions)
9496 return;
9497 n = splay_tree_lookup (names_used, (splay_tree_key) name);
9498 if (n)
9499 {
9500 tree use = (tree) n->value;
9501 location_t loc = EXPR_LOCATION (use);
9502 tree olddecl = OVL_FIRST (TREE_OPERAND (use, 0));
9503 /* [basic.scope.class]
9504
9505 A name N used in a class S shall refer to the same declaration
9506 in its context and when re-evaluated in the completed scope of
9507 S. */
9508 auto ov = make_temp_override (global_dc->m_pedantic_errors);
9509 if (TREE_CODE (decl) == TYPE_DECL
9510 && TREE_CODE (olddecl) == TYPE_DECL
9511 && same_type_p (TREE_TYPE (decl), TREE_TYPE (olddecl)))
9512 /* Different declaration, but same meaning; just warn. */;
9513 else if (flag_permissive)
9514 /* Let -fpermissive make it a warning like past versions. */;
9515 else
9516 /* Make it an error. */
9517 global_dc->m_pedantic_errors = 1;
9518
9519 auto_diagnostic_group d;
9520 if (pedwarn (location_of (decl), OPT_Wchanges_meaning,
9521 "declaration of %q#D changes meaning of %qD",
9522 decl, OVL_NAME (decl)))
9523 {
9524 inform (loc, "used here to mean %q#D", olddecl);
9525 inform (location_of (olddecl), "declared here" );
9526 }
9527 }
9528 }
9529
9530 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
9531 Secondary vtables are merged with primary vtables; this function
9532 will return the VAR_DECL for the primary vtable. */
9533
9534 tree
9535 get_vtbl_decl_for_binfo (tree binfo)
9536 {
9537 tree decl;
9538
9539 decl = BINFO_VTABLE (binfo);
9540 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
9541 {
9542 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
9543 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
9544 }
9545 if (decl)
9546 gcc_assert (VAR_P (decl));
9547 return decl;
9548 }
9549
9550
9551 /* Returns the binfo for the primary base of BINFO. If the resulting
9552 BINFO is a virtual base, and it is inherited elsewhere in the
9553 hierarchy, then the returned binfo might not be the primary base of
9554 BINFO in the complete object. Check BINFO_PRIMARY_P or
9555 BINFO_LOST_PRIMARY_P to be sure. */
9556
9557 static tree
9558 get_primary_binfo (tree binfo)
9559 {
9560 tree primary_base;
9561
9562 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
9563 if (!primary_base)
9564 return NULL_TREE;
9565
9566 return copied_binfo (primary_base, binfo);
9567 }
9568
9569 /* As above, but iterate until we reach the binfo that actually provides the
9570 vptr for BINFO. */
9571
9572 static tree
9573 most_primary_binfo (tree binfo)
9574 {
9575 tree b = binfo;
9576 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9577 && !BINFO_LOST_PRIMARY_P (b))
9578 {
9579 tree primary_base = get_primary_binfo (b);
9580 gcc_assert (BINFO_PRIMARY_P (primary_base)
9581 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9582 b = primary_base;
9583 }
9584 return b;
9585 }
9586
9587 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
9588 type. Note that the virtual inheritance might be above or below BINFO in
9589 the hierarchy. */
9590
9591 bool
9592 vptr_via_virtual_p (tree binfo)
9593 {
9594 if (TYPE_P (binfo))
9595 binfo = TYPE_BINFO (binfo);
9596 tree primary = most_primary_binfo (binfo);
9597 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
9598 a morally virtual base. */
9599 tree virt = binfo_via_virtual (primary, NULL_TREE);
9600 return virt != NULL_TREE;
9601 }
9602
9603 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
9604
9605 static int
9606 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
9607 {
9608 if (!indented_p)
9609 fprintf (stream, "%*s", indent, "");
9610 return 1;
9611 }
9612
9613 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
9614 INDENT should be zero when called from the top level; it is
9615 incremented recursively. IGO indicates the next expected BINFO in
9616 inheritance graph ordering. */
9617
9618 static tree
9619 dump_class_hierarchy_r (FILE *stream,
9620 dump_flags_t flags,
9621 tree binfo,
9622 tree igo,
9623 int indent)
9624 {
9625 int indented = 0;
9626 tree base_binfo;
9627 int i;
9628
9629 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
9630 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
9631 (HOST_WIDE_INT) (uintptr_t) binfo);
9632 if (binfo != igo)
9633 {
9634 fprintf (stream, "alternative-path\n");
9635 return igo;
9636 }
9637 igo = TREE_CHAIN (binfo);
9638
9639 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
9640 tree_to_shwi (BINFO_OFFSET (binfo)));
9641 if (is_empty_class (BINFO_TYPE (binfo)))
9642 fprintf (stream, " empty");
9643 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
9644 fprintf (stream, " nearly-empty");
9645 if (BINFO_VIRTUAL_P (binfo))
9646 fprintf (stream, " virtual");
9647 fprintf (stream, "\n");
9648
9649 if (BINFO_PRIMARY_P (binfo))
9650 {
9651 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9652 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
9653 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
9654 TFF_PLAIN_IDENTIFIER),
9655 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
9656 }
9657 if (BINFO_LOST_PRIMARY_P (binfo))
9658 {
9659 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9660 fprintf (stream, " lost-primary");
9661 }
9662 if (indented)
9663 fprintf (stream, "\n");
9664
9665 if (!(flags & TDF_SLIM))
9666 {
9667 int indented = 0;
9668
9669 if (BINFO_SUBVTT_INDEX (binfo))
9670 {
9671 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9672 fprintf (stream, " subvttidx=%s",
9673 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
9674 TFF_PLAIN_IDENTIFIER));
9675 }
9676 if (BINFO_VPTR_INDEX (binfo))
9677 {
9678 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9679 fprintf (stream, " vptridx=%s",
9680 expr_as_string (BINFO_VPTR_INDEX (binfo),
9681 TFF_PLAIN_IDENTIFIER));
9682 }
9683 if (BINFO_VPTR_FIELD (binfo))
9684 {
9685 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9686 fprintf (stream, " vbaseoffset=%s",
9687 expr_as_string (BINFO_VPTR_FIELD (binfo),
9688 TFF_PLAIN_IDENTIFIER));
9689 }
9690 if (BINFO_VTABLE (binfo))
9691 {
9692 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
9693 fprintf (stream, " vptr=%s",
9694 expr_as_string (BINFO_VTABLE (binfo),
9695 TFF_PLAIN_IDENTIFIER));
9696 }
9697
9698 if (indented)
9699 fprintf (stream, "\n");
9700 }
9701
9702 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
9703 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
9704
9705 return igo;
9706 }
9707
9708 /* Dump the BINFO hierarchy for T. */
9709
9710 static void
9711 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
9712 {
9713 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9714 fprintf (stream, " size=" HOST_WIDE_INT_PRINT_UNSIGNED " align=%u\n",
9715 tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT,
9716 TYPE_ALIGN (t) / BITS_PER_UNIT);
9717 if (tree as_base = CLASSTYPE_AS_BASE (t))
9718 fprintf (stream, " base size=" HOST_WIDE_INT_PRINT_UNSIGNED
9719 " base align=%u\n",
9720 tree_to_shwi (TYPE_SIZE (as_base)) / BITS_PER_UNIT,
9721 TYPE_ALIGN (as_base) / BITS_PER_UNIT);
9722 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
9723 fprintf (stream, "\n");
9724 }
9725
9726 /* Debug interface to hierarchy dumping. */
9727
9728 void
9729 debug_class (tree t)
9730 {
9731 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
9732 }
9733
9734 static void
9735 dump_class_hierarchy (tree t)
9736 {
9737 dump_flags_t flags;
9738 if (FILE *stream = dump_begin (class_dump_id, &flags))
9739 {
9740 dump_class_hierarchy_1 (stream, flags, t);
9741 dump_end (class_dump_id, stream);
9742 }
9743 }
9744
9745 static void
9746 dump_array (FILE * stream, tree decl)
9747 {
9748 tree value;
9749 unsigned HOST_WIDE_INT ix;
9750 HOST_WIDE_INT elt;
9751 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
9752
9753 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
9754 / BITS_PER_UNIT);
9755 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
9756 fprintf (stream, " %s entries",
9757 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
9758 TFF_PLAIN_IDENTIFIER));
9759 fprintf (stream, "\n");
9760
9761 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
9762 ix, value)
9763 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
9764 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
9765 }
9766
9767 static void
9768 dump_vtable (tree t, tree binfo, tree vtable)
9769 {
9770 dump_flags_t flags;
9771 FILE *stream = dump_begin (class_dump_id, &flags);
9772
9773 if (!stream)
9774 return;
9775
9776 if (!(flags & TDF_SLIM))
9777 {
9778 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9779
9780 fprintf (stream, "%s for %s",
9781 ctor_vtbl_p ? "Construction vtable" : "Vtable",
9782 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
9783 if (ctor_vtbl_p)
9784 {
9785 if (!BINFO_VIRTUAL_P (binfo))
9786 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
9787 (HOST_WIDE_INT) (uintptr_t) binfo);
9788 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
9789 }
9790 fprintf (stream, "\n");
9791 dump_array (stream, vtable);
9792 fprintf (stream, "\n");
9793 }
9794
9795 dump_end (class_dump_id, stream);
9796 }
9797
9798 static void
9799 dump_vtt (tree t, tree vtt)
9800 {
9801 dump_flags_t flags;
9802 FILE *stream = dump_begin (class_dump_id, &flags);
9803
9804 if (!stream)
9805 return;
9806
9807 if (!(flags & TDF_SLIM))
9808 {
9809 fprintf (stream, "VTT for %s\n",
9810 type_as_string (t, TFF_PLAIN_IDENTIFIER));
9811 dump_array (stream, vtt);
9812 fprintf (stream, "\n");
9813 }
9814
9815 dump_end (class_dump_id, stream);
9816 }
9817
9818 /* Dump a function or thunk and its thunkees. */
9819
9820 static void
9821 dump_thunk (FILE *stream, int indent, tree thunk)
9822 {
9823 static const char spaces[] = " ";
9824 tree name = DECL_NAME (thunk);
9825 tree thunks;
9826
9827 fprintf (stream, "%.*s%p %s %s", indent, spaces,
9828 (void *)thunk,
9829 !DECL_THUNK_P (thunk) ? "function"
9830 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9831 name ? IDENTIFIER_POINTER (name) : "<unset>");
9832 if (DECL_THUNK_P (thunk))
9833 {
9834 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9835 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9836
9837 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9838 if (!virtual_adjust)
9839 /*NOP*/;
9840 else if (DECL_THIS_THUNK_P (thunk))
9841 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9842 tree_to_shwi (virtual_adjust));
9843 else
9844 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9845 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9846 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9847 if (THUNK_ALIAS (thunk))
9848 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9849 }
9850 fprintf (stream, "\n");
9851 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9852 dump_thunk (stream, indent + 2, thunks);
9853 }
9854
9855 /* Dump the thunks for FN. */
9856
9857 void
9858 debug_thunks (tree fn)
9859 {
9860 dump_thunk (stderr, 0, fn);
9861 }
9862
9863 /* Virtual function table initialization. */
9864
9865 /* Create all the necessary vtables for T and its base classes. */
9866
9867 static void
9868 finish_vtbls (tree t)
9869 {
9870 tree vbase;
9871 vec<constructor_elt, va_gc> *v = NULL;
9872 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9873
9874 /* We lay out the primary and secondary vtables in one contiguous
9875 vtable. The primary vtable is first, followed by the non-virtual
9876 secondary vtables in inheritance graph order. */
9877 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9878 vtable, t, &v);
9879
9880 /* Then come the virtual bases, also in inheritance graph order. */
9881 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9882 {
9883 if (!BINFO_VIRTUAL_P (vbase))
9884 continue;
9885 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9886 }
9887
9888 if (BINFO_VTABLE (TYPE_BINFO (t)))
9889 initialize_vtable (TYPE_BINFO (t), v);
9890 }
9891
9892 /* Initialize the vtable for BINFO with the INITS. */
9893
9894 static void
9895 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9896 {
9897 tree decl;
9898
9899 layout_vtable_decl (binfo, vec_safe_length (inits));
9900 decl = get_vtbl_decl_for_binfo (binfo);
9901 initialize_artificial_var (decl, inits);
9902 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9903 }
9904
9905 /* Build the VTT (virtual table table) for T.
9906 A class requires a VTT if it has virtual bases.
9907
9908 This holds
9909 1 - primary virtual pointer for complete object T
9910 2 - secondary VTTs for each direct non-virtual base of T which requires a
9911 VTT
9912 3 - secondary virtual pointers for each direct or indirect base of T which
9913 has virtual bases or is reachable via a virtual path from T.
9914 4 - secondary VTTs for each direct or indirect virtual base of T.
9915
9916 Secondary VTTs look like complete object VTTs without part 4. */
9917
9918 static void
9919 build_vtt (tree t)
9920 {
9921 tree type;
9922 tree vtt;
9923 tree index;
9924 vec<constructor_elt, va_gc> *inits;
9925
9926 /* Build up the initializers for the VTT. */
9927 inits = NULL;
9928 index = size_zero_node;
9929 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9930
9931 /* If we didn't need a VTT, we're done. */
9932 if (!inits)
9933 return;
9934
9935 /* Figure out the type of the VTT. */
9936 type = build_array_of_n_type (const_ptr_type_node,
9937 inits->length ());
9938
9939 /* Now, build the VTT object itself. */
9940 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9941 initialize_artificial_var (vtt, inits);
9942 /* Add the VTT to the vtables list. */
9943 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9944 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9945
9946 dump_vtt (t, vtt);
9947 }
9948
9949 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9950 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9951 and CHAIN the vtable pointer for this binfo after construction is
9952 complete. VALUE can also be another BINFO, in which case we recurse. */
9953
9954 static tree
9955 binfo_ctor_vtable (tree binfo)
9956 {
9957 tree vt;
9958
9959 while (1)
9960 {
9961 vt = BINFO_VTABLE (binfo);
9962 if (TREE_CODE (vt) == TREE_LIST)
9963 vt = TREE_VALUE (vt);
9964 if (TREE_CODE (vt) == TREE_BINFO)
9965 binfo = vt;
9966 else
9967 break;
9968 }
9969
9970 return vt;
9971 }
9972
9973 /* Data for secondary VTT initialization. */
9974 struct secondary_vptr_vtt_init_data
9975 {
9976 /* Is this the primary VTT? */
9977 bool top_level_p;
9978
9979 /* Current index into the VTT. */
9980 tree index;
9981
9982 /* Vector of initializers built up. */
9983 vec<constructor_elt, va_gc> *inits;
9984
9985 /* The type being constructed by this secondary VTT. */
9986 tree type_being_constructed;
9987 };
9988
9989 /* Recursively build the VTT-initializer for BINFO (which is in the
9990 hierarchy dominated by T). INITS points to the end of the initializer
9991 list to date. INDEX is the VTT index where the next element will be
9992 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9993 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9994 for virtual bases of T. When it is not so, we build the constructor
9995 vtables for the BINFO-in-T variant. */
9996
9997 static void
9998 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9999 tree *index)
10000 {
10001 int i;
10002 tree b;
10003 tree init;
10004 secondary_vptr_vtt_init_data data;
10005 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
10006
10007 /* We only need VTTs for subobjects with virtual bases. */
10008 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10009 return;
10010
10011 /* We need to use a construction vtable if this is not the primary
10012 VTT. */
10013 if (!top_level_p)
10014 {
10015 build_ctor_vtbl_group (binfo, t);
10016
10017 /* Record the offset in the VTT where this sub-VTT can be found. */
10018 BINFO_SUBVTT_INDEX (binfo) = *index;
10019 }
10020
10021 /* Add the address of the primary vtable for the complete object. */
10022 init = binfo_ctor_vtable (binfo);
10023 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
10024 if (top_level_p)
10025 {
10026 gcc_assert (!BINFO_VPTR_INDEX (binfo));
10027 BINFO_VPTR_INDEX (binfo) = *index;
10028 }
10029 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
10030
10031 /* Recursively add the secondary VTTs for non-virtual bases. */
10032 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
10033 if (!BINFO_VIRTUAL_P (b))
10034 build_vtt_inits (b, t, inits, index);
10035
10036 /* Add secondary virtual pointers for all subobjects of BINFO with
10037 either virtual bases or reachable along a virtual path, except
10038 subobjects that are non-virtual primary bases. */
10039 data.top_level_p = top_level_p;
10040 data.index = *index;
10041 data.inits = *inits;
10042 data.type_being_constructed = BINFO_TYPE (binfo);
10043
10044 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
10045
10046 *index = data.index;
10047
10048 /* data.inits might have grown as we added secondary virtual pointers.
10049 Make sure our caller knows about the new vector. */
10050 *inits = data.inits;
10051
10052 if (top_level_p)
10053 /* Add the secondary VTTs for virtual bases in inheritance graph
10054 order. */
10055 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
10056 {
10057 if (!BINFO_VIRTUAL_P (b))
10058 continue;
10059
10060 build_vtt_inits (b, t, inits, index);
10061 }
10062 else
10063 /* Remove the ctor vtables we created. */
10064 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
10065 }
10066
10067 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
10068 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
10069
10070 static tree
10071 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
10072 {
10073 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
10074
10075 /* We don't care about bases that don't have vtables. */
10076 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
10077 return dfs_skip_bases;
10078
10079 /* We're only interested in proper subobjects of the type being
10080 constructed. */
10081 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
10082 return NULL_TREE;
10083
10084 /* We're only interested in bases with virtual bases or reachable
10085 via a virtual path from the type being constructed. */
10086 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
10087 || binfo_via_virtual (binfo, data->type_being_constructed)))
10088 return dfs_skip_bases;
10089
10090 /* We're not interested in non-virtual primary bases. */
10091 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
10092 return NULL_TREE;
10093
10094 /* Record the index where this secondary vptr can be found. */
10095 if (data->top_level_p)
10096 {
10097 gcc_assert (!BINFO_VPTR_INDEX (binfo));
10098 BINFO_VPTR_INDEX (binfo) = data->index;
10099
10100 if (BINFO_VIRTUAL_P (binfo))
10101 {
10102 /* It's a primary virtual base, and this is not a
10103 construction vtable. Find the base this is primary of in
10104 the inheritance graph, and use that base's vtable
10105 now. */
10106 while (BINFO_PRIMARY_P (binfo))
10107 binfo = BINFO_INHERITANCE_CHAIN (binfo);
10108 }
10109 }
10110
10111 /* Add the initializer for the secondary vptr itself. */
10112 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
10113
10114 /* Advance the vtt index. */
10115 data->index = size_binop (PLUS_EXPR, data->index,
10116 TYPE_SIZE_UNIT (ptr_type_node));
10117
10118 return NULL_TREE;
10119 }
10120
10121 /* Called from build_vtt_inits via dfs_walk. After building
10122 constructor vtables and generating the sub-vtt from them, we need
10123 to restore the BINFO_VTABLES that were scribbled on. DATA is the
10124 binfo of the base whose sub vtt was generated. */
10125
10126 static tree
10127 dfs_fixup_binfo_vtbls (tree binfo, void* data)
10128 {
10129 tree vtable = BINFO_VTABLE (binfo);
10130
10131 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10132 /* If this class has no vtable, none of its bases do. */
10133 return dfs_skip_bases;
10134
10135 if (!vtable)
10136 /* This might be a primary base, so have no vtable in this
10137 hierarchy. */
10138 return NULL_TREE;
10139
10140 /* If we scribbled the construction vtable vptr into BINFO, clear it
10141 out now. */
10142 if (TREE_CODE (vtable) == TREE_LIST
10143 && (TREE_PURPOSE (vtable) == (tree) data))
10144 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
10145
10146 return NULL_TREE;
10147 }
10148
10149 /* Build the construction vtable group for BINFO which is in the
10150 hierarchy dominated by T. */
10151
10152 static void
10153 build_ctor_vtbl_group (tree binfo, tree t)
10154 {
10155 tree type;
10156 tree vtbl;
10157 tree id;
10158 tree vbase;
10159 vec<constructor_elt, va_gc> *v;
10160
10161 /* See if we've already created this construction vtable group. */
10162 id = mangle_ctor_vtbl_for_type (t, binfo);
10163 if (get_global_binding (id))
10164 return;
10165
10166 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
10167 /* Build a version of VTBL (with the wrong type) for use in
10168 constructing the addresses of secondary vtables in the
10169 construction vtable group. */
10170 vtbl = build_vtable (t, id, ptr_type_node);
10171
10172 /* Don't export construction vtables from shared libraries. Even on
10173 targets that don't support hidden visibility, this tells
10174 can_refer_decl_in_current_unit_p not to assume that it's safe to
10175 access from a different compilation unit (bz 54314). */
10176 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
10177 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
10178
10179 v = NULL;
10180 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
10181 binfo, vtbl, t, &v);
10182
10183 /* Add the vtables for each of our virtual bases using the vbase in T
10184 binfo. */
10185 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10186 vbase;
10187 vbase = TREE_CHAIN (vbase))
10188 {
10189 tree b;
10190
10191 if (!BINFO_VIRTUAL_P (vbase))
10192 continue;
10193 b = copied_binfo (vbase, binfo);
10194
10195 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
10196 }
10197
10198 /* Figure out the type of the construction vtable. */
10199 type = build_array_of_n_type (vtable_entry_type, v->length ());
10200 layout_type (type);
10201 TREE_TYPE (vtbl) = type;
10202 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
10203 layout_decl (vtbl, 0);
10204
10205 /* Initialize the construction vtable. */
10206 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
10207 initialize_artificial_var (vtbl, v);
10208 dump_vtable (t, binfo, vtbl);
10209 }
10210
10211 /* Add the vtbl initializers for BINFO (and its bases other than
10212 non-virtual primaries) to the list of INITS. BINFO is in the
10213 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
10214 the constructor the vtbl inits should be accumulated for. (If this
10215 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
10216 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
10217 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
10218 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
10219 but are not necessarily the same in terms of layout. */
10220
10221 static void
10222 accumulate_vtbl_inits (tree binfo,
10223 tree orig_binfo,
10224 tree rtti_binfo,
10225 tree vtbl,
10226 tree t,
10227 vec<constructor_elt, va_gc> **inits)
10228 {
10229 int i;
10230 tree base_binfo;
10231 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10232
10233 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
10234
10235 /* If it doesn't have a vptr, we don't do anything. */
10236 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
10237 return;
10238
10239 /* If we're building a construction vtable, we're not interested in
10240 subobjects that don't require construction vtables. */
10241 if (ctor_vtbl_p
10242 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
10243 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
10244 return;
10245
10246 /* Build the initializers for the BINFO-in-T vtable. */
10247 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
10248
10249 /* Walk the BINFO and its bases. We walk in preorder so that as we
10250 initialize each vtable we can figure out at what offset the
10251 secondary vtable lies from the primary vtable. We can't use
10252 dfs_walk here because we need to iterate through bases of BINFO
10253 and RTTI_BINFO simultaneously. */
10254 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10255 {
10256 /* Skip virtual bases. */
10257 if (BINFO_VIRTUAL_P (base_binfo))
10258 continue;
10259 accumulate_vtbl_inits (base_binfo,
10260 BINFO_BASE_BINFO (orig_binfo, i),
10261 rtti_binfo, vtbl, t,
10262 inits);
10263 }
10264 }
10265
10266 /* Called from accumulate_vtbl_inits. Adds the initializers for the
10267 BINFO vtable to L. */
10268
10269 static void
10270 dfs_accumulate_vtbl_inits (tree binfo,
10271 tree orig_binfo,
10272 tree rtti_binfo,
10273 tree orig_vtbl,
10274 tree t,
10275 vec<constructor_elt, va_gc> **l)
10276 {
10277 tree vtbl = NULL_TREE;
10278 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10279 int n_inits;
10280
10281 if (ctor_vtbl_p
10282 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
10283 {
10284 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
10285 primary virtual base. If it is not the same primary in
10286 the hierarchy of T, we'll need to generate a ctor vtable
10287 for it, to place at its location in T. If it is the same
10288 primary, we still need a VTT entry for the vtable, but it
10289 should point to the ctor vtable for the base it is a
10290 primary for within the sub-hierarchy of RTTI_BINFO.
10291
10292 There are three possible cases:
10293
10294 1) We are in the same place.
10295 2) We are a primary base within a lost primary virtual base of
10296 RTTI_BINFO.
10297 3) We are primary to something not a base of RTTI_BINFO. */
10298
10299 tree b;
10300 tree last = NULL_TREE;
10301
10302 /* First, look through the bases we are primary to for RTTI_BINFO
10303 or a virtual base. */
10304 b = binfo;
10305 while (BINFO_PRIMARY_P (b))
10306 {
10307 b = BINFO_INHERITANCE_CHAIN (b);
10308 last = b;
10309 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10310 goto found;
10311 }
10312 /* If we run out of primary links, keep looking down our
10313 inheritance chain; we might be an indirect primary. */
10314 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
10315 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
10316 break;
10317 found:
10318
10319 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
10320 base B and it is a base of RTTI_BINFO, this is case 2. In
10321 either case, we share our vtable with LAST, i.e. the
10322 derived-most base within B of which we are a primary. */
10323 if (b == rtti_binfo
10324 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
10325 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
10326 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
10327 binfo_ctor_vtable after everything's been set up. */
10328 vtbl = last;
10329
10330 /* Otherwise, this is case 3 and we get our own. */
10331 }
10332 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
10333 return;
10334
10335 n_inits = vec_safe_length (*l);
10336
10337 if (!vtbl)
10338 {
10339 tree index;
10340 int non_fn_entries;
10341
10342 /* Add the initializer for this vtable. */
10343 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
10344 &non_fn_entries, l);
10345
10346 /* Figure out the position to which the VPTR should point. */
10347 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
10348 index = size_binop (MULT_EXPR,
10349 TYPE_SIZE_UNIT (vtable_entry_type),
10350 size_int (non_fn_entries + n_inits));
10351 vtbl = fold_build_pointer_plus (vtbl, index);
10352 }
10353
10354 if (ctor_vtbl_p)
10355 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
10356 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
10357 straighten this out. */
10358 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
10359 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
10360 /* Throw away any unneeded intializers. */
10361 (*l)->truncate (n_inits);
10362 else
10363 /* For an ordinary vtable, set BINFO_VTABLE. */
10364 BINFO_VTABLE (binfo) = vtbl;
10365 }
10366
10367 static GTY(()) tree abort_fndecl_addr;
10368 static GTY(()) tree dvirt_fn;
10369
10370 /* Construct the initializer for BINFO's virtual function table. BINFO
10371 is part of the hierarchy dominated by T. If we're building a
10372 construction vtable, the ORIG_BINFO is the binfo we should use to
10373 find the actual function pointers to put in the vtable - but they
10374 can be overridden on the path to most-derived in the graph that
10375 ORIG_BINFO belongs. Otherwise,
10376 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
10377 BINFO that should be indicated by the RTTI information in the
10378 vtable; it will be a base class of T, rather than T itself, if we
10379 are building a construction vtable.
10380
10381 The value returned is a TREE_LIST suitable for wrapping in a
10382 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
10383 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
10384 number of non-function entries in the vtable.
10385
10386 It might seem that this function should never be called with a
10387 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
10388 base is always subsumed by a derived class vtable. However, when
10389 we are building construction vtables, we do build vtables for
10390 primary bases; we need these while the primary base is being
10391 constructed. */
10392
10393 static void
10394 build_vtbl_initializer (tree binfo,
10395 tree orig_binfo,
10396 tree t,
10397 tree rtti_binfo,
10398 int* non_fn_entries_p,
10399 vec<constructor_elt, va_gc> **inits)
10400 {
10401 tree v;
10402 vtbl_init_data vid;
10403 unsigned ix, jx;
10404 tree vbinfo;
10405 vec<tree, va_gc> *vbases;
10406 constructor_elt *e;
10407
10408 /* Initialize VID. */
10409 memset (&vid, 0, sizeof (vid));
10410 vid.binfo = binfo;
10411 vid.derived = t;
10412 vid.rtti_binfo = rtti_binfo;
10413 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
10414 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
10415 vid.generate_vcall_entries = true;
10416 /* The first vbase or vcall offset is at index -3 in the vtable. */
10417 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
10418
10419 /* Add entries to the vtable for RTTI. */
10420 build_rtti_vtbl_entries (binfo, &vid);
10421
10422 /* Create an array for keeping track of the functions we've
10423 processed. When we see multiple functions with the same
10424 signature, we share the vcall offsets. */
10425 vec_alloc (vid.fns, 32);
10426 /* Add the vcall and vbase offset entries. */
10427 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
10428
10429 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
10430 build_vbase_offset_vtbl_entries. */
10431 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
10432 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
10433 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
10434
10435 /* If the target requires padding between data entries, add that now. */
10436 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
10437 {
10438 int n_entries = vec_safe_length (vid.inits);
10439
10440 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries,
10441 true);
10442
10443 /* Move data entries into their new positions and add padding
10444 after the new positions. Iterate backwards so we don't
10445 overwrite entries that we would need to process later. */
10446 for (ix = n_entries - 1;
10447 vid.inits->iterate (ix, &e);
10448 ix--)
10449 {
10450 int j;
10451 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
10452 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
10453
10454 (*vid.inits)[new_position] = *e;
10455
10456 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
10457 {
10458 constructor_elt *f = &(*vid.inits)[new_position - j];
10459 f->index = NULL_TREE;
10460 f->value = build1 (NOP_EXPR, vtable_entry_type,
10461 null_pointer_node);
10462 }
10463 }
10464 }
10465
10466 if (non_fn_entries_p)
10467 *non_fn_entries_p = vec_safe_length (vid.inits);
10468
10469 /* The initializers for virtual functions were built up in reverse
10470 order. Straighten them out and add them to the running list in one
10471 step. */
10472 jx = vec_safe_length (*inits);
10473 vec_safe_grow (*inits, jx + vid.inits->length (), true);
10474
10475 for (ix = vid.inits->length () - 1;
10476 vid.inits->iterate (ix, &e);
10477 ix--, jx++)
10478 (**inits)[jx] = *e;
10479
10480 /* Go through all the ordinary virtual functions, building up
10481 initializers. */
10482 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
10483 {
10484 tree delta;
10485 tree vcall_index;
10486 tree fn, fn_original;
10487 tree init = NULL_TREE;
10488
10489 fn = BV_FN (v);
10490 fn_original = fn;
10491 if (DECL_THUNK_P (fn))
10492 {
10493 if (!DECL_NAME (fn))
10494 finish_thunk (fn);
10495 if (THUNK_ALIAS (fn))
10496 {
10497 fn = THUNK_ALIAS (fn);
10498 BV_FN (v) = fn;
10499 }
10500 fn_original = THUNK_TARGET (fn);
10501 }
10502
10503 /* If the only definition of this function signature along our
10504 primary base chain is from a lost primary, this vtable slot will
10505 never be used, so just zero it out. This is important to avoid
10506 requiring extra thunks which cannot be generated with the function.
10507
10508 We first check this in update_vtable_entry_for_fn, so we handle
10509 restored primary bases properly; we also need to do it here so we
10510 zero out unused slots in ctor vtables, rather than filling them
10511 with erroneous values (though harmless, apart from relocation
10512 costs). */
10513 if (BV_LOST_PRIMARY (v))
10514 init = size_zero_node;
10515
10516 if (! init)
10517 {
10518 /* Pull the offset for `this', and the function to call, out of
10519 the list. */
10520 delta = BV_DELTA (v);
10521 vcall_index = BV_VCALL_INDEX (v);
10522
10523 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
10524 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
10525
10526 /* You can't call an abstract virtual function; it's abstract.
10527 So, we replace these functions with __pure_virtual. */
10528 if (DECL_PURE_VIRTUAL_P (fn_original))
10529 {
10530 fn = abort_fndecl;
10531 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10532 {
10533 if (abort_fndecl_addr == NULL)
10534 abort_fndecl_addr
10535 = fold_convert (vfunc_ptr_type_node,
10536 build_fold_addr_expr (fn));
10537 init = abort_fndecl_addr;
10538 }
10539 }
10540 /* Likewise for deleted virtuals. */
10541 else if (DECL_DELETED_FN (fn_original))
10542 {
10543 if (!dvirt_fn)
10544 {
10545 tree name = get_identifier ("__cxa_deleted_virtual");
10546 dvirt_fn = get_global_binding (name);
10547 if (!dvirt_fn)
10548 dvirt_fn = push_library_fn
10549 (name,
10550 build_function_type_list (void_type_node, NULL_TREE),
10551 NULL_TREE, ECF_NORETURN | ECF_COLD);
10552 }
10553 fn = dvirt_fn;
10554 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10555 init = fold_convert (vfunc_ptr_type_node,
10556 build_fold_addr_expr (fn));
10557 }
10558 else
10559 {
10560 if (!integer_zerop (delta) || vcall_index)
10561 {
10562 fn = make_thunk (fn, /*this_adjusting=*/1,
10563 delta, vcall_index);
10564 if (!DECL_NAME (fn))
10565 finish_thunk (fn);
10566 }
10567 /* Take the address of the function, considering it to be of an
10568 appropriate generic type. */
10569 if (!TARGET_VTABLE_USES_DESCRIPTORS)
10570 init = fold_convert (vfunc_ptr_type_node,
10571 build_fold_addr_expr (fn));
10572 /* Don't refer to a virtual destructor from a constructor
10573 vtable or a vtable for an abstract class, since destroying
10574 an object under construction is undefined behavior and we
10575 don't want it to be considered a candidate for speculative
10576 devirtualization. But do create the thunk for ABI
10577 compliance. */
10578 if (DECL_DESTRUCTOR_P (fn_original)
10579 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
10580 || orig_binfo != binfo))
10581 init = size_zero_node;
10582 }
10583 }
10584
10585 /* And add it to the chain of initializers. */
10586 if (TARGET_VTABLE_USES_DESCRIPTORS)
10587 {
10588 int i;
10589 if (init == size_zero_node)
10590 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10591 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10592 else
10593 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
10594 {
10595 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
10596 fn, build_int_cst (NULL_TREE, i));
10597 TREE_CONSTANT (fdesc) = 1;
10598
10599 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), fdesc);
10600 }
10601 }
10602 else
10603 CONSTRUCTOR_APPEND_ELT (*inits, size_int (jx++), init);
10604 }
10605 }
10606
10607 /* Adds to vid->inits the initializers for the vbase and vcall
10608 offsets in BINFO, which is in the hierarchy dominated by T. */
10609
10610 static void
10611 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
10612 {
10613 tree b;
10614
10615 /* If this is a derived class, we must first create entries
10616 corresponding to the primary base class. */
10617 b = get_primary_binfo (binfo);
10618 if (b)
10619 build_vcall_and_vbase_vtbl_entries (b, vid);
10620
10621 /* Add the vbase entries for this base. */
10622 build_vbase_offset_vtbl_entries (binfo, vid);
10623 /* Add the vcall entries for this base. */
10624 build_vcall_offset_vtbl_entries (binfo, vid);
10625 }
10626
10627 /* Returns the initializers for the vbase offset entries in the vtable
10628 for BINFO (which is part of the class hierarchy dominated by T), in
10629 reverse order. VBASE_OFFSET_INDEX gives the vtable index
10630 where the next vbase offset will go. */
10631
10632 static void
10633 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10634 {
10635 tree vbase;
10636 tree t;
10637 tree non_primary_binfo;
10638
10639 /* If there are no virtual baseclasses, then there is nothing to
10640 do. */
10641 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
10642 return;
10643
10644 t = vid->derived;
10645
10646 /* We might be a primary base class. Go up the inheritance hierarchy
10647 until we find the most derived class of which we are a primary base:
10648 it is the offset of that which we need to use. */
10649 non_primary_binfo = binfo;
10650 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
10651 {
10652 tree b;
10653
10654 /* If we have reached a virtual base, then it must be a primary
10655 base (possibly multi-level) of vid->binfo, or we wouldn't
10656 have called build_vcall_and_vbase_vtbl_entries for it. But it
10657 might be a lost primary, so just skip down to vid->binfo. */
10658 if (BINFO_VIRTUAL_P (non_primary_binfo))
10659 {
10660 non_primary_binfo = vid->binfo;
10661 break;
10662 }
10663
10664 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
10665 if (get_primary_binfo (b) != non_primary_binfo)
10666 break;
10667 non_primary_binfo = b;
10668 }
10669
10670 /* Go through the virtual bases, adding the offsets. */
10671 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
10672 vbase;
10673 vbase = TREE_CHAIN (vbase))
10674 {
10675 tree b;
10676 tree delta;
10677
10678 if (!BINFO_VIRTUAL_P (vbase))
10679 continue;
10680
10681 /* Find the instance of this virtual base in the complete
10682 object. */
10683 b = copied_binfo (vbase, binfo);
10684
10685 /* If we've already got an offset for this virtual base, we
10686 don't need another one. */
10687 if (BINFO_VTABLE_PATH_MARKED (b))
10688 continue;
10689 BINFO_VTABLE_PATH_MARKED (b) = 1;
10690
10691 /* Figure out where we can find this vbase offset. */
10692 delta = size_binop (MULT_EXPR,
10693 vid->index,
10694 fold_convert (ssizetype,
10695 TYPE_SIZE_UNIT (vtable_entry_type)));
10696 if (vid->primary_vtbl_p)
10697 BINFO_VPTR_FIELD (b) = delta;
10698
10699 if (binfo != TYPE_BINFO (t))
10700 /* The vbase offset had better be the same. */
10701 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
10702
10703 /* The next vbase will come at a more negative offset. */
10704 vid->index = size_binop (MINUS_EXPR, vid->index,
10705 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10706
10707 /* The initializer is the delta from BINFO to this virtual base.
10708 The vbase offsets go in reverse inheritance-graph order, and
10709 we are walking in inheritance graph order so these end up in
10710 the right order. */
10711 delta = size_diffop_loc (input_location,
10712 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
10713
10714 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
10715 fold_build1_loc (input_location, NOP_EXPR,
10716 vtable_entry_type, delta));
10717 }
10718 }
10719
10720 /* Adds the initializers for the vcall offset entries in the vtable
10721 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
10722 to VID->INITS. */
10723
10724 static void
10725 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
10726 {
10727 /* We only need these entries if this base is a virtual base. We
10728 compute the indices -- but do not add to the vtable -- when
10729 building the main vtable for a class. */
10730 if (binfo == TYPE_BINFO (vid->derived)
10731 || (BINFO_VIRTUAL_P (binfo)
10732 /* If BINFO is RTTI_BINFO, then (since BINFO does not
10733 correspond to VID->DERIVED), we are building a primary
10734 construction virtual table. Since this is a primary
10735 virtual table, we do not need the vcall offsets for
10736 BINFO. */
10737 && binfo != vid->rtti_binfo))
10738 {
10739 /* We need a vcall offset for each of the virtual functions in this
10740 vtable. For example:
10741
10742 class A { virtual void f (); };
10743 class B1 : virtual public A { virtual void f (); };
10744 class B2 : virtual public A { virtual void f (); };
10745 class C: public B1, public B2 { virtual void f (); };
10746
10747 A C object has a primary base of B1, which has a primary base of A. A
10748 C also has a secondary base of B2, which no longer has a primary base
10749 of A. So the B2-in-C construction vtable needs a secondary vtable for
10750 A, which will adjust the A* to a B2* to call f. We have no way of
10751 knowing what (or even whether) this offset will be when we define B2,
10752 so we store this "vcall offset" in the A sub-vtable and look it up in
10753 a "virtual thunk" for B2::f.
10754
10755 We need entries for all the functions in our primary vtable and
10756 in our non-virtual bases' secondary vtables. */
10757 vid->vbase = binfo;
10758 /* If we are just computing the vcall indices -- but do not need
10759 the actual entries -- not that. */
10760 if (!BINFO_VIRTUAL_P (binfo))
10761 vid->generate_vcall_entries = false;
10762 /* Now, walk through the non-virtual bases, adding vcall offsets. */
10763 add_vcall_offset_vtbl_entries_r (binfo, vid);
10764 }
10765 }
10766
10767 /* Build vcall offsets, starting with those for BINFO. */
10768
10769 static void
10770 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
10771 {
10772 int i;
10773 tree primary_binfo;
10774 tree base_binfo;
10775
10776 /* Don't walk into virtual bases -- except, of course, for the
10777 virtual base for which we are building vcall offsets. Any
10778 primary virtual base will have already had its offsets generated
10779 through the recursion in build_vcall_and_vbase_vtbl_entries. */
10780 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
10781 return;
10782
10783 /* If BINFO has a primary base, process it first. */
10784 primary_binfo = get_primary_binfo (binfo);
10785 if (primary_binfo)
10786 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
10787
10788 /* Add BINFO itself to the list. */
10789 add_vcall_offset_vtbl_entries_1 (binfo, vid);
10790
10791 /* Scan the non-primary bases of BINFO. */
10792 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
10793 if (base_binfo != primary_binfo)
10794 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
10795 }
10796
10797 /* Called from build_vcall_offset_vtbl_entries_r. */
10798
10799 static void
10800 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
10801 {
10802 /* Make entries for the rest of the virtuals. */
10803 tree orig_fn;
10804
10805 /* The ABI requires that the methods be processed in declaration
10806 order. */
10807 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
10808 orig_fn;
10809 orig_fn = DECL_CHAIN (orig_fn))
10810 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
10811 add_vcall_offset (orig_fn, binfo, vid);
10812 }
10813
10814 /* Add a vcall offset entry for ORIG_FN to the vtable. */
10815
10816 static void
10817 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
10818 {
10819 size_t i;
10820 tree vcall_offset;
10821 tree derived_entry;
10822
10823 /* If there is already an entry for a function with the same
10824 signature as FN, then we do not need a second vcall offset.
10825 Check the list of functions already present in the derived
10826 class vtable. */
10827 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10828 {
10829 if (same_signature_p (derived_entry, orig_fn)
10830 /* We only use one vcall offset for virtual destructors,
10831 even though there are two virtual table entries. */
10832 || (DECL_DESTRUCTOR_P (derived_entry)
10833 && DECL_DESTRUCTOR_P (orig_fn)))
10834 return;
10835 }
10836
10837 /* If we are building these vcall offsets as part of building
10838 the vtable for the most derived class, remember the vcall
10839 offset. */
10840 if (vid->binfo == TYPE_BINFO (vid->derived))
10841 {
10842 tree_pair_s elt = {orig_fn, vid->index};
10843 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10844 }
10845
10846 /* The next vcall offset will be found at a more negative
10847 offset. */
10848 vid->index = size_binop (MINUS_EXPR, vid->index,
10849 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10850
10851 /* Keep track of this function. */
10852 vec_safe_push (vid->fns, orig_fn);
10853
10854 if (vid->generate_vcall_entries)
10855 {
10856 tree base;
10857 tree fn;
10858
10859 /* Find the overriding function. */
10860 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10861 if (fn == error_mark_node)
10862 vcall_offset = build_zero_cst (vtable_entry_type);
10863 else
10864 {
10865 base = TREE_VALUE (fn);
10866
10867 /* The vbase we're working on is a primary base of
10868 vid->binfo. But it might be a lost primary, so its
10869 BINFO_OFFSET might be wrong, so we just use the
10870 BINFO_OFFSET from vid->binfo. */
10871 vcall_offset = size_diffop_loc (input_location,
10872 BINFO_OFFSET (base),
10873 BINFO_OFFSET (vid->binfo));
10874 vcall_offset = fold_build1_loc (input_location,
10875 NOP_EXPR, vtable_entry_type,
10876 vcall_offset);
10877 }
10878 /* Add the initializer to the vtable. */
10879 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10880 }
10881 }
10882
10883 /* Return vtbl initializers for the RTTI entries corresponding to the
10884 BINFO's vtable. The RTTI entries should indicate the object given
10885 by VID->rtti_binfo. */
10886
10887 static void
10888 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10889 {
10890 tree b;
10891 tree t;
10892 tree offset;
10893 tree decl;
10894 tree init;
10895
10896 t = BINFO_TYPE (vid->rtti_binfo);
10897
10898 /* To find the complete object, we will first convert to our most
10899 primary base, and then add the offset in the vtbl to that value. */
10900 b = most_primary_binfo (binfo);
10901 offset = size_diffop_loc (input_location,
10902 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10903
10904 /* The second entry is the address of the typeinfo object. */
10905 if (flag_rtti)
10906 decl = build_address (get_tinfo_decl (t));
10907 else
10908 decl = integer_zero_node;
10909
10910 /* Convert the declaration to a type that can be stored in the
10911 vtable. */
10912 init = build_nop (vfunc_ptr_type_node, decl);
10913 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10914
10915 /* Add the offset-to-top entry. It comes earlier in the vtable than
10916 the typeinfo entry. Convert the offset to look like a
10917 function pointer, so that we can put it in the vtable. */
10918 init = build_nop (vfunc_ptr_type_node, offset);
10919 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10920 }
10921
10922 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10923 accessibility. */
10924
10925 bool
10926 uniquely_derived_from_p (tree parent, tree type)
10927 {
10928 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10929 return base && base != error_mark_node;
10930 }
10931
10932 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10933
10934 bool
10935 publicly_uniquely_derived_p (tree parent, tree type)
10936 {
10937 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10938 NULL, tf_none);
10939 return base && base != error_mark_node;
10940 }
10941
10942 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10943 class between them, if any. */
10944
10945 tree
10946 common_enclosing_class (tree ctx1, tree ctx2)
10947 {
10948 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10949 return NULL_TREE;
10950 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10951 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10952 if (ctx1 == ctx2)
10953 return ctx1;
10954 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10955 TYPE_MARKED_P (t) = true;
10956 tree found = NULL_TREE;
10957 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10958 if (TYPE_MARKED_P (t))
10959 {
10960 found = t;
10961 break;
10962 }
10963 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10964 TYPE_MARKED_P (t) = false;
10965 return found;
10966 }
10967
10968 #include "gt-cp-class.h"