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