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