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