* call.c: Fix comment formatting.
[official-gcc.git] / gcc / cp / class.c
blob48dadea487c4fdd736a5bbdd8e30a28d283613d7
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, 2002 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"
36 #include "target.h"
38 /* The number of nested classes being processed. If we are not in the
39 scope of any class, this is zero. */
41 int current_class_depth;
43 /* In order to deal with nested classes, we keep a stack of classes.
44 The topmost entry is the innermost class, and is the entry at index
45 CURRENT_CLASS_DEPTH */
47 typedef struct class_stack_node {
48 /* The name of the class. */
49 tree name;
51 /* The _TYPE node for the class. */
52 tree type;
54 /* The access specifier pending for new declarations in the scope of
55 this class. */
56 tree access;
58 /* If were defining TYPE, the names used in this class. */
59 splay_tree names_used;
60 }* class_stack_node_t;
62 typedef struct vtbl_init_data_s
64 /* The base for which we're building initializers. */
65 tree binfo;
66 /* The type of the most-derived type. */
67 tree derived;
68 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
69 unless ctor_vtbl_p is true. */
70 tree rtti_binfo;
71 /* The negative-index vtable initializers built up so far. These
72 are in order from least negative index to most negative index. */
73 tree inits;
74 /* The last (i.e., most negative) entry in INITS. */
75 tree* last_init;
76 /* The binfo for the virtual base for which we're building
77 vcall offset initializers. */
78 tree vbase;
79 /* The functions in vbase for which we have already provided vcall
80 offsets. */
81 varray_type fns;
82 /* The vtable index of the next vcall or vbase offset. */
83 tree index;
84 /* Nonzero if we are building the initializer for the primary
85 vtable. */
86 int primary_vtbl_p;
87 /* Nonzero if we are building the initializer for a construction
88 vtable. */
89 int ctor_vtbl_p;
90 } vtbl_init_data;
92 /* The type of a function passed to walk_subobject_offsets. */
93 typedef int (*subobject_offset_fn) PARAMS ((tree, tree, splay_tree));
95 /* The stack itself. This is an dynamically resized array. The
96 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
97 static int current_class_stack_size;
98 static class_stack_node_t current_class_stack;
100 /* An array of all local classes present in this translation unit, in
101 declaration order. */
102 varray_type local_classes;
104 static tree get_vfield_name PARAMS ((tree));
105 static void finish_struct_anon PARAMS ((tree));
106 static tree build_vtable_entry PARAMS ((tree, tree, tree));
107 static tree get_vtable_name PARAMS ((tree));
108 static tree get_basefndecls PARAMS ((tree, tree));
109 static int build_primary_vtable PARAMS ((tree, tree));
110 static int build_secondary_vtable PARAMS ((tree, tree));
111 static void finish_vtbls PARAMS ((tree));
112 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
113 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
114 static void delete_duplicate_fields PARAMS ((tree));
115 static void finish_struct_bits PARAMS ((tree));
116 static int alter_access PARAMS ((tree, tree, tree));
117 static void handle_using_decl PARAMS ((tree, tree));
118 static int strictly_overrides PARAMS ((tree, tree));
119 static void check_for_override PARAMS ((tree, tree));
120 static tree dfs_modify_vtables PARAMS ((tree, void *));
121 static tree modify_all_vtables PARAMS ((tree, int *, tree));
122 static void determine_primary_base PARAMS ((tree, int *));
123 static void finish_struct_methods PARAMS ((tree));
124 static void maybe_warn_about_overly_private_class PARAMS ((tree));
125 static int field_decl_cmp PARAMS ((const tree *, const tree *));
126 static int method_name_cmp PARAMS ((const tree *, const tree *));
127 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
128 static tree fixed_type_or_null PARAMS ((tree, int *, int *));
129 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
130 int, int, tree));
131 static tree build_vtable_entry_ref PARAMS ((tree, tree, tree));
132 static tree build_vtbl_ref_1 PARAMS ((tree, tree));
133 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
134 static int count_fields PARAMS ((tree));
135 static int add_fields_to_vec PARAMS ((tree, tree, int));
136 static void check_bitfield_decl PARAMS ((tree));
137 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
138 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
139 int *));
140 static bool build_base_field PARAMS ((record_layout_info, tree, int *,
141 splay_tree, tree));
142 static bool build_base_fields PARAMS ((record_layout_info, int *,
143 splay_tree, tree));
144 static void check_methods PARAMS ((tree));
145 static void remove_zero_width_bit_fields PARAMS ((tree));
146 static void check_bases PARAMS ((tree, int *, int *, int *));
147 static void check_bases_and_members PARAMS ((tree, int *));
148 static tree create_vtable_ptr PARAMS ((tree, int *, tree *));
149 static void layout_class_type PARAMS ((tree, int *, int *, tree *));
150 static void fixup_pending_inline PARAMS ((tree));
151 static void fixup_inline_methods PARAMS ((tree));
152 static void set_primary_base PARAMS ((tree, tree, int *));
153 static void propagate_binfo_offsets PARAMS ((tree, tree, tree));
154 static void layout_virtual_bases PARAMS ((tree, splay_tree));
155 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
156 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
157 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
158 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
159 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
160 static void layout_vtable_decl PARAMS ((tree, int));
161 static tree dfs_find_final_overrider PARAMS ((tree, void *));
162 static tree find_final_overrider PARAMS ((tree, tree, tree));
163 static int make_new_vtable PARAMS ((tree, tree));
164 static int maybe_indent_hierarchy PARAMS ((FILE *, int, int));
165 static void dump_class_hierarchy_r PARAMS ((FILE *, int, tree, tree, int));
166 static void dump_class_hierarchy PARAMS ((tree));
167 static void dump_array PARAMS ((FILE *, tree));
168 static void dump_vtable PARAMS ((tree, tree, tree));
169 static void dump_vtt PARAMS ((tree, tree));
170 static tree build_vtable PARAMS ((tree, tree, tree));
171 static void initialize_vtable PARAMS ((tree, tree));
172 static void initialize_array PARAMS ((tree, tree));
173 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
174 tree, tree,
175 splay_tree, tree));
176 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
177 static bool layout_empty_base PARAMS ((tree, tree, splay_tree, tree));
178 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
179 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
180 tree));
181 static void set_vindex PARAMS ((tree, int *));
182 static void build_rtti_vtbl_entries PARAMS ((tree, vtbl_init_data *));
183 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
184 vtbl_init_data *));
185 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
186 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
187 static tree dfs_unshared_virtual_bases PARAMS ((tree, void *));
188 static void mark_primary_bases PARAMS ((tree));
189 static tree mark_primary_virtual_base PARAMS ((tree, tree));
190 static void clone_constructors_and_destructors PARAMS ((tree));
191 static tree build_clone PARAMS ((tree, tree));
192 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
193 static tree copy_virtuals PARAMS ((tree));
194 static void build_ctor_vtbl_group PARAMS ((tree, tree));
195 static void build_vtt PARAMS ((tree));
196 static tree binfo_ctor_vtable PARAMS ((tree));
197 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
198 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
199 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
200 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
201 static tree get_original_base PARAMS ((tree, tree));
202 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
203 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
204 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
205 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
206 tree, splay_tree, tree, int));
207 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
208 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
209 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
210 splay_tree_key k2));
211 static void warn_about_ambiguous_direct_bases PARAMS ((tree));
212 static bool type_requires_array_cookie PARAMS ((tree));
214 /* Macros for dfs walking during vtt construction. See
215 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
216 and dfs_fixup_binfo_vtbls. */
217 #define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
218 #define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
220 /* Variables shared between class.c and call.c. */
222 #ifdef GATHER_STATISTICS
223 int n_vtables = 0;
224 int n_vtable_entries = 0;
225 int n_vtable_searches = 0;
226 int n_vtable_elems = 0;
227 int n_convert_harshness = 0;
228 int n_compute_conversion_costs = 0;
229 int n_build_method_call = 0;
230 int n_inner_fields_searched = 0;
231 #endif
233 /* Convert to or from a base subobject. EXPR is an expression of type
234 `A' or `A*', an expression of type `B' or `B*' is returned. To
235 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
236 the B base instance within A. To convert base A to derived B, CODE
237 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
238 In this latter case, A must not be a morally virtual base of B.
239 NONNULL is true if EXPR is known to be non-NULL (this is only
240 needed when EXPR is of pointer type). CV qualifiers are preserved
241 from EXPR. */
243 tree
244 build_base_path (code, expr, binfo, nonnull)
245 enum tree_code code;
246 tree expr;
247 tree binfo;
248 int nonnull;
250 tree v_binfo = NULL_TREE;
251 tree d_binfo = NULL_TREE;
252 tree probe;
253 tree offset;
254 tree target_type;
255 tree null_test = NULL;
256 tree ptr_target_type;
257 int fixed_type_p;
258 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
260 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
261 return error_mark_node;
263 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
265 d_binfo = probe;
266 if (!v_binfo && TREE_VIA_VIRTUAL (probe))
267 v_binfo = probe;
270 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
271 if (want_pointer)
272 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
274 my_friendly_assert (code == MINUS_EXPR
275 ? same_type_p (BINFO_TYPE (binfo), probe)
276 : code == PLUS_EXPR
277 ? same_type_p (BINFO_TYPE (d_binfo), probe)
278 : false, 20010723);
280 if (code == MINUS_EXPR && v_binfo)
282 error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
283 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
284 return error_mark_node;
287 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
288 if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
289 expr = save_expr (expr);
291 if (!want_pointer)
292 expr = build_unary_op (ADDR_EXPR, expr, 0);
293 else if (!nonnull)
294 null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
296 offset = BINFO_OFFSET (binfo);
298 if (v_binfo && fixed_type_p <= 0)
300 /* Going via virtual base V_BINFO. We need the static offset
301 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
302 V_BINFO. That offset is an entry in D_BINFO's vtable. */
303 tree v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
304 TREE_TYPE (TREE_TYPE (expr)));
306 v_binfo = binfo_for_vbase (BINFO_TYPE (v_binfo), BINFO_TYPE (d_binfo));
308 v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
309 v_offset, BINFO_VPTR_FIELD (v_binfo));
310 v_offset = build1 (NOP_EXPR,
311 build_pointer_type (ptrdiff_type_node),
312 v_offset);
313 v_offset = build_indirect_ref (v_offset, NULL);
314 TREE_CONSTANT (v_offset) = 1;
316 offset = cp_convert (ptrdiff_type_node,
317 size_diffop (offset, BINFO_OFFSET (v_binfo)));
319 if (!integer_zerop (offset))
320 v_offset = build (code, ptrdiff_type_node, v_offset, offset);
322 if (fixed_type_p < 0)
323 /* Negative fixed_type_p means this is a constructor or destructor;
324 virtual base layout is fixed in in-charge [cd]tors, but not in
325 base [cd]tors. */
326 offset = build (COND_EXPR, ptrdiff_type_node,
327 build (EQ_EXPR, boolean_type_node,
328 current_in_charge_parm, integer_zero_node),
329 v_offset,
330 BINFO_OFFSET (binfo));
331 else
332 offset = v_offset;
335 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
337 target_type = cp_build_qualified_type
338 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
339 ptr_target_type = build_pointer_type (target_type);
340 if (want_pointer)
341 target_type = ptr_target_type;
343 expr = build1 (NOP_EXPR, ptr_target_type, expr);
345 if (!integer_zerop (offset))
346 expr = build (code, ptr_target_type, expr, offset);
347 else
348 null_test = NULL;
350 if (!want_pointer)
351 expr = build_indirect_ref (expr, NULL);
353 if (null_test)
354 expr = build (COND_EXPR, target_type, null_test,
355 build1 (NOP_EXPR, target_type, integer_zero_node),
356 expr);
358 return expr;
361 /* Convert OBJECT to the base TYPE. If CHECK_ACCESS is true, an error
362 message is emitted if TYPE is inaccessible. OBJECT is assumed to
363 be non-NULL. */
365 tree
366 convert_to_base (tree object, tree type, bool check_access)
368 tree binfo;
370 binfo = lookup_base (TREE_TYPE (object), type,
371 check_access ? ba_check : ba_ignore,
372 NULL);
373 if (!binfo || binfo == error_mark_node)
374 return error_mark_node;
376 return build_base_path (PLUS_EXPR, object, binfo, /*nonnull=*/1);
380 /* Virtual function things. */
382 static tree
383 build_vtable_entry_ref (array_ref, instance, idx)
384 tree array_ref, instance, idx;
386 tree i, i2, vtable, first_fn, basetype;
388 basetype = TREE_TYPE (instance);
389 if (TREE_CODE (basetype) == REFERENCE_TYPE)
390 basetype = TREE_TYPE (basetype);
392 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
393 first_fn = TYPE_BINFO_VTABLE (basetype);
395 i = fold (build_array_ref (first_fn, idx));
396 i = fold (build_c_cast (ptrdiff_type_node,
397 build_unary_op (ADDR_EXPR, i, 0)));
398 i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
399 i2 = fold (build_c_cast (ptrdiff_type_node,
400 build_unary_op (ADDR_EXPR, i2, 0)));
401 i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
403 if (TREE_CODE (i) != INTEGER_CST)
404 abort ();
406 return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
409 /* Given an object INSTANCE, return an expression which yields the
410 vtable element corresponding to INDEX. There are many special
411 cases for INSTANCE which we take care of here, mainly to avoid
412 creating extra tree nodes when we don't have to. */
414 static tree
415 build_vtbl_ref_1 (instance, idx)
416 tree instance, idx;
418 tree aref;
419 tree vtbl = NULL_TREE;
421 /* Try to figure out what a reference refers to, and
422 access its virtual function table directly. */
424 int cdtorp = 0;
425 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
427 tree basetype = TREE_TYPE (instance);
428 if (TREE_CODE (basetype) == REFERENCE_TYPE)
429 basetype = TREE_TYPE (basetype);
431 if (fixed_type && !cdtorp)
433 tree binfo = lookup_base (fixed_type, basetype,
434 ba_ignore|ba_quiet, NULL);
435 if (binfo)
436 vtbl = BINFO_VTABLE (binfo);
439 if (!vtbl)
441 vtbl = build_vfield_ref (instance, basetype);
444 assemble_external (vtbl);
446 aref = build_array_ref (vtbl, idx);
447 TREE_CONSTANT (aref) = 1;
449 return aref;
452 tree
453 build_vtbl_ref (instance, idx)
454 tree instance, idx;
456 tree aref = build_vtbl_ref_1 (instance, idx);
458 if (flag_vtable_gc)
459 aref = build_vtable_entry_ref (aref, instance, idx);
461 return aref;
464 /* Given an object INSTANCE, return an expression which yields a
465 function pointer corresponding to vtable element INDEX. */
467 tree
468 build_vfn_ref (instance, idx)
469 tree instance, idx;
471 tree aref = build_vtbl_ref_1 (instance, idx);
473 /* When using function descriptors, the address of the
474 vtable entry is treated as a function pointer. */
475 if (TARGET_VTABLE_USES_DESCRIPTORS)
476 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
477 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
479 if (flag_vtable_gc)
480 aref = build_vtable_entry_ref (aref, instance, idx);
482 return aref;
485 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
486 for the given TYPE. */
488 static tree
489 get_vtable_name (type)
490 tree type;
492 return mangle_vtbl_for_type (type);
495 /* Return an IDENTIFIER_NODE for the name of the virtual table table
496 for TYPE. */
498 tree
499 get_vtt_name (type)
500 tree type;
502 return mangle_vtt_for_type (type);
505 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
506 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
507 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
509 static tree
510 build_vtable (class_type, name, vtable_type)
511 tree class_type;
512 tree name;
513 tree vtable_type;
515 tree decl;
517 decl = build_lang_decl (VAR_DECL, name, vtable_type);
518 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
519 now to avoid confusion in mangle_decl. */
520 SET_DECL_ASSEMBLER_NAME (decl, name);
521 DECL_CONTEXT (decl) = class_type;
522 DECL_ARTIFICIAL (decl) = 1;
523 TREE_STATIC (decl) = 1;
524 TREE_READONLY (decl) = 1;
525 DECL_VIRTUAL_P (decl) = 1;
526 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
528 import_export_vtable (decl, class_type, 0);
530 return decl;
533 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
534 or even complete. If this does not exist, create it. If COMPLETE is
535 non-zero, then complete the definition of it -- that will render it
536 impossible to actually build the vtable, but is useful to get at those
537 which are known to exist in the runtime. */
539 tree
540 get_vtable_decl (type, complete)
541 tree type;
542 int complete;
544 tree name = get_vtable_name (type);
545 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
547 if (decl)
549 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
550 && DECL_VIRTUAL_P (decl), 20000118);
551 return decl;
554 decl = build_vtable (type, name, void_type_node);
555 decl = pushdecl_top_level (decl);
556 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
557 20000517);
559 /* At one time the vtable info was grabbed 2 words at a time. This
560 fails on sparc unless you have 8-byte alignment. (tiemann) */
561 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
562 DECL_ALIGN (decl));
564 if (complete)
566 DECL_EXTERNAL (decl) = 1;
567 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
570 return decl;
573 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
574 BV_VCALL_INDEX for each entry is cleared. */
576 static tree
577 copy_virtuals (binfo)
578 tree binfo;
580 tree copies;
581 tree t;
583 copies = copy_list (BINFO_VIRTUALS (binfo));
584 for (t = copies; t; t = TREE_CHAIN (t))
586 BV_VCALL_INDEX (t) = NULL_TREE;
587 BV_USE_VCALL_INDEX_P (t) = 0;
590 return copies;
593 /* Build the primary virtual function table for TYPE. If BINFO is
594 non-NULL, build the vtable starting with the initial approximation
595 that it is the same as the one which is the head of the association
596 list. Returns a non-zero value if a new vtable is actually
597 created. */
599 static int
600 build_primary_vtable (binfo, type)
601 tree binfo, type;
603 tree decl;
604 tree virtuals;
606 decl = get_vtable_decl (type, /*complete=*/0);
608 if (binfo)
610 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
611 /* We have already created a vtable for this base, so there's
612 no need to do it again. */
613 return 0;
615 virtuals = copy_virtuals (binfo);
616 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
617 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
618 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
620 else
622 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
623 20000118);
624 virtuals = NULL_TREE;
627 #ifdef GATHER_STATISTICS
628 n_vtables += 1;
629 n_vtable_elems += list_length (virtuals);
630 #endif
632 /* Initialize the association list for this type, based
633 on our first approximation. */
634 TYPE_BINFO_VTABLE (type) = decl;
635 TYPE_BINFO_VIRTUALS (type) = virtuals;
636 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
637 return 1;
640 /* Give BINFO a new virtual function table which is initialized
641 with a skeleton-copy of its original initialization. The only
642 entry that changes is the `delta' entry, so we can really
643 share a lot of structure.
645 FOR_TYPE is the most derived type which caused this table to
646 be needed.
648 Returns non-zero if we haven't met BINFO before.
650 The order in which vtables are built (by calling this function) for
651 an object must remain the same, otherwise a binary incompatibility
652 can result. */
654 static int
655 build_secondary_vtable (binfo, for_type)
656 tree binfo, for_type;
658 my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
660 if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
661 /* We already created a vtable for this base. There's no need to
662 do it again. */
663 return 0;
665 /* Remember that we've created a vtable for this BINFO, so that we
666 don't try to do so again. */
667 SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
669 /* Make fresh virtual list, so we can smash it later. */
670 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
672 /* Secondary vtables are laid out as part of the same structure as
673 the primary vtable. */
674 BINFO_VTABLE (binfo) = NULL_TREE;
675 return 1;
678 /* Create a new vtable for BINFO which is the hierarchy dominated by
679 T. Return non-zero if we actually created a new vtable. */
681 static int
682 make_new_vtable (t, binfo)
683 tree t;
684 tree binfo;
686 if (binfo == TYPE_BINFO (t))
687 /* In this case, it is *type*'s vtable we are modifying. We start
688 with the approximation that its vtable is that of the
689 immediate base class. */
690 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
691 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
692 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
694 else
695 /* This is our very own copy of `basetype' to play with. Later,
696 we will fill in all the virtual functions that override the
697 virtual functions in these base classes which are not defined
698 by the current type. */
699 return build_secondary_vtable (binfo, t);
702 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
703 (which is in the hierarchy dominated by T) list FNDECL as its
704 BV_FN. DELTA is the required constant adjustment from the `this'
705 pointer where the vtable entry appears to the `this' required when
706 the function is actually called. */
708 static void
709 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
710 tree t;
711 tree binfo;
712 tree fndecl;
713 tree delta;
714 tree *virtuals;
716 tree v;
718 v = *virtuals;
720 if (fndecl != BV_FN (v)
721 || !tree_int_cst_equal (delta, BV_DELTA (v)))
723 tree base_fndecl;
725 /* We need a new vtable for BINFO. */
726 if (make_new_vtable (t, binfo))
728 /* If we really did make a new vtable, we also made a copy
729 of the BINFO_VIRTUALS list. Now, we have to find the
730 corresponding entry in that list. */
731 *virtuals = BINFO_VIRTUALS (binfo);
732 while (BV_FN (*virtuals) != BV_FN (v))
733 *virtuals = TREE_CHAIN (*virtuals);
734 v = *virtuals;
737 base_fndecl = BV_FN (v);
738 BV_DELTA (v) = delta;
739 BV_VCALL_INDEX (v) = NULL_TREE;
740 BV_FN (v) = fndecl;
742 /* Now assign virtual dispatch information, if unset. We can
743 dispatch this through any overridden base function.
745 FIXME this can choose a secondary vtable if the primary is not
746 also lexically first, leading to useless conversions.
747 In the V3 ABI, there's no reason for DECL_VIRTUAL_CONTEXT to
748 ever be different from DECL_CONTEXT. */
749 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
751 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
752 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
757 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
758 functions present in the vtable so far. */
760 static void
761 set_vindex (decl, vfuns_p)
762 tree decl;
763 int *vfuns_p;
765 int vindex;
767 vindex = *vfuns_p;
768 *vfuns_p += (TARGET_VTABLE_USES_DESCRIPTORS
769 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
770 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
773 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
774 the method after the class has already been defined because a
775 declaration for it was seen. (Even though that is erroneous, we
776 add the method for improved error recovery.) */
778 void
779 add_method (type, method, error_p)
780 tree type;
781 tree method;
782 int error_p;
784 int using = (DECL_CONTEXT (method) != type);
785 int len;
786 int slot;
787 tree method_vec;
788 int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
789 && DECL_TEMPLATE_CONV_FN_P (method));
791 if (!CLASSTYPE_METHOD_VEC (type))
792 /* Make a new method vector. We start with 8 entries. We must
793 allocate at least two (for constructors and destructors), and
794 we're going to end up with an assignment operator at some point
795 as well.
797 We could use a TREE_LIST for now, and convert it to a TREE_VEC
798 in finish_struct, but we would probably waste more memory
799 making the links in the list than we would by over-allocating
800 the size of the vector here. Furthermore, we would complicate
801 all the code that expects this to be a vector. */
802 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
804 method_vec = CLASSTYPE_METHOD_VEC (type);
805 len = TREE_VEC_LENGTH (method_vec);
807 /* Constructors and destructors go in special slots. */
808 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
809 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
810 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
811 slot = CLASSTYPE_DESTRUCTOR_SLOT;
812 else
814 int have_template_convs_p = 0;
816 /* See if we already have an entry with this name. */
817 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
819 tree m = TREE_VEC_ELT (method_vec, slot);
821 if (!m)
822 break;
823 m = OVL_CURRENT (m);
825 if (template_conv_p)
827 have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
828 && DECL_TEMPLATE_CONV_FN_P (m));
830 /* If we need to move things up, see if there's
831 space. */
832 if (!have_template_convs_p)
834 slot = len - 1;
835 if (TREE_VEC_ELT (method_vec, slot))
836 slot++;
838 break;
840 if (DECL_NAME (m) == DECL_NAME (method))
841 break;
844 if (slot == len)
846 /* We need a bigger method vector. */
847 int new_len;
848 tree new_vec;
850 /* In the non-error case, we are processing a class
851 definition. Double the size of the vector to give room
852 for new methods. */
853 if (!error_p)
854 new_len = 2 * len;
855 /* In the error case, the vector is already complete. We
856 don't expect many errors, and the rest of the front-end
857 will get confused if there are empty slots in the vector. */
858 else
859 new_len = len + 1;
861 new_vec = make_tree_vec (new_len);
862 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
863 len * sizeof (tree));
864 len = new_len;
865 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
868 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
870 /* Type conversion operators have to come before ordinary
871 methods; add_conversions depends on this to speed up
872 looking for conversion operators. So, if necessary, we
873 slide some of the vector elements up. In theory, this
874 makes this algorithm O(N^2) but we don't expect many
875 conversion operators. */
876 if (template_conv_p)
877 slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
878 else
879 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
881 tree fn = TREE_VEC_ELT (method_vec, slot);
883 if (!fn)
884 /* There are no more entries in the vector, so we
885 can insert the new conversion operator here. */
886 break;
888 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
889 /* We can insert the new function right at the
890 SLOTth position. */
891 break;
894 if (template_conv_p && have_template_convs_p)
895 /*OK*/;
896 else if (!TREE_VEC_ELT (method_vec, slot))
897 /* There is nothing in the Ith slot, so we can avoid
898 moving anything. */
900 else
902 /* We know the last slot in the vector is empty
903 because we know that at this point there's room
904 for a new function. */
905 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
906 &TREE_VEC_ELT (method_vec, slot),
907 (len - slot - 1) * sizeof (tree));
908 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
913 if (template_class_depth (type))
914 /* TYPE is a template class. Don't issue any errors now; wait
915 until instantiation time to complain. */
917 else
919 tree fns;
921 /* Check to see if we've already got this method. */
922 for (fns = TREE_VEC_ELT (method_vec, slot);
923 fns;
924 fns = OVL_NEXT (fns))
926 tree fn = OVL_CURRENT (fns);
927 tree parms1;
928 tree parms2;
929 bool same = 1;
931 if (TREE_CODE (fn) != TREE_CODE (method))
932 continue;
934 /* [over.load] Member function declarations with the
935 same name and the same parameter types cannot be
936 overloaded if any of them is a static member
937 function declaration.
939 [namespace.udecl] When a using-declaration brings names
940 from a base class into a derived class scope, member
941 functions in the derived class override and/or hide member
942 functions with the same name and parameter types in a base
943 class (rather than conflicting). */
944 parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
945 parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
947 /* Compare the quals on the 'this' parm. Don't compare
948 the whole types, as used functions are treated as
949 coming from the using class in overload resolution. */
950 if (! DECL_STATIC_FUNCTION_P (fn)
951 && ! DECL_STATIC_FUNCTION_P (method)
952 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
953 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
954 same = 0;
955 if (! DECL_STATIC_FUNCTION_P (fn))
956 parms1 = TREE_CHAIN (parms1);
957 if (! DECL_STATIC_FUNCTION_P (method))
958 parms2 = TREE_CHAIN (parms2);
960 if (same && compparms (parms1, parms2)
961 && (!DECL_CONV_FN_P (fn)
962 || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
963 TREE_TYPE (TREE_TYPE (method)))))
965 if (using && DECL_CONTEXT (fn) == type)
966 /* Defer to the local function. */
967 return;
968 else
970 cp_error_at ("`%#D' and `%#D' cannot be overloaded",
971 method, fn, method);
973 /* We don't call duplicate_decls here to merge
974 the declarations because that will confuse
975 things if the methods have inline
976 definitions. In particular, we will crash
977 while processing the definitions. */
978 return;
984 /* Actually insert the new method. */
985 TREE_VEC_ELT (method_vec, slot)
986 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
988 /* Add the new binding. */
989 if (!DECL_CONSTRUCTOR_P (method)
990 && !DECL_DESTRUCTOR_P (method))
991 push_class_level_binding (DECL_NAME (method),
992 TREE_VEC_ELT (method_vec, slot));
995 /* Subroutines of finish_struct. */
997 /* Look through the list of fields for this struct, deleting
998 duplicates as we go. This must be recursive to handle
999 anonymous unions.
1001 FIELD is the field which may not appear anywhere in FIELDS.
1002 FIELD_PTR, if non-null, is the starting point at which
1003 chained deletions may take place.
1004 The value returned is the first acceptable entry found
1005 in FIELDS.
1007 Note that anonymous fields which are not of UNION_TYPE are
1008 not duplicates, they are just anonymous fields. This happens
1009 when we have unnamed bitfields, for example. */
1011 static tree
1012 delete_duplicate_fields_1 (field, fields)
1013 tree field, fields;
1015 tree x;
1016 tree prev = 0;
1017 if (DECL_NAME (field) == 0)
1019 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1020 return fields;
1022 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1023 fields = delete_duplicate_fields_1 (x, fields);
1024 return fields;
1026 else
1028 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1030 if (DECL_NAME (x) == 0)
1032 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1033 continue;
1034 TYPE_FIELDS (TREE_TYPE (x))
1035 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1036 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1038 if (prev == 0)
1039 fields = TREE_CHAIN (fields);
1040 else
1041 TREE_CHAIN (prev) = TREE_CHAIN (x);
1044 else if (TREE_CODE (field) == USING_DECL)
1045 /* A using declaration is allowed to appear more than
1046 once. We'll prune these from the field list later, and
1047 handle_using_decl will complain about invalid multiple
1048 uses. */
1050 else if (DECL_NAME (field) == DECL_NAME (x))
1052 if (TREE_CODE (field) == CONST_DECL
1053 && TREE_CODE (x) == CONST_DECL)
1054 cp_error_at ("duplicate enum value `%D'", x);
1055 else if (TREE_CODE (field) == CONST_DECL
1056 || TREE_CODE (x) == CONST_DECL)
1057 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1059 else if (DECL_DECLARES_TYPE_P (field)
1060 && DECL_DECLARES_TYPE_P (x))
1062 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1063 continue;
1064 cp_error_at ("duplicate nested type `%D'", x);
1066 else if (DECL_DECLARES_TYPE_P (field)
1067 || DECL_DECLARES_TYPE_P (x))
1069 /* Hide tag decls. */
1070 if ((TREE_CODE (field) == TYPE_DECL
1071 && DECL_ARTIFICIAL (field))
1072 || (TREE_CODE (x) == TYPE_DECL
1073 && DECL_ARTIFICIAL (x)))
1074 continue;
1075 cp_error_at ("duplicate field `%D' (as type and non-type)",
1078 else
1079 cp_error_at ("duplicate member `%D'", x);
1080 if (prev == 0)
1081 fields = TREE_CHAIN (fields);
1082 else
1083 TREE_CHAIN (prev) = TREE_CHAIN (x);
1087 return fields;
1090 static void
1091 delete_duplicate_fields (fields)
1092 tree fields;
1094 tree x;
1095 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1096 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1099 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1100 legit, otherwise return 0. */
1102 static int
1103 alter_access (t, fdecl, access)
1104 tree t;
1105 tree fdecl;
1106 tree access;
1108 tree elem;
1110 if (!DECL_LANG_SPECIFIC (fdecl))
1111 retrofit_lang_decl (fdecl);
1113 if (DECL_DISCRIMINATOR_P (fdecl))
1114 abort ();
1116 elem = purpose_member (t, DECL_ACCESS (fdecl));
1117 if (elem)
1119 if (TREE_VALUE (elem) != access)
1121 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1122 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1123 else
1124 error ("conflicting access specifications for field `%s', ignored",
1125 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1127 else
1129 /* They're changing the access to the same thing they changed
1130 it to before. That's OK. */
1134 else
1136 enforce_access (t, fdecl);
1137 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1138 return 1;
1140 return 0;
1143 /* Process the USING_DECL, which is a member of T. */
1145 static void
1146 handle_using_decl (using_decl, t)
1147 tree using_decl;
1148 tree t;
1150 tree ctype = DECL_INITIAL (using_decl);
1151 tree name = DECL_NAME (using_decl);
1152 tree access
1153 = TREE_PRIVATE (using_decl) ? access_private_node
1154 : TREE_PROTECTED (using_decl) ? access_protected_node
1155 : access_public_node;
1156 tree fdecl, binfo;
1157 tree flist = NULL_TREE;
1158 tree old_value;
1160 binfo = binfo_or_else (ctype, t);
1161 if (! binfo)
1162 return;
1164 if (constructor_name_p (name, ctype))
1166 cp_error_at ("`%D' names constructor", using_decl);
1167 return;
1169 if (constructor_name_p (name, t))
1171 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1172 return;
1175 fdecl = lookup_member (binfo, name, 0, 0);
1177 if (!fdecl)
1179 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1180 return;
1183 if (BASELINK_P (fdecl))
1184 /* Ignore base type this came from. */
1185 fdecl = BASELINK_FUNCTIONS (fdecl);
1187 old_value = IDENTIFIER_CLASS_VALUE (name);
1188 if (old_value)
1190 if (is_overloaded_fn (old_value))
1191 old_value = OVL_CURRENT (old_value);
1193 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1194 /* OK */;
1195 else
1196 old_value = NULL_TREE;
1199 if (is_overloaded_fn (fdecl))
1200 flist = fdecl;
1202 if (! old_value)
1204 else if (is_overloaded_fn (old_value))
1206 if (flist)
1207 /* It's OK to use functions from a base when there are functions with
1208 the same name already present in the current class. */;
1209 else
1211 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1212 cp_error_at (" because of local method `%#D' with same name",
1213 OVL_CURRENT (old_value));
1214 return;
1217 else if (!DECL_ARTIFICIAL (old_value))
1219 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1220 cp_error_at (" because of local member `%#D' with same name", old_value);
1221 return;
1224 /* Make type T see field decl FDECL with access ACCESS.*/
1225 if (flist)
1226 for (; flist; flist = OVL_NEXT (flist))
1228 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1229 alter_access (t, OVL_CURRENT (flist), access);
1231 else
1232 alter_access (t, fdecl, access);
1235 /* Run through the base clases of T, updating
1236 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1237 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1238 the bases. */
1240 static void
1241 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1242 no_const_asn_ref_p)
1243 tree t;
1244 int *cant_have_default_ctor_p;
1245 int *cant_have_const_ctor_p;
1246 int *no_const_asn_ref_p;
1248 int n_baseclasses;
1249 int i;
1250 int seen_non_virtual_nearly_empty_base_p;
1251 tree binfos;
1253 binfos = TYPE_BINFO_BASETYPES (t);
1254 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1255 seen_non_virtual_nearly_empty_base_p = 0;
1257 /* An aggregate cannot have baseclasses. */
1258 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1260 for (i = 0; i < n_baseclasses; ++i)
1262 tree base_binfo;
1263 tree basetype;
1265 /* Figure out what base we're looking at. */
1266 base_binfo = TREE_VEC_ELT (binfos, i);
1267 basetype = TREE_TYPE (base_binfo);
1269 /* If the type of basetype is incomplete, then we already
1270 complained about that fact (and we should have fixed it up as
1271 well). */
1272 if (!COMPLETE_TYPE_P (basetype))
1274 int j;
1275 /* The base type is of incomplete type. It is
1276 probably best to pretend that it does not
1277 exist. */
1278 if (i == n_baseclasses-1)
1279 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1280 TREE_VEC_LENGTH (binfos) -= 1;
1281 n_baseclasses -= 1;
1282 for (j = i; j+1 < n_baseclasses; j++)
1283 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1284 continue;
1287 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1288 here because the case of virtual functions but non-virtual
1289 dtor is handled in finish_struct_1. */
1290 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1291 && TYPE_HAS_DESTRUCTOR (basetype))
1292 warning ("base class `%#T' has a non-virtual destructor",
1293 basetype);
1295 /* If the base class doesn't have copy constructors or
1296 assignment operators that take const references, then the
1297 derived class cannot have such a member automatically
1298 generated. */
1299 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1300 *cant_have_const_ctor_p = 1;
1301 if (TYPE_HAS_ASSIGN_REF (basetype)
1302 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1303 *no_const_asn_ref_p = 1;
1304 /* Similarly, if the base class doesn't have a default
1305 constructor, then the derived class won't have an
1306 automatically generated default constructor. */
1307 if (TYPE_HAS_CONSTRUCTOR (basetype)
1308 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1310 *cant_have_default_ctor_p = 1;
1311 if (! TYPE_HAS_CONSTRUCTOR (t))
1312 pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1313 basetype);
1316 if (TREE_VIA_VIRTUAL (base_binfo))
1317 /* A virtual base does not effect nearly emptiness. */
1319 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1321 if (seen_non_virtual_nearly_empty_base_p)
1322 /* And if there is more than one nearly empty base, then the
1323 derived class is not nearly empty either. */
1324 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1325 else
1326 /* Remember we've seen one. */
1327 seen_non_virtual_nearly_empty_base_p = 1;
1329 else if (!is_empty_class (basetype))
1330 /* If the base class is not empty or nearly empty, then this
1331 class cannot be nearly empty. */
1332 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1334 /* A lot of properties from the bases also apply to the derived
1335 class. */
1336 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1337 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1338 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1339 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1340 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1341 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1342 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1343 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1344 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1345 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1349 /* Binfo FROM is within a virtual hierarchy which is being reseated to
1350 TO. Move primary information from FROM to TO, and recursively traverse
1351 into FROM's bases. The hierarchy is dominated by TYPE. MAPPINGS is an
1352 assoc list of binfos that have already been reseated. */
1354 static void
1355 force_canonical_binfo_r (to, from, type, mappings)
1356 tree to;
1357 tree from;
1358 tree type;
1359 tree mappings;
1361 int i, n_baseclasses = BINFO_N_BASETYPES (from);
1363 my_friendly_assert (to != from, 20010905);
1364 BINFO_INDIRECT_PRIMARY_P (to)
1365 = BINFO_INDIRECT_PRIMARY_P (from);
1366 BINFO_INDIRECT_PRIMARY_P (from) = 0;
1367 BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1368 BINFO_UNSHARED_MARKED (from) = 0;
1369 BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1370 BINFO_LOST_PRIMARY_P (from) = 0;
1371 if (BINFO_PRIMARY_P (from))
1373 tree primary = BINFO_PRIMARY_BASE_OF (from);
1374 tree assoc;
1376 /* We might have just moved the primary base too, see if it's on our
1377 mappings. */
1378 assoc = purpose_member (primary, mappings);
1379 if (assoc)
1380 primary = TREE_VALUE (assoc);
1381 BINFO_PRIMARY_BASE_OF (to) = primary;
1382 BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1384 my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1385 20010104);
1386 mappings = tree_cons (from, to, mappings);
1388 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
1389 && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
1391 tree from_primary = get_primary_binfo (from);
1393 if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
1394 force_canonical_binfo (get_primary_binfo (to), from_primary,
1395 type, mappings);
1398 for (i = 0; i != n_baseclasses; i++)
1400 tree from_binfo = BINFO_BASETYPE (from, i);
1401 tree to_binfo = BINFO_BASETYPE (to, i);
1403 if (TREE_VIA_VIRTUAL (from_binfo))
1405 if (BINFO_PRIMARY_P (from_binfo) &&
1406 purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1407 /* This base is a primary of some binfo we have already
1408 reseated. We must reseat this one too. */
1409 force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1411 else
1412 force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1416 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1417 make TO the canonical binfo, within the hierarchy dominated by TYPE.
1418 MAPPINGS is an assoc list of binfos that have already been reseated.
1419 Adjust any non-virtual bases within FROM, and also move any virtual bases
1420 which are canonical. This complication arises because selecting primary
1421 bases walks in inheritance graph order, but we don't share binfos for
1422 virtual bases, hence we can fill in the primaries for a virtual base,
1423 and then discover that a later base requires the virtual as its
1424 primary. */
1426 static void
1427 force_canonical_binfo (to, from, type, mappings)
1428 tree to;
1429 tree from;
1430 tree type;
1431 tree mappings;
1433 tree assoc = purpose_member (BINFO_TYPE (to),
1434 CLASSTYPE_VBASECLASSES (type));
1435 if (TREE_VALUE (assoc) != to)
1437 TREE_VALUE (assoc) = to;
1438 force_canonical_binfo_r (to, from, type, mappings);
1442 /* Make BASE_BINFO the a primary virtual base within the hierarchy
1443 dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
1444 otherwise (because something else has already made it primary). */
1446 static tree
1447 mark_primary_virtual_base (base_binfo, type)
1448 tree base_binfo;
1449 tree type;
1451 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1453 if (BINFO_PRIMARY_P (shared_binfo))
1455 /* It's already allocated in the hierarchy. BINFO won't have a
1456 primary base in this hierarchy, even though the complete object
1457 BINFO is for, would do. */
1458 return NULL_TREE;
1461 /* We need to make sure that the assoc list
1462 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1463 primary BINFO for the virtual base, as this is the one
1464 that'll really exist. */
1465 if (base_binfo != shared_binfo)
1466 force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1468 return base_binfo;
1471 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1472 base, then BINFO has no primary base in this graph. Called from
1473 mark_primary_bases. DATA is the most derived type. */
1475 static tree dfs_unshared_virtual_bases (binfo, data)
1476 tree binfo;
1477 void *data;
1479 tree t = (tree) data;
1481 if (!BINFO_UNSHARED_MARKED (binfo)
1482 && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1484 /* This morally virtual base has a primary base when it
1485 is a complete object. We need to locate the shared instance
1486 of this binfo in the type dominated by T. We duplicate the
1487 primary base information from there to here. */
1488 tree vbase;
1489 tree unshared_base;
1491 for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1492 vbase = BINFO_INHERITANCE_CHAIN (vbase))
1493 continue;
1494 unshared_base = get_original_base (binfo,
1495 binfo_for_vbase (BINFO_TYPE (vbase),
1496 t));
1497 my_friendly_assert (unshared_base != binfo, 20010612);
1498 BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1499 if (!BINFO_LOST_PRIMARY_P (binfo))
1500 BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1503 if (binfo != TYPE_BINFO (t))
1504 /* The vtable fields will have been copied when duplicating the
1505 base binfos. That information is bogus, make sure we don't try
1506 and use it. */
1507 BINFO_VTABLE (binfo) = NULL_TREE;
1509 /* If this is a virtual primary base, make sure its offset matches
1510 that which it is primary for. */
1511 if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
1512 binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
1514 tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
1515 BINFO_OFFSET (binfo));
1516 if (!integer_zerop (delta))
1517 propagate_binfo_offsets (binfo, delta, t);
1520 BINFO_UNSHARED_MARKED (binfo) = 0;
1521 return NULL;
1524 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1525 dominated by TYPE that are primary bases. */
1527 static void
1528 mark_primary_bases (type)
1529 tree type;
1531 tree binfo;
1533 /* Walk the bases in inheritance graph order. */
1534 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1536 tree base_binfo;
1538 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1539 /* Not a dynamic base. */
1540 continue;
1542 base_binfo = get_primary_binfo (binfo);
1544 if (TREE_VIA_VIRTUAL (base_binfo))
1545 base_binfo = mark_primary_virtual_base (base_binfo, type);
1547 if (base_binfo)
1548 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1549 else
1550 BINFO_LOST_PRIMARY_P (binfo) = 1;
1552 BINFO_UNSHARED_MARKED (binfo) = 1;
1554 /* There could remain unshared morally virtual bases which were not
1555 visited in the inheritance graph walk. These bases will have lost
1556 their virtual primary base (should they have one). We must now
1557 find them. Also we must fix up the BINFO_OFFSETs of primary
1558 virtual bases. We could not do that as we went along, as they
1559 were originally copied from the bases we inherited from by
1560 unshare_base_binfos. That may have decided differently about
1561 where a virtual primary base went. */
1562 dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1565 /* Make the BINFO the primary base of T. */
1567 static void
1568 set_primary_base (t, binfo, vfuns_p)
1569 tree t;
1570 tree binfo;
1571 int *vfuns_p;
1573 tree basetype;
1575 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1576 basetype = BINFO_TYPE (binfo);
1577 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1578 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1579 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1580 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1581 *vfuns_p = CLASSTYPE_VSIZE (basetype);
1584 /* Determine the primary class for T. */
1586 static void
1587 determine_primary_base (t, vfuns_p)
1588 tree t;
1589 int *vfuns_p;
1591 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1592 tree vbases;
1593 tree type_binfo;
1595 /* If there are no baseclasses, there is certainly no primary base. */
1596 if (n_baseclasses == 0)
1597 return;
1599 type_binfo = TYPE_BINFO (t);
1601 for (i = 0; i < n_baseclasses; i++)
1603 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1604 tree basetype = BINFO_TYPE (base_binfo);
1606 if (TYPE_CONTAINS_VPTR_P (basetype))
1608 /* Even a virtual baseclass can contain our RTTI
1609 information. But, we prefer a non-virtual polymorphic
1610 baseclass. */
1611 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1612 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1614 /* We prefer a non-virtual base, although a virtual one will
1615 do. */
1616 if (TREE_VIA_VIRTUAL (base_binfo))
1617 continue;
1619 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1621 set_primary_base (t, base_binfo, vfuns_p);
1622 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1624 else
1626 tree vfields;
1628 /* Only add unique vfields, and flatten them out as we go. */
1629 for (vfields = CLASSTYPE_VFIELDS (basetype);
1630 vfields;
1631 vfields = TREE_CHAIN (vfields))
1632 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1633 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1634 CLASSTYPE_VFIELDS (t)
1635 = tree_cons (base_binfo,
1636 VF_BASETYPE_VALUE (vfields),
1637 CLASSTYPE_VFIELDS (t));
1642 if (!TYPE_VFIELD (t))
1643 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1645 /* Find the indirect primary bases - those virtual bases which are primary
1646 bases of something else in this hierarchy. */
1647 for (vbases = CLASSTYPE_VBASECLASSES (t);
1648 vbases;
1649 vbases = TREE_CHAIN (vbases))
1651 tree vbase_binfo = TREE_VALUE (vbases);
1653 /* See if this virtual base is an indirect primary base. To be so,
1654 it must be a primary base within the hierarchy of one of our
1655 direct bases. */
1656 for (i = 0; i < n_baseclasses; ++i)
1658 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1659 tree v;
1661 for (v = CLASSTYPE_VBASECLASSES (basetype);
1663 v = TREE_CHAIN (v))
1665 tree base_vbase = TREE_VALUE (v);
1667 if (BINFO_PRIMARY_P (base_vbase)
1668 && same_type_p (BINFO_TYPE (base_vbase),
1669 BINFO_TYPE (vbase_binfo)))
1671 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1672 break;
1676 /* If we've discovered that this virtual base is an indirect
1677 primary base, then we can move on to the next virtual
1678 base. */
1679 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1680 break;
1684 /* A "nearly-empty" virtual base class can be the primary base
1685 class, if no non-virtual polymorphic base can be found. */
1686 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1688 /* If not NULL, this is the best primary base candidate we have
1689 found so far. */
1690 tree candidate = NULL_TREE;
1691 tree base_binfo;
1693 /* Loop over the baseclasses. */
1694 for (base_binfo = TYPE_BINFO (t);
1695 base_binfo;
1696 base_binfo = TREE_CHAIN (base_binfo))
1698 tree basetype = BINFO_TYPE (base_binfo);
1700 if (TREE_VIA_VIRTUAL (base_binfo)
1701 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1703 /* If this is not an indirect primary base, then it's
1704 definitely our primary base. */
1705 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1707 candidate = base_binfo;
1708 break;
1711 /* If this is an indirect primary base, it still could be
1712 our primary base -- unless we later find there's another
1713 nearly-empty virtual base that isn't an indirect
1714 primary base. */
1715 if (!candidate)
1716 candidate = base_binfo;
1720 /* If we've got a primary base, use it. */
1721 if (candidate)
1723 set_primary_base (t, candidate, vfuns_p);
1724 CLASSTYPE_VFIELDS (t)
1725 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1729 /* Mark the primary base classes at this point. */
1730 mark_primary_bases (t);
1733 /* Set memoizing fields and bits of T (and its variants) for later
1734 use. */
1736 static void
1737 finish_struct_bits (t)
1738 tree t;
1740 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1742 /* Fix up variants (if any). */
1743 tree variants = TYPE_NEXT_VARIANT (t);
1744 while (variants)
1746 /* These fields are in the _TYPE part of the node, not in
1747 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1748 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1749 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1750 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1751 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1752 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1754 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1755 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1756 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1757 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1758 /* Copy whatever these are holding today. */
1759 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1760 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1761 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1762 TYPE_SIZE (variants) = TYPE_SIZE (t);
1763 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1764 variants = TYPE_NEXT_VARIANT (variants);
1767 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1768 /* For a class w/o baseclasses, `finish_struct' has set
1769 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1770 definition). Similarly for a class whose base classes do not
1771 have vtables. When neither of these is true, we might have
1772 removed abstract virtuals (by providing a definition), added
1773 some (by declaring new ones), or redeclared ones from a base
1774 class. We need to recalculate what's really an abstract virtual
1775 at this point (by looking in the vtables). */
1776 get_pure_virtuals (t);
1778 if (n_baseclasses)
1780 /* Notice whether this class has type conversion functions defined. */
1781 tree binfo = TYPE_BINFO (t);
1782 tree binfos = BINFO_BASETYPES (binfo);
1783 tree basetype;
1785 for (i = n_baseclasses-1; i >= 0; i--)
1787 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1789 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1793 /* If this type has a copy constructor or a destructor, force its mode to
1794 be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero. This
1795 will cause it to be passed by invisible reference and prevent it from
1796 being returned in a register. */
1797 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1799 tree variants;
1800 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1801 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1803 TYPE_MODE (variants) = BLKmode;
1804 TREE_ADDRESSABLE (variants) = 1;
1809 /* Issue warnings about T having private constructors, but no friends,
1810 and so forth.
1812 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1813 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1814 non-private static member functions. */
1816 static void
1817 maybe_warn_about_overly_private_class (t)
1818 tree t;
1820 int has_member_fn = 0;
1821 int has_nonprivate_method = 0;
1822 tree fn;
1824 if (!warn_ctor_dtor_privacy
1825 /* If the class has friends, those entities might create and
1826 access instances, so we should not warn. */
1827 || (CLASSTYPE_FRIEND_CLASSES (t)
1828 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1829 /* We will have warned when the template was declared; there's
1830 no need to warn on every instantiation. */
1831 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1832 /* There's no reason to even consider warning about this
1833 class. */
1834 return;
1836 /* We only issue one warning, if more than one applies, because
1837 otherwise, on code like:
1839 class A {
1840 // Oops - forgot `public:'
1841 A();
1842 A(const A&);
1843 ~A();
1846 we warn several times about essentially the same problem. */
1848 /* Check to see if all (non-constructor, non-destructor) member
1849 functions are private. (Since there are no friends or
1850 non-private statics, we can't ever call any of the private member
1851 functions.) */
1852 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1853 /* We're not interested in compiler-generated methods; they don't
1854 provide any way to call private members. */
1855 if (!DECL_ARTIFICIAL (fn))
1857 if (!TREE_PRIVATE (fn))
1859 if (DECL_STATIC_FUNCTION_P (fn))
1860 /* A non-private static member function is just like a
1861 friend; it can create and invoke private member
1862 functions, and be accessed without a class
1863 instance. */
1864 return;
1866 has_nonprivate_method = 1;
1867 break;
1869 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1870 has_member_fn = 1;
1873 if (!has_nonprivate_method && has_member_fn)
1875 /* There are no non-private methods, and there's at least one
1876 private member function that isn't a constructor or
1877 destructor. (If all the private members are
1878 constructors/destructors we want to use the code below that
1879 issues error messages specifically referring to
1880 constructors/destructors.) */
1881 int i;
1882 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1883 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1884 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1885 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1887 has_nonprivate_method = 1;
1888 break;
1890 if (!has_nonprivate_method)
1892 warning ("all member functions in class `%T' are private", t);
1893 return;
1897 /* Even if some of the member functions are non-private, the class
1898 won't be useful for much if all the constructors or destructors
1899 are private: such an object can never be created or destroyed. */
1900 if (TYPE_HAS_DESTRUCTOR (t))
1902 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1904 if (TREE_PRIVATE (dtor))
1906 warning ("`%#T' only defines a private destructor and has no friends",
1908 return;
1912 if (TYPE_HAS_CONSTRUCTOR (t))
1914 int nonprivate_ctor = 0;
1916 /* If a non-template class does not define a copy
1917 constructor, one is defined for it, enabling it to avoid
1918 this warning. For a template class, this does not
1919 happen, and so we would normally get a warning on:
1921 template <class T> class C { private: C(); };
1923 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1924 complete non-template or fully instantiated classes have this
1925 flag set. */
1926 if (!TYPE_HAS_INIT_REF (t))
1927 nonprivate_ctor = 1;
1928 else
1929 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1931 fn = OVL_NEXT (fn))
1933 tree ctor = OVL_CURRENT (fn);
1934 /* Ideally, we wouldn't count copy constructors (or, in
1935 fact, any constructor that takes an argument of the
1936 class type as a parameter) because such things cannot
1937 be used to construct an instance of the class unless
1938 you already have one. But, for now at least, we're
1939 more generous. */
1940 if (! TREE_PRIVATE (ctor))
1942 nonprivate_ctor = 1;
1943 break;
1947 if (nonprivate_ctor == 0)
1949 warning ("`%#T' only defines private constructors and has no friends",
1951 return;
1956 /* Function to help qsort sort FIELD_DECLs by name order. */
1958 static int
1959 field_decl_cmp (x, y)
1960 const tree *x, *y;
1962 if (DECL_NAME (*x) == DECL_NAME (*y))
1963 /* A nontype is "greater" than a type. */
1964 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1965 if (DECL_NAME (*x) == NULL_TREE)
1966 return -1;
1967 if (DECL_NAME (*y) == NULL_TREE)
1968 return 1;
1969 if (DECL_NAME (*x) < DECL_NAME (*y))
1970 return -1;
1971 return 1;
1974 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1976 static int
1977 method_name_cmp (m1, m2)
1978 const tree *m1, *m2;
1980 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1981 return 0;
1982 if (*m1 == NULL_TREE)
1983 return -1;
1984 if (*m2 == NULL_TREE)
1985 return 1;
1986 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1987 return -1;
1988 return 1;
1991 /* Warn about duplicate methods in fn_fields. Also compact method
1992 lists so that lookup can be made faster.
1994 Data Structure: List of method lists. The outer list is a
1995 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1996 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1997 links the entire list of methods for TYPE_METHODS. Friends are
1998 chained in the same way as member functions (? TREE_CHAIN or
1999 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2000 list. That allows them to be quickly deleted, and requires no
2001 extra storage.
2003 Sort methods that are not special (i.e., constructors, destructors,
2004 and type conversion operators) so that we can find them faster in
2005 search. */
2007 static void
2008 finish_struct_methods (t)
2009 tree t;
2011 tree fn_fields;
2012 tree method_vec;
2013 int slot, len;
2015 if (!TYPE_METHODS (t))
2017 /* Clear these for safety; perhaps some parsing error could set
2018 these incorrectly. */
2019 TYPE_HAS_CONSTRUCTOR (t) = 0;
2020 TYPE_HAS_DESTRUCTOR (t) = 0;
2021 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2022 return;
2025 method_vec = CLASSTYPE_METHOD_VEC (t);
2026 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2027 len = TREE_VEC_LENGTH (method_vec);
2029 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2030 and the next few with type conversion operators (if any). */
2031 for (fn_fields = TYPE_METHODS (t); fn_fields;
2032 fn_fields = TREE_CHAIN (fn_fields))
2033 /* Clear out this flag. */
2034 DECL_IN_AGGR_P (fn_fields) = 0;
2036 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2037 /* We thought there was a destructor, but there wasn't. Some
2038 parse errors cause this anomalous situation. */
2039 TYPE_HAS_DESTRUCTOR (t) = 0;
2041 /* Issue warnings about private constructors and such. If there are
2042 no methods, then some public defaults are generated. */
2043 maybe_warn_about_overly_private_class (t);
2045 /* Now sort the methods. */
2046 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2047 len--;
2048 TREE_VEC_LENGTH (method_vec) = len;
2050 /* The type conversion ops have to live at the front of the vec, so we
2051 can't sort them. */
2052 for (slot = 2; slot < len; ++slot)
2054 tree fn = TREE_VEC_ELT (method_vec, slot);
2056 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2057 break;
2059 if (len - slot > 1)
2060 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2061 (int (*)(const void *, const void *))method_name_cmp);
2064 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2066 void
2067 duplicate_tag_error (t)
2068 tree t;
2070 error ("redefinition of `%#T'", t);
2071 cp_error_at ("previous definition of `%#T'", t);
2073 /* Pretend we haven't defined this type. */
2075 /* All of the component_decl's were TREE_CHAINed together in the parser.
2076 finish_struct_methods walks these chains and assembles all methods with
2077 the same base name into DECL_CHAINs. Now we don't need the parser chains
2078 anymore, so we unravel them. */
2080 /* This used to be in finish_struct, but it turns out that the
2081 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2082 things... */
2083 if (CLASSTYPE_METHOD_VEC (t))
2085 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2086 int i, len = TREE_VEC_LENGTH (method_vec);
2087 for (i = 0; i < len; i++)
2089 tree unchain = TREE_VEC_ELT (method_vec, i);
2090 while (unchain != NULL_TREE)
2092 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2093 unchain = OVL_NEXT (unchain);
2098 if (TYPE_LANG_SPECIFIC (t))
2100 tree binfo = TYPE_BINFO (t);
2101 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2102 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2103 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2104 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2106 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2107 BINFO_BASETYPES(binfo) = NULL_TREE;
2109 TYPE_LANG_SPECIFIC (t)->u.h.is_lang_type_class = 1;
2110 TYPE_BINFO (t) = binfo;
2111 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2112 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2113 TYPE_REDEFINED (t) = 1;
2114 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2115 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2117 TYPE_SIZE (t) = NULL_TREE;
2118 TYPE_MODE (t) = VOIDmode;
2119 TYPE_FIELDS (t) = NULL_TREE;
2120 TYPE_METHODS (t) = NULL_TREE;
2121 TYPE_VFIELD (t) = NULL_TREE;
2122 TYPE_CONTEXT (t) = NULL_TREE;
2124 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2125 TYPE_LANG_FLAG_0 (t) = 0;
2126 TYPE_LANG_FLAG_1 (t) = 0;
2127 TYPE_LANG_FLAG_2 (t) = 0;
2128 TYPE_LANG_FLAG_3 (t) = 0;
2129 TYPE_LANG_FLAG_4 (t) = 0;
2130 TYPE_LANG_FLAG_5 (t) = 0;
2131 TYPE_LANG_FLAG_6 (t) = 0;
2132 /* But not this one. */
2133 SET_IS_AGGR_TYPE (t, 1);
2136 /* Make BINFO's vtable have N entries, including RTTI entries,
2137 vbase and vcall offsets, etc. Set its type and call the backend
2138 to lay it out. */
2140 static void
2141 layout_vtable_decl (binfo, n)
2142 tree binfo;
2143 int n;
2145 tree atype;
2146 tree vtable;
2148 atype = build_cplus_array_type (vtable_entry_type,
2149 build_index_type (size_int (n - 1)));
2150 layout_type (atype);
2152 /* We may have to grow the vtable. */
2153 vtable = get_vtbl_decl_for_binfo (binfo);
2154 if (!same_type_p (TREE_TYPE (vtable), atype))
2156 TREE_TYPE (vtable) = atype;
2157 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2158 layout_decl (vtable, 0);
2160 /* At one time the vtable info was grabbed 2 words at a time. This
2161 fails on Sparc unless you have 8-byte alignment. */
2162 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2163 DECL_ALIGN (vtable));
2167 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2168 have the same signature. */
2171 same_signature_p (fndecl, base_fndecl)
2172 tree fndecl, base_fndecl;
2174 /* One destructor overrides another if they are the same kind of
2175 destructor. */
2176 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2177 && special_function_p (base_fndecl) == special_function_p (fndecl))
2178 return 1;
2179 /* But a non-destructor never overrides a destructor, nor vice
2180 versa, nor do different kinds of destructors override
2181 one-another. For example, a complete object destructor does not
2182 override a deleting destructor. */
2183 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2184 return 0;
2186 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2188 tree types, base_types;
2189 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2190 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2191 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2192 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2193 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2194 return 1;
2196 return 0;
2199 typedef struct find_final_overrider_data_s {
2200 /* The function for which we are trying to find a final overrider. */
2201 tree fn;
2202 /* The base class in which the function was declared. */
2203 tree declaring_base;
2204 /* The most derived class in the hierarchy. */
2205 tree most_derived_type;
2206 /* The final overriding function. */
2207 tree overriding_fn;
2208 /* The functions that we thought might be final overriders, but
2209 aren't. */
2210 tree candidates;
2211 /* The BINFO for the class in which the final overriding function
2212 appears. */
2213 tree overriding_base;
2214 } find_final_overrider_data;
2216 /* Called from find_final_overrider via dfs_walk. */
2218 static tree
2219 dfs_find_final_overrider (binfo, data)
2220 tree binfo;
2221 void *data;
2223 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2225 if (same_type_p (BINFO_TYPE (binfo),
2226 BINFO_TYPE (ffod->declaring_base))
2227 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2228 BINFO_OFFSET (ffod->declaring_base)))
2230 tree path;
2231 tree method;
2233 /* We haven't found an overrider yet. */
2234 method = NULL_TREE;
2235 /* We've found a path to the declaring base. Walk down the path
2236 looking for an overrider for FN. */
2237 for (path = reverse_path (binfo);
2238 path;
2239 path = TREE_CHAIN (path))
2241 method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2242 ffod->fn);
2243 if (method)
2244 break;
2247 /* If we found an overrider, record the overriding function, and
2248 the base from which it came. */
2249 if (path)
2251 tree base;
2253 /* Assume the path is non-virtual. See if there are any
2254 virtual bases from (but not including) the overrider up
2255 to and including the base where the function is
2256 defined. */
2257 for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
2258 if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
2260 base = ffod->declaring_base;
2261 break;
2264 /* If we didn't already have an overrider, or any
2265 candidates, then this function is the best candidate so
2266 far. */
2267 if (!ffod->overriding_fn && !ffod->candidates)
2269 ffod->overriding_fn = method;
2270 ffod->overriding_base = TREE_VALUE (path);
2272 else if (ffod->overriding_fn)
2274 /* We had a best overrider; let's see how this compares. */
2276 if (ffod->overriding_fn == method
2277 && (tree_int_cst_equal
2278 (BINFO_OFFSET (TREE_VALUE (path)),
2279 BINFO_OFFSET (ffod->overriding_base))))
2280 /* We found the same overrider we already have, and in the
2281 same place; it's still the best. */;
2282 else if (strictly_overrides (ffod->overriding_fn, method))
2283 /* The old function overrides this function; it's still the
2284 best. */;
2285 else if (strictly_overrides (method, ffod->overriding_fn))
2287 /* The new function overrides the old; it's now the
2288 best. */
2289 ffod->overriding_fn = method;
2290 ffod->overriding_base = TREE_VALUE (path);
2292 else
2294 /* Ambiguous. */
2295 ffod->candidates
2296 = build_tree_list (NULL_TREE,
2297 ffod->overriding_fn);
2298 if (method != ffod->overriding_fn)
2299 ffod->candidates
2300 = tree_cons (NULL_TREE, method, ffod->candidates);
2301 ffod->overriding_fn = NULL_TREE;
2302 ffod->overriding_base = NULL_TREE;
2305 else
2307 /* We had a list of ambiguous overrides; let's see how this
2308 new one compares. */
2310 tree candidates;
2311 bool incomparable = false;
2313 /* If there were previous candidates, and this function
2314 overrides all of them, then it is the new best
2315 candidate. */
2316 for (candidates = ffod->candidates;
2317 candidates;
2318 candidates = TREE_CHAIN (candidates))
2320 /* If the candidate overrides the METHOD, then we
2321 needn't worry about it any further. */
2322 if (strictly_overrides (TREE_VALUE (candidates),
2323 method))
2325 method = NULL_TREE;
2326 break;
2329 /* If the METHOD doesn't override the candidate,
2330 then it is incomporable. */
2331 if (!strictly_overrides (method,
2332 TREE_VALUE (candidates)))
2333 incomparable = true;
2336 /* If METHOD overrode all the candidates, then it is the
2337 new best candidate. */
2338 if (!candidates && !incomparable)
2340 ffod->overriding_fn = method;
2341 ffod->overriding_base = TREE_VALUE (path);
2342 ffod->candidates = NULL_TREE;
2344 /* If METHOD didn't override all the candidates, then it
2345 is another candidate. */
2346 else if (method && incomparable)
2347 ffod->candidates
2348 = tree_cons (NULL_TREE, method, ffod->candidates);
2353 return NULL_TREE;
2356 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2357 FN and whose TREE_VALUE is the binfo for the base where the
2358 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2359 base object in which FN is declared. */
2361 static tree
2362 find_final_overrider (t, binfo, fn)
2363 tree t;
2364 tree binfo;
2365 tree fn;
2367 find_final_overrider_data ffod;
2369 /* Getting this right is a little tricky. This is valid:
2371 struct S { virtual void f (); };
2372 struct T { virtual void f (); };
2373 struct U : public S, public T { };
2375 even though calling `f' in `U' is ambiguous. But,
2377 struct R { virtual void f(); };
2378 struct S : virtual public R { virtual void f (); };
2379 struct T : virtual public R { virtual void f (); };
2380 struct U : public S, public T { };
2382 is not -- there's no way to decide whether to put `S::f' or
2383 `T::f' in the vtable for `R'.
2385 The solution is to look at all paths to BINFO. If we find
2386 different overriders along any two, then there is a problem. */
2387 ffod.fn = fn;
2388 ffod.declaring_base = binfo;
2389 ffod.most_derived_type = t;
2390 ffod.overriding_fn = NULL_TREE;
2391 ffod.overriding_base = NULL_TREE;
2392 ffod.candidates = NULL_TREE;
2394 dfs_walk (TYPE_BINFO (t),
2395 dfs_find_final_overrider,
2396 NULL,
2397 &ffod);
2399 /* If there was no winner, issue an error message. */
2400 if (!ffod.overriding_fn)
2402 error ("no unique final overrider for `%D' in `%T'", fn, t);
2403 return error_mark_node;
2406 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2409 /* Returns the function from the BINFO_VIRTUALS entry in T which matches
2410 the signature of FUNCTION_DECL FN, or NULL_TREE if none. In other words,
2411 the function that the slot in T's primary vtable points to. */
2413 static tree get_matching_virtual PARAMS ((tree, tree));
2414 static tree
2415 get_matching_virtual (t, fn)
2416 tree t, fn;
2418 tree f;
2420 for (f = BINFO_VIRTUALS (TYPE_BINFO (t)); f; f = TREE_CHAIN (f))
2421 if (same_signature_p (BV_FN (f), fn))
2422 return BV_FN (f);
2423 return NULL_TREE;
2426 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2427 dominated by T. FN has been overriden in BINFO; VIRTUALS points to the
2428 corresponding position in the BINFO_VIRTUALS list. */
2430 static void
2431 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2432 tree t;
2433 tree binfo;
2434 tree fn;
2435 tree *virtuals;
2437 tree b;
2438 tree overrider;
2439 tree delta;
2440 tree virtual_base;
2441 tree first_defn;
2442 bool lost = false;
2444 /* Find the nearest primary base (possibly binfo itself) which defines
2445 this function; this is the class the caller will convert to when
2446 calling FN through BINFO. */
2447 for (b = binfo; ; b = get_primary_binfo (b))
2449 if (look_for_overrides_here (BINFO_TYPE (b), fn))
2450 break;
2452 /* The nearest definition is from a lost primary. */
2453 if (BINFO_LOST_PRIMARY_P (b))
2454 lost = true;
2456 first_defn = b;
2458 /* Find the final overrider. */
2459 overrider = find_final_overrider (t, b, fn);
2460 if (overrider == error_mark_node)
2461 return;
2463 /* Check for unsupported covariant returns again now that we've
2464 calculated the base offsets. */
2465 check_final_overrider (TREE_PURPOSE (overrider), fn);
2467 /* Assume that we will produce a thunk that convert all the way to
2468 the final overrider, and not to an intermediate virtual base. */
2469 virtual_base = NULL_TREE;
2471 /* See if we can convert to an intermediate virtual base first, and then
2472 use the vcall offset located there to finish the conversion. */
2473 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2475 /* If we find the final overrider, then we can stop
2476 walking. */
2477 if (same_type_p (BINFO_TYPE (b),
2478 BINFO_TYPE (TREE_VALUE (overrider))))
2479 break;
2481 /* If we find a virtual base, and we haven't yet found the
2482 overrider, then there is a virtual base between the
2483 declaring base (first_defn) and the final overrider. */
2484 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2485 virtual_base = b;
2488 /* Compute the constant adjustment to the `this' pointer. The
2489 `this' pointer, when this function is called, will point at BINFO
2490 (or one of its primary bases, which are at the same offset). */
2492 if (virtual_base)
2493 /* The `this' pointer needs to be adjusted from the declaration to
2494 the nearest virtual base. */
2495 delta = size_diffop (BINFO_OFFSET (virtual_base),
2496 BINFO_OFFSET (first_defn));
2497 else if (lost)
2498 /* If the nearest definition is in a lost primary, we don't need an
2499 entry in our vtable. Except possibly in a constructor vtable,
2500 if we happen to get our primary back. In that case, the offset
2501 will be zero, as it will be a primary base. */
2502 delta = size_zero_node;
2503 else
2505 /* The `this' pointer needs to be adjusted from pointing to
2506 BINFO to pointing at the base where the final overrider
2507 appears. */
2508 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2509 BINFO_OFFSET (binfo));
2511 if (! integer_zerop (delta))
2513 /* We'll need a thunk. But if we have a (perhaps formerly)
2514 primary virtual base, we have a vcall slot for this function,
2515 so we can use it rather than create a non-virtual thunk. */
2517 b = get_primary_binfo (first_defn);
2518 for (; b; b = get_primary_binfo (b))
2520 tree f = get_matching_virtual (BINFO_TYPE (b), fn);
2521 if (!f)
2522 /* b doesn't have this function; no suitable vbase. */
2523 break;
2524 if (TREE_VIA_VIRTUAL (b))
2526 /* Found one; we can treat ourselves as a virtual base. */
2527 virtual_base = binfo;
2528 delta = size_zero_node;
2529 break;
2535 modify_vtable_entry (t,
2536 binfo,
2537 TREE_PURPOSE (overrider),
2538 delta,
2539 virtuals);
2541 if (virtual_base)
2542 BV_USE_VCALL_INDEX_P (*virtuals) = 1;
2545 /* Called from modify_all_vtables via dfs_walk. */
2547 static tree
2548 dfs_modify_vtables (binfo, data)
2549 tree binfo;
2550 void *data;
2552 if (/* There's no need to modify the vtable for a non-virtual
2553 primary base; we're not going to use that vtable anyhow.
2554 We do still need to do this for virtual primary bases, as they
2555 could become non-primary in a construction vtable. */
2556 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2557 /* Similarly, a base without a vtable needs no modification. */
2558 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2560 tree t;
2561 tree virtuals;
2562 tree old_virtuals;
2564 t = (tree) data;
2566 make_new_vtable (t, binfo);
2568 /* Now, go through each of the virtual functions in the virtual
2569 function table for BINFO. Find the final overrider, and
2570 update the BINFO_VIRTUALS list appropriately. */
2571 for (virtuals = BINFO_VIRTUALS (binfo),
2572 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2573 virtuals;
2574 virtuals = TREE_CHAIN (virtuals),
2575 old_virtuals = TREE_CHAIN (old_virtuals))
2576 update_vtable_entry_for_fn (t,
2577 binfo,
2578 BV_FN (old_virtuals),
2579 &virtuals);
2582 SET_BINFO_MARKED (binfo);
2584 return NULL_TREE;
2587 /* Update all of the primary and secondary vtables for T. Create new
2588 vtables as required, and initialize their RTTI information. Each
2589 of the functions in VIRTUALS is declared in T and may override a
2590 virtual function from a base class; find and modify the appropriate
2591 entries to point to the overriding functions. Returns a list, in
2592 declaration order, of the virtual functions that are declared in T,
2593 but do not appear in the primary base class vtable, and which
2594 should therefore be appended to the end of the vtable for T. */
2596 static tree
2597 modify_all_vtables (t, vfuns_p, virtuals)
2598 tree t;
2599 int *vfuns_p;
2600 tree virtuals;
2602 tree binfo = TYPE_BINFO (t);
2603 tree *fnsp;
2605 /* Update all of the vtables. */
2606 dfs_walk (binfo,
2607 dfs_modify_vtables,
2608 dfs_unmarked_real_bases_queue_p,
2610 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2612 /* Add virtual functions not already in our primary vtable. These
2613 will be both those introduced by this class, and those overridden
2614 from secondary bases. It does not include virtuals merely
2615 inherited from secondary bases. */
2616 for (fnsp = &virtuals; *fnsp; )
2618 tree fn = TREE_VALUE (*fnsp);
2620 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2621 || DECL_VINDEX (fn) == error_mark_node)
2623 /* Set the vtable index. */
2624 set_vindex (fn, vfuns_p);
2625 /* We don't need to convert to a base class when calling
2626 this function. */
2627 DECL_VIRTUAL_CONTEXT (fn) = t;
2629 /* We don't need to adjust the `this' pointer when
2630 calling this function. */
2631 BV_DELTA (*fnsp) = integer_zero_node;
2632 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2634 /* This is a function not already in our vtable. Keep it. */
2635 fnsp = &TREE_CHAIN (*fnsp);
2637 else
2638 /* We've already got an entry for this function. Skip it. */
2639 *fnsp = TREE_CHAIN (*fnsp);
2642 return virtuals;
2645 /* Here, we already know that they match in every respect.
2646 All we have to check is where they had their declarations.
2648 Return non-zero iff FNDECL1 is declared in a class which has a
2649 proper base class containing FNDECL2. We don't care about
2650 ambiguity or accessibility. */
2652 static int
2653 strictly_overrides (fndecl1, fndecl2)
2654 tree fndecl1, fndecl2;
2656 base_kind kind;
2658 return (lookup_base (DECL_CONTEXT (fndecl1), DECL_CONTEXT (fndecl2),
2659 ba_ignore | ba_quiet, &kind)
2660 && kind != bk_same_type);
2663 /* Get the base virtual function declarations in T that have the
2664 indicated NAME. */
2666 static tree
2667 get_basefndecls (name, t)
2668 tree name, t;
2670 tree methods;
2671 tree base_fndecls = NULL_TREE;
2672 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2673 int i;
2675 for (methods = TYPE_METHODS (t); methods; methods = TREE_CHAIN (methods))
2676 if (TREE_CODE (methods) == FUNCTION_DECL
2677 && DECL_VINDEX (methods) != NULL_TREE
2678 && DECL_NAME (methods) == name)
2679 base_fndecls = tree_cons (NULL_TREE, methods, base_fndecls);
2681 if (base_fndecls)
2682 return base_fndecls;
2684 for (i = 0; i < n_baseclasses; i++)
2686 tree basetype = TYPE_BINFO_BASETYPE (t, i);
2687 base_fndecls = chainon (get_basefndecls (name, basetype),
2688 base_fndecls);
2691 return base_fndecls;
2694 /* If this declaration supersedes the declaration of
2695 a method declared virtual in the base class, then
2696 mark this field as being virtual as well. */
2698 static void
2699 check_for_override (decl, ctype)
2700 tree decl, ctype;
2702 if (TREE_CODE (decl) == TEMPLATE_DECL)
2703 /* In [temp.mem] we have:
2705 A specialization of a member function template does not
2706 override a virtual function from a base class. */
2707 return;
2708 if ((DECL_DESTRUCTOR_P (decl)
2709 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2710 && look_for_overrides (ctype, decl)
2711 && !DECL_STATIC_FUNCTION_P (decl))
2712 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2713 the error_mark_node so that we know it is an overriding
2714 function. */
2715 DECL_VINDEX (decl) = decl;
2717 if (DECL_VIRTUAL_P (decl))
2719 if (!DECL_VINDEX (decl))
2720 DECL_VINDEX (decl) = error_mark_node;
2721 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2725 /* Warn about hidden virtual functions that are not overridden in t.
2726 We know that constructors and destructors don't apply. */
2728 void
2729 warn_hidden (t)
2730 tree t;
2732 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2733 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2734 int i;
2736 /* We go through each separately named virtual function. */
2737 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2739 tree fns;
2740 tree name;
2741 tree fndecl;
2742 tree base_fndecls;
2743 int j;
2745 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2746 have the same name. Figure out what name that is. */
2747 name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2748 /* There are no possibly hidden functions yet. */
2749 base_fndecls = NULL_TREE;
2750 /* Iterate through all of the base classes looking for possibly
2751 hidden functions. */
2752 for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
2754 tree basetype = TYPE_BINFO_BASETYPE (t, j);
2755 base_fndecls = chainon (get_basefndecls (name, basetype),
2756 base_fndecls);
2759 /* If there are no functions to hide, continue. */
2760 if (!base_fndecls)
2761 continue;
2763 /* Remove any overridden functions. */
2764 for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
2766 fndecl = OVL_CURRENT (fns);
2767 if (DECL_VINDEX (fndecl))
2769 tree *prev = &base_fndecls;
2771 while (*prev)
2772 /* If the method from the base class has the same
2773 signature as the method from the derived class, it
2774 has been overridden. */
2775 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2776 *prev = TREE_CHAIN (*prev);
2777 else
2778 prev = &TREE_CHAIN (*prev);
2782 /* Now give a warning for all base functions without overriders,
2783 as they are hidden. */
2784 while (base_fndecls)
2786 /* Here we know it is a hider, and no overrider exists. */
2787 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2788 cp_warning_at (" by `%D'",
2789 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2790 base_fndecls = TREE_CHAIN (base_fndecls);
2795 /* Check for things that are invalid. There are probably plenty of other
2796 things we should check for also. */
2798 static void
2799 finish_struct_anon (t)
2800 tree t;
2802 tree field;
2804 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2806 if (TREE_STATIC (field))
2807 continue;
2808 if (TREE_CODE (field) != FIELD_DECL)
2809 continue;
2811 if (DECL_NAME (field) == NULL_TREE
2812 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2814 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2815 for (; elt; elt = TREE_CHAIN (elt))
2817 /* We're generally only interested in entities the user
2818 declared, but we also find nested classes by noticing
2819 the TYPE_DECL that we create implicitly. You're
2820 allowed to put one anonymous union inside another,
2821 though, so we explicitly tolerate that. We use
2822 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2823 we also allow unnamed types used for defining fields. */
2824 if (DECL_ARTIFICIAL (elt)
2825 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2826 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2827 continue;
2829 if (DECL_NAME (elt) == constructor_name (t))
2830 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2831 elt);
2833 if (TREE_CODE (elt) != FIELD_DECL)
2835 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2836 elt);
2837 continue;
2840 if (TREE_PRIVATE (elt))
2841 cp_pedwarn_at ("private member `%#D' in anonymous union",
2842 elt);
2843 else if (TREE_PROTECTED (elt))
2844 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2845 elt);
2847 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2848 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2854 /* Create default constructors, assignment operators, and so forth for
2855 the type indicated by T, if they are needed.
2856 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2857 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2858 class cannot have a default constructor, copy constructor taking a
2859 const reference argument, or an assignment operator taking a const
2860 reference, respectively. If a virtual destructor is created, its
2861 DECL is returned; otherwise the return value is NULL_TREE. */
2863 static tree
2864 add_implicitly_declared_members (t, cant_have_default_ctor,
2865 cant_have_const_cctor,
2866 cant_have_const_assignment)
2867 tree t;
2868 int cant_have_default_ctor;
2869 int cant_have_const_cctor;
2870 int cant_have_const_assignment;
2872 tree default_fn;
2873 tree implicit_fns = NULL_TREE;
2874 tree virtual_dtor = NULL_TREE;
2875 tree *f;
2877 ++adding_implicit_members;
2879 /* Destructor. */
2880 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2882 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2883 check_for_override (default_fn, t);
2885 /* If we couldn't make it work, then pretend we didn't need it. */
2886 if (default_fn == void_type_node)
2887 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2888 else
2890 TREE_CHAIN (default_fn) = implicit_fns;
2891 implicit_fns = default_fn;
2893 if (DECL_VINDEX (default_fn))
2894 virtual_dtor = default_fn;
2897 else
2898 /* Any non-implicit destructor is non-trivial. */
2899 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2901 /* Default constructor. */
2902 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2904 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2905 TREE_CHAIN (default_fn) = implicit_fns;
2906 implicit_fns = default_fn;
2909 /* Copy constructor. */
2910 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2912 /* ARM 12.18: You get either X(X&) or X(const X&), but
2913 not both. --Chip */
2914 default_fn
2915 = implicitly_declare_fn (sfk_copy_constructor, t,
2916 /*const_p=*/!cant_have_const_cctor);
2917 TREE_CHAIN (default_fn) = implicit_fns;
2918 implicit_fns = default_fn;
2921 /* Assignment operator. */
2922 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2924 default_fn
2925 = implicitly_declare_fn (sfk_assignment_operator, t,
2926 /*const_p=*/!cant_have_const_assignment);
2927 TREE_CHAIN (default_fn) = implicit_fns;
2928 implicit_fns = default_fn;
2931 /* Now, hook all of the new functions on to TYPE_METHODS,
2932 and add them to the CLASSTYPE_METHOD_VEC. */
2933 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2934 add_method (t, *f, /*error_p=*/0);
2935 *f = TYPE_METHODS (t);
2936 TYPE_METHODS (t) = implicit_fns;
2938 --adding_implicit_members;
2940 return virtual_dtor;
2943 /* Subroutine of finish_struct_1. Recursively count the number of fields
2944 in TYPE, including anonymous union members. */
2946 static int
2947 count_fields (fields)
2948 tree fields;
2950 tree x;
2951 int n_fields = 0;
2952 for (x = fields; x; x = TREE_CHAIN (x))
2954 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2955 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2956 else
2957 n_fields += 1;
2959 return n_fields;
2962 /* Subroutine of finish_struct_1. Recursively add all the fields in the
2963 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
2965 static int
2966 add_fields_to_vec (fields, field_vec, idx)
2967 tree fields, field_vec;
2968 int idx;
2970 tree x;
2971 for (x = fields; x; x = TREE_CHAIN (x))
2973 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2974 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2975 else
2976 TREE_VEC_ELT (field_vec, idx++) = x;
2978 return idx;
2981 /* FIELD is a bit-field. We are finishing the processing for its
2982 enclosing type. Issue any appropriate messages and set appropriate
2983 flags. */
2985 static void
2986 check_bitfield_decl (field)
2987 tree field;
2989 tree type = TREE_TYPE (field);
2990 tree w = NULL_TREE;
2992 /* Detect invalid bit-field type. */
2993 if (DECL_INITIAL (field)
2994 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
2996 cp_error_at ("bit-field `%#D' with non-integral type", field);
2997 w = error_mark_node;
3000 /* Detect and ignore out of range field width. */
3001 if (DECL_INITIAL (field))
3003 w = DECL_INITIAL (field);
3005 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3006 STRIP_NOPS (w);
3008 /* detect invalid field size. */
3009 if (TREE_CODE (w) == CONST_DECL)
3010 w = DECL_INITIAL (w);
3011 else
3012 w = decl_constant_value (w);
3014 if (TREE_CODE (w) != INTEGER_CST)
3016 cp_error_at ("bit-field `%D' width not an integer constant",
3017 field);
3018 w = error_mark_node;
3020 else if (tree_int_cst_sgn (w) < 0)
3022 cp_error_at ("negative width in bit-field `%D'", field);
3023 w = error_mark_node;
3025 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3027 cp_error_at ("zero width for bit-field `%D'", field);
3028 w = error_mark_node;
3030 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3031 && TREE_CODE (type) != ENUMERAL_TYPE
3032 && TREE_CODE (type) != BOOLEAN_TYPE)
3033 cp_warning_at ("width of `%D' exceeds its type", field);
3034 else if (TREE_CODE (type) == ENUMERAL_TYPE
3035 && (0 > compare_tree_int (w,
3036 min_precision (TYPE_MIN_VALUE (type),
3037 TREE_UNSIGNED (type)))
3038 || 0 > compare_tree_int (w,
3039 min_precision
3040 (TYPE_MAX_VALUE (type),
3041 TREE_UNSIGNED (type)))))
3042 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3043 field, type);
3046 /* Remove the bit-field width indicator so that the rest of the
3047 compiler does not treat that value as an initializer. */
3048 DECL_INITIAL (field) = NULL_TREE;
3050 if (w != error_mark_node)
3052 DECL_SIZE (field) = convert (bitsizetype, w);
3053 DECL_BIT_FIELD (field) = 1;
3055 if (integer_zerop (w)
3056 && ! (* targetm.ms_bitfield_layout_p) (DECL_FIELD_CONTEXT (field)))
3058 #ifdef EMPTY_FIELD_BOUNDARY
3059 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3060 EMPTY_FIELD_BOUNDARY);
3061 #endif
3062 #ifdef PCC_BITFIELD_TYPE_MATTERS
3063 if (PCC_BITFIELD_TYPE_MATTERS)
3065 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3066 TYPE_ALIGN (type));
3067 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3069 #endif
3072 else
3074 /* Non-bit-fields are aligned for their type. */
3075 DECL_BIT_FIELD (field) = 0;
3076 CLEAR_DECL_C_BIT_FIELD (field);
3077 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3078 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3082 /* FIELD is a non bit-field. We are finishing the processing for its
3083 enclosing type T. Issue any appropriate messages and set appropriate
3084 flags. */
3086 static void
3087 check_field_decl (field, t, cant_have_const_ctor,
3088 cant_have_default_ctor, no_const_asn_ref,
3089 any_default_members)
3090 tree field;
3091 tree t;
3092 int *cant_have_const_ctor;
3093 int *cant_have_default_ctor;
3094 int *no_const_asn_ref;
3095 int *any_default_members;
3097 tree type = strip_array_types (TREE_TYPE (field));
3099 /* An anonymous union cannot contain any fields which would change
3100 the settings of CANT_HAVE_CONST_CTOR and friends. */
3101 if (ANON_UNION_TYPE_P (type))
3103 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3104 structs. So, we recurse through their fields here. */
3105 else if (ANON_AGGR_TYPE_P (type))
3107 tree fields;
3109 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3110 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3111 check_field_decl (fields, t, cant_have_const_ctor,
3112 cant_have_default_ctor, no_const_asn_ref,
3113 any_default_members);
3115 /* Check members with class type for constructors, destructors,
3116 etc. */
3117 else if (CLASS_TYPE_P (type))
3119 /* Never let anything with uninheritable virtuals
3120 make it through without complaint. */
3121 abstract_virtuals_error (field, type);
3123 if (TREE_CODE (t) == UNION_TYPE)
3125 if (TYPE_NEEDS_CONSTRUCTING (type))
3126 cp_error_at ("member `%#D' with constructor not allowed in union",
3127 field);
3128 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3129 cp_error_at ("member `%#D' with destructor not allowed in union",
3130 field);
3131 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3132 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3133 field);
3135 else
3137 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3138 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3139 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3140 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3141 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3144 if (!TYPE_HAS_CONST_INIT_REF (type))
3145 *cant_have_const_ctor = 1;
3147 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3148 *no_const_asn_ref = 1;
3150 if (TYPE_HAS_CONSTRUCTOR (type)
3151 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3152 *cant_have_default_ctor = 1;
3154 if (DECL_INITIAL (field) != NULL_TREE)
3156 /* `build_class_init_list' does not recognize
3157 non-FIELD_DECLs. */
3158 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3159 cp_error_at ("multiple fields in union `%T' initialized");
3160 *any_default_members = 1;
3163 /* Non-bit-fields are aligned for their type, except packed fields
3164 which require only BITS_PER_UNIT alignment. */
3165 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3166 (DECL_PACKED (field)
3167 ? BITS_PER_UNIT
3168 : TYPE_ALIGN (TREE_TYPE (field))));
3169 if (! DECL_PACKED (field))
3170 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3173 /* Check the data members (both static and non-static), class-scoped
3174 typedefs, etc., appearing in the declaration of T. Issue
3175 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3176 declaration order) of access declarations; each TREE_VALUE in this
3177 list is a USING_DECL.
3179 In addition, set the following flags:
3181 EMPTY_P
3182 The class is empty, i.e., contains no non-static data members.
3184 CANT_HAVE_DEFAULT_CTOR_P
3185 This class cannot have an implicitly generated default
3186 constructor.
3188 CANT_HAVE_CONST_CTOR_P
3189 This class cannot have an implicitly generated copy constructor
3190 taking a const reference.
3192 CANT_HAVE_CONST_ASN_REF
3193 This class cannot have an implicitly generated assignment
3194 operator taking a const reference.
3196 All of these flags should be initialized before calling this
3197 function.
3199 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3200 fields can be added by adding to this chain. */
3202 static void
3203 check_field_decls (t, access_decls, empty_p,
3204 cant_have_default_ctor_p, cant_have_const_ctor_p,
3205 no_const_asn_ref_p)
3206 tree t;
3207 tree *access_decls;
3208 int *empty_p;
3209 int *cant_have_default_ctor_p;
3210 int *cant_have_const_ctor_p;
3211 int *no_const_asn_ref_p;
3213 tree *field;
3214 tree *next;
3215 int has_pointers;
3216 int any_default_members;
3218 /* First, delete any duplicate fields. */
3219 delete_duplicate_fields (TYPE_FIELDS (t));
3221 /* Assume there are no access declarations. */
3222 *access_decls = NULL_TREE;
3223 /* Assume this class has no pointer members. */
3224 has_pointers = 0;
3225 /* Assume none of the members of this class have default
3226 initializations. */
3227 any_default_members = 0;
3229 for (field = &TYPE_FIELDS (t); *field; field = next)
3231 tree x = *field;
3232 tree type = TREE_TYPE (x);
3234 next = &TREE_CHAIN (x);
3236 if (TREE_CODE (x) == FIELD_DECL)
3238 DECL_PACKED (x) |= TYPE_PACKED (t);
3240 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3241 /* We don't treat zero-width bitfields as making a class
3242 non-empty. */
3244 else
3246 /* The class is non-empty. */
3247 *empty_p = 0;
3248 /* The class is not even nearly empty. */
3249 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3253 if (TREE_CODE (x) == USING_DECL)
3255 /* Prune the access declaration from the list of fields. */
3256 *field = TREE_CHAIN (x);
3258 /* Save the access declarations for our caller. */
3259 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3261 /* Since we've reset *FIELD there's no reason to skip to the
3262 next field. */
3263 next = field;
3264 continue;
3267 if (TREE_CODE (x) == TYPE_DECL
3268 || TREE_CODE (x) == TEMPLATE_DECL)
3269 continue;
3271 /* If we've gotten this far, it's a data member, possibly static,
3272 or an enumerator. */
3274 DECL_CONTEXT (x) = t;
3276 /* ``A local class cannot have static data members.'' ARM 9.4 */
3277 if (current_function_decl && TREE_STATIC (x))
3278 cp_error_at ("field `%D' in local class cannot be static", x);
3280 /* Perform error checking that did not get done in
3281 grokdeclarator. */
3282 if (TREE_CODE (type) == FUNCTION_TYPE)
3284 cp_error_at ("field `%D' invalidly declared function type",
3286 type = build_pointer_type (type);
3287 TREE_TYPE (x) = type;
3289 else if (TREE_CODE (type) == METHOD_TYPE)
3291 cp_error_at ("field `%D' invalidly declared method type", x);
3292 type = build_pointer_type (type);
3293 TREE_TYPE (x) = type;
3295 else if (TREE_CODE (type) == OFFSET_TYPE)
3297 cp_error_at ("field `%D' invalidly declared offset type", x);
3298 type = build_pointer_type (type);
3299 TREE_TYPE (x) = type;
3302 if (type == error_mark_node)
3303 continue;
3305 /* When this goes into scope, it will be a non-local reference. */
3306 DECL_NONLOCAL (x) = 1;
3308 if (TREE_CODE (x) == CONST_DECL)
3309 continue;
3311 if (TREE_CODE (x) == VAR_DECL)
3313 if (TREE_CODE (t) == UNION_TYPE)
3314 /* Unions cannot have static members. */
3315 cp_error_at ("field `%D' declared static in union", x);
3317 continue;
3320 /* Now it can only be a FIELD_DECL. */
3322 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3323 CLASSTYPE_NON_AGGREGATE (t) = 1;
3325 /* If this is of reference type, check if it needs an init.
3326 Also do a little ANSI jig if necessary. */
3327 if (TREE_CODE (type) == REFERENCE_TYPE)
3329 CLASSTYPE_NON_POD_P (t) = 1;
3330 if (DECL_INITIAL (x) == NULL_TREE)
3331 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3333 /* ARM $12.6.2: [A member initializer list] (or, for an
3334 aggregate, initialization by a brace-enclosed list) is the
3335 only way to initialize nonstatic const and reference
3336 members. */
3337 *cant_have_default_ctor_p = 1;
3338 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3340 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3341 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3344 type = strip_array_types (type);
3346 if (TREE_CODE (type) == POINTER_TYPE)
3347 has_pointers = 1;
3349 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3350 CLASSTYPE_HAS_MUTABLE (t) = 1;
3352 if (! pod_type_p (type))
3353 /* DR 148 now allows pointers to members (which are POD themselves),
3354 to be allowed in POD structs. */
3355 CLASSTYPE_NON_POD_P (t) = 1;
3357 if (! zero_init_p (type))
3358 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3360 /* If any field is const, the structure type is pseudo-const. */
3361 if (CP_TYPE_CONST_P (type))
3363 C_TYPE_FIELDS_READONLY (t) = 1;
3364 if (DECL_INITIAL (x) == NULL_TREE)
3365 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3367 /* ARM $12.6.2: [A member initializer list] (or, for an
3368 aggregate, initialization by a brace-enclosed list) is the
3369 only way to initialize nonstatic const and reference
3370 members. */
3371 *cant_have_default_ctor_p = 1;
3372 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3374 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3375 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3377 /* A field that is pseudo-const makes the structure likewise. */
3378 else if (IS_AGGR_TYPE (type))
3380 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3381 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3382 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3385 /* Core issue 80: A nonstatic data member is required to have a
3386 different name from the class iff the class has a
3387 user-defined constructor. */
3388 if (DECL_NAME (x) == constructor_name (t)
3389 && TYPE_HAS_CONSTRUCTOR (t))
3390 cp_pedwarn_at ("field `%#D' with same name as class", x);
3392 /* We set DECL_C_BIT_FIELD in grokbitfield.
3393 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3394 if (DECL_C_BIT_FIELD (x))
3395 check_bitfield_decl (x);
3396 else
3397 check_field_decl (x, t,
3398 cant_have_const_ctor_p,
3399 cant_have_default_ctor_p,
3400 no_const_asn_ref_p,
3401 &any_default_members);
3404 /* Effective C++ rule 11. */
3405 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3406 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3408 warning ("`%#T' has pointer data members", t);
3410 if (! TYPE_HAS_INIT_REF (t))
3412 warning (" but does not override `%T(const %T&)'", t, t);
3413 if (! TYPE_HAS_ASSIGN_REF (t))
3414 warning (" or `operator=(const %T&)'", t);
3416 else if (! TYPE_HAS_ASSIGN_REF (t))
3417 warning (" but does not override `operator=(const %T&)'", t);
3421 /* Check anonymous struct/anonymous union fields. */
3422 finish_struct_anon (t);
3424 /* We've built up the list of access declarations in reverse order.
3425 Fix that now. */
3426 *access_decls = nreverse (*access_decls);
3429 /* If TYPE is an empty class type, records its OFFSET in the table of
3430 OFFSETS. */
3432 static int
3433 record_subobject_offset (type, offset, offsets)
3434 tree type;
3435 tree offset;
3436 splay_tree offsets;
3438 splay_tree_node n;
3440 if (!is_empty_class (type))
3441 return 0;
3443 /* Record the location of this empty object in OFFSETS. */
3444 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3445 if (!n)
3446 n = splay_tree_insert (offsets,
3447 (splay_tree_key) offset,
3448 (splay_tree_value) NULL_TREE);
3449 n->value = ((splay_tree_value)
3450 tree_cons (NULL_TREE,
3451 type,
3452 (tree) n->value));
3454 return 0;
3457 /* Returns non-zero if TYPE is an empty class type and there is
3458 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3460 static int
3461 check_subobject_offset (type, offset, offsets)
3462 tree type;
3463 tree offset;
3464 splay_tree offsets;
3466 splay_tree_node n;
3467 tree t;
3469 if (!is_empty_class (type))
3470 return 0;
3472 /* Record the location of this empty object in OFFSETS. */
3473 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3474 if (!n)
3475 return 0;
3477 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3478 if (same_type_p (TREE_VALUE (t), type))
3479 return 1;
3481 return 0;
3484 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3485 F for every subobject, passing it the type, offset, and table of
3486 OFFSETS. If VBASES_P is non-zero, then even virtual non-primary
3487 bases should be traversed; otherwise, they are ignored.
3489 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3490 than MAX_OFFSET will not be walked.
3492 If F returns a non-zero value, the traversal ceases, and that value
3493 is returned. Otherwise, returns zero. */
3495 static int
3496 walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
3497 tree type;
3498 subobject_offset_fn f;
3499 tree offset;
3500 splay_tree offsets;
3501 tree max_offset;
3502 int vbases_p;
3504 int r = 0;
3506 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3507 stop. */
3508 if (max_offset && INT_CST_LT (max_offset, offset))
3509 return 0;
3511 if (CLASS_TYPE_P (type))
3513 tree field;
3514 int i;
3516 /* Record the location of TYPE. */
3517 r = (*f) (type, offset, offsets);
3518 if (r)
3519 return r;
3521 /* Iterate through the direct base classes of TYPE. */
3522 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3524 tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3526 if (!vbases_p
3527 && TREE_VIA_VIRTUAL (binfo)
3528 && !BINFO_PRIMARY_P (binfo))
3529 continue;
3531 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3533 size_binop (PLUS_EXPR,
3534 offset,
3535 BINFO_OFFSET (binfo)),
3536 offsets,
3537 max_offset,
3538 vbases_p);
3539 if (r)
3540 return r;
3543 /* Iterate through the fields of TYPE. */
3544 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3545 if (TREE_CODE (field) == FIELD_DECL)
3547 r = walk_subobject_offsets (TREE_TYPE (field),
3549 size_binop (PLUS_EXPR,
3550 offset,
3551 DECL_FIELD_OFFSET (field)),
3552 offsets,
3553 max_offset,
3554 /*vbases_p=*/1);
3555 if (r)
3556 return r;
3559 else if (TREE_CODE (type) == ARRAY_TYPE)
3561 tree domain = TYPE_DOMAIN (type);
3562 tree index;
3564 /* Step through each of the elements in the array. */
3565 for (index = size_zero_node;
3566 INT_CST_LT (index, TYPE_MAX_VALUE (domain));
3567 index = size_binop (PLUS_EXPR, index, size_one_node))
3569 r = walk_subobject_offsets (TREE_TYPE (type),
3571 offset,
3572 offsets,
3573 max_offset,
3574 /*vbases_p=*/1);
3575 if (r)
3576 return r;
3577 offset = size_binop (PLUS_EXPR, offset,
3578 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3579 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3580 there's no point in iterating through the remaining
3581 elements of the array. */
3582 if (max_offset && INT_CST_LT (max_offset, offset))
3583 break;
3587 return 0;
3590 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3591 OFFSETS. If VBASES_P is non-zero, virtual bases of TYPE are
3592 examined. */
3594 static void
3595 record_subobject_offsets (type, offset, offsets, vbases_p)
3596 tree type;
3597 tree offset;
3598 splay_tree offsets;
3599 int vbases_p;
3601 walk_subobject_offsets (type, record_subobject_offset, offset,
3602 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3605 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3606 OFFSET) conflict with entries in OFFSETS. If VBASES_P is non-zero,
3607 virtual bases of TYPE are examined. */
3609 static int
3610 layout_conflict_p (type, offset, offsets, vbases_p)
3611 tree type;
3612 tree offset;
3613 splay_tree offsets;
3614 int vbases_p;
3616 splay_tree_node max_node;
3618 /* Get the node in OFFSETS that indicates the maximum offset where
3619 an empty subobject is located. */
3620 max_node = splay_tree_max (offsets);
3621 /* If there aren't any empty subobjects, then there's no point in
3622 performing this check. */
3623 if (!max_node)
3624 return 0;
3626 return walk_subobject_offsets (type, check_subobject_offset, offset,
3627 offsets, (tree) (max_node->key),
3628 vbases_p);
3631 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3632 non-static data member of the type indicated by RLI. BINFO is the
3633 binfo corresponding to the base subobject, OFFSETS maps offsets to
3634 types already located at those offsets. T is the most derived
3635 type. This function determines the position of the DECL. */
3637 static void
3638 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
3639 record_layout_info rli;
3640 tree decl;
3641 tree binfo;
3642 splay_tree offsets;
3643 tree t;
3645 tree offset = NULL_TREE;
3646 tree type = TREE_TYPE (decl);
3647 /* If we are laying out a base class, rather than a field, then
3648 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
3649 int field_p = !DECL_ARTIFICIAL (decl);
3651 /* Try to place the field. It may take more than one try if we have
3652 a hard time placing the field without putting two objects of the
3653 same type at the same address. */
3654 while (1)
3656 struct record_layout_info_s old_rli = *rli;
3658 /* Place this field. */
3659 place_field (rli, decl);
3660 offset = byte_position (decl);
3662 /* We have to check to see whether or not there is already
3663 something of the same type at the offset we're about to use.
3664 For example:
3666 struct S {};
3667 struct T : public S { int i; };
3668 struct U : public S, public T {};
3670 Here, we put S at offset zero in U. Then, we can't put T at
3671 offset zero -- its S component would be at the same address
3672 as the S we already allocated. So, we have to skip ahead.
3673 Since all data members, including those whose type is an
3674 empty class, have non-zero size, any overlap can happen only
3675 with a direct or indirect base-class -- it can't happen with
3676 a data member. */
3677 if (layout_conflict_p (TREE_TYPE (decl),
3678 offset,
3679 offsets,
3680 field_p))
3682 /* Strip off the size allocated to this field. That puts us
3683 at the first place we could have put the field with
3684 proper alignment. */
3685 *rli = old_rli;
3687 /* Bump up by the alignment required for the type. */
3688 rli->bitpos
3689 = size_binop (PLUS_EXPR, rli->bitpos,
3690 bitsize_int (binfo
3691 ? CLASSTYPE_ALIGN (type)
3692 : TYPE_ALIGN (type)));
3693 normalize_rli (rli);
3695 else
3696 /* There was no conflict. We're done laying out this field. */
3697 break;
3700 /* Now that we know where it will be placed, update its
3701 BINFO_OFFSET. */
3702 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3703 propagate_binfo_offsets (binfo,
3704 convert (ssizetype, offset), t);
3707 /* Layout the empty base BINFO. EOC indicates the byte currently just
3708 past the end of the class, and should be correctly aligned for a
3709 class of the type indicated by BINFO; OFFSETS gives the offsets of
3710 the empty bases allocated so far. T is the most derived
3711 type. Return non-zero iff we added it at the end. */
3713 static bool
3714 layout_empty_base (binfo, eoc, offsets, t)
3715 tree binfo;
3716 tree eoc;
3717 splay_tree offsets;
3718 tree t;
3720 tree alignment;
3721 tree basetype = BINFO_TYPE (binfo);
3722 bool atend = false;
3724 /* This routine should only be used for empty classes. */
3725 my_friendly_assert (is_empty_class (basetype), 20000321);
3726 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3728 /* This is an empty base class. We first try to put it at offset
3729 zero. */
3730 if (layout_conflict_p (BINFO_TYPE (binfo),
3731 BINFO_OFFSET (binfo),
3732 offsets,
3733 /*vbases_p=*/0))
3735 /* That didn't work. Now, we move forward from the next
3736 available spot in the class. */
3737 atend = true;
3738 propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
3739 while (1)
3741 if (!layout_conflict_p (BINFO_TYPE (binfo),
3742 BINFO_OFFSET (binfo),
3743 offsets,
3744 /*vbases_p=*/0))
3745 /* We finally found a spot where there's no overlap. */
3746 break;
3748 /* There's overlap here, too. Bump along to the next spot. */
3749 propagate_binfo_offsets (binfo, alignment, t);
3752 return atend;
3755 /* Build a FIELD_DECL for the base given by BINFO in the class
3756 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
3757 *BASE_ALIGN is a running maximum of the alignments of any base
3758 class. OFFSETS gives the location of empty base subobjects. T is
3759 the most derived type. Return non-zero if the new object cannot be
3760 nearly-empty. */
3762 static bool
3763 build_base_field (rli, binfo, empty_p, offsets, t)
3764 record_layout_info rli;
3765 tree binfo;
3766 int *empty_p;
3767 splay_tree offsets;
3768 tree t;
3770 tree basetype = BINFO_TYPE (binfo);
3771 tree decl;
3772 bool atend = false;
3774 if (!COMPLETE_TYPE_P (basetype))
3775 /* This error is now reported in xref_tag, thus giving better
3776 location information. */
3777 return atend;
3779 decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
3780 DECL_ARTIFICIAL (decl) = 1;
3781 DECL_FIELD_CONTEXT (decl) = rli->t;
3782 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3783 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3784 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3785 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3786 /* Tell the backend not to round up to TYPE_ALIGN. */
3787 DECL_PACKED (decl) = 1;
3789 if (!integer_zerop (DECL_SIZE (decl)))
3791 /* The containing class is non-empty because it has a non-empty
3792 base class. */
3793 *empty_p = 0;
3795 /* Try to place the field. It may take more than one try if we
3796 have a hard time placing the field without putting two
3797 objects of the same type at the same address. */
3798 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t);
3800 else
3802 unsigned HOST_WIDE_INT eoc;
3804 /* On some platforms (ARM), even empty classes will not be
3805 byte-aligned. */
3806 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
3807 eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
3808 atend |= layout_empty_base (binfo, size_int (eoc), offsets, t);
3811 /* Record the offsets of BINFO and its base subobjects. */
3812 record_subobject_offsets (BINFO_TYPE (binfo),
3813 BINFO_OFFSET (binfo),
3814 offsets,
3815 /*vbases_p=*/0);
3816 return atend;
3819 /* Layout all of the non-virtual base classes. Record empty
3820 subobjects in OFFSETS. T is the most derived type. Return
3821 non-zero if the type cannot be nearly empty. */
3823 static bool
3824 build_base_fields (rli, empty_p, offsets, t)
3825 record_layout_info rli;
3826 int *empty_p;
3827 splay_tree offsets;
3828 tree t;
3830 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3831 subobjects. */
3832 tree rec = rli->t;
3833 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3834 int i;
3835 bool atend = 0;
3837 /* The primary base class is always allocated first. */
3838 if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
3839 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
3840 empty_p, offsets, t);
3842 /* Now allocate the rest of the bases. */
3843 for (i = 0; i < n_baseclasses; ++i)
3845 tree base_binfo;
3847 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3849 /* The primary base was already allocated above, so we don't
3850 need to allocate it again here. */
3851 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
3852 continue;
3854 /* A primary virtual base class is allocated just like any other
3855 base class, but a non-primary virtual base is allocated
3856 later, in layout_virtual_bases. */
3857 if (TREE_VIA_VIRTUAL (base_binfo)
3858 && !BINFO_PRIMARY_P (base_binfo))
3859 continue;
3861 atend |= build_base_field (rli, base_binfo, empty_p, offsets, t);
3863 return atend;
3866 /* Go through the TYPE_METHODS of T issuing any appropriate
3867 diagnostics, figuring out which methods override which other
3868 methods, and so forth. */
3870 static void
3871 check_methods (t)
3872 tree t;
3874 tree x;
3876 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3878 /* If this was an evil function, don't keep it in class. */
3879 if (DECL_ASSEMBLER_NAME_SET_P (x)
3880 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3881 continue;
3883 check_for_override (x, t);
3884 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3885 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3887 /* The name of the field is the original field name
3888 Save this in auxiliary field for later overloading. */
3889 if (DECL_VINDEX (x))
3891 TYPE_POLYMORPHIC_P (t) = 1;
3892 if (DECL_PURE_VIRTUAL_P (x))
3893 CLASSTYPE_PURE_VIRTUALS (t)
3894 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3899 /* FN is a constructor or destructor. Clone the declaration to create
3900 a specialized in-charge or not-in-charge version, as indicated by
3901 NAME. */
3903 static tree
3904 build_clone (fn, name)
3905 tree fn;
3906 tree name;
3908 tree parms;
3909 tree clone;
3911 /* Copy the function. */
3912 clone = copy_decl (fn);
3913 /* Remember where this function came from. */
3914 DECL_CLONED_FUNCTION (clone) = fn;
3915 DECL_ABSTRACT_ORIGIN (clone) = fn;
3916 /* Reset the function name. */
3917 DECL_NAME (clone) = name;
3918 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3919 /* There's no pending inline data for this function. */
3920 DECL_PENDING_INLINE_INFO (clone) = NULL;
3921 DECL_PENDING_INLINE_P (clone) = 0;
3922 /* And it hasn't yet been deferred. */
3923 DECL_DEFERRED_FN (clone) = 0;
3925 /* The base-class destructor is not virtual. */
3926 if (name == base_dtor_identifier)
3928 DECL_VIRTUAL_P (clone) = 0;
3929 if (TREE_CODE (clone) != TEMPLATE_DECL)
3930 DECL_VINDEX (clone) = NULL_TREE;
3933 /* If there was an in-charge parameter, drop it from the function
3934 type. */
3935 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3937 tree basetype;
3938 tree parmtypes;
3939 tree exceptions;
3941 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3942 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3943 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3944 /* Skip the `this' parameter. */
3945 parmtypes = TREE_CHAIN (parmtypes);
3946 /* Skip the in-charge parameter. */
3947 parmtypes = TREE_CHAIN (parmtypes);
3948 /* And the VTT parm, in a complete [cd]tor. */
3949 if (DECL_HAS_VTT_PARM_P (fn)
3950 && ! DECL_NEEDS_VTT_PARM_P (clone))
3951 parmtypes = TREE_CHAIN (parmtypes);
3952 /* If this is subobject constructor or destructor, add the vtt
3953 parameter. */
3954 TREE_TYPE (clone)
3955 = build_cplus_method_type (basetype,
3956 TREE_TYPE (TREE_TYPE (clone)),
3957 parmtypes);
3958 if (exceptions)
3959 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3960 exceptions);
3963 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
3964 aren't function parameters; those are the template parameters. */
3965 if (TREE_CODE (clone) != TEMPLATE_DECL)
3967 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
3968 /* Remove the in-charge parameter. */
3969 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3971 TREE_CHAIN (DECL_ARGUMENTS (clone))
3972 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3973 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
3975 /* And the VTT parm, in a complete [cd]tor. */
3976 if (DECL_HAS_VTT_PARM_P (fn))
3978 if (DECL_NEEDS_VTT_PARM_P (clone))
3979 DECL_HAS_VTT_PARM_P (clone) = 1;
3980 else
3982 TREE_CHAIN (DECL_ARGUMENTS (clone))
3983 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3984 DECL_HAS_VTT_PARM_P (clone) = 0;
3988 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
3990 DECL_CONTEXT (parms) = clone;
3991 cxx_dup_lang_specific_decl (parms);
3995 /* Create the RTL for this function. */
3996 SET_DECL_RTL (clone, NULL_RTX);
3997 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
3999 /* Make it easy to find the CLONE given the FN. */
4000 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4001 TREE_CHAIN (fn) = clone;
4003 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4004 if (TREE_CODE (clone) == TEMPLATE_DECL)
4006 tree result;
4008 DECL_TEMPLATE_RESULT (clone)
4009 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4010 result = DECL_TEMPLATE_RESULT (clone);
4011 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4012 DECL_TI_TEMPLATE (result) = clone;
4014 else if (DECL_DEFERRED_FN (fn))
4015 defer_fn (clone);
4017 return clone;
4020 /* Produce declarations for all appropriate clones of FN. If
4021 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4022 CLASTYPE_METHOD_VEC as well. */
4024 void
4025 clone_function_decl (fn, update_method_vec_p)
4026 tree fn;
4027 int update_method_vec_p;
4029 tree clone;
4031 /* Avoid inappropriate cloning. */
4032 if (TREE_CHAIN (fn)
4033 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4034 return;
4036 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4038 /* For each constructor, we need two variants: an in-charge version
4039 and a not-in-charge version. */
4040 clone = build_clone (fn, complete_ctor_identifier);
4041 if (update_method_vec_p)
4042 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4043 clone = build_clone (fn, base_ctor_identifier);
4044 if (update_method_vec_p)
4045 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4047 else
4049 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4051 /* For each destructor, we need three variants: an in-charge
4052 version, a not-in-charge version, and an in-charge deleting
4053 version. We clone the deleting version first because that
4054 means it will go second on the TYPE_METHODS list -- and that
4055 corresponds to the correct layout order in the virtual
4056 function table.
4058 For a non-virtual destructor, we do not build a deleting
4059 destructor. */
4060 if (DECL_VIRTUAL_P (fn))
4062 clone = build_clone (fn, deleting_dtor_identifier);
4063 if (update_method_vec_p)
4064 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4066 clone = build_clone (fn, complete_dtor_identifier);
4067 if (update_method_vec_p)
4068 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4069 clone = build_clone (fn, base_dtor_identifier);
4070 if (update_method_vec_p)
4071 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4074 /* Note that this is an abstract function that is never emitted. */
4075 DECL_ABSTRACT (fn) = 1;
4078 /* DECL is an in charge constructor, which is being defined. This will
4079 have had an in class declaration, from whence clones were
4080 declared. An out-of-class definition can specify additional default
4081 arguments. As it is the clones that are involved in overload
4082 resolution, we must propagate the information from the DECL to its
4083 clones. */
4085 void
4086 adjust_clone_args (decl)
4087 tree decl;
4089 tree clone;
4091 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4092 clone = TREE_CHAIN (clone))
4094 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4095 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4096 tree decl_parms, clone_parms;
4098 clone_parms = orig_clone_parms;
4100 /* Skip the 'this' parameter. */
4101 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4102 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4104 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4105 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4106 if (DECL_HAS_VTT_PARM_P (decl))
4107 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4109 clone_parms = orig_clone_parms;
4110 if (DECL_HAS_VTT_PARM_P (clone))
4111 clone_parms = TREE_CHAIN (clone_parms);
4113 for (decl_parms = orig_decl_parms; decl_parms;
4114 decl_parms = TREE_CHAIN (decl_parms),
4115 clone_parms = TREE_CHAIN (clone_parms))
4117 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4118 TREE_TYPE (clone_parms)), 20010424);
4120 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4122 /* A default parameter has been added. Adjust the
4123 clone's parameters. */
4124 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4125 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4126 tree type;
4128 clone_parms = orig_decl_parms;
4130 if (DECL_HAS_VTT_PARM_P (clone))
4132 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4133 TREE_VALUE (orig_clone_parms),
4134 clone_parms);
4135 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4137 type = build_cplus_method_type (basetype,
4138 TREE_TYPE (TREE_TYPE (clone)),
4139 clone_parms);
4140 if (exceptions)
4141 type = build_exception_variant (type, exceptions);
4142 TREE_TYPE (clone) = type;
4144 clone_parms = NULL_TREE;
4145 break;
4148 my_friendly_assert (!clone_parms, 20010424);
4152 /* For each of the constructors and destructors in T, create an
4153 in-charge and not-in-charge variant. */
4155 static void
4156 clone_constructors_and_destructors (t)
4157 tree t;
4159 tree fns;
4161 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4162 out now. */
4163 if (!CLASSTYPE_METHOD_VEC (t))
4164 return;
4166 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4167 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4168 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4169 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4172 /* Remove all zero-width bit-fields from T. */
4174 static void
4175 remove_zero_width_bit_fields (t)
4176 tree t;
4178 tree *fieldsp;
4180 fieldsp = &TYPE_FIELDS (t);
4181 while (*fieldsp)
4183 if (TREE_CODE (*fieldsp) == FIELD_DECL
4184 && DECL_C_BIT_FIELD (*fieldsp)
4185 && DECL_INITIAL (*fieldsp))
4186 *fieldsp = TREE_CHAIN (*fieldsp);
4187 else
4188 fieldsp = &TREE_CHAIN (*fieldsp);
4192 /* Returns TRUE iff we need a cookie when dynamically allocating an
4193 array whose elements have the indicated class TYPE. */
4195 static bool
4196 type_requires_array_cookie (type)
4197 tree type;
4199 tree fns;
4200 bool has_two_argument_delete_p = false;
4202 my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4204 /* If there's a non-trivial destructor, we need a cookie. In order
4205 to iterate through the array calling the destructor for each
4206 element, we'll have to know how many elements there are. */
4207 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4208 return true;
4210 /* If the usual deallocation function is a two-argument whose second
4211 argument is of type `size_t', then we have to pass the size of
4212 the array to the deallocation function, so we will need to store
4213 a cookie. */
4214 fns = lookup_fnfields (TYPE_BINFO (type),
4215 ansi_opname (VEC_DELETE_EXPR),
4216 /*protect=*/0);
4217 /* If there are no `operator []' members, or the lookup is
4218 ambiguous, then we don't need a cookie. */
4219 if (!fns || fns == error_mark_node)
4220 return false;
4221 /* Loop through all of the functions. */
4222 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4224 tree fn;
4225 tree second_parm;
4227 /* Select the current function. */
4228 fn = OVL_CURRENT (fns);
4229 /* See if this function is a one-argument delete function. If
4230 it is, then it will be the usual deallocation function. */
4231 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4232 if (second_parm == void_list_node)
4233 return false;
4234 /* Otherwise, if we have a two-argument function and the second
4235 argument is `size_t', it will be the usual deallocation
4236 function -- unless there is one-argument function, too. */
4237 if (TREE_CHAIN (second_parm) == void_list_node
4238 && same_type_p (TREE_VALUE (second_parm), sizetype))
4239 has_two_argument_delete_p = true;
4242 return has_two_argument_delete_p;
4245 /* Check the validity of the bases and members declared in T. Add any
4246 implicitly-generated functions (like copy-constructors and
4247 assignment operators). Compute various flag bits (like
4248 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4249 level: i.e., independently of the ABI in use. */
4251 static void
4252 check_bases_and_members (t, empty_p)
4253 tree t;
4254 int *empty_p;
4256 /* Nonzero if we are not allowed to generate a default constructor
4257 for this case. */
4258 int cant_have_default_ctor;
4259 /* Nonzero if the implicitly generated copy constructor should take
4260 a non-const reference argument. */
4261 int cant_have_const_ctor;
4262 /* Nonzero if the the implicitly generated assignment operator
4263 should take a non-const reference argument. */
4264 int no_const_asn_ref;
4265 tree access_decls;
4267 /* By default, we use const reference arguments and generate default
4268 constructors. */
4269 cant_have_default_ctor = 0;
4270 cant_have_const_ctor = 0;
4271 no_const_asn_ref = 0;
4273 /* Assume that the class is nearly empty; we'll clear this flag if
4274 it turns out not to be nearly empty. */
4275 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4277 /* Check all the base-classes. */
4278 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4279 &no_const_asn_ref);
4281 /* Check all the data member declarations. */
4282 check_field_decls (t, &access_decls, empty_p,
4283 &cant_have_default_ctor,
4284 &cant_have_const_ctor,
4285 &no_const_asn_ref);
4287 /* Check all the method declarations. */
4288 check_methods (t);
4290 /* A nearly-empty class has to be vptr-containing; a nearly empty
4291 class contains just a vptr. */
4292 if (!TYPE_CONTAINS_VPTR_P (t))
4293 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4295 /* Do some bookkeeping that will guide the generation of implicitly
4296 declared member functions. */
4297 TYPE_HAS_COMPLEX_INIT_REF (t)
4298 |= (TYPE_HAS_INIT_REF (t)
4299 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4300 || TYPE_POLYMORPHIC_P (t));
4301 TYPE_NEEDS_CONSTRUCTING (t)
4302 |= (TYPE_HAS_CONSTRUCTOR (t)
4303 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4304 || TYPE_POLYMORPHIC_P (t));
4305 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4306 || TYPE_POLYMORPHIC_P (t));
4307 CLASSTYPE_NON_POD_P (t)
4308 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4309 || TYPE_HAS_ASSIGN_REF (t));
4310 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4311 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4312 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4314 /* Synthesize any needed methods. Note that methods will be synthesized
4315 for anonymous unions; grok_x_components undoes that. */
4316 add_implicitly_declared_members (t, cant_have_default_ctor,
4317 cant_have_const_ctor,
4318 no_const_asn_ref);
4320 /* Create the in-charge and not-in-charge variants of constructors
4321 and destructors. */
4322 clone_constructors_and_destructors (t);
4324 /* Process the using-declarations. */
4325 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4326 handle_using_decl (TREE_VALUE (access_decls), t);
4328 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4329 finish_struct_methods (t);
4331 /* Figure out whether or not we will need a cookie when dynamically
4332 allocating an array of this type. */
4333 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4334 = type_requires_array_cookie (t);
4337 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4338 accordingly. If a new vfield was created (because T doesn't have a
4339 primary base class), then the newly created field is returned. It
4340 is not added to the TYPE_FIELDS list; it is the caller's
4341 responsibility to do that. Accumulate declared virtual functions
4342 on VIRTUALS_P. */
4344 static tree
4345 create_vtable_ptr (t, empty_p, virtuals_p)
4346 tree t;
4347 int *empty_p;
4348 tree *virtuals_p;
4350 tree fn;
4352 /* Collect the virtual functions declared in T. */
4353 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4354 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4355 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4357 tree new_virtual = make_node (TREE_LIST);
4359 BV_FN (new_virtual) = fn;
4360 BV_DELTA (new_virtual) = integer_zero_node;
4362 TREE_CHAIN (new_virtual) = *virtuals_p;
4363 *virtuals_p = new_virtual;
4366 /* If we couldn't find an appropriate base class, create a new field
4367 here. Even if there weren't any new virtual functions, we might need a
4368 new virtual function table if we're supposed to include vptrs in
4369 all classes that need them. */
4370 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
4372 /* We build this decl with vtbl_ptr_type_node, which is a
4373 `vtable_entry_type*'. It might seem more precise to use
4374 `vtable_entry_type (*)[N]' where N is the number of firtual
4375 functions. However, that would require the vtable pointer in
4376 base classes to have a different type than the vtable pointer
4377 in derived classes. We could make that happen, but that
4378 still wouldn't solve all the problems. In particular, the
4379 type-based alias analysis code would decide that assignments
4380 to the base class vtable pointer can't alias assignments to
4381 the derived class vtable pointer, since they have different
4382 types. Thus, in an derived class destructor, where the base
4383 class constructor was inlined, we could generate bad code for
4384 setting up the vtable pointer.
4386 Therefore, we use one type for all vtable pointers. We still
4387 use a type-correct type; it's just doesn't indicate the array
4388 bounds. That's better than using `void*' or some such; it's
4389 cleaner, and it let's the alias analysis code know that these
4390 stores cannot alias stores to void*! */
4391 tree field;
4393 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4394 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4395 DECL_VIRTUAL_P (field) = 1;
4396 DECL_ARTIFICIAL (field) = 1;
4397 DECL_FIELD_CONTEXT (field) = t;
4398 DECL_FCONTEXT (field) = t;
4399 DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4400 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4402 TYPE_VFIELD (t) = field;
4404 /* This class is non-empty. */
4405 *empty_p = 0;
4407 if (CLASSTYPE_N_BASECLASSES (t))
4408 /* If there were any baseclasses, they can't possibly be at
4409 offset zero any more, because that's where the vtable
4410 pointer is. So, converting to a base class is going to
4411 take work. */
4412 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4414 return field;
4417 return NULL_TREE;
4420 /* Fixup the inline function given by INFO now that the class is
4421 complete. */
4423 static void
4424 fixup_pending_inline (fn)
4425 tree fn;
4427 if (DECL_PENDING_INLINE_INFO (fn))
4429 tree args = DECL_ARGUMENTS (fn);
4430 while (args)
4432 DECL_CONTEXT (args) = fn;
4433 args = TREE_CHAIN (args);
4438 /* Fixup the inline methods and friends in TYPE now that TYPE is
4439 complete. */
4441 static void
4442 fixup_inline_methods (type)
4443 tree type;
4445 tree method = TYPE_METHODS (type);
4447 if (method && TREE_CODE (method) == TREE_VEC)
4449 if (TREE_VEC_ELT (method, 1))
4450 method = TREE_VEC_ELT (method, 1);
4451 else if (TREE_VEC_ELT (method, 0))
4452 method = TREE_VEC_ELT (method, 0);
4453 else
4454 method = TREE_VEC_ELT (method, 2);
4457 /* Do inline member functions. */
4458 for (; method; method = TREE_CHAIN (method))
4459 fixup_pending_inline (method);
4461 /* Do friends. */
4462 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4463 method;
4464 method = TREE_CHAIN (method))
4465 fixup_pending_inline (TREE_VALUE (method));
4466 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4469 /* Add OFFSET to all base types of BINFO which is a base in the
4470 hierarchy dominated by T.
4472 OFFSET, which is a type offset, is number of bytes. */
4474 static void
4475 propagate_binfo_offsets (binfo, offset, t)
4476 tree binfo;
4477 tree offset;
4478 tree t;
4480 int i;
4481 tree primary_binfo;
4483 /* Update BINFO's offset. */
4484 BINFO_OFFSET (binfo)
4485 = convert (sizetype,
4486 size_binop (PLUS_EXPR,
4487 convert (ssizetype, BINFO_OFFSET (binfo)),
4488 offset));
4490 /* Find the primary base class. */
4491 primary_binfo = get_primary_binfo (binfo);
4493 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4494 downwards. */
4495 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4497 tree base_binfo;
4499 /* On the first time through the loop, do the primary base.
4500 Because the primary base need not be an immediate base, we
4501 must handle the primary base specially. */
4502 if (i == -1)
4504 if (!primary_binfo)
4505 continue;
4507 base_binfo = primary_binfo;
4509 else
4511 base_binfo = BINFO_BASETYPE (binfo, i);
4512 /* Don't do the primary base twice. */
4513 if (base_binfo == primary_binfo)
4514 continue;
4517 /* Skip virtual bases that aren't our canonical primary base. */
4518 if (TREE_VIA_VIRTUAL (base_binfo)
4519 && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4520 || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4521 continue;
4523 propagate_binfo_offsets (base_binfo, offset, t);
4527 /* Called via dfs_walk from layout_virtual bases. */
4529 static tree
4530 dfs_set_offset_for_unshared_vbases (binfo, data)
4531 tree binfo;
4532 void *data;
4534 /* If this is a virtual base, make sure it has the same offset as
4535 the shared copy. If it's a primary base, then we know it's
4536 correct. */
4537 if (TREE_VIA_VIRTUAL (binfo))
4539 tree t = (tree) data;
4540 tree vbase;
4541 tree offset;
4543 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4544 if (vbase != binfo)
4546 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4547 propagate_binfo_offsets (binfo, offset, t);
4551 return NULL_TREE;
4554 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4555 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4556 empty subobjects of T. */
4558 static void
4559 layout_virtual_bases (t, offsets)
4560 tree t;
4561 splay_tree offsets;
4563 tree vbases, dsize;
4564 unsigned HOST_WIDE_INT eoc;
4565 bool first_vbase = true;
4567 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4568 return;
4570 #ifdef STRUCTURE_SIZE_BOUNDARY
4571 /* Packed structures don't need to have minimum size. */
4572 if (! TYPE_PACKED (t))
4573 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4574 #endif
4576 /* DSIZE is the size of the class without the virtual bases. */
4577 dsize = TYPE_SIZE (t);
4579 /* Make every class have alignment of at least one. */
4580 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4582 /* Go through the virtual bases, allocating space for each virtual
4583 base that is not already a primary base class. These are
4584 allocated in inheritance graph order. */
4585 for (vbases = TYPE_BINFO (t);
4586 vbases;
4587 vbases = TREE_CHAIN (vbases))
4589 tree vbase;
4591 if (!TREE_VIA_VIRTUAL (vbases))
4592 continue;
4594 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4596 if (!BINFO_PRIMARY_P (vbase))
4598 /* This virtual base is not a primary base of any class in the
4599 hierarchy, so we have to add space for it. */
4600 tree basetype, usize;
4601 unsigned int desired_align;
4603 basetype = BINFO_TYPE (vbase);
4605 desired_align = CLASSTYPE_ALIGN (basetype);
4606 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4608 /* Add padding so that we can put the virtual base class at an
4609 appropriately aligned offset. */
4610 dsize = round_up (dsize, desired_align);
4611 usize = size_binop (CEIL_DIV_EXPR, dsize, bitsize_unit_node);
4613 /* We try to squish empty virtual bases in just like
4614 ordinary empty bases. */
4615 if (is_empty_class (basetype))
4616 layout_empty_base (vbase,
4617 convert (sizetype, usize),
4618 offsets, t);
4619 else
4621 tree offset;
4623 offset = convert (ssizetype, usize);
4624 offset = size_diffop (offset,
4625 convert (ssizetype,
4626 BINFO_OFFSET (vbase)));
4628 /* And compute the offset of the virtual base. */
4629 propagate_binfo_offsets (vbase, offset, t);
4630 /* Every virtual baseclass takes a least a UNIT, so that
4631 we can take it's address and get something different
4632 for each base. */
4633 dsize = size_binop (PLUS_EXPR, dsize,
4634 size_binop (MAX_EXPR, bitsize_unit_node,
4635 CLASSTYPE_SIZE (basetype)));
4638 /* If the first virtual base might have been placed at a
4639 lower address, had we started from CLASSTYPE_SIZE, rather
4640 than TYPE_SIZE, issue a warning. There can be both false
4641 positives and false negatives from this warning in rare
4642 cases; to deal with all the possibilities would probably
4643 require performing both layout algorithms and comparing
4644 the results which is not particularly tractable. */
4645 if (warn_abi
4646 && first_vbase
4647 && tree_int_cst_lt (size_binop (CEIL_DIV_EXPR,
4648 round_up (CLASSTYPE_SIZE (t),
4649 desired_align),
4650 bitsize_unit_node),
4651 BINFO_OFFSET (vbase)))
4652 warning ("offset of virtual base `%T' is not ABI-compliant and may change in a future version of GCC",
4653 basetype);
4655 /* Keep track of the offsets assigned to this virtual base. */
4656 record_subobject_offsets (BINFO_TYPE (vbase),
4657 BINFO_OFFSET (vbase),
4658 offsets,
4659 /*vbases_p=*/0);
4661 first_vbase = false;
4665 /* Now, go through the TYPE_BINFO hierarchy, setting the
4666 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4667 bases and their direct and indirect bases. The ambiguity checks
4668 in lookup_base depend on the BINFO_OFFSETs being set
4669 correctly. */
4670 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4672 /* If we had empty base classes that protruded beyond the end of the
4673 class, we didn't update DSIZE above; we were hoping to overlay
4674 multiple such bases at the same location. */
4675 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4676 dsize = size_binop (MAX_EXPR, dsize, bitsize_int (eoc * BITS_PER_UNIT));
4678 /* Now, make sure that the total size of the type is a multiple of
4679 its alignment. */
4680 dsize = round_up (dsize, TYPE_ALIGN (t));
4681 TYPE_SIZE (t) = dsize;
4682 TYPE_SIZE_UNIT (t) = convert (sizetype,
4683 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4684 bitsize_unit_node));
4686 /* Check for ambiguous virtual bases. */
4687 if (extra_warnings)
4688 for (vbases = CLASSTYPE_VBASECLASSES (t);
4689 vbases;
4690 vbases = TREE_CHAIN (vbases))
4692 tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
4694 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4695 warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4696 basetype, t);
4700 /* Returns the offset of the byte just past the end of the base class
4701 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4702 only non-virtual bases are included. */
4704 static unsigned HOST_WIDE_INT
4705 end_of_class (t, include_virtuals_p)
4706 tree t;
4707 int include_virtuals_p;
4709 unsigned HOST_WIDE_INT result = 0;
4710 int i;
4712 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4714 tree base_binfo;
4715 tree offset;
4716 tree size;
4717 unsigned HOST_WIDE_INT end_of_base;
4719 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4721 if (!include_virtuals_p
4722 && TREE_VIA_VIRTUAL (base_binfo)
4723 && !BINFO_PRIMARY_P (base_binfo))
4724 continue;
4726 if (is_empty_class (BINFO_TYPE (base_binfo)))
4727 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4728 allocate some space for it. It cannot have virtual bases,
4729 so TYPE_SIZE_UNIT is fine. */
4730 size = TYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4731 else
4732 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4733 offset = size_binop (PLUS_EXPR,
4734 BINFO_OFFSET (base_binfo),
4735 size);
4736 end_of_base = tree_low_cst (offset, /*pos=*/1);
4737 if (end_of_base > result)
4738 result = end_of_base;
4741 return result;
4744 /* Warn about direct bases of T that are inaccessible because they are
4745 ambiguous. For example:
4747 struct S {};
4748 struct T : public S {};
4749 struct U : public S, public T {};
4751 Here, `(S*) new U' is not allowed because there are two `S'
4752 subobjects of U. */
4754 static void
4755 warn_about_ambiguous_direct_bases (t)
4756 tree t;
4758 int i;
4760 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4762 tree basetype = TYPE_BINFO_BASETYPE (t, i);
4764 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4765 warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4766 basetype, t);
4770 /* Compare two INTEGER_CSTs K1 and K2. */
4772 static int
4773 splay_tree_compare_integer_csts (k1, k2)
4774 splay_tree_key k1;
4775 splay_tree_key k2;
4777 return tree_int_cst_compare ((tree) k1, (tree) k2);
4780 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4781 BINFO_OFFSETs for all of the base-classes. Position the vtable
4782 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
4784 static void
4785 layout_class_type (t, empty_p, vfuns_p, virtuals_p)
4786 tree t;
4787 int *empty_p;
4788 int *vfuns_p;
4789 tree *virtuals_p;
4791 tree non_static_data_members;
4792 tree field;
4793 tree vptr;
4794 record_layout_info rli;
4795 unsigned HOST_WIDE_INT eoc;
4796 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4797 types that appear at that offset. */
4798 splay_tree empty_base_offsets;
4799 /* True if the last field layed out was a bit-field. */
4800 bool last_field_was_bitfield = false;
4802 /* Keep track of the first non-static data member. */
4803 non_static_data_members = TYPE_FIELDS (t);
4805 /* Start laying out the record. */
4806 rli = start_record_layout (t);
4808 /* If possible, we reuse the virtual function table pointer from one
4809 of our base classes. */
4810 determine_primary_base (t, vfuns_p);
4812 /* Create a pointer to our virtual function table. */
4813 vptr = create_vtable_ptr (t, empty_p, virtuals_p);
4815 /* The vptr is always the first thing in the class. */
4816 if (vptr)
4818 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4819 place_field (rli, vptr);
4822 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4823 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4824 NULL, NULL);
4825 if (build_base_fields (rli, empty_p, empty_base_offsets, t))
4826 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4828 /* Layout the non-static data members. */
4829 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4831 tree type;
4832 tree padding;
4834 /* We still pass things that aren't non-static data members to
4835 the back-end, in case it wants to do something with them. */
4836 if (TREE_CODE (field) != FIELD_DECL)
4838 place_field (rli, field);
4839 /* If the static data member has incomplete type, keep track
4840 of it so that it can be completed later. (The handling
4841 of pending statics in finish_record_layout is
4842 insufficient; consider:
4844 struct S1;
4845 struct S2 { static S1 s1; };
4847 At this point, finish_record_layout will be called, but
4848 S1 is still incomplete.) */
4849 if (TREE_CODE (field) == VAR_DECL)
4850 maybe_register_incomplete_var (field);
4851 continue;
4854 type = TREE_TYPE (field);
4856 /* If this field is a bit-field whose width is greater than its
4857 type, then there are some special rules for allocating
4858 it. */
4859 if (DECL_C_BIT_FIELD (field)
4860 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4862 integer_type_kind itk;
4863 tree integer_type;
4865 /* We must allocate the bits as if suitably aligned for the
4866 longest integer type that fits in this many bits. type
4867 of the field. Then, we are supposed to use the left over
4868 bits as additional padding. */
4869 for (itk = itk_char; itk != itk_none; ++itk)
4870 if (INT_CST_LT (DECL_SIZE (field),
4871 TYPE_SIZE (integer_types[itk])))
4872 break;
4874 /* ITK now indicates a type that is too large for the
4875 field. We have to back up by one to find the largest
4876 type that fits. */
4877 integer_type = integer_types[itk - 1];
4878 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4879 TYPE_SIZE (integer_type));
4880 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4881 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4882 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4884 else
4885 padding = NULL_TREE;
4887 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4888 empty_base_offsets, t);
4890 /* If a bit-field does not immediately follow another bit-field,
4891 and yet it starts in the middle of a byte, we have failed to
4892 comply with the ABI. */
4893 if (warn_abi
4894 && DECL_C_BIT_FIELD (field)
4895 && !last_field_was_bitfield
4896 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
4897 DECL_FIELD_BIT_OFFSET (field),
4898 bitsize_unit_node)))
4899 cp_warning_at ("offset of `%D' is not ABI-compliant and may change in a future version of GCC",
4900 field);
4902 /* If we needed additional padding after this field, add it
4903 now. */
4904 if (padding)
4906 tree padding_field;
4908 padding_field = build_decl (FIELD_DECL,
4909 NULL_TREE,
4910 char_type_node);
4911 DECL_BIT_FIELD (padding_field) = 1;
4912 DECL_SIZE (padding_field) = padding;
4913 DECL_ALIGN (padding_field) = 1;
4914 DECL_USER_ALIGN (padding_field) = 0;
4915 layout_nonempty_base_or_field (rli, padding_field,
4916 NULL_TREE,
4917 empty_base_offsets, t);
4920 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
4923 /* It might be the case that we grew the class to allocate a
4924 zero-sized base class. That won't be reflected in RLI, yet,
4925 because we are willing to overlay multiple bases at the same
4926 offset. However, now we need to make sure that RLI is big enough
4927 to reflect the entire class. */
4928 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4929 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4930 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
4932 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc));
4933 rli->bitpos = bitsize_zero_node;
4936 /* We make all structures have at least one element, so that they
4937 have non-zero size. The class may be empty even if it has
4938 basetypes. Therefore, we add the fake field after all the other
4939 fields; if there are already FIELD_DECLs on the list, their
4940 offsets will not be disturbed. */
4941 if (!eoc && *empty_p)
4943 tree padding;
4945 padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
4946 place_field (rli, padding);
4949 /* Let the back-end lay out the type. Note that at this point we
4950 have only included non-virtual base-classes; we will lay out the
4951 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4952 this call are not necessarily correct; they are just the size and
4953 alignment when no virtual base clases are used. */
4954 finish_record_layout (rli);
4956 /* Delete all zero-width bit-fields from the list of fields. Now
4957 that the type is laid out they are no longer important. */
4958 remove_zero_width_bit_fields (t);
4960 /* Remember the size and alignment of the class before adding
4961 the virtual bases. */
4962 if (*empty_p)
4964 CLASSTYPE_SIZE (t) = bitsize_zero_node;
4965 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
4967 /* If this is a POD, we can't reuse its tail padding. */
4968 else if (!CLASSTYPE_NON_POD_P (t))
4970 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4971 CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
4973 else
4975 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4976 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
4979 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4980 CLASSTYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (t);
4982 /* Set the TYPE_DECL for this type to contain the right
4983 value for DECL_OFFSET, so that we can use it as part
4984 of a COMPONENT_REF for multiple inheritance. */
4985 layout_decl (TYPE_MAIN_DECL (t), 0);
4987 /* Now fix up any virtual base class types that we left lying
4988 around. We must get these done before we try to lay out the
4989 virtual function table. As a side-effect, this will remove the
4990 base subobject fields. */
4991 layout_virtual_bases (t, empty_base_offsets);
4993 /* Warn about direct bases that can't be talked about due to
4994 ambiguity. */
4995 warn_about_ambiguous_direct_bases (t);
4997 /* Clean up. */
4998 splay_tree_delete (empty_base_offsets);
5001 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5002 (or C++ class declaration).
5004 For C++, we must handle the building of derived classes.
5005 Also, C++ allows static class members. The way that this is
5006 handled is to keep the field name where it is (as the DECL_NAME
5007 of the field), and place the overloaded decl in the bit position
5008 of the field. layout_record and layout_union will know about this.
5010 More C++ hair: inline functions have text in their
5011 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5012 meaningful tree structure. After the struct has been laid out, set
5013 things up so that this can happen.
5015 And still more: virtual functions. In the case of single inheritance,
5016 when a new virtual function is seen which redefines a virtual function
5017 from the base class, the new virtual function is placed into
5018 the virtual function table at exactly the same address that
5019 it had in the base class. When this is extended to multiple
5020 inheritance, the same thing happens, except that multiple virtual
5021 function tables must be maintained. The first virtual function
5022 table is treated in exactly the same way as in the case of single
5023 inheritance. Additional virtual function tables have different
5024 DELTAs, which tell how to adjust `this' to point to the right thing.
5026 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5028 void
5029 finish_struct_1 (t)
5030 tree t;
5032 tree x;
5033 int vfuns;
5034 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
5035 tree virtuals = NULL_TREE;
5036 int n_fields = 0;
5037 tree vfield;
5038 int empty = 1;
5040 if (COMPLETE_TYPE_P (t))
5042 if (IS_AGGR_TYPE (t))
5043 error ("redefinition of `%#T'", t);
5044 else
5045 abort ();
5046 popclass ();
5047 return;
5050 /* If this type was previously laid out as a forward reference,
5051 make sure we lay it out again. */
5052 TYPE_SIZE (t) = NULL_TREE;
5053 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5054 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5055 vfuns = 0;
5056 CLASSTYPE_RTTI (t) = NULL_TREE;
5058 fixup_inline_methods (t);
5060 /* Do end-of-class semantic processing: checking the validity of the
5061 bases and members and add implicitly generated methods. */
5062 check_bases_and_members (t, &empty);
5064 /* Layout the class itself. */
5065 layout_class_type (t, &empty, &vfuns, &virtuals);
5067 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5068 vfield = TYPE_VFIELD (t);
5069 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5071 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5073 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5074 BINFO_TYPE (primary)),
5075 20010726);
5076 /* The vtable better be at the start. */
5077 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5078 20010726);
5079 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5080 20010726);
5082 vfield = copy_decl (vfield);
5083 DECL_FIELD_CONTEXT (vfield) = t;
5084 TYPE_VFIELD (t) = vfield;
5086 else
5087 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5089 virtuals = modify_all_vtables (t, &vfuns, nreverse (virtuals));
5091 /* If we created a new vtbl pointer for this class, add it to the
5092 list. */
5093 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5094 CLASSTYPE_VFIELDS (t)
5095 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5097 /* If necessary, create the primary vtable for this class. */
5098 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
5100 /* We must enter these virtuals into the table. */
5101 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5102 build_primary_vtable (NULL_TREE, t);
5103 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5104 /* Here we know enough to change the type of our virtual
5105 function table, but we will wait until later this function. */
5106 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5108 /* If this type has basetypes with constructors, then those
5109 constructors might clobber the virtual function table. But
5110 they don't if the derived class shares the exact vtable of the base
5111 class. */
5112 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5114 /* If we didn't need a new vtable, see if we should copy one from
5115 the base. */
5116 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5118 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5120 /* If this class uses a different vtable than its primary base
5121 then when we will need to initialize our vptr after the base
5122 class constructor runs. */
5123 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5124 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5127 if (TYPE_CONTAINS_VPTR_P (t))
5129 if (TYPE_BINFO_VTABLE (t))
5130 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5131 20000116);
5132 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5133 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5134 20000116);
5136 CLASSTYPE_VSIZE (t) = vfuns;
5137 /* Add entries for virtual functions introduced by this class. */
5138 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), virtuals);
5141 finish_struct_bits (t);
5143 /* Complete the rtl for any static member objects of the type we're
5144 working on. */
5145 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5146 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5147 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
5148 DECL_MODE (x) = TYPE_MODE (t);
5150 /* Done with FIELDS...now decide whether to sort these for
5151 faster lookups later.
5153 The C front-end only does this when n_fields > 15. We use
5154 a smaller number because most searches fail (succeeding
5155 ultimately as the search bores through the inheritance
5156 hierarchy), and we want this failure to occur quickly. */
5158 n_fields = count_fields (TYPE_FIELDS (t));
5159 if (n_fields > 7)
5161 tree field_vec = make_tree_vec (n_fields);
5162 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5163 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5164 (int (*)(const void *, const void *))field_decl_cmp);
5165 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5166 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5167 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5170 if (TYPE_HAS_CONSTRUCTOR (t))
5172 tree vfields = CLASSTYPE_VFIELDS (t);
5174 for (vfields = CLASSTYPE_VFIELDS (t);
5175 vfields; vfields = TREE_CHAIN (vfields))
5176 /* Mark the fact that constructor for T could affect anybody
5177 inheriting from T who wants to initialize vtables for
5178 VFIELDS's type. */
5179 if (VF_BINFO_VALUE (vfields))
5180 TREE_ADDRESSABLE (vfields) = 1;
5183 /* Make the rtl for any new vtables we have created, and unmark
5184 the base types we marked. */
5185 finish_vtbls (t);
5187 /* Build the VTT for T. */
5188 build_vtt (t);
5190 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5191 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5192 warning ("`%#T' has virtual functions but non-virtual destructor", t);
5194 complete_vars (t);
5196 if (warn_overloaded_virtual)
5197 warn_hidden (t);
5199 maybe_suppress_debug_info (t);
5201 dump_class_hierarchy (t);
5203 /* Finish debugging output for this type. */
5204 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5207 /* When T was built up, the member declarations were added in reverse
5208 order. Rearrange them to declaration order. */
5210 void
5211 unreverse_member_declarations (t)
5212 tree t;
5214 tree next;
5215 tree prev;
5216 tree x;
5218 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5219 reverse order. Put them in declaration order now. */
5220 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5221 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5223 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5224 reverse order, so we can't just use nreverse. */
5225 prev = NULL_TREE;
5226 for (x = TYPE_FIELDS (t);
5227 x && TREE_CODE (x) != TYPE_DECL;
5228 x = next)
5230 next = TREE_CHAIN (x);
5231 TREE_CHAIN (x) = prev;
5232 prev = x;
5234 if (prev)
5236 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5237 if (prev)
5238 TYPE_FIELDS (t) = prev;
5242 tree
5243 finish_struct (t, attributes)
5244 tree t, attributes;
5246 const char *saved_filename = input_filename;
5247 int saved_lineno = lineno;
5249 /* Now that we've got all the field declarations, reverse everything
5250 as necessary. */
5251 unreverse_member_declarations (t);
5253 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5255 /* Nadger the current location so that diagnostics point to the start of
5256 the struct, not the end. */
5257 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5258 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5260 if (processing_template_decl)
5262 finish_struct_methods (t);
5263 TYPE_SIZE (t) = bitsize_zero_node;
5265 else
5266 finish_struct_1 (t);
5268 input_filename = saved_filename;
5269 lineno = saved_lineno;
5271 TYPE_BEING_DEFINED (t) = 0;
5273 if (current_class_type)
5274 popclass ();
5275 else
5276 error ("trying to finish struct, but kicked out due to previous parse errors");
5278 if (processing_template_decl && at_function_scope_p ())
5279 add_stmt (build_min (TAG_DEFN, t));
5281 return t;
5284 /* Return the dynamic type of INSTANCE, if known.
5285 Used to determine whether the virtual function table is needed
5286 or not.
5288 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5289 of our knowledge of its type. *NONNULL should be initialized
5290 before this function is called. */
5292 static tree
5293 fixed_type_or_null (instance, nonnull, cdtorp)
5294 tree instance;
5295 int *nonnull;
5296 int *cdtorp;
5298 switch (TREE_CODE (instance))
5300 case INDIRECT_REF:
5301 if (POINTER_TYPE_P (TREE_TYPE (instance)))
5302 return NULL_TREE;
5303 else
5304 return fixed_type_or_null (TREE_OPERAND (instance, 0),
5305 nonnull, cdtorp);
5307 case CALL_EXPR:
5308 /* This is a call to a constructor, hence it's never zero. */
5309 if (TREE_HAS_CONSTRUCTOR (instance))
5311 if (nonnull)
5312 *nonnull = 1;
5313 return TREE_TYPE (instance);
5315 return NULL_TREE;
5317 case SAVE_EXPR:
5318 /* This is a call to a constructor, hence it's never zero. */
5319 if (TREE_HAS_CONSTRUCTOR (instance))
5321 if (nonnull)
5322 *nonnull = 1;
5323 return TREE_TYPE (instance);
5325 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5327 case RTL_EXPR:
5328 return NULL_TREE;
5330 case PLUS_EXPR:
5331 case MINUS_EXPR:
5332 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5333 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5334 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5335 /* Propagate nonnull. */
5336 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5337 return NULL_TREE;
5339 case NOP_EXPR:
5340 case CONVERT_EXPR:
5341 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5343 case ADDR_EXPR:
5344 if (nonnull)
5345 *nonnull = 1;
5346 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5348 case COMPONENT_REF:
5349 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5351 case VAR_DECL:
5352 case FIELD_DECL:
5353 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5354 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5356 if (nonnull)
5357 *nonnull = 1;
5358 return TREE_TYPE (TREE_TYPE (instance));
5360 /* fall through... */
5361 case TARGET_EXPR:
5362 case PARM_DECL:
5363 case RESULT_DECL:
5364 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5366 if (nonnull)
5367 *nonnull = 1;
5368 return TREE_TYPE (instance);
5370 else if (instance == current_class_ptr)
5372 if (nonnull)
5373 *nonnull = 1;
5375 /* if we're in a ctor or dtor, we know our type. */
5376 if (DECL_LANG_SPECIFIC (current_function_decl)
5377 && (DECL_CONSTRUCTOR_P (current_function_decl)
5378 || DECL_DESTRUCTOR_P (current_function_decl)))
5380 if (cdtorp)
5381 *cdtorp = 1;
5382 return TREE_TYPE (TREE_TYPE (instance));
5385 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5387 /* Reference variables should be references to objects. */
5388 if (nonnull)
5389 *nonnull = 1;
5391 if (TREE_CODE (instance) == VAR_DECL
5392 && DECL_INITIAL (instance))
5393 return fixed_type_or_null (DECL_INITIAL (instance),
5394 nonnull, cdtorp);
5396 return NULL_TREE;
5398 default:
5399 return NULL_TREE;
5403 /* Return non-zero if the dynamic type of INSTANCE is known, and
5404 equivalent to the static type. We also handle the case where
5405 INSTANCE is really a pointer. Return negative if this is a
5406 ctor/dtor. There the dynamic type is known, but this might not be
5407 the most derived base of the original object, and hence virtual
5408 bases may not be layed out according to this type.
5410 Used to determine whether the virtual function table is needed
5411 or not.
5413 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5414 of our knowledge of its type. *NONNULL should be initialized
5415 before this function is called. */
5418 resolves_to_fixed_type_p (instance, nonnull)
5419 tree instance;
5420 int *nonnull;
5422 tree t = TREE_TYPE (instance);
5423 int cdtorp = 0;
5425 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5426 if (fixed == NULL_TREE)
5427 return 0;
5428 if (POINTER_TYPE_P (t))
5429 t = TREE_TYPE (t);
5430 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5431 return 0;
5432 return cdtorp ? -1 : 1;
5436 void
5437 init_class_processing ()
5439 current_class_depth = 0;
5440 current_class_stack_size = 10;
5441 current_class_stack
5442 = (class_stack_node_t) xmalloc (current_class_stack_size
5443 * sizeof (struct class_stack_node));
5444 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5446 access_default_node = build_int_2 (0, 0);
5447 access_public_node = build_int_2 (ak_public, 0);
5448 access_protected_node = build_int_2 (ak_protected, 0);
5449 access_private_node = build_int_2 (ak_private, 0);
5450 access_default_virtual_node = build_int_2 (4, 0);
5451 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5452 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5453 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5455 ridpointers[(int) RID_PUBLIC] = access_public_node;
5456 ridpointers[(int) RID_PRIVATE] = access_private_node;
5457 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5460 /* Set current scope to NAME. CODE tells us if this is a
5461 STRUCT, UNION, or ENUM environment.
5463 NAME may end up being NULL_TREE if this is an anonymous or
5464 late-bound struct (as in "struct { ... } foo;") */
5466 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5467 appropriate values, found by looking up the type definition of
5468 NAME (as a CODE).
5470 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5471 which can be seen locally to the class. They are shadowed by
5472 any subsequent local declaration (including parameter names).
5474 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5475 which have static meaning (i.e., static members, static
5476 member functions, enum declarations, etc).
5478 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5479 which can be seen locally to the class (as in 1), but
5480 know that we are doing this for declaration purposes
5481 (i.e. friend foo::bar (int)).
5483 So that we may avoid calls to lookup_name, we cache the _TYPE
5484 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5486 For multiple inheritance, we perform a two-pass depth-first search
5487 of the type lattice. The first pass performs a pre-order search,
5488 marking types after the type has had its fields installed in
5489 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5490 unmarks the marked types. If a field or member function name
5491 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5492 that name becomes `error_mark_node'. */
5494 void
5495 pushclass (type, modify)
5496 tree type;
5497 int modify;
5499 type = TYPE_MAIN_VARIANT (type);
5501 /* Make sure there is enough room for the new entry on the stack. */
5502 if (current_class_depth + 1 >= current_class_stack_size)
5504 current_class_stack_size *= 2;
5505 current_class_stack
5506 = (class_stack_node_t) xrealloc (current_class_stack,
5507 current_class_stack_size
5508 * sizeof (struct class_stack_node));
5511 /* Insert a new entry on the class stack. */
5512 current_class_stack[current_class_depth].name = current_class_name;
5513 current_class_stack[current_class_depth].type = current_class_type;
5514 current_class_stack[current_class_depth].access = current_access_specifier;
5515 current_class_stack[current_class_depth].names_used = 0;
5516 current_class_depth++;
5518 /* Now set up the new type. */
5519 current_class_name = TYPE_NAME (type);
5520 if (TREE_CODE (current_class_name) == TYPE_DECL)
5521 current_class_name = DECL_NAME (current_class_name);
5522 current_class_type = type;
5524 /* By default, things in classes are private, while things in
5525 structures or unions are public. */
5526 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5527 ? access_private_node
5528 : access_public_node);
5530 if (previous_class_type != NULL_TREE
5531 && (type != previous_class_type
5532 || !COMPLETE_TYPE_P (previous_class_type))
5533 && current_class_depth == 1)
5535 /* Forcibly remove any old class remnants. */
5536 invalidate_class_lookup_cache ();
5539 /* If we're about to enter a nested class, clear
5540 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5541 if (modify && current_class_depth > 1)
5542 clear_identifier_class_values ();
5544 pushlevel_class ();
5546 if (modify)
5548 if (type != previous_class_type || current_class_depth > 1)
5549 push_class_decls (type);
5550 else
5552 tree item;
5554 /* We are re-entering the same class we just left, so we
5555 don't have to search the whole inheritance matrix to find
5556 all the decls to bind again. Instead, we install the
5557 cached class_shadowed list, and walk through it binding
5558 names and setting up IDENTIFIER_TYPE_VALUEs. */
5559 set_class_shadows (previous_class_values);
5560 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5562 tree id = TREE_PURPOSE (item);
5563 tree decl = TREE_TYPE (item);
5565 push_class_binding (id, decl);
5566 if (TREE_CODE (decl) == TYPE_DECL)
5567 set_identifier_type_value (id, TREE_TYPE (decl));
5569 unuse_fields (type);
5572 storetags (CLASSTYPE_TAGS (type));
5576 /* When we exit a toplevel class scope, we save the
5577 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5578 reenter the class. Here, we've entered some other class, so we
5579 must invalidate our cache. */
5581 void
5582 invalidate_class_lookup_cache ()
5584 tree t;
5586 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5587 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5588 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5590 previous_class_values = NULL_TREE;
5591 previous_class_type = NULL_TREE;
5594 /* Get out of the current class scope. If we were in a class scope
5595 previously, that is the one popped to. */
5597 void
5598 popclass ()
5600 poplevel_class ();
5601 pop_class_decls ();
5603 current_class_depth--;
5604 current_class_name = current_class_stack[current_class_depth].name;
5605 current_class_type = current_class_stack[current_class_depth].type;
5606 current_access_specifier = current_class_stack[current_class_depth].access;
5607 if (current_class_stack[current_class_depth].names_used)
5608 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5611 /* Returns 1 if current_class_type is either T or a nested type of T.
5612 We start looking from 1 because entry 0 is from global scope, and has
5613 no type. */
5616 currently_open_class (t)
5617 tree t;
5619 int i;
5620 if (t == current_class_type)
5621 return 1;
5622 for (i = 1; i < current_class_depth; ++i)
5623 if (current_class_stack [i].type == t)
5624 return 1;
5625 return 0;
5628 /* If either current_class_type or one of its enclosing classes are derived
5629 from T, return the appropriate type. Used to determine how we found
5630 something via unqualified lookup. */
5632 tree
5633 currently_open_derived_class (t)
5634 tree t;
5636 int i;
5638 if (DERIVED_FROM_P (t, current_class_type))
5639 return current_class_type;
5641 for (i = current_class_depth - 1; i > 0; --i)
5642 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5643 return current_class_stack[i].type;
5645 return NULL_TREE;
5648 /* When entering a class scope, all enclosing class scopes' names with
5649 static meaning (static variables, static functions, types and enumerators)
5650 have to be visible. This recursive function calls pushclass for all
5651 enclosing class contexts until global or a local scope is reached.
5652 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5653 formal of the same name. */
5655 void
5656 push_nested_class (type, modify)
5657 tree type;
5658 int modify;
5660 tree context;
5662 /* A namespace might be passed in error cases, like A::B:C. */
5663 if (type == NULL_TREE
5664 || type == error_mark_node
5665 || TREE_CODE (type) == NAMESPACE_DECL
5666 || ! IS_AGGR_TYPE (type)
5667 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5668 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5669 return;
5671 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5673 if (context && CLASS_TYPE_P (context))
5674 push_nested_class (context, 2);
5675 pushclass (type, modify);
5678 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5680 void
5681 pop_nested_class ()
5683 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5685 popclass ();
5686 if (context && CLASS_TYPE_P (context))
5687 pop_nested_class ();
5690 /* Returns the number of extern "LANG" blocks we are nested within. */
5693 current_lang_depth ()
5695 return VARRAY_ACTIVE_SIZE (current_lang_base);
5698 /* Set global variables CURRENT_LANG_NAME to appropriate value
5699 so that behavior of name-mangling machinery is correct. */
5701 void
5702 push_lang_context (name)
5703 tree name;
5705 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5707 if (name == lang_name_cplusplus)
5709 current_lang_name = name;
5711 else if (name == lang_name_java)
5713 current_lang_name = name;
5714 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5715 (See record_builtin_java_type in decl.c.) However, that causes
5716 incorrect debug entries if these types are actually used.
5717 So we re-enable debug output after extern "Java". */
5718 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5719 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5720 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5721 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5722 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5723 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5724 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5725 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5727 else if (name == lang_name_c)
5729 current_lang_name = name;
5731 else
5732 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5735 /* Get out of the current language scope. */
5737 void
5738 pop_lang_context ()
5740 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5741 VARRAY_POP (current_lang_base);
5744 /* Type instantiation routines. */
5746 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5747 matches the TARGET_TYPE. If there is no satisfactory match, return
5748 error_mark_node, and issue an error message if COMPLAIN is
5749 non-zero. Permit pointers to member function if PTRMEM is non-zero.
5750 If TEMPLATE_ONLY, the name of the overloaded function
5751 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5752 template arguments. */
5754 static tree
5755 resolve_address_of_overloaded_function (target_type,
5756 overload,
5757 complain,
5758 ptrmem,
5759 template_only,
5760 explicit_targs)
5761 tree target_type;
5762 tree overload;
5763 int complain;
5764 int ptrmem;
5765 int template_only;
5766 tree explicit_targs;
5768 /* Here's what the standard says:
5770 [over.over]
5772 If the name is a function template, template argument deduction
5773 is done, and if the argument deduction succeeds, the deduced
5774 arguments are used to generate a single template function, which
5775 is added to the set of overloaded functions considered.
5777 Non-member functions and static member functions match targets of
5778 type "pointer-to-function" or "reference-to-function." Nonstatic
5779 member functions match targets of type "pointer-to-member
5780 function;" the function type of the pointer to member is used to
5781 select the member function from the set of overloaded member
5782 functions. If a nonstatic member function is selected, the
5783 reference to the overloaded function name is required to have the
5784 form of a pointer to member as described in 5.3.1.
5786 If more than one function is selected, any template functions in
5787 the set are eliminated if the set also contains a non-template
5788 function, and any given template function is eliminated if the
5789 set contains a second template function that is more specialized
5790 than the first according to the partial ordering rules 14.5.5.2.
5791 After such eliminations, if any, there shall remain exactly one
5792 selected function. */
5794 int is_ptrmem = 0;
5795 int is_reference = 0;
5796 /* We store the matches in a TREE_LIST rooted here. The functions
5797 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5798 interoperability with most_specialized_instantiation. */
5799 tree matches = NULL_TREE;
5800 tree fn;
5802 /* By the time we get here, we should be seeing only real
5803 pointer-to-member types, not the internal POINTER_TYPE to
5804 METHOD_TYPE representation. */
5805 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5806 && (TREE_CODE (TREE_TYPE (target_type))
5807 == METHOD_TYPE)), 0);
5809 if (TREE_CODE (overload) == COMPONENT_REF)
5810 overload = TREE_OPERAND (overload, 1);
5812 /* Check that the TARGET_TYPE is reasonable. */
5813 if (TYPE_PTRFN_P (target_type))
5814 /* This is OK. */;
5815 else if (TYPE_PTRMEMFUNC_P (target_type))
5816 /* This is OK, too. */
5817 is_ptrmem = 1;
5818 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5820 /* This is OK, too. This comes from a conversion to reference
5821 type. */
5822 target_type = build_reference_type (target_type);
5823 is_reference = 1;
5825 else
5827 if (complain)
5828 error ("\
5829 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5830 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5831 return error_mark_node;
5834 /* If we can find a non-template function that matches, we can just
5835 use it. There's no point in generating template instantiations
5836 if we're just going to throw them out anyhow. But, of course, we
5837 can only do this when we don't *need* a template function. */
5838 if (!template_only)
5840 tree fns;
5842 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5844 tree fn = OVL_FUNCTION (fns);
5845 tree fntype;
5847 if (TREE_CODE (fn) == TEMPLATE_DECL)
5848 /* We're not looking for templates just yet. */
5849 continue;
5851 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5852 != is_ptrmem)
5853 /* We're looking for a non-static member, and this isn't
5854 one, or vice versa. */
5855 continue;
5857 /* See if there's a match. */
5858 fntype = TREE_TYPE (fn);
5859 if (is_ptrmem)
5860 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5861 else if (!is_reference)
5862 fntype = build_pointer_type (fntype);
5864 if (can_convert_arg (target_type, fntype, fn))
5865 matches = tree_cons (fn, NULL_TREE, matches);
5869 /* Now, if we've already got a match (or matches), there's no need
5870 to proceed to the template functions. But, if we don't have a
5871 match we need to look at them, too. */
5872 if (!matches)
5874 tree target_fn_type;
5875 tree target_arg_types;
5876 tree target_ret_type;
5877 tree fns;
5879 if (is_ptrmem)
5880 target_fn_type
5881 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5882 else
5883 target_fn_type = TREE_TYPE (target_type);
5884 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5885 target_ret_type = TREE_TYPE (target_fn_type);
5887 /* Never do unification on the 'this' parameter. */
5888 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5889 target_arg_types = TREE_CHAIN (target_arg_types);
5891 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5893 tree fn = OVL_FUNCTION (fns);
5894 tree instantiation;
5895 tree instantiation_type;
5896 tree targs;
5898 if (TREE_CODE (fn) != TEMPLATE_DECL)
5899 /* We're only looking for templates. */
5900 continue;
5902 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5903 != is_ptrmem)
5904 /* We're not looking for a non-static member, and this is
5905 one, or vice versa. */
5906 continue;
5908 /* Try to do argument deduction. */
5909 targs = make_tree_vec (DECL_NTPARMS (fn));
5910 if (fn_type_unification (fn, explicit_targs, targs,
5911 target_arg_types, target_ret_type,
5912 DEDUCE_EXACT, -1) != 0)
5913 /* Argument deduction failed. */
5914 continue;
5916 /* Instantiate the template. */
5917 instantiation = instantiate_template (fn, targs);
5918 if (instantiation == error_mark_node)
5919 /* Instantiation failed. */
5920 continue;
5922 /* See if there's a match. */
5923 instantiation_type = TREE_TYPE (instantiation);
5924 if (is_ptrmem)
5925 instantiation_type =
5926 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5927 else if (!is_reference)
5928 instantiation_type = build_pointer_type (instantiation_type);
5929 if (can_convert_arg (target_type, instantiation_type, instantiation))
5930 matches = tree_cons (instantiation, fn, matches);
5933 /* Now, remove all but the most specialized of the matches. */
5934 if (matches)
5936 tree match = most_specialized_instantiation (matches);
5938 if (match != error_mark_node)
5939 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5943 /* Now we should have exactly one function in MATCHES. */
5944 if (matches == NULL_TREE)
5946 /* There were *no* matches. */
5947 if (complain)
5949 error ("no matches converting function `%D' to type `%#T'",
5950 DECL_NAME (OVL_FUNCTION (overload)),
5951 target_type);
5953 /* print_candidates expects a chain with the functions in
5954 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5955 so why be clever?). */
5956 for (; overload; overload = OVL_NEXT (overload))
5957 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5958 matches);
5960 print_candidates (matches);
5962 return error_mark_node;
5964 else if (TREE_CHAIN (matches))
5966 /* There were too many matches. */
5968 if (complain)
5970 tree match;
5972 error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5973 DECL_NAME (OVL_FUNCTION (overload)),
5974 target_type);
5976 /* Since print_candidates expects the functions in the
5977 TREE_VALUE slot, we flip them here. */
5978 for (match = matches; match; match = TREE_CHAIN (match))
5979 TREE_VALUE (match) = TREE_PURPOSE (match);
5981 print_candidates (matches);
5984 return error_mark_node;
5987 /* Good, exactly one match. Now, convert it to the correct type. */
5988 fn = TREE_PURPOSE (matches);
5990 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5991 && !ptrmem && !flag_ms_extensions)
5993 static int explained;
5995 if (!complain)
5996 return error_mark_node;
5998 pedwarn ("assuming pointer to member `%D'", fn);
5999 if (!explained)
6001 pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6002 explained = 1;
6005 mark_used (fn);
6007 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6008 return build_unary_op (ADDR_EXPR, fn, 0);
6009 else
6011 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6012 will mark the function as addressed, but here we must do it
6013 explicitly. */
6014 cxx_mark_addressable (fn);
6016 return fn;
6020 /* This function will instantiate the type of the expression given in
6021 RHS to match the type of LHSTYPE. If errors exist, then return
6022 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6023 we complain on errors. If we are not complaining, never modify rhs,
6024 as overload resolution wants to try many possible instantiations, in
6025 the hope that at least one will work.
6027 For non-recursive calls, LHSTYPE should be a function, pointer to
6028 function, or a pointer to member function. */
6030 tree
6031 instantiate_type (lhstype, rhs, flags)
6032 tree lhstype, rhs;
6033 tsubst_flags_t flags;
6035 int complain = (flags & tf_error);
6036 int strict = (flags & tf_no_attributes)
6037 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6038 int allow_ptrmem = flags & tf_ptrmem_ok;
6040 flags &= ~tf_ptrmem_ok;
6042 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6044 if (complain)
6045 error ("not enough type information");
6046 return error_mark_node;
6049 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6051 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6052 return rhs;
6053 if (complain)
6054 error ("argument of type `%T' does not match `%T'",
6055 TREE_TYPE (rhs), lhstype);
6056 return error_mark_node;
6059 if (TREE_CODE (rhs) == BASELINK)
6060 rhs = BASELINK_FUNCTIONS (rhs);
6062 /* We don't overwrite rhs if it is an overloaded function.
6063 Copying it would destroy the tree link. */
6064 if (TREE_CODE (rhs) != OVERLOAD)
6065 rhs = copy_node (rhs);
6067 /* This should really only be used when attempting to distinguish
6068 what sort of a pointer to function we have. For now, any
6069 arithmetic operation which is not supported on pointers
6070 is rejected as an error. */
6072 switch (TREE_CODE (rhs))
6074 case TYPE_EXPR:
6075 case CONVERT_EXPR:
6076 case SAVE_EXPR:
6077 case CONSTRUCTOR:
6078 case BUFFER_REF:
6079 abort ();
6080 return error_mark_node;
6082 case INDIRECT_REF:
6083 case ARRAY_REF:
6085 tree new_rhs;
6087 new_rhs = instantiate_type (build_pointer_type (lhstype),
6088 TREE_OPERAND (rhs, 0), flags);
6089 if (new_rhs == error_mark_node)
6090 return error_mark_node;
6092 TREE_TYPE (rhs) = lhstype;
6093 TREE_OPERAND (rhs, 0) = new_rhs;
6094 return rhs;
6097 case NOP_EXPR:
6098 rhs = copy_node (TREE_OPERAND (rhs, 0));
6099 TREE_TYPE (rhs) = unknown_type_node;
6100 return instantiate_type (lhstype, rhs, flags);
6102 case COMPONENT_REF:
6103 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6105 case OFFSET_REF:
6106 rhs = TREE_OPERAND (rhs, 1);
6107 if (BASELINK_P (rhs))
6108 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs),
6109 flags | allow_ptrmem);
6111 /* This can happen if we are forming a pointer-to-member for a
6112 member template. */
6113 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6115 /* Fall through. */
6117 case TEMPLATE_ID_EXPR:
6119 tree fns = TREE_OPERAND (rhs, 0);
6120 tree args = TREE_OPERAND (rhs, 1);
6122 return
6123 resolve_address_of_overloaded_function (lhstype,
6124 fns,
6125 complain,
6126 allow_ptrmem,
6127 /*template_only=*/1,
6128 args);
6131 case OVERLOAD:
6132 return
6133 resolve_address_of_overloaded_function (lhstype,
6134 rhs,
6135 complain,
6136 allow_ptrmem,
6137 /*template_only=*/0,
6138 /*explicit_targs=*/NULL_TREE);
6140 case TREE_LIST:
6141 /* Now we should have a baselink. */
6142 my_friendly_assert (BASELINK_P (rhs), 990412);
6144 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
6146 case CALL_EXPR:
6147 /* This is too hard for now. */
6148 abort ();
6149 return error_mark_node;
6151 case PLUS_EXPR:
6152 case MINUS_EXPR:
6153 case COMPOUND_EXPR:
6154 TREE_OPERAND (rhs, 0)
6155 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6156 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6157 return error_mark_node;
6158 TREE_OPERAND (rhs, 1)
6159 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6160 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6161 return error_mark_node;
6163 TREE_TYPE (rhs) = lhstype;
6164 return rhs;
6166 case MULT_EXPR:
6167 case TRUNC_DIV_EXPR:
6168 case FLOOR_DIV_EXPR:
6169 case CEIL_DIV_EXPR:
6170 case ROUND_DIV_EXPR:
6171 case RDIV_EXPR:
6172 case TRUNC_MOD_EXPR:
6173 case FLOOR_MOD_EXPR:
6174 case CEIL_MOD_EXPR:
6175 case ROUND_MOD_EXPR:
6176 case FIX_ROUND_EXPR:
6177 case FIX_FLOOR_EXPR:
6178 case FIX_CEIL_EXPR:
6179 case FIX_TRUNC_EXPR:
6180 case FLOAT_EXPR:
6181 case NEGATE_EXPR:
6182 case ABS_EXPR:
6183 case MAX_EXPR:
6184 case MIN_EXPR:
6185 case FFS_EXPR:
6187 case BIT_AND_EXPR:
6188 case BIT_IOR_EXPR:
6189 case BIT_XOR_EXPR:
6190 case LSHIFT_EXPR:
6191 case RSHIFT_EXPR:
6192 case LROTATE_EXPR:
6193 case RROTATE_EXPR:
6195 case PREINCREMENT_EXPR:
6196 case PREDECREMENT_EXPR:
6197 case POSTINCREMENT_EXPR:
6198 case POSTDECREMENT_EXPR:
6199 if (complain)
6200 error ("invalid operation on uninstantiated type");
6201 return error_mark_node;
6203 case TRUTH_AND_EXPR:
6204 case TRUTH_OR_EXPR:
6205 case TRUTH_XOR_EXPR:
6206 case LT_EXPR:
6207 case LE_EXPR:
6208 case GT_EXPR:
6209 case GE_EXPR:
6210 case EQ_EXPR:
6211 case NE_EXPR:
6212 case TRUTH_ANDIF_EXPR:
6213 case TRUTH_ORIF_EXPR:
6214 case TRUTH_NOT_EXPR:
6215 if (complain)
6216 error ("not enough type information");
6217 return error_mark_node;
6219 case COND_EXPR:
6220 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6222 if (complain)
6223 error ("not enough type information");
6224 return error_mark_node;
6226 TREE_OPERAND (rhs, 1)
6227 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6228 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6229 return error_mark_node;
6230 TREE_OPERAND (rhs, 2)
6231 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6232 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6233 return error_mark_node;
6235 TREE_TYPE (rhs) = lhstype;
6236 return rhs;
6238 case MODIFY_EXPR:
6239 TREE_OPERAND (rhs, 1)
6240 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6241 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6242 return error_mark_node;
6244 TREE_TYPE (rhs) = lhstype;
6245 return rhs;
6247 case ADDR_EXPR:
6249 if (PTRMEM_OK_P (rhs))
6250 flags |= tf_ptrmem_ok;
6252 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6254 case ENTRY_VALUE_EXPR:
6255 abort ();
6256 return error_mark_node;
6258 case ERROR_MARK:
6259 return error_mark_node;
6261 default:
6262 abort ();
6263 return error_mark_node;
6267 /* Return the name of the virtual function pointer field
6268 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6269 this may have to look back through base types to find the
6270 ultimate field name. (For single inheritance, these could
6271 all be the same name. Who knows for multiple inheritance). */
6273 static tree
6274 get_vfield_name (type)
6275 tree type;
6277 tree binfo = TYPE_BINFO (type);
6278 char *buf;
6280 while (BINFO_BASETYPES (binfo)
6281 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6282 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6283 binfo = BINFO_BASETYPE (binfo, 0);
6285 type = BINFO_TYPE (binfo);
6286 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6287 + TYPE_NAME_LENGTH (type) + 2);
6288 sprintf (buf, VFIELD_NAME_FORMAT,
6289 IDENTIFIER_POINTER (constructor_name (type)));
6290 return get_identifier (buf);
6293 void
6294 print_class_statistics ()
6296 #ifdef GATHER_STATISTICS
6297 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6298 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6299 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6300 n_build_method_call, n_inner_fields_searched);
6301 if (n_vtables)
6303 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6304 n_vtables, n_vtable_searches);
6305 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6306 n_vtable_entries, n_vtable_elems);
6308 #endif
6311 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6312 according to [class]:
6313 The class-name is also inserted
6314 into the scope of the class itself. For purposes of access checking,
6315 the inserted class name is treated as if it were a public member name. */
6317 void
6318 build_self_reference ()
6320 tree name = constructor_name (current_class_type);
6321 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6322 tree saved_cas;
6324 DECL_NONLOCAL (value) = 1;
6325 DECL_CONTEXT (value) = current_class_type;
6326 DECL_ARTIFICIAL (value) = 1;
6328 if (processing_template_decl)
6329 value = push_template_decl (value);
6331 saved_cas = current_access_specifier;
6332 current_access_specifier = access_public_node;
6333 finish_member_declaration (value);
6334 current_access_specifier = saved_cas;
6337 /* Returns 1 if TYPE contains only padding bytes. */
6340 is_empty_class (type)
6341 tree type;
6343 if (type == error_mark_node)
6344 return 0;
6346 if (! IS_AGGR_TYPE (type))
6347 return 0;
6349 return integer_zerop (CLASSTYPE_SIZE (type));
6352 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6353 a *_TYPE node. NODE can also be a local class. */
6355 tree
6356 get_enclosing_class (type)
6357 tree type;
6359 tree node = type;
6361 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6363 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6365 case 'd':
6366 node = DECL_CONTEXT (node);
6367 break;
6369 case 't':
6370 if (node != type)
6371 return node;
6372 node = TYPE_CONTEXT (node);
6373 break;
6375 default:
6376 abort ();
6379 return NULL_TREE;
6382 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6385 is_base_of_enclosing_class (base, type)
6386 tree base, type;
6388 while (type)
6390 if (lookup_base (type, base, ba_any, NULL))
6391 return 1;
6393 type = get_enclosing_class (type);
6395 return 0;
6398 /* Note that NAME was looked up while the current class was being
6399 defined and that the result of that lookup was DECL. */
6401 void
6402 maybe_note_name_used_in_class (name, decl)
6403 tree name;
6404 tree decl;
6406 splay_tree names_used;
6408 /* If we're not defining a class, there's nothing to do. */
6409 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6410 return;
6412 /* If there's already a binding for this NAME, then we don't have
6413 anything to worry about. */
6414 if (IDENTIFIER_CLASS_VALUE (name))
6415 return;
6417 if (!current_class_stack[current_class_depth - 1].names_used)
6418 current_class_stack[current_class_depth - 1].names_used
6419 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6420 names_used = current_class_stack[current_class_depth - 1].names_used;
6422 splay_tree_insert (names_used,
6423 (splay_tree_key) name,
6424 (splay_tree_value) decl);
6427 /* Note that NAME was declared (as DECL) in the current class. Check
6428 to see that the declaration is valid. */
6430 void
6431 note_name_declared_in_class (name, decl)
6432 tree name;
6433 tree decl;
6435 splay_tree names_used;
6436 splay_tree_node n;
6438 /* Look to see if we ever used this name. */
6439 names_used
6440 = current_class_stack[current_class_depth - 1].names_used;
6441 if (!names_used)
6442 return;
6444 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6445 if (n)
6447 /* [basic.scope.class]
6449 A name N used in a class S shall refer to the same declaration
6450 in its context and when re-evaluated in the completed scope of
6451 S. */
6452 error ("declaration of `%#D'", decl);
6453 cp_error_at ("changes meaning of `%D' from `%+#D'",
6454 DECL_NAME (OVL_CURRENT (decl)),
6455 (tree) n->value);
6459 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6460 Secondary vtables are merged with primary vtables; this function
6461 will return the VAR_DECL for the primary vtable. */
6463 tree
6464 get_vtbl_decl_for_binfo (binfo)
6465 tree binfo;
6467 tree decl;
6469 decl = BINFO_VTABLE (binfo);
6470 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6472 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6473 2000403);
6474 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6476 if (decl)
6477 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6478 return decl;
6481 /* Called from get_primary_binfo via dfs_walk. DATA is a TREE_LIST
6482 who's TREE_PURPOSE is the TYPE of the required primary base and
6483 who's TREE_VALUE is a list of candidate binfos that we fill in. */
6485 static tree
6486 dfs_get_primary_binfo (binfo, data)
6487 tree binfo;
6488 void *data;
6490 tree cons = (tree) data;
6491 tree primary_base = TREE_PURPOSE (cons);
6493 if (TREE_VIA_VIRTUAL (binfo)
6494 && same_type_p (BINFO_TYPE (binfo), primary_base))
6495 /* This is the right type of binfo, but it might be an unshared
6496 instance, and the shared instance is later in the dfs walk. We
6497 must keep looking. */
6498 TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6500 return NULL_TREE;
6503 /* Returns the unshared binfo for the primary base of BINFO. Note
6504 that in a complex hierarchy the resulting BINFO may not actually
6505 *be* primary. In particular if the resulting BINFO is a virtual
6506 base, and it occurs elsewhere in the hierarchy, then this
6507 occurrence may not actually be a primary base in the complete
6508 object. Check BINFO_PRIMARY_P to be sure. */
6510 tree
6511 get_primary_binfo (binfo)
6512 tree binfo;
6514 tree primary_base;
6515 tree result = NULL_TREE;
6516 tree virtuals;
6518 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6519 if (!primary_base)
6520 return NULL_TREE;
6522 /* A non-virtual primary base is always a direct base, and easy to
6523 find. */
6524 if (!TREE_VIA_VIRTUAL (primary_base))
6526 int i;
6528 /* Scan the direct basetypes until we find a base with the same
6529 type as the primary base. */
6530 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6532 tree base_binfo = BINFO_BASETYPE (binfo, i);
6534 if (same_type_p (BINFO_TYPE (base_binfo),
6535 BINFO_TYPE (primary_base)))
6536 return base_binfo;
6539 /* We should always find the primary base. */
6540 abort ();
6543 /* For a primary virtual base, we have to scan the entire hierarchy
6544 rooted at BINFO; the virtual base could be an indirect virtual
6545 base. There could be more than one instance of the primary base
6546 in the hierarchy, and if one is the canonical binfo we want that
6547 one. If it exists, it should be the first one we find, but as a
6548 consistency check we find them all and make sure. */
6549 virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6550 dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6551 virtuals = TREE_VALUE (virtuals);
6553 /* We must have found at least one instance. */
6554 my_friendly_assert (virtuals, 20010612);
6556 if (TREE_CHAIN (virtuals))
6558 /* We found more than one instance of the base. We must make
6559 sure that, if one is the canonical one, it is the first one
6560 we found. As the chain is in reverse dfs order, that means
6561 the last on the list. */
6562 tree complete_binfo;
6563 tree canonical;
6565 for (complete_binfo = binfo;
6566 BINFO_INHERITANCE_CHAIN (complete_binfo);
6567 complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6568 continue;
6569 canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6570 BINFO_TYPE (complete_binfo));
6572 for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6574 result = TREE_VALUE (virtuals);
6576 if (canonical == result)
6578 /* This is the unshared instance. Make sure it was the
6579 first one found. */
6580 my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
6581 break;
6585 else
6586 result = TREE_VALUE (virtuals);
6587 return result;
6590 /* If INDENTED_P is zero, indent to INDENT. Return non-zero. */
6592 static int
6593 maybe_indent_hierarchy (stream, indent, indented_p)
6594 FILE *stream;
6595 int indent;
6596 int indented_p;
6598 if (!indented_p)
6599 fprintf (stream, "%*s", indent, "");
6600 return 1;
6603 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6604 dominated by T) to stderr. INDENT should be zero when called from
6605 the top level; it is incremented recursively. */
6607 static void
6608 dump_class_hierarchy_r (stream, flags, t, binfo, indent)
6609 FILE *stream;
6610 int flags;
6611 tree t;
6612 tree binfo;
6613 int indent;
6615 int i;
6616 int indented = 0;
6618 indented = maybe_indent_hierarchy (stream, indent, 0);
6619 fprintf (stream, "%s (0x%lx) ",
6620 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6621 (unsigned long) binfo);
6622 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6623 tree_low_cst (BINFO_OFFSET (binfo), 0));
6624 if (is_empty_class (BINFO_TYPE (binfo)))
6625 fprintf (stream, " empty");
6626 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6627 fprintf (stream, " nearly-empty");
6628 if (TREE_VIA_VIRTUAL (binfo))
6630 tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6632 fprintf (stream, " virtual");
6633 if (canonical == binfo)
6634 fprintf (stream, " canonical");
6635 else
6636 fprintf (stream, " non-canonical");
6638 fprintf (stream, "\n");
6640 indented = 0;
6641 if (BINFO_PRIMARY_BASE_OF (binfo))
6643 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6644 fprintf (stream, " primary-for %s (0x%lx)",
6645 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6646 TFF_PLAIN_IDENTIFIER),
6647 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6649 if (BINFO_LOST_PRIMARY_P (binfo))
6651 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6652 fprintf (stream, " lost-primary");
6654 if (indented)
6655 fprintf (stream, "\n");
6657 if (!(flags & TDF_SLIM))
6659 int indented = 0;
6661 if (BINFO_SUBVTT_INDEX (binfo))
6663 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6664 fprintf (stream, " subvttidx=%s",
6665 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6666 TFF_PLAIN_IDENTIFIER));
6668 if (BINFO_VPTR_INDEX (binfo))
6670 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6671 fprintf (stream, " vptridx=%s",
6672 expr_as_string (BINFO_VPTR_INDEX (binfo),
6673 TFF_PLAIN_IDENTIFIER));
6675 if (BINFO_VPTR_FIELD (binfo))
6677 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6678 fprintf (stream, " vbaseoffset=%s",
6679 expr_as_string (BINFO_VPTR_FIELD (binfo),
6680 TFF_PLAIN_IDENTIFIER));
6682 if (BINFO_VTABLE (binfo))
6684 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6685 fprintf (stream, " vptr=%s",
6686 expr_as_string (BINFO_VTABLE (binfo),
6687 TFF_PLAIN_IDENTIFIER));
6690 if (indented)
6691 fprintf (stream, "\n");
6695 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6696 dump_class_hierarchy_r (stream, flags,
6697 t, BINFO_BASETYPE (binfo, i),
6698 indent + 2);
6701 /* Dump the BINFO hierarchy for T. */
6703 static void
6704 dump_class_hierarchy (t)
6705 tree t;
6707 int flags;
6708 FILE *stream = dump_begin (TDI_class, &flags);
6710 if (!stream)
6711 return;
6713 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6714 fprintf (stream, " size=%lu align=%lu\n",
6715 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6716 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6717 dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
6718 fprintf (stream, "\n");
6719 dump_end (TDI_class, stream);
6722 static void
6723 dump_array (stream, decl)
6724 FILE *stream;
6725 tree decl;
6727 tree inits;
6728 int ix;
6729 HOST_WIDE_INT elt;
6730 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6732 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6733 / BITS_PER_UNIT);
6734 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6735 fprintf (stream, " %s entries",
6736 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6737 TFF_PLAIN_IDENTIFIER));
6738 fprintf (stream, "\n");
6740 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6741 inits; ix++, inits = TREE_CHAIN (inits))
6742 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
6743 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6746 static void
6747 dump_vtable (t, binfo, vtable)
6748 tree t;
6749 tree binfo;
6750 tree vtable;
6752 int flags;
6753 FILE *stream = dump_begin (TDI_class, &flags);
6755 if (!stream)
6756 return;
6758 if (!(flags & TDF_SLIM))
6760 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6762 fprintf (stream, "%s for %s",
6763 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6764 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6765 if (ctor_vtbl_p)
6767 if (!TREE_VIA_VIRTUAL (binfo))
6768 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6769 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6771 fprintf (stream, "\n");
6772 dump_array (stream, vtable);
6773 fprintf (stream, "\n");
6776 dump_end (TDI_class, stream);
6779 static void
6780 dump_vtt (t, vtt)
6781 tree t;
6782 tree vtt;
6784 int flags;
6785 FILE *stream = dump_begin (TDI_class, &flags);
6787 if (!stream)
6788 return;
6790 if (!(flags & TDF_SLIM))
6792 fprintf (stream, "VTT for %s\n",
6793 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6794 dump_array (stream, vtt);
6795 fprintf (stream, "\n");
6798 dump_end (TDI_class, stream);
6801 /* Virtual function table initialization. */
6803 /* Create all the necessary vtables for T and its base classes. */
6805 static void
6806 finish_vtbls (t)
6807 tree t;
6809 tree list;
6810 tree vbase;
6811 int i;
6813 /* We lay out the primary and secondary vtables in one contiguous
6814 vtable. The primary vtable is first, followed by the non-virtual
6815 secondary vtables in inheritance graph order. */
6816 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6817 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6818 TYPE_BINFO (t), t, list);
6820 /* Then come the virtual bases, also in inheritance graph order. */
6821 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6823 tree real_base;
6825 if (!TREE_VIA_VIRTUAL (vbase))
6826 continue;
6828 /* Although we walk in inheritance order, that might not get the
6829 canonical base. */
6830 real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
6832 accumulate_vtbl_inits (real_base, real_base,
6833 TYPE_BINFO (t), t, list);
6836 /* Fill in BINFO_VPTR_FIELD in the immediate binfos for our virtual
6837 base classes, for the benefit of the debugging backends. */
6838 for (i = 0; i < BINFO_N_BASETYPES (TYPE_BINFO (t)); ++i)
6840 tree base = BINFO_BASETYPE (TYPE_BINFO (t), i);
6841 if (TREE_VIA_VIRTUAL (base))
6843 vbase = binfo_for_vbase (BINFO_TYPE (base), t);
6844 BINFO_VPTR_FIELD (base) = BINFO_VPTR_FIELD (vbase);
6848 if (TYPE_BINFO_VTABLE (t))
6849 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6852 /* Initialize the vtable for BINFO with the INITS. */
6854 static void
6855 initialize_vtable (binfo, inits)
6856 tree binfo;
6857 tree inits;
6859 tree decl;
6861 layout_vtable_decl (binfo, list_length (inits));
6862 decl = get_vtbl_decl_for_binfo (binfo);
6863 initialize_array (decl, inits);
6864 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6867 /* Initialize DECL (a declaration for a namespace-scope array) with
6868 the INITS. */
6870 static void
6871 initialize_array (decl, inits)
6872 tree decl;
6873 tree inits;
6875 tree context;
6877 context = DECL_CONTEXT (decl);
6878 DECL_CONTEXT (decl) = NULL_TREE;
6879 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6880 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6881 DECL_CONTEXT (decl) = context;
6884 /* Build the VTT (virtual table table) for T.
6885 A class requires a VTT if it has virtual bases.
6887 This holds
6888 1 - primary virtual pointer for complete object T
6889 2 - secondary VTTs for each direct non-virtual base of T which requires a
6891 3 - secondary virtual pointers for each direct or indirect base of T which
6892 has virtual bases or is reachable via a virtual path from T.
6893 4 - secondary VTTs for each direct or indirect virtual base of T.
6895 Secondary VTTs look like complete object VTTs without part 4. */
6897 static void
6898 build_vtt (t)
6899 tree t;
6901 tree inits;
6902 tree type;
6903 tree vtt;
6904 tree index;
6906 /* Build up the initializers for the VTT. */
6907 inits = NULL_TREE;
6908 index = size_zero_node;
6909 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6911 /* If we didn't need a VTT, we're done. */
6912 if (!inits)
6913 return;
6915 /* Figure out the type of the VTT. */
6916 type = build_index_type (size_int (list_length (inits) - 1));
6917 type = build_cplus_array_type (const_ptr_type_node, type);
6919 /* Now, build the VTT object itself. */
6920 vtt = build_vtable (t, get_vtt_name (t), type);
6921 pushdecl_top_level (vtt);
6922 initialize_array (vtt, inits);
6924 dump_vtt (t, vtt);
6927 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
6928 from within some hierarchy which is inherited from the type of BINFO.
6929 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
6930 BINFO. */
6932 static tree
6933 get_original_base (base_binfo, binfo)
6934 tree base_binfo;
6935 tree binfo;
6937 tree derived;
6938 int ix;
6940 if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
6941 return binfo;
6942 if (TREE_VIA_VIRTUAL (base_binfo))
6943 return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
6944 derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
6946 for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
6947 if (same_type_p (BINFO_TYPE (base_binfo),
6948 BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
6949 return BINFO_BASETYPE (derived, ix);
6950 abort ();
6951 return NULL;
6954 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
6955 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
6956 and CHAIN the vtable pointer for this binfo after construction is
6957 complete. VALUE can also be another BINFO, in which case we recurse. */
6959 static tree
6960 binfo_ctor_vtable (binfo)
6961 tree binfo;
6963 tree vt;
6965 while (1)
6967 vt = BINFO_VTABLE (binfo);
6968 if (TREE_CODE (vt) == TREE_LIST)
6969 vt = TREE_VALUE (vt);
6970 if (TREE_CODE (vt) == TREE_VEC)
6971 binfo = vt;
6972 else
6973 break;
6976 return vt;
6979 /* Recursively build the VTT-initializer for BINFO (which is in the
6980 hierarchy dominated by T). INITS points to the end of the initializer
6981 list to date. INDEX is the VTT index where the next element will be
6982 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
6983 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
6984 for virtual bases of T. When it is not so, we build the constructor
6985 vtables for the BINFO-in-T variant. */
6987 static tree *
6988 build_vtt_inits (binfo, t, inits, index)
6989 tree binfo;
6990 tree t;
6991 tree *inits;
6992 tree *index;
6994 int i;
6995 tree b;
6996 tree init;
6997 tree secondary_vptrs;
6998 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7000 /* We only need VTTs for subobjects with virtual bases. */
7001 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7002 return inits;
7004 /* We need to use a construction vtable if this is not the primary
7005 VTT. */
7006 if (!top_level_p)
7008 build_ctor_vtbl_group (binfo, t);
7010 /* Record the offset in the VTT where this sub-VTT can be found. */
7011 BINFO_SUBVTT_INDEX (binfo) = *index;
7014 /* Add the address of the primary vtable for the complete object. */
7015 init = binfo_ctor_vtable (binfo);
7016 *inits = build_tree_list (NULL_TREE, init);
7017 inits = &TREE_CHAIN (*inits);
7018 if (top_level_p)
7020 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7021 BINFO_VPTR_INDEX (binfo) = *index;
7023 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7025 /* Recursively add the secondary VTTs for non-virtual bases. */
7026 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7028 b = BINFO_BASETYPE (binfo, i);
7029 if (!TREE_VIA_VIRTUAL (b))
7030 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
7031 inits, index);
7034 /* Add secondary virtual pointers for all subobjects of BINFO with
7035 either virtual bases or reachable along a virtual path, except
7036 subobjects that are non-virtual primary bases. */
7037 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7038 TREE_TYPE (secondary_vptrs) = *index;
7039 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7040 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7042 dfs_walk_real (binfo,
7043 dfs_build_secondary_vptr_vtt_inits,
7044 NULL,
7045 dfs_ctor_vtable_bases_queue_p,
7046 secondary_vptrs);
7047 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7048 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7049 secondary_vptrs);
7051 *index = TREE_TYPE (secondary_vptrs);
7053 /* The secondary vptrs come back in reverse order. After we reverse
7054 them, and add the INITS, the last init will be the first element
7055 of the chain. */
7056 secondary_vptrs = TREE_VALUE (secondary_vptrs);
7057 if (secondary_vptrs)
7059 *inits = nreverse (secondary_vptrs);
7060 inits = &TREE_CHAIN (secondary_vptrs);
7061 my_friendly_assert (*inits == NULL_TREE, 20000517);
7064 /* Add the secondary VTTs for virtual bases. */
7065 if (top_level_p)
7066 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7068 tree vbase;
7070 if (!TREE_VIA_VIRTUAL (b))
7071 continue;
7073 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7074 inits = build_vtt_inits (vbase, t, inits, index);
7077 if (!top_level_p)
7079 tree data = tree_cons (t, binfo, NULL_TREE);
7080 VTT_TOP_LEVEL_P (data) = 0;
7081 VTT_MARKED_BINFO_P (data) = 0;
7083 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7084 dfs_ctor_vtable_bases_queue_p,
7085 data);
7088 return inits;
7091 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7092 for the base in most derived. DATA is a TREE_LIST who's
7093 TREE_CHAIN is the type of the base being
7094 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7095 flag of DATA indicates that this is a constructor vtable. The
7096 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7098 static tree
7099 dfs_build_secondary_vptr_vtt_inits (binfo, data)
7100 tree binfo;
7101 void *data;
7103 tree l;
7104 tree t;
7105 tree init;
7106 tree index;
7107 int top_level_p;
7109 l = (tree) data;
7110 t = TREE_CHAIN (l);
7111 top_level_p = VTT_TOP_LEVEL_P (l);
7113 SET_BINFO_MARKED (binfo);
7115 /* We don't care about bases that don't have vtables. */
7116 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7117 return NULL_TREE;
7119 /* We're only interested in proper subobjects of T. */
7120 if (same_type_p (BINFO_TYPE (binfo), t))
7121 return NULL_TREE;
7123 /* We're not interested in non-virtual primary bases. */
7124 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7125 return NULL_TREE;
7127 /* If BINFO has virtual bases or is reachable via a virtual path
7128 from T, it'll have a secondary vptr. */
7129 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7130 && !binfo_via_virtual (binfo, t))
7131 return NULL_TREE;
7133 /* Record the index where this secondary vptr can be found. */
7134 index = TREE_TYPE (l);
7135 if (top_level_p)
7137 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7138 BINFO_VPTR_INDEX (binfo) = index;
7140 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7141 TYPE_SIZE_UNIT (ptr_type_node));
7143 /* Add the initializer for the secondary vptr itself. */
7144 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7146 /* It's a primary virtual base, and this is not the construction
7147 vtable. Find the base this is primary of in the inheritance graph,
7148 and use that base's vtable now. */
7149 while (BINFO_PRIMARY_BASE_OF (binfo))
7150 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7152 init = binfo_ctor_vtable (binfo);
7153 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7155 return NULL_TREE;
7158 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7159 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7160 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7161 hierarchy. */
7163 static tree
7164 dfs_ctor_vtable_bases_queue_p (binfo, data)
7165 tree binfo;
7166 void *data;
7168 if (TREE_VIA_VIRTUAL (binfo))
7169 /* Get the shared version. */
7170 binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
7172 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7173 return NULL_TREE;
7174 return binfo;
7177 /* Called from build_vtt_inits via dfs_walk. After building constructor
7178 vtables and generating the sub-vtt from them, we need to restore the
7179 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7180 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7182 static tree
7183 dfs_fixup_binfo_vtbls (binfo, data)
7184 tree binfo;
7185 void *data;
7187 CLEAR_BINFO_MARKED (binfo);
7189 /* We don't care about bases that don't have vtables. */
7190 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7191 return NULL_TREE;
7193 /* If we scribbled the construction vtable vptr into BINFO, clear it
7194 out now. */
7195 if (BINFO_VTABLE (binfo)
7196 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7197 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7198 == TREE_VALUE ((tree) data)))
7199 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7201 return NULL_TREE;
7204 /* Build the construction vtable group for BINFO which is in the
7205 hierarchy dominated by T. */
7207 static void
7208 build_ctor_vtbl_group (binfo, t)
7209 tree binfo;
7210 tree t;
7212 tree list;
7213 tree type;
7214 tree vtbl;
7215 tree inits;
7216 tree id;
7217 tree vbase;
7219 /* See if we've already created this construction vtable group. */
7220 id = mangle_ctor_vtbl_for_type (t, binfo);
7221 if (IDENTIFIER_GLOBAL_VALUE (id))
7222 return;
7224 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7225 /* Build a version of VTBL (with the wrong type) for use in
7226 constructing the addresses of secondary vtables in the
7227 construction vtable group. */
7228 vtbl = build_vtable (t, id, ptr_type_node);
7229 list = build_tree_list (vtbl, NULL_TREE);
7230 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7231 binfo, t, list);
7233 /* Add the vtables for each of our virtual bases using the vbase in T
7234 binfo. */
7235 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7236 vbase;
7237 vbase = TREE_CHAIN (vbase))
7239 tree b;
7240 tree orig_base;
7242 if (!TREE_VIA_VIRTUAL (vbase))
7243 continue;
7244 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7245 orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7247 accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7249 inits = TREE_VALUE (list);
7251 /* Figure out the type of the construction vtable. */
7252 type = build_index_type (size_int (list_length (inits) - 1));
7253 type = build_cplus_array_type (vtable_entry_type, type);
7254 TREE_TYPE (vtbl) = type;
7256 /* Initialize the construction vtable. */
7257 pushdecl_top_level (vtbl);
7258 initialize_array (vtbl, inits);
7259 dump_vtable (t, binfo, vtbl);
7262 /* Add the vtbl initializers for BINFO (and its bases other than
7263 non-virtual primaries) to the list of INITS. BINFO is in the
7264 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7265 the constructor the vtbl inits should be accumulated for. (If this
7266 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7267 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7268 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7269 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7270 but are not necessarily the same in terms of layout. */
7272 static void
7273 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7274 tree binfo;
7275 tree orig_binfo;
7276 tree rtti_binfo;
7277 tree t;
7278 tree inits;
7280 int i;
7281 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7283 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7284 BINFO_TYPE (orig_binfo)),
7285 20000517);
7287 /* If it doesn't have a vptr, we don't do anything. */
7288 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7289 return;
7291 /* If we're building a construction vtable, we're not interested in
7292 subobjects that don't require construction vtables. */
7293 if (ctor_vtbl_p
7294 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7295 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7296 return;
7298 /* Build the initializers for the BINFO-in-T vtable. */
7299 TREE_VALUE (inits)
7300 = chainon (TREE_VALUE (inits),
7301 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7302 rtti_binfo, t, inits));
7304 /* Walk the BINFO and its bases. We walk in preorder so that as we
7305 initialize each vtable we can figure out at what offset the
7306 secondary vtable lies from the primary vtable. We can't use
7307 dfs_walk here because we need to iterate through bases of BINFO
7308 and RTTI_BINFO simultaneously. */
7309 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7311 tree base_binfo = BINFO_BASETYPE (binfo, i);
7313 /* Skip virtual bases. */
7314 if (TREE_VIA_VIRTUAL (base_binfo))
7315 continue;
7316 accumulate_vtbl_inits (base_binfo,
7317 BINFO_BASETYPE (orig_binfo, i),
7318 rtti_binfo, t,
7319 inits);
7323 /* Called from accumulate_vtbl_inits. Returns the initializers for
7324 the BINFO vtable. */
7326 static tree
7327 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7328 tree binfo;
7329 tree orig_binfo;
7330 tree rtti_binfo;
7331 tree t;
7332 tree l;
7334 tree inits = NULL_TREE;
7335 tree vtbl = NULL_TREE;
7336 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7338 if (ctor_vtbl_p
7339 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7341 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7342 primary virtual base. If it is not the same primary in
7343 the hierarchy of T, we'll need to generate a ctor vtable
7344 for it, to place at its location in T. If it is the same
7345 primary, we still need a VTT entry for the vtable, but it
7346 should point to the ctor vtable for the base it is a
7347 primary for within the sub-hierarchy of RTTI_BINFO.
7349 There are three possible cases:
7351 1) We are in the same place.
7352 2) We are a primary base within a lost primary virtual base of
7353 RTTI_BINFO.
7354 3) We are primary to something not a base of RTTI_BINFO. */
7356 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7357 tree last = NULL_TREE;
7359 /* First, look through the bases we are primary to for RTTI_BINFO
7360 or a virtual base. */
7361 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7363 last = b;
7364 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7365 break;
7367 /* If we run out of primary links, keep looking down our
7368 inheritance chain; we might be an indirect primary. */
7369 if (b == NULL_TREE)
7370 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7371 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7372 break;
7374 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7375 base B and it is a base of RTTI_BINFO, this is case 2. In
7376 either case, we share our vtable with LAST, i.e. the
7377 derived-most base within B of which we are a primary. */
7378 if (b == rtti_binfo
7379 || (b && binfo_for_vbase (BINFO_TYPE (b),
7380 BINFO_TYPE (rtti_binfo))))
7381 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7382 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7383 binfo_ctor_vtable after everything's been set up. */
7384 vtbl = last;
7386 /* Otherwise, this is case 3 and we get our own. */
7388 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7389 return inits;
7391 if (!vtbl)
7393 tree index;
7394 int non_fn_entries;
7396 /* Compute the initializer for this vtable. */
7397 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7398 &non_fn_entries);
7400 /* Figure out the position to which the VPTR should point. */
7401 vtbl = TREE_PURPOSE (l);
7402 vtbl = build1 (ADDR_EXPR,
7403 vtbl_ptr_type_node,
7404 vtbl);
7405 TREE_CONSTANT (vtbl) = 1;
7406 index = size_binop (PLUS_EXPR,
7407 size_int (non_fn_entries),
7408 size_int (list_length (TREE_VALUE (l))));
7409 index = size_binop (MULT_EXPR,
7410 TYPE_SIZE_UNIT (vtable_entry_type),
7411 index);
7412 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7413 TREE_CONSTANT (vtbl) = 1;
7416 if (ctor_vtbl_p)
7417 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7418 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7419 straighten this out. */
7420 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7421 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7422 inits = NULL_TREE;
7423 else
7424 /* For an ordinary vtable, set BINFO_VTABLE. */
7425 BINFO_VTABLE (binfo) = vtbl;
7427 return inits;
7430 /* Construct the initializer for BINFO's virtual function table. BINFO
7431 is part of the hierarchy dominated by T. If we're building a
7432 construction vtable, the ORIG_BINFO is the binfo we should use to
7433 find the actual function pointers to put in the vtable - but they
7434 can be overridden on the path to most-derived in the graph that
7435 ORIG_BINFO belongs. Otherwise,
7436 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7437 BINFO that should be indicated by the RTTI information in the
7438 vtable; it will be a base class of T, rather than T itself, if we
7439 are building a construction vtable.
7441 The value returned is a TREE_LIST suitable for wrapping in a
7442 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7443 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7444 number of non-function entries in the vtable.
7446 It might seem that this function should never be called with a
7447 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7448 base is always subsumed by a derived class vtable. However, when
7449 we are building construction vtables, we do build vtables for
7450 primary bases; we need these while the primary base is being
7451 constructed. */
7453 static tree
7454 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7455 tree binfo;
7456 tree orig_binfo;
7457 tree t;
7458 tree rtti_binfo;
7459 int *non_fn_entries_p;
7461 tree v, b;
7462 tree vfun_inits;
7463 tree vbase;
7464 vtbl_init_data vid;
7466 /* Initialize VID. */
7467 memset (&vid, 0, sizeof (vid));
7468 vid.binfo = binfo;
7469 vid.derived = t;
7470 vid.rtti_binfo = rtti_binfo;
7471 vid.last_init = &vid.inits;
7472 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7473 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7474 /* The first vbase or vcall offset is at index -3 in the vtable. */
7475 vid.index = ssize_int (-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7477 /* Add entries to the vtable for RTTI. */
7478 build_rtti_vtbl_entries (binfo, &vid);
7480 /* Create an array for keeping track of the functions we've
7481 processed. When we see multiple functions with the same
7482 signature, we share the vcall offsets. */
7483 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7484 /* Add the vcall and vbase offset entries. */
7485 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7486 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7487 build_vbase_offset_vtbl_entries. */
7488 for (vbase = CLASSTYPE_VBASECLASSES (t);
7489 vbase;
7490 vbase = TREE_CHAIN (vbase))
7491 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7493 /* If the target requires padding between data entries, add that now. */
7494 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7496 tree cur, *prev;
7498 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7500 tree add = cur;
7501 int i;
7503 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7504 add = tree_cons (NULL_TREE, null_pointer_node, add);
7505 *prev = add;
7509 if (non_fn_entries_p)
7510 *non_fn_entries_p = list_length (vid.inits);
7512 /* Go through all the ordinary virtual functions, building up
7513 initializers. */
7514 vfun_inits = NULL_TREE;
7515 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7517 tree delta;
7518 tree vcall_index;
7519 tree fn;
7520 tree pfn;
7521 tree init = NULL_TREE;
7523 fn = BV_FN (v);
7525 /* If the only definition of this function signature along our
7526 primary base chain is from a lost primary, this vtable slot will
7527 never be used, so just zero it out. This is important to avoid
7528 requiring extra thunks which cannot be generated with the function.
7530 We first check this in update_vtable_entry_for_fn, so we handle
7531 restored primary bases properly; we also need to do it here so we
7532 zero out unused slots in ctor vtables, rather than filling themff
7533 with erroneous values (though harmless, apart from relocation
7534 costs). */
7535 for (b = binfo; ; b = get_primary_binfo (b))
7537 /* We found a defn before a lost primary; go ahead as normal. */
7538 if (look_for_overrides_here (BINFO_TYPE (b), fn))
7539 break;
7541 /* The nearest definition is from a lost primary; clear the
7542 slot. */
7543 if (BINFO_LOST_PRIMARY_P (b))
7545 init = size_zero_node;
7546 break;
7550 if (! init)
7552 /* Pull the offset for `this', and the function to call, out of
7553 the list. */
7554 delta = BV_DELTA (v);
7556 if (BV_USE_VCALL_INDEX_P (v))
7558 vcall_index = BV_VCALL_INDEX (v);
7559 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
7561 else
7562 vcall_index = NULL_TREE;
7564 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7565 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7567 /* You can't call an abstract virtual function; it's abstract.
7568 So, we replace these functions with __pure_virtual. */
7569 if (DECL_PURE_VIRTUAL_P (fn))
7570 fn = abort_fndecl;
7572 /* Take the address of the function, considering it to be of an
7573 appropriate generic type. */
7574 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7575 /* The address of a function can't change. */
7576 TREE_CONSTANT (pfn) = 1;
7578 /* Enter it in the vtable. */
7579 init = build_vtable_entry (delta, vcall_index, pfn);
7582 /* And add it to the chain of initializers. */
7583 if (TARGET_VTABLE_USES_DESCRIPTORS)
7585 int i;
7586 if (init == size_zero_node)
7587 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7588 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7589 else
7590 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7592 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7593 TREE_OPERAND (init, 0),
7594 build_int_2 (i, 0));
7595 TREE_CONSTANT (fdesc) = 1;
7597 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7600 else
7601 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7604 /* The initializers for virtual functions were built up in reverse
7605 order; straighten them out now. */
7606 vfun_inits = nreverse (vfun_inits);
7608 /* The negative offset initializers are also in reverse order. */
7609 vid.inits = nreverse (vid.inits);
7611 /* Chain the two together. */
7612 return chainon (vid.inits, vfun_inits);
7615 /* Adds to vid->inits the initializers for the vbase and vcall
7616 offsets in BINFO, which is in the hierarchy dominated by T. */
7618 static void
7619 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7620 tree binfo;
7621 vtbl_init_data *vid;
7623 tree b;
7625 /* If this is a derived class, we must first create entries
7626 corresponding to the primary base class. */
7627 b = get_primary_binfo (binfo);
7628 if (b)
7629 build_vcall_and_vbase_vtbl_entries (b, vid);
7631 /* Add the vbase entries for this base. */
7632 build_vbase_offset_vtbl_entries (binfo, vid);
7633 /* Add the vcall entries for this base. */
7634 build_vcall_offset_vtbl_entries (binfo, vid);
7637 /* Returns the initializers for the vbase offset entries in the vtable
7638 for BINFO (which is part of the class hierarchy dominated by T), in
7639 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7640 where the next vbase offset will go. */
7642 static void
7643 build_vbase_offset_vtbl_entries (binfo, vid)
7644 tree binfo;
7645 vtbl_init_data *vid;
7647 tree vbase;
7648 tree t;
7649 tree non_primary_binfo;
7651 /* If there are no virtual baseclasses, then there is nothing to
7652 do. */
7653 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7654 return;
7656 t = vid->derived;
7658 /* We might be a primary base class. Go up the inheritance hierarchy
7659 until we find the most derived class of which we are a primary base:
7660 it is the offset of that which we need to use. */
7661 non_primary_binfo = binfo;
7662 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7664 tree b;
7666 /* If we have reached a virtual base, then it must be a primary
7667 base (possibly multi-level) of vid->binfo, or we wouldn't
7668 have called build_vcall_and_vbase_vtbl_entries for it. But it
7669 might be a lost primary, so just skip down to vid->binfo. */
7670 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7672 non_primary_binfo = vid->binfo;
7673 break;
7676 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7677 if (get_primary_binfo (b) != non_primary_binfo)
7678 break;
7679 non_primary_binfo = b;
7682 /* Go through the virtual bases, adding the offsets. */
7683 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7684 vbase;
7685 vbase = TREE_CHAIN (vbase))
7687 tree b;
7688 tree delta;
7690 if (!TREE_VIA_VIRTUAL (vbase))
7691 continue;
7693 /* Find the instance of this virtual base in the complete
7694 object. */
7695 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7697 /* If we've already got an offset for this virtual base, we
7698 don't need another one. */
7699 if (BINFO_VTABLE_PATH_MARKED (b))
7700 continue;
7701 SET_BINFO_VTABLE_PATH_MARKED (b);
7703 /* Figure out where we can find this vbase offset. */
7704 delta = size_binop (MULT_EXPR,
7705 vid->index,
7706 convert (ssizetype,
7707 TYPE_SIZE_UNIT (vtable_entry_type)));
7708 if (vid->primary_vtbl_p)
7709 BINFO_VPTR_FIELD (b) = delta;
7711 if (binfo != TYPE_BINFO (t))
7713 tree orig_vbase;
7715 /* Find the instance of this virtual base in the type of BINFO. */
7716 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7717 BINFO_TYPE (binfo));
7719 /* The vbase offset had better be the same. */
7720 if (!tree_int_cst_equal (delta,
7721 BINFO_VPTR_FIELD (orig_vbase)))
7722 abort ();
7725 /* The next vbase will come at a more negative offset. */
7726 vid->index = size_binop (MINUS_EXPR, vid->index,
7727 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7729 /* The initializer is the delta from BINFO to this virtual base.
7730 The vbase offsets go in reverse inheritance-graph order, and
7731 we are walking in inheritance graph order so these end up in
7732 the right order. */
7733 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7735 *vid->last_init
7736 = build_tree_list (NULL_TREE,
7737 fold (build1 (NOP_EXPR,
7738 vtable_entry_type,
7739 delta)));
7740 vid->last_init = &TREE_CHAIN (*vid->last_init);
7744 /* Adds the initializers for the vcall offset entries in the vtable
7745 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7746 to VID->INITS. */
7748 static void
7749 build_vcall_offset_vtbl_entries (binfo, vid)
7750 tree binfo;
7751 vtbl_init_data *vid;
7753 /* We only need these entries if this base is a virtual base. */
7754 if (!TREE_VIA_VIRTUAL (binfo))
7755 return;
7757 /* We need a vcall offset for each of the virtual functions in this
7758 vtable. For example:
7760 class A { virtual void f (); };
7761 class B1 : virtual public A { virtual void f (); };
7762 class B2 : virtual public A { virtual void f (); };
7763 class C: public B1, public B2 { virtual void f (); };
7765 A C object has a primary base of B1, which has a primary base of A. A
7766 C also has a secondary base of B2, which no longer has a primary base
7767 of A. So the B2-in-C construction vtable needs a secondary vtable for
7768 A, which will adjust the A* to a B2* to call f. We have no way of
7769 knowing what (or even whether) this offset will be when we define B2,
7770 so we store this "vcall offset" in the A sub-vtable and look it up in
7771 a "virtual thunk" for B2::f.
7773 We need entries for all the functions in our primary vtable and
7774 in our non-virtual bases' secondary vtables. */
7775 vid->vbase = binfo;
7776 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7777 add_vcall_offset_vtbl_entries_r (binfo, vid);
7780 /* Build vcall offsets, starting with those for BINFO. */
7782 static void
7783 add_vcall_offset_vtbl_entries_r (binfo, vid)
7784 tree binfo;
7785 vtbl_init_data *vid;
7787 int i;
7788 tree primary_binfo;
7790 /* Don't walk into virtual bases -- except, of course, for the
7791 virtual base for which we are building vcall offsets. Any
7792 primary virtual base will have already had its offsets generated
7793 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7794 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7795 return;
7797 /* If BINFO has a primary base, process it first. */
7798 primary_binfo = get_primary_binfo (binfo);
7799 if (primary_binfo)
7800 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7802 /* Add BINFO itself to the list. */
7803 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7805 /* Scan the non-primary bases of BINFO. */
7806 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7808 tree base_binfo;
7810 base_binfo = BINFO_BASETYPE (binfo, i);
7811 if (base_binfo != primary_binfo)
7812 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7816 /* Called from build_vcall_offset_vtbl_entries_r. */
7818 static void
7819 add_vcall_offset_vtbl_entries_1 (binfo, vid)
7820 tree binfo;
7821 vtbl_init_data* vid;
7823 tree derived_virtuals;
7824 tree base_virtuals;
7825 tree orig_virtuals;
7826 tree binfo_inits;
7827 /* If BINFO is a primary base, the most derived class which has BINFO as
7828 a primary base; otherwise, just BINFO. */
7829 tree non_primary_binfo;
7831 binfo_inits = NULL_TREE;
7833 /* We might be a primary base class. Go up the inheritance hierarchy
7834 until we find the most derived class of which we are a primary base:
7835 it is the BINFO_VIRTUALS there that we need to consider. */
7836 non_primary_binfo = binfo;
7837 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7839 tree b;
7841 /* If we have reached a virtual base, then it must be vid->vbase,
7842 because we ignore other virtual bases in
7843 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7844 base (possibly multi-level) of vid->binfo, or we wouldn't
7845 have called build_vcall_and_vbase_vtbl_entries for it. But it
7846 might be a lost primary, so just skip down to vid->binfo. */
7847 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7849 if (non_primary_binfo != vid->vbase)
7850 abort ();
7851 non_primary_binfo = vid->binfo;
7852 break;
7855 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7856 if (get_primary_binfo (b) != non_primary_binfo)
7857 break;
7858 non_primary_binfo = b;
7861 if (vid->ctor_vtbl_p)
7862 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7863 where rtti_binfo is the most derived type. */
7864 non_primary_binfo = get_original_base
7865 (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
7867 /* Make entries for the rest of the virtuals. */
7868 for (base_virtuals = BINFO_VIRTUALS (binfo),
7869 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7870 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7871 base_virtuals;
7872 base_virtuals = TREE_CHAIN (base_virtuals),
7873 derived_virtuals = TREE_CHAIN (derived_virtuals),
7874 orig_virtuals = TREE_CHAIN (orig_virtuals))
7876 tree orig_fn;
7877 tree fn;
7878 tree base;
7879 tree base_binfo;
7880 size_t i;
7881 tree vcall_offset;
7883 /* Find the declaration that originally caused this function to
7884 be present in BINFO_TYPE (binfo). */
7885 orig_fn = BV_FN (orig_virtuals);
7887 /* When processing BINFO, we only want to generate vcall slots for
7888 function slots introduced in BINFO. So don't try to generate
7889 one if the function isn't even defined in BINFO. */
7890 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7891 continue;
7893 /* Find the overriding function. */
7894 fn = BV_FN (derived_virtuals);
7896 /* If there is already an entry for a function with the same
7897 signature as FN, then we do not need a second vcall offset.
7898 Check the list of functions already present in the derived
7899 class vtable. */
7900 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7902 tree derived_entry;
7904 derived_entry = VARRAY_TREE (vid->fns, i);
7905 if (same_signature_p (BV_FN (derived_entry), fn)
7906 /* We only use one vcall offset for virtual destructors,
7907 even though there are two virtual table entries. */
7908 || (DECL_DESTRUCTOR_P (BV_FN (derived_entry))
7909 && DECL_DESTRUCTOR_P (fn)))
7911 if (!vid->ctor_vtbl_p)
7912 BV_VCALL_INDEX (derived_virtuals)
7913 = BV_VCALL_INDEX (derived_entry);
7914 break;
7917 if (i != VARRAY_ACTIVE_SIZE (vid->fns))
7918 continue;
7920 /* The FN comes from BASE. So, we must calculate the adjustment from
7921 vid->vbase to BASE. We can just look for BASE in the complete
7922 object because we are converting from a virtual base, so if there
7923 were multiple copies, there would not be a unique final overrider
7924 and vid->derived would be ill-formed. */
7925 base = DECL_CONTEXT (fn);
7926 base_binfo = lookup_base (vid->derived, base, ba_any, NULL);
7928 /* Compute the vcall offset. */
7929 /* As mentioned above, the vbase we're working on is a primary base of
7930 vid->binfo. But it might be a lost primary, so its BINFO_OFFSET
7931 might be wrong, so we just use the BINFO_OFFSET from vid->binfo. */
7932 vcall_offset = BINFO_OFFSET (vid->binfo);
7933 vcall_offset = size_diffop (BINFO_OFFSET (base_binfo),
7934 vcall_offset);
7935 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
7936 vcall_offset));
7938 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7939 vid->last_init = &TREE_CHAIN (*vid->last_init);
7941 /* Keep track of the vtable index where this vcall offset can be
7942 found. For a construction vtable, we already made this
7943 annotation when we built the original vtable. */
7944 if (!vid->ctor_vtbl_p)
7945 BV_VCALL_INDEX (derived_virtuals) = vid->index;
7947 /* The next vcall offset will be found at a more negative
7948 offset. */
7949 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7951 /* Keep track of this function. */
7952 VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
7956 /* Return vtbl initializers for the RTTI entries coresponding to the
7957 BINFO's vtable. The RTTI entries should indicate the object given
7958 by VID->rtti_binfo. */
7960 static void
7961 build_rtti_vtbl_entries (binfo, vid)
7962 tree binfo;
7963 vtbl_init_data *vid;
7965 tree b;
7966 tree t;
7967 tree basetype;
7968 tree offset;
7969 tree decl;
7970 tree init;
7972 basetype = BINFO_TYPE (binfo);
7973 t = BINFO_TYPE (vid->rtti_binfo);
7975 /* To find the complete object, we will first convert to our most
7976 primary base, and then add the offset in the vtbl to that value. */
7977 b = binfo;
7978 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
7979 && !BINFO_LOST_PRIMARY_P (b))
7981 tree primary_base;
7983 primary_base = get_primary_binfo (b);
7984 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
7985 b = primary_base;
7987 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
7989 /* The second entry is the address of the typeinfo object. */
7990 if (flag_rtti)
7991 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
7992 else
7993 decl = integer_zero_node;
7995 /* Convert the declaration to a type that can be stored in the
7996 vtable. */
7997 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
7998 TREE_CONSTANT (init) = 1;
7999 *vid->last_init = build_tree_list (NULL_TREE, init);
8000 vid->last_init = &TREE_CHAIN (*vid->last_init);
8002 /* Add the offset-to-top entry. It comes earlier in the vtable that
8003 the the typeinfo entry. Convert the offset to look like a
8004 function pointer, so that we can put it in the vtable. */
8005 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
8006 TREE_CONSTANT (init) = 1;
8007 *vid->last_init = build_tree_list (NULL_TREE, init);
8008 vid->last_init = &TREE_CHAIN (*vid->last_init);
8011 /* Build an entry in the virtual function table. DELTA is the offset
8012 for the `this' pointer. VCALL_INDEX is the vtable index containing
8013 the vcall offset; NULL_TREE if none. ENTRY is the virtual function
8014 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
8015 but it may not actually be a virtual function table pointer. (For
8016 example, it might be the address of the RTTI object, under the new
8017 ABI.) */
8019 static tree
8020 build_vtable_entry (delta, vcall_index, entry)
8021 tree delta;
8022 tree vcall_index;
8023 tree entry;
8025 tree fn = TREE_OPERAND (entry, 0);
8027 if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
8028 && fn != abort_fndecl)
8030 entry = make_thunk (entry, delta, vcall_index);
8031 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
8032 TREE_READONLY (entry) = 1;
8033 TREE_CONSTANT (entry) = 1;
8035 #ifdef GATHER_STATISTICS
8036 n_vtable_entries += 1;
8037 #endif
8038 return entry;