]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/class.c
Optimize load double into xmm with zero_extend
[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);
10746f37
JM
142static void check_field_decl (tree, tree, int *, int *, int *);
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
JM
349 {
350 rvalue = !real_lvalue_p (expr);
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;
a6f5e048 804 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;
2038 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
3080 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
3081 we also allow unnamed types used for defining fields. */
3082 if (DECL_ARTIFICIAL (elt)
3083 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3084 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
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
3544static void
94edc4ab 3545check_field_decl (tree field,
0cbd7506
MS
3546 tree t,
3547 int* cant_have_const_ctor,
3548 int* no_const_asn_ref,
10746f37 3549 int* any_default_members)
1e30f9b4
MM
3550{
3551 tree type = strip_array_types (TREE_TYPE (field));
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 {
3561 tree fields;
3562
910ad8de 3563 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
17aec3eb 3564 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
1e30f9b4 3565 check_field_decl (fields, t, cant_have_const_ctor,
10746f37 3566 no_const_asn_ref, any_default_members);
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
MM
3622 if (DECL_INITIAL (field) != NULL_TREE)
3623 {
3624 /* `build_class_init_list' does not recognize
3625 non-FIELD_DECLs. */
0e5f8a59 3626 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
1f070f2b 3627 error ("multiple fields in union %qT initialized", t);
1e30f9b4
MM
3628 *any_default_members = 1;
3629 }
6bb88f3b 3630}
1e30f9b4 3631
08b962b0
MM
3632/* Check the data members (both static and non-static), class-scoped
3633 typedefs, etc., appearing in the declaration of T. Issue
3634 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3635 declaration order) of access declarations; each TREE_VALUE in this
3636 list is a USING_DECL.
8d08fdba 3637
08b962b0 3638 In addition, set the following flags:
8d08fdba 3639
08b962b0
MM
3640 EMPTY_P
3641 The class is empty, i.e., contains no non-static data members.
8d08fdba 3642
08b962b0
MM
3643 CANT_HAVE_CONST_CTOR_P
3644 This class cannot have an implicitly generated copy constructor
3645 taking a const reference.
8d08fdba 3646
08b962b0
MM
3647 CANT_HAVE_CONST_ASN_REF
3648 This class cannot have an implicitly generated assignment
3649 operator taking a const reference.
8d08fdba 3650
08b962b0
MM
3651 All of these flags should be initialized before calling this
3652 function.
8d08fdba 3653
08b962b0
MM
3654 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3655 fields can be added by adding to this chain. */
8d08fdba 3656
607cf131 3657static void
58731fd1 3658check_field_decls (tree t, tree *access_decls,
58731fd1 3659 int *cant_have_const_ctor_p,
10746f37 3660 int *no_const_asn_ref_p)
08b962b0
MM
3661{
3662 tree *field;
3663 tree *next;
dd29d26b 3664 bool has_pointers;
08b962b0 3665 int any_default_members;
22002050 3666 int cant_pack = 0;
c32097d8 3667 int field_access = -1;
08b962b0
MM
3668
3669 /* Assume there are no access declarations. */
3670 *access_decls = NULL_TREE;
3671 /* Assume this class has no pointer members. */
dd29d26b 3672 has_pointers = false;
08b962b0
MM
3673 /* Assume none of the members of this class have default
3674 initializations. */
3675 any_default_members = 0;
3676
3677 for (field = &TYPE_FIELDS (t); *field; field = next)
8d08fdba 3678 {
08b962b0
MM
3679 tree x = *field;
3680 tree type = TREE_TYPE (x);
c32097d8 3681 int this_field_access;
8d08fdba 3682
910ad8de 3683 next = &DECL_CHAIN (x);
8d08fdba 3684
cffa8729 3685 if (TREE_CODE (x) == USING_DECL)
f30432d7 3686 {
08b962b0
MM
3687 /* Save the access declarations for our caller. */
3688 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
f30432d7
MS
3689 continue;
3690 }
8d08fdba 3691
050367a3
MM
3692 if (TREE_CODE (x) == TYPE_DECL
3693 || TREE_CODE (x) == TEMPLATE_DECL)
f30432d7 3694 continue;
8d08fdba 3695
f30432d7 3696 /* If we've gotten this far, it's a data member, possibly static,
e92cc029 3697 or an enumerator. */
8d0d1915
JM
3698 if (TREE_CODE (x) != CONST_DECL)
3699 DECL_CONTEXT (x) = t;
8d08fdba 3700
58ec3cc5
MM
3701 /* When this goes into scope, it will be a non-local reference. */
3702 DECL_NONLOCAL (x) = 1;
3703
4dadc66d
PC
3704 if (TREE_CODE (t) == UNION_TYPE
3705 && cxx_dialect < cxx11)
58ec3cc5 3706 {
4dadc66d 3707 /* [class.union] (C++98)
58ec3cc5
MM
3708
3709 If a union contains a static data member, or a member of
4dadc66d
PC
3710 reference type, the program is ill-formed.
3711
3712 In C++11 this limitation doesn't exist anymore. */
5a6ccc94 3713 if (VAR_P (x))
58ec3cc5 3714 {
4dadc66d
PC
3715 error ("in C++98 %q+D may not be static because it is "
3716 "a member of a union", x);
58ec3cc5
MM
3717 continue;
3718 }
3719 if (TREE_CODE (type) == REFERENCE_TYPE)
3720 {
4dadc66d
PC
3721 error ("in C++98 %q+D may not have reference type %qT "
3722 "because it is a member of a union", x, type);
58ec3cc5
MM
3723 continue;
3724 }
3725 }
3726
f30432d7
MS
3727 /* Perform error checking that did not get done in
3728 grokdeclarator. */
52fb2769 3729 if (TREE_CODE (type) == FUNCTION_TYPE)
f30432d7 3730 {
dee15844 3731 error ("field %q+D invalidly declared function type", x);
52fb2769
NS
3732 type = build_pointer_type (type);
3733 TREE_TYPE (x) = type;
f30432d7 3734 }
52fb2769 3735 else if (TREE_CODE (type) == METHOD_TYPE)
f30432d7 3736 {
dee15844 3737 error ("field %q+D invalidly declared method type", x);
52fb2769
NS
3738 type = build_pointer_type (type);
3739 TREE_TYPE (x) = type;
f30432d7 3740 }
8d08fdba 3741
52fb2769 3742 if (type == error_mark_node)
f30432d7 3743 continue;
c8094d83 3744
5a6ccc94 3745 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
73a8adb6 3746 continue;
8d08fdba 3747
f30432d7 3748 /* Now it can only be a FIELD_DECL. */
8d08fdba 3749
f30432d7 3750 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
08b962b0 3751 CLASSTYPE_NON_AGGREGATE (t) = 1;
8d08fdba 3752
3b49d762 3753 /* If at least one non-static data member is non-literal, the whole
cec362c9
PC
3754 class becomes non-literal. Per Core/1453, volatile non-static
3755 data members and base classes are also not allowed.
3756 Note: if the type is incomplete we will complain later on. */
3757 if (COMPLETE_TYPE_P (type)
3758 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3b49d762
GDR
3759 CLASSTYPE_LITERAL_P (t) = false;
3760
c32097d8
JM
3761 /* A standard-layout class is a class that:
3762 ...
3763 has the same access control (Clause 11) for all non-static data members,
3764 ... */
3765 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3766 if (field_access == -1)
3767 field_access = this_field_access;
3768 else if (this_field_access != field_access)
3769 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3770
0fcedd9c 3771 /* If this is of reference type, check if it needs an init. */
52fb2769 3772 if (TREE_CODE (type) == REFERENCE_TYPE)
0cbd7506 3773 {
c32097d8
JM
3774 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3775 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
f30432d7 3776 if (DECL_INITIAL (x) == NULL_TREE)
6eb35968 3777 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
864822bd
VV
3778 if (cxx_dialect < cxx11)
3779 {
3780 /* ARM $12.6.2: [A member initializer list] (or, for an
3781 aggregate, initialization by a brace-enclosed list) is the
3782 only way to initialize nonstatic const and reference
3783 members. */
3784 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3785 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3786 }
f30432d7 3787 }
8d08fdba 3788
1e30f9b4 3789 type = strip_array_types (type);
dd29d26b 3790
1937f939
JM
3791 if (TYPE_PACKED (t))
3792 {
c32097d8 3793 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
4666cd04 3794 {
15827d12
PC
3795 warning_at
3796 (DECL_SOURCE_LOCATION (x), 0,
3797 "ignoring packed attribute because of unpacked non-POD field %q#D",
4666cd04 3798 x);
22002050 3799 cant_pack = 1;
4666cd04 3800 }
2cd36c22
AN
3801 else if (DECL_C_BIT_FIELD (x)
3802 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
1937f939
JM
3803 DECL_PACKED (x) = 1;
3804 }
3805
3806 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3807 /* We don't treat zero-width bitfields as making a class
3808 non-empty. */
3809 ;
3810 else
3811 {
3812 /* The class is non-empty. */
3813 CLASSTYPE_EMPTY_P (t) = 0;
3814 /* The class is not even nearly empty. */
3815 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3816 /* If one of the data members contains an empty class,
3817 so does T. */
3818 if (CLASS_TYPE_P (type)
3819 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3820 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3821 }
3822
dd29d26b
GB
3823 /* This is used by -Weffc++ (see below). Warn only for pointers
3824 to members which might hold dynamic memory. So do not warn
3825 for pointers to functions or pointers to members. */
3826 if (TYPE_PTR_P (type)
66b1156a 3827 && !TYPE_PTRFN_P (type))
dd29d26b 3828 has_pointers = true;
824b9a4c 3829
58ec3cc5
MM
3830 if (CLASS_TYPE_P (type))
3831 {
3832 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3833 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3834 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3835 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3836 }
3837
52fb2769 3838 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
08b962b0 3839 CLASSTYPE_HAS_MUTABLE (t) = 1;
a7a7710d 3840
42306d73
PC
3841 if (DECL_MUTABLE_P (x))
3842 {
3843 if (CP_TYPE_CONST_P (type))
3844 {
3845 error ("member %q+D cannot be declared both %<const%> "
3846 "and %<mutable%>", x);
3847 continue;
3848 }
3849 if (TREE_CODE (type) == REFERENCE_TYPE)
3850 {
3851 error ("member %q+D cannot be declared as a %<mutable%> "
3852 "reference", x);
3853 continue;
3854 }
3855 }
3856
c32097d8 3857 if (! layout_pod_type_p (type))
0cbd7506
MS
3858 /* DR 148 now allows pointers to members (which are POD themselves),
3859 to be allowed in POD structs. */
c32097d8
JM
3860 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3861
3862 if (!std_layout_type_p (type))
3863 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
52fb2769 3864
94e6e4c4
AO
3865 if (! zero_init_p (type))
3866 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3867
640c2adf
FC
3868 /* We set DECL_C_BIT_FIELD in grokbitfield.
3869 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3870 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3871 check_field_decl (x, t,
3872 cant_have_const_ctor_p,
3873 no_const_asn_ref_p,
10746f37 3874 &any_default_members);
640c2adf 3875
ec3ebf45
OG
3876 /* Now that we've removed bit-field widths from DECL_INITIAL,
3877 anything left in DECL_INITIAL is an NSDMI that makes the class
3e605b20
JM
3878 non-aggregate in C++11. */
3879 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
ec3ebf45
OG
3880 CLASSTYPE_NON_AGGREGATE (t) = true;
3881
f30432d7 3882 /* If any field is const, the structure type is pseudo-const. */
52fb2769 3883 if (CP_TYPE_CONST_P (type))
f30432d7
MS
3884 {
3885 C_TYPE_FIELDS_READONLY (t) = 1;
3886 if (DECL_INITIAL (x) == NULL_TREE)
6eb35968 3887 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
864822bd
VV
3888 if (cxx_dialect < cxx11)
3889 {
3890 /* ARM $12.6.2: [A member initializer list] (or, for an
3891 aggregate, initialization by a brace-enclosed list) is the
3892 only way to initialize nonstatic const and reference
3893 members. */
3894 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3895 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3896 }
f30432d7 3897 }
08b962b0 3898 /* A field that is pseudo-const makes the structure likewise. */
5552b43c 3899 else if (CLASS_TYPE_P (type))
f30432d7 3900 {
08b962b0 3901 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
6eb35968
DE
3902 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3903 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3904 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
f30432d7 3905 }
8d08fdba 3906
c10bffd0
JM
3907 /* Core issue 80: A nonstatic data member is required to have a
3908 different name from the class iff the class has a
b87d79e6 3909 user-declared constructor. */
0fcedd9c
JM
3910 if (constructor_name_p (DECL_NAME (x), t)
3911 && TYPE_HAS_USER_CONSTRUCTOR (t))
15827d12
PC
3912 permerror (DECL_SOURCE_LOCATION (x),
3913 "field %q#D with same name as class", x);
8d08fdba
MS
3914 }
3915
dd29d26b
GB
3916 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3917 it should also define a copy constructor and an assignment operator to
3918 implement the correct copy semantic (deep vs shallow, etc.). As it is
3919 not feasible to check whether the constructors do allocate dynamic memory
3920 and store it within members, we approximate the warning like this:
3921
3922 -- Warn only if there are members which are pointers
3923 -- Warn only if there is a non-trivial constructor (otherwise,
3924 there cannot be memory allocated).
3925 -- Warn only if there is a non-trivial destructor. We assume that the
3926 user at least implemented the cleanup correctly, and a destructor
3927 is needed to free dynamic memory.
c8094d83 3928
77880ae4 3929 This seems enough for practical purposes. */
22002050
JM
3930 if (warn_ecpp
3931 && has_pointers
0fcedd9c 3932 && TYPE_HAS_USER_CONSTRUCTOR (t)
22002050 3933 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
066ec0a4 3934 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
824b9a4c 3935 {
b323323f 3936 warning (OPT_Weffc__, "%q#T has pointer data members", t);
c8094d83 3937
066ec0a4 3938 if (! TYPE_HAS_COPY_CTOR (t))
824b9a4c 3939 {
74fa0285 3940 warning (OPT_Weffc__,
3db45ab5 3941 " but does not override %<%T(const %T&)%>", t, t);
066ec0a4 3942 if (!TYPE_HAS_COPY_ASSIGN (t))
74fa0285 3943 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
824b9a4c 3944 }
066ec0a4 3945 else if (! TYPE_HAS_COPY_ASSIGN (t))
74fa0285 3946 warning (OPT_Weffc__,
3db45ab5 3947 " but does not override %<operator=(const %T&)%>", t);
824b9a4c 3948 }
08b962b0 3949
0e5f8a59
JM
3950 /* Non-static data member initializers make the default constructor
3951 non-trivial. */
3952 if (any_default_members)
3953 {
3954 TYPE_NEEDS_CONSTRUCTING (t) = true;
3955 TYPE_HAS_COMPLEX_DFLT (t) = true;
3956 }
3957
22002050
JM
3958 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3959 if (cant_pack)
3960 TYPE_PACKED (t) = 0;
607cf131
MM
3961
3962 /* Check anonymous struct/anonymous union fields. */
3963 finish_struct_anon (t);
3964
08b962b0
MM
3965 /* We've built up the list of access declarations in reverse order.
3966 Fix that now. */
3967 *access_decls = nreverse (*access_decls);
08b962b0
MM
3968}
3969
c20118a8
MM
3970/* If TYPE is an empty class type, records its OFFSET in the table of
3971 OFFSETS. */
607cf131 3972
c20118a8 3973static int
94edc4ab 3974record_subobject_offset (tree type, tree offset, splay_tree offsets)
5c24fba6 3975{
c20118a8 3976 splay_tree_node n;
5c24fba6 3977
c20118a8
MM
3978 if (!is_empty_class (type))
3979 return 0;
5c24fba6 3980
c20118a8
MM
3981 /* Record the location of this empty object in OFFSETS. */
3982 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3983 if (!n)
c8094d83 3984 n = splay_tree_insert (offsets,
c20118a8
MM
3985 (splay_tree_key) offset,
3986 (splay_tree_value) NULL_TREE);
c8094d83 3987 n->value = ((splay_tree_value)
c20118a8
MM
3988 tree_cons (NULL_TREE,
3989 type,
3990 (tree) n->value));
3991
3992 return 0;
607cf131
MM
3993}
3994
838dfd8a 3995/* Returns nonzero if TYPE is an empty class type and there is
c20118a8 3996 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
9785e4b1 3997
c20118a8 3998static int
94edc4ab 3999check_subobject_offset (tree type, tree offset, splay_tree offsets)
9785e4b1 4000{
c20118a8
MM
4001 splay_tree_node n;
4002 tree t;
4003
4004 if (!is_empty_class (type))
4005 return 0;
4006
4007 /* Record the location of this empty object in OFFSETS. */
4008 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4009 if (!n)
4010 return 0;
4011
4012 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4013 if (same_type_p (TREE_VALUE (t), type))
4014 return 1;
4015
4016 return 0;
9785e4b1
MM
4017}
4018
c20118a8
MM
4019/* Walk through all the subobjects of TYPE (located at OFFSET). Call
4020 F for every subobject, passing it the type, offset, and table of
2003cd37
MM
4021 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4022 be traversed.
5cdba4ff
MM
4023
4024 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4025 than MAX_OFFSET will not be walked.
4026
838dfd8a 4027 If F returns a nonzero value, the traversal ceases, and that value
5cdba4ff 4028 is returned. Otherwise, returns zero. */
d77249e7 4029
c20118a8 4030static int
c8094d83 4031walk_subobject_offsets (tree type,
0cbd7506
MS
4032 subobject_offset_fn f,
4033 tree offset,
4034 splay_tree offsets,
4035 tree max_offset,
4036 int vbases_p)
5c24fba6 4037{
c20118a8 4038 int r = 0;
ff944b49 4039 tree type_binfo = NULL_TREE;
c20118a8 4040
5cdba4ff
MM
4041 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4042 stop. */
807e902e 4043 if (max_offset && tree_int_cst_lt (max_offset, offset))
5cdba4ff
MM
4044 return 0;
4045
dbe91deb
NS
4046 if (type == error_mark_node)
4047 return 0;
3db45ab5 4048
c8094d83 4049 if (!TYPE_P (type))
ff944b49 4050 {
90d84934 4051 type_binfo = type;
ff944b49
MM
4052 type = BINFO_TYPE (type);
4053 }
4054
c20118a8 4055 if (CLASS_TYPE_P (type))
5c24fba6 4056 {
c20118a8 4057 tree field;
17bbb839 4058 tree binfo;
c20118a8
MM
4059 int i;
4060
5ec1192e
MM
4061 /* Avoid recursing into objects that are not interesting. */
4062 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4063 return 0;
4064
c20118a8
MM
4065 /* Record the location of TYPE. */
4066 r = (*f) (type, offset, offsets);
4067 if (r)
4068 return r;
4069
4070 /* Iterate through the direct base classes of TYPE. */
ff944b49
MM
4071 if (!type_binfo)
4072 type_binfo = TYPE_BINFO (type);
fa743e8c 4073 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
c20118a8 4074 {
ff944b49
MM
4075 tree binfo_offset;
4076
90d84934 4077 if (BINFO_VIRTUAL_P (binfo))
17bbb839 4078 continue;
5c24fba6 4079
90d84934
JM
4080 tree orig_binfo;
4081 /* We cannot rely on BINFO_OFFSET being set for the base
4082 class yet, but the offsets for direct non-virtual
4083 bases can be calculated by going back to the TYPE. */
4084 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4085 binfo_offset = size_binop (PLUS_EXPR,
4086 offset,
4087 BINFO_OFFSET (orig_binfo));
ff944b49
MM
4088
4089 r = walk_subobject_offsets (binfo,
c20118a8 4090 f,
ff944b49 4091 binfo_offset,
c20118a8 4092 offsets,
5cdba4ff 4093 max_offset,
90d84934 4094 /*vbases_p=*/0);
c20118a8
MM
4095 if (r)
4096 return r;
4097 }
4098
90d84934 4099 if (CLASSTYPE_VBASECLASSES (type))
17bbb839 4100 {
58c42dc2 4101 unsigned ix;
9771b263 4102 vec<tree, va_gc> *vbases;
17bbb839 4103
ff944b49
MM
4104 /* Iterate through the virtual base classes of TYPE. In G++
4105 3.2, we included virtual bases in the direct base class
4106 loop above, which results in incorrect results; the
4107 correct offsets for virtual bases are only known when
4108 working with the most derived type. */
4109 if (vbases_p)
9ba5ff0f 4110 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
9771b263 4111 vec_safe_iterate (vbases, ix, &binfo); ix++)
ff944b49 4112 {
ff944b49
MM
4113 r = walk_subobject_offsets (binfo,
4114 f,
4115 size_binop (PLUS_EXPR,
4116 offset,
4117 BINFO_OFFSET (binfo)),
4118 offsets,
4119 max_offset,
4120 /*vbases_p=*/0);
4121 if (r)
4122 return r;
4123 }
4124 else
17bbb839 4125 {
ff944b49
MM
4126 /* We still have to walk the primary base, if it is
4127 virtual. (If it is non-virtual, then it was walked
4128 above.) */
58c42dc2 4129 tree vbase = get_primary_binfo (type_binfo);
c8094d83 4130
809e3e7f 4131 if (vbase && BINFO_VIRTUAL_P (vbase)
fc6633e0
NS
4132 && BINFO_PRIMARY_P (vbase)
4133 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
ff944b49 4134 {
c8094d83 4135 r = (walk_subobject_offsets
dbbf88d1
NS
4136 (vbase, f, offset,
4137 offsets, max_offset, /*vbases_p=*/0));
4138 if (r)
4139 return r;
ff944b49 4140 }
17bbb839
MM
4141 }
4142 }
4143
c20118a8 4144 /* Iterate through the fields of TYPE. */
910ad8de 4145 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
e765a228
JM
4146 if (TREE_CODE (field) == FIELD_DECL
4147 && TREE_TYPE (field) != error_mark_node
4148 && !DECL_ARTIFICIAL (field))
c20118a8 4149 {
956d9305
MM
4150 tree field_offset;
4151
90d84934 4152 field_offset = byte_position (field);
956d9305 4153
c20118a8
MM
4154 r = walk_subobject_offsets (TREE_TYPE (field),
4155 f,
4156 size_binop (PLUS_EXPR,
4157 offset,
956d9305 4158 field_offset),
c20118a8 4159 offsets,
5cdba4ff 4160 max_offset,
c20118a8
MM
4161 /*vbases_p=*/1);
4162 if (r)
4163 return r;
4164 }
5c24fba6 4165 }
c20118a8
MM
4166 else if (TREE_CODE (type) == ARRAY_TYPE)
4167 {
5ec1192e 4168 tree element_type = strip_array_types (type);
c20118a8
MM
4169 tree domain = TYPE_DOMAIN (type);
4170 tree index;
5c24fba6 4171
5ec1192e
MM
4172 /* Avoid recursing into objects that are not interesting. */
4173 if (!CLASS_TYPE_P (element_type)
7e9a3ad3 4174 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
05dd97db 4175 || !domain)
5ec1192e
MM
4176 return 0;
4177
c20118a8 4178 /* Step through each of the elements in the array. */
17bbb839 4179 for (index = size_zero_node;
90d84934 4180 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
c20118a8
MM
4181 index = size_binop (PLUS_EXPR, index, size_one_node))
4182 {
4183 r = walk_subobject_offsets (TREE_TYPE (type),
4184 f,
4185 offset,
4186 offsets,
5cdba4ff 4187 max_offset,
c20118a8
MM
4188 /*vbases_p=*/1);
4189 if (r)
4190 return r;
c8094d83 4191 offset = size_binop (PLUS_EXPR, offset,
c20118a8 4192 TYPE_SIZE_UNIT (TREE_TYPE (type)));
5cdba4ff
MM
4193 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4194 there's no point in iterating through the remaining
4195 elements of the array. */
807e902e 4196 if (max_offset && tree_int_cst_lt (max_offset, offset))
5cdba4ff 4197 break;
c20118a8
MM
4198 }
4199 }
4200
4201 return 0;
4202}
4203
c0572427
MM
4204/* Record all of the empty subobjects of TYPE (either a type or a
4205 binfo). If IS_DATA_MEMBER is true, then a non-static data member
c5a35c3c
MM
4206 is being placed at OFFSET; otherwise, it is a base class that is
4207 being placed at OFFSET. */
c20118a8
MM
4208
4209static void
c8094d83 4210record_subobject_offsets (tree type,
0cbd7506
MS
4211 tree offset,
4212 splay_tree offsets,
c5a35c3c 4213 bool is_data_member)
c20118a8 4214{
c5a35c3c 4215 tree max_offset;
c0572427
MM
4216 /* If recording subobjects for a non-static data member or a
4217 non-empty base class , we do not need to record offsets beyond
4218 the size of the biggest empty class. Additional data members
4219 will go at the end of the class. Additional base classes will go
4220 either at offset zero (if empty, in which case they cannot
4221 overlap with offsets past the size of the biggest empty class) or
4222 at the end of the class.
4223
4224 However, if we are placing an empty base class, then we must record
c5a35c3c
MM
4225 all offsets, as either the empty class is at offset zero (where
4226 other empty classes might later be placed) or at the end of the
4227 class (where other objects might then be placed, so other empty
4228 subobjects might later overlap). */
3db45ab5 4229 if (is_data_member
c0572427 4230 || !is_empty_class (BINFO_TYPE (type)))
c5a35c3c
MM
4231 max_offset = sizeof_biggest_empty_class;
4232 else
4233 max_offset = NULL_TREE;
c20118a8 4234 walk_subobject_offsets (type, record_subobject_offset, offset,
c5a35c3c 4235 offsets, max_offset, is_data_member);
5c24fba6
MM
4236}
4237
838dfd8a
KH
4238/* Returns nonzero if any of the empty subobjects of TYPE (located at
4239 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
c20118a8 4240 virtual bases of TYPE are examined. */
9785e4b1
MM
4241
4242static int
94edc4ab 4243layout_conflict_p (tree type,
0cbd7506
MS
4244 tree offset,
4245 splay_tree offsets,
4246 int vbases_p)
9785e4b1 4247{
5cdba4ff
MM
4248 splay_tree_node max_node;
4249
4250 /* Get the node in OFFSETS that indicates the maximum offset where
4251 an empty subobject is located. */
4252 max_node = splay_tree_max (offsets);
4253 /* If there aren't any empty subobjects, then there's no point in
4254 performing this check. */
4255 if (!max_node)
4256 return 0;
4257
c20118a8 4258 return walk_subobject_offsets (type, check_subobject_offset, offset,
5cdba4ff
MM
4259 offsets, (tree) (max_node->key),
4260 vbases_p);
9785e4b1
MM
4261}
4262
5c24fba6
MM
4263/* DECL is a FIELD_DECL corresponding either to a base subobject of a
4264 non-static data member of the type indicated by RLI. BINFO is the
c20118a8 4265 binfo corresponding to the base subobject, OFFSETS maps offsets to
17bbb839
MM
4266 types already located at those offsets. This function determines
4267 the position of the DECL. */
5c24fba6
MM
4268
4269static void
c8094d83
MS
4270layout_nonempty_base_or_field (record_layout_info rli,
4271 tree decl,
4272 tree binfo,
17bbb839 4273 splay_tree offsets)
5c24fba6 4274{
c20118a8 4275 tree offset = NULL_TREE;
17bbb839
MM
4276 bool field_p;
4277 tree type;
c8094d83 4278
17bbb839
MM
4279 if (binfo)
4280 {
4281 /* For the purposes of determining layout conflicts, we want to
4282 use the class type of BINFO; TREE_TYPE (DECL) will be the
4283 CLASSTYPE_AS_BASE version, which does not contain entries for
4284 zero-sized bases. */
4285 type = TREE_TYPE (binfo);
4286 field_p = false;
4287 }
4288 else
4289 {
4290 type = TREE_TYPE (decl);
4291 field_p = true;
4292 }
c20118a8 4293
5c24fba6
MM
4294 /* Try to place the field. It may take more than one try if we have
4295 a hard time placing the field without putting two objects of the
4296 same type at the same address. */
4297 while (1)
4298 {
defd0dea 4299 struct record_layout_info_s old_rli = *rli;
5c24fba6 4300
770ae6cc
RK
4301 /* Place this field. */
4302 place_field (rli, decl);
da3d4dfa 4303 offset = byte_position (decl);
1e2e9f54 4304
5c24fba6
MM
4305 /* We have to check to see whether or not there is already
4306 something of the same type at the offset we're about to use.
1e2e9f54 4307 For example, consider:
c8094d83 4308
1e2e9f54
MM
4309 struct S {};
4310 struct T : public S { int i; };
4311 struct U : public S, public T {};
c8094d83 4312
5c24fba6
MM
4313 Here, we put S at offset zero in U. Then, we can't put T at
4314 offset zero -- its S component would be at the same address
4315 as the S we already allocated. So, we have to skip ahead.
4316 Since all data members, including those whose type is an
838dfd8a 4317 empty class, have nonzero size, any overlap can happen only
5c24fba6
MM
4318 with a direct or indirect base-class -- it can't happen with
4319 a data member. */
1e2e9f54
MM
4320 /* In a union, overlap is permitted; all members are placed at
4321 offset zero. */
4322 if (TREE_CODE (rli->t) == UNION_TYPE)
4323 break;
c8094d83 4324 if (layout_conflict_p (field_p ? type : binfo, offset,
ff944b49 4325 offsets, field_p))
5c24fba6 4326 {
5c24fba6
MM
4327 /* Strip off the size allocated to this field. That puts us
4328 at the first place we could have put the field with
4329 proper alignment. */
770ae6cc
RK
4330 *rli = old_rli;
4331
c20118a8 4332 /* Bump up by the alignment required for the type. */
770ae6cc 4333 rli->bitpos
c8094d83
MS
4334 = size_binop (PLUS_EXPR, rli->bitpos,
4335 bitsize_int (binfo
c20118a8
MM
4336 ? CLASSTYPE_ALIGN (type)
4337 : TYPE_ALIGN (type)));
770ae6cc 4338 normalize_rli (rli);
5c24fba6 4339 }
d68f848b
JM
4340 else if (TREE_CODE (type) == NULLPTR_TYPE
4341 && warn_abi && abi_version_crosses (9))
4342 {
4343 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4344 the offset wasn't aligned like a pointer when we started to
4345 layout this field, that affects its position. */
4346 tree pos = rli_size_unit_so_far (&old_rli);
4347 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4348 {
4349 if (abi_version_at_least (9))
4350 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4351 "alignment of %qD increased in -fabi-version=9 "
4352 "(GCC 5.2)", decl);
4353 else
4354 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4355 "of %qD will increase in -fabi-version=9", decl);
4356 }
4357 break;
4358 }
5c24fba6
MM
4359 else
4360 /* There was no conflict. We're done laying out this field. */
4361 break;
4362 }
c20118a8 4363
623fe76a 4364 /* Now that we know where it will be placed, update its
c20118a8
MM
4365 BINFO_OFFSET. */
4366 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
90024bdc 4367 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
17bbb839
MM
4368 this point because their BINFO_OFFSET is copied from another
4369 hierarchy. Therefore, we may not need to add the entire
4370 OFFSET. */
c8094d83 4371 propagate_binfo_offsets (binfo,
db3927fb 4372 size_diffop_loc (input_location,
cda0a029
JM
4373 fold_convert (ssizetype, offset),
4374 fold_convert (ssizetype,
dbbf88d1 4375 BINFO_OFFSET (binfo))));
5c24fba6
MM
4376}
4377
90024bdc 4378/* Returns true if TYPE is empty and OFFSET is nonzero. */
7ba539c6
MM
4379
4380static int
4381empty_base_at_nonzero_offset_p (tree type,
4382 tree offset,
12308bc6 4383 splay_tree /*offsets*/)
7ba539c6
MM
4384{
4385 return is_empty_class (type) && !integer_zerop (offset);
4386}
4387
9785e4b1 4388/* Layout the empty base BINFO. EOC indicates the byte currently just
ec386958 4389 past the end of the class, and should be correctly aligned for a
c20118a8 4390 class of the type indicated by BINFO; OFFSETS gives the offsets of
623fe76a 4391 the empty bases allocated so far. T is the most derived
838dfd8a 4392 type. Return nonzero iff we added it at the end. */
9785e4b1 4393
06d9f09f 4394static bool
d9d9dbc0
JM
4395layout_empty_base (record_layout_info rli, tree binfo,
4396 tree eoc, splay_tree offsets)
9785e4b1 4397{
ec386958 4398 tree alignment;
9785e4b1 4399 tree basetype = BINFO_TYPE (binfo);
06d9f09f 4400 bool atend = false;
956d9305 4401
9785e4b1 4402 /* This routine should only be used for empty classes. */
50bc768d 4403 gcc_assert (is_empty_class (basetype));
1b50716d 4404 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
9785e4b1 4405
3075b327 4406 if (!integer_zerop (BINFO_OFFSET (binfo)))
90d84934
JM
4407 propagate_binfo_offsets
4408 (binfo, size_diffop_loc (input_location,
db3927fb 4409 size_zero_node, BINFO_OFFSET (binfo)));
c8094d83 4410
9785e4b1
MM
4411 /* This is an empty base class. We first try to put it at offset
4412 zero. */
ff944b49 4413 if (layout_conflict_p (binfo,
c20118a8 4414 BINFO_OFFSET (binfo),
c8094d83 4415 offsets,
c20118a8 4416 /*vbases_p=*/0))
9785e4b1
MM
4417 {
4418 /* That didn't work. Now, we move forward from the next
4419 available spot in the class. */
06d9f09f 4420 atend = true;
cda0a029 4421 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
c8094d83 4422 while (1)
9785e4b1 4423 {
ff944b49 4424 if (!layout_conflict_p (binfo,
c8094d83 4425 BINFO_OFFSET (binfo),
c20118a8
MM
4426 offsets,
4427 /*vbases_p=*/0))
9785e4b1
MM
4428 /* We finally found a spot where there's no overlap. */
4429 break;
4430
4431 /* There's overlap here, too. Bump along to the next spot. */
dbbf88d1 4432 propagate_binfo_offsets (binfo, alignment);
9785e4b1
MM
4433 }
4434 }
d9d9dbc0
JM
4435
4436 if (CLASSTYPE_USER_ALIGN (basetype))
4437 {
4438 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4439 if (warn_packed)
4440 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4441 TYPE_USER_ALIGN (rli->t) = 1;
4442 }
4443
06d9f09f 4444 return atend;
9785e4b1
MM
4445}
4446
78dcd41a 4447/* Layout the base given by BINFO in the class indicated by RLI.
58731fd1 4448 *BASE_ALIGN is a running maximum of the alignments of
17bbb839
MM
4449 any base class. OFFSETS gives the location of empty base
4450 subobjects. T is the most derived type. Return nonzero if the new
4451 object cannot be nearly-empty. A new FIELD_DECL is inserted at
c8094d83 4452 *NEXT_FIELD, unless BINFO is for an empty base class.
5c24fba6 4453
17bbb839
MM
4454 Returns the location at which the next field should be inserted. */
4455
4456static tree *
58731fd1 4457build_base_field (record_layout_info rli, tree binfo,
17bbb839 4458 splay_tree offsets, tree *next_field)
d77249e7 4459{
17bbb839 4460 tree t = rli->t;
d77249e7 4461 tree basetype = BINFO_TYPE (binfo);
d77249e7 4462
d0f062fb 4463 if (!COMPLETE_TYPE_P (basetype))
d77249e7
MM
4464 /* This error is now reported in xref_tag, thus giving better
4465 location information. */
17bbb839 4466 return next_field;
c8094d83 4467
17bbb839
MM
4468 /* Place the base class. */
4469 if (!is_empty_class (basetype))
5c24fba6 4470 {
17bbb839
MM
4471 tree decl;
4472
5c24fba6
MM
4473 /* The containing class is non-empty because it has a non-empty
4474 base class. */
58731fd1 4475 CLASSTYPE_EMPTY_P (t) = 0;
c8094d83 4476
17bbb839 4477 /* Create the FIELD_DECL. */
c2255bc4
AH
4478 decl = build_decl (input_location,
4479 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
17bbb839 4480 DECL_ARTIFICIAL (decl) = 1;
78e0d62b 4481 DECL_IGNORED_P (decl) = 1;
17bbb839 4482 DECL_FIELD_CONTEXT (decl) = t;
1ad8aeeb
DG
4483 if (CLASSTYPE_AS_BASE (basetype))
4484 {
4485 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4486 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4487 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4488 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4489 DECL_MODE (decl) = TYPE_MODE (basetype);
4490 DECL_FIELD_IS_BASE (decl) = 1;
4491
4492 /* Try to place the field. It may take more than one try if we
4493 have a hard time placing the field without putting two
4494 objects of the same type at the same address. */
4495 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4496 /* Add the new FIELD_DECL to the list of fields for T. */
910ad8de 4497 DECL_CHAIN (decl) = *next_field;
1ad8aeeb 4498 *next_field = decl;
910ad8de 4499 next_field = &DECL_CHAIN (decl);
1ad8aeeb 4500 }
5c24fba6
MM
4501 }
4502 else
ec386958 4503 {
17bbb839 4504 tree eoc;
7ba539c6 4505 bool atend;
ec386958
MM
4506
4507 /* On some platforms (ARM), even empty classes will not be
4508 byte-aligned. */
db3927fb
AH
4509 eoc = round_up_loc (input_location,
4510 rli_size_unit_so_far (rli),
17bbb839 4511 CLASSTYPE_ALIGN_UNIT (basetype));
d9d9dbc0 4512 atend = layout_empty_base (rli, binfo, eoc, offsets);
7ba539c6
MM
4513 /* A nearly-empty class "has no proper base class that is empty,
4514 not morally virtual, and at an offset other than zero." */
809e3e7f 4515 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
7ba539c6
MM
4516 {
4517 if (atend)
4518 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
c5a35c3c 4519 /* The check above (used in G++ 3.2) is insufficient because
7ba539c6 4520 an empty class placed at offset zero might itself have an
90024bdc 4521 empty base at a nonzero offset. */
c8094d83 4522 else if (walk_subobject_offsets (basetype,
7ba539c6
MM
4523 empty_base_at_nonzero_offset_p,
4524 size_zero_node,
4525 /*offsets=*/NULL,
4526 /*max_offset=*/NULL_TREE,
4527 /*vbases_p=*/true))
90d84934 4528 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
7ba539c6 4529 }
c8094d83 4530
17bbb839
MM
4531 /* We do not create a FIELD_DECL for empty base classes because
4532 it might overlap some other field. We want to be able to
4533 create CONSTRUCTORs for the class by iterating over the
4534 FIELD_DECLs, and the back end does not handle overlapping
4535 FIELD_DECLs. */
58731fd1
MM
4536
4537 /* An empty virtual base causes a class to be non-empty
4538 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4539 here because that was already done when the virtual table
4540 pointer was created. */
ec386958 4541 }
5c24fba6 4542
5c24fba6 4543 /* Record the offsets of BINFO and its base subobjects. */
ff944b49 4544 record_subobject_offsets (binfo,
c20118a8 4545 BINFO_OFFSET (binfo),
c8094d83 4546 offsets,
c5a35c3c 4547 /*is_data_member=*/false);
17bbb839
MM
4548
4549 return next_field;
d77249e7
MM
4550}
4551
c20118a8 4552/* Layout all of the non-virtual base classes. Record empty
17bbb839
MM
4553 subobjects in OFFSETS. T is the most derived type. Return nonzero
4554 if the type cannot be nearly empty. The fields created
4555 corresponding to the base classes will be inserted at
4556 *NEXT_FIELD. */
607cf131 4557
17bbb839 4558static void
58731fd1 4559build_base_fields (record_layout_info rli,
17bbb839 4560 splay_tree offsets, tree *next_field)
607cf131
MM
4561{
4562 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4563 subobjects. */
17bbb839 4564 tree t = rli->t;
604a3205 4565 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
5c24fba6 4566 int i;
607cf131 4567
3461fba7 4568 /* The primary base class is always allocated first. */
17bbb839
MM
4569 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4570 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
58731fd1 4571 offsets, next_field);
d77249e7
MM
4572
4573 /* Now allocate the rest of the bases. */
607cf131
MM
4574 for (i = 0; i < n_baseclasses; ++i)
4575 {
d77249e7 4576 tree base_binfo;
607cf131 4577
604a3205 4578 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
911a71a7 4579
3461fba7
NS
4580 /* The primary base was already allocated above, so we don't
4581 need to allocate it again here. */
17bbb839 4582 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
607cf131
MM
4583 continue;
4584
dbbf88d1
NS
4585 /* Virtual bases are added at the end (a primary virtual base
4586 will have already been added). */
809e3e7f 4587 if (BINFO_VIRTUAL_P (base_binfo))
607cf131
MM
4588 continue;
4589
58731fd1 4590 next_field = build_base_field (rli, base_binfo,
17bbb839 4591 offsets, next_field);
607cf131 4592 }
607cf131
MM
4593}
4594
58010b57
MM
4595/* Go through the TYPE_METHODS of T issuing any appropriate
4596 diagnostics, figuring out which methods override which other
3ef397c1 4597 methods, and so forth. */
58010b57
MM
4598
4599static void
94edc4ab 4600check_methods (tree t)
58010b57
MM
4601{
4602 tree x;
58010b57 4603
910ad8de 4604 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
58010b57 4605 {
58010b57 4606 check_for_override (x, t);
aaf8a23e 4607 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
dee15844 4608 error ("initializer specified for non-virtual method %q+D", x);
58010b57
MM
4609 /* The name of the field is the original field name
4610 Save this in auxiliary field for later overloading. */
aaf8a23e 4611 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
58010b57 4612 {
3ef397c1 4613 TYPE_POLYMORPHIC_P (t) = 1;
fee7654e 4614 if (DECL_PURE_VIRTUAL_P (x))
9771b263 4615 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
58010b57 4616 }
46408846
JM
4617 /* All user-provided destructors are non-trivial.
4618 Constructors and assignment ops are handled in
4619 grok_special_member_properties. */
20f2653e 4620 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
9f4faeae 4621 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
b8fd7909
JM
4622 if (!DECL_VIRTUAL_P (x)
4623 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x)))
4624 error_at (DECL_SOURCE_LOCATION (x),
4625 "%<transaction_safe_dynamic%> may only be specified for "
4626 "a virtual function");
58010b57 4627 }
58010b57
MM
4628}
4629
db9b2174
MM
4630/* FN is a constructor or destructor. Clone the declaration to create
4631 a specialized in-charge or not-in-charge version, as indicated by
4632 NAME. */
4633
4634static tree
94edc4ab 4635build_clone (tree fn, tree name)
db9b2174
MM
4636{
4637 tree parms;
4638 tree clone;
4639
4640 /* Copy the function. */
4641 clone = copy_decl (fn);
db9b2174
MM
4642 /* Reset the function name. */
4643 DECL_NAME (clone) = name;
b97e8a14
JM
4644 /* Remember where this function came from. */
4645 DECL_ABSTRACT_ORIGIN (clone) = fn;
4646 /* Make it easy to find the CLONE given the FN. */
910ad8de
NF
4647 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4648 DECL_CHAIN (fn) = clone;
b97e8a14
JM
4649
4650 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4651 if (TREE_CODE (clone) == TEMPLATE_DECL)
4652 {
4653 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4654 DECL_TEMPLATE_RESULT (clone) = result;
4655 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4656 DECL_TI_TEMPLATE (result) = clone;
4657 TREE_TYPE (clone) = TREE_TYPE (result);
4658 return clone;
4659 }
971e17ff
AS
4660 else
4661 {
4662 // Clone constraints.
4663 if (flag_concepts)
4664 if (tree ci = get_constraints (fn))
4665 set_constraints (clone, copy_node (ci));
4666 }
4667
b97e8a14 4668
ad115a3c 4669 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
b97e8a14 4670 DECL_CLONED_FUNCTION (clone) = fn;
db9b2174
MM
4671 /* There's no pending inline data for this function. */
4672 DECL_PENDING_INLINE_INFO (clone) = NULL;
4673 DECL_PENDING_INLINE_P (clone) = 0;
db9b2174 4674
298d6f60
MM
4675 /* The base-class destructor is not virtual. */
4676 if (name == base_dtor_identifier)
4677 {
4678 DECL_VIRTUAL_P (clone) = 0;
4679 if (TREE_CODE (clone) != TEMPLATE_DECL)
4680 DECL_VINDEX (clone) = NULL_TREE;
4681 }
4682
4e7512c9 4683 /* If there was an in-charge parameter, drop it from the function
db9b2174
MM
4684 type. */
4685 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4686 {
4687 tree basetype;
4688 tree parmtypes;
4689 tree exceptions;
4690
4691 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4692 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4693 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4694 /* Skip the `this' parameter. */
4695 parmtypes = TREE_CHAIN (parmtypes);
4696 /* Skip the in-charge parameter. */
4697 parmtypes = TREE_CHAIN (parmtypes);
e0fff4b3
JM
4698 /* And the VTT parm, in a complete [cd]tor. */
4699 if (DECL_HAS_VTT_PARM_P (fn)
4700 && ! DECL_NEEDS_VTT_PARM_P (clone))
4701 parmtypes = TREE_CHAIN (parmtypes);
3ec6bad3
MM
4702 /* If this is subobject constructor or destructor, add the vtt
4703 parameter. */
c8094d83 4704 TREE_TYPE (clone)
43dc123f
MM
4705 = build_method_type_directly (basetype,
4706 TREE_TYPE (TREE_TYPE (clone)),
4707 parmtypes);
db9b2174
MM
4708 if (exceptions)
4709 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4710 exceptions);
c8094d83 4711 TREE_TYPE (clone)
e9525111
MM
4712 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4713 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
db9b2174
MM
4714 }
4715
b97e8a14
JM
4716 /* Copy the function parameters. */
4717 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4718 /* Remove the in-charge parameter. */
4719 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4720 {
910ad8de
NF
4721 DECL_CHAIN (DECL_ARGUMENTS (clone))
4722 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
b97e8a14
JM
4723 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4724 }
4725 /* And the VTT parm, in a complete [cd]tor. */
4726 if (DECL_HAS_VTT_PARM_P (fn))
db9b2174 4727 {
b97e8a14
JM
4728 if (DECL_NEEDS_VTT_PARM_P (clone))
4729 DECL_HAS_VTT_PARM_P (clone) = 1;
4730 else
db9b2174 4731 {
910ad8de
NF
4732 DECL_CHAIN (DECL_ARGUMENTS (clone))
4733 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
b97e8a14 4734 DECL_HAS_VTT_PARM_P (clone) = 0;
3ec6bad3 4735 }
b97e8a14 4736 }
3ec6bad3 4737
910ad8de 4738 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
b97e8a14
JM
4739 {
4740 DECL_CONTEXT (parms) = clone;
4741 cxx_dup_lang_specific_decl (parms);
db9b2174
MM
4742 }
4743
db9b2174 4744 /* Create the RTL for this function. */
245763e3 4745 SET_DECL_RTL (clone, NULL);
0e6df31e 4746 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
c8094d83 4747
b97e8a14
JM
4748 return clone;
4749}
db9b2174 4750
b97e8a14
JM
4751/* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4752 not invoke this function directly.
4753
4754 For a non-thunk function, returns the address of the slot for storing
4755 the function it is a clone of. Otherwise returns NULL_TREE.
4756
4757 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4758 cloned_function is unset. This is to support the separate
4759 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4760 on a template makes sense, but not the former. */
4761
4762tree *
4763decl_cloned_function_p (const_tree decl, bool just_testing)
4764{
4765 tree *ptr;
4766 if (just_testing)
4767 decl = STRIP_TEMPLATE (decl);
4768
4769 if (TREE_CODE (decl) != FUNCTION_DECL
4770 || !DECL_LANG_SPECIFIC (decl)
4771 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4772 {
4773#if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4774 if (!just_testing)
4775 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4776 else
4777#endif
4778 return NULL;
db9b2174
MM
4779 }
4780
b97e8a14
JM
4781 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4782 if (just_testing && *ptr == NULL_TREE)
4783 return NULL;
4784 else
4785 return ptr;
db9b2174
MM
4786}
4787
4788/* Produce declarations for all appropriate clones of FN. If
838dfd8a 4789 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
db9b2174
MM
4790 CLASTYPE_METHOD_VEC as well. */
4791
4792void
94edc4ab 4793clone_function_decl (tree fn, int update_method_vec_p)
db9b2174
MM
4794{
4795 tree clone;
4796
c00996a3 4797 /* Avoid inappropriate cloning. */
910ad8de
NF
4798 if (DECL_CHAIN (fn)
4799 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
c00996a3
JM
4800 return;
4801
298d6f60 4802 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
db9b2174 4803 {
298d6f60
MM
4804 /* For each constructor, we need two variants: an in-charge version
4805 and a not-in-charge version. */
db9b2174
MM
4806 clone = build_clone (fn, complete_ctor_identifier);
4807 if (update_method_vec_p)
b2a9b208 4808 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
db9b2174
MM
4809 clone = build_clone (fn, base_ctor_identifier);
4810 if (update_method_vec_p)
b2a9b208 4811 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
db9b2174
MM
4812 }
4813 else
298d6f60 4814 {
50bc768d 4815 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
298d6f60 4816
3ec6bad3 4817 /* For each destructor, we need three variants: an in-charge
298d6f60 4818 version, a not-in-charge version, and an in-charge deleting
4e7512c9
MM
4819 version. We clone the deleting version first because that
4820 means it will go second on the TYPE_METHODS list -- and that
4821 corresponds to the correct layout order in the virtual
c8094d83 4822 function table.
52682a1b 4823
0cbd7506 4824 For a non-virtual destructor, we do not build a deleting
52682a1b
MM
4825 destructor. */
4826 if (DECL_VIRTUAL_P (fn))
4827 {
4828 clone = build_clone (fn, deleting_dtor_identifier);
4829 if (update_method_vec_p)
b2a9b208 4830 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
52682a1b 4831 }
4e7512c9 4832 clone = build_clone (fn, complete_dtor_identifier);
298d6f60 4833 if (update_method_vec_p)
b2a9b208 4834 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
298d6f60
MM
4835 clone = build_clone (fn, base_dtor_identifier);
4836 if (update_method_vec_p)
b2a9b208 4837 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
298d6f60 4838 }
5daf7c0a
JM
4839
4840 /* Note that this is an abstract function that is never emitted. */
00de328a 4841 DECL_ABSTRACT_P (fn) = true;
db9b2174
MM
4842}
4843
5f6eeeb3
NS
4844/* DECL is an in charge constructor, which is being defined. This will
4845 have had an in class declaration, from whence clones were
4846 declared. An out-of-class definition can specify additional default
4847 arguments. As it is the clones that are involved in overload
4848 resolution, we must propagate the information from the DECL to its
00a17e31 4849 clones. */
5f6eeeb3
NS
4850
4851void
94edc4ab 4852adjust_clone_args (tree decl)
5f6eeeb3
NS
4853{
4854 tree clone;
c8094d83 4855
910ad8de
NF
4856 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4857 clone = DECL_CHAIN (clone))
5f6eeeb3
NS
4858 {
4859 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4860 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4861 tree decl_parms, clone_parms;
4862
4863 clone_parms = orig_clone_parms;
c8094d83 4864
00a17e31 4865 /* Skip the 'this' parameter. */
5f6eeeb3
NS
4866 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4867 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4868
4869 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4870 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4871 if (DECL_HAS_VTT_PARM_P (decl))
4872 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
c8094d83 4873
5f6eeeb3
NS
4874 clone_parms = orig_clone_parms;
4875 if (DECL_HAS_VTT_PARM_P (clone))
4876 clone_parms = TREE_CHAIN (clone_parms);
c8094d83 4877
5f6eeeb3
NS
4878 for (decl_parms = orig_decl_parms; decl_parms;
4879 decl_parms = TREE_CHAIN (decl_parms),
4880 clone_parms = TREE_CHAIN (clone_parms))
4881 {
50bc768d
NS
4882 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4883 TREE_TYPE (clone_parms)));
c8094d83 4884
5f6eeeb3
NS
4885 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4886 {
4887 /* A default parameter has been added. Adjust the
00a17e31 4888 clone's parameters. */
5f6eeeb3 4889 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3c3905fc 4890 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
5f6eeeb3
NS
4891 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4892 tree type;
4893
4894 clone_parms = orig_decl_parms;
4895
4896 if (DECL_HAS_VTT_PARM_P (clone))
4897 {
4898 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4899 TREE_VALUE (orig_clone_parms),
4900 clone_parms);
4901 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4902 }
43dc123f
MM
4903 type = build_method_type_directly (basetype,
4904 TREE_TYPE (TREE_TYPE (clone)),
4905 clone_parms);
5f6eeeb3
NS
4906 if (exceptions)
4907 type = build_exception_variant (type, exceptions);
3c3905fc
JM
4908 if (attrs)
4909 type = cp_build_type_attribute_variant (type, attrs);
5f6eeeb3 4910 TREE_TYPE (clone) = type;
c8094d83 4911
5f6eeeb3
NS
4912 clone_parms = NULL_TREE;
4913 break;
4914 }
4915 }
50bc768d 4916 gcc_assert (!clone_parms);
5f6eeeb3
NS
4917 }
4918}
4919
db9b2174
MM
4920/* For each of the constructors and destructors in T, create an
4921 in-charge and not-in-charge variant. */
4922
4923static void
94edc4ab 4924clone_constructors_and_destructors (tree t)
db9b2174
MM
4925{
4926 tree fns;
4927
db9b2174
MM
4928 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4929 out now. */
4930 if (!CLASSTYPE_METHOD_VEC (t))
4931 return;
4932
db9b2174
MM
4933 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4934 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
298d6f60
MM
4935 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4936 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
db9b2174
MM
4937}
4938
593a0835
PC
4939/* Deduce noexcept for a destructor DTOR. */
4940
4941void
4942deduce_noexcept_on_destructor (tree dtor)
4943{
4944 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4945 {
b15ea309 4946 tree eh_spec = unevaluated_noexcept_spec ();
593a0835
PC
4947 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4948 }
4949}
4950
4951/* For each destructor in T, deduce noexcept:
4952
4953 12.4/3: A declaration of a destructor that does not have an
4954 exception-specification is implicitly considered to have the
4955 same exception-specification as an implicit declaration (15.4). */
4956
4957static void
4958deduce_noexcept_on_destructors (tree t)
4959{
593a0835
PC
4960 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4961 out now. */
4962 if (!CLASSTYPE_METHOD_VEC (t))
4963 return;
4964
a5e90b2a 4965 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
593a0835
PC
4966 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4967}
4968
0a35513e
AH
4969/* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4970 of TYPE for virtual functions which FNDECL overrides. Return a
4971 mask of the tm attributes found therein. */
4972
4973static int
4974look_for_tm_attr_overrides (tree type, tree fndecl)
4975{
4976 tree binfo = TYPE_BINFO (type);
4977 tree base_binfo;
4978 int ix, found = 0;
4979
4980 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4981 {
4982 tree o, basetype = BINFO_TYPE (base_binfo);
4983
4984 if (!TYPE_POLYMORPHIC_P (basetype))
4985 continue;
4986
4987 o = look_for_overrides_here (basetype, fndecl);
4988 if (o)
b8fd7909
JM
4989 {
4990 if (lookup_attribute ("transaction_safe_dynamic",
4991 DECL_ATTRIBUTES (o)))
4992 /* transaction_safe_dynamic is not inherited. */;
4993 else
4994 found |= tm_attr_to_mask (find_tm_attribute
4995 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4996 }
0a35513e
AH
4997 else
4998 found |= look_for_tm_attr_overrides (basetype, fndecl);
4999 }
5000
5001 return found;
5002}
5003
5004/* Subroutine of set_method_tm_attributes. Handle the checks and
5005 inheritance for one virtual method FNDECL. */
5006
5007static void
5008set_one_vmethod_tm_attributes (tree type, tree fndecl)
5009{
5010 tree tm_attr;
5011 int found, have;
5012
5013 found = look_for_tm_attr_overrides (type, fndecl);
5014
5015 /* If FNDECL doesn't actually override anything (i.e. T is the
5016 class that first declares FNDECL virtual), then we're done. */
5017 if (found == 0)
5018 return;
5019
5020 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5021 have = tm_attr_to_mask (tm_attr);
5022
5023 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5024 tm_pure must match exactly, otherwise no weakening of
5025 tm_safe > tm_callable > nothing. */
5026 /* ??? The tm_pure attribute didn't make the transition to the
5027 multivendor language spec. */
5028 if (have == TM_ATTR_PURE)
5029 {
5030 if (found != TM_ATTR_PURE)
5031 {
5032 found &= -found;
5033 goto err_override;
5034 }
5035 }
5036 /* If the overridden function is tm_pure, then FNDECL must be. */
5037 else if (found == TM_ATTR_PURE && tm_attr)
5038 goto err_override;
5039 /* Look for base class combinations that cannot be satisfied. */
5040 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5041 {
5042 found &= ~TM_ATTR_PURE;
5043 found &= -found;
5044 error_at (DECL_SOURCE_LOCATION (fndecl),
5045 "method overrides both %<transaction_pure%> and %qE methods",
5046 tm_mask_to_attr (found));
5047 }
5048 /* If FNDECL did not declare an attribute, then inherit the most
5049 restrictive one. */
5050 else if (tm_attr == NULL)
5051 {
5052 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
5053 }
5054 /* Otherwise validate that we're not weaker than a function
5055 that is being overridden. */
5056 else
5057 {
5058 found &= -found;
5059 if (found <= TM_ATTR_CALLABLE && have > found)
5060 goto err_override;
5061 }
5062 return;
5063
5064 err_override:
5065 error_at (DECL_SOURCE_LOCATION (fndecl),
5066 "method declared %qE overriding %qE method",
5067 tm_attr, tm_mask_to_attr (found));
5068}
5069
5070/* For each of the methods in T, propagate a class-level tm attribute. */
5071
5072static void
5073set_method_tm_attributes (tree t)
5074{
5075 tree class_tm_attr, fndecl;
5076
5077 /* Don't bother collecting tm attributes if transactional memory
5078 support is not enabled. */
5079 if (!flag_tm)
5080 return;
5081
5082 /* Process virtual methods first, as they inherit directly from the
5083 base virtual function and also require validation of new attributes. */
5084 if (TYPE_CONTAINS_VPTR_P (t))
5085 {
5086 tree vchain;
5087 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5088 vchain = TREE_CHAIN (vchain))
00a42fb3
AH
5089 {
5090 fndecl = BV_FN (vchain);
5091 if (DECL_THUNK_P (fndecl))
5092 fndecl = THUNK_TARGET (fndecl);
5093 set_one_vmethod_tm_attributes (t, fndecl);
5094 }
0a35513e
AH
5095 }
5096
5097 /* If the class doesn't have an attribute, nothing more to do. */
5098 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5099 if (class_tm_attr == NULL)
5100 return;
5101
5102 /* Any method that does not yet have a tm attribute inherits
5103 the one from the class. */
5104 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5105 {
5106 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5107 apply_tm_attr (fndecl, class_tm_attr);
5108 }
5109}
5110
8c95264b
MLI
5111/* Returns true iff class T has a user-defined constructor other than
5112 the default constructor. */
5113
5114bool
5115type_has_user_nondefault_constructor (tree t)
5116{
5117 tree fns;
5118
5119 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5120 return false;
5121
5122 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5123 {
5124 tree fn = OVL_CURRENT (fns);
5125 if (!DECL_ARTIFICIAL (fn)
c2b58ba2
JM
5126 && (TREE_CODE (fn) == TEMPLATE_DECL
5127 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5128 != NULL_TREE)))
8c95264b
MLI
5129 return true;
5130 }
5131
5132 return false;
5133}
5134
6ad86a5b
FC
5135/* Returns the defaulted constructor if T has one. Otherwise, returns
5136 NULL_TREE. */
5137
5138tree
5139in_class_defaulted_default_constructor (tree t)
5140{
5141 tree fns, args;
5142
5143 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5144 return NULL_TREE;
5145
5146 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5147 {
5148 tree fn = OVL_CURRENT (fns);
5149
5150 if (DECL_DEFAULTED_IN_CLASS_P (fn))
5151 {
5152 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
5153 while (args && TREE_PURPOSE (args))
5154 args = TREE_CHAIN (args);
5155 if (!args || args == void_list_node)
5156 return fn;
5157 }
5158 }
5159
5160 return NULL_TREE;
5161}
5162
b87d79e6 5163/* Returns true iff FN is a user-provided function, i.e. user-declared
20f2653e
JM
5164 and not defaulted at its first declaration; or explicit, private,
5165 protected, or non-const. */
b87d79e6 5166
20f2653e 5167bool
b87d79e6
JM
5168user_provided_p (tree fn)
5169{
5170 if (TREE_CODE (fn) == TEMPLATE_DECL)
5171 return true;
5172 else
5173 return (!DECL_ARTIFICIAL (fn)
eca7fc57
JM
5174 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5175 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
b87d79e6
JM
5176}
5177
5178/* Returns true iff class T has a user-provided constructor. */
5179
5180bool
5181type_has_user_provided_constructor (tree t)
5182{
5183 tree fns;
5184
fd97a96a
JM
5185 if (!CLASS_TYPE_P (t))
5186 return false;
5187
b87d79e6
JM
5188 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5189 return false;
5190
5191 /* This can happen in error cases; avoid crashing. */
5192 if (!CLASSTYPE_METHOD_VEC (t))
5193 return false;
5194
5195 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5196 if (user_provided_p (OVL_CURRENT (fns)))
5197 return true;
5198
5199 return false;
5200}
5201
a3320d62
JM
5202/* Returns true iff class T has a user-provided or explicit constructor. */
5203
5204bool
5205type_has_user_provided_or_explicit_constructor (tree t)
5206{
5207 tree fns;
5208
5209 if (!CLASS_TYPE_P (t))
5210 return false;
5211
5212 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5213 return false;
5214
5215 /* This can happen in error cases; avoid crashing. */
5216 if (!CLASSTYPE_METHOD_VEC (t))
5217 return false;
5218
5219 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5220 {
5221 tree fn = OVL_CURRENT (fns);
5222 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5223 return true;
5224 }
5225
5226 return false;
5227}
5228
a710f1f8
JM
5229/* Returns true iff class T has a non-user-provided (i.e. implicitly
5230 declared or explicitly defaulted in the class body) default
5231 constructor. */
b87d79e6
JM
5232
5233bool
a710f1f8 5234type_has_non_user_provided_default_constructor (tree t)
b87d79e6 5235{
71b8cb01 5236 tree fns;
b87d79e6 5237
a710f1f8 5238 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
b87d79e6 5239 return false;
a710f1f8
JM
5240 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5241 return true;
b87d79e6
JM
5242
5243 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5244 {
5245 tree fn = OVL_CURRENT (fns);
7ad8d488 5246 if (TREE_CODE (fn) == FUNCTION_DECL
a710f1f8 5247 && !user_provided_p (fn)
71b8cb01
JM
5248 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
5249 return true;
b87d79e6
JM
5250 }
5251
5252 return false;
5253}
5254
32bfcf80
JM
5255/* TYPE is being used as a virtual base, and has a non-trivial move
5256 assignment. Return true if this is due to there being a user-provided
5257 move assignment in TYPE or one of its subobjects; if there isn't, then
5258 multiple move assignment can't cause any harm. */
5259
5260bool
5261vbase_has_user_provided_move_assign (tree type)
5262{
5263 /* Does the type itself have a user-provided move assignment operator? */
5264 for (tree fns
5265 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5266 fns; fns = OVL_NEXT (fns))
5267 {
5268 tree fn = OVL_CURRENT (fns);
5269 if (move_fn_p (fn) && user_provided_p (fn))
5270 return true;
5271 }
5272
5273 /* Do any of its bases? */
5274 tree binfo = TYPE_BINFO (type);
5275 tree base_binfo;
5276 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5277 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5278 return true;
5279
5280 /* Or non-static data members? */
5281 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5282 {
5283 if (TREE_CODE (field) == FIELD_DECL
5284 && CLASS_TYPE_P (TREE_TYPE (field))
5285 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5286 return true;
5287 }
5288
5289 /* Seems not. */
5290 return false;
5291}
5292
6132bdd7
JM
5293/* If default-initialization leaves part of TYPE uninitialized, returns
5294 a DECL for the field or TYPE itself (DR 253). */
5295
5296tree
5297default_init_uninitialized_part (tree type)
5298{
5299 tree t, r, binfo;
5300 int i;
5301
5302 type = strip_array_types (type);
5303 if (!CLASS_TYPE_P (type))
5304 return type;
a710f1f8 5305 if (!type_has_non_user_provided_default_constructor (type))
6132bdd7
JM
5306 return NULL_TREE;
5307 for (binfo = TYPE_BINFO (type), i = 0;
5308 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5309 {
5310 r = default_init_uninitialized_part (BINFO_TYPE (t));
5311 if (r)
5312 return r;
5313 }
5314 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5315 if (TREE_CODE (t) == FIELD_DECL
5316 && !DECL_ARTIFICIAL (t)
5317 && !DECL_INITIAL (t))
5318 {
5319 r = default_init_uninitialized_part (TREE_TYPE (t));
5320 if (r)
5321 return DECL_P (r) ? r : t;
5322 }
5323
5324 return NULL_TREE;
5325}
5326
fd3faf2b 5327/* Returns true iff for class T, a trivial synthesized default constructor
0930cc0e
JM
5328 would be constexpr. */
5329
5330bool
fd3faf2b 5331trivial_default_constructor_is_constexpr (tree t)
0930cc0e 5332{
fd3faf2b 5333 /* A defaulted trivial default constructor is constexpr
0930cc0e 5334 if there is nothing to initialize. */
fd3faf2b 5335 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
0930cc0e
JM
5336 return is_really_empty_class (t);
5337}
5338
91ea6df3
GDR
5339/* Returns true iff class T has a constexpr default constructor. */
5340
5341bool
5342type_has_constexpr_default_constructor (tree t)
5343{
5344 tree fns;
5345
5346 if (!CLASS_TYPE_P (t))
69f36ba6
JM
5347 {
5348 /* The caller should have stripped an enclosing array. */
5349 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5350 return false;
5351 }
0930cc0e 5352 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
fd3faf2b
JM
5353 {
5354 if (!TYPE_HAS_COMPLEX_DFLT (t))
5355 return trivial_default_constructor_is_constexpr (t);
81c160c6
JM
5356 /* Non-trivial, we need to check subobject constructors. */
5357 lazily_declare_fn (sfk_constructor, t);
fd3faf2b 5358 }
f7d042e2 5359 fns = locate_ctor (t);
91ea6df3
GDR
5360 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5361}
5362
81c160c6
JM
5363/* Returns true iff class T has a constexpr default constructor or has an
5364 implicitly declared default constructor that we can't tell if it's constexpr
5365 without forcing a lazy declaration (which might cause undesired
5366 instantiations). */
5367
5368bool
5369type_maybe_constexpr_default_constructor (tree t)
5370{
5371 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5372 && TYPE_HAS_COMPLEX_DFLT (t))
5373 /* Assume it's constexpr. */
5374 return true;
5375 return type_has_constexpr_default_constructor (t);
5376}
5377
46408846
JM
5378/* Returns true iff class TYPE has a virtual destructor. */
5379
5380bool
5381type_has_virtual_destructor (tree type)
5382{
5383 tree dtor;
5384
5385 if (!CLASS_TYPE_P (type))
5386 return false;
5387
5388 gcc_assert (COMPLETE_TYPE_P (type));
5389 dtor = CLASSTYPE_DESTRUCTORS (type);
5390 return (dtor && DECL_VIRTUAL_P (dtor));
5391}
5392
ac177431
JM
5393/* Returns true iff class T has a move constructor. */
5394
5395bool
5396type_has_move_constructor (tree t)
5397{
5398 tree fns;
5399
5400 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5401 {
5402 gcc_assert (COMPLETE_TYPE_P (t));
5403 lazily_declare_fn (sfk_move_constructor, t);
5404 }
5405
5406 if (!CLASSTYPE_METHOD_VEC (t))
5407 return false;
5408
5409 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5410 if (move_fn_p (OVL_CURRENT (fns)))
5411 return true;
5412
5413 return false;
5414}
5415
5416/* Returns true iff class T has a move assignment operator. */
5417
5418bool
5419type_has_move_assign (tree t)
5420{
5421 tree fns;
5422
5423 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5424 {
5425 gcc_assert (COMPLETE_TYPE_P (t));
5426 lazily_declare_fn (sfk_move_assignment, t);
5427 }
5428
fa4ba4af 5429 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
ac177431
JM
5430 fns; fns = OVL_NEXT (fns))
5431 if (move_fn_p (OVL_CURRENT (fns)))
5432 return true;
5433
5434 return false;
5435}
5436
a2e70335
JM
5437/* Returns true iff class T has a move constructor that was explicitly
5438 declared in the class body. Note that this is different from
5439 "user-provided", which doesn't include functions that are defaulted in
5440 the class. */
5441
5442bool
5443type_has_user_declared_move_constructor (tree t)
5444{
5445 tree fns;
5446
5447 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5448 return false;
5449
5450 if (!CLASSTYPE_METHOD_VEC (t))
5451 return false;
5452
5453 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5454 {
5455 tree fn = OVL_CURRENT (fns);
5456 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5457 return true;
5458 }
5459
5460 return false;
5461}
5462
5463/* Returns true iff class T has a move assignment operator that was
5464 explicitly declared in the class body. */
5465
5466bool
5467type_has_user_declared_move_assign (tree t)
5468{
5469 tree fns;
5470
5471 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5472 return false;
5473
fa4ba4af 5474 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
a2e70335
JM
5475 fns; fns = OVL_NEXT (fns))
5476 {
5477 tree fn = OVL_CURRENT (fns);
5478 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5479 return true;
5480 }
5481
5482 return false;
5483}
5484
95552437 5485/* Nonzero if we need to build up a constructor call when initializing an
eca7fc57 5486 object of this class, either because it has a user-declared constructor
95552437
JM
5487 or because it doesn't have a default constructor (so we need to give an
5488 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5489 what you care about is whether or not an object can be produced by a
5490 constructor (e.g. so we don't set TREE_READONLY on const variables of
5491 such type); use this function when what you care about is whether or not
5492 to try to call a constructor to create an object. The latter case is
5493 the former plus some cases of constructors that cannot be called. */
5494
5495bool
5496type_build_ctor_call (tree t)
5497{
5498 tree inner;
5499 if (TYPE_NEEDS_CONSTRUCTING (t))
5500 return true;
5501 inner = strip_array_types (t);
eca7fc57
JM
5502 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5503 return false;
5504 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5505 return true;
83f31d8d
JM
5506 if (cxx_dialect < cxx11)
5507 return false;
eca7fc57
JM
5508 /* A user-declared constructor might be private, and a constructor might
5509 be trivial but deleted. */
5510 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5511 fns; fns = OVL_NEXT (fns))
5512 {
5513 tree fn = OVL_CURRENT (fns);
5514 if (!DECL_ARTIFICIAL (fn)
5515 || DECL_DELETED_FN (fn))
5516 return true;
5517 }
5518 return false;
5519}
5520
5521/* Like type_build_ctor_call, but for destructors. */
5522
5523bool
5524type_build_dtor_call (tree t)
5525{
5526 tree inner;
5527 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5528 return true;
5529 inner = strip_array_types (t);
5530 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5531 || !COMPLETE_TYPE_P (inner))
5532 return false;
83f31d8d
JM
5533 if (cxx_dialect < cxx11)
5534 return false;
eca7fc57
JM
5535 /* A user-declared destructor might be private, and a destructor might
5536 be trivial but deleted. */
5537 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5538 fns; fns = OVL_NEXT (fns))
5539 {
5540 tree fn = OVL_CURRENT (fns);
5541 if (!DECL_ARTIFICIAL (fn)
5542 || DECL_DELETED_FN (fn))
5543 return true;
5544 }
5545 return false;
95552437
JM
5546}
5547
58010b57
MM
5548/* Remove all zero-width bit-fields from T. */
5549
5550static void
94edc4ab 5551remove_zero_width_bit_fields (tree t)
58010b57
MM
5552{
5553 tree *fieldsp;
5554
c8094d83 5555 fieldsp = &TYPE_FIELDS (t);
58010b57
MM
5556 while (*fieldsp)
5557 {
5558 if (TREE_CODE (*fieldsp) == FIELD_DECL
c8094d83 5559 && DECL_C_BIT_FIELD (*fieldsp)
84894f85
DS
5560 /* We should not be confused by the fact that grokbitfield
5561 temporarily sets the width of the bit field into
5562 DECL_INITIAL (*fieldsp).
5563 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5564 to that width. */
2a924bb4
MP
5565 && (DECL_SIZE (*fieldsp) == NULL_TREE
5566 || integer_zerop (DECL_SIZE (*fieldsp))))
910ad8de 5567 *fieldsp = DECL_CHAIN (*fieldsp);
58010b57 5568 else
910ad8de 5569 fieldsp = &DECL_CHAIN (*fieldsp);
58010b57
MM
5570 }
5571}
5572
dbc957f1
MM
5573/* Returns TRUE iff we need a cookie when dynamically allocating an
5574 array whose elements have the indicated class TYPE. */
5575
5576static bool
94edc4ab 5577type_requires_array_cookie (tree type)
dbc957f1
MM
5578{
5579 tree fns;
18fee3ee 5580 bool has_two_argument_delete_p = false;
dbc957f1 5581
50bc768d 5582 gcc_assert (CLASS_TYPE_P (type));
dbc957f1
MM
5583
5584 /* If there's a non-trivial destructor, we need a cookie. In order
5585 to iterate through the array calling the destructor for each
5586 element, we'll have to know how many elements there are. */
5587 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5588 return true;
5589
5590 /* If the usual deallocation function is a two-argument whose second
5591 argument is of type `size_t', then we have to pass the size of
5592 the array to the deallocation function, so we will need to store
5593 a cookie. */
c8094d83 5594 fns = lookup_fnfields (TYPE_BINFO (type),
dbc957f1
MM
5595 ansi_opname (VEC_DELETE_EXPR),
5596 /*protect=*/0);
5597 /* If there are no `operator []' members, or the lookup is
5598 ambiguous, then we don't need a cookie. */
5599 if (!fns || fns == error_mark_node)
5600 return false;
5601 /* Loop through all of the functions. */
50ad9642 5602 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
dbc957f1
MM
5603 {
5604 tree fn;
5605 tree second_parm;
5606
5607 /* Select the current function. */
5608 fn = OVL_CURRENT (fns);
5609 /* See if this function is a one-argument delete function. If
5610 it is, then it will be the usual deallocation function. */
5611 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5612 if (second_parm == void_list_node)
5613 return false;
4b8cb94c
SM
5614 /* Do not consider this function if its second argument is an
5615 ellipsis. */
5616 if (!second_parm)
5617 continue;
dbc957f1
MM
5618 /* Otherwise, if we have a two-argument function and the second
5619 argument is `size_t', it will be the usual deallocation
5620 function -- unless there is one-argument function, too. */
5621 if (TREE_CHAIN (second_parm) == void_list_node
c79154c4 5622 && same_type_p (TREE_VALUE (second_parm), size_type_node))
dbc957f1
MM
5623 has_two_argument_delete_p = true;
5624 }
5625
5626 return has_two_argument_delete_p;
5627}
5628
3b49d762
GDR
5629/* Finish computing the `literal type' property of class type T.
5630
5631 At this point, we have already processed base classes and
5632 non-static data members. We need to check whether the copy
5633 constructor is trivial, the destructor is trivial, and there
5634 is a trivial default constructor or at least one constexpr
5635 constructor other than the copy constructor. */
5636
5637static void
5638finalize_literal_type_property (tree t)
5639{
0515f4d2
JM
5640 tree fn;
5641
604b2bfc 5642 if (cxx_dialect < cxx11
b198484e 5643 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
3b49d762
GDR
5644 CLASSTYPE_LITERAL_P (t) = false;
5645 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
b198484e 5646 && CLASSTYPE_NON_AGGREGATE (t)
3b49d762
GDR
5647 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5648 CLASSTYPE_LITERAL_P (t) = false;
0515f4d2
JM
5649
5650 if (!CLASSTYPE_LITERAL_P (t))
5651 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5652 if (DECL_DECLARED_CONSTEXPR_P (fn)
5653 && TREE_CODE (fn) != TEMPLATE_DECL
5654 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5655 && !DECL_CONSTRUCTOR_P (fn))
5656 {
5657 DECL_DECLARED_CONSTEXPR_P (fn) = false;
b432106b 5658 if (!DECL_GENERATED_P (fn))
f732fa7b
JM
5659 {
5660 error ("enclosing class of constexpr non-static member "
5661 "function %q+#D is not a literal type", fn);
5662 explain_non_literal_class (t);
5663 }
0515f4d2 5664 }
3b49d762
GDR
5665}
5666
f732fa7b
JM
5667/* T is a non-literal type used in a context which requires a constant
5668 expression. Explain why it isn't literal. */
5669
5670void
5671explain_non_literal_class (tree t)
5672{
6e2830c3 5673 static hash_set<tree> *diagnosed;
f732fa7b
JM
5674
5675 if (!CLASS_TYPE_P (t))
5676 return;
5677 t = TYPE_MAIN_VARIANT (t);
5678
5679 if (diagnosed == NULL)
6e2830c3
TS
5680 diagnosed = new hash_set<tree>;
5681 if (diagnosed->add (t))
f732fa7b
JM
5682 /* Already explained. */
5683 return;
5684
5685 inform (0, "%q+T is not literal because:", t);
5686 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5687 inform (0, " %q+T has a non-trivial destructor", t);
5688 else if (CLASSTYPE_NON_AGGREGATE (t)
5689 && !TYPE_HAS_TRIVIAL_DFLT (t)
5690 && !TYPE_HAS_CONSTEXPR_CTOR (t))
fd3faf2b
JM
5691 {
5692 inform (0, " %q+T is not an aggregate, does not have a trivial "
5693 "default constructor, and has no constexpr constructor that "
5694 "is not a copy or move constructor", t);
a710f1f8 5695 if (type_has_non_user_provided_default_constructor (t))
efff2fb4
PC
5696 {
5697 /* Note that we can't simply call locate_ctor because when the
5698 constructor is deleted it just returns NULL_TREE. */
5699 tree fns;
5700 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5701 {
5702 tree fn = OVL_CURRENT (fns);
5703 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5704
5705 parms = skip_artificial_parms_for (fn, parms);
5706
5707 if (sufficient_parms_p (parms))
5708 {
5709 if (DECL_DELETED_FN (fn))
5710 maybe_explain_implicit_delete (fn);
5711 else
5712 explain_invalid_constexpr_fn (fn);
5713 break;
5714 }
5715 }
5716 }
fd3faf2b 5717 }
f732fa7b
JM
5718 else
5719 {
5720 tree binfo, base_binfo, field; int i;
5721 for (binfo = TYPE_BINFO (t), i = 0;
5722 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5723 {
5724 tree basetype = TREE_TYPE (base_binfo);
5725 if (!CLASSTYPE_LITERAL_P (basetype))
5726 {
5727 inform (0, " base class %qT of %q+T is non-literal",
5728 basetype, t);
5729 explain_non_literal_class (basetype);
5730 return;
5731 }
5732 }
5733 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5734 {
5735 tree ftype;
5736 if (TREE_CODE (field) != FIELD_DECL)
5737 continue;
5738 ftype = TREE_TYPE (field);
5739 if (!literal_type_p (ftype))
5740 {
15827d12
PC
5741 inform (DECL_SOURCE_LOCATION (field),
5742 " non-static data member %qD has non-literal type",
5743 field);
f732fa7b
JM
5744 if (CLASS_TYPE_P (ftype))
5745 explain_non_literal_class (ftype);
5746 }
cec362c9 5747 if (CP_TYPE_VOLATILE_P (ftype))
15827d12
PC
5748 inform (DECL_SOURCE_LOCATION (field),
5749 " non-static data member %qD has volatile type", field);
f732fa7b
JM
5750 }
5751 }
5752}
5753
607cf131
MM
5754/* Check the validity of the bases and members declared in T. Add any
5755 implicitly-generated functions (like copy-constructors and
5756 assignment operators). Compute various flag bits (like
c32097d8 5757 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
607cf131
MM
5758 level: i.e., independently of the ABI in use. */
5759
5760static void
58731fd1 5761check_bases_and_members (tree t)
607cf131 5762{
607cf131
MM
5763 /* Nonzero if the implicitly generated copy constructor should take
5764 a non-const reference argument. */
5765 int cant_have_const_ctor;
78dcd41a 5766 /* Nonzero if the implicitly generated assignment operator
607cf131
MM
5767 should take a non-const reference argument. */
5768 int no_const_asn_ref;
5769 tree access_decls;
b87d79e6
JM
5770 bool saved_complex_asn_ref;
5771 bool saved_nontrivial_dtor;
20f2653e 5772 tree fn;
607cf131
MM
5773
5774 /* By default, we use const reference arguments and generate default
5775 constructors. */
607cf131
MM
5776 cant_have_const_ctor = 0;
5777 no_const_asn_ref = 0;
5778
7e9a3ad3
MS
5779 /* Check all the base-classes and set FMEM members to point to arrays
5780 of potential interest. */
5781 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
607cf131 5782
52d95c21
JM
5783 /* Deduce noexcept on destructors. This needs to happen after we've set
5784 triviality flags appropriately for our bases. */
604b2bfc 5785 if (cxx_dialect >= cxx11)
52d95c21
JM
5786 deduce_noexcept_on_destructors (t);
5787
9f4faeae
MM
5788 /* Check all the method declarations. */
5789 check_methods (t);
5790
b87d79e6
JM
5791 /* Save the initial values of these flags which only indicate whether
5792 or not the class has user-provided functions. As we analyze the
5793 bases and members we can set these flags for other reasons. */
066ec0a4 5794 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
b87d79e6
JM
5795 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5796
9f4faeae
MM
5797 /* Check all the data member declarations. We cannot call
5798 check_field_decls until we have called check_bases check_methods,
5799 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5800 being set appropriately. */
58731fd1 5801 check_field_decls (t, &access_decls,
607cf131 5802 &cant_have_const_ctor,
10746f37 5803 &no_const_asn_ref);
607cf131 5804
bbd15aac
MM
5805 /* A nearly-empty class has to be vptr-containing; a nearly empty
5806 class contains just a vptr. */
5807 if (!TYPE_CONTAINS_VPTR_P (t))
f9c528ea
MM
5808 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5809
607cf131
MM
5810 /* Do some bookkeeping that will guide the generation of implicitly
5811 declared member functions. */
066ec0a4 5812 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
ac177431 5813 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
0fcedd9c 5814 /* We need to call a constructor for this class if it has a
b87d79e6 5815 user-provided constructor, or if the default constructor is going
0fcedd9c
JM
5816 to initialize the vptr. (This is not an if-and-only-if;
5817 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5818 themselves need constructing.) */
607cf131 5819 TYPE_NEEDS_CONSTRUCTING (t)
b87d79e6 5820 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
0fcedd9c
JM
5821 /* [dcl.init.aggr]
5822
b87d79e6 5823 An aggregate is an array or a class with no user-provided
0fcedd9c
JM
5824 constructors ... and no virtual functions.
5825
5826 Again, other conditions for being an aggregate are checked
5827 elsewhere. */
5775a06a 5828 CLASSTYPE_NON_AGGREGATE (t)
a3320d62
JM
5829 |= (type_has_user_provided_or_explicit_constructor (t)
5830 || TYPE_POLYMORPHIC_P (t));
c32097d8
JM
5831 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5832 retain the old definition internally for ABI reasons. */
5833 CLASSTYPE_NON_LAYOUT_POD_P (t)
c8094d83 5834 |= (CLASSTYPE_NON_AGGREGATE (t)
b87d79e6 5835 || saved_nontrivial_dtor || saved_complex_asn_ref);
c32097d8 5836 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
066ec0a4 5837 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
ac177431 5838 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
f782c65c 5839 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
607cf131 5840
a710f1f8
JM
5841 /* If the only explicitly declared default constructor is user-provided,
5842 set TYPE_HAS_COMPLEX_DFLT. */
5843 if (!TYPE_HAS_COMPLEX_DFLT (t)
5844 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5845 && !type_has_non_user_provided_default_constructor (t))
5846 TYPE_HAS_COMPLEX_DFLT (t) = true;
5847
38f09da3 5848 /* Warn if a public base of a polymorphic type has an accessible
880a467b
NS
5849 non-virtual destructor. It is only now that we know the class is
5850 polymorphic. Although a polymorphic base will have a already
5851 been diagnosed during its definition, we warn on use too. */
5852 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5853 {
38f09da3
NS
5854 tree binfo = TYPE_BINFO (t);
5855 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5856 tree base_binfo;
880a467b
NS
5857 unsigned i;
5858
38f09da3 5859 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
880a467b
NS
5860 {
5861 tree basetype = TREE_TYPE (base_binfo);
5862
38f09da3
NS
5863 if ((*accesses)[i] == access_public_node
5864 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5865 && accessible_nvdtor_p (basetype))
880a467b
NS
5866 warning (OPT_Wnon_virtual_dtor,
5867 "base class %q#T has accessible non-virtual destructor",
5868 basetype);
5869 }
5870 }
5871
0fcedd9c
JM
5872 /* If the class has no user-declared constructor, but does have
5873 non-static const or reference data members that can never be
5874 initialized, issue a warning. */
c73d5dd9 5875 if (warn_uninitialized
0fcedd9c
JM
5876 /* Classes with user-declared constructors are presumed to
5877 initialize these members. */
5878 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5879 /* Aggregates can be initialized with brace-enclosed
5880 initializers. */
5881 && CLASSTYPE_NON_AGGREGATE (t))
5882 {
5883 tree field;
5884
910ad8de 5885 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
0fcedd9c
JM
5886 {
5887 tree type;
5888
f315d618
JJ
5889 if (TREE_CODE (field) != FIELD_DECL
5890 || DECL_INITIAL (field) != NULL_TREE)
0fcedd9c
JM
5891 continue;
5892
5893 type = TREE_TYPE (field);
5894 if (TREE_CODE (type) == REFERENCE_TYPE)
15827d12
PC
5895 warning_at (DECL_SOURCE_LOCATION (field),
5896 OPT_Wuninitialized, "non-static reference %q#D "
5897 "in class without a constructor", field);
0fcedd9c
JM
5898 else if (CP_TYPE_CONST_P (type)
5899 && (!CLASS_TYPE_P (type)
5900 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
15827d12
PC
5901 warning_at (DECL_SOURCE_LOCATION (field),
5902 OPT_Wuninitialized, "non-static const member %q#D "
5903 "in class without a constructor", field);
0fcedd9c
JM
5904 }
5905 }
5906
03fd3f84 5907 /* Synthesize any needed methods. */
85b5d65a 5908 add_implicitly_declared_members (t, &access_decls,
607cf131 5909 cant_have_const_ctor,
10746f37 5910 no_const_asn_ref);
607cf131 5911
20f2653e
JM
5912 /* Check defaulted declarations here so we have cant_have_const_ctor
5913 and don't need to worry about clones. */
910ad8de 5914 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
85b5d65a 5915 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
20f2653e
JM
5916 {
5917 int copy = copy_fn_p (fn);
5918 if (copy > 0)
5919 {
5920 bool imp_const_p
5921 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5922 : !no_const_asn_ref);
5923 bool fn_const_p = (copy == 2);
5924
5925 if (fn_const_p && !imp_const_p)
5926 /* If the function is defaulted outside the class, we just
5927 give the synthesis error. */
5928 error ("%q+D declared to take const reference, but implicit "
5929 "declaration would take non-const", fn);
20f2653e
JM
5930 }
5931 defaulted_late_check (fn);
5932 }
5933
d5f4eddd
JM
5934 if (LAMBDA_TYPE_P (t))
5935 {
d5f4eddd
JM
5936 /* "This class type is not an aggregate." */
5937 CLASSTYPE_NON_AGGREGATE (t) = 1;
5938 }
5939
3b49d762
GDR
5940 /* Compute the 'literal type' property before we
5941 do anything with non-static member functions. */
5942 finalize_literal_type_property (t);
5943
db9b2174
MM
5944 /* Create the in-charge and not-in-charge variants of constructors
5945 and destructors. */
5946 clone_constructors_and_destructors (t);
5947
aa52c1ff
JM
5948 /* Process the using-declarations. */
5949 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5950 handle_using_decl (TREE_VALUE (access_decls), t);
5951
607cf131
MM
5952 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5953 finish_struct_methods (t);
dbc957f1
MM
5954
5955 /* Figure out whether or not we will need a cookie when dynamically
5956 allocating an array of this type. */
e2500fed 5957 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
dbc957f1 5958 = type_requires_array_cookie (t);
607cf131
MM
5959}
5960
3ef397c1 5961/* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5c24fba6
MM
5962 accordingly. If a new vfield was created (because T doesn't have a
5963 primary base class), then the newly created field is returned. It
c35cce41 5964 is not added to the TYPE_FIELDS list; it is the caller's
e6858a84
NS
5965 responsibility to do that. Accumulate declared virtual functions
5966 on VIRTUALS_P. */
3ef397c1 5967
5c24fba6 5968static tree
94edc4ab 5969create_vtable_ptr (tree t, tree* virtuals_p)
3ef397c1
MM
5970{
5971 tree fn;
5972
e6858a84 5973 /* Collect the virtual functions declared in T. */
910ad8de 5974 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
aaf8a23e
JH
5975 if (TREE_CODE (fn) == FUNCTION_DECL
5976 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
e6858a84
NS
5977 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5978 {
5979 tree new_virtual = make_node (TREE_LIST);
c8094d83 5980
e6858a84
NS
5981 BV_FN (new_virtual) = fn;
5982 BV_DELTA (new_virtual) = integer_zero_node;
d1f05f93 5983 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
3ef397c1 5984
e6858a84
NS
5985 TREE_CHAIN (new_virtual) = *virtuals_p;
5986 *virtuals_p = new_virtual;
5987 }
c8094d83 5988
da3d4dfa
MM
5989 /* If we couldn't find an appropriate base class, create a new field
5990 here. Even if there weren't any new virtual functions, we might need a
bbd15aac
MM
5991 new virtual function table if we're supposed to include vptrs in
5992 all classes that need them. */
e6858a84 5993 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
3ef397c1
MM
5994 {
5995 /* We build this decl with vtbl_ptr_type_node, which is a
5996 `vtable_entry_type*'. It might seem more precise to use
a692ad2e 5997 `vtable_entry_type (*)[N]' where N is the number of virtual
3ef397c1
MM
5998 functions. However, that would require the vtable pointer in
5999 base classes to have a different type than the vtable pointer
6000 in derived classes. We could make that happen, but that
6001 still wouldn't solve all the problems. In particular, the
6002 type-based alias analysis code would decide that assignments
6003 to the base class vtable pointer can't alias assignments to
6004 the derived class vtable pointer, since they have different
4639c5c6 6005 types. Thus, in a derived class destructor, where the base
3ef397c1 6006 class constructor was inlined, we could generate bad code for
c8094d83 6007 setting up the vtable pointer.
3ef397c1 6008
0cbd7506 6009 Therefore, we use one type for all vtable pointers. We still
3ef397c1
MM
6010 use a type-correct type; it's just doesn't indicate the array
6011 bounds. That's better than using `void*' or some such; it's
6012 cleaner, and it let's the alias analysis code know that these
6013 stores cannot alias stores to void*! */
0abe00c5
NS
6014 tree field;
6015
c2255bc4
AH
6016 field = build_decl (input_location,
6017 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
0abe00c5
NS
6018 DECL_VIRTUAL_P (field) = 1;
6019 DECL_ARTIFICIAL (field) = 1;
6020 DECL_FIELD_CONTEXT (field) = t;
6021 DECL_FCONTEXT (field) = t;
7c08df6c
JM
6022 if (TYPE_PACKED (t))
6023 DECL_PACKED (field) = 1;
c8094d83 6024
0abe00c5 6025 TYPE_VFIELD (t) = field;
c8094d83 6026
0abe00c5 6027 /* This class is non-empty. */
58731fd1 6028 CLASSTYPE_EMPTY_P (t) = 0;
3ef397c1 6029
0abe00c5 6030 return field;
3ef397c1 6031 }
5c24fba6
MM
6032
6033 return NULL_TREE;
3ef397c1
MM
6034}
6035
9d4c0187
MM
6036/* Add OFFSET to all base types of BINFO which is a base in the
6037 hierarchy dominated by T.
80fd5f48 6038
911a71a7 6039 OFFSET, which is a type offset, is number of bytes. */
80fd5f48
MM
6040
6041static void
dbbf88d1 6042propagate_binfo_offsets (tree binfo, tree offset)
80fd5f48 6043{
911a71a7
MM
6044 int i;
6045 tree primary_binfo;
fa743e8c 6046 tree base_binfo;
80fd5f48 6047
911a71a7
MM
6048 /* Update BINFO's offset. */
6049 BINFO_OFFSET (binfo)
cda0a029 6050 = fold_convert (sizetype,
911a71a7 6051 size_binop (PLUS_EXPR,
cda0a029 6052 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
911a71a7 6053 offset));
80fd5f48 6054
911a71a7
MM
6055 /* Find the primary base class. */
6056 primary_binfo = get_primary_binfo (binfo);
6057
fc6633e0 6058 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
090ad434 6059 propagate_binfo_offsets (primary_binfo, offset);
c8094d83 6060
911a71a7
MM
6061 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6062 downwards. */
fa743e8c 6063 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
80fd5f48 6064 {
090ad434
NS
6065 /* Don't do the primary base twice. */
6066 if (base_binfo == primary_binfo)
6067 continue;
911a71a7 6068
090ad434 6069 if (BINFO_VIRTUAL_P (base_binfo))
911a71a7
MM
6070 continue;
6071
dbbf88d1 6072 propagate_binfo_offsets (base_binfo, offset);
911a71a7 6073 }
9d4c0187
MM
6074}
6075
17bbb839 6076/* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
c20118a8
MM
6077 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6078 empty subobjects of T. */
80fd5f48 6079
d2c5305b 6080static void
17bbb839 6081layout_virtual_bases (record_layout_info rli, splay_tree offsets)
80fd5f48 6082{
dbbf88d1 6083 tree vbase;
17bbb839 6084 tree t = rli->t;
17bbb839 6085 tree *next_field;
9785e4b1 6086
604a3205 6087 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
9785e4b1
MM
6088 return;
6089
17bbb839
MM
6090 /* Find the last field. The artificial fields created for virtual
6091 bases will go after the last extant field to date. */
6092 next_field = &TYPE_FIELDS (t);
6093 while (*next_field)
910ad8de 6094 next_field = &DECL_CHAIN (*next_field);
80fd5f48 6095
9d4c0187 6096 /* Go through the virtual bases, allocating space for each virtual
3461fba7
NS
6097 base that is not already a primary base class. These are
6098 allocated in inheritance graph order. */
dbbf88d1 6099 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
c35cce41 6100 {
809e3e7f 6101 if (!BINFO_VIRTUAL_P (vbase))
1f84ec23 6102 continue;
eca7f13c 6103
9965d119 6104 if (!BINFO_PRIMARY_P (vbase))
c35cce41
MM
6105 {
6106 /* This virtual base is not a primary base of any class in the
6107 hierarchy, so we have to add space for it. */
58731fd1 6108 next_field = build_base_field (rli, vbase,
17bbb839 6109 offsets, next_field);
c35cce41
MM
6110 }
6111 }
80fd5f48
MM
6112}
6113
ba9a991f
MM
6114/* Returns the offset of the byte just past the end of the base class
6115 BINFO. */
6116
6117static tree
6118end_of_base (tree binfo)
6119{
6120 tree size;
6121
1ad8aeeb
DG
6122 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6123 size = TYPE_SIZE_UNIT (char_type_node);
6124 else if (is_empty_class (BINFO_TYPE (binfo)))
ba9a991f
MM
6125 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6126 allocate some space for it. It cannot have virtual bases, so
6127 TYPE_SIZE_UNIT is fine. */
6128 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6129 else
6130 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6131
6132 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6133}
6134
9785e4b1
MM
6135/* Returns the offset of the byte just past the end of the base class
6136 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6137 only non-virtual bases are included. */
80fd5f48 6138
17bbb839 6139static tree
94edc4ab 6140end_of_class (tree t, int include_virtuals_p)
80fd5f48 6141{
17bbb839 6142 tree result = size_zero_node;
9771b263 6143 vec<tree, va_gc> *vbases;
ba9a991f 6144 tree binfo;
9ba5ff0f 6145 tree base_binfo;
ba9a991f 6146 tree offset;
9785e4b1 6147 int i;
80fd5f48 6148
fa743e8c
NS
6149 for (binfo = TYPE_BINFO (t), i = 0;
6150 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9785e4b1 6151 {
9785e4b1 6152 if (!include_virtuals_p
fc6633e0
NS
6153 && BINFO_VIRTUAL_P (base_binfo)
6154 && (!BINFO_PRIMARY_P (base_binfo)
6155 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
9785e4b1 6156 continue;
80fd5f48 6157
fa743e8c 6158 offset = end_of_base (base_binfo);
807e902e 6159 if (tree_int_cst_lt (result, offset))
17bbb839 6160 result = offset;
9785e4b1 6161 }
80fd5f48 6162
90d84934 6163 if (include_virtuals_p)
9ba5ff0f 6164 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
9771b263 6165 vec_safe_iterate (vbases, i, &base_binfo); i++)
ba9a991f 6166 {
9ba5ff0f 6167 offset = end_of_base (base_binfo);
807e902e 6168 if (tree_int_cst_lt (result, offset))
ba9a991f
MM
6169 result = offset;
6170 }
6171
9785e4b1 6172 return result;
80fd5f48
MM
6173}
6174
17bbb839 6175/* Warn about bases of T that are inaccessible because they are
78b45a24
MM
6176 ambiguous. For example:
6177
6178 struct S {};
6179 struct T : public S {};
6180 struct U : public S, public T {};
6181
6182 Here, `(S*) new U' is not allowed because there are two `S'
6183 subobjects of U. */
6184
6185static void
94edc4ab 6186warn_about_ambiguous_bases (tree t)
78b45a24
MM
6187{
6188 int i;
9771b263 6189 vec<tree, va_gc> *vbases;
17bbb839 6190 tree basetype;
58c42dc2 6191 tree binfo;
fa743e8c 6192 tree base_binfo;
78b45a24 6193
18e4be85
NS
6194 /* If there are no repeated bases, nothing can be ambiguous. */
6195 if (!CLASSTYPE_REPEATED_BASE_P (t))
6196 return;
c8094d83 6197
17bbb839 6198 /* Check direct bases. */
fa743e8c
NS
6199 for (binfo = TYPE_BINFO (t), i = 0;
6200 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
78b45a24 6201 {
fa743e8c 6202 basetype = BINFO_TYPE (base_binfo);
78b45a24 6203
22854930 6204 if (!uniquely_derived_from_p (basetype, t))
d4ee4d25 6205 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
17bbb839 6206 basetype, t);
78b45a24 6207 }
17bbb839
MM
6208
6209 /* Check for ambiguous virtual bases. */
6210 if (extra_warnings)
9ba5ff0f 6211 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
9771b263 6212 vec_safe_iterate (vbases, i, &binfo); i++)
17bbb839 6213 {
58c42dc2 6214 basetype = BINFO_TYPE (binfo);
c8094d83 6215
22854930
PC
6216 if (!uniquely_derived_from_p (basetype, t))
6217 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6218 "to ambiguity", basetype, t);
17bbb839 6219 }
78b45a24
MM
6220}
6221
c20118a8
MM
6222/* Compare two INTEGER_CSTs K1 and K2. */
6223
6224static int
94edc4ab 6225splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
c20118a8
MM
6226{
6227 return tree_int_cst_compare ((tree) k1, (tree) k2);
6228}
6229
17bbb839
MM
6230/* Increase the size indicated in RLI to account for empty classes
6231 that are "off the end" of the class. */
6232
6233static void
6234include_empty_classes (record_layout_info rli)
6235{
6236 tree eoc;
e3ccdd50 6237 tree rli_size;
17bbb839
MM
6238
6239 /* It might be the case that we grew the class to allocate a
6240 zero-sized base class. That won't be reflected in RLI, yet,
6241 because we are willing to overlay multiple bases at the same
6242 offset. However, now we need to make sure that RLI is big enough
6243 to reflect the entire class. */
c8094d83 6244 eoc = end_of_class (rli->t,
17bbb839 6245 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
e3ccdd50
MM
6246 rli_size = rli_size_unit_so_far (rli);
6247 if (TREE_CODE (rli_size) == INTEGER_CST
807e902e 6248 && tree_int_cst_lt (rli_size, eoc))
17bbb839 6249 {
90d84934
JM
6250 /* The size should have been rounded to a whole byte. */
6251 gcc_assert (tree_int_cst_equal
6252 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
c8094d83
MS
6253 rli->bitpos
6254 = size_binop (PLUS_EXPR,
e3ccdd50
MM
6255 rli->bitpos,
6256 size_binop (MULT_EXPR,
cda0a029 6257 fold_convert (bitsizetype,
e3ccdd50
MM
6258 size_binop (MINUS_EXPR,
6259 eoc, rli_size)),
6260 bitsize_int (BITS_PER_UNIT)));
6261 normalize_rli (rli);
17bbb839
MM
6262 }
6263}
6264
2ef16140
MM
6265/* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6266 BINFO_OFFSETs for all of the base-classes. Position the vtable
00a17e31 6267 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
607cf131 6268
2ef16140 6269static void
e93ee644 6270layout_class_type (tree t, tree *virtuals_p)
2ef16140 6271{
5c24fba6
MM
6272 tree non_static_data_members;
6273 tree field;
6274 tree vptr;
6275 record_layout_info rli;
c20118a8
MM
6276 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6277 types that appear at that offset. */
6278 splay_tree empty_base_offsets;
c65cb8d1 6279 /* True if the last field laid out was a bit-field. */
eca7f13c 6280 bool last_field_was_bitfield = false;
17bbb839
MM
6281 /* The location at which the next field should be inserted. */
6282 tree *next_field;
6283 /* T, as a base class. */
6284 tree base_t;
5c24fba6
MM
6285
6286 /* Keep track of the first non-static data member. */
6287 non_static_data_members = TYPE_FIELDS (t);
6288
770ae6cc
RK
6289 /* Start laying out the record. */
6290 rli = start_record_layout (t);
534170eb 6291
fc6633e0
NS
6292 /* Mark all the primary bases in the hierarchy. */
6293 determine_primary_bases (t);
8026246f 6294
5c24fba6 6295 /* Create a pointer to our virtual function table. */
58731fd1 6296 vptr = create_vtable_ptr (t, virtuals_p);
5c24fba6 6297
3461fba7 6298 /* The vptr is always the first thing in the class. */
1f84ec23 6299 if (vptr)
5c24fba6 6300 {
910ad8de 6301 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
17bbb839 6302 TYPE_FIELDS (t) = vptr;
910ad8de 6303 next_field = &DECL_CHAIN (vptr);
770ae6cc 6304 place_field (rli, vptr);
5c24fba6 6305 }
17bbb839
MM
6306 else
6307 next_field = &TYPE_FIELDS (t);
5c24fba6 6308
72a50ab0 6309 /* Build FIELD_DECLs for all of the non-virtual base-types. */
c8094d83 6310 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
c20118a8 6311 NULL, NULL);
58731fd1 6312 build_base_fields (rli, empty_base_offsets, next_field);
c8094d83 6313
5c24fba6 6314 /* Layout the non-static data members. */
910ad8de 6315 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
5c24fba6 6316 {
01955e96
MM
6317 tree type;
6318 tree padding;
5c24fba6
MM
6319
6320 /* We still pass things that aren't non-static data members to
3b426391 6321 the back end, in case it wants to do something with them. */
5c24fba6
MM
6322 if (TREE_CODE (field) != FIELD_DECL)
6323 {
770ae6cc 6324 place_field (rli, field);
0154eaa8 6325 /* If the static data member has incomplete type, keep track
c8094d83 6326 of it so that it can be completed later. (The handling
0154eaa8
MM
6327 of pending statics in finish_record_layout is
6328 insufficient; consider:
6329
6330 struct S1;
6331 struct S2 { static S1 s1; };
c8094d83 6332
0cbd7506 6333 At this point, finish_record_layout will be called, but
0154eaa8 6334 S1 is still incomplete.) */
5a6ccc94 6335 if (VAR_P (field))
532b37d9
MM
6336 {
6337 maybe_register_incomplete_var (field);
6338 /* The visibility of static data members is determined
6339 at their point of declaration, not their point of
6340 definition. */
6341 determine_visibility (field);
6342 }
5c24fba6
MM
6343 continue;
6344 }
6345
01955e96 6346 type = TREE_TYPE (field);
4e3bd7d5
VR
6347 if (type == error_mark_node)
6348 continue;
c8094d83 6349
1e099144 6350 padding = NULL_TREE;
01955e96
MM
6351
6352 /* If this field is a bit-field whose width is greater than its
3461fba7
NS
6353 type, then there are some special rules for allocating
6354 it. */
01955e96 6355 if (DECL_C_BIT_FIELD (field)
807e902e 6356 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
01955e96 6357 {
09639a83 6358 unsigned int itk;
01955e96 6359 tree integer_type;
555456b1 6360 bool was_unnamed_p = false;
01955e96
MM
6361 /* We must allocate the bits as if suitably aligned for the
6362 longest integer type that fits in this many bits. type
6363 of the field. Then, we are supposed to use the left over
6364 bits as additional padding. */
6365 for (itk = itk_char; itk != itk_none; ++itk)
64c31785 6366 if (integer_types[itk] != NULL_TREE
807e902e
KZ
6367 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6368 TYPE_SIZE (integer_types[itk]))
6369 || tree_int_cst_lt (DECL_SIZE (field),
6370 TYPE_SIZE (integer_types[itk]))))
01955e96
MM
6371 break;
6372
6373 /* ITK now indicates a type that is too large for the
6374 field. We have to back up by one to find the largest
6375 type that fits. */
64c31785
KT
6376 do
6377 {
6378 --itk;
6379 integer_type = integer_types[itk];
6380 } while (itk > 0 && integer_type == NULL_TREE);
2d3e278d 6381
90d84934
JM
6382 /* Figure out how much additional padding is required. */
6383 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
2d3e278d 6384 {
90d84934 6385 if (TREE_CODE (t) == UNION_TYPE)
1e099144
MM
6386 /* In a union, the padding field must have the full width
6387 of the bit-field; all fields start at offset zero. */
6388 padding = DECL_SIZE (field);
6389 else
90d84934
JM
6390 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6391 TYPE_SIZE (integer_type));
2d3e278d 6392 }
1d0275e2 6393
63e5f567
MM
6394 /* An unnamed bitfield does not normally affect the
6395 alignment of the containing class on a target where
6396 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6397 make any exceptions for unnamed bitfields when the
6398 bitfields are longer than their types. Therefore, we
6399 temporarily give the field a name. */
6400 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6401 {
6402 was_unnamed_p = true;
6403 DECL_NAME (field) = make_anon_name ();
6404 }
1d0275e2 6405
01955e96
MM
6406 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6407 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
11cf4d18 6408 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
555456b1
MM
6409 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6410 empty_base_offsets);
6411 if (was_unnamed_p)
6412 DECL_NAME (field) = NULL_TREE;
6413 /* Now that layout has been performed, set the size of the
6414 field to the size of its declared type; the rest of the
6415 field is effectively invisible. */
6416 DECL_SIZE (field) = TYPE_SIZE (type);
29edb15c 6417 /* We must also reset the DECL_MODE of the field. */
90d84934 6418 DECL_MODE (field) = TYPE_MODE (type);
01955e96 6419 }
555456b1
MM
6420 else
6421 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6422 empty_base_offsets);
01955e96 6423
2003cd37 6424 /* Remember the location of any empty classes in FIELD. */
90d84934
JM
6425 record_subobject_offsets (TREE_TYPE (field),
6426 byte_position(field),
6427 empty_base_offsets,
6428 /*is_data_member=*/true);
2003cd37 6429
eca7f13c
MM
6430 /* If a bit-field does not immediately follow another bit-field,
6431 and yet it starts in the middle of a byte, we have failed to
6432 comply with the ABI. */
6433 if (warn_abi
c8094d83 6434 && DECL_C_BIT_FIELD (field)
660845bf
ZL
6435 /* The TREE_NO_WARNING flag gets set by Objective-C when
6436 laying out an Objective-C class. The ObjC ABI differs
6437 from the C++ ABI, and so we do not want a warning
6438 here. */
6439 && !TREE_NO_WARNING (field)
eca7f13c
MM
6440 && !last_field_was_bitfield
6441 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6442 DECL_FIELD_BIT_OFFSET (field),
6443 bitsize_unit_node)))
15827d12
PC
6444 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6445 "offset of %qD is not ABI-compliant and may "
6446 "change in a future version of GCC", field);
eca7f13c 6447
38a4afee
MM
6448 /* The middle end uses the type of expressions to determine the
6449 possible range of expression values. In order to optimize
6450 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
3db45ab5 6451 must be made aware of the width of "i", via its type.
38a4afee 6452
3db45ab5 6453 Because C++ does not have integer types of arbitrary width,
38a4afee
MM
6454 we must (for the purposes of the front end) convert from the
6455 type assigned here to the declared type of the bitfield
6456 whenever a bitfield expression is used as an rvalue.
6457 Similarly, when assigning a value to a bitfield, the value
6458 must be converted to the type given the bitfield here. */
6459 if (DECL_C_BIT_FIELD (field))
6460 {
38a4afee 6461 unsigned HOST_WIDE_INT width;
24030e4c 6462 tree ftype = TREE_TYPE (field);
ae7e9ddd 6463 width = tree_to_uhwi (DECL_SIZE (field));
38a4afee 6464 if (width != TYPE_PRECISION (ftype))
24030e4c
JJ
6465 {
6466 TREE_TYPE (field)
6467 = c_build_bitfield_integer_type (width,
6468 TYPE_UNSIGNED (ftype));
6469 TREE_TYPE (field)
6470 = cp_build_qualified_type (TREE_TYPE (field),
a3360e77 6471 cp_type_quals (ftype));
24030e4c 6472 }
38a4afee
MM
6473 }
6474
01955e96
MM
6475 /* If we needed additional padding after this field, add it
6476 now. */
6477 if (padding)
6478 {
6479 tree padding_field;
6480
c2255bc4
AH
6481 padding_field = build_decl (input_location,
6482 FIELD_DECL,
01955e96 6483 NULL_TREE,
c8094d83 6484 char_type_node);
01955e96
MM
6485 DECL_BIT_FIELD (padding_field) = 1;
6486 DECL_SIZE (padding_field) = padding;
1e099144 6487 DECL_CONTEXT (padding_field) = t;
ea258926 6488 DECL_ARTIFICIAL (padding_field) = 1;
78e0d62b 6489 DECL_IGNORED_P (padding_field) = 1;
c20118a8 6490 layout_nonempty_base_or_field (rli, padding_field,
c8094d83 6491 NULL_TREE,
17bbb839 6492 empty_base_offsets);
01955e96 6493 }
eca7f13c
MM
6494
6495 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
5c24fba6
MM
6496 }
6497
90d84934 6498 if (!integer_zerop (rli->bitpos))
e3ccdd50
MM
6499 {
6500 /* Make sure that we are on a byte boundary so that the size of
6501 the class without virtual bases will always be a round number
6502 of bytes. */
db3927fb 6503 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
e3ccdd50
MM
6504 normalize_rli (rli);
6505 }
17bbb839 6506
3ef397c1
MM
6507 /* Delete all zero-width bit-fields from the list of fields. Now
6508 that the type is laid out they are no longer important. */
6509 remove_zero_width_bit_fields (t);
6510
17bbb839 6511 /* Create the version of T used for virtual bases. We do not use
9e1e64ec 6512 make_class_type for this version; this is an artificial type. For
17bbb839 6513 a POD type, we just reuse T. */
c32097d8 6514 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
06ceef4e 6515 {
17bbb839 6516 base_t = make_node (TREE_CODE (t));
c8094d83 6517
90d84934
JM
6518 /* Set the size and alignment for the new type. */
6519 tree eoc;
6520
6521 /* If the ABI version is not at least two, and the last
6522 field was a bit-field, RLI may not be on a byte
6523 boundary. In particular, rli_size_unit_so_far might
6524 indicate the last complete byte, while rli_size_so_far
6525 indicates the total number of bits used. Therefore,
6526 rli_size_so_far, rather than rli_size_unit_so_far, is
6527 used to compute TYPE_SIZE_UNIT. */
6528 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6529 TYPE_SIZE_UNIT (base_t)
6530 = size_binop (MAX_EXPR,
cda0a029 6531 fold_convert (sizetype,
90d84934
JM
6532 size_binop (CEIL_DIV_EXPR,
6533 rli_size_so_far (rli),
6534 bitsize_int (BITS_PER_UNIT))),
6535 eoc);
6536 TYPE_SIZE (base_t)
6537 = size_binop (MAX_EXPR,
6538 rli_size_so_far (rli),
6539 size_binop (MULT_EXPR,
cda0a029 6540 fold_convert (bitsizetype, eoc),
90d84934 6541 bitsize_int (BITS_PER_UNIT)));
17bbb839
MM
6542 TYPE_ALIGN (base_t) = rli->record_align;
6543 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6544
6545 /* Copy the fields from T. */
6546 next_field = &TYPE_FIELDS (base_t);
910ad8de 6547 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
17bbb839
MM
6548 if (TREE_CODE (field) == FIELD_DECL)
6549 {
c50e614b 6550 *next_field = copy_node (field);
17bbb839 6551 DECL_CONTEXT (*next_field) = base_t;
910ad8de 6552 next_field = &DECL_CHAIN (*next_field);
17bbb839 6553 }
c50e614b 6554 *next_field = NULL_TREE;
17bbb839
MM
6555
6556 /* Record the base version of the type. */
6557 CLASSTYPE_AS_BASE (t) = base_t;
5a5cccaa 6558 TYPE_CONTEXT (base_t) = t;
83b14b88 6559 }
1f84ec23 6560 else
17bbb839 6561 CLASSTYPE_AS_BASE (t) = t;
0b41abe6 6562
5ec1192e
MM
6563 /* Every empty class contains an empty class. */
6564 if (CLASSTYPE_EMPTY_P (t))
6565 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6566
8d08fdba
MS
6567 /* Set the TYPE_DECL for this type to contain the right
6568 value for DECL_OFFSET, so that we can use it as part
6569 of a COMPONENT_REF for multiple inheritance. */
d2e5ee5c 6570 layout_decl (TYPE_MAIN_DECL (t), 0);
8d08fdba 6571
7177d104
MS
6572 /* Now fix up any virtual base class types that we left lying
6573 around. We must get these done before we try to lay out the
5c24fba6
MM
6574 virtual function table. As a side-effect, this will remove the
6575 base subobject fields. */
17bbb839
MM
6576 layout_virtual_bases (rli, empty_base_offsets);
6577
c8094d83 6578 /* Make sure that empty classes are reflected in RLI at this
17bbb839
MM
6579 point. */
6580 include_empty_classes(rli);
6581
6582 /* Make sure not to create any structures with zero size. */
58731fd1 6583 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
c8094d83 6584 place_field (rli,
c2255bc4
AH
6585 build_decl (input_location,
6586 FIELD_DECL, NULL_TREE, char_type_node));
17bbb839 6587
a402c1b1
JM
6588 /* If this is a non-POD, declaring it packed makes a difference to how it
6589 can be used as a field; don't let finalize_record_size undo it. */
6590 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6591 rli->packed_maybe_necessary = true;
6592
3b426391 6593 /* Let the back end lay out the type. */
17bbb839 6594 finish_record_layout (rli, /*free_p=*/true);
9785e4b1 6595
26d40c3d
JM
6596 if (TYPE_SIZE_UNIT (t)
6597 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6598 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6599 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7e9a3ad3 6600 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
26d40c3d 6601
17bbb839
MM
6602 /* Warn about bases that can't be talked about due to ambiguity. */
6603 warn_about_ambiguous_bases (t);
78b45a24 6604
00bfffa4 6605 /* Now that we're done with layout, give the base fields the real types. */
910ad8de 6606 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
00bfffa4
JM
6607 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6608 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6609
9785e4b1 6610 /* Clean up. */
c20118a8 6611 splay_tree_delete (empty_base_offsets);
c5a35c3c
MM
6612
6613 if (CLASSTYPE_EMPTY_P (t)
3db45ab5 6614 && tree_int_cst_lt (sizeof_biggest_empty_class,
c0572427
MM
6615 TYPE_SIZE_UNIT (t)))
6616 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
2ef16140 6617}
c35cce41 6618
af287697
MM
6619/* Determine the "key method" for the class type indicated by TYPE,
6620 and set CLASSTYPE_KEY_METHOD accordingly. */
9aad8f83 6621
af287697
MM
6622void
6623determine_key_method (tree type)
9aad8f83
MA
6624{
6625 tree method;
6626
6627 if (TYPE_FOR_JAVA (type)
6628 || processing_template_decl
6629 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6630 || CLASSTYPE_INTERFACE_KNOWN (type))
af287697 6631 return;
9aad8f83 6632
af287697
MM
6633 /* The key method is the first non-pure virtual function that is not
6634 inline at the point of class definition. On some targets the
6635 key function may not be inline; those targets should not call
6636 this function until the end of the translation unit. */
9aad8f83 6637 for (method = TYPE_METHODS (type); method != NULL_TREE;
910ad8de 6638 method = DECL_CHAIN (method))
aaf8a23e
JH
6639 if (TREE_CODE (method) == FUNCTION_DECL
6640 && DECL_VINDEX (method) != NULL_TREE
9aad8f83
MA
6641 && ! DECL_DECLARED_INLINE_P (method)
6642 && ! DECL_PURE_VIRTUAL_P (method))
af287697
MM
6643 {
6644 CLASSTYPE_KEY_METHOD (type) = method;
6645 break;
6646 }
9aad8f83 6647
af287697 6648 return;
9aad8f83
MA
6649}
6650
385b73ab
DN
6651
6652/* Allocate and return an instance of struct sorted_fields_type with
6653 N fields. */
6654
6655static struct sorted_fields_type *
6656sorted_fields_type_new (int n)
6657{
6658 struct sorted_fields_type *sft;
766090c2 6659 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
385b73ab
DN
6660 + n * sizeof (tree));
6661 sft->len = n;
6662
6663 return sft;
6664}
6665
7e9a3ad3
MS
6666/* Helper of find_flexarrays. Return true when FLD refers to a non-static
6667 class data member of non-zero size, otherwise false. */
6668
6669static inline bool
6670field_nonempty_p (const_tree fld)
6671{
6672 if (TREE_CODE (fld) == ERROR_MARK)
6673 return false;
6674
6675 tree type = TREE_TYPE (fld);
6676 if (TREE_CODE (fld) == FIELD_DECL
6677 && TREE_CODE (type) != ERROR_MARK
6678 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6679 {
6680 return TYPE_SIZE (type)
6681 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6682 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6683 }
6684
6685 return false;
6686}
6687
6688/* Used by find_flexarrays and related. */
6689struct flexmems_t {
6690 /* The first flexible array member or non-zero array member found
6691 in order of layout. */
6692 tree array;
6693 /* First non-static non-empty data member in the class or its bases. */
6694 tree first;
6695 /* First non-static non-empty data member following either the flexible
6696 array member, if found, or the zero-length array member. */
6697 tree after;
6698};
6699
6700/* Find either the first flexible array member or the first zero-length
6701 array, in that order or preference, among members of class T (but not
6702 its base classes), and set members of FMEM accordingly. */
6703
6704static void
6705find_flexarrays (tree t, flexmems_t *fmem)
6706{
6707 for (tree fld = TYPE_FIELDS (t), next; fld; fld = next)
6708 {
6709 /* Find the next non-static data member if it exists. */
6710 for (next = fld;
6711 (next = DECL_CHAIN (next))
6712 && TREE_CODE (next) != FIELD_DECL; );
05dd97db 6713
7e9a3ad3
MS
6714 tree fldtype = TREE_TYPE (fld);
6715 if (TREE_CODE (fld) != TYPE_DECL
6716 && RECORD_OR_UNION_TYPE_P (fldtype)
6717 && TYPE_ANONYMOUS_P (fldtype))
6718 {
6719 /* Members of anonymous structs and unions are treated as if
6720 they were members of the containing class. Descend into
6721 the anonymous struct or union and find a flexible array
6722 member or zero-length array among its fields. */
6723 find_flexarrays (fldtype, fmem);
6724 continue;
6725 }
6726
6727 /* Skip anything that's not a (non-static) data member. */
6728 if (TREE_CODE (fld) != FIELD_DECL)
6729 continue;
6730
6731 /* Skip virtual table pointers. */
6732 if (DECL_ARTIFICIAL (fld))
6733 continue;
6734
6735 if (field_nonempty_p (fld))
6736 {
6737 /* Remember the first non-static data member. */
6738 if (!fmem->first)
6739 fmem->first = fld;
05dd97db 6740
7e9a3ad3
MS
6741 /* Remember the first non-static data member after the flexible
6742 array member, if one has been found, or the zero-length array
6743 if it has been found. */
6744 if (!fmem->after && fmem->array)
6745 fmem->after = fld;
6746 }
05dd97db 6747
7e9a3ad3
MS
6748 /* Skip non-arrays. */
6749 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6750 continue;
6751
6752 /* Determine the upper bound of the array if it has one. */
d1243d27 6753 if (TYPE_DOMAIN (fldtype))
7e9a3ad3
MS
6754 {
6755 if (fmem->array)
6756 {
6757 /* Make a record of the zero-length array if either one
6758 such field or a flexible array member has been seen to
6759 handle the pathological and unlikely case of multiple
6760 such members. */
6761 if (!fmem->after)
6762 fmem->after = fld;
6763 }
d1243d27 6764 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
7e9a3ad3
MS
6765 /* Remember the first zero-length array unless a flexible array
6766 member has already been seen. */
6767 fmem->array = fld;
6768 }
6769 else
6770 {
6771 /* Flexible array members have no upper bound. */
6772 if (fmem->array)
6773 {
6774 /* Replace the zero-length array if it's been stored and
6775 reset the after pointer. */
05dd97db 6776 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7e9a3ad3
MS
6777 {
6778 fmem->array = fld;
6779 fmem->after = NULL_TREE;
6780 }
6781 }
05dd97db 6782 else
7e9a3ad3
MS
6783 fmem->array = fld;
6784 }
6785 }
6786}
6787
6788/* Issue diagnostics for invalid flexible array members or zero-length
6789 arrays that are not the last elements of the containing class or its
6790 base classes or that are its sole members. */
6791
6792static void
6793diagnose_flexarrays (tree t, const flexmems_t *fmem)
6794{
6795 /* Members of anonymous structs and unions are considered to be members
6796 of the containing struct or union. */
6797 if (TYPE_ANONYMOUS_P (t) || !fmem->array)
6798 return;
6799
6800 const char *msg = 0;
6801
d1243d27 6802 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7e9a3ad3
MS
6803 {
6804 if (fmem->after)
6805 msg = G_("zero-size array member %qD not at end of %q#T");
6806 else if (!fmem->first)
6807 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6808
6809 if (msg && pedwarn (DECL_SOURCE_LOCATION (fmem->array),
6810 OPT_Wpedantic, msg, fmem->array, t))
6811
6812 inform (location_of (t), "in the definition of %q#T", t);
6813 }
6814 else
6815 {
6816 if (fmem->after)
6817 msg = G_("flexible array member %qD not at end of %q#T");
6818 else if (!fmem->first)
6819 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6820
6821 if (msg)
6822 {
6823 error_at (DECL_SOURCE_LOCATION (fmem->array), msg,
6824 fmem->array, t);
6825
6826 /* In the unlikely event that the member following the flexible
6827 array member is declared in a different class, point to it.
6828 Otherwise it should be obvious. */
6829 if (fmem->after
6830 && (DECL_CONTEXT (fmem->after) != DECL_CONTEXT (fmem->array)))
6831 inform (DECL_SOURCE_LOCATION (fmem->after),
6832 "next member %q#D declared here",
6833 fmem->after);
05dd97db 6834
7e9a3ad3
MS
6835 inform (location_of (t), "in the definition of %q#T", t);
6836 }
6837 }
6838}
6839
6840
6841/* Recursively check to make sure that any flexible array or zero-length
6842 array members of class T or its bases are valid (i.e., not the sole
6843 non-static data member of T and, if one exists, that it is the last
6844 non-static data member of T and its base classes. FMEM is expected
6845 to be initially null and is used internally by recursive calls to
6846 the function. Issue the appropriate diagnostics for the array member
6847 that fails the checks. */
6848
6849static void
6850check_flexarrays (tree t, flexmems_t *fmem /* = NULL */)
6851{
6852 /* Initialize the result of a search for flexible array and zero-length
6853 array members. Avoid doing any work if the most interesting FMEM data
6854 have already been populated. */
6855 flexmems_t flexmems = flexmems_t ();
6856 if (!fmem)
6857 fmem = &flexmems;
6858 else if (fmem->array && fmem->first && fmem->after)
6859 return;
6860
6861 /* Recursively check the primary base class first. */
6862 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6863 {
6864 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6865 check_flexarrays (basetype, fmem);
6866 }
6867
6868 /* Recursively check the base classes. */
6869 int nbases = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
6870 for (int i = 0; i < nbases; ++i)
6871 {
6872 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6873
6874 /* The primary base class was already checked above. */
6875 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6876 continue;
6877
6878 /* Virtual base classes are at the end. */
6879 if (BINFO_VIRTUAL_P (base_binfo))
6880 continue;
6881
6882 /* Check the base class. */
6883 check_flexarrays (BINFO_TYPE (base_binfo), fmem);
6884 }
6885
6886 if (fmem == &flexmems)
6887 {
6888 /* Check virtual base classes only once per derived class.
6889 I.e., this check is not performed recursively for base
6890 classes. */
6891 int i;
6892 tree base_binfo;
6893 vec<tree, va_gc> *vbases;
6894 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6895 vec_safe_iterate (vbases, i, &base_binfo); i++)
6896 {
6897 /* Check the virtual base class. */
6898 tree basetype = TREE_TYPE (base_binfo);
6899
6900 check_flexarrays (basetype, fmem);
6901 }
6902 }
6903
6904 /* Search the members of the current (derived) class. */
6905 find_flexarrays (t, fmem);
6906
6907 if (fmem == &flexmems)
05dd97db 6908 {
7e9a3ad3
MS
6909 /* Issue diagnostics for invalid flexible and zero-length array members
6910 found in base classes or among the members of the current class. */
6911 diagnose_flexarrays (t, fmem);
6912 }
6913}
385b73ab 6914
548502d3 6915/* Perform processing required when the definition of T (a class type)
7e9a3ad3
MS
6916 is complete. Diagnose invalid definitions of flexible array members
6917 and zero-size arrays. */
2ef16140
MM
6918
6919void
94edc4ab 6920finish_struct_1 (tree t)
2ef16140
MM
6921{
6922 tree x;
00a17e31 6923 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
e6858a84 6924 tree virtuals = NULL_TREE;
2ef16140 6925
d0f062fb 6926 if (COMPLETE_TYPE_P (t))
2ef16140 6927 {
9e1e64ec 6928 gcc_assert (MAYBE_CLASS_TYPE_P (t));
1f070f2b 6929 error ("redefinition of %q#T", t);
2ef16140
MM
6930 popclass ();
6931 return;
6932 }
6933
2ef16140
MM
6934 /* If this type was previously laid out as a forward reference,
6935 make sure we lay it out again. */
2ef16140 6936 TYPE_SIZE (t) = NULL_TREE;
911a71a7 6937 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
2ef16140 6938
5ec1192e
MM
6939 /* Make assumptions about the class; we'll reset the flags if
6940 necessary. */
58731fd1
MM
6941 CLASSTYPE_EMPTY_P (t) = 1;
6942 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
5ec1192e 6943 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
3b49d762 6944 CLASSTYPE_LITERAL_P (t) = true;
58731fd1 6945
2ef16140 6946 /* Do end-of-class semantic processing: checking the validity of the
03702748 6947 bases and members and add implicitly generated methods. */
58731fd1 6948 check_bases_and_members (t);
2ef16140 6949
f4f206f4 6950 /* Find the key method. */
a63996f1 6951 if (TYPE_CONTAINS_VPTR_P (t))
9aad8f83 6952 {
af287697
MM
6953 /* The Itanium C++ ABI permits the key method to be chosen when
6954 the class is defined -- even though the key method so
6955 selected may later turn out to be an inline function. On
6956 some systems (such as ARM Symbian OS) the key method cannot
6957 be determined until the end of the translation unit. On such
6958 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6959 will cause the class to be added to KEYED_CLASSES. Then, in
6960 finish_file we will determine the key method. */
6961 if (targetm.cxx.key_method_may_be_inline ())
6962 determine_key_method (t);
9aad8f83
MA
6963
6964 /* If a polymorphic class has no key method, we may emit the vtable
5796bf34
JM
6965 in every translation unit where the class definition appears. If
6966 we're devirtualizing, we can look into the vtable even if we
6967 aren't emitting it. */
a41844e5 6968 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
9aad8f83
MA
6969 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6970 }
6971
2ef16140 6972 /* Layout the class itself. */
e93ee644 6973 layout_class_type (t, &virtuals);
a0c68737
NS
6974 if (CLASSTYPE_AS_BASE (t) != t)
6975 /* We use the base type for trivial assignments, and hence it
6976 needs a mode. */
6977 compute_record_mode (CLASSTYPE_AS_BASE (t));
8ebeee52 6978
7e9a3ad3
MS
6979 /* With the layout complete, check for flexible array members and
6980 zero-length arrays that might overlap other members in the final
6981 layout. */
6982 check_flexarrays (t);
6983
e93ee644 6984 virtuals = modify_all_vtables (t, nreverse (virtuals));
db5ae43f 6985
5e19c053 6986 /* If necessary, create the primary vtable for this class. */
e6858a84 6987 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
8d08fdba 6988 {
8d08fdba 6989 /* We must enter these virtuals into the table. */
3ef397c1 6990 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
da3d4dfa 6991 build_primary_vtable (NULL_TREE, t);
dbbf88d1 6992 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
0533d788
MM
6993 /* Here we know enough to change the type of our virtual
6994 function table, but we will wait until later this function. */
28531dd0 6995 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
d3061adb
JM
6996
6997 /* If we're warning about ABI tags, check the types of the new
6998 virtual functions. */
6999 if (warn_abi_tag)
7000 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7001 check_abi_tags (t, TREE_VALUE (v));
8d08fdba
MS
7002 }
7003
bbd15aac 7004 if (TYPE_CONTAINS_VPTR_P (t))
8d08fdba 7005 {
e93ee644
MM
7006 int vindex;
7007 tree fn;
7008
604a3205 7009 if (BINFO_VTABLE (TYPE_BINFO (t)))
50bc768d 7010 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
1eb4bea9 7011 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
50bc768d 7012 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
1eb4bea9 7013
e6858a84 7014 /* Add entries for virtual functions introduced by this class. */
604a3205
NS
7015 BINFO_VIRTUALS (TYPE_BINFO (t))
7016 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
e93ee644
MM
7017
7018 /* Set DECL_VINDEX for all functions declared in this class. */
c8094d83
MS
7019 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7020 fn;
7021 fn = TREE_CHAIN (fn),
e93ee644
MM
7022 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7023 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
4977bab6
ZW
7024 {
7025 tree fndecl = BV_FN (fn);
7026
7027 if (DECL_THUNK_P (fndecl))
7028 /* A thunk. We should never be calling this entry directly
7029 from this vtable -- we'd use the entry for the non
7030 thunk base function. */
7031 DECL_VINDEX (fndecl) = NULL_TREE;
7032 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7d60be94 7033 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
4977bab6 7034 }
8d08fdba
MS
7035 }
7036
d2c5305b 7037 finish_struct_bits (t);
0a35513e 7038 set_method_tm_attributes (t);
d9a6bd32
JJ
7039 if (flag_openmp || flag_openmp_simd)
7040 finish_omp_declare_simd_methods (t);
8d08fdba 7041
f30432d7
MS
7042 /* Complete the rtl for any static member objects of the type we're
7043 working on. */
910ad8de 7044 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
5a6ccc94 7045 if (VAR_P (x) && TREE_STATIC (x)
650fcd07 7046 && TREE_TYPE (x) != error_mark_node
c7f4981a 7047 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
19e7881c 7048 DECL_MODE (x) = TYPE_MODE (t);
8d08fdba 7049
f90cdf34 7050 /* Done with FIELDS...now decide whether to sort these for
58010b57 7051 faster lookups later.
f90cdf34 7052
6c73ad72 7053 We use a small number because most searches fail (succeeding
f90cdf34
MT
7054 ultimately as the search bores through the inheritance
7055 hierarchy), and we want this failure to occur quickly. */
7056
cba0366c 7057 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
f90cdf34 7058
b9e75696
JM
7059 /* Complain if one of the field types requires lower visibility. */
7060 constrain_class_visibility (t);
7061
8d7a5379
MM
7062 /* Make the rtl for any new vtables we have created, and unmark
7063 the base types we marked. */
7064 finish_vtbls (t);
c8094d83 7065
23656158
MM
7066 /* Build the VTT for T. */
7067 build_vtt (t);
8d7a5379 7068
f03e8526
MM
7069 /* This warning does not make sense for Java classes, since they
7070 cannot have destructors. */
880a467b 7071 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
abce9208
PC
7072 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7073 && !CLASSTYPE_FINAL (t))
880a467b
NS
7074 warning (OPT_Wnon_virtual_dtor,
7075 "%q#T has virtual functions and accessible"
7076 " non-virtual destructor", t);
8d08fdba 7077
0154eaa8 7078 complete_vars (t);
8d08fdba 7079
9e9ff709
MS
7080 if (warn_overloaded_virtual)
7081 warn_hidden (t);
8d08fdba 7082
43d9ad1d
DS
7083 /* Class layout, assignment of virtual table slots, etc., is now
7084 complete. Give the back end a chance to tweak the visibility of
7085 the class or perform any other required target modifications. */
7086 targetm.cxx.adjust_class_at_definition (t);
7087
ae673f14 7088 maybe_suppress_debug_info (t);
8d08fdba 7089
2077db1b
CT
7090 if (flag_vtable_verify)
7091 vtv_save_class_info (t);
7092
b7442fb5 7093 dump_class_hierarchy (t);
c8094d83 7094
d2e5ee5c 7095 /* Finish debugging output for this type. */
881c6935 7096 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
bfcbe068 7097
e7b6bcf3 7098 if (TYPE_TRANSPARENT_AGGR (t))
bfcbe068 7099 {
e7b6bcf3
JJ
7100 tree field = first_field (t);
7101 if (field == NULL_TREE || error_operand_p (field))
7102 {
42b40eff 7103 error ("type transparent %q#T does not have any fields", t);
e7b6bcf3
JJ
7104 TYPE_TRANSPARENT_AGGR (t) = 0;
7105 }
7106 else if (DECL_ARTIFICIAL (field))
7107 {
7108 if (DECL_FIELD_IS_BASE (field))
7109 error ("type transparent class %qT has base classes", t);
7110 else
7111 {
7112 gcc_checking_assert (DECL_VIRTUAL_P (field));
7113 error ("type transparent class %qT has virtual functions", t);
7114 }
7115 TYPE_TRANSPARENT_AGGR (t) = 0;
7116 }
42b40eff
PC
7117 else if (TYPE_MODE (t) != DECL_MODE (field))
7118 {
7119 error ("type transparent %q#T cannot be made transparent because "
7120 "the type of the first field has a different ABI from the "
7121 "class overall", t);
7122 TYPE_TRANSPARENT_AGGR (t) = 0;
7123 }
bfcbe068 7124 }
8d08fdba 7125}
f30432d7 7126
cba0366c
FC
7127/* Insert FIELDS into T for the sorted case if the FIELDS count is
7128 equal to THRESHOLD or greater than THRESHOLD. */
7129
7130static void
7131insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
7132{
7133 int n_fields = count_fields (fields);
7134 if (n_fields >= threshold)
7135 {
7136 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7137 add_fields_to_record_type (fields, field_vec, 0);
7138 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7139 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7140 }
7141}
7142
7143/* Insert lately defined enum ENUMTYPE into T for the sorted case. */
7144
7145void
7146insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
7147{
7148 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
7149 if (sorted_fields)
7150 {
7151 int i;
7152 int n_fields
7153 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
7154 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7155
7156 for (i = 0; i < sorted_fields->len; ++i)
7157 field_vec->elts[i] = sorted_fields->elts[i];
7158
7159 add_enum_fields_to_record_type (enumtype, field_vec,
7160 sorted_fields->len);
7161 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7162 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7163 }
7164}
7165
61a127b3
MM
7166/* When T was built up, the member declarations were added in reverse
7167 order. Rearrange them to declaration order. */
7168
7169void
94edc4ab 7170unreverse_member_declarations (tree t)
61a127b3
MM
7171{
7172 tree next;
7173 tree prev;
7174 tree x;
7175
7088fca9
KL
7176 /* The following lists are all in reverse order. Put them in
7177 declaration order now. */
61a127b3 7178 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
7088fca9 7179 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
61a127b3
MM
7180
7181 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
7182 reverse order, so we can't just use nreverse. */
7183 prev = NULL_TREE;
c8094d83
MS
7184 for (x = TYPE_FIELDS (t);
7185 x && TREE_CODE (x) != TYPE_DECL;
61a127b3
MM
7186 x = next)
7187 {
910ad8de
NF
7188 next = DECL_CHAIN (x);
7189 DECL_CHAIN (x) = prev;
61a127b3
MM
7190 prev = x;
7191 }
7192 if (prev)
7193 {
910ad8de 7194 DECL_CHAIN (TYPE_FIELDS (t)) = x;
61a127b3
MM
7195 if (prev)
7196 TYPE_FIELDS (t) = prev;
7197 }
7198}
7199
f30432d7 7200tree
94edc4ab 7201finish_struct (tree t, tree attributes)
f30432d7 7202{
82a98427 7203 location_t saved_loc = input_location;
1f0d71c5 7204
61a127b3
MM
7205 /* Now that we've got all the field declarations, reverse everything
7206 as necessary. */
7207 unreverse_member_declarations (t);
f30432d7 7208
91d231cb 7209 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
e0ff153d 7210 fixup_attribute_variants (t);
6467930b 7211
1f0d71c5
NS
7212 /* Nadger the current location so that diagnostics point to the start of
7213 the struct, not the end. */
f31686a3 7214 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
1f0d71c5 7215
5566b478 7216 if (processing_template_decl)
f30432d7 7217 {
7fb213d8
GB
7218 tree x;
7219
b0e0b31f 7220 finish_struct_methods (t);
867580ce 7221 TYPE_SIZE (t) = bitsize_zero_node;
ae54ec16 7222 TYPE_SIZE_UNIT (t) = size_zero_node;
7fb213d8
GB
7223
7224 /* We need to emit an error message if this type was used as a parameter
7225 and it is an abstract type, even if it is a template. We construct
7226 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7227 account and we call complete_vars with this type, which will check
7228 the PARM_DECLS. Note that while the type is being defined,
7229 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7230 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
585b44d3 7231 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
910ad8de 7232 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
7fb213d8 7233 if (DECL_PURE_VIRTUAL_P (x))
9771b263 7234 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7fb213d8 7235 complete_vars (t);
e58d4228
JM
7236 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
7237 an enclosing scope is a template class, so that this function be
7238 found by lookup_fnfields_1 when the using declaration is not
7239 instantiated yet. */
7240 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7241 if (TREE_CODE (x) == USING_DECL)
7242 {
7243 tree fn = strip_using_decl (x);
7244 if (is_overloaded_fn (fn))
7245 for (; fn; fn = OVL_NEXT (fn))
7246 add_method (t, OVL_CURRENT (fn), x);
7247 }
040ca4b3
JM
7248
7249 /* Remember current #pragma pack value. */
7250 TYPE_PRECISION (t) = maximum_field_alignment;
947296ca
JM
7251
7252 /* Fix up any variants we've already built. */
7253 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7254 {
7255 TYPE_SIZE (x) = TYPE_SIZE (t);
7256 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7257 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7258 TYPE_METHODS (x) = TYPE_METHODS (t);
7259 }
6f1b4c42 7260 }
f30432d7 7261 else
9f33663b 7262 finish_struct_1 (t);
5566b478 7263
0090caca
JM
7264 if (is_std_init_list (t))
7265 {
7266 /* People keep complaining that the compiler crashes on an invalid
7267 definition of initializer_list, so I guess we should explicitly
7268 reject it. What the compiler internals care about is that it's a
7269 template and has a pointer field followed by an integer field. */
7270 bool ok = false;
7271 if (processing_template_decl)
7272 {
7273 tree f = next_initializable_field (TYPE_FIELDS (t));
7274 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7275 {
7276 f = next_initializable_field (DECL_CHAIN (f));
14e51ef2 7277 if (f && same_type_p (TREE_TYPE (f), size_type_node))
0090caca
JM
7278 ok = true;
7279 }
7280 }
7281 if (!ok)
40fecdd6
JM
7282 fatal_error (input_location,
7283 "definition of std::initializer_list does not match "
0090caca
JM
7284 "#include <initializer_list>");
7285 }
7286
82a98427 7287 input_location = saved_loc;
1f0d71c5 7288
5566b478 7289 TYPE_BEING_DEFINED (t) = 0;
8f032717 7290
5566b478 7291 if (current_class_type)
b74a0560 7292 popclass ();
5566b478 7293 else
357351e5 7294 error ("trying to finish struct, but kicked out due to previous parse errors");
5566b478 7295
637f68e8
JM
7296 if (processing_template_decl && at_function_scope_p ()
7297 /* Lambdas are defined by the LAMBDA_EXPR. */
7298 && !LAMBDA_TYPE_P (t))
5f261ba9 7299 add_stmt (build_min (TAG_DEFN, t));
ae673f14 7300
5566b478 7301 return t;
f30432d7 7302}
8d08fdba 7303\f
abcc192b 7304/* Hash table to avoid endless recursion when handling references. */
8d67ee55 7305static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
abcc192b 7306
51ddb82e 7307/* Return the dynamic type of INSTANCE, if known.
8d08fdba
MS
7308 Used to determine whether the virtual function table is needed
7309 or not.
7310
7311 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
97d953bb
MM
7312 of our knowledge of its type. *NONNULL should be initialized
7313 before this function is called. */
e92cc029 7314
d8e178a0 7315static tree
555551c2 7316fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
8d08fdba 7317{
555551c2
MM
7318#define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7319
8d08fdba
MS
7320 switch (TREE_CODE (instance))
7321 {
7322 case INDIRECT_REF:
608afcc5 7323 if (POINTER_TYPE_P (TREE_TYPE (instance)))
a0de9d20
JM
7324 return NULL_TREE;
7325 else
555551c2 7326 return RECUR (TREE_OPERAND (instance, 0));
a0de9d20 7327
8d08fdba
MS
7328 case CALL_EXPR:
7329 /* This is a call to a constructor, hence it's never zero. */
7330 if (TREE_HAS_CONSTRUCTOR (instance))
7331 {
7332 if (nonnull)
7333 *nonnull = 1;
51ddb82e 7334 return TREE_TYPE (instance);
8d08fdba 7335 }
51ddb82e 7336 return NULL_TREE;
8d08fdba
MS
7337
7338 case SAVE_EXPR:
7339 /* This is a call to a constructor, hence it's never zero. */
7340 if (TREE_HAS_CONSTRUCTOR (instance))
7341 {
7342 if (nonnull)
7343 *nonnull = 1;
51ddb82e 7344 return TREE_TYPE (instance);
8d08fdba 7345 }
555551c2 7346 return RECUR (TREE_OPERAND (instance, 0));
8d08fdba 7347
5be014d5 7348 case POINTER_PLUS_EXPR:
8d08fdba
MS
7349 case PLUS_EXPR:
7350 case MINUS_EXPR:
394fd776 7351 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
555551c2 7352 return RECUR (TREE_OPERAND (instance, 0));
8d08fdba
MS
7353 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7354 /* Propagate nonnull. */
555551c2
MM
7355 return RECUR (TREE_OPERAND (instance, 0));
7356
51ddb82e 7357 return NULL_TREE;
8d08fdba 7358
63a906f0 7359 CASE_CONVERT:
555551c2 7360 return RECUR (TREE_OPERAND (instance, 0));
8d08fdba
MS
7361
7362 case ADDR_EXPR:
88f19756 7363 instance = TREE_OPERAND (instance, 0);
8d08fdba 7364 if (nonnull)
88f19756
RH
7365 {
7366 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7367 with a real object -- given &p->f, p can still be null. */
7368 tree t = get_base_address (instance);
7369 /* ??? Probably should check DECL_WEAK here. */
7370 if (t && DECL_P (t))
7371 *nonnull = 1;
7372 }
555551c2 7373 return RECUR (instance);
8d08fdba
MS
7374
7375 case COMPONENT_REF:
642124c6
RH
7376 /* If this component is really a base class reference, then the field
7377 itself isn't definitive. */
7378 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
555551c2
MM
7379 return RECUR (TREE_OPERAND (instance, 0));
7380 return RECUR (TREE_OPERAND (instance, 1));
8d08fdba 7381
8d08fdba
MS
7382 case VAR_DECL:
7383 case FIELD_DECL:
7384 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
9e1e64ec 7385 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
8d08fdba
MS
7386 {
7387 if (nonnull)
7388 *nonnull = 1;
51ddb82e 7389 return TREE_TYPE (TREE_TYPE (instance));
8d08fdba 7390 }
e92cc029 7391 /* fall through... */
8d08fdba
MS
7392 case TARGET_EXPR:
7393 case PARM_DECL:
f63ab951 7394 case RESULT_DECL:
9e1e64ec 7395 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
8d08fdba
MS
7396 {
7397 if (nonnull)
7398 *nonnull = 1;
51ddb82e 7399 return TREE_TYPE (instance);
8d08fdba 7400 }
394fd776 7401 else if (instance == current_class_ptr)
0cbd7506
MS
7402 {
7403 if (nonnull)
7404 *nonnull = 1;
7405
f10eaa2d
JM
7406 /* if we're in a ctor or dtor, we know our type. If
7407 current_class_ptr is set but we aren't in a function, we're in
7408 an NSDMI (and therefore a constructor). */
7409 if (current_scope () != current_function_decl
7410 || (DECL_LANG_SPECIFIC (current_function_decl)
7411 && (DECL_CONSTRUCTOR_P (current_function_decl)
7412 || DECL_DESTRUCTOR_P (current_function_decl))))
0cbd7506
MS
7413 {
7414 if (cdtorp)
7415 *cdtorp = 1;
7416 return TREE_TYPE (TREE_TYPE (instance));
7417 }
7418 }
394fd776 7419 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
0cbd7506 7420 {
555551c2 7421 /* We only need one hash table because it is always left empty. */
c203e8a7
TS
7422 if (!fixed_type_or_null_ref_ht)
7423 fixed_type_or_null_ref_ht
8d67ee55 7424 = new hash_table<nofree_ptr_hash<tree_node> > (37);
555551c2 7425
0cbd7506
MS
7426 /* Reference variables should be references to objects. */
7427 if (nonnull)
8d08fdba 7428 *nonnull = 1;
c8094d83 7429
555551c2 7430 /* Enter the INSTANCE in a table to prevent recursion; a
772f8889
MM
7431 variable's initializer may refer to the variable
7432 itself. */
5a6ccc94 7433 if (VAR_P (instance)
772f8889 7434 && DECL_INITIAL (instance)
bae14a37 7435 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
c203e8a7 7436 && !fixed_type_or_null_ref_ht->find (instance))
772f8889
MM
7437 {
7438 tree type;
703c8606 7439 tree_node **slot;
555551c2 7440
c203e8a7 7441 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
555551c2
MM
7442 *slot = instance;
7443 type = RECUR (DECL_INITIAL (instance));
c203e8a7 7444 fixed_type_or_null_ref_ht->remove_elt (instance);
555551c2 7445
772f8889
MM
7446 return type;
7447 }
8d08fdba 7448 }
51ddb82e 7449 return NULL_TREE;
8d08fdba
MS
7450
7451 default:
51ddb82e 7452 return NULL_TREE;
8d08fdba 7453 }
555551c2 7454#undef RECUR
8d08fdba 7455}
51ddb82e 7456
838dfd8a 7457/* Return nonzero if the dynamic type of INSTANCE is known, and
338d90b8
NS
7458 equivalent to the static type. We also handle the case where
7459 INSTANCE is really a pointer. Return negative if this is a
7460 ctor/dtor. There the dynamic type is known, but this might not be
7461 the most derived base of the original object, and hence virtual
c65cb8d1 7462 bases may not be laid out according to this type.
51ddb82e
JM
7463
7464 Used to determine whether the virtual function table is needed
7465 or not.
7466
7467 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
97d953bb
MM
7468 of our knowledge of its type. *NONNULL should be initialized
7469 before this function is called. */
51ddb82e
JM
7470
7471int
94edc4ab 7472resolves_to_fixed_type_p (tree instance, int* nonnull)
51ddb82e
JM
7473{
7474 tree t = TREE_TYPE (instance);
394fd776 7475 int cdtorp = 0;
4d3baecc
JM
7476 tree fixed;
7477
65f0c5b3 7478 /* processing_template_decl can be false in a template if we're in
234bef96
PC
7479 instantiate_non_dependent_expr, but we still want to suppress
7480 this check. */
e0e1b357 7481 if (in_template_function ())
4d3baecc
JM
7482 {
7483 /* In a template we only care about the type of the result. */
7484 if (nonnull)
7485 *nonnull = true;
7486 return true;
7487 }
7488
7489 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
51ddb82e
JM
7490 if (fixed == NULL_TREE)
7491 return 0;
7492 if (POINTER_TYPE_P (t))
7493 t = TREE_TYPE (t);
394fd776
NS
7494 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7495 return 0;
7496 return cdtorp ? -1 : 1;
51ddb82e
JM
7497}
7498
8d08fdba
MS
7499\f
7500void
94edc4ab 7501init_class_processing (void)
8d08fdba
MS
7502{
7503 current_class_depth = 0;
61a127b3 7504 current_class_stack_size = 10;
c8094d83 7505 current_class_stack
0ac1b889 7506 = XNEWVEC (struct class_stack_node, current_class_stack_size);
9771b263 7507 vec_alloc (local_classes, 8);
c5a35c3c 7508 sizeof_biggest_empty_class = size_zero_node;
8d08fdba 7509
0e5921e8
ZW
7510 ridpointers[(int) RID_PUBLIC] = access_public_node;
7511 ridpointers[(int) RID_PRIVATE] = access_private_node;
7512 ridpointers[(int) RID_PROTECTED] = access_protected_node;
8d08fdba
MS
7513}
7514
39fb05d0
MM
7515/* Restore the cached PREVIOUS_CLASS_LEVEL. */
7516
7517static void
7518restore_class_cache (void)
7519{
39fb05d0 7520 tree type;
39fb05d0
MM
7521
7522 /* We are re-entering the same class we just left, so we don't
7523 have to search the whole inheritance matrix to find all the
7524 decls to bind again. Instead, we install the cached
7525 class_shadowed list and walk through it binding names. */
7526 push_binding_level (previous_class_level);
7527 class_binding_level = previous_class_level;
39fb05d0 7528 /* Restore IDENTIFIER_TYPE_VALUE. */
c8094d83
MS
7529 for (type = class_binding_level->type_shadowed;
7530 type;
39fb05d0
MM
7531 type = TREE_CHAIN (type))
7532 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7533}
7534
a723baf1
MM
7535/* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7536 appropriate for TYPE.
8d08fdba 7537
8d08fdba
MS
7538 So that we may avoid calls to lookup_name, we cache the _TYPE
7539 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7540
7541 For multiple inheritance, we perform a two-pass depth-first search
39fb05d0 7542 of the type lattice. */
8d08fdba
MS
7543
7544void
29370796 7545pushclass (tree type)
8d08fdba 7546{
c888c93b
MM
7547 class_stack_node_t csn;
7548
0771d9d7
JM
7549 type = TYPE_MAIN_VARIANT (type);
7550
61a127b3 7551 /* Make sure there is enough room for the new entry on the stack. */
c8094d83 7552 if (current_class_depth + 1 >= current_class_stack_size)
8d08fdba 7553 {
61a127b3
MM
7554 current_class_stack_size *= 2;
7555 current_class_stack
7767580e 7556 = XRESIZEVEC (struct class_stack_node, current_class_stack,
3db45ab5 7557 current_class_stack_size);
8d08fdba
MS
7558 }
7559
61a127b3 7560 /* Insert a new entry on the class stack. */
c888c93b
MM
7561 csn = current_class_stack + current_class_depth;
7562 csn->name = current_class_name;
7563 csn->type = current_class_type;
7564 csn->access = current_access_specifier;
7565 csn->names_used = 0;
7566 csn->hidden = 0;
61a127b3
MM
7567 current_class_depth++;
7568
7569 /* Now set up the new type. */
8d08fdba
MS
7570 current_class_name = TYPE_NAME (type);
7571 if (TREE_CODE (current_class_name) == TYPE_DECL)
7572 current_class_name = DECL_NAME (current_class_name);
7573 current_class_type = type;
7574
61a127b3
MM
7575 /* By default, things in classes are private, while things in
7576 structures or unions are public. */
c8094d83
MS
7577 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7578 ? access_private_node
61a127b3
MM
7579 : access_public_node);
7580
89b578be
MM
7581 if (previous_class_level
7582 && type != previous_class_level->this_entity
8d08fdba
MS
7583 && current_class_depth == 1)
7584 {
7585 /* Forcibly remove any old class remnants. */
8f032717 7586 invalidate_class_lookup_cache ();
8d08fdba
MS
7587 }
7588
c8094d83 7589 if (!previous_class_level
89b578be
MM
7590 || type != previous_class_level->this_entity
7591 || current_class_depth > 1)
90ea9897 7592 pushlevel_class ();
29370796 7593 else
39fb05d0 7594 restore_class_cache ();
8f032717
MM
7595}
7596
39fb05d0
MM
7597/* When we exit a toplevel class scope, we save its binding level so
7598 that we can restore it quickly. Here, we've entered some other
7599 class, so we must invalidate our cache. */
8d08fdba 7600
8f032717 7601void
94edc4ab 7602invalidate_class_lookup_cache (void)
8f032717 7603{
89b578be 7604 previous_class_level = NULL;
8d08fdba 7605}
c8094d83 7606
8d08fdba 7607/* Get out of the current class scope. If we were in a class scope
b74a0560 7608 previously, that is the one popped to. */
e92cc029 7609
8d08fdba 7610void
94edc4ab 7611popclass (void)
8d08fdba 7612{
0771d9d7 7613 poplevel_class ();
8d08fdba
MS
7614
7615 current_class_depth--;
61a127b3
MM
7616 current_class_name = current_class_stack[current_class_depth].name;
7617 current_class_type = current_class_stack[current_class_depth].type;
7618 current_access_specifier = current_class_stack[current_class_depth].access;
8f032717
MM
7619 if (current_class_stack[current_class_depth].names_used)
7620 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8d08fdba
MS
7621}
7622
c888c93b
MM
7623/* Mark the top of the class stack as hidden. */
7624
7625void
7626push_class_stack (void)
7627{
7628 if (current_class_depth)
7629 ++current_class_stack[current_class_depth - 1].hidden;
7630}
7631
7632/* Mark the top of the class stack as un-hidden. */
7633
7634void
7635pop_class_stack (void)
7636{
7637 if (current_class_depth)
7638 --current_class_stack[current_class_depth - 1].hidden;
7639}
7640
fa6098f8 7641/* Returns 1 if the class type currently being defined is either T or
971e17ff
AS
7642 a nested type of T. Returns the type from the current_class_stack,
7643 which might be equivalent to but not equal to T in case of
7644 constrained partial specializations. */
b9082e8a 7645
971e17ff 7646tree
94edc4ab 7647currently_open_class (tree t)
b9082e8a
JM
7648{
7649 int i;
fa6098f8 7650
1cb801bc 7651 if (!CLASS_TYPE_P (t))
971e17ff 7652 return NULL_TREE;
1cb801bc 7653
3e5e84be
JM
7654 t = TYPE_MAIN_VARIANT (t);
7655
fa6098f8
MM
7656 /* We start looking from 1 because entry 0 is from global scope,
7657 and has no type. */
7658 for (i = current_class_depth; i > 0; --i)
c888c93b 7659 {
fa6098f8
MM
7660 tree c;
7661 if (i == current_class_depth)
7662 c = current_class_type;
7663 else
7664 {
7665 if (current_class_stack[i].hidden)
7666 break;
7667 c = current_class_stack[i].type;
7668 }
7669 if (!c)
7670 continue;
7671 if (same_type_p (c, t))
971e17ff 7672 return c;
c888c93b 7673 }
971e17ff 7674 return NULL_TREE;
b9082e8a
JM
7675}
7676
70adf8a9
JM
7677/* If either current_class_type or one of its enclosing classes are derived
7678 from T, return the appropriate type. Used to determine how we found
7679 something via unqualified lookup. */
7680
7681tree
94edc4ab 7682currently_open_derived_class (tree t)
70adf8a9
JM
7683{
7684 int i;
7685
9bcb9aae 7686 /* The bases of a dependent type are unknown. */
1fb3244a
MM
7687 if (dependent_type_p (t))
7688 return NULL_TREE;
7689
c44e68a5
KL
7690 if (!current_class_type)
7691 return NULL_TREE;
7692
70adf8a9
JM
7693 if (DERIVED_FROM_P (t, current_class_type))
7694 return current_class_type;
7695
7696 for (i = current_class_depth - 1; i > 0; --i)
c888c93b
MM
7697 {
7698 if (current_class_stack[i].hidden)
7699 break;
7700 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7701 return current_class_stack[i].type;
7702 }
70adf8a9
JM
7703
7704 return NULL_TREE;
7705}
7706
2d7d7f0f
JM
7707/* Return the outermost enclosing class type that is still open, or
7708 NULL_TREE. */
7709
7710tree
7711outermost_open_class (void)
7712{
7713 if (!current_class_type)
7714 return NULL_TREE;
7715 tree r = NULL_TREE;
cea83a3a
JM
7716 if (TYPE_BEING_DEFINED (current_class_type))
7717 r = current_class_type;
7718 for (int i = current_class_depth - 1; i > 0; --i)
2d7d7f0f
JM
7719 {
7720 if (current_class_stack[i].hidden)
7721 break;
7722 tree t = current_class_stack[i].type;
7723 if (!TYPE_BEING_DEFINED (t))
7724 break;
7725 r = t;
7726 }
7727 return r;
7728}
7729
a6846853
JM
7730/* Returns the innermost class type which is not a lambda closure type. */
7731
7732tree
7733current_nonlambda_class_type (void)
7734{
7735 int i;
7736
7737 /* We start looking from 1 because entry 0 is from global scope,
7738 and has no type. */
7739 for (i = current_class_depth; i > 0; --i)
7740 {
7741 tree c;
7742 if (i == current_class_depth)
7743 c = current_class_type;
7744 else
7745 {
7746 if (current_class_stack[i].hidden)
7747 break;
7748 c = current_class_stack[i].type;
7749 }
7750 if (!c)
7751 continue;
7752 if (!LAMBDA_TYPE_P (c))
7753 return c;
7754 }
7755 return NULL_TREE;
7756}
7757
8d08fdba 7758/* When entering a class scope, all enclosing class scopes' names with
14d22dd6
MM
7759 static meaning (static variables, static functions, types and
7760 enumerators) have to be visible. This recursive function calls
7761 pushclass for all enclosing class contexts until global or a local
7762 scope is reached. TYPE is the enclosed class. */
8d08fdba
MS
7763
7764void
14d22dd6 7765push_nested_class (tree type)
8d08fdba 7766{
b262d64c 7767 /* A namespace might be passed in error cases, like A::B:C. */
c8094d83 7768 if (type == NULL_TREE
56d0c6e3 7769 || !CLASS_TYPE_P (type))
a28e3c7f 7770 return;
c8094d83 7771
56d0c6e3 7772 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
8d08fdba 7773
29370796 7774 pushclass (type);
8d08fdba
MS
7775}
7776
a723baf1 7777/* Undoes a push_nested_class call. */
8d08fdba
MS
7778
7779void
94edc4ab 7780pop_nested_class (void)
8d08fdba 7781{
d2e5ee5c 7782 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8d08fdba 7783
b74a0560 7784 popclass ();
6b400b21 7785 if (context && CLASS_TYPE_P (context))
b74a0560 7786 pop_nested_class ();
8d08fdba
MS
7787}
7788
46ccf50a
JM
7789/* Returns the number of extern "LANG" blocks we are nested within. */
7790
7791int
94edc4ab 7792current_lang_depth (void)
46ccf50a 7793{
9771b263 7794 return vec_safe_length (current_lang_base);
46ccf50a
JM
7795}
7796
8d08fdba
MS
7797/* Set global variables CURRENT_LANG_NAME to appropriate value
7798 so that behavior of name-mangling machinery is correct. */
7799
7800void
94edc4ab 7801push_lang_context (tree name)
8d08fdba 7802{
9771b263 7803 vec_safe_push (current_lang_base, current_lang_name);
8d08fdba 7804
e229f2cd 7805 if (name == lang_name_cplusplus)
8d08fdba 7806 {
8d08fdba
MS
7807 current_lang_name = name;
7808 }
e229f2cd
PB
7809 else if (name == lang_name_java)
7810 {
e229f2cd
PB
7811 current_lang_name = name;
7812 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7813 (See record_builtin_java_type in decl.c.) However, that causes
7814 incorrect debug entries if these types are actually used.
00a17e31 7815 So we re-enable debug output after extern "Java". */
e3cd9945
APB
7816 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7817 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7818 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7819 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7820 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7821 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7822 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7823 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
e229f2cd 7824 }
8d08fdba
MS
7825 else if (name == lang_name_c)
7826 {
8d08fdba
MS
7827 current_lang_name = name;
7828 }
7829 else
9e637a26 7830 error ("language string %<\"%E\"%> not recognized", name);
8d08fdba 7831}
c8094d83 7832
8d08fdba 7833/* Get out of the current language scope. */
e92cc029 7834
8d08fdba 7835void
94edc4ab 7836pop_lang_context (void)
8d08fdba 7837{
9771b263 7838 current_lang_name = current_lang_base->pop ();
8d08fdba 7839}
8d08fdba
MS
7840\f
7841/* Type instantiation routines. */
7842
104bf76a
MM
7843/* Given an OVERLOAD and a TARGET_TYPE, return the function that
7844 matches the TARGET_TYPE. If there is no satisfactory match, return
eff3a276
MM
7845 error_mark_node, and issue an error & warning messages under
7846 control of FLAGS. Permit pointers to member function if FLAGS
7847 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7848 a template-id, and EXPLICIT_TARGS are the explicitly provided
248e1b22
MM
7849 template arguments.
7850
7851 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7852 is the base path used to reference those member functions. If
5e7b9f60
JM
7853 the address is resolved to a member function, access checks will be
7854 performed and errors issued if appropriate. */
104bf76a 7855
2c73f9f5 7856static tree
c8094d83 7857resolve_address_of_overloaded_function (tree target_type,
94edc4ab 7858 tree overload,
988db853 7859 tsubst_flags_t complain,
92af500d 7860 bool template_only,
eff3a276
MM
7861 tree explicit_targs,
7862 tree access_path)
2c73f9f5 7863{
104bf76a 7864 /* Here's what the standard says:
c8094d83 7865
104bf76a
MM
7866 [over.over]
7867
7868 If the name is a function template, template argument deduction
7869 is done, and if the argument deduction succeeds, the deduced
7870 arguments are used to generate a single template function, which
7871 is added to the set of overloaded functions considered.
7872
7873 Non-member functions and static member functions match targets of
7874 type "pointer-to-function" or "reference-to-function." Nonstatic
7875 member functions match targets of type "pointer-to-member
7876 function;" the function type of the pointer to member is used to
7877 select the member function from the set of overloaded member
7878 functions. If a nonstatic member function is selected, the
7879 reference to the overloaded function name is required to have the
7880 form of a pointer to member as described in 5.3.1.
7881
7882 If more than one function is selected, any template functions in
7883 the set are eliminated if the set also contains a non-template
7884 function, and any given template function is eliminated if the
7885 set contains a second template function that is more specialized
7886 than the first according to the partial ordering rules 14.5.5.2.
7887 After such eliminations, if any, there shall remain exactly one
7888 selected function. */
7889
7890 int is_ptrmem = 0;
104bf76a
MM
7891 /* We store the matches in a TREE_LIST rooted here. The functions
7892 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7893 interoperability with most_specialized_instantiation. */
7894 tree matches = NULL_TREE;
50714e79 7895 tree fn;
7bead48f 7896 tree target_fn_type;
104bf76a 7897
d8f8dca1
MM
7898 /* By the time we get here, we should be seeing only real
7899 pointer-to-member types, not the internal POINTER_TYPE to
7900 METHOD_TYPE representation. */
50e10fa8 7901 gcc_assert (!TYPE_PTR_P (target_type)
50bc768d 7902 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
104bf76a 7903
50bc768d 7904 gcc_assert (is_overloaded_fn (overload));
c8094d83 7905
104bf76a 7906 /* Check that the TARGET_TYPE is reasonable. */
6721db5d
JM
7907 if (TYPE_PTRFN_P (target_type)
7908 || TYPE_REFFN_P (target_type))
381ddaa6 7909 /* This is OK. */;
104bf76a
MM
7910 else if (TYPE_PTRMEMFUNC_P (target_type))
7911 /* This is OK, too. */
7912 is_ptrmem = 1;
7913 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
db80e34e
JJ
7914 /* This is OK, too. This comes from a conversion to reference
7915 type. */
7916 target_type = build_reference_type (target_type);
c8094d83 7917 else
104bf76a 7918 {
988db853 7919 if (complain & tf_error)
c4f73174 7920 error ("cannot resolve overloaded function %qD based on"
0cbd7506
MS
7921 " conversion to type %qT",
7922 DECL_NAME (OVL_FUNCTION (overload)), target_type);
104bf76a
MM
7923 return error_mark_node;
7924 }
c8094d83 7925
7bead48f
JM
7926 /* Non-member functions and static member functions match targets of type
7927 "pointer-to-function" or "reference-to-function." Nonstatic member
7928 functions match targets of type "pointer-to-member-function;" the
7929 function type of the pointer to member is used to select the member
7930 function from the set of overloaded member functions.
7931
7932 So figure out the FUNCTION_TYPE that we want to match against. */
7933 target_fn_type = static_fn_type (target_type);
7934
104bf76a
MM
7935 /* If we can find a non-template function that matches, we can just
7936 use it. There's no point in generating template instantiations
7937 if we're just going to throw them out anyhow. But, of course, we
7938 can only do this when we don't *need* a template function. */
7939 if (!template_only)
7940 {
7941 tree fns;
7942
a723baf1 7943 for (fns = overload; fns; fns = OVL_NEXT (fns))
104bf76a 7944 {
a723baf1 7945 tree fn = OVL_CURRENT (fns);
2c73f9f5 7946
104bf76a
MM
7947 if (TREE_CODE (fn) == TEMPLATE_DECL)
7948 /* We're not looking for templates just yet. */
7949 continue;
7950
7951 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7952 != is_ptrmem)
7953 /* We're looking for a non-static member, and this isn't
7954 one, or vice versa. */
7955 continue;
34ff2673 7956
d63d5d0c
ILT
7957 /* Ignore functions which haven't been explicitly
7958 declared. */
34ff2673
RS
7959 if (DECL_ANTICIPATED (fn))
7960 continue;
7961
104bf76a 7962 /* See if there's a match. */
b8fd7909
JM
7963 tree fntype = static_fn_type (fn);
7964 if (same_type_p (target_fn_type, fntype)
7965 || can_convert_tx_safety (target_fn_type, fntype))
e1b3e07d 7966 matches = tree_cons (fn, NULL_TREE, matches);
104bf76a
MM
7967 }
7968 }
7969
7970 /* Now, if we've already got a match (or matches), there's no need
7971 to proceed to the template functions. But, if we don't have a
7972 match we need to look at them, too. */
c8094d83 7973 if (!matches)
2c73f9f5 7974 {
104bf76a 7975 tree target_arg_types;
8d3631f8 7976 tree target_ret_type;
104bf76a 7977 tree fns;
c166b898
ILT
7978 tree *args;
7979 unsigned int nargs, ia;
7980 tree arg;
104bf76a 7981
4393e105 7982 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8d3631f8 7983 target_ret_type = TREE_TYPE (target_fn_type);
e5214479 7984
c166b898
ILT
7985 nargs = list_length (target_arg_types);
7986 args = XALLOCAVEC (tree, nargs);
7987 for (arg = target_arg_types, ia = 0;
7988 arg != NULL_TREE && arg != void_list_node;
7989 arg = TREE_CHAIN (arg), ++ia)
7990 args[ia] = TREE_VALUE (arg);
7991 nargs = ia;
7992
a723baf1 7993 for (fns = overload; fns; fns = OVL_NEXT (fns))
104bf76a 7994 {
a723baf1 7995 tree fn = OVL_CURRENT (fns);
104bf76a 7996 tree instantiation;
104bf76a
MM
7997 tree targs;
7998
7999 if (TREE_CODE (fn) != TEMPLATE_DECL)
8000 /* We're only looking for templates. */
8001 continue;
8002
8003 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8004 != is_ptrmem)
4393e105 8005 /* We're not looking for a non-static member, and this is
104bf76a
MM
8006 one, or vice versa. */
8007 continue;
8008
79d8a272
JM
8009 tree ret = target_ret_type;
8010
8011 /* If the template has a deduced return type, don't expose it to
8012 template argument deduction. */
8013 if (undeduced_auto_decl (fn))
8014 ret = NULL_TREE;
8015
104bf76a 8016 /* Try to do argument deduction. */
f31c0a32 8017 targs = make_tree_vec (DECL_NTPARMS (fn));
cd057e3a 8018 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
79d8a272 8019 nargs, ret,
cd057e3a 8020 DEDUCE_EXACT, LOOKUP_NORMAL,
2b24855e 8021 false, false);
104bf76a
MM
8022 if (instantiation == error_mark_node)
8023 /* Instantiation failed. */
8024 continue;
8025
971e17ff
AS
8026 /* Constraints must be satisfied. This is done before
8027 return type deduction since that instantiates the
8028 function. */
8029 if (flag_concepts && !constraints_satisfied_p (instantiation))
8030 continue;
8031
79d8a272
JM
8032 /* And now force instantiation to do return type deduction. */
8033 if (undeduced_auto_decl (instantiation))
8034 {
8035 ++function_depth;
8036 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8037 --function_depth;
8038
8039 require_deduced_type (instantiation);
8040 }
8041
104bf76a 8042 /* See if there's a match. */
b8fd7909
JM
8043 tree fntype = static_fn_type (instantiation);
8044 if (same_type_p (target_fn_type, fntype)
8045 || can_convert_tx_safety (target_fn_type, fntype))
e1b3e07d 8046 matches = tree_cons (instantiation, fn, matches);
104bf76a
MM
8047 }
8048
8049 /* Now, remove all but the most specialized of the matches. */
8050 if (matches)
8051 {
e5214479 8052 tree match = most_specialized_instantiation (matches);
104bf76a
MM
8053
8054 if (match != error_mark_node)
3db45ab5
MS
8055 matches = tree_cons (TREE_PURPOSE (match),
8056 NULL_TREE,
7ca383e6 8057 NULL_TREE);
104bf76a
MM
8058 }
8059 }
8060
8061 /* Now we should have exactly one function in MATCHES. */
8062 if (matches == NULL_TREE)
8063 {
8064 /* There were *no* matches. */
988db853 8065 if (complain & tf_error)
104bf76a 8066 {
0cbd7506 8067 error ("no matches converting function %qD to type %q#T",
95e20768 8068 DECL_NAME (OVL_CURRENT (overload)),
0cbd7506 8069 target_type);
6b9b6b15 8070
c224bdc1 8071 print_candidates (overload);
104bf76a
MM
8072 }
8073 return error_mark_node;
2c73f9f5 8074 }
104bf76a
MM
8075 else if (TREE_CHAIN (matches))
8076 {
e04c614e
JM
8077 /* There were too many matches. First check if they're all
8078 the same function. */
3649b9b7 8079 tree match = NULL_TREE;
104bf76a 8080
e04c614e 8081 fn = TREE_PURPOSE (matches);
3649b9b7 8082
beb42d20
ST
8083 /* For multi-versioned functions, more than one match is just fine and
8084 decls_match will return false as they are different. */
8085 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8086 if (!decls_match (fn, TREE_PURPOSE (match))
8087 && !targetm.target_option.function_versions
8088 (fn, TREE_PURPOSE (match)))
8089 break;
e04c614e
JM
8090
8091 if (match)
104bf76a 8092 {
988db853 8093 if (complain & tf_error)
e04c614e
JM
8094 {
8095 error ("converting overloaded function %qD to type %q#T is ambiguous",
8096 DECL_NAME (OVL_FUNCTION (overload)),
8097 target_type);
104bf76a 8098
e04c614e
JM
8099 /* Since print_candidates expects the functions in the
8100 TREE_VALUE slot, we flip them here. */
8101 for (match = matches; match; match = TREE_CHAIN (match))
8102 TREE_VALUE (match) = TREE_PURPOSE (match);
104bf76a 8103
e04c614e
JM
8104 print_candidates (matches);
8105 }
104bf76a 8106
e04c614e 8107 return error_mark_node;
104bf76a 8108 }
104bf76a
MM
8109 }
8110
50714e79
MM
8111 /* Good, exactly one match. Now, convert it to the correct type. */
8112 fn = TREE_PURPOSE (matches);
8113
b1ce3eb2 8114 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
988db853 8115 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
19420d00 8116 {
b1ce3eb2 8117 static int explained;
c8094d83 8118
988db853 8119 if (!(complain & tf_error))
0cbd7506 8120 return error_mark_node;
19420d00 8121
cbe5f3b3 8122 permerror (input_location, "assuming pointer to member %qD", fn);
b1ce3eb2 8123 if (!explained)
0cbd7506 8124 {
1f5b3869 8125 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
0cbd7506
MS
8126 explained = 1;
8127 }
19420d00 8128 }
84583208 8129
3649b9b7
ST
8130 /* If a pointer to a function that is multi-versioned is requested, the
8131 pointer to the dispatcher function is returned instead. This works
8132 well because indirectly calling the function will dispatch the right
8133 function version at run-time. */
8134 if (DECL_FUNCTION_VERSIONED (fn))
8135 {
beb42d20
ST
8136 fn = get_function_version_dispatcher (fn);
8137 if (fn == NULL)
8138 return error_mark_node;
3649b9b7 8139 /* Mark all the versions corresponding to the dispatcher as used. */
988db853 8140 if (!(complain & tf_conv))
3649b9b7
ST
8141 mark_versions_used (fn);
8142 }
8143
84583208
MM
8144 /* If we're doing overload resolution purely for the purpose of
8145 determining conversion sequences, we should not consider the
8146 function used. If this conversion sequence is selected, the
8147 function will be marked as used at this point. */
988db853 8148 if (!(complain & tf_conv))
eff3a276 8149 {
4ad610c9 8150 /* Make =delete work with SFINAE. */
988db853 8151 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
4ad610c9 8152 return error_mark_node;
988db853 8153 if (!mark_used (fn, complain) && !(complain & tf_error))
9f635aba 8154 return error_mark_node;
248e1b22
MM
8155 }
8156
8157 /* We could not check access to member functions when this
8158 expression was originally created since we did not know at that
8159 time to which function the expression referred. */
5e7b9f60 8160 if (DECL_FUNCTION_MEMBER_P (fn))
248e1b22
MM
8161 {
8162 gcc_assert (access_path);
988db853 8163 perform_or_defer_access_check (access_path, fn, fn, complain);
eff3a276 8164 }
a6ecf8b6 8165
50714e79 8166 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
988db853 8167 return cp_build_addr_expr (fn, complain);
50714e79
MM
8168 else
8169 {
5ade1ed2 8170 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
50714e79
MM
8171 will mark the function as addressed, but here we must do it
8172 explicitly. */
dffd7eb6 8173 cxx_mark_addressable (fn);
50714e79
MM
8174
8175 return fn;
8176 }
2c73f9f5
ML
8177}
8178
ec255269
MS
8179/* This function will instantiate the type of the expression given in
8180 RHS to match the type of LHSTYPE. If errors exist, then return
988db853 8181 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
5e76004e
NS
8182 we complain on errors. If we are not complaining, never modify rhs,
8183 as overload resolution wants to try many possible instantiations, in
8184 the hope that at least one will work.
c8094d83 8185
e6e174e5
JM
8186 For non-recursive calls, LHSTYPE should be a function, pointer to
8187 function, or a pointer to member function. */
e92cc029 8188
8d08fdba 8189tree
988db853 8190instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8d08fdba 8191{
988db853 8192 tsubst_flags_t complain_in = complain;
eff3a276 8193 tree access_path = NULL_TREE;
c8094d83 8194
988db853 8195 complain &= ~tf_ptrmem_ok;
c8094d83 8196
fbfc8363 8197 if (lhstype == unknown_type_node)
8d08fdba 8198 {
988db853 8199 if (complain & tf_error)
8251199e 8200 error ("not enough type information");
8d08fdba
MS
8201 return error_mark_node;
8202 }
8203
8204 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
abff8e06 8205 {
6721db5d
JM
8206 tree fntype = non_reference (lhstype);
8207 if (same_type_p (fntype, TREE_TYPE (rhs)))
abff8e06 8208 return rhs;
c8094d83 8209 if (flag_ms_extensions
6721db5d 8210 && TYPE_PTRMEMFUNC_P (fntype)
a723baf1
MM
8211 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8212 /* Microsoft allows `A::f' to be resolved to a
8213 pointer-to-member. */
8214 ;
8215 else
8216 {
988db853 8217 if (complain & tf_error)
c3c1f2b7 8218 error ("cannot convert %qE from type %qT to type %qT",
6721db5d 8219 rhs, TREE_TYPE (rhs), fntype);
a723baf1
MM
8220 return error_mark_node;
8221 }
abff8e06 8222 }
8d08fdba 8223
c5ce25ce 8224 if (BASELINK_P (rhs))
eff3a276
MM
8225 {
8226 access_path = BASELINK_ACCESS_BINFO (rhs);
8227 rhs = BASELINK_FUNCTIONS (rhs);
8228 }
50ad9642 8229
5ae9ba3e
MM
8230 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8231 deduce any type information. */
8232 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8233 {
988db853 8234 if (complain & tf_error)
5ae9ba3e
MM
8235 error ("not enough type information");
8236 return error_mark_node;
8237 }
8238
eff3a276
MM
8239 /* There only a few kinds of expressions that may have a type
8240 dependent on overload resolution. */
8241 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8242 || TREE_CODE (rhs) == COMPONENT_REF
3f3fd87d 8243 || is_overloaded_fn (rhs)
95e20768 8244 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
c73964b2 8245
8d08fdba
MS
8246 /* This should really only be used when attempting to distinguish
8247 what sort of a pointer to function we have. For now, any
8248 arithmetic operation which is not supported on pointers
8249 is rejected as an error. */
8250
8251 switch (TREE_CODE (rhs))
8252 {
8d08fdba 8253 case COMPONENT_REF:
92af500d 8254 {
5ae9ba3e 8255 tree member = TREE_OPERAND (rhs, 1);
92af500d 8256
988db853 8257 member = instantiate_type (lhstype, member, complain);
5ae9ba3e 8258 if (member != error_mark_node
92af500d 8259 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
04c06002 8260 /* Do not lose object's side effects. */
5ae9ba3e
MM
8261 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8262 TREE_OPERAND (rhs, 0), member);
8263 return member;
92af500d 8264 }
8d08fdba 8265
2a238a97 8266 case OFFSET_REF:
05e0b2f4
JM
8267 rhs = TREE_OPERAND (rhs, 1);
8268 if (BASELINK_P (rhs))
988db853 8269 return instantiate_type (lhstype, rhs, complain_in);
05e0b2f4 8270
2a238a97
MM
8271 /* This can happen if we are forming a pointer-to-member for a
8272 member template. */
50bc768d 8273 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
05e0b2f4 8274
2a238a97 8275 /* Fall through. */
874503bc 8276
386b8a85 8277 case TEMPLATE_ID_EXPR:
2bdb0643
JM
8278 {
8279 tree fns = TREE_OPERAND (rhs, 0);
8280 tree args = TREE_OPERAND (rhs, 1);
8281
19420d00 8282 return
988db853 8283 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
92af500d 8284 /*template_only=*/true,
eff3a276 8285 args, access_path);
2bdb0643 8286 }
386b8a85 8287
2c73f9f5 8288 case OVERLOAD:
a723baf1 8289 case FUNCTION_DECL:
c8094d83 8290 return
988db853 8291 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
92af500d 8292 /*template_only=*/false,
eff3a276
MM
8293 /*explicit_targs=*/NULL_TREE,
8294 access_path);
2c73f9f5 8295
ca36f057 8296 case ADDR_EXPR:
19420d00
NS
8297 {
8298 if (PTRMEM_OK_P (rhs))
988db853 8299 complain |= tf_ptrmem_ok;
c8094d83 8300
988db853 8301 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
19420d00 8302 }
ca36f057
MM
8303
8304 case ERROR_MARK:
8305 return error_mark_node;
8306
8307 default:
8dc2b103 8308 gcc_unreachable ();
ca36f057 8309 }
8dc2b103 8310 return error_mark_node;
ca36f057
MM
8311}
8312\f
8313/* Return the name of the virtual function pointer field
8314 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8315 this may have to look back through base types to find the
8316 ultimate field name. (For single inheritance, these could
8317 all be the same name. Who knows for multiple inheritance). */
8318
8319static tree
94edc4ab 8320get_vfield_name (tree type)
ca36f057 8321{
37a247a0 8322 tree binfo, base_binfo;
ca36f057
MM
8323 char *buf;
8324
37a247a0 8325 for (binfo = TYPE_BINFO (type);
fa743e8c 8326 BINFO_N_BASE_BINFOS (binfo);
37a247a0
NS
8327 binfo = base_binfo)
8328 {
8329 base_binfo = BINFO_BASE_BINFO (binfo, 0);
ca36f057 8330
37a247a0
NS
8331 if (BINFO_VIRTUAL_P (base_binfo)
8332 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8333 break;
8334 }
c8094d83 8335
ca36f057 8336 type = BINFO_TYPE (binfo);
67f5655f 8337 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
3db45ab5 8338 + TYPE_NAME_LENGTH (type) + 2);
ea122333
JM
8339 sprintf (buf, VFIELD_NAME_FORMAT,
8340 IDENTIFIER_POINTER (constructor_name (type)));
ca36f057
MM
8341 return get_identifier (buf);
8342}
8343
8344void
94edc4ab 8345print_class_statistics (void)
ca36f057 8346{
7aa6d18a
SB
8347 if (! GATHER_STATISTICS)
8348 return;
8349
ca36f057
MM
8350 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8351 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
ca36f057
MM
8352 if (n_vtables)
8353 {
8354 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8355 n_vtables, n_vtable_searches);
8356 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8357 n_vtable_entries, n_vtable_elems);
8358 }
ca36f057
MM
8359}
8360
8361/* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8362 according to [class]:
0cbd7506 8363 The class-name is also inserted
ca36f057
MM
8364 into the scope of the class itself. For purposes of access checking,
8365 the inserted class name is treated as if it were a public member name. */
8366
8367void
94edc4ab 8368build_self_reference (void)
ca36f057
MM
8369{
8370 tree name = constructor_name (current_class_type);
8371 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8372 tree saved_cas;
8373
8374 DECL_NONLOCAL (value) = 1;
8375 DECL_CONTEXT (value) = current_class_type;
8376 DECL_ARTIFICIAL (value) = 1;
a3d87771 8377 SET_DECL_SELF_REFERENCE_P (value);
6f1abb06 8378 set_underlying_type (value);
ca36f057
MM
8379
8380 if (processing_template_decl)
8381 value = push_template_decl (value);
8382
8383 saved_cas = current_access_specifier;
8384 current_access_specifier = access_public_node;
8385 finish_member_declaration (value);
8386 current_access_specifier = saved_cas;
8387}
8388
8389/* Returns 1 if TYPE contains only padding bytes. */
8390
8391int
94edc4ab 8392is_empty_class (tree type)
ca36f057 8393{
ca36f057
MM
8394 if (type == error_mark_node)
8395 return 0;
8396
2588c9e9 8397 if (! CLASS_TYPE_P (type))
ca36f057
MM
8398 return 0;
8399
90d84934 8400 return CLASSTYPE_EMPTY_P (type);
ca36f057
MM
8401}
8402
2588c9e9 8403/* Returns true if TYPE contains no actual data, just various
0930cc0e 8404 possible combinations of empty classes and possibly a vptr. */
2588c9e9
JM
8405
8406bool
8407is_really_empty_class (tree type)
8408{
2588c9e9
JM
8409 if (CLASS_TYPE_P (type))
8410 {
8411 tree field;
8412 tree binfo;
8413 tree base_binfo;
8414 int i;
8415
0930cc0e
JM
8416 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8417 out, but we'd like to be able to check this before then. */
8418 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8419 return true;
8420
2588c9e9
JM
8421 for (binfo = TYPE_BINFO (type), i = 0;
8422 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8423 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8424 return false;
910ad8de 8425 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2588c9e9
JM
8426 if (TREE_CODE (field) == FIELD_DECL
8427 && !DECL_ARTIFICIAL (field)
08d6d8bb
JM
8428 /* An unnamed bit-field is not a data member. */
8429 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
2588c9e9
JM
8430 && !is_really_empty_class (TREE_TYPE (field)))
8431 return false;
8432 return true;
8433 }
8434 else if (TREE_CODE (type) == ARRAY_TYPE)
08d6d8bb
JM
8435 return (integer_zerop (array_type_nelts_top (type))
8436 || is_really_empty_class (TREE_TYPE (type)));
2588c9e9
JM
8437 return false;
8438}
8439
ca36f057
MM
8440/* Note that NAME was looked up while the current class was being
8441 defined and that the result of that lookup was DECL. */
8442
8443void
94edc4ab 8444maybe_note_name_used_in_class (tree name, tree decl)
ca36f057
MM
8445{
8446 splay_tree names_used;
8447
8448 /* If we're not defining a class, there's nothing to do. */
39fb05d0 8449 if (!(innermost_scope_kind() == sk_class
d5f4eddd
JM
8450 && TYPE_BEING_DEFINED (current_class_type)
8451 && !LAMBDA_TYPE_P (current_class_type)))
ca36f057 8452 return;
c8094d83 8453
ca36f057
MM
8454 /* If there's already a binding for this NAME, then we don't have
8455 anything to worry about. */
c8094d83 8456 if (lookup_member (current_class_type, name,
db422ace 8457 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
ca36f057
MM
8458 return;
8459
8460 if (!current_class_stack[current_class_depth - 1].names_used)
8461 current_class_stack[current_class_depth - 1].names_used
8462 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8463 names_used = current_class_stack[current_class_depth - 1].names_used;
8464
8465 splay_tree_insert (names_used,
c8094d83 8466 (splay_tree_key) name,
ca36f057
MM
8467 (splay_tree_value) decl);
8468}
8469
8470/* Note that NAME was declared (as DECL) in the current class. Check
0e339752 8471 to see that the declaration is valid. */
ca36f057
MM
8472
8473void
94edc4ab 8474note_name_declared_in_class (tree name, tree decl)
ca36f057
MM
8475{
8476 splay_tree names_used;
8477 splay_tree_node n;
8478
8479 /* Look to see if we ever used this name. */
c8094d83 8480 names_used
ca36f057
MM
8481 = current_class_stack[current_class_depth - 1].names_used;
8482 if (!names_used)
8483 return;
8ce1235b
KT
8484 /* The C language allows members to be declared with a type of the same
8485 name, and the C++ standard says this diagnostic is not required. So
8486 allow it in extern "C" blocks unless predantic is specified.
8487 Allow it in all cases if -ms-extensions is specified. */
8488 if ((!pedantic && current_lang_name == lang_name_c)
8489 || flag_ms_extensions)
8490 return;
ca36f057
MM
8491 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8492 if (n)
8493 {
8494 /* [basic.scope.class]
c8094d83 8495
ca36f057
MM
8496 A name N used in a class S shall refer to the same declaration
8497 in its context and when re-evaluated in the completed scope of
8498 S. */
cbe5f3b3 8499 permerror (input_location, "declaration of %q#D", decl);
15827d12
PC
8500 permerror (location_of ((tree) n->value),
8501 "changes meaning of %qD from %q#D",
8502 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
ca36f057
MM
8503 }
8504}
8505
3461fba7
NS
8506/* Returns the VAR_DECL for the complete vtable associated with BINFO.
8507 Secondary vtables are merged with primary vtables; this function
8508 will return the VAR_DECL for the primary vtable. */
ca36f057 8509
c35cce41 8510tree
94edc4ab 8511get_vtbl_decl_for_binfo (tree binfo)
c35cce41
MM
8512{
8513 tree decl;
8514
8515 decl = BINFO_VTABLE (binfo);
5be014d5 8516 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
c35cce41 8517 {
50bc768d 8518 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
c35cce41
MM
8519 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8520 }
8521 if (decl)
5a6ccc94 8522 gcc_assert (VAR_P (decl));
c35cce41
MM
8523 return decl;
8524}
8525
911a71a7 8526
dbbf88d1
NS
8527/* Returns the binfo for the primary base of BINFO. If the resulting
8528 BINFO is a virtual base, and it is inherited elsewhere in the
8529 hierarchy, then the returned binfo might not be the primary base of
8530 BINFO in the complete object. Check BINFO_PRIMARY_P or
8531 BINFO_LOST_PRIMARY_P to be sure. */
911a71a7 8532
b5791fdc 8533static tree
94edc4ab 8534get_primary_binfo (tree binfo)
911a71a7
MM
8535{
8536 tree primary_base;
c8094d83 8537
911a71a7
MM
8538 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8539 if (!primary_base)
8540 return NULL_TREE;
8541
b5791fdc 8542 return copied_binfo (primary_base, binfo);
911a71a7
MM
8543}
8544
b5a28d80
JM
8545/* As above, but iterate until we reach the binfo that actually provides the
8546 vptr for BINFO. */
8547
8548static tree
8549most_primary_binfo (tree binfo)
8550{
8551 tree b = binfo;
8552 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8553 && !BINFO_LOST_PRIMARY_P (b))
8554 {
8555 tree primary_base = get_primary_binfo (b);
8556 gcc_assert (BINFO_PRIMARY_P (primary_base)
8557 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8558 b = primary_base;
8559 }
8560 return b;
8561}
8562
8563/* Returns true if BINFO gets its vptr from a virtual base of the most derived
8564 type. Note that the virtual inheritance might be above or below BINFO in
8565 the hierarchy. */
8566
8567bool
8568vptr_via_virtual_p (tree binfo)
8569{
8570 if (TYPE_P (binfo))
8571 binfo = TYPE_BINFO (binfo);
8572 tree primary = most_primary_binfo (binfo);
8573 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8574 a morally virtual base. */
8575 tree virt = binfo_via_virtual (primary, NULL_TREE);
8576 return virt != NULL_TREE;
8577}
8578
838dfd8a 8579/* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
b7442fb5
NS
8580
8581static int
94edc4ab 8582maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
b7442fb5
NS
8583{
8584 if (!indented_p)
8585 fprintf (stream, "%*s", indent, "");
8586 return 1;
8587}
8588
dbbf88d1
NS
8589/* Dump the offsets of all the bases rooted at BINFO to STREAM.
8590 INDENT should be zero when called from the top level; it is
8591 incremented recursively. IGO indicates the next expected BINFO in
9bcb9aae 8592 inheritance graph ordering. */
c35cce41 8593
dbbf88d1
NS
8594static tree
8595dump_class_hierarchy_r (FILE *stream,
0cbd7506
MS
8596 int flags,
8597 tree binfo,
8598 tree igo,
8599 int indent)
ca36f057 8600{
b7442fb5 8601 int indented = 0;
fa743e8c
NS
8602 tree base_binfo;
8603 int i;
c8094d83 8604
b7442fb5 8605 indented = maybe_indent_hierarchy (stream, indent, 0);
6c5bf58a 8606 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
fc6633e0 8607 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
6c5bf58a 8608 (HOST_WIDE_INT) (uintptr_t) binfo);
dbbf88d1
NS
8609 if (binfo != igo)
8610 {
8611 fprintf (stream, "alternative-path\n");
8612 return igo;
8613 }
8614 igo = TREE_CHAIN (binfo);
c8094d83 8615
9965d119 8616 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
9439e9a1 8617 tree_to_shwi (BINFO_OFFSET (binfo)));
9965d119
NS
8618 if (is_empty_class (BINFO_TYPE (binfo)))
8619 fprintf (stream, " empty");
8620 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8621 fprintf (stream, " nearly-empty");
809e3e7f 8622 if (BINFO_VIRTUAL_P (binfo))
dbbf88d1 8623 fprintf (stream, " virtual");
9965d119 8624 fprintf (stream, "\n");
ca36f057 8625
b7442fb5 8626 indented = 0;
fc6633e0 8627 if (BINFO_PRIMARY_P (binfo))
b7442fb5
NS
8628 {
8629 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6c5bf58a 8630 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
fc6633e0 8631 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
b7442fb5 8632 TFF_PLAIN_IDENTIFIER),
6c5bf58a 8633 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
b7442fb5
NS
8634 }
8635 if (BINFO_LOST_PRIMARY_P (binfo))
8636 {
8637 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8638 fprintf (stream, " lost-primary");
8639 }
8640 if (indented)
8641 fprintf (stream, "\n");
8642
8643 if (!(flags & TDF_SLIM))
8644 {
8645 int indented = 0;
c8094d83 8646
b7442fb5
NS
8647 if (BINFO_SUBVTT_INDEX (binfo))
8648 {
8649 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8650 fprintf (stream, " subvttidx=%s",
8651 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8652 TFF_PLAIN_IDENTIFIER));
8653 }
8654 if (BINFO_VPTR_INDEX (binfo))
8655 {
8656 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8657 fprintf (stream, " vptridx=%s",
8658 expr_as_string (BINFO_VPTR_INDEX (binfo),
8659 TFF_PLAIN_IDENTIFIER));
8660 }
8661 if (BINFO_VPTR_FIELD (binfo))
8662 {
8663 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8664 fprintf (stream, " vbaseoffset=%s",
8665 expr_as_string (BINFO_VPTR_FIELD (binfo),
8666 TFF_PLAIN_IDENTIFIER));
8667 }
8668 if (BINFO_VTABLE (binfo))
8669 {
8670 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8671 fprintf (stream, " vptr=%s",
8672 expr_as_string (BINFO_VTABLE (binfo),
8673 TFF_PLAIN_IDENTIFIER));
8674 }
c8094d83 8675
b7442fb5
NS
8676 if (indented)
8677 fprintf (stream, "\n");
8678 }
dbbf88d1 8679
fa743e8c
NS
8680 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8681 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
c8094d83 8682
dbbf88d1 8683 return igo;
c35cce41
MM
8684}
8685
8686/* Dump the BINFO hierarchy for T. */
8687
b7442fb5 8688static void
bb885938 8689dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
c35cce41 8690{
b7442fb5
NS
8691 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8692 fprintf (stream, " size=%lu align=%lu\n",
9439e9a1 8693 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
b7442fb5 8694 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
dbbf88d1 8695 fprintf (stream, " base size=%lu base align=%lu\n",
9439e9a1 8696 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
dbbf88d1
NS
8697 / BITS_PER_UNIT),
8698 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8699 / BITS_PER_UNIT));
8700 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
b7442fb5 8701 fprintf (stream, "\n");
bb885938
NS
8702}
8703
da1d7781 8704/* Debug interface to hierarchy dumping. */
bb885938 8705
ac1f3b7e 8706void
bb885938
NS
8707debug_class (tree t)
8708{
8709 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8710}
8711
8712static void
8713dump_class_hierarchy (tree t)
8714{
8715 int flags;
f8a36c78 8716 FILE *stream = get_dump_info (TDI_class, &flags);
bb885938
NS
8717
8718 if (stream)
8719 {
8720 dump_class_hierarchy_1 (stream, flags, t);
bb885938 8721 }
b7442fb5
NS
8722}
8723
8724static void
94edc4ab 8725dump_array (FILE * stream, tree decl)
b7442fb5 8726{
4038c495
GB
8727 tree value;
8728 unsigned HOST_WIDE_INT ix;
b7442fb5
NS
8729 HOST_WIDE_INT elt;
8730 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8731
9439e9a1 8732 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
b7442fb5
NS
8733 / BITS_PER_UNIT);
8734 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8735 fprintf (stream, " %s entries",
8736 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8737 TFF_PLAIN_IDENTIFIER));
8738 fprintf (stream, "\n");
8739
4038c495
GB
8740 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8741 ix, value)
4fdc14ca 8742 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
4038c495 8743 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
b7442fb5
NS
8744}
8745
8746static void
94edc4ab 8747dump_vtable (tree t, tree binfo, tree vtable)
b7442fb5
NS
8748{
8749 int flags;
f8a36c78 8750 FILE *stream = get_dump_info (TDI_class, &flags);
b7442fb5
NS
8751
8752 if (!stream)
8753 return;
8754
8755 if (!(flags & TDF_SLIM))
9965d119 8756 {
b7442fb5 8757 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
c8094d83 8758
b7442fb5
NS
8759 fprintf (stream, "%s for %s",
8760 ctor_vtbl_p ? "Construction vtable" : "Vtable",
fc6633e0 8761 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
b7442fb5
NS
8762 if (ctor_vtbl_p)
8763 {
809e3e7f 8764 if (!BINFO_VIRTUAL_P (binfo))
6c5bf58a
KT
8765 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8766 (HOST_WIDE_INT) (uintptr_t) binfo);
b7442fb5
NS
8767 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8768 }
8769 fprintf (stream, "\n");
8770 dump_array (stream, vtable);
8771 fprintf (stream, "\n");
9965d119 8772 }
b7442fb5
NS
8773}
8774
8775static void
94edc4ab 8776dump_vtt (tree t, tree vtt)
b7442fb5
NS
8777{
8778 int flags;
f8a36c78 8779 FILE *stream = get_dump_info (TDI_class, &flags);
b7442fb5
NS
8780
8781 if (!stream)
8782 return;
8783
8784 if (!(flags & TDF_SLIM))
8785 {
8786 fprintf (stream, "VTT for %s\n",
8787 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8788 dump_array (stream, vtt);
8789 fprintf (stream, "\n");
8790 }
ca36f057
MM
8791}
8792
bb885938
NS
8793/* Dump a function or thunk and its thunkees. */
8794
8795static void
8796dump_thunk (FILE *stream, int indent, tree thunk)
8797{
8798 static const char spaces[] = " ";
8799 tree name = DECL_NAME (thunk);
8800 tree thunks;
c8094d83 8801
bb885938
NS
8802 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8803 (void *)thunk,
8804 !DECL_THUNK_P (thunk) ? "function"
8805 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8806 name ? IDENTIFIER_POINTER (name) : "<unset>");
e00853fd 8807 if (DECL_THUNK_P (thunk))
bb885938
NS
8808 {
8809 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8810 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8811
8812 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8813 if (!virtual_adjust)
8814 /*NOP*/;
8815 else if (DECL_THIS_THUNK_P (thunk))
8816 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9439e9a1 8817 tree_to_shwi (virtual_adjust));
bb885938
NS
8818 else
8819 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9439e9a1 8820 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
bb885938 8821 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
e00853fd
NS
8822 if (THUNK_ALIAS (thunk))
8823 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
bb885938
NS
8824 }
8825 fprintf (stream, "\n");
8826 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8827 dump_thunk (stream, indent + 2, thunks);
8828}
8829
8830/* Dump the thunks for FN. */
8831
ac1f3b7e 8832void
bb885938
NS
8833debug_thunks (tree fn)
8834{
8835 dump_thunk (stderr, 0, fn);
8836}
8837
ca36f057
MM
8838/* Virtual function table initialization. */
8839
8840/* Create all the necessary vtables for T and its base classes. */
8841
8842static void
94edc4ab 8843finish_vtbls (tree t)
ca36f057 8844{
3461fba7 8845 tree vbase;
9771b263 8846 vec<constructor_elt, va_gc> *v = NULL;
9d6a019c 8847 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
ca36f057 8848
3461fba7
NS
8849 /* We lay out the primary and secondary vtables in one contiguous
8850 vtable. The primary vtable is first, followed by the non-virtual
8851 secondary vtables in inheritance graph order. */
9d6a019c
NF
8852 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8853 vtable, t, &v);
c8094d83 8854
3461fba7
NS
8855 /* Then come the virtual bases, also in inheritance graph order. */
8856 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8857 {
809e3e7f 8858 if (!BINFO_VIRTUAL_P (vbase))
3461fba7 8859 continue;
9d6a019c 8860 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
ff668506
JM
8861 }
8862
604a3205 8863 if (BINFO_VTABLE (TYPE_BINFO (t)))
9d6a019c 8864 initialize_vtable (TYPE_BINFO (t), v);
ca36f057
MM
8865}
8866
8867/* Initialize the vtable for BINFO with the INITS. */
8868
8869static void
9771b263 8870initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
ca36f057 8871{
ca36f057
MM
8872 tree decl;
8873
9771b263 8874 layout_vtable_decl (binfo, vec_safe_length (inits));
c35cce41 8875 decl = get_vtbl_decl_for_binfo (binfo);
19c29b2f 8876 initialize_artificial_var (decl, inits);
b7442fb5 8877 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
23656158
MM
8878}
8879
9965d119
NS
8880/* Build the VTT (virtual table table) for T.
8881 A class requires a VTT if it has virtual bases.
c8094d83 8882
9965d119
NS
8883 This holds
8884 1 - primary virtual pointer for complete object T
90ecce3e
JM
8885 2 - secondary VTTs for each direct non-virtual base of T which requires a
8886 VTT
9965d119
NS
8887 3 - secondary virtual pointers for each direct or indirect base of T which
8888 has virtual bases or is reachable via a virtual path from T.
8889 4 - secondary VTTs for each direct or indirect virtual base of T.
c8094d83 8890
9965d119 8891 Secondary VTTs look like complete object VTTs without part 4. */
23656158
MM
8892
8893static void
94edc4ab 8894build_vtt (tree t)
23656158 8895{
23656158
MM
8896 tree type;
8897 tree vtt;
3ec6bad3 8898 tree index;
9771b263 8899 vec<constructor_elt, va_gc> *inits;
23656158 8900
23656158 8901 /* Build up the initializers for the VTT. */
9d6a019c 8902 inits = NULL;
3ec6bad3 8903 index = size_zero_node;
9965d119 8904 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
23656158
MM
8905
8906 /* If we didn't need a VTT, we're done. */
8907 if (!inits)
8908 return;
8909
8910 /* Figure out the type of the VTT. */
dcedcddb 8911 type = build_array_of_n_type (const_ptr_type_node,
9771b263 8912 inits->length ());
c8094d83 8913
23656158 8914 /* Now, build the VTT object itself. */
3e355d92 8915 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
19c29b2f 8916 initialize_artificial_var (vtt, inits);
548502d3 8917 /* Add the VTT to the vtables list. */
910ad8de
NF
8918 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8919 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
b7442fb5
NS
8920
8921 dump_vtt (t, vtt);
23656158
MM
8922}
8923
13de7ec4
JM
8924/* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8925 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8926 and CHAIN the vtable pointer for this binfo after construction is
00a17e31 8927 complete. VALUE can also be another BINFO, in which case we recurse. */
13de7ec4
JM
8928
8929static tree
94edc4ab 8930binfo_ctor_vtable (tree binfo)
13de7ec4
JM
8931{
8932 tree vt;
8933
8934 while (1)
8935 {
8936 vt = BINFO_VTABLE (binfo);
8937 if (TREE_CODE (vt) == TREE_LIST)
8938 vt = TREE_VALUE (vt);
95b4aca6 8939 if (TREE_CODE (vt) == TREE_BINFO)
13de7ec4
JM
8940 binfo = vt;
8941 else
8942 break;
8943 }
8944
8945 return vt;
8946}
8947
a3a0fc7f 8948/* Data for secondary VTT initialization. */
a79683d5 8949struct secondary_vptr_vtt_init_data
a3a0fc7f
NS
8950{
8951 /* Is this the primary VTT? */
8952 bool top_level_p;
8953
8954 /* Current index into the VTT. */
8955 tree index;
8956
9d6a019c 8957 /* Vector of initializers built up. */
9771b263 8958 vec<constructor_elt, va_gc> *inits;
a3a0fc7f
NS
8959
8960 /* The type being constructed by this secondary VTT. */
8961 tree type_being_constructed;
a79683d5 8962};
a3a0fc7f 8963
23656158 8964/* Recursively build the VTT-initializer for BINFO (which is in the
9965d119
NS
8965 hierarchy dominated by T). INITS points to the end of the initializer
8966 list to date. INDEX is the VTT index where the next element will be
8967 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8968 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8969 for virtual bases of T. When it is not so, we build the constructor
8970 vtables for the BINFO-in-T variant. */
23656158 8971
9d6a019c 8972static void
9771b263
DN
8973build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8974 tree *index)
23656158
MM
8975{
8976 int i;
8977 tree b;
8978 tree init;
a3a0fc7f 8979 secondary_vptr_vtt_init_data data;
539ed333 8980 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
23656158
MM
8981
8982 /* We only need VTTs for subobjects with virtual bases. */
5775a06a 8983 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9d6a019c 8984 return;
23656158
MM
8985
8986 /* We need to use a construction vtable if this is not the primary
8987 VTT. */
9965d119 8988 if (!top_level_p)
3ec6bad3
MM
8989 {
8990 build_ctor_vtbl_group (binfo, t);
8991
8992 /* Record the offset in the VTT where this sub-VTT can be found. */
8993 BINFO_SUBVTT_INDEX (binfo) = *index;
8994 }
23656158
MM
8995
8996 /* Add the address of the primary vtable for the complete object. */
13de7ec4 8997 init = binfo_ctor_vtable (binfo);
9d6a019c 8998 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9965d119
NS
8999 if (top_level_p)
9000 {
50bc768d 9001 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9965d119
NS
9002 BINFO_VPTR_INDEX (binfo) = *index;
9003 }
3ec6bad3 9004 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
c8094d83 9005
23656158 9006 /* Recursively add the secondary VTTs for non-virtual bases. */
fa743e8c
NS
9007 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9008 if (!BINFO_VIRTUAL_P (b))
9d6a019c 9009 build_vtt_inits (b, t, inits, index);
c8094d83 9010
23656158 9011 /* Add secondary virtual pointers for all subobjects of BINFO with
9965d119
NS
9012 either virtual bases or reachable along a virtual path, except
9013 subobjects that are non-virtual primary bases. */
a3a0fc7f
NS
9014 data.top_level_p = top_level_p;
9015 data.index = *index;
9d6a019c 9016 data.inits = *inits;
a3a0fc7f 9017 data.type_being_constructed = BINFO_TYPE (binfo);
c8094d83 9018
5d5a519f 9019 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9965d119 9020
a3a0fc7f 9021 *index = data.index;
23656158 9022
9d6a019c
NF
9023 /* data.inits might have grown as we added secondary virtual pointers.
9024 Make sure our caller knows about the new vector. */
9025 *inits = data.inits;
23656158 9026
9965d119 9027 if (top_level_p)
a3a0fc7f
NS
9028 /* Add the secondary VTTs for virtual bases in inheritance graph
9029 order. */
9ccf6541
MM
9030 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9031 {
809e3e7f 9032 if (!BINFO_VIRTUAL_P (b))
9ccf6541 9033 continue;
c8094d83 9034
9d6a019c 9035 build_vtt_inits (b, t, inits, index);
9ccf6541 9036 }
a3a0fc7f
NS
9037 else
9038 /* Remove the ctor vtables we created. */
5d5a519f 9039 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
23656158
MM
9040}
9041
8df83eae 9042/* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
a3a0fc7f 9043 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
23656158
MM
9044
9045static tree
a3a0fc7f 9046dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
23656158 9047{
a3a0fc7f 9048 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
23656158 9049
23656158
MM
9050 /* We don't care about bases that don't have vtables. */
9051 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
5d5a519f 9052 return dfs_skip_bases;
23656158 9053
a3a0fc7f
NS
9054 /* We're only interested in proper subobjects of the type being
9055 constructed. */
539ed333 9056 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
23656158
MM
9057 return NULL_TREE;
9058
a3a0fc7f
NS
9059 /* We're only interested in bases with virtual bases or reachable
9060 via a virtual path from the type being constructed. */
5d5a519f
NS
9061 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9062 || binfo_via_virtual (binfo, data->type_being_constructed)))
9063 return dfs_skip_bases;
c8094d83 9064
5d5a519f
NS
9065 /* We're not interested in non-virtual primary bases. */
9066 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
db3d8cde 9067 return NULL_TREE;
c8094d83 9068
3ec6bad3 9069 /* Record the index where this secondary vptr can be found. */
a3a0fc7f 9070 if (data->top_level_p)
9965d119 9071 {
50bc768d 9072 gcc_assert (!BINFO_VPTR_INDEX (binfo));
a3a0fc7f 9073 BINFO_VPTR_INDEX (binfo) = data->index;
3ec6bad3 9074
a3a0fc7f
NS
9075 if (BINFO_VIRTUAL_P (binfo))
9076 {
0cbd7506
MS
9077 /* It's a primary virtual base, and this is not a
9078 construction vtable. Find the base this is primary of in
9079 the inheritance graph, and use that base's vtable
9080 now. */
a3a0fc7f
NS
9081 while (BINFO_PRIMARY_P (binfo))
9082 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9083 }
9965d119 9084 }
c8094d83 9085
a3a0fc7f 9086 /* Add the initializer for the secondary vptr itself. */
9d6a019c 9087 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
23656158 9088
a3a0fc7f
NS
9089 /* Advance the vtt index. */
9090 data->index = size_binop (PLUS_EXPR, data->index,
9091 TYPE_SIZE_UNIT (ptr_type_node));
9965d119 9092
a3a0fc7f 9093 return NULL_TREE;
9965d119
NS
9094}
9095
a3a0fc7f
NS
9096/* Called from build_vtt_inits via dfs_walk. After building
9097 constructor vtables and generating the sub-vtt from them, we need
9098 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9099 binfo of the base whose sub vtt was generated. */
23656158
MM
9100
9101static tree
94edc4ab 9102dfs_fixup_binfo_vtbls (tree binfo, void* data)
23656158 9103{
a3a0fc7f 9104 tree vtable = BINFO_VTABLE (binfo);
23656158 9105
5d5a519f
NS
9106 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9107 /* If this class has no vtable, none of its bases do. */
9108 return dfs_skip_bases;
c8094d83 9109
5d5a519f
NS
9110 if (!vtable)
9111 /* This might be a primary base, so have no vtable in this
9112 hierarchy. */
9113 return NULL_TREE;
c8094d83 9114
23656158
MM
9115 /* If we scribbled the construction vtable vptr into BINFO, clear it
9116 out now. */
5d5a519f 9117 if (TREE_CODE (vtable) == TREE_LIST
a3a0fc7f
NS
9118 && (TREE_PURPOSE (vtable) == (tree) data))
9119 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
23656158
MM
9120
9121 return NULL_TREE;
9122}
9123
9124/* Build the construction vtable group for BINFO which is in the
9125 hierarchy dominated by T. */
9126
9127static void
94edc4ab 9128build_ctor_vtbl_group (tree binfo, tree t)
23656158 9129{
23656158
MM
9130 tree type;
9131 tree vtbl;
23656158 9132 tree id;
9ccf6541 9133 tree vbase;
9771b263 9134 vec<constructor_elt, va_gc> *v;
23656158 9135
7bdcf888 9136 /* See if we've already created this construction vtable group. */
1f84ec23 9137 id = mangle_ctor_vtbl_for_type (t, binfo);
23656158
MM
9138 if (IDENTIFIER_GLOBAL_VALUE (id))
9139 return;
9140
539ed333 9141 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
23656158
MM
9142 /* Build a version of VTBL (with the wrong type) for use in
9143 constructing the addresses of secondary vtables in the
9144 construction vtable group. */
459c43ad 9145 vtbl = build_vtable (t, id, ptr_type_node);
505970fc 9146 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
2ee8a2d5
JM
9147 /* Don't export construction vtables from shared libraries. Even on
9148 targets that don't support hidden visibility, this tells
9149 can_refer_decl_in_current_unit_p not to assume that it's safe to
9150 access from a different compilation unit (bz 54314). */
9151 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9152 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9d6a019c
NF
9153
9154 v = NULL;
23656158 9155 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9d6a019c 9156 binfo, vtbl, t, &v);
9965d119
NS
9157
9158 /* Add the vtables for each of our virtual bases using the vbase in T
9159 binfo. */
c8094d83
MS
9160 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9161 vbase;
9ccf6541
MM
9162 vbase = TREE_CHAIN (vbase))
9163 {
9164 tree b;
9165
809e3e7f 9166 if (!BINFO_VIRTUAL_P (vbase))
9ccf6541 9167 continue;
dbbf88d1 9168 b = copied_binfo (vbase, binfo);
c8094d83 9169
9d6a019c 9170 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9ccf6541 9171 }
23656158
MM
9172
9173 /* Figure out the type of the construction vtable. */
9771b263 9174 type = build_array_of_n_type (vtable_entry_type, v->length ());
8208d7dc 9175 layout_type (type);
23656158 9176 TREE_TYPE (vtbl) = type;
8208d7dc
DJ
9177 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9178 layout_decl (vtbl, 0);
23656158
MM
9179
9180 /* Initialize the construction vtable. */
548502d3 9181 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9d6a019c 9182 initialize_artificial_var (vtbl, v);
b7442fb5 9183 dump_vtable (t, binfo, vtbl);
23656158
MM
9184}
9185
9965d119
NS
9186/* Add the vtbl initializers for BINFO (and its bases other than
9187 non-virtual primaries) to the list of INITS. BINFO is in the
9188 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9189 the constructor the vtbl inits should be accumulated for. (If this
9190 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9191 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9192 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9193 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9194 but are not necessarily the same in terms of layout. */
ca36f057
MM
9195
9196static void
94edc4ab 9197accumulate_vtbl_inits (tree binfo,
0cbd7506
MS
9198 tree orig_binfo,
9199 tree rtti_binfo,
9d6a019c 9200 tree vtbl,
0cbd7506 9201 tree t,
9771b263 9202 vec<constructor_elt, va_gc> **inits)
ca36f057 9203{
23656158 9204 int i;
fa743e8c 9205 tree base_binfo;
539ed333 9206 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
23656158 9207
539ed333 9208 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
23656158 9209
00a17e31 9210 /* If it doesn't have a vptr, we don't do anything. */
623fe76a
NS
9211 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9212 return;
c8094d83 9213
23656158
MM
9214 /* If we're building a construction vtable, we're not interested in
9215 subobjects that don't require construction vtables. */
c8094d83 9216 if (ctor_vtbl_p
5775a06a 9217 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9965d119 9218 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
23656158
MM
9219 return;
9220
9221 /* Build the initializers for the BINFO-in-T vtable. */
9d6a019c 9222 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
c8094d83 9223
c35cce41
MM
9224 /* Walk the BINFO and its bases. We walk in preorder so that as we
9225 initialize each vtable we can figure out at what offset the
23656158
MM
9226 secondary vtable lies from the primary vtable. We can't use
9227 dfs_walk here because we need to iterate through bases of BINFO
9228 and RTTI_BINFO simultaneously. */
fa743e8c 9229 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
23656158 9230 {
23656158 9231 /* Skip virtual bases. */
809e3e7f 9232 if (BINFO_VIRTUAL_P (base_binfo))
23656158
MM
9233 continue;
9234 accumulate_vtbl_inits (base_binfo,
604a3205 9235 BINFO_BASE_BINFO (orig_binfo, i),
9d6a019c 9236 rtti_binfo, vtbl, t,
23656158
MM
9237 inits);
9238 }
ca36f057
MM
9239}
9240
9d6a019c
NF
9241/* Called from accumulate_vtbl_inits. Adds the initializers for the
9242 BINFO vtable to L. */
ca36f057 9243
9d6a019c 9244static void
94edc4ab 9245dfs_accumulate_vtbl_inits (tree binfo,
0cbd7506
MS
9246 tree orig_binfo,
9247 tree rtti_binfo,
9d6a019c 9248 tree orig_vtbl,
0cbd7506 9249 tree t,
9771b263 9250 vec<constructor_elt, va_gc> **l)
ca36f057 9251{
9965d119 9252 tree vtbl = NULL_TREE;
539ed333 9253 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9d6a019c 9254 int n_inits;
9965d119 9255
13de7ec4 9256 if (ctor_vtbl_p
809e3e7f 9257 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9965d119 9258 {
13de7ec4
JM
9259 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9260 primary virtual base. If it is not the same primary in
9261 the hierarchy of T, we'll need to generate a ctor vtable
9262 for it, to place at its location in T. If it is the same
9263 primary, we still need a VTT entry for the vtable, but it
9264 should point to the ctor vtable for the base it is a
9265 primary for within the sub-hierarchy of RTTI_BINFO.
c8094d83 9266
13de7ec4 9267 There are three possible cases:
c8094d83 9268
13de7ec4
JM
9269 1) We are in the same place.
9270 2) We are a primary base within a lost primary virtual base of
9271 RTTI_BINFO.
049d2def 9272 3) We are primary to something not a base of RTTI_BINFO. */
c8094d83 9273
fc6633e0 9274 tree b;
13de7ec4 9275 tree last = NULL_TREE;
85a9a0a2 9276
13de7ec4
JM
9277 /* First, look through the bases we are primary to for RTTI_BINFO
9278 or a virtual base. */
fc6633e0
NS
9279 b = binfo;
9280 while (BINFO_PRIMARY_P (b))
7bdcf888 9281 {
fc6633e0 9282 b = BINFO_INHERITANCE_CHAIN (b);
13de7ec4 9283 last = b;
809e3e7f 9284 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
fc6633e0 9285 goto found;
7bdcf888 9286 }
13de7ec4
JM
9287 /* If we run out of primary links, keep looking down our
9288 inheritance chain; we might be an indirect primary. */
fc6633e0
NS
9289 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9290 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9291 break;
9292 found:
c8094d83 9293
13de7ec4
JM
9294 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9295 base B and it is a base of RTTI_BINFO, this is case 2. In
9296 either case, we share our vtable with LAST, i.e. the
9297 derived-most base within B of which we are a primary. */
9298 if (b == rtti_binfo
58c42dc2 9299 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
049d2def
JM
9300 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9301 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9302 binfo_ctor_vtable after everything's been set up. */
9303 vtbl = last;
13de7ec4 9304
049d2def 9305 /* Otherwise, this is case 3 and we get our own. */
9965d119 9306 }
dbbf88d1 9307 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9d6a019c
NF
9308 return;
9309
9771b263 9310 n_inits = vec_safe_length (*l);
7bdcf888 9311
9965d119 9312 if (!vtbl)
ca36f057 9313 {
c35cce41
MM
9314 tree index;
9315 int non_fn_entries;
9316
9d6a019c
NF
9317 /* Add the initializer for this vtable. */
9318 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9319 &non_fn_entries, l);
c35cce41 9320
23656158 9321 /* Figure out the position to which the VPTR should point. */
9d6a019c 9322 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
23656158
MM
9323 index = size_binop (MULT_EXPR,
9324 TYPE_SIZE_UNIT (vtable_entry_type),
5d49b6a7
RG
9325 size_int (non_fn_entries + n_inits));
9326 vtbl = fold_build_pointer_plus (vtbl, index);
9965d119 9327 }
23656158 9328
7bdcf888 9329 if (ctor_vtbl_p)
9965d119
NS
9330 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9331 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9332 straighten this out. */
9333 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
809e3e7f 9334 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9d6a019c 9335 /* Throw away any unneeded intializers. */
9771b263 9336 (*l)->truncate (n_inits);
7bdcf888
NS
9337 else
9338 /* For an ordinary vtable, set BINFO_VTABLE. */
9339 BINFO_VTABLE (binfo) = vtbl;
ca36f057
MM
9340}
9341
1b746b0f
AP
9342static GTY(()) tree abort_fndecl_addr;
9343
90ecce3e 9344/* Construct the initializer for BINFO's virtual function table. BINFO
aabb4cd6 9345 is part of the hierarchy dominated by T. If we're building a
23656158 9346 construction vtable, the ORIG_BINFO is the binfo we should use to
9965d119
NS
9347 find the actual function pointers to put in the vtable - but they
9348 can be overridden on the path to most-derived in the graph that
9349 ORIG_BINFO belongs. Otherwise,
911a71a7 9350 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
23656158
MM
9351 BINFO that should be indicated by the RTTI information in the
9352 vtable; it will be a base class of T, rather than T itself, if we
9353 are building a construction vtable.
aabb4cd6
MM
9354
9355 The value returned is a TREE_LIST suitable for wrapping in a
9356 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9357 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
c8094d83 9358 number of non-function entries in the vtable.
911a71a7
MM
9359
9360 It might seem that this function should never be called with a
9965d119 9361 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
911a71a7 9362 base is always subsumed by a derived class vtable. However, when
9965d119 9363 we are building construction vtables, we do build vtables for
911a71a7
MM
9364 primary bases; we need these while the primary base is being
9365 constructed. */
ca36f057 9366
9d6a019c 9367static void
94edc4ab 9368build_vtbl_initializer (tree binfo,
0cbd7506
MS
9369 tree orig_binfo,
9370 tree t,
9371 tree rtti_binfo,
9d6a019c 9372 int* non_fn_entries_p,
9771b263 9373 vec<constructor_elt, va_gc> **inits)
ca36f057 9374{
02dea3ff 9375 tree v;
911a71a7 9376 vtbl_init_data vid;
9d6a019c 9377 unsigned ix, jx;
58c42dc2 9378 tree vbinfo;
9771b263 9379 vec<tree, va_gc> *vbases;
9d6a019c 9380 constructor_elt *e;
c8094d83 9381
911a71a7 9382 /* Initialize VID. */
961192e1 9383 memset (&vid, 0, sizeof (vid));
911a71a7
MM
9384 vid.binfo = binfo;
9385 vid.derived = t;
73ea87d7 9386 vid.rtti_binfo = rtti_binfo;
539ed333
NS
9387 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9388 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
548502d3 9389 vid.generate_vcall_entries = true;
c35cce41 9390 /* The first vbase or vcall offset is at index -3 in the vtable. */
ce552f75 9391 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
c35cce41 9392
9bab6c90 9393 /* Add entries to the vtable for RTTI. */
73ea87d7 9394 build_rtti_vtbl_entries (binfo, &vid);
9bab6c90 9395
b485e15b
MM
9396 /* Create an array for keeping track of the functions we've
9397 processed. When we see multiple functions with the same
9398 signature, we share the vcall offsets. */
9771b263 9399 vec_alloc (vid.fns, 32);
c35cce41 9400 /* Add the vcall and vbase offset entries. */
911a71a7 9401 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
c8094d83 9402
79cda2d1 9403 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
c35cce41 9404 build_vbase_offset_vtbl_entries. */
9ba5ff0f 9405 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9771b263 9406 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
58c42dc2 9407 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
ca36f057 9408
a6f5e048
RH
9409 /* If the target requires padding between data entries, add that now. */
9410 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9411 {
9771b263 9412 int n_entries = vec_safe_length (vid.inits);
9d6a019c 9413
9771b263 9414 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
a6f5e048 9415
9d6a019c
NF
9416 /* Move data entries into their new positions and add padding
9417 after the new positions. Iterate backwards so we don't
9418 overwrite entries that we would need to process later. */
9419 for (ix = n_entries - 1;
9771b263 9420 vid.inits->iterate (ix, &e);
9d6a019c 9421 ix--)
a6f5e048 9422 {
9d6a019c 9423 int j;
25d8a217
NF
9424 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9425 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9d6a019c 9426
9771b263 9427 (*vid.inits)[new_position] = *e;
a6f5e048 9428
9d6a019c
NF
9429 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9430 {
9771b263 9431 constructor_elt *f = &(*vid.inits)[new_position - j];
9d6a019c
NF
9432 f->index = NULL_TREE;
9433 f->value = build1 (NOP_EXPR, vtable_entry_type,
9434 null_pointer_node);
9435 }
a6f5e048
RH
9436 }
9437 }
9438
c35cce41 9439 if (non_fn_entries_p)
9771b263 9440 *non_fn_entries_p = vec_safe_length (vid.inits);
9d6a019c
NF
9441
9442 /* The initializers for virtual functions were built up in reverse
9443 order. Straighten them out and add them to the running list in one
9444 step. */
9771b263
DN
9445 jx = vec_safe_length (*inits);
9446 vec_safe_grow (*inits, jx + vid.inits->length ());
9d6a019c 9447
9771b263
DN
9448 for (ix = vid.inits->length () - 1;
9449 vid.inits->iterate (ix, &e);
9d6a019c 9450 ix--, jx++)
9771b263 9451 (**inits)[jx] = *e;
ca36f057
MM
9452
9453 /* Go through all the ordinary virtual functions, building up
9454 initializers. */
23656158 9455 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
ca36f057
MM
9456 {
9457 tree delta;
9458 tree vcall_index;
4977bab6 9459 tree fn, fn_original;
f11ee281 9460 tree init = NULL_TREE;
c8094d83 9461
ca36f057 9462 fn = BV_FN (v);
07fa4878
NS
9463 fn_original = fn;
9464 if (DECL_THUNK_P (fn))
4977bab6 9465 {
07fa4878
NS
9466 if (!DECL_NAME (fn))
9467 finish_thunk (fn);
e00853fd 9468 if (THUNK_ALIAS (fn))
bb885938
NS
9469 {
9470 fn = THUNK_ALIAS (fn);
9471 BV_FN (v) = fn;
9472 }
07fa4878 9473 fn_original = THUNK_TARGET (fn);
4977bab6 9474 }
c8094d83 9475
d0cd8b44
JM
9476 /* If the only definition of this function signature along our
9477 primary base chain is from a lost primary, this vtable slot will
9478 never be used, so just zero it out. This is important to avoid
9479 requiring extra thunks which cannot be generated with the function.
9480
f11ee281
JM
9481 We first check this in update_vtable_entry_for_fn, so we handle
9482 restored primary bases properly; we also need to do it here so we
39a13be5 9483 zero out unused slots in ctor vtables, rather than filling them
f11ee281
JM
9484 with erroneous values (though harmless, apart from relocation
9485 costs). */
02dea3ff
JM
9486 if (BV_LOST_PRIMARY (v))
9487 init = size_zero_node;
d0cd8b44 9488
f11ee281
JM
9489 if (! init)
9490 {
9491 /* Pull the offset for `this', and the function to call, out of
9492 the list. */
9493 delta = BV_DELTA (v);
548502d3 9494 vcall_index = BV_VCALL_INDEX (v);
f11ee281 9495
50bc768d
NS
9496 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9497 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
f11ee281
JM
9498
9499 /* You can't call an abstract virtual function; it's abstract.
9500 So, we replace these functions with __pure_virtual. */
4977bab6 9501 if (DECL_PURE_VIRTUAL_P (fn_original))
4977bab6 9502 {
1b746b0f 9503 fn = abort_fndecl;
21b6aca3
JJ
9504 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9505 {
9506 if (abort_fndecl_addr == NULL)
9507 abort_fndecl_addr
9508 = fold_convert (vfunc_ptr_type_node,
9509 build_fold_addr_expr (fn));
9510 init = abort_fndecl_addr;
9511 }
1b746b0f 9512 }
4ce7d589
JM
9513 /* Likewise for deleted virtuals. */
9514 else if (DECL_DELETED_FN (fn_original))
9515 {
9516 fn = get_identifier ("__cxa_deleted_virtual");
9517 if (!get_global_value_if_present (fn, &fn))
9518 fn = push_library_fn (fn, (build_function_type_list
9519 (void_type_node, NULL_TREE)),
8595a07d 9520 NULL_TREE, ECF_NORETURN);
4ce7d589
JM
9521 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9522 init = fold_convert (vfunc_ptr_type_node,
9523 build_fold_addr_expr (fn));
9524 }
1b746b0f
AP
9525 else
9526 {
9527 if (!integer_zerop (delta) || vcall_index)
9528 {
9529 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9530 if (!DECL_NAME (fn))
9531 finish_thunk (fn);
9532 }
9533 /* Take the address of the function, considering it to be of an
9534 appropriate generic type. */
21b6aca3
JJ
9535 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9536 init = fold_convert (vfunc_ptr_type_node,
9537 build_fold_addr_expr (fn));
d74db8ff
JM
9538 /* Don't refer to a virtual destructor from a constructor
9539 vtable or a vtable for an abstract class, since destroying
9540 an object under construction is undefined behavior and we
9541 don't want it to be considered a candidate for speculative
9542 devirtualization. But do create the thunk for ABI
9543 compliance. */
9544 if (DECL_DESTRUCTOR_P (fn_original)
9545 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9546 || orig_binfo != binfo))
9547 init = size_zero_node;
4977bab6 9548 }
f11ee281 9549 }
d0cd8b44 9550
ca36f057 9551 /* And add it to the chain of initializers. */
67231816
RH
9552 if (TARGET_VTABLE_USES_DESCRIPTORS)
9553 {
9554 int i;
9555 if (init == size_zero_node)
9556 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9d6a019c 9557 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
67231816
RH
9558 else
9559 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9560 {
f293ce4b 9561 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
21b6aca3 9562 fn, build_int_cst (NULL_TREE, i));
67231816
RH
9563 TREE_CONSTANT (fdesc) = 1;
9564
9d6a019c 9565 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
67231816
RH
9566 }
9567 }
9568 else
9d6a019c 9569 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
ca36f057 9570 }
ca36f057
MM
9571}
9572
d0cd8b44 9573/* Adds to vid->inits the initializers for the vbase and vcall
c35cce41 9574 offsets in BINFO, which is in the hierarchy dominated by T. */
ca36f057 9575
c35cce41 9576static void
94edc4ab 9577build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
ca36f057 9578{
c35cce41 9579 tree b;
8d08fdba 9580
c35cce41 9581 /* If this is a derived class, we must first create entries
9bab6c90 9582 corresponding to the primary base class. */
911a71a7 9583 b = get_primary_binfo (binfo);
c35cce41 9584 if (b)
911a71a7 9585 build_vcall_and_vbase_vtbl_entries (b, vid);
c35cce41
MM
9586
9587 /* Add the vbase entries for this base. */
911a71a7 9588 build_vbase_offset_vtbl_entries (binfo, vid);
c35cce41 9589 /* Add the vcall entries for this base. */
911a71a7 9590 build_vcall_offset_vtbl_entries (binfo, vid);
ca36f057 9591}
8d08fdba 9592
ca36f057
MM
9593/* Returns the initializers for the vbase offset entries in the vtable
9594 for BINFO (which is part of the class hierarchy dominated by T), in
c35cce41
MM
9595 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9596 where the next vbase offset will go. */
8d08fdba 9597
c35cce41 9598static void
94edc4ab 9599build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
ca36f057 9600{
c35cce41
MM
9601 tree vbase;
9602 tree t;
90b1ca2f 9603 tree non_primary_binfo;
8d08fdba 9604
ca36f057
MM
9605 /* If there are no virtual baseclasses, then there is nothing to
9606 do. */
5775a06a 9607 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
c35cce41 9608 return;
ca36f057 9609
911a71a7 9610 t = vid->derived;
c8094d83 9611
90b1ca2f
NS
9612 /* We might be a primary base class. Go up the inheritance hierarchy
9613 until we find the most derived class of which we are a primary base:
9614 it is the offset of that which we need to use. */
9615 non_primary_binfo = binfo;
9616 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9617 {
9618 tree b;
9619
9620 /* If we have reached a virtual base, then it must be a primary
9621 base (possibly multi-level) of vid->binfo, or we wouldn't
9622 have called build_vcall_and_vbase_vtbl_entries for it. But it
9623 might be a lost primary, so just skip down to vid->binfo. */
809e3e7f 9624 if (BINFO_VIRTUAL_P (non_primary_binfo))
90b1ca2f
NS
9625 {
9626 non_primary_binfo = vid->binfo;
9627 break;
9628 }
9629
9630 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9631 if (get_primary_binfo (b) != non_primary_binfo)
9632 break;
9633 non_primary_binfo = b;
9634 }
ca36f057 9635
c35cce41
MM
9636 /* Go through the virtual bases, adding the offsets. */
9637 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9638 vbase;
9639 vbase = TREE_CHAIN (vbase))
9640 {
9641 tree b;
9642 tree delta;
c8094d83 9643
809e3e7f 9644 if (!BINFO_VIRTUAL_P (vbase))
c35cce41 9645 continue;
ca36f057 9646
c35cce41
MM
9647 /* Find the instance of this virtual base in the complete
9648 object. */
dbbf88d1 9649 b = copied_binfo (vbase, binfo);
c35cce41
MM
9650
9651 /* If we've already got an offset for this virtual base, we
9652 don't need another one. */
9653 if (BINFO_VTABLE_PATH_MARKED (b))
9654 continue;
dbbf88d1 9655 BINFO_VTABLE_PATH_MARKED (b) = 1;
c35cce41
MM
9656
9657 /* Figure out where we can find this vbase offset. */
c8094d83 9658 delta = size_binop (MULT_EXPR,
911a71a7 9659 vid->index,
cda0a029 9660 fold_convert (ssizetype,
c35cce41 9661 TYPE_SIZE_UNIT (vtable_entry_type)));
911a71a7 9662 if (vid->primary_vtbl_p)
c35cce41
MM
9663 BINFO_VPTR_FIELD (b) = delta;
9664
9665 if (binfo != TYPE_BINFO (t))
50bc768d
NS
9666 /* The vbase offset had better be the same. */
9667 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
c35cce41
MM
9668
9669 /* The next vbase will come at a more negative offset. */
a6f5e048
RH
9670 vid->index = size_binop (MINUS_EXPR, vid->index,
9671 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
c35cce41
MM
9672
9673 /* The initializer is the delta from BINFO to this virtual base.
4e7512c9
MM
9674 The vbase offsets go in reverse inheritance-graph order, and
9675 we are walking in inheritance graph order so these end up in
9676 the right order. */
db3927fb
AH
9677 delta = size_diffop_loc (input_location,
9678 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
c8094d83 9679
9d6a019c
NF
9680 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9681 fold_build1_loc (input_location, NOP_EXPR,
9682 vtable_entry_type, delta));
c35cce41 9683 }
8d08fdba 9684}
ca36f057 9685
b485e15b 9686/* Adds the initializers for the vcall offset entries in the vtable
d0cd8b44
JM
9687 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9688 to VID->INITS. */
b485e15b
MM
9689
9690static void
94edc4ab 9691build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
b485e15b 9692{
548502d3
MM
9693 /* We only need these entries if this base is a virtual base. We
9694 compute the indices -- but do not add to the vtable -- when
9695 building the main vtable for a class. */
b9302915
MM
9696 if (binfo == TYPE_BINFO (vid->derived)
9697 || (BINFO_VIRTUAL_P (binfo)
9698 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9699 correspond to VID->DERIVED), we are building a primary
9700 construction virtual table. Since this is a primary
9701 virtual table, we do not need the vcall offsets for
9702 BINFO. */
9703 && binfo != vid->rtti_binfo))
548502d3
MM
9704 {
9705 /* We need a vcall offset for each of the virtual functions in this
9706 vtable. For example:
b485e15b 9707
548502d3
MM
9708 class A { virtual void f (); };
9709 class B1 : virtual public A { virtual void f (); };
9710 class B2 : virtual public A { virtual void f (); };
9711 class C: public B1, public B2 { virtual void f (); };
d0cd8b44 9712
548502d3
MM
9713 A C object has a primary base of B1, which has a primary base of A. A
9714 C also has a secondary base of B2, which no longer has a primary base
9715 of A. So the B2-in-C construction vtable needs a secondary vtable for
9716 A, which will adjust the A* to a B2* to call f. We have no way of
9717 knowing what (or even whether) this offset will be when we define B2,
9718 so we store this "vcall offset" in the A sub-vtable and look it up in
9719 a "virtual thunk" for B2::f.
b485e15b 9720
548502d3
MM
9721 We need entries for all the functions in our primary vtable and
9722 in our non-virtual bases' secondary vtables. */
9723 vid->vbase = binfo;
9724 /* If we are just computing the vcall indices -- but do not need
9725 the actual entries -- not that. */
809e3e7f 9726 if (!BINFO_VIRTUAL_P (binfo))
548502d3
MM
9727 vid->generate_vcall_entries = false;
9728 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9729 add_vcall_offset_vtbl_entries_r (binfo, vid);
9730 }
b485e15b
MM
9731}
9732
9733/* Build vcall offsets, starting with those for BINFO. */
9734
9735static void
94edc4ab 9736add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
b485e15b
MM
9737{
9738 int i;
9739 tree primary_binfo;
fa743e8c 9740 tree base_binfo;
b485e15b
MM
9741
9742 /* Don't walk into virtual bases -- except, of course, for the
d0cd8b44
JM
9743 virtual base for which we are building vcall offsets. Any
9744 primary virtual base will have already had its offsets generated
9745 through the recursion in build_vcall_and_vbase_vtbl_entries. */
809e3e7f 9746 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
b485e15b 9747 return;
c8094d83 9748
b485e15b
MM
9749 /* If BINFO has a primary base, process it first. */
9750 primary_binfo = get_primary_binfo (binfo);
9751 if (primary_binfo)
9752 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9753
9754 /* Add BINFO itself to the list. */
9755 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9756
9757 /* Scan the non-primary bases of BINFO. */
fa743e8c
NS
9758 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9759 if (base_binfo != primary_binfo)
9760 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
b485e15b
MM
9761}
9762
9965d119 9763/* Called from build_vcall_offset_vtbl_entries_r. */
e92cc029 9764
b485e15b 9765static void
94edc4ab 9766add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
8d08fdba 9767{
e6a66567 9768 /* Make entries for the rest of the virtuals. */
90d84934
JM
9769 tree orig_fn;
9770
9771 /* The ABI requires that the methods be processed in declaration
9772 order. */
9773 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9774 orig_fn;
9775 orig_fn = DECL_CHAIN (orig_fn))
aaf8a23e 9776 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
90d84934 9777 add_vcall_offset (orig_fn, binfo, vid);
e6a66567 9778}
b485e15b 9779
95675950 9780/* Add a vcall offset entry for ORIG_FN to the vtable. */
b485e15b 9781
e6a66567 9782static void
95675950 9783add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
e6a66567
MM
9784{
9785 size_t i;
9786 tree vcall_offset;
1e625046 9787 tree derived_entry;
9bab6c90 9788
e6a66567
MM
9789 /* If there is already an entry for a function with the same
9790 signature as FN, then we do not need a second vcall offset.
9791 Check the list of functions already present in the derived
9792 class vtable. */
9771b263 9793 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
e6a66567 9794 {
e6a66567
MM
9795 if (same_signature_p (derived_entry, orig_fn)
9796 /* We only use one vcall offset for virtual destructors,
9797 even though there are two virtual table entries. */
9798 || (DECL_DESTRUCTOR_P (derived_entry)
9799 && DECL_DESTRUCTOR_P (orig_fn)))
9800 return;
9801 }
4e7512c9 9802
e6a66567
MM
9803 /* If we are building these vcall offsets as part of building
9804 the vtable for the most derived class, remember the vcall
9805 offset. */
9806 if (vid->binfo == TYPE_BINFO (vid->derived))
0871761b 9807 {
f32682ca 9808 tree_pair_s elt = {orig_fn, vid->index};
9771b263 9809 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
0871761b 9810 }
c8094d83 9811
e6a66567
MM
9812 /* The next vcall offset will be found at a more negative
9813 offset. */
9814 vid->index = size_binop (MINUS_EXPR, vid->index,
9815 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9816
9817 /* Keep track of this function. */
9771b263 9818 vec_safe_push (vid->fns, orig_fn);
e6a66567
MM
9819
9820 if (vid->generate_vcall_entries)
9821 {
9822 tree base;
e6a66567 9823 tree fn;
548502d3 9824
e6a66567 9825 /* Find the overriding function. */
95675950 9826 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
e6a66567 9827 if (fn == error_mark_node)
e8160c9a 9828 vcall_offset = build_zero_cst (vtable_entry_type);
e6a66567
MM
9829 else
9830 {
95675950
MM
9831 base = TREE_VALUE (fn);
9832
9833 /* The vbase we're working on is a primary base of
9834 vid->binfo. But it might be a lost primary, so its
9835 BINFO_OFFSET might be wrong, so we just use the
9836 BINFO_OFFSET from vid->binfo. */
db3927fb
AH
9837 vcall_offset = size_diffop_loc (input_location,
9838 BINFO_OFFSET (base),
95675950 9839 BINFO_OFFSET (vid->binfo));
db3927fb
AH
9840 vcall_offset = fold_build1_loc (input_location,
9841 NOP_EXPR, vtable_entry_type,
7866705a 9842 vcall_offset);
548502d3 9843 }
34cd5ae7 9844 /* Add the initializer to the vtable. */
9d6a019c 9845 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
c35cce41 9846 }
570221c2 9847}
b54ccf71 9848
34cd5ae7 9849/* Return vtbl initializers for the RTTI entries corresponding to the
aabb4cd6 9850 BINFO's vtable. The RTTI entries should indicate the object given
73ea87d7 9851 by VID->rtti_binfo. */
b54ccf71 9852
9bab6c90 9853static void
94edc4ab 9854build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
b54ccf71 9855{
ca36f057 9856 tree b;
aabb4cd6 9857 tree t;
ca36f057
MM
9858 tree offset;
9859 tree decl;
9860 tree init;
b54ccf71 9861
73ea87d7 9862 t = BINFO_TYPE (vid->rtti_binfo);
b54ccf71 9863
ca36f057
MM
9864 /* To find the complete object, we will first convert to our most
9865 primary base, and then add the offset in the vtbl to that value. */
b5a28d80 9866 b = most_primary_binfo (binfo);
db3927fb
AH
9867 offset = size_diffop_loc (input_location,
9868 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8f032717 9869
8fa33dfa
MM
9870 /* The second entry is the address of the typeinfo object. */
9871 if (flag_rtti)
7993382e 9872 decl = build_address (get_tinfo_decl (t));
ca36f057 9873 else
8fa33dfa 9874 decl = integer_zero_node;
c8094d83 9875
8fa33dfa
MM
9876 /* Convert the declaration to a type that can be stored in the
9877 vtable. */
7993382e 9878 init = build_nop (vfunc_ptr_type_node, decl);
9d6a019c 9879 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
8f032717 9880
78dcd41a
VR
9881 /* Add the offset-to-top entry. It comes earlier in the vtable than
9882 the typeinfo entry. Convert the offset to look like a
c4372ef4 9883 function pointer, so that we can put it in the vtable. */
7993382e 9884 init = build_nop (vfunc_ptr_type_node, offset);
9d6a019c 9885 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
8f032717 9886}
0f59171d 9887
22854930
PC
9888/* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9889 accessibility. */
9890
9891bool
9892uniquely_derived_from_p (tree parent, tree type)
9893{
9894 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9895 return base && base != error_mark_node;
9896}
9897
9898/* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9899
9900bool
9901publicly_uniquely_derived_p (tree parent, tree type)
9902{
9903 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9904 NULL, tf_none);
9905 return base && base != error_mark_node;
9906}
9907
3a6a88c8
JM
9908/* CTX1 and CTX2 are declaration contexts. Return the innermost common
9909 class between them, if any. */
9910
9911tree
9912common_enclosing_class (tree ctx1, tree ctx2)
9913{
9914 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9915 return NULL_TREE;
9916 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9917 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9918 if (ctx1 == ctx2)
9919 return ctx1;
9920 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9921 TYPE_MARKED_P (t) = true;
9922 tree found = NULL_TREE;
9923 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9924 if (TYPE_MARKED_P (t))
9925 {
9926 found = t;
9927 break;
9928 }
9929 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9930 TYPE_MARKED_P (t) = false;
9931 return found;
9932}
9933
1b746b0f 9934#include "gt-cp-class.h"