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