]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/class.c
posix-threads.cc (_Jv_MutexLock): Moved back to posix-threads.h.
[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
2930 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
2931 cannot have a default constructor, copy constructor taking a const
2932 reference argument, or an assignment operator, respectively. If a
2933 virtual destructor is created, its DECL is returned; otherwise the
2934 return value is NULL_TREE. */
2935
2936static tree
2937add_implicitly_declared_members (t, cant_have_default_ctor,
2938 cant_have_const_cctor,
2939 cant_have_assignment)
2940 tree t;
2941 int cant_have_default_ctor;
2942 int cant_have_const_cctor;
2943 int cant_have_assignment;
2944{
2945 tree default_fn;
2946 tree implicit_fns = NULL_TREE;
2947 tree name = TYPE_IDENTIFIER (t);
2948 tree virtual_dtor = NULL_TREE;
2949 tree *f;
2950
2951 /* Destructor. */
834c6dff 2952 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
61a127b3
MM
2953 {
2954 default_fn = cons_up_default_function (t, name, 0);
2955 check_for_override (default_fn, t);
2956
2957 /* If we couldn't make it work, then pretend we didn't need it. */
2958 if (default_fn == void_type_node)
834c6dff 2959 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
61a127b3
MM
2960 else
2961 {
2962 TREE_CHAIN (default_fn) = implicit_fns;
2963 implicit_fns = default_fn;
2964
2965 if (DECL_VINDEX (default_fn))
2966 virtual_dtor = default_fn;
2967 }
2968 }
834c6dff
MM
2969 else
2970 /* Any non-implicit destructor is non-trivial. */
2971 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
61a127b3
MM
2972
2973 /* Default constructor. */
6eabb241 2974 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
61a127b3
MM
2975 {
2976 default_fn = cons_up_default_function (t, name, 2);
2977 TREE_CHAIN (default_fn) = implicit_fns;
2978 implicit_fns = default_fn;
2979 }
2980
2981 /* Copy constructor. */
6eabb241 2982 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
61a127b3
MM
2983 {
2984 /* ARM 12.18: You get either X(X&) or X(const X&), but
2985 not both. --Chip */
2986 default_fn = cons_up_default_function (t, name,
2987 3 + cant_have_const_cctor);
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
MM
2994 {
2995 default_fn = cons_up_default_function (t, name,
2996 5 + cant_have_assignment);
2997 TREE_CHAIN (default_fn) = implicit_fns;
2998 implicit_fns = default_fn;
2999 }
3000
3001 /* Now, hook all of the new functions on to TYPE_METHODS,
3002 and add them to the CLASSTYPE_METHOD_VEC. */
3003 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3004 add_method (t, 0, *f);
3005 *f = TYPE_METHODS (t);
3006 TYPE_METHODS (t) = implicit_fns;
3007
3008 return virtual_dtor;
3009}
3010
f90cdf34
MT
3011/* Subroutine of finish_struct_1. Recursively count the number of fields
3012 in TYPE, including anonymous union members. */
3013
3014static int
3015count_fields (fields)
3016 tree fields;
3017{
3018 tree x;
3019 int n_fields = 0;
3020 for (x = fields; x; x = TREE_CHAIN (x))
3021 {
3022 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3023 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3024 else
3025 n_fields += 1;
3026 }
3027 return n_fields;
3028}
3029
3030/* Subroutine of finish_struct_1. Recursively add all the fields in the
3031 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3032
3033static int
3034add_fields_to_vec (fields, field_vec, idx)
3035 tree fields, field_vec;
3036 int idx;
3037{
3038 tree x;
3039 for (x = fields; x; x = TREE_CHAIN (x))
3040 {
3041 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3042 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3043 else
3044 TREE_VEC_ELT (field_vec, idx++) = x;
3045 }
3046 return idx;
3047}
3048
1e30f9b4
MM
3049/* FIELD is a bit-field. We are finishing the processing for its
3050 enclosing type. Issue any appropriate messages and set appropriate
3051 flags. */
3052
3053static void
3054check_bitfield_decl (field)
3055 tree field;
3056{
3057 tree type = TREE_TYPE (field);
cd8ed629 3058 tree w = NULL_TREE;
1e30f9b4 3059
cd8ed629 3060 /* Detect invalid bit-field type. */
1e30f9b4
MM
3061 if (DECL_INITIAL (field)
3062 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3063 {
3064 cp_error_at ("bit-field `%#D' with non-integral type", field);
cd8ed629 3065 w = error_mark_node;
1e30f9b4
MM
3066 }
3067
3068 /* Detect and ignore out of range field width. */
3069 if (DECL_INITIAL (field))
3070 {
cd8ed629 3071 w = DECL_INITIAL (field);
1e30f9b4
MM
3072
3073 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3074 STRIP_NOPS (w);
3075
3076 /* detect invalid field size. */
3077 if (TREE_CODE (w) == CONST_DECL)
3078 w = DECL_INITIAL (w);
3079 else if (TREE_READONLY_DECL_P (w))
3080 w = decl_constant_value (w);
3081
3082 if (TREE_CODE (w) != INTEGER_CST)
3083 {
3084 cp_error_at ("bit-field `%D' width not an integer constant",
3085 field);
cd8ed629 3086 w = error_mark_node;
1e30f9b4 3087 }
05bccae2 3088 else if (tree_int_cst_sgn (w) < 0)
1e30f9b4 3089 {
1e30f9b4 3090 cp_error_at ("negative width in bit-field `%D'", field);
cd8ed629 3091 w = error_mark_node;
1e30f9b4 3092 }
05bccae2 3093 else if (integer_zerop (w) && DECL_NAME (field) != 0)
1e30f9b4 3094 {
1e30f9b4 3095 cp_error_at ("zero width for bit-field `%D'", field);
cd8ed629 3096 w = error_mark_node;
1e30f9b4 3097 }
05bccae2 3098 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
1e30f9b4
MM
3099 && TREE_CODE (type) != ENUMERAL_TYPE
3100 && TREE_CODE (type) != BOOLEAN_TYPE)
3101 cp_warning_at ("width of `%D' exceeds its type", field);
3102 else if (TREE_CODE (type) == ENUMERAL_TYPE
05bccae2
RK
3103 && (0 > compare_tree_int (w,
3104 min_precision (TYPE_MIN_VALUE (type),
3105 TREE_UNSIGNED (type)))
3106 || 0 > compare_tree_int (w,
3107 min_precision
3108 (TYPE_MAX_VALUE (type),
3109 TREE_UNSIGNED (type)))))
1e30f9b4
MM
3110 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3111 field, type);
cd8ed629
MM
3112 }
3113
3114 /* Remove the bit-field width indicator so that the rest of the
3115 compiler does not treat that value as an initializer. */
3116 DECL_INITIAL (field) = NULL_TREE;
1e30f9b4 3117
cd8ed629
MM
3118 if (w != error_mark_node)
3119 {
3120 DECL_SIZE (field) = convert (bitsizetype, w);
3121 DECL_BIT_FIELD (field) = 1;
1e30f9b4 3122
cd8ed629
MM
3123 if (integer_zerop (w))
3124 {
1e30f9b4 3125#ifdef EMPTY_FIELD_BOUNDARY
cd8ed629
MM
3126 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3127 EMPTY_FIELD_BOUNDARY);
1e30f9b4
MM
3128#endif
3129#ifdef PCC_BITFIELD_TYPE_MATTERS
cd8ed629
MM
3130 if (PCC_BITFIELD_TYPE_MATTERS)
3131 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3132 TYPE_ALIGN (type));
1e30f9b4 3133#endif
1e30f9b4
MM
3134 }
3135 }
3136 else
cd8ed629
MM
3137 {
3138 /* Non-bit-fields are aligned for their type. */
3139 DECL_BIT_FIELD (field) = 0;
3140 CLEAR_DECL_C_BIT_FIELD (field);
3141 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3142 }
1e30f9b4
MM
3143}
3144
3145/* FIELD is a non bit-field. We are finishing the processing for its
3146 enclosing type T. Issue any appropriate messages and set appropriate
3147 flags. */
3148
3149static void
3150check_field_decl (field, t, cant_have_const_ctor,
3151 cant_have_default_ctor, no_const_asn_ref,
3152 any_default_members)
3153 tree field;
3154 tree t;
3155 int *cant_have_const_ctor;
3156 int *cant_have_default_ctor;
3157 int *no_const_asn_ref;
3158 int *any_default_members;
3159{
3160 tree type = strip_array_types (TREE_TYPE (field));
3161
3162 /* An anonymous union cannot contain any fields which would change
3163 the settings of CANT_HAVE_CONST_CTOR and friends. */
3164 if (ANON_UNION_TYPE_P (type))
3165 ;
3166 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3167 structs. So, we recurse through their fields here. */
3168 else if (ANON_AGGR_TYPE_P (type))
3169 {
3170 tree fields;
3171
3172 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
17aec3eb 3173 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
1e30f9b4
MM
3174 check_field_decl (fields, t, cant_have_const_ctor,
3175 cant_have_default_ctor, no_const_asn_ref,
3176 any_default_members);
3177 }
3178 /* Check members with class type for constructors, destructors,
3179 etc. */
3180 else if (CLASS_TYPE_P (type))
3181 {
3182 /* Never let anything with uninheritable virtuals
3183 make it through without complaint. */
3184 abstract_virtuals_error (field, type);
3185
3186 if (TREE_CODE (t) == UNION_TYPE)
3187 {
3188 if (TYPE_NEEDS_CONSTRUCTING (type))
3189 cp_error_at ("member `%#D' with constructor not allowed in union",
3190 field);
834c6dff 3191 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1e30f9b4
MM
3192 cp_error_at ("member `%#D' with destructor not allowed in union",
3193 field);
3194 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3195 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3196 field);
3197 }
3198 else
3199 {
3200 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
834c6dff
MM
3201 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3202 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
1e30f9b4
MM
3203 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3204 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3205 }
3206
3207 if (!TYPE_HAS_CONST_INIT_REF (type))
3208 *cant_have_const_ctor = 1;
3209
3210 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3211 *no_const_asn_ref = 1;
3212
3213 if (TYPE_HAS_CONSTRUCTOR (type)
3214 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3215 *cant_have_default_ctor = 1;
3216 }
3217 if (DECL_INITIAL (field) != NULL_TREE)
3218 {
3219 /* `build_class_init_list' does not recognize
3220 non-FIELD_DECLs. */
3221 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3222 cp_error_at ("multiple fields in union `%T' initialized");
3223 *any_default_members = 1;
3224 }
3225
3226 /* Non-bit-fields are aligned for their type, except packed fields
3227 which require only BITS_PER_UNIT alignment. */
3228 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3229 (DECL_PACKED (field)
3230 ? BITS_PER_UNIT
3231 : TYPE_ALIGN (TREE_TYPE (field))));
6bb88f3b 3232}
1e30f9b4 3233
08b962b0
MM
3234/* Check the data members (both static and non-static), class-scoped
3235 typedefs, etc., appearing in the declaration of T. Issue
3236 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3237 declaration order) of access declarations; each TREE_VALUE in this
3238 list is a USING_DECL.
8d08fdba 3239
08b962b0 3240 In addition, set the following flags:
8d08fdba 3241
08b962b0
MM
3242 EMPTY_P
3243 The class is empty, i.e., contains no non-static data members.
8d08fdba 3244
08b962b0
MM
3245 CANT_HAVE_DEFAULT_CTOR_P
3246 This class cannot have an implicitly generated default
3247 constructor.
8d08fdba 3248
08b962b0
MM
3249 CANT_HAVE_CONST_CTOR_P
3250 This class cannot have an implicitly generated copy constructor
3251 taking a const reference.
8d08fdba 3252
08b962b0
MM
3253 CANT_HAVE_CONST_ASN_REF
3254 This class cannot have an implicitly generated assignment
3255 operator taking a const reference.
8d08fdba 3256
08b962b0
MM
3257 All of these flags should be initialized before calling this
3258 function.
8d08fdba 3259
08b962b0
MM
3260 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3261 fields can be added by adding to this chain. */
8d08fdba 3262
607cf131 3263static void
08b962b0
MM
3264check_field_decls (t, access_decls, empty_p,
3265 cant_have_default_ctor_p, cant_have_const_ctor_p,
3266 no_const_asn_ref_p)
3267 tree t;
3268 tree *access_decls;
3269 int *empty_p;
3270 int *cant_have_default_ctor_p;
3271 int *cant_have_const_ctor_p;
3272 int *no_const_asn_ref_p;
3273{
3274 tree *field;
3275 tree *next;
3276 int has_pointers;
3277 int any_default_members;
3278
58010b57
MM
3279 /* First, delete any duplicate fields. */
3280 delete_duplicate_fields (TYPE_FIELDS (t));
3281
08b962b0
MM
3282 /* Assume there are no access declarations. */
3283 *access_decls = NULL_TREE;
3284 /* Assume this class has no pointer members. */
3285 has_pointers = 0;
3286 /* Assume none of the members of this class have default
3287 initializations. */
3288 any_default_members = 0;
3289
3290 for (field = &TYPE_FIELDS (t); *field; field = next)
8d08fdba 3291 {
08b962b0
MM
3292 tree x = *field;
3293 tree type = TREE_TYPE (x);
8d08fdba 3294
f30432d7 3295 GNU_xref_member (current_class_name, x);
8d08fdba 3296
08b962b0 3297 next = &TREE_CHAIN (x);
8d08fdba 3298
c91a56d2 3299 if (TREE_CODE (x) == FIELD_DECL)
691c003d
MS
3300 {
3301 DECL_PACKED (x) |= TYPE_PACKED (t);
e6267549
JM
3302
3303 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
08b962b0
MM
3304 /* We don't treat zero-width bitfields as making a class
3305 non-empty. */
3306 ;
e6267549 3307 else
f9c528ea
MM
3308 {
3309 /* The class is non-empty. */
3310 *empty_p = 0;
3311 /* The class is not even nearly empty. */
3312 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3313 }
691c003d 3314 }
c91a56d2 3315
cffa8729 3316 if (TREE_CODE (x) == USING_DECL)
f30432d7 3317 {
08b962b0
MM
3318 /* Prune the access declaration from the list of fields. */
3319 *field = TREE_CHAIN (x);
3320
3321 /* Save the access declarations for our caller. */
3322 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3323
3324 /* Since we've reset *FIELD there's no reason to skip to the
3325 next field. */
3326 next = field;
f30432d7
MS
3327 continue;
3328 }
8d08fdba 3329
050367a3
MM
3330 if (TREE_CODE (x) == TYPE_DECL
3331 || TREE_CODE (x) == TEMPLATE_DECL)
f30432d7 3332 continue;
8d08fdba 3333
f30432d7 3334 /* If we've gotten this far, it's a data member, possibly static,
e92cc029 3335 or an enumerator. */
8d08fdba 3336
17aec3eb 3337 DECL_CONTEXT (x) = t;
8d08fdba 3338
f30432d7
MS
3339 /* ``A local class cannot have static data members.'' ARM 9.4 */
3340 if (current_function_decl && TREE_STATIC (x))
8251199e 3341 cp_error_at ("field `%D' in local class cannot be static", x);
8d08fdba 3342
f30432d7
MS
3343 /* Perform error checking that did not get done in
3344 grokdeclarator. */
52fb2769 3345 if (TREE_CODE (type) == FUNCTION_TYPE)
f30432d7 3346 {
8251199e 3347 cp_error_at ("field `%D' invalidly declared function type",
f30432d7 3348 x);
52fb2769
NS
3349 type = build_pointer_type (type);
3350 TREE_TYPE (x) = type;
f30432d7 3351 }
52fb2769 3352 else if (TREE_CODE (type) == METHOD_TYPE)
f30432d7 3353 {
8251199e 3354 cp_error_at ("field `%D' invalidly declared method type", x);
52fb2769
NS
3355 type = build_pointer_type (type);
3356 TREE_TYPE (x) = type;
f30432d7 3357 }
52fb2769 3358 else if (TREE_CODE (type) == OFFSET_TYPE)
f30432d7 3359 {
8251199e 3360 cp_error_at ("field `%D' invalidly declared offset type", x);
52fb2769
NS
3361 type = build_pointer_type (type);
3362 TREE_TYPE (x) = type;
f30432d7 3363 }
8d08fdba 3364
52fb2769 3365 if (type == error_mark_node)
f30432d7 3366 continue;
8d08fdba 3367
f30432d7
MS
3368 /* When this goes into scope, it will be a non-local reference. */
3369 DECL_NONLOCAL (x) = 1;
8d08fdba 3370
f30432d7
MS
3371 if (TREE_CODE (x) == CONST_DECL)
3372 continue;
8d08fdba 3373
f30432d7
MS
3374 if (TREE_CODE (x) == VAR_DECL)
3375 {
3376 if (TREE_CODE (t) == UNION_TYPE)
3377 /* Unions cannot have static members. */
8251199e 3378 cp_error_at ("field `%D' declared static in union", x);
8d08fdba 3379
f30432d7
MS
3380 continue;
3381 }
8d08fdba 3382
f30432d7 3383 /* Now it can only be a FIELD_DECL. */
8d08fdba 3384
f30432d7 3385 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
08b962b0 3386 CLASSTYPE_NON_AGGREGATE (t) = 1;
8d08fdba 3387
f30432d7
MS
3388 /* If this is of reference type, check if it needs an init.
3389 Also do a little ANSI jig if necessary. */
52fb2769 3390 if (TREE_CODE (type) == REFERENCE_TYPE)
f30432d7 3391 {
08b962b0 3392 CLASSTYPE_NON_POD_P (t) = 1;
f30432d7 3393 if (DECL_INITIAL (x) == NULL_TREE)
08b962b0 3394 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
8d08fdba 3395
f30432d7
MS
3396 /* ARM $12.6.2: [A member initializer list] (or, for an
3397 aggregate, initialization by a brace-enclosed list) is the
3398 only way to initialize nonstatic const and reference
3399 members. */
08b962b0 3400 *cant_have_default_ctor_p = 1;
e349ee73 3401 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
f30432d7
MS
3402
3403 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3404 {
3405 if (DECL_NAME (x))
8251199e 3406 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
f30432d7 3407 else
8251199e 3408 cp_warning_at ("non-static reference in class without a constructor", x);
8d08fdba 3409 }
f30432d7 3410 }
8d08fdba 3411
1e30f9b4 3412 type = strip_array_types (type);
52fb2769
NS
3413
3414 if (TREE_CODE (type) == POINTER_TYPE)
824b9a4c
MS
3415 has_pointers = 1;
3416
52fb2769 3417 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
08b962b0 3418 CLASSTYPE_HAS_MUTABLE (t) = 1;
a7a7710d 3419
c4d6cee3
JM
3420 if (! pod_type_p (type)
3421 /* For some reason, pointers to members are POD types themselves,
3422 but are not allowed in POD structs. Silly. */
3423 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
08b962b0 3424 CLASSTYPE_NON_POD_P (t) = 1;
52fb2769 3425
f30432d7 3426 /* If any field is const, the structure type is pseudo-const. */
52fb2769 3427 if (CP_TYPE_CONST_P (type))
f30432d7
MS
3428 {
3429 C_TYPE_FIELDS_READONLY (t) = 1;
3430 if (DECL_INITIAL (x) == NULL_TREE)
08b962b0 3431 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
f30432d7
MS
3432
3433 /* ARM $12.6.2: [A member initializer list] (or, for an
3434 aggregate, initialization by a brace-enclosed list) is the
3435 only way to initialize nonstatic const and reference
3436 members. */
08b962b0 3437 *cant_have_default_ctor_p = 1;
e349ee73 3438 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
f30432d7 3439
6eabb241 3440 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
f30432d7
MS
3441 {
3442 if (DECL_NAME (x))
8251199e 3443 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
f30432d7 3444 else
8251199e 3445 cp_warning_at ("non-static const member in class without a constructor", x);
f30432d7
MS
3446 }
3447 }
08b962b0
MM
3448 /* A field that is pseudo-const makes the structure likewise. */
3449 else if (IS_AGGR_TYPE (type))
f30432d7 3450 {
08b962b0
MM
3451 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3452 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3453 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
f30432d7 3454 }
8d08fdba 3455
162bc98d
JM
3456 /* We set DECL_C_BIT_FIELD in grokbitfield.
3457 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3458 if (DECL_C_BIT_FIELD (x))
1e30f9b4 3459 check_bitfield_decl (x);
f30432d7 3460 else
1e30f9b4 3461 check_field_decl (x, t,
08b962b0
MM
3462 cant_have_const_ctor_p,
3463 cant_have_default_ctor_p,
3464 no_const_asn_ref_p,
1e30f9b4 3465 &any_default_members);
8d08fdba
MS
3466 }
3467
824b9a4c 3468 /* Effective C++ rule 11. */
7834ab39 3469 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
824b9a4c
MS
3470 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3471 {
8251199e 3472 cp_warning ("`%#T' has pointer data members", t);
824b9a4c
MS
3473
3474 if (! TYPE_HAS_INIT_REF (t))
3475 {
8251199e 3476 cp_warning (" but does not override `%T(const %T&)'", t, t);
824b9a4c 3477 if (! TYPE_HAS_ASSIGN_REF (t))
8251199e 3478 cp_warning (" or `operator=(const %T&)'", t);
824b9a4c
MS
3479 }
3480 else if (! TYPE_HAS_ASSIGN_REF (t))
8251199e 3481 cp_warning (" but does not override `operator=(const %T&)'", t);
824b9a4c 3482 }
08b962b0 3483
607cf131
MM
3484
3485 /* Check anonymous struct/anonymous union fields. */
3486 finish_struct_anon (t);
3487
08b962b0
MM
3488 /* We've built up the list of access declarations in reverse order.
3489 Fix that now. */
3490 *access_decls = nreverse (*access_decls);
08b962b0
MM
3491}
3492
58010b57
MM
3493/* Return a FIELD_DECL for a pointer-to-virtual-table or
3494 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
3495 field are as indicated. The CLASS_TYPE in which this field occurs
07a3462a
JW
3496 is also indicated. FCONTEXT is the type that is needed for the debug
3497 info output routines. *EMPTY_P is set to a non-zero value by this
58010b57
MM
3498 function to indicate that a class containing this field is
3499 non-empty. */
3500
3501static tree
07a3462a 3502build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
58010b57
MM
3503 empty_p)
3504 tree name;
3505 tree assembler_name;
3506 tree type;
3507 tree class_type;
07a3462a 3508 tree fcontext;
58010b57
MM
3509 int *empty_p;
3510{
3511 tree field;
3512
3513 /* This class is non-empty. */
3514 *empty_p = 0;
3515
3516 /* Build the FIELD_DECL. */
3517 field = build_lang_decl (FIELD_DECL, name, type);
3518 DECL_ASSEMBLER_NAME (field) = assembler_name;
3519 DECL_VIRTUAL_P (field) = 1;
3520 DECL_ARTIFICIAL (field) = 1;
3521 DECL_FIELD_CONTEXT (field) = class_type;
07a3462a 3522 DECL_FCONTEXT (field) = fcontext;
58010b57
MM
3523 DECL_ALIGN (field) = TYPE_ALIGN (type);
3524
3525 /* Return it. */
3526 return field;
3527}
3528
5c24fba6
MM
3529/* Record the type of BINFO in the slot in DATA (which is really a
3530 `varray_type *') corresponding to the BINFO_OFFSET. */
607cf131 3531
5c24fba6
MM
3532static tree
3533dfs_record_base_offsets (binfo, data)
3534 tree binfo;
3535 void *data;
3536{
3537 varray_type *v;
770ae6cc 3538 unsigned HOST_WIDE_INT offset = tree_low_cst (BINFO_OFFSET (binfo), 1);
5c24fba6
MM
3539
3540 v = (varray_type *) data;
3541 while (VARRAY_SIZE (*v) <= offset)
3542 VARRAY_GROW (*v, 2 * VARRAY_SIZE (*v));
3543 VARRAY_TREE (*v, offset) = tree_cons (NULL_TREE,
3544 BINFO_TYPE (binfo),
3545 VARRAY_TREE (*v, offset));
3546
3547 return NULL_TREE;
607cf131
MM
3548}
3549
9785e4b1
MM
3550/* Add the offset of BINFO and its bases to BASE_OFFSETS. */
3551
3552static void
3553record_base_offsets (binfo, base_offsets)
3554 tree binfo;
3555 varray_type *base_offsets;
3556{
9785e4b1
MM
3557 dfs_walk (binfo,
3558 dfs_record_base_offsets,
3559 dfs_skip_vbases,
3560 base_offsets);
9785e4b1
MM
3561}
3562
5c24fba6
MM
3563/* Returns non-NULL if there is already an entry in DATA (which is
3564 really a `varray_type') indicating that an object with the same
3565 type of BINFO is already at the BINFO_OFFSET for BINFO. */
d77249e7
MM
3566
3567static tree
5c24fba6
MM
3568dfs_search_base_offsets (binfo, data)
3569 tree binfo;
3570 void *data;
3571{
3572 if (is_empty_class (BINFO_TYPE (binfo)))
3573 {
3574 varray_type v = (varray_type) data;
770ae6cc
RK
3575 /* Find the offset for this BINFO. */
3576 unsigned HOST_WIDE_INT offset = tree_low_cst (BINFO_OFFSET (binfo), 1);
5c24fba6
MM
3577 tree t;
3578
5c24fba6
MM
3579 /* If we haven't yet encountered any objects at offsets that
3580 big, then there's no conflict. */
3581 if (VARRAY_SIZE (v) <= offset)
3582 return NULL_TREE;
3583 /* Otherwise, go through the objects already allocated at this
3584 offset. */
3585 for (t = VARRAY_TREE (v, offset); t; t = TREE_CHAIN (t))
3586 if (same_type_p (TREE_VALUE (t), BINFO_TYPE (binfo)))
3587 return binfo;
3588 }
3589
3590 return NULL_TREE;
3591}
3592
9785e4b1
MM
3593/* Returns non-zero if there's a conflict between BINFO and a base
3594 already mentioned in BASE_OFFSETS if BINFO is placed at its current
3595 BINFO_OFFSET. */
3596
3597static int
3598layout_conflict_p (binfo, base_offsets)
3599 tree binfo;
3600 varray_type base_offsets;
3601{
3602 return dfs_walk (binfo, dfs_search_base_offsets, dfs_skip_vbases,
3603 base_offsets) != NULL_TREE;
3604}
3605
5c24fba6
MM
3606/* DECL is a FIELD_DECL corresponding either to a base subobject of a
3607 non-static data member of the type indicated by RLI. BINFO is the
3608 binfo corresponding to the base subobject, or, if this is a
3609 non-static data-member, a dummy BINFO for the type of the data
01955e96
MM
3610 member. BINFO may be NULL if checks to see if the field overlaps
3611 an existing field with the same type are not required. V maps
3612 offsets to types already located at those offsets. This function
3613 determines the position of the DECL. */
5c24fba6
MM
3614
3615static void
3616layout_nonempty_base_or_field (rli, decl, binfo, v)
3617 record_layout_info rli;
3618 tree decl;
3619 tree binfo;
3620 varray_type v;
3621{
3622 /* Try to place the field. It may take more than one try if we have
3623 a hard time placing the field without putting two objects of the
3624 same type at the same address. */
3625 while (1)
3626 {
3627 tree offset;
770ae6cc 3628 struct record_layout_info old_rli = *rli;
5c24fba6 3629
770ae6cc
RK
3630 /* Place this field. */
3631 place_field (rli, decl);
5c24fba6
MM
3632
3633 /* Now that we know where it wil be placed, update its
3634 BINFO_OFFSET. */
da3d4dfa 3635 offset = byte_position (decl);
01955e96 3636 if (binfo)
da3d4dfa
MM
3637 propagate_binfo_offsets (binfo,
3638 convert (ssizetype, offset));
5c24fba6
MM
3639
3640 /* We have to check to see whether or not there is already
3641 something of the same type at the offset we're about to use.
3642 For example:
3643
3644 struct S {};
3645 struct T : public S { int i; };
3646 struct U : public S, public T {};
3647
3648 Here, we put S at offset zero in U. Then, we can't put T at
3649 offset zero -- its S component would be at the same address
3650 as the S we already allocated. So, we have to skip ahead.
3651 Since all data members, including those whose type is an
3652 empty class, have non-zero size, any overlap can happen only
3653 with a direct or indirect base-class -- it can't happen with
3654 a data member. */
9785e4b1 3655 if (binfo && flag_new_abi && layout_conflict_p (binfo, v))
5c24fba6
MM
3656 {
3657 /* Undo the propogate_binfo_offsets call. */
770ae6cc 3658 offset = size_diffop (size_zero_node, offset);
da3d4dfa 3659 propagate_binfo_offsets (binfo, convert (ssizetype, offset));
770ae6cc 3660
5c24fba6
MM
3661 /* Strip off the size allocated to this field. That puts us
3662 at the first place we could have put the field with
3663 proper alignment. */
770ae6cc
RK
3664 *rli = old_rli;
3665
3666 /* Bump up by the alignment required for the type, without
5c24fba6 3667 virtual base classes. */
770ae6cc
RK
3668 rli->bitpos
3669 = size_binop (PLUS_EXPR, rli->bitpos,
3670 bitsize_int (CLASSTYPE_ALIGN (BINFO_TYPE (binfo))));
3671 normalize_rli (rli);
5c24fba6
MM
3672 }
3673 else
3674 /* There was no conflict. We're done laying out this field. */
3675 break;
3676 }
3677}
3678
9785e4b1 3679/* Layout the empty base BINFO. EOC indicates the byte currently just
ec386958
MM
3680 past the end of the class, and should be correctly aligned for a
3681 class of the type indicated by BINFO; BINFO_OFFSETS gives the
3682 offsets of the other bases allocated so far. */
9785e4b1
MM
3683
3684static void
3685layout_empty_base (binfo, eoc, binfo_offsets)
3686 tree binfo;
3687 tree eoc;
3688 varray_type binfo_offsets;
3689{
ec386958 3690 tree alignment;
9785e4b1
MM
3691 tree basetype = BINFO_TYPE (binfo);
3692
3693 /* This routine should only be used for empty classes. */
3694 my_friendly_assert (is_empty_class (basetype), 20000321);
ec386958 3695 alignment = ssize_int (CLASSTYPE_ALIGN (basetype));
9785e4b1
MM
3696
3697 /* This is an empty base class. We first try to put it at offset
3698 zero. */
3699 if (layout_conflict_p (binfo, binfo_offsets))
3700 {
3701 /* That didn't work. Now, we move forward from the next
3702 available spot in the class. */
770ae6cc 3703 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
9785e4b1
MM
3704 while (1)
3705 {
3706 if (!layout_conflict_p (binfo, binfo_offsets))
3707 /* We finally found a spot where there's no overlap. */
3708 break;
3709
3710 /* There's overlap here, too. Bump along to the next spot. */
ec386958 3711 propagate_binfo_offsets (binfo, alignment);
9785e4b1
MM
3712 }
3713 }
3714}
3715
5c24fba6
MM
3716/* Build a FIELD_DECL for the base given by BINFO in the class
3717 *indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
3718 *BASE_ALIGN is a running maximum of the alignments of any base
3719 *class. */
3720
3721static void
3722build_base_field (rli, binfo, empty_p, base_align, v)
3723 record_layout_info rli;
d77249e7
MM
3724 tree binfo;
3725 int *empty_p;
d77249e7 3726 unsigned int *base_align;
5c24fba6 3727 varray_type *v;
d77249e7
MM
3728{
3729 tree basetype = BINFO_TYPE (binfo);
3730 tree decl;
3731
d0f062fb 3732 if (!COMPLETE_TYPE_P (basetype))
d77249e7
MM
3733 /* This error is now reported in xref_tag, thus giving better
3734 location information. */
5c24fba6 3735 return;
d77249e7
MM
3736
3737 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
3738 DECL_ARTIFICIAL (decl) = 1;
5c24fba6 3739 DECL_FIELD_CONTEXT (decl) = rli->t;
d77249e7 3740 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
06ceef4e 3741 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
d77249e7
MM
3742 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3743
d77249e7
MM
3744 if (! flag_new_abi)
3745 {
3746 /* Brain damage for backwards compatibility. For no good
9785e4b1 3747 reason, the old basetype layout made every base have at least
d77249e7 3748 as large as the alignment for the bases up to that point,
9785e4b1 3749 gratuitously wasting space. So we do the same thing here. */
d77249e7
MM
3750 *base_align = MAX (*base_align, DECL_ALIGN (decl));
3751 DECL_SIZE (decl)
665f2503 3752 = size_binop (MAX_EXPR, DECL_SIZE (decl), bitsize_int (*base_align));
06ceef4e 3753 DECL_SIZE_UNIT (decl)
665f2503
RK
3754 = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
3755 size_int (*base_align / BITS_PER_UNIT));
d77249e7
MM
3756 }
3757
5c24fba6
MM
3758 if (!integer_zerop (DECL_SIZE (decl)))
3759 {
3760 /* The containing class is non-empty because it has a non-empty
3761 base class. */
3762 *empty_p = 0;
3763
3764 /* Try to place the field. It may take more than one try if we
3765 have a hard time placing the field without putting two
3766 objects of the same type at the same address. */
3767 layout_nonempty_base_or_field (rli, decl, binfo, *v);
3768 }
3769 else
ec386958
MM
3770 {
3771 unsigned HOST_WIDE_INT eoc;
3772
3773 /* On some platforms (ARM), even empty classes will not be
3774 byte-aligned. */
3775 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
3776 eoc = CEIL (eoc, DECL_ALIGN (decl)) * DECL_ALIGN (decl);
3777 layout_empty_base (binfo, size_int (eoc), *v);
3778 }
5c24fba6
MM
3779
3780 /* Check for inaccessible base classes. If the same base class
3781 appears more than once in the hierarchy, but isn't virtual, then
3782 it's ambiguous. */
3783 if (get_base_distance (basetype, rli->t, 0, NULL) == -2)
3784 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
3785 basetype, rli->t);
3786
3787 /* Record the offsets of BINFO and its base subobjects. */
9785e4b1 3788 record_base_offsets (binfo, v);
d77249e7
MM
3789}
3790
5c24fba6
MM
3791/* Layout all of the non-virtual base classes. Returns a map from
3792 offsets to types present at those offsets. */
607cf131 3793
5c24fba6
MM
3794static varray_type
3795build_base_fields (rli, empty_p)
3796 record_layout_info rli;
607cf131
MM
3797 int *empty_p;
3798{
3799 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3800 subobjects. */
5c24fba6 3801 tree rec = rli->t;
607cf131 3802 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
5c24fba6
MM
3803 int i;
3804 varray_type v;
607cf131
MM
3805 unsigned int base_align = 0;
3806
5c24fba6
MM
3807 /* Create the table mapping offsets to empty base classes. */
3808 VARRAY_TREE_INIT (v, 32, "v");
3809
d77249e7
MM
3810 /* Under the new ABI, the primary base class is always allocated
3811 first. */
3812 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
5c24fba6
MM
3813 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
3814 empty_p, &base_align, &v);
d77249e7
MM
3815
3816 /* Now allocate the rest of the bases. */
607cf131
MM
3817 for (i = 0; i < n_baseclasses; ++i)
3818 {
d77249e7 3819 tree base_binfo;
607cf131 3820
d77249e7
MM
3821 /* Under the new ABI, the primary base was already allocated
3822 above, so we don't need to allocate it again here. */
3823 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
607cf131
MM
3824 continue;
3825
d77249e7
MM
3826 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3827
8026246f
MM
3828 /* A primary virtual base class is allocated just like any other
3829 base class, but a non-primary virtual base is allocated
9785e4b1 3830 later, in layout_virtual_bases. */
8026246f 3831 if (TREE_VIA_VIRTUAL (base_binfo)
d77249e7 3832 && !BINFO_PRIMARY_MARKED_P (base_binfo))
607cf131
MM
3833 continue;
3834
5c24fba6 3835 build_base_field (rli, base_binfo, empty_p, &base_align, &v);
607cf131
MM
3836 }
3837
5c24fba6 3838 return v;
607cf131
MM
3839}
3840
58010b57
MM
3841/* Go through the TYPE_METHODS of T issuing any appropriate
3842 diagnostics, figuring out which methods override which other
3ef397c1 3843 methods, and so forth. */
58010b57
MM
3844
3845static void
3846check_methods (t)
3847 tree t;
3848{
3849 tree x;
834c6dff 3850 int seen_one_arg_array_delete_p = 0;
58010b57
MM
3851
3852 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3853 {
3854 GNU_xref_member (current_class_name, x);
3855
3856 /* If this was an evil function, don't keep it in class. */
3857 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3858 continue;
3859
58010b57 3860 check_for_override (x, t);
fee7654e 3861 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
58010b57
MM
3862 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3863
3864 /* The name of the field is the original field name
3865 Save this in auxiliary field for later overloading. */
3866 if (DECL_VINDEX (x))
3867 {
3ef397c1 3868 TYPE_POLYMORPHIC_P (t) = 1;
fee7654e
MM
3869 if (DECL_PURE_VIRTUAL_P (x))
3870 CLASSTYPE_PURE_VIRTUALS (t)
3871 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
58010b57 3872 }
834c6dff
MM
3873
3874 if (DECL_ARRAY_DELETE_OPERATOR_P (x))
3875 {
3876 tree second_parm;
3877
3878 /* When dynamically allocating an array of this type, we
3879 need a "cookie" to record how many elements we allocated,
3880 even if the array elements have no non-trivial
3881 destructor, if the usual array deallocation function
3882 takes a second argument of type size_t. The standard (in
3883 [class.free]) requires that the second argument be set
3884 correctly. */
3885 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
3886 /* This is overly conservative, but we must maintain this
3887 behavior for backwards compatibility. */
3888 if (!flag_new_abi && second_parm != void_list_node)
3889 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
3890 /* Under the new ABI, we choose only those function that are
3891 explicitly declared as `operator delete[] (void *,
3892 size_t)'. */
3893 else if (flag_new_abi
3894 && !seen_one_arg_array_delete_p
3895 && second_parm
3896 && TREE_CHAIN (second_parm) == void_list_node
3897 && same_type_p (TREE_VALUE (second_parm), sizetype))
3898 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
3899 /* If there's no second parameter, then this is the usual
3900 deallocation function. */
3901 else if (second_parm == void_list_node)
3902 seen_one_arg_array_delete_p = 1;
3903 }
58010b57 3904 }
58010b57
MM
3905}
3906
3907/* Remove all zero-width bit-fields from T. */
3908
3909static void
3910remove_zero_width_bit_fields (t)
3911 tree t;
3912{
3913 tree *fieldsp;
3914
3915 fieldsp = &TYPE_FIELDS (t);
3916 while (*fieldsp)
3917 {
3918 if (TREE_CODE (*fieldsp) == FIELD_DECL
3919 && DECL_C_BIT_FIELD (*fieldsp)
3920 && DECL_INITIAL (*fieldsp))
3921 *fieldsp = TREE_CHAIN (*fieldsp);
3922 else
3923 fieldsp = &TREE_CHAIN (*fieldsp);
3924 }
3925}
3926
607cf131
MM
3927/* Check the validity of the bases and members declared in T. Add any
3928 implicitly-generated functions (like copy-constructors and
3929 assignment operators). Compute various flag bits (like
3930 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
3931 level: i.e., independently of the ABI in use. */
3932
3933static void
3934check_bases_and_members (t, empty_p)
3935 tree t;
3936 int *empty_p;
3937{
3938 /* Nonzero if we are not allowed to generate a default constructor
3939 for this case. */
3940 int cant_have_default_ctor;
3941 /* Nonzero if the implicitly generated copy constructor should take
3942 a non-const reference argument. */
3943 int cant_have_const_ctor;
3944 /* Nonzero if the the implicitly generated assignment operator
3945 should take a non-const reference argument. */
3946 int no_const_asn_ref;
3947 tree access_decls;
3948
3949 /* By default, we use const reference arguments and generate default
3950 constructors. */
3951 cant_have_default_ctor = 0;
3952 cant_have_const_ctor = 0;
3953 no_const_asn_ref = 0;
3954
f9c528ea
MM
3955 /* Assume that the class is nearly empty; we'll clear this flag if
3956 it turns out not to be nearly empty. */
3957 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
3958
607cf131
MM
3959 /* Check all the base-classes. */
3960 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
3961 &no_const_asn_ref);
3962
3963 /* Check all the data member declarations. */
3964 check_field_decls (t, &access_decls, empty_p,
3965 &cant_have_default_ctor,
3966 &cant_have_const_ctor,
3967 &no_const_asn_ref);
3968
3969 /* Check all the method declarations. */
3970 check_methods (t);
3971
bbd15aac
MM
3972 /* A nearly-empty class has to be vptr-containing; a nearly empty
3973 class contains just a vptr. */
3974 if (!TYPE_CONTAINS_VPTR_P (t))
f9c528ea
MM
3975 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3976
607cf131
MM
3977 /* Do some bookkeeping that will guide the generation of implicitly
3978 declared member functions. */
3979 TYPE_HAS_COMPLEX_INIT_REF (t)
3ef397c1
MM
3980 |= (TYPE_HAS_INIT_REF (t)
3981 || TYPE_USES_VIRTUAL_BASECLASSES (t)
3982 || TYPE_POLYMORPHIC_P (t));
607cf131 3983 TYPE_NEEDS_CONSTRUCTING (t)
3ef397c1
MM
3984 |= (TYPE_HAS_CONSTRUCTOR (t)
3985 || TYPE_USES_VIRTUAL_BASECLASSES (t)
3986 || TYPE_POLYMORPHIC_P (t));
3987 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
3988 || TYPE_POLYMORPHIC_P (t));
607cf131
MM
3989 CLASSTYPE_NON_POD_P (t)
3990 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
3991 || TYPE_HAS_ASSIGN_REF (t));
3992 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3993 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3994 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3995
3996 /* Synthesize any needed methods. Note that methods will be synthesized
3997 for anonymous unions; grok_x_components undoes that. */
3998 add_implicitly_declared_members (t, cant_have_default_ctor,
3999 cant_have_const_ctor,
4000 no_const_asn_ref);
4001
aa52c1ff
JM
4002 /* Process the using-declarations. */
4003 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4004 handle_using_decl (TREE_VALUE (access_decls), t);
4005
607cf131
MM
4006 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4007 finish_struct_methods (t);
607cf131
MM
4008}
4009
3ef397c1 4010/* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5c24fba6
MM
4011 accordingly. If a new vfield was created (because T doesn't have a
4012 primary base class), then the newly created field is returned. It
c35cce41 4013 is not added to the TYPE_FIELDS list; it is the caller's
5c24fba6 4014 responsibility to do that. */
3ef397c1 4015
5c24fba6 4016static tree
da3d4dfa 4017create_vtable_ptr (t, empty_p, vfuns_p,
051e6fd7 4018 new_virtuals_p, overridden_virtuals_p)
3ef397c1
MM
4019 tree t;
4020 int *empty_p;
da3d4dfa 4021 int *vfuns_p;
051e6fd7
MM
4022 tree *new_virtuals_p;
4023 tree *overridden_virtuals_p;
3ef397c1
MM
4024{
4025 tree fn;
4026
3ef397c1
MM
4027 /* Loop over the virtual functions, adding them to our various
4028 vtables. */
4029 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4030 if (DECL_VINDEX (fn))
051e6fd7 4031 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
da3d4dfa 4032 vfuns_p, fn, t);
3ef397c1 4033
da3d4dfa
MM
4034 /* If we couldn't find an appropriate base class, create a new field
4035 here. Even if there weren't any new virtual functions, we might need a
bbd15aac
MM
4036 new virtual function table if we're supposed to include vptrs in
4037 all classes that need them. */
da3d4dfa
MM
4038 if (!TYPE_VFIELD (t)
4039 && (*vfuns_p
4040 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())))
3ef397c1
MM
4041 {
4042 /* We build this decl with vtbl_ptr_type_node, which is a
4043 `vtable_entry_type*'. It might seem more precise to use
4044 `vtable_entry_type (*)[N]' where N is the number of firtual
4045 functions. However, that would require the vtable pointer in
4046 base classes to have a different type than the vtable pointer
4047 in derived classes. We could make that happen, but that
4048 still wouldn't solve all the problems. In particular, the
4049 type-based alias analysis code would decide that assignments
4050 to the base class vtable pointer can't alias assignments to
4051 the derived class vtable pointer, since they have different
4052 types. Thus, in an derived class destructor, where the base
4053 class constructor was inlined, we could generate bad code for
4054 setting up the vtable pointer.
4055
4056 Therefore, we use one type for all vtable pointers. We still
4057 use a type-correct type; it's just doesn't indicate the array
4058 bounds. That's better than using `void*' or some such; it's
4059 cleaner, and it let's the alias analysis code know that these
4060 stores cannot alias stores to void*! */
4061 TYPE_VFIELD (t)
4062 = build_vtbl_or_vbase_field (get_vfield_name (t),
4063 get_identifier (VFIELD_BASE),
4064 vtbl_ptr_type_node,
4065 t,
07a3462a 4066 t,
3ef397c1
MM
4067 empty_p);
4068
5c24fba6
MM
4069 if (flag_new_abi && CLASSTYPE_N_BASECLASSES (t))
4070 /* If there were any baseclasses, they can't possibly be at
4071 offset zero any more, because that's where the vtable
4072 pointer is. So, converting to a base class is going to
4073 take work. */
4074 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
3ef397c1 4075
5c24fba6 4076 return TYPE_VFIELD (t);
3ef397c1 4077 }
5c24fba6
MM
4078
4079 return NULL_TREE;
3ef397c1
MM
4080}
4081
2ef16140
MM
4082/* Fixup the inline function given by INFO now that the class is
4083 complete. */
08b962b0 4084
2ef16140
MM
4085static void
4086fixup_pending_inline (info)
4087 struct pending_inline *info;
4088{
4089 if (info)
4090 {
4091 tree args;
4092 tree fn = info->fndecl;
08b962b0 4093
2ef16140
MM
4094 args = DECL_ARGUMENTS (fn);
4095 while (args)
4096 {
4097 DECL_CONTEXT (args) = fn;
4098 args = TREE_CHAIN (args);
4099 }
4100 }
4101}
08b962b0 4102
2ef16140
MM
4103/* Fixup the inline methods and friends in TYPE now that TYPE is
4104 complete. */
08b962b0 4105
2ef16140
MM
4106static void
4107fixup_inline_methods (type)
4108 tree type;
08b962b0 4109{
2ef16140 4110 tree method = TYPE_METHODS (type);
08b962b0 4111
2ef16140 4112 if (method && TREE_CODE (method) == TREE_VEC)
08b962b0 4113 {
2ef16140
MM
4114 if (TREE_VEC_ELT (method, 1))
4115 method = TREE_VEC_ELT (method, 1);
4116 else if (TREE_VEC_ELT (method, 0))
4117 method = TREE_VEC_ELT (method, 0);
08b962b0 4118 else
2ef16140 4119 method = TREE_VEC_ELT (method, 2);
08b962b0
MM
4120 }
4121
2ef16140
MM
4122 /* Do inline member functions. */
4123 for (; method; method = TREE_CHAIN (method))
4124 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
08b962b0 4125
2ef16140
MM
4126 /* Do friends. */
4127 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4128 method;
4129 method = TREE_CHAIN (method))
4130 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
351c54c8 4131 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
2ef16140 4132}
08b962b0 4133
9d4c0187
MM
4134/* Called from propagate_binfo_offsets via dfs_walk. */
4135
4136static tree
4137dfs_propagate_binfo_offsets (binfo, data)
4138 tree binfo;
4139 void *data;
4140{
4141 tree offset = (tree) data;
4142
770ae6cc
RK
4143 /* Update the BINFO_OFFSET for this base. Allow for the case where it
4144 might be negative. */
4145 BINFO_OFFSET (binfo)
4146 = convert (sizetype, size_binop (PLUS_EXPR,
4147 convert (ssizetype, BINFO_OFFSET (binfo)),
4148 offset));
9d4c0187
MM
4149 SET_BINFO_MARKED (binfo);
4150
4151 return NULL_TREE;
4152}
4153
4154/* Add OFFSET to all base types of BINFO which is a base in the
4155 hierarchy dominated by T.
80fd5f48
MM
4156
4157 OFFSET, which is a type offset, is number of bytes.
4158
4159 Note that we don't have to worry about having two paths to the
4160 same base type, since this type owns its association list. */
4161
4162static void
4163propagate_binfo_offsets (binfo, offset)
4164 tree binfo;
4165 tree offset;
4166{
9d4c0187
MM
4167 dfs_walk (binfo,
4168 dfs_propagate_binfo_offsets,
4169 dfs_skip_nonprimary_vbases_unmarkedp,
4170 offset);
4171 dfs_walk (binfo,
4172 dfs_unmark,
4173 dfs_skip_nonprimary_vbases_markedp,
4174 NULL);
80fd5f48
MM
4175}
4176
9d4c0187 4177/* Called via dfs_walk from layout_virtual bases. */
80fd5f48
MM
4178
4179static tree
9d4c0187 4180dfs_set_offset_for_shared_vbases (binfo, data)
80fd5f48
MM
4181 tree binfo;
4182 void *data;
4183{
9d4c0187 4184 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
80fd5f48 4185 {
9d4c0187
MM
4186 /* Update the shared copy. */
4187 tree shared_binfo;
80fd5f48 4188
9d4c0187
MM
4189 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4190 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
80fd5f48
MM
4191 }
4192
9d4c0187
MM
4193 return NULL_TREE;
4194}
4195
4196/* Called via dfs_walk from layout_virtual bases. */
4197
4198static tree
4199dfs_set_offset_for_unshared_vbases (binfo, data)
4200 tree binfo;
4201 void *data;
4202{
4203 /* If this is a virtual base, make sure it has the same offset as
4204 the shared copy. If it's a primary base, then we know it's
4205 correct. */
4206 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4207 {
4208 tree t = (tree) data;
4209 tree vbase;
4210 tree offset;
4211
4212 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
cd8ed629 4213 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
9d4c0187
MM
4214 propagate_binfo_offsets (binfo, offset);
4215 }
80fd5f48
MM
4216
4217 return NULL_TREE;
4218}
4219
4220/* Set BINFO_OFFSET for all of the virtual bases for T. Update
9785e4b1
MM
4221 TYPE_ALIGN and TYPE_SIZE for T. BASE_OFFSETS is a varray mapping
4222 offsets to the types at those offsets. */
80fd5f48 4223
d2c5305b 4224static void
9785e4b1 4225layout_virtual_bases (t, base_offsets)
80fd5f48 4226 tree t;
9785e4b1 4227 varray_type *base_offsets;
80fd5f48 4228{
c35cce41 4229 tree vbases;
665f2503 4230 unsigned HOST_WIDE_INT dsize;
9785e4b1
MM
4231 unsigned HOST_WIDE_INT eoc;
4232
4233 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4234 return;
4235
4236#ifdef STRUCTURE_SIZE_BOUNDARY
4237 /* Packed structures don't need to have minimum size. */
736fb497
MM
4238 if (! TYPE_PACKED (t))
4239 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
9785e4b1 4240#endif
80fd5f48 4241
80fd5f48 4242 /* DSIZE is the size of the class without the virtual bases. */
665f2503
RK
4243 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4244
80fd5f48
MM
4245 /* Make every class have alignment of at least one. */
4246 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4247
9d4c0187 4248 /* Go through the virtual bases, allocating space for each virtual
c35cce41
MM
4249 base that is not already a primary base class. Under the new
4250 ABI, these are allocated according to a depth-first left-to-right
4251 postorder traversal; in the new ABI, inheritance graph order is
4252 used instead. */
4253 for (vbases = (flag_new_abi
4254 ? TYPE_BINFO (t)
4255 : CLASSTYPE_VBASECLASSES (t));
4256 vbases;
4257 vbases = TREE_CHAIN (vbases))
4258 {
4259 tree vbase;
4260
4261 if (!TREE_VIA_VIRTUAL (vbases))
4262 continue;
4263
4264 if (flag_new_abi)
4265 vbase = BINFO_FOR_VBASE (BINFO_TYPE (vbases), t);
4266 else
4267 vbase = vbases;
4268
4269 if (!BINFO_VBASE_PRIMARY_P (vbase))
4270 {
4271 /* This virtual base is not a primary base of any class in the
4272 hierarchy, so we have to add space for it. */
4273 tree basetype;
4274 unsigned int desired_align;
80fd5f48 4275
c35cce41 4276 basetype = BINFO_TYPE (vbase);
9785e4b1 4277
c35cce41
MM
4278 if (flag_new_abi)
4279 desired_align = CLASSTYPE_ALIGN (basetype);
4280 else
4281 /* Under the old ABI, virtual bases were aligned as for the
ec386958
MM
4282 entire base object (including its virtual bases). That's
4283 wasteful, in general. */
c35cce41
MM
4284 desired_align = TYPE_ALIGN (basetype);
4285 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4286
4287 /* Add padding so that we can put the virtual base class at an
4288 appropriately aligned offset. */
4289 dsize = CEIL (dsize, desired_align) * desired_align;
4290
4291 /* Under the new ABI, we try to squish empty virtual bases in
4292 just like ordinary empty bases. */
4293 if (flag_new_abi && is_empty_class (basetype))
4294 layout_empty_base (vbase,
4295 size_int (CEIL (dsize, BITS_PER_UNIT)),
4296 *base_offsets);
4297 else
4298 {
4299 /* And compute the offset of the virtual base. */
4300 propagate_binfo_offsets (vbase,
4301 ssize_int (CEIL (dsize, BITS_PER_UNIT)));
4302 /* Every virtual baseclass takes a least a UNIT, so that
4303 we can take it's address and get something different
4304 for each base. */
4305 dsize += MAX (BITS_PER_UNIT,
4306 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4307 }
9785e4b1 4308
c35cce41
MM
4309 /* Keep track of the offsets assigned to this virtual base. */
4310 record_base_offsets (vbase, base_offsets);
4311 }
4312 }
80fd5f48 4313
9d4c0187
MM
4314 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4315 BINFO_OFFSET set correctly. Those we just allocated certainly
4316 will. The others are primary baseclasses; we walk the hierarchy
4317 to find the primary copies and update the shared copy. */
4318 dfs_walk (TYPE_BINFO (t),
4319 dfs_set_offset_for_shared_vbases,
4320 dfs_unmarked_real_bases_queue_p,
4321 t);
4322
4323 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4324 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4325 bases and their direct and indirect bases. The ambiguity checks
4326 in get_base_distance depend on the BINFO_OFFSETs being set
4327 correctly. */
4328 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
c35cce41
MM
4329 for (vbases = CLASSTYPE_VBASECLASSES (t);
4330 vbases;
4331 vbases = TREE_CHAIN (vbases))
4332 dfs_walk (vbases, dfs_set_offset_for_unshared_vbases, NULL, t);
9d4c0187 4333
9785e4b1
MM
4334 /* If we had empty base classes that protruded beyond the end of the
4335 class, we didn't update DSIZE above; we were hoping to overlay
4336 multiple such bases at the same location. */
4337 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4338 if (eoc * BITS_PER_UNIT > dsize)
4339 dsize = (eoc + 1) * BITS_PER_UNIT;
4340
80fd5f48
MM
4341 /* Now, make sure that the total size of the type is a multiple of
4342 its alignment. */
4343 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
fed3cef0
RK
4344 TYPE_SIZE (t) = bitsize_int (dsize);
4345 TYPE_SIZE_UNIT (t) = convert (sizetype,
770ae6cc
RK
4346 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4347 bitsize_unit_node));
9785e4b1
MM
4348
4349 /* Check for ambiguous virtual bases. */
4350 if (extra_warnings)
c35cce41
MM
4351 for (vbases = CLASSTYPE_VBASECLASSES (t);
4352 vbases;
4353 vbases = TREE_CHAIN (vbases))
9785e4b1 4354 {
c35cce41 4355 tree basetype = BINFO_TYPE (vbases);
9785e4b1
MM
4356 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4357 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4358 basetype, t);
4359 }
80fd5f48
MM
4360}
4361
9785e4b1
MM
4362/* Returns the offset of the byte just past the end of the base class
4363 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4364 only non-virtual bases are included. */
80fd5f48 4365
9785e4b1
MM
4366static unsigned HOST_WIDE_INT
4367end_of_class (t, include_virtuals_p)
4368 tree t;
4369 int include_virtuals_p;
80fd5f48 4370{
9785e4b1
MM
4371 unsigned HOST_WIDE_INT result = 0;
4372 int i;
80fd5f48 4373
9785e4b1
MM
4374 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4375 {
4376 tree base_binfo;
4377 tree offset;
4378 unsigned HOST_WIDE_INT end_of_base;
5c24fba6 4379
9785e4b1
MM
4380 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4381
4382 if (!include_virtuals_p
4383 && TREE_VIA_VIRTUAL (base_binfo)
4384 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4385 continue;
80fd5f48 4386
9785e4b1
MM
4387 offset = size_binop (PLUS_EXPR,
4388 BINFO_OFFSET (base_binfo),
4389 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
4390 end_of_base = tree_low_cst (offset, /*pos=*/1);
4391 if (end_of_base > result)
4392 result = end_of_base;
4393 }
80fd5f48 4394
9785e4b1 4395 return result;
80fd5f48
MM
4396}
4397
2ef16140
MM
4398/* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4399 BINFO_OFFSETs for all of the base-classes. Position the vtable
4400 pointer. */
607cf131 4401
2ef16140 4402static void
da3d4dfa 4403layout_class_type (t, empty_p, vfuns_p,
051e6fd7 4404 new_virtuals_p, overridden_virtuals_p)
2ef16140
MM
4405 tree t;
4406 int *empty_p;
da3d4dfa 4407 int *vfuns_p;
051e6fd7
MM
4408 tree *new_virtuals_p;
4409 tree *overridden_virtuals_p;
2ef16140 4410{
5c24fba6
MM
4411 tree non_static_data_members;
4412 tree field;
4413 tree vptr;
4414 record_layout_info rli;
4415 varray_type v;
9785e4b1 4416 unsigned HOST_WIDE_INT eoc;
5c24fba6
MM
4417
4418 /* Keep track of the first non-static data member. */
4419 non_static_data_members = TYPE_FIELDS (t);
4420
770ae6cc
RK
4421 /* Start laying out the record. */
4422 rli = start_record_layout (t);
534170eb 4423
8026246f
MM
4424 /* If possible, we reuse the virtual function table pointer from one
4425 of our base classes. */
da3d4dfa 4426 determine_primary_base (t, vfuns_p);
8026246f 4427
5c24fba6 4428 /* Create a pointer to our virtual function table. */
da3d4dfa 4429 vptr = create_vtable_ptr (t, empty_p, vfuns_p,
5c24fba6
MM
4430 new_virtuals_p, overridden_virtuals_p);
4431
4432 /* Under the new ABI, the vptr is always the first thing in the
4433 class. */
4434 if (flag_new_abi && vptr)
4435 {
4436 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
770ae6cc 4437 place_field (rli, vptr);
5c24fba6
MM
4438 }
4439
607cf131 4440 /* Add pointers to all of our virtual base-classes. */
5c24fba6 4441 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (rli, empty_p),
607cf131
MM
4442 TYPE_FIELDS (t));
4443 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5c24fba6 4444 v = build_base_fields (rli, empty_p);
8d08fdba 4445
c1aa4de7
MM
4446 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4447 we have to save this before we start modifying
4448 TYPE_NONCOPIED_PARTS. */
2ef16140 4449 fixup_inline_methods (t);
c1aa4de7 4450
5c24fba6 4451 /* Layout the non-static data members. */
770ae6cc 4452 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
5c24fba6
MM
4453 {
4454 tree binfo;
01955e96
MM
4455 tree type;
4456 tree padding;
5c24fba6
MM
4457
4458 /* We still pass things that aren't non-static data members to
4459 the back-end, in case it wants to do something with them. */
4460 if (TREE_CODE (field) != FIELD_DECL)
4461 {
770ae6cc 4462 place_field (rli, field);
5c24fba6
MM
4463 continue;
4464 }
4465
01955e96
MM
4466 type = TREE_TYPE (field);
4467
4468 /* If this field is a bit-field whose width is greater than its
4469 type, then there are some special rules for allocating it
4470 under the new ABI. Under the old ABI, there were no special
4471 rules, but the back-end can't handle bitfields longer than a
4472 `long long', so we use the same mechanism. */
4473 if (DECL_C_BIT_FIELD (field)
4474 && ((flag_new_abi
4475 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4476 || (!flag_new_abi
770ae6cc
RK
4477 && 0 < compare_tree_int (DECL_SIZE (field),
4478 TYPE_PRECISION
4479 (long_long_unsigned_type_node)))))
01955e96
MM
4480 {
4481 integer_type_kind itk;
4482 tree integer_type;
4483
4484 /* We must allocate the bits as if suitably aligned for the
4485 longest integer type that fits in this many bits. type
4486 of the field. Then, we are supposed to use the left over
4487 bits as additional padding. */
4488 for (itk = itk_char; itk != itk_none; ++itk)
4489 if (INT_CST_LT (DECL_SIZE (field),
4490 TYPE_SIZE (integer_types[itk])))
4491 break;
4492
4493 /* ITK now indicates a type that is too large for the
4494 field. We have to back up by one to find the largest
4495 type that fits. */
4496 integer_type = integer_types[itk - 1];
770ae6cc
RK
4497 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4498 TYPE_SIZE (integer_type));
01955e96
MM
4499 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4500 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4501 }
4502 else
4503 padding = NULL_TREE;
4504
5c24fba6 4505 /* Create a dummy BINFO corresponding to this field. */
01955e96 4506 binfo = make_binfo (size_zero_node, type, NULL_TREE, NULL_TREE);
5c24fba6
MM
4507 unshare_base_binfos (binfo);
4508 layout_nonempty_base_or_field (rli, field, binfo, v);
01955e96
MM
4509
4510 /* If we needed additional padding after this field, add it
4511 now. */
4512 if (padding)
4513 {
4514 tree padding_field;
4515
4516 padding_field = build_decl (FIELD_DECL,
4517 NULL_TREE,
4518 char_type_node);
4519 DECL_BIT_FIELD (padding_field) = 1;
4520 DECL_SIZE (padding_field) = padding;
4521 DECL_ALIGN (padding_field) = 1;
4522 layout_nonempty_base_or_field (rli, padding_field, NULL_TREE, v);
4523 }
5c24fba6
MM
4524 }
4525
5c24fba6
MM
4526 /* It might be the case that we grew the class to allocate a
4527 zero-sized base class. That won't be reflected in RLI, yet,
4528 because we are willing to overlay multiple bases at the same
4529 offset. However, now we need to make sure that RLI is big enough
4530 to reflect the entire class. */
9785e4b1 4531 eoc = end_of_class (t, /*include_virtuals_p=*/0);
770ae6cc
RK
4532 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4533 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
5c24fba6 4534 {
9785e4b1
MM
4535 /* We don't handle zero-sized base classes specially under the
4536 old ABI, so if we get here, we had better be operating under
4537 the new ABI rules. */
4538 my_friendly_assert (flag_new_abi, 20000321);
770ae6cc
RK
4539 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc + 1));
4540 rli->bitpos = bitsize_zero_node;
5c24fba6
MM
4541 }
4542
58010b57 4543 /* We make all structures have at least one element, so that they
5c24fba6
MM
4544 have non-zero size. In the new ABI, the class may be empty even
4545 if it has basetypes. Therefore, we add the fake field after all
4546 the other fields; if there are already FIELD_DECLs on the list,
4547 their offsets will not be disturbed. */
2ef16140 4548 if (*empty_p)
691c003d 4549 {
5c24fba6
MM
4550 tree padding;
4551
534170eb 4552 padding = build_lang_decl (FIELD_DECL, NULL_TREE, char_type_node);
770ae6cc 4553 place_field (rli, padding);
c1aa4de7 4554 TYPE_NONCOPIED_PARTS (t)
534170eb 4555 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
c1aa4de7 4556 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
691c003d 4557 }
c1aa4de7 4558
5c24fba6
MM
4559 /* Under the old ABI, the vptr comes at the very end of the
4560 class. */
4561 if (!flag_new_abi && vptr)
4562 {
770ae6cc 4563 place_field (rli, vptr);
5c24fba6
MM
4564 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), vptr);
4565 }
4566
3ef397c1
MM
4567 /* Let the back-end lay out the type. Note that at this point we
4568 have only included non-virtual base-classes; we will lay out the
4569 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4570 this call are not necessarily correct; they are just the size and
4571 alignment when no virtual base clases are used. */
5c24fba6 4572 finish_record_layout (rli);
58010b57 4573
3ef397c1
MM
4574 /* Delete all zero-width bit-fields from the list of fields. Now
4575 that the type is laid out they are no longer important. */
4576 remove_zero_width_bit_fields (t);
4577
9a71c18b 4578 /* Remember the size and alignment of the class before adding
0b41abe6 4579 the virtual bases. */
2ef16140 4580 if (*empty_p && flag_new_abi)
06ceef4e 4581 {
770ae6cc 4582 CLASSTYPE_SIZE (t) = bitsize_zero_node;
fed3cef0 4583 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
06ceef4e 4584 }
6bc39009
JM
4585 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
4586 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
06ceef4e
RK
4587 {
4588 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4589 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
4590 }
732dcb6f 4591 else
06ceef4e
RK
4592 {
4593 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4594 CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
4595 }
4596
0b41abe6
JM
4597 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4598
8d08fdba
MS
4599 /* Set the TYPE_DECL for this type to contain the right
4600 value for DECL_OFFSET, so that we can use it as part
4601 of a COMPONENT_REF for multiple inheritance. */
d2e5ee5c 4602 layout_decl (TYPE_MAIN_DECL (t), 0);
8d08fdba 4603
7177d104
MS
4604 /* Now fix up any virtual base class types that we left lying
4605 around. We must get these done before we try to lay out the
5c24fba6
MM
4606 virtual function table. As a side-effect, this will remove the
4607 base subobject fields. */
9785e4b1
MM
4608 layout_virtual_bases (t, &v);
4609
4610 /* Clean up. */
4611 VARRAY_FREE (v);
2ef16140 4612}
c35cce41 4613
2ef16140
MM
4614/* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
4615 (or C++ class declaration).
4616
4617 For C++, we must handle the building of derived classes.
4618 Also, C++ allows static class members. The way that this is
4619 handled is to keep the field name where it is (as the DECL_NAME
665f2503 4620 of the field), and place the overloaded decl in the bit position
2ef16140
MM
4621 of the field. layout_record and layout_union will know about this.
4622
4623 More C++ hair: inline functions have text in their
4624 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
4625 meaningful tree structure. After the struct has been laid out, set
4626 things up so that this can happen.
4627
4628 And still more: virtual functions. In the case of single inheritance,
4629 when a new virtual function is seen which redefines a virtual function
4630 from the base class, the new virtual function is placed into
4631 the virtual function table at exactly the same address that
4632 it had in the base class. When this is extended to multiple
4633 inheritance, the same thing happens, except that multiple virtual
4634 function tables must be maintained. The first virtual function
4635 table is treated in exactly the same way as in the case of single
4636 inheritance. Additional virtual function tables have different
4637 DELTAs, which tell how to adjust `this' to point to the right thing.
4638
4639 ATTRIBUTES is the set of decl attributes to be applied, if any. */
4640
4641void
4642finish_struct_1 (t)
4643 tree t;
4644{
4645 tree x;
da3d4dfa 4646 int vfuns;
051e6fd7
MM
4647 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
4648 a FUNCTION_DECL. Each of these functions is a virtual function
4649 declared in T that does not override any virtual function from a
4650 base class. */
4651 tree new_virtuals = NULL_TREE;
4652 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
4653 except that each declaration here overrides the declaration from
4654 a base class. */
4655 tree overridden_virtuals = NULL_TREE;
2ef16140
MM
4656 int n_fields = 0;
4657 tree vfield;
2ef16140
MM
4658 int empty = 1;
4659
d0f062fb 4660 if (COMPLETE_TYPE_P (t))
2ef16140
MM
4661 {
4662 if (IS_AGGR_TYPE (t))
4663 cp_error ("redefinition of `%#T'", t);
4664 else
4665 my_friendly_abort (172);
4666 popclass ();
4667 return;
4668 }
4669
4670 GNU_xref_decl (current_function_decl, t);
4671
4672 /* If this type was previously laid out as a forward reference,
4673 make sure we lay it out again. */
2ef16140
MM
4674 TYPE_SIZE (t) = NULL_TREE;
4675 CLASSTYPE_GOT_SEMICOLON (t) = 0;
2ef16140 4676 CLASSTYPE_VFIELD_PARENT (t) = -1;
da3d4dfa 4677 vfuns = 0;
2ef16140 4678 CLASSTYPE_RTTI (t) = NULL_TREE;
2ef16140
MM
4679
4680 /* Do end-of-class semantic processing: checking the validity of the
03702748 4681 bases and members and add implicitly generated methods. */
2ef16140
MM
4682 check_bases_and_members (t, &empty);
4683
4684 /* Layout the class itself. */
da3d4dfa 4685 layout_class_type (t, &empty, &vfuns,
051e6fd7 4686 &new_virtuals, &overridden_virtuals);
8ebeee52 4687
2986ae00
MS
4688 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4689 might need to know it for setting up the offsets in the vtable
4690 (or in thunks) below. */
3ef397c1 4691 vfield = TYPE_VFIELD (t);
2986ae00
MS
4692 if (vfield != NULL_TREE
4693 && DECL_FIELD_CONTEXT (vfield) != t)
4694 {
ec386958
MM
4695 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4696
2986ae00
MS
4697 vfield = copy_node (vfield);
4698 copy_lang_decl (vfield);
4699
2986ae00 4700 DECL_FIELD_CONTEXT (vfield) = t;
770ae6cc
RK
4701 DECL_FIELD_OFFSET (vfield)
4702 = size_binop (PLUS_EXPR,
ec386958 4703 BINFO_OFFSET (binfo),
770ae6cc 4704 DECL_FIELD_OFFSET (vfield));
d3a3fb6a 4705 TYPE_VFIELD (t) = vfield;
2986ae00 4706 }
4c6b7393 4707
a68ad5bd 4708 overridden_virtuals
da3d4dfa 4709 = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
db5ae43f 4710
5e19c053 4711 /* If necessary, create the primary vtable for this class. */
051e6fd7 4712 if (new_virtuals
a68ad5bd 4713 || overridden_virtuals
bbd15aac 4714 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
8d08fdba 4715 {
051e6fd7 4716 new_virtuals = nreverse (new_virtuals);
8d08fdba 4717 /* We must enter these virtuals into the table. */
3ef397c1 4718 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
da3d4dfa 4719 build_primary_vtable (NULL_TREE, t);
3c9d6359 4720 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
0533d788
MM
4721 /* Here we know enough to change the type of our virtual
4722 function table, but we will wait until later this function. */
28531dd0 4723 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
8d08fdba
MS
4724
4725 /* If this type has basetypes with constructors, then those
4726 constructors might clobber the virtual function table. But
4727 they don't if the derived class shares the exact vtable of the base
4728 class. */
4729
4730 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4731 }
bbd15aac
MM
4732 /* If we didn't need a new vtable, see if we should copy one from
4733 the base. */
3ef397c1 4734 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
8d08fdba 4735 {
3ef397c1
MM
4736 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
4737
8d08fdba
MS
4738 /* This class contributes nothing new to the virtual function
4739 table. However, it may have declared functions which
4740 went into the virtual function table "inherited" from the
4741 base class. If so, we grab a copy of those updated functions,
4742 and pretend they are ours. */
4743
4744 /* See if we should steal the virtual info from base class. */
4745 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4746 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4747 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4748 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4749 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4750 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4751 }
4752
bbd15aac 4753 if (TYPE_CONTAINS_VPTR_P (t))
8d08fdba 4754 {
1eb4bea9
MM
4755 if (TYPE_BINFO_VTABLE (t))
4756 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
4757 20000116);
4758 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4759 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
4760 20000116);
4761
da3d4dfa 4762 CLASSTYPE_VSIZE (t) = vfuns;
1eb4bea9
MM
4763 /* Entries for virtual functions defined in the primary base are
4764 followed by entries for new functions unique to this class. */
4765 TYPE_BINFO_VIRTUALS (t)
051e6fd7 4766 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
a68ad5bd
MM
4767 /* Finally, add entries for functions that override virtuals
4768 from non-primary bases. */
4769 TYPE_BINFO_VIRTUALS (t)
4770 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
8d08fdba
MS
4771 }
4772
3ef397c1
MM
4773 /* If we created a new vtbl pointer for this class, add it to the
4774 list. */
ca36f057 4775 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3ef397c1
MM
4776 CLASSTYPE_VFIELDS (t)
4777 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
8d08fdba 4778
d2c5305b 4779 finish_struct_bits (t);
8d08fdba 4780
f30432d7
MS
4781 /* Complete the rtl for any static member objects of the type we're
4782 working on. */
58010b57 4783 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
8d08fdba 4784 {
8d08fdba
MS
4785 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4786 && TREE_TYPE (x) == t)
4787 {
4788 DECL_MODE (x) = TYPE_MODE (t);
4789 make_decl_rtl (x, NULL, 0);
4790 }
4791 }
4792
f90cdf34 4793 /* Done with FIELDS...now decide whether to sort these for
58010b57 4794 faster lookups later.
f90cdf34
MT
4795
4796 The C front-end only does this when n_fields > 15. We use
4797 a smaller number because most searches fail (succeeding
4798 ultimately as the search bores through the inheritance
4799 hierarchy), and we want this failure to occur quickly. */
4800
58010b57
MM
4801 n_fields = count_fields (TYPE_FIELDS (t));
4802 if (n_fields > 7)
f90cdf34
MT
4803 {
4804 tree field_vec = make_tree_vec (n_fields);
58010b57 4805 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
f90cdf34
MT
4806 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
4807 (int (*)(const void *, const void *))field_decl_cmp);
4808 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
4809 retrofit_lang_decl (TYPE_MAIN_DECL (t));
4810 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
4811 }
4812
8d08fdba
MS
4813 if (TYPE_HAS_CONSTRUCTOR (t))
4814 {
4815 tree vfields = CLASSTYPE_VFIELDS (t);
4816
4817 while (vfields)
4818 {
4819 /* Mark the fact that constructor for T
4820 could affect anybody inheriting from T
4821 who wants to initialize vtables for VFIELDS's type. */
4822 if (VF_DERIVED_VALUE (vfields))
4823 TREE_ADDRESSABLE (vfields) = 1;
4824 vfields = TREE_CHAIN (vfields);
4825 }
8d08fdba 4826 }
8d08fdba 4827
8d7a5379
MM
4828 /* Make the rtl for any new vtables we have created, and unmark
4829 the base types we marked. */
4830 finish_vtbls (t);
4831
c35cce41 4832 if (TYPE_VFIELD (t))
8d08fdba 4833 {
e92cc029 4834 /* In addition to this one, all the other vfields should be listed. */
8d08fdba
MS
4835 /* Before that can be done, we have to have FIELD_DECLs for them, and
4836 a place to find them. */
c1aa4de7
MM
4837 TYPE_NONCOPIED_PARTS (t)
4838 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
3ef397c1 4839 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
8d08fdba
MS
4840
4841 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
58010b57 4842 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
8251199e 4843 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
8d08fdba
MS
4844 t);
4845 }
4846
8145f082 4847 hack_incomplete_structures (t);
8d08fdba 4848
9e9ff709
MS
4849 if (warn_overloaded_virtual)
4850 warn_hidden (t);
8d08fdba 4851
ae673f14 4852 maybe_suppress_debug_info (t);
8d08fdba 4853
d2e5ee5c
MS
4854 /* Finish debugging output for this type. */
4855 rest_of_type_compilation (t, toplevel_bindings_p ());
8d08fdba 4856}
f30432d7 4857
61a127b3
MM
4858/* When T was built up, the member declarations were added in reverse
4859 order. Rearrange them to declaration order. */
4860
4861void
4862unreverse_member_declarations (t)
4863 tree t;
4864{
4865 tree next;
4866 tree prev;
4867 tree x;
4868
4869 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4870 reverse order. Put them in declaration order now. */
4871 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
4872 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
4873
4874 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4875 reverse order, so we can't just use nreverse. */
4876 prev = NULL_TREE;
4877 for (x = TYPE_FIELDS (t);
4878 x && TREE_CODE (x) != TYPE_DECL;
4879 x = next)
4880 {
4881 next = TREE_CHAIN (x);
4882 TREE_CHAIN (x) = prev;
4883 prev = x;
4884 }
4885 if (prev)
4886 {
4887 TREE_CHAIN (TYPE_FIELDS (t)) = x;
4888 if (prev)
4889 TYPE_FIELDS (t) = prev;
4890 }
4891}
4892
f30432d7 4893tree
9f33663b 4894finish_struct (t, attributes)
61a127b3 4895 tree t, attributes;
f30432d7 4896{
61a127b3
MM
4897 /* Now that we've got all the field declarations, reverse everything
4898 as necessary. */
4899 unreverse_member_declarations (t);
f30432d7 4900
6467930b
MS
4901 cplus_decl_attributes (t, attributes, NULL_TREE);
4902
5566b478 4903 if (processing_template_decl)
f30432d7 4904 {
b0e0b31f 4905 finish_struct_methods (t);
867580ce 4906 TYPE_SIZE (t) = bitsize_zero_node;
6f1b4c42 4907 }
f30432d7 4908 else
9f33663b 4909 finish_struct_1 (t);
5566b478
MS
4910
4911 TYPE_BEING_DEFINED (t) = 0;
8f032717 4912
5566b478 4913 if (current_class_type)
b74a0560 4914 popclass ();
5566b478 4915 else
8251199e 4916 error ("trying to finish struct, but kicked out due to previous parse errors.");
5566b478 4917
ae673f14
JM
4918 if (processing_template_decl)
4919 {
4920 tree scope = current_scope ();
4921 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
4922 add_tree (build_min (TAG_DEFN, t));
4923 }
4924
5566b478 4925 return t;
f30432d7 4926}
8d08fdba 4927\f
51ddb82e 4928/* Return the dynamic type of INSTANCE, if known.
8d08fdba
MS
4929 Used to determine whether the virtual function table is needed
4930 or not.
4931
4932 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
97d953bb
MM
4933 of our knowledge of its type. *NONNULL should be initialized
4934 before this function is called. */
e92cc029 4935
d8e178a0 4936static tree
51ddb82e 4937fixed_type_or_null (instance, nonnull)
8d08fdba
MS
4938 tree instance;
4939 int *nonnull;
4940{
4941 switch (TREE_CODE (instance))
4942 {
4943 case INDIRECT_REF:
4944 /* Check that we are not going through a cast of some sort. */
4945 if (TREE_TYPE (instance)
4946 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4947 instance = TREE_OPERAND (instance, 0);
4948 /* fall through... */
4949 case CALL_EXPR:
4950 /* This is a call to a constructor, hence it's never zero. */
4951 if (TREE_HAS_CONSTRUCTOR (instance))
4952 {
4953 if (nonnull)
4954 *nonnull = 1;
51ddb82e 4955 return TREE_TYPE (instance);
8d08fdba 4956 }
51ddb82e 4957 return NULL_TREE;
8d08fdba
MS
4958
4959 case SAVE_EXPR:
4960 /* This is a call to a constructor, hence it's never zero. */
4961 if (TREE_HAS_CONSTRUCTOR (instance))
4962 {
4963 if (nonnull)
4964 *nonnull = 1;
51ddb82e 4965 return TREE_TYPE (instance);
8d08fdba 4966 }
51ddb82e 4967 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba
MS
4968
4969 case RTL_EXPR:
51ddb82e 4970 return NULL_TREE;
8d08fdba
MS
4971
4972 case PLUS_EXPR:
4973 case MINUS_EXPR:
4974 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4975 /* Propagate nonnull. */
51ddb82e 4976 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba 4977 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
51ddb82e
JM
4978 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4979 return NULL_TREE;
8d08fdba
MS
4980
4981 case NOP_EXPR:
4982 case CONVERT_EXPR:
51ddb82e 4983 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba
MS
4984
4985 case ADDR_EXPR:
4986 if (nonnull)
4987 *nonnull = 1;
51ddb82e 4988 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
8d08fdba
MS
4989
4990 case COMPONENT_REF:
51ddb82e 4991 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
8d08fdba 4992
8d08fdba
MS
4993 case VAR_DECL:
4994 case FIELD_DECL:
4995 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4996 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4997 {
4998 if (nonnull)
4999 *nonnull = 1;
51ddb82e 5000 return TREE_TYPE (TREE_TYPE (instance));
8d08fdba 5001 }
e92cc029 5002 /* fall through... */
8d08fdba
MS
5003 case TARGET_EXPR:
5004 case PARM_DECL:
5005 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5006 {
5007 if (nonnull)
5008 *nonnull = 1;
51ddb82e 5009 return TREE_TYPE (instance);
8d08fdba
MS
5010 }
5011 else if (nonnull)
5012 {
4ac14744 5013 if (instance == current_class_ptr
8d08fdba
MS
5014 && flag_this_is_variable <= 0)
5015 {
51ddb82e
JM
5016 /* Normally, 'this' must be non-null. */
5017 if (flag_this_is_variable == 0)
5018 *nonnull = 1;
5019
5020 /* <0 means we're in a constructor and we know our type. */
8d08fdba 5021 if (flag_this_is_variable < 0)
51ddb82e 5022 return TREE_TYPE (TREE_TYPE (instance));
8d08fdba
MS
5023 }
5024 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5025 /* Reference variables should be references to objects. */
5026 *nonnull = 1;
5027 }
51ddb82e 5028 return NULL_TREE;
8d08fdba
MS
5029
5030 default:
51ddb82e 5031 return NULL_TREE;
8d08fdba
MS
5032 }
5033}
51ddb82e
JM
5034
5035/* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5036 to the static type. We also handle the case where INSTANCE is really
5037 a pointer.
5038
5039 Used to determine whether the virtual function table is needed
5040 or not.
5041
5042 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
97d953bb
MM
5043 of our knowledge of its type. *NONNULL should be initialized
5044 before this function is called. */
51ddb82e
JM
5045
5046int
5047resolves_to_fixed_type_p (instance, nonnull)
5048 tree instance;
5049 int *nonnull;
5050{
5051 tree t = TREE_TYPE (instance);
5052 tree fixed = fixed_type_or_null (instance, nonnull);
5053 if (fixed == NULL_TREE)
5054 return 0;
5055 if (POINTER_TYPE_P (t))
5056 t = TREE_TYPE (t);
3bfdc719 5057 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
51ddb82e
JM
5058}
5059
8d08fdba
MS
5060\f
5061void
5062init_class_processing ()
5063{
5064 current_class_depth = 0;
61a127b3
MM
5065 current_class_stack_size = 10;
5066 current_class_stack
5067 = (class_stack_node_t) xmalloc (current_class_stack_size
5068 * sizeof (struct class_stack_node));
8d08fdba 5069
be99da77 5070 access_default_node = build_int_2 (0, 0);
c35cce41
MM
5071 access_public_node = build_int_2 (ak_public, 0);
5072 access_protected_node = build_int_2 (ak_protected, 0);
5073 access_private_node = build_int_2 (ak_private, 0);
be99da77 5074 access_default_virtual_node = build_int_2 (4, 0);
c35cce41
MM
5075 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5076 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5077 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
8d08fdba
MS
5078}
5079
5080/* Set current scope to NAME. CODE tells us if this is a
5081 STRUCT, UNION, or ENUM environment.
5082
5083 NAME may end up being NULL_TREE if this is an anonymous or
5084 late-bound struct (as in "struct { ... } foo;") */
5085
5086/* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5087 appropriate values, found by looking up the type definition of
5088 NAME (as a CODE).
5089
5090 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5091 which can be seen locally to the class. They are shadowed by
5092 any subsequent local declaration (including parameter names).
5093
5094 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5095 which have static meaning (i.e., static members, static
5096 member functions, enum declarations, etc).
5097
5098 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5099 which can be seen locally to the class (as in 1), but
5100 know that we are doing this for declaration purposes
5101 (i.e. friend foo::bar (int)).
5102
5103 So that we may avoid calls to lookup_name, we cache the _TYPE
5104 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5105
5106 For multiple inheritance, we perform a two-pass depth-first search
5107 of the type lattice. The first pass performs a pre-order search,
5108 marking types after the type has had its fields installed in
5109 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5110 unmarks the marked types. If a field or member function name
5111 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5112 that name becomes `error_mark_node'. */
5113
5114void
5115pushclass (type, modify)
5116 tree type;
5117 int modify;
5118{
7fb4a8f7 5119 type = TYPE_MAIN_VARIANT (type);
8d08fdba 5120
61a127b3
MM
5121 /* Make sure there is enough room for the new entry on the stack. */
5122 if (current_class_depth + 1 >= current_class_stack_size)
8d08fdba 5123 {
61a127b3
MM
5124 current_class_stack_size *= 2;
5125 current_class_stack
5126 = (class_stack_node_t) xrealloc (current_class_stack,
5127 current_class_stack_size
5128 * sizeof (struct class_stack_node));
8d08fdba
MS
5129 }
5130
61a127b3
MM
5131 /* Insert a new entry on the class stack. */
5132 current_class_stack[current_class_depth].name = current_class_name;
5133 current_class_stack[current_class_depth].type = current_class_type;
5134 current_class_stack[current_class_depth].access = current_access_specifier;
8f032717 5135 current_class_stack[current_class_depth].names_used = 0;
61a127b3
MM
5136 current_class_depth++;
5137
5138 /* Now set up the new type. */
8d08fdba
MS
5139 current_class_name = TYPE_NAME (type);
5140 if (TREE_CODE (current_class_name) == TYPE_DECL)
5141 current_class_name = DECL_NAME (current_class_name);
5142 current_class_type = type;
5143
61a127b3
MM
5144 /* By default, things in classes are private, while things in
5145 structures or unions are public. */
5146 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5147 ? access_private_node
5148 : access_public_node);
5149
8d08fdba 5150 if (previous_class_type != NULL_TREE
8f032717 5151 && (type != previous_class_type
d0f062fb 5152 || !COMPLETE_TYPE_P (previous_class_type))
8d08fdba
MS
5153 && current_class_depth == 1)
5154 {
5155 /* Forcibly remove any old class remnants. */
8f032717 5156 invalidate_class_lookup_cache ();
8d08fdba
MS
5157 }
5158
8f032717
MM
5159 /* If we're about to enter a nested class, clear
5160 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5161 if (modify && current_class_depth > 1)
5162 clear_identifier_class_values ();
5163
8d08fdba
MS
5164 pushlevel_class ();
5165
37c46b43 5166#if 0
5566b478
MS
5167 if (CLASSTYPE_TEMPLATE_INFO (type))
5168 overload_template_name (type);
37c46b43 5169#endif
5566b478 5170
8d08fdba
MS
5171 if (modify)
5172 {
5566b478 5173 if (type != previous_class_type || current_class_depth > 1)
8f032717 5174 push_class_decls (type);
8d08fdba
MS
5175 else
5176 {
5177 tree item;
5178
f181d4ae
MM
5179 /* We are re-entering the same class we just left, so we
5180 don't have to search the whole inheritance matrix to find
5181 all the decls to bind again. Instead, we install the
5182 cached class_shadowed list, and walk through it binding
5183 names and setting up IDENTIFIER_TYPE_VALUEs. */
8d08fdba
MS
5184 set_class_shadows (previous_class_values);
5185 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5186 {
5187 tree id = TREE_PURPOSE (item);
d8f8dca1 5188 tree decl = TREE_TYPE (item);
8d08fdba 5189
f181d4ae 5190 push_class_binding (id, decl);
8d08fdba
MS
5191 if (TREE_CODE (decl) == TYPE_DECL)
5192 set_identifier_type_value (id, TREE_TYPE (decl));
5193 }
5194 unuse_fields (type);
5195 }
5196
280f9385 5197 storetags (CLASSTYPE_TAGS (type));
8f032717
MM
5198 }
5199}
5200
5201/* When we exit a toplevel class scope, we save the
5202 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5203 reenter the class. Here, we've entered some other class, so we
5204 must invalidate our cache. */
8d08fdba 5205
8f032717
MM
5206void
5207invalidate_class_lookup_cache ()
5208{
8f032717
MM
5209 tree t;
5210
5211 /* This code can be seen as a cache miss. When we've cached a
5212 class' scope's bindings and we can't use them, we need to reset
5213 them. This is it! */
5214 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5215 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
8f032717
MM
5216
5217 previous_class_type = NULL_TREE;
8d08fdba
MS
5218}
5219
5220/* Get out of the current class scope. If we were in a class scope
b74a0560 5221 previously, that is the one popped to. */
e92cc029 5222
8d08fdba 5223void
b74a0560 5224popclass ()
8d08fdba 5225{
273a708f 5226 poplevel_class ();
8d08fdba 5227 /* Since poplevel_class does the popping of class decls nowadays,
b74a0560
MM
5228 this really only frees the obstack used for these decls. */
5229 pop_class_decls ();
8d08fdba
MS
5230
5231 current_class_depth--;
61a127b3
MM
5232 current_class_name = current_class_stack[current_class_depth].name;
5233 current_class_type = current_class_stack[current_class_depth].type;
5234 current_access_specifier = current_class_stack[current_class_depth].access;
8f032717
MM
5235 if (current_class_stack[current_class_depth].names_used)
5236 splay_tree_delete (current_class_stack[current_class_depth].names_used);
8d08fdba
MS
5237}
5238
70adf8a9
JM
5239/* Returns 1 if current_class_type is either T or a nested type of T.
5240 We start looking from 1 because entry 0 is from global scope, and has
5241 no type. */
b9082e8a
JM
5242
5243int
5244currently_open_class (t)
5245 tree t;
5246{
5247 int i;
5248 if (t == current_class_type)
5249 return 1;
70adf8a9 5250 for (i = 1; i < current_class_depth; ++i)
61a127b3 5251 if (current_class_stack [i].type == t)
b9082e8a
JM
5252 return 1;
5253 return 0;
5254}
5255
70adf8a9
JM
5256/* If either current_class_type or one of its enclosing classes are derived
5257 from T, return the appropriate type. Used to determine how we found
5258 something via unqualified lookup. */
5259
5260tree
5261currently_open_derived_class (t)
5262 tree t;
5263{
5264 int i;
5265
5266 if (DERIVED_FROM_P (t, current_class_type))
5267 return current_class_type;
5268
5269 for (i = current_class_depth - 1; i > 0; --i)
5270 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5271 return current_class_stack[i].type;
5272
5273 return NULL_TREE;
5274}
5275
8d08fdba
MS
5276/* When entering a class scope, all enclosing class scopes' names with
5277 static meaning (static variables, static functions, types and enumerators)
5278 have to be visible. This recursive function calls pushclass for all
5279 enclosing class contexts until global or a local scope is reached.
5280 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5281 formal of the same name. */
5282
5283void
5284push_nested_class (type, modify)
5285 tree type;
5286 int modify;
5287{
a28e3c7f
MS
5288 tree context;
5289
b262d64c 5290 /* A namespace might be passed in error cases, like A::B:C. */
07c88314
MM
5291 if (type == NULL_TREE
5292 || type == error_mark_node
b262d64c 5293 || TREE_CODE (type) == NAMESPACE_DECL
07c88314 5294 || ! IS_AGGR_TYPE (type)
73b0fce8
KL
5295 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5296 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
a28e3c7f
MS
5297 return;
5298
d2e5ee5c 5299 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
8d08fdba 5300
6b400b21 5301 if (context && CLASS_TYPE_P (context))
8d08fdba
MS
5302 push_nested_class (context, 2);
5303 pushclass (type, modify);
5304}
5305
5306/* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5307
5308void
b74a0560 5309pop_nested_class ()
8d08fdba 5310{
d2e5ee5c 5311 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8d08fdba 5312
b74a0560 5313 popclass ();
6b400b21 5314 if (context && CLASS_TYPE_P (context))
b74a0560 5315 pop_nested_class ();
8d08fdba
MS
5316}
5317
5318/* Set global variables CURRENT_LANG_NAME to appropriate value
5319 so that behavior of name-mangling machinery is correct. */
5320
5321void
5322push_lang_context (name)
5323 tree name;
5324{
5325 *current_lang_stack++ = current_lang_name;
9cd64686
MM
5326 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5327 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
8d08fdba 5328 {
9cd64686
MM
5329 size_t old_size = VARRAY_SIZE (current_lang_base);
5330
5331 VARRAY_GROW (current_lang_base, old_size + 10);
5332 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
8d08fdba
MS
5333 }
5334
e229f2cd 5335 if (name == lang_name_cplusplus)
8d08fdba
MS
5336 {
5337 strict_prototype = strict_prototypes_lang_cplusplus;
5338 current_lang_name = name;
5339 }
e229f2cd
PB
5340 else if (name == lang_name_java)
5341 {
5342 strict_prototype = strict_prototypes_lang_cplusplus;
5343 current_lang_name = name;
5344 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5345 (See record_builtin_java_type in decl.c.) However, that causes
5346 incorrect debug entries if these types are actually used.
5347 So we re-enable debug output after extern "Java". */
5348 DECL_IGNORED_P (java_byte_type_node) = 0;
5349 DECL_IGNORED_P (java_short_type_node) = 0;
5350 DECL_IGNORED_P (java_int_type_node) = 0;
5351 DECL_IGNORED_P (java_long_type_node) = 0;
5352 DECL_IGNORED_P (java_float_type_node) = 0;
5353 DECL_IGNORED_P (java_double_type_node) = 0;
5354 DECL_IGNORED_P (java_char_type_node) = 0;
5355 DECL_IGNORED_P (java_boolean_type_node) = 0;
5356 }
8d08fdba
MS
5357 else if (name == lang_name_c)
5358 {
5359 strict_prototype = strict_prototypes_lang_c;
5360 current_lang_name = name;
5361 }
5362 else
8251199e 5363 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
8d08fdba
MS
5364}
5365
5366/* Get out of the current language scope. */
e92cc029 5367
8d08fdba
MS
5368void
5369pop_lang_context ()
5370{
9cd64686
MM
5371 /* Clear the current entry so that garbage collector won't hold on
5372 to it. */
5373 *current_lang_stack = NULL_TREE;
8d08fdba 5374 current_lang_name = *--current_lang_stack;
eff71ab0
PB
5375 if (current_lang_name == lang_name_cplusplus
5376 || current_lang_name == lang_name_java)
8d08fdba
MS
5377 strict_prototype = strict_prototypes_lang_cplusplus;
5378 else if (current_lang_name == lang_name_c)
5379 strict_prototype = strict_prototypes_lang_c;
5380}
8d08fdba
MS
5381\f
5382/* Type instantiation routines. */
5383
104bf76a
MM
5384/* Given an OVERLOAD and a TARGET_TYPE, return the function that
5385 matches the TARGET_TYPE. If there is no satisfactory match, return
5386 error_mark_node, and issue an error message if COMPLAIN is
5387 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5388 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5389 template arguments. */
5390
2c73f9f5 5391static tree
104bf76a
MM
5392resolve_address_of_overloaded_function (target_type,
5393 overload,
5394 complain,
5395 template_only,
5396 explicit_targs)
5397 tree target_type;
5398 tree overload;
2c73f9f5 5399 int complain;
104bf76a
MM
5400 int template_only;
5401 tree explicit_targs;
2c73f9f5 5402{
104bf76a
MM
5403 /* Here's what the standard says:
5404
5405 [over.over]
5406
5407 If the name is a function template, template argument deduction
5408 is done, and if the argument deduction succeeds, the deduced
5409 arguments are used to generate a single template function, which
5410 is added to the set of overloaded functions considered.
5411
5412 Non-member functions and static member functions match targets of
5413 type "pointer-to-function" or "reference-to-function." Nonstatic
5414 member functions match targets of type "pointer-to-member
5415 function;" the function type of the pointer to member is used to
5416 select the member function from the set of overloaded member
5417 functions. If a nonstatic member function is selected, the
5418 reference to the overloaded function name is required to have the
5419 form of a pointer to member as described in 5.3.1.
5420
5421 If more than one function is selected, any template functions in
5422 the set are eliminated if the set also contains a non-template
5423 function, and any given template function is eliminated if the
5424 set contains a second template function that is more specialized
5425 than the first according to the partial ordering rules 14.5.5.2.
5426 After such eliminations, if any, there shall remain exactly one
5427 selected function. */
5428
5429 int is_ptrmem = 0;
5430 int is_reference = 0;
5431 /* We store the matches in a TREE_LIST rooted here. The functions
5432 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5433 interoperability with most_specialized_instantiation. */
5434 tree matches = NULL_TREE;
50714e79 5435 tree fn;
104bf76a 5436
d8f8dca1
MM
5437 /* By the time we get here, we should be seeing only real
5438 pointer-to-member types, not the internal POINTER_TYPE to
5439 METHOD_TYPE representation. */
5440 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5441 && (TREE_CODE (TREE_TYPE (target_type))
5442 == METHOD_TYPE)), 0);
104bf76a
MM
5443
5444 /* Check that the TARGET_TYPE is reasonable. */
5445 if (TYPE_PTRFN_P (target_type))
5446 /* This is OK. */
5447 ;
5448 else if (TYPE_PTRMEMFUNC_P (target_type))
5449 /* This is OK, too. */
5450 is_ptrmem = 1;
5451 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5452 {
5453 /* This is OK, too. This comes from a conversion to reference
5454 type. */
5455 target_type = build_reference_type (target_type);
5456 is_reference = 1;
5457 }
5458 else
5459 {
5460 if (complain)
5461 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5462 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5463 return error_mark_node;
5464 }
5465
5466 /* If we can find a non-template function that matches, we can just
5467 use it. There's no point in generating template instantiations
5468 if we're just going to throw them out anyhow. But, of course, we
5469 can only do this when we don't *need* a template function. */
5470 if (!template_only)
5471 {
5472 tree fns;
5473
5474 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5475 {
5476 tree fn = OVL_FUNCTION (fns);
5477 tree fntype;
2c73f9f5 5478
104bf76a
MM
5479 if (TREE_CODE (fn) == TEMPLATE_DECL)
5480 /* We're not looking for templates just yet. */
5481 continue;
5482
5483 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5484 != is_ptrmem)
5485 /* We're looking for a non-static member, and this isn't
5486 one, or vice versa. */
5487 continue;
5488
5489 /* See if there's a match. */
5490 fntype = TREE_TYPE (fn);
5491 if (is_ptrmem)
5492 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5493 else if (!is_reference)
5494 fntype = build_pointer_type (fntype);
5495
5496 if (can_convert_arg (target_type, fntype, fn))
e1b3e07d 5497 matches = tree_cons (fn, NULL_TREE, matches);
104bf76a
MM
5498 }
5499 }
5500
5501 /* Now, if we've already got a match (or matches), there's no need
5502 to proceed to the template functions. But, if we don't have a
5503 match we need to look at them, too. */
5504 if (!matches)
2c73f9f5 5505 {
104bf76a
MM
5506 tree target_fn_type;
5507 tree target_arg_types;
8d3631f8 5508 tree target_ret_type;
104bf76a
MM
5509 tree fns;
5510
5511 if (is_ptrmem)
4393e105
MM
5512 target_fn_type
5513 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
2c73f9f5 5514 else
4393e105
MM
5515 target_fn_type = TREE_TYPE (target_type);
5516 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8d3631f8 5517 target_ret_type = TREE_TYPE (target_fn_type);
4393e105 5518
104bf76a
MM
5519 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5520 {
5521 tree fn = OVL_FUNCTION (fns);
104bf76a
MM
5522 tree instantiation;
5523 tree instantiation_type;
5524 tree targs;
5525
5526 if (TREE_CODE (fn) != TEMPLATE_DECL)
5527 /* We're only looking for templates. */
5528 continue;
5529
5530 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5531 != is_ptrmem)
4393e105 5532 /* We're not looking for a non-static member, and this is
104bf76a
MM
5533 one, or vice versa. */
5534 continue;
5535
104bf76a 5536 /* Try to do argument deduction. */
f31c0a32 5537 targs = make_tree_vec (DECL_NTPARMS (fn));
4393e105 5538 if (fn_type_unification (fn, explicit_targs, targs,
8d3631f8 5539 target_arg_types, target_ret_type,
03017874 5540 DEDUCE_EXACT) != 0)
104bf76a
MM
5541 /* Argument deduction failed. */
5542 continue;
5543
5544 /* Instantiate the template. */
5545 instantiation = instantiate_template (fn, targs);
5546 if (instantiation == error_mark_node)
5547 /* Instantiation failed. */
5548 continue;
5549
5550 /* See if there's a match. */
5551 instantiation_type = TREE_TYPE (instantiation);
5552 if (is_ptrmem)
5553 instantiation_type =
5554 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5555 else if (!is_reference)
5556 instantiation_type = build_pointer_type (instantiation_type);
5557 if (can_convert_arg (target_type, instantiation_type, instantiation))
e1b3e07d 5558 matches = tree_cons (instantiation, fn, matches);
104bf76a
MM
5559 }
5560
5561 /* Now, remove all but the most specialized of the matches. */
5562 if (matches)
5563 {
5564 tree match = most_specialized_instantiation (matches,
5565 explicit_targs);
5566
5567 if (match != error_mark_node)
e1b3e07d 5568 matches = tree_cons (match, NULL_TREE, NULL_TREE);
104bf76a
MM
5569 }
5570 }
5571
5572 /* Now we should have exactly one function in MATCHES. */
5573 if (matches == NULL_TREE)
5574 {
5575 /* There were *no* matches. */
5576 if (complain)
5577 {
6b9b6b15 5578 cp_error ("no matches converting function `%D' to type `%#T'",
104bf76a
MM
5579 DECL_NAME (OVL_FUNCTION (overload)),
5580 target_type);
6b9b6b15
JM
5581
5582 /* print_candidates expects a chain with the functions in
5583 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5584 so why be clever?). */
5585 for (; overload; overload = OVL_NEXT (overload))
e1b3e07d
MM
5586 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5587 matches);
6b9b6b15
JM
5588
5589 print_candidates (matches);
104bf76a
MM
5590 }
5591 return error_mark_node;
2c73f9f5 5592 }
104bf76a
MM
5593 else if (TREE_CHAIN (matches))
5594 {
5595 /* There were too many matches. */
5596
5597 if (complain)
5598 {
5599 tree match;
5600
5601 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5602 DECL_NAME (OVL_FUNCTION (overload)),
5603 target_type);
5604
5605 /* Since print_candidates expects the functions in the
5606 TREE_VALUE slot, we flip them here. */
5607 for (match = matches; match; match = TREE_CHAIN (match))
5608 TREE_VALUE (match) = TREE_PURPOSE (match);
5609
5610 print_candidates (matches);
5611 }
5612
5613 return error_mark_node;
5614 }
5615
50714e79
MM
5616 /* Good, exactly one match. Now, convert it to the correct type. */
5617 fn = TREE_PURPOSE (matches);
5618
a6ecf8b6
JM
5619 mark_used (fn);
5620
50714e79
MM
5621 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5622 return build_unary_op (ADDR_EXPR, fn, 0);
5623 else
5624 {
5625 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5626 will mark the function as addressed, but here we must do it
5627 explicitly. */
5628 mark_addressable (fn);
5629
5630 return fn;
5631 }
2c73f9f5
ML
5632}
5633
ec255269
MS
5634/* This function will instantiate the type of the expression given in
5635 RHS to match the type of LHSTYPE. If errors exist, then return
2036a15c 5636 error_mark_node. We only complain is COMPLAIN is set. If we are
ec255269
MS
5637 not complaining, never modify rhs, as overload resolution wants to
5638 try many possible instantiations, in hopes that at least one will
5639 work.
8d08fdba 5640
940ff223
JM
5641 FLAGS is a bitmask, as we see at the top of the function.
5642
e6e174e5
JM
5643 For non-recursive calls, LHSTYPE should be a function, pointer to
5644 function, or a pointer to member function. */
e92cc029 5645
8d08fdba 5646tree
940ff223 5647instantiate_type (lhstype, rhs, flags)
8d08fdba 5648 tree lhstype, rhs;
940ff223 5649 int flags;
8d08fdba 5650{
940ff223
JM
5651 int complain = (flags & 1);
5652 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
5653
8d08fdba
MS
5654 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5655 {
5656 if (complain)
8251199e 5657 error ("not enough type information");
8d08fdba
MS
5658 return error_mark_node;
5659 }
5660
5661 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
abff8e06 5662 {
940ff223 5663 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
abff8e06
JM
5664 return rhs;
5665 if (complain)
8251199e 5666 cp_error ("argument of type `%T' does not match `%T'",
abff8e06
JM
5667 TREE_TYPE (rhs), lhstype);
5668 return error_mark_node;
5669 }
8d08fdba 5670
2c73f9f5
ML
5671 /* We don't overwrite rhs if it is an overloaded function.
5672 Copying it would destroy the tree link. */
5673 if (TREE_CODE (rhs) != OVERLOAD)
5674 rhs = copy_node (rhs);
c73964b2 5675
8d08fdba
MS
5676 /* This should really only be used when attempting to distinguish
5677 what sort of a pointer to function we have. For now, any
5678 arithmetic operation which is not supported on pointers
5679 is rejected as an error. */
5680
5681 switch (TREE_CODE (rhs))
5682 {
5683 case TYPE_EXPR:
5684 case CONVERT_EXPR:
5685 case SAVE_EXPR:
5686 case CONSTRUCTOR:
5687 case BUFFER_REF:
5688 my_friendly_abort (177);
5689 return error_mark_node;
5690
5691 case INDIRECT_REF:
5692 case ARRAY_REF:
ec255269
MS
5693 {
5694 tree new_rhs;
8d08fdba 5695
ec255269 5696 new_rhs = instantiate_type (build_pointer_type (lhstype),
940ff223 5697 TREE_OPERAND (rhs, 0), flags);
ec255269
MS
5698 if (new_rhs == error_mark_node)
5699 return error_mark_node;
5700
5701 TREE_TYPE (rhs) = lhstype;
5702 TREE_OPERAND (rhs, 0) = new_rhs;
5703 return rhs;
5704 }
8d08fdba
MS
5705
5706 case NOP_EXPR:
5707 rhs = copy_node (TREE_OPERAND (rhs, 0));
5708 TREE_TYPE (rhs) = unknown_type_node;
940ff223 5709 return instantiate_type (lhstype, rhs, flags);
8d08fdba
MS
5710
5711 case COMPONENT_REF:
5712 {
d2c192ad 5713 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
50714e79 5714
d2c192ad
JM
5715 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
5716 && complain && !flag_ms_extensions)
50714e79 5717 {
d2c192ad
JM
5718 /* Note: we check this after the recursive call to avoid
5719 complaining about cases where overload resolution fails. */
5720
5721 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5722 tree fn = PTRMEM_CST_MEMBER (r);
5723
5724 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
5725
5726 cp_pedwarn
5727 ("object-dependent reference to `%E' can only be used in a call",
5728 DECL_NAME (fn));
5729 cp_pedwarn
5730 (" to form a pointer to member function, say `&%T::%E'",
5731 t, DECL_NAME (fn));
8d08fdba 5732 }
d2c192ad 5733
50714e79 5734 return r;
8d08fdba
MS
5735 }
5736
2a238a97 5737 case OFFSET_REF:
05e0b2f4
JM
5738 rhs = TREE_OPERAND (rhs, 1);
5739 if (BASELINK_P (rhs))
5740 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5741
2a238a97
MM
5742 /* This can happen if we are forming a pointer-to-member for a
5743 member template. */
2a238a97 5744 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
05e0b2f4 5745
2a238a97 5746 /* Fall through. */
874503bc 5747
386b8a85 5748 case TEMPLATE_ID_EXPR:
104bf76a
MM
5749 return
5750 resolve_address_of_overloaded_function (lhstype,
5751 TREE_OPERAND (rhs, 0),
5752 complain,
5753 /*template_only=*/1,
5754 TREE_OPERAND (rhs, 1));
386b8a85 5755
2c73f9f5 5756 case OVERLOAD:
104bf76a
MM
5757 return
5758 resolve_address_of_overloaded_function (lhstype,
5759 rhs,
5760 complain,
5761 /*template_only=*/0,
5762 /*explicit_targs=*/NULL_TREE);
2c73f9f5
ML
5763
5764 case TREE_LIST:
940ff223
JM
5765 /* Now we should have a baselink. */
5766 my_friendly_assert (BASELINK_P (rhs), 990412);
e5966228 5767
940ff223 5768 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
8d08fdba
MS
5769
5770 case CALL_EXPR:
5771 /* This is too hard for now. */
5772 my_friendly_abort (183);
5773 return error_mark_node;
5774
5775 case PLUS_EXPR:
5776 case MINUS_EXPR:
5777 case COMPOUND_EXPR:
a0a33927 5778 TREE_OPERAND (rhs, 0)
940ff223 5779 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
8d08fdba
MS
5780 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5781 return error_mark_node;
a0a33927 5782 TREE_OPERAND (rhs, 1)
940ff223 5783 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
8d08fdba
MS
5784 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5785 return error_mark_node;
5786
5787 TREE_TYPE (rhs) = lhstype;
5788 return rhs;
5789
5790 case MULT_EXPR:
5791 case TRUNC_DIV_EXPR:
5792 case FLOOR_DIV_EXPR:
5793 case CEIL_DIV_EXPR:
5794 case ROUND_DIV_EXPR:
5795 case RDIV_EXPR:
5796 case TRUNC_MOD_EXPR:
5797 case FLOOR_MOD_EXPR:
5798 case CEIL_MOD_EXPR:
5799 case ROUND_MOD_EXPR:
5800 case FIX_ROUND_EXPR:
5801 case FIX_FLOOR_EXPR:
5802 case FIX_CEIL_EXPR:
5803 case FIX_TRUNC_EXPR:
5804 case FLOAT_EXPR:
5805 case NEGATE_EXPR:
5806 case ABS_EXPR:
5807 case MAX_EXPR:
5808 case MIN_EXPR:
5809 case FFS_EXPR:
5810
5811 case BIT_AND_EXPR:
5812 case BIT_IOR_EXPR:
5813 case BIT_XOR_EXPR:
5814 case LSHIFT_EXPR:
5815 case RSHIFT_EXPR:
5816 case LROTATE_EXPR:
5817 case RROTATE_EXPR:
5818
5819 case PREINCREMENT_EXPR:
5820 case PREDECREMENT_EXPR:
5821 case POSTINCREMENT_EXPR:
5822 case POSTDECREMENT_EXPR:
5823 if (complain)
8251199e 5824 error ("invalid operation on uninstantiated type");
8d08fdba
MS
5825 return error_mark_node;
5826
5827 case TRUTH_AND_EXPR:
5828 case TRUTH_OR_EXPR:
5829 case TRUTH_XOR_EXPR:
5830 case LT_EXPR:
5831 case LE_EXPR:
5832 case GT_EXPR:
5833 case GE_EXPR:
5834 case EQ_EXPR:
5835 case NE_EXPR:
5836 case TRUTH_ANDIF_EXPR:
5837 case TRUTH_ORIF_EXPR:
5838 case TRUTH_NOT_EXPR:
5839 if (complain)
8251199e 5840 error ("not enough type information");
8d08fdba
MS
5841 return error_mark_node;
5842
ca36f057
MM
5843 case COND_EXPR:
5844 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5845 {
5846 if (complain)
5847 error ("not enough type information");
5848 return error_mark_node;
5849 }
5850 TREE_OPERAND (rhs, 1)
5851 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5852 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5853 return error_mark_node;
5854 TREE_OPERAND (rhs, 2)
5855 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
5856 if (TREE_OPERAND (rhs, 2) == error_mark_node)
5857 return error_mark_node;
5858
5859 TREE_TYPE (rhs) = lhstype;
5860 return rhs;
5861
5862 case MODIFY_EXPR:
5863 TREE_OPERAND (rhs, 1)
5864 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5865 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5866 return error_mark_node;
5867
5868 TREE_TYPE (rhs) = lhstype;
5869 return rhs;
5870
5871 case ADDR_EXPR:
5872 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5873
5874 case ENTRY_VALUE_EXPR:
5875 my_friendly_abort (184);
5876 return error_mark_node;
5877
5878 case ERROR_MARK:
5879 return error_mark_node;
5880
5881 default:
5882 my_friendly_abort (185);
5883 return error_mark_node;
5884 }
5885}
5886\f
5887/* Return the name of the virtual function pointer field
5888 (as an IDENTIFIER_NODE) for the given TYPE. Note that
5889 this may have to look back through base types to find the
5890 ultimate field name. (For single inheritance, these could
5891 all be the same name. Who knows for multiple inheritance). */
5892
5893static tree
5894get_vfield_name (type)
5895 tree type;
5896{
5897 tree binfo = TYPE_BINFO (type);
5898 char *buf;
5899
5900 while (BINFO_BASETYPES (binfo)
5901 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5902 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5903 binfo = BINFO_BASETYPE (binfo, 0);
5904
5905 type = BINFO_TYPE (binfo);
5906 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5907 + TYPE_NAME_LENGTH (type) + 2);
5908 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5909 return get_identifier (buf);
5910}
5911
5912void
5913print_class_statistics ()
5914{
5915#ifdef GATHER_STATISTICS
5916 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5917 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5918 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5919 n_build_method_call, n_inner_fields_searched);
5920 if (n_vtables)
5921 {
5922 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5923 n_vtables, n_vtable_searches);
5924 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5925 n_vtable_entries, n_vtable_elems);
5926 }
5927#endif
5928}
5929
5930/* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5931 according to [class]:
5932 The class-name is also inserted
5933 into the scope of the class itself. For purposes of access checking,
5934 the inserted class name is treated as if it were a public member name. */
5935
5936void
5937build_self_reference ()
5938{
5939 tree name = constructor_name (current_class_type);
5940 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5941 tree saved_cas;
5942
5943 DECL_NONLOCAL (value) = 1;
5944 DECL_CONTEXT (value) = current_class_type;
5945 DECL_ARTIFICIAL (value) = 1;
5946
5947 if (processing_template_decl)
5948 value = push_template_decl (value);
5949
5950 saved_cas = current_access_specifier;
5951 current_access_specifier = access_public_node;
5952 finish_member_declaration (value);
5953 current_access_specifier = saved_cas;
5954}
5955
5956/* Returns 1 if TYPE contains only padding bytes. */
5957
5958int
5959is_empty_class (type)
5960 tree type;
5961{
5962 tree t;
5963
5964 if (type == error_mark_node)
5965 return 0;
5966
5967 if (! IS_AGGR_TYPE (type))
5968 return 0;
5969
5970 if (flag_new_abi)
5971 return integer_zerop (CLASSTYPE_SIZE (type));
5972
5973 if (TYPE_BINFO_BASETYPES (type))
5974 return 0;
5975 t = TYPE_FIELDS (type);
5976 while (t && TREE_CODE (t) != FIELD_DECL)
5977 t = TREE_CHAIN (t);
5978 return (t == NULL_TREE);
5979}
5980
5981/* Find the enclosing class of the given NODE. NODE can be a *_DECL or
5982 a *_TYPE node. NODE can also be a local class. */
5983
5984tree
5985get_enclosing_class (type)
5986 tree type;
5987{
5988 tree node = type;
5989
5990 while (node && TREE_CODE (node) != NAMESPACE_DECL)
5991 {
5992 switch (TREE_CODE_CLASS (TREE_CODE (node)))
5993 {
5994 case 'd':
5995 node = DECL_CONTEXT (node);
5996 break;
5997
5998 case 't':
5999 if (node != type)
6000 return node;
6001 node = TYPE_CONTEXT (node);
6002 break;
6003
6004 default:
6005 my_friendly_abort (0);
6006 }
6007 }
6008 return NULL_TREE;
6009}
6010
6011/* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6012
6013int
6014is_base_of_enclosing_class (base, type)
6015 tree base, type;
6016{
6017 while (type)
6018 {
6019 if (get_binfo (base, type, 0))
6020 return 1;
6021
6022 type = get_enclosing_class (type);
6023 }
6024 return 0;
6025}
6026
6027/* Note that NAME was looked up while the current class was being
6028 defined and that the result of that lookup was DECL. */
6029
6030void
6031maybe_note_name_used_in_class (name, decl)
6032 tree name;
6033 tree decl;
6034{
6035 splay_tree names_used;
6036
6037 /* If we're not defining a class, there's nothing to do. */
6038 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6039 return;
6040
6041 /* If there's already a binding for this NAME, then we don't have
6042 anything to worry about. */
6043 if (IDENTIFIER_CLASS_VALUE (name))
6044 return;
6045
6046 if (!current_class_stack[current_class_depth - 1].names_used)
6047 current_class_stack[current_class_depth - 1].names_used
6048 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6049 names_used = current_class_stack[current_class_depth - 1].names_used;
6050
6051 splay_tree_insert (names_used,
6052 (splay_tree_key) name,
6053 (splay_tree_value) decl);
6054}
6055
6056/* Note that NAME was declared (as DECL) in the current class. Check
6057 to see that the declaration is legal. */
6058
6059void
6060note_name_declared_in_class (name, decl)
6061 tree name;
6062 tree decl;
6063{
6064 splay_tree names_used;
6065 splay_tree_node n;
6066
6067 /* Look to see if we ever used this name. */
6068 names_used
6069 = current_class_stack[current_class_depth - 1].names_used;
6070 if (!names_used)
6071 return;
6072
6073 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6074 if (n)
6075 {
6076 /* [basic.scope.class]
6077
6078 A name N used in a class S shall refer to the same declaration
6079 in its context and when re-evaluated in the completed scope of
6080 S. */
6081 cp_error ("declaration of `%#D'", decl);
6082 cp_error_at ("changes meaning of `%s' from `%+#D'",
6083 IDENTIFIER_POINTER (DECL_NAME (OVL_CURRENT (decl))),
6084 (tree) n->value);
6085 }
6086}
6087
c35cce41
MM
6088/* Returns the VAR_DECL for the complete vtable associated with
6089 BINFO. (Under the new ABI, secondary vtables are merged with
6090 primary vtables; this function will return the VAR_DECL for the
6091 primary vtable.) */
ca36f057 6092
c35cce41
MM
6093tree
6094get_vtbl_decl_for_binfo (binfo)
6095 tree binfo;
6096{
6097 tree decl;
6098
6099 decl = BINFO_VTABLE (binfo);
6100 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6101 {
6102 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6103 2000403);
6104 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6105 }
6106 if (decl)
6107 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6108 return decl;
6109}
6110
6111/* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6112 dominated by T) to stderr. INDENT should be zero when called from
6113 the top level; it is incremented recursively. */
6114
6115static void
6116dump_class_hierarchy_r (t, binfo, indent)
6117 tree t;
ca36f057
MM
6118 tree binfo;
6119 int indent;
6120{
6121 int i;
6122
6123 fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
6124 (unsigned long) binfo,
6125 type_as_string (binfo, TS_PLAIN));
6126 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
6127 tree_low_cst (BINFO_OFFSET (binfo), 0));
c35cce41
MM
6128 if (TREE_VIA_VIRTUAL (binfo))
6129 fprintf (stderr, " virtual");
6130 if (BINFO_PRIMARY_MARKED_P (binfo)
6131 || (TREE_VIA_VIRTUAL (binfo)
6132 && BINFO_VBASE_PRIMARY_P (BINFO_FOR_VBASE (BINFO_TYPE (binfo),
6133 t))))
6134 fprintf (stderr, " primary");
6135 fprintf (stderr, "\n");
ca36f057
MM
6136
6137 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
c35cce41
MM
6138 dump_class_hierarchy_r (t, BINFO_BASETYPE (binfo, i), indent + 2);
6139}
6140
6141/* Dump the BINFO hierarchy for T. */
6142
6143void
6144dump_class_hierarchy (t)
6145 tree t;
6146{
6147 tree vbase;
6148
6149 dump_class_hierarchy_r (t, TYPE_BINFO (t), 0);
6150 fprintf (stderr, "virtual bases\n");
6151 for (vbase = CLASSTYPE_VBASECLASSES (t); vbase; vbase = TREE_CHAIN (vbase))
6152 dump_class_hierarchy_r (t, vbase, 0);
ca36f057
MM
6153}
6154
6155/* Virtual function table initialization. */
6156
6157/* Create all the necessary vtables for T and its base classes. */
6158
6159static void
6160finish_vtbls (t)
6161 tree t;
6162{
6163 if (merge_primary_and_secondary_vtables_p ())
6164 {
6165 tree list;
6166 tree vbase;
6167
6168 /* Under the new ABI, we lay out the primary and secondary
6169 vtables in one contiguous vtable. The primary vtable is
6170 first, followed by the non-virtual secondary vtables in
6171 inheritance graph order. */
c35cce41
MM
6172 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6173 TREE_TYPE (list) = t;
ca36f057
MM
6174 accumulate_vtbl_inits (TYPE_BINFO (t), list);
6175 /* Then come the virtual bases, also in inheritance graph
6176 order. */
c35cce41
MM
6177 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6178 {
6179 if (!TREE_VIA_VIRTUAL (vbase))
6180 continue;
6181 accumulate_vtbl_inits (BINFO_FOR_VBASE (BINFO_TYPE (vbase), t),
6182 list);
6183 }
ca36f057
MM
6184
6185 if (TYPE_BINFO_VTABLE (t))
6186 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6187 }
6188 else
6189 {
6190 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
6191 dfs_unmarked_real_bases_queue_p, t);
6192 dfs_walk (TYPE_BINFO (t), dfs_unmark,
6193 dfs_marked_real_bases_queue_p, t);
6194 }
6195}
6196
6197/* Called from finish_vtbls via dfs_walk. */
6198
6199static tree
6200dfs_finish_vtbls (binfo, data)
6201 tree binfo;
6202 void *data;
6203{
6204 tree t = (tree) data;
6205
6206 if (!BINFO_PRIMARY_MARKED_P (binfo)
6207 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
6208 && BINFO_NEW_VTABLE_MARKED (binfo, t))
6209 initialize_vtable (binfo,
c35cce41 6210 build_vtbl_initializer (binfo, t, NULL));
ca36f057
MM
6211
6212 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo, t);
6213 SET_BINFO_MARKED (binfo);
6214
6215 return NULL_TREE;
6216}
6217
6218/* Initialize the vtable for BINFO with the INITS. */
6219
6220static void
6221initialize_vtable (binfo, inits)
6222 tree binfo;
6223 tree inits;
6224{
6225 tree context;
6226 tree decl;
6227
6228 layout_vtable_decl (binfo, list_length (inits));
c35cce41 6229 decl = get_vtbl_decl_for_binfo (binfo);
ca36f057
MM
6230 context = DECL_CONTEXT (decl);
6231 DECL_CONTEXT (decl) = 0;
6232 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6233 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6234 DECL_CONTEXT (decl) = context;
6235}
6236
6237/* Add the vtbl initializers for BINFO (and its non-primary,
6238 non-virtual bases) to the list of INITS. */
6239
6240static void
6241accumulate_vtbl_inits (binfo, inits)
6242 tree binfo;
6243 tree inits;
6244{
c35cce41
MM
6245 /* Walk the BINFO and its bases. We walk in preorder so that as we
6246 initialize each vtable we can figure out at what offset the
6247 secondary vtable lies from the primary vtable. */
ca36f057
MM
6248 dfs_walk_real (binfo,
6249 dfs_accumulate_vtbl_inits,
c35cce41 6250 NULL,
ca36f057
MM
6251 dfs_skip_vbases,
6252 inits);
6253}
6254
6255/* Called from finish_vtbls via dfs_walk when using the new ABI.
6256 Accumulates the vtable initializers for all of the vtables into
6257 TREE_VALUE (DATA). */
6258
6259static tree
6260dfs_accumulate_vtbl_inits (binfo, data)
6261 tree binfo;
6262 void *data;
6263{
6264 tree l;
6265 tree t;
6266
6267 l = (tree) data;
c35cce41 6268 t = TREE_TYPE (l);
ca36f057
MM
6269
6270 if (!BINFO_PRIMARY_MARKED_P (binfo)
6271 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
6272 && BINFO_NEW_VTABLE_MARKED (binfo, t))
6273 {
c35cce41
MM
6274 tree inits;
6275 tree vtbl;
6276 tree index;
6277 int non_fn_entries;
6278
6279 /* Compute the initializer for this vtable. */
6280 inits = build_vtbl_initializer (binfo, t, &non_fn_entries);
6281
6282 /* Set BINFO_VTABLE to the address where the VPTR should point. */
6283 vtbl = TREE_PURPOSE (l);
6284 vtbl = build1 (ADDR_EXPR,
6285 build_pointer_type (TREE_TYPE (vtbl)),
6286 vtbl);
6287 index = size_binop (PLUS_EXPR,
6288 size_int (non_fn_entries),
6289 size_int (list_length (TREE_VALUE (l))));
6290 BINFO_VTABLE (binfo)
6291 = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
6292 size_binop (MULT_EXPR,
6293 TYPE_SIZE_UNIT (TREE_TYPE (vtbl)),
6294 index));
ca36f057
MM
6295
6296 /* Add the initializers for this vtable to the initializers for
6297 the other vtables we've already got. */
c35cce41 6298 TREE_VALUE (l) = chainon (TREE_VALUE (l), inits);
ca36f057
MM
6299 }
6300
6301 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo, t);
6302
6303 return NULL_TREE;
6304}
6305
6306/* Construct the initializer for BINFOs virtual function table. BINFO
6307 is part of the hierarchy dominated by T. The value returned is a
6308 TREE_LIST suitable for wrapping in a CONSTRUCTOR to use as the
c35cce41
MM
6309 DECL_INITIAL for a vtable. If NON_FN_ENTRIES_P is not NULL,
6310 *NON_FN_ENTRIES_P is set to the number of non-function entries in
6311 the vtable. */
ca36f057
MM
6312
6313static tree
c35cce41 6314build_vtbl_initializer (binfo, t, non_fn_entries_p)
ca36f057
MM
6315 tree binfo;
6316 tree t;
c35cce41 6317 int *non_fn_entries_p;
ca36f057
MM
6318{
6319 tree v = BINFO_VIRTUALS (binfo);
6320 tree inits = NULL_TREE;
c35cce41
MM
6321 tree vfun_inits;
6322 tree vbase;
6323 vcall_offset_data vod;
ca36f057 6324
c35cce41
MM
6325 /* Initialize those parts of VOD that matter. */
6326 vod.derived = t;
6327 vod.inits = NULL_TREE;
6328 vod.primary_p = (binfo == TYPE_BINFO (t));
6329 /* The first vbase or vcall offset is at index -3 in the vtable. */
6330 vod.index = build_int_2 (-3, -1);
6331
6332 /* Add the vcall and vbase offset entries. */
6333 build_vcall_and_vbase_vtbl_entries (binfo, &vod);
6334 inits = vod.inits;
6335 /* Clear BINFO_VTABLE_PAATH_MARKED; it's set by
6336 build_vbase_offset_vtbl_entries. */
6337 for (vbase = CLASSTYPE_VBASECLASSES (t);
6338 vbase;
6339 vbase = TREE_CHAIN (vbase))
6340 CLEAR_BINFO_VTABLE_PATH_MARKED (vbase);
ca36f057
MM
6341
6342 /* Add entries to the vtable for RTTI. */
c35cce41
MM
6343 inits = chainon (inits, build_rtti_vtbl_entries (binfo, t));
6344
6345 if (non_fn_entries_p)
6346 *non_fn_entries_p = list_length (inits);
ca36f057
MM
6347
6348 /* Go through all the ordinary virtual functions, building up
6349 initializers. */
c35cce41 6350 vfun_inits = NULL_TREE;
ca36f057
MM
6351 while (v)
6352 {
6353 tree delta;
6354 tree vcall_index;
6355 tree fn;
6356 tree pfn;
6357 tree init;
6358
6359 /* Pull the offset for `this', and the function to call, out of
6360 the list. */
6361 delta = BV_DELTA (v);
6362 vcall_index = BV_VCALL_INDEX (v);
6363 fn = BV_FN (v);
6364 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
6365 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
6366
6367 /* You can't call an abstract virtual function; it's abstract.
6368 So, we replace these functions with __pure_virtual. */
6369 if (DECL_PURE_VIRTUAL_P (fn))
6370 fn = abort_fndecl;
6371
6372 /* Take the address of the function, considering it to be of an
6373 appropriate generic type. */
6374 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
6375 /* The address of a function can't change. */
6376 TREE_CONSTANT (pfn) = 1;
6377 /* Enter it in the vtable. */
6378 init = build_vtable_entry (delta, vcall_index, pfn);
6379 /* And add it to the chain of initializers. */
c35cce41 6380 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
ca36f057
MM
6381
6382 /* Keep going. */
6383 v = TREE_CHAIN (v);
6384 }
6385
c35cce41
MM
6386 /* The initializers for virtual functions were built up in reverse
6387 order; straighten them out now. */
6388 vfun_inits = nreverse (vfun_inits);
6389
6390 /* The complete initializer is the INITS, followed by the
6391 VFUN_INITS. */
6392 return chainon (inits, vfun_inits);
ca36f057
MM
6393}
6394
c35cce41
MM
6395/* Sets vod->inits to be the initializers for the vbase and vcall
6396 offsets in BINFO, which is in the hierarchy dominated by T. */
ca36f057 6397
c35cce41
MM
6398static void
6399build_vcall_and_vbase_vtbl_entries (binfo, vod)
ca36f057 6400 tree binfo;
c35cce41 6401 vcall_offset_data *vod;
ca36f057 6402{
c35cce41
MM
6403 tree b;
6404 tree inits;
8d08fdba 6405
c35cce41
MM
6406 /* If this is a derived class, we must first create entries
6407 corresponding to the base class. These entries must go closer to
6408 the vptr, so we save them up and add them to the end of the list
6409 later. */
6410 inits = vod->inits;
6411 vod->inits = NULL_TREE;
6412 b = BINFO_PRIMARY_BINFO (binfo);
6413 if (b)
6414 build_vcall_and_vbase_vtbl_entries (b, vod);
6415
6416 /* Add the vbase entries for this base. */
6417 build_vbase_offset_vtbl_entries (binfo, vod);
6418 /* Add the vcall entries for this base. */
6419 build_vcall_offset_vtbl_entries (binfo, vod);
6420
6421 vod->inits = chainon (vod->inits, inits);
ca36f057 6422}
8d08fdba 6423
ca36f057
MM
6424/* Returns the initializers for the vbase offset entries in the vtable
6425 for BINFO (which is part of the class hierarchy dominated by T), in
c35cce41
MM
6426 reverse order. VBASE_OFFSET_INDEX gives the vtable index
6427 where the next vbase offset will go. */
8d08fdba 6428
c35cce41
MM
6429static void
6430build_vbase_offset_vtbl_entries (binfo, vod)
ca36f057 6431 tree binfo;
c35cce41 6432 vcall_offset_data *vod;
ca36f057 6433{
c35cce41
MM
6434 tree vbase;
6435 tree t;
8d08fdba 6436
ca36f057
MM
6437 /* Under the old ABI, pointers to virtual bases are stored in each
6438 object. */
6439 if (!vbase_offsets_in_vtable_p ())
c35cce41 6440 return;
8d08fdba 6441
ca36f057
MM
6442 /* If there are no virtual baseclasses, then there is nothing to
6443 do. */
6444 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
c35cce41 6445 return;
ca36f057 6446
c35cce41 6447 t = vod->derived;
ca36f057 6448
c35cce41
MM
6449 /* Go through the virtual bases, adding the offsets. */
6450 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
6451 vbase;
6452 vbase = TREE_CHAIN (vbase))
6453 {
6454 tree b;
6455 tree delta;
6456
6457 if (!TREE_VIA_VIRTUAL (vbase))
6458 continue;
ca36f057 6459
c35cce41
MM
6460 /* Find the instance of this virtual base in the complete
6461 object. */
6462 b = BINFO_FOR_VBASE (BINFO_TYPE (vbase), t);
6463
6464 /* If we've already got an offset for this virtual base, we
6465 don't need another one. */
6466 if (BINFO_VTABLE_PATH_MARKED (b))
6467 continue;
6468 SET_BINFO_VTABLE_PATH_MARKED (b);
6469
6470 /* Figure out where we can find this vbase offset. */
6471 delta = size_binop (MULT_EXPR,
6472 convert (ssizetype, vod->index),
6473 convert (ssizetype,
6474 TYPE_SIZE_UNIT (vtable_entry_type)));
6475 if (vod->primary_p)
6476 BINFO_VPTR_FIELD (b) = delta;
6477
6478 if (binfo != TYPE_BINFO (t))
6479 {
6480 tree orig_vbase;
6481
6482 /* Find the instance of this virtual base in the type of BINFO. */
6483 orig_vbase = BINFO_FOR_VBASE (BINFO_TYPE (vbase),
6484 BINFO_TYPE (binfo));
6485
6486 /* The vbase offset had better be the same. */
6487 if (!tree_int_cst_equal (delta,
6488 BINFO_VPTR_FIELD (orig_vbase)))
6489 my_friendly_abort (20000403);
6490 }
6491
6492 /* The next vbase will come at a more negative offset. */
6493 vod->index = fold (build (MINUS_EXPR, integer_type_node,
6494 vod->index, integer_one_node));
6495
6496 /* The initializer is the delta from BINFO to this virtual base.
6497 The vbase offsets go in reverse inheritance-graph order, and
6498 we are walking in inheritance graph order so these end up in
6499 the right order. */
6500 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (binfo));
6501 vod->inits = tree_cons (NULL_TREE,
6502 fold (build1 (NOP_EXPR,
6503 vtable_entry_type,
6504 delta)),
6505 vod->inits);
6506 }
8d08fdba 6507}
ca36f057
MM
6508
6509/* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
e92cc029 6510
8d08fdba 6511static tree
ca36f057
MM
6512dfs_vcall_offset_queue_p (binfo, data)
6513 tree binfo;
6514 void *data;
8d08fdba 6515{
ca36f057 6516 vcall_offset_data* vod = (vcall_offset_data *) data;
8d08fdba 6517
ca36f057 6518 return (binfo == vod->vbase) ? binfo : dfs_skip_vbases (binfo, NULL);
8d08fdba
MS
6519}
6520
ca36f057
MM
6521/* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
6522
6523static tree
6524dfs_build_vcall_offset_vtbl_entries (binfo, data)
6525 tree binfo;
6526 void *data;
8d08fdba 6527{
ca36f057
MM
6528 vcall_offset_data* vod;
6529 tree virtuals;
6530 tree binfo_inits;
c35cce41
MM
6531 tree b;
6532 int i;
ca36f057
MM
6533
6534 vod = (vcall_offset_data *) data;
6535 binfo_inits = NULL_TREE;
c35cce41
MM
6536
6537 /* Skip virtuals that we have already handled in a primary base
6538 class. */
6539 virtuals = BINFO_VIRTUALS (binfo);
6540 b = BINFO_PRIMARY_BINFO (binfo);
6541 if (b)
6542 for (i = 0; i < CLASSTYPE_VSIZE (BINFO_TYPE (b)); ++i)
6543 virtuals = TREE_CHAIN (virtuals);
ca36f057 6544
c35cce41
MM
6545 /* Make entries for the rest of the virtuals. */
6546 while (virtuals)
8d08fdba 6547 {
ca36f057
MM
6548 /* Figure out what function we're looking at. */
6549 tree fn = TREE_VALUE (virtuals);
6550 tree base = DECL_CONTEXT (fn);
6551 /* The FN comes from BASE. So, we must caculate the adjustment
6552 from the virtual base that derived from BINFO to BASE. */
6553 tree base_binfo = get_binfo (base, vod->derived, /*protect=*/0);
6554
6555 binfo_inits
6556 = tree_cons (NULL_TREE,
6557 fold (build1 (NOP_EXPR, vtable_entry_type,
6558 size_diffop (BINFO_OFFSET (base_binfo),
6559 BINFO_OFFSET (vod->vbase)))),
6560 binfo_inits);
c35cce41
MM
6561 vod->index = fold (build (MINUS_EXPR, integer_type_node,
6562 vod->index, integer_one_node));
6563 virtuals = TREE_CHAIN (virtuals);
8d08fdba 6564 }
ca36f057 6565
c35cce41
MM
6566 /* The offests are built up in reverse order, so we straighten them
6567 here. We simultaneously add them to VOD->INITS; we're walking
6568 the bases in inheritance graph order, and the initializers are
6569 supposed to appear in reverse inheritance order, so that's
6570 correct. */
6571 while (binfo_inits)
6572 {
6573 tree next;
6574
6575 next = TREE_CHAIN (binfo_inits);
6576 TREE_CHAIN (binfo_inits) = vod->inits;
6577 vod->inits = binfo_inits;
6578 binfo_inits = next;
6579 }
ca36f057
MM
6580
6581 return NULL_TREE;
8d08fdba
MS
6582}
6583
c35cce41
MM
6584/* Adds the initializers for the vcall offset entries in the vtable
6585 for BINFO (which is part of the class hierarchy dominated by T) to
6586 VOD->INITS. */
c91a56d2 6587
c35cce41
MM
6588static void
6589build_vcall_offset_vtbl_entries (binfo, vod)
ca36f057 6590 tree binfo;
c35cce41 6591 vcall_offset_data *vod;
c91a56d2 6592{
c35cce41 6593 tree inits;
c91a56d2 6594
ca36f057
MM
6595 /* Under the old ABI, the adjustments to the `this' pointer were made
6596 elsewhere. */
6597 if (!vcall_offsets_in_vtable_p ())
c35cce41 6598 return;
9188c363 6599
ca36f057
MM
6600 /* We only need these entries if this base is a virtual base. */
6601 if (!TREE_VIA_VIRTUAL (binfo))
c35cce41 6602 return;
570221c2 6603
ca36f057
MM
6604 /* We need a vcall offset for each of the virtual functions in this
6605 vtable. For example:
570221c2 6606
ca36f057
MM
6607 class A { virtual void f (); };
6608 class B : virtual public A { };
6609 class C: virtual public A, public B {};
6610
6611 Now imagine:
570221c2 6612
ca36f057
MM
6613 B* b = new C;
6614 b->f();
5a11e05b 6615
ca36f057
MM
6616 The location of `A' is not at a fixed offset relative to `B'; the
6617 offset depends on the complete object derived from `B'. So,
6618 `B' vtable contains an entry for `f' that indicates by what
6619 amount the `this' pointer for `B' needs to be adjusted to arrive
6620 at `A'.
a59ca936 6621
ca36f057
MM
6622 We need entries for all the functions in our primary vtable and
6623 in our non-virtual bases vtables. For each base, the entries
6624 appear in the same order as in the base; but the bases themselves
6625 appear in reverse depth-first, left-to-right order. */
c35cce41
MM
6626 vod->vbase = binfo;
6627 inits = vod->inits;
6628 vod->inits = NULL_TREE;
6629 dfs_walk_real (binfo,
6630 dfs_build_vcall_offset_vtbl_entries,
6631 NULL,
6632 dfs_vcall_offset_queue_p,
6633 vod);
6634 vod->inits = chainon (vod->inits, inits);
570221c2 6635}
b54ccf71 6636
ca36f057
MM
6637/* Return vtbl initializers for the RTTI entries coresponding to the
6638 BINFO's vtable. BINFO is a part of the hierarchy dominated by
6639 T. */
b54ccf71 6640
ca36f057
MM
6641static tree
6642build_rtti_vtbl_entries (binfo, t)
6643 tree binfo;
6644 tree t;
b54ccf71 6645{
ca36f057
MM
6646 tree b;
6647 tree basetype;
ca36f057
MM
6648 tree offset;
6649 tree decl;
6650 tree init;
c35cce41 6651 tree inits;
b54ccf71 6652
ca36f057
MM
6653 basetype = BINFO_TYPE (binfo);
6654 inits = NULL_TREE;
b54ccf71 6655
ca36f057
MM
6656 /* For a COM object there is no RTTI entry. */
6657 if (CLASSTYPE_COM_INTERFACE (basetype))
6658 return inits;
b54ccf71 6659
ca36f057
MM
6660 /* To find the complete object, we will first convert to our most
6661 primary base, and then add the offset in the vtbl to that value. */
6662 b = binfo;
6663 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b)))
b54ccf71 6664 {
c35cce41
MM
6665 tree primary_base;
6666
6667 primary_base = BINFO_PRIMARY_BINFO (b);
6668 if (!BINFO_PRIMARY_MARKED_P (primary_base))
6669 break;
6670 b = primary_base;
b54ccf71 6671 }
c35cce41 6672 offset = size_diffop (size_zero_node, BINFO_OFFSET (b));
8f032717 6673
ca36f057
MM
6674 /* The second entry is, in the case of the new ABI, the address of
6675 the typeinfo object, or, in the case of the old ABI, a function
6676 which returns a typeinfo object. */
6677 if (new_abi_rtti_p ())
6678 {
6679 if (flag_rtti)
6680 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
6681 else
6682 decl = integer_zero_node;
8f032717 6683
ca36f057
MM
6684 /* Convert the declaration to a type that can be stored in the
6685 vtable. */
6686 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
6687 TREE_CONSTANT (init) = 1;
6688 }
6689 else
6690 {
6691 if (flag_rtti)
6692 decl = get_tinfo_decl (t);
6693 else
6694 decl = abort_fndecl;
8f032717 6695
ca36f057
MM
6696 /* Convert the declaration to a type that can be stored in the
6697 vtable. */
6698 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, decl);
6699 TREE_CONSTANT (init) = 1;
6700 init = build_vtable_entry (offset, integer_zero_node, init);
6701 }
ca36f057 6702 inits = tree_cons (NULL_TREE, init, inits);
8f032717 6703
c35cce41
MM
6704 /* Add the offset-to-top entry. It comes earlier in the vtable that
6705 the the typeinfo entry. */
6706 if (flag_vtable_thunks)
6707 {
6708 /* Convert the offset to look like a function pointer, so that
6709 we can put it in the vtable. */
6710 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
6711 TREE_CONSTANT (init) = 1;
6712 inits = tree_cons (NULL_TREE, init, inits);
6713 }
6714
ca36f057 6715 return inits;
8f032717
MM
6716}
6717
ca36f057
MM
6718/* Build an entry in the virtual function table. DELTA is the offset
6719 for the `this' pointer. VCALL_INDEX is the vtable index containing
6720 the vcall offset; zero if none. ENTRY is the virtual function
6721 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
6722 but it may not actually be a virtual function table pointer. (For
6723 example, it might be the address of the RTTI object, under the new
6724 ABI.) */
8f032717 6725
ca36f057
MM
6726static tree
6727build_vtable_entry (delta, vcall_index, entry)
6728 tree delta;
6729 tree vcall_index;
6730 tree entry;
8f032717 6731{
ca36f057 6732 if (flag_vtable_thunks)
8f032717 6733 {
ca36f057
MM
6734 HOST_WIDE_INT idelta;
6735 HOST_WIDE_INT ivindex;
6736
6737 idelta = tree_low_cst (delta, 0);
6738 ivindex = tree_low_cst (vcall_index, 0);
6739 if ((idelta || ivindex)
6740 && ! DECL_PURE_VIRTUAL_P (TREE_OPERAND (entry, 0)))
6741 {
6742 entry = make_thunk (entry, idelta, ivindex);
6743 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
6744 TREE_READONLY (entry) = 1;
6745 TREE_CONSTANT (entry) = 1;
6746 }
6747#ifdef GATHER_STATISTICS
6748 n_vtable_entries += 1;
6749#endif
6750 return entry;
8f032717 6751 }
ca36f057
MM
6752 else
6753 {
6754 extern int flag_huge_objects;
6755 tree elems = tree_cons (NULL_TREE, delta,
6756 tree_cons (NULL_TREE, integer_zero_node,
6757 build_tree_list (NULL_TREE, entry)));
6758 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
4a314e0c 6759
ca36f057
MM
6760 /* We don't use vcall offsets when not using vtable thunks. */
6761 my_friendly_assert (integer_zerop (vcall_index), 20000125);
4a314e0c 6762
ca36f057
MM
6763 /* DELTA used to be constructed by `size_int' and/or size_binop,
6764 which caused overflow problems when it was negative. That should
6765 be fixed now. */
4a314e0c 6766
ca36f057
MM
6767 if (! int_fits_type_p (delta, delta_type_node))
6768 {
6769 if (flag_huge_objects)
6770 sorry ("object size exceeds built-in limit for virtual function table implementation");
6771 else
6772 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
6773 }
6774
6775 TREE_CONSTANT (entry) = 1;
6776 TREE_STATIC (entry) = 1;
6777 TREE_READONLY (entry) = 1;
4a314e0c 6778
ca36f057
MM
6779#ifdef GATHER_STATISTICS
6780 n_vtable_entries += 1;
6781#endif
6782
6783 return entry;
6784 }
4a314e0c 6785}