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