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