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