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