]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/class.c
expr.c (string_constant): Handle also read-only variables initialized to string literals.
[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))
fee7654e 1442 /* For a class w/o baseclasses, `finish_struct' has set
132c7dd3
NS
1443 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by definition).
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);
1451
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
MM
3752 if (DECL_PURE_VIRTUAL_P (x))
3753 CLASSTYPE_PURE_VIRTUALS (t)
3754 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
58010b57
MM
3755 }
3756 }
58010b57
MM
3757}
3758
db9b2174
MM
3759/* FN is a constructor or destructor. Clone the declaration to create
3760 a specialized in-charge or not-in-charge version, as indicated by
3761 NAME. */
3762
3763static tree
94edc4ab 3764build_clone (tree fn, tree name)
db9b2174
MM
3765{
3766 tree parms;
3767 tree clone;
3768
3769 /* Copy the function. */
3770 clone = copy_decl (fn);
3771 /* Remember where this function came from. */
3772 DECL_CLONED_FUNCTION (clone) = fn;
5daf7c0a 3773 DECL_ABSTRACT_ORIGIN (clone) = fn;
db9b2174
MM
3774 /* Reset the function name. */
3775 DECL_NAME (clone) = name;
71cb9286 3776 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
db9b2174
MM
3777 /* There's no pending inline data for this function. */
3778 DECL_PENDING_INLINE_INFO (clone) = NULL;
3779 DECL_PENDING_INLINE_P (clone) = 0;
3780 /* And it hasn't yet been deferred. */
3781 DECL_DEFERRED_FN (clone) = 0;
3782
298d6f60
MM
3783 /* The base-class destructor is not virtual. */
3784 if (name == base_dtor_identifier)
3785 {
3786 DECL_VIRTUAL_P (clone) = 0;
3787 if (TREE_CODE (clone) != TEMPLATE_DECL)
3788 DECL_VINDEX (clone) = NULL_TREE;
3789 }
3790
4e7512c9 3791 /* If there was an in-charge parameter, drop it from the function
db9b2174
MM
3792 type. */
3793 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3794 {
3795 tree basetype;
3796 tree parmtypes;
3797 tree exceptions;
3798
3799 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3800 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3801 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3802 /* Skip the `this' parameter. */
3803 parmtypes = TREE_CHAIN (parmtypes);
3804 /* Skip the in-charge parameter. */
3805 parmtypes = TREE_CHAIN (parmtypes);
e0fff4b3
JM
3806 /* And the VTT parm, in a complete [cd]tor. */
3807 if (DECL_HAS_VTT_PARM_P (fn)
3808 && ! DECL_NEEDS_VTT_PARM_P (clone))
3809 parmtypes = TREE_CHAIN (parmtypes);
3ec6bad3
MM
3810 /* If this is subobject constructor or destructor, add the vtt
3811 parameter. */
db9b2174 3812 TREE_TYPE (clone)
43dc123f
MM
3813 = build_method_type_directly (basetype,
3814 TREE_TYPE (TREE_TYPE (clone)),
3815 parmtypes);
db9b2174
MM
3816 if (exceptions)
3817 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3818 exceptions);
221bfb03 3819 TREE_TYPE (clone)
e9525111
MM
3820 = cp_build_type_attribute_variant (TREE_TYPE (clone),
3821 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
db9b2174
MM
3822 }
3823
e0fff4b3
JM
3824 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
3825 aren't function parameters; those are the template parameters. */
db9b2174
MM
3826 if (TREE_CODE (clone) != TEMPLATE_DECL)
3827 {
3828 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
3829 /* Remove the in-charge parameter. */
3830 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3831 {
3832 TREE_CHAIN (DECL_ARGUMENTS (clone))
3833 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3834 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
3835 }
e0fff4b3
JM
3836 /* And the VTT parm, in a complete [cd]tor. */
3837 if (DECL_HAS_VTT_PARM_P (fn))
3ec6bad3 3838 {
e0fff4b3
JM
3839 if (DECL_NEEDS_VTT_PARM_P (clone))
3840 DECL_HAS_VTT_PARM_P (clone) = 1;
3841 else
3842 {
3843 TREE_CHAIN (DECL_ARGUMENTS (clone))
3844 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3845 DECL_HAS_VTT_PARM_P (clone) = 0;
3846 }
3ec6bad3
MM
3847 }
3848
db9b2174
MM
3849 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
3850 {
3851 DECL_CONTEXT (parms) = clone;
63e1b1c4 3852 cxx_dup_lang_specific_decl (parms);
db9b2174
MM
3853 }
3854 }
3855
db9b2174 3856 /* Create the RTL for this function. */
19e7881c 3857 SET_DECL_RTL (clone, NULL_RTX);
0e6df31e 3858 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
db9b2174
MM
3859
3860 /* Make it easy to find the CLONE given the FN. */
3861 TREE_CHAIN (clone) = TREE_CHAIN (fn);
3862 TREE_CHAIN (fn) = clone;
3863
3864 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
3865 if (TREE_CODE (clone) == TEMPLATE_DECL)
3866 {
3867 tree result;
3868
3869 DECL_TEMPLATE_RESULT (clone)
3870 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
3871 result = DECL_TEMPLATE_RESULT (clone);
3872 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
3873 DECL_TI_TEMPLATE (result) = clone;
3874 }
3875
3876 return clone;
3877}
3878
3879/* Produce declarations for all appropriate clones of FN. If
838dfd8a 3880 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
db9b2174
MM
3881 CLASTYPE_METHOD_VEC as well. */
3882
3883void
94edc4ab 3884clone_function_decl (tree fn, int update_method_vec_p)
db9b2174
MM
3885{
3886 tree clone;
3887
c00996a3 3888 /* Avoid inappropriate cloning. */
1f84ec23
MM
3889 if (TREE_CHAIN (fn)
3890 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
c00996a3
JM
3891 return;
3892
298d6f60 3893 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
db9b2174 3894 {
298d6f60
MM
3895 /* For each constructor, we need two variants: an in-charge version
3896 and a not-in-charge version. */
db9b2174
MM
3897 clone = build_clone (fn, complete_ctor_identifier);
3898 if (update_method_vec_p)
aaaa46d2 3899 add_method (DECL_CONTEXT (clone), clone);
db9b2174
MM
3900 clone = build_clone (fn, base_ctor_identifier);
3901 if (update_method_vec_p)
aaaa46d2 3902 add_method (DECL_CONTEXT (clone), clone);
db9b2174
MM
3903 }
3904 else
298d6f60 3905 {
50bc768d 3906 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
298d6f60 3907
3ec6bad3 3908 /* For each destructor, we need three variants: an in-charge
298d6f60 3909 version, a not-in-charge version, and an in-charge deleting
4e7512c9
MM
3910 version. We clone the deleting version first because that
3911 means it will go second on the TYPE_METHODS list -- and that
3912 corresponds to the correct layout order in the virtual
52682a1b
MM
3913 function table.
3914
3915 For a non-virtual destructor, we do not build a deleting
3916 destructor. */
3917 if (DECL_VIRTUAL_P (fn))
3918 {
3919 clone = build_clone (fn, deleting_dtor_identifier);
3920 if (update_method_vec_p)
aaaa46d2 3921 add_method (DECL_CONTEXT (clone), clone);
52682a1b 3922 }
4e7512c9 3923 clone = build_clone (fn, complete_dtor_identifier);
298d6f60 3924 if (update_method_vec_p)
aaaa46d2 3925 add_method (DECL_CONTEXT (clone), clone);
298d6f60
MM
3926 clone = build_clone (fn, base_dtor_identifier);
3927 if (update_method_vec_p)
aaaa46d2 3928 add_method (DECL_CONTEXT (clone), clone);
298d6f60 3929 }
5daf7c0a
JM
3930
3931 /* Note that this is an abstract function that is never emitted. */
3932 DECL_ABSTRACT (fn) = 1;
db9b2174
MM
3933}
3934
5f6eeeb3
NS
3935/* DECL is an in charge constructor, which is being defined. This will
3936 have had an in class declaration, from whence clones were
3937 declared. An out-of-class definition can specify additional default
3938 arguments. As it is the clones that are involved in overload
3939 resolution, we must propagate the information from the DECL to its
00a17e31 3940 clones. */
5f6eeeb3
NS
3941
3942void
94edc4ab 3943adjust_clone_args (tree decl)
5f6eeeb3
NS
3944{
3945 tree clone;
3946
3947 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
3948 clone = TREE_CHAIN (clone))
3949 {
3950 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
3951 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
3952 tree decl_parms, clone_parms;
3953
3954 clone_parms = orig_clone_parms;
3955
00a17e31 3956 /* Skip the 'this' parameter. */
5f6eeeb3
NS
3957 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
3958 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
3959
3960 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
3961 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
3962 if (DECL_HAS_VTT_PARM_P (decl))
3963 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
3964
3965 clone_parms = orig_clone_parms;
3966 if (DECL_HAS_VTT_PARM_P (clone))
3967 clone_parms = TREE_CHAIN (clone_parms);
3968
3969 for (decl_parms = orig_decl_parms; decl_parms;
3970 decl_parms = TREE_CHAIN (decl_parms),
3971 clone_parms = TREE_CHAIN (clone_parms))
3972 {
50bc768d
NS
3973 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
3974 TREE_TYPE (clone_parms)));
5f6eeeb3
NS
3975
3976 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
3977 {
3978 /* A default parameter has been added. Adjust the
00a17e31 3979 clone's parameters. */
5f6eeeb3
NS
3980 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3981 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3982 tree type;
3983
3984 clone_parms = orig_decl_parms;
3985
3986 if (DECL_HAS_VTT_PARM_P (clone))
3987 {
3988 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
3989 TREE_VALUE (orig_clone_parms),
3990 clone_parms);
3991 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
3992 }
43dc123f
MM
3993 type = build_method_type_directly (basetype,
3994 TREE_TYPE (TREE_TYPE (clone)),
3995 clone_parms);
5f6eeeb3
NS
3996 if (exceptions)
3997 type = build_exception_variant (type, exceptions);
3998 TREE_TYPE (clone) = type;
3999
4000 clone_parms = NULL_TREE;
4001 break;
4002 }
4003 }
50bc768d 4004 gcc_assert (!clone_parms);
5f6eeeb3
NS
4005 }
4006}
4007
db9b2174
MM
4008/* For each of the constructors and destructors in T, create an
4009 in-charge and not-in-charge variant. */
4010
4011static void
94edc4ab 4012clone_constructors_and_destructors (tree t)
db9b2174
MM
4013{
4014 tree fns;
4015
db9b2174
MM
4016 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4017 out now. */
4018 if (!CLASSTYPE_METHOD_VEC (t))
4019 return;
4020
db9b2174
MM
4021 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4022 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
298d6f60
MM
4023 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4024 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
db9b2174
MM
4025}
4026
58010b57
MM
4027/* Remove all zero-width bit-fields from T. */
4028
4029static void
94edc4ab 4030remove_zero_width_bit_fields (tree t)
58010b57
MM
4031{
4032 tree *fieldsp;
4033
4034 fieldsp = &TYPE_FIELDS (t);
4035 while (*fieldsp)
4036 {
4037 if (TREE_CODE (*fieldsp) == FIELD_DECL
4038 && DECL_C_BIT_FIELD (*fieldsp)
4039 && DECL_INITIAL (*fieldsp))
4040 *fieldsp = TREE_CHAIN (*fieldsp);
4041 else
4042 fieldsp = &TREE_CHAIN (*fieldsp);
4043 }
4044}
4045
dbc957f1
MM
4046/* Returns TRUE iff we need a cookie when dynamically allocating an
4047 array whose elements have the indicated class TYPE. */
4048
4049static bool
94edc4ab 4050type_requires_array_cookie (tree type)
dbc957f1
MM
4051{
4052 tree fns;
18fee3ee 4053 bool has_two_argument_delete_p = false;
dbc957f1 4054
50bc768d 4055 gcc_assert (CLASS_TYPE_P (type));
dbc957f1
MM
4056
4057 /* If there's a non-trivial destructor, we need a cookie. In order
4058 to iterate through the array calling the destructor for each
4059 element, we'll have to know how many elements there are. */
4060 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4061 return true;
4062
4063 /* If the usual deallocation function is a two-argument whose second
4064 argument is of type `size_t', then we have to pass the size of
4065 the array to the deallocation function, so we will need to store
4066 a cookie. */
4067 fns = lookup_fnfields (TYPE_BINFO (type),
4068 ansi_opname (VEC_DELETE_EXPR),
4069 /*protect=*/0);
4070 /* If there are no `operator []' members, or the lookup is
4071 ambiguous, then we don't need a cookie. */
4072 if (!fns || fns == error_mark_node)
4073 return false;
4074 /* Loop through all of the functions. */
50ad9642 4075 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
dbc957f1
MM
4076 {
4077 tree fn;
4078 tree second_parm;
4079
4080 /* Select the current function. */
4081 fn = OVL_CURRENT (fns);
4082 /* See if this function is a one-argument delete function. If
4083 it is, then it will be the usual deallocation function. */
4084 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4085 if (second_parm == void_list_node)
4086 return false;
4087 /* Otherwise, if we have a two-argument function and the second
4088 argument is `size_t', it will be the usual deallocation
4089 function -- unless there is one-argument function, too. */
4090 if (TREE_CHAIN (second_parm) == void_list_node
4091 && same_type_p (TREE_VALUE (second_parm), sizetype))
4092 has_two_argument_delete_p = true;
4093 }
4094
4095 return has_two_argument_delete_p;
4096}
4097
607cf131
MM
4098/* Check the validity of the bases and members declared in T. Add any
4099 implicitly-generated functions (like copy-constructors and
4100 assignment operators). Compute various flag bits (like
4101 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4102 level: i.e., independently of the ABI in use. */
4103
4104static void
58731fd1 4105check_bases_and_members (tree t)
607cf131
MM
4106{
4107 /* Nonzero if we are not allowed to generate a default constructor
4108 for this case. */
4109 int cant_have_default_ctor;
4110 /* Nonzero if the implicitly generated copy constructor should take
4111 a non-const reference argument. */
4112 int cant_have_const_ctor;
4113 /* Nonzero if the the implicitly generated assignment operator
4114 should take a non-const reference argument. */
4115 int no_const_asn_ref;
4116 tree access_decls;
4117
4118 /* By default, we use const reference arguments and generate default
4119 constructors. */
4120 cant_have_default_ctor = 0;
4121 cant_have_const_ctor = 0;
4122 no_const_asn_ref = 0;
4123
00a17e31 4124 /* Check all the base-classes. */
607cf131
MM
4125 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4126 &no_const_asn_ref);
4127
4128 /* Check all the data member declarations. */
58731fd1 4129 check_field_decls (t, &access_decls,
607cf131
MM
4130 &cant_have_default_ctor,
4131 &cant_have_const_ctor,
4132 &no_const_asn_ref);
4133
4134 /* Check all the method declarations. */
4135 check_methods (t);
4136
bbd15aac
MM
4137 /* A nearly-empty class has to be vptr-containing; a nearly empty
4138 class contains just a vptr. */
4139 if (!TYPE_CONTAINS_VPTR_P (t))
f9c528ea
MM
4140 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4141
607cf131
MM
4142 /* Do some bookkeeping that will guide the generation of implicitly
4143 declared member functions. */
4144 TYPE_HAS_COMPLEX_INIT_REF (t)
3ef397c1
MM
4145 |= (TYPE_HAS_INIT_REF (t)
4146 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4147 || TYPE_POLYMORPHIC_P (t));
607cf131 4148 TYPE_NEEDS_CONSTRUCTING (t)
3ef397c1
MM
4149 |= (TYPE_HAS_CONSTRUCTOR (t)
4150 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4151 || TYPE_POLYMORPHIC_P (t));
4152 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4153 || TYPE_POLYMORPHIC_P (t));
607cf131
MM
4154 CLASSTYPE_NON_POD_P (t)
4155 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4156 || TYPE_HAS_ASSIGN_REF (t));
607cf131 4157 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
0830ae44 4158 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
607cf131 4159
62d1db17 4160 /* Synthesize any needed methods. */
607cf131
MM
4161 add_implicitly_declared_members (t, cant_have_default_ctor,
4162 cant_have_const_ctor,
4163 no_const_asn_ref);
4164
db9b2174
MM
4165 /* Create the in-charge and not-in-charge variants of constructors
4166 and destructors. */
4167 clone_constructors_and_destructors (t);
4168
aa52c1ff
JM
4169 /* Process the using-declarations. */
4170 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4171 handle_using_decl (TREE_VALUE (access_decls), t);
4172
607cf131
MM
4173 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4174 finish_struct_methods (t);
dbc957f1
MM
4175
4176 /* Figure out whether or not we will need a cookie when dynamically
4177 allocating an array of this type. */
e2500fed 4178 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
dbc957f1 4179 = type_requires_array_cookie (t);
607cf131
MM
4180}
4181
3ef397c1 4182/* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5c24fba6
MM
4183 accordingly. If a new vfield was created (because T doesn't have a
4184 primary base class), then the newly created field is returned. It
c35cce41 4185 is not added to the TYPE_FIELDS list; it is the caller's
e6858a84
NS
4186 responsibility to do that. Accumulate declared virtual functions
4187 on VIRTUALS_P. */
3ef397c1 4188
5c24fba6 4189static tree
94edc4ab 4190create_vtable_ptr (tree t, tree* virtuals_p)
3ef397c1
MM
4191{
4192 tree fn;
4193
e6858a84 4194 /* Collect the virtual functions declared in T. */
3ef397c1 4195 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
e6858a84
NS
4196 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4197 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4198 {
4199 tree new_virtual = make_node (TREE_LIST);
4200
4201 BV_FN (new_virtual) = fn;
4202 BV_DELTA (new_virtual) = integer_zero_node;
3ef397c1 4203
e6858a84
NS
4204 TREE_CHAIN (new_virtual) = *virtuals_p;
4205 *virtuals_p = new_virtual;
4206 }
4207
da3d4dfa
MM
4208 /* If we couldn't find an appropriate base class, create a new field
4209 here. Even if there weren't any new virtual functions, we might need a
bbd15aac
MM
4210 new virtual function table if we're supposed to include vptrs in
4211 all classes that need them. */
e6858a84 4212 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
3ef397c1
MM
4213 {
4214 /* We build this decl with vtbl_ptr_type_node, which is a
4215 `vtable_entry_type*'. It might seem more precise to use
a692ad2e 4216 `vtable_entry_type (*)[N]' where N is the number of virtual
3ef397c1
MM
4217 functions. However, that would require the vtable pointer in
4218 base classes to have a different type than the vtable pointer
4219 in derived classes. We could make that happen, but that
4220 still wouldn't solve all the problems. In particular, the
4221 type-based alias analysis code would decide that assignments
4222 to the base class vtable pointer can't alias assignments to
4223 the derived class vtable pointer, since they have different
4639c5c6 4224 types. Thus, in a derived class destructor, where the base
3ef397c1
MM
4225 class constructor was inlined, we could generate bad code for
4226 setting up the vtable pointer.
4227
4228 Therefore, we use one type for all vtable pointers. We still
4229 use a type-correct type; it's just doesn't indicate the array
4230 bounds. That's better than using `void*' or some such; it's
4231 cleaner, and it let's the alias analysis code know that these
4232 stores cannot alias stores to void*! */
0abe00c5
NS
4233 tree field;
4234
4235 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4236 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4237 DECL_VIRTUAL_P (field) = 1;
4238 DECL_ARTIFICIAL (field) = 1;
4239 DECL_FIELD_CONTEXT (field) = t;
4240 DECL_FCONTEXT (field) = t;
0abe00c5
NS
4241
4242 TYPE_VFIELD (t) = field;
4243
4244 /* This class is non-empty. */
58731fd1 4245 CLASSTYPE_EMPTY_P (t) = 0;
3ef397c1 4246
604a3205 4247 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
5c24fba6
MM
4248 /* If there were any baseclasses, they can't possibly be at
4249 offset zero any more, because that's where the vtable
4250 pointer is. So, converting to a base class is going to
4251 take work. */
4252 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
3ef397c1 4253
0abe00c5 4254 return field;
3ef397c1 4255 }
5c24fba6
MM
4256
4257 return NULL_TREE;
3ef397c1
MM
4258}
4259
2ef16140
MM
4260/* Fixup the inline function given by INFO now that the class is
4261 complete. */
08b962b0 4262
2ef16140 4263static void
94edc4ab 4264fixup_pending_inline (tree fn)
2ef16140 4265{
0e5921e8 4266 if (DECL_PENDING_INLINE_INFO (fn))
2ef16140 4267 {
0e5921e8 4268 tree args = DECL_ARGUMENTS (fn);
2ef16140
MM
4269 while (args)
4270 {
4271 DECL_CONTEXT (args) = fn;
4272 args = TREE_CHAIN (args);
4273 }
4274 }
4275}
08b962b0 4276
2ef16140
MM
4277/* Fixup the inline methods and friends in TYPE now that TYPE is
4278 complete. */
08b962b0 4279
2ef16140 4280static void
94edc4ab 4281fixup_inline_methods (tree type)
08b962b0 4282{
2ef16140 4283 tree method = TYPE_METHODS (type);
08b962b0 4284
2ef16140 4285 if (method && TREE_CODE (method) == TREE_VEC)
08b962b0 4286 {
2ef16140
MM
4287 if (TREE_VEC_ELT (method, 1))
4288 method = TREE_VEC_ELT (method, 1);
4289 else if (TREE_VEC_ELT (method, 0))
4290 method = TREE_VEC_ELT (method, 0);
08b962b0 4291 else
2ef16140 4292 method = TREE_VEC_ELT (method, 2);
08b962b0
MM
4293 }
4294
2ef16140
MM
4295 /* Do inline member functions. */
4296 for (; method; method = TREE_CHAIN (method))
0e5921e8 4297 fixup_pending_inline (method);
08b962b0 4298
2ef16140
MM
4299 /* Do friends. */
4300 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4301 method;
4302 method = TREE_CHAIN (method))
0e5921e8 4303 fixup_pending_inline (TREE_VALUE (method));
351c54c8 4304 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
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. */
5186 CLASSTYPE_PURE_VIRTUALS (t) = NULL_TREE;
5187 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
5188 if (DECL_PURE_VIRTUAL_P (x))
5189 CLASSTYPE_PURE_VIRTUALS (t)
5190 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
5191 complete_vars (t);
6f1b4c42 5192 }
f30432d7 5193 else
9f33663b 5194 finish_struct_1 (t);
5566b478 5195
82a98427 5196 input_location = saved_loc;
1f0d71c5 5197
5566b478 5198 TYPE_BEING_DEFINED (t) = 0;
8f032717 5199
5566b478 5200 if (current_class_type)
b74a0560 5201 popclass ();
5566b478 5202 else
357351e5 5203 error ("trying to finish struct, but kicked out due to previous parse errors");
5566b478 5204
5f261ba9
MM
5205 if (processing_template_decl && at_function_scope_p ())
5206 add_stmt (build_min (TAG_DEFN, t));
ae673f14 5207
5566b478 5208 return t;
f30432d7 5209}
8d08fdba 5210\f
51ddb82e 5211/* Return the dynamic type of INSTANCE, if known.
8d08fdba
MS
5212 Used to determine whether the virtual function table is needed
5213 or not.
5214
5215 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
97d953bb
MM
5216 of our knowledge of its type. *NONNULL should be initialized
5217 before this function is called. */
e92cc029 5218
d8e178a0 5219static tree
94edc4ab 5220fixed_type_or_null (tree instance, int* nonnull, int* cdtorp)
8d08fdba
MS
5221{
5222 switch (TREE_CODE (instance))
5223 {
5224 case INDIRECT_REF:
608afcc5 5225 if (POINTER_TYPE_P (TREE_TYPE (instance)))
a0de9d20
JM
5226 return NULL_TREE;
5227 else
5228 return fixed_type_or_null (TREE_OPERAND (instance, 0),
5229 nonnull, cdtorp);
5230
8d08fdba
MS
5231 case CALL_EXPR:
5232 /* This is a call to a constructor, hence it's never zero. */
5233 if (TREE_HAS_CONSTRUCTOR (instance))
5234 {
5235 if (nonnull)
5236 *nonnull = 1;
51ddb82e 5237 return TREE_TYPE (instance);
8d08fdba 5238 }
51ddb82e 5239 return NULL_TREE;
8d08fdba
MS
5240
5241 case SAVE_EXPR:
5242 /* This is a call to a constructor, hence it's never zero. */
5243 if (TREE_HAS_CONSTRUCTOR (instance))
5244 {
5245 if (nonnull)
5246 *nonnull = 1;
51ddb82e 5247 return TREE_TYPE (instance);
8d08fdba 5248 }
394fd776 5249 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
8d08fdba 5250
8d08fdba
MS
5251 case PLUS_EXPR:
5252 case MINUS_EXPR:
394fd776
NS
5253 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5254 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
8d08fdba
MS
5255 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5256 /* Propagate nonnull. */
f63ab951 5257 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
51ddb82e 5258 return NULL_TREE;
8d08fdba
MS
5259
5260 case NOP_EXPR:
5261 case CONVERT_EXPR:
394fd776 5262 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
8d08fdba
MS
5263
5264 case ADDR_EXPR:
88f19756 5265 instance = TREE_OPERAND (instance, 0);
8d08fdba 5266 if (nonnull)
88f19756
RH
5267 {
5268 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
5269 with a real object -- given &p->f, p can still be null. */
5270 tree t = get_base_address (instance);
5271 /* ??? Probably should check DECL_WEAK here. */
5272 if (t && DECL_P (t))
5273 *nonnull = 1;
5274 }
5275 return fixed_type_or_null (instance, nonnull, cdtorp);
8d08fdba
MS
5276
5277 case COMPONENT_REF:
642124c6
RH
5278 /* If this component is really a base class reference, then the field
5279 itself isn't definitive. */
5280 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
5281 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
394fd776 5282 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
8d08fdba 5283
8d08fdba
MS
5284 case VAR_DECL:
5285 case FIELD_DECL:
5286 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5287 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5288 {
5289 if (nonnull)
5290 *nonnull = 1;
51ddb82e 5291 return TREE_TYPE (TREE_TYPE (instance));
8d08fdba 5292 }
e92cc029 5293 /* fall through... */
8d08fdba
MS
5294 case TARGET_EXPR:
5295 case PARM_DECL:
f63ab951 5296 case RESULT_DECL:
8d08fdba
MS
5297 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5298 {
5299 if (nonnull)
5300 *nonnull = 1;
51ddb82e 5301 return TREE_TYPE (instance);
8d08fdba 5302 }
394fd776
NS
5303 else if (instance == current_class_ptr)
5304 {
5305 if (nonnull)
5306 *nonnull = 1;
5307
00a17e31 5308 /* if we're in a ctor or dtor, we know our type. */
394fd776
NS
5309 if (DECL_LANG_SPECIFIC (current_function_decl)
5310 && (DECL_CONSTRUCTOR_P (current_function_decl)
5311 || DECL_DESTRUCTOR_P (current_function_decl)))
5312 {
5313 if (cdtorp)
5314 *cdtorp = 1;
5315 return TREE_TYPE (TREE_TYPE (instance));
5316 }
5317 }
5318 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5319 {
5320 /* Reference variables should be references to objects. */
5321 if (nonnull)
8d08fdba 5322 *nonnull = 1;
772f8889
MM
5323
5324 /* DECL_VAR_MARKED_P is used to prevent recursion; a
5325 variable's initializer may refer to the variable
5326 itself. */
5327 if (TREE_CODE (instance) == VAR_DECL
5328 && DECL_INITIAL (instance)
5329 && !DECL_VAR_MARKED_P (instance))
5330 {
5331 tree type;
5332 DECL_VAR_MARKED_P (instance) = 1;
5333 type = fixed_type_or_null (DECL_INITIAL (instance),
5334 nonnull, cdtorp);
5335 DECL_VAR_MARKED_P (instance) = 0;
5336 return type;
5337 }
8d08fdba 5338 }
51ddb82e 5339 return NULL_TREE;
8d08fdba
MS
5340
5341 default:
51ddb82e 5342 return NULL_TREE;
8d08fdba
MS
5343 }
5344}
51ddb82e 5345
838dfd8a 5346/* Return nonzero if the dynamic type of INSTANCE is known, and
338d90b8
NS
5347 equivalent to the static type. We also handle the case where
5348 INSTANCE is really a pointer. Return negative if this is a
5349 ctor/dtor. There the dynamic type is known, but this might not be
5350 the most derived base of the original object, and hence virtual
5351 bases may not be layed out according to this type.
51ddb82e
JM
5352
5353 Used to determine whether the virtual function table is needed
5354 or not.
5355
5356 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
97d953bb
MM
5357 of our knowledge of its type. *NONNULL should be initialized
5358 before this function is called. */
51ddb82e
JM
5359
5360int
94edc4ab 5361resolves_to_fixed_type_p (tree instance, int* nonnull)
51ddb82e
JM
5362{
5363 tree t = TREE_TYPE (instance);
394fd776
NS
5364 int cdtorp = 0;
5365
5366 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
51ddb82e
JM
5367 if (fixed == NULL_TREE)
5368 return 0;
5369 if (POINTER_TYPE_P (t))
5370 t = TREE_TYPE (t);
394fd776
NS
5371 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5372 return 0;
5373 return cdtorp ? -1 : 1;
51ddb82e
JM
5374}
5375
8d08fdba
MS
5376\f
5377void
94edc4ab 5378init_class_processing (void)
8d08fdba
MS
5379{
5380 current_class_depth = 0;
61a127b3
MM
5381 current_class_stack_size = 10;
5382 current_class_stack
c68b0a84 5383 = xmalloc (current_class_stack_size * sizeof (struct class_stack_node));
1f6e1acc 5384 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
8d08fdba 5385
0e5921e8
ZW
5386 ridpointers[(int) RID_PUBLIC] = access_public_node;
5387 ridpointers[(int) RID_PRIVATE] = access_private_node;
5388 ridpointers[(int) RID_PROTECTED] = access_protected_node;
8d08fdba
MS
5389}
5390
39fb05d0
MM
5391/* Restore the cached PREVIOUS_CLASS_LEVEL. */
5392
5393static void
5394restore_class_cache (void)
5395{
39fb05d0 5396 tree type;
39fb05d0
MM
5397
5398 /* We are re-entering the same class we just left, so we don't
5399 have to search the whole inheritance matrix to find all the
5400 decls to bind again. Instead, we install the cached
5401 class_shadowed list and walk through it binding names. */
5402 push_binding_level (previous_class_level);
5403 class_binding_level = previous_class_level;
39fb05d0
MM
5404 /* Restore IDENTIFIER_TYPE_VALUE. */
5405 for (type = class_binding_level->type_shadowed;
5406 type;
5407 type = TREE_CHAIN (type))
5408 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
5409}
5410
a723baf1
MM
5411/* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
5412 appropriate for TYPE.
8d08fdba 5413
8d08fdba
MS
5414 So that we may avoid calls to lookup_name, we cache the _TYPE
5415 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5416
5417 For multiple inheritance, we perform a two-pass depth-first search
39fb05d0 5418 of the type lattice. */
8d08fdba
MS
5419
5420void
29370796 5421pushclass (tree type)
8d08fdba 5422{
7fb4a8f7 5423 type = TYPE_MAIN_VARIANT (type);
8d08fdba 5424
61a127b3
MM
5425 /* Make sure there is enough room for the new entry on the stack. */
5426 if (current_class_depth + 1 >= current_class_stack_size)
8d08fdba 5427 {
61a127b3
MM
5428 current_class_stack_size *= 2;
5429 current_class_stack
c68b0a84
KG
5430 = xrealloc (current_class_stack,
5431 current_class_stack_size
5432 * sizeof (struct class_stack_node));
8d08fdba
MS
5433 }
5434
61a127b3
MM
5435 /* Insert a new entry on the class stack. */
5436 current_class_stack[current_class_depth].name = current_class_name;
5437 current_class_stack[current_class_depth].type = current_class_type;
5438 current_class_stack[current_class_depth].access = current_access_specifier;
8f032717 5439 current_class_stack[current_class_depth].names_used = 0;
61a127b3
MM
5440 current_class_depth++;
5441
5442 /* Now set up the new type. */
8d08fdba
MS
5443 current_class_name = TYPE_NAME (type);
5444 if (TREE_CODE (current_class_name) == TYPE_DECL)
5445 current_class_name = DECL_NAME (current_class_name);
5446 current_class_type = type;
5447
61a127b3
MM
5448 /* By default, things in classes are private, while things in
5449 structures or unions are public. */
5450 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5451 ? access_private_node
5452 : access_public_node);
5453
89b578be
MM
5454 if (previous_class_level
5455 && type != previous_class_level->this_entity
8d08fdba
MS
5456 && current_class_depth == 1)
5457 {
5458 /* Forcibly remove any old class remnants. */
8f032717 5459 invalidate_class_lookup_cache ();
8d08fdba
MS
5460 }
5461
89b578be
MM
5462 if (!previous_class_level
5463 || type != previous_class_level->this_entity
5464 || current_class_depth > 1)
90ea9897 5465 pushlevel_class ();
29370796 5466 else
39fb05d0 5467 restore_class_cache ();
29370796
NS
5468
5469 cxx_remember_type_decls (CLASSTYPE_NESTED_UTDS (type));
8f032717
MM
5470}
5471
39fb05d0
MM
5472/* When we exit a toplevel class scope, we save its binding level so
5473 that we can restore it quickly. Here, we've entered some other
5474 class, so we must invalidate our cache. */
8d08fdba 5475
8f032717 5476void
94edc4ab 5477invalidate_class_lookup_cache (void)
8f032717 5478{
89b578be 5479 previous_class_level = NULL;
8d08fdba
MS
5480}
5481
5482/* Get out of the current class scope. If we were in a class scope
b74a0560 5483 previously, that is the one popped to. */
e92cc029 5484
8d08fdba 5485void
94edc4ab 5486popclass (void)
8d08fdba 5487{
273a708f 5488 poplevel_class ();
8d08fdba
MS
5489
5490 current_class_depth--;
61a127b3
MM
5491 current_class_name = current_class_stack[current_class_depth].name;
5492 current_class_type = current_class_stack[current_class_depth].type;
5493 current_access_specifier = current_class_stack[current_class_depth].access;
8f032717
MM
5494 if (current_class_stack[current_class_depth].names_used)
5495 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8d08fdba
MS
5496}
5497
70adf8a9
JM
5498/* Returns 1 if current_class_type is either T or a nested type of T.
5499 We start looking from 1 because entry 0 is from global scope, and has
5500 no type. */
b9082e8a
JM
5501
5502int
94edc4ab 5503currently_open_class (tree t)
b9082e8a
JM
5504{
5505 int i;
14d22dd6 5506 if (current_class_type && same_type_p (t, current_class_type))
b9082e8a 5507 return 1;
70adf8a9 5508 for (i = 1; i < current_class_depth; ++i)
14d22dd6
MM
5509 if (current_class_stack[i].type
5510 && same_type_p (current_class_stack [i].type, t))
b9082e8a
JM
5511 return 1;
5512 return 0;
5513}
5514
70adf8a9
JM
5515/* If either current_class_type or one of its enclosing classes are derived
5516 from T, return the appropriate type. Used to determine how we found
5517 something via unqualified lookup. */
5518
5519tree
94edc4ab 5520currently_open_derived_class (tree t)
70adf8a9
JM
5521{
5522 int i;
5523
9bcb9aae 5524 /* The bases of a dependent type are unknown. */
1fb3244a
MM
5525 if (dependent_type_p (t))
5526 return NULL_TREE;
5527
c44e68a5
KL
5528 if (!current_class_type)
5529 return NULL_TREE;
5530
70adf8a9
JM
5531 if (DERIVED_FROM_P (t, current_class_type))
5532 return current_class_type;
5533
5534 for (i = current_class_depth - 1; i > 0; --i)
5535 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5536 return current_class_stack[i].type;
5537
5538 return NULL_TREE;
5539}
5540
8d08fdba 5541/* When entering a class scope, all enclosing class scopes' names with
14d22dd6
MM
5542 static meaning (static variables, static functions, types and
5543 enumerators) have to be visible. This recursive function calls
5544 pushclass for all enclosing class contexts until global or a local
5545 scope is reached. TYPE is the enclosed class. */
8d08fdba
MS
5546
5547void
14d22dd6 5548push_nested_class (tree type)
8d08fdba 5549{
a28e3c7f
MS
5550 tree context;
5551
b262d64c 5552 /* A namespace might be passed in error cases, like A::B:C. */
07c88314
MM
5553 if (type == NULL_TREE
5554 || type == error_mark_node
b262d64c 5555 || TREE_CODE (type) == NAMESPACE_DECL
07c88314 5556 || ! IS_AGGR_TYPE (type)
73b0fce8 5557 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
a1281f45 5558 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
a28e3c7f
MS
5559 return;
5560
d2e5ee5c 5561 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
8d08fdba 5562
6b400b21 5563 if (context && CLASS_TYPE_P (context))
14d22dd6 5564 push_nested_class (context);
29370796 5565 pushclass (type);
8d08fdba
MS
5566}
5567
a723baf1 5568/* Undoes a push_nested_class call. */
8d08fdba
MS
5569
5570void
94edc4ab 5571pop_nested_class (void)
8d08fdba 5572{
d2e5ee5c 5573 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8d08fdba 5574
b74a0560 5575 popclass ();
6b400b21 5576 if (context && CLASS_TYPE_P (context))
b74a0560 5577 pop_nested_class ();
8d08fdba
MS
5578}
5579
46ccf50a
JM
5580/* Returns the number of extern "LANG" blocks we are nested within. */
5581
5582int
94edc4ab 5583current_lang_depth (void)
46ccf50a
JM
5584{
5585 return VARRAY_ACTIVE_SIZE (current_lang_base);
5586}
5587
8d08fdba
MS
5588/* Set global variables CURRENT_LANG_NAME to appropriate value
5589 so that behavior of name-mangling machinery is correct. */
5590
5591void
94edc4ab 5592push_lang_context (tree name)
8d08fdba 5593{
46ccf50a 5594 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
8d08fdba 5595
e229f2cd 5596 if (name == lang_name_cplusplus)
8d08fdba 5597 {
8d08fdba
MS
5598 current_lang_name = name;
5599 }
e229f2cd
PB
5600 else if (name == lang_name_java)
5601 {
e229f2cd
PB
5602 current_lang_name = name;
5603 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5604 (See record_builtin_java_type in decl.c.) However, that causes
5605 incorrect debug entries if these types are actually used.
00a17e31 5606 So we re-enable debug output after extern "Java". */
e3cd9945
APB
5607 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5608 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5609 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5610 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5611 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5612 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5613 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5614 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
e229f2cd 5615 }
8d08fdba
MS
5616 else if (name == lang_name_c)
5617 {
8d08fdba
MS
5618 current_lang_name = name;
5619 }
5620 else
4460cef2 5621 error ("language string `\"%E\"' not recognized", name);
8d08fdba
MS
5622}
5623
5624/* Get out of the current language scope. */
e92cc029 5625
8d08fdba 5626void
94edc4ab 5627pop_lang_context (void)
8d08fdba 5628{
46ccf50a
JM
5629 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5630 VARRAY_POP (current_lang_base);
8d08fdba 5631}
8d08fdba
MS
5632\f
5633/* Type instantiation routines. */
5634
104bf76a
MM
5635/* Given an OVERLOAD and a TARGET_TYPE, return the function that
5636 matches the TARGET_TYPE. If there is no satisfactory match, return
92af500d
NS
5637 error_mark_node, and issue a error & warning messages under control
5638 of FLAGS. Permit pointers to member function if FLAGS permits. If
5639 TEMPLATE_ONLY, the name of the overloaded function was a
5640 template-id, and EXPLICIT_TARGS are the explicitly provided
104bf76a
MM
5641 template arguments. */
5642
2c73f9f5 5643static tree
94edc4ab
NN
5644resolve_address_of_overloaded_function (tree target_type,
5645 tree overload,
92af500d
NS
5646 tsubst_flags_t flags,
5647 bool template_only,
94edc4ab 5648 tree explicit_targs)
2c73f9f5 5649{
104bf76a
MM
5650 /* Here's what the standard says:
5651
5652 [over.over]
5653
5654 If the name is a function template, template argument deduction
5655 is done, and if the argument deduction succeeds, the deduced
5656 arguments are used to generate a single template function, which
5657 is added to the set of overloaded functions considered.
5658
5659 Non-member functions and static member functions match targets of
5660 type "pointer-to-function" or "reference-to-function." Nonstatic
5661 member functions match targets of type "pointer-to-member
5662 function;" the function type of the pointer to member is used to
5663 select the member function from the set of overloaded member
5664 functions. If a nonstatic member function is selected, the
5665 reference to the overloaded function name is required to have the
5666 form of a pointer to member as described in 5.3.1.
5667
5668 If more than one function is selected, any template functions in
5669 the set are eliminated if the set also contains a non-template
5670 function, and any given template function is eliminated if the
5671 set contains a second template function that is more specialized
5672 than the first according to the partial ordering rules 14.5.5.2.
5673 After such eliminations, if any, there shall remain exactly one
5674 selected function. */
5675
5676 int is_ptrmem = 0;
5677 int is_reference = 0;
5678 /* We store the matches in a TREE_LIST rooted here. The functions
5679 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5680 interoperability with most_specialized_instantiation. */
5681 tree matches = NULL_TREE;
50714e79 5682 tree fn;
104bf76a 5683
d8f8dca1
MM
5684 /* By the time we get here, we should be seeing only real
5685 pointer-to-member types, not the internal POINTER_TYPE to
5686 METHOD_TYPE representation. */
50bc768d
NS
5687 gcc_assert (TREE_CODE (target_type) != POINTER_TYPE
5688 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
104bf76a 5689
50bc768d 5690 gcc_assert (is_overloaded_fn (overload));
92af500d 5691
104bf76a
MM
5692 /* Check that the TARGET_TYPE is reasonable. */
5693 if (TYPE_PTRFN_P (target_type))
381ddaa6 5694 /* This is OK. */;
104bf76a
MM
5695 else if (TYPE_PTRMEMFUNC_P (target_type))
5696 /* This is OK, too. */
5697 is_ptrmem = 1;
5698 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5699 {
5700 /* This is OK, too. This comes from a conversion to reference
5701 type. */
5702 target_type = build_reference_type (target_type);
5703 is_reference = 1;
5704 }
5705 else
5706 {
92af500d 5707 if (flags & tf_error)
33bd39a2 5708 error ("\
381ddaa6
JM
5709cannot resolve overloaded function `%D' based on conversion to type `%T'",
5710 DECL_NAME (OVL_FUNCTION (overload)), target_type);
104bf76a
MM
5711 return error_mark_node;
5712 }
5713
5714 /* If we can find a non-template function that matches, we can just
5715 use it. There's no point in generating template instantiations
5716 if we're just going to throw them out anyhow. But, of course, we
5717 can only do this when we don't *need* a template function. */
5718 if (!template_only)
5719 {
5720 tree fns;
5721
a723baf1 5722 for (fns = overload; fns; fns = OVL_NEXT (fns))
104bf76a 5723 {
a723baf1 5724 tree fn = OVL_CURRENT (fns);
104bf76a 5725 tree fntype;
2c73f9f5 5726
104bf76a
MM
5727 if (TREE_CODE (fn) == TEMPLATE_DECL)
5728 /* We're not looking for templates just yet. */
5729 continue;
5730
5731 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5732 != is_ptrmem)
5733 /* We're looking for a non-static member, and this isn't
5734 one, or vice versa. */
5735 continue;
34ff2673
RS
5736
5737 /* Ignore anticipated decls of undeclared builtins. */
5738 if (DECL_ANTICIPATED (fn))
5739 continue;
5740
104bf76a
MM
5741 /* See if there's a match. */
5742 fntype = TREE_TYPE (fn);
5743 if (is_ptrmem)
5744 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5745 else if (!is_reference)
5746 fntype = build_pointer_type (fntype);
5747
5748 if (can_convert_arg (target_type, fntype, fn))
e1b3e07d 5749 matches = tree_cons (fn, NULL_TREE, matches);
104bf76a
MM
5750 }
5751 }
5752
5753 /* Now, if we've already got a match (or matches), there's no need
5754 to proceed to the template functions. But, if we don't have a
5755 match we need to look at them, too. */
5756 if (!matches)
2c73f9f5 5757 {
104bf76a
MM
5758 tree target_fn_type;
5759 tree target_arg_types;
8d3631f8 5760 tree target_ret_type;
104bf76a
MM
5761 tree fns;
5762
5763 if (is_ptrmem)
4393e105
MM
5764 target_fn_type
5765 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
2c73f9f5 5766 else
4393e105
MM
5767 target_fn_type = TREE_TYPE (target_type);
5768 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8d3631f8 5769 target_ret_type = TREE_TYPE (target_fn_type);
e5214479
JM
5770
5771 /* Never do unification on the 'this' parameter. */
5772 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5773 target_arg_types = TREE_CHAIN (target_arg_types);
4393e105 5774
a723baf1 5775 for (fns = overload; fns; fns = OVL_NEXT (fns))
104bf76a 5776 {
a723baf1 5777 tree fn = OVL_CURRENT (fns);
104bf76a
MM
5778 tree instantiation;
5779 tree instantiation_type;
5780 tree targs;
5781
5782 if (TREE_CODE (fn) != TEMPLATE_DECL)
5783 /* We're only looking for templates. */
5784 continue;
5785
5786 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5787 != is_ptrmem)
4393e105 5788 /* We're not looking for a non-static member, and this is
104bf76a
MM
5789 one, or vice versa. */
5790 continue;
5791
104bf76a 5792 /* Try to do argument deduction. */
f31c0a32 5793 targs = make_tree_vec (DECL_NTPARMS (fn));
4393e105 5794 if (fn_type_unification (fn, explicit_targs, targs,
8d3631f8 5795 target_arg_types, target_ret_type,
e5214479 5796 DEDUCE_EXACT, -1) != 0)
104bf76a
MM
5797 /* Argument deduction failed. */
5798 continue;
5799
5800 /* Instantiate the template. */
92af500d 5801 instantiation = instantiate_template (fn, targs, flags);
104bf76a
MM
5802 if (instantiation == error_mark_node)
5803 /* Instantiation failed. */
5804 continue;
5805
5806 /* See if there's a match. */
5807 instantiation_type = TREE_TYPE (instantiation);
5808 if (is_ptrmem)
5809 instantiation_type =
5810 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5811 else if (!is_reference)
5812 instantiation_type = build_pointer_type (instantiation_type);
5813 if (can_convert_arg (target_type, instantiation_type, instantiation))
e1b3e07d 5814 matches = tree_cons (instantiation, fn, matches);
104bf76a
MM
5815 }
5816
5817 /* Now, remove all but the most specialized of the matches. */
5818 if (matches)
5819 {
e5214479 5820 tree match = most_specialized_instantiation (matches);
104bf76a
MM
5821
5822 if (match != error_mark_node)
e1b3e07d 5823 matches = tree_cons (match, NULL_TREE, NULL_TREE);
104bf76a
MM
5824 }
5825 }
5826
5827 /* Now we should have exactly one function in MATCHES. */
5828 if (matches == NULL_TREE)
5829 {
5830 /* There were *no* matches. */
92af500d 5831 if (flags & tf_error)
104bf76a 5832 {
33bd39a2 5833 error ("no matches converting function `%D' to type `%#T'",
104bf76a
MM
5834 DECL_NAME (OVL_FUNCTION (overload)),
5835 target_type);
6b9b6b15
JM
5836
5837 /* print_candidates expects a chain with the functions in
5838 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5839 so why be clever?). */
5840 for (; overload; overload = OVL_NEXT (overload))
e1b3e07d
MM
5841 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5842 matches);
6b9b6b15
JM
5843
5844 print_candidates (matches);
104bf76a
MM
5845 }
5846 return error_mark_node;
2c73f9f5 5847 }
104bf76a
MM
5848 else if (TREE_CHAIN (matches))
5849 {
5850 /* There were too many matches. */
5851
92af500d 5852 if (flags & tf_error)
104bf76a
MM
5853 {
5854 tree match;
5855
33bd39a2 5856 error ("converting overloaded function `%D' to type `%#T' is ambiguous",
104bf76a
MM
5857 DECL_NAME (OVL_FUNCTION (overload)),
5858 target_type);
5859
5860 /* Since print_candidates expects the functions in the
5861 TREE_VALUE slot, we flip them here. */
5862 for (match = matches; match; match = TREE_CHAIN (match))
5863 TREE_VALUE (match) = TREE_PURPOSE (match);
5864
5865 print_candidates (matches);
5866 }
5867
5868 return error_mark_node;
5869 }
5870
50714e79
MM
5871 /* Good, exactly one match. Now, convert it to the correct type. */
5872 fn = TREE_PURPOSE (matches);
5873
b1ce3eb2 5874 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
92af500d 5875 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
19420d00 5876 {
b1ce3eb2
NS
5877 static int explained;
5878
92af500d 5879 if (!(flags & tf_error))
19420d00
NS
5880 return error_mark_node;
5881
33bd39a2 5882 pedwarn ("assuming pointer to member `%D'", fn);
b1ce3eb2
NS
5883 if (!explained)
5884 {
33bd39a2 5885 pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
b1ce3eb2
NS
5886 explained = 1;
5887 }
19420d00 5888 }
84583208
MM
5889
5890 /* If we're doing overload resolution purely for the purpose of
5891 determining conversion sequences, we should not consider the
5892 function used. If this conversion sequence is selected, the
5893 function will be marked as used at this point. */
5894 if (!(flags & tf_conv))
5895 mark_used (fn);
a6ecf8b6 5896
50714e79
MM
5897 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5898 return build_unary_op (ADDR_EXPR, fn, 0);
5899 else
5900 {
5901 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5902 will mark the function as addressed, but here we must do it
5903 explicitly. */
dffd7eb6 5904 cxx_mark_addressable (fn);
50714e79
MM
5905
5906 return fn;
5907 }
2c73f9f5
ML
5908}
5909
ec255269
MS
5910/* This function will instantiate the type of the expression given in
5911 RHS to match the type of LHSTYPE. If errors exist, then return
92af500d 5912 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
5e76004e
NS
5913 we complain on errors. If we are not complaining, never modify rhs,
5914 as overload resolution wants to try many possible instantiations, in
5915 the hope that at least one will work.
5916
e6e174e5
JM
5917 For non-recursive calls, LHSTYPE should be a function, pointer to
5918 function, or a pointer to member function. */
e92cc029 5919
8d08fdba 5920tree
94edc4ab 5921instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
8d08fdba 5922{
92af500d 5923 tsubst_flags_t flags_in = flags;
19420d00 5924
c2ea3a40 5925 flags &= ~tf_ptrmem_ok;
105d8e1f 5926
8d08fdba
MS
5927 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5928 {
92af500d 5929 if (flags & tf_error)
8251199e 5930 error ("not enough type information");
8d08fdba
MS
5931 return error_mark_node;
5932 }
5933
5934 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
abff8e06 5935 {
8f4b394d 5936 if (same_type_p (lhstype, TREE_TYPE (rhs)))
abff8e06 5937 return rhs;
a723baf1
MM
5938 if (flag_ms_extensions
5939 && TYPE_PTRMEMFUNC_P (lhstype)
5940 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
5941 /* Microsoft allows `A::f' to be resolved to a
5942 pointer-to-member. */
5943 ;
5944 else
5945 {
92af500d 5946 if (flags & tf_error)
a723baf1
MM
5947 error ("argument of type `%T' does not match `%T'",
5948 TREE_TYPE (rhs), lhstype);
5949 return error_mark_node;
5950 }
abff8e06 5951 }
8d08fdba 5952
50ad9642
MM
5953 if (TREE_CODE (rhs) == BASELINK)
5954 rhs = BASELINK_FUNCTIONS (rhs);
5955
2c73f9f5
ML
5956 /* We don't overwrite rhs if it is an overloaded function.
5957 Copying it would destroy the tree link. */
5958 if (TREE_CODE (rhs) != OVERLOAD)
5959 rhs = copy_node (rhs);
c73964b2 5960
8d08fdba
MS
5961 /* This should really only be used when attempting to distinguish
5962 what sort of a pointer to function we have. For now, any
5963 arithmetic operation which is not supported on pointers
5964 is rejected as an error. */
5965
5966 switch (TREE_CODE (rhs))
5967 {
5968 case TYPE_EXPR:
5969 case CONVERT_EXPR:
5970 case SAVE_EXPR:
5971 case CONSTRUCTOR:
8dc2b103 5972 gcc_unreachable ();
8d08fdba
MS
5973
5974 case INDIRECT_REF:
5975 case ARRAY_REF:
ec255269
MS
5976 {
5977 tree new_rhs;
8d08fdba 5978
ec255269 5979 new_rhs = instantiate_type (build_pointer_type (lhstype),
940ff223 5980 TREE_OPERAND (rhs, 0), flags);
ec255269
MS
5981 if (new_rhs == error_mark_node)
5982 return error_mark_node;
5983
5984 TREE_TYPE (rhs) = lhstype;
5985 TREE_OPERAND (rhs, 0) = new_rhs;
5986 return rhs;
5987 }
8d08fdba
MS
5988
5989 case NOP_EXPR:
5990 rhs = copy_node (TREE_OPERAND (rhs, 0));
5991 TREE_TYPE (rhs) = unknown_type_node;
940ff223 5992 return instantiate_type (lhstype, rhs, flags);
8d08fdba
MS
5993
5994 case COMPONENT_REF:
92af500d
NS
5995 {
5996 tree addr = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5997
5998 if (addr != error_mark_node
5999 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
04c06002 6000 /* Do not lose object's side effects. */
f293ce4b
RS
6001 addr = build2 (COMPOUND_EXPR, TREE_TYPE (addr),
6002 TREE_OPERAND (rhs, 0), addr);
92af500d
NS
6003 return addr;
6004 }
8d08fdba 6005
2a238a97 6006 case OFFSET_REF:
05e0b2f4
JM
6007 rhs = TREE_OPERAND (rhs, 1);
6008 if (BASELINK_P (rhs))
92af500d 6009 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags_in);
05e0b2f4 6010
2a238a97
MM
6011 /* This can happen if we are forming a pointer-to-member for a
6012 member template. */
50bc768d 6013 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
05e0b2f4 6014
2a238a97 6015 /* Fall through. */
874503bc 6016
386b8a85 6017 case TEMPLATE_ID_EXPR:
2bdb0643
JM
6018 {
6019 tree fns = TREE_OPERAND (rhs, 0);
6020 tree args = TREE_OPERAND (rhs, 1);
6021
19420d00 6022 return
92af500d
NS
6023 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
6024 /*template_only=*/true,
2bdb0643 6025 args);
2bdb0643 6026 }
386b8a85 6027
2c73f9f5 6028 case OVERLOAD:
a723baf1 6029 case FUNCTION_DECL:
104bf76a 6030 return
92af500d
NS
6031 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
6032 /*template_only=*/false,
104bf76a 6033 /*explicit_targs=*/NULL_TREE);
2c73f9f5
ML
6034
6035 case TREE_LIST:
00a17e31 6036 /* Now we should have a baselink. */
50bc768d 6037 gcc_assert (BASELINK_P (rhs));
e5966228 6038
da15dae6 6039 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
8d08fdba
MS
6040
6041 case CALL_EXPR:
6042 /* This is too hard for now. */
8dc2b103 6043 gcc_unreachable ();
8d08fdba
MS
6044
6045 case PLUS_EXPR:
6046 case MINUS_EXPR:
6047 case COMPOUND_EXPR:
a0a33927 6048 TREE_OPERAND (rhs, 0)
940ff223 6049 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
8d08fdba
MS
6050 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6051 return error_mark_node;
a0a33927 6052 TREE_OPERAND (rhs, 1)
940ff223 6053 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
8d08fdba
MS
6054 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6055 return error_mark_node;
6056
6057 TREE_TYPE (rhs) = lhstype;
6058 return rhs;
6059
6060 case MULT_EXPR:
6061 case TRUNC_DIV_EXPR:
6062 case FLOOR_DIV_EXPR:
6063 case CEIL_DIV_EXPR:
6064 case ROUND_DIV_EXPR:
6065 case RDIV_EXPR:
6066 case TRUNC_MOD_EXPR:
6067 case FLOOR_MOD_EXPR:
6068 case CEIL_MOD_EXPR:
6069 case ROUND_MOD_EXPR:
6070 case FIX_ROUND_EXPR:
6071 case FIX_FLOOR_EXPR:
6072 case FIX_CEIL_EXPR:
6073 case FIX_TRUNC_EXPR:
6074 case FLOAT_EXPR:
6075 case NEGATE_EXPR:
6076 case ABS_EXPR:
6077 case MAX_EXPR:
6078 case MIN_EXPR:
8d08fdba
MS
6079
6080 case BIT_AND_EXPR:
6081 case BIT_IOR_EXPR:
6082 case BIT_XOR_EXPR:
6083 case LSHIFT_EXPR:
6084 case RSHIFT_EXPR:
6085 case LROTATE_EXPR:
6086 case RROTATE_EXPR:
6087
6088 case PREINCREMENT_EXPR:
6089 case PREDECREMENT_EXPR:
6090 case POSTINCREMENT_EXPR:
6091 case POSTDECREMENT_EXPR:
92af500d 6092 if (flags & tf_error)
8251199e 6093 error ("invalid operation on uninstantiated type");
8d08fdba
MS
6094 return error_mark_node;
6095
6096 case TRUTH_AND_EXPR:
6097 case TRUTH_OR_EXPR:
6098 case TRUTH_XOR_EXPR:
6099 case LT_EXPR:
6100 case LE_EXPR:
6101 case GT_EXPR:
6102 case GE_EXPR:
6103 case EQ_EXPR:
6104 case NE_EXPR:
6105 case TRUTH_ANDIF_EXPR:
6106 case TRUTH_ORIF_EXPR:
6107 case TRUTH_NOT_EXPR:
92af500d 6108 if (flags & tf_error)
8251199e 6109 error ("not enough type information");
8d08fdba
MS
6110 return error_mark_node;
6111
ca36f057
MM
6112 case COND_EXPR:
6113 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6114 {
92af500d 6115 if (flags & tf_error)
ca36f057
MM
6116 error ("not enough type information");
6117 return error_mark_node;
6118 }
6119 TREE_OPERAND (rhs, 1)
6120 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6121 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6122 return error_mark_node;
6123 TREE_OPERAND (rhs, 2)
6124 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6125 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6126 return error_mark_node;
6127
6128 TREE_TYPE (rhs) = lhstype;
6129 return rhs;
6130
6131 case MODIFY_EXPR:
6132 TREE_OPERAND (rhs, 1)
6133 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6134 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6135 return error_mark_node;
6136
6137 TREE_TYPE (rhs) = lhstype;
6138 return rhs;
6139
6140 case ADDR_EXPR:
19420d00
NS
6141 {
6142 if (PTRMEM_OK_P (rhs))
c2ea3a40 6143 flags |= tf_ptrmem_ok;
19420d00 6144
ca36f057 6145 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
19420d00 6146 }
ca36f057
MM
6147
6148 case ERROR_MARK:
6149 return error_mark_node;
6150
6151 default:
8dc2b103 6152 gcc_unreachable ();
ca36f057 6153 }
8dc2b103 6154 return error_mark_node;
ca36f057
MM
6155}
6156\f
6157/* Return the name of the virtual function pointer field
6158 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6159 this may have to look back through base types to find the
6160 ultimate field name. (For single inheritance, these could
6161 all be the same name. Who knows for multiple inheritance). */
6162
6163static tree
94edc4ab 6164get_vfield_name (tree type)
ca36f057 6165{
37a247a0 6166 tree binfo, base_binfo;
ca36f057
MM
6167 char *buf;
6168
37a247a0 6169 for (binfo = TYPE_BINFO (type);
fa743e8c 6170 BINFO_N_BASE_BINFOS (binfo);
37a247a0
NS
6171 binfo = base_binfo)
6172 {
6173 base_binfo = BINFO_BASE_BINFO (binfo, 0);
ca36f057 6174
37a247a0
NS
6175 if (BINFO_VIRTUAL_P (base_binfo)
6176 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
6177 break;
6178 }
6179
ca36f057 6180 type = BINFO_TYPE (binfo);
c68b0a84 6181 buf = alloca (sizeof (VFIELD_NAME_FORMAT) + TYPE_NAME_LENGTH (type) + 2);
ea122333
JM
6182 sprintf (buf, VFIELD_NAME_FORMAT,
6183 IDENTIFIER_POINTER (constructor_name (type)));
ca36f057
MM
6184 return get_identifier (buf);
6185}
6186
6187void
94edc4ab 6188print_class_statistics (void)
ca36f057
MM
6189{
6190#ifdef GATHER_STATISTICS
6191 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6192 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
ca36f057
MM
6193 if (n_vtables)
6194 {
6195 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6196 n_vtables, n_vtable_searches);
6197 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6198 n_vtable_entries, n_vtable_elems);
6199 }
6200#endif
6201}
6202
6203/* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6204 according to [class]:
6205 The class-name is also inserted
6206 into the scope of the class itself. For purposes of access checking,
6207 the inserted class name is treated as if it were a public member name. */
6208
6209void
94edc4ab 6210build_self_reference (void)
ca36f057
MM
6211{
6212 tree name = constructor_name (current_class_type);
6213 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6214 tree saved_cas;
6215
6216 DECL_NONLOCAL (value) = 1;
6217 DECL_CONTEXT (value) = current_class_type;
6218 DECL_ARTIFICIAL (value) = 1;
a3d87771 6219 SET_DECL_SELF_REFERENCE_P (value);
ca36f057
MM
6220
6221 if (processing_template_decl)
6222 value = push_template_decl (value);
6223
6224 saved_cas = current_access_specifier;
6225 current_access_specifier = access_public_node;
6226 finish_member_declaration (value);
6227 current_access_specifier = saved_cas;
6228}
6229
6230/* Returns 1 if TYPE contains only padding bytes. */
6231
6232int
94edc4ab 6233is_empty_class (tree type)
ca36f057 6234{
ca36f057
MM
6235 if (type == error_mark_node)
6236 return 0;
6237
6238 if (! IS_AGGR_TYPE (type))
6239 return 0;
6240
58731fd1
MM
6241 /* In G++ 3.2, whether or not a class was empty was determined by
6242 looking at its size. */
6243 if (abi_version_at_least (2))
6244 return CLASSTYPE_EMPTY_P (type);
6245 else
6246 return integer_zerop (CLASSTYPE_SIZE (type));
ca36f057
MM
6247}
6248
956d9305
MM
6249/* Returns true if TYPE contains an empty class. */
6250
6251static bool
6252contains_empty_class_p (tree type)
6253{
6254 if (is_empty_class (type))
6255 return true;
6256 if (CLASS_TYPE_P (type))
6257 {
6258 tree field;
fa743e8c
NS
6259 tree binfo;
6260 tree base_binfo;
956d9305
MM
6261 int i;
6262
fa743e8c
NS
6263 for (binfo = TYPE_BINFO (type), i = 0;
6264 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6265 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
956d9305
MM
6266 return true;
6267 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
17bbb839
MM
6268 if (TREE_CODE (field) == FIELD_DECL
6269 && !DECL_ARTIFICIAL (field)
6270 && is_empty_class (TREE_TYPE (field)))
956d9305
MM
6271 return true;
6272 }
6273 else if (TREE_CODE (type) == ARRAY_TYPE)
6274 return contains_empty_class_p (TREE_TYPE (type));
6275 return false;
6276}
6277
ca36f057
MM
6278/* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6279 a *_TYPE node. NODE can also be a local class. */
6280
6281tree
94edc4ab 6282get_enclosing_class (tree type)
ca36f057
MM
6283{
6284 tree node = type;
6285
6286 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6287 {
6288 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6289 {
6290 case 'd':
6291 node = DECL_CONTEXT (node);
6292 break;
6293
6294 case 't':
6295 if (node != type)
6296 return node;
6297 node = TYPE_CONTEXT (node);
6298 break;
6299
6300 default:
8dc2b103 6301 gcc_unreachable ();
ca36f057
MM
6302 }
6303 }
6304 return NULL_TREE;
6305}
6306
ca36f057
MM
6307/* Note that NAME was looked up while the current class was being
6308 defined and that the result of that lookup was DECL. */
6309
6310void
94edc4ab 6311maybe_note_name_used_in_class (tree name, tree decl)
ca36f057
MM
6312{
6313 splay_tree names_used;
6314
6315 /* If we're not defining a class, there's nothing to do. */
39fb05d0
MM
6316 if (!(innermost_scope_kind() == sk_class
6317 && TYPE_BEING_DEFINED (current_class_type)))
ca36f057
MM
6318 return;
6319
6320 /* If there's already a binding for this NAME, then we don't have
6321 anything to worry about. */
39fb05d0
MM
6322 if (lookup_member (current_class_type, name,
6323 /*protect=*/0, /*want_type=*/false))
ca36f057
MM
6324 return;
6325
6326 if (!current_class_stack[current_class_depth - 1].names_used)
6327 current_class_stack[current_class_depth - 1].names_used
6328 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6329 names_used = current_class_stack[current_class_depth - 1].names_used;
6330
6331 splay_tree_insert (names_used,
6332 (splay_tree_key) name,
6333 (splay_tree_value) decl);
6334}
6335
6336/* Note that NAME was declared (as DECL) in the current class. Check
0e339752 6337 to see that the declaration is valid. */
ca36f057
MM
6338
6339void
94edc4ab 6340note_name_declared_in_class (tree name, tree decl)
ca36f057
MM
6341{
6342 splay_tree names_used;
6343 splay_tree_node n;
6344
6345 /* Look to see if we ever used this name. */
6346 names_used
6347 = current_class_stack[current_class_depth - 1].names_used;
6348 if (!names_used)
6349 return;
6350
6351 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6352 if (n)
6353 {
6354 /* [basic.scope.class]
6355
6356 A name N used in a class S shall refer to the same declaration
6357 in its context and when re-evaluated in the completed scope of
6358 S. */
33bd39a2 6359 error ("declaration of `%#D'", decl);
38da6039
NS
6360 cp_error_at ("changes meaning of `%D' from `%+#D'",
6361 DECL_NAME (OVL_CURRENT (decl)),
ca36f057
MM
6362 (tree) n->value);
6363 }
6364}
6365
3461fba7
NS
6366/* Returns the VAR_DECL for the complete vtable associated with BINFO.
6367 Secondary vtables are merged with primary vtables; this function
6368 will return the VAR_DECL for the primary vtable. */
ca36f057 6369
c35cce41 6370tree
94edc4ab 6371get_vtbl_decl_for_binfo (tree binfo)
c35cce41
MM
6372{
6373 tree decl;
6374
6375 decl = BINFO_VTABLE (binfo);
6376 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6377 {
50bc768d 6378 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
c35cce41
MM
6379 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6380 }
6381 if (decl)
50bc768d 6382 gcc_assert (TREE_CODE (decl) == VAR_DECL);
c35cce41
MM
6383 return decl;
6384}
6385
911a71a7 6386
dbbf88d1
NS
6387/* Returns the binfo for the primary base of BINFO. If the resulting
6388 BINFO is a virtual base, and it is inherited elsewhere in the
6389 hierarchy, then the returned binfo might not be the primary base of
6390 BINFO in the complete object. Check BINFO_PRIMARY_P or
6391 BINFO_LOST_PRIMARY_P to be sure. */
911a71a7
MM
6392
6393tree
94edc4ab 6394get_primary_binfo (tree binfo)
911a71a7
MM
6395{
6396 tree primary_base;
dbbf88d1 6397 tree result;
623fe76a 6398
911a71a7
MM
6399 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6400 if (!primary_base)
6401 return NULL_TREE;
6402
dbbf88d1 6403 result = copied_binfo (primary_base, binfo);
911a71a7
MM
6404 return result;
6405}
6406
838dfd8a 6407/* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
b7442fb5
NS
6408
6409static int
94edc4ab 6410maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
b7442fb5
NS
6411{
6412 if (!indented_p)
6413 fprintf (stream, "%*s", indent, "");
6414 return 1;
6415}
6416
dbbf88d1
NS
6417/* Dump the offsets of all the bases rooted at BINFO to STREAM.
6418 INDENT should be zero when called from the top level; it is
6419 incremented recursively. IGO indicates the next expected BINFO in
9bcb9aae 6420 inheritance graph ordering. */
c35cce41 6421
dbbf88d1
NS
6422static tree
6423dump_class_hierarchy_r (FILE *stream,
94edc4ab 6424 int flags,
94edc4ab 6425 tree binfo,
dbbf88d1 6426 tree igo,
94edc4ab 6427 int indent)
ca36f057 6428{
b7442fb5 6429 int indented = 0;
fa743e8c
NS
6430 tree base_binfo;
6431 int i;
b7442fb5
NS
6432
6433 indented = maybe_indent_hierarchy (stream, indent, 0);
6434 fprintf (stream, "%s (0x%lx) ",
fc6633e0 6435 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
b7442fb5 6436 (unsigned long) binfo);
dbbf88d1
NS
6437 if (binfo != igo)
6438 {
6439 fprintf (stream, "alternative-path\n");
6440 return igo;
6441 }
6442 igo = TREE_CHAIN (binfo);
6443
9965d119 6444 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
ca36f057 6445 tree_low_cst (BINFO_OFFSET (binfo), 0));
9965d119
NS
6446 if (is_empty_class (BINFO_TYPE (binfo)))
6447 fprintf (stream, " empty");
6448 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6449 fprintf (stream, " nearly-empty");
809e3e7f 6450 if (BINFO_VIRTUAL_P (binfo))
dbbf88d1 6451 fprintf (stream, " virtual");
9965d119 6452 fprintf (stream, "\n");
ca36f057 6453
b7442fb5 6454 indented = 0;
fc6633e0 6455 if (BINFO_PRIMARY_P (binfo))
b7442fb5
NS
6456 {
6457 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6458 fprintf (stream, " primary-for %s (0x%lx)",
fc6633e0 6459 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
b7442fb5 6460 TFF_PLAIN_IDENTIFIER),
fc6633e0 6461 (unsigned long)BINFO_INHERITANCE_CHAIN (binfo));
b7442fb5
NS
6462 }
6463 if (BINFO_LOST_PRIMARY_P (binfo))
6464 {
6465 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6466 fprintf (stream, " lost-primary");
6467 }
6468 if (indented)
6469 fprintf (stream, "\n");
6470
6471 if (!(flags & TDF_SLIM))
6472 {
6473 int indented = 0;
6474
6475 if (BINFO_SUBVTT_INDEX (binfo))
6476 {
6477 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6478 fprintf (stream, " subvttidx=%s",
6479 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6480 TFF_PLAIN_IDENTIFIER));
6481 }
6482 if (BINFO_VPTR_INDEX (binfo))
6483 {
6484 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6485 fprintf (stream, " vptridx=%s",
6486 expr_as_string (BINFO_VPTR_INDEX (binfo),
6487 TFF_PLAIN_IDENTIFIER));
6488 }
6489 if (BINFO_VPTR_FIELD (binfo))
6490 {
6491 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6492 fprintf (stream, " vbaseoffset=%s",
6493 expr_as_string (BINFO_VPTR_FIELD (binfo),
6494 TFF_PLAIN_IDENTIFIER));
6495 }
6496 if (BINFO_VTABLE (binfo))
6497 {
6498 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6499 fprintf (stream, " vptr=%s",
6500 expr_as_string (BINFO_VTABLE (binfo),
6501 TFF_PLAIN_IDENTIFIER));
6502 }
6503
6504 if (indented)
6505 fprintf (stream, "\n");
6506 }
dbbf88d1 6507
fa743e8c
NS
6508 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
6509 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
dbbf88d1
NS
6510
6511 return igo;
c35cce41
MM
6512}
6513
6514/* Dump the BINFO hierarchy for T. */
6515
b7442fb5 6516static void
bb885938 6517dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
c35cce41 6518{
b7442fb5
NS
6519 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6520 fprintf (stream, " size=%lu align=%lu\n",
6521 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6522 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
dbbf88d1
NS
6523 fprintf (stream, " base size=%lu base align=%lu\n",
6524 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
6525 / BITS_PER_UNIT),
6526 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
6527 / BITS_PER_UNIT));
6528 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
b7442fb5 6529 fprintf (stream, "\n");
bb885938
NS
6530}
6531
da1d7781 6532/* Debug interface to hierarchy dumping. */
bb885938
NS
6533
6534extern void
6535debug_class (tree t)
6536{
6537 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
6538}
6539
6540static void
6541dump_class_hierarchy (tree t)
6542{
6543 int flags;
6544 FILE *stream = dump_begin (TDI_class, &flags);
6545
6546 if (stream)
6547 {
6548 dump_class_hierarchy_1 (stream, flags, t);
6549 dump_end (TDI_class, stream);
6550 }
b7442fb5
NS
6551}
6552
6553static void
94edc4ab 6554dump_array (FILE * stream, tree decl)
b7442fb5
NS
6555{
6556 tree inits;
6557 int ix;
6558 HOST_WIDE_INT elt;
6559 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6560
6561 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6562 / BITS_PER_UNIT);
6563 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6564 fprintf (stream, " %s entries",
6565 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6566 TFF_PLAIN_IDENTIFIER));
6567 fprintf (stream, "\n");
6568
522801e7 6569 for (ix = 0, inits = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
b7442fb5 6570 inits; ix++, inits = TREE_CHAIN (inits))
4fdc14ca 6571 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
b7442fb5
NS
6572 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6573}
6574
6575static void
94edc4ab 6576dump_vtable (tree t, tree binfo, tree vtable)
b7442fb5
NS
6577{
6578 int flags;
6579 FILE *stream = dump_begin (TDI_class, &flags);
6580
6581 if (!stream)
6582 return;
6583
6584 if (!(flags & TDF_SLIM))
9965d119 6585 {
b7442fb5 6586 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
9965d119 6587
b7442fb5
NS
6588 fprintf (stream, "%s for %s",
6589 ctor_vtbl_p ? "Construction vtable" : "Vtable",
fc6633e0 6590 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
b7442fb5
NS
6591 if (ctor_vtbl_p)
6592 {
809e3e7f 6593 if (!BINFO_VIRTUAL_P (binfo))
b7442fb5
NS
6594 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6595 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6596 }
6597 fprintf (stream, "\n");
6598 dump_array (stream, vtable);
6599 fprintf (stream, "\n");
9965d119 6600 }
b7442fb5
NS
6601
6602 dump_end (TDI_class, stream);
6603}
6604
6605static void
94edc4ab 6606dump_vtt (tree t, tree vtt)
b7442fb5
NS
6607{
6608 int flags;
6609 FILE *stream = dump_begin (TDI_class, &flags);
6610
6611 if (!stream)
6612 return;
6613
6614 if (!(flags & TDF_SLIM))
6615 {
6616 fprintf (stream, "VTT for %s\n",
6617 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6618 dump_array (stream, vtt);
6619 fprintf (stream, "\n");
6620 }
6621
6622 dump_end (TDI_class, stream);
ca36f057
MM
6623}
6624
bb885938
NS
6625/* Dump a function or thunk and its thunkees. */
6626
6627static void
6628dump_thunk (FILE *stream, int indent, tree thunk)
6629{
6630 static const char spaces[] = " ";
6631 tree name = DECL_NAME (thunk);
6632 tree thunks;
6633
6634 fprintf (stream, "%.*s%p %s %s", indent, spaces,
6635 (void *)thunk,
6636 !DECL_THUNK_P (thunk) ? "function"
6637 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
6638 name ? IDENTIFIER_POINTER (name) : "<unset>");
e00853fd 6639 if (DECL_THUNK_P (thunk))
bb885938
NS
6640 {
6641 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
6642 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
6643
6644 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
6645 if (!virtual_adjust)
6646 /*NOP*/;
6647 else if (DECL_THIS_THUNK_P (thunk))
6648 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
6649 tree_low_cst (virtual_adjust, 0));
6650 else
6651 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
6652 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
6653 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
e00853fd
NS
6654 if (THUNK_ALIAS (thunk))
6655 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
bb885938
NS
6656 }
6657 fprintf (stream, "\n");
6658 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
6659 dump_thunk (stream, indent + 2, thunks);
6660}
6661
6662/* Dump the thunks for FN. */
6663
6664extern void
6665debug_thunks (tree fn)
6666{
6667 dump_thunk (stderr, 0, fn);
6668}
6669
ca36f057
MM
6670/* Virtual function table initialization. */
6671
6672/* Create all the necessary vtables for T and its base classes. */
6673
6674static void
94edc4ab 6675finish_vtbls (tree t)
ca36f057 6676{
3461fba7
NS
6677 tree list;
6678 tree vbase;
ca36f057 6679
3461fba7
NS
6680 /* We lay out the primary and secondary vtables in one contiguous
6681 vtable. The primary vtable is first, followed by the non-virtual
6682 secondary vtables in inheritance graph order. */
604a3205 6683 list = build_tree_list (BINFO_VTABLE (TYPE_BINFO (t)), NULL_TREE);
3461fba7
NS
6684 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6685 TYPE_BINFO (t), t, list);
6686
6687 /* Then come the virtual bases, also in inheritance graph order. */
6688 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6689 {
809e3e7f 6690 if (!BINFO_VIRTUAL_P (vbase))
3461fba7 6691 continue;
dbbf88d1 6692 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), t, list);
ff668506
JM
6693 }
6694
604a3205 6695 if (BINFO_VTABLE (TYPE_BINFO (t)))
3461fba7 6696 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
ca36f057
MM
6697}
6698
6699/* Initialize the vtable for BINFO with the INITS. */
6700
6701static void
94edc4ab 6702initialize_vtable (tree binfo, tree inits)
ca36f057 6703{
ca36f057
MM
6704 tree decl;
6705
6706 layout_vtable_decl (binfo, list_length (inits));
c35cce41 6707 decl = get_vtbl_decl_for_binfo (binfo);
19c29b2f 6708 initialize_artificial_var (decl, inits);
b7442fb5 6709 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
23656158
MM
6710}
6711
9965d119
NS
6712/* Build the VTT (virtual table table) for T.
6713 A class requires a VTT if it has virtual bases.
6714
6715 This holds
6716 1 - primary virtual pointer for complete object T
90ecce3e
JM
6717 2 - secondary VTTs for each direct non-virtual base of T which requires a
6718 VTT
9965d119
NS
6719 3 - secondary virtual pointers for each direct or indirect base of T which
6720 has virtual bases or is reachable via a virtual path from T.
6721 4 - secondary VTTs for each direct or indirect virtual base of T.
6722
6723 Secondary VTTs look like complete object VTTs without part 4. */
23656158
MM
6724
6725static void
94edc4ab 6726build_vtt (tree t)
23656158
MM
6727{
6728 tree inits;
6729 tree type;
6730 tree vtt;
3ec6bad3 6731 tree index;
23656158 6732
23656158
MM
6733 /* Build up the initializers for the VTT. */
6734 inits = NULL_TREE;
3ec6bad3 6735 index = size_zero_node;
9965d119 6736 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
23656158
MM
6737
6738 /* If we didn't need a VTT, we're done. */
6739 if (!inits)
6740 return;
6741
6742 /* Figure out the type of the VTT. */
442e01b6 6743 type = build_index_type (size_int (list_length (inits) - 1));
23656158
MM
6744 type = build_cplus_array_type (const_ptr_type_node, type);
6745
6746 /* Now, build the VTT object itself. */
669ec2b4 6747 vtt = build_vtable (t, get_vtt_name (t), type);
19c29b2f 6748 initialize_artificial_var (vtt, inits);
548502d3
MM
6749 /* Add the VTT to the vtables list. */
6750 TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
6751 TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
b7442fb5
NS
6752
6753 dump_vtt (t, vtt);
23656158
MM
6754}
6755
13de7ec4
JM
6756/* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
6757 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
6758 and CHAIN the vtable pointer for this binfo after construction is
00a17e31 6759 complete. VALUE can also be another BINFO, in which case we recurse. */
13de7ec4
JM
6760
6761static tree
94edc4ab 6762binfo_ctor_vtable (tree binfo)
13de7ec4
JM
6763{
6764 tree vt;
6765
6766 while (1)
6767 {
6768 vt = BINFO_VTABLE (binfo);
6769 if (TREE_CODE (vt) == TREE_LIST)
6770 vt = TREE_VALUE (vt);
95b4aca6 6771 if (TREE_CODE (vt) == TREE_BINFO)
13de7ec4
JM
6772 binfo = vt;
6773 else
6774 break;
6775 }
6776
6777 return vt;
6778}
6779
23656158 6780/* Recursively build the VTT-initializer for BINFO (which is in the
9965d119
NS
6781 hierarchy dominated by T). INITS points to the end of the initializer
6782 list to date. INDEX is the VTT index where the next element will be
6783 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
6784 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
6785 for virtual bases of T. When it is not so, we build the constructor
6786 vtables for the BINFO-in-T variant. */
23656158
MM
6787
6788static tree *
94edc4ab 6789build_vtt_inits (tree binfo, tree t, tree* inits, tree* index)
23656158
MM
6790{
6791 int i;
6792 tree b;
6793 tree init;
6794 tree secondary_vptrs;
9965d119 6795 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
23656158
MM
6796
6797 /* We only need VTTs for subobjects with virtual bases. */
6798 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
6799 return inits;
6800
6801 /* We need to use a construction vtable if this is not the primary
6802 VTT. */
9965d119 6803 if (!top_level_p)
3ec6bad3
MM
6804 {
6805 build_ctor_vtbl_group (binfo, t);
6806
6807 /* Record the offset in the VTT where this sub-VTT can be found. */
6808 BINFO_SUBVTT_INDEX (binfo) = *index;
6809 }
23656158
MM
6810
6811 /* Add the address of the primary vtable for the complete object. */
13de7ec4 6812 init = binfo_ctor_vtable (binfo);
23656158
MM
6813 *inits = build_tree_list (NULL_TREE, init);
6814 inits = &TREE_CHAIN (*inits);
9965d119
NS
6815 if (top_level_p)
6816 {
50bc768d 6817 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9965d119
NS
6818 BINFO_VPTR_INDEX (binfo) = *index;
6819 }
3ec6bad3
MM
6820 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
6821
23656158 6822 /* Recursively add the secondary VTTs for non-virtual bases. */
fa743e8c
NS
6823 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
6824 if (!BINFO_VIRTUAL_P (b))
6825 inits = build_vtt_inits (BINFO_BASE_BINFO (binfo, i), t, inits, index);
3ec6bad3 6826
23656158 6827 /* Add secondary virtual pointers for all subobjects of BINFO with
9965d119
NS
6828 either virtual bases or reachable along a virtual path, except
6829 subobjects that are non-virtual primary bases. */
9ccf6541 6830 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
3ec6bad3 6831 TREE_TYPE (secondary_vptrs) = *index;
9965d119
NS
6832 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
6833 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
6834
23656158 6835 dfs_walk_real (binfo,
9ccf6541 6836 dfs_build_secondary_vptr_vtt_inits,
23656158 6837 NULL,
9965d119 6838 dfs_ctor_vtable_bases_queue_p,
23656158 6839 secondary_vptrs);
9965d119
NS
6840 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
6841 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
6842 secondary_vptrs);
6843
3ec6bad3 6844 *index = TREE_TYPE (secondary_vptrs);
23656158
MM
6845
6846 /* The secondary vptrs come back in reverse order. After we reverse
6847 them, and add the INITS, the last init will be the first element
6848 of the chain. */
6849 secondary_vptrs = TREE_VALUE (secondary_vptrs);
6850 if (secondary_vptrs)
6851 {
6852 *inits = nreverse (secondary_vptrs);
6853 inits = &TREE_CHAIN (secondary_vptrs);
50bc768d 6854 gcc_assert (*inits == NULL_TREE);
23656158
MM
6855 }
6856
6857 /* Add the secondary VTTs for virtual bases. */
9965d119 6858 if (top_level_p)
9ccf6541
MM
6859 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
6860 {
809e3e7f 6861 if (!BINFO_VIRTUAL_P (b))
9ccf6541
MM
6862 continue;
6863
dbbf88d1 6864 inits = build_vtt_inits (b, t, inits, index);
9ccf6541 6865 }
23656158 6866
9965d119
NS
6867 if (!top_level_p)
6868 {
6869 tree data = tree_cons (t, binfo, NULL_TREE);
6870 VTT_TOP_LEVEL_P (data) = 0;
6871 VTT_MARKED_BINFO_P (data) = 0;
6872
6873 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
6874 dfs_ctor_vtable_bases_queue_p,
6875 data);
6876 }
23656158
MM
6877
6878 return inits;
6879}
6880
8df83eae
RK
6881/* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
6882 in most derived. DATA is a TREE_LIST who's TREE_CHAIN is the type of the
6883 base being constructed whilst this secondary vptr is live. The
9965d119 6884 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
23656158
MM
6885
6886static tree
8df83eae 6887dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data)
23656158
MM
6888{
6889 tree l;
6890 tree t;
6891 tree init;
3ec6bad3 6892 tree index;
9965d119 6893 int top_level_p;
23656158
MM
6894
6895 l = (tree) data;
9ccf6541 6896 t = TREE_CHAIN (l);
9965d119
NS
6897 top_level_p = VTT_TOP_LEVEL_P (l);
6898
dbbf88d1 6899 BINFO_MARKED (binfo) = 1;
23656158
MM
6900
6901 /* We don't care about bases that don't have vtables. */
6902 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
6903 return NULL_TREE;
6904
6905 /* We're only interested in proper subobjects of T. */
6906 if (same_type_p (BINFO_TYPE (binfo), t))
6907 return NULL_TREE;
6908
6909 /* We're not interested in non-virtual primary bases. */
809e3e7f 6910 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
23656158
MM
6911 return NULL_TREE;
6912
9965d119
NS
6913 /* If BINFO has virtual bases or is reachable via a virtual path
6914 from T, it'll have a secondary vptr. */
db3d8cde 6915 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
9965d119 6916 && !binfo_via_virtual (binfo, t))
db3d8cde 6917 return NULL_TREE;
23656158 6918
3ec6bad3
MM
6919 /* Record the index where this secondary vptr can be found. */
6920 index = TREE_TYPE (l);
9965d119
NS
6921 if (top_level_p)
6922 {
50bc768d 6923 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9965d119
NS
6924 BINFO_VPTR_INDEX (binfo) = index;
6925 }
3ec6bad3
MM
6926 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
6927 TYPE_SIZE_UNIT (ptr_type_node));
6928
6929 /* Add the initializer for the secondary vptr itself. */
809e3e7f 6930 if (top_level_p && BINFO_VIRTUAL_P (binfo))
9965d119
NS
6931 {
6932 /* It's a primary virtual base, and this is not the construction
6933 vtable. Find the base this is primary of in the inheritance graph,
00a17e31 6934 and use that base's vtable now. */
fc6633e0
NS
6935 while (BINFO_PRIMARY_P (binfo))
6936 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9965d119 6937 }
13de7ec4 6938 init = binfo_ctor_vtable (binfo);
23656158 6939 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
3ec6bad3 6940
23656158
MM
6941 return NULL_TREE;
6942}
6943
9965d119 6944/* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
b7ad2f8b
NS
6945 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
6946 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
6947 hierarchy. */
9965d119
NS
6948
6949static tree
dbbf88d1
NS
6950dfs_ctor_vtable_bases_queue_p (tree derived, int ix,
6951 void* data)
9965d119 6952{
604a3205 6953 tree binfo = BINFO_BASE_BINFO (derived, ix);
dbbf88d1 6954
9965d119
NS
6955 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
6956 return NULL_TREE;
6957 return binfo;
6958}
6959
6960/* Called from build_vtt_inits via dfs_walk. After building constructor
6961 vtables and generating the sub-vtt from them, we need to restore the
6962 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
6963 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
23656158
MM
6964
6965static tree
94edc4ab 6966dfs_fixup_binfo_vtbls (tree binfo, void* data)
23656158 6967{
dbbf88d1 6968 BINFO_MARKED (binfo) = 0;
23656158
MM
6969
6970 /* We don't care about bases that don't have vtables. */
6971 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
6972 return NULL_TREE;
6973
6974 /* If we scribbled the construction vtable vptr into BINFO, clear it
6975 out now. */
85a9a0a2
NS
6976 if (BINFO_VTABLE (binfo)
6977 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
9ccf6541
MM
6978 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
6979 == TREE_VALUE ((tree) data)))
6980 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
23656158
MM
6981
6982 return NULL_TREE;
6983}
6984
6985/* Build the construction vtable group for BINFO which is in the
6986 hierarchy dominated by T. */
6987
6988static void
94edc4ab 6989build_ctor_vtbl_group (tree binfo, tree t)
23656158
MM
6990{
6991 tree list;
6992 tree type;
6993 tree vtbl;
6994 tree inits;
6995 tree id;
9ccf6541 6996 tree vbase;
23656158 6997
7bdcf888 6998 /* See if we've already created this construction vtable group. */
1f84ec23 6999 id = mangle_ctor_vtbl_for_type (t, binfo);
23656158
MM
7000 if (IDENTIFIER_GLOBAL_VALUE (id))
7001 return;
7002
50bc768d 7003 gcc_assert (!same_type_p (BINFO_TYPE (binfo), t));
23656158
MM
7004 /* Build a version of VTBL (with the wrong type) for use in
7005 constructing the addresses of secondary vtables in the
7006 construction vtable group. */
459c43ad 7007 vtbl = build_vtable (t, id, ptr_type_node);
505970fc 7008 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
23656158
MM
7009 list = build_tree_list (vtbl, NULL_TREE);
7010 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7011 binfo, t, list);
9965d119
NS
7012
7013 /* Add the vtables for each of our virtual bases using the vbase in T
7014 binfo. */
7015 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9ccf6541
MM
7016 vbase;
7017 vbase = TREE_CHAIN (vbase))
7018 {
7019 tree b;
7020
809e3e7f 7021 if (!BINFO_VIRTUAL_P (vbase))
9ccf6541 7022 continue;
dbbf88d1 7023 b = copied_binfo (vbase, binfo);
9965d119 7024
dbbf88d1 7025 accumulate_vtbl_inits (b, vbase, binfo, t, list);
9ccf6541 7026 }
99389463 7027 inits = TREE_VALUE (list);
23656158
MM
7028
7029 /* Figure out the type of the construction vtable. */
442e01b6 7030 type = build_index_type (size_int (list_length (inits) - 1));
23656158
MM
7031 type = build_cplus_array_type (vtable_entry_type, type);
7032 TREE_TYPE (vtbl) = type;
7033
7034 /* Initialize the construction vtable. */
548502d3 7035 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
19c29b2f 7036 initialize_artificial_var (vtbl, inits);
b7442fb5 7037 dump_vtable (t, binfo, vtbl);
23656158
MM
7038}
7039
9965d119
NS
7040/* Add the vtbl initializers for BINFO (and its bases other than
7041 non-virtual primaries) to the list of INITS. BINFO is in the
7042 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7043 the constructor the vtbl inits should be accumulated for. (If this
7044 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7045 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7046 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7047 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7048 but are not necessarily the same in terms of layout. */
ca36f057
MM
7049
7050static void
94edc4ab
NN
7051accumulate_vtbl_inits (tree binfo,
7052 tree orig_binfo,
7053 tree rtti_binfo,
7054 tree t,
7055 tree inits)
ca36f057 7056{
23656158 7057 int i;
fa743e8c 7058 tree base_binfo;
9965d119 7059 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
23656158 7060
50bc768d 7061 gcc_assert (same_type_p (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
23656158 7062
00a17e31 7063 /* If it doesn't have a vptr, we don't do anything. */
623fe76a
NS
7064 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7065 return;
7066
23656158
MM
7067 /* If we're building a construction vtable, we're not interested in
7068 subobjects that don't require construction vtables. */
7069 if (ctor_vtbl_p
9ccf6541 7070 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
9965d119 7071 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
23656158
MM
7072 return;
7073
7074 /* Build the initializers for the BINFO-in-T vtable. */
7075 TREE_VALUE (inits)
7076 = chainon (TREE_VALUE (inits),
7077 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7078 rtti_binfo, t, inits));
7079
c35cce41
MM
7080 /* Walk the BINFO and its bases. We walk in preorder so that as we
7081 initialize each vtable we can figure out at what offset the
23656158
MM
7082 secondary vtable lies from the primary vtable. We can't use
7083 dfs_walk here because we need to iterate through bases of BINFO
7084 and RTTI_BINFO simultaneously. */
fa743e8c 7085 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
23656158 7086 {
23656158 7087 /* Skip virtual bases. */
809e3e7f 7088 if (BINFO_VIRTUAL_P (base_binfo))
23656158
MM
7089 continue;
7090 accumulate_vtbl_inits (base_binfo,
604a3205 7091 BINFO_BASE_BINFO (orig_binfo, i),
9965d119 7092 rtti_binfo, t,
23656158
MM
7093 inits);
7094 }
ca36f057
MM
7095}
7096
3461fba7
NS
7097/* Called from accumulate_vtbl_inits. Returns the initializers for
7098 the BINFO vtable. */
ca36f057
MM
7099
7100static tree
94edc4ab
NN
7101dfs_accumulate_vtbl_inits (tree binfo,
7102 tree orig_binfo,
7103 tree rtti_binfo,
7104 tree t,
7105 tree l)
ca36f057 7106{
23656158 7107 tree inits = NULL_TREE;
9965d119
NS
7108 tree vtbl = NULL_TREE;
7109 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7110
13de7ec4 7111 if (ctor_vtbl_p
809e3e7f 7112 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9965d119 7113 {
13de7ec4
JM
7114 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7115 primary virtual base. If it is not the same primary in
7116 the hierarchy of T, we'll need to generate a ctor vtable
7117 for it, to place at its location in T. If it is the same
7118 primary, we still need a VTT entry for the vtable, but it
7119 should point to the ctor vtable for the base it is a
7120 primary for within the sub-hierarchy of RTTI_BINFO.
7bdcf888 7121
13de7ec4 7122 There are three possible cases:
7bdcf888 7123
13de7ec4
JM
7124 1) We are in the same place.
7125 2) We are a primary base within a lost primary virtual base of
7126 RTTI_BINFO.
049d2def 7127 3) We are primary to something not a base of RTTI_BINFO. */
7bdcf888 7128
fc6633e0 7129 tree b;
13de7ec4 7130 tree last = NULL_TREE;
85a9a0a2 7131
13de7ec4
JM
7132 /* First, look through the bases we are primary to for RTTI_BINFO
7133 or a virtual base. */
fc6633e0
NS
7134 b = binfo;
7135 while (BINFO_PRIMARY_P (b))
7bdcf888 7136 {
fc6633e0 7137 b = BINFO_INHERITANCE_CHAIN (b);
13de7ec4 7138 last = b;
809e3e7f 7139 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
fc6633e0 7140 goto found;
7bdcf888 7141 }
13de7ec4
JM
7142 /* If we run out of primary links, keep looking down our
7143 inheritance chain; we might be an indirect primary. */
fc6633e0
NS
7144 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7145 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
7146 break;
7147 found:
7148
13de7ec4
JM
7149 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7150 base B and it is a base of RTTI_BINFO, this is case 2. In
7151 either case, we share our vtable with LAST, i.e. the
7152 derived-most base within B of which we are a primary. */
7153 if (b == rtti_binfo
58c42dc2 7154 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
049d2def
JM
7155 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7156 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7157 binfo_ctor_vtable after everything's been set up. */
7158 vtbl = last;
13de7ec4 7159
049d2def 7160 /* Otherwise, this is case 3 and we get our own. */
9965d119 7161 }
dbbf88d1 7162 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9965d119 7163 return inits;
7bdcf888 7164
9965d119 7165 if (!vtbl)
ca36f057 7166 {
c35cce41
MM
7167 tree index;
7168 int non_fn_entries;
7169
7170 /* Compute the initializer for this vtable. */
23656158 7171 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
aabb4cd6 7172 &non_fn_entries);
c35cce41 7173
23656158 7174 /* Figure out the position to which the VPTR should point. */
c35cce41 7175 vtbl = TREE_PURPOSE (l);
6de9cd9a 7176 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, vtbl);
c35cce41
MM
7177 index = size_binop (PLUS_EXPR,
7178 size_int (non_fn_entries),
7179 size_int (list_length (TREE_VALUE (l))));
23656158
MM
7180 index = size_binop (MULT_EXPR,
7181 TYPE_SIZE_UNIT (vtable_entry_type),
7182 index);
f293ce4b 7183 vtbl = build2 (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
9965d119 7184 }
23656158 7185
7bdcf888 7186 if (ctor_vtbl_p)
9965d119
NS
7187 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7188 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7189 straighten this out. */
7190 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
809e3e7f 7191 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
7bdcf888
NS
7192 inits = NULL_TREE;
7193 else
7194 /* For an ordinary vtable, set BINFO_VTABLE. */
7195 BINFO_VTABLE (binfo) = vtbl;
ca36f057 7196
23656158 7197 return inits;
ca36f057
MM
7198}
7199
90ecce3e 7200/* Construct the initializer for BINFO's virtual function table. BINFO
aabb4cd6 7201 is part of the hierarchy dominated by T. If we're building a
23656158 7202 construction vtable, the ORIG_BINFO is the binfo we should use to
9965d119
NS
7203 find the actual function pointers to put in the vtable - but they
7204 can be overridden on the path to most-derived in the graph that
7205 ORIG_BINFO belongs. Otherwise,
911a71a7 7206 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
23656158
MM
7207 BINFO that should be indicated by the RTTI information in the
7208 vtable; it will be a base class of T, rather than T itself, if we
7209 are building a construction vtable.
aabb4cd6
MM
7210
7211 The value returned is a TREE_LIST suitable for wrapping in a
7212 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7213 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
911a71a7
MM
7214 number of non-function entries in the vtable.
7215
7216 It might seem that this function should never be called with a
9965d119 7217 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
911a71a7 7218 base is always subsumed by a derived class vtable. However, when
9965d119 7219 we are building construction vtables, we do build vtables for
911a71a7
MM
7220 primary bases; we need these while the primary base is being
7221 constructed. */
ca36f057
MM
7222
7223static tree
94edc4ab
NN
7224build_vtbl_initializer (tree binfo,
7225 tree orig_binfo,
7226 tree t,
7227 tree rtti_binfo,
7228 int* non_fn_entries_p)
ca36f057 7229{
d0cd8b44 7230 tree v, b;
911a71a7 7231 tree vfun_inits;
911a71a7 7232 vtbl_init_data vid;
58c42dc2
NS
7233 unsigned ix;
7234 tree vbinfo;
9ba5ff0f
NS
7235 VEC (tree) *vbases;
7236
911a71a7 7237 /* Initialize VID. */
961192e1 7238 memset (&vid, 0, sizeof (vid));
911a71a7
MM
7239 vid.binfo = binfo;
7240 vid.derived = t;
73ea87d7 7241 vid.rtti_binfo = rtti_binfo;
911a71a7
MM
7242 vid.last_init = &vid.inits;
7243 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7244 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
548502d3 7245 vid.generate_vcall_entries = true;
c35cce41 7246 /* The first vbase or vcall offset is at index -3 in the vtable. */
ce552f75 7247 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
c35cce41 7248
9bab6c90 7249 /* Add entries to the vtable for RTTI. */
73ea87d7 7250 build_rtti_vtbl_entries (binfo, &vid);
9bab6c90 7251
b485e15b
MM
7252 /* Create an array for keeping track of the functions we've
7253 processed. When we see multiple functions with the same
7254 signature, we share the vcall offsets. */
7255 VARRAY_TREE_INIT (vid.fns, 32, "fns");
c35cce41 7256 /* Add the vcall and vbase offset entries. */
911a71a7 7257 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
58c42dc2 7258
79cda2d1 7259 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
c35cce41 7260 build_vbase_offset_vtbl_entries. */
9ba5ff0f
NS
7261 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
7262 VEC_iterate (tree, vbases, ix, vbinfo); ix++)
58c42dc2 7263 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
ca36f057 7264
a6f5e048
RH
7265 /* If the target requires padding between data entries, add that now. */
7266 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7267 {
7268 tree cur, *prev;
7269
7270 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7271 {
7272 tree add = cur;
7273 int i;
7274
7275 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7befdb9f 7276 add = tree_cons (NULL_TREE,
2e88ae22
SE
7277 build1 (NOP_EXPR, vtable_entry_type,
7278 null_pointer_node),
7befdb9f 7279 add);
a6f5e048
RH
7280 *prev = add;
7281 }
7282 }
7283
c35cce41 7284 if (non_fn_entries_p)
911a71a7 7285 *non_fn_entries_p = list_length (vid.inits);
ca36f057
MM
7286
7287 /* Go through all the ordinary virtual functions, building up
7288 initializers. */
c35cce41 7289 vfun_inits = NULL_TREE;
23656158 7290 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
ca36f057
MM
7291 {
7292 tree delta;
7293 tree vcall_index;
4977bab6 7294 tree fn, fn_original;
f11ee281 7295 tree init = NULL_TREE;
73ea87d7 7296
ca36f057 7297 fn = BV_FN (v);
07fa4878
NS
7298 fn_original = fn;
7299 if (DECL_THUNK_P (fn))
4977bab6 7300 {
07fa4878
NS
7301 if (!DECL_NAME (fn))
7302 finish_thunk (fn);
e00853fd 7303 if (THUNK_ALIAS (fn))
bb885938
NS
7304 {
7305 fn = THUNK_ALIAS (fn);
7306 BV_FN (v) = fn;
7307 }
07fa4878 7308 fn_original = THUNK_TARGET (fn);
4977bab6
ZW
7309 }
7310
d0cd8b44
JM
7311 /* If the only definition of this function signature along our
7312 primary base chain is from a lost primary, this vtable slot will
7313 never be used, so just zero it out. This is important to avoid
7314 requiring extra thunks which cannot be generated with the function.
7315
f11ee281
JM
7316 We first check this in update_vtable_entry_for_fn, so we handle
7317 restored primary bases properly; we also need to do it here so we
7318 zero out unused slots in ctor vtables, rather than filling themff
7319 with erroneous values (though harmless, apart from relocation
7320 costs). */
7321 for (b = binfo; ; b = get_primary_binfo (b))
7322 {
7323 /* We found a defn before a lost primary; go ahead as normal. */
4977bab6 7324 if (look_for_overrides_here (BINFO_TYPE (b), fn_original))
f11ee281
JM
7325 break;
7326
7327 /* The nearest definition is from a lost primary; clear the
7328 slot. */
7329 if (BINFO_LOST_PRIMARY_P (b))
7330 {
7331 init = size_zero_node;
d0cd8b44 7332 break;
f11ee281
JM
7333 }
7334 }
d0cd8b44 7335
f11ee281
JM
7336 if (! init)
7337 {
7338 /* Pull the offset for `this', and the function to call, out of
7339 the list. */
7340 delta = BV_DELTA (v);
548502d3 7341 vcall_index = BV_VCALL_INDEX (v);
f11ee281 7342
50bc768d
NS
7343 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
7344 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
f11ee281
JM
7345
7346 /* You can't call an abstract virtual function; it's abstract.
7347 So, we replace these functions with __pure_virtual. */
4977bab6 7348 if (DECL_PURE_VIRTUAL_P (fn_original))
f11ee281 7349 fn = abort_fndecl;
bb5e8a7f 7350 else if (!integer_zerop (delta) || vcall_index)
4977bab6
ZW
7351 {
7352 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
7353 if (!DECL_NAME (fn))
07fa4878 7354 finish_thunk (fn);
4977bab6 7355 }
f11ee281
JM
7356 /* Take the address of the function, considering it to be of an
7357 appropriate generic type. */
bb5e8a7f 7358 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
f11ee281 7359 }
d0cd8b44 7360
ca36f057 7361 /* And add it to the chain of initializers. */
67231816
RH
7362 if (TARGET_VTABLE_USES_DESCRIPTORS)
7363 {
7364 int i;
7365 if (init == size_zero_node)
7366 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7367 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7368 else
7369 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7370 {
f293ce4b
RS
7371 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
7372 TREE_OPERAND (init, 0),
7d60be94 7373 build_int_cst (NULL_TREE, i));
67231816 7374 TREE_CONSTANT (fdesc) = 1;
6de9cd9a 7375 TREE_INVARIANT (fdesc) = 1;
67231816
RH
7376
7377 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7378 }
7379 }
7380 else
7381 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
ca36f057
MM
7382 }
7383
c35cce41
MM
7384 /* The initializers for virtual functions were built up in reverse
7385 order; straighten them out now. */
7386 vfun_inits = nreverse (vfun_inits);
7387
9bab6c90 7388 /* The negative offset initializers are also in reverse order. */
911a71a7 7389 vid.inits = nreverse (vid.inits);
9bab6c90
MM
7390
7391 /* Chain the two together. */
911a71a7 7392 return chainon (vid.inits, vfun_inits);
ca36f057
MM
7393}
7394
d0cd8b44 7395/* Adds to vid->inits the initializers for the vbase and vcall
c35cce41 7396 offsets in BINFO, which is in the hierarchy dominated by T. */
ca36f057 7397
c35cce41 7398static void
94edc4ab 7399build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
ca36f057 7400{
c35cce41 7401 tree b;
8d08fdba 7402
c35cce41 7403 /* If this is a derived class, we must first create entries
9bab6c90 7404 corresponding to the primary base class. */
911a71a7 7405 b = get_primary_binfo (binfo);
c35cce41 7406 if (b)
911a71a7 7407 build_vcall_and_vbase_vtbl_entries (b, vid);
c35cce41
MM
7408
7409 /* Add the vbase entries for this base. */
911a71a7 7410 build_vbase_offset_vtbl_entries (binfo, vid);
c35cce41 7411 /* Add the vcall entries for this base. */
911a71a7 7412 build_vcall_offset_vtbl_entries (binfo, vid);
ca36f057 7413}
8d08fdba 7414
ca36f057
MM
7415/* Returns the initializers for the vbase offset entries in the vtable
7416 for BINFO (which is part of the class hierarchy dominated by T), in
c35cce41
MM
7417 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7418 where the next vbase offset will go. */
8d08fdba 7419
c35cce41 7420static void
94edc4ab 7421build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
ca36f057 7422{
c35cce41
MM
7423 tree vbase;
7424 tree t;
90b1ca2f 7425 tree non_primary_binfo;
8d08fdba 7426
ca36f057
MM
7427 /* If there are no virtual baseclasses, then there is nothing to
7428 do. */
7429 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
c35cce41 7430 return;
ca36f057 7431
911a71a7 7432 t = vid->derived;
90b1ca2f
NS
7433
7434 /* We might be a primary base class. Go up the inheritance hierarchy
7435 until we find the most derived class of which we are a primary base:
7436 it is the offset of that which we need to use. */
7437 non_primary_binfo = binfo;
7438 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7439 {
7440 tree b;
7441
7442 /* If we have reached a virtual base, then it must be a primary
7443 base (possibly multi-level) of vid->binfo, or we wouldn't
7444 have called build_vcall_and_vbase_vtbl_entries for it. But it
7445 might be a lost primary, so just skip down to vid->binfo. */
809e3e7f 7446 if (BINFO_VIRTUAL_P (non_primary_binfo))
90b1ca2f
NS
7447 {
7448 non_primary_binfo = vid->binfo;
7449 break;
7450 }
7451
7452 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7453 if (get_primary_binfo (b) != non_primary_binfo)
7454 break;
7455 non_primary_binfo = b;
7456 }
ca36f057 7457
c35cce41
MM
7458 /* Go through the virtual bases, adding the offsets. */
7459 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7460 vbase;
7461 vbase = TREE_CHAIN (vbase))
7462 {
7463 tree b;
7464 tree delta;
7465
809e3e7f 7466 if (!BINFO_VIRTUAL_P (vbase))
c35cce41 7467 continue;
ca36f057 7468
c35cce41
MM
7469 /* Find the instance of this virtual base in the complete
7470 object. */
dbbf88d1 7471 b = copied_binfo (vbase, binfo);
c35cce41
MM
7472
7473 /* If we've already got an offset for this virtual base, we
7474 don't need another one. */
7475 if (BINFO_VTABLE_PATH_MARKED (b))
7476 continue;
dbbf88d1 7477 BINFO_VTABLE_PATH_MARKED (b) = 1;
c35cce41
MM
7478
7479 /* Figure out where we can find this vbase offset. */
7480 delta = size_binop (MULT_EXPR,
911a71a7 7481 vid->index,
c35cce41
MM
7482 convert (ssizetype,
7483 TYPE_SIZE_UNIT (vtable_entry_type)));
911a71a7 7484 if (vid->primary_vtbl_p)
c35cce41
MM
7485 BINFO_VPTR_FIELD (b) = delta;
7486
7487 if (binfo != TYPE_BINFO (t))
50bc768d
NS
7488 /* The vbase offset had better be the same. */
7489 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
c35cce41
MM
7490
7491 /* The next vbase will come at a more negative offset. */
a6f5e048
RH
7492 vid->index = size_binop (MINUS_EXPR, vid->index,
7493 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
c35cce41
MM
7494
7495 /* The initializer is the delta from BINFO to this virtual base.
4e7512c9
MM
7496 The vbase offsets go in reverse inheritance-graph order, and
7497 we are walking in inheritance graph order so these end up in
7498 the right order. */
90b1ca2f
NS
7499 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7500
911a71a7 7501 *vid->last_init
9bab6c90
MM
7502 = build_tree_list (NULL_TREE,
7503 fold (build1 (NOP_EXPR,
7504 vtable_entry_type,
7505 delta)));
911a71a7 7506 vid->last_init = &TREE_CHAIN (*vid->last_init);
c35cce41 7507 }
8d08fdba 7508}
ca36f057 7509
b485e15b 7510/* Adds the initializers for the vcall offset entries in the vtable
d0cd8b44
JM
7511 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7512 to VID->INITS. */
b485e15b
MM
7513
7514static void
94edc4ab 7515build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
b485e15b 7516{
548502d3
MM
7517 /* We only need these entries if this base is a virtual base. We
7518 compute the indices -- but do not add to the vtable -- when
7519 building the main vtable for a class. */
809e3e7f 7520 if (BINFO_VIRTUAL_P (binfo) || binfo == TYPE_BINFO (vid->derived))
548502d3
MM
7521 {
7522 /* We need a vcall offset for each of the virtual functions in this
7523 vtable. For example:
b485e15b 7524
548502d3
MM
7525 class A { virtual void f (); };
7526 class B1 : virtual public A { virtual void f (); };
7527 class B2 : virtual public A { virtual void f (); };
7528 class C: public B1, public B2 { virtual void f (); };
d0cd8b44 7529
548502d3
MM
7530 A C object has a primary base of B1, which has a primary base of A. A
7531 C also has a secondary base of B2, which no longer has a primary base
7532 of A. So the B2-in-C construction vtable needs a secondary vtable for
7533 A, which will adjust the A* to a B2* to call f. We have no way of
7534 knowing what (or even whether) this offset will be when we define B2,
7535 so we store this "vcall offset" in the A sub-vtable and look it up in
7536 a "virtual thunk" for B2::f.
b485e15b 7537
548502d3
MM
7538 We need entries for all the functions in our primary vtable and
7539 in our non-virtual bases' secondary vtables. */
7540 vid->vbase = binfo;
7541 /* If we are just computing the vcall indices -- but do not need
7542 the actual entries -- not that. */
809e3e7f 7543 if (!BINFO_VIRTUAL_P (binfo))
548502d3
MM
7544 vid->generate_vcall_entries = false;
7545 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7546 add_vcall_offset_vtbl_entries_r (binfo, vid);
7547 }
b485e15b
MM
7548}
7549
7550/* Build vcall offsets, starting with those for BINFO. */
7551
7552static void
94edc4ab 7553add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
b485e15b
MM
7554{
7555 int i;
7556 tree primary_binfo;
fa743e8c 7557 tree base_binfo;
b485e15b
MM
7558
7559 /* Don't walk into virtual bases -- except, of course, for the
d0cd8b44
JM
7560 virtual base for which we are building vcall offsets. Any
7561 primary virtual base will have already had its offsets generated
7562 through the recursion in build_vcall_and_vbase_vtbl_entries. */
809e3e7f 7563 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
b485e15b
MM
7564 return;
7565
7566 /* If BINFO has a primary base, process it first. */
7567 primary_binfo = get_primary_binfo (binfo);
7568 if (primary_binfo)
7569 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7570
7571 /* Add BINFO itself to the list. */
7572 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7573
7574 /* Scan the non-primary bases of BINFO. */
fa743e8c
NS
7575 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7576 if (base_binfo != primary_binfo)
7577 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
b485e15b
MM
7578}
7579
9965d119 7580/* Called from build_vcall_offset_vtbl_entries_r. */
e92cc029 7581
b485e15b 7582static void
94edc4ab 7583add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
8d08fdba 7584{
e6a66567
MM
7585 /* Make entries for the rest of the virtuals. */
7586 if (abi_version_at_least (2))
31f8e4f3 7587 {
e6a66567 7588 tree orig_fn;
911a71a7 7589
e6a66567
MM
7590 /* The ABI requires that the methods be processed in declaration
7591 order. G++ 3.2 used the order in the vtable. */
7592 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
7593 orig_fn;
7594 orig_fn = TREE_CHAIN (orig_fn))
7595 if (DECL_VINDEX (orig_fn))
95675950 7596 add_vcall_offset (orig_fn, binfo, vid);
e6a66567
MM
7597 }
7598 else
7599 {
7600 tree derived_virtuals;
7601 tree base_virtuals;
7602 tree orig_virtuals;
7603 /* If BINFO is a primary base, the most derived class which has
7604 BINFO as a primary base; otherwise, just BINFO. */
7605 tree non_primary_binfo;
7606
7607 /* We might be a primary base class. Go up the inheritance hierarchy
7608 until we find the most derived class of which we are a primary base:
7609 it is the BINFO_VIRTUALS there that we need to consider. */
7610 non_primary_binfo = binfo;
7611 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
911a71a7 7612 {
e6a66567
MM
7613 tree b;
7614
7615 /* If we have reached a virtual base, then it must be vid->vbase,
7616 because we ignore other virtual bases in
7617 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7618 base (possibly multi-level) of vid->binfo, or we wouldn't
7619 have called build_vcall_and_vbase_vtbl_entries for it. But it
7620 might be a lost primary, so just skip down to vid->binfo. */
809e3e7f 7621 if (BINFO_VIRTUAL_P (non_primary_binfo))
e6a66567 7622 {
8dc2b103 7623 gcc_assert (non_primary_binfo == vid->vbase);
e6a66567
MM
7624 non_primary_binfo = vid->binfo;
7625 break;
7626 }
911a71a7 7627
e6a66567
MM
7628 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7629 if (get_primary_binfo (b) != non_primary_binfo)
7630 break;
7631 non_primary_binfo = b;
7632 }
4e7512c9 7633
e6a66567
MM
7634 if (vid->ctor_vtbl_p)
7635 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7636 where rtti_binfo is the most derived type. */
dbbf88d1
NS
7637 non_primary_binfo
7638 = original_binfo (non_primary_binfo, vid->rtti_binfo);
e6a66567
MM
7639
7640 for (base_virtuals = BINFO_VIRTUALS (binfo),
7641 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7642 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7643 base_virtuals;
7644 base_virtuals = TREE_CHAIN (base_virtuals),
7645 derived_virtuals = TREE_CHAIN (derived_virtuals),
7646 orig_virtuals = TREE_CHAIN (orig_virtuals))
7647 {
7648 tree orig_fn;
73ea87d7 7649
e6a66567
MM
7650 /* Find the declaration that originally caused this function to
7651 be present in BINFO_TYPE (binfo). */
7652 orig_fn = BV_FN (orig_virtuals);
9bab6c90 7653
e6a66567
MM
7654 /* When processing BINFO, we only want to generate vcall slots for
7655 function slots introduced in BINFO. So don't try to generate
7656 one if the function isn't even defined in BINFO. */
7657 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7658 continue;
b485e15b 7659
95675950 7660 add_vcall_offset (orig_fn, binfo, vid);
e6a66567
MM
7661 }
7662 }
7663}
b485e15b 7664
95675950 7665/* Add a vcall offset entry for ORIG_FN to the vtable. */
b485e15b 7666
e6a66567 7667static void
95675950 7668add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
e6a66567
MM
7669{
7670 size_t i;
7671 tree vcall_offset;
9bab6c90 7672
e6a66567
MM
7673 /* If there is already an entry for a function with the same
7674 signature as FN, then we do not need a second vcall offset.
7675 Check the list of functions already present in the derived
7676 class vtable. */
7677 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7678 {
7679 tree derived_entry;
aabb4cd6 7680
e6a66567
MM
7681 derived_entry = VARRAY_TREE (vid->fns, i);
7682 if (same_signature_p (derived_entry, orig_fn)
7683 /* We only use one vcall offset for virtual destructors,
7684 even though there are two virtual table entries. */
7685 || (DECL_DESTRUCTOR_P (derived_entry)
7686 && DECL_DESTRUCTOR_P (orig_fn)))
7687 return;
7688 }
4e7512c9 7689
e6a66567
MM
7690 /* If we are building these vcall offsets as part of building
7691 the vtable for the most derived class, remember the vcall
7692 offset. */
7693 if (vid->binfo == TYPE_BINFO (vid->derived))
0871761b
NS
7694 {
7695 tree_pair_p elt = VEC_safe_push (tree_pair_s,
7696 CLASSTYPE_VCALL_INDICES (vid->derived),
7697 NULL);
7698 elt->purpose = orig_fn;
7699 elt->value = vid->index;
7700 }
7701
e6a66567
MM
7702 /* The next vcall offset will be found at a more negative
7703 offset. */
7704 vid->index = size_binop (MINUS_EXPR, vid->index,
7705 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7706
7707 /* Keep track of this function. */
7708 VARRAY_PUSH_TREE (vid->fns, orig_fn);
7709
7710 if (vid->generate_vcall_entries)
7711 {
7712 tree base;
e6a66567 7713 tree fn;
548502d3 7714
e6a66567 7715 /* Find the overriding function. */
95675950 7716 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
e6a66567
MM
7717 if (fn == error_mark_node)
7718 vcall_offset = build1 (NOP_EXPR, vtable_entry_type,
7719 integer_zero_node);
7720 else
7721 {
95675950
MM
7722 base = TREE_VALUE (fn);
7723
7724 /* The vbase we're working on is a primary base of
7725 vid->binfo. But it might be a lost primary, so its
7726 BINFO_OFFSET might be wrong, so we just use the
7727 BINFO_OFFSET from vid->binfo. */
7728 vcall_offset = size_diffop (BINFO_OFFSET (base),
7729 BINFO_OFFSET (vid->binfo));
548502d3
MM
7730 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
7731 vcall_offset));
548502d3 7732 }
34cd5ae7 7733 /* Add the initializer to the vtable. */
e6a66567
MM
7734 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7735 vid->last_init = &TREE_CHAIN (*vid->last_init);
c35cce41 7736 }
570221c2 7737}
b54ccf71 7738
34cd5ae7 7739/* Return vtbl initializers for the RTTI entries corresponding to the
aabb4cd6 7740 BINFO's vtable. The RTTI entries should indicate the object given
73ea87d7 7741 by VID->rtti_binfo. */
b54ccf71 7742
9bab6c90 7743static void
94edc4ab 7744build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
b54ccf71 7745{
ca36f057 7746 tree b;
aabb4cd6 7747 tree t;
ca36f057 7748 tree basetype;
ca36f057
MM
7749 tree offset;
7750 tree decl;
7751 tree init;
b54ccf71 7752
ca36f057 7753 basetype = BINFO_TYPE (binfo);
73ea87d7 7754 t = BINFO_TYPE (vid->rtti_binfo);
b54ccf71 7755
ca36f057
MM
7756 /* To find the complete object, we will first convert to our most
7757 primary base, and then add the offset in the vtbl to that value. */
7758 b = binfo;
9965d119
NS
7759 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
7760 && !BINFO_LOST_PRIMARY_P (b))
b54ccf71 7761 {
c35cce41
MM
7762 tree primary_base;
7763
911a71a7 7764 primary_base = get_primary_binfo (b);
fc6633e0
NS
7765 gcc_assert (BINFO_PRIMARY_P (primary_base)
7766 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
c35cce41 7767 b = primary_base;
b54ccf71 7768 }
73ea87d7 7769 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8f032717 7770
8fa33dfa
MM
7771 /* The second entry is the address of the typeinfo object. */
7772 if (flag_rtti)
7993382e 7773 decl = build_address (get_tinfo_decl (t));
ca36f057 7774 else
8fa33dfa
MM
7775 decl = integer_zero_node;
7776
7777 /* Convert the declaration to a type that can be stored in the
7778 vtable. */
7993382e 7779 init = build_nop (vfunc_ptr_type_node, decl);
911a71a7
MM
7780 *vid->last_init = build_tree_list (NULL_TREE, init);
7781 vid->last_init = &TREE_CHAIN (*vid->last_init);
8f032717 7782
c35cce41 7783 /* Add the offset-to-top entry. It comes earlier in the vtable that
c4372ef4
NS
7784 the the typeinfo entry. Convert the offset to look like a
7785 function pointer, so that we can put it in the vtable. */
7993382e 7786 init = build_nop (vfunc_ptr_type_node, offset);
c4372ef4
NS
7787 *vid->last_init = build_tree_list (NULL_TREE, init);
7788 vid->last_init = &TREE_CHAIN (*vid->last_init);
8f032717 7789}
0f59171d
RH
7790
7791/* Fold a OBJ_TYPE_REF expression to the address of a function.
7792 KNOWN_TYPE carries the true type of OBJ_TYPE_REF_OBJECT(REF). */
7793
7794tree
7795cp_fold_obj_type_ref (tree ref, tree known_type)
7796{
7797 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
7798 HOST_WIDE_INT i = 0;
604a3205 7799 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
0f59171d
RH
7800 tree fndecl;
7801
7802 while (i != index)
7803 {
7804 i += (TARGET_VTABLE_USES_DESCRIPTORS
7805 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
7806 v = TREE_CHAIN (v);
7807 }
7808
7809 fndecl = BV_FN (v);
7810
7811#ifdef ENABLE_CHECKING
8dc2b103
NS
7812 gcc_assert (tree_int_cst_equal (OBJ_TYPE_REF_TOKEN (ref),
7813 DECL_VINDEX (fndecl)));
0f59171d
RH
7814#endif
7815
7816 return build_address (fndecl);
7817}
d7afec4b 7818