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