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