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