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