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