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