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