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