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