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