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