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