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