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