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