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