Merge from mainline
[official-gcc.git] / gcc / cp / class.c
blob5716ee5e3f32062f8bec3eb62c4a0a6b544e72e4
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 type of the most-derived type. */
70 tree derived;
71 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
72 unless ctor_vtbl_p is true. */
73 tree rtti_binfo;
74 /* The negative-index vtable initializers built up so far. These
75 are in order from least negative index to most negative index. */
76 tree inits;
77 /* The last (i.e., most negative) entry in INITS. */
78 tree* last_init;
79 /* The binfo for the virtual base for which we're building
80 vcall offset initializers. */
81 tree vbase;
82 /* The functions in vbase for which we have already provided vcall
83 offsets. */
84 varray_type fns;
85 /* The vtable index of the next vcall or vbase offset. */
86 tree index;
87 /* Nonzero if we are building the initializer for the primary
88 vtable. */
89 int primary_vtbl_p;
90 /* Nonzero if we are building the initializer for a construction
91 vtable. */
92 int ctor_vtbl_p;
93 } vtbl_init_data;
95 /* The type of a function passed to walk_subobject_offsets. */
96 typedef int (*subobject_offset_fn) PARAMS ((tree, tree, splay_tree));
98 /* The stack itself. This is an dynamically resized array. The
99 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
100 static int current_class_stack_size;
101 static class_stack_node_t current_class_stack;
103 /* An array of all local classes present in this translation unit, in
104 declaration order. */
105 varray_type local_classes;
107 static tree get_vfield_name PARAMS ((tree));
108 static void finish_struct_anon PARAMS ((tree));
109 static tree build_vbase_pointer PARAMS ((tree, tree));
110 static tree build_vtable_entry PARAMS ((tree, tree, tree));
111 static tree get_vtable_name PARAMS ((tree));
112 static tree get_basefndecls PARAMS ((tree, tree));
113 static int build_primary_vtable PARAMS ((tree, tree));
114 static int build_secondary_vtable PARAMS ((tree, tree));
115 static void finish_vtbls PARAMS ((tree));
116 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
117 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
118 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
119 static void delete_duplicate_fields PARAMS ((tree));
120 static void finish_struct_bits PARAMS ((tree));
121 static int alter_access PARAMS ((tree, tree, tree));
122 static void handle_using_decl 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 *, 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));
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 bool build_base_field PARAMS ((record_layout_info, tree, int *,
146 splay_tree, tree));
147 static bool build_base_fields PARAMS ((record_layout_info, int *,
148 splay_tree, tree));
149 static void check_methods PARAMS ((tree));
150 static void remove_zero_width_bit_fields PARAMS ((tree));
151 static void check_bases PARAMS ((tree, int *, int *, int *));
152 static void check_bases_and_members PARAMS ((tree, int *));
153 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
154 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
155 static void fixup_pending_inline PARAMS ((tree));
156 static void fixup_inline_methods PARAMS ((tree));
157 static void set_primary_base PARAMS ((tree, tree, int *));
158 static void propagate_binfo_offsets PARAMS ((tree, tree, tree));
159 static void layout_virtual_bases PARAMS ((tree, splay_tree));
160 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
161 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
162 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
163 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
164 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
165 static void layout_vtable_decl PARAMS ((tree, int));
166 static tree dfs_find_final_overrider PARAMS ((tree, void *));
167 static tree find_final_overrider PARAMS ((tree, tree, tree));
168 static int make_new_vtable PARAMS ((tree, tree));
169 static int maybe_indent_hierarchy PARAMS ((FILE *, int, int));
170 static void dump_class_hierarchy_r PARAMS ((FILE *, int, tree, tree, int));
171 static void dump_class_hierarchy PARAMS ((tree));
172 static void dump_array PARAMS ((FILE *, tree));
173 static void dump_vtable PARAMS ((tree, tree, tree));
174 static void dump_vtt PARAMS ((tree, tree));
175 static tree build_vtable PARAMS ((tree, tree, tree));
176 static void initialize_vtable PARAMS ((tree, tree));
177 static void initialize_array PARAMS ((tree, tree));
178 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
179 tree, tree,
180 splay_tree, tree));
181 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
182 static bool layout_empty_base PARAMS ((tree, tree, splay_tree, tree));
183 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
184 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
185 tree));
186 static void set_vindex PARAMS ((tree, int *));
187 static void build_rtti_vtbl_entries PARAMS ((tree, vtbl_init_data *));
188 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
189 vtbl_init_data *));
190 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
191 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
192 static tree dfs_unshared_virtual_bases PARAMS ((tree, void *));
193 static void mark_primary_bases PARAMS ((tree));
194 static tree mark_primary_virtual_base PARAMS ((tree, tree, tree));
195 static void clone_constructors_and_destructors PARAMS ((tree));
196 static tree build_clone PARAMS ((tree, tree));
197 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
198 static tree copy_virtuals PARAMS ((tree));
199 static void build_ctor_vtbl_group PARAMS ((tree, tree));
200 static void build_vtt PARAMS ((tree));
201 static tree binfo_ctor_vtable PARAMS ((tree));
202 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
203 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
204 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
205 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
206 static tree get_original_base PARAMS ((tree, tree));
207 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
208 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
209 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
210 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
211 tree, splay_tree, tree, int));
212 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
213 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
214 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
215 splay_tree_key k2));
216 static void warn_about_ambiguous_direct_bases PARAMS ((tree));
217 static bool type_requires_array_cookie PARAMS ((tree));
218 static void check_destructor_triviality PARAMS ((tree));
220 /* Macros for dfs walking during vtt construction. See
221 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
222 and dfs_fixup_binfo_vtbls. */
223 #define VTT_TOP_LEVEL_P(node) TREE_UNSIGNED(node)
224 #define VTT_MARKED_BINFO_P(node) TREE_USED(node)
226 /* Variables shared between class.c and call.c. */
228 #ifdef GATHER_STATISTICS
229 int n_vtables = 0;
230 int n_vtable_entries = 0;
231 int n_vtable_searches = 0;
232 int n_vtable_elems = 0;
233 int n_convert_harshness = 0;
234 int n_compute_conversion_costs = 0;
235 int n_build_method_call = 0;
236 int n_inner_fields_searched = 0;
237 #endif
239 /* Virtual base class layout. */
241 /* Returns a pointer to the virtual base class of EXP that has the
242 indicated TYPE. EXP is of class type, not a pointer type. */
244 static tree
245 build_vbase_pointer (exp, type)
246 tree exp, type;
248 tree vbase;
249 tree vbase_ptr;
251 /* Find the shared copy of TYPE; that's where the vtable offset is
252 recorded. */
253 vbase = binfo_for_vbase (type, TREE_TYPE (exp));
254 /* Find the virtual function table pointer. */
255 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
256 /* Compute the location where the offset will lie. */
257 vbase_ptr = build (PLUS_EXPR,
258 TREE_TYPE (vbase_ptr),
259 vbase_ptr,
260 BINFO_VPTR_FIELD (vbase));
261 vbase_ptr = build1 (NOP_EXPR,
262 build_pointer_type (ptrdiff_type_node),
263 vbase_ptr);
264 /* Add the contents of this location to EXP. */
265 return build (PLUS_EXPR,
266 build_pointer_type (type),
267 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
268 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
271 /* Build multi-level access to EXPR using hierarchy path PATH.
272 CODE is PLUS_EXPR if we are going with the grain,
273 and MINUS_EXPR if we are not (in which case, we cannot traverse
274 virtual baseclass links).
276 TYPE is the type we want this path to have on exit.
278 NONNULL is non-zero if we know (for any reason) that EXPR is
279 not, in fact, zero. */
281 tree
282 build_vbase_path (code, type, expr, path, nonnull)
283 enum tree_code code;
284 tree type, expr, path;
285 int nonnull;
287 register int changed = 0;
288 tree last = NULL_TREE, last_virtual = NULL_TREE;
289 int fixed_type_p;
290 tree null_expr = 0, nonnull_expr;
291 tree basetype;
292 tree offset = integer_zero_node;
294 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
295 return build1 (NOP_EXPR, type, expr);
297 /* We could do better if we had additional logic to convert back to the
298 unconverted type (the static type of the complete object), and then
299 convert back to the type we want. Until that is done, we only optimize
300 if the complete type is the same type as expr has. */
301 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
302 if (fixed_type_p < 0)
303 /* Virtual base layout is not fixed, even in ctors and dtors. */
304 fixed_type_p = 0;
306 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
307 expr = save_expr (expr);
308 nonnull_expr = expr;
310 path = reverse_path (path);
312 basetype = BINFO_TYPE (path);
314 while (path)
316 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
318 last_virtual = BINFO_TYPE (TREE_VALUE (path));
319 if (code == PLUS_EXPR)
321 changed = ! fixed_type_p;
323 if (changed)
325 tree ind;
327 /* We already check for ambiguous things in the caller, just
328 find a path. */
329 if (last)
331 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
332 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
334 ind = build_indirect_ref (nonnull_expr, NULL);
335 nonnull_expr = build_vbase_pointer (ind, last_virtual);
336 if (nonnull == 0
337 && TREE_CODE (type) == POINTER_TYPE
338 && null_expr == NULL_TREE)
340 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
341 expr = build (COND_EXPR, build_pointer_type (last_virtual),
342 build (EQ_EXPR, boolean_type_node, expr,
343 integer_zero_node),
344 null_expr, nonnull_expr);
347 /* else we'll figure out the offset below. */
349 /* Happens in the case of parse errors. */
350 if (nonnull_expr == error_mark_node)
351 return error_mark_node;
353 else
355 cp_error ("cannot cast up from virtual baseclass `%T'",
356 last_virtual);
357 return error_mark_node;
360 last = TREE_VALUE (path);
361 path = TREE_CHAIN (path);
363 /* LAST is now the last basetype assoc on the path. */
365 /* A pointer to a virtual base member of a non-null object
366 is non-null. Therefore, we only need to test for zeroness once.
367 Make EXPR the canonical expression to deal with here. */
368 if (null_expr)
370 TREE_OPERAND (expr, 2) = nonnull_expr;
371 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
372 = TREE_TYPE (nonnull_expr);
374 else
375 expr = nonnull_expr;
377 /* If we go through any virtual base pointers, make sure that
378 casts to BASETYPE from the last virtual base class use
379 the right value for BASETYPE. */
380 if (changed)
382 tree intype = TREE_TYPE (TREE_TYPE (expr));
384 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
385 offset
386 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
388 else
389 offset = BINFO_OFFSET (last);
391 if (! integer_zerop (offset))
393 /* Bash types to make the backend happy. */
394 offset = cp_convert (type, offset);
396 /* If expr might be 0, we need to preserve that zeroness. */
397 if (nonnull == 0)
399 if (null_expr)
400 TREE_TYPE (null_expr) = type;
401 else
402 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
403 if (TREE_SIDE_EFFECTS (expr))
404 expr = save_expr (expr);
406 return build (COND_EXPR, type,
407 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
408 null_expr,
409 build (code, type, expr, offset));
411 else return build (code, type, expr, offset);
414 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
415 be used multiple times in initialization of multiple inheritance. */
416 if (null_expr)
418 TREE_TYPE (expr) = type;
419 return expr;
421 else
422 return build1 (NOP_EXPR, type, expr);
426 /* Virtual function things. */
428 /* We want to give the assembler the vtable identifier as well as
429 the offset to the function pointer. So we generate
431 __asm__ __volatile__ (".vtable_entry %c0, %c1"
432 : : "s"(&class_vtable),
433 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
435 static void
436 build_vtable_entry_ref (basetype, idx)
437 tree basetype, idx;
439 static char asm_stmt[] = ".vtable_entry %c0, %c1";
440 tree s, i, i2;
441 tree vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
442 tree first_fn = TYPE_BINFO_VTABLE (basetype);
444 s = build_unary_op (ADDR_EXPR, vtable, 0);
445 s = build_tree_list (build_string (1, "s"), s);
447 i = build_array_ref (first_fn, idx);
448 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
449 i2 = build_array_ref (vtable, build_int_2 (0,0));
450 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
451 i = cp_build_binary_op (MINUS_EXPR, i, i2);
452 i = build_tree_list (build_string (1, "i"), i);
454 finish_asm_stmt (ridpointers[RID_VOLATILE],
455 build_string (sizeof(asm_stmt)-1, asm_stmt),
456 NULL_TREE, chainon (s, i), NULL_TREE);
459 /* Given an object INSTANCE, return an expression which yields the
460 virtual function vtable element corresponding to INDEX. There are
461 many special cases for INSTANCE which we take care of here, mainly
462 to avoid creating extra tree nodes when we don't have to. */
464 tree
465 build_vtbl_ref (instance, idx)
466 tree instance, idx;
468 tree vtbl, aref;
469 tree basetype = TREE_TYPE (instance);
471 if (TREE_CODE (basetype) == REFERENCE_TYPE)
472 basetype = TREE_TYPE (basetype);
474 if (instance == current_class_ref)
475 vtbl = build_vfield_ref (instance, basetype);
476 else
478 if (optimize)
480 /* Try to figure out what a reference refers to, and
481 access its virtual function table directly. */
482 tree ref = NULL_TREE;
484 if (TREE_CODE (instance) == INDIRECT_REF
485 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
486 ref = TREE_OPERAND (instance, 0);
487 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
488 ref = instance;
490 if (ref && TREE_CODE (ref) == VAR_DECL
491 && DECL_INITIAL (ref))
493 tree init = DECL_INITIAL (ref);
495 while (TREE_CODE (init) == NOP_EXPR
496 || TREE_CODE (init) == NON_LVALUE_EXPR)
497 init = TREE_OPERAND (init, 0);
498 if (TREE_CODE (init) == ADDR_EXPR)
500 init = TREE_OPERAND (init, 0);
501 if (IS_AGGR_TYPE (TREE_TYPE (init))
502 && (TREE_CODE (init) == PARM_DECL
503 || TREE_CODE (init) == VAR_DECL))
504 instance = init;
509 if (IS_AGGR_TYPE (TREE_TYPE (instance))
510 && (TREE_CODE (instance) == RESULT_DECL
511 || TREE_CODE (instance) == PARM_DECL
512 || TREE_CODE (instance) == VAR_DECL))
514 vtbl = TYPE_BINFO_VTABLE (basetype);
515 /* Knowing the dynamic type of INSTANCE we can easily obtain
516 the correct vtable entry. We resolve this back to be in
517 terms of the primary vtable. */
518 if (TREE_CODE (vtbl) == PLUS_EXPR)
520 idx = fold (build (PLUS_EXPR,
521 TREE_TYPE (idx),
522 idx,
523 build (EXACT_DIV_EXPR,
524 TREE_TYPE (idx),
525 TREE_OPERAND (vtbl, 1),
526 TYPE_SIZE_UNIT (vtable_entry_type))));
527 vtbl = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
530 else
531 vtbl = build_vfield_ref (instance, basetype);
534 assemble_external (vtbl);
536 if (flag_vtable_gc)
537 build_vtable_entry_ref (basetype, idx);
539 aref = build_array_ref (vtbl, idx);
541 return aref;
544 /* Given an object INSTANCE, return an expression which yields the
545 virtual function corresponding to IDX. */
547 tree
548 build_vfn_ref (instance, idx)
549 tree instance;
550 tree idx;
552 tree aref = build_vtbl_ref (instance, idx);
554 return aref;
557 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
558 for the given TYPE. */
560 static tree
561 get_vtable_name (type)
562 tree type;
564 return mangle_vtbl_for_type (type);
567 /* Return an IDENTIFIER_NODE for the name of the virtual table table
568 for TYPE. */
570 tree
571 get_vtt_name (type)
572 tree type;
574 return mangle_vtt_for_type (type);
577 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
578 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
579 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
581 static tree
582 build_vtable (class_type, name, vtable_type)
583 tree class_type;
584 tree name;
585 tree vtable_type;
587 tree decl;
589 decl = build_lang_decl (VAR_DECL, name, vtable_type);
590 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
591 now to avoid confusion in mangle_decl. */
592 SET_DECL_ASSEMBLER_NAME (decl, name);
593 DECL_CONTEXT (decl) = class_type;
594 DECL_ARTIFICIAL (decl) = 1;
595 TREE_STATIC (decl) = 1;
596 TREE_READONLY (decl) = 1;
597 DECL_VIRTUAL_P (decl) = 1;
598 import_export_vtable (decl, class_type, 0);
600 return decl;
603 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
604 or even complete. If this does not exist, create it. If COMPLETE is
605 non-zero, then complete the definition of it -- that will render it
606 impossible to actually build the vtable, but is useful to get at those
607 which are known to exist in the runtime. */
609 tree
610 get_vtable_decl (type, complete)
611 tree type;
612 int complete;
614 tree name = get_vtable_name (type);
615 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
617 if (decl)
619 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
620 && DECL_VIRTUAL_P (decl), 20000118);
621 return decl;
624 decl = build_vtable (type, name, void_type_node);
625 decl = pushdecl_top_level (decl);
626 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
627 20000517);
629 /* At one time the vtable info was grabbed 2 words at a time. This
630 fails on sparc unless you have 8-byte alignment. (tiemann) */
631 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
632 DECL_ALIGN (decl));
634 if (complete)
636 DECL_EXTERNAL (decl) = 1;
637 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
640 return decl;
643 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
644 BV_VCALL_INDEX for each entry is cleared. */
646 static tree
647 copy_virtuals (binfo)
648 tree binfo;
650 tree copies;
651 tree t;
653 copies = copy_list (BINFO_VIRTUALS (binfo));
654 for (t = copies; t; t = TREE_CHAIN (t))
656 BV_VCALL_INDEX (t) = NULL_TREE;
657 BV_USE_VCALL_INDEX_P (t) = 0;
660 return copies;
663 /* Build the primary virtual function table for TYPE. If BINFO is
664 non-NULL, build the vtable starting with the initial approximation
665 that it is the same as the one which is the head of the association
666 list. Returns a non-zero value if a new vtable is actually
667 created. */
669 static int
670 build_primary_vtable (binfo, type)
671 tree binfo, type;
673 tree decl;
674 tree virtuals;
676 decl = get_vtable_decl (type, /*complete=*/0);
678 if (binfo)
680 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
681 /* We have already created a vtable for this base, so there's
682 no need to do it again. */
683 return 0;
685 virtuals = copy_virtuals (binfo);
686 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
687 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
688 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
690 else
692 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
693 20000118);
694 virtuals = NULL_TREE;
697 #ifdef GATHER_STATISTICS
698 n_vtables += 1;
699 n_vtable_elems += list_length (virtuals);
700 #endif
702 /* Initialize the association list for this type, based
703 on our first approximation. */
704 TYPE_BINFO_VTABLE (type) = decl;
705 TYPE_BINFO_VIRTUALS (type) = virtuals;
706 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
707 return 1;
710 /* Give BINFO a new virtual function table which is initialized
711 with a skeleton-copy of its original initialization. The only
712 entry that changes is the `delta' entry, so we can really
713 share a lot of structure.
715 FOR_TYPE is the most derived type which caused this table to
716 be needed.
718 Returns non-zero if we haven't met BINFO before.
720 The order in which vtables are built (by calling this function) for
721 an object must remain the same, otherwise a binary incompatibility
722 can result. */
724 static int
725 build_secondary_vtable (binfo, for_type)
726 tree binfo, for_type;
728 my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
730 if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
731 /* We already created a vtable for this base. There's no need to
732 do it again. */
733 return 0;
735 /* Remember that we've created a vtable for this BINFO, so that we
736 don't try to do so again. */
737 SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
739 /* Make fresh virtual list, so we can smash it later. */
740 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
742 /* Secondary vtables are laid out as part of the same structure as
743 the primary vtable. */
744 BINFO_VTABLE (binfo) = NULL_TREE;
745 return 1;
748 /* Create a new vtable for BINFO which is the hierarchy dominated by
749 T. Return non-zero if we actually created a new vtable. */
751 static int
752 make_new_vtable (t, binfo)
753 tree t;
754 tree binfo;
756 if (binfo == TYPE_BINFO (t))
757 /* In this case, it is *type*'s vtable we are modifying. We start
758 with the approximation that its vtable is that of the
759 immediate base class. */
760 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
761 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
762 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
764 else
765 /* This is our very own copy of `basetype' to play with. Later,
766 we will fill in all the virtual functions that override the
767 virtual functions in these base classes which are not defined
768 by the current type. */
769 return build_secondary_vtable (binfo, t);
772 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
773 (which is in the hierarchy dominated by T) list FNDECL as its
774 BV_FN. DELTA is the required constant adjustment from the `this'
775 pointer where the vtable entry appears to the `this' required when
776 the function is actually called. */
778 static void
779 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
780 tree t;
781 tree binfo;
782 tree fndecl;
783 tree delta;
784 tree *virtuals;
786 tree v;
788 v = *virtuals;
790 if (fndecl != BV_FN (v)
791 || !tree_int_cst_equal (delta, BV_DELTA (v)))
793 tree base_fndecl;
795 /* We need a new vtable for BINFO. */
796 if (make_new_vtable (t, binfo))
798 /* If we really did make a new vtable, we also made a copy
799 of the BINFO_VIRTUALS list. Now, we have to find the
800 corresponding entry in that list. */
801 *virtuals = BINFO_VIRTUALS (binfo);
802 while (BV_FN (*virtuals) != BV_FN (v))
803 *virtuals = TREE_CHAIN (*virtuals);
804 v = *virtuals;
807 base_fndecl = BV_FN (v);
808 BV_DELTA (v) = delta;
809 BV_VCALL_INDEX (v) = NULL_TREE;
810 BV_FN (v) = fndecl;
812 /* Now assign virtual dispatch information, if unset. We can
813 dispatch this through any overridden base function.
815 FIXME this can choose a secondary vtable if the primary is not
816 also lexically first, leading to useless conversions.
817 In the V3 ABI, there's no reason for DECL_VIRTUAL_CONTEXT to
818 ever be different from DECL_CONTEXT. */
819 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
821 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
822 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
827 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
828 functions present in the vtable so far. */
830 static void
831 set_vindex (decl, vfuns_p)
832 tree decl;
833 int *vfuns_p;
835 int vindex;
837 vindex = (*vfuns_p)++;
838 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
841 /* Add a virtual function to all the appropriate vtables for the class
842 T. DECL_VINDEX(X) should be error_mark_node, if we want to
843 allocate a new slot in our table. If it is error_mark_node, we
844 know that no other function from another vtable is overridden by X.
845 VFUNS_P keeps track of how many virtuals there are in our
846 main vtable for the type, and we build upon the NEW_VIRTUALS list
847 and return it. */
849 static void
850 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
851 vfuns_p, fndecl, t)
852 tree *new_virtuals_p;
853 tree *overridden_virtuals_p;
854 int *vfuns_p;
855 tree fndecl;
856 tree t; /* Structure type. */
858 tree new_virtual;
860 /* If this function doesn't override anything from a base class, we
861 can just assign it a new DECL_VINDEX now. Otherwise, if it does
862 override something, we keep it around and assign its DECL_VINDEX
863 later, in modify_all_vtables. */
864 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
865 /* We've already dealt with this function. */
866 return;
868 new_virtual = make_node (TREE_LIST);
869 BV_FN (new_virtual) = fndecl;
870 BV_DELTA (new_virtual) = integer_zero_node;
872 if (DECL_VINDEX (fndecl) == error_mark_node)
874 /* FNDECL is a new virtual function; it doesn't override any
875 virtual function in a base class. */
877 /* We remember that this was the base sub-object for rtti. */
878 CLASSTYPE_RTTI (t) = t;
880 /* Now assign virtual dispatch information. */
881 set_vindex (fndecl, vfuns_p);
882 DECL_VIRTUAL_CONTEXT (fndecl) = t;
884 /* Save the state we've computed on the NEW_VIRTUALS list. */
885 TREE_CHAIN (new_virtual) = *new_virtuals_p;
886 *new_virtuals_p = new_virtual;
888 else
890 /* FNDECL overrides a function from a base class. */
891 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
892 *overridden_virtuals_p = new_virtual;
896 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
897 the method after the class has already been defined because a
898 declaration for it was seen. (Even though that is erroneous, we
899 add the method for improved error recovery.) */
901 void
902 add_method (type, method, error_p)
903 tree type;
904 tree method;
905 int error_p;
907 int using = (DECL_CONTEXT (method) != type);
908 int len;
909 int slot;
910 tree method_vec;
912 if (!CLASSTYPE_METHOD_VEC (type))
913 /* Make a new method vector. We start with 8 entries. We must
914 allocate at least two (for constructors and destructors), and
915 we're going to end up with an assignment operator at some point
916 as well.
918 We could use a TREE_LIST for now, and convert it to a TREE_VEC
919 in finish_struct, but we would probably waste more memory
920 making the links in the list than we would by over-allocating
921 the size of the vector here. Furthermore, we would complicate
922 all the code that expects this to be a vector. */
923 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
925 method_vec = CLASSTYPE_METHOD_VEC (type);
926 len = TREE_VEC_LENGTH (method_vec);
928 /* Constructors and destructors go in special slots. */
929 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
930 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
931 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
932 slot = CLASSTYPE_DESTRUCTOR_SLOT;
933 else
935 /* See if we already have an entry with this name. */
936 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
937 if (!TREE_VEC_ELT (method_vec, slot)
938 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
939 slot)))
940 == DECL_NAME (method)))
941 break;
943 if (slot == len)
945 /* We need a bigger method vector. */
946 int new_len;
947 tree new_vec;
949 /* In the non-error case, we are processing a class
950 definition. Double the size of the vector to give room
951 for new methods. */
952 if (!error_p)
953 new_len = 2 * len;
954 /* In the error case, the vector is already complete. We
955 don't expect many errors, and the rest of the front-end
956 will get confused if there are empty slots in the vector. */
957 else
958 new_len = len + 1;
960 new_vec = make_tree_vec (new_len);
961 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
962 len * sizeof (tree));
963 len = new_len;
964 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
967 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
969 /* Type conversion operators have to come before ordinary
970 methods; add_conversions depends on this to speed up
971 looking for conversion operators. So, if necessary, we
972 slide some of the vector elements up. In theory, this
973 makes this algorithm O(N^2) but we don't expect many
974 conversion operators. */
975 for (slot = 2; slot < len; ++slot)
977 tree fn = TREE_VEC_ELT (method_vec, slot);
979 if (!fn)
980 /* There are no more entries in the vector, so we
981 can insert the new conversion operator here. */
982 break;
984 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
985 /* We can insert the new function right at the
986 SLOTth position. */
987 break;
990 if (!TREE_VEC_ELT (method_vec, slot))
991 /* There is nothing in the Ith slot, so we can avoid
992 moving anything. */
994 else
996 /* We know the last slot in the vector is empty
997 because we know that at this point there's room
998 for a new function. */
999 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
1000 &TREE_VEC_ELT (method_vec, slot),
1001 (len - slot - 1) * sizeof (tree));
1002 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1007 if (template_class_depth (type))
1008 /* TYPE is a template class. Don't issue any errors now; wait
1009 until instantiation time to complain. */
1011 else
1013 tree fns;
1015 /* Check to see if we've already got this method. */
1016 for (fns = TREE_VEC_ELT (method_vec, slot);
1017 fns;
1018 fns = OVL_NEXT (fns))
1020 tree fn = OVL_CURRENT (fns);
1022 if (TREE_CODE (fn) != TREE_CODE (method))
1023 continue;
1025 if (TREE_CODE (method) != TEMPLATE_DECL)
1027 /* [over.load] Member function declarations with the
1028 same name and the same parameter types cannot be
1029 overloaded if any of them is a static member
1030 function declaration. */
1031 if ((DECL_STATIC_FUNCTION_P (fn)
1032 != DECL_STATIC_FUNCTION_P (method))
1033 || using)
1035 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1036 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1038 if (! DECL_STATIC_FUNCTION_P (fn))
1039 parms1 = TREE_CHAIN (parms1);
1040 if (! DECL_STATIC_FUNCTION_P (method))
1041 parms2 = TREE_CHAIN (parms2);
1043 if (compparms (parms1, parms2))
1045 if (using)
1046 /* Defer to the local function. */
1047 return;
1048 else
1049 cp_error ("`%#D' and `%#D' cannot be overloaded",
1050 fn, method);
1055 if (!decls_match (fn, method))
1056 continue;
1058 /* There has already been a declaration of this method
1059 or member template. */
1060 cp_error_at ("`%D' has already been declared in `%T'",
1061 method, type);
1063 /* We don't call duplicate_decls here to merge the
1064 declarations because that will confuse things if the
1065 methods have inline definitions. In particular, we
1066 will crash while processing the definitions. */
1067 return;
1071 /* Actually insert the new method. */
1072 TREE_VEC_ELT (method_vec, slot)
1073 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1075 /* Add the new binding. */
1076 if (!DECL_CONSTRUCTOR_P (method)
1077 && !DECL_DESTRUCTOR_P (method))
1078 push_class_level_binding (DECL_NAME (method),
1079 TREE_VEC_ELT (method_vec, slot));
1082 /* Subroutines of finish_struct. */
1084 /* Look through the list of fields for this struct, deleting
1085 duplicates as we go. This must be recursive to handle
1086 anonymous unions.
1088 FIELD is the field which may not appear anywhere in FIELDS.
1089 FIELD_PTR, if non-null, is the starting point at which
1090 chained deletions may take place.
1091 The value returned is the first acceptable entry found
1092 in FIELDS.
1094 Note that anonymous fields which are not of UNION_TYPE are
1095 not duplicates, they are just anonymous fields. This happens
1096 when we have unnamed bitfields, for example. */
1098 static tree
1099 delete_duplicate_fields_1 (field, fields)
1100 tree field, fields;
1102 tree x;
1103 tree prev = 0;
1104 if (DECL_NAME (field) == 0)
1106 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1107 return fields;
1109 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1110 fields = delete_duplicate_fields_1 (x, fields);
1111 return fields;
1113 else
1115 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1117 if (DECL_NAME (x) == 0)
1119 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1120 continue;
1121 TYPE_FIELDS (TREE_TYPE (x))
1122 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1123 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1125 if (prev == 0)
1126 fields = TREE_CHAIN (fields);
1127 else
1128 TREE_CHAIN (prev) = TREE_CHAIN (x);
1131 else if (TREE_CODE (field) == USING_DECL)
1132 /* A using declaration may is allowed to appear more than
1133 once. We'll prune these from the field list later, and
1134 handle_using_decl will complain about invalid multiple
1135 uses. */
1137 else if (DECL_NAME (field) == DECL_NAME (x))
1139 if (TREE_CODE (field) == CONST_DECL
1140 && TREE_CODE (x) == CONST_DECL)
1141 cp_error_at ("duplicate enum value `%D'", x);
1142 else if (TREE_CODE (field) == CONST_DECL
1143 || TREE_CODE (x) == CONST_DECL)
1144 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1146 else if (DECL_DECLARES_TYPE_P (field)
1147 && DECL_DECLARES_TYPE_P (x))
1149 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1150 continue;
1151 cp_error_at ("duplicate nested type `%D'", x);
1153 else if (DECL_DECLARES_TYPE_P (field)
1154 || DECL_DECLARES_TYPE_P (x))
1156 /* Hide tag decls. */
1157 if ((TREE_CODE (field) == TYPE_DECL
1158 && DECL_ARTIFICIAL (field))
1159 || (TREE_CODE (x) == TYPE_DECL
1160 && DECL_ARTIFICIAL (x)))
1161 continue;
1162 cp_error_at ("duplicate field `%D' (as type and non-type)",
1165 else
1166 cp_error_at ("duplicate member `%D'", x);
1167 if (prev == 0)
1168 fields = TREE_CHAIN (fields);
1169 else
1170 TREE_CHAIN (prev) = TREE_CHAIN (x);
1174 return fields;
1177 static void
1178 delete_duplicate_fields (fields)
1179 tree fields;
1181 tree x;
1182 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1183 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1186 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1187 legit, otherwise return 0. */
1189 static int
1190 alter_access (t, fdecl, access)
1191 tree t;
1192 tree fdecl;
1193 tree access;
1195 tree elem;
1197 if (!DECL_LANG_SPECIFIC (fdecl))
1198 retrofit_lang_decl (fdecl);
1200 if (DECL_DISCRIMINATOR_P (fdecl))
1201 abort ();
1203 elem = purpose_member (t, DECL_ACCESS (fdecl));
1204 if (elem)
1206 if (TREE_VALUE (elem) != access)
1208 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1209 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1210 else
1211 error ("conflicting access specifications for field `%s', ignored",
1212 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1214 else
1216 /* They're changing the access to the same thing they changed
1217 it to before. That's OK. */
1221 else
1223 enforce_access (t, fdecl);
1224 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1225 return 1;
1227 return 0;
1230 /* Process the USING_DECL, which is a member of T. */
1232 static void
1233 handle_using_decl (using_decl, t)
1234 tree using_decl;
1235 tree t;
1237 tree ctype = DECL_INITIAL (using_decl);
1238 tree name = DECL_NAME (using_decl);
1239 tree access
1240 = TREE_PRIVATE (using_decl) ? access_private_node
1241 : TREE_PROTECTED (using_decl) ? access_protected_node
1242 : access_public_node;
1243 tree fdecl, binfo;
1244 tree flist = NULL_TREE;
1245 tree old_value;
1247 binfo = binfo_or_else (ctype, t);
1248 if (! binfo)
1249 return;
1251 if (constructor_name_p (name, ctype))
1253 cp_error_at ("`%D' names constructor", using_decl);
1254 return;
1256 if (constructor_name_p (name, t))
1258 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1259 return;
1262 fdecl = lookup_member (binfo, name, 0, 0);
1264 if (!fdecl)
1266 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1267 return;
1270 if (BASELINK_P (fdecl))
1271 /* Ignore base type this came from. */
1272 fdecl = BASELINK_FUNCTIONS (fdecl);
1274 old_value = IDENTIFIER_CLASS_VALUE (name);
1275 if (old_value)
1277 if (is_overloaded_fn (old_value))
1278 old_value = OVL_CURRENT (old_value);
1280 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1281 /* OK */;
1282 else
1283 old_value = NULL_TREE;
1286 if (is_overloaded_fn (fdecl))
1287 flist = fdecl;
1289 if (! old_value)
1291 else if (is_overloaded_fn (old_value))
1293 if (flist)
1294 /* It's OK to use functions from a base when there are functions with
1295 the same name already present in the current class. */;
1296 else
1298 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1299 cp_error_at (" because of local method `%#D' with same name",
1300 OVL_CURRENT (old_value));
1301 return;
1304 else if (!DECL_ARTIFICIAL (old_value))
1306 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1307 cp_error_at (" because of local member `%#D' with same name", old_value);
1308 return;
1311 /* Make type T see field decl FDECL with access ACCESS.*/
1312 if (flist)
1313 for (; flist; flist = OVL_NEXT (flist))
1315 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1316 alter_access (t, OVL_CURRENT (flist), access);
1318 else
1319 alter_access (t, fdecl, access);
1322 /* Run through the base clases of T, updating
1323 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1324 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1325 the bases. */
1327 static void
1328 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1329 no_const_asn_ref_p)
1330 tree t;
1331 int *cant_have_default_ctor_p;
1332 int *cant_have_const_ctor_p;
1333 int *no_const_asn_ref_p;
1335 int n_baseclasses;
1336 int i;
1337 int seen_non_virtual_nearly_empty_base_p;
1338 tree binfos;
1340 binfos = TYPE_BINFO_BASETYPES (t);
1341 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1342 seen_non_virtual_nearly_empty_base_p = 0;
1344 /* An aggregate cannot have baseclasses. */
1345 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1347 for (i = 0; i < n_baseclasses; ++i)
1349 tree base_binfo;
1350 tree basetype;
1352 /* Figure out what base we're looking at. */
1353 base_binfo = TREE_VEC_ELT (binfos, i);
1354 basetype = TREE_TYPE (base_binfo);
1356 /* If the type of basetype is incomplete, then we already
1357 complained about that fact (and we should have fixed it up as
1358 well). */
1359 if (!COMPLETE_TYPE_P (basetype))
1361 int j;
1362 /* The base type is of incomplete type. It is
1363 probably best to pretend that it does not
1364 exist. */
1365 if (i == n_baseclasses-1)
1366 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1367 TREE_VEC_LENGTH (binfos) -= 1;
1368 n_baseclasses -= 1;
1369 for (j = i; j+1 < n_baseclasses; j++)
1370 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1371 continue;
1374 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1375 here because the case of virtual functions but non-virtual
1376 dtor is handled in finish_struct_1. */
1377 if (warn_ecpp && !TYPE_POLYMORPHIC_P (basetype))
1378 cp_warning ("base class `%#T' has a non-virtual destructor",
1379 basetype);
1381 /* If the base class doesn't have copy constructors or
1382 assignment operators that take const references, then the
1383 derived class cannot have such a member automatically
1384 generated. */
1385 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1386 *cant_have_const_ctor_p = 1;
1387 if (TYPE_HAS_ASSIGN_REF (basetype)
1388 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1389 *no_const_asn_ref_p = 1;
1390 /* Similarly, if the base class doesn't have a default
1391 constructor, then the derived class won't have an
1392 automatically generated default constructor. */
1393 if (TYPE_HAS_CONSTRUCTOR (basetype)
1394 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1396 *cant_have_default_ctor_p = 1;
1397 if (! TYPE_HAS_CONSTRUCTOR (t))
1398 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1399 basetype);
1402 if (TREE_VIA_VIRTUAL (base_binfo))
1403 /* A virtual base does not effect nearly emptiness. */
1405 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1407 if (seen_non_virtual_nearly_empty_base_p)
1408 /* And if there is more than one nearly empty base, then the
1409 derived class is not nearly empty either. */
1410 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1411 else
1412 /* Remember we've seen one. */
1413 seen_non_virtual_nearly_empty_base_p = 1;
1415 else if (!is_empty_class (basetype))
1416 /* If the base class is not empty or nearly empty, then this
1417 class cannot be nearly empty. */
1418 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1420 /* A lot of properties from the bases also apply to the derived
1421 class. */
1422 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1423 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1424 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1425 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1426 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1427 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1428 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1429 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1430 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1431 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1435 /* Binfo FROM is within a virtual heirarchy which is being reseated to
1436 TO. Move primary information from FROM to TO, and recursively traverse
1437 into FROM's bases. The heirarchy is dominated by TYPE. MAPPINGS is an
1438 assoc list of binfos that have already been reseated. */
1440 static void
1441 force_canonical_binfo_r (to, from, type, mappings)
1442 tree to;
1443 tree from;
1444 tree type;
1445 tree mappings;
1447 int i, n_baseclasses = BINFO_N_BASETYPES (from);
1449 BINFO_INDIRECT_PRIMARY_P (to)
1450 = BINFO_INDIRECT_PRIMARY_P (from);
1451 BINFO_INDIRECT_PRIMARY_P (from) = 0;
1452 BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1453 BINFO_UNSHARED_MARKED (from) = 0;
1454 BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1455 BINFO_LOST_PRIMARY_P (from) = 0;
1456 if (BINFO_PRIMARY_P (from))
1458 tree primary = BINFO_PRIMARY_BASE_OF (from);
1459 tree assoc;
1461 /* We might have just moved the primary base too, see if it's on our
1462 mappings. */
1463 assoc = purpose_member (primary, mappings);
1464 if (assoc)
1465 primary = TREE_VALUE (assoc);
1466 BINFO_PRIMARY_BASE_OF (to) = primary;
1467 BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1469 my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1470 20010104);
1471 mappings = tree_cons (from, to, mappings);
1472 for (i = 0; i != n_baseclasses; i++)
1474 tree from_binfo = BINFO_BASETYPE (from, i);
1475 tree to_binfo = BINFO_BASETYPE (to, i);
1477 if (TREE_VIA_VIRTUAL (from_binfo))
1479 if (BINFO_PRIMARY_P (from_binfo) &&
1480 purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1481 /* This base is a primary of some binfo we have already
1482 reseated. We must reseat this one too. */
1483 force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1485 else
1486 force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1490 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1491 make TO the canonical binfo, within the heirarchy dominated by TYPE.
1492 MAPPINGS is an assoc list of binfos that have already been reseated.
1493 Adjust any non-virtual bases within FROM, and also move any virtual bases
1494 which are canonical. This complication arises because selecting primary
1495 bases walks in inheritance graph order, but we don't share binfos for
1496 virtual bases, hence we can fill in the primaries for a virtual base,
1497 and then discover that a later base requires the virtual as its
1498 primary. */
1500 static void
1501 force_canonical_binfo (to, from, type, mappings)
1502 tree to;
1503 tree from;
1504 tree type;
1505 tree mappings;
1507 tree assoc = purpose_member (BINFO_TYPE (to),
1508 CLASSTYPE_VBASECLASSES (type));
1509 TREE_VALUE (assoc) = to;
1510 force_canonical_binfo_r (to, from, type, mappings);
1513 /* Make BASE_BINFO the primary virtual base of BINFO within the hierarchy
1514 dominated by TYPE. Returns BASE_BINFO, if it can be made so, NULL
1515 otherwise (because something else has already made it primary). */
1517 static tree
1518 mark_primary_virtual_base (binfo, base_binfo, type)
1519 tree binfo;
1520 tree base_binfo;
1521 tree type;
1523 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1525 if (BINFO_PRIMARY_P (shared_binfo))
1527 /* It's already allocated in the hierarchy. BINFO won't have a
1528 primary base in this hierachy, even though the complete object
1529 BINFO is for, would do. */
1530 BINFO_LOST_PRIMARY_P (binfo) = 1;
1532 return NULL_TREE;
1535 /* We need to make sure that the assoc list
1536 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1537 primary BINFO for the virtual base, as this is the one
1538 that'll really exist. */
1539 if (base_binfo != shared_binfo)
1540 force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1542 return base_binfo;
1545 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1546 base, then BINFO has no primary base in this graph. Called from
1547 mark_primary_bases. DATA is the most derived type. */
1549 static tree dfs_unshared_virtual_bases (binfo, data)
1550 tree binfo;
1551 void *data;
1553 tree t = (tree) data;
1555 if (!BINFO_UNSHARED_MARKED (binfo)
1556 && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1558 /* This morally virtual base has a primary base when it
1559 is a complete object. We need to locate the shared instance
1560 of this binfo in the type dominated by T. We duplicate the
1561 primary base information from there to here. */
1562 tree vbase;
1563 tree unshared_base;
1565 for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1566 vbase = BINFO_INHERITANCE_CHAIN (vbase))
1567 continue;
1568 unshared_base = get_original_base (binfo,
1569 binfo_for_vbase (BINFO_TYPE (vbase),
1570 t));
1571 my_friendly_assert (unshared_base != binfo, 20010612);
1572 BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1573 if (!BINFO_LOST_PRIMARY_P (binfo))
1574 BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1577 if (binfo != TYPE_BINFO (t))
1578 /* The vtable fields will have been copied when duplicating the
1579 base binfos. That information is bogus, make sure we don't try
1580 and use it. */
1581 BINFO_VTABLE (binfo) = NULL_TREE;
1583 /* If this is a virtual primary base, make sure its offset matches
1584 that which it is primary for. */
1585 if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
1586 binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
1588 tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
1589 BINFO_OFFSET (binfo));
1590 if (!integer_zerop (delta))
1591 propagate_binfo_offsets (binfo, delta, t);
1594 BINFO_UNSHARED_MARKED (binfo) = 0;
1595 return NULL;
1598 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1599 dominated by TYPE that are primary bases. */
1601 static void
1602 mark_primary_bases (type)
1603 tree type;
1605 tree binfo;
1607 /* Walk the bases in inheritance graph order. */
1608 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1610 tree base_binfo;
1612 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1613 /* Not a dynamic base. */
1614 continue;
1616 base_binfo = get_primary_binfo (binfo);
1618 if (TREE_VIA_VIRTUAL (base_binfo))
1619 base_binfo = mark_primary_virtual_base (binfo, base_binfo, type);
1621 if (base_binfo)
1622 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1624 BINFO_UNSHARED_MARKED (binfo) = 1;
1626 /* There could remain unshared morally virtual bases which were not
1627 visited in the inheritance graph walk. These bases will have lost
1628 their virtual primary base (should they have one). We must now
1629 find them. Also we must fix up the BINFO_OFFSETs of primary
1630 virtual bases. We could not do that as we went along, as they
1631 were originally copied from the bases we inherited from by
1632 unshare_base_binfos. That may have decided differently about
1633 where a virtual primary base went. */
1634 dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1637 /* Make the BINFO the primary base of T. */
1639 static void
1640 set_primary_base (t, binfo, vfuns_p)
1641 tree t;
1642 tree binfo;
1643 int *vfuns_p;
1645 tree basetype;
1647 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1648 basetype = BINFO_TYPE (binfo);
1649 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1650 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1651 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1652 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1653 *vfuns_p = CLASSTYPE_VSIZE (basetype);
1656 /* Determine the primary class for T. */
1658 static void
1659 determine_primary_base (t, vfuns_p)
1660 tree t;
1661 int *vfuns_p;
1663 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1664 tree vbases;
1665 tree type_binfo;
1667 /* If there are no baseclasses, there is certainly no primary base. */
1668 if (n_baseclasses == 0)
1669 return;
1671 type_binfo = TYPE_BINFO (t);
1673 for (i = 0; i < n_baseclasses; i++)
1675 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1676 tree basetype = BINFO_TYPE (base_binfo);
1678 if (TYPE_CONTAINS_VPTR_P (basetype))
1680 /* Even a virtual baseclass can contain our RTTI
1681 information. But, we prefer a non-virtual polymorphic
1682 baseclass. */
1683 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1684 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1686 /* We prefer a non-virtual base, although a virtual one will
1687 do. */
1688 if (TREE_VIA_VIRTUAL (base_binfo))
1689 continue;
1691 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1693 set_primary_base (t, base_binfo, vfuns_p);
1694 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1696 else
1698 tree vfields;
1700 /* Only add unique vfields, and flatten them out as we go. */
1701 for (vfields = CLASSTYPE_VFIELDS (basetype);
1702 vfields;
1703 vfields = TREE_CHAIN (vfields))
1704 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1705 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1706 CLASSTYPE_VFIELDS (t)
1707 = tree_cons (base_binfo,
1708 VF_BASETYPE_VALUE (vfields),
1709 CLASSTYPE_VFIELDS (t));
1714 if (!TYPE_VFIELD (t))
1715 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1717 /* Find the indirect primary bases - those virtual bases which are primary
1718 bases of something else in this hierarchy. */
1719 for (vbases = CLASSTYPE_VBASECLASSES (t);
1720 vbases;
1721 vbases = TREE_CHAIN (vbases))
1723 tree vbase_binfo = TREE_VALUE (vbases);
1725 /* See if this virtual base is an indirect primary base. To be so,
1726 it must be a primary base within the hierarchy of one of our
1727 direct bases. */
1728 for (i = 0; i < n_baseclasses; ++i)
1730 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1731 tree v;
1733 for (v = CLASSTYPE_VBASECLASSES (basetype);
1735 v = TREE_CHAIN (v))
1737 tree base_vbase = TREE_VALUE (v);
1739 if (BINFO_PRIMARY_P (base_vbase)
1740 && same_type_p (BINFO_TYPE (base_vbase),
1741 BINFO_TYPE (vbase_binfo)))
1743 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1744 break;
1748 /* If we've discovered that this virtual base is an indirect
1749 primary base, then we can move on to the next virtual
1750 base. */
1751 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1752 break;
1756 /* A "nearly-empty" virtual base class can be the primary base
1757 class, if no non-virtual polymorphic base can be found. */
1758 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1760 /* If not NULL, this is the best primary base candidate we have
1761 found so far. */
1762 tree candidate = NULL_TREE;
1763 tree base_binfo;
1765 /* Loop over the baseclasses. */
1766 for (base_binfo = TYPE_BINFO (t);
1767 base_binfo;
1768 base_binfo = TREE_CHAIN (base_binfo))
1770 tree basetype = BINFO_TYPE (base_binfo);
1772 if (TREE_VIA_VIRTUAL (base_binfo)
1773 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1775 /* If this is not an indirect primary base, then it's
1776 definitely our primary base. */
1777 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1779 candidate = base_binfo;
1780 break;
1783 /* If this is an indirect primary base, it still could be
1784 our primary base -- unless we later find there's another
1785 nearly-empty virtual base that isn't an indirect
1786 primary base. */
1787 if (!candidate)
1788 candidate = base_binfo;
1792 /* If we've got a primary base, use it. */
1793 if (candidate)
1795 set_primary_base (t, candidate, vfuns_p);
1796 CLASSTYPE_VFIELDS (t)
1797 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1801 /* Mark the primary base classes at this point. */
1802 mark_primary_bases (t);
1805 /* Set memoizing fields and bits of T (and its variants) for later
1806 use. */
1808 static void
1809 finish_struct_bits (t)
1810 tree t;
1812 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1814 /* Fix up variants (if any). */
1815 tree variants = TYPE_NEXT_VARIANT (t);
1816 while (variants)
1818 /* These fields are in the _TYPE part of the node, not in
1819 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1820 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1821 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1822 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1823 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1824 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1826 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1827 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1828 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1829 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1830 /* Copy whatever these are holding today. */
1831 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1832 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1833 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1834 TYPE_SIZE (variants) = TYPE_SIZE (t);
1835 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1836 variants = TYPE_NEXT_VARIANT (variants);
1839 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1840 /* For a class w/o baseclasses, `finish_struct' has set
1841 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1842 definition). Similarly for a class whose base classes do not
1843 have vtables. When neither of these is true, we might have
1844 removed abstract virtuals (by providing a definition), added
1845 some (by declaring new ones), or redeclared ones from a base
1846 class. We need to recalculate what's really an abstract virtual
1847 at this point (by looking in the vtables). */
1848 get_pure_virtuals (t);
1850 if (n_baseclasses)
1852 /* Notice whether this class has type conversion functions defined. */
1853 tree binfo = TYPE_BINFO (t);
1854 tree binfos = BINFO_BASETYPES (binfo);
1855 tree basetype;
1857 for (i = n_baseclasses-1; i >= 0; i--)
1859 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1861 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1865 /* If this type has a copy constructor, force its mode to be BLKmode, and
1866 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1867 be passed by invisible reference and prevent it from being returned in
1868 a register.
1870 Also do this if the class has BLKmode but can still be returned in
1871 registers, since function_cannot_inline_p won't let us inline
1872 functions returning such a type. This affects the HP-PA. */
1873 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1874 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1875 && CLASSTYPE_NON_AGGREGATE (t)))
1877 tree variants;
1878 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1879 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1881 TYPE_MODE (variants) = BLKmode;
1882 TREE_ADDRESSABLE (variants) = 1;
1887 /* Issue warnings about T having private constructors, but no friends,
1888 and so forth.
1890 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1891 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1892 non-private static member functions. */
1894 static void
1895 maybe_warn_about_overly_private_class (t)
1896 tree t;
1898 int has_member_fn = 0;
1899 int has_nonprivate_method = 0;
1900 tree fn;
1902 if (!warn_ctor_dtor_privacy
1903 /* If the class has friends, those entities might create and
1904 access instances, so we should not warn. */
1905 || (CLASSTYPE_FRIEND_CLASSES (t)
1906 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1907 /* We will have warned when the template was declared; there's
1908 no need to warn on every instantiation. */
1909 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1910 /* There's no reason to even consider warning about this
1911 class. */
1912 return;
1914 /* We only issue one warning, if more than one applies, because
1915 otherwise, on code like:
1917 class A {
1918 // Oops - forgot `public:'
1919 A();
1920 A(const A&);
1921 ~A();
1924 we warn several times about essentially the same problem. */
1926 /* Check to see if all (non-constructor, non-destructor) member
1927 functions are private. (Since there are no friends or
1928 non-private statics, we can't ever call any of the private member
1929 functions.) */
1930 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1931 /* We're not interested in compiler-generated methods; they don't
1932 provide any way to call private members. */
1933 if (!DECL_ARTIFICIAL (fn))
1935 if (!TREE_PRIVATE (fn))
1937 if (DECL_STATIC_FUNCTION_P (fn))
1938 /* A non-private static member function is just like a
1939 friend; it can create and invoke private member
1940 functions, and be accessed without a class
1941 instance. */
1942 return;
1944 has_nonprivate_method = 1;
1945 break;
1947 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1948 has_member_fn = 1;
1951 if (!has_nonprivate_method && has_member_fn)
1953 /* There are no non-private methods, and there's at least one
1954 private member function that isn't a constructor or
1955 destructor. (If all the private members are
1956 constructors/destructors we want to use the code below that
1957 issues error messages specifically referring to
1958 constructors/destructors.) */
1959 int i;
1960 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1961 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1962 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1963 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1965 has_nonprivate_method = 1;
1966 break;
1968 if (!has_nonprivate_method)
1970 cp_warning ("all member functions in class `%T' are private", t);
1971 return;
1975 /* Even if some of the member functions are non-private, the class
1976 won't be useful for much if all the constructors or destructors
1977 are private: such an object can never be created or destroyed. */
1978 if (TYPE_HAS_DESTRUCTOR (t)
1979 && TREE_PRIVATE (CLASSTYPE_DESTRUCTOR (t)))
1981 cp_warning ("`%#T' only defines a private destructor and has no friends",
1983 return;
1986 if (TYPE_HAS_CONSTRUCTOR (t))
1988 int nonprivate_ctor = 0;
1990 /* If a non-template class does not define a copy
1991 constructor, one is defined for it, enabling it to avoid
1992 this warning. For a template class, this does not
1993 happen, and so we would normally get a warning on:
1995 template <class T> class C { private: C(); };
1997 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1998 complete non-template or fully instantiated classes have this
1999 flag set. */
2000 if (!TYPE_HAS_INIT_REF (t))
2001 nonprivate_ctor = 1;
2002 else
2003 for (fn = CLASSTYPE_CONSTRUCTORS (t);
2005 fn = OVL_NEXT (fn))
2007 tree ctor = OVL_CURRENT (fn);
2008 /* Ideally, we wouldn't count copy constructors (or, in
2009 fact, any constructor that takes an argument of the
2010 class type as a parameter) because such things cannot
2011 be used to construct an instance of the class unless
2012 you already have one. But, for now at least, we're
2013 more generous. */
2014 if (! TREE_PRIVATE (ctor))
2016 nonprivate_ctor = 1;
2017 break;
2021 if (nonprivate_ctor == 0)
2023 cp_warning ("`%#T' only defines private constructors and has no friends",
2025 return;
2030 /* Function to help qsort sort FIELD_DECLs by name order. */
2032 static int
2033 field_decl_cmp (x, y)
2034 const tree *x, *y;
2036 if (DECL_NAME (*x) == DECL_NAME (*y))
2037 /* A nontype is "greater" than a type. */
2038 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2039 if (DECL_NAME (*x) == NULL_TREE)
2040 return -1;
2041 if (DECL_NAME (*y) == NULL_TREE)
2042 return 1;
2043 if (DECL_NAME (*x) < DECL_NAME (*y))
2044 return -1;
2045 return 1;
2048 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2050 static int
2051 method_name_cmp (m1, m2)
2052 const tree *m1, *m2;
2054 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2055 return 0;
2056 if (*m1 == NULL_TREE)
2057 return -1;
2058 if (*m2 == NULL_TREE)
2059 return 1;
2060 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2061 return -1;
2062 return 1;
2065 /* Warn about duplicate methods in fn_fields. Also compact method
2066 lists so that lookup can be made faster.
2068 Data Structure: List of method lists. The outer list is a
2069 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2070 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2071 links the entire list of methods for TYPE_METHODS. Friends are
2072 chained in the same way as member functions (? TREE_CHAIN or
2073 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2074 list. That allows them to be quickly deleted, and requires no
2075 extra storage.
2077 Sort methods that are not special (i.e., constructors, destructors,
2078 and type conversion operators) so that we can find them faster in
2079 search. */
2081 static void
2082 finish_struct_methods (t)
2083 tree t;
2085 tree fn_fields;
2086 tree method_vec;
2087 int slot, len;
2089 if (!TYPE_METHODS (t))
2091 /* Clear these for safety; perhaps some parsing error could set
2092 these incorrectly. */
2093 TYPE_HAS_CONSTRUCTOR (t) = 0;
2094 TYPE_HAS_DESTRUCTOR (t) = 0;
2095 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2096 return;
2099 method_vec = CLASSTYPE_METHOD_VEC (t);
2100 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2101 len = TREE_VEC_LENGTH (method_vec);
2103 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2104 and the next few with type conversion operators (if any). */
2105 for (fn_fields = TYPE_METHODS (t); fn_fields;
2106 fn_fields = TREE_CHAIN (fn_fields))
2107 /* Clear out this flag. */
2108 DECL_IN_AGGR_P (fn_fields) = 0;
2110 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTOR (t))
2111 /* We thought there was a destructor, but there wasn't. Some
2112 parse errors cause this anomalous situation. */
2113 TYPE_HAS_DESTRUCTOR (t) = 0;
2115 /* Issue warnings about private constructors and such. If there are
2116 no methods, then some public defaults are generated. */
2117 maybe_warn_about_overly_private_class (t);
2119 /* Now sort the methods. */
2120 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2121 len--;
2122 TREE_VEC_LENGTH (method_vec) = len;
2124 /* The type conversion ops have to live at the front of the vec, so we
2125 can't sort them. */
2126 for (slot = 2; slot < len; ++slot)
2128 tree fn = TREE_VEC_ELT (method_vec, slot);
2130 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2131 break;
2133 if (len - slot > 1)
2134 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2135 (int (*)(const void *, const void *))method_name_cmp);
2138 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2140 void
2141 duplicate_tag_error (t)
2142 tree t;
2144 cp_error ("redefinition of `%#T'", t);
2145 cp_error_at ("previous definition of `%#T'", t);
2147 /* Pretend we haven't defined this type. */
2149 /* All of the component_decl's were TREE_CHAINed together in the parser.
2150 finish_struct_methods walks these chains and assembles all methods with
2151 the same base name into DECL_CHAINs. Now we don't need the parser chains
2152 anymore, so we unravel them. */
2154 /* This used to be in finish_struct, but it turns out that the
2155 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2156 things... */
2157 if (CLASSTYPE_METHOD_VEC (t))
2159 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2160 int i, len = TREE_VEC_LENGTH (method_vec);
2161 for (i = 0; i < len; i++)
2163 tree unchain = TREE_VEC_ELT (method_vec, i);
2164 while (unchain != NULL_TREE)
2166 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2167 unchain = OVL_NEXT (unchain);
2172 if (TYPE_LANG_SPECIFIC (t))
2174 tree binfo = TYPE_BINFO (t);
2175 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2176 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2177 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2178 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2180 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2181 BINFO_BASETYPES(binfo) = NULL_TREE;
2183 TYPE_BINFO (t) = binfo;
2184 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2185 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2186 TYPE_REDEFINED (t) = 1;
2187 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2188 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2190 TYPE_SIZE (t) = NULL_TREE;
2191 TYPE_MODE (t) = VOIDmode;
2192 TYPE_FIELDS (t) = NULL_TREE;
2193 TYPE_METHODS (t) = NULL_TREE;
2194 TYPE_VFIELD (t) = NULL_TREE;
2195 TYPE_CONTEXT (t) = NULL_TREE;
2196 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2198 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2199 TYPE_LANG_FLAG_0 (t) = 0;
2200 TYPE_LANG_FLAG_1 (t) = 0;
2201 TYPE_LANG_FLAG_2 (t) = 0;
2202 TYPE_LANG_FLAG_3 (t) = 0;
2203 TYPE_LANG_FLAG_4 (t) = 0;
2204 TYPE_LANG_FLAG_5 (t) = 0;
2205 TYPE_LANG_FLAG_6 (t) = 0;
2206 /* But not this one. */
2207 SET_IS_AGGR_TYPE (t, 1);
2210 /* Make BINFO's vtable have N entries, including RTTI entries,
2211 vbase and vcall offsets, etc. Set its type and call the backend
2212 to lay it out. */
2214 static void
2215 layout_vtable_decl (binfo, n)
2216 tree binfo;
2217 int n;
2219 tree atype;
2220 tree vtable;
2222 atype = build_cplus_array_type (vtable_entry_type,
2223 build_index_type (size_int (n - 1)));
2224 layout_type (atype);
2226 /* We may have to grow the vtable. */
2227 vtable = get_vtbl_decl_for_binfo (binfo);
2228 if (!same_type_p (TREE_TYPE (vtable), atype))
2230 TREE_TYPE (vtable) = atype;
2231 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2232 layout_decl (vtable, 0);
2234 /* At one time the vtable info was grabbed 2 words at a time. This
2235 fails on Sparc unless you have 8-byte alignment. */
2236 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2237 DECL_ALIGN (vtable));
2241 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2242 have the same signature. */
2245 same_signature_p (fndecl, base_fndecl)
2246 tree fndecl, base_fndecl;
2248 /* One destructor overrides another if they are the same kind of
2249 destructor. */
2250 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2251 && special_function_p (base_fndecl) == special_function_p (fndecl))
2252 return 1;
2253 /* But a non-destructor never overrides a destructor, nor vice
2254 versa, nor do different kinds of destructors override
2255 one-another. For example, a complete object destructor does not
2256 override a deleting destructor. */
2257 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2258 return 0;
2260 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2262 tree types, base_types;
2263 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2264 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2265 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2266 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2267 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2268 return 1;
2270 return 0;
2273 typedef struct find_final_overrider_data_s {
2274 /* The function for which we are trying to find a final overrider. */
2275 tree fn;
2276 /* The base class in which the function was declared. */
2277 tree declaring_base;
2278 /* The most derived class in the hierarchy. */
2279 tree most_derived_type;
2280 /* The final overriding function. */
2281 tree overriding_fn;
2282 /* The functions that we thought might be final overriders, but
2283 aren't. */
2284 tree candidates;
2285 /* The BINFO for the class in which the final overriding function
2286 appears. */
2287 tree overriding_base;
2288 } find_final_overrider_data;
2290 /* Called from find_final_overrider via dfs_walk. */
2292 static tree
2293 dfs_find_final_overrider (binfo, data)
2294 tree binfo;
2295 void *data;
2297 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2299 if (same_type_p (BINFO_TYPE (binfo),
2300 BINFO_TYPE (ffod->declaring_base))
2301 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2302 BINFO_OFFSET (ffod->declaring_base)))
2304 tree path;
2305 tree method;
2307 /* We haven't found an overrider yet. */
2308 method = NULL_TREE;
2309 /* We've found a path to the declaring base. Walk down the path
2310 looking for an overrider for FN. */
2311 for (path = reverse_path (binfo);
2312 path;
2313 path = TREE_CHAIN (path))
2315 method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2316 ffod->fn);
2317 if (method)
2318 break;
2321 /* If we found an overrider, record the overriding function, and
2322 the base from which it came. */
2323 if (path)
2325 tree base;
2327 /* Assume the path is non-virtual. See if there are any
2328 virtual bases from (but not including) the overrider up
2329 to and including the base where the function is
2330 defined. */
2331 for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
2332 if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
2334 base = ffod->declaring_base;
2335 break;
2338 /* If we didn't already have an overrider, or any
2339 candidates, then this function is the best candidate so
2340 far. */
2341 if (!ffod->overriding_fn && !ffod->candidates)
2343 ffod->overriding_fn = method;
2344 ffod->overriding_base = TREE_VALUE (path);
2346 else if (ffod->overriding_fn)
2348 /* We had a best overrider; let's see how this compares. */
2350 if (ffod->overriding_fn == method
2351 && (tree_int_cst_equal
2352 (BINFO_OFFSET (TREE_VALUE (path)),
2353 BINFO_OFFSET (ffod->overriding_base))))
2354 /* We found the same overrider we already have, and in the
2355 same place; it's still the best. */;
2356 else if (strictly_overrides (ffod->overriding_fn, method))
2357 /* The old function overrides this function; it's still the
2358 best. */;
2359 else if (strictly_overrides (method, ffod->overriding_fn))
2361 /* The new function overrides the old; it's now the
2362 best. */
2363 ffod->overriding_fn = method;
2364 ffod->overriding_base = TREE_VALUE (path);
2366 else
2368 /* Ambiguous. */
2369 ffod->candidates
2370 = build_tree_list (NULL_TREE,
2371 ffod->overriding_fn);
2372 if (method != ffod->overriding_fn)
2373 ffod->candidates
2374 = tree_cons (NULL_TREE, method, ffod->candidates);
2375 ffod->overriding_fn = NULL_TREE;
2376 ffod->overriding_base = NULL_TREE;
2379 else
2381 /* We had a list of ambiguous overrides; let's see how this
2382 new one compares. */
2384 tree candidates;
2385 bool incomparable = false;
2387 /* If there were previous candidates, and this function
2388 overrides all of them, then it is the new best
2389 candidate. */
2390 for (candidates = ffod->candidates;
2391 candidates;
2392 candidates = TREE_CHAIN (candidates))
2394 /* If the candidate overrides the METHOD, then we
2395 needn't worry about it any further. */
2396 if (strictly_overrides (TREE_VALUE (candidates),
2397 method))
2399 method = NULL_TREE;
2400 break;
2403 /* If the METHOD doesn't override the candidate,
2404 then it is incomporable. */
2405 if (!strictly_overrides (method,
2406 TREE_VALUE (candidates)))
2407 incomparable = true;
2410 /* If METHOD overrode all the candidates, then it is the
2411 new best candidate. */
2412 if (!candidates && !incomparable)
2414 ffod->overriding_fn = method;
2415 ffod->overriding_base = TREE_VALUE (path);
2416 ffod->candidates = NULL_TREE;
2418 /* If METHOD didn't override all the candidates, then it
2419 is another candidate. */
2420 else if (method && incomparable)
2421 ffod->candidates
2422 = tree_cons (NULL_TREE, method, ffod->candidates);
2427 return NULL_TREE;
2430 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2431 FN and whose TREE_VALUE is the binfo for the base where the
2432 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2433 base object in which FN is declared. */
2435 static tree
2436 find_final_overrider (t, binfo, fn)
2437 tree t;
2438 tree binfo;
2439 tree fn;
2441 find_final_overrider_data ffod;
2443 /* Getting this right is a little tricky. This is legal:
2445 struct S { virtual void f (); };
2446 struct T { virtual void f (); };
2447 struct U : public S, public T { };
2449 even though calling `f' in `U' is ambiguous. But,
2451 struct R { virtual void f(); };
2452 struct S : virtual public R { virtual void f (); };
2453 struct T : virtual public R { virtual void f (); };
2454 struct U : public S, public T { };
2456 is not -- there's no way to decide whether to put `S::f' or
2457 `T::f' in the vtable for `R'.
2459 The solution is to look at all paths to BINFO. If we find
2460 different overriders along any two, then there is a problem. */
2461 ffod.fn = fn;
2462 ffod.declaring_base = binfo;
2463 ffod.most_derived_type = t;
2464 ffod.overriding_fn = NULL_TREE;
2465 ffod.overriding_base = NULL_TREE;
2466 ffod.candidates = NULL_TREE;
2468 dfs_walk (TYPE_BINFO (t),
2469 dfs_find_final_overrider,
2470 NULL,
2471 &ffod);
2473 /* If there was no winner, issue an error message. */
2474 if (!ffod.overriding_fn)
2476 cp_error ("no unique final overrider for `%D' in `%T'", fn, t);
2477 return error_mark_node;
2480 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2483 /* Returns the function from the BINFO_VIRTUALS entry in T which matches
2484 the signature of FUNCTION_DECL FN, or NULL_TREE if none. In other words,
2485 the function that the slot in T's primary vtable points to. */
2487 static tree get_matching_virtual PARAMS ((tree, tree));
2488 static tree
2489 get_matching_virtual (t, fn)
2490 tree t, fn;
2492 tree f;
2494 for (f = BINFO_VIRTUALS (TYPE_BINFO (t)); f; f = TREE_CHAIN (f))
2495 if (same_signature_p (BV_FN (f), fn))
2496 return BV_FN (f);
2497 return NULL_TREE;
2500 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2501 dominated by T. FN has been overriden in BINFO; VIRTUALS points to the
2502 corresponding position in the BINFO_VIRTUALS list. */
2504 static void
2505 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2506 tree t;
2507 tree binfo;
2508 tree fn;
2509 tree *virtuals;
2511 tree b;
2512 tree overrider;
2513 tree delta;
2514 tree virtual_base;
2515 tree first_defn;
2517 /* Find the nearest primary base (possibly binfo itself) which defines
2518 this function; this is the class the caller will convert to when
2519 calling FN through BINFO. */
2520 for (b = binfo; ; b = get_primary_binfo (b))
2522 if (look_for_overrides_here (BINFO_TYPE (b), fn))
2523 break;
2525 first_defn = b;
2527 /* Find the final overrider. */
2528 overrider = find_final_overrider (t, b, fn);
2529 if (overrider == error_mark_node)
2530 return;
2532 /* Assume that we will produce a thunk that convert all the way to
2533 the final overrider, and not to an intermediate virtual base. */
2534 virtual_base = NULL_TREE;
2536 /* We will convert to an intermediate virtual base first, and then
2537 use the vcall offset located there to finish the conversion. */
2538 while (b)
2540 /* If we find the final overrider, then we can stop
2541 walking. */
2542 if (same_type_p (BINFO_TYPE (b),
2543 BINFO_TYPE (TREE_VALUE (overrider))))
2544 break;
2546 /* If we find a virtual base, and we haven't yet found the
2547 overrider, then there is a virtual base between the
2548 declaring base (first_defn) and the final overrider. */
2549 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2550 virtual_base = b;
2552 b = BINFO_INHERITANCE_CHAIN (b);
2555 /* Compute the constant adjustment to the `this' pointer. The
2556 `this' pointer, when this function is called, will point at BINFO
2557 (or one of its primary bases, which are at the same offset). */
2559 if (virtual_base)
2560 /* The `this' pointer needs to be adjusted from the declaration to
2561 the nearest virtual base. */
2562 delta = size_diffop (BINFO_OFFSET (virtual_base),
2563 BINFO_OFFSET (first_defn));
2564 else
2566 /* The `this' pointer needs to be adjusted from pointing to
2567 BINFO to pointing at the base where the final overrider
2568 appears. */
2569 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2570 BINFO_OFFSET (binfo));
2572 if (! integer_zerop (delta))
2574 /* We'll need a thunk. But if we have a (perhaps formerly)
2575 primary virtual base, we have a vcall slot for this function,
2576 so we can use it rather than create a non-virtual thunk. */
2578 b = get_primary_binfo (first_defn);
2579 for (; b; b = get_primary_binfo (b))
2581 tree f = get_matching_virtual (BINFO_TYPE (b), fn);
2582 if (!f)
2583 /* b doesn't have this function; no suitable vbase. */
2584 break;
2585 if (TREE_VIA_VIRTUAL (b))
2587 /* Found one; we can treat ourselves as a virtual base. */
2588 virtual_base = binfo;
2589 delta = size_zero_node;
2590 break;
2596 modify_vtable_entry (t,
2597 binfo,
2598 TREE_PURPOSE (overrider),
2599 delta,
2600 virtuals);
2602 if (virtual_base)
2603 BV_USE_VCALL_INDEX_P (*virtuals) = 1;
2606 /* Called from modify_all_vtables via dfs_walk. */
2608 static tree
2609 dfs_modify_vtables (binfo, data)
2610 tree binfo;
2611 void *data;
2613 if (/* There's no need to modify the vtable for a non-virtual
2614 primary base; we're not going to use that vtable anyhow.
2615 We do still need to do this for virtual primary bases, as they
2616 could become non-primary in a construction vtable. */
2617 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2618 /* Similarly, a base without a vtable needs no modification. */
2619 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2621 tree t;
2622 tree virtuals;
2623 tree old_virtuals;
2625 t = (tree) data;
2627 make_new_vtable (t, binfo);
2629 /* Now, go through each of the virtual functions in the virtual
2630 function table for BINFO. Find the final overrider, and
2631 update the BINFO_VIRTUALS list appropriately. */
2632 for (virtuals = BINFO_VIRTUALS (binfo),
2633 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2634 virtuals;
2635 virtuals = TREE_CHAIN (virtuals),
2636 old_virtuals = TREE_CHAIN (old_virtuals))
2637 update_vtable_entry_for_fn (t,
2638 binfo,
2639 BV_FN (old_virtuals),
2640 &virtuals);
2643 SET_BINFO_MARKED (binfo);
2645 return NULL_TREE;
2648 /* Update all of the primary and secondary vtables for T. Create new
2649 vtables as required, and initialize their RTTI information. Each
2650 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2651 function from a base class; find and modify the appropriate entries
2652 to point to the overriding functions. Returns a list, in
2653 declaration order, of the functions that are overridden in this
2654 class, but do not appear in the primary base class vtable, and
2655 which should therefore be appended to the end of the vtable for T. */
2657 static tree
2658 modify_all_vtables (t, vfuns_p, overridden_virtuals)
2659 tree t;
2660 int *vfuns_p;
2661 tree overridden_virtuals;
2663 tree binfo = TYPE_BINFO (t);
2664 tree *fnsp;
2666 /* Update all of the vtables. */
2667 dfs_walk (binfo,
2668 dfs_modify_vtables,
2669 dfs_unmarked_real_bases_queue_p,
2671 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2673 /* Include overriding functions for secondary vtables in our primary
2674 vtable. */
2675 for (fnsp = &overridden_virtuals; *fnsp; )
2677 tree fn = TREE_VALUE (*fnsp);
2679 if (!BINFO_VIRTUALS (binfo)
2680 || !value_member (fn, BINFO_VIRTUALS (binfo)))
2682 /* Set the vtable index. */
2683 set_vindex (fn, vfuns_p);
2684 /* We don't need to convert to a base class when calling
2685 this function. */
2686 DECL_VIRTUAL_CONTEXT (fn) = t;
2688 /* We don't need to adjust the `this' pointer when
2689 calling this function. */
2690 BV_DELTA (*fnsp) = integer_zero_node;
2691 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2693 /* This is an overridden function not already in our
2694 vtable. Keep it. */
2695 fnsp = &TREE_CHAIN (*fnsp);
2697 else
2698 /* We've already got an entry for this function. Skip it. */
2699 *fnsp = TREE_CHAIN (*fnsp);
2702 return overridden_virtuals;
2705 /* Here, we already know that they match in every respect.
2706 All we have to check is where they had their declarations. */
2708 static int
2709 strictly_overrides (fndecl1, fndecl2)
2710 tree fndecl1, fndecl2;
2712 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
2713 DECL_CONTEXT (fndecl1),
2714 0, (tree *)0);
2715 if (distance == -2 || distance > 0)
2716 return 1;
2717 return 0;
2720 /* Get the base virtual function declarations in T that are either
2721 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2722 the overrider/hider. */
2724 static tree
2725 get_basefndecls (fndecl, t)
2726 tree fndecl, t;
2728 tree methods = TYPE_METHODS (t);
2729 tree base_fndecls = NULL_TREE;
2730 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2731 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2733 while (methods)
2735 if (TREE_CODE (methods) == FUNCTION_DECL
2736 && DECL_VINDEX (methods) != NULL_TREE
2737 && DECL_NAME (fndecl) == DECL_NAME (methods))
2738 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
2740 methods = TREE_CHAIN (methods);
2743 if (base_fndecls)
2744 return base_fndecls;
2746 for (i = 0; i < n_baseclasses; i++)
2748 tree base_binfo = TREE_VEC_ELT (binfos, i);
2749 tree basetype = BINFO_TYPE (base_binfo);
2751 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2752 base_fndecls);
2755 return base_fndecls;
2758 /* Mark the functions that have been hidden with their overriders.
2759 Since we start out with all functions already marked with a hider,
2760 no need to mark functions that are just hidden.
2762 Subroutine of warn_hidden. */
2764 static void
2765 mark_overriders (fndecl, base_fndecls)
2766 tree fndecl, base_fndecls;
2768 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2769 if (same_signature_p (fndecl, TREE_VALUE (base_fndecls)))
2770 TREE_PURPOSE (base_fndecls) = fndecl;
2773 /* If this declaration supersedes the declaration of
2774 a method declared virtual in the base class, then
2775 mark this field as being virtual as well. */
2777 static void
2778 check_for_override (decl, ctype)
2779 tree decl, ctype;
2781 if (TREE_CODE (decl) == TEMPLATE_DECL)
2782 /* In [temp.mem] we have:
2784 A specialization of a member function template does not
2785 override a virtual function from a base class. */
2786 return;
2787 if ((DECL_DESTRUCTOR_P (decl)
2788 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2789 && look_for_overrides (ctype, decl)
2790 && !DECL_STATIC_FUNCTION_P (decl))
2792 /* Set DECL_VINDEX to a value that is neither an
2793 INTEGER_CST nor the error_mark_node so that
2794 add_virtual_function will realize this is an
2795 overriding function. */
2796 DECL_VINDEX (decl) = decl;
2798 if (DECL_VIRTUAL_P (decl))
2800 if (DECL_VINDEX (decl) == NULL_TREE)
2801 DECL_VINDEX (decl) = error_mark_node;
2802 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2806 /* Warn about hidden virtual functions that are not overridden in t.
2807 We know that constructors and destructors don't apply. */
2809 void
2810 warn_hidden (t)
2811 tree t;
2813 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2814 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2815 int i;
2817 /* We go through each separately named virtual function. */
2818 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2820 tree fns = TREE_VEC_ELT (method_vec, i);
2821 tree fndecl = NULL_TREE;
2823 tree base_fndecls = NULL_TREE;
2824 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2825 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2827 /* First see if we have any virtual functions in this batch. */
2828 for (; fns; fns = OVL_NEXT (fns))
2830 fndecl = OVL_CURRENT (fns);
2831 if (DECL_VINDEX (fndecl))
2832 break;
2835 if (fns == NULL_TREE)
2836 continue;
2838 /* First we get a list of all possible functions that might be
2839 hidden from each base class. */
2840 for (i = 0; i < n_baseclasses; i++)
2842 tree base_binfo = TREE_VEC_ELT (binfos, i);
2843 tree basetype = BINFO_TYPE (base_binfo);
2845 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2846 base_fndecls);
2849 fns = OVL_NEXT (fns);
2851 /* ...then mark up all the base functions with overriders, preferring
2852 overriders to hiders. */
2853 if (base_fndecls)
2854 for (; fns; fns = OVL_NEXT (fns))
2856 fndecl = OVL_CURRENT (fns);
2857 if (DECL_VINDEX (fndecl))
2858 mark_overriders (fndecl, base_fndecls);
2861 /* Now give a warning for all base functions without overriders,
2862 as they are hidden. */
2863 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2864 if (!same_signature_p (TREE_PURPOSE (base_fndecls),
2865 TREE_VALUE (base_fndecls)))
2867 /* Here we know it is a hider, and no overrider exists. */
2868 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2869 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
2874 /* Check for things that are invalid. There are probably plenty of other
2875 things we should check for also. */
2877 static void
2878 finish_struct_anon (t)
2879 tree t;
2881 tree field;
2883 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2885 if (TREE_STATIC (field))
2886 continue;
2887 if (TREE_CODE (field) != FIELD_DECL)
2888 continue;
2890 if (DECL_NAME (field) == NULL_TREE
2891 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2893 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2894 for (; elt; elt = TREE_CHAIN (elt))
2896 /* We're generally only interested in entities the user
2897 declared, but we also find nested classes by noticing
2898 the TYPE_DECL that we create implicitly. You're
2899 allowed to put one anonymous union inside another,
2900 though, so we explicitly tolerate that. */
2901 if (DECL_ARTIFICIAL (elt)
2902 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2903 || ANON_AGGR_TYPE_P (TREE_TYPE (elt))))
2904 continue;
2906 if (DECL_NAME (elt) == constructor_name (t))
2907 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2908 elt);
2910 if (TREE_CODE (elt) != FIELD_DECL)
2912 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2913 elt);
2914 continue;
2917 if (TREE_PRIVATE (elt))
2918 cp_pedwarn_at ("private member `%#D' in anonymous union",
2919 elt);
2920 else if (TREE_PROTECTED (elt))
2921 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2922 elt);
2924 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2925 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2931 /* T is a class type that has an implicitly defined destructor. If it
2932 is trivial, set TYPE_HAS_NONTRIVIAL_DESTRUCTOR. */
2934 static void
2935 check_destructor_triviality (t)
2936 tree t;
2938 int i;
2939 tree f;
2941 /* [class.dtor]
2943 A destructor is trivial if it is an implicitly declared
2944 destructor and if:
2946 -- all of the direct base classes of its class have trivial
2947 destructors and
2949 -- for all of the non-static data members of its class that
2950 are of class type (or array thereof), each such class has a
2951 trivial destructor. */
2953 if (TYPE_BINFO_BASETYPES (t))
2954 for (i = 0; i < TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t)); ++i)
2955 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_BINFO_BASETYPE (t, i)))
2957 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
2958 return;
2961 for (f = TYPE_FIELDS (t); f; f = TREE_CHAIN (f))
2962 if (TREE_CODE (f) == FIELD_DECL
2963 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (strip_array_types
2964 (TREE_TYPE (f))))
2966 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
2967 return;
2971 /* Create default constructors, assignment operators, and so forth for
2972 the type indicated by T, if they are needed.
2973 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2974 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2975 class cannot have a default constructor, copy constructor taking a
2976 const reference argument, or an assignment operator taking a const
2977 reference, respectively. If a virtual destructor is created, its
2978 DECL is returned; otherwise the return value is NULL_TREE. */
2980 static tree
2981 add_implicitly_declared_members (t, cant_have_default_ctor,
2982 cant_have_const_cctor,
2983 cant_have_const_assignment)
2984 tree t;
2985 int cant_have_default_ctor;
2986 int cant_have_const_cctor;
2987 int cant_have_const_assignment;
2989 tree default_fn;
2990 tree implicit_fns = NULL_TREE;
2991 tree virtual_dtor = NULL_TREE;
2992 tree *f;
2994 /* If a destructor was explicitly declared, it is non-trivial. */
2995 if (CLASSTYPE_DESTRUCTOR (t))
2996 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
2997 /* Otherwise, declare one now. */
2998 else
3000 /* Figure out whether the destructor will be trivial. */
3001 check_destructor_triviality (t);
3003 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
3004 check_for_override (default_fn, t);
3006 /* If we couldn't make it work, then pretend we didn't need it. */
3007 if (default_fn == void_type_node)
3009 else
3011 TREE_CHAIN (default_fn) = implicit_fns;
3012 implicit_fns = default_fn;
3014 if (DECL_VINDEX (default_fn))
3015 virtual_dtor = default_fn;
3019 /* Default constructor. */
3020 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3022 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
3023 TREE_CHAIN (default_fn) = implicit_fns;
3024 implicit_fns = default_fn;
3027 /* Copy constructor. */
3028 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3030 /* ARM 12.18: You get either X(X&) or X(const X&), but
3031 not both. --Chip */
3032 default_fn
3033 = implicitly_declare_fn (sfk_copy_constructor, t,
3034 /*const_p=*/!cant_have_const_cctor);
3035 TREE_CHAIN (default_fn) = implicit_fns;
3036 implicit_fns = default_fn;
3039 /* Assignment operator. */
3040 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3042 default_fn
3043 = implicitly_declare_fn (sfk_assignment_operator, t,
3044 /*const_p=*/!cant_have_const_assignment);
3045 TREE_CHAIN (default_fn) = implicit_fns;
3046 implicit_fns = default_fn;
3049 /* Now, hook all of the new functions on to TYPE_METHODS,
3050 and add them to the CLASSTYPE_METHOD_VEC. */
3051 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3052 add_method (t, *f, /*error_p=*/0);
3053 *f = TYPE_METHODS (t);
3054 TYPE_METHODS (t) = implicit_fns;
3056 return virtual_dtor;
3059 /* Subroutine of finish_struct_1. Recursively count the number of fields
3060 in TYPE, including anonymous union members. */
3062 static int
3063 count_fields (fields)
3064 tree fields;
3066 tree x;
3067 int n_fields = 0;
3068 for (x = fields; x; x = TREE_CHAIN (x))
3070 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3071 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3072 else
3073 n_fields += 1;
3075 return n_fields;
3078 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3079 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3081 static int
3082 add_fields_to_vec (fields, field_vec, idx)
3083 tree fields, field_vec;
3084 int idx;
3086 tree x;
3087 for (x = fields; x; x = TREE_CHAIN (x))
3089 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3090 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3091 else
3092 TREE_VEC_ELT (field_vec, idx++) = x;
3094 return idx;
3097 /* FIELD is a bit-field. We are finishing the processing for its
3098 enclosing type. Issue any appropriate messages and set appropriate
3099 flags. */
3101 static void
3102 check_bitfield_decl (field)
3103 tree field;
3105 tree type = TREE_TYPE (field);
3106 tree w = NULL_TREE;
3108 /* Detect invalid bit-field type. */
3109 if (DECL_INITIAL (field)
3110 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3112 cp_error_at ("bit-field `%#D' with non-integral type", field);
3113 w = error_mark_node;
3116 /* Detect and ignore out of range field width. */
3117 if (DECL_INITIAL (field))
3119 w = DECL_INITIAL (field);
3121 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3122 STRIP_NOPS (w);
3124 /* detect invalid field size. */
3125 if (TREE_CODE (w) == CONST_DECL)
3126 w = DECL_INITIAL (w);
3127 else
3128 w = decl_constant_value (w);
3130 if (TREE_CODE (w) != INTEGER_CST)
3132 cp_error_at ("bit-field `%D' width not an integer constant",
3133 field);
3134 w = error_mark_node;
3136 else if (tree_int_cst_sgn (w) < 0)
3138 cp_error_at ("negative width in bit-field `%D'", field);
3139 w = error_mark_node;
3141 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3143 cp_error_at ("zero width for bit-field `%D'", field);
3144 w = error_mark_node;
3146 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3147 && TREE_CODE (type) != ENUMERAL_TYPE
3148 && TREE_CODE (type) != BOOLEAN_TYPE)
3149 cp_warning_at ("width of `%D' exceeds its type", field);
3150 else if (TREE_CODE (type) == ENUMERAL_TYPE
3151 && (0 > compare_tree_int (w,
3152 min_precision (TYPE_MIN_VALUE (type),
3153 TREE_UNSIGNED (type)))
3154 || 0 > compare_tree_int (w,
3155 min_precision
3156 (TYPE_MAX_VALUE (type),
3157 TREE_UNSIGNED (type)))))
3158 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3159 field, type);
3162 /* Remove the bit-field width indicator so that the rest of the
3163 compiler does not treat that value as an initializer. */
3164 DECL_INITIAL (field) = NULL_TREE;
3166 if (w != error_mark_node)
3168 DECL_SIZE (field) = convert (bitsizetype, w);
3169 DECL_BIT_FIELD (field) = 1;
3171 if (integer_zerop (w))
3173 #ifdef EMPTY_FIELD_BOUNDARY
3174 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3175 EMPTY_FIELD_BOUNDARY);
3176 #endif
3177 #ifdef PCC_BITFIELD_TYPE_MATTERS
3178 if (PCC_BITFIELD_TYPE_MATTERS)
3180 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3181 TYPE_ALIGN (type));
3182 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3184 #endif
3187 else
3189 /* Non-bit-fields are aligned for their type. */
3190 DECL_BIT_FIELD (field) = 0;
3191 CLEAR_DECL_C_BIT_FIELD (field);
3192 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3193 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3197 /* FIELD is a non bit-field. We are finishing the processing for its
3198 enclosing type T. Issue any appropriate messages and set appropriate
3199 flags. */
3201 static void
3202 check_field_decl (field, t, cant_have_const_ctor,
3203 cant_have_default_ctor, no_const_asn_ref,
3204 any_default_members)
3205 tree field;
3206 tree t;
3207 int *cant_have_const_ctor;
3208 int *cant_have_default_ctor;
3209 int *no_const_asn_ref;
3210 int *any_default_members;
3212 tree type = strip_array_types (TREE_TYPE (field));
3214 /* An anonymous union cannot contain any fields which would change
3215 the settings of CANT_HAVE_CONST_CTOR and friends. */
3216 if (ANON_UNION_TYPE_P (type))
3218 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3219 structs. So, we recurse through their fields here. */
3220 else if (ANON_AGGR_TYPE_P (type))
3222 tree fields;
3224 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3225 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3226 check_field_decl (fields, t, cant_have_const_ctor,
3227 cant_have_default_ctor, no_const_asn_ref,
3228 any_default_members);
3230 /* Check members with class type for constructors, destructors,
3231 etc. */
3232 else if (CLASS_TYPE_P (type))
3234 /* Never let anything with uninheritable virtuals
3235 make it through without complaint. */
3236 abstract_virtuals_error (field, type);
3238 if (TREE_CODE (t) == UNION_TYPE)
3240 if (TYPE_NEEDS_CONSTRUCTING (type))
3241 cp_error_at ("member `%#D' with constructor not allowed in union",
3242 field);
3243 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3244 cp_error_at ("member `%#D' with destructor not allowed in union",
3245 field);
3246 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3247 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3248 field);
3250 else
3252 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3253 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3254 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3255 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3256 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3259 if (!TYPE_HAS_CONST_INIT_REF (type))
3260 *cant_have_const_ctor = 1;
3262 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3263 *no_const_asn_ref = 1;
3265 if (TYPE_HAS_CONSTRUCTOR (type)
3266 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3267 *cant_have_default_ctor = 1;
3269 if (DECL_INITIAL (field) != NULL_TREE)
3271 /* `build_class_init_list' does not recognize
3272 non-FIELD_DECLs. */
3273 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3274 cp_error_at ("multiple fields in union `%T' initialized");
3275 *any_default_members = 1;
3278 /* Non-bit-fields are aligned for their type, except packed fields
3279 which require only BITS_PER_UNIT alignment. */
3280 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3281 (DECL_PACKED (field)
3282 ? BITS_PER_UNIT
3283 : TYPE_ALIGN (TREE_TYPE (field))));
3284 if (! DECL_PACKED (field))
3285 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3288 /* Check the data members (both static and non-static), class-scoped
3289 typedefs, etc., appearing in the declaration of T. Issue
3290 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3291 declaration order) of access declarations; each TREE_VALUE in this
3292 list is a USING_DECL.
3294 In addition, set the following flags:
3296 EMPTY_P
3297 The class is empty, i.e., contains no non-static data members.
3299 CANT_HAVE_DEFAULT_CTOR_P
3300 This class cannot have an implicitly generated default
3301 constructor.
3303 CANT_HAVE_CONST_CTOR_P
3304 This class cannot have an implicitly generated copy constructor
3305 taking a const reference.
3307 CANT_HAVE_CONST_ASN_REF_P
3308 This class cannot have an implicitly generated assignment
3309 operator taking a const reference.
3311 All of these flags should be initialized before calling this
3312 function.
3314 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3315 fields can be added by adding to this chain. */
3317 static void
3318 check_field_decls (t, access_decls, empty_p,
3319 cant_have_default_ctor_p,
3320 cant_have_const_ctor_p,
3321 cant_have_const_asn_ref_p)
3322 tree t;
3323 tree *access_decls;
3324 int *empty_p;
3325 int *cant_have_default_ctor_p;
3326 int *cant_have_const_ctor_p;
3327 int *cant_have_const_asn_ref_p;
3329 tree *field;
3330 tree *next;
3331 int has_pointers;
3332 int any_default_members;
3334 /* First, delete any duplicate fields. */
3335 delete_duplicate_fields (TYPE_FIELDS (t));
3337 /* Assume there are no access declarations. */
3338 *access_decls = NULL_TREE;
3339 /* Assume this class has no pointer members. */
3340 has_pointers = 0;
3341 /* Assume none of the members of this class have default
3342 initializations. */
3343 any_default_members = 0;
3345 for (field = &TYPE_FIELDS (t); *field; field = next)
3347 tree x = *field;
3348 tree type = TREE_TYPE (x);
3350 GNU_xref_member (current_class_name, x);
3352 next = &TREE_CHAIN (x);
3354 if (TREE_CODE (x) == FIELD_DECL)
3356 DECL_PACKED (x) |= TYPE_PACKED (t);
3358 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3359 /* We don't treat zero-width bitfields as making a class
3360 non-empty. */
3362 else
3364 /* The class is non-empty. */
3365 *empty_p = 0;
3366 /* The class is not even nearly empty. */
3367 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3371 if (TREE_CODE (x) == USING_DECL)
3373 /* Prune the access declaration from the list of fields. */
3374 *field = TREE_CHAIN (x);
3376 /* Save the access declarations for our caller. */
3377 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3379 /* Since we've reset *FIELD there's no reason to skip to the
3380 next field. */
3381 next = field;
3382 continue;
3385 if (TREE_CODE (x) == TYPE_DECL
3386 || TREE_CODE (x) == TEMPLATE_DECL)
3387 continue;
3389 /* If we've gotten this far, it's a data member, possibly static,
3390 or an enumerator. */
3392 DECL_CONTEXT (x) = t;
3394 /* ``A local class cannot have static data members.'' ARM 9.4 */
3395 if (current_function_decl && TREE_STATIC (x))
3396 cp_error_at ("field `%D' in local class cannot be static", x);
3398 /* Perform error checking that did not get done in
3399 grokdeclarator. */
3400 if (TREE_CODE (type) == FUNCTION_TYPE)
3402 cp_error_at ("field `%D' invalidly declared function type",
3404 type = build_pointer_type (type);
3405 TREE_TYPE (x) = type;
3407 else if (TREE_CODE (type) == METHOD_TYPE)
3409 cp_error_at ("field `%D' invalidly declared method type", x);
3410 type = build_pointer_type (type);
3411 TREE_TYPE (x) = type;
3413 else if (TREE_CODE (type) == OFFSET_TYPE)
3415 cp_error_at ("field `%D' invalidly declared offset type", x);
3416 type = build_pointer_type (type);
3417 TREE_TYPE (x) = type;
3420 if (type == error_mark_node)
3421 continue;
3423 /* When this goes into scope, it will be a non-local reference. */
3424 DECL_NONLOCAL (x) = 1;
3426 if (TREE_CODE (x) == CONST_DECL)
3427 continue;
3429 if (TREE_CODE (x) == VAR_DECL)
3431 if (TREE_CODE (t) == UNION_TYPE)
3432 /* Unions cannot have static members. */
3433 cp_error_at ("field `%D' declared static in union", x);
3435 continue;
3438 /* Now it can only be a FIELD_DECL. */
3440 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3441 CLASSTYPE_NON_AGGREGATE (t) = 1;
3443 /* If this is of reference type, check if it needs an init.
3444 Also do a little ANSI jig if necessary. */
3445 if (TREE_CODE (type) == REFERENCE_TYPE)
3447 CLASSTYPE_NON_POD_P (t) = 1;
3448 if (DECL_INITIAL (x) == NULL_TREE)
3449 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3451 /* ARM $12.6.2: [A member initializer list] (or, for an
3452 aggregate, initialization by a brace-enclosed list) is the
3453 only way to initialize nonstatic const and reference
3454 members. */
3455 *cant_have_default_ctor_p = 1;
3456 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3458 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3459 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3462 type = strip_array_types (type);
3464 if (TREE_CODE (type) == POINTER_TYPE)
3465 has_pointers = 1;
3467 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3468 CLASSTYPE_HAS_MUTABLE (t) = 1;
3470 if (! pod_type_p (type))
3471 /* DR 148 now allows pointers to members (which are POD themselves),
3472 to be allowed in POD structs. */
3473 CLASSTYPE_NON_POD_P (t) = 1;
3475 /* If any field is const, the structure type is pseudo-const. */
3476 if (CP_TYPE_CONST_P (type))
3478 C_TYPE_FIELDS_READONLY (t) = 1;
3479 if (DECL_INITIAL (x) == NULL_TREE)
3480 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3482 /* ARM $12.6.2: [A member initializer list] (or, for an
3483 aggregate, initialization by a brace-enclosed list) is the
3484 only way to initialize nonstatic const and reference
3485 members. */
3486 *cant_have_default_ctor_p = 1;
3487 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3489 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3490 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3492 /* A field that is pseudo-const makes the structure likewise. */
3493 else if (IS_AGGR_TYPE (type))
3495 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3496 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3497 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3500 /* Core issue 80: A nonstatic data member is required to have a
3501 different name from the class iff the class has a
3502 user-defined constructor. */
3503 if (DECL_NAME (x) == constructor_name (t)
3504 && TYPE_HAS_CONSTRUCTOR (t))
3505 cp_pedwarn_at ("field `%#D' with same name as class", x);
3507 /* We set DECL_C_BIT_FIELD in grokbitfield.
3508 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3509 if (DECL_C_BIT_FIELD (x))
3510 check_bitfield_decl (x);
3511 else
3512 check_field_decl (x, t,
3513 cant_have_const_ctor_p,
3514 cant_have_default_ctor_p,
3515 cant_have_const_asn_ref_p,
3516 &any_default_members);
3519 /* Effective C++ rule 11. */
3520 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3521 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3523 cp_warning ("`%#T' has pointer data members", t);
3525 if (! TYPE_HAS_INIT_REF (t))
3527 cp_warning (" but does not override `%T(const %T&)'", t, t);
3528 if (! TYPE_HAS_ASSIGN_REF (t))
3529 cp_warning (" or `operator=(const %T&)'", t);
3531 else if (! TYPE_HAS_ASSIGN_REF (t))
3532 cp_warning (" but does not override `operator=(const %T&)'", t);
3536 /* Check anonymous struct/anonymous union fields. */
3537 finish_struct_anon (t);
3539 /* We've built up the list of access declarations in reverse order.
3540 Fix that now. */
3541 *access_decls = nreverse (*access_decls);
3544 /* If TYPE is an empty class type, records its OFFSET in the table of
3545 OFFSETS. */
3547 static int
3548 record_subobject_offset (type, offset, offsets)
3549 tree type;
3550 tree offset;
3551 splay_tree offsets;
3553 splay_tree_node n;
3555 if (!is_empty_class (type))
3556 return 0;
3558 /* Record the location of this empty object in OFFSETS. */
3559 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3560 if (!n)
3561 n = splay_tree_insert (offsets,
3562 (splay_tree_key) offset,
3563 (splay_tree_value) NULL_TREE);
3564 n->value = ((splay_tree_value)
3565 tree_cons (NULL_TREE,
3566 type,
3567 (tree) n->value));
3569 return 0;
3572 /* Returns non-zero if TYPE is an empty class type and there is
3573 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3575 static int
3576 check_subobject_offset (type, offset, offsets)
3577 tree type;
3578 tree offset;
3579 splay_tree offsets;
3581 splay_tree_node n;
3582 tree t;
3584 if (!is_empty_class (type))
3585 return 0;
3587 /* Record the location of this empty object in OFFSETS. */
3588 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3589 if (!n)
3590 return 0;
3592 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3593 if (same_type_p (TREE_VALUE (t), type))
3594 return 1;
3596 return 0;
3599 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3600 F for every subobject, passing it the type, offset, and table of
3601 OFFSETS. If VBASES_P is non-zero, then even virtual non-primary
3602 bases should be traversed; otherwise, they are ignored.
3604 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3605 than MAX_OFFSET will not be walked.
3607 If F returns a non-zero value, the traversal ceases, and that value
3608 is returned. Otherwise, returns zero. */
3610 static int
3611 walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
3612 tree type;
3613 subobject_offset_fn f;
3614 tree offset;
3615 splay_tree offsets;
3616 tree max_offset;
3617 int vbases_p;
3619 int r = 0;
3621 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3622 stop. */
3623 if (max_offset && INT_CST_LT (max_offset, offset))
3624 return 0;
3626 if (CLASS_TYPE_P (type))
3628 tree field;
3629 int i;
3631 /* Record the location of TYPE. */
3632 r = (*f) (type, offset, offsets);
3633 if (r)
3634 return r;
3636 /* Iterate through the direct base classes of TYPE. */
3637 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3639 tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3641 if (!vbases_p
3642 && TREE_VIA_VIRTUAL (binfo)
3643 && !BINFO_PRIMARY_P (binfo))
3644 continue;
3646 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3648 size_binop (PLUS_EXPR,
3649 offset,
3650 BINFO_OFFSET (binfo)),
3651 offsets,
3652 max_offset,
3653 vbases_p);
3654 if (r)
3655 return r;
3658 /* Iterate through the fields of TYPE. */
3659 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3660 if (TREE_CODE (field) == FIELD_DECL)
3662 r = walk_subobject_offsets (TREE_TYPE (field),
3664 size_binop (PLUS_EXPR,
3665 offset,
3666 DECL_FIELD_OFFSET (field)),
3667 offsets,
3668 max_offset,
3669 /*vbases_p=*/1);
3670 if (r)
3671 return r;
3674 else if (TREE_CODE (type) == ARRAY_TYPE)
3676 tree domain = TYPE_DOMAIN (type);
3677 tree index;
3679 /* Step through each of the elements in the array. */
3680 for (index = size_zero_node;
3681 INT_CST_LT (index, TYPE_MAX_VALUE (domain));
3682 index = size_binop (PLUS_EXPR, index, size_one_node))
3684 r = walk_subobject_offsets (TREE_TYPE (type),
3686 offset,
3687 offsets,
3688 max_offset,
3689 /*vbases_p=*/1);
3690 if (r)
3691 return r;
3692 offset = size_binop (PLUS_EXPR, offset,
3693 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3694 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3695 there's no point in iterating through the remaining
3696 elements of the array. */
3697 if (max_offset && INT_CST_LT (max_offset, offset))
3698 break;
3702 return 0;
3705 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3706 OFFSETS. If VBASES_P is non-zero, virtual bases of TYPE are
3707 examined. */
3709 static void
3710 record_subobject_offsets (type, offset, offsets, vbases_p)
3711 tree type;
3712 tree offset;
3713 splay_tree offsets;
3714 int vbases_p;
3716 walk_subobject_offsets (type, record_subobject_offset, offset,
3717 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3720 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3721 OFFSET) conflict with entries in OFFSETS. If VBASES_P is non-zero,
3722 virtual bases of TYPE are examined. */
3724 static int
3725 layout_conflict_p (type, offset, offsets, vbases_p)
3726 tree type;
3727 tree offset;
3728 splay_tree offsets;
3729 int vbases_p;
3731 splay_tree_node max_node;
3733 /* Get the node in OFFSETS that indicates the maximum offset where
3734 an empty subobject is located. */
3735 max_node = splay_tree_max (offsets);
3736 /* If there aren't any empty subobjects, then there's no point in
3737 performing this check. */
3738 if (!max_node)
3739 return 0;
3741 return walk_subobject_offsets (type, check_subobject_offset, offset,
3742 offsets, (tree) (max_node->key),
3743 vbases_p);
3746 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3747 non-static data member of the type indicated by RLI. BINFO is the
3748 binfo corresponding to the base subobject, OFFSETS maps offsets to
3749 types already located at those offsets. T is the most derived
3750 type. This function determines the position of the DECL. */
3752 static void
3753 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
3754 record_layout_info rli;
3755 tree decl;
3756 tree binfo;
3757 splay_tree offsets;
3758 tree t;
3760 tree offset = NULL_TREE;
3761 tree type = TREE_TYPE (decl);
3762 /* If we are laying out a base class, rather than a field, then
3763 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
3764 int field_p = !DECL_ARTIFICIAL (decl);
3766 /* Try to place the field. It may take more than one try if we have
3767 a hard time placing the field without putting two objects of the
3768 same type at the same address. */
3769 while (1)
3771 struct record_layout_info_s old_rli = *rli;
3773 /* Place this field. */
3774 place_field (rli, decl);
3775 offset = byte_position (decl);
3777 /* We have to check to see whether or not there is already
3778 something of the same type at the offset we're about to use.
3779 For example:
3781 struct S {};
3782 struct T : public S { int i; };
3783 struct U : public S, public T {};
3785 Here, we put S at offset zero in U. Then, we can't put T at
3786 offset zero -- its S component would be at the same address
3787 as the S we already allocated. So, we have to skip ahead.
3788 Since all data members, including those whose type is an
3789 empty class, have non-zero size, any overlap can happen only
3790 with a direct or indirect base-class -- it can't happen with
3791 a data member. */
3792 if (layout_conflict_p (TREE_TYPE (decl),
3793 offset,
3794 offsets,
3795 field_p))
3797 /* Strip off the size allocated to this field. That puts us
3798 at the first place we could have put the field with
3799 proper alignment. */
3800 *rli = old_rli;
3802 /* Bump up by the alignment required for the type. */
3803 rli->bitpos
3804 = size_binop (PLUS_EXPR, rli->bitpos,
3805 bitsize_int (binfo
3806 ? CLASSTYPE_ALIGN (type)
3807 : TYPE_ALIGN (type)));
3808 normalize_rli (rli);
3810 else
3811 /* There was no conflict. We're done laying out this field. */
3812 break;
3815 /* Now that we know where it will be placed, update its
3816 BINFO_OFFSET. */
3817 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3818 propagate_binfo_offsets (binfo,
3819 convert (ssizetype, offset), t);
3822 /* Layout the empty base BINFO. EOC indicates the byte currently just
3823 past the end of the class, and should be correctly aligned for a
3824 class of the type indicated by BINFO; OFFSETS gives the offsets of
3825 the empty bases allocated so far. T is the most derived
3826 type. Return non-zero iff we added it at the end. */
3828 static bool
3829 layout_empty_base (binfo, eoc, offsets, t)
3830 tree binfo;
3831 tree eoc;
3832 splay_tree offsets;
3833 tree t;
3835 tree alignment;
3836 tree basetype = BINFO_TYPE (binfo);
3837 bool atend = false;
3839 /* This routine should only be used for empty classes. */
3840 my_friendly_assert (is_empty_class (basetype), 20000321);
3841 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3843 /* This is an empty base class. We first try to put it at offset
3844 zero. */
3845 if (layout_conflict_p (BINFO_TYPE (binfo),
3846 BINFO_OFFSET (binfo),
3847 offsets,
3848 /*vbases_p=*/0))
3850 /* That didn't work. Now, we move forward from the next
3851 available spot in the class. */
3852 atend = true;
3853 propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
3854 while (1)
3856 if (!layout_conflict_p (BINFO_TYPE (binfo),
3857 BINFO_OFFSET (binfo),
3858 offsets,
3859 /*vbases_p=*/0))
3860 /* We finally found a spot where there's no overlap. */
3861 break;
3863 /* There's overlap here, too. Bump along to the next spot. */
3864 propagate_binfo_offsets (binfo, alignment, t);
3867 return atend;
3870 /* Build a FIELD_DECL for the base given by BINFO in the class
3871 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
3872 *BASE_ALIGN is a running maximum of the alignments of any base
3873 class. OFFSETS gives the location of empty base subobjects. T is
3874 the most derived type. Return non-zero if the new object cannot be
3875 nearly-empty. */
3877 static bool
3878 build_base_field (rli, binfo, empty_p, offsets, t)
3879 record_layout_info rli;
3880 tree binfo;
3881 int *empty_p;
3882 splay_tree offsets;
3883 tree t;
3885 tree basetype = BINFO_TYPE (binfo);
3886 tree decl;
3887 bool atend = false;
3889 if (!COMPLETE_TYPE_P (basetype))
3890 /* This error is now reported in xref_tag, thus giving better
3891 location information. */
3892 return atend;
3894 decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
3895 DECL_ARTIFICIAL (decl) = 1;
3896 DECL_FIELD_CONTEXT (decl) = rli->t;
3897 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3898 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3899 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3900 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3902 if (!integer_zerop (DECL_SIZE (decl)))
3904 /* The containing class is non-empty because it has a non-empty
3905 base class. */
3906 *empty_p = 0;
3908 /* Try to place the field. It may take more than one try if we
3909 have a hard time placing the field without putting two
3910 objects of the same type at the same address. */
3911 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t);
3913 else
3915 unsigned HOST_WIDE_INT eoc;
3917 /* On some platforms (ARM), even empty classes will not be
3918 byte-aligned. */
3919 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
3920 eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
3921 atend |= layout_empty_base (binfo, size_int (eoc), offsets, t);
3924 /* Record the offsets of BINFO and its base subobjects. */
3925 record_subobject_offsets (BINFO_TYPE (binfo),
3926 BINFO_OFFSET (binfo),
3927 offsets,
3928 /*vbases_p=*/0);
3929 return atend;
3932 /* Layout all of the non-virtual base classes. Record empty
3933 subobjects in OFFSETS. T is the most derived type. Return
3934 non-zero if the type cannot be nearly empty. */
3936 static bool
3937 build_base_fields (rli, empty_p, offsets, t)
3938 record_layout_info rli;
3939 int *empty_p;
3940 splay_tree offsets;
3941 tree t;
3943 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3944 subobjects. */
3945 tree rec = rli->t;
3946 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3947 int i;
3948 bool atend = 0;
3950 /* The primary base class is always allocated first. */
3951 if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
3952 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
3953 empty_p, offsets, t);
3955 /* Now allocate the rest of the bases. */
3956 for (i = 0; i < n_baseclasses; ++i)
3958 tree base_binfo;
3960 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3962 /* The primary base was already allocated above, so we don't
3963 need to allocate it again here. */
3964 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
3965 continue;
3967 /* A primary virtual base class is allocated just like any other
3968 base class, but a non-primary virtual base is allocated
3969 later, in layout_virtual_bases. */
3970 if (TREE_VIA_VIRTUAL (base_binfo)
3971 && !BINFO_PRIMARY_P (base_binfo))
3972 continue;
3974 atend |= build_base_field (rli, base_binfo, empty_p, offsets, t);
3976 return atend;
3979 /* Go through the TYPE_METHODS of T issuing any appropriate
3980 diagnostics, figuring out which methods override which other
3981 methods, and so forth. */
3983 static void
3984 check_methods (t)
3985 tree t;
3987 tree x;
3989 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3991 GNU_xref_member (current_class_name, x);
3993 /* If this was an evil function, don't keep it in class. */
3994 if (DECL_ASSEMBLER_NAME_SET_P (x)
3995 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3996 continue;
3998 check_for_override (x, t);
3999 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4000 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4002 /* The name of the field is the original field name
4003 Save this in auxiliary field for later overloading. */
4004 if (DECL_VINDEX (x))
4006 TYPE_POLYMORPHIC_P (t) = 1;
4007 if (DECL_PURE_VIRTUAL_P (x))
4008 CLASSTYPE_PURE_VIRTUALS (t)
4009 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4014 /* FN is a constructor or destructor. Clone the declaration to create
4015 a specialized in-charge or not-in-charge version, as indicated by
4016 NAME. */
4018 static tree
4019 build_clone (fn, name)
4020 tree fn;
4021 tree name;
4023 tree parms;
4024 tree clone;
4026 /* Copy the function. */
4027 clone = copy_decl (fn);
4028 /* Remember where this function came from. */
4029 DECL_CLONED_FUNCTION (clone) = fn;
4030 DECL_ABSTRACT_ORIGIN (clone) = fn;
4031 /* Reset the function name. */
4032 DECL_NAME (clone) = name;
4033 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4034 /* There's no pending inline data for this function. */
4035 DECL_PENDING_INLINE_INFO (clone) = NULL;
4036 DECL_PENDING_INLINE_P (clone) = 0;
4037 /* And it hasn't yet been deferred. */
4038 DECL_DEFERRED_FN (clone) = 0;
4040 /* The base-class destructor is not virtual. */
4041 if (name == base_dtor_identifier)
4043 DECL_VIRTUAL_P (clone) = 0;
4044 if (TREE_CODE (clone) != TEMPLATE_DECL)
4045 DECL_VINDEX (clone) = NULL_TREE;
4048 /* If there was an in-charge parameter, drop it from the function
4049 type. */
4050 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4052 tree basetype;
4053 tree parmtypes;
4054 tree exceptions;
4056 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4057 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4058 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4059 /* Skip the `this' parameter. */
4060 parmtypes = TREE_CHAIN (parmtypes);
4061 /* Skip the in-charge parameter. */
4062 parmtypes = TREE_CHAIN (parmtypes);
4063 /* And the VTT parm, in a complete [cd]tor. */
4064 if (DECL_HAS_VTT_PARM_P (fn)
4065 && ! DECL_NEEDS_VTT_PARM_P (clone))
4066 parmtypes = TREE_CHAIN (parmtypes);
4067 /* If this is subobject constructor or destructor, add the vtt
4068 parameter. */
4069 TREE_TYPE (clone)
4070 = build_cplus_method_type (basetype,
4071 TREE_TYPE (TREE_TYPE (clone)),
4072 parmtypes);
4073 if (exceptions)
4074 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4075 exceptions);
4078 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
4079 aren't function parameters; those are the template parameters. */
4080 if (TREE_CODE (clone) != TEMPLATE_DECL)
4082 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4083 /* Remove the in-charge parameter. */
4084 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4086 TREE_CHAIN (DECL_ARGUMENTS (clone))
4087 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4088 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4090 /* And the VTT parm, in a complete [cd]tor. */
4091 if (DECL_HAS_VTT_PARM_P (fn))
4093 if (DECL_NEEDS_VTT_PARM_P (clone))
4094 DECL_HAS_VTT_PARM_P (clone) = 1;
4095 else
4097 TREE_CHAIN (DECL_ARGUMENTS (clone))
4098 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4099 DECL_HAS_VTT_PARM_P (clone) = 0;
4103 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4105 DECL_CONTEXT (parms) = clone;
4106 copy_lang_decl (parms);
4110 /* Create the RTL for this function. */
4111 SET_DECL_RTL (clone, NULL_RTX);
4112 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4114 /* Make it easy to find the CLONE given the FN. */
4115 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4116 TREE_CHAIN (fn) = clone;
4118 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4119 if (TREE_CODE (clone) == TEMPLATE_DECL)
4121 tree result;
4123 DECL_TEMPLATE_RESULT (clone)
4124 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4125 result = DECL_TEMPLATE_RESULT (clone);
4126 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4127 DECL_TI_TEMPLATE (result) = clone;
4129 else if (DECL_DEFERRED_FN (fn))
4130 defer_fn (clone);
4132 return clone;
4135 /* Produce declarations for all appropriate clones of FN. If
4136 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4137 CLASTYPE_METHOD_VEC as well. */
4139 void
4140 clone_function_decl (fn, update_method_vec_p)
4141 tree fn;
4142 int update_method_vec_p;
4144 tree clone;
4146 /* Avoid inappropriate cloning. */
4147 if (TREE_CHAIN (fn)
4148 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4149 return;
4151 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4153 /* For each constructor, we need two variants: an in-charge version
4154 and a not-in-charge version. */
4155 clone = build_clone (fn, complete_ctor_identifier);
4156 if (update_method_vec_p)
4157 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4158 clone = build_clone (fn, base_ctor_identifier);
4159 if (update_method_vec_p)
4160 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4162 else
4164 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4166 /* For each destructor, we need three variants: an in-charge
4167 version, a not-in-charge version, and an in-charge deleting
4168 version. We clone the deleting version first because that
4169 means it will go second on the TYPE_METHODS list -- and that
4170 corresponds to the correct layout order in the virtual
4171 function table.
4173 For a non-virtual destructor, we do not build a deleting
4174 destructor. */
4175 if (DECL_VIRTUAL_P (fn))
4177 clone = build_clone (fn, deleting_dtor_identifier);
4178 if (update_method_vec_p)
4179 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4181 clone = build_clone (fn, complete_dtor_identifier);
4182 if (update_method_vec_p)
4183 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4184 clone = build_clone (fn, base_dtor_identifier);
4185 if (update_method_vec_p)
4186 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4189 /* Note that this is an abstract function that is never emitted. */
4190 DECL_ABSTRACT (fn) = 1;
4193 /* DECL is an in charge constructor, which is being defined. This will
4194 have had an in class declaration, from whence clones were
4195 declared. An out-of-class definition can specify additional default
4196 arguments. As it is the clones that are involved in overload
4197 resolution, we must propagate the information from the DECL to its
4198 clones. */
4200 void
4201 adjust_clone_args (decl)
4202 tree decl;
4204 tree clone;
4206 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4207 clone = TREE_CHAIN (clone))
4209 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4210 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4211 tree decl_parms, clone_parms;
4213 clone_parms = orig_clone_parms;
4215 /* Skip the 'this' parameter. */
4216 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4217 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4219 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4220 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4221 if (DECL_HAS_VTT_PARM_P (decl))
4222 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4224 clone_parms = orig_clone_parms;
4225 if (DECL_HAS_VTT_PARM_P (clone))
4226 clone_parms = TREE_CHAIN (clone_parms);
4228 for (decl_parms = orig_decl_parms; decl_parms;
4229 decl_parms = TREE_CHAIN (decl_parms),
4230 clone_parms = TREE_CHAIN (clone_parms))
4232 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4233 TREE_TYPE (clone_parms)), 20010424);
4235 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4237 /* A default parameter has been added. Adjust the
4238 clone's parameters. */
4239 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4240 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4241 tree type;
4243 clone_parms = orig_decl_parms;
4245 if (DECL_HAS_VTT_PARM_P (clone))
4247 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4248 TREE_VALUE (orig_clone_parms),
4249 clone_parms);
4250 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4252 type = build_cplus_method_type (basetype,
4253 TREE_TYPE (TREE_TYPE (clone)),
4254 clone_parms);
4255 if (exceptions)
4256 type = build_exception_variant (type, exceptions);
4257 TREE_TYPE (clone) = type;
4259 clone_parms = NULL_TREE;
4260 break;
4263 my_friendly_assert (!clone_parms, 20010424);
4267 /* For each of the constructors and destructors in T, create an
4268 in-charge and not-in-charge variant. */
4270 static void
4271 clone_constructors_and_destructors (t)
4272 tree t;
4274 tree fns;
4276 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4277 out now. */
4278 if (!CLASSTYPE_METHOD_VEC (t))
4279 return;
4281 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4282 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4283 clone_function_decl (CLASSTYPE_DESTRUCTOR (t), /*update_method_vec_p=*/1);
4286 /* Remove all zero-width bit-fields from T. */
4288 static void
4289 remove_zero_width_bit_fields (t)
4290 tree t;
4292 tree *fieldsp;
4294 fieldsp = &TYPE_FIELDS (t);
4295 while (*fieldsp)
4297 if (TREE_CODE (*fieldsp) == FIELD_DECL
4298 && DECL_C_BIT_FIELD (*fieldsp)
4299 && DECL_INITIAL (*fieldsp))
4300 *fieldsp = TREE_CHAIN (*fieldsp);
4301 else
4302 fieldsp = &TREE_CHAIN (*fieldsp);
4306 /* Returns TRUE iff we need a cookie when dynamically allocating an
4307 array whose elements have the indicated class TYPE. */
4309 static bool
4310 type_requires_array_cookie (type)
4311 tree type;
4313 tree fns;
4314 bool has_two_argument_delete_p = false;
4316 my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4318 /* If there's a non-trivial destructor, we need a cookie. In order
4319 to iterate through the array calling the destructor for each
4320 element, we'll have to know how many elements there are. */
4321 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4322 return true;
4324 /* If the usual deallocation function is a two-argument whose second
4325 argument is of type `size_t', then we have to pass the size of
4326 the array to the deallocation function, so we will need to store
4327 a cookie. */
4328 fns = lookup_fnfields (TYPE_BINFO (type),
4329 ansi_opname (VEC_DELETE_EXPR),
4330 /*protect=*/0);
4331 /* If there are no `operator []' members, or the lookup is
4332 ambiguous, then we don't need a cookie. */
4333 if (!fns || fns == error_mark_node)
4334 return false;
4335 /* Loop through all of the functions. */
4336 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4338 tree fn;
4339 tree second_parm;
4341 /* Select the current function. */
4342 fn = OVL_CURRENT (fns);
4343 /* See if this function is a one-argument delete function. If
4344 it is, then it will be the usual deallocation function. */
4345 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4346 if (second_parm == void_list_node)
4347 return false;
4348 /* Otherwise, if we have a two-argument function and the second
4349 argument is `size_t', it will be the usual deallocation
4350 function -- unless there is one-argument function, too. */
4351 if (TREE_CHAIN (second_parm) == void_list_node
4352 && same_type_p (TREE_VALUE (second_parm), sizetype))
4353 has_two_argument_delete_p = true;
4356 return has_two_argument_delete_p;
4359 /* Check the validity of the bases and members declared in T. Add any
4360 implicitly-generated functions (like copy-constructors and
4361 assignment operators). Compute various flag bits (like
4362 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4363 level: i.e., independently of the ABI in use. */
4365 static void
4366 check_bases_and_members (t, empty_p)
4367 tree t;
4368 int *empty_p;
4370 /* Nonzero if we are not allowed to generate a default constructor
4371 for this case. */
4372 int cant_have_default_ctor;
4373 /* Nonzero if the implicitly generated copy constructor should take
4374 a non-const reference argument. */
4375 int cant_have_const_ctor;
4376 /* Nonzero if the the implicitly generated assignment operator
4377 should take a non-const reference argument. */
4378 int no_const_asn_ref;
4379 tree access_decls;
4381 /* By default, we use const reference arguments and generate default
4382 constructors. */
4383 cant_have_default_ctor = 0;
4384 cant_have_const_ctor = 0;
4385 no_const_asn_ref = 0;
4387 /* Assume that the class is nearly empty; we'll clear this flag if
4388 it turns out not to be nearly empty. */
4389 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4391 /* Check all the base-classes. */
4392 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4393 &no_const_asn_ref);
4395 /* Check all the data member declarations. */
4396 check_field_decls (t, &access_decls, empty_p,
4397 &cant_have_default_ctor,
4398 &cant_have_const_ctor,
4399 &no_const_asn_ref);
4401 /* Check all the method declarations. */
4402 check_methods (t);
4404 /* A nearly-empty class has to be vptr-containing; a nearly empty
4405 class contains just a vptr. */
4406 if (!TYPE_CONTAINS_VPTR_P (t))
4407 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4409 /* Do some bookkeeping that will guide the generation of implicitly
4410 declared member functions. */
4411 TYPE_HAS_COMPLEX_INIT_REF (t)
4412 |= (TYPE_HAS_INIT_REF (t)
4413 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4414 || TYPE_POLYMORPHIC_P (t));
4415 TYPE_NEEDS_CONSTRUCTING (t)
4416 |= (TYPE_HAS_CONSTRUCTOR (t)
4417 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4418 || TYPE_POLYMORPHIC_P (t));
4419 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4420 || TYPE_POLYMORPHIC_P (t));
4421 CLASSTYPE_NON_POD_P (t)
4422 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4423 || TYPE_HAS_ASSIGN_REF (t));
4424 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4425 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4426 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4428 /* Synthesize any needed methods. Note that methods will be
4429 synthesized for anonymous unions but undone later by
4430 fixup_anonymous_aggr. */
4431 add_implicitly_declared_members (t, cant_have_default_ctor,
4432 cant_have_const_ctor,
4433 no_const_asn_ref);
4435 /* Create the in-charge and not-in-charge variants of constructors
4436 and destructors. */
4437 clone_constructors_and_destructors (t);
4439 /* Process the using-declarations. */
4440 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4441 handle_using_decl (TREE_VALUE (access_decls), t);
4443 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4444 finish_struct_methods (t);
4446 /* Figure out whether or not we will need a cookie when dynamically
4447 allocating an array of this type. */
4448 TYPE_LANG_SPECIFIC (t)->vec_new_uses_cookie
4449 = type_requires_array_cookie (t);
4452 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4453 accordingly. If a new vfield was created (because T doesn't have a
4454 primary base class), then the newly created field is returned. It
4455 is not added to the TYPE_FIELDS list; it is the caller's
4456 responsibility to do that. */
4458 static tree
4459 create_vtable_ptr (t, empty_p, vfuns_p,
4460 new_virtuals_p, overridden_virtuals_p)
4461 tree t;
4462 int *empty_p;
4463 int *vfuns_p;
4464 tree *new_virtuals_p;
4465 tree *overridden_virtuals_p;
4467 tree fn;
4469 /* Loop over the virtual functions, adding them to our various
4470 vtables. */
4471 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4472 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
4473 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4474 vfuns_p, fn, t);
4476 /* If we couldn't find an appropriate base class, create a new field
4477 here. Even if there weren't any new virtual functions, we might need a
4478 new virtual function table if we're supposed to include vptrs in
4479 all classes that need them. */
4480 if (!TYPE_VFIELD (t) && (*vfuns_p || TYPE_CONTAINS_VPTR_P (t)))
4482 /* We build this decl with vtbl_ptr_type_node, which is a
4483 `vtable_entry_type*'. It might seem more precise to use
4484 `vtable_entry_type (*)[N]' where N is the number of firtual
4485 functions. However, that would require the vtable pointer in
4486 base classes to have a different type than the vtable pointer
4487 in derived classes. We could make that happen, but that
4488 still wouldn't solve all the problems. In particular, the
4489 type-based alias analysis code would decide that assignments
4490 to the base class vtable pointer can't alias assignments to
4491 the derived class vtable pointer, since they have different
4492 types. Thus, in an derived class destructor, where the base
4493 class constructor was inlined, we could generate bad code for
4494 setting up the vtable pointer.
4496 Therefore, we use one type for all vtable pointers. We still
4497 use a type-correct type; it just doesn't indicate the array
4498 bounds. That's better than using `void*' or some such; it's
4499 cleaner, and it let's the alias analysis code know that these
4500 stores cannot alias stores to void*! */
4501 tree field;
4503 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4504 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4505 DECL_VIRTUAL_P (field) = 1;
4506 DECL_ARTIFICIAL (field) = 1;
4507 DECL_FIELD_CONTEXT (field) = t;
4508 DECL_FCONTEXT (field) = t;
4509 DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4510 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4512 TYPE_VFIELD (t) = field;
4514 /* This class is non-empty. */
4515 *empty_p = 0;
4517 if (CLASSTYPE_N_BASECLASSES (t))
4518 /* If there were any baseclasses, they can't possibly be at
4519 offset zero any more, because that's where the vtable
4520 pointer is. So, converting to a base class is going to
4521 take work. */
4522 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4524 return field;
4527 return NULL_TREE;
4530 /* Fixup the inline function given by INFO now that the class is
4531 complete. */
4533 static void
4534 fixup_pending_inline (fn)
4535 tree fn;
4537 if (DECL_PENDING_INLINE_INFO (fn))
4539 tree args = DECL_ARGUMENTS (fn);
4540 while (args)
4542 DECL_CONTEXT (args) = fn;
4543 args = TREE_CHAIN (args);
4548 /* Fixup the inline methods and friends in TYPE now that TYPE is
4549 complete. */
4551 static void
4552 fixup_inline_methods (type)
4553 tree type;
4555 tree method;
4557 /* Do inline member functions. */
4558 for (method = TYPE_METHODS (type);
4559 method;
4560 method = TREE_CHAIN (method))
4561 fixup_pending_inline (method);
4563 /* Do friends. */
4564 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4565 method;
4566 method = TREE_CHAIN (method))
4567 fixup_pending_inline (TREE_VALUE (method));
4568 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4571 /* Add OFFSET to all base types of BINFO which is a base in the
4572 hierarchy dominated by T.
4574 OFFSET, which is a type offset, is number of bytes. */
4576 static void
4577 propagate_binfo_offsets (binfo, offset, t)
4578 tree binfo;
4579 tree offset;
4580 tree t;
4582 int i;
4583 tree primary_binfo;
4585 /* Update BINFO's offset. */
4586 BINFO_OFFSET (binfo)
4587 = convert (sizetype,
4588 size_binop (PLUS_EXPR,
4589 convert (ssizetype, BINFO_OFFSET (binfo)),
4590 offset));
4592 /* Find the primary base class. */
4593 primary_binfo = get_primary_binfo (binfo);
4595 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4596 downwards. */
4597 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4599 tree base_binfo;
4601 /* On the first time through the loop, do the primary base.
4602 Because the primary base need not be an immediate base, we
4603 must handle the primary base specially. */
4604 if (i == -1)
4606 if (!primary_binfo)
4607 continue;
4609 base_binfo = primary_binfo;
4611 else
4613 base_binfo = BINFO_BASETYPE (binfo, i);
4614 /* Don't do the primary base twice. */
4615 if (base_binfo == primary_binfo)
4616 continue;
4619 /* Skip virtual bases that aren't our canonical primary base. */
4620 if (TREE_VIA_VIRTUAL (base_binfo)
4621 && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4622 || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4623 continue;
4625 propagate_binfo_offsets (base_binfo, offset, t);
4629 /* Called via dfs_walk from layout_virtual bases. */
4631 static tree
4632 dfs_set_offset_for_unshared_vbases (binfo, data)
4633 tree binfo;
4634 void *data;
4636 /* If this is a virtual base, make sure it has the same offset as
4637 the shared copy. If it's a primary base, then we know it's
4638 correct. */
4639 if (TREE_VIA_VIRTUAL (binfo))
4641 tree t = (tree) data;
4642 tree vbase;
4643 tree offset;
4645 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4646 if (vbase != binfo)
4648 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4649 propagate_binfo_offsets (binfo, offset, t);
4653 return NULL_TREE;
4656 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4657 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4658 empty subobjects of T. */
4660 static void
4661 layout_virtual_bases (t, offsets)
4662 tree t;
4663 splay_tree offsets;
4665 tree vbases;
4666 unsigned HOST_WIDE_INT dsize;
4667 unsigned HOST_WIDE_INT eoc;
4669 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4670 return;
4672 #ifdef STRUCTURE_SIZE_BOUNDARY
4673 /* Packed structures don't need to have minimum size. */
4674 if (! TYPE_PACKED (t))
4675 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4676 #endif
4678 /* DSIZE is the size of the class without the virtual bases. */
4679 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4681 /* Make every class have alignment of at least one. */
4682 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4684 /* Go through the virtual bases, allocating space for each virtual
4685 base that is not already a primary base class. These are
4686 allocated in inheritance graph order. */
4687 for (vbases = TYPE_BINFO (t);
4688 vbases;
4689 vbases = TREE_CHAIN (vbases))
4691 tree vbase;
4693 if (!TREE_VIA_VIRTUAL (vbases))
4694 continue;
4695 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4697 if (!BINFO_PRIMARY_P (vbase))
4699 /* This virtual base is not a primary base of any class in the
4700 hierarchy, so we have to add space for it. */
4701 tree basetype;
4702 unsigned int desired_align;
4704 basetype = BINFO_TYPE (vbase);
4706 desired_align = CLASSTYPE_ALIGN (basetype);
4707 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4709 /* Add padding so that we can put the virtual base class at an
4710 appropriately aligned offset. */
4711 dsize = CEIL (dsize, desired_align) * desired_align;
4713 /* We try to squish empty virtual bases in just like
4714 ordinary empty bases. */
4715 if (is_empty_class (basetype))
4716 layout_empty_base (vbase,
4717 size_int (CEIL (dsize, BITS_PER_UNIT)),
4718 offsets, t);
4719 else
4721 tree offset;
4723 offset = ssize_int (CEIL (dsize, BITS_PER_UNIT));
4724 offset = size_diffop (offset,
4725 convert (ssizetype,
4726 BINFO_OFFSET (vbase)));
4728 /* And compute the offset of the virtual base. */
4729 propagate_binfo_offsets (vbase, offset, t);
4730 /* Every virtual baseclass takes a least a UNIT, so that
4731 we can take it's address and get something different
4732 for each base. */
4733 dsize += MAX (BITS_PER_UNIT,
4734 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4737 /* Keep track of the offsets assigned to this virtual base. */
4738 record_subobject_offsets (BINFO_TYPE (vbase),
4739 BINFO_OFFSET (vbase),
4740 offsets,
4741 /*vbases_p=*/0);
4745 /* Now, go through the TYPE_BINFO hierarchy, setting the
4746 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4747 bases and their direct and indirect bases. The ambiguity checks
4748 in get_base_distance depend on the BINFO_OFFSETs being set
4749 correctly. */
4750 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4752 /* If we had empty base classes that protruded beyond the end of the
4753 class, we didn't update DSIZE above; we were hoping to overlay
4754 multiple such bases at the same location. */
4755 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4756 if (eoc * BITS_PER_UNIT > dsize)
4757 dsize = eoc * BITS_PER_UNIT;
4759 /* Now, make sure that the total size of the type is a multiple of
4760 its alignment. */
4761 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4762 TYPE_SIZE (t) = bitsize_int (dsize);
4763 TYPE_SIZE_UNIT (t) = convert (sizetype,
4764 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4765 bitsize_unit_node));
4767 /* Check for ambiguous virtual bases. */
4768 if (extra_warnings)
4769 for (vbases = CLASSTYPE_VBASECLASSES (t);
4770 vbases;
4771 vbases = TREE_CHAIN (vbases))
4773 tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
4774 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4775 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4776 basetype, t);
4780 /* Returns the offset of the byte just past the end of the base class
4781 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4782 only non-virtual bases are included. */
4784 static unsigned HOST_WIDE_INT
4785 end_of_class (t, include_virtuals_p)
4786 tree t;
4787 int include_virtuals_p;
4789 unsigned HOST_WIDE_INT result = 0;
4790 int i;
4792 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4794 tree base_binfo;
4795 tree offset;
4796 tree size;
4797 unsigned HOST_WIDE_INT end_of_base;
4799 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4801 if (!include_virtuals_p
4802 && TREE_VIA_VIRTUAL (base_binfo)
4803 && !BINFO_PRIMARY_P (base_binfo))
4804 continue;
4806 if (is_empty_class (BINFO_TYPE (base_binfo)))
4807 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4808 allocate some space for it. It cannot have virtual bases,
4809 so TYPE_SIZE_UNIT is fine. */
4810 size = TYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4811 else
4812 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4813 offset = size_binop (PLUS_EXPR,
4814 BINFO_OFFSET (base_binfo),
4815 size);
4816 end_of_base = tree_low_cst (offset, /*pos=*/1);
4817 if (end_of_base > result)
4818 result = end_of_base;
4821 return result;
4824 /* Warn about direct bases of T that are inaccessible because they are
4825 ambiguous. For example:
4827 struct S {};
4828 struct T : public S {};
4829 struct U : public S, public T {};
4831 Here, `(S*) new U' is not allowed because there are two `S'
4832 subobjects of U. */
4834 static void
4835 warn_about_ambiguous_direct_bases (t)
4836 tree t;
4838 int i;
4840 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4842 tree basetype = TYPE_BINFO_BASETYPE (t, i);
4844 if (get_base_distance (basetype, t, 0, NULL) == -2)
4845 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4846 basetype, t);
4850 /* Compare two INTEGER_CSTs K1 and K2. */
4852 static int
4853 splay_tree_compare_integer_csts (k1, k2)
4854 splay_tree_key k1;
4855 splay_tree_key k2;
4857 return tree_int_cst_compare ((tree) k1, (tree) k2);
4860 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4861 BINFO_OFFSETs for all of the base-classes. Position the vtable
4862 pointer. */
4864 static void
4865 layout_class_type (t, empty_p, vfuns_p,
4866 new_virtuals_p, overridden_virtuals_p)
4867 tree t;
4868 int *empty_p;
4869 int *vfuns_p;
4870 tree *new_virtuals_p;
4871 tree *overridden_virtuals_p;
4873 tree non_static_data_members;
4874 tree field;
4875 tree vptr;
4876 record_layout_info rli;
4877 unsigned HOST_WIDE_INT eoc;
4878 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4879 types that appear at that offset. */
4880 splay_tree empty_base_offsets;
4882 /* Keep track of the first non-static data member. */
4883 non_static_data_members = TYPE_FIELDS (t);
4885 /* Start laying out the record. */
4886 rli = start_record_layout (t);
4888 /* If possible, we reuse the virtual function table pointer from one
4889 of our base classes. */
4890 determine_primary_base (t, vfuns_p);
4892 /* Create a pointer to our virtual function table. */
4893 vptr = create_vtable_ptr (t, empty_p, vfuns_p,
4894 new_virtuals_p, overridden_virtuals_p);
4896 /* The vptr is always the first thing in the class. */
4897 if (vptr)
4899 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4900 place_field (rli, vptr);
4903 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4904 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4905 NULL, NULL);
4906 if (build_base_fields (rli, empty_p, empty_base_offsets, t))
4907 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4909 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4910 we have to save this before we zap TYPE_NONCOPIED_PARTS. */
4911 fixup_inline_methods (t);
4913 /* Layout the non-static data members. */
4914 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4916 tree type;
4917 tree padding;
4919 /* We still pass things that aren't non-static data members to
4920 the back-end, in case it wants to do something with them. */
4921 if (TREE_CODE (field) != FIELD_DECL)
4923 place_field (rli, field);
4924 continue;
4927 type = TREE_TYPE (field);
4929 /* If this field is a bit-field whose width is greater than its
4930 type, then there are some special rules for allocating
4931 it. */
4932 if (DECL_C_BIT_FIELD (field)
4933 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4935 integer_type_kind itk;
4936 tree integer_type;
4938 /* We must allocate the bits as if suitably aligned for the
4939 longest integer type that fits in this many bits. type
4940 of the field. Then, we are supposed to use the left over
4941 bits as additional padding. */
4942 for (itk = itk_char; itk != itk_none; ++itk)
4943 if (INT_CST_LT (DECL_SIZE (field),
4944 TYPE_SIZE (integer_types[itk])))
4945 break;
4947 /* ITK now indicates a type that is too large for the
4948 field. We have to back up by one to find the largest
4949 type that fits. */
4950 integer_type = integer_types[itk - 1];
4951 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4952 TYPE_SIZE (integer_type));
4953 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4954 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4955 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4957 else
4958 padding = NULL_TREE;
4960 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4961 empty_base_offsets, t);
4963 /* If we needed additional padding after this field, add it
4964 now. */
4965 if (padding)
4967 tree padding_field;
4969 padding_field = build_decl (FIELD_DECL,
4970 NULL_TREE,
4971 char_type_node);
4972 DECL_BIT_FIELD (padding_field) = 1;
4973 DECL_SIZE (padding_field) = padding;
4974 DECL_ALIGN (padding_field) = 1;
4975 DECL_USER_ALIGN (padding_field) = 0;
4976 layout_nonempty_base_or_field (rli, padding_field,
4977 NULL_TREE,
4978 empty_base_offsets, t);
4982 /* It might be the case that we grew the class to allocate a
4983 zero-sized base class. That won't be reflected in RLI, yet,
4984 because we are willing to overlay multiple bases at the same
4985 offset. However, now we need to make sure that RLI is big enough
4986 to reflect the entire class. */
4987 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4988 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4989 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
4991 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc));
4992 rli->bitpos = bitsize_zero_node;
4995 /* We make all structures have at least one element, so that they
4996 have non-zero size. The class may be empty even if it has
4997 basetypes. Therefore, we add the fake field after all the other
4998 fields; if there are already FIELD_DECLs on the list, their
4999 offsets will not be disturbed. */
5000 if (!eoc && *empty_p)
5002 tree padding;
5004 padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
5005 place_field (rli, padding);
5008 /* Let the back-end lay out the type. Note that at this point we
5009 have only included non-virtual base-classes; we will lay out the
5010 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
5011 this call are not necessarily correct; they are just the size and
5012 alignment when no virtual base clases are used. */
5013 finish_record_layout (rli);
5015 /* Delete all zero-width bit-fields from the list of fields. Now
5016 that the type is laid out they are no longer important. */
5017 remove_zero_width_bit_fields (t);
5019 /* Remember the size and alignment of the class before adding
5020 the virtual bases. */
5021 if (*empty_p)
5023 CLASSTYPE_SIZE (t) = bitsize_zero_node;
5024 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
5026 else
5028 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
5029 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
5032 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5033 CLASSTYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (t);
5035 /* Set the TYPE_DECL for this type to contain the right
5036 value for DECL_OFFSET, so that we can use it as part
5037 of a COMPONENT_REF for multiple inheritance. */
5038 layout_decl (TYPE_MAIN_DECL (t), 0);
5040 /* Now fix up any virtual base class types that we left lying
5041 around. We must get these done before we try to lay out the
5042 virtual function table. As a side-effect, this will remove the
5043 base subobject fields. */
5044 layout_virtual_bases (t, empty_base_offsets);
5046 /* Warn about direct bases that can't be talked about due to
5047 ambiguity. */
5048 warn_about_ambiguous_direct_bases (t);
5050 /* Clean up. */
5051 splay_tree_delete (empty_base_offsets);
5054 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5055 (or C++ class declaration).
5057 For C++, we must handle the building of derived classes.
5058 Also, C++ allows static class members. The way that this is
5059 handled is to keep the field name where it is (as the DECL_NAME
5060 of the field), and place the overloaded decl in the bit position
5061 of the field. layout_record and layout_union will know about this.
5063 More C++ hair: inline functions have text in their
5064 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5065 meaningful tree structure. After the struct has been laid out, set
5066 things up so that this can happen.
5068 And still more: virtual functions. In the case of single inheritance,
5069 when a new virtual function is seen which redefines a virtual function
5070 from the base class, the new virtual function is placed into
5071 the virtual function table at exactly the same address that
5072 it had in the base class. When this is extended to multiple
5073 inheritance, the same thing happens, except that multiple virtual
5074 function tables must be maintained. The first virtual function
5075 table is treated in exactly the same way as in the case of single
5076 inheritance. Additional virtual function tables have different
5077 DELTAs, which tell how to adjust `this' to point to the right thing.
5079 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5081 void
5082 finish_struct_1 (t)
5083 tree t;
5085 tree x;
5086 int vfuns;
5087 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5088 a FUNCTION_DECL. Each of these functions is a virtual function
5089 declared in T that does not override any virtual function from a
5090 base class. */
5091 tree new_virtuals = NULL_TREE;
5092 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5093 except that each declaration here overrides the declaration from
5094 a base class. */
5095 tree overridden_virtuals = NULL_TREE;
5096 int n_fields = 0;
5097 tree vfield;
5098 int empty = 1;
5100 if (COMPLETE_TYPE_P (t))
5102 if (IS_AGGR_TYPE (t))
5103 cp_error ("redefinition of `%#T'", t);
5104 else
5105 my_friendly_abort (172);
5106 popclass ();
5107 return;
5110 GNU_xref_decl (current_function_decl, t);
5112 /* If this type was previously laid out as a forward reference,
5113 make sure we lay it out again. */
5114 TYPE_SIZE (t) = NULL_TREE;
5115 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5116 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5117 vfuns = 0;
5118 CLASSTYPE_RTTI (t) = NULL_TREE;
5120 /* Do end-of-class semantic processing: checking the validity of the
5121 bases and members and add implicitly generated methods. */
5122 check_bases_and_members (t, &empty);
5124 /* Layout the class itself. */
5125 layout_class_type (t, &empty, &vfuns,
5126 &new_virtuals, &overridden_virtuals);
5128 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5129 vfield = TYPE_VFIELD (t);
5130 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5132 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5134 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5135 BINFO_TYPE (primary)),
5136 20010726);
5137 /* The vtable better be at the start. */
5138 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5139 20010726);
5140 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5141 20010726);
5143 vfield = copy_decl (vfield);
5144 DECL_FIELD_CONTEXT (vfield) = t;
5145 TYPE_VFIELD (t) = vfield;
5147 else
5148 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5150 overridden_virtuals
5151 = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
5153 /* If we created a new vtbl pointer for this class, add it to the
5154 list. */
5155 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5156 CLASSTYPE_VFIELDS (t)
5157 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5159 /* If necessary, create the primary vtable for this class. */
5160 if (new_virtuals || overridden_virtuals || TYPE_CONTAINS_VPTR_P (t))
5162 new_virtuals = nreverse (new_virtuals);
5163 /* We must enter these virtuals into the table. */
5164 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5165 build_primary_vtable (NULL_TREE, t);
5166 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5167 /* Here we know enough to change the type of our virtual
5168 function table, but we will wait until later this function. */
5169 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5171 /* If this type has basetypes with constructors, then those
5172 constructors might clobber the virtual function table. But
5173 they don't if the derived class shares the exact vtable of the base
5174 class. */
5176 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5178 /* If we didn't need a new vtable, see if we should copy one from
5179 the base. */
5180 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5182 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5184 /* If this class uses a different vtable than its primary base
5185 then when we will need to initialize our vptr after the base
5186 class constructor runs. */
5187 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5188 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5191 if (TYPE_CONTAINS_VPTR_P (t))
5193 if (TYPE_BINFO_VTABLE (t))
5194 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5195 20000116);
5196 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5197 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5198 20000116);
5200 CLASSTYPE_VSIZE (t) = vfuns;
5201 /* Entries for virtual functions defined in the primary base are
5202 followed by entries for new functions unique to this class. */
5203 TYPE_BINFO_VIRTUALS (t)
5204 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5205 /* Finally, add entries for functions that override virtuals
5206 from non-primary bases. */
5207 TYPE_BINFO_VIRTUALS (t)
5208 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5211 finish_struct_bits (t);
5213 /* Complete the rtl for any static member objects of the type we're
5214 working on. */
5215 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5216 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5217 && TREE_TYPE (x) == t)
5218 DECL_MODE (x) = TYPE_MODE (t);
5220 /* Done with FIELDS...now decide whether to sort these for
5221 faster lookups later.
5223 The C front-end only does this when n_fields > 15. We use
5224 a smaller number because most searches fail (succeeding
5225 ultimately as the search bores through the inheritance
5226 hierarchy), and we want this failure to occur quickly. */
5228 n_fields = count_fields (TYPE_FIELDS (t));
5229 if (n_fields > 7)
5231 tree field_vec = make_tree_vec (n_fields);
5232 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5233 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5234 (int (*)(const void *, const void *))field_decl_cmp);
5235 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5236 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5237 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5240 if (TYPE_HAS_CONSTRUCTOR (t))
5242 tree vfields = CLASSTYPE_VFIELDS (t);
5244 while (vfields)
5246 /* Mark the fact that constructor for T
5247 could affect anybody inheriting from T
5248 who wants to initialize vtables for VFIELDS's type. */
5249 if (VF_DERIVED_VALUE (vfields))
5250 TREE_ADDRESSABLE (vfields) = 1;
5251 vfields = TREE_CHAIN (vfields);
5255 /* Make the rtl for any new vtables we have created, and unmark
5256 the base types we marked. */
5257 finish_vtbls (t);
5259 /* Build the VTT for T. */
5260 build_vtt (t);
5262 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5263 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5264 cp_warning ("`%#T' has virtual functions but non-virtual destructor", t);
5266 hack_incomplete_structures (t);
5268 if (warn_overloaded_virtual)
5269 warn_hidden (t);
5271 maybe_suppress_debug_info (t);
5273 dump_class_hierarchy (t);
5275 /* Finish debugging output for this type. */
5276 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5279 /* When T was built up, the member declarations were added in reverse
5280 order. Rearrange them to declaration order. */
5282 void
5283 unreverse_member_declarations (t)
5284 tree t;
5286 tree next;
5287 tree prev;
5288 tree x;
5290 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5291 reverse order. Put them in declaration order now. */
5292 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5293 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5295 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5296 reverse order, so we can't just use nreverse. */
5297 prev = NULL_TREE;
5298 for (x = TYPE_FIELDS (t);
5299 x && TREE_CODE (x) != TYPE_DECL;
5300 x = next)
5302 next = TREE_CHAIN (x);
5303 TREE_CHAIN (x) = prev;
5304 prev = x;
5306 if (prev)
5308 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5309 if (prev)
5310 TYPE_FIELDS (t) = prev;
5314 tree
5315 finish_struct (t, attributes)
5316 tree t, attributes;
5318 const char *saved_filename = input_filename;
5319 int saved_lineno = lineno;
5321 /* Now that we've got all the field declarations, reverse everything
5322 as necessary. */
5323 unreverse_member_declarations (t);
5325 cplus_decl_attributes (&t, attributes, NULL_TREE, 0);
5327 /* Nadger the current location so that diagnostics point to the start of
5328 the struct, not the end. */
5329 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5330 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5332 if (processing_template_decl)
5334 finish_struct_methods (t);
5335 TYPE_SIZE (t) = bitsize_zero_node;
5337 else
5338 finish_struct_1 (t);
5340 input_filename = saved_filename;
5341 lineno = saved_lineno;
5343 TYPE_BEING_DEFINED (t) = 0;
5345 if (current_class_type)
5346 popclass ();
5347 else
5348 error ("trying to finish struct, but kicked out due to previous parse errors.");
5350 if (processing_template_decl)
5352 tree scope = current_scope ();
5353 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5354 add_stmt (build_min (TAG_DEFN, t));
5357 return t;
5360 /* Return the dynamic type of INSTANCE, if known.
5361 Used to determine whether the virtual function table is needed
5362 or not.
5364 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5365 of our knowledge of its type. *NONNULL should be initialized
5366 before this function is called. */
5368 static tree
5369 fixed_type_or_null (instance, nonnull, cdtorp)
5370 tree instance;
5371 int *nonnull;
5372 int *cdtorp;
5374 switch (TREE_CODE (instance))
5376 case INDIRECT_REF:
5377 /* Check that we are not going through a cast of some sort. */
5378 if (TREE_TYPE (instance)
5379 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5380 instance = TREE_OPERAND (instance, 0);
5381 /* fall through... */
5382 case CALL_EXPR:
5383 /* This is a call to a constructor, hence it's never zero. */
5384 if (TREE_HAS_CONSTRUCTOR (instance))
5386 if (nonnull)
5387 *nonnull = 1;
5388 return TREE_TYPE (instance);
5390 return NULL_TREE;
5392 case SAVE_EXPR:
5393 /* This is a call to a constructor, hence it's never zero. */
5394 if (TREE_HAS_CONSTRUCTOR (instance))
5396 if (nonnull)
5397 *nonnull = 1;
5398 return TREE_TYPE (instance);
5400 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5402 case RTL_EXPR:
5403 return NULL_TREE;
5405 case PLUS_EXPR:
5406 case MINUS_EXPR:
5407 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5408 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5409 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5410 /* Propagate nonnull. */
5411 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5412 return NULL_TREE;
5414 case NOP_EXPR:
5415 case CONVERT_EXPR:
5416 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5418 case ADDR_EXPR:
5419 if (nonnull)
5420 *nonnull = 1;
5421 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5423 case COMPONENT_REF:
5424 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5426 case VAR_DECL:
5427 case FIELD_DECL:
5428 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5429 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5431 if (nonnull)
5432 *nonnull = 1;
5433 return TREE_TYPE (TREE_TYPE (instance));
5435 /* fall through... */
5436 case TARGET_EXPR:
5437 case PARM_DECL:
5438 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5440 if (nonnull)
5441 *nonnull = 1;
5442 return TREE_TYPE (instance);
5444 else if (instance == current_class_ptr)
5446 if (nonnull)
5447 *nonnull = 1;
5449 /* if we're in a ctor or dtor, we know our type. */
5450 if (DECL_LANG_SPECIFIC (current_function_decl)
5451 && (DECL_CONSTRUCTOR_P (current_function_decl)
5452 || DECL_DESTRUCTOR_P (current_function_decl)))
5454 if (cdtorp)
5455 *cdtorp = 1;
5456 return TREE_TYPE (TREE_TYPE (instance));
5459 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5461 /* Reference variables should be references to objects. */
5462 if (nonnull)
5463 *nonnull = 1;
5465 return NULL_TREE;
5467 default:
5468 return NULL_TREE;
5472 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5473 to the static type. We also handle the case where INSTANCE is really
5474 a pointer. Return negative if this is a ctor/dtor. There the dynamic type
5475 is known, but this might not be the most derived base of the original object,
5476 and hence virtual bases may not be layed out according to this type.
5478 Used to determine whether the virtual function table is needed
5479 or not.
5481 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5482 of our knowledge of its type. *NONNULL should be initialized
5483 before this function is called. */
5486 resolves_to_fixed_type_p (instance, nonnull)
5487 tree instance;
5488 int *nonnull;
5490 tree t = TREE_TYPE (instance);
5491 int cdtorp = 0;
5493 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5494 if (fixed == NULL_TREE)
5495 return 0;
5496 if (POINTER_TYPE_P (t))
5497 t = TREE_TYPE (t);
5498 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5499 return 0;
5500 return cdtorp ? -1 : 1;
5504 void
5505 init_class_processing ()
5507 current_class_depth = 0;
5508 current_class_stack_size = 10;
5509 current_class_stack
5510 = (class_stack_node_t) xmalloc (current_class_stack_size
5511 * sizeof (struct class_stack_node));
5512 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5513 ggc_add_tree_varray_root (&local_classes, 1);
5515 access_default_node = build_int_2 (0, 0);
5516 access_public_node = build_int_2 (ak_public, 0);
5517 access_protected_node = build_int_2 (ak_protected, 0);
5518 access_private_node = build_int_2 (ak_private, 0);
5519 access_default_virtual_node = build_int_2 (4, 0);
5520 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5521 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5522 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5524 ridpointers[(int) RID_PUBLIC] = access_public_node;
5525 ridpointers[(int) RID_PRIVATE] = access_private_node;
5526 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5529 /* Set current scope to NAME. CODE tells us if this is a
5530 STRUCT, UNION, or ENUM environment.
5532 NAME may end up being NULL_TREE if this is an anonymous or
5533 late-bound struct (as in "struct { ... } foo;") */
5535 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5536 appropriate values, found by looking up the type definition of
5537 NAME (as a CODE).
5539 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5540 which can be seen locally to the class. They are shadowed by
5541 any subsequent local declaration (including parameter names).
5543 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5544 which have static meaning (i.e., static members, static
5545 member functions, enum declarations, etc).
5547 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5548 which can be seen locally to the class (as in 1), but
5549 know that we are doing this for declaration purposes
5550 (i.e. friend foo::bar (int)).
5552 So that we may avoid calls to lookup_name, we cache the _TYPE
5553 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5555 For multiple inheritance, we perform a two-pass depth-first search
5556 of the type lattice. The first pass performs a pre-order search,
5557 marking types after the type has had its fields installed in
5558 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5559 unmarks the marked types. If a field or member function name
5560 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5561 that name becomes `error_mark_node'. */
5563 void
5564 pushclass (type, modify)
5565 tree type;
5566 int modify;
5568 type = TYPE_MAIN_VARIANT (type);
5570 /* Make sure there is enough room for the new entry on the stack. */
5571 if (current_class_depth + 1 >= current_class_stack_size)
5573 current_class_stack_size *= 2;
5574 current_class_stack
5575 = (class_stack_node_t) xrealloc (current_class_stack,
5576 current_class_stack_size
5577 * sizeof (struct class_stack_node));
5580 /* Insert a new entry on the class stack. */
5581 current_class_stack[current_class_depth].name = current_class_name;
5582 current_class_stack[current_class_depth].type = current_class_type;
5583 current_class_stack[current_class_depth].access = current_access_specifier;
5584 current_class_stack[current_class_depth].names_used = 0;
5585 current_class_depth++;
5587 /* Now set up the new type. */
5588 current_class_name = TYPE_NAME (type);
5589 if (TREE_CODE (current_class_name) == TYPE_DECL)
5590 current_class_name = DECL_NAME (current_class_name);
5591 current_class_type = type;
5593 /* By default, things in classes are private, while things in
5594 structures or unions are public. */
5595 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5596 ? access_private_node
5597 : access_public_node);
5599 if (previous_class_type != NULL_TREE
5600 && (type != previous_class_type
5601 || !COMPLETE_TYPE_P (previous_class_type))
5602 && current_class_depth == 1)
5604 /* Forcibly remove any old class remnants. */
5605 invalidate_class_lookup_cache ();
5608 /* If we're about to enter a nested class, clear
5609 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5610 if (modify && current_class_depth > 1)
5611 clear_identifier_class_values ();
5613 pushlevel_class ();
5615 if (modify)
5617 if (type != previous_class_type || current_class_depth > 1)
5618 push_class_decls (type);
5619 else
5621 tree item;
5623 /* We are re-entering the same class we just left, so we
5624 don't have to search the whole inheritance matrix to find
5625 all the decls to bind again. Instead, we install the
5626 cached class_shadowed list, and walk through it binding
5627 names and setting up IDENTIFIER_TYPE_VALUEs. */
5628 set_class_shadows (previous_class_values);
5629 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5631 tree id = TREE_PURPOSE (item);
5632 tree decl = TREE_TYPE (item);
5634 push_class_binding (id, decl);
5635 if (TREE_CODE (decl) == TYPE_DECL)
5636 set_identifier_type_value (id, TREE_TYPE (decl));
5638 unuse_fields (type);
5641 storetags (CLASSTYPE_TAGS (type));
5645 /* When we exit a toplevel class scope, we save the
5646 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5647 reenter the class. Here, we've entered some other class, so we
5648 must invalidate our cache. */
5650 void
5651 invalidate_class_lookup_cache ()
5653 tree t;
5655 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5656 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5657 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5659 previous_class_values = NULL_TREE;
5660 previous_class_type = NULL_TREE;
5663 /* Get out of the current class scope. If we were in a class scope
5664 previously, that is the one popped to. */
5666 void
5667 popclass ()
5669 poplevel_class ();
5670 /* Since poplevel_class does the popping of class decls nowadays,
5671 this really only frees the obstack used for these decls. */
5672 pop_class_decls ();
5674 current_class_depth--;
5675 current_class_name = current_class_stack[current_class_depth].name;
5676 current_class_type = current_class_stack[current_class_depth].type;
5677 current_access_specifier = current_class_stack[current_class_depth].access;
5678 if (current_class_stack[current_class_depth].names_used)
5679 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5682 /* Returns 1 if current_class_type is either T or a nested type of T.
5683 We start looking from 1 because entry 0 is from global scope, and has
5684 no type. */
5687 currently_open_class (t)
5688 tree t;
5690 int i;
5691 if (t == current_class_type)
5692 return 1;
5693 for (i = 1; i < current_class_depth; ++i)
5694 if (current_class_stack [i].type == t)
5695 return 1;
5696 return 0;
5699 /* If either current_class_type or one of its enclosing classes are derived
5700 from T, return the appropriate type. Used to determine how we found
5701 something via unqualified lookup. */
5703 tree
5704 currently_open_derived_class (t)
5705 tree t;
5707 int i;
5709 if (DERIVED_FROM_P (t, current_class_type))
5710 return current_class_type;
5712 for (i = current_class_depth - 1; i > 0; --i)
5713 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5714 return current_class_stack[i].type;
5716 return NULL_TREE;
5719 /* When entering a class scope, all enclosing class scopes' names with
5720 static meaning (static variables, static functions, types and enumerators)
5721 have to be visible. This recursive function calls pushclass for all
5722 enclosing class contexts until global or a local scope is reached.
5723 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5724 formal of the same name. */
5726 void
5727 push_nested_class (type, modify)
5728 tree type;
5729 int modify;
5731 tree context;
5733 /* FIXME: This check should go in push_scope, analagous to the
5734 handling of pop_scope vs. pop_nested_class. */
5735 if (!CLASS_TYPE_P (type))
5736 return;
5738 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5740 if (context && CLASS_TYPE_P (context))
5741 push_nested_class (context, 2);
5742 pushclass (type, modify);
5745 /* Undoes a push_nested_class call. */
5747 void
5748 pop_nested_class ()
5750 tree context;
5752 context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5754 popclass ();
5755 if (context && CLASS_TYPE_P (context))
5756 pop_nested_class ();
5759 /* Returns the number of extern "LANG" blocks we are nested within. */
5762 current_lang_depth ()
5764 return VARRAY_ACTIVE_SIZE (current_lang_base);
5767 /* Set global variables CURRENT_LANG_NAME to appropriate value
5768 so that behavior of name-mangling machinery is correct. */
5770 void
5771 push_lang_context (name)
5772 tree name;
5774 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5776 if (name == lang_name_cplusplus)
5778 current_lang_name = name;
5780 else if (name == lang_name_java)
5782 current_lang_name = name;
5783 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5784 (See record_builtin_java_type in decl.c.) However, that causes
5785 incorrect debug entries if these types are actually used.
5786 So we re-enable debug output after extern "Java". */
5787 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5788 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5789 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5790 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5791 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5792 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5793 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5794 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5796 else if (name == lang_name_c)
5798 current_lang_name = name;
5800 else
5801 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5804 /* Get out of the current language scope. */
5806 void
5807 pop_lang_context ()
5809 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5810 VARRAY_POP (current_lang_base);
5813 /* Type instantiation routines. */
5815 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5816 matches the TARGET_TYPE. If there is no satisfactory match, return
5817 error_mark_node, and issue an error message if COMPLAIN is
5818 non-zero. Permit pointers to member function if PTRMEM is non-zero.
5819 If TEMPLATE_ONLY, the name of the overloaded function
5820 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5821 template arguments. */
5823 static tree
5824 resolve_address_of_overloaded_function (target_type,
5825 overload,
5826 complain,
5827 ptrmem,
5828 template_only,
5829 explicit_targs)
5830 tree target_type;
5831 tree overload;
5832 int complain;
5833 int ptrmem;
5834 int template_only;
5835 tree explicit_targs;
5837 /* Here's what the standard says:
5839 [over.over]
5841 If the name is a function template, template argument deduction
5842 is done, and if the argument deduction succeeds, the deduced
5843 arguments are used to generate a single template function, which
5844 is added to the set of overloaded functions considered.
5846 Non-member functions and static member functions match targets of
5847 type "pointer-to-function" or "reference-to-function." Nonstatic
5848 member functions match targets of type "pointer-to-member
5849 function;" the function type of the pointer to member is used to
5850 select the member function from the set of overloaded member
5851 functions. If a nonstatic member function is selected, the
5852 reference to the overloaded function name is required to have the
5853 form of a pointer to member as described in 5.3.1.
5855 If more than one function is selected, any template functions in
5856 the set are eliminated if the set also contains a non-template
5857 function, and any given template function is eliminated if the
5858 set contains a second template function that is more specialized
5859 than the first according to the partial ordering rules 14.5.5.2.
5860 After such eliminations, if any, there shall remain exactly one
5861 selected function. */
5863 int is_ptrmem = 0;
5864 int is_reference = 0;
5865 /* We store the matches in a TREE_LIST rooted here. The functions
5866 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5867 interoperability with most_specialized_instantiation. */
5868 tree matches = NULL_TREE;
5869 tree fn;
5871 /* By the time we get here, we should be seeing only real
5872 pointer-to-member types, not the internal POINTER_TYPE to
5873 METHOD_TYPE representation. */
5874 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5875 && (TREE_CODE (TREE_TYPE (target_type))
5876 == METHOD_TYPE)), 0);
5878 if (TREE_CODE (overload) == COMPONENT_REF)
5879 overload = TREE_OPERAND (overload, 1);
5881 /* Check that the TARGET_TYPE is reasonable. */
5882 if (TYPE_PTRFN_P (target_type))
5883 /* This is OK. */;
5884 else if (TYPE_PTRMEMFUNC_P (target_type))
5885 /* This is OK, too. */
5886 is_ptrmem = 1;
5887 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5889 /* This is OK, too. This comes from a conversion to reference
5890 type. */
5891 target_type = build_reference_type (target_type);
5892 is_reference = 1;
5894 else
5896 if (complain)
5897 cp_error ("\
5898 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5899 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5900 return error_mark_node;
5903 /* If we can find a non-template function that matches, we can just
5904 use it. There's no point in generating template instantiations
5905 if we're just going to throw them out anyhow. But, of course, we
5906 can only do this when we don't *need* a template function. */
5907 if (!template_only)
5909 tree fns;
5911 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5913 tree fn = OVL_FUNCTION (fns);
5914 tree fntype;
5916 if (TREE_CODE (fn) == TEMPLATE_DECL)
5917 /* We're not looking for templates just yet. */
5918 continue;
5920 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5921 != is_ptrmem)
5922 /* We're looking for a non-static member, and this isn't
5923 one, or vice versa. */
5924 continue;
5926 /* See if there's a match. */
5927 fntype = TREE_TYPE (fn);
5928 if (is_ptrmem)
5929 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5930 else if (!is_reference)
5931 fntype = build_pointer_type (fntype);
5933 if (can_convert_arg (target_type, fntype, fn))
5934 matches = tree_cons (fn, NULL_TREE, matches);
5938 /* Now, if we've already got a match (or matches), there's no need
5939 to proceed to the template functions. But, if we don't have a
5940 match we need to look at them, too. */
5941 if (!matches)
5943 tree target_fn_type;
5944 tree target_arg_types;
5945 tree target_ret_type;
5946 tree fns;
5948 if (is_ptrmem)
5949 target_fn_type
5950 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5951 else
5952 target_fn_type = TREE_TYPE (target_type);
5953 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5954 target_ret_type = TREE_TYPE (target_fn_type);
5956 /* Never do unification on the 'this' parameter. */
5957 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5958 target_arg_types = TREE_CHAIN (target_arg_types);
5960 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5962 tree fn = OVL_FUNCTION (fns);
5963 tree instantiation;
5964 tree instantiation_type;
5965 tree targs;
5967 if (TREE_CODE (fn) != TEMPLATE_DECL)
5968 /* We're only looking for templates. */
5969 continue;
5971 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5972 != is_ptrmem)
5973 /* We're not looking for a non-static member, and this is
5974 one, or vice versa. */
5975 continue;
5977 /* Try to do argument deduction. */
5978 targs = make_tree_vec (DECL_NTPARMS (fn));
5979 if (fn_type_unification (fn, explicit_targs, targs,
5980 target_arg_types, target_ret_type,
5981 DEDUCE_EXACT, -1) != 0)
5982 /* Argument deduction failed. */
5983 continue;
5985 /* Instantiate the template. */
5986 instantiation = instantiate_template (fn, targs);
5987 if (instantiation == error_mark_node)
5988 /* Instantiation failed. */
5989 continue;
5991 /* See if there's a match. */
5992 instantiation_type = TREE_TYPE (instantiation);
5993 if (is_ptrmem)
5994 instantiation_type =
5995 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5996 else if (!is_reference)
5997 instantiation_type = build_pointer_type (instantiation_type);
5998 if (can_convert_arg (target_type, instantiation_type, instantiation))
5999 matches = tree_cons (instantiation, fn, matches);
6002 /* Now, remove all but the most specialized of the matches. */
6003 if (matches)
6005 tree match = most_specialized_instantiation (matches);
6007 if (match != error_mark_node)
6008 matches = tree_cons (match, NULL_TREE, NULL_TREE);
6012 /* Now we should have exactly one function in MATCHES. */
6013 if (matches == NULL_TREE)
6015 /* There were *no* matches. */
6016 if (complain)
6018 cp_error ("no matches converting function `%D' to type `%#T'",
6019 DECL_NAME (OVL_FUNCTION (overload)),
6020 target_type);
6022 /* print_candidates expects a chain with the functions in
6023 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6024 so why be clever?). */
6025 for (; overload; overload = OVL_NEXT (overload))
6026 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6027 matches);
6029 print_candidates (matches);
6031 return error_mark_node;
6033 else if (TREE_CHAIN (matches))
6035 /* There were too many matches. */
6037 if (complain)
6039 tree match;
6041 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6042 DECL_NAME (OVL_FUNCTION (overload)),
6043 target_type);
6045 /* Since print_candidates expects the functions in the
6046 TREE_VALUE slot, we flip them here. */
6047 for (match = matches; match; match = TREE_CHAIN (match))
6048 TREE_VALUE (match) = TREE_PURPOSE (match);
6050 print_candidates (matches);
6053 return error_mark_node;
6056 /* Good, exactly one match. Now, convert it to the correct type. */
6057 fn = TREE_PURPOSE (matches);
6059 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6060 && !ptrmem && !flag_ms_extensions)
6062 static int explained;
6064 if (!complain)
6065 return error_mark_node;
6067 cp_pedwarn ("assuming pointer to member `%D'", fn);
6068 if (!explained)
6070 cp_pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6071 explained = 1;
6074 mark_used (fn);
6076 if (TYPE_PTRFN_P (target_type))
6077 return build_unary_op (ADDR_EXPR, fn, 0);
6078 else if (TYPE_PTRMEMFUNC_P (target_type))
6079 return build_ptrfuncmem_cst (fn);
6080 else
6082 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6083 will mark the function as addressed, but here we must do it
6084 explicitly. */
6085 mark_addressable (fn);
6087 return fn;
6091 /* This function will instantiate the type of the expression given in
6092 RHS to match the type of LHSTYPE. If errors exist, then return
6093 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6094 we complain on errors. If we are not complaining, never modify rhs,
6095 as overload resolution wants to try many possible instantiations, in
6096 the hope that at least one will work.
6098 For non-recursive calls, LHSTYPE should be a function, pointer to
6099 function, or a pointer to member function. */
6101 tree
6102 instantiate_type (lhstype, rhs, flags)
6103 tree lhstype, rhs;
6104 enum instantiate_type_flags flags;
6106 int complain = (flags & itf_complain);
6107 int strict = (flags & itf_no_attributes)
6108 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6109 int allow_ptrmem = flags & itf_ptrmem_ok;
6111 flags &= ~itf_ptrmem_ok;
6113 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6115 if (complain)
6116 error ("not enough type information");
6117 return error_mark_node;
6120 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6122 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6123 return rhs;
6124 if (complain)
6125 cp_error ("argument of type `%T' does not match `%T'",
6126 TREE_TYPE (rhs), lhstype);
6127 return error_mark_node;
6130 /* We don't overwrite rhs if it is an overloaded function.
6131 Copying it would destroy the tree link. */
6132 if (TREE_CODE (rhs) != OVERLOAD)
6133 rhs = copy_node (rhs);
6135 /* This should really only be used when attempting to distinguish
6136 what sort of a pointer to function we have. For now, any
6137 arithmetic operation which is not supported on pointers
6138 is rejected as an error. */
6140 switch (TREE_CODE (rhs))
6142 case TYPE_EXPR:
6143 case CONVERT_EXPR:
6144 case SAVE_EXPR:
6145 case CONSTRUCTOR:
6146 case BUFFER_REF:
6147 my_friendly_abort (177);
6148 return error_mark_node;
6150 case INDIRECT_REF:
6151 case ARRAY_REF:
6153 tree new_rhs;
6155 new_rhs = instantiate_type (build_pointer_type (lhstype),
6156 TREE_OPERAND (rhs, 0), flags);
6157 if (new_rhs == error_mark_node)
6158 return error_mark_node;
6160 TREE_TYPE (rhs) = lhstype;
6161 TREE_OPERAND (rhs, 0) = new_rhs;
6162 return rhs;
6165 case NOP_EXPR:
6166 rhs = copy_node (TREE_OPERAND (rhs, 0));
6167 TREE_TYPE (rhs) = unknown_type_node;
6168 return instantiate_type (lhstype, rhs, flags);
6170 case COMPONENT_REF:
6171 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6173 case OFFSET_REF:
6174 rhs = TREE_OPERAND (rhs, 1);
6175 if (BASELINK_P (rhs))
6176 return instantiate_type (lhstype,
6177 BASELINK_FUNCTIONS (rhs),
6178 flags | allow_ptrmem);
6180 /* This can happen if we are forming a pointer-to-member for a
6181 member template. */
6182 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6184 /* Fall through. */
6186 case TEMPLATE_ID_EXPR:
6188 tree fns = TREE_OPERAND (rhs, 0);
6189 tree args = TREE_OPERAND (rhs, 1);
6191 if (BASELINK_P (fns))
6192 fns = BASELINK_FUNCTIONS (fns);
6194 return
6195 resolve_address_of_overloaded_function (lhstype,
6196 fns,
6197 complain,
6198 allow_ptrmem,
6199 /*template_only=*/1,
6200 args);
6203 case OVERLOAD:
6204 return
6205 resolve_address_of_overloaded_function (lhstype,
6206 rhs,
6207 complain,
6208 allow_ptrmem,
6209 /*template_only=*/0,
6210 /*explicit_targs=*/NULL_TREE);
6212 case BASELINK:
6213 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
6215 case CALL_EXPR:
6216 /* This is too hard for now. */
6217 my_friendly_abort (183);
6218 return error_mark_node;
6220 case PLUS_EXPR:
6221 case MINUS_EXPR:
6222 case COMPOUND_EXPR:
6223 TREE_OPERAND (rhs, 0)
6224 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6225 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6226 return error_mark_node;
6227 TREE_OPERAND (rhs, 1)
6228 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6229 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6230 return error_mark_node;
6232 TREE_TYPE (rhs) = lhstype;
6233 return rhs;
6235 case MULT_EXPR:
6236 case TRUNC_DIV_EXPR:
6237 case FLOOR_DIV_EXPR:
6238 case CEIL_DIV_EXPR:
6239 case ROUND_DIV_EXPR:
6240 case RDIV_EXPR:
6241 case TRUNC_MOD_EXPR:
6242 case FLOOR_MOD_EXPR:
6243 case CEIL_MOD_EXPR:
6244 case ROUND_MOD_EXPR:
6245 case FIX_ROUND_EXPR:
6246 case FIX_FLOOR_EXPR:
6247 case FIX_CEIL_EXPR:
6248 case FIX_TRUNC_EXPR:
6249 case FLOAT_EXPR:
6250 case NEGATE_EXPR:
6251 case ABS_EXPR:
6252 case MAX_EXPR:
6253 case MIN_EXPR:
6254 case FFS_EXPR:
6256 case BIT_AND_EXPR:
6257 case BIT_IOR_EXPR:
6258 case BIT_XOR_EXPR:
6259 case LSHIFT_EXPR:
6260 case RSHIFT_EXPR:
6261 case LROTATE_EXPR:
6262 case RROTATE_EXPR:
6264 case PREINCREMENT_EXPR:
6265 case PREDECREMENT_EXPR:
6266 case POSTINCREMENT_EXPR:
6267 case POSTDECREMENT_EXPR:
6268 if (complain)
6269 error ("invalid operation on uninstantiated type");
6270 return error_mark_node;
6272 case TRUTH_AND_EXPR:
6273 case TRUTH_OR_EXPR:
6274 case TRUTH_XOR_EXPR:
6275 case LT_EXPR:
6276 case LE_EXPR:
6277 case GT_EXPR:
6278 case GE_EXPR:
6279 case EQ_EXPR:
6280 case NE_EXPR:
6281 case TRUTH_ANDIF_EXPR:
6282 case TRUTH_ORIF_EXPR:
6283 case TRUTH_NOT_EXPR:
6284 if (complain)
6285 error ("not enough type information");
6286 return error_mark_node;
6288 case COND_EXPR:
6289 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6291 if (complain)
6292 error ("not enough type information");
6293 return error_mark_node;
6295 TREE_OPERAND (rhs, 1)
6296 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6297 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6298 return error_mark_node;
6299 TREE_OPERAND (rhs, 2)
6300 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6301 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6302 return error_mark_node;
6304 TREE_TYPE (rhs) = lhstype;
6305 return rhs;
6307 case MODIFY_EXPR:
6308 TREE_OPERAND (rhs, 1)
6309 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6310 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6311 return error_mark_node;
6313 TREE_TYPE (rhs) = lhstype;
6314 return rhs;
6316 case ADDR_EXPR:
6318 if (PTRMEM_OK_P (rhs))
6319 flags |= itf_ptrmem_ok;
6321 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6323 case ENTRY_VALUE_EXPR:
6324 my_friendly_abort (184);
6325 return error_mark_node;
6327 case ERROR_MARK:
6328 return error_mark_node;
6330 default:
6331 my_friendly_abort (185);
6332 return error_mark_node;
6336 /* Return the name of the virtual function pointer field
6337 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6338 this may have to look back through base types to find the
6339 ultimate field name. (For single inheritance, these could
6340 all be the same name. Who knows for multiple inheritance). */
6342 static tree
6343 get_vfield_name (type)
6344 tree type;
6346 tree binfo = TYPE_BINFO (type);
6347 char *buf;
6349 while (BINFO_BASETYPES (binfo)
6350 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6351 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6352 binfo = BINFO_BASETYPE (binfo, 0);
6354 type = BINFO_TYPE (binfo);
6355 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6356 + TYPE_NAME_LENGTH (type) + 2);
6357 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6358 return get_identifier (buf);
6361 void
6362 print_class_statistics ()
6364 #ifdef GATHER_STATISTICS
6365 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6366 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6367 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6368 n_build_method_call, n_inner_fields_searched);
6369 if (n_vtables)
6371 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6372 n_vtables, n_vtable_searches);
6373 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6374 n_vtable_entries, n_vtable_elems);
6376 #endif
6379 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6380 according to [class]:
6382 The class-name is also inserted into the scope of the class
6383 itself. For purposes of access checking, the inserted class name
6384 is treated as if it were a public member name. */
6386 void
6387 build_self_reference ()
6389 tree name = constructor_name (current_class_type);
6390 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6391 tree saved_cas;
6393 DECL_NONLOCAL (value) = 1;
6394 DECL_CONTEXT (value) = current_class_type;
6395 DECL_ARTIFICIAL (value) = 1;
6397 if (processing_template_decl)
6398 value = push_template_decl (value);
6400 saved_cas = current_access_specifier;
6401 current_access_specifier = access_public_node;
6402 finish_member_declaration (value);
6403 current_access_specifier = saved_cas;
6406 /* Returns 1 if TYPE contains only padding bytes. */
6409 is_empty_class (type)
6410 tree type;
6412 if (type == error_mark_node)
6413 return 0;
6415 if (! IS_AGGR_TYPE (type))
6416 return 0;
6418 return integer_zerop (CLASSTYPE_SIZE (type));
6421 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6422 a *_TYPE node. NODE can also be a local class. */
6424 tree
6425 get_enclosing_class (type)
6426 tree type;
6428 tree node = type;
6430 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6432 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6434 case 'd':
6435 node = DECL_CONTEXT (node);
6436 break;
6438 case 't':
6439 if (node != type)
6440 return node;
6441 node = TYPE_CONTEXT (node);
6442 break;
6444 default:
6445 my_friendly_abort (0);
6448 return NULL_TREE;
6451 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6454 is_base_of_enclosing_class (base, type)
6455 tree base, type;
6457 while (type)
6459 if (get_binfo (base, type, 0))
6460 return 1;
6462 type = get_enclosing_class (type);
6464 return 0;
6467 /* Note that NAME was looked up while the current class was being
6468 defined and that the result of that lookup was DECL. */
6470 void
6471 maybe_note_name_used_in_class (name, decl)
6472 tree name;
6473 tree decl;
6475 splay_tree names_used;
6477 /* If we're not defining a class, there's nothing to do. */
6478 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6479 return;
6481 /* If there's already a binding for this NAME, then we don't have
6482 anything to worry about. */
6483 if (IDENTIFIER_CLASS_VALUE (name))
6484 return;
6486 if (!current_class_stack[current_class_depth - 1].names_used)
6487 current_class_stack[current_class_depth - 1].names_used
6488 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6489 names_used = current_class_stack[current_class_depth - 1].names_used;
6491 splay_tree_insert (names_used,
6492 (splay_tree_key) name,
6493 (splay_tree_value) decl);
6496 /* Note that NAME was declared (as DECL) in the current class. Check
6497 to see that the declaration is legal. */
6499 void
6500 note_name_declared_in_class (name, decl)
6501 tree name;
6502 tree decl;
6504 splay_tree names_used;
6505 splay_tree_node n;
6507 /* Look to see if we ever used this name. */
6508 names_used
6509 = current_class_stack[current_class_depth - 1].names_used;
6510 if (!names_used)
6511 return;
6513 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6514 if (n)
6516 /* [basic.scope.class]
6518 A name N used in a class S shall refer to the same declaration
6519 in its context and when re-evaluated in the completed scope of
6520 S. */
6521 cp_error ("declaration of `%#D'", decl);
6522 cp_error_at ("changes meaning of `%D' from `%+#D'",
6523 DECL_NAME (OVL_CURRENT (decl)),
6524 (tree) n->value);
6528 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6529 Secondary vtables are merged with primary vtables; this function
6530 will return the VAR_DECL for the primary vtable. */
6532 tree
6533 get_vtbl_decl_for_binfo (binfo)
6534 tree binfo;
6536 tree decl;
6538 decl = BINFO_VTABLE (binfo);
6539 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6541 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6542 2000403);
6543 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6545 if (decl)
6546 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6547 return decl;
6550 /* Called from get_primary_binfo via dfs_walk. DATA is a TREE_LIST
6551 who's TREE_PURPOSE is the TYPE of the required primary base and
6552 who's TREE_VALUE is a list of candidate binfos that we fill in. */
6554 static tree
6555 dfs_get_primary_binfo (binfo, data)
6556 tree binfo;
6557 void *data;
6559 tree cons = (tree) data;
6560 tree primary_base = TREE_PURPOSE (cons);
6562 if (TREE_VIA_VIRTUAL (binfo)
6563 && same_type_p (BINFO_TYPE (binfo), primary_base))
6564 /* This is the right type of binfo, but it might be an unshared
6565 instance, and the shared instance is later in the dfs walk. We
6566 must keep looking. */
6567 TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6569 return NULL_TREE;
6572 /* Returns the unshared binfo for the primary base of BINFO. Note
6573 that in a complex hierarchy the resulting BINFO may not actually
6574 *be* primary. In particular if the resulting BINFO is a virtual
6575 base, and it occurs elsewhere in the hierarchy, then this
6576 occurrence may not actually be a primary base in the complete
6577 object. Check BINFO_PRIMARY_P to be sure. */
6579 tree
6580 get_primary_binfo (binfo)
6581 tree binfo;
6583 tree primary_base;
6584 tree result = NULL_TREE;
6585 tree virtuals;
6587 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6588 if (!primary_base)
6589 return NULL_TREE;
6591 /* A non-virtual primary base is always a direct base, and easy to
6592 find. */
6593 if (!TREE_VIA_VIRTUAL (primary_base))
6595 int i;
6597 /* Scan the direct basetypes until we find a base with the same
6598 type as the primary base. */
6599 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6601 tree base_binfo = BINFO_BASETYPE (binfo, i);
6603 if (same_type_p (BINFO_TYPE (base_binfo),
6604 BINFO_TYPE (primary_base)))
6605 return base_binfo;
6608 /* We should always find the primary base. */
6609 my_friendly_abort (20000729);
6612 /* For a primary virtual base, we have to scan the entire hierarchy
6613 rooted at BINFO; the virtual base could be an indirect virtual
6614 base. There could be more than one instance of the primary base
6615 in the hierarchy, and if one is the canonical binfo we want that
6616 one. If it exists, it should be the first one we find, but as a
6617 consistency check we find them all and make sure. */
6618 virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6619 dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6620 virtuals = TREE_VALUE (virtuals);
6622 /* We must have found at least one instance. */
6623 my_friendly_assert (virtuals, 20010612);
6625 if (TREE_CHAIN (virtuals))
6627 /* We found more than one instance of the base. We must make
6628 sure that, if one is the canonical one, it is the first one
6629 we found. As the chain is in reverse dfs order, that means
6630 the last on the list. */
6631 tree complete_binfo;
6632 tree canonical;
6634 for (complete_binfo = binfo;
6635 BINFO_INHERITANCE_CHAIN (complete_binfo);
6636 complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6637 continue;
6638 canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6639 BINFO_TYPE (complete_binfo));
6641 for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6643 result = TREE_VALUE (virtuals);
6645 if (canonical == result)
6647 /* This is the unshared instance. Make sure it was the
6648 first one found. */
6649 my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
6650 break;
6654 else
6655 result = TREE_VALUE (virtuals);
6656 return result;
6659 /* If INDENTED_P is zero, indent to INDENT. Return non-zero. */
6661 static int
6662 maybe_indent_hierarchy (stream, indent, indented_p)
6663 FILE *stream;
6664 int indent;
6665 int indented_p;
6667 if (!indented_p)
6668 fprintf (stream, "%*s", indent, "");
6669 return 1;
6672 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6673 dominated by T) to stderr. INDENT should be zero when called from
6674 the top level; it is incremented recursively. */
6676 static void
6677 dump_class_hierarchy_r (stream, flags, t, binfo, indent)
6678 FILE *stream;
6679 int flags;
6680 tree t;
6681 tree binfo;
6682 int indent;
6684 int i;
6685 int indented = 0;
6687 indented = maybe_indent_hierarchy (stream, indent, 0);
6688 fprintf (stream, "%s (0x%lx) ",
6689 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6690 (unsigned long) binfo);
6691 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6692 tree_low_cst (BINFO_OFFSET (binfo), 0));
6693 if (is_empty_class (BINFO_TYPE (binfo)))
6694 fprintf (stream, " empty");
6695 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6696 fprintf (stream, " nearly-empty");
6697 if (TREE_VIA_VIRTUAL (binfo))
6699 tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6701 fprintf (stream, " virtual");
6702 if (canonical == binfo)
6703 fprintf (stream, " canonical");
6704 else
6705 fprintf (stream, " non-canonical");
6707 fprintf (stream, "\n");
6709 indented = 0;
6710 if (BINFO_PRIMARY_BASE_OF (binfo))
6712 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6713 fprintf (stream, " primary-for %s (0x%lx)",
6714 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6715 TFF_PLAIN_IDENTIFIER),
6716 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6718 if (BINFO_LOST_PRIMARY_P (binfo))
6720 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6721 fprintf (stream, " lost-primary");
6723 if (indented)
6724 fprintf (stream, "\n");
6726 if (!(flags & TDF_SLIM))
6728 int indented = 0;
6730 if (BINFO_SUBVTT_INDEX (binfo))
6732 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6733 fprintf (stream, " subvttidx=%s",
6734 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6735 TFF_PLAIN_IDENTIFIER));
6737 if (BINFO_VPTR_INDEX (binfo))
6739 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6740 fprintf (stream, " vptridx=%s",
6741 expr_as_string (BINFO_VPTR_INDEX (binfo),
6742 TFF_PLAIN_IDENTIFIER));
6744 if (BINFO_VPTR_FIELD (binfo))
6746 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6747 fprintf (stream, " vbaseoffset=%s",
6748 expr_as_string (BINFO_VPTR_FIELD (binfo),
6749 TFF_PLAIN_IDENTIFIER));
6751 if (BINFO_VTABLE (binfo))
6753 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6754 fprintf (stream, " vptr=%s",
6755 expr_as_string (BINFO_VTABLE (binfo),
6756 TFF_PLAIN_IDENTIFIER));
6759 if (indented)
6760 fprintf (stream, "\n");
6764 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6765 dump_class_hierarchy_r (stream, flags,
6766 t, BINFO_BASETYPE (binfo, i),
6767 indent + 2);
6770 /* Dump the BINFO hierarchy for T. */
6772 static void
6773 dump_class_hierarchy (t)
6774 tree t;
6776 int flags;
6777 FILE *stream = dump_begin (TDI_class, &flags);
6779 if (!stream)
6780 return;
6782 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6783 fprintf (stream, " size=%lu align=%lu\n",
6784 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6785 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6786 dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
6787 fprintf (stream, "\n");
6788 dump_end (TDI_class, stream);
6791 static void
6792 dump_array (stream, decl)
6793 FILE *stream;
6794 tree decl;
6796 tree inits;
6797 int ix;
6798 HOST_WIDE_INT elt;
6799 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6801 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6802 / BITS_PER_UNIT);
6803 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6804 fprintf (stream, " %s entries",
6805 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6806 TFF_PLAIN_IDENTIFIER));
6807 fprintf (stream, "\n");
6809 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6810 inits; ix++, inits = TREE_CHAIN (inits))
6811 fprintf (stream, "%-4d %s\n", ix * elt,
6812 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6815 static void
6816 dump_vtable (t, binfo, vtable)
6817 tree t;
6818 tree binfo;
6819 tree vtable;
6821 int flags;
6822 FILE *stream = dump_begin (TDI_class, &flags);
6824 if (!stream)
6825 return;
6827 if (!(flags & TDF_SLIM))
6829 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6831 fprintf (stream, "%s for %s",
6832 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6833 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6834 if (ctor_vtbl_p)
6836 if (!TREE_VIA_VIRTUAL (binfo))
6837 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6838 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6840 fprintf (stream, "\n");
6841 dump_array (stream, vtable);
6842 fprintf (stream, "\n");
6845 dump_end (TDI_class, stream);
6848 static void
6849 dump_vtt (t, vtt)
6850 tree t;
6851 tree vtt;
6853 int flags;
6854 FILE *stream = dump_begin (TDI_class, &flags);
6856 if (!stream)
6857 return;
6859 if (!(flags & TDF_SLIM))
6861 fprintf (stream, "VTT for %s\n",
6862 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6863 dump_array (stream, vtt);
6864 fprintf (stream, "\n");
6867 dump_end (TDI_class, stream);
6870 /* Virtual function table initialization. */
6872 /* Create all the necessary vtables for T and its base classes. */
6874 static void
6875 finish_vtbls (t)
6876 tree t;
6878 tree list;
6879 tree vbase;
6881 /* We lay out the primary and secondary vtables in one contiguous
6882 vtable. The primary vtable is first, followed by the non-virtual
6883 secondary vtables in inheritance graph order. */
6884 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6885 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6886 TYPE_BINFO (t), t, list);
6888 /* Then come the virtual bases, also in inheritance graph order. */
6889 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6891 tree real_base;
6893 if (!TREE_VIA_VIRTUAL (vbase))
6894 continue;
6896 /* Although we walk in inheritance order, that might not get the
6897 canonical base. */
6898 real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
6900 accumulate_vtbl_inits (real_base, real_base,
6901 TYPE_BINFO (t), t, list);
6904 if (TYPE_BINFO_VTABLE (t))
6905 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6908 /* Initialize the vtable for BINFO with the INITS. */
6910 static void
6911 initialize_vtable (binfo, inits)
6912 tree binfo;
6913 tree inits;
6915 tree decl;
6917 layout_vtable_decl (binfo, list_length (inits));
6918 decl = get_vtbl_decl_for_binfo (binfo);
6919 initialize_array (decl, inits);
6920 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6923 /* Initialize DECL (a declaration for a namespace-scope array) with
6924 the INITS. */
6926 static void
6927 initialize_array (decl, inits)
6928 tree decl;
6929 tree inits;
6931 tree context;
6933 context = DECL_CONTEXT (decl);
6934 DECL_CONTEXT (decl) = NULL_TREE;
6935 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6936 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6937 DECL_CONTEXT (decl) = context;
6940 /* Build the VTT (virtual table table) for T.
6941 A class requires a VTT if it has virtual bases.
6943 This holds
6944 1 - primary virtual pointer for complete object T
6945 2 - secondary VTTs for each direct non-virtual base of T which requires a
6947 3 - secondary virtual pointers for each direct or indirect base of T which
6948 has virtual bases or is reachable via a virtual path from T.
6949 4 - secondary VTTs for each direct or indirect virtual base of T.
6951 Secondary VTTs look like complete object VTTs without part 4. */
6953 static void
6954 build_vtt (t)
6955 tree t;
6957 tree inits;
6958 tree type;
6959 tree vtt;
6960 tree index;
6962 /* Build up the initializers for the VTT. */
6963 inits = NULL_TREE;
6964 index = size_zero_node;
6965 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6967 /* If we didn't need a VTT, we're done. */
6968 if (!inits)
6969 return;
6971 /* Figure out the type of the VTT. */
6972 type = build_index_type (size_int (list_length (inits) - 1));
6973 type = build_cplus_array_type (const_ptr_type_node, type);
6975 /* Now, build the VTT object itself. */
6976 vtt = build_vtable (t, get_vtt_name (t), type);
6977 pushdecl_top_level (vtt);
6978 initialize_array (vtt, inits);
6980 dump_vtt (t, vtt);
6983 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
6984 from within some heirarchy which is inherited from the type of BINFO.
6985 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
6986 BINFO. */
6988 static tree
6989 get_original_base (base_binfo, binfo)
6990 tree base_binfo;
6991 tree binfo;
6993 tree derived;
6994 int ix;
6996 if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
6997 return binfo;
6998 if (TREE_VIA_VIRTUAL (base_binfo))
6999 return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
7000 derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
7002 for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
7003 if (same_type_p (BINFO_TYPE (base_binfo),
7004 BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
7005 return BINFO_BASETYPE (derived, ix);
7006 my_friendly_abort (20010223);
7007 return NULL;
7010 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7011 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7012 and CHAIN the vtable pointer for this binfo after construction is
7013 complete. VALUE can also be another BINFO, in which case we recurse. */
7015 static tree
7016 binfo_ctor_vtable (binfo)
7017 tree binfo;
7019 tree vt;
7021 while (1)
7023 vt = BINFO_VTABLE (binfo);
7024 if (TREE_CODE (vt) == TREE_LIST)
7025 vt = TREE_VALUE (vt);
7026 if (TREE_CODE (vt) == TREE_VEC)
7027 binfo = vt;
7028 else
7029 break;
7032 return vt;
7035 /* Recursively build the VTT-initializer for BINFO (which is in the
7036 hierarchy dominated by T). INITS points to the end of the initializer
7037 list to date. INDEX is the VTT index where the next element will be
7038 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7039 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7040 for virtual bases of T. When it is not so, we build the constructor
7041 vtables for the BINFO-in-T variant. */
7043 static tree *
7044 build_vtt_inits (binfo, t, inits, index)
7045 tree binfo;
7046 tree t;
7047 tree *inits;
7048 tree *index;
7050 int i;
7051 tree b;
7052 tree init;
7053 tree secondary_vptrs;
7054 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7056 /* We only need VTTs for subobjects with virtual bases. */
7057 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7058 return inits;
7060 /* We need to use a construction vtable if this is not the primary
7061 VTT. */
7062 if (!top_level_p)
7064 build_ctor_vtbl_group (binfo, t);
7066 /* Record the offset in the VTT where this sub-VTT can be found. */
7067 BINFO_SUBVTT_INDEX (binfo) = *index;
7070 /* Add the address of the primary vtable for the complete object. */
7071 init = binfo_ctor_vtable (binfo);
7072 *inits = build_tree_list (NULL_TREE, init);
7073 inits = &TREE_CHAIN (*inits);
7074 if (top_level_p)
7076 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7077 BINFO_VPTR_INDEX (binfo) = *index;
7079 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7081 /* Recursively add the secondary VTTs for non-virtual bases. */
7082 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7084 b = BINFO_BASETYPE (binfo, i);
7085 if (!TREE_VIA_VIRTUAL (b))
7086 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
7087 inits, index);
7090 /* Add secondary virtual pointers for all subobjects of BINFO with
7091 either virtual bases or reachable along a virtual path, except
7092 subobjects that are non-virtual primary bases. */
7093 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7094 TREE_TYPE (secondary_vptrs) = *index;
7095 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7096 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7098 dfs_walk_real (binfo,
7099 dfs_build_secondary_vptr_vtt_inits,
7100 NULL,
7101 dfs_ctor_vtable_bases_queue_p,
7102 secondary_vptrs);
7103 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7104 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7105 secondary_vptrs);
7107 *index = TREE_TYPE (secondary_vptrs);
7109 /* The secondary vptrs come back in reverse order. After we reverse
7110 them, and add the INITS, the last init will be the first element
7111 of the chain. */
7112 secondary_vptrs = TREE_VALUE (secondary_vptrs);
7113 if (secondary_vptrs)
7115 *inits = nreverse (secondary_vptrs);
7116 inits = &TREE_CHAIN (secondary_vptrs);
7117 my_friendly_assert (*inits == NULL_TREE, 20000517);
7120 /* Add the secondary VTTs for virtual bases. */
7121 if (top_level_p)
7122 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7124 tree vbase;
7126 if (!TREE_VIA_VIRTUAL (b))
7127 continue;
7129 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7130 inits = build_vtt_inits (vbase, t, inits, index);
7133 if (!top_level_p)
7135 tree data = tree_cons (t, binfo, NULL_TREE);
7136 VTT_TOP_LEVEL_P (data) = 0;
7137 VTT_MARKED_BINFO_P (data) = 0;
7139 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7140 dfs_ctor_vtable_bases_queue_p,
7141 data);
7144 return inits;
7147 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7148 for the base in most derived. DATA is a TREE_LIST who's
7149 TREE_CHAIN is the type of the base being
7150 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7151 flag of DATA indicates that this is a constructor vtable. The
7152 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7154 static tree
7155 dfs_build_secondary_vptr_vtt_inits (binfo, data)
7156 tree binfo;
7157 void *data;
7159 tree l;
7160 tree t;
7161 tree init;
7162 tree index;
7163 int top_level_p;
7165 l = (tree) data;
7166 t = TREE_CHAIN (l);
7167 top_level_p = VTT_TOP_LEVEL_P (l);
7169 SET_BINFO_MARKED (binfo);
7171 /* We don't care about bases that don't have vtables. */
7172 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7173 return NULL_TREE;
7175 /* We're only interested in proper subobjects of T. */
7176 if (same_type_p (BINFO_TYPE (binfo), t))
7177 return NULL_TREE;
7179 /* We're not interested in non-virtual primary bases. */
7180 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7181 return NULL_TREE;
7183 /* If BINFO has virtual bases or is reachable via a virtual path
7184 from T, it'll have a secondary vptr. */
7185 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7186 && !binfo_via_virtual (binfo, t))
7187 return NULL_TREE;
7189 /* Record the index where this secondary vptr can be found. */
7190 index = TREE_TYPE (l);
7191 if (top_level_p)
7193 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7194 BINFO_VPTR_INDEX (binfo) = index;
7196 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7197 TYPE_SIZE_UNIT (ptr_type_node));
7199 /* Add the initializer for the secondary vptr itself. */
7200 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7202 /* It's a primary virtual base, and this is not the construction
7203 vtable. Find the base this is primary of in the inheritance graph,
7204 and use that base's vtable now. */
7205 while (BINFO_PRIMARY_BASE_OF (binfo))
7206 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7208 init = binfo_ctor_vtable (binfo);
7209 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7211 return NULL_TREE;
7214 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7215 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7216 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7217 hierarchy. */
7219 static tree
7220 dfs_ctor_vtable_bases_queue_p (binfo, data)
7221 tree binfo;
7222 void *data;
7224 if (TREE_VIA_VIRTUAL (binfo))
7225 /* Get the shared version. */
7226 binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
7228 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7229 return NULL_TREE;
7230 return binfo;
7233 /* Called from build_vtt_inits via dfs_walk. After building constructor
7234 vtables and generating the sub-vtt from them, we need to restore the
7235 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7236 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7238 static tree
7239 dfs_fixup_binfo_vtbls (binfo, data)
7240 tree binfo;
7241 void *data;
7243 CLEAR_BINFO_MARKED (binfo);
7245 /* We don't care about bases that don't have vtables. */
7246 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7247 return NULL_TREE;
7249 /* If we scribbled the construction vtable vptr into BINFO, clear it
7250 out now. */
7251 if (BINFO_VTABLE (binfo)
7252 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7253 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7254 == TREE_VALUE ((tree) data)))
7255 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7257 return NULL_TREE;
7260 /* Build the construction vtable group for BINFO which is in the
7261 hierarchy dominated by T. */
7263 static void
7264 build_ctor_vtbl_group (binfo, t)
7265 tree binfo;
7266 tree t;
7268 tree list;
7269 tree type;
7270 tree vtbl;
7271 tree inits;
7272 tree id;
7273 tree vbase;
7275 /* See if we've already created this construction vtable group. */
7276 id = mangle_ctor_vtbl_for_type (t, binfo);
7277 if (IDENTIFIER_GLOBAL_VALUE (id))
7278 return;
7280 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7281 /* Build a version of VTBL (with the wrong type) for use in
7282 constructing the addresses of secondary vtables in the
7283 construction vtable group. */
7284 vtbl = build_vtable (t, id, ptr_type_node);
7285 list = build_tree_list (vtbl, NULL_TREE);
7286 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7287 binfo, t, list);
7289 /* Add the vtables for each of our virtual bases using the vbase in T
7290 binfo. */
7291 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7292 vbase;
7293 vbase = TREE_CHAIN (vbase))
7295 tree b;
7296 tree orig_base;
7298 if (!TREE_VIA_VIRTUAL (vbase))
7299 continue;
7300 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7301 orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7303 accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7305 inits = TREE_VALUE (list);
7307 /* Figure out the type of the construction vtable. */
7308 type = build_index_type (size_int (list_length (inits) - 1));
7309 type = build_cplus_array_type (vtable_entry_type, type);
7310 TREE_TYPE (vtbl) = type;
7312 /* Initialize the construction vtable. */
7313 pushdecl_top_level (vtbl);
7314 initialize_array (vtbl, inits);
7315 dump_vtable (t, binfo, vtbl);
7318 /* Add the vtbl initializers for BINFO (and its bases other than
7319 non-virtual primaries) to the list of INITS. BINFO is in the
7320 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7321 the constructor the vtbl inits should be accumulated for. (If this
7322 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7323 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7324 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7325 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7326 but are not necessarily the same in terms of layout. */
7328 static void
7329 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7330 tree binfo;
7331 tree orig_binfo;
7332 tree rtti_binfo;
7333 tree t;
7334 tree inits;
7336 int i;
7337 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7339 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7340 BINFO_TYPE (orig_binfo)),
7341 20000517);
7343 /* If it doesn't have a vptr, we don't do anything. */
7344 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7345 return;
7347 /* If we're building a construction vtable, we're not interested in
7348 subobjects that don't require construction vtables. */
7349 if (ctor_vtbl_p
7350 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7351 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7352 return;
7354 /* Build the initializers for the BINFO-in-T vtable. */
7355 TREE_VALUE (inits)
7356 = chainon (TREE_VALUE (inits),
7357 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7358 rtti_binfo, t, inits));
7360 /* Walk the BINFO and its bases. We walk in preorder so that as we
7361 initialize each vtable we can figure out at what offset the
7362 secondary vtable lies from the primary vtable. We can't use
7363 dfs_walk here because we need to iterate through bases of BINFO
7364 and RTTI_BINFO simultaneously. */
7365 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7367 tree base_binfo = BINFO_BASETYPE (binfo, i);
7369 /* Skip virtual bases. */
7370 if (TREE_VIA_VIRTUAL (base_binfo))
7371 continue;
7372 accumulate_vtbl_inits (base_binfo,
7373 BINFO_BASETYPE (orig_binfo, i),
7374 rtti_binfo, t,
7375 inits);
7379 /* Called from accumulate_vtbl_inits. Returns the initializers for
7380 the BINFO vtable. */
7382 static tree
7383 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7384 tree binfo;
7385 tree orig_binfo;
7386 tree rtti_binfo;
7387 tree t;
7388 tree l;
7390 tree inits = NULL_TREE;
7391 tree vtbl = NULL_TREE;
7392 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7394 if (ctor_vtbl_p
7395 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7397 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7398 primary virtual base. If it is not the same primary in
7399 the hierarchy of T, we'll need to generate a ctor vtable
7400 for it, to place at its location in T. If it is the same
7401 primary, we still need a VTT entry for the vtable, but it
7402 should point to the ctor vtable for the base it is a
7403 primary for within the sub-hierarchy of RTTI_BINFO.
7405 There are three possible cases:
7407 1) We are in the same place.
7408 2) We are a primary base within a lost primary virtual base of
7409 RTTI_BINFO.
7410 3) We are primary to something not a base of RTTI_BINFO. */
7412 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7413 tree last = NULL_TREE;
7415 /* First, look through the bases we are primary to for RTTI_BINFO
7416 or a virtual base. */
7417 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7419 last = b;
7420 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7421 break;
7423 /* If we run out of primary links, keep looking down our
7424 inheritance chain; we might be an indirect primary. */
7425 if (b == NULL_TREE)
7426 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7427 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7428 break;
7430 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7431 base B and it is a base of RTTI_BINFO, this is case 2. In
7432 either case, we share our vtable with LAST, i.e. the
7433 derived-most base within B of which we are a primary. */
7434 if (b == rtti_binfo
7435 || (b && binfo_for_vbase (BINFO_TYPE (b),
7436 BINFO_TYPE (rtti_binfo))))
7437 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7438 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7439 binfo_ctor_vtable after everything's been set up. */
7440 vtbl = last;
7442 /* Otherwise, this is case 3 and we get our own. */
7444 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7445 return inits;
7447 if (!vtbl)
7449 tree index;
7450 int non_fn_entries;
7452 /* Compute the initializer for this vtable. */
7453 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7454 &non_fn_entries);
7456 /* Figure out the position to which the VPTR should point. */
7457 vtbl = TREE_PURPOSE (l);
7458 vtbl = build1 (ADDR_EXPR,
7459 vtbl_ptr_type_node,
7460 vtbl);
7461 TREE_CONSTANT (vtbl) = 1;
7462 index = size_binop (PLUS_EXPR,
7463 size_int (non_fn_entries),
7464 size_int (list_length (TREE_VALUE (l))));
7465 index = size_binop (MULT_EXPR,
7466 TYPE_SIZE_UNIT (vtable_entry_type),
7467 index);
7468 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7469 TREE_CONSTANT (vtbl) = 1;
7472 if (ctor_vtbl_p)
7473 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7474 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7475 straighten this out. */
7476 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7477 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7478 inits = NULL_TREE;
7479 else
7480 /* For an ordinary vtable, set BINFO_VTABLE. */
7481 BINFO_VTABLE (binfo) = vtbl;
7483 return inits;
7486 /* Construct the initializer for BINFO's virtual function table. BINFO
7487 is part of the hierarchy dominated by T. If we're building a
7488 construction vtable, the ORIG_BINFO is the binfo we should use to
7489 find the actual function pointers to put in the vtable - but they
7490 can be overridden on the path to most-derived in the graph that
7491 ORIG_BINFO belongs. Otherwise,
7492 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7493 BINFO that should be indicated by the RTTI information in the
7494 vtable; it will be a base class of T, rather than T itself, if we
7495 are building a construction vtable.
7497 The value returned is a TREE_LIST suitable for wrapping in a
7498 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7499 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7500 number of non-function entries in the vtable.
7502 It might seem that this function should never be called with a
7503 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7504 base is always subsumed by a derived class vtable. However, when
7505 we are building construction vtables, we do build vtables for
7506 primary bases; we need these while the primary base is being
7507 constructed. */
7509 static tree
7510 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7511 tree binfo;
7512 tree orig_binfo;
7513 tree t;
7514 tree rtti_binfo;
7515 int *non_fn_entries_p;
7517 tree v, b;
7518 tree vfun_inits;
7519 tree vbase;
7520 vtbl_init_data vid;
7522 /* Initialize VID. */
7523 memset (&vid, 0, sizeof (vid));
7524 vid.binfo = binfo;
7525 vid.derived = t;
7526 vid.rtti_binfo = rtti_binfo;
7527 vid.last_init = &vid.inits;
7528 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7529 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7530 /* The first vbase or vcall offset is at index -3 in the vtable. */
7531 vid.index = ssize_int (-3);
7533 /* Add entries to the vtable for RTTI. */
7534 build_rtti_vtbl_entries (binfo, &vid);
7536 /* Create an array for keeping track of the functions we've
7537 processed. When we see multiple functions with the same
7538 signature, we share the vcall offsets. */
7539 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7540 /* Add the vcall and vbase offset entries. */
7541 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7542 /* Clean up. */
7543 VARRAY_FREE (vid.fns);
7544 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7545 build_vbase_offset_vtbl_entries. */
7546 for (vbase = CLASSTYPE_VBASECLASSES (t);
7547 vbase;
7548 vbase = TREE_CHAIN (vbase))
7549 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7551 if (non_fn_entries_p)
7552 *non_fn_entries_p = list_length (vid.inits);
7554 /* Go through all the ordinary virtual functions, building up
7555 initializers. */
7556 vfun_inits = NULL_TREE;
7557 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7559 tree delta;
7560 tree vcall_index;
7561 tree fn;
7562 tree pfn;
7563 tree init;
7565 /* Pull the offset for `this', and the function to call, out of
7566 the list. */
7567 delta = BV_DELTA (v);
7569 if (BV_USE_VCALL_INDEX_P (v))
7571 vcall_index = BV_VCALL_INDEX (v);
7572 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
7574 else
7575 vcall_index = NULL_TREE;
7577 fn = BV_FN (v);
7578 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7579 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7581 /* You can't call an abstract virtual function; it's abstract.
7582 So, we replace these functions with __pure_virtual. */
7583 if (DECL_PURE_VIRTUAL_P (fn))
7584 fn = abort_fndecl;
7586 /* Take the address of the function, considering it to be of an
7587 appropriate generic type. */
7588 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7589 /* The address of a function can't change. */
7590 TREE_CONSTANT (pfn) = 1;
7592 /* Enter it in the vtable. */
7593 init = build_vtable_entry (delta, vcall_index, pfn);
7595 /* If the only definition of this function signature along our
7596 primary base chain is from a lost primary, this vtable slot will
7597 never be used, so just zero it out. This is important to avoid
7598 requiring extra thunks which cannot be generated with the function.
7600 We could also handle this in update_vtable_entry_for_fn; doing it
7601 here means we zero out unused slots in ctor vtables as well,
7602 rather than filling them with erroneous values (though harmless,
7603 apart from relocation costs). */
7604 if (fn != abort_fndecl)
7605 for (b = binfo; ; b = get_primary_binfo (b))
7607 /* We found a defn before a lost primary; go ahead as normal. */
7608 if (look_for_overrides_here (BINFO_TYPE (b), fn))
7609 break;
7611 /* The nearest definition is from a lost primary; clear the
7612 slot. */
7613 if (BINFO_LOST_PRIMARY_P (b))
7615 init = size_zero_node;
7616 break;
7620 /* And add it to the chain of initializers. */
7621 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7624 /* The initializers for virtual functions were built up in reverse
7625 order; straighten them out now. */
7626 vfun_inits = nreverse (vfun_inits);
7628 /* The negative offset initializers are also in reverse order. */
7629 vid.inits = nreverse (vid.inits);
7631 /* Chain the two together. */
7632 return chainon (vid.inits, vfun_inits);
7635 /* Adds to vid->inits the initializers for the vbase and vcall
7636 offsets in BINFO, which is in the hierarchy dominated by T. */
7638 static void
7639 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7640 tree binfo;
7641 vtbl_init_data *vid;
7643 tree b;
7645 /* If this is a derived class, we must first create entries
7646 corresponding to the primary base class. */
7647 b = get_primary_binfo (binfo);
7648 if (b)
7649 build_vcall_and_vbase_vtbl_entries (b, vid);
7651 /* Add the vbase entries for this base. */
7652 build_vbase_offset_vtbl_entries (binfo, vid);
7653 /* Add the vcall entries for this base. */
7654 build_vcall_offset_vtbl_entries (binfo, vid);
7657 /* Returns the initializers for the vbase offset entries in the vtable
7658 for BINFO (which is part of the class hierarchy dominated by T), in
7659 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7660 where the next vbase offset will go. */
7662 static void
7663 build_vbase_offset_vtbl_entries (binfo, vid)
7664 tree binfo;
7665 vtbl_init_data *vid;
7667 tree vbase;
7668 tree t;
7669 tree non_primary_binfo;
7671 /* If there are no virtual baseclasses, then there is nothing to
7672 do. */
7673 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7674 return;
7676 t = vid->derived;
7678 /* We might be a primary base class. Go up the inheritance hierarchy
7679 until we find the most derived class of which we are a primary base:
7680 it is the offset of that which we need to use. */
7681 non_primary_binfo = binfo;
7682 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7684 tree b;
7686 /* If we have reached a virtual base, then it must be a primary
7687 base (possibly multi-level) of vid->binfo, or we wouldn't
7688 have called build_vcall_and_vbase_vtbl_entries for it. But it
7689 might be a lost primary, so just skip down to vid->binfo. */
7690 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7692 non_primary_binfo = vid->binfo;
7693 break;
7696 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7697 if (get_primary_binfo (b) != non_primary_binfo)
7698 break;
7699 non_primary_binfo = b;
7702 /* Go through the virtual bases, adding the offsets. */
7703 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7704 vbase;
7705 vbase = TREE_CHAIN (vbase))
7707 tree b;
7708 tree delta;
7710 if (!TREE_VIA_VIRTUAL (vbase))
7711 continue;
7713 /* Find the instance of this virtual base in the complete
7714 object. */
7715 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7717 /* If we've already got an offset for this virtual base, we
7718 don't need another one. */
7719 if (BINFO_VTABLE_PATH_MARKED (b))
7720 continue;
7721 SET_BINFO_VTABLE_PATH_MARKED (b);
7723 /* Figure out where we can find this vbase offset. */
7724 delta = size_binop (MULT_EXPR,
7725 vid->index,
7726 convert (ssizetype,
7727 TYPE_SIZE_UNIT (vtable_entry_type)));
7728 if (vid->primary_vtbl_p)
7729 BINFO_VPTR_FIELD (b) = delta;
7731 if (binfo != TYPE_BINFO (t))
7733 tree orig_vbase;
7735 /* Find the instance of this virtual base in the type of BINFO. */
7736 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7737 BINFO_TYPE (binfo));
7739 /* The vbase offset had better be the same. */
7740 if (!tree_int_cst_equal (delta,
7741 BINFO_VPTR_FIELD (orig_vbase)))
7742 my_friendly_abort (20000403);
7745 /* The next vbase will come at a more negative offset. */
7746 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7748 /* The initializer is the delta from BINFO to this virtual base.
7749 The vbase offsets go in reverse inheritance-graph order, and
7750 we are walking in inheritance graph order so these end up in
7751 the right order. */
7752 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7754 *vid->last_init
7755 = build_tree_list (NULL_TREE,
7756 fold (build1 (NOP_EXPR,
7757 vtable_entry_type,
7758 delta)));
7759 vid->last_init = &TREE_CHAIN (*vid->last_init);
7763 /* Adds the initializers for the vcall offset entries in the vtable
7764 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7765 to VID->INITS. */
7767 static void
7768 build_vcall_offset_vtbl_entries (binfo, vid)
7769 tree binfo;
7770 vtbl_init_data *vid;
7772 /* We only need these entries if this base is a virtual base. */
7773 if (!TREE_VIA_VIRTUAL (binfo))
7774 return;
7776 /* We need a vcall offset for each of the virtual functions in this
7777 vtable. For example:
7779 class A { virtual void f (); };
7780 class B1 : virtual public A { virtual void f (); };
7781 class B2 : virtual public A { virtual void f (); };
7782 class C: public B1, public B2 { virtual void f (); };
7784 A C object has a primary base of B1, which has a primary base of A. A
7785 C also has a secondary base of B2, which no longer has a primary base
7786 of A. So the B2-in-C construction vtable needs a secondary vtable for
7787 A, which will adjust the A* to a B2* to call f. We have no way of
7788 knowing what (or even whether) this offset will be when we define B2,
7789 so we store this "vcall offset" in the A sub-vtable and look it up in
7790 a "virtual thunk" for B2::f.
7792 The location of `A' is not at a fixed offset relative to `B'; the
7793 offset depends on the complete object derived from `B'. So,
7794 `B' vtable contains an entry for `f' that indicates by what
7795 amount the `this' pointer for `B' needs to be adjusted to arrive
7796 at `A'.
7798 We need entries for all the functions in our primary vtable and
7799 in our non-virtual bases' secondary vtables. */
7800 vid->vbase = binfo;
7801 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7802 add_vcall_offset_vtbl_entries_r (binfo, vid);
7805 /* Build vcall offsets, starting with those for BINFO. */
7807 static void
7808 add_vcall_offset_vtbl_entries_r (binfo, vid)
7809 tree binfo;
7810 vtbl_init_data *vid;
7812 int i;
7813 tree primary_binfo;
7815 /* Don't walk into virtual bases -- except, of course, for the
7816 virtual base for which we are building vcall offsets. Any
7817 primary virtual base will have already had its offsets generated
7818 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7819 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7820 return;
7822 /* If BINFO has a primary base, process it first. */
7823 primary_binfo = get_primary_binfo (binfo);
7824 if (primary_binfo)
7825 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7827 /* Add BINFO itself to the list. */
7828 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7830 /* Scan the non-primary bases of BINFO. */
7831 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7833 tree base_binfo;
7835 base_binfo = BINFO_BASETYPE (binfo, i);
7836 if (base_binfo != primary_binfo)
7837 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7841 /* Called from build_vcall_offset_vtbl_entries_r. */
7843 static void
7844 add_vcall_offset_vtbl_entries_1 (binfo, vid)
7845 tree binfo;
7846 vtbl_init_data* vid;
7848 tree derived_virtuals;
7849 tree base_virtuals;
7850 tree orig_virtuals;
7851 tree binfo_inits;
7852 /* If BINFO is a primary base, the most derived class which has BINFO as
7853 a primary base; otherwise, just BINFO. */
7854 tree non_primary_binfo;
7856 binfo_inits = NULL_TREE;
7858 /* We might be a primary base class. Go up the inheritance hierarchy
7859 until we find the most derived class of which we are a primary base:
7860 it is the BINFO_VIRTUALS there that we need to consider. */
7861 non_primary_binfo = binfo;
7862 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7864 tree b;
7866 /* If we have reached a virtual base, then it must be vid->vbase,
7867 because we ignore other virtual bases in
7868 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7869 base (possibly multi-level) of vid->binfo, or we wouldn't
7870 have called build_vcall_and_vbase_vtbl_entries for it. But it
7871 might be a lost primary, so just skip down to vid->binfo. */
7872 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7874 if (non_primary_binfo != vid->vbase)
7875 abort ();
7876 non_primary_binfo = vid->binfo;
7877 break;
7880 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7881 if (get_primary_binfo (b) != non_primary_binfo)
7882 break;
7883 non_primary_binfo = b;
7886 if (vid->ctor_vtbl_p)
7887 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7888 where rtti_binfo is the most derived type. */
7889 non_primary_binfo = get_original_base
7890 (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
7892 /* Make entries for the rest of the virtuals. */
7893 for (base_virtuals = BINFO_VIRTUALS (binfo),
7894 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7895 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7896 base_virtuals;
7897 base_virtuals = TREE_CHAIN (base_virtuals),
7898 derived_virtuals = TREE_CHAIN (derived_virtuals),
7899 orig_virtuals = TREE_CHAIN (orig_virtuals))
7901 tree orig_fn;
7902 tree fn;
7903 tree base;
7904 tree base_binfo;
7905 size_t i;
7906 tree vcall_offset;
7908 /* Find the declaration that originally caused this function to
7909 be present in BINFO_TYPE (binfo). */
7910 orig_fn = BV_FN (orig_virtuals);
7912 /* When processing BINFO, we only want to generate vcall slots for
7913 function slots introduced in BINFO. So don't try to generate
7914 one if the function isn't even defined in BINFO. */
7915 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7916 continue;
7918 /* Find the overriding function. */
7919 fn = BV_FN (derived_virtuals);
7921 /* If there is already an entry for a function with the same
7922 signature as FN, then we do not need a second vcall offset.
7923 Check the list of functions already present in the derived
7924 class vtable. */
7925 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7927 tree derived_entry;
7929 derived_entry = VARRAY_TREE (vid->fns, i);
7930 if (same_signature_p (BV_FN (derived_entry), fn)
7931 /* We only use one vcall offset for virtual destructors,
7932 even though there are two virtual table entries. */
7933 || (DECL_DESTRUCTOR_P (BV_FN (derived_entry))
7934 && DECL_DESTRUCTOR_P (fn)))
7936 if (!vid->ctor_vtbl_p)
7937 BV_VCALL_INDEX (derived_virtuals)
7938 = BV_VCALL_INDEX (derived_entry);
7939 break;
7942 if (i != VARRAY_ACTIVE_SIZE (vid->fns))
7943 continue;
7945 /* The FN comes from BASE. So, we must calculate the adjustment from
7946 vid->vbase to BASE. We can just look for BASE in the complete
7947 object because we are converting from a virtual base, so if there
7948 were multiple copies, there would not be a unique final overrider
7949 and vid->derived would be ill-formed. */
7950 base = DECL_CONTEXT (fn);
7951 base_binfo = get_binfo (base, vid->derived, /*protect=*/0);
7953 /* Compute the vcall offset. */
7954 /* As mentioned above, the vbase we're working on is a primary base of
7955 vid->binfo. But it might be a lost primary, so its BINFO_OFFSET
7956 might be wrong, so we just use the BINFO_OFFSET from vid->binfo. */
7957 vcall_offset = BINFO_OFFSET (vid->binfo);
7958 vcall_offset = size_diffop (BINFO_OFFSET (base_binfo),
7959 vcall_offset);
7960 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
7961 vcall_offset));
7963 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7964 vid->last_init = &TREE_CHAIN (*vid->last_init);
7966 /* Keep track of the vtable index where this vcall offset can be
7967 found. For a construction vtable, we already made this
7968 annotation when we built the original vtable. */
7969 if (!vid->ctor_vtbl_p)
7970 BV_VCALL_INDEX (derived_virtuals) = vid->index;
7972 /* The next vcall offset will be found at a more negative
7973 offset. */
7974 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7976 /* Keep track of this function. */
7977 VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
7981 /* Return vtbl initializers for the RTTI entries coresponding to the
7982 BINFO's vtable. The RTTI entries should indicate the object given
7983 by VID->rtti_binfo. */
7985 static void
7986 build_rtti_vtbl_entries (binfo, vid)
7987 tree binfo;
7988 vtbl_init_data *vid;
7990 tree b;
7991 tree t;
7992 tree basetype;
7993 tree offset;
7994 tree decl;
7995 tree init;
7997 basetype = BINFO_TYPE (binfo);
7998 t = BINFO_TYPE (vid->rtti_binfo);
8000 /* To find the complete object, we will first convert to our most
8001 primary base, and then add the offset in the vtbl to that value. */
8002 b = binfo;
8003 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8004 && !BINFO_LOST_PRIMARY_P (b))
8006 tree primary_base;
8008 primary_base = get_primary_binfo (b);
8009 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
8010 b = primary_base;
8012 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8014 /* The second entry is the address of the typeinfo object. */
8015 if (flag_rtti)
8016 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
8017 else
8018 decl = integer_zero_node;
8020 /* Convert the declaration to a type that can be stored in the
8021 vtable. */
8022 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
8023 TREE_CONSTANT (init) = 1;
8024 *vid->last_init = build_tree_list (NULL_TREE, init);
8025 vid->last_init = &TREE_CHAIN (*vid->last_init);
8027 /* Add the offset-to-top entry. It comes earlier in the vtable that
8028 the the typeinfo entry. Convert the offset to look like a
8029 function pointer, so that we can put it in the vtable. */
8030 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
8031 TREE_CONSTANT (init) = 1;
8032 *vid->last_init = build_tree_list (NULL_TREE, init);
8033 vid->last_init = &TREE_CHAIN (*vid->last_init);
8036 /* Build an entry in the virtual function table. DELTA is the offset
8037 for the `this' pointer. VCALL_INDEX is the vtable index containing
8038 the vcall offset; NULL_TREE if none. ENTRY is the virtual function
8039 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
8040 but it may not actually be a virtual function table pointer. (For
8041 example, it might be the address of the RTTI object, under the new
8042 ABI.) */
8044 static tree
8045 build_vtable_entry (delta, vcall_index, entry)
8046 tree delta;
8047 tree vcall_index;
8048 tree entry;
8050 tree fn = TREE_OPERAND (entry, 0);
8052 if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
8053 && fn != abort_fndecl)
8055 entry = make_thunk (entry, delta, vcall_index);
8056 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
8057 TREE_READONLY (entry) = 1;
8058 TREE_CONSTANT (entry) = 1;
8060 #ifdef GATHER_STATISTICS
8061 n_vtable_entries += 1;
8062 #endif
8063 return entry;