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