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