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