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