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