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