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