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