* cp-tree.h (CLASSTYPE_DESTRUCTORS): Fix typo in comment.
[official-gcc.git] / gcc / cp / class.c
blob2d8ab83dee2aa55a19135e7f49057bfb0cea62ed
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "rtl.h"
32 #include "output.h"
33 #include "toplev.h"
34 #include "ggc.h"
35 #include "lex.h"
37 #include "obstack.h"
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
41 /* The number of nested classes being processed. If we are not in the
42 scope of any class, this is zero. */
44 int current_class_depth;
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 */
50 typedef struct class_stack_node {
51 /* The name of the class. */
52 tree name;
54 /* The _TYPE node for the class. */
55 tree type;
57 /* The access specifier pending for new declarations in the scope of
58 this class. */
59 tree access;
61 /* If were defining TYPE, the names used in this class. */
62 splay_tree names_used;
63 }* class_stack_node_t;
65 typedef struct vtbl_init_data_s
67 /* The base for which we're building initializers. */
68 tree binfo;
69 /* The binfo for the most-derived type. */
70 tree derived;
71 /* The negative-index vtable initializers built up so far. These
72 are in order from least negative index to most negative index. */
73 tree inits;
74 /* The last (i.e., most negative entry in INITS. */
75 tree* last_init;
76 /* The binfo for the virtual base for which we're building
77 vcall offset initializers. */
78 tree vbase;
79 /* The functions in vbase for which we have already provided vcall
80 offsets. */
81 varray_type fns;
82 /* The vtable index of the next vcall or vbase offset. */
83 tree index;
84 /* Nonzero if we are building the initializer for the primary
85 vtable. */
86 int primary_vtbl_p;
87 /* Nonzero if we are building the initializer for a construction
88 vtable. */
89 int ctor_vtbl_p;
90 } vtbl_init_data;
92 /* The type of a function passed to walk_subobject_offsets. */
93 typedef int (*subobject_offset_fn) PARAMS ((tree, tree, splay_tree));
95 /* The stack itself. This is an dynamically resized array. The
96 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
97 static int current_class_stack_size;
98 static class_stack_node_t current_class_stack;
100 /* An array of all local classes present in this translation unit, in
101 declaration order. */
102 varray_type local_classes;
104 static tree get_vfield_name PARAMS ((tree));
105 static void finish_struct_anon PARAMS ((tree));
106 static tree build_vbase_pointer PARAMS ((tree, tree));
107 static tree build_vtable_entry PARAMS ((tree, tree, tree, int));
108 static tree get_vtable_name PARAMS ((tree));
109 static tree get_derived_offset PARAMS ((tree, tree));
110 static tree get_basefndecls PARAMS ((tree, tree));
111 static int build_primary_vtable PARAMS ((tree, tree));
112 static int build_secondary_vtable PARAMS ((tree, tree));
113 static tree dfs_finish_vtbls PARAMS ((tree, void *));
114 static void finish_vtbls PARAMS ((tree));
115 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
116 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
117 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
118 static void delete_duplicate_fields PARAMS ((tree));
119 static void finish_struct_bits PARAMS ((tree));
120 static int alter_access PARAMS ((tree, tree, tree));
121 static void handle_using_decl PARAMS ((tree, tree));
122 static int same_signature_p PARAMS ((tree, tree));
123 static int strictly_overrides PARAMS ((tree, tree));
124 static void mark_overriders PARAMS ((tree, tree));
125 static void check_for_override PARAMS ((tree, tree));
126 static tree dfs_modify_vtables PARAMS ((tree, void *));
127 static tree modify_all_vtables PARAMS ((tree, int *, tree));
128 static void determine_primary_base PARAMS ((tree, int *));
129 static void finish_struct_methods PARAMS ((tree));
130 static void maybe_warn_about_overly_private_class PARAMS ((tree));
131 static int field_decl_cmp PARAMS ((const tree *, const tree *));
132 static int method_name_cmp PARAMS ((const tree *, const tree *));
133 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
134 static tree fixed_type_or_null PARAMS ((tree, int *));
135 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
136 int, int, tree));
137 static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
138 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
139 static int count_fields PARAMS ((tree));
140 static int add_fields_to_vec PARAMS ((tree, tree, int));
141 static void check_bitfield_decl PARAMS ((tree));
142 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
143 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
144 int *));
145 static void build_base_field PARAMS ((record_layout_info, tree, int *,
146 splay_tree));
147 static void build_base_fields PARAMS ((record_layout_info, int *,
148 splay_tree));
149 static tree build_vbase_pointer_fields PARAMS ((record_layout_info, int *));
150 static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, tree,
151 int *));
152 static void check_methods PARAMS ((tree));
153 static void remove_zero_width_bit_fields PARAMS ((tree));
154 static void check_bases PARAMS ((tree, int *, int *, int *));
155 static void check_bases_and_members PARAMS ((tree, int *));
156 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
157 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
158 static void fixup_pending_inline PARAMS ((tree));
159 static void fixup_inline_methods PARAMS ((tree));
160 static void set_primary_base PARAMS ((tree, tree, int *));
161 static void propagate_binfo_offsets PARAMS ((tree, tree));
162 static void layout_virtual_bases PARAMS ((tree, splay_tree));
163 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
164 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
165 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
166 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
167 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
168 static void layout_vtable_decl PARAMS ((tree, int));
169 static tree dfs_find_final_overrider PARAMS ((tree, void *));
170 static tree find_final_overrider PARAMS ((tree, tree, tree));
171 static int make_new_vtable PARAMS ((tree, tree));
172 static void dump_class_hierarchy_r PARAMS ((FILE *, tree, tree, int));
173 extern void dump_class_hierarchy PARAMS ((const char *, tree));
174 static tree build_vtable PARAMS ((tree, tree, tree));
175 static void initialize_vtable PARAMS ((tree, tree));
176 static void initialize_array PARAMS ((tree, tree));
177 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
178 tree, tree,
179 splay_tree));
180 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
181 static void layout_empty_base PARAMS ((tree, tree, splay_tree));
182 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
183 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
184 tree));
185 static void set_vindex PARAMS ((tree, int *));
186 static void build_rtti_vtbl_entries PARAMS ((tree, tree, vtbl_init_data *));
187 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
188 vtbl_init_data *));
189 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
190 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
191 static void mark_primary_bases PARAMS ((tree));
192 static tree mark_primary_virtual_base PARAMS ((tree, tree, tree));
193 static void clone_constructors_and_destructors PARAMS ((tree));
194 static tree build_clone PARAMS ((tree, tree));
195 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
196 static tree copy_virtuals PARAMS ((tree));
197 static void build_ctor_vtbl_group PARAMS ((tree, tree));
198 static void build_vtt PARAMS ((tree));
199 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
200 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
201 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
202 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
203 static tree get_matching_base PARAMS ((tree, tree));
204 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
205 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
206 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
207 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
208 tree, splay_tree, int));
209 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
210 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
211 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
212 splay_tree_key k2));
215 /* Macros for dfs walking during vtt construction. See
216 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
217 and dfs_fixup_binfo_vtbls. */
218 #define VTT_TOP_LEVEL_P(node) TREE_UNSIGNED(node)
219 #define VTT_MARKED_BINFO_P(node) TREE_USED(node)
221 /* Variables shared between class.c and call.c. */
223 #ifdef GATHER_STATISTICS
224 int n_vtables = 0;
225 int n_vtable_entries = 0;
226 int n_vtable_searches = 0;
227 int n_vtable_elems = 0;
228 int n_convert_harshness = 0;
229 int n_compute_conversion_costs = 0;
230 int n_build_method_call = 0;
231 int n_inner_fields_searched = 0;
232 #endif
234 /* Virtual base class layout. */
236 /* Returns a list of virtual base class pointers as a chain of
237 FIELD_DECLS. */
239 static tree
240 build_vbase_pointer_fields (rli, empty_p)
241 record_layout_info rli;
242 int *empty_p;
244 /* Chain to hold all the new FIELD_DECLs which point at virtual
245 base classes. */
246 tree rec = rli->t;
247 tree vbase_decls = NULL_TREE;
248 tree binfos = TYPE_BINFO_BASETYPES (rec);
249 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
250 tree decl;
251 int i;
253 /* Under the new ABI, there are no vbase pointers in the object.
254 Instead, the offsets are stored in the vtable. */
255 if (vbase_offsets_in_vtable_p ())
256 return NULL_TREE;
258 /* Loop over the baseclasses, adding vbase pointers as needed. */
259 for (i = 0; i < n_baseclasses; i++)
261 register tree base_binfo = TREE_VEC_ELT (binfos, i);
262 register tree basetype = BINFO_TYPE (base_binfo);
264 if (!COMPLETE_TYPE_P (basetype))
265 /* This error is now reported in xref_tag, thus giving better
266 location information. */
267 continue;
269 /* All basetypes are recorded in the association list of the
270 derived type. */
272 if (TREE_VIA_VIRTUAL (base_binfo))
274 int j;
275 const char *name;
277 /* The offset for a virtual base class is only used in computing
278 virtual function tables and for initializing virtual base
279 pointers. It is built once `get_vbase_types' is called. */
281 /* If this basetype can come from another vbase pointer
282 without an additional indirection, we will share
283 that pointer. If an indirection is involved, we
284 make our own pointer. */
285 for (j = 0; j < n_baseclasses; j++)
287 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
288 if (! TREE_VIA_VIRTUAL (other_base_binfo)
289 && binfo_for_vbase (basetype, BINFO_TYPE (other_base_binfo)))
290 goto got_it;
292 FORMAT_VBASE_NAME (name, basetype);
293 decl = build_vtbl_or_vbase_field (get_identifier (name),
294 get_identifier (VTABLE_BASE),
295 build_pointer_type (basetype),
296 rec,
297 basetype,
298 empty_p);
299 BINFO_VPTR_FIELD (base_binfo) = decl;
300 TREE_CHAIN (decl) = vbase_decls;
301 place_field (rli, decl);
302 vbase_decls = decl;
303 *empty_p = 0;
305 got_it:
306 /* The space this decl occupies has already been accounted for. */
311 return vbase_decls;
314 /* Returns a pointer to the virtual base class of EXP that has the
315 indicated TYPE. EXP is of class type, not a pointer type. */
317 static tree
318 build_vbase_pointer (exp, type)
319 tree exp, type;
321 if (vbase_offsets_in_vtable_p ())
323 tree vbase;
324 tree vbase_ptr;
326 /* Find the shared copy of TYPE; that's where the vtable offset
327 is recorded. */
328 vbase = binfo_for_vbase (type, TREE_TYPE (exp));
329 /* Find the virtual function table pointer. */
330 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
331 /* Compute the location where the offset will lie. */
332 vbase_ptr = build (PLUS_EXPR,
333 TREE_TYPE (vbase_ptr),
334 vbase_ptr,
335 BINFO_VPTR_FIELD (vbase));
336 vbase_ptr = build1 (NOP_EXPR,
337 build_pointer_type (ptrdiff_type_node),
338 vbase_ptr);
339 /* Add the contents of this location to EXP. */
340 return build (PLUS_EXPR,
341 build_pointer_type (type),
342 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
343 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
345 else
347 char *name;
348 FORMAT_VBASE_NAME (name, type);
349 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
353 /* Build multi-level access to EXPR using hierarchy path PATH.
354 CODE is PLUS_EXPR if we are going with the grain,
355 and MINUS_EXPR if we are not (in which case, we cannot traverse
356 virtual baseclass links).
358 TYPE is the type we want this path to have on exit.
360 NONNULL is non-zero if we know (for any reason) that EXPR is
361 not, in fact, zero. */
363 tree
364 build_vbase_path (code, type, expr, path, nonnull)
365 enum tree_code code;
366 tree type, expr, path;
367 int nonnull;
369 register int changed = 0;
370 tree last = NULL_TREE, last_virtual = NULL_TREE;
371 int fixed_type_p;
372 tree null_expr = 0, nonnull_expr;
373 tree basetype;
374 tree offset = integer_zero_node;
376 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
377 return build1 (NOP_EXPR, type, expr);
379 /* We could do better if we had additional logic to convert back to the
380 unconverted type (the static type of the complete object), and then
381 convert back to the type we want. Until that is done, we only optimize
382 if the complete type is the same type as expr has. */
383 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
385 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
386 expr = save_expr (expr);
387 nonnull_expr = expr;
389 path = reverse_path (path);
391 basetype = BINFO_TYPE (path);
393 while (path)
395 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
397 last_virtual = BINFO_TYPE (TREE_VALUE (path));
398 if (code == PLUS_EXPR)
400 changed = ! fixed_type_p;
402 if (changed)
404 tree ind;
406 /* We already check for ambiguous things in the caller, just
407 find a path. */
408 if (last)
410 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
411 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
413 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
414 nonnull_expr = build_vbase_pointer (ind, last_virtual);
415 if (nonnull == 0
416 && TREE_CODE (type) == POINTER_TYPE
417 && null_expr == NULL_TREE)
419 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
420 expr = build (COND_EXPR, build_pointer_type (last_virtual),
421 build (EQ_EXPR, boolean_type_node, expr,
422 integer_zero_node),
423 null_expr, nonnull_expr);
426 /* else we'll figure out the offset below. */
428 /* Happens in the case of parse errors. */
429 if (nonnull_expr == error_mark_node)
430 return error_mark_node;
432 else
434 cp_error ("cannot cast up from virtual baseclass `%T'",
435 last_virtual);
436 return error_mark_node;
439 last = TREE_VALUE (path);
440 path = TREE_CHAIN (path);
442 /* LAST is now the last basetype assoc on the path. */
444 /* A pointer to a virtual base member of a non-null object
445 is non-null. Therefore, we only need to test for zeroness once.
446 Make EXPR the canonical expression to deal with here. */
447 if (null_expr)
449 TREE_OPERAND (expr, 2) = nonnull_expr;
450 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
451 = TREE_TYPE (nonnull_expr);
453 else
454 expr = nonnull_expr;
456 /* If we go through any virtual base pointers, make sure that
457 casts to BASETYPE from the last virtual base class use
458 the right value for BASETYPE. */
459 if (changed)
461 tree intype = TREE_TYPE (TREE_TYPE (expr));
463 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
464 offset
465 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
467 else
468 offset = BINFO_OFFSET (last);
470 if (! integer_zerop (offset))
472 /* Bash types to make the backend happy. */
473 offset = cp_convert (type, offset);
475 /* If expr might be 0, we need to preserve that zeroness. */
476 if (nonnull == 0)
478 if (null_expr)
479 TREE_TYPE (null_expr) = type;
480 else
481 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
482 if (TREE_SIDE_EFFECTS (expr))
483 expr = save_expr (expr);
485 return build (COND_EXPR, type,
486 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
487 null_expr,
488 build (code, type, expr, offset));
490 else return build (code, type, expr, offset);
493 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
494 be used multiple times in initialization of multiple inheritance. */
495 if (null_expr)
497 TREE_TYPE (expr) = type;
498 return expr;
500 else
501 return build1 (NOP_EXPR, type, expr);
505 /* Virtual function things. */
507 /* We want to give the assembler the vtable identifier as well as
508 the offset to the function pointer. So we generate
510 __asm__ __volatile__ (".vtable_entry %c0, %c1"
511 : : "s"(&class_vtable),
512 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
514 static void
515 build_vtable_entry_ref (basetype, vtbl, idx)
516 tree basetype, vtbl, idx;
518 static char asm_stmt[] = ".vtable_entry %c0, %c1";
519 tree s, i, i2;
521 s = build_unary_op (ADDR_EXPR,
522 get_vtbl_decl_for_binfo (TYPE_BINFO (basetype)),
524 s = build_tree_list (build_string (1, "s"), s);
526 i = build_array_ref (vtbl, idx);
527 if (!flag_vtable_thunks)
528 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
529 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
530 i2 = build_array_ref (vtbl, build_int_2(0,0));
531 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
532 i = cp_build_binary_op (MINUS_EXPR, i, i2);
533 i = build_tree_list (build_string (1, "i"), i);
535 finish_asm_stmt (ridpointers[RID_VOLATILE],
536 build_string (sizeof(asm_stmt)-1, asm_stmt),
537 NULL_TREE, chainon (s, i), NULL_TREE);
540 /* Given an object INSTANCE, return an expression which yields the
541 virtual function vtable element corresponding to INDEX. There are
542 many special cases for INSTANCE which we take care of here, mainly
543 to avoid creating extra tree nodes when we don't have to. */
545 tree
546 build_vtbl_ref (instance, idx)
547 tree instance, idx;
549 tree vtbl, aref;
550 tree basetype = TREE_TYPE (instance);
552 if (TREE_CODE (basetype) == REFERENCE_TYPE)
553 basetype = TREE_TYPE (basetype);
555 if (instance == current_class_ref)
556 vtbl = build_vfield_ref (instance, basetype);
557 else
559 if (optimize)
561 /* Try to figure out what a reference refers to, and
562 access its virtual function table directly. */
563 tree ref = NULL_TREE;
565 if (TREE_CODE (instance) == INDIRECT_REF
566 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
567 ref = TREE_OPERAND (instance, 0);
568 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
569 ref = instance;
571 if (ref && TREE_CODE (ref) == VAR_DECL
572 && DECL_INITIAL (ref))
574 tree init = DECL_INITIAL (ref);
576 while (TREE_CODE (init) == NOP_EXPR
577 || TREE_CODE (init) == NON_LVALUE_EXPR)
578 init = TREE_OPERAND (init, 0);
579 if (TREE_CODE (init) == ADDR_EXPR)
581 init = TREE_OPERAND (init, 0);
582 if (IS_AGGR_TYPE (TREE_TYPE (init))
583 && (TREE_CODE (init) == PARM_DECL
584 || TREE_CODE (init) == VAR_DECL))
585 instance = init;
590 if (IS_AGGR_TYPE (TREE_TYPE (instance))
591 && (TREE_CODE (instance) == RESULT_DECL
592 || TREE_CODE (instance) == PARM_DECL
593 || TREE_CODE (instance) == VAR_DECL))
595 vtbl = TYPE_BINFO_VTABLE (basetype);
596 /* Knowing the dynamic type of INSTANCE we can easily obtain
597 the correct vtable entry. In the new ABI, we resolve
598 this back to be in terms of the primary vtable. */
599 if (TREE_CODE (vtbl) == PLUS_EXPR)
601 idx = fold (build (PLUS_EXPR,
602 TREE_TYPE (idx),
603 idx,
604 build (EXACT_DIV_EXPR,
605 TREE_TYPE (idx),
606 TREE_OPERAND (vtbl, 1),
607 TYPE_SIZE_UNIT (vtable_entry_type))));
608 vtbl = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
611 else
612 vtbl = build_vfield_ref (instance, basetype);
615 assemble_external (vtbl);
617 if (flag_vtable_gc)
618 build_vtable_entry_ref (basetype, vtbl, idx);
620 aref = build_array_ref (vtbl, idx);
622 return aref;
625 /* Given an object INSTANCE, return an expression which yields the
626 virtual function corresponding to INDEX. There are many special
627 cases for INSTANCE which we take care of here, mainly to avoid
628 creating extra tree nodes when we don't have to. */
630 tree
631 build_vfn_ref (ptr_to_instptr, instance, idx)
632 tree *ptr_to_instptr, instance;
633 tree idx;
635 tree aref = build_vtbl_ref (instance, idx);
637 /* When using thunks, there is no extra delta, and we get the pfn
638 directly. */
639 if (flag_vtable_thunks)
640 return aref;
642 if (ptr_to_instptr)
644 /* Save the intermediate result in a SAVE_EXPR so we don't have to
645 compute each component of the virtual function pointer twice. */
646 if (TREE_CODE (aref) == INDIRECT_REF)
647 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
649 *ptr_to_instptr
650 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
651 *ptr_to_instptr,
652 cp_convert (ptrdiff_type_node,
653 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
656 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
659 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
660 for the given TYPE. */
662 static tree
663 get_vtable_name (type)
664 tree type;
666 return mangle_vtbl_for_type (type);
669 /* Return an IDENTIFIER_NODE for the name of the virtual table table
670 for TYPE. */
672 tree
673 get_vtt_name (type)
674 tree type;
676 return mangle_vtt_for_type (type);
679 /* Return the offset to the main vtable for a given base BINFO. */
681 tree
682 get_vfield_offset (binfo)
683 tree binfo;
685 return
686 size_binop (PLUS_EXPR, byte_position (TYPE_VFIELD (BINFO_TYPE (binfo))),
687 BINFO_OFFSET (binfo));
690 /* Get the offset to the start of the original binfo that we derived
691 this binfo from. If we find TYPE first, return the offset only
692 that far. The shortened search is useful because the this pointer
693 on method calling is expected to point to a DECL_CONTEXT (fndecl)
694 object, and not a baseclass of it. */
696 static tree
697 get_derived_offset (binfo, type)
698 tree binfo, type;
700 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
701 tree offset2;
703 while (!same_type_p (BINFO_TYPE (binfo), type))
704 binfo = get_primary_binfo (binfo);
706 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
707 return size_binop (MINUS_EXPR, offset1, offset2);
710 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
711 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
712 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
714 static tree
715 build_vtable (class_type, name, vtable_type)
716 tree class_type;
717 tree name;
718 tree vtable_type;
720 tree decl;
722 decl = build_lang_decl (VAR_DECL, name, vtable_type);
723 DECL_CONTEXT (decl) = class_type;
724 DECL_ARTIFICIAL (decl) = 1;
725 TREE_STATIC (decl) = 1;
726 #ifndef WRITABLE_VTABLES
727 /* Make them READONLY by default. (mrs) */
728 TREE_READONLY (decl) = 1;
729 #endif
730 DECL_VIRTUAL_P (decl) = 1;
731 import_export_vtable (decl, class_type, 0);
733 return decl;
736 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
737 or even complete. If this does not exist, create it. If COMPLETE is
738 non-zero, then complete the definition of it -- that will render it
739 impossible to actually build the vtable, but is useful to get at those
740 which are known to exist in the runtime. */
742 tree
743 get_vtable_decl (type, complete)
744 tree type;
745 int complete;
747 tree name = get_vtable_name (type);
748 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
750 if (decl)
752 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
753 && DECL_VIRTUAL_P (decl), 20000118);
754 return decl;
757 decl = build_vtable (type, name, void_type_node);
758 decl = pushdecl_top_level (decl);
759 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
760 20000517);
762 /* At one time the vtable info was grabbed 2 words at a time. This
763 fails on sparc unless you have 8-byte alignment. (tiemann) */
764 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
765 DECL_ALIGN (decl));
767 if (complete)
769 DECL_EXTERNAL (decl) = 1;
770 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
773 return decl;
776 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
777 BV_VCALL_INDEX for each entry is cleared. */
779 static tree
780 copy_virtuals (binfo)
781 tree binfo;
783 tree copies;
784 tree t;
786 copies = copy_list (BINFO_VIRTUALS (binfo));
787 for (t = copies; t; t = TREE_CHAIN (t))
789 BV_VCALL_INDEX (t) = NULL_TREE;
790 BV_USE_VCALL_INDEX_P (t) = 0;
791 BV_GENERATE_THUNK_WITH_VTABLE_P (t) = 0;
794 return copies;
797 /* Build the primary virtual function table for TYPE. If BINFO is
798 non-NULL, build the vtable starting with the initial approximation
799 that it is the same as the one which is the head of the association
800 list. Returns a non-zero value if a new vtable is actually
801 created. */
803 static int
804 build_primary_vtable (binfo, type)
805 tree binfo, type;
807 tree decl;
808 tree virtuals;
810 decl = get_vtable_decl (type, /*complete=*/0);
812 if (binfo)
814 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
815 /* We have already created a vtable for this base, so there's
816 no need to do it again. */
817 return 0;
819 virtuals = copy_virtuals (binfo);
820 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
821 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
822 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
824 else
826 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
827 20000118);
828 virtuals = NULL_TREE;
831 #ifdef GATHER_STATISTICS
832 n_vtables += 1;
833 n_vtable_elems += list_length (virtuals);
834 #endif
836 /* Initialize the association list for this type, based
837 on our first approximation. */
838 TYPE_BINFO_VTABLE (type) = decl;
839 TYPE_BINFO_VIRTUALS (type) = virtuals;
840 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
841 return 1;
844 /* Give TYPE a new virtual function table which is initialized
845 with a skeleton-copy of its original initialization. The only
846 entry that changes is the `delta' entry, so we can really
847 share a lot of structure.
849 FOR_TYPE is the derived type which caused this table to
850 be needed.
852 BINFO is the type association which provided TYPE for FOR_TYPE.
854 The order in which vtables are built (by calling this function) for
855 an object must remain the same, otherwise a binary incompatibility
856 can result. */
858 static int
859 build_secondary_vtable (binfo, for_type)
860 tree binfo, for_type;
862 tree basetype;
863 tree orig_decl = BINFO_VTABLE (binfo);
864 tree name;
865 tree new_decl;
866 tree offset;
867 tree path = binfo;
868 char *buf;
869 const char *buf2;
870 char joiner = '_';
871 int i;
873 #ifdef JOINER
874 joiner = JOINER;
875 #endif
877 if (TREE_VIA_VIRTUAL (binfo))
878 my_friendly_assert (binfo == binfo_for_vbase (BINFO_TYPE (binfo),
879 current_class_type),
880 170);
882 if (BINFO_NEW_VTABLE_MARKED (binfo, current_class_type))
883 /* We already created a vtable for this base. There's no need to
884 do it again. */
885 return 0;
887 /* Remember that we've created a vtable for this BINFO, so that we
888 don't try to do so again. */
889 SET_BINFO_NEW_VTABLE_MARKED (binfo, current_class_type);
891 /* Make fresh virtual list, so we can smash it later. */
892 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
894 if (TREE_VIA_VIRTUAL (binfo))
896 tree binfo1 = binfo_for_vbase (BINFO_TYPE (binfo), for_type);
898 /* XXX - This should never happen, if it does, the caller should
899 ensure that the binfo is from for_type's binfos, not from any
900 base type's. We can remove all this code after a while. */
901 if (binfo1 != binfo)
902 warning ("internal inconsistency: binfo offset error for rtti");
904 offset = BINFO_OFFSET (binfo1);
906 else
907 offset = BINFO_OFFSET (binfo);
909 /* In the new ABI, secondary vtables are laid out as part of the
910 same structure as the primary vtable. */
911 if (merge_primary_and_secondary_vtables_p ())
913 BINFO_VTABLE (binfo) = NULL_TREE;
914 return 1;
917 /* Create the declaration for the secondary vtable. */
918 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
919 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
920 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
922 /* We know that the vtable that we are going to create doesn't exist
923 yet in the global namespace, and when we finish, it will be
924 pushed into the global namespace. In complex MI hierarchies, we
925 have to loop while the name we are thinking of adding is globally
926 defined, adding more name components to the vtable name as we
927 loop, until the name is unique. This is because in complex MI
928 cases, we might have the same base more than once. This means
929 that the order in which this function is called for vtables must
930 remain the same, otherwise binary compatibility can be
931 compromised. */
933 while (1)
935 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
936 + 1 + i);
937 char *new_buf2;
939 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
940 buf2);
941 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
942 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
943 name = get_identifier (buf);
945 /* If this name doesn't clash, then we can use it, otherwise
946 we add more to the name until it is unique. */
948 if (! IDENTIFIER_GLOBAL_VALUE (name))
949 break;
951 /* Set values for next loop through, if the name isn't unique. */
953 path = BINFO_INHERITANCE_CHAIN (path);
955 /* We better not run out of stuff to make it unique. */
956 my_friendly_assert (path != NULL_TREE, 368);
958 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
960 if (for_type == basetype)
962 /* If we run out of basetypes in the path, we have already
963 found created a vtable with that name before, we now
964 resort to tacking on _%d to distinguish them. */
965 int j = 2;
966 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
967 buf1 = (char *) alloca (i);
968 do {
969 sprintf (buf1, "%s%c%s%c%d",
970 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
971 buf2, joiner, j);
972 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
973 + strlen (buf1) + 1);
974 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
975 name = get_identifier (buf);
977 /* If this name doesn't clash, then we can use it,
978 otherwise we add something different to the name until
979 it is unique. */
980 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
982 /* Hey, they really like MI don't they? Increase the 3
983 above to 6, and the 999 to 999999. :-) */
984 my_friendly_assert (j <= 999, 369);
986 break;
989 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
990 new_buf2 = (char *) alloca (i);
991 sprintf (new_buf2, "%s%c%s",
992 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
993 buf2 = new_buf2;
996 new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
997 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
998 DECL_USER_ALIGN (new_decl) = DECL_USER_ALIGN (orig_decl);
999 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
1001 #ifdef GATHER_STATISTICS
1002 n_vtables += 1;
1003 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
1004 #endif
1006 return 1;
1009 /* Create a new vtable for BINFO which is the hierarchy dominated by
1010 T. */
1012 static int
1013 make_new_vtable (t, binfo)
1014 tree t;
1015 tree binfo;
1017 if (binfo == TYPE_BINFO (t))
1018 /* In this case, it is *type*'s vtable we are modifying. We start
1019 with the approximation that it's vtable is that of the
1020 immediate base class. */
1021 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
1023 else
1024 /* This is our very own copy of `basetype' to play with. Later,
1025 we will fill in all the virtual functions that override the
1026 virtual functions in these base classes which are not defined
1027 by the current type. */
1028 return build_secondary_vtable (binfo, t);
1031 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
1032 (which is in the hierarchy dominated by T) list FNDECL as its
1033 BV_FN. DELTA is the required constant adjustment from the `this'
1034 pointer where the vtable entry appears to the `this' required when
1035 the function is actually called. */
1037 static void
1038 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
1039 tree t;
1040 tree binfo;
1041 tree fndecl;
1042 tree delta;
1043 tree *virtuals;
1045 tree v;
1047 v = *virtuals;
1049 if (fndecl != BV_FN (v)
1050 || !tree_int_cst_equal (delta, BV_DELTA (v)))
1052 tree base_fndecl;
1054 /* We need a new vtable for BINFO. */
1055 if (make_new_vtable (t, binfo))
1057 /* If we really did make a new vtable, we also made a copy
1058 of the BINFO_VIRTUALS list. Now, we have to find the
1059 corresponding entry in that list. */
1060 *virtuals = BINFO_VIRTUALS (binfo);
1061 while (BV_FN (*virtuals) != BV_FN (v))
1062 *virtuals = TREE_CHAIN (*virtuals);
1063 v = *virtuals;
1066 base_fndecl = BV_FN (v);
1067 BV_DELTA (v) = delta;
1068 BV_VCALL_INDEX (v) = NULL_TREE;
1069 BV_FN (v) = fndecl;
1071 /* Now assign virtual dispatch information, if unset. We can
1072 dispatch this, through any overridden base function. */
1073 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1075 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1076 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1081 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
1082 functions present in the vtable so far. */
1084 static void
1085 set_vindex (decl, vfuns_p)
1086 tree decl;
1087 int *vfuns_p;
1089 int vindex;
1091 vindex = (*vfuns_p)++;
1092 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
1095 /* Add a virtual function to all the appropriate vtables for the class
1096 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1097 allocate a new slot in our table. If it is error_mark_node, we
1098 know that no other function from another vtable is overridden by X.
1099 VFUNS_P keeps track of how many virtuals there are in our
1100 main vtable for the type, and we build upon the NEW_VIRTUALS list
1101 and return it. */
1103 static void
1104 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1105 vfuns_p, fndecl, t)
1106 tree *new_virtuals_p;
1107 tree *overridden_virtuals_p;
1108 int *vfuns_p;
1109 tree fndecl;
1110 tree t; /* Structure type. */
1112 tree new_virtual;
1114 /* If this function doesn't override anything from a base class, we
1115 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1116 override something, we keep it around and assign its DECL_VINDEX
1117 later, in modify_all_vtables. */
1118 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1119 /* We've already dealt with this function. */
1120 return;
1122 new_virtual = make_node (TREE_LIST);
1123 BV_FN (new_virtual) = fndecl;
1124 BV_DELTA (new_virtual) = integer_zero_node;
1126 if (DECL_VINDEX (fndecl) == error_mark_node)
1128 /* FNDECL is a new virtual function; it doesn't override any
1129 virtual function in a base class. */
1131 /* We remember that this was the base sub-object for rtti. */
1132 CLASSTYPE_RTTI (t) = t;
1134 /* Now assign virtual dispatch information. */
1135 set_vindex (fndecl, vfuns_p);
1136 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1138 /* Save the state we've computed on the NEW_VIRTUALS list. */
1139 TREE_CHAIN (new_virtual) = *new_virtuals_p;
1140 *new_virtuals_p = new_virtual;
1142 else
1144 /* FNDECL overrides a function from a base class. */
1145 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1146 *overridden_virtuals_p = new_virtual;
1150 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
1151 the method after the class has already been defined because a
1152 declaration for it was seen. (Even though that is erroneous, we
1153 add the method for improved error recovery.) */
1155 void
1156 add_method (type, method, error_p)
1157 tree type;
1158 tree method;
1159 int error_p;
1161 int using = (DECL_CONTEXT (method) != type);
1162 int len;
1163 int slot;
1164 tree method_vec;
1166 if (!CLASSTYPE_METHOD_VEC (type))
1167 /* Make a new method vector. We start with 8 entries. We must
1168 allocate at least two (for constructors and destructors), and
1169 we're going to end up with an assignment operator at some point
1170 as well.
1172 We could use a TREE_LIST for now, and convert it to a TREE_VEC
1173 in finish_struct, but we would probably waste more memory
1174 making the links in the list than we would by over-allocating
1175 the size of the vector here. Furthermore, we would complicate
1176 all the code that expects this to be a vector. */
1177 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1179 method_vec = CLASSTYPE_METHOD_VEC (type);
1180 len = TREE_VEC_LENGTH (method_vec);
1182 /* Constructors and destructors go in special slots. */
1183 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1184 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1185 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1186 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1187 else
1189 /* See if we already have an entry with this name. */
1190 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
1191 if (!TREE_VEC_ELT (method_vec, slot)
1192 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1193 slot)))
1194 == DECL_NAME (method)))
1195 break;
1197 if (slot == len)
1199 /* We need a bigger method vector. */
1200 int new_len;
1201 tree new_vec;
1203 /* In the non-error case, we are processing a class
1204 definition. Double the size of the vector to give room
1205 for new methods. */
1206 if (!error_p)
1207 new_len = 2 * len;
1208 /* In the error case, the vector is already complete. We
1209 don't expect many errors, and the rest of the front-end
1210 will get confused if there are empty slots in the vector. */
1211 else
1212 new_len = len + 1;
1214 new_vec = make_tree_vec (new_len);
1215 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1216 (PTR) &TREE_VEC_ELT (new_vec, 0),
1217 len * sizeof (tree));
1218 len = new_len;
1219 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1222 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1224 /* Type conversion operators have to come before ordinary
1225 methods; add_conversions depends on this to speed up
1226 looking for conversion operators. So, if necessary, we
1227 slide some of the vector elements up. In theory, this
1228 makes this algorithm O(N^2) but we don't expect many
1229 conversion operators. */
1230 for (slot = 2; slot < len; ++slot)
1232 tree fn = TREE_VEC_ELT (method_vec, slot);
1234 if (!fn)
1235 /* There are no more entries in the vector, so we
1236 can insert the new conversion operator here. */
1237 break;
1239 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1240 /* We can insert the new function right at the
1241 SLOTth position. */
1242 break;
1245 if (!TREE_VEC_ELT (method_vec, slot))
1246 /* There is nothing in the Ith slot, so we can avoid
1247 moving anything. */
1249 else
1251 /* We know the last slot in the vector is empty
1252 because we know that at this point there's room
1253 for a new function. */
1254 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1255 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1256 (len - slot - 1) * sizeof (tree));
1257 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1262 if (template_class_depth (type))
1263 /* TYPE is a template class. Don't issue any errors now; wait
1264 until instantiation time to complain. */
1266 else
1268 tree fns;
1270 /* Check to see if we've already got this method. */
1271 for (fns = TREE_VEC_ELT (method_vec, slot);
1272 fns;
1273 fns = OVL_NEXT (fns))
1275 tree fn = OVL_CURRENT (fns);
1277 if (TREE_CODE (fn) != TREE_CODE (method))
1278 continue;
1280 if (TREE_CODE (method) != TEMPLATE_DECL)
1282 /* [over.load] Member function declarations with the
1283 same name and the same parameter types cannot be
1284 overloaded if any of them is a static member
1285 function declaration. */
1286 if ((DECL_STATIC_FUNCTION_P (fn)
1287 != DECL_STATIC_FUNCTION_P (method))
1288 || using)
1290 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1291 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1293 if (! DECL_STATIC_FUNCTION_P (fn))
1294 parms1 = TREE_CHAIN (parms1);
1295 if (! DECL_STATIC_FUNCTION_P (method))
1296 parms2 = TREE_CHAIN (parms2);
1298 if (compparms (parms1, parms2))
1300 if (using)
1301 /* Defer to the local function. */
1302 return;
1303 else
1304 cp_error ("`%#D' and `%#D' cannot be overloaded",
1305 fn, method);
1309 /* Since this is an ordinary function in a
1310 non-template class, it's mangled name can be used
1311 as a unique identifier. This technique is only
1312 an optimization; we would get the same results if
1313 we just used decls_match here. */
1314 if (DECL_ASSEMBLER_NAME (fn)
1315 != DECL_ASSEMBLER_NAME (method))
1316 continue;
1318 else if (!decls_match (fn, method))
1319 continue;
1321 /* There has already been a declaration of this method
1322 or member template. */
1323 cp_error_at ("`%D' has already been declared in `%T'",
1324 method, type);
1326 /* We don't call duplicate_decls here to merge the
1327 declarations because that will confuse things if the
1328 methods have inline definitions. In particular, we
1329 will crash while processing the definitions. */
1330 return;
1334 /* Actually insert the new method. */
1335 TREE_VEC_ELT (method_vec, slot)
1336 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1338 /* Add the new binding. */
1339 if (!DECL_CONSTRUCTOR_P (method)
1340 && !DECL_DESTRUCTOR_P (method))
1341 push_class_level_binding (DECL_NAME (method),
1342 TREE_VEC_ELT (method_vec, slot));
1345 /* Subroutines of finish_struct. */
1347 /* Look through the list of fields for this struct, deleting
1348 duplicates as we go. This must be recursive to handle
1349 anonymous unions.
1351 FIELD is the field which may not appear anywhere in FIELDS.
1352 FIELD_PTR, if non-null, is the starting point at which
1353 chained deletions may take place.
1354 The value returned is the first acceptable entry found
1355 in FIELDS.
1357 Note that anonymous fields which are not of UNION_TYPE are
1358 not duplicates, they are just anonymous fields. This happens
1359 when we have unnamed bitfields, for example. */
1361 static tree
1362 delete_duplicate_fields_1 (field, fields)
1363 tree field, fields;
1365 tree x;
1366 tree prev = 0;
1367 if (DECL_NAME (field) == 0)
1369 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1370 return fields;
1372 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1373 fields = delete_duplicate_fields_1 (x, fields);
1374 return fields;
1376 else
1378 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1380 if (DECL_NAME (x) == 0)
1382 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1383 continue;
1384 TYPE_FIELDS (TREE_TYPE (x))
1385 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1386 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1388 if (prev == 0)
1389 fields = TREE_CHAIN (fields);
1390 else
1391 TREE_CHAIN (prev) = TREE_CHAIN (x);
1394 else if (TREE_CODE (field) == USING_DECL)
1395 /* A using declaration may is allowed to appear more than
1396 once. We'll prune these from the field list later, and
1397 handle_using_decl will complain about invalid multiple
1398 uses. */
1400 else if (DECL_NAME (field) == DECL_NAME (x))
1402 if (TREE_CODE (field) == CONST_DECL
1403 && TREE_CODE (x) == CONST_DECL)
1404 cp_error_at ("duplicate enum value `%D'", x);
1405 else if (TREE_CODE (field) == CONST_DECL
1406 || TREE_CODE (x) == CONST_DECL)
1407 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1409 else if (DECL_DECLARES_TYPE_P (field)
1410 && DECL_DECLARES_TYPE_P (x))
1412 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1413 continue;
1414 cp_error_at ("duplicate nested type `%D'", x);
1416 else if (DECL_DECLARES_TYPE_P (field)
1417 || DECL_DECLARES_TYPE_P (x))
1419 /* Hide tag decls. */
1420 if ((TREE_CODE (field) == TYPE_DECL
1421 && DECL_ARTIFICIAL (field))
1422 || (TREE_CODE (x) == TYPE_DECL
1423 && DECL_ARTIFICIAL (x)))
1424 continue;
1425 cp_error_at ("duplicate field `%D' (as type and non-type)",
1428 else
1429 cp_error_at ("duplicate member `%D'", x);
1430 if (prev == 0)
1431 fields = TREE_CHAIN (fields);
1432 else
1433 TREE_CHAIN (prev) = TREE_CHAIN (x);
1437 return fields;
1440 static void
1441 delete_duplicate_fields (fields)
1442 tree fields;
1444 tree x;
1445 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1446 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1449 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1450 legit, otherwise return 0. */
1452 static int
1453 alter_access (t, fdecl, access)
1454 tree t;
1455 tree fdecl;
1456 tree access;
1458 tree elem;
1460 if (!DECL_LANG_SPECIFIC (fdecl))
1461 retrofit_lang_decl (fdecl);
1463 elem = purpose_member (t, DECL_ACCESS (fdecl));
1464 if (elem)
1466 if (TREE_VALUE (elem) != access)
1468 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1469 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1470 else
1471 error ("conflicting access specifications for field `%s', ignored",
1472 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1474 else
1476 /* They're changing the access to the same thing they changed
1477 it to before. That's OK. */
1481 else
1483 enforce_access (t, fdecl);
1484 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1485 return 1;
1487 return 0;
1490 /* Process the USING_DECL, which is a member of T. */
1492 static void
1493 handle_using_decl (using_decl, t)
1494 tree using_decl;
1495 tree t;
1497 tree ctype = DECL_INITIAL (using_decl);
1498 tree name = DECL_NAME (using_decl);
1499 tree access
1500 = TREE_PRIVATE (using_decl) ? access_private_node
1501 : TREE_PROTECTED (using_decl) ? access_protected_node
1502 : access_public_node;
1503 tree fdecl, binfo;
1504 tree flist = NULL_TREE;
1505 tree old_value;
1507 binfo = binfo_or_else (ctype, t);
1508 if (! binfo)
1509 return;
1511 if (name == constructor_name (ctype)
1512 || name == constructor_name_full (ctype))
1514 cp_error_at ("`%D' names constructor", using_decl);
1515 return;
1517 if (name == constructor_name (t)
1518 || name == constructor_name_full (t))
1520 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1521 return;
1524 fdecl = lookup_member (binfo, name, 0, 0);
1526 if (!fdecl)
1528 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1529 return;
1532 if (BASELINK_P (fdecl))
1533 /* Ignore base type this came from. */
1534 fdecl = TREE_VALUE (fdecl);
1536 old_value = IDENTIFIER_CLASS_VALUE (name);
1537 if (old_value)
1539 if (is_overloaded_fn (old_value))
1540 old_value = OVL_CURRENT (old_value);
1542 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1543 /* OK */;
1544 else
1545 old_value = NULL_TREE;
1548 if (is_overloaded_fn (fdecl))
1549 flist = fdecl;
1551 if (! old_value)
1553 else if (is_overloaded_fn (old_value))
1555 if (flist)
1556 /* It's OK to use functions from a base when there are functions with
1557 the same name already present in the current class. */;
1558 else
1560 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1561 cp_error_at (" because of local method `%#D' with same name",
1562 OVL_CURRENT (old_value));
1563 return;
1566 else if (!DECL_ARTIFICIAL (old_value))
1568 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1569 cp_error_at (" because of local member `%#D' with same name", old_value);
1570 return;
1573 /* Make type T see field decl FDECL with access ACCESS.*/
1574 if (flist)
1575 for (; flist; flist = OVL_NEXT (flist))
1577 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1578 alter_access (t, OVL_CURRENT (flist), access);
1580 else
1581 alter_access (t, fdecl, access);
1584 /* Run through the base clases of T, updating
1585 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1586 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1587 the bases. */
1589 static void
1590 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1591 no_const_asn_ref_p)
1592 tree t;
1593 int *cant_have_default_ctor_p;
1594 int *cant_have_const_ctor_p;
1595 int *no_const_asn_ref_p;
1597 int n_baseclasses;
1598 int i;
1599 int seen_nearly_empty_base_p;
1600 tree binfos;
1602 binfos = TYPE_BINFO_BASETYPES (t);
1603 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1604 seen_nearly_empty_base_p = 0;
1606 /* An aggregate cannot have baseclasses. */
1607 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1609 for (i = 0; i < n_baseclasses; ++i)
1611 tree base_binfo;
1612 tree basetype;
1614 /* Figure out what base we're looking at. */
1615 base_binfo = TREE_VEC_ELT (binfos, i);
1616 basetype = TREE_TYPE (base_binfo);
1618 /* If the type of basetype is incomplete, then we already
1619 complained about that fact (and we should have fixed it up as
1620 well). */
1621 if (!COMPLETE_TYPE_P (basetype))
1623 int j;
1624 /* The base type is of incomplete type. It is
1625 probably best to pretend that it does not
1626 exist. */
1627 if (i == n_baseclasses-1)
1628 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1629 TREE_VEC_LENGTH (binfos) -= 1;
1630 n_baseclasses -= 1;
1631 for (j = i; j+1 < n_baseclasses; j++)
1632 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1633 continue;
1636 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1637 here because the case of virtual functions but non-virtual
1638 dtor is handled in finish_struct_1. */
1639 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1640 && TYPE_HAS_DESTRUCTOR (basetype))
1641 cp_warning ("base class `%#T' has a non-virtual destructor",
1642 basetype);
1644 /* If the base class doesn't have copy constructors or
1645 assignment operators that take const references, then the
1646 derived class cannot have such a member automatically
1647 generated. */
1648 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1649 *cant_have_const_ctor_p = 1;
1650 if (TYPE_HAS_ASSIGN_REF (basetype)
1651 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1652 *no_const_asn_ref_p = 1;
1653 /* Similarly, if the base class doesn't have a default
1654 constructor, then the derived class won't have an
1655 automatically generated default constructor. */
1656 if (TYPE_HAS_CONSTRUCTOR (basetype)
1657 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1659 *cant_have_default_ctor_p = 1;
1660 if (! TYPE_HAS_CONSTRUCTOR (t))
1661 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1662 basetype);
1665 /* If the base class is not empty or nearly empty, then this
1666 class cannot be nearly empty. */
1667 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1668 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1669 /* And if there is more than one nearly empty base, then the
1670 derived class is not nearly empty either. */
1671 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1672 && seen_nearly_empty_base_p)
1673 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1674 /* If this is the first nearly empty base class, then remember
1675 that we saw it. */
1676 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1677 seen_nearly_empty_base_p = 1;
1679 /* A lot of properties from the bases also apply to the derived
1680 class. */
1681 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1682 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1683 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1684 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1685 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1686 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1687 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1688 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1689 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1690 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1692 /* Derived classes can implicitly become COMified if their bases
1693 are COM. */
1694 if (CLASSTYPE_COM_INTERFACE (basetype))
1695 CLASSTYPE_COM_INTERFACE (t) = 1;
1696 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1698 cp_error
1699 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1700 t, basetype);
1701 CLASSTYPE_COM_INTERFACE (t) = 0;
1706 /* Binfo FROM is within a virtual heirarchy which is being reseated to
1707 TO. Move primary information from FROM to TO, and recursively traverse
1708 into FROM's bases. The heirarchy is dominated by TYPE. MAPPINGS is an
1709 assoc list of binfos that have already been reseated. */
1711 static void
1712 force_canonical_binfo_r (to, from, type, mappings)
1713 tree to;
1714 tree from;
1715 tree type;
1716 tree mappings;
1718 int i, n_baseclasses = BINFO_N_BASETYPES (from);
1720 BINFO_INDIRECT_PRIMARY_P (to)
1721 = BINFO_INDIRECT_PRIMARY_P (from);
1722 BINFO_INDIRECT_PRIMARY_P (from) = 0;
1723 if (BINFO_PRIMARY_P (from))
1725 tree primary = BINFO_PRIMARY_BASE_OF (from);
1726 tree assoc;
1728 /* We might have just moved the primary base too, see if it's on our
1729 mappings. */
1730 assoc = purpose_member (primary, mappings);
1731 if (assoc)
1732 primary = TREE_VALUE (assoc);
1733 BINFO_PRIMARY_BASE_OF (to) = primary;
1734 BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1736 my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)), 20010104);
1737 mappings = tree_cons (from, to, mappings);
1738 for (i = 0; i != n_baseclasses; i++)
1740 tree from_binfo = BINFO_BASETYPE (from, i);
1741 tree to_binfo = BINFO_BASETYPE (to, i);
1743 if (TREE_VIA_VIRTUAL (from_binfo))
1745 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (from_binfo), type);
1747 if (shared_binfo == from_binfo)
1748 force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1750 else
1751 force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1755 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1756 make TO the canonical binfo, within the heirarchy dominated by TYPE.
1757 MAPPINGS is an assoc list of binfos that have already been reseated.
1758 Adjust any non-virtual bases within FROM, and also move any virtual bases
1759 which are canonical. This complication arises because selecting primary
1760 bases walks in inheritance graph order, but we don't share binfos for
1761 virtual bases, hence we can fill in the primaries for a virtual base,
1762 and then discover that a later base requires the virtual as its
1763 primary. */
1765 static void
1766 force_canonical_binfo (to, from, type, mappings)
1767 tree to;
1768 tree from;
1769 tree type;
1770 tree mappings;
1772 tree assoc = purpose_member (BINFO_TYPE (to),
1773 CLASSTYPE_VBASECLASSES (type));
1774 TREE_VALUE (assoc) = to;
1775 force_canonical_binfo_r (to, from, type, mappings);
1778 /* Make BASE_BINFO the primary virtual base of BINFO within the hierarchy
1779 dominated by TYPE. Returns BASE_BINFO, if it can be made so, NULL
1780 otherwise (because something else has already made it primary). */
1782 static tree
1783 mark_primary_virtual_base (binfo, base_binfo, type)
1784 tree binfo;
1785 tree base_binfo;
1786 tree type;
1788 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1789 tree delta;
1791 if (BINFO_PRIMARY_P (shared_binfo))
1793 /* It's already allocated in the hierarchy. BINFO won't have a
1794 primary base in this hierachy, even though the complete object
1795 BINFO is for, would do. */
1796 BINFO_LOST_PRIMARY_P (binfo) = 1;
1798 return NULL_TREE;
1801 /* We need to make sure that the assoc list
1802 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1803 primary BINFO for the virtual base, as this is the one
1804 that'll really exist. */
1805 if (base_binfo != shared_binfo)
1806 force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1808 delta = size_diffop (BINFO_OFFSET (binfo), BINFO_OFFSET (base_binfo));
1809 if (!integer_zerop (delta))
1811 propagate_binfo_offsets (base_binfo, delta);
1812 BINFO_OFFSET (base_binfo) = BINFO_OFFSET (binfo);
1814 return base_binfo;
1817 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1818 dominated by TYPE that are primary bases. */
1820 static void
1821 mark_primary_bases (type)
1822 tree type;
1824 tree binfo;
1826 /* Walk the bases in inheritance graph order. */
1827 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1829 tree base_binfo;
1831 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1832 /* Not a dynamic base. */
1833 continue;
1835 base_binfo = get_primary_binfo (binfo);
1837 if (TREE_VIA_VIRTUAL (base_binfo))
1838 base_binfo = mark_primary_virtual_base (binfo, base_binfo, type);
1840 if (base_binfo)
1841 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1845 /* Make the BINFO the primary base of T. */
1847 static void
1848 set_primary_base (t, binfo, vfuns_p)
1849 tree t;
1850 tree binfo;
1851 int *vfuns_p;
1853 tree basetype;
1855 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1856 basetype = BINFO_TYPE (binfo);
1857 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1858 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1859 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1860 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1861 *vfuns_p = CLASSTYPE_VSIZE (basetype);
1864 /* Determine the primary class for T. */
1866 static void
1867 determine_primary_base (t, vfuns_p)
1868 tree t;
1869 int *vfuns_p;
1871 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1872 tree vbases;
1873 tree type_binfo;
1875 /* If there are no baseclasses, there is certainly no primary base. */
1876 if (n_baseclasses == 0)
1877 return;
1879 type_binfo = TYPE_BINFO (t);
1881 for (i = 0; i < n_baseclasses; i++)
1883 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1884 tree basetype = BINFO_TYPE (base_binfo);
1886 if (TYPE_CONTAINS_VPTR_P (basetype))
1888 /* Even a virtual baseclass can contain our RTTI
1889 information. But, we prefer a non-virtual polymorphic
1890 baseclass. */
1891 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1892 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1894 /* A virtual baseclass can't be the primary base under the
1895 old ABI. And under the new ABI we still prefer a
1896 non-virtual base. */
1897 if (TREE_VIA_VIRTUAL (base_binfo))
1898 continue;
1900 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1902 set_primary_base (t, base_binfo, vfuns_p);
1903 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1905 else
1907 tree vfields;
1909 /* Only add unique vfields, and flatten them out as we go. */
1910 for (vfields = CLASSTYPE_VFIELDS (basetype);
1911 vfields;
1912 vfields = TREE_CHAIN (vfields))
1913 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1914 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1915 CLASSTYPE_VFIELDS (t)
1916 = tree_cons (base_binfo,
1917 VF_BASETYPE_VALUE (vfields),
1918 CLASSTYPE_VFIELDS (t));
1923 if (!TYPE_VFIELD (t))
1924 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1926 /* Find the indirect primary bases - those virtual bases which are primary
1927 bases of something else in this hierarchy. */
1928 for (vbases = CLASSTYPE_VBASECLASSES (t);
1929 vbases;
1930 vbases = TREE_CHAIN (vbases))
1932 tree vbase_binfo = TREE_VALUE (vbases);
1934 /* See if this virtual base is an indirect primary base. To be so,
1935 it must be a primary base within the hierarchy of one of our
1936 direct bases. */
1937 for (i = 0; i < n_baseclasses; ++i)
1939 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1940 tree v;
1942 for (v = CLASSTYPE_VBASECLASSES (basetype);
1944 v = TREE_CHAIN (v))
1946 tree base_vbase = TREE_VALUE (v);
1948 if (BINFO_PRIMARY_P (base_vbase)
1949 && same_type_p (BINFO_TYPE (base_vbase),
1950 BINFO_TYPE (vbase_binfo)))
1952 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1953 break;
1957 /* If we've discovered that this virtual base is an indirect
1958 primary base, then we can move on to the next virtual
1959 base. */
1960 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1961 break;
1965 /* The new ABI allows for the use of a "nearly-empty" virtual base
1966 class as the primary base class if no non-virtual polymorphic
1967 base can be found. */
1968 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1970 /* If not NULL, this is the best primary base candidate we have
1971 found so far. */
1972 tree candidate = NULL_TREE;
1973 tree base_binfo;
1975 /* Loop over the baseclasses. */
1976 for (base_binfo = TYPE_BINFO (t);
1977 base_binfo;
1978 base_binfo = TREE_CHAIN (base_binfo))
1980 tree basetype = BINFO_TYPE (base_binfo);
1982 if (TREE_VIA_VIRTUAL (base_binfo)
1983 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1985 /* If this is not an indirect primary base, then it's
1986 definitely our primary base. */
1987 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1989 candidate = base_binfo;
1990 break;
1993 /* If this is an indirect primary base, it still could be
1994 our primary base -- unless we later find there's another
1995 nearly-empty virtual base that isn't an indirect
1996 primary base. */
1997 if (!candidate)
1998 candidate = base_binfo;
2002 /* If we've got a primary base, use it. */
2003 if (candidate)
2005 set_primary_base (t, candidate, vfuns_p);
2006 CLASSTYPE_VFIELDS (t)
2007 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
2011 /* Mark the primary base classes at this point. */
2012 mark_primary_bases (t);
2015 /* Set memoizing fields and bits of T (and its variants) for later
2016 use. */
2018 static void
2019 finish_struct_bits (t)
2020 tree t;
2022 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2024 /* Fix up variants (if any). */
2025 tree variants = TYPE_NEXT_VARIANT (t);
2026 while (variants)
2028 /* These fields are in the _TYPE part of the node, not in
2029 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2030 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
2031 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
2032 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2033 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2034 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2036 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
2037 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
2038 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2039 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2040 /* Copy whatever these are holding today. */
2041 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2042 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
2043 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2044 TYPE_SIZE (variants) = TYPE_SIZE (t);
2045 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
2046 variants = TYPE_NEXT_VARIANT (variants);
2049 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
2050 /* For a class w/o baseclasses, `finish_struct' has set
2051 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2052 definition). Similarly for a class whose base classes do not
2053 have vtables. When neither of these is true, we might have
2054 removed abstract virtuals (by providing a definition), added
2055 some (by declaring new ones), or redeclared ones from a base
2056 class. We need to recalculate what's really an abstract virtual
2057 at this point (by looking in the vtables). */
2058 get_pure_virtuals (t);
2060 if (n_baseclasses)
2062 /* Notice whether this class has type conversion functions defined. */
2063 tree binfo = TYPE_BINFO (t);
2064 tree binfos = BINFO_BASETYPES (binfo);
2065 tree basetype;
2067 for (i = n_baseclasses-1; i >= 0; i--)
2069 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2071 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
2075 /* If this type has a copy constructor, force its mode to be BLKmode, and
2076 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2077 be passed by invisible reference and prevent it from being returned in
2078 a register.
2080 Also do this if the class has BLKmode but can still be returned in
2081 registers, since function_cannot_inline_p won't let us inline
2082 functions returning such a type. This affects the HP-PA. */
2083 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
2084 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
2085 && CLASSTYPE_NON_AGGREGATE (t)))
2087 tree variants;
2088 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2089 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2091 TYPE_MODE (variants) = BLKmode;
2092 TREE_ADDRESSABLE (variants) = 1;
2097 /* Issue warnings about T having private constructors, but no friends,
2098 and so forth.
2100 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2101 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2102 non-private static member functions. */
2104 static void
2105 maybe_warn_about_overly_private_class (t)
2106 tree t;
2108 int has_member_fn = 0;
2109 int has_nonprivate_method = 0;
2110 tree fn;
2112 if (!warn_ctor_dtor_privacy
2113 /* If the class has friends, those entities might create and
2114 access instances, so we should not warn. */
2115 || (CLASSTYPE_FRIEND_CLASSES (t)
2116 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2117 /* We will have warned when the template was declared; there's
2118 no need to warn on every instantiation. */
2119 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2120 /* There's no reason to even consider warning about this
2121 class. */
2122 return;
2124 /* We only issue one warning, if more than one applies, because
2125 otherwise, on code like:
2127 class A {
2128 // Oops - forgot `public:'
2129 A();
2130 A(const A&);
2131 ~A();
2134 we warn several times about essentially the same problem. */
2136 /* Check to see if all (non-constructor, non-destructor) member
2137 functions are private. (Since there are no friends or
2138 non-private statics, we can't ever call any of the private member
2139 functions.) */
2140 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2141 /* We're not interested in compiler-generated methods; they don't
2142 provide any way to call private members. */
2143 if (!DECL_ARTIFICIAL (fn))
2145 if (!TREE_PRIVATE (fn))
2147 if (DECL_STATIC_FUNCTION_P (fn))
2148 /* A non-private static member function is just like a
2149 friend; it can create and invoke private member
2150 functions, and be accessed without a class
2151 instance. */
2152 return;
2154 has_nonprivate_method = 1;
2155 break;
2157 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2158 has_member_fn = 1;
2161 if (!has_nonprivate_method && has_member_fn)
2163 /* There are no non-private methods, and there's at least one
2164 private member function that isn't a constructor or
2165 destructor. (If all the private members are
2166 constructors/destructors we want to use the code below that
2167 issues error messages specifically referring to
2168 constructors/destructors.) */
2169 int i;
2170 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2171 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2172 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2173 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2175 has_nonprivate_method = 1;
2176 break;
2178 if (!has_nonprivate_method)
2180 cp_warning ("all member functions in class `%T' are private", t);
2181 return;
2185 /* Even if some of the member functions are non-private, the class
2186 won't be useful for much if all the constructors or destructors
2187 are private: such an object can never be created or destroyed. */
2188 if (TYPE_HAS_DESTRUCTOR (t))
2190 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2192 if (TREE_PRIVATE (dtor))
2194 cp_warning ("`%#T' only defines a private destructor and has no friends",
2196 return;
2200 if (TYPE_HAS_CONSTRUCTOR (t))
2202 int nonprivate_ctor = 0;
2204 /* If a non-template class does not define a copy
2205 constructor, one is defined for it, enabling it to avoid
2206 this warning. For a template class, this does not
2207 happen, and so we would normally get a warning on:
2209 template <class T> class C { private: C(); };
2211 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2212 complete non-template or fully instantiated classes have this
2213 flag set. */
2214 if (!TYPE_HAS_INIT_REF (t))
2215 nonprivate_ctor = 1;
2216 else
2217 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2219 fn = OVL_NEXT (fn))
2221 tree ctor = OVL_CURRENT (fn);
2222 /* Ideally, we wouldn't count copy constructors (or, in
2223 fact, any constructor that takes an argument of the
2224 class type as a parameter) because such things cannot
2225 be used to construct an instance of the class unless
2226 you already have one. But, for now at least, we're
2227 more generous. */
2228 if (! TREE_PRIVATE (ctor))
2230 nonprivate_ctor = 1;
2231 break;
2235 if (nonprivate_ctor == 0)
2237 cp_warning ("`%#T' only defines private constructors and has no friends",
2239 return;
2244 /* Function to help qsort sort FIELD_DECLs by name order. */
2246 static int
2247 field_decl_cmp (x, y)
2248 const tree *x, *y;
2250 if (DECL_NAME (*x) == DECL_NAME (*y))
2251 /* A nontype is "greater" than a type. */
2252 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2253 if (DECL_NAME (*x) == NULL_TREE)
2254 return -1;
2255 if (DECL_NAME (*y) == NULL_TREE)
2256 return 1;
2257 if (DECL_NAME (*x) < DECL_NAME (*y))
2258 return -1;
2259 return 1;
2262 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2264 static int
2265 method_name_cmp (m1, m2)
2266 const tree *m1, *m2;
2268 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2269 return 0;
2270 if (*m1 == NULL_TREE)
2271 return -1;
2272 if (*m2 == NULL_TREE)
2273 return 1;
2274 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2275 return -1;
2276 return 1;
2279 /* Warn about duplicate methods in fn_fields. Also compact method
2280 lists so that lookup can be made faster.
2282 Data Structure: List of method lists. The outer list is a
2283 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2284 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2285 links the entire list of methods for TYPE_METHODS. Friends are
2286 chained in the same way as member functions (? TREE_CHAIN or
2287 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2288 list. That allows them to be quickly deleted, and requires no
2289 extra storage.
2291 Sort methods that are not special (i.e., constructors, destructors,
2292 and type conversion operators) so that we can find them faster in
2293 search. */
2295 static void
2296 finish_struct_methods (t)
2297 tree t;
2299 tree fn_fields;
2300 tree method_vec;
2301 int slot, len;
2303 if (!TYPE_METHODS (t))
2305 /* Clear these for safety; perhaps some parsing error could set
2306 these incorrectly. */
2307 TYPE_HAS_CONSTRUCTOR (t) = 0;
2308 TYPE_HAS_DESTRUCTOR (t) = 0;
2309 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2310 return;
2313 method_vec = CLASSTYPE_METHOD_VEC (t);
2314 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2315 len = TREE_VEC_LENGTH (method_vec);
2317 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2318 and the next few with type conversion operators (if any). */
2319 for (fn_fields = TYPE_METHODS (t); fn_fields;
2320 fn_fields = TREE_CHAIN (fn_fields))
2321 /* Clear out this flag. */
2322 DECL_IN_AGGR_P (fn_fields) = 0;
2324 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2325 /* We thought there was a destructor, but there wasn't. Some
2326 parse errors cause this anomalous situation. */
2327 TYPE_HAS_DESTRUCTOR (t) = 0;
2329 /* Issue warnings about private constructors and such. If there are
2330 no methods, then some public defaults are generated. */
2331 maybe_warn_about_overly_private_class (t);
2333 /* Now sort the methods. */
2334 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2335 len--;
2336 TREE_VEC_LENGTH (method_vec) = len;
2338 /* The type conversion ops have to live at the front of the vec, so we
2339 can't sort them. */
2340 for (slot = 2; slot < len; ++slot)
2342 tree fn = TREE_VEC_ELT (method_vec, slot);
2344 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2345 break;
2347 if (len - slot > 1)
2348 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2349 (int (*)(const void *, const void *))method_name_cmp);
2352 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2354 void
2355 duplicate_tag_error (t)
2356 tree t;
2358 cp_error ("redefinition of `%#T'", t);
2359 cp_error_at ("previous definition of `%#T'", t);
2361 /* Pretend we haven't defined this type. */
2363 /* All of the component_decl's were TREE_CHAINed together in the parser.
2364 finish_struct_methods walks these chains and assembles all methods with
2365 the same base name into DECL_CHAINs. Now we don't need the parser chains
2366 anymore, so we unravel them. */
2368 /* This used to be in finish_struct, but it turns out that the
2369 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2370 things... */
2371 if (CLASSTYPE_METHOD_VEC (t))
2373 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2374 int i, len = TREE_VEC_LENGTH (method_vec);
2375 for (i = 0; i < len; i++)
2377 tree unchain = TREE_VEC_ELT (method_vec, i);
2378 while (unchain != NULL_TREE)
2380 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2381 unchain = OVL_NEXT (unchain);
2386 if (TYPE_LANG_SPECIFIC (t))
2388 tree binfo = TYPE_BINFO (t);
2389 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2390 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2391 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2392 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2394 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2395 BINFO_BASETYPES(binfo) = NULL_TREE;
2397 TYPE_BINFO (t) = binfo;
2398 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2399 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2400 TYPE_REDEFINED (t) = 1;
2401 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2402 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2404 TYPE_SIZE (t) = NULL_TREE;
2405 TYPE_MODE (t) = VOIDmode;
2406 TYPE_FIELDS (t) = NULL_TREE;
2407 TYPE_METHODS (t) = NULL_TREE;
2408 TYPE_VFIELD (t) = NULL_TREE;
2409 TYPE_CONTEXT (t) = NULL_TREE;
2410 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2412 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2413 TYPE_LANG_FLAG_0 (t) = 0;
2414 TYPE_LANG_FLAG_1 (t) = 0;
2415 TYPE_LANG_FLAG_2 (t) = 0;
2416 TYPE_LANG_FLAG_3 (t) = 0;
2417 TYPE_LANG_FLAG_4 (t) = 0;
2418 TYPE_LANG_FLAG_5 (t) = 0;
2419 TYPE_LANG_FLAG_6 (t) = 0;
2420 /* But not this one. */
2421 SET_IS_AGGR_TYPE (t, 1);
2424 /* Make the BINFO's vtablehave N entries, including RTTI entries,
2425 vbase and vcall offsets, etc. Set its type and call the backend
2426 to lay it out. */
2428 static void
2429 layout_vtable_decl (binfo, n)
2430 tree binfo;
2431 int n;
2433 tree itype;
2434 tree atype;
2435 tree vtable;
2437 itype = size_int (n);
2438 atype = build_cplus_array_type (vtable_entry_type,
2439 build_index_type (itype));
2440 layout_type (atype);
2442 /* We may have to grow the vtable. */
2443 vtable = get_vtbl_decl_for_binfo (binfo);
2444 if (!same_type_p (TREE_TYPE (vtable), atype))
2446 TREE_TYPE (vtable) = atype;
2447 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2448 layout_decl (vtable, 0);
2450 /* At one time the vtable info was grabbed 2 words at a time. This
2451 fails on Sparc unless you have 8-byte alignment. */
2452 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2453 DECL_ALIGN (vtable));
2457 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2458 have the same signature. */
2460 static int
2461 same_signature_p (fndecl, base_fndecl)
2462 tree fndecl, base_fndecl;
2464 /* One destructor overrides another if they are the same kind of
2465 destructor. */
2466 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2467 && special_function_p (base_fndecl) == special_function_p (fndecl))
2468 return 1;
2469 /* But a non-destructor never overrides a destructor, nor vice
2470 versa, nor do different kinds of destructors override
2471 one-another. For example, a complete object destructor does not
2472 override a deleting destructor. */
2473 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2474 return 0;
2476 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2478 tree types, base_types;
2479 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2480 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2481 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2482 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2483 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2484 return 1;
2486 return 0;
2489 typedef struct find_final_overrider_data_s {
2490 /* The function for which we are trying to find a final overrider. */
2491 tree fn;
2492 /* The base class in which the function was declared. */
2493 tree declaring_base;
2494 /* The most derived class in the hierarchy. */
2495 tree most_derived_type;
2496 /* The final overriding function. */
2497 tree overriding_fn;
2498 /* The BINFO for the class in which the final overriding function
2499 appears. */
2500 tree overriding_base;
2501 } find_final_overrider_data;
2503 /* Called from find_final_overrider via dfs_walk. */
2505 static tree
2506 dfs_find_final_overrider (binfo, data)
2507 tree binfo;
2508 void *data;
2510 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2512 if (same_type_p (BINFO_TYPE (binfo),
2513 BINFO_TYPE (ffod->declaring_base))
2514 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2515 BINFO_OFFSET (ffod->declaring_base)))
2517 tree path;
2518 tree method;
2520 /* We haven't found an overrider yet. */
2521 method = NULL_TREE;
2522 /* We've found a path to the declaring base. Walk down the path
2523 looking for an overrider for FN. */
2524 for (path = reverse_path (binfo);
2525 path;
2526 path = TREE_CHAIN (path))
2528 for (method = TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path)));
2529 method;
2530 method = TREE_CHAIN (method))
2531 if (DECL_VIRTUAL_P (method)
2532 && same_signature_p (method, ffod->fn))
2533 break;
2535 if (method)
2536 break;
2539 /* If we found an overrider, record the overriding function, and
2540 the base from which it came. */
2541 if (path)
2543 tree base;
2545 /* Assume the path is non-virtual. See if there are any
2546 virtual bases from (but not including) the overrider up
2547 to and including the base where the function is
2548 defined. */
2549 for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
2550 if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
2552 base = ffod->declaring_base;
2553 break;
2556 if (ffod->overriding_fn && ffod->overriding_fn != method)
2558 /* We've found a different overrider along a different
2559 path. That can be OK if the new one overrides the
2560 old one. Consider:
2562 struct S { virtual void f(); };
2563 struct T : public virtual S { virtual void f(); };
2564 struct U : public virtual S, public virtual T {};
2566 Here `T::f' is the final overrider for `S::f'. */
2567 if (strictly_overrides (method, ffod->overriding_fn))
2569 ffod->overriding_fn = method;
2570 ffod->overriding_base = TREE_VALUE (path);
2572 else if (!strictly_overrides (ffod->overriding_fn, method))
2574 cp_error ("no unique final overrider for `%D' in `%T'",
2575 ffod->most_derived_type,
2576 ffod->fn);
2577 cp_error ("candidates are: `%#D'", ffod->overriding_fn);
2578 cp_error (" `%#D'", method);
2579 return error_mark_node;
2582 else if (ffod->overriding_base
2583 && (!tree_int_cst_equal
2584 (BINFO_OFFSET (TREE_VALUE (path)),
2585 BINFO_OFFSET (ffod->overriding_base))))
2587 /* We've found two instances of the same base that
2588 provide overriders. */
2589 cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'",
2590 ffod->fn,
2591 BINFO_TYPE (ffod->overriding_base),
2592 ffod->most_derived_type);
2593 return error_mark_node;
2595 else
2597 ffod->overriding_fn = method;
2598 ffod->overriding_base = TREE_VALUE (path);
2603 return NULL_TREE;
2606 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2607 FN and whose TREE_VALUE is the binfo for the base where the
2608 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2609 base object in which FN is declared. */
2611 static tree
2612 find_final_overrider (t, binfo, fn)
2613 tree t;
2614 tree binfo;
2615 tree fn;
2617 find_final_overrider_data ffod;
2619 /* Getting this right is a little tricky. This is legal:
2621 struct S { virtual void f (); };
2622 struct T { virtual void f (); };
2623 struct U : public S, public T { };
2625 even though calling `f' in `U' is ambiguous. But,
2627 struct R { virtual void f(); };
2628 struct S : virtual public R { virtual void f (); };
2629 struct T : virtual public R { virtual void f (); };
2630 struct U : public S, public T { };
2632 is not -- there's no way to decide whether to put `S::f' or
2633 `T::f' in the vtable for `R'.
2635 The solution is to look at all paths to BINFO. If we find
2636 different overriders along any two, then there is a problem. */
2637 ffod.fn = fn;
2638 ffod.declaring_base = binfo;
2639 ffod.most_derived_type = t;
2640 ffod.overriding_fn = NULL_TREE;
2641 ffod.overriding_base = NULL_TREE;
2643 if (dfs_walk (TYPE_BINFO (t),
2644 dfs_find_final_overrider,
2645 NULL,
2646 &ffod))
2647 return error_mark_node;
2649 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2652 /* Update a entry in the vtable for BINFO, which is in the hierarchy
2653 dominated by T. FN has been overridden in BINFO; VIRTUALS points
2654 to the corresponding position in the BINFO_VIRTUALS list. */
2656 static void
2657 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2658 tree t;
2659 tree binfo;
2660 tree fn;
2661 tree *virtuals;
2663 tree b;
2664 tree overrider;
2665 tree delta;
2666 tree virtual_base;
2667 int generate_thunk_with_vtable_p;
2669 /* Find the function which originally caused this vtable
2670 entry to be present. */
2671 b = binfo;
2672 while (1)
2674 tree primary_base;
2675 tree f;
2677 primary_base = get_primary_binfo (b);
2678 if (!primary_base)
2679 break;
2681 for (f = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (primary_base)));
2683 f = TREE_CHAIN (f))
2684 if (same_signature_p (BV_FN (f), fn))
2685 break;
2687 if (!f)
2688 break;
2690 fn = BV_FN (f);
2691 b = primary_base;
2694 /* Find the final overrider. */
2695 overrider = find_final_overrider (t, b, fn);
2696 if (overrider == error_mark_node)
2697 return;
2699 /* Compute the constant adjustment to the `this' pointer. The
2700 `this' pointer, when this function is called, will point at the
2701 class whose vtable this is. */
2702 delta = size_binop (PLUS_EXPR,
2703 get_derived_offset (binfo,
2704 DECL_VIRTUAL_CONTEXT (fn)),
2705 BINFO_OFFSET (binfo));
2707 /* Assume that we will produce a thunk that convert all the way to
2708 the final overrider, and not to an intermediate virtual base. */
2709 virtual_base = NULL_TREE;
2711 /* Assume that we will always generate thunks with the vtables that
2712 reference them. */
2713 generate_thunk_with_vtable_p = 1;
2715 /* Under the new ABI, we will convert to an intermediate virtual
2716 base first, and then use the vcall offset located there to finish
2717 the conversion. */
2718 while (b)
2720 /* If we find BINFO, then the final overrider is in a class
2721 derived from BINFO, so the thunks can be generated with
2722 the final overrider. */
2723 if (!virtual_base
2724 && same_type_p (BINFO_TYPE (b), BINFO_TYPE (binfo)))
2725 generate_thunk_with_vtable_p = 0;
2727 /* If we find the final overrider, then we can stop
2728 walking. */
2729 if (same_type_p (BINFO_TYPE (b),
2730 BINFO_TYPE (TREE_VALUE (overrider))))
2731 break;
2733 /* If we find a virtual base, and we haven't yet found the
2734 overrider, then there is a virtual base between the
2735 declaring base and the final overrider. */
2736 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2738 generate_thunk_with_vtable_p = 1;
2739 virtual_base = b;
2742 b = BINFO_INHERITANCE_CHAIN (b);
2745 if (virtual_base)
2746 /* The `this' pointer needs to be adjusted to the nearest virtual
2747 base. */
2748 delta = size_diffop (BINFO_OFFSET (virtual_base), delta);
2749 else
2750 /* The `this' pointer needs to be adjusted from pointing to
2751 BINFO to pointing at the base where the final overrider
2752 appears. */
2753 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)), delta);
2755 modify_vtable_entry (t,
2756 binfo,
2757 TREE_PURPOSE (overrider),
2758 delta,
2759 virtuals);
2761 if (virtual_base)
2762 BV_USE_VCALL_INDEX_P (*virtuals) = 1;
2763 if (generate_thunk_with_vtable_p)
2764 BV_GENERATE_THUNK_WITH_VTABLE_P (*virtuals) = 1;
2767 /* Called from modify_all_vtables via dfs_walk. */
2769 static tree
2770 dfs_modify_vtables (binfo, data)
2771 tree binfo;
2772 void *data;
2774 if (/* There's no need to modify the vtable for a non-virtual
2775 primary base; we're not going to use that vtable anyhow
2776 (virtual primary bases can become non-primary in a
2777 class derivation of this one.) */
2778 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2779 /* Similarly, a base without a vtable needs no modification. */
2780 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2782 tree t;
2783 tree virtuals;
2784 tree old_virtuals;
2786 t = (tree) data;
2788 /* If we're supporting RTTI then we always need a new vtable to
2789 point to the RTTI information. Under the new ABI we may need
2790 a new vtable to contain vcall and vbase offsets. */
2791 make_new_vtable (t, binfo);
2793 /* Now, go through each of the virtual functions in the virtual
2794 function table for BINFO. Find the final overrider, and
2795 update the BINFO_VIRTUALS list appropriately. */
2796 for (virtuals = BINFO_VIRTUALS (binfo),
2797 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2798 virtuals;
2799 virtuals = TREE_CHAIN (virtuals),
2800 old_virtuals = TREE_CHAIN (old_virtuals))
2801 update_vtable_entry_for_fn (t,
2802 binfo,
2803 BV_FN (old_virtuals),
2804 &virtuals);
2807 SET_BINFO_MARKED (binfo);
2809 return NULL_TREE;
2812 /* Update all of the primary and secondary vtables for T. Create new
2813 vtables as required, and initialize their RTTI information. Each
2814 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2815 function from a base class; find and modify the appropriate entries
2816 to point to the overriding functions. Returns a list, in
2817 declaration order, of the functions that are overridden in this
2818 class, but do not appear in the primary base class vtable, and
2819 which should therefore be appended to the end of the vtable for T. */
2821 static tree
2822 modify_all_vtables (t, vfuns_p, overridden_virtuals)
2823 tree t;
2824 int *vfuns_p;
2825 tree overridden_virtuals;
2827 tree binfo;
2829 binfo = TYPE_BINFO (t);
2831 /* Update all of the vtables. */
2832 dfs_walk (binfo,
2833 dfs_modify_vtables,
2834 dfs_unmarked_real_bases_queue_p,
2836 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2838 /* If we should include overriding functions for secondary vtables
2839 in our primary vtable, add them now. */
2840 if (all_overridden_vfuns_in_vtables_p ())
2842 tree *fnsp = &overridden_virtuals;
2844 while (*fnsp)
2846 tree fn = TREE_VALUE (*fnsp);
2848 if (!BINFO_VIRTUALS (binfo)
2849 || !value_member (fn, BINFO_VIRTUALS (binfo)))
2851 /* Set the vtable index. */
2852 set_vindex (fn, vfuns_p);
2853 /* We don't need to convert to a base class when calling
2854 this function. */
2855 DECL_VIRTUAL_CONTEXT (fn) = t;
2857 /* We don't need to adjust the `this' pointer when
2858 calling this function. */
2859 BV_DELTA (*fnsp) = integer_zero_node;
2860 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2862 /* This is an overridden function not already in our
2863 vtable. Keep it. */
2864 fnsp = &TREE_CHAIN (*fnsp);
2866 else
2867 /* We've already got an entry for this function. Skip
2868 it. */
2869 *fnsp = TREE_CHAIN (*fnsp);
2872 else
2873 overridden_virtuals = NULL_TREE;
2875 return overridden_virtuals;
2878 /* Here, we already know that they match in every respect.
2879 All we have to check is where they had their declarations. */
2881 static int
2882 strictly_overrides (fndecl1, fndecl2)
2883 tree fndecl1, fndecl2;
2885 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
2886 DECL_CONTEXT (fndecl1),
2887 0, (tree *)0);
2888 if (distance == -2 || distance > 0)
2889 return 1;
2890 return 0;
2893 /* Get the base virtual function declarations in T that are either
2894 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2895 the overrider/hider. */
2897 static tree
2898 get_basefndecls (fndecl, t)
2899 tree fndecl, t;
2901 tree methods = TYPE_METHODS (t);
2902 tree base_fndecls = NULL_TREE;
2903 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2904 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2906 while (methods)
2908 if (TREE_CODE (methods) == FUNCTION_DECL
2909 && DECL_VINDEX (methods) != NULL_TREE
2910 && DECL_NAME (fndecl) == DECL_NAME (methods))
2911 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
2913 methods = TREE_CHAIN (methods);
2916 if (base_fndecls)
2917 return base_fndecls;
2919 for (i = 0; i < n_baseclasses; i++)
2921 tree base_binfo = TREE_VEC_ELT (binfos, i);
2922 tree basetype = BINFO_TYPE (base_binfo);
2924 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2925 base_fndecls);
2928 return base_fndecls;
2931 /* Mark the functions that have been hidden with their overriders.
2932 Since we start out with all functions already marked with a hider,
2933 no need to mark functions that are just hidden.
2935 Subroutine of warn_hidden. */
2937 static void
2938 mark_overriders (fndecl, base_fndecls)
2939 tree fndecl, base_fndecls;
2941 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2942 if (same_signature_p (fndecl, TREE_VALUE (base_fndecls)))
2943 TREE_PURPOSE (base_fndecls) = fndecl;
2946 /* If this declaration supersedes the declaration of
2947 a method declared virtual in the base class, then
2948 mark this field as being virtual as well. */
2950 static void
2951 check_for_override (decl, ctype)
2952 tree decl, ctype;
2954 if (TREE_CODE (decl) == TEMPLATE_DECL)
2955 /* In [temp.mem] we have:
2957 A specialization of a member function template does not
2958 override a virtual function from a base class. */
2959 return;
2960 if ((DECL_DESTRUCTOR_P (decl)
2961 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2962 && look_for_overrides (ctype, decl)
2963 && !DECL_STATIC_FUNCTION_P (decl))
2965 /* Set DECL_VINDEX to a value that is neither an
2966 INTEGER_CST nor the error_mark_node so that
2967 add_virtual_function will realize this is an
2968 overriding function. */
2969 DECL_VINDEX (decl) = decl;
2971 if (DECL_VIRTUAL_P (decl))
2973 if (DECL_VINDEX (decl) == NULL_TREE)
2974 DECL_VINDEX (decl) = error_mark_node;
2975 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2979 /* Warn about hidden virtual functions that are not overridden in t.
2980 We know that constructors and destructors don't apply. */
2982 void
2983 warn_hidden (t)
2984 tree t;
2986 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2987 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2988 int i;
2990 /* We go through each separately named virtual function. */
2991 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2993 tree fns = TREE_VEC_ELT (method_vec, i);
2994 tree fndecl = NULL_TREE;
2996 tree base_fndecls = NULL_TREE;
2997 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2998 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3000 /* First see if we have any virtual functions in this batch. */
3001 for (; fns; fns = OVL_NEXT (fns))
3003 fndecl = OVL_CURRENT (fns);
3004 if (DECL_VINDEX (fndecl))
3005 break;
3008 if (fns == NULL_TREE)
3009 continue;
3011 /* First we get a list of all possible functions that might be
3012 hidden from each base class. */
3013 for (i = 0; i < n_baseclasses; i++)
3015 tree base_binfo = TREE_VEC_ELT (binfos, i);
3016 tree basetype = BINFO_TYPE (base_binfo);
3018 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3019 base_fndecls);
3022 fns = OVL_NEXT (fns);
3024 /* ...then mark up all the base functions with overriders, preferring
3025 overriders to hiders. */
3026 if (base_fndecls)
3027 for (; fns; fns = OVL_NEXT (fns))
3029 fndecl = OVL_CURRENT (fns);
3030 if (DECL_VINDEX (fndecl))
3031 mark_overriders (fndecl, base_fndecls);
3034 /* Now give a warning for all base functions without overriders,
3035 as they are hidden. */
3036 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3037 if (!same_signature_p (TREE_PURPOSE (base_fndecls),
3038 TREE_VALUE (base_fndecls)))
3040 /* Here we know it is a hider, and no overrider exists. */
3041 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3042 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3047 /* Check for things that are invalid. There are probably plenty of other
3048 things we should check for also. */
3050 static void
3051 finish_struct_anon (t)
3052 tree t;
3054 tree field;
3056 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3058 if (TREE_STATIC (field))
3059 continue;
3060 if (TREE_CODE (field) != FIELD_DECL)
3061 continue;
3063 if (DECL_NAME (field) == NULL_TREE
3064 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3066 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3067 for (; elt; elt = TREE_CHAIN (elt))
3069 if (DECL_ARTIFICIAL (elt))
3070 continue;
3072 if (DECL_NAME (elt) == constructor_name (t))
3073 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
3074 elt);
3076 if (TREE_CODE (elt) != FIELD_DECL)
3078 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3079 elt);
3080 continue;
3083 if (TREE_PRIVATE (elt))
3084 cp_pedwarn_at ("private member `%#D' in anonymous union",
3085 elt);
3086 else if (TREE_PROTECTED (elt))
3087 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3088 elt);
3090 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3091 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3097 /* Create default constructors, assignment operators, and so forth for
3098 the type indicated by T, if they are needed.
3099 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3100 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
3101 class cannot have a default constructor, copy constructor taking a
3102 const reference argument, or an assignment operator taking a const
3103 reference, respectively. If a virtual destructor is created, its
3104 DECL is returned; otherwise the return value is NULL_TREE. */
3106 static tree
3107 add_implicitly_declared_members (t, cant_have_default_ctor,
3108 cant_have_const_cctor,
3109 cant_have_const_assignment)
3110 tree t;
3111 int cant_have_default_ctor;
3112 int cant_have_const_cctor;
3113 int cant_have_const_assignment;
3115 tree default_fn;
3116 tree implicit_fns = NULL_TREE;
3117 tree virtual_dtor = NULL_TREE;
3118 tree *f;
3120 /* Destructor. */
3121 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3123 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
3124 check_for_override (default_fn, t);
3126 /* If we couldn't make it work, then pretend we didn't need it. */
3127 if (default_fn == void_type_node)
3128 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
3129 else
3131 TREE_CHAIN (default_fn) = implicit_fns;
3132 implicit_fns = default_fn;
3134 if (DECL_VINDEX (default_fn))
3135 virtual_dtor = default_fn;
3138 else
3139 /* Any non-implicit destructor is non-trivial. */
3140 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3142 /* Default constructor. */
3143 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3145 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
3146 TREE_CHAIN (default_fn) = implicit_fns;
3147 implicit_fns = default_fn;
3150 /* Copy constructor. */
3151 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3153 /* ARM 12.18: You get either X(X&) or X(const X&), but
3154 not both. --Chip */
3155 default_fn
3156 = implicitly_declare_fn (sfk_copy_constructor, t,
3157 /*const_p=*/!cant_have_const_cctor);
3158 TREE_CHAIN (default_fn) = implicit_fns;
3159 implicit_fns = default_fn;
3162 /* Assignment operator. */
3163 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3165 default_fn
3166 = implicitly_declare_fn (sfk_assignment_operator, t,
3167 /*const_p=*/!cant_have_const_assignment);
3168 TREE_CHAIN (default_fn) = implicit_fns;
3169 implicit_fns = default_fn;
3172 /* Now, hook all of the new functions on to TYPE_METHODS,
3173 and add them to the CLASSTYPE_METHOD_VEC. */
3174 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3175 add_method (t, *f, /*error_p=*/0);
3176 *f = TYPE_METHODS (t);
3177 TYPE_METHODS (t) = implicit_fns;
3179 return virtual_dtor;
3182 /* Subroutine of finish_struct_1. Recursively count the number of fields
3183 in TYPE, including anonymous union members. */
3185 static int
3186 count_fields (fields)
3187 tree fields;
3189 tree x;
3190 int n_fields = 0;
3191 for (x = fields; x; x = TREE_CHAIN (x))
3193 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3194 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3195 else
3196 n_fields += 1;
3198 return n_fields;
3201 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3202 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3204 static int
3205 add_fields_to_vec (fields, field_vec, idx)
3206 tree fields, field_vec;
3207 int idx;
3209 tree x;
3210 for (x = fields; x; x = TREE_CHAIN (x))
3212 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3213 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3214 else
3215 TREE_VEC_ELT (field_vec, idx++) = x;
3217 return idx;
3220 /* FIELD is a bit-field. We are finishing the processing for its
3221 enclosing type. Issue any appropriate messages and set appropriate
3222 flags. */
3224 static void
3225 check_bitfield_decl (field)
3226 tree field;
3228 tree type = TREE_TYPE (field);
3229 tree w = NULL_TREE;
3231 /* Detect invalid bit-field type. */
3232 if (DECL_INITIAL (field)
3233 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3235 cp_error_at ("bit-field `%#D' with non-integral type", field);
3236 w = error_mark_node;
3239 /* Detect and ignore out of range field width. */
3240 if (DECL_INITIAL (field))
3242 w = DECL_INITIAL (field);
3244 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3245 STRIP_NOPS (w);
3247 /* detect invalid field size. */
3248 if (TREE_CODE (w) == CONST_DECL)
3249 w = DECL_INITIAL (w);
3250 else
3251 w = decl_constant_value (w);
3253 if (TREE_CODE (w) != INTEGER_CST)
3255 cp_error_at ("bit-field `%D' width not an integer constant",
3256 field);
3257 w = error_mark_node;
3259 else if (tree_int_cst_sgn (w) < 0)
3261 cp_error_at ("negative width in bit-field `%D'", field);
3262 w = error_mark_node;
3264 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3266 cp_error_at ("zero width for bit-field `%D'", field);
3267 w = error_mark_node;
3269 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3270 && TREE_CODE (type) != ENUMERAL_TYPE
3271 && TREE_CODE (type) != BOOLEAN_TYPE)
3272 cp_warning_at ("width of `%D' exceeds its type", field);
3273 else if (TREE_CODE (type) == ENUMERAL_TYPE
3274 && (0 > compare_tree_int (w,
3275 min_precision (TYPE_MIN_VALUE (type),
3276 TREE_UNSIGNED (type)))
3277 || 0 > compare_tree_int (w,
3278 min_precision
3279 (TYPE_MAX_VALUE (type),
3280 TREE_UNSIGNED (type)))))
3281 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3282 field, type);
3285 /* Remove the bit-field width indicator so that the rest of the
3286 compiler does not treat that value as an initializer. */
3287 DECL_INITIAL (field) = NULL_TREE;
3289 if (w != error_mark_node)
3291 DECL_SIZE (field) = convert (bitsizetype, w);
3292 DECL_BIT_FIELD (field) = 1;
3294 if (integer_zerop (w))
3296 #ifdef EMPTY_FIELD_BOUNDARY
3297 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3298 EMPTY_FIELD_BOUNDARY);
3299 #endif
3300 #ifdef PCC_BITFIELD_TYPE_MATTERS
3301 if (PCC_BITFIELD_TYPE_MATTERS)
3303 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3304 TYPE_ALIGN (type));
3305 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3307 #endif
3310 else
3312 /* Non-bit-fields are aligned for their type. */
3313 DECL_BIT_FIELD (field) = 0;
3314 CLEAR_DECL_C_BIT_FIELD (field);
3315 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3316 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3320 /* FIELD is a non bit-field. We are finishing the processing for its
3321 enclosing type T. Issue any appropriate messages and set appropriate
3322 flags. */
3324 static void
3325 check_field_decl (field, t, cant_have_const_ctor,
3326 cant_have_default_ctor, no_const_asn_ref,
3327 any_default_members)
3328 tree field;
3329 tree t;
3330 int *cant_have_const_ctor;
3331 int *cant_have_default_ctor;
3332 int *no_const_asn_ref;
3333 int *any_default_members;
3335 tree type = strip_array_types (TREE_TYPE (field));
3337 /* An anonymous union cannot contain any fields which would change
3338 the settings of CANT_HAVE_CONST_CTOR and friends. */
3339 if (ANON_UNION_TYPE_P (type))
3341 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3342 structs. So, we recurse through their fields here. */
3343 else if (ANON_AGGR_TYPE_P (type))
3345 tree fields;
3347 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3348 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3349 check_field_decl (fields, t, cant_have_const_ctor,
3350 cant_have_default_ctor, no_const_asn_ref,
3351 any_default_members);
3353 /* Check members with class type for constructors, destructors,
3354 etc. */
3355 else if (CLASS_TYPE_P (type))
3357 /* Never let anything with uninheritable virtuals
3358 make it through without complaint. */
3359 abstract_virtuals_error (field, type);
3361 if (TREE_CODE (t) == UNION_TYPE)
3363 if (TYPE_NEEDS_CONSTRUCTING (type))
3364 cp_error_at ("member `%#D' with constructor not allowed in union",
3365 field);
3366 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3367 cp_error_at ("member `%#D' with destructor not allowed in union",
3368 field);
3369 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3370 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3371 field);
3373 else
3375 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3376 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3377 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3378 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3379 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3382 if (!TYPE_HAS_CONST_INIT_REF (type))
3383 *cant_have_const_ctor = 1;
3385 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3386 *no_const_asn_ref = 1;
3388 if (TYPE_HAS_CONSTRUCTOR (type)
3389 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3390 *cant_have_default_ctor = 1;
3392 if (DECL_INITIAL (field) != NULL_TREE)
3394 /* `build_class_init_list' does not recognize
3395 non-FIELD_DECLs. */
3396 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3397 cp_error_at ("multiple fields in union `%T' initialized");
3398 *any_default_members = 1;
3401 /* Non-bit-fields are aligned for their type, except packed fields
3402 which require only BITS_PER_UNIT alignment. */
3403 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3404 (DECL_PACKED (field)
3405 ? BITS_PER_UNIT
3406 : TYPE_ALIGN (TREE_TYPE (field))));
3407 if (! DECL_PACKED (field))
3408 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3411 /* Check the data members (both static and non-static), class-scoped
3412 typedefs, etc., appearing in the declaration of T. Issue
3413 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3414 declaration order) of access declarations; each TREE_VALUE in this
3415 list is a USING_DECL.
3417 In addition, set the following flags:
3419 EMPTY_P
3420 The class is empty, i.e., contains no non-static data members.
3422 CANT_HAVE_DEFAULT_CTOR_P
3423 This class cannot have an implicitly generated default
3424 constructor.
3426 CANT_HAVE_CONST_CTOR_P
3427 This class cannot have an implicitly generated copy constructor
3428 taking a const reference.
3430 CANT_HAVE_CONST_ASN_REF
3431 This class cannot have an implicitly generated assignment
3432 operator taking a const reference.
3434 All of these flags should be initialized before calling this
3435 function.
3437 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3438 fields can be added by adding to this chain. */
3440 static void
3441 check_field_decls (t, access_decls, empty_p,
3442 cant_have_default_ctor_p, cant_have_const_ctor_p,
3443 no_const_asn_ref_p)
3444 tree t;
3445 tree *access_decls;
3446 int *empty_p;
3447 int *cant_have_default_ctor_p;
3448 int *cant_have_const_ctor_p;
3449 int *no_const_asn_ref_p;
3451 tree *field;
3452 tree *next;
3453 int has_pointers;
3454 int any_default_members;
3456 /* First, delete any duplicate fields. */
3457 delete_duplicate_fields (TYPE_FIELDS (t));
3459 /* Assume there are no access declarations. */
3460 *access_decls = NULL_TREE;
3461 /* Assume this class has no pointer members. */
3462 has_pointers = 0;
3463 /* Assume none of the members of this class have default
3464 initializations. */
3465 any_default_members = 0;
3467 for (field = &TYPE_FIELDS (t); *field; field = next)
3469 tree x = *field;
3470 tree type = TREE_TYPE (x);
3472 GNU_xref_member (current_class_name, x);
3474 next = &TREE_CHAIN (x);
3476 if (TREE_CODE (x) == FIELD_DECL)
3478 DECL_PACKED (x) |= TYPE_PACKED (t);
3480 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3481 /* We don't treat zero-width bitfields as making a class
3482 non-empty. */
3484 else
3486 /* The class is non-empty. */
3487 *empty_p = 0;
3488 /* The class is not even nearly empty. */
3489 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3493 if (TREE_CODE (x) == USING_DECL)
3495 /* Prune the access declaration from the list of fields. */
3496 *field = TREE_CHAIN (x);
3498 /* Save the access declarations for our caller. */
3499 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3501 /* Since we've reset *FIELD there's no reason to skip to the
3502 next field. */
3503 next = field;
3504 continue;
3507 if (TREE_CODE (x) == TYPE_DECL
3508 || TREE_CODE (x) == TEMPLATE_DECL)
3509 continue;
3511 /* If we've gotten this far, it's a data member, possibly static,
3512 or an enumerator. */
3514 DECL_CONTEXT (x) = t;
3516 /* ``A local class cannot have static data members.'' ARM 9.4 */
3517 if (current_function_decl && TREE_STATIC (x))
3518 cp_error_at ("field `%D' in local class cannot be static", x);
3520 /* Perform error checking that did not get done in
3521 grokdeclarator. */
3522 if (TREE_CODE (type) == FUNCTION_TYPE)
3524 cp_error_at ("field `%D' invalidly declared function type",
3526 type = build_pointer_type (type);
3527 TREE_TYPE (x) = type;
3529 else if (TREE_CODE (type) == METHOD_TYPE)
3531 cp_error_at ("field `%D' invalidly declared method type", x);
3532 type = build_pointer_type (type);
3533 TREE_TYPE (x) = type;
3535 else if (TREE_CODE (type) == OFFSET_TYPE)
3537 cp_error_at ("field `%D' invalidly declared offset type", x);
3538 type = build_pointer_type (type);
3539 TREE_TYPE (x) = type;
3542 if (type == error_mark_node)
3543 continue;
3545 /* When this goes into scope, it will be a non-local reference. */
3546 DECL_NONLOCAL (x) = 1;
3548 if (TREE_CODE (x) == CONST_DECL)
3549 continue;
3551 if (TREE_CODE (x) == VAR_DECL)
3553 if (TREE_CODE (t) == UNION_TYPE)
3554 /* Unions cannot have static members. */
3555 cp_error_at ("field `%D' declared static in union", x);
3557 continue;
3560 /* Now it can only be a FIELD_DECL. */
3562 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3563 CLASSTYPE_NON_AGGREGATE (t) = 1;
3565 /* If this is of reference type, check if it needs an init.
3566 Also do a little ANSI jig if necessary. */
3567 if (TREE_CODE (type) == REFERENCE_TYPE)
3569 CLASSTYPE_NON_POD_P (t) = 1;
3570 if (DECL_INITIAL (x) == NULL_TREE)
3571 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3573 /* ARM $12.6.2: [A member initializer list] (or, for an
3574 aggregate, initialization by a brace-enclosed list) is the
3575 only way to initialize nonstatic const and reference
3576 members. */
3577 *cant_have_default_ctor_p = 1;
3578 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3580 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3581 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3584 type = strip_array_types (type);
3586 if (TREE_CODE (type) == POINTER_TYPE)
3587 has_pointers = 1;
3589 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3590 CLASSTYPE_HAS_MUTABLE (t) = 1;
3592 if (! pod_type_p (type)
3593 /* For some reason, pointers to members are POD types themselves,
3594 but are not allowed in POD structs. Silly. */
3595 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3596 CLASSTYPE_NON_POD_P (t) = 1;
3598 /* If any field is const, the structure type is pseudo-const. */
3599 if (CP_TYPE_CONST_P (type))
3601 C_TYPE_FIELDS_READONLY (t) = 1;
3602 if (DECL_INITIAL (x) == NULL_TREE)
3603 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3605 /* ARM $12.6.2: [A member initializer list] (or, for an
3606 aggregate, initialization by a brace-enclosed list) is the
3607 only way to initialize nonstatic const and reference
3608 members. */
3609 *cant_have_default_ctor_p = 1;
3610 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3612 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3613 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3615 /* A field that is pseudo-const makes the structure likewise. */
3616 else if (IS_AGGR_TYPE (type))
3618 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3619 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3620 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3623 /* Core issue 80: A nonstatic data member is required to have a
3624 different name from the class iff the class has a
3625 user-defined constructor. */
3626 if (DECL_NAME (x) == constructor_name (t)
3627 && TYPE_HAS_CONSTRUCTOR (t))
3628 cp_pedwarn_at ("field `%#D' with same name as class", x);
3630 /* We set DECL_C_BIT_FIELD in grokbitfield.
3631 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3632 if (DECL_C_BIT_FIELD (x))
3633 check_bitfield_decl (x);
3634 else
3635 check_field_decl (x, t,
3636 cant_have_const_ctor_p,
3637 cant_have_default_ctor_p,
3638 no_const_asn_ref_p,
3639 &any_default_members);
3642 /* Effective C++ rule 11. */
3643 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3644 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3646 cp_warning ("`%#T' has pointer data members", t);
3648 if (! TYPE_HAS_INIT_REF (t))
3650 cp_warning (" but does not override `%T(const %T&)'", t, t);
3651 if (! TYPE_HAS_ASSIGN_REF (t))
3652 cp_warning (" or `operator=(const %T&)'", t);
3654 else if (! TYPE_HAS_ASSIGN_REF (t))
3655 cp_warning (" but does not override `operator=(const %T&)'", t);
3659 /* Check anonymous struct/anonymous union fields. */
3660 finish_struct_anon (t);
3662 /* We've built up the list of access declarations in reverse order.
3663 Fix that now. */
3664 *access_decls = nreverse (*access_decls);
3667 /* Return a FIELD_DECL for a pointer-to-virtual-table or
3668 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
3669 field are as indicated. The CLASS_TYPE in which this field occurs
3670 is also indicated. FCONTEXT is the type that is needed for the debug
3671 info output routines. *EMPTY_P is set to a non-zero value by this
3672 function to indicate that a class containing this field is
3673 non-empty. */
3675 static tree
3676 build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
3677 empty_p)
3678 tree name;
3679 tree assembler_name;
3680 tree type;
3681 tree class_type;
3682 tree fcontext;
3683 int *empty_p;
3685 tree field;
3687 /* This class is non-empty. */
3688 *empty_p = 0;
3690 /* Build the FIELD_DECL. */
3691 field = build_decl (FIELD_DECL, name, type);
3692 DECL_ASSEMBLER_NAME (field) = assembler_name;
3693 DECL_VIRTUAL_P (field) = 1;
3694 DECL_ARTIFICIAL (field) = 1;
3695 DECL_FIELD_CONTEXT (field) = class_type;
3696 DECL_FCONTEXT (field) = fcontext;
3697 DECL_ALIGN (field) = TYPE_ALIGN (type);
3698 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (type);
3700 /* Return it. */
3701 return field;
3704 /* If TYPE is an empty class type, records its OFFSET in the table of
3705 OFFSETS. */
3707 static int
3708 record_subobject_offset (type, offset, offsets)
3709 tree type;
3710 tree offset;
3711 splay_tree offsets;
3713 splay_tree_node n;
3715 if (!is_empty_class (type))
3716 return 0;
3718 /* Record the location of this empty object in OFFSETS. */
3719 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3720 if (!n)
3721 n = splay_tree_insert (offsets,
3722 (splay_tree_key) offset,
3723 (splay_tree_value) NULL_TREE);
3724 n->value = ((splay_tree_value)
3725 tree_cons (NULL_TREE,
3726 type,
3727 (tree) n->value));
3729 return 0;
3732 /* Returns non-zero if TYPE is an empty class type and there is
3733 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3735 static int
3736 check_subobject_offset (type, offset, offsets)
3737 tree type;
3738 tree offset;
3739 splay_tree offsets;
3741 splay_tree_node n;
3742 tree t;
3744 if (!is_empty_class (type))
3745 return 0;
3747 /* Record the location of this empty object in OFFSETS. */
3748 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3749 if (!n)
3750 return 0;
3752 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3753 if (same_type_p (TREE_VALUE (t), type))
3754 return 1;
3756 return 0;
3759 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3760 F for every subobject, passing it the type, offset, and table of
3761 OFFSETS. If VBASES_P is non-zero, then even virtual non-primary
3762 bases should be traversed; otherwise, they are ignored. If F
3763 returns a non-zero value, the traversal ceases, and that value is
3764 returned. Otherwise, returns zero. */
3766 static int
3767 walk_subobject_offsets (type, f, offset, offsets, vbases_p)
3768 tree type;
3769 subobject_offset_fn f;
3770 tree offset;
3771 splay_tree offsets;
3772 int vbases_p;
3774 int r = 0;
3776 if (CLASS_TYPE_P (type))
3778 tree field;
3779 int i;
3781 /* Record the location of TYPE. */
3782 r = (*f) (type, offset, offsets);
3783 if (r)
3784 return r;
3786 /* Iterate through the direct base classes of TYPE. */
3787 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3789 tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3791 if (!vbases_p
3792 && TREE_VIA_VIRTUAL (binfo)
3793 && !BINFO_PRIMARY_P (binfo))
3794 continue;
3796 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3798 size_binop (PLUS_EXPR,
3799 offset,
3800 BINFO_OFFSET (binfo)),
3801 offsets,
3802 vbases_p);
3803 if (r)
3804 return r;
3807 /* Iterate through the fields of TYPE. */
3808 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3809 if (TREE_CODE (field) == FIELD_DECL)
3811 r = walk_subobject_offsets (TREE_TYPE (field),
3813 size_binop (PLUS_EXPR,
3814 offset,
3815 DECL_FIELD_OFFSET (field)),
3816 offsets,
3817 /*vbases_p=*/1);
3818 if (r)
3819 return r;
3822 else if (TREE_CODE (type) == ARRAY_TYPE)
3824 tree domain = TYPE_DOMAIN (type);
3825 tree index;
3827 /* Step through each of the elements in the array. */
3828 for (index = size_zero_node;
3829 INT_CST_LT (index, TYPE_MAX_VALUE (domain));
3830 index = size_binop (PLUS_EXPR, index, size_one_node))
3832 r = walk_subobject_offsets (TREE_TYPE (type),
3834 offset,
3835 offsets,
3836 /*vbases_p=*/1);
3837 if (r)
3838 return r;
3839 offset = size_binop (PLUS_EXPR, offset,
3840 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3844 return 0;
3847 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3848 OFFSETS. If VBASES_P is non-zero, virtual bases of TYPE are
3849 examined. */
3851 static void
3852 record_subobject_offsets (type, offset, offsets, vbases_p)
3853 tree type;
3854 tree offset;
3855 splay_tree offsets;
3856 int vbases_p;
3858 walk_subobject_offsets (type, record_subobject_offset, offset,
3859 offsets, vbases_p);
3862 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3863 OFFSET) conflict with entries in OFFSETS. If VBASES_P is non-zero,
3864 virtual bases of TYPE are examined. */
3866 static int
3867 layout_conflict_p (type, offset, offsets, vbases_p)
3868 tree type;
3869 tree offset;
3870 splay_tree offsets;
3871 int vbases_p;
3873 return walk_subobject_offsets (type, check_subobject_offset, offset,
3874 offsets, vbases_p);
3877 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3878 non-static data member of the type indicated by RLI. BINFO is the
3879 binfo corresponding to the base subobject, OFFSETS maps offsets to
3880 types already located at those offsets. This function determines
3881 the position of the DECL. */
3883 static void
3884 layout_nonempty_base_or_field (rli, decl, binfo, offsets)
3885 record_layout_info rli;
3886 tree decl;
3887 tree binfo;
3888 splay_tree offsets;
3890 tree offset = NULL_TREE;
3891 tree type = TREE_TYPE (decl);
3892 /* If we are laying out a base class, rather than a field, then
3893 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
3894 int field_p = !DECL_ARTIFICIAL (decl);
3896 /* Try to place the field. It may take more than one try if we have
3897 a hard time placing the field without putting two objects of the
3898 same type at the same address. */
3899 while (1)
3901 struct record_layout_info_s old_rli = *rli;
3903 /* Place this field. */
3904 place_field (rli, decl);
3905 offset = byte_position (decl);
3907 /* We have to check to see whether or not there is already
3908 something of the same type at the offset we're about to use.
3909 For example:
3911 struct S {};
3912 struct T : public S { int i; };
3913 struct U : public S, public T {};
3915 Here, we put S at offset zero in U. Then, we can't put T at
3916 offset zero -- its S component would be at the same address
3917 as the S we already allocated. So, we have to skip ahead.
3918 Since all data members, including those whose type is an
3919 empty class, have non-zero size, any overlap can happen only
3920 with a direct or indirect base-class -- it can't happen with
3921 a data member. */
3922 if (layout_conflict_p (TREE_TYPE (decl),
3923 offset,
3924 offsets,
3925 field_p))
3927 /* Strip off the size allocated to this field. That puts us
3928 at the first place we could have put the field with
3929 proper alignment. */
3930 *rli = old_rli;
3932 /* Bump up by the alignment required for the type. */
3933 rli->bitpos
3934 = size_binop (PLUS_EXPR, rli->bitpos,
3935 bitsize_int (binfo
3936 ? CLASSTYPE_ALIGN (type)
3937 : TYPE_ALIGN (type)));
3938 normalize_rli (rli);
3940 else
3941 /* There was no conflict. We're done laying out this field. */
3942 break;
3945 /* Now that we know where it wil be placed, update its
3946 BINFO_OFFSET. */
3947 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3948 propagate_binfo_offsets (binfo,
3949 convert (ssizetype, offset));
3952 /* Layout the empty base BINFO. EOC indicates the byte currently just
3953 past the end of the class, and should be correctly aligned for a
3954 class of the type indicated by BINFO; OFFSETS gives the offsets of
3955 the empty bases allocated so far. */
3957 static void
3958 layout_empty_base (binfo, eoc, offsets)
3959 tree binfo;
3960 tree eoc;
3961 splay_tree offsets;
3963 tree alignment;
3964 tree basetype = BINFO_TYPE (binfo);
3966 /* This routine should only be used for empty classes. */
3967 my_friendly_assert (is_empty_class (basetype), 20000321);
3968 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3970 /* This is an empty base class. We first try to put it at offset
3971 zero. */
3972 if (layout_conflict_p (BINFO_TYPE (binfo),
3973 BINFO_OFFSET (binfo),
3974 offsets,
3975 /*vbases_p=*/0))
3977 /* That didn't work. Now, we move forward from the next
3978 available spot in the class. */
3979 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3980 while (1)
3982 if (!layout_conflict_p (BINFO_TYPE (binfo),
3983 BINFO_OFFSET (binfo),
3984 offsets,
3985 /*vbases_p=*/0))
3986 /* We finally found a spot where there's no overlap. */
3987 break;
3989 /* There's overlap here, too. Bump along to the next spot. */
3990 propagate_binfo_offsets (binfo, alignment);
3995 /* Build a FIELD_DECL for the base given by BINFO in the class
3996 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
3997 *BASE_ALIGN is a running maximum of the alignments of any base
3998 class. OFFSETS gives the location of empty base subobjects. */
4000 static void
4001 build_base_field (rli, binfo, empty_p, offsets)
4002 record_layout_info rli;
4003 tree binfo;
4004 int *empty_p;
4005 splay_tree offsets;
4007 tree basetype = BINFO_TYPE (binfo);
4008 tree decl;
4010 if (!COMPLETE_TYPE_P (basetype))
4011 /* This error is now reported in xref_tag, thus giving better
4012 location information. */
4013 return;
4015 decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
4016 DECL_ARTIFICIAL (decl) = 1;
4017 DECL_FIELD_CONTEXT (decl) = rli->t;
4018 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4019 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4020 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4021 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4023 if (!integer_zerop (DECL_SIZE (decl)))
4025 /* The containing class is non-empty because it has a non-empty
4026 base class. */
4027 *empty_p = 0;
4029 /* Try to place the field. It may take more than one try if we
4030 have a hard time placing the field without putting two
4031 objects of the same type at the same address. */
4032 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4034 else
4036 unsigned HOST_WIDE_INT eoc;
4038 /* On some platforms (ARM), even empty classes will not be
4039 byte-aligned. */
4040 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
4041 eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
4042 layout_empty_base (binfo, size_int (eoc), offsets);
4045 /* Check for inaccessible base classes. If the same base class
4046 appears more than once in the hierarchy, but isn't virtual, then
4047 it's ambiguous. */
4048 if (get_base_distance (basetype, rli->t, 0, NULL) == -2)
4049 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4050 basetype, rli->t);
4052 /* Record the offsets of BINFO and its base subobjects. */
4053 record_subobject_offsets (BINFO_TYPE (binfo),
4054 BINFO_OFFSET (binfo),
4055 offsets,
4056 /*vbases_p=*/0);
4059 /* Layout all of the non-virtual base classes. Record empty
4060 subobjects in OFFSETS. */
4062 static void
4063 build_base_fields (rli, empty_p, offsets)
4064 record_layout_info rli;
4065 int *empty_p;
4066 splay_tree offsets;
4068 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4069 subobjects. */
4070 tree rec = rli->t;
4071 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
4072 int i;
4074 /* Under the new ABI, the primary base class is always allocated
4075 first. */
4076 if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
4077 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
4078 empty_p, offsets);
4080 /* Now allocate the rest of the bases. */
4081 for (i = 0; i < n_baseclasses; ++i)
4083 tree base_binfo;
4085 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4087 /* Under the new ABI, the primary base was already allocated
4088 above, so we don't need to allocate it again here. */
4089 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
4090 continue;
4092 /* A primary virtual base class is allocated just like any other
4093 base class, but a non-primary virtual base is allocated
4094 later, in layout_virtual_bases. */
4095 if (TREE_VIA_VIRTUAL (base_binfo)
4096 && !BINFO_PRIMARY_P (base_binfo))
4097 continue;
4099 build_base_field (rli, base_binfo, empty_p, offsets);
4103 /* Go through the TYPE_METHODS of T issuing any appropriate
4104 diagnostics, figuring out which methods override which other
4105 methods, and so forth. */
4107 static void
4108 check_methods (t)
4109 tree t;
4111 tree x;
4112 int seen_one_arg_array_delete_p = 0;
4114 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4116 GNU_xref_member (current_class_name, x);
4118 /* If this was an evil function, don't keep it in class. */
4119 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4120 continue;
4122 check_for_override (x, t);
4123 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4124 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4126 /* The name of the field is the original field name
4127 Save this in auxiliary field for later overloading. */
4128 if (DECL_VINDEX (x))
4130 TYPE_POLYMORPHIC_P (t) = 1;
4131 if (DECL_PURE_VIRTUAL_P (x))
4132 CLASSTYPE_PURE_VIRTUALS (t)
4133 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4136 if (DECL_ARRAY_DELETE_OPERATOR_P (x))
4138 tree second_parm;
4140 /* When dynamically allocating an array of this type, we
4141 need a "cookie" to record how many elements we allocated,
4142 even if the array elements have no non-trivial
4143 destructor, if the usual array deallocation function
4144 takes a second argument of type size_t. The standard (in
4145 [class.free]) requires that the second argument be set
4146 correctly. */
4147 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
4148 /* Under the new ABI, we choose only those function that are
4149 explicitly declared as `operator delete[] (void *,
4150 size_t)'. */
4151 if (!seen_one_arg_array_delete_p
4152 && second_parm
4153 && TREE_CHAIN (second_parm) == void_list_node
4154 && same_type_p (TREE_VALUE (second_parm), sizetype))
4155 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4156 /* If there's no second parameter, then this is the usual
4157 deallocation function. */
4158 else if (second_parm == void_list_node)
4159 seen_one_arg_array_delete_p = 1;
4164 /* FN is a constructor or destructor. Clone the declaration to create
4165 a specialized in-charge or not-in-charge version, as indicated by
4166 NAME. */
4168 static tree
4169 build_clone (fn, name)
4170 tree fn;
4171 tree name;
4173 tree parms;
4174 tree clone;
4176 /* Copy the function. */
4177 clone = copy_decl (fn);
4178 /* Remember where this function came from. */
4179 DECL_CLONED_FUNCTION (clone) = fn;
4180 /* Reset the function name. */
4181 DECL_NAME (clone) = name;
4182 DECL_ASSEMBLER_NAME (clone) = DECL_NAME (clone);
4183 /* There's no pending inline data for this function. */
4184 DECL_PENDING_INLINE_INFO (clone) = NULL;
4185 DECL_PENDING_INLINE_P (clone) = 0;
4186 /* And it hasn't yet been deferred. */
4187 DECL_DEFERRED_FN (clone) = 0;
4188 /* There's no magic VTT parameter in the clone. */
4189 DECL_VTT_PARM (clone) = NULL_TREE;
4191 /* The base-class destructor is not virtual. */
4192 if (name == base_dtor_identifier)
4194 DECL_VIRTUAL_P (clone) = 0;
4195 if (TREE_CODE (clone) != TEMPLATE_DECL)
4196 DECL_VINDEX (clone) = NULL_TREE;
4199 /* If there was an in-charge parameter, drop it from the function
4200 type. */
4201 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4203 tree basetype;
4204 tree parmtypes;
4205 tree exceptions;
4207 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4208 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4209 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4210 /* Skip the `this' parameter. */
4211 parmtypes = TREE_CHAIN (parmtypes);
4212 /* Skip the in-charge parameter. */
4213 parmtypes = TREE_CHAIN (parmtypes);
4214 /* If this is subobject constructor or destructor, add the vtt
4215 parameter. */
4216 if (DECL_NEEDS_VTT_PARM_P (clone))
4217 parmtypes = hash_tree_chain (vtt_parm_type, parmtypes);
4218 TREE_TYPE (clone)
4219 = build_cplus_method_type (basetype,
4220 TREE_TYPE (TREE_TYPE (clone)),
4221 parmtypes);
4222 if (exceptions)
4223 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4224 exceptions);
4227 /* Copy the function parameters. But, DECL_ARGUMENTS aren't
4228 function parameters; instead, those are the template parameters. */
4229 if (TREE_CODE (clone) != TEMPLATE_DECL)
4231 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4232 /* Remove the in-charge parameter. */
4233 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4235 TREE_CHAIN (DECL_ARGUMENTS (clone))
4236 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4237 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4240 /* Add the VTT parameter. */
4241 if (DECL_NEEDS_VTT_PARM_P (clone))
4243 tree parm;
4245 parm = build_artificial_parm (vtt_parm_identifier,
4246 vtt_parm_type);
4247 TREE_CHAIN (parm) = TREE_CHAIN (DECL_ARGUMENTS (clone));
4248 TREE_CHAIN (DECL_ARGUMENTS (clone)) = parm;
4251 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4253 DECL_CONTEXT (parms) = clone;
4254 copy_lang_decl (parms);
4258 /* Mangle the function name. */
4259 set_mangled_name_for_decl (clone);
4261 /* Create the RTL for this function. */
4262 DECL_RTL (clone) = NULL_RTX;
4263 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4265 /* Make it easy to find the CLONE given the FN. */
4266 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4267 TREE_CHAIN (fn) = clone;
4269 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4270 if (TREE_CODE (clone) == TEMPLATE_DECL)
4272 tree result;
4274 DECL_TEMPLATE_RESULT (clone)
4275 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4276 result = DECL_TEMPLATE_RESULT (clone);
4277 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4278 DECL_TI_TEMPLATE (result) = clone;
4280 else if (DECL_DEFERRED_FN (fn))
4281 defer_fn (clone);
4283 return clone;
4286 /* Produce declarations for all appropriate clones of FN. If
4287 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4288 CLASTYPE_METHOD_VEC as well. */
4290 void
4291 clone_function_decl (fn, update_method_vec_p)
4292 tree fn;
4293 int update_method_vec_p;
4295 tree clone;
4297 /* Avoid inappropriate cloning. */
4298 if (TREE_CHAIN (fn)
4299 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4300 return;
4302 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4304 /* For each constructor, we need two variants: an in-charge version
4305 and a not-in-charge version. */
4306 clone = build_clone (fn, complete_ctor_identifier);
4307 if (update_method_vec_p)
4308 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4309 clone = build_clone (fn, base_ctor_identifier);
4310 if (update_method_vec_p)
4311 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4313 else
4315 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4317 /* For each destructor, we need three variants: an in-charge
4318 version, a not-in-charge version, and an in-charge deleting
4319 version. We clone the deleting version first because that
4320 means it will go second on the TYPE_METHODS list -- and that
4321 corresponds to the correct layout order in the virtual
4322 function table.
4324 For a non-virtual destructor, we do not build a deleting
4325 destructor. */
4326 if (DECL_VIRTUAL_P (fn))
4328 clone = build_clone (fn, deleting_dtor_identifier);
4329 if (update_method_vec_p)
4330 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4332 clone = build_clone (fn, complete_dtor_identifier);
4333 if (update_method_vec_p)
4334 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4335 clone = build_clone (fn, base_dtor_identifier);
4336 if (update_method_vec_p)
4337 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4341 /* For each of the constructors and destructors in T, create an
4342 in-charge and not-in-charge variant. */
4344 static void
4345 clone_constructors_and_destructors (t)
4346 tree t;
4348 tree fns;
4350 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4351 out now. */
4352 if (!CLASSTYPE_METHOD_VEC (t))
4353 return;
4355 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4356 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4357 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4358 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4361 /* Remove all zero-width bit-fields from T. */
4363 static void
4364 remove_zero_width_bit_fields (t)
4365 tree t;
4367 tree *fieldsp;
4369 fieldsp = &TYPE_FIELDS (t);
4370 while (*fieldsp)
4372 if (TREE_CODE (*fieldsp) == FIELD_DECL
4373 && DECL_C_BIT_FIELD (*fieldsp)
4374 && DECL_INITIAL (*fieldsp))
4375 *fieldsp = TREE_CHAIN (*fieldsp);
4376 else
4377 fieldsp = &TREE_CHAIN (*fieldsp);
4381 /* Check the validity of the bases and members declared in T. Add any
4382 implicitly-generated functions (like copy-constructors and
4383 assignment operators). Compute various flag bits (like
4384 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4385 level: i.e., independently of the ABI in use. */
4387 static void
4388 check_bases_and_members (t, empty_p)
4389 tree t;
4390 int *empty_p;
4392 /* Nonzero if we are not allowed to generate a default constructor
4393 for this case. */
4394 int cant_have_default_ctor;
4395 /* Nonzero if the implicitly generated copy constructor should take
4396 a non-const reference argument. */
4397 int cant_have_const_ctor;
4398 /* Nonzero if the the implicitly generated assignment operator
4399 should take a non-const reference argument. */
4400 int no_const_asn_ref;
4401 tree access_decls;
4403 /* By default, we use const reference arguments and generate default
4404 constructors. */
4405 cant_have_default_ctor = 0;
4406 cant_have_const_ctor = 0;
4407 no_const_asn_ref = 0;
4409 /* Assume that the class is nearly empty; we'll clear this flag if
4410 it turns out not to be nearly empty. */
4411 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4413 /* Check all the base-classes. */
4414 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4415 &no_const_asn_ref);
4417 /* Check all the data member declarations. */
4418 check_field_decls (t, &access_decls, empty_p,
4419 &cant_have_default_ctor,
4420 &cant_have_const_ctor,
4421 &no_const_asn_ref);
4423 /* Check all the method declarations. */
4424 check_methods (t);
4426 /* A nearly-empty class has to be vptr-containing; a nearly empty
4427 class contains just a vptr. */
4428 if (!TYPE_CONTAINS_VPTR_P (t))
4429 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4431 /* Do some bookkeeping that will guide the generation of implicitly
4432 declared member functions. */
4433 TYPE_HAS_COMPLEX_INIT_REF (t)
4434 |= (TYPE_HAS_INIT_REF (t)
4435 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4436 || TYPE_POLYMORPHIC_P (t));
4437 TYPE_NEEDS_CONSTRUCTING (t)
4438 |= (TYPE_HAS_CONSTRUCTOR (t)
4439 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4440 || TYPE_POLYMORPHIC_P (t));
4441 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4442 || TYPE_POLYMORPHIC_P (t));
4443 CLASSTYPE_NON_POD_P (t)
4444 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4445 || TYPE_HAS_ASSIGN_REF (t));
4446 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4447 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4448 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4450 /* Synthesize any needed methods. Note that methods will be synthesized
4451 for anonymous unions; grok_x_components undoes that. */
4452 add_implicitly_declared_members (t, cant_have_default_ctor,
4453 cant_have_const_ctor,
4454 no_const_asn_ref);
4456 /* Create the in-charge and not-in-charge variants of constructors
4457 and destructors. */
4458 clone_constructors_and_destructors (t);
4460 /* Process the using-declarations. */
4461 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4462 handle_using_decl (TREE_VALUE (access_decls), t);
4464 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4465 finish_struct_methods (t);
4468 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4469 accordingly. If a new vfield was created (because T doesn't have a
4470 primary base class), then the newly created field is returned. It
4471 is not added to the TYPE_FIELDS list; it is the caller's
4472 responsibility to do that. */
4474 static tree
4475 create_vtable_ptr (t, empty_p, vfuns_p,
4476 new_virtuals_p, overridden_virtuals_p)
4477 tree t;
4478 int *empty_p;
4479 int *vfuns_p;
4480 tree *new_virtuals_p;
4481 tree *overridden_virtuals_p;
4483 tree fn;
4485 /* Loop over the virtual functions, adding them to our various
4486 vtables. */
4487 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4488 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
4489 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4490 vfuns_p, fn, t);
4492 /* If we couldn't find an appropriate base class, create a new field
4493 here. Even if there weren't any new virtual functions, we might need a
4494 new virtual function table if we're supposed to include vptrs in
4495 all classes that need them. */
4496 if (!TYPE_VFIELD (t)
4497 && (*vfuns_p
4498 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())))
4500 /* We build this decl with vtbl_ptr_type_node, which is a
4501 `vtable_entry_type*'. It might seem more precise to use
4502 `vtable_entry_type (*)[N]' where N is the number of firtual
4503 functions. However, that would require the vtable pointer in
4504 base classes to have a different type than the vtable pointer
4505 in derived classes. We could make that happen, but that
4506 still wouldn't solve all the problems. In particular, the
4507 type-based alias analysis code would decide that assignments
4508 to the base class vtable pointer can't alias assignments to
4509 the derived class vtable pointer, since they have different
4510 types. Thus, in an derived class destructor, where the base
4511 class constructor was inlined, we could generate bad code for
4512 setting up the vtable pointer.
4514 Therefore, we use one type for all vtable pointers. We still
4515 use a type-correct type; it's just doesn't indicate the array
4516 bounds. That's better than using `void*' or some such; it's
4517 cleaner, and it let's the alias analysis code know that these
4518 stores cannot alias stores to void*! */
4519 TYPE_VFIELD (t)
4520 = build_vtbl_or_vbase_field (get_vfield_name (t),
4521 get_identifier (VFIELD_BASE),
4522 vtbl_ptr_type_node,
4525 empty_p);
4527 if (CLASSTYPE_N_BASECLASSES (t))
4528 /* If there were any baseclasses, they can't possibly be at
4529 offset zero any more, because that's where the vtable
4530 pointer is. So, converting to a base class is going to
4531 take work. */
4532 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4534 return TYPE_VFIELD (t);
4537 return NULL_TREE;
4540 /* Fixup the inline function given by INFO now that the class is
4541 complete. */
4543 static void
4544 fixup_pending_inline (fn)
4545 tree fn;
4547 if (DECL_PENDING_INLINE_INFO (fn))
4549 tree args = DECL_ARGUMENTS (fn);
4550 while (args)
4552 DECL_CONTEXT (args) = fn;
4553 args = TREE_CHAIN (args);
4558 /* Fixup the inline methods and friends in TYPE now that TYPE is
4559 complete. */
4561 static void
4562 fixup_inline_methods (type)
4563 tree type;
4565 tree method = TYPE_METHODS (type);
4567 if (method && TREE_CODE (method) == TREE_VEC)
4569 if (TREE_VEC_ELT (method, 1))
4570 method = TREE_VEC_ELT (method, 1);
4571 else if (TREE_VEC_ELT (method, 0))
4572 method = TREE_VEC_ELT (method, 0);
4573 else
4574 method = TREE_VEC_ELT (method, 2);
4577 /* Do inline member functions. */
4578 for (; method; method = TREE_CHAIN (method))
4579 fixup_pending_inline (method);
4581 /* Do friends. */
4582 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4583 method;
4584 method = TREE_CHAIN (method))
4585 fixup_pending_inline (TREE_VALUE (method));
4586 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4589 /* Add OFFSET to all base types of BINFO which is a base in the
4590 hierarchy dominated by T.
4592 OFFSET, which is a type offset, is number of bytes. */
4594 static void
4595 propagate_binfo_offsets (binfo, offset)
4596 tree binfo;
4597 tree offset;
4599 int i;
4600 tree primary_binfo;
4602 /* Update BINFO's offset. */
4603 BINFO_OFFSET (binfo)
4604 = convert (sizetype,
4605 size_binop (PLUS_EXPR,
4606 convert (ssizetype, BINFO_OFFSET (binfo)),
4607 offset));
4609 /* Find the primary base class. */
4610 primary_binfo = get_primary_binfo (binfo);
4612 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4613 downwards. */
4614 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4616 tree base_binfo;
4618 /* On the first through the loop, do the primary base. Because
4619 the primary base need not be an immediate base, we must
4620 handle the primary base specially. */
4621 if (i == -1)
4623 if (!primary_binfo)
4624 continue;
4626 base_binfo = primary_binfo;
4628 else
4630 base_binfo = BINFO_BASETYPE (binfo, i);
4631 /* Don't do the primary base twice. */
4632 if (base_binfo == primary_binfo)
4633 continue;
4636 /* Skip virtual bases that aren't our primary base. */
4637 if (TREE_VIA_VIRTUAL (base_binfo)
4638 && BINFO_PRIMARY_BASE_OF (base_binfo) != binfo)
4639 continue;
4641 propagate_binfo_offsets (base_binfo, offset);
4645 /* Called via dfs_walk from layout_virtual bases. */
4647 static tree
4648 dfs_set_offset_for_unshared_vbases (binfo, data)
4649 tree binfo;
4650 void *data;
4652 /* If this is a virtual base, make sure it has the same offset as
4653 the shared copy. If it's a primary base, then we know it's
4654 correct. */
4655 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_P (binfo))
4657 tree t = (tree) data;
4658 tree vbase;
4659 tree offset;
4661 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4662 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4663 propagate_binfo_offsets (binfo, offset);
4666 return NULL_TREE;
4669 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4670 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4671 empty subobjects of T. */
4673 static void
4674 layout_virtual_bases (t, offsets)
4675 tree t;
4676 splay_tree offsets;
4678 tree vbases;
4679 unsigned HOST_WIDE_INT dsize;
4680 unsigned HOST_WIDE_INT eoc;
4682 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4683 return;
4685 #ifdef STRUCTURE_SIZE_BOUNDARY
4686 /* Packed structures don't need to have minimum size. */
4687 if (! TYPE_PACKED (t))
4688 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4689 #endif
4691 /* DSIZE is the size of the class without the virtual bases. */
4692 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4694 /* Make every class have alignment of at least one. */
4695 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4697 /* Go through the virtual bases, allocating space for each virtual
4698 base that is not already a primary base class. Under the new
4699 ABI, these are allocated according to a depth-first left-to-right
4700 postorder traversal; in the new ABI, inheritance graph order is
4701 used instead. */
4702 for (vbases = TYPE_BINFO (t);
4703 vbases;
4704 vbases = TREE_CHAIN (vbases))
4706 tree vbase;
4708 if (!TREE_VIA_VIRTUAL (vbases))
4709 continue;
4710 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4712 if (!BINFO_PRIMARY_P (vbase))
4714 /* This virtual base is not a primary base of any class in the
4715 hierarchy, so we have to add space for it. */
4716 tree basetype;
4717 unsigned int desired_align;
4719 basetype = BINFO_TYPE (vbase);
4721 desired_align = CLASSTYPE_ALIGN (basetype);
4722 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4724 /* Add padding so that we can put the virtual base class at an
4725 appropriately aligned offset. */
4726 dsize = CEIL (dsize, desired_align) * desired_align;
4728 /* Under the new ABI, we try to squish empty virtual bases in
4729 just like ordinary empty bases. */
4730 if (is_empty_class (basetype))
4731 layout_empty_base (vbase,
4732 size_int (CEIL (dsize, BITS_PER_UNIT)),
4733 offsets);
4734 else
4736 tree offset;
4738 offset = ssize_int (CEIL (dsize, BITS_PER_UNIT));
4739 offset = size_diffop (offset,
4740 convert (ssizetype,
4741 BINFO_OFFSET (vbase)));
4743 /* And compute the offset of the virtual base. */
4744 propagate_binfo_offsets (vbase, offset);
4745 /* Every virtual baseclass takes a least a UNIT, so that
4746 we can take it's address and get something different
4747 for each base. */
4748 dsize += MAX (BITS_PER_UNIT,
4749 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4752 /* Keep track of the offsets assigned to this virtual base. */
4753 record_subobject_offsets (BINFO_TYPE (vbase),
4754 BINFO_OFFSET (vbase),
4755 offsets,
4756 /*vbases_p=*/0);
4760 /* Now, go through the TYPE_BINFO hierarchy, setting the
4761 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4762 bases and their direct and indirect bases. The ambiguity checks
4763 in get_base_distance depend on the BINFO_OFFSETs being set
4764 correctly. */
4765 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4767 /* If we had empty base classes that protruded beyond the end of the
4768 class, we didn't update DSIZE above; we were hoping to overlay
4769 multiple such bases at the same location. */
4770 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4771 if (eoc * BITS_PER_UNIT > dsize)
4772 dsize = (eoc + 1) * BITS_PER_UNIT;
4774 /* Now, make sure that the total size of the type is a multiple of
4775 its alignment. */
4776 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4777 TYPE_SIZE (t) = bitsize_int (dsize);
4778 TYPE_SIZE_UNIT (t) = convert (sizetype,
4779 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4780 bitsize_unit_node));
4782 /* Check for ambiguous virtual bases. */
4783 if (extra_warnings)
4784 for (vbases = CLASSTYPE_VBASECLASSES (t);
4785 vbases;
4786 vbases = TREE_CHAIN (vbases))
4788 tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
4789 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4790 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4791 basetype, t);
4795 /* Returns the offset of the byte just past the end of the base class
4796 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4797 only non-virtual bases are included. */
4799 static unsigned HOST_WIDE_INT
4800 end_of_class (t, include_virtuals_p)
4801 tree t;
4802 int include_virtuals_p;
4804 unsigned HOST_WIDE_INT result = 0;
4805 int i;
4807 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4809 tree base_binfo;
4810 tree offset;
4811 unsigned HOST_WIDE_INT end_of_base;
4813 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4815 if (!include_virtuals_p
4816 && TREE_VIA_VIRTUAL (base_binfo)
4817 && !BINFO_PRIMARY_P (base_binfo))
4818 continue;
4820 offset = size_binop (PLUS_EXPR,
4821 BINFO_OFFSET (base_binfo),
4822 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
4823 end_of_base = tree_low_cst (offset, /*pos=*/1);
4824 if (end_of_base > result)
4825 result = end_of_base;
4828 return result;
4831 /* Compare two INTEGER_CSTs K1 and K2. */
4833 static int
4834 splay_tree_compare_integer_csts (k1, k2)
4835 splay_tree_key k1;
4836 splay_tree_key k2;
4838 return tree_int_cst_compare ((tree) k1, (tree) k2);
4841 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4842 BINFO_OFFSETs for all of the base-classes. Position the vtable
4843 pointer. */
4845 static void
4846 layout_class_type (t, empty_p, vfuns_p,
4847 new_virtuals_p, overridden_virtuals_p)
4848 tree t;
4849 int *empty_p;
4850 int *vfuns_p;
4851 tree *new_virtuals_p;
4852 tree *overridden_virtuals_p;
4854 tree non_static_data_members;
4855 tree field;
4856 tree vptr;
4857 record_layout_info rli;
4858 unsigned HOST_WIDE_INT eoc;
4859 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4860 types that appear at that offset. */
4861 splay_tree empty_base_offsets;
4863 /* Keep track of the first non-static data member. */
4864 non_static_data_members = TYPE_FIELDS (t);
4866 /* Start laying out the record. */
4867 rli = start_record_layout (t);
4869 /* If possible, we reuse the virtual function table pointer from one
4870 of our base classes. */
4871 determine_primary_base (t, vfuns_p);
4873 /* Create a pointer to our virtual function table. */
4874 vptr = create_vtable_ptr (t, empty_p, vfuns_p,
4875 new_virtuals_p, overridden_virtuals_p);
4877 /* Under the new ABI, the vptr is always the first thing in the
4878 class. */
4879 if (vptr)
4881 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4882 place_field (rli, vptr);
4885 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4886 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4887 NULL, NULL);
4888 build_base_fields (rli, empty_p, empty_base_offsets);
4889 /* Add pointers to all of our virtual base-classes. */
4890 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (rli, empty_p),
4891 TYPE_FIELDS (t));
4893 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4894 we have to save this before we start modifying
4895 TYPE_NONCOPIED_PARTS. */
4896 fixup_inline_methods (t);
4898 /* Layout the non-static data members. */
4899 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4901 tree type;
4902 tree padding;
4904 /* We still pass things that aren't non-static data members to
4905 the back-end, in case it wants to do something with them. */
4906 if (TREE_CODE (field) != FIELD_DECL)
4908 place_field (rli, field);
4909 continue;
4912 type = TREE_TYPE (field);
4914 /* If this field is a bit-field whose width is greater than its
4915 type, then there are some special rules for allocating it
4916 under the new ABI. Under the old ABI, there were no special
4917 rules, but the back-end can't handle bitfields longer than a
4918 `long long', so we use the same mechanism. */
4919 if (DECL_C_BIT_FIELD (field)
4920 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4922 integer_type_kind itk;
4923 tree integer_type;
4925 /* We must allocate the bits as if suitably aligned for the
4926 longest integer type that fits in this many bits. type
4927 of the field. Then, we are supposed to use the left over
4928 bits as additional padding. */
4929 for (itk = itk_char; itk != itk_none; ++itk)
4930 if (INT_CST_LT (DECL_SIZE (field),
4931 TYPE_SIZE (integer_types[itk])))
4932 break;
4934 /* ITK now indicates a type that is too large for the
4935 field. We have to back up by one to find the largest
4936 type that fits. */
4937 integer_type = integer_types[itk - 1];
4938 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4939 TYPE_SIZE (integer_type));
4940 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4941 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4942 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4944 else
4945 padding = NULL_TREE;
4947 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4948 empty_base_offsets);
4950 /* If we needed additional padding after this field, add it
4951 now. */
4952 if (padding)
4954 tree padding_field;
4956 padding_field = build_decl (FIELD_DECL,
4957 NULL_TREE,
4958 char_type_node);
4959 DECL_BIT_FIELD (padding_field) = 1;
4960 DECL_SIZE (padding_field) = padding;
4961 DECL_ALIGN (padding_field) = 1;
4962 DECL_USER_ALIGN (padding_field) = 0;
4963 layout_nonempty_base_or_field (rli, padding_field,
4964 NULL_TREE,
4965 empty_base_offsets);
4969 /* It might be the case that we grew the class to allocate a
4970 zero-sized base class. That won't be reflected in RLI, yet,
4971 because we are willing to overlay multiple bases at the same
4972 offset. However, now we need to make sure that RLI is big enough
4973 to reflect the entire class. */
4974 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4975 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4976 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
4978 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc + 1));
4979 rli->bitpos = bitsize_zero_node;
4982 /* We make all structures have at least one element, so that they
4983 have non-zero size. In the new ABI, the class may be empty even
4984 if it has basetypes. Therefore, we add the fake field after all
4985 the other fields; if there are already FIELD_DECLs on the list,
4986 their offsets will not be disturbed. */
4987 if (*empty_p)
4989 tree padding;
4991 padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
4992 place_field (rli, padding);
4993 TYPE_NONCOPIED_PARTS (t)
4994 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
4995 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4998 /* Let the back-end lay out the type. Note that at this point we
4999 have only included non-virtual base-classes; we will lay out the
5000 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
5001 this call are not necessarily correct; they are just the size and
5002 alignment when no virtual base clases are used. */
5003 finish_record_layout (rli);
5005 /* Delete all zero-width bit-fields from the list of fields. Now
5006 that the type is laid out they are no longer important. */
5007 remove_zero_width_bit_fields (t);
5009 /* Remember the size and alignment of the class before adding
5010 the virtual bases. */
5011 if (*empty_p)
5013 CLASSTYPE_SIZE (t) = bitsize_zero_node;
5014 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
5016 else
5018 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
5019 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
5022 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5023 CLASSTYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (t);
5025 /* Set the TYPE_DECL for this type to contain the right
5026 value for DECL_OFFSET, so that we can use it as part
5027 of a COMPONENT_REF for multiple inheritance. */
5028 layout_decl (TYPE_MAIN_DECL (t), 0);
5030 /* Now fix up any virtual base class types that we left lying
5031 around. We must get these done before we try to lay out the
5032 virtual function table. As a side-effect, this will remove the
5033 base subobject fields. */
5034 layout_virtual_bases (t, empty_base_offsets);
5036 /* Clean up. */
5037 splay_tree_delete (empty_base_offsets);
5040 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5041 (or C++ class declaration).
5043 For C++, we must handle the building of derived classes.
5044 Also, C++ allows static class members. The way that this is
5045 handled is to keep the field name where it is (as the DECL_NAME
5046 of the field), and place the overloaded decl in the bit position
5047 of the field. layout_record and layout_union will know about this.
5049 More C++ hair: inline functions have text in their
5050 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5051 meaningful tree structure. After the struct has been laid out, set
5052 things up so that this can happen.
5054 And still more: virtual functions. In the case of single inheritance,
5055 when a new virtual function is seen which redefines a virtual function
5056 from the base class, the new virtual function is placed into
5057 the virtual function table at exactly the same address that
5058 it had in the base class. When this is extended to multiple
5059 inheritance, the same thing happens, except that multiple virtual
5060 function tables must be maintained. The first virtual function
5061 table is treated in exactly the same way as in the case of single
5062 inheritance. Additional virtual function tables have different
5063 DELTAs, which tell how to adjust `this' to point to the right thing.
5065 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5067 void
5068 finish_struct_1 (t)
5069 tree t;
5071 tree x;
5072 int vfuns;
5073 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5074 a FUNCTION_DECL. Each of these functions is a virtual function
5075 declared in T that does not override any virtual function from a
5076 base class. */
5077 tree new_virtuals = NULL_TREE;
5078 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5079 except that each declaration here overrides the declaration from
5080 a base class. */
5081 tree overridden_virtuals = NULL_TREE;
5082 int n_fields = 0;
5083 tree vfield;
5084 int empty = 1;
5086 if (COMPLETE_TYPE_P (t))
5088 if (IS_AGGR_TYPE (t))
5089 cp_error ("redefinition of `%#T'", t);
5090 else
5091 my_friendly_abort (172);
5092 popclass ();
5093 return;
5096 GNU_xref_decl (current_function_decl, t);
5098 /* If this type was previously laid out as a forward reference,
5099 make sure we lay it out again. */
5100 TYPE_SIZE (t) = NULL_TREE;
5101 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5102 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5103 vfuns = 0;
5104 CLASSTYPE_RTTI (t) = NULL_TREE;
5106 /* Do end-of-class semantic processing: checking the validity of the
5107 bases and members and add implicitly generated methods. */
5108 check_bases_and_members (t, &empty);
5110 /* Layout the class itself. */
5111 layout_class_type (t, &empty, &vfuns,
5112 &new_virtuals, &overridden_virtuals);
5114 if (flag_dump_class_layout)
5115 dump_class_hierarchy (*flag_dump_class_layout
5116 ? flag_dump_class_layout : NULL,
5119 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
5120 might need to know it for setting up the offsets in the vtable
5121 (or in thunks) below. */
5122 vfield = TYPE_VFIELD (t);
5123 if (vfield != NULL_TREE
5124 && DECL_FIELD_CONTEXT (vfield) != t)
5126 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
5128 vfield = copy_decl (vfield);
5130 DECL_FIELD_CONTEXT (vfield) = t;
5131 DECL_FIELD_OFFSET (vfield)
5132 = size_binop (PLUS_EXPR,
5133 BINFO_OFFSET (binfo),
5134 DECL_FIELD_OFFSET (vfield));
5135 TYPE_VFIELD (t) = vfield;
5138 overridden_virtuals
5139 = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
5141 /* If we created a new vtbl pointer for this class, add it to the
5142 list. */
5143 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5144 CLASSTYPE_VFIELDS (t)
5145 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5147 /* If necessary, create the primary vtable for this class. */
5148 if (new_virtuals
5149 || overridden_virtuals
5150 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
5152 new_virtuals = nreverse (new_virtuals);
5153 /* We must enter these virtuals into the table. */
5154 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5155 build_primary_vtable (NULL_TREE, t);
5156 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5157 /* Here we know enough to change the type of our virtual
5158 function table, but we will wait until later this function. */
5159 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5161 /* If this type has basetypes with constructors, then those
5162 constructors might clobber the virtual function table. But
5163 they don't if the derived class shares the exact vtable of the base
5164 class. */
5166 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5168 /* If we didn't need a new vtable, see if we should copy one from
5169 the base. */
5170 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5172 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5174 /* If this class uses a different vtable than its primary base
5175 then when we will need to initialize our vptr after the base
5176 class constructor runs. */
5177 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5178 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5181 if (TYPE_CONTAINS_VPTR_P (t))
5183 if (TYPE_BINFO_VTABLE (t))
5184 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5185 20000116);
5186 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5187 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5188 20000116);
5190 CLASSTYPE_VSIZE (t) = vfuns;
5191 /* Entries for virtual functions defined in the primary base are
5192 followed by entries for new functions unique to this class. */
5193 TYPE_BINFO_VIRTUALS (t)
5194 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5195 /* Finally, add entries for functions that override virtuals
5196 from non-primary bases. */
5197 TYPE_BINFO_VIRTUALS (t)
5198 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5201 finish_struct_bits (t);
5203 /* Complete the rtl for any static member objects of the type we're
5204 working on. */
5205 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5207 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5208 && TREE_TYPE (x) == t)
5210 DECL_MODE (x) = TYPE_MODE (t);
5211 make_decl_rtl (x, NULL);
5215 /* Done with FIELDS...now decide whether to sort these for
5216 faster lookups later.
5218 The C front-end only does this when n_fields > 15. We use
5219 a smaller number because most searches fail (succeeding
5220 ultimately as the search bores through the inheritance
5221 hierarchy), and we want this failure to occur quickly. */
5223 n_fields = count_fields (TYPE_FIELDS (t));
5224 if (n_fields > 7)
5226 tree field_vec = make_tree_vec (n_fields);
5227 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5228 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5229 (int (*)(const void *, const void *))field_decl_cmp);
5230 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5231 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5232 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5235 if (TYPE_HAS_CONSTRUCTOR (t))
5237 tree vfields = CLASSTYPE_VFIELDS (t);
5239 while (vfields)
5241 /* Mark the fact that constructor for T
5242 could affect anybody inheriting from T
5243 who wants to initialize vtables for VFIELDS's type. */
5244 if (VF_DERIVED_VALUE (vfields))
5245 TREE_ADDRESSABLE (vfields) = 1;
5246 vfields = TREE_CHAIN (vfields);
5250 /* Make the rtl for any new vtables we have created, and unmark
5251 the base types we marked. */
5252 finish_vtbls (t);
5254 /* Build the VTT for T. */
5255 build_vtt (t);
5257 if (TYPE_VFIELD (t))
5259 /* In addition to this one, all the other vfields should be listed. */
5260 /* Before that can be done, we have to have FIELD_DECLs for them, and
5261 a place to find them. */
5262 TYPE_NONCOPIED_PARTS (t)
5263 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5264 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5266 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5267 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5268 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5272 hack_incomplete_structures (t);
5274 if (warn_overloaded_virtual)
5275 warn_hidden (t);
5277 maybe_suppress_debug_info (t);
5279 /* Finish debugging output for this type. */
5280 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5283 /* When T was built up, the member declarations were added in reverse
5284 order. Rearrange them to declaration order. */
5286 void
5287 unreverse_member_declarations (t)
5288 tree t;
5290 tree next;
5291 tree prev;
5292 tree x;
5294 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5295 reverse order. Put them in declaration order now. */
5296 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5297 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5299 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5300 reverse order, so we can't just use nreverse. */
5301 prev = NULL_TREE;
5302 for (x = TYPE_FIELDS (t);
5303 x && TREE_CODE (x) != TYPE_DECL;
5304 x = next)
5306 next = TREE_CHAIN (x);
5307 TREE_CHAIN (x) = prev;
5308 prev = x;
5310 if (prev)
5312 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5313 if (prev)
5314 TYPE_FIELDS (t) = prev;
5318 tree
5319 finish_struct (t, attributes)
5320 tree t, attributes;
5322 const char *saved_filename = input_filename;
5323 int saved_lineno = lineno;
5325 /* Now that we've got all the field declarations, reverse everything
5326 as necessary. */
5327 unreverse_member_declarations (t);
5329 cplus_decl_attributes (t, attributes, NULL_TREE);
5331 /* Nadger the current location so that diagnostics point to the start of
5332 the struct, not the end. */
5333 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5334 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5336 if (processing_template_decl)
5338 finish_struct_methods (t);
5339 TYPE_SIZE (t) = bitsize_zero_node;
5341 else
5342 finish_struct_1 (t);
5344 input_filename = saved_filename;
5345 lineno = saved_lineno;
5347 TYPE_BEING_DEFINED (t) = 0;
5349 if (current_class_type)
5350 popclass ();
5351 else
5352 error ("trying to finish struct, but kicked out due to previous parse errors.");
5354 if (processing_template_decl)
5356 tree scope = current_scope ();
5357 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5358 add_stmt (build_min (TAG_DEFN, t));
5361 return t;
5364 /* Return the dynamic type of INSTANCE, if known.
5365 Used to determine whether the virtual function table is needed
5366 or not.
5368 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5369 of our knowledge of its type. *NONNULL should be initialized
5370 before this function is called. */
5372 static tree
5373 fixed_type_or_null (instance, nonnull)
5374 tree instance;
5375 int *nonnull;
5377 switch (TREE_CODE (instance))
5379 case INDIRECT_REF:
5380 /* Check that we are not going through a cast of some sort. */
5381 if (TREE_TYPE (instance)
5382 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5383 instance = TREE_OPERAND (instance, 0);
5384 /* fall through... */
5385 case CALL_EXPR:
5386 /* This is a call to a constructor, hence it's never zero. */
5387 if (TREE_HAS_CONSTRUCTOR (instance))
5389 if (nonnull)
5390 *nonnull = 1;
5391 return TREE_TYPE (instance);
5393 return NULL_TREE;
5395 case SAVE_EXPR:
5396 /* This is a call to a constructor, hence it's never zero. */
5397 if (TREE_HAS_CONSTRUCTOR (instance))
5399 if (nonnull)
5400 *nonnull = 1;
5401 return TREE_TYPE (instance);
5403 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5405 case RTL_EXPR:
5406 return NULL_TREE;
5408 case PLUS_EXPR:
5409 case MINUS_EXPR:
5410 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5411 /* Propagate nonnull. */
5412 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5413 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5414 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5415 return NULL_TREE;
5417 case NOP_EXPR:
5418 case CONVERT_EXPR:
5419 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5421 case ADDR_EXPR:
5422 if (nonnull)
5423 *nonnull = 1;
5424 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5426 case COMPONENT_REF:
5427 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5429 case VAR_DECL:
5430 case FIELD_DECL:
5431 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5432 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5434 if (nonnull)
5435 *nonnull = 1;
5436 return TREE_TYPE (TREE_TYPE (instance));
5438 /* fall through... */
5439 case TARGET_EXPR:
5440 case PARM_DECL:
5441 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5443 if (nonnull)
5444 *nonnull = 1;
5445 return TREE_TYPE (instance);
5447 else if (nonnull)
5449 if (instance == current_class_ptr
5450 && flag_this_is_variable <= 0)
5452 /* Normally, 'this' must be non-null. */
5453 if (flag_this_is_variable == 0)
5454 *nonnull = 1;
5456 /* <0 means we're in a constructor and we know our type. */
5457 if (flag_this_is_variable < 0)
5458 return TREE_TYPE (TREE_TYPE (instance));
5460 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5461 /* Reference variables should be references to objects. */
5462 *nonnull = 1;
5464 return NULL_TREE;
5466 default:
5467 return NULL_TREE;
5471 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5472 to the static type. We also handle the case where INSTANCE is really
5473 a pointer.
5475 Used to determine whether the virtual function table is needed
5476 or not.
5478 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5479 of our knowledge of its type. *NONNULL should be initialized
5480 before this function is called. */
5483 resolves_to_fixed_type_p (instance, nonnull)
5484 tree instance;
5485 int *nonnull;
5487 tree t = TREE_TYPE (instance);
5488 tree fixed = fixed_type_or_null (instance, nonnull);
5489 if (fixed == NULL_TREE)
5490 return 0;
5491 if (POINTER_TYPE_P (t))
5492 t = TREE_TYPE (t);
5493 return same_type_ignoring_top_level_qualifiers_p (t, fixed);
5497 void
5498 init_class_processing ()
5500 current_class_depth = 0;
5501 current_class_stack_size = 10;
5502 current_class_stack
5503 = (class_stack_node_t) xmalloc (current_class_stack_size
5504 * sizeof (struct class_stack_node));
5505 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5506 ggc_add_tree_varray_root (&local_classes, 1);
5508 access_default_node = build_int_2 (0, 0);
5509 access_public_node = build_int_2 (ak_public, 0);
5510 access_protected_node = build_int_2 (ak_protected, 0);
5511 access_private_node = build_int_2 (ak_private, 0);
5512 access_default_virtual_node = build_int_2 (4, 0);
5513 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5514 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5515 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5517 ridpointers[(int) RID_PUBLIC] = access_public_node;
5518 ridpointers[(int) RID_PRIVATE] = access_private_node;
5519 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5522 /* Set current scope to NAME. CODE tells us if this is a
5523 STRUCT, UNION, or ENUM environment.
5525 NAME may end up being NULL_TREE if this is an anonymous or
5526 late-bound struct (as in "struct { ... } foo;") */
5528 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5529 appropriate values, found by looking up the type definition of
5530 NAME (as a CODE).
5532 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5533 which can be seen locally to the class. They are shadowed by
5534 any subsequent local declaration (including parameter names).
5536 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5537 which have static meaning (i.e., static members, static
5538 member functions, enum declarations, etc).
5540 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5541 which can be seen locally to the class (as in 1), but
5542 know that we are doing this for declaration purposes
5543 (i.e. friend foo::bar (int)).
5545 So that we may avoid calls to lookup_name, we cache the _TYPE
5546 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5548 For multiple inheritance, we perform a two-pass depth-first search
5549 of the type lattice. The first pass performs a pre-order search,
5550 marking types after the type has had its fields installed in
5551 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5552 unmarks the marked types. If a field or member function name
5553 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5554 that name becomes `error_mark_node'. */
5556 void
5557 pushclass (type, modify)
5558 tree type;
5559 int modify;
5561 type = TYPE_MAIN_VARIANT (type);
5563 /* Make sure there is enough room for the new entry on the stack. */
5564 if (current_class_depth + 1 >= current_class_stack_size)
5566 current_class_stack_size *= 2;
5567 current_class_stack
5568 = (class_stack_node_t) xrealloc (current_class_stack,
5569 current_class_stack_size
5570 * sizeof (struct class_stack_node));
5573 /* Insert a new entry on the class stack. */
5574 current_class_stack[current_class_depth].name = current_class_name;
5575 current_class_stack[current_class_depth].type = current_class_type;
5576 current_class_stack[current_class_depth].access = current_access_specifier;
5577 current_class_stack[current_class_depth].names_used = 0;
5578 current_class_depth++;
5580 /* Now set up the new type. */
5581 current_class_name = TYPE_NAME (type);
5582 if (TREE_CODE (current_class_name) == TYPE_DECL)
5583 current_class_name = DECL_NAME (current_class_name);
5584 current_class_type = type;
5586 /* By default, things in classes are private, while things in
5587 structures or unions are public. */
5588 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5589 ? access_private_node
5590 : access_public_node);
5592 if (previous_class_type != NULL_TREE
5593 && (type != previous_class_type
5594 || !COMPLETE_TYPE_P (previous_class_type))
5595 && current_class_depth == 1)
5597 /* Forcibly remove any old class remnants. */
5598 invalidate_class_lookup_cache ();
5601 /* If we're about to enter a nested class, clear
5602 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5603 if (modify && current_class_depth > 1)
5604 clear_identifier_class_values ();
5606 pushlevel_class ();
5608 if (modify)
5610 if (type != previous_class_type || current_class_depth > 1)
5611 push_class_decls (type);
5612 else
5614 tree item;
5616 /* We are re-entering the same class we just left, so we
5617 don't have to search the whole inheritance matrix to find
5618 all the decls to bind again. Instead, we install the
5619 cached class_shadowed list, and walk through it binding
5620 names and setting up IDENTIFIER_TYPE_VALUEs. */
5621 set_class_shadows (previous_class_values);
5622 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5624 tree id = TREE_PURPOSE (item);
5625 tree decl = TREE_TYPE (item);
5627 push_class_binding (id, decl);
5628 if (TREE_CODE (decl) == TYPE_DECL)
5629 set_identifier_type_value (id, TREE_TYPE (decl));
5631 unuse_fields (type);
5634 storetags (CLASSTYPE_TAGS (type));
5638 /* When we exit a toplevel class scope, we save the
5639 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5640 reenter the class. Here, we've entered some other class, so we
5641 must invalidate our cache. */
5643 void
5644 invalidate_class_lookup_cache ()
5646 tree t;
5648 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5649 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5650 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5652 previous_class_values = NULL_TREE;
5653 previous_class_type = NULL_TREE;
5656 /* Get out of the current class scope. If we were in a class scope
5657 previously, that is the one popped to. */
5659 void
5660 popclass ()
5662 poplevel_class ();
5663 /* Since poplevel_class does the popping of class decls nowadays,
5664 this really only frees the obstack used for these decls. */
5665 pop_class_decls ();
5667 current_class_depth--;
5668 current_class_name = current_class_stack[current_class_depth].name;
5669 current_class_type = current_class_stack[current_class_depth].type;
5670 current_access_specifier = current_class_stack[current_class_depth].access;
5671 if (current_class_stack[current_class_depth].names_used)
5672 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5675 /* Returns 1 if current_class_type is either T or a nested type of T.
5676 We start looking from 1 because entry 0 is from global scope, and has
5677 no type. */
5680 currently_open_class (t)
5681 tree t;
5683 int i;
5684 if (t == current_class_type)
5685 return 1;
5686 for (i = 1; i < current_class_depth; ++i)
5687 if (current_class_stack [i].type == t)
5688 return 1;
5689 return 0;
5692 /* If either current_class_type or one of its enclosing classes are derived
5693 from T, return the appropriate type. Used to determine how we found
5694 something via unqualified lookup. */
5696 tree
5697 currently_open_derived_class (t)
5698 tree t;
5700 int i;
5702 if (DERIVED_FROM_P (t, current_class_type))
5703 return current_class_type;
5705 for (i = current_class_depth - 1; i > 0; --i)
5706 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5707 return current_class_stack[i].type;
5709 return NULL_TREE;
5712 /* When entering a class scope, all enclosing class scopes' names with
5713 static meaning (static variables, static functions, types and enumerators)
5714 have to be visible. This recursive function calls pushclass for all
5715 enclosing class contexts until global or a local scope is reached.
5716 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5717 formal of the same name. */
5719 void
5720 push_nested_class (type, modify)
5721 tree type;
5722 int modify;
5724 tree context;
5726 /* A namespace might be passed in error cases, like A::B:C. */
5727 if (type == NULL_TREE
5728 || type == error_mark_node
5729 || TREE_CODE (type) == NAMESPACE_DECL
5730 || ! IS_AGGR_TYPE (type)
5731 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5732 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5733 return;
5735 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5737 if (context && CLASS_TYPE_P (context))
5738 push_nested_class (context, 2);
5739 pushclass (type, modify);
5742 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5744 void
5745 pop_nested_class ()
5747 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5749 popclass ();
5750 if (context && CLASS_TYPE_P (context))
5751 pop_nested_class ();
5754 /* Set global variables CURRENT_LANG_NAME to appropriate value
5755 so that behavior of name-mangling machinery is correct. */
5757 void
5758 push_lang_context (name)
5759 tree name;
5761 *current_lang_stack++ = current_lang_name;
5762 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5763 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5765 size_t old_size = VARRAY_SIZE (current_lang_base);
5767 VARRAY_GROW (current_lang_base, old_size + 10);
5768 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5771 if (name == lang_name_cplusplus)
5773 current_lang_name = name;
5775 else if (name == lang_name_java)
5777 current_lang_name = name;
5778 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5779 (See record_builtin_java_type in decl.c.) However, that causes
5780 incorrect debug entries if these types are actually used.
5781 So we re-enable debug output after extern "Java". */
5782 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5783 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5784 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5785 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5786 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5787 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5788 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5789 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5791 else if (name == lang_name_c)
5793 current_lang_name = name;
5795 else
5796 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5799 /* Get out of the current language scope. */
5801 void
5802 pop_lang_context ()
5804 /* Clear the current entry so that garbage collector won't hold on
5805 to it. */
5806 *current_lang_stack = NULL_TREE;
5807 current_lang_name = *--current_lang_stack;
5810 /* Type instantiation routines. */
5812 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5813 matches the TARGET_TYPE. If there is no satisfactory match, return
5814 error_mark_node, and issue an error message if COMPLAIN is
5815 non-zero. Permit pointers to member function if PTRMEM is non-zero.
5816 If TEMPLATE_ONLY, the name of the overloaded function
5817 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5818 template arguments. */
5820 static tree
5821 resolve_address_of_overloaded_function (target_type,
5822 overload,
5823 complain,
5824 ptrmem,
5825 template_only,
5826 explicit_targs)
5827 tree target_type;
5828 tree overload;
5829 int complain;
5830 int ptrmem;
5831 int template_only;
5832 tree explicit_targs;
5834 /* Here's what the standard says:
5836 [over.over]
5838 If the name is a function template, template argument deduction
5839 is done, and if the argument deduction succeeds, the deduced
5840 arguments are used to generate a single template function, which
5841 is added to the set of overloaded functions considered.
5843 Non-member functions and static member functions match targets of
5844 type "pointer-to-function" or "reference-to-function." Nonstatic
5845 member functions match targets of type "pointer-to-member
5846 function;" the function type of the pointer to member is used to
5847 select the member function from the set of overloaded member
5848 functions. If a nonstatic member function is selected, the
5849 reference to the overloaded function name is required to have the
5850 form of a pointer to member as described in 5.3.1.
5852 If more than one function is selected, any template functions in
5853 the set are eliminated if the set also contains a non-template
5854 function, and any given template function is eliminated if the
5855 set contains a second template function that is more specialized
5856 than the first according to the partial ordering rules 14.5.5.2.
5857 After such eliminations, if any, there shall remain exactly one
5858 selected function. */
5860 int is_ptrmem = 0;
5861 int is_reference = 0;
5862 /* We store the matches in a TREE_LIST rooted here. The functions
5863 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5864 interoperability with most_specialized_instantiation. */
5865 tree matches = NULL_TREE;
5866 tree fn;
5868 /* By the time we get here, we should be seeing only real
5869 pointer-to-member types, not the internal POINTER_TYPE to
5870 METHOD_TYPE representation. */
5871 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5872 && (TREE_CODE (TREE_TYPE (target_type))
5873 == METHOD_TYPE)), 0);
5875 if (TREE_CODE (overload) == COMPONENT_REF)
5876 overload = TREE_OPERAND (overload, 1);
5878 /* Check that the TARGET_TYPE is reasonable. */
5879 if (TYPE_PTRFN_P (target_type))
5880 /* This is OK. */;
5881 else if (TYPE_PTRMEMFUNC_P (target_type))
5882 /* This is OK, too. */
5883 is_ptrmem = 1;
5884 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5886 /* This is OK, too. This comes from a conversion to reference
5887 type. */
5888 target_type = build_reference_type (target_type);
5889 is_reference = 1;
5891 else
5893 if (complain)
5894 cp_error ("\
5895 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5896 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5897 return error_mark_node;
5900 /* If we can find a non-template function that matches, we can just
5901 use it. There's no point in generating template instantiations
5902 if we're just going to throw them out anyhow. But, of course, we
5903 can only do this when we don't *need* a template function. */
5904 if (!template_only)
5906 tree fns;
5908 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5910 tree fn = OVL_FUNCTION (fns);
5911 tree fntype;
5913 if (TREE_CODE (fn) == TEMPLATE_DECL)
5914 /* We're not looking for templates just yet. */
5915 continue;
5917 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5918 != is_ptrmem)
5919 /* We're looking for a non-static member, and this isn't
5920 one, or vice versa. */
5921 continue;
5923 /* See if there's a match. */
5924 fntype = TREE_TYPE (fn);
5925 if (is_ptrmem)
5926 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5927 else if (!is_reference)
5928 fntype = build_pointer_type (fntype);
5930 if (can_convert_arg (target_type, fntype, fn))
5931 matches = tree_cons (fn, NULL_TREE, matches);
5935 /* Now, if we've already got a match (or matches), there's no need
5936 to proceed to the template functions. But, if we don't have a
5937 match we need to look at them, too. */
5938 if (!matches)
5940 tree target_fn_type;
5941 tree target_arg_types;
5942 tree target_ret_type;
5943 tree fns;
5945 if (is_ptrmem)
5946 target_fn_type
5947 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5948 else
5949 target_fn_type = TREE_TYPE (target_type);
5950 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5951 target_ret_type = TREE_TYPE (target_fn_type);
5953 /* Never do unification on the 'this' parameter. */
5954 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5955 target_arg_types = TREE_CHAIN (target_arg_types);
5957 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5959 tree fn = OVL_FUNCTION (fns);
5960 tree instantiation;
5961 tree instantiation_type;
5962 tree targs;
5964 if (TREE_CODE (fn) != TEMPLATE_DECL)
5965 /* We're only looking for templates. */
5966 continue;
5968 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5969 != is_ptrmem)
5970 /* We're not looking for a non-static member, and this is
5971 one, or vice versa. */
5972 continue;
5974 /* Try to do argument deduction. */
5975 targs = make_tree_vec (DECL_NTPARMS (fn));
5976 if (fn_type_unification (fn, explicit_targs, targs,
5977 target_arg_types, target_ret_type,
5978 DEDUCE_EXACT, -1) != 0)
5979 /* Argument deduction failed. */
5980 continue;
5982 /* Instantiate the template. */
5983 instantiation = instantiate_template (fn, targs);
5984 if (instantiation == error_mark_node)
5985 /* Instantiation failed. */
5986 continue;
5988 /* See if there's a match. */
5989 instantiation_type = TREE_TYPE (instantiation);
5990 if (is_ptrmem)
5991 instantiation_type =
5992 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5993 else if (!is_reference)
5994 instantiation_type = build_pointer_type (instantiation_type);
5995 if (can_convert_arg (target_type, instantiation_type, instantiation))
5996 matches = tree_cons (instantiation, fn, matches);
5999 /* Now, remove all but the most specialized of the matches. */
6000 if (matches)
6002 tree match = most_specialized_instantiation (matches);
6004 if (match != error_mark_node)
6005 matches = tree_cons (match, NULL_TREE, NULL_TREE);
6009 /* Now we should have exactly one function in MATCHES. */
6010 if (matches == NULL_TREE)
6012 /* There were *no* matches. */
6013 if (complain)
6015 cp_error ("no matches converting function `%D' to type `%#T'",
6016 DECL_NAME (OVL_FUNCTION (overload)),
6017 target_type);
6019 /* print_candidates expects a chain with the functions in
6020 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6021 so why be clever?). */
6022 for (; overload; overload = OVL_NEXT (overload))
6023 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6024 matches);
6026 print_candidates (matches);
6028 return error_mark_node;
6030 else if (TREE_CHAIN (matches))
6032 /* There were too many matches. */
6034 if (complain)
6036 tree match;
6038 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6039 DECL_NAME (OVL_FUNCTION (overload)),
6040 target_type);
6042 /* Since print_candidates expects the functions in the
6043 TREE_VALUE slot, we flip them here. */
6044 for (match = matches; match; match = TREE_CHAIN (match))
6045 TREE_VALUE (match) = TREE_PURPOSE (match);
6047 print_candidates (matches);
6050 return error_mark_node;
6053 /* Good, exactly one match. Now, convert it to the correct type. */
6054 fn = TREE_PURPOSE (matches);
6056 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6057 && !ptrmem && !flag_ms_extensions)
6059 static int explained;
6061 if (!complain)
6062 return error_mark_node;
6064 cp_pedwarn ("assuming pointer to member `%D'", fn);
6065 if (!explained)
6067 cp_pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6068 explained = 1;
6071 mark_used (fn);
6073 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6074 return build_unary_op (ADDR_EXPR, fn, 0);
6075 else
6077 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6078 will mark the function as addressed, but here we must do it
6079 explicitly. */
6080 mark_addressable (fn);
6082 return fn;
6086 /* This function will instantiate the type of the expression given in
6087 RHS to match the type of LHSTYPE. If errors exist, then return
6088 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6089 we complain on errors. If we are not complaining, never modify rhs,
6090 as overload resolution wants to try many possible instantiations, in
6091 the hope that at least one will work.
6093 For non-recursive calls, LHSTYPE should be a function, pointer to
6094 function, or a pointer to member function. */
6096 tree
6097 instantiate_type (lhstype, rhs, flags)
6098 tree lhstype, rhs;
6099 enum instantiate_type_flags flags;
6101 int complain = (flags & itf_complain);
6102 int strict = (flags & itf_no_attributes)
6103 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6104 int allow_ptrmem = flags & itf_ptrmem_ok;
6106 flags &= ~itf_ptrmem_ok;
6108 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6110 if (complain)
6111 error ("not enough type information");
6112 return error_mark_node;
6115 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6117 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6118 return rhs;
6119 if (complain)
6120 cp_error ("argument of type `%T' does not match `%T'",
6121 TREE_TYPE (rhs), lhstype);
6122 return error_mark_node;
6125 /* We don't overwrite rhs if it is an overloaded function.
6126 Copying it would destroy the tree link. */
6127 if (TREE_CODE (rhs) != OVERLOAD)
6128 rhs = copy_node (rhs);
6130 /* This should really only be used when attempting to distinguish
6131 what sort of a pointer to function we have. For now, any
6132 arithmetic operation which is not supported on pointers
6133 is rejected as an error. */
6135 switch (TREE_CODE (rhs))
6137 case TYPE_EXPR:
6138 case CONVERT_EXPR:
6139 case SAVE_EXPR:
6140 case CONSTRUCTOR:
6141 case BUFFER_REF:
6142 my_friendly_abort (177);
6143 return error_mark_node;
6145 case INDIRECT_REF:
6146 case ARRAY_REF:
6148 tree new_rhs;
6150 new_rhs = instantiate_type (build_pointer_type (lhstype),
6151 TREE_OPERAND (rhs, 0), flags);
6152 if (new_rhs == error_mark_node)
6153 return error_mark_node;
6155 TREE_TYPE (rhs) = lhstype;
6156 TREE_OPERAND (rhs, 0) = new_rhs;
6157 return rhs;
6160 case NOP_EXPR:
6161 rhs = copy_node (TREE_OPERAND (rhs, 0));
6162 TREE_TYPE (rhs) = unknown_type_node;
6163 return instantiate_type (lhstype, rhs, flags);
6165 case COMPONENT_REF:
6166 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6168 case OFFSET_REF:
6169 rhs = TREE_OPERAND (rhs, 1);
6170 if (BASELINK_P (rhs))
6171 return instantiate_type (lhstype, TREE_VALUE (rhs),
6172 flags | allow_ptrmem);
6174 /* This can happen if we are forming a pointer-to-member for a
6175 member template. */
6176 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6178 /* Fall through. */
6180 case TEMPLATE_ID_EXPR:
6182 tree fns = TREE_OPERAND (rhs, 0);
6183 tree args = TREE_OPERAND (rhs, 1);
6185 return
6186 resolve_address_of_overloaded_function (lhstype,
6187 fns,
6188 complain,
6189 allow_ptrmem,
6190 /*template_only=*/1,
6191 args);
6194 case OVERLOAD:
6195 return
6196 resolve_address_of_overloaded_function (lhstype,
6197 rhs,
6198 complain,
6199 allow_ptrmem,
6200 /*template_only=*/0,
6201 /*explicit_targs=*/NULL_TREE);
6203 case TREE_LIST:
6204 /* Now we should have a baselink. */
6205 my_friendly_assert (BASELINK_P (rhs), 990412);
6207 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6209 case CALL_EXPR:
6210 /* This is too hard for now. */
6211 my_friendly_abort (183);
6212 return error_mark_node;
6214 case PLUS_EXPR:
6215 case MINUS_EXPR:
6216 case COMPOUND_EXPR:
6217 TREE_OPERAND (rhs, 0)
6218 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6219 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6220 return error_mark_node;
6221 TREE_OPERAND (rhs, 1)
6222 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6223 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6224 return error_mark_node;
6226 TREE_TYPE (rhs) = lhstype;
6227 return rhs;
6229 case MULT_EXPR:
6230 case TRUNC_DIV_EXPR:
6231 case FLOOR_DIV_EXPR:
6232 case CEIL_DIV_EXPR:
6233 case ROUND_DIV_EXPR:
6234 case RDIV_EXPR:
6235 case TRUNC_MOD_EXPR:
6236 case FLOOR_MOD_EXPR:
6237 case CEIL_MOD_EXPR:
6238 case ROUND_MOD_EXPR:
6239 case FIX_ROUND_EXPR:
6240 case FIX_FLOOR_EXPR:
6241 case FIX_CEIL_EXPR:
6242 case FIX_TRUNC_EXPR:
6243 case FLOAT_EXPR:
6244 case NEGATE_EXPR:
6245 case ABS_EXPR:
6246 case MAX_EXPR:
6247 case MIN_EXPR:
6248 case FFS_EXPR:
6250 case BIT_AND_EXPR:
6251 case BIT_IOR_EXPR:
6252 case BIT_XOR_EXPR:
6253 case LSHIFT_EXPR:
6254 case RSHIFT_EXPR:
6255 case LROTATE_EXPR:
6256 case RROTATE_EXPR:
6258 case PREINCREMENT_EXPR:
6259 case PREDECREMENT_EXPR:
6260 case POSTINCREMENT_EXPR:
6261 case POSTDECREMENT_EXPR:
6262 if (complain)
6263 error ("invalid operation on uninstantiated type");
6264 return error_mark_node;
6266 case TRUTH_AND_EXPR:
6267 case TRUTH_OR_EXPR:
6268 case TRUTH_XOR_EXPR:
6269 case LT_EXPR:
6270 case LE_EXPR:
6271 case GT_EXPR:
6272 case GE_EXPR:
6273 case EQ_EXPR:
6274 case NE_EXPR:
6275 case TRUTH_ANDIF_EXPR:
6276 case TRUTH_ORIF_EXPR:
6277 case TRUTH_NOT_EXPR:
6278 if (complain)
6279 error ("not enough type information");
6280 return error_mark_node;
6282 case COND_EXPR:
6283 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6285 if (complain)
6286 error ("not enough type information");
6287 return error_mark_node;
6289 TREE_OPERAND (rhs, 1)
6290 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6291 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6292 return error_mark_node;
6293 TREE_OPERAND (rhs, 2)
6294 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6295 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6296 return error_mark_node;
6298 TREE_TYPE (rhs) = lhstype;
6299 return rhs;
6301 case MODIFY_EXPR:
6302 TREE_OPERAND (rhs, 1)
6303 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6304 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6305 return error_mark_node;
6307 TREE_TYPE (rhs) = lhstype;
6308 return rhs;
6310 case ADDR_EXPR:
6312 if (PTRMEM_OK_P (rhs))
6313 flags |= itf_ptrmem_ok;
6315 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6317 case ENTRY_VALUE_EXPR:
6318 my_friendly_abort (184);
6319 return error_mark_node;
6321 case ERROR_MARK:
6322 return error_mark_node;
6324 default:
6325 my_friendly_abort (185);
6326 return error_mark_node;
6330 /* Return the name of the virtual function pointer field
6331 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6332 this may have to look back through base types to find the
6333 ultimate field name. (For single inheritance, these could
6334 all be the same name. Who knows for multiple inheritance). */
6336 static tree
6337 get_vfield_name (type)
6338 tree type;
6340 tree binfo = TYPE_BINFO (type);
6341 char *buf;
6343 while (BINFO_BASETYPES (binfo)
6344 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6345 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6346 binfo = BINFO_BASETYPE (binfo, 0);
6348 type = BINFO_TYPE (binfo);
6349 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6350 + TYPE_NAME_LENGTH (type) + 2);
6351 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6352 return get_identifier (buf);
6355 void
6356 print_class_statistics ()
6358 #ifdef GATHER_STATISTICS
6359 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6360 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6361 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6362 n_build_method_call, n_inner_fields_searched);
6363 if (n_vtables)
6365 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6366 n_vtables, n_vtable_searches);
6367 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6368 n_vtable_entries, n_vtable_elems);
6370 #endif
6373 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6374 according to [class]:
6375 The class-name is also inserted
6376 into the scope of the class itself. For purposes of access checking,
6377 the inserted class name is treated as if it were a public member name. */
6379 void
6380 build_self_reference ()
6382 tree name = constructor_name (current_class_type);
6383 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6384 tree saved_cas;
6386 DECL_NONLOCAL (value) = 1;
6387 DECL_CONTEXT (value) = current_class_type;
6388 DECL_ARTIFICIAL (value) = 1;
6390 if (processing_template_decl)
6391 value = push_template_decl (value);
6393 saved_cas = current_access_specifier;
6394 current_access_specifier = access_public_node;
6395 finish_member_declaration (value);
6396 current_access_specifier = saved_cas;
6399 /* Returns 1 if TYPE contains only padding bytes. */
6402 is_empty_class (type)
6403 tree type;
6405 if (type == error_mark_node)
6406 return 0;
6408 if (! IS_AGGR_TYPE (type))
6409 return 0;
6411 return integer_zerop (CLASSTYPE_SIZE (type));
6414 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6415 a *_TYPE node. NODE can also be a local class. */
6417 tree
6418 get_enclosing_class (type)
6419 tree type;
6421 tree node = type;
6423 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6425 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6427 case 'd':
6428 node = DECL_CONTEXT (node);
6429 break;
6431 case 't':
6432 if (node != type)
6433 return node;
6434 node = TYPE_CONTEXT (node);
6435 break;
6437 default:
6438 my_friendly_abort (0);
6441 return NULL_TREE;
6444 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6447 is_base_of_enclosing_class (base, type)
6448 tree base, type;
6450 while (type)
6452 if (get_binfo (base, type, 0))
6453 return 1;
6455 type = get_enclosing_class (type);
6457 return 0;
6460 /* Note that NAME was looked up while the current class was being
6461 defined and that the result of that lookup was DECL. */
6463 void
6464 maybe_note_name_used_in_class (name, decl)
6465 tree name;
6466 tree decl;
6468 splay_tree names_used;
6470 /* If we're not defining a class, there's nothing to do. */
6471 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6472 return;
6474 /* If there's already a binding for this NAME, then we don't have
6475 anything to worry about. */
6476 if (IDENTIFIER_CLASS_VALUE (name))
6477 return;
6479 if (!current_class_stack[current_class_depth - 1].names_used)
6480 current_class_stack[current_class_depth - 1].names_used
6481 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6482 names_used = current_class_stack[current_class_depth - 1].names_used;
6484 splay_tree_insert (names_used,
6485 (splay_tree_key) name,
6486 (splay_tree_value) decl);
6489 /* Note that NAME was declared (as DECL) in the current class. Check
6490 to see that the declaration is legal. */
6492 void
6493 note_name_declared_in_class (name, decl)
6494 tree name;
6495 tree decl;
6497 splay_tree names_used;
6498 splay_tree_node n;
6500 /* Look to see if we ever used this name. */
6501 names_used
6502 = current_class_stack[current_class_depth - 1].names_used;
6503 if (!names_used)
6504 return;
6506 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6507 if (n)
6509 /* [basic.scope.class]
6511 A name N used in a class S shall refer to the same declaration
6512 in its context and when re-evaluated in the completed scope of
6513 S. */
6514 cp_error ("declaration of `%#D'", decl);
6515 cp_error_at ("changes meaning of `%D' from `%+#D'",
6516 DECL_NAME (OVL_CURRENT (decl)),
6517 (tree) n->value);
6521 /* Returns the VAR_DECL for the complete vtable associated with
6522 BINFO. (Under the new ABI, secondary vtables are merged with
6523 primary vtables; this function will return the VAR_DECL for the
6524 primary vtable.) */
6526 tree
6527 get_vtbl_decl_for_binfo (binfo)
6528 tree binfo;
6530 tree decl;
6532 decl = BINFO_VTABLE (binfo);
6533 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6535 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6536 2000403);
6537 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6539 if (decl)
6540 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6541 return decl;
6544 /* Called from get_primary_binfo via dfs_walk. */
6546 static tree
6547 dfs_get_primary_binfo (binfo, data)
6548 tree binfo;
6549 void *data;
6551 tree primary_base = (tree) data;
6553 if (TREE_VIA_VIRTUAL (binfo)
6554 && same_type_p (BINFO_TYPE (binfo), BINFO_TYPE (primary_base)))
6555 return binfo;
6557 return NULL_TREE;
6560 /* Returns the binfo for the primary base of BINFO. Note that in a
6561 complex hierarchy the resulting BINFO may not actually *be*
6562 primary. In particular if the resulting BINFO is a virtual base,
6563 and it occurs elsewhere in the hierarchy, then this occurrence may
6564 not actually be a primary base in the complete object. Check
6565 BINFO_PRIMARY_P to be sure. */
6567 tree
6568 get_primary_binfo (binfo)
6569 tree binfo;
6571 tree primary_base;
6572 tree result;
6574 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6575 if (!primary_base)
6576 return NULL_TREE;
6578 /* A non-virtual primary base is always a direct base, and easy to
6579 find. */
6580 if (!TREE_VIA_VIRTUAL (primary_base))
6582 int i;
6584 /* Scan the direct basetypes until we find a base with the same
6585 type as the primary base. */
6586 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6588 tree base_binfo = BINFO_BASETYPE (binfo, i);
6590 if (same_type_p (BINFO_TYPE (base_binfo),
6591 BINFO_TYPE (primary_base)))
6592 return base_binfo;
6595 /* We should always find the primary base. */
6596 my_friendly_abort (20000729);
6599 /* For a primary virtual base, we have to scan the entire hierarchy
6600 rooted at BINFO; the virtual base could be an indirect virtual
6601 base. */
6602 result = dfs_walk (binfo, dfs_get_primary_binfo, NULL, primary_base);
6603 my_friendly_assert (result != NULL_TREE, 20000730);
6604 return result;
6607 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6608 dominated by T) to stderr. INDENT should be zero when called from
6609 the top level; it is incremented recursively. */
6611 static void
6612 dump_class_hierarchy_r (stream, t, binfo, indent)
6613 FILE *stream;
6614 tree t;
6615 tree binfo;
6616 int indent;
6618 int i;
6620 fprintf (stream, "%*s0x%lx (%s) ", indent, "",
6621 (unsigned long) binfo,
6622 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6623 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6624 tree_low_cst (BINFO_OFFSET (binfo), 0));
6625 if (is_empty_class (BINFO_TYPE (binfo)))
6626 fprintf (stream, " empty");
6627 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6628 fprintf (stream, " nearly-empty");
6629 if (TREE_VIA_VIRTUAL (binfo))
6631 tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6633 if (canonical == binfo)
6634 fprintf (stream, " virtual-canonical");
6635 else
6636 fprintf (stream, " virtual-non-canonical");
6638 if (BINFO_PRIMARY_P (binfo))
6639 fprintf (stream, " primary-for 0x%lx (%s)",
6640 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo),
6641 type_as_string (BINFO_PRIMARY_BASE_OF (binfo), TFF_PLAIN_IDENTIFIER));
6642 if (BINFO_LOST_PRIMARY_P (binfo))
6643 fprintf (stream, " lost-primary");
6644 fprintf (stream, "\n");
6646 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6647 dump_class_hierarchy_r (stream, t, BINFO_BASETYPE (binfo, i), indent + 2);
6650 /* Dump the BINFO hierarchy for T. */
6652 void
6653 dump_class_hierarchy (name, t)
6654 const char *name;
6655 tree t;
6657 FILE *stream = stderr;
6659 if (name)
6661 static int append = 0;
6663 stream = fopen (name, append++ ? "a" : "w");
6664 if (!stream)
6665 error ("could not open dump file `%s'", name);
6666 return;
6668 fprintf (stream, "%s\n",
6669 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6670 dump_class_hierarchy_r (stream, t, TYPE_BINFO (t), 0);
6671 fprintf (stream, "\n");
6672 if (name)
6673 fclose (stream);
6676 /* Virtual function table initialization. */
6678 /* Create all the necessary vtables for T and its base classes. */
6680 static void
6681 finish_vtbls (t)
6682 tree t;
6684 if (merge_primary_and_secondary_vtables_p ())
6686 tree list;
6687 tree vbase;
6689 /* Under the new ABI, we lay out the primary and secondary
6690 vtables in one contiguous vtable. The primary vtable is
6691 first, followed by the non-virtual secondary vtables in
6692 inheritance graph order. */
6693 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6694 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6695 TYPE_BINFO (t), t, list);
6696 /* Then come the virtual bases, also in inheritance graph
6697 order. */
6698 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6700 tree real_base;
6702 if (!TREE_VIA_VIRTUAL (vbase))
6703 continue;
6705 /* Although we walk in inheritance order, that might not get the
6706 canonical base. */
6707 real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
6709 accumulate_vtbl_inits (real_base, real_base,
6710 TYPE_BINFO (t), t, list);
6713 if (TYPE_BINFO_VTABLE (t))
6714 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6716 else
6718 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
6719 dfs_unmarked_real_bases_queue_p, t);
6720 dfs_walk (TYPE_BINFO (t), dfs_unmark,
6721 dfs_marked_real_bases_queue_p, t);
6725 /* Called from finish_vtbls via dfs_walk. */
6727 static tree
6728 dfs_finish_vtbls (binfo, data)
6729 tree binfo;
6730 void *data;
6732 tree t = (tree) data;
6734 if (BINFO_NEW_VTABLE_MARKED (binfo, t))
6735 initialize_vtable (binfo,
6736 build_vtbl_initializer (binfo, binfo, t,
6737 TYPE_BINFO (t), NULL));
6739 SET_BINFO_MARKED (binfo);
6741 return NULL_TREE;
6744 /* Initialize the vtable for BINFO with the INITS. */
6746 static void
6747 initialize_vtable (binfo, inits)
6748 tree binfo;
6749 tree inits;
6751 tree decl;
6753 layout_vtable_decl (binfo, list_length (inits));
6754 decl = get_vtbl_decl_for_binfo (binfo);
6755 initialize_array (decl, inits);
6758 /* Initialize DECL (a declaration for a namespace-scope array) with
6759 the INITS. */
6761 static void
6762 initialize_array (decl, inits)
6763 tree decl;
6764 tree inits;
6766 tree context;
6768 context = DECL_CONTEXT (decl);
6769 DECL_CONTEXT (decl) = NULL_TREE;
6770 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6771 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6772 DECL_CONTEXT (decl) = context;
6775 /* Build the VTT (virtual table table) for T.
6776 A class requires a VTT if it has virtual bases.
6778 This holds
6779 1 - primary virtual pointer for complete object T
6780 2 - secondary VTTs for each direct non-virtual base of T which requires a VTT
6781 3 - secondary virtual pointers for each direct or indirect base of T which
6782 has virtual bases or is reachable via a virtual path from T.
6783 4 - secondary VTTs for each direct or indirect virtual base of T.
6785 Secondary VTTs look like complete object VTTs without part 4. */
6787 static void
6788 build_vtt (t)
6789 tree t;
6791 tree inits;
6792 tree type;
6793 tree vtt;
6794 tree index;
6796 /* Build up the initializers for the VTT. */
6797 inits = NULL_TREE;
6798 index = size_zero_node;
6799 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6801 /* If we didn't need a VTT, we're done. */
6802 if (!inits)
6803 return;
6805 /* Figure out the type of the VTT. */
6806 type = build_index_type (size_int (list_length (inits)));
6807 type = build_cplus_array_type (const_ptr_type_node, type);
6809 /* Now, build the VTT object itself. */
6810 vtt = build_vtable (t, get_vtt_name (t), type);
6811 pushdecl_top_level (vtt);
6812 initialize_array (vtt, inits);
6815 /* The type corresponding to BINFO is a base class of T, but BINFO is
6816 in the base class hierarchy of a class derived from T. Return the
6817 base, in T's hierarchy, that corresponds to BINFO. */
6819 static tree
6820 get_matching_base (binfo, t)
6821 tree binfo;
6822 tree t;
6824 tree derived;
6825 int i;
6827 if (same_type_p (BINFO_TYPE (binfo), t))
6828 return binfo;
6830 if (TREE_VIA_VIRTUAL (binfo))
6831 return binfo_for_vbase (BINFO_TYPE (binfo), t);
6833 derived = get_matching_base (BINFO_INHERITANCE_CHAIN (binfo), t);
6834 for (i = 0; i < BINFO_N_BASETYPES (derived); ++i)
6835 if (same_type_p (BINFO_TYPE (BINFO_BASETYPE (derived, i)),
6836 BINFO_TYPE (binfo)))
6837 return BINFO_BASETYPE (derived, i);
6839 my_friendly_abort (20000628);
6840 return NULL_TREE;
6843 /* Recursively build the VTT-initializer for BINFO (which is in the
6844 hierarchy dominated by T). INITS points to the end of the initializer
6845 list to date. INDEX is the VTT index where the next element will be
6846 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
6847 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
6848 for virtual bases of T. When it is not so, we build the constructor
6849 vtables for the BINFO-in-T variant. */
6851 static tree *
6852 build_vtt_inits (binfo, t, inits, index)
6853 tree binfo;
6854 tree t;
6855 tree *inits;
6856 tree *index;
6858 int i;
6859 tree b;
6860 tree init;
6861 tree secondary_vptrs;
6862 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
6864 /* We only need VTTs for subobjects with virtual bases. */
6865 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
6866 return inits;
6868 /* We need to use a construction vtable if this is not the primary
6869 VTT. */
6870 if (!top_level_p)
6872 build_ctor_vtbl_group (binfo, t);
6874 /* Record the offset in the VTT where this sub-VTT can be found. */
6875 BINFO_SUBVTT_INDEX (binfo) = *index;
6878 /* Add the address of the primary vtable for the complete object. */
6879 init = BINFO_VTABLE (binfo);
6880 if (TREE_CODE (init) == TREE_LIST)
6881 init = TREE_VALUE (init);
6882 *inits = build_tree_list (NULL_TREE, init);
6883 inits = &TREE_CHAIN (*inits);
6884 if (top_level_p)
6886 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
6887 BINFO_VPTR_INDEX (binfo) = *index;
6889 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
6891 /* Recursively add the secondary VTTs for non-virtual bases. */
6892 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6894 b = BINFO_BASETYPE (binfo, i);
6895 if (!TREE_VIA_VIRTUAL (b))
6896 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
6897 inits, index);
6900 /* Add secondary virtual pointers for all subobjects of BINFO with
6901 either virtual bases or reachable along a virtual path, except
6902 subobjects that are non-virtual primary bases. */
6903 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
6904 TREE_TYPE (secondary_vptrs) = *index;
6905 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
6906 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
6908 dfs_walk_real (binfo,
6909 dfs_build_secondary_vptr_vtt_inits,
6910 NULL,
6911 dfs_ctor_vtable_bases_queue_p,
6912 secondary_vptrs);
6913 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
6914 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
6915 secondary_vptrs);
6917 *index = TREE_TYPE (secondary_vptrs);
6919 /* The secondary vptrs come back in reverse order. After we reverse
6920 them, and add the INITS, the last init will be the first element
6921 of the chain. */
6922 secondary_vptrs = TREE_VALUE (secondary_vptrs);
6923 if (secondary_vptrs)
6925 *inits = nreverse (secondary_vptrs);
6926 inits = &TREE_CHAIN (secondary_vptrs);
6927 my_friendly_assert (*inits == NULL_TREE, 20000517);
6930 /* Add the secondary VTTs for virtual bases. */
6931 if (top_level_p)
6932 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
6934 tree vbase;
6936 if (!TREE_VIA_VIRTUAL (b))
6937 continue;
6939 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
6940 inits = build_vtt_inits (vbase, t, inits, index);
6943 if (!top_level_p)
6945 tree data = tree_cons (t, binfo, NULL_TREE);
6946 VTT_TOP_LEVEL_P (data) = 0;
6947 VTT_MARKED_BINFO_P (data) = 0;
6949 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
6950 dfs_ctor_vtable_bases_queue_p,
6951 data);
6954 return inits;
6957 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
6958 for the base in most derived. DATA is a TREE_LIST who's
6959 TREE_CHAIN is the type of the base being
6960 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
6961 flag of DATA indicates that this is a constructor vtable. The
6962 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
6964 static tree
6965 dfs_build_secondary_vptr_vtt_inits (binfo, data)
6966 tree binfo;
6967 void *data;
6969 tree l;
6970 tree t;
6971 tree init;
6972 tree index;
6973 int top_level_p;
6975 l = (tree) data;
6976 t = TREE_CHAIN (l);
6977 top_level_p = VTT_TOP_LEVEL_P (l);
6979 SET_BINFO_MARKED (binfo);
6981 /* We don't care about bases that don't have vtables. */
6982 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
6983 return NULL_TREE;
6985 /* We're only interested in proper subobjects of T. */
6986 if (same_type_p (BINFO_TYPE (binfo), t))
6987 return NULL_TREE;
6989 /* We're not interested in non-virtual primary bases. */
6990 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
6991 return NULL_TREE;
6993 /* If BINFO has virtual bases or is reachable via a virtual path
6994 from T, it'll have a secondary vptr. */
6995 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
6996 && !binfo_via_virtual (binfo, t))
6997 return NULL_TREE;
6999 /* Record the index where this secondary vptr can be found. */
7000 index = TREE_TYPE (l);
7001 if (top_level_p)
7003 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7004 BINFO_VPTR_INDEX (binfo) = index;
7006 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7007 TYPE_SIZE_UNIT (ptr_type_node));
7009 /* Add the initializer for the secondary vptr itself. */
7010 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7012 /* It's a primary virtual base, and this is not the construction
7013 vtable. Find the base this is primary of in the inheritance graph,
7014 and use that base's vtable now. */
7015 while (BINFO_PRIMARY_BASE_OF (binfo))
7016 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7018 init = BINFO_VTABLE (binfo);
7019 if (TREE_CODE (init) == TREE_LIST)
7020 init = TREE_VALUE (init);
7021 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7023 return NULL_TREE;
7026 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7027 TREE_UNSIGNED indicates that a constructor vtable is being built.
7028 TREE_USED indicates whether marked or unmarked bases should be walked.
7029 TREE_PURPOSE is the TREE_TYPE that dominates the hierarchy. */
7031 static tree
7032 dfs_ctor_vtable_bases_queue_p (binfo, data)
7033 tree binfo;
7034 void *data;
7036 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_P (binfo))
7038 tree type = TREE_PURPOSE ((tree) data);
7040 /* This is a non-primary virtual base, get the shared version. */
7041 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
7042 if (VTT_TOP_LEVEL_P ((tree) data) && BINFO_PRIMARY_P (binfo))
7043 return NULL_TREE;
7045 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7046 return NULL_TREE;
7047 return binfo;
7050 /* Called from build_vtt_inits via dfs_walk. After building constructor
7051 vtables and generating the sub-vtt from them, we need to restore the
7052 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7053 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7055 static tree
7056 dfs_fixup_binfo_vtbls (binfo, data)
7057 tree binfo;
7058 void *data;
7060 CLEAR_BINFO_MARKED (binfo);
7062 /* We don't care about bases that don't have vtables. */
7063 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7064 return NULL_TREE;
7066 /* If we scribbled the construction vtable vptr into BINFO, clear it
7067 out now. */
7068 if (TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7069 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7070 == TREE_VALUE ((tree) data)))
7071 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7073 return NULL_TREE;
7076 /* Build the construction vtable group for BINFO which is in the
7077 hierarchy dominated by T. */
7079 static void
7080 build_ctor_vtbl_group (binfo, t)
7081 tree binfo;
7082 tree t;
7084 tree list;
7085 tree type;
7086 tree vtbl;
7087 tree inits;
7088 tree id;
7089 tree vbase;
7091 /* See if we've already create this construction vtable group. */
7092 id = mangle_ctor_vtbl_for_type (t, binfo);
7093 if (IDENTIFIER_GLOBAL_VALUE (id))
7094 return;
7096 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7097 /* Build a version of VTBL (with the wrong type) for use in
7098 constructing the addresses of secondary vtables in the
7099 construction vtable group. */
7100 vtbl = build_vtable (t, id, ptr_type_node);
7101 list = build_tree_list (vtbl, NULL_TREE);
7102 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7103 binfo, t, list);
7105 /* Add the vtables for each of our virtual bases using the vbase in T
7106 binfo. */
7107 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7108 vbase;
7109 vbase = TREE_CHAIN (vbase))
7111 tree b;
7112 tree orig_base;
7114 if (!TREE_VIA_VIRTUAL (vbase))
7115 continue;
7116 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7117 orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7119 accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7121 inits = TREE_VALUE (list);
7123 /* Figure out the type of the construction vtable. */
7124 type = build_index_type (size_int (list_length (inits)));
7125 type = build_cplus_array_type (vtable_entry_type, type);
7126 TREE_TYPE (vtbl) = type;
7128 /* Initialize the construction vtable. */
7129 pushdecl_top_level (vtbl);
7130 initialize_array (vtbl, inits);
7133 /* Add the vtbl initializers for BINFO (and its bases other than
7134 non-virtual primaries) to the list of INITS. BINFO is in the
7135 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7136 the constructor the vtbl inits should be accumulated for. (If this
7137 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7138 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7139 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7140 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7141 but are not necessarily the same in terms of layout. */
7143 static void
7144 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7145 tree binfo;
7146 tree orig_binfo;
7147 tree rtti_binfo;
7148 tree t;
7149 tree inits;
7151 int i;
7152 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7154 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7155 BINFO_TYPE (orig_binfo)),
7156 20000517);
7158 /* If we're building a construction vtable, we're not interested in
7159 subobjects that don't require construction vtables. */
7160 if (ctor_vtbl_p
7161 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7162 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7163 return;
7165 /* Build the initializers for the BINFO-in-T vtable. */
7166 TREE_VALUE (inits)
7167 = chainon (TREE_VALUE (inits),
7168 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7169 rtti_binfo, t, inits));
7171 /* Walk the BINFO and its bases. We walk in preorder so that as we
7172 initialize each vtable we can figure out at what offset the
7173 secondary vtable lies from the primary vtable. We can't use
7174 dfs_walk here because we need to iterate through bases of BINFO
7175 and RTTI_BINFO simultaneously. */
7176 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7178 tree base_binfo = BINFO_BASETYPE (binfo, i);
7180 /* Skip virtual bases. */
7181 if (TREE_VIA_VIRTUAL (base_binfo))
7182 continue;
7183 accumulate_vtbl_inits (base_binfo,
7184 BINFO_BASETYPE (orig_binfo, i),
7185 rtti_binfo, t,
7186 inits);
7190 /* Called from accumulate_vtbl_inits when using the new ABI.
7191 Accumulates the vtable initializers for all of the vtables into
7192 TREE_VALUE (DATA). Returns the initializers for the BINFO vtable. */
7194 static tree
7195 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7196 tree binfo;
7197 tree orig_binfo;
7198 tree rtti_binfo;
7199 tree t;
7200 tree l;
7202 tree inits = NULL_TREE;
7203 tree vtbl = NULL_TREE;
7204 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7206 if (ctor_vtbl_p
7207 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7209 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a primary
7210 virtual base. If it is not the same primary in the hierarchy of T,
7211 we'll need to generate a ctor vtable for it, to place at its
7212 location in T. If it is the same primary, we still need a VTT
7213 entry for the vtable, but that must be the base it is a
7214 primary for within the sub-hierarchy of RTTI_BINFO. */
7215 tree parent;
7216 tree best_primary = NULL;
7217 tree primary_for;
7219 my_friendly_assert (BINFO_PRIMARY_P (binfo), 20010131);
7221 for (primary_for = BINFO_PRIMARY_BASE_OF (binfo);
7222 primary_for;
7223 primary_for = BINFO_PRIMARY_BASE_OF (primary_for))
7225 for (parent = primary_for;
7226 parent;
7227 parent = BINFO_INHERITANCE_CHAIN (parent))
7229 if (parent == rtti_binfo)
7231 best_primary = primary_for;
7232 break;
7235 if (!parent)
7236 break;
7238 if (best_primary)
7240 vtbl = BINFO_VTABLE (best_primary);
7241 if (TREE_CODE (vtbl) == TREE_LIST)
7243 my_friendly_assert (TREE_PURPOSE (vtbl) == rtti_binfo,
7244 20010126);
7245 vtbl = TREE_VALUE (vtbl);
7249 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7250 return inits;
7252 if (!vtbl)
7254 tree index;
7255 int non_fn_entries;
7257 /* Compute the initializer for this vtable. */
7258 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7259 &non_fn_entries);
7261 /* Figure out the position to which the VPTR should point. */
7262 vtbl = TREE_PURPOSE (l);
7263 vtbl = build1 (ADDR_EXPR,
7264 vtbl_ptr_type_node,
7265 vtbl);
7266 index = size_binop (PLUS_EXPR,
7267 size_int (non_fn_entries),
7268 size_int (list_length (TREE_VALUE (l))));
7269 index = size_binop (MULT_EXPR,
7270 TYPE_SIZE_UNIT (vtable_entry_type),
7271 index);
7272 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7273 TREE_CONSTANT (vtbl) = 1;
7276 if (!ctor_vtbl_p)
7278 /* For an ordinary vtable, set BINFO_VTABLE. */
7279 BINFO_VTABLE (binfo) = vtbl;
7280 if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7281 inits = NULL_TREE;
7283 else
7284 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7285 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7286 straighten this out. */
7287 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7289 return inits;
7292 /* Construct the initializer for BINFOs virtual function table. BINFO
7293 is part of the hierarchy dominated by T. If we're building a
7294 construction vtable, the ORIG_BINFO is the binfo we should use to
7295 find the actual function pointers to put in the vtable - but they
7296 can be overridden on the path to most-derived in the graph that
7297 ORIG_BINFO belongs. Otherwise,
7298 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7299 BINFO that should be indicated by the RTTI information in the
7300 vtable; it will be a base class of T, rather than T itself, if we
7301 are building a construction vtable.
7303 The value returned is a TREE_LIST suitable for wrapping in a
7304 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7305 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7306 number of non-function entries in the vtable.
7308 It might seem that this function should never be called with a
7309 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7310 base is always subsumed by a derived class vtable. However, when
7311 we are building construction vtables, we do build vtables for
7312 primary bases; we need these while the primary base is being
7313 constructed. */
7315 static tree
7316 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7317 tree binfo;
7318 tree orig_binfo;
7319 tree t;
7320 tree rtti_binfo;
7321 int *non_fn_entries_p;
7323 tree v;
7324 tree vfun_inits;
7325 tree vbase;
7326 vtbl_init_data vid;
7328 /* Initialize VID. */
7329 memset (&vid, 0, sizeof (vid));
7330 vid.binfo = binfo;
7331 vid.derived = t;
7332 vid.last_init = &vid.inits;
7333 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7334 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7335 /* The first vbase or vcall offset is at index -3 in the vtable. */
7336 vid.index = ssize_int (-3);
7338 /* Add entries to the vtable for RTTI. */
7339 build_rtti_vtbl_entries (binfo, rtti_binfo, &vid);
7341 /* Create an array for keeping track of the functions we've
7342 processed. When we see multiple functions with the same
7343 signature, we share the vcall offsets. */
7344 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7345 /* Add the vcall and vbase offset entries. */
7346 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7347 /* Clean up. */
7348 VARRAY_FREE (vid.fns);
7349 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7350 build_vbase_offset_vtbl_entries. */
7351 for (vbase = CLASSTYPE_VBASECLASSES (t);
7352 vbase;
7353 vbase = TREE_CHAIN (vbase))
7354 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7356 if (non_fn_entries_p)
7357 *non_fn_entries_p = list_length (vid.inits);
7359 /* Go through all the ordinary virtual functions, building up
7360 initializers. */
7361 vfun_inits = NULL_TREE;
7362 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7364 tree delta;
7365 tree vcall_index;
7366 tree fn;
7367 tree pfn;
7368 tree init;
7370 /* Pull the offset for `this', and the function to call, out of
7371 the list. */
7372 delta = BV_DELTA (v);
7374 if (BV_USE_VCALL_INDEX_P (v))
7376 vcall_index = BV_VCALL_INDEX (v);
7377 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
7379 else
7380 vcall_index = NULL_TREE;
7382 fn = BV_FN (v);
7383 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7384 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7386 /* You can't call an abstract virtual function; it's abstract.
7387 So, we replace these functions with __pure_virtual. */
7388 if (DECL_PURE_VIRTUAL_P (fn))
7389 fn = abort_fndecl;
7391 /* Take the address of the function, considering it to be of an
7392 appropriate generic type. */
7393 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7394 /* The address of a function can't change. */
7395 TREE_CONSTANT (pfn) = 1;
7396 /* Enter it in the vtable. */
7397 init = build_vtable_entry (delta, vcall_index, pfn,
7398 BV_GENERATE_THUNK_WITH_VTABLE_P (v));
7399 /* And add it to the chain of initializers. */
7400 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7403 /* The initializers for virtual functions were built up in reverse
7404 order; straighten them out now. */
7405 vfun_inits = nreverse (vfun_inits);
7407 /* The negative offset initializers are also in reverse order. */
7408 vid.inits = nreverse (vid.inits);
7410 /* Chain the two together. */
7411 return chainon (vid.inits, vfun_inits);
7414 /* Sets vid->inits to be the initializers for the vbase and vcall
7415 offsets in BINFO, which is in the hierarchy dominated by T. */
7417 static void
7418 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7419 tree binfo;
7420 vtbl_init_data *vid;
7422 tree b;
7424 /* If this is a derived class, we must first create entries
7425 corresponding to the primary base class. */
7426 b = get_primary_binfo (binfo);
7427 if (b)
7428 build_vcall_and_vbase_vtbl_entries (b, vid);
7430 /* Add the vbase entries for this base. */
7431 build_vbase_offset_vtbl_entries (binfo, vid);
7432 /* Add the vcall entries for this base. */
7433 build_vcall_offset_vtbl_entries (binfo, vid);
7436 /* Returns the initializers for the vbase offset entries in the vtable
7437 for BINFO (which is part of the class hierarchy dominated by T), in
7438 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7439 where the next vbase offset will go. */
7441 static void
7442 build_vbase_offset_vtbl_entries (binfo, vid)
7443 tree binfo;
7444 vtbl_init_data *vid;
7446 tree vbase;
7447 tree t;
7449 /* Under the old ABI, pointers to virtual bases are stored in each
7450 object. */
7451 if (!vbase_offsets_in_vtable_p ())
7452 return;
7454 /* If there are no virtual baseclasses, then there is nothing to
7455 do. */
7456 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7457 return;
7459 t = vid->derived;
7461 /* Go through the virtual bases, adding the offsets. */
7462 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7463 vbase;
7464 vbase = TREE_CHAIN (vbase))
7466 tree b;
7467 tree delta;
7469 if (!TREE_VIA_VIRTUAL (vbase))
7470 continue;
7472 /* Find the instance of this virtual base in the complete
7473 object. */
7474 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7476 /* If we've already got an offset for this virtual base, we
7477 don't need another one. */
7478 if (BINFO_VTABLE_PATH_MARKED (b))
7479 continue;
7480 SET_BINFO_VTABLE_PATH_MARKED (b);
7482 /* Figure out where we can find this vbase offset. */
7483 delta = size_binop (MULT_EXPR,
7484 vid->index,
7485 convert (ssizetype,
7486 TYPE_SIZE_UNIT (vtable_entry_type)));
7487 if (vid->primary_vtbl_p)
7488 BINFO_VPTR_FIELD (b) = delta;
7490 if (binfo != TYPE_BINFO (t))
7492 tree orig_vbase;
7494 /* Find the instance of this virtual base in the type of BINFO. */
7495 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7496 BINFO_TYPE (binfo));
7498 /* The vbase offset had better be the same. */
7499 if (!tree_int_cst_equal (delta,
7500 BINFO_VPTR_FIELD (orig_vbase)))
7501 my_friendly_abort (20000403);
7504 /* The next vbase will come at a more negative offset. */
7505 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7507 /* The initializer is the delta from BINFO to this virtual base.
7508 The vbase offsets go in reverse inheritance-graph order, and
7509 we are walking in inheritance graph order so these end up in
7510 the right order. */
7511 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (binfo));
7512 *vid->last_init
7513 = build_tree_list (NULL_TREE,
7514 fold (build1 (NOP_EXPR,
7515 vtable_entry_type,
7516 delta)));
7517 vid->last_init = &TREE_CHAIN (*vid->last_init);
7521 /* Adds the initializers for the vcall offset entries in the vtable
7522 for BINFO (which is part of the class hierarchy dominated by T) to
7523 VID->INITS. */
7525 static void
7526 build_vcall_offset_vtbl_entries (binfo, vid)
7527 tree binfo;
7528 vtbl_init_data *vid;
7530 /* Under the old ABI, the adjustments to the `this' pointer were made
7531 elsewhere. */
7532 if (!vcall_offsets_in_vtable_p ())
7533 return;
7535 /* We only need these entries if this base is a virtual base. */
7536 if (!TREE_VIA_VIRTUAL (binfo))
7537 return;
7539 /* We need a vcall offset for each of the virtual functions in this
7540 vtable. For example:
7542 class A { virtual void f (); };
7543 class B : virtual public A { };
7544 class C: virtual public A, public B {};
7546 Now imagine:
7548 B* b = new C;
7549 b->f();
7551 The location of `A' is not at a fixed offset relative to `B'; the
7552 offset depends on the complete object derived from `B'. So,
7553 `B' vtable contains an entry for `f' that indicates by what
7554 amount the `this' pointer for `B' needs to be adjusted to arrive
7555 at `A'.
7557 We need entries for all the functions in our primary vtable and
7558 in our non-virtual bases vtables. */
7559 vid->vbase = binfo;
7560 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7561 add_vcall_offset_vtbl_entries_r (binfo, vid);
7564 /* Build vcall offsets, starting with those for BINFO. */
7566 static void
7567 add_vcall_offset_vtbl_entries_r (binfo, vid)
7568 tree binfo;
7569 vtbl_init_data *vid;
7571 int i;
7572 tree primary_binfo;
7574 /* Don't walk into virtual bases -- except, of course, for the
7575 virtual base for which we are building vcall offsets. */
7576 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7577 return;
7579 /* If BINFO has a primary base, process it first. */
7580 primary_binfo = get_primary_binfo (binfo);
7581 if (primary_binfo)
7582 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7584 /* Add BINFO itself to the list. */
7585 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7587 /* Scan the non-primary bases of BINFO. */
7588 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7590 tree base_binfo;
7592 base_binfo = BINFO_BASETYPE (binfo, i);
7593 if (base_binfo != primary_binfo)
7594 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7598 /* Called from build_vcall_offset_vtbl_entries_r. */
7600 static void
7601 add_vcall_offset_vtbl_entries_1 (binfo, vid)
7602 tree binfo;
7603 vtbl_init_data* vid;
7605 tree derived_virtuals;
7606 tree base_virtuals;
7607 tree orig_virtuals;
7608 tree binfo_inits;
7609 /* If BINFO is a primary base, this is the least derived class of
7610 BINFO that is not a primary base. */
7611 tree non_primary_binfo;
7613 binfo_inits = NULL_TREE;
7615 /* We might be a primary base class. Go up the inheritance
7616 hierarchy until we find the class of which we are a primary base:
7617 it is the BINFO_VIRTUALS there that we need to consider. */
7618 non_primary_binfo = binfo;
7619 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7621 tree b;
7623 /* If we have reached a virtual base, then it must be the
7624 virtual base for which we are building vcall offsets. In
7625 turn, the virtual base must be a (possibly indirect) primary
7626 base of the class that we are initializing, or we wouldn't
7627 care about its vtable offsets. */
7628 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7630 non_primary_binfo = vid->binfo;
7631 break;
7634 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7635 if (get_primary_binfo (b) != non_primary_binfo)
7636 break;
7637 non_primary_binfo = b;
7640 /* Make entries for the rest of the virtuals. */
7641 for (base_virtuals = BINFO_VIRTUALS (binfo),
7642 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7643 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7644 base_virtuals;
7645 base_virtuals = TREE_CHAIN (base_virtuals),
7646 derived_virtuals = TREE_CHAIN (derived_virtuals),
7647 orig_virtuals = TREE_CHAIN (orig_virtuals))
7649 tree orig_fn;
7650 tree fn;
7651 tree base;
7652 tree base_binfo;
7653 size_t i;
7655 /* Find the declaration that originally caused this function to
7656 be present. */
7657 orig_fn = BV_FN (orig_virtuals);
7659 /* We do not need an entry if this function is declared in a
7660 virtual base (or one of its virtual bases), and not
7661 overridden in the section of the hierarchy dominated by the
7662 virtual base for which we are building vcall offsets. */
7663 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7664 continue;
7666 /* Find the overriding function. */
7667 fn = BV_FN (derived_virtuals);
7669 /* If there is already an entry for a function with the same
7670 signature as FN, then we do not need a second vcall offset.
7671 Check the list of functions already present in the derived
7672 class vtable. */
7673 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7675 tree derived_entry;
7677 derived_entry = VARRAY_TREE (vid->fns, i);
7678 if (same_signature_p (BV_FN (derived_entry), fn))
7680 BV_VCALL_INDEX (derived_virtuals)
7681 = BV_VCALL_INDEX (derived_entry);
7682 break;
7685 if (i != VARRAY_ACTIVE_SIZE (vid->fns))
7686 continue;
7688 /* The FN comes from BASE. So, we must calculate the adjustment
7689 from the virtual base that derived from BINFO to BASE. */
7690 base = DECL_CONTEXT (fn);
7691 base_binfo = get_binfo (base, vid->derived, /*protect=*/0);
7693 /* Compute the vcall offset. */
7694 *vid->last_init
7695 = (build_tree_list
7696 (NULL_TREE,
7697 fold (build1 (NOP_EXPR, vtable_entry_type,
7698 size_diffop (BINFO_OFFSET (base_binfo),
7699 BINFO_OFFSET (vid->vbase))))));
7700 vid->last_init = &TREE_CHAIN (*vid->last_init);
7702 /* Keep track of the vtable index where this vcall offset can be
7703 found. For a construction vtable, we already made this
7704 annotation when we build the original vtable. */
7705 if (!vid->ctor_vtbl_p)
7706 BV_VCALL_INDEX (derived_virtuals) = vid->index;
7708 /* The next vcall offset will be found at a more negative
7709 offset. */
7710 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7712 /* Keep track of this function. */
7713 VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
7717 /* Return vtbl initializers for the RTTI entries coresponding to the
7718 BINFO's vtable. The RTTI entries should indicate the object given
7719 by RTTI_BINFO. */
7721 static void
7722 build_rtti_vtbl_entries (binfo, rtti_binfo, vid)
7723 tree binfo;
7724 tree rtti_binfo;
7725 vtbl_init_data *vid;
7727 tree b;
7728 tree t;
7729 tree basetype;
7730 tree offset;
7731 tree decl;
7732 tree init;
7734 basetype = BINFO_TYPE (binfo);
7735 t = BINFO_TYPE (rtti_binfo);
7737 /* For a COM object there is no RTTI entry. */
7738 if (CLASSTYPE_COM_INTERFACE (basetype))
7739 return;
7741 /* To find the complete object, we will first convert to our most
7742 primary base, and then add the offset in the vtbl to that value. */
7743 b = binfo;
7744 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
7745 && !BINFO_LOST_PRIMARY_P (b))
7747 tree primary_base;
7749 primary_base = get_primary_binfo (b);
7750 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
7751 b = primary_base;
7753 offset = size_diffop (BINFO_OFFSET (rtti_binfo), BINFO_OFFSET (b));
7755 /* The second entry is, in the case of the new ABI, the address of
7756 the typeinfo object, or, in the case of the old ABI, a function
7757 which returns a typeinfo object. */
7758 if (new_abi_rtti_p ())
7760 if (flag_rtti)
7761 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
7762 else
7763 decl = integer_zero_node;
7765 /* Convert the declaration to a type that can be stored in the
7766 vtable. */
7767 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
7768 TREE_CONSTANT (init) = 1;
7770 else
7772 if (flag_rtti)
7773 decl = get_tinfo_decl (t);
7774 else
7775 decl = abort_fndecl;
7777 /* Convert the declaration to a type that can be stored in the
7778 vtable. */
7779 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, decl);
7780 TREE_CONSTANT (init) = 1;
7781 init = build_vtable_entry (offset, NULL_TREE, init,
7782 /*generate_with_vtable_p=*/0);
7784 *vid->last_init = build_tree_list (NULL_TREE, init);
7785 vid->last_init = &TREE_CHAIN (*vid->last_init);
7787 /* Add the offset-to-top entry. It comes earlier in the vtable that
7788 the the typeinfo entry. */
7789 if (flag_vtable_thunks)
7791 /* Convert the offset to look like a function pointer, so that
7792 we can put it in the vtable. */
7793 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
7794 TREE_CONSTANT (init) = 1;
7795 *vid->last_init = build_tree_list (NULL_TREE, init);
7796 vid->last_init = &TREE_CHAIN (*vid->last_init);
7800 /* Build an entry in the virtual function table. DELTA is the offset
7801 for the `this' pointer. VCALL_INDEX is the vtable index containing
7802 the vcall offset; zero if none. ENTRY is the virtual function
7803 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
7804 but it may not actually be a virtual function table pointer. (For
7805 example, it might be the address of the RTTI object, under the new
7806 ABI.) */
7808 static tree
7809 build_vtable_entry (delta, vcall_index, entry, generate_with_vtable_p)
7810 tree delta;
7811 tree vcall_index;
7812 tree entry;
7813 int generate_with_vtable_p;
7815 if (flag_vtable_thunks)
7817 tree fn;
7819 fn = TREE_OPERAND (entry, 0);
7820 if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
7821 && fn != abort_fndecl
7822 && !DECL_TINFO_FN_P (fn))
7824 entry = make_thunk (entry, delta, vcall_index,
7825 generate_with_vtable_p);
7826 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
7827 TREE_READONLY (entry) = 1;
7828 TREE_CONSTANT (entry) = 1;
7830 #ifdef GATHER_STATISTICS
7831 n_vtable_entries += 1;
7832 #endif
7833 return entry;
7835 else
7837 tree elems = tree_cons (NULL_TREE, delta,
7838 tree_cons (NULL_TREE, integer_zero_node,
7839 build_tree_list (NULL_TREE, entry)));
7840 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
7842 /* We don't use vcall offsets when not using vtable thunks. */
7843 my_friendly_assert (vcall_index == NULL_TREE, 20000125);
7845 /* DELTA used to be constructed by `size_int' and/or size_binop,
7846 which caused overflow problems when it was negative. That should
7847 be fixed now. */
7849 if (! int_fits_type_p (delta, delta_type_node))
7851 if (flag_huge_objects)
7852 sorry ("object size exceeds built-in limit for virtual function table implementation");
7853 else
7854 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
7857 TREE_CONSTANT (entry) = 1;
7858 TREE_STATIC (entry) = 1;
7859 TREE_READONLY (entry) = 1;
7861 #ifdef GATHER_STATISTICS
7862 n_vtable_entries += 1;
7863 #endif
7865 return entry;