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