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