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