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