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