FSF GCC merge 02/23/03
[official-gcc.git] / gcc / cp / class.c
blob89c11e653a002af6e9078a56d056cb9b1de28951
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, 2003 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC 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 GCC 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 GCC; 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 "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "flags.h"
33 #include "rtl.h"
34 #include "output.h"
35 #include "toplev.h"
36 #include "lex.h"
37 #include "target.h"
39 /* The number of nested classes being processed. If we are not in the
40 scope of any class, this is zero. */
42 int current_class_depth;
44 /* In order to deal with nested classes, we keep a stack of classes.
45 The topmost entry is the innermost class, and is the entry at index
46 CURRENT_CLASS_DEPTH */
48 typedef struct class_stack_node {
49 /* The name of the class. */
50 tree name;
52 /* The _TYPE node for the class. */
53 tree type;
55 /* The access specifier pending for new declarations in the scope of
56 this class. */
57 tree access;
59 /* If were defining TYPE, the names used in this class. */
60 splay_tree names_used;
61 }* class_stack_node_t;
63 typedef struct vtbl_init_data_s
65 /* The base for which we're building initializers. */
66 tree binfo;
67 /* The type of the most-derived type. */
68 tree derived;
69 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
70 unless ctor_vtbl_p is true. */
71 tree rtti_binfo;
72 /* The negative-index vtable initializers built up so far. These
73 are in order from least negative index to most negative index. */
74 tree inits;
75 /* The last (i.e., most negative) entry in INITS. */
76 tree* last_init;
77 /* The binfo for the virtual base for which we're building
78 vcall offset initializers. */
79 tree vbase;
80 /* The functions in vbase for which we have already provided vcall
81 offsets. */
82 varray_type fns;
83 /* The vtable index of the next vcall or vbase offset. */
84 tree index;
85 /* Nonzero if we are building the initializer for the primary
86 vtable. */
87 int primary_vtbl_p;
88 /* Nonzero if we are building the initializer for a construction
89 vtable. */
90 int ctor_vtbl_p;
91 /* True when adding vcall offset entries to the vtable. False when
92 merely computing the indices. */
93 bool generate_vcall_entries;
94 } vtbl_init_data;
96 /* The type of a function passed to walk_subobject_offsets. */
97 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
99 /* The stack itself. This is a dynamically resized array. The
100 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
101 static int current_class_stack_size;
102 static class_stack_node_t current_class_stack;
104 /* An array of all local classes present in this translation unit, in
105 declaration order. */
106 varray_type local_classes;
108 static tree get_vfield_name (tree);
109 static void finish_struct_anon (tree);
110 static tree get_vtable_name (tree);
111 static tree get_basefndecls (tree, tree);
112 static int build_primary_vtable (tree, tree);
113 static int build_secondary_vtable (tree);
114 static void finish_vtbls (tree);
115 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
116 static tree delete_duplicate_fields_1 (tree, tree);
117 static void delete_duplicate_fields (tree);
118 static void finish_struct_bits (tree);
119 static int alter_access (tree, tree, tree);
120 static void handle_using_decl (tree, tree);
121 static void check_for_override (tree, tree);
122 static tree dfs_modify_vtables (tree, void *);
123 static tree modify_all_vtables (tree, tree);
124 static void determine_primary_base (tree);
125 static void finish_struct_methods (tree);
126 static void maybe_warn_about_overly_private_class (tree);
127 static int field_decl_cmp (const void *, const void *);
128 static int resort_field_decl_cmp (const void *, const void *);
129 static int method_name_cmp (const void *, const void *);
130 static int resort_method_name_cmp (const void *, const void *);
131 static void add_implicitly_declared_members (tree, int, int, int);
132 static tree fixed_type_or_null (tree, int *, int *);
133 static tree resolve_address_of_overloaded_function (tree, tree, int,
134 int, int, tree);
135 static tree build_vtable_entry_ref (tree, tree, tree);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static tree build_vtbl_initializer (tree, tree, tree, tree, int *);
138 static int count_fields (tree);
139 static int add_fields_to_vec (tree, tree, int);
140 static void check_bitfield_decl (tree);
141 static void check_field_decl (tree, tree, int *, int *, int *, int *);
142 static void check_field_decls (tree, tree *, int *, int *, int *);
143 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
144 static void build_base_fields (record_layout_info, splay_tree, tree *);
145 static void check_methods (tree);
146 static void remove_zero_width_bit_fields (tree);
147 static void check_bases (tree, int *, int *, int *);
148 static void check_bases_and_members (tree);
149 static tree create_vtable_ptr (tree, tree *);
150 static void include_empty_classes (record_layout_info);
151 static void layout_class_type (tree, tree *);
152 static void fixup_pending_inline (tree);
153 static void fixup_inline_methods (tree);
154 static void set_primary_base (tree, tree);
155 static void propagate_binfo_offsets (tree, tree);
156 static void layout_virtual_bases (record_layout_info, splay_tree);
157 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
158 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
159 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
160 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
161 static void add_vcall_offset (tree, tree, vtbl_init_data *);
162 static void layout_vtable_decl (tree, int);
163 static tree dfs_find_final_overrider (tree, void *);
164 static tree dfs_find_final_overrider_post (tree, void *);
165 static tree dfs_find_final_overrider_q (tree, int, void *);
166 static tree find_final_overrider (tree, tree, tree);
167 static int make_new_vtable (tree, tree);
168 static int maybe_indent_hierarchy (FILE *, int, int);
169 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
170 static void dump_class_hierarchy (tree);
171 static void dump_array (FILE *, tree);
172 static void dump_vtable (tree, tree, tree);
173 static void dump_vtt (tree, tree);
174 static tree build_vtable (tree, tree, tree);
175 static void initialize_vtable (tree, tree);
176 static void initialize_array (tree, tree);
177 static void layout_nonempty_base_or_field (record_layout_info,
178 tree, tree, splay_tree);
179 static tree end_of_class (tree, int);
180 static bool layout_empty_base (tree, tree, splay_tree);
181 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree);
182 static tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree,
183 tree);
184 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
185 static void build_vcall_and_vbase_vtbl_entries (tree,
186 vtbl_init_data *);
187 static void mark_primary_bases (tree);
188 static void clone_constructors_and_destructors (tree);
189 static tree build_clone (tree, tree);
190 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
191 static tree copy_virtuals (tree);
192 static void build_ctor_vtbl_group (tree, tree);
193 static void build_vtt (tree);
194 static tree binfo_ctor_vtable (tree);
195 static tree *build_vtt_inits (tree, tree, tree *, tree *);
196 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
197 static tree dfs_ctor_vtable_bases_queue_p (tree, int, void *data);
198 static tree dfs_fixup_binfo_vtbls (tree, void *);
199 static int record_subobject_offset (tree, tree, splay_tree);
200 static int check_subobject_offset (tree, tree, splay_tree);
201 static int walk_subobject_offsets (tree, subobject_offset_fn,
202 tree, splay_tree, tree, int);
203 static void record_subobject_offsets (tree, tree, splay_tree, int);
204 static int layout_conflict_p (tree, tree, splay_tree, int);
205 static int splay_tree_compare_integer_csts (splay_tree_key k1,
206 splay_tree_key k2);
207 static void warn_about_ambiguous_bases (tree);
208 static bool type_requires_array_cookie (tree);
209 static bool contains_empty_class_p (tree);
210 static bool base_derived_from (tree, tree);
211 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
212 static tree end_of_base (tree);
213 static tree get_vcall_index (tree, tree);
215 /* Macros for dfs walking during vtt construction. See
216 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
217 and dfs_fixup_binfo_vtbls. */
218 #define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
219 #define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
221 /* Variables shared between class.c and call.c. */
223 #ifdef GATHER_STATISTICS
224 int n_vtables = 0;
225 int n_vtable_entries = 0;
226 int n_vtable_searches = 0;
227 int n_vtable_elems = 0;
228 int n_convert_harshness = 0;
229 int n_compute_conversion_costs = 0;
230 int n_build_method_call = 0;
231 int n_inner_fields_searched = 0;
232 #endif
234 /* Convert to or from a base subobject. EXPR is an expression of type
235 `A' or `A*', an expression of type `B' or `B*' is returned. To
236 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
237 the B base instance within A. To convert base A to derived B, CODE
238 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
239 In this latter case, A must not be a morally virtual base of B.
240 NONNULL is true if EXPR is known to be non-NULL (this is only
241 needed when EXPR is of pointer type). CV qualifiers are preserved
242 from EXPR. */
244 tree
245 build_base_path (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 if (!want_pointer)
288 /* This must happen before the call to save_expr. */
289 expr = build_unary_op (ADDR_EXPR, expr, 0);
291 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
292 if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
293 expr = save_expr (expr);
295 if (want_pointer && !nonnull)
296 null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
298 offset = BINFO_OFFSET (binfo);
300 if (v_binfo && fixed_type_p <= 0)
302 /* Going via virtual base V_BINFO. We need the static offset
303 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
304 V_BINFO. That offset is an entry in D_BINFO's vtable. */
305 tree v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
306 TREE_TYPE (TREE_TYPE (expr)));
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 (tree array_ref, tree instance, tree idx)
385 tree i, i2, vtable, first_fn, basetype;
387 basetype = TREE_TYPE (instance);
388 if (TREE_CODE (basetype) == REFERENCE_TYPE)
389 basetype = TREE_TYPE (basetype);
391 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
392 first_fn = TYPE_BINFO_VTABLE (basetype);
394 i = fold (build_array_ref (first_fn, idx));
395 i = fold (build_c_cast (ptrdiff_type_node,
396 build_unary_op (ADDR_EXPR, i, 0)));
397 i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
398 i2 = fold (build_c_cast (ptrdiff_type_node,
399 build_unary_op (ADDR_EXPR, i2, 0)));
400 i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
402 if (TREE_CODE (i) != INTEGER_CST)
403 abort ();
405 return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
408 /* Given an object INSTANCE, return an expression which yields the
409 vtable element corresponding to INDEX. There are many special
410 cases for INSTANCE which we take care of here, mainly to avoid
411 creating extra tree nodes when we don't have to. */
413 static tree
414 build_vtbl_ref_1 (tree instance, tree idx)
416 tree aref;
417 tree vtbl = NULL_TREE;
419 /* Try to figure out what a reference refers to, and
420 access its virtual function table directly. */
422 int cdtorp = 0;
423 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
425 tree basetype = TREE_TYPE (instance);
426 if (TREE_CODE (basetype) == REFERENCE_TYPE)
427 basetype = TREE_TYPE (basetype);
429 if (fixed_type && !cdtorp)
431 tree binfo = lookup_base (fixed_type, basetype,
432 ba_ignore|ba_quiet, NULL);
433 if (binfo)
434 vtbl = BINFO_VTABLE (binfo);
437 if (!vtbl)
438 vtbl = build_vfield_ref (instance, basetype);
440 assemble_external (vtbl);
442 aref = build_array_ref (vtbl, idx);
443 TREE_CONSTANT (aref) = 1;
445 return aref;
448 tree
449 build_vtbl_ref (tree instance, tree idx)
451 tree aref = build_vtbl_ref_1 (instance, idx);
453 if (flag_vtable_gc)
454 aref = build_vtable_entry_ref (aref, instance, idx);
456 return aref;
459 /* Given an object INSTANCE, return an expression which yields a
460 function pointer corresponding to vtable element INDEX. */
462 tree
463 build_vfn_ref (tree instance, tree idx)
465 tree aref = build_vtbl_ref_1 (instance, idx);
467 /* When using function descriptors, the address of the
468 vtable entry is treated as a function pointer. */
469 if (TARGET_VTABLE_USES_DESCRIPTORS)
470 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
471 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
473 if (flag_vtable_gc)
474 aref = build_vtable_entry_ref (aref, instance, idx);
476 return aref;
479 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
480 for the given TYPE. */
482 static tree
483 get_vtable_name (tree type)
485 return mangle_vtbl_for_type (type);
488 /* Return an IDENTIFIER_NODE for the name of the virtual table table
489 for TYPE. */
491 tree
492 get_vtt_name (tree type)
494 return mangle_vtt_for_type (type);
497 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
498 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
499 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
501 static tree
502 build_vtable (tree class_type, tree name, tree vtable_type)
504 tree decl;
506 decl = build_lang_decl (VAR_DECL, name, vtable_type);
507 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
508 now to avoid confusion in mangle_decl. */
509 SET_DECL_ASSEMBLER_NAME (decl, name);
510 DECL_CONTEXT (decl) = class_type;
511 DECL_ARTIFICIAL (decl) = 1;
512 TREE_STATIC (decl) = 1;
513 TREE_READONLY (decl) = 1;
514 DECL_VIRTUAL_P (decl) = 1;
515 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
517 import_export_vtable (decl, class_type, 0);
519 return decl;
522 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
523 or even complete. If this does not exist, create it. If COMPLETE is
524 nonzero, then complete the definition of it -- that will render it
525 impossible to actually build the vtable, but is useful to get at those
526 which are known to exist in the runtime. */
528 tree
529 get_vtable_decl (tree type, int complete)
531 tree decl;
533 if (CLASSTYPE_VTABLES (type))
534 return CLASSTYPE_VTABLES (type);
536 decl = build_vtable (type, get_vtable_name (type), void_type_node);
537 CLASSTYPE_VTABLES (type) = decl;
539 /* At one time the vtable info was grabbed 2 words at a time. This
540 fails on sparc unless you have 8-byte alignment. (tiemann) */
541 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
542 DECL_ALIGN (decl));
544 if (complete)
546 DECL_EXTERNAL (decl) = 1;
547 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
550 return decl;
553 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
554 BV_VCALL_INDEX for each entry is cleared. */
556 static tree
557 copy_virtuals (tree binfo)
559 tree copies;
560 tree t;
562 copies = copy_list (BINFO_VIRTUALS (binfo));
563 for (t = copies; t; t = TREE_CHAIN (t))
564 BV_VCALL_INDEX (t) = NULL_TREE;
566 return copies;
569 /* Build the primary virtual function table for TYPE. If BINFO is
570 non-NULL, build the vtable starting with the initial approximation
571 that it is the same as the one which is the head of the association
572 list. Returns a nonzero value if a new vtable is actually
573 created. */
575 static int
576 build_primary_vtable (tree binfo, tree type)
578 tree decl;
579 tree virtuals;
581 decl = get_vtable_decl (type, /*complete=*/0);
583 if (binfo)
585 if (BINFO_NEW_VTABLE_MARKED (binfo))
586 /* We have already created a vtable for this base, so there's
587 no need to do it again. */
588 return 0;
590 virtuals = copy_virtuals (binfo);
591 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
592 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
593 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
595 else
597 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
598 20000118);
599 virtuals = NULL_TREE;
602 #ifdef GATHER_STATISTICS
603 n_vtables += 1;
604 n_vtable_elems += list_length (virtuals);
605 #endif
607 /* Initialize the association list for this type, based
608 on our first approximation. */
609 TYPE_BINFO_VTABLE (type) = decl;
610 TYPE_BINFO_VIRTUALS (type) = virtuals;
611 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
612 return 1;
615 /* Give BINFO a new virtual function table which is initialized
616 with a skeleton-copy of its original initialization. The only
617 entry that changes is the `delta' entry, so we can really
618 share a lot of structure.
620 FOR_TYPE is the most derived type which caused this table to
621 be needed.
623 Returns nonzero if we haven't met BINFO before.
625 The order in which vtables are built (by calling this function) for
626 an object must remain the same, otherwise a binary incompatibility
627 can result. */
629 static int
630 build_secondary_vtable (tree binfo)
632 if (BINFO_NEW_VTABLE_MARKED (binfo))
633 /* We already created a vtable for this base. There's no need to
634 do it again. */
635 return 0;
637 /* Remember that we've created a vtable for this BINFO, so that we
638 don't try to do so again. */
639 SET_BINFO_NEW_VTABLE_MARKED (binfo);
641 /* Make fresh virtual list, so we can smash it later. */
642 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
644 /* Secondary vtables are laid out as part of the same structure as
645 the primary vtable. */
646 BINFO_VTABLE (binfo) = NULL_TREE;
647 return 1;
650 /* Create a new vtable for BINFO which is the hierarchy dominated by
651 T. Return nonzero if we actually created a new vtable. */
653 static int
654 make_new_vtable (tree t, tree binfo)
656 if (binfo == TYPE_BINFO (t))
657 /* In this case, it is *type*'s vtable we are modifying. We start
658 with the approximation that its vtable is that of the
659 immediate base class. */
660 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
661 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
662 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
664 else
665 /* This is our very own copy of `basetype' to play with. Later,
666 we will fill in all the virtual functions that override the
667 virtual functions in these base classes which are not defined
668 by the current type. */
669 return build_secondary_vtable (binfo);
672 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
673 (which is in the hierarchy dominated by T) list FNDECL as its
674 BV_FN. DELTA is the required constant adjustment from the `this'
675 pointer where the vtable entry appears to the `this' required when
676 the function is actually called. */
678 static void
679 modify_vtable_entry (tree t,
680 tree binfo,
681 tree fndecl,
682 tree delta,
683 tree *virtuals)
685 tree v;
687 v = *virtuals;
689 if (fndecl != BV_FN (v)
690 || !tree_int_cst_equal (delta, BV_DELTA (v)))
692 /* We need a new vtable for BINFO. */
693 if (make_new_vtable (t, binfo))
695 /* If we really did make a new vtable, we also made a copy
696 of the BINFO_VIRTUALS list. Now, we have to find the
697 corresponding entry in that list. */
698 *virtuals = BINFO_VIRTUALS (binfo);
699 while (BV_FN (*virtuals) != BV_FN (v))
700 *virtuals = TREE_CHAIN (*virtuals);
701 v = *virtuals;
704 BV_DELTA (v) = delta;
705 BV_VCALL_INDEX (v) = NULL_TREE;
706 BV_FN (v) = fndecl;
711 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
712 the method after the class has already been defined because a
713 declaration for it was seen. (Even though that is erroneous, we
714 add the method for improved error recovery.) */
716 void
717 add_method (tree type, tree method, int error_p)
719 int using = (DECL_CONTEXT (method) != type);
720 int len;
721 int slot;
722 tree method_vec;
723 int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
724 && DECL_TEMPLATE_CONV_FN_P (method));
726 if (!CLASSTYPE_METHOD_VEC (type))
727 /* Make a new method vector. We start with 8 entries. We must
728 allocate at least two (for constructors and destructors), and
729 we're going to end up with an assignment operator at some point
730 as well.
732 We could use a TREE_LIST for now, and convert it to a TREE_VEC
733 in finish_struct, but we would probably waste more memory
734 making the links in the list than we would by over-allocating
735 the size of the vector here. Furthermore, we would complicate
736 all the code that expects this to be a vector. */
737 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
739 method_vec = CLASSTYPE_METHOD_VEC (type);
740 len = TREE_VEC_LENGTH (method_vec);
742 /* Constructors and destructors go in special slots. */
743 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
744 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
745 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
746 slot = CLASSTYPE_DESTRUCTOR_SLOT;
747 else
749 int have_template_convs_p = 0;
751 /* See if we already have an entry with this name. */
752 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
754 tree m = TREE_VEC_ELT (method_vec, slot);
756 if (!m)
757 break;
758 m = OVL_CURRENT (m);
760 if (template_conv_p)
762 have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
763 && DECL_TEMPLATE_CONV_FN_P (m));
765 /* If we need to move things up, see if there's
766 space. */
767 if (!have_template_convs_p)
769 slot = len - 1;
770 if (TREE_VEC_ELT (method_vec, slot))
771 slot++;
773 break;
775 if (DECL_NAME (m) == DECL_NAME (method))
776 break;
779 if (slot == len)
781 /* We need a bigger method vector. */
782 int new_len;
783 tree new_vec;
785 /* In the non-error case, we are processing a class
786 definition. Double the size of the vector to give room
787 for new methods. */
788 if (!error_p)
789 new_len = 2 * len;
790 /* In the error case, the vector is already complete. We
791 don't expect many errors, and the rest of the front-end
792 will get confused if there are empty slots in the vector. */
793 else
794 new_len = len + 1;
796 new_vec = make_tree_vec (new_len);
797 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
798 len * sizeof (tree));
799 len = new_len;
800 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
803 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
805 /* Type conversion operators have to come before ordinary
806 methods; add_conversions depends on this to speed up
807 looking for conversion operators. So, if necessary, we
808 slide some of the vector elements up. In theory, this
809 makes this algorithm O(N^2) but we don't expect many
810 conversion operators. */
811 if (template_conv_p)
812 slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
813 else
814 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
816 tree fn = TREE_VEC_ELT (method_vec, slot);
818 if (!fn)
819 /* There are no more entries in the vector, so we
820 can insert the new conversion operator here. */
821 break;
823 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
824 /* We can insert the new function right at the
825 SLOTth position. */
826 break;
829 if (template_conv_p && have_template_convs_p)
830 /*OK*/;
831 else if (!TREE_VEC_ELT (method_vec, slot))
832 /* There is nothing in the Ith slot, so we can avoid
833 moving anything. */
835 else
837 /* We know the last slot in the vector is empty
838 because we know that at this point there's room
839 for a new function. */
840 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
841 &TREE_VEC_ELT (method_vec, slot),
842 (len - slot - 1) * sizeof (tree));
843 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
848 if (template_class_depth (type))
849 /* TYPE is a template class. Don't issue any errors now; wait
850 until instantiation time to complain. */
852 else
854 tree fns;
856 /* Check to see if we've already got this method. */
857 for (fns = TREE_VEC_ELT (method_vec, slot);
858 fns;
859 fns = OVL_NEXT (fns))
861 tree fn = OVL_CURRENT (fns);
862 tree parms1;
863 tree parms2;
864 bool same = 1;
866 if (TREE_CODE (fn) != TREE_CODE (method))
867 continue;
869 /* [over.load] Member function declarations with the
870 same name and the same parameter types cannot be
871 overloaded if any of them is a static member
872 function declaration.
874 [namespace.udecl] When a using-declaration brings names
875 from a base class into a derived class scope, member
876 functions in the derived class override and/or hide member
877 functions with the same name and parameter types in a base
878 class (rather than conflicting). */
879 parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
880 parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
882 /* Compare the quals on the 'this' parm. Don't compare
883 the whole types, as used functions are treated as
884 coming from the using class in overload resolution. */
885 if (! DECL_STATIC_FUNCTION_P (fn)
886 && ! DECL_STATIC_FUNCTION_P (method)
887 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
888 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
889 same = 0;
891 /* For templates, the template parms must be identical. */
892 if (TREE_CODE (fn) == TEMPLATE_DECL
893 && !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
894 DECL_TEMPLATE_PARMS (method)))
895 same = 0;
897 if (! DECL_STATIC_FUNCTION_P (fn))
898 parms1 = TREE_CHAIN (parms1);
899 if (! DECL_STATIC_FUNCTION_P (method))
900 parms2 = TREE_CHAIN (parms2);
902 if (same && compparms (parms1, parms2)
903 && (!DECL_CONV_FN_P (fn)
904 || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
905 TREE_TYPE (TREE_TYPE (method)))))
907 if (using && DECL_CONTEXT (fn) == type)
908 /* Defer to the local function. */
909 return;
910 else
912 cp_error_at ("`%#D' and `%#D' cannot be overloaded",
913 method, fn, method);
915 /* We don't call duplicate_decls here to merge
916 the declarations because that will confuse
917 things if the methods have inline
918 definitions. In particular, we will crash
919 while processing the definitions. */
920 return;
926 /* Actually insert the new method. */
927 TREE_VEC_ELT (method_vec, slot)
928 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
930 /* Add the new binding. */
931 if (!DECL_CONSTRUCTOR_P (method)
932 && !DECL_DESTRUCTOR_P (method))
933 push_class_level_binding (DECL_NAME (method),
934 TREE_VEC_ELT (method_vec, slot));
937 /* Subroutines of finish_struct. */
939 /* Look through the list of fields for this struct, deleting
940 duplicates as we go. This must be recursive to handle
941 anonymous unions.
943 FIELD is the field which may not appear anywhere in FIELDS.
944 FIELD_PTR, if non-null, is the starting point at which
945 chained deletions may take place.
946 The value returned is the first acceptable entry found
947 in FIELDS.
949 Note that anonymous fields which are not of UNION_TYPE are
950 not duplicates, they are just anonymous fields. This happens
951 when we have unnamed bitfields, for example. */
953 static tree
954 delete_duplicate_fields_1 (tree field, tree fields)
956 tree x;
957 tree prev = 0;
958 if (DECL_NAME (field) == 0)
960 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
961 return fields;
963 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
964 fields = delete_duplicate_fields_1 (x, fields);
965 return fields;
967 else
969 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
971 if (DECL_NAME (x) == 0)
973 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
974 continue;
975 TYPE_FIELDS (TREE_TYPE (x))
976 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
977 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
979 if (prev == 0)
980 fields = TREE_CHAIN (fields);
981 else
982 TREE_CHAIN (prev) = TREE_CHAIN (x);
985 else if (TREE_CODE (field) == USING_DECL)
986 /* A using declaration is allowed to appear more than
987 once. We'll prune these from the field list later, and
988 handle_using_decl will complain about invalid multiple
989 uses. */
991 else if (DECL_NAME (field) == DECL_NAME (x))
993 if (TREE_CODE (field) == CONST_DECL
994 && TREE_CODE (x) == CONST_DECL)
995 cp_error_at ("duplicate enum value `%D'", x);
996 else if (TREE_CODE (field) == CONST_DECL
997 || TREE_CODE (x) == CONST_DECL)
998 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1000 else if (DECL_DECLARES_TYPE_P (field)
1001 && DECL_DECLARES_TYPE_P (x))
1003 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1004 continue;
1005 cp_error_at ("duplicate nested type `%D'", x);
1007 else if (DECL_DECLARES_TYPE_P (field)
1008 || DECL_DECLARES_TYPE_P (x))
1010 /* Hide tag decls. */
1011 if ((TREE_CODE (field) == TYPE_DECL
1012 && DECL_ARTIFICIAL (field))
1013 || (TREE_CODE (x) == TYPE_DECL
1014 && DECL_ARTIFICIAL (x)))
1015 continue;
1016 cp_error_at ("duplicate field `%D' (as type and non-type)",
1019 else
1020 cp_error_at ("duplicate member `%D'", x);
1021 if (prev == 0)
1022 fields = TREE_CHAIN (fields);
1023 else
1024 TREE_CHAIN (prev) = TREE_CHAIN (x);
1028 return fields;
1031 static void
1032 delete_duplicate_fields (tree fields)
1034 tree x;
1035 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1036 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1039 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1040 legit, otherwise return 0. */
1042 static int
1043 alter_access (tree t, tree fdecl, tree access)
1045 tree elem;
1047 if (!DECL_LANG_SPECIFIC (fdecl))
1048 retrofit_lang_decl (fdecl);
1050 if (DECL_DISCRIMINATOR_P (fdecl))
1051 abort ();
1053 elem = purpose_member (t, DECL_ACCESS (fdecl));
1054 if (elem)
1056 if (TREE_VALUE (elem) != access)
1058 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1059 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1060 else
1061 error ("conflicting access specifications for field `%s', ignored",
1062 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1064 else
1066 /* They're changing the access to the same thing they changed
1067 it to before. That's OK. */
1071 else
1073 enforce_access (t, fdecl);
1074 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1075 return 1;
1077 return 0;
1080 /* Process the USING_DECL, which is a member of T. */
1082 static void
1083 handle_using_decl (tree using_decl, tree t)
1085 tree ctype = DECL_INITIAL (using_decl);
1086 tree name = DECL_NAME (using_decl);
1087 tree access
1088 = TREE_PRIVATE (using_decl) ? access_private_node
1089 : TREE_PROTECTED (using_decl) ? access_protected_node
1090 : access_public_node;
1091 tree fdecl, binfo;
1092 tree flist = NULL_TREE;
1093 tree old_value;
1095 binfo = lookup_base (t, ctype, ba_any, NULL);
1096 if (! binfo)
1098 error_not_base_type (t, ctype);
1099 return;
1102 if (constructor_name_p (name, ctype))
1104 cp_error_at ("`%D' names constructor", using_decl);
1105 return;
1107 if (constructor_name_p (name, t))
1109 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1110 return;
1113 fdecl = lookup_member (binfo, name, 0, false);
1115 if (!fdecl)
1117 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1118 return;
1121 if (BASELINK_P (fdecl))
1122 /* Ignore base type this came from. */
1123 fdecl = BASELINK_FUNCTIONS (fdecl);
1125 old_value = IDENTIFIER_CLASS_VALUE (name);
1126 if (old_value)
1128 if (is_overloaded_fn (old_value))
1129 old_value = OVL_CURRENT (old_value);
1131 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1132 /* OK */;
1133 else
1134 old_value = NULL_TREE;
1137 if (is_overloaded_fn (fdecl))
1138 flist = fdecl;
1140 if (! old_value)
1142 else if (is_overloaded_fn (old_value))
1144 if (flist)
1145 /* It's OK to use functions from a base when there are functions with
1146 the same name already present in the current class. */;
1147 else
1149 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1150 cp_error_at (" because of local method `%#D' with same name",
1151 OVL_CURRENT (old_value));
1152 return;
1155 else if (!DECL_ARTIFICIAL (old_value))
1157 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1158 cp_error_at (" because of local member `%#D' with same name", old_value);
1159 return;
1162 /* Make type T see field decl FDECL with access ACCESS.*/
1163 if (flist)
1164 for (; flist; flist = OVL_NEXT (flist))
1166 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1167 alter_access (t, OVL_CURRENT (flist), access);
1169 else
1170 alter_access (t, fdecl, access);
1173 /* Run through the base clases of T, updating
1174 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1175 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1176 the bases. */
1178 static void
1179 check_bases (tree t,
1180 int* cant_have_default_ctor_p,
1181 int* cant_have_const_ctor_p,
1182 int* no_const_asn_ref_p)
1184 int n_baseclasses;
1185 int i;
1186 int seen_non_virtual_nearly_empty_base_p;
1187 tree binfos;
1189 binfos = TYPE_BINFO_BASETYPES (t);
1190 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1191 seen_non_virtual_nearly_empty_base_p = 0;
1193 /* An aggregate cannot have baseclasses. */
1194 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1196 for (i = 0; i < n_baseclasses; ++i)
1198 tree base_binfo;
1199 tree basetype;
1201 /* Figure out what base we're looking at. */
1202 base_binfo = TREE_VEC_ELT (binfos, i);
1203 basetype = TREE_TYPE (base_binfo);
1205 /* If the type of basetype is incomplete, then we already
1206 complained about that fact (and we should have fixed it up as
1207 well). */
1208 if (!COMPLETE_TYPE_P (basetype))
1210 int j;
1211 /* The base type is of incomplete type. It is
1212 probably best to pretend that it does not
1213 exist. */
1214 if (i == n_baseclasses-1)
1215 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1216 TREE_VEC_LENGTH (binfos) -= 1;
1217 n_baseclasses -= 1;
1218 for (j = i; j+1 < n_baseclasses; j++)
1219 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1220 continue;
1223 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1224 here because the case of virtual functions but non-virtual
1225 dtor is handled in finish_struct_1. */
1226 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1227 && TYPE_HAS_DESTRUCTOR (basetype))
1228 warning ("base class `%#T' has a non-virtual destructor",
1229 basetype);
1231 /* If the base class doesn't have copy constructors or
1232 assignment operators that take const references, then the
1233 derived class cannot have such a member automatically
1234 generated. */
1235 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1236 *cant_have_const_ctor_p = 1;
1237 if (TYPE_HAS_ASSIGN_REF (basetype)
1238 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1239 *no_const_asn_ref_p = 1;
1240 /* Similarly, if the base class doesn't have a default
1241 constructor, then the derived class won't have an
1242 automatically generated default constructor. */
1243 if (TYPE_HAS_CONSTRUCTOR (basetype)
1244 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1246 *cant_have_default_ctor_p = 1;
1247 if (! TYPE_HAS_CONSTRUCTOR (t))
1248 pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1249 basetype);
1252 if (TREE_VIA_VIRTUAL (base_binfo))
1253 /* A virtual base does not effect nearly emptiness. */
1255 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1257 if (seen_non_virtual_nearly_empty_base_p)
1258 /* And if there is more than one nearly empty base, then the
1259 derived class is not nearly empty either. */
1260 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1261 else
1262 /* Remember we've seen one. */
1263 seen_non_virtual_nearly_empty_base_p = 1;
1265 else if (!is_empty_class (basetype))
1266 /* If the base class is not empty or nearly empty, then this
1267 class cannot be nearly empty. */
1268 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1270 /* A lot of properties from the bases also apply to the derived
1271 class. */
1272 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1273 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1274 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1275 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1276 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1277 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1278 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1279 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1280 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1281 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1282 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1283 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1287 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1288 dominated by TYPE that are primary bases. */
1290 static void
1291 mark_primary_bases (tree type)
1293 tree binfo;
1295 /* Walk the bases in inheritance graph order. */
1296 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1298 tree base_binfo = get_primary_binfo (binfo);
1300 if (!base_binfo)
1301 /* Not a dynamic base. */;
1302 else if (BINFO_PRIMARY_P (base_binfo))
1303 BINFO_LOST_PRIMARY_P (binfo) = 1;
1304 else
1306 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1307 /* A virtual binfo might have been copied from within
1308 another hierarchy. As we're about to use it as a primary
1309 base, make sure the offsets match. */
1310 if (TREE_VIA_VIRTUAL (base_binfo))
1312 tree delta = size_diffop (convert (ssizetype,
1313 BINFO_OFFSET (binfo)),
1314 convert (ssizetype,
1315 BINFO_OFFSET (base_binfo)));
1317 propagate_binfo_offsets (base_binfo, delta);
1323 /* Make the BINFO the primary base of T. */
1325 static void
1326 set_primary_base (tree t, tree binfo)
1328 tree basetype;
1330 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1331 basetype = BINFO_TYPE (binfo);
1332 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1333 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1334 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1337 /* Determine the primary class for T. */
1339 static void
1340 determine_primary_base (tree t)
1342 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1343 tree vbases;
1344 tree type_binfo;
1346 /* If there are no baseclasses, there is certainly no primary base. */
1347 if (n_baseclasses == 0)
1348 return;
1350 type_binfo = TYPE_BINFO (t);
1352 for (i = 0; i < n_baseclasses; i++)
1354 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1355 tree basetype = BINFO_TYPE (base_binfo);
1357 if (TYPE_CONTAINS_VPTR_P (basetype))
1359 /* We prefer a non-virtual base, although a virtual one will
1360 do. */
1361 if (TREE_VIA_VIRTUAL (base_binfo))
1362 continue;
1364 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1366 set_primary_base (t, base_binfo);
1367 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1369 else
1371 tree vfields;
1373 /* Only add unique vfields, and flatten them out as we go. */
1374 for (vfields = CLASSTYPE_VFIELDS (basetype);
1375 vfields;
1376 vfields = TREE_CHAIN (vfields))
1377 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1378 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1379 CLASSTYPE_VFIELDS (t)
1380 = tree_cons (base_binfo,
1381 VF_BASETYPE_VALUE (vfields),
1382 CLASSTYPE_VFIELDS (t));
1387 if (!TYPE_VFIELD (t))
1388 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1390 /* Find the indirect primary bases - those virtual bases which are primary
1391 bases of something else in this hierarchy. */
1392 for (vbases = CLASSTYPE_VBASECLASSES (t);
1393 vbases;
1394 vbases = TREE_CHAIN (vbases))
1396 tree vbase_binfo = TREE_VALUE (vbases);
1398 /* See if this virtual base is an indirect primary base. To be so,
1399 it must be a primary base within the hierarchy of one of our
1400 direct bases. */
1401 for (i = 0; i < n_baseclasses; ++i)
1403 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1404 tree v;
1406 for (v = CLASSTYPE_VBASECLASSES (basetype);
1408 v = TREE_CHAIN (v))
1410 tree base_vbase = TREE_VALUE (v);
1412 if (BINFO_PRIMARY_P (base_vbase)
1413 && same_type_p (BINFO_TYPE (base_vbase),
1414 BINFO_TYPE (vbase_binfo)))
1416 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1417 break;
1421 /* If we've discovered that this virtual base is an indirect
1422 primary base, then we can move on to the next virtual
1423 base. */
1424 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1425 break;
1429 /* A "nearly-empty" virtual base class can be the primary base
1430 class, if no non-virtual polymorphic base can be found. */
1431 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1433 /* If not NULL, this is the best primary base candidate we have
1434 found so far. */
1435 tree candidate = NULL_TREE;
1436 tree base_binfo;
1438 /* Loop over the baseclasses. */
1439 for (base_binfo = TYPE_BINFO (t);
1440 base_binfo;
1441 base_binfo = TREE_CHAIN (base_binfo))
1443 tree basetype = BINFO_TYPE (base_binfo);
1445 if (TREE_VIA_VIRTUAL (base_binfo)
1446 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1448 /* If this is not an indirect primary base, then it's
1449 definitely our primary base. */
1450 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1452 candidate = base_binfo;
1453 break;
1456 /* If this is an indirect primary base, it still could be
1457 our primary base -- unless we later find there's another
1458 nearly-empty virtual base that isn't an indirect
1459 primary base. */
1460 if (!candidate)
1461 candidate = base_binfo;
1465 /* If we've got a primary base, use it. */
1466 if (candidate)
1468 set_primary_base (t, candidate);
1469 CLASSTYPE_VFIELDS (t)
1470 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1474 /* Mark the primary base classes at this point. */
1475 mark_primary_bases (t);
1478 /* Set memoizing fields and bits of T (and its variants) for later
1479 use. */
1481 static void
1482 finish_struct_bits (tree t)
1484 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1486 /* Fix up variants (if any). */
1487 tree variants = TYPE_NEXT_VARIANT (t);
1488 while (variants)
1490 /* These fields are in the _TYPE part of the node, not in
1491 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1492 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1493 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1494 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1495 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1496 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1498 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1499 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1500 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1501 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1502 /* Copy whatever these are holding today. */
1503 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1504 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1505 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1506 TYPE_SIZE (variants) = TYPE_SIZE (t);
1507 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1508 variants = TYPE_NEXT_VARIANT (variants);
1511 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1512 /* For a class w/o baseclasses, `finish_struct' has set
1513 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1514 definition). Similarly for a class whose base classes do not
1515 have vtables. When neither of these is true, we might have
1516 removed abstract virtuals (by providing a definition), added
1517 some (by declaring new ones), or redeclared ones from a base
1518 class. We need to recalculate what's really an abstract virtual
1519 at this point (by looking in the vtables). */
1520 get_pure_virtuals (t);
1522 if (n_baseclasses)
1524 /* Notice whether this class has type conversion functions defined. */
1525 tree binfo = TYPE_BINFO (t);
1526 tree binfos = BINFO_BASETYPES (binfo);
1527 tree basetype;
1529 for (i = n_baseclasses-1; i >= 0; i--)
1531 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1533 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1537 /* If this type has a copy constructor or a destructor, force its mode to
1538 be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero. This
1539 will cause it to be passed by invisible reference and prevent it from
1540 being returned in a register. */
1541 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1543 tree variants;
1544 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1545 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1547 TYPE_MODE (variants) = BLKmode;
1548 TREE_ADDRESSABLE (variants) = 1;
1553 /* Issue warnings about T having private constructors, but no friends,
1554 and so forth.
1556 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1557 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1558 non-private static member functions. */
1560 static void
1561 maybe_warn_about_overly_private_class (tree t)
1563 int has_member_fn = 0;
1564 int has_nonprivate_method = 0;
1565 tree fn;
1567 if (!warn_ctor_dtor_privacy
1568 /* If the class has friends, those entities might create and
1569 access instances, so we should not warn. */
1570 || (CLASSTYPE_FRIEND_CLASSES (t)
1571 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1572 /* We will have warned when the template was declared; there's
1573 no need to warn on every instantiation. */
1574 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1575 /* There's no reason to even consider warning about this
1576 class. */
1577 return;
1579 /* We only issue one warning, if more than one applies, because
1580 otherwise, on code like:
1582 class A {
1583 // Oops - forgot `public:'
1584 A();
1585 A(const A&);
1586 ~A();
1589 we warn several times about essentially the same problem. */
1591 /* Check to see if all (non-constructor, non-destructor) member
1592 functions are private. (Since there are no friends or
1593 non-private statics, we can't ever call any of the private member
1594 functions.) */
1595 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1596 /* We're not interested in compiler-generated methods; they don't
1597 provide any way to call private members. */
1598 if (!DECL_ARTIFICIAL (fn))
1600 if (!TREE_PRIVATE (fn))
1602 if (DECL_STATIC_FUNCTION_P (fn))
1603 /* A non-private static member function is just like a
1604 friend; it can create and invoke private member
1605 functions, and be accessed without a class
1606 instance. */
1607 return;
1609 has_nonprivate_method = 1;
1610 /* Keep searching for a static member function. */
1612 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1613 has_member_fn = 1;
1616 if (!has_nonprivate_method && has_member_fn)
1618 /* There are no non-private methods, and there's at least one
1619 private member function that isn't a constructor or
1620 destructor. (If all the private members are
1621 constructors/destructors we want to use the code below that
1622 issues error messages specifically referring to
1623 constructors/destructors.) */
1624 int i;
1625 tree binfo = TYPE_BINFO (t);
1627 for (i = 0; i < BINFO_N_BASETYPES (binfo); i++)
1628 if (BINFO_BASEACCESS (binfo, i) != access_private_node)
1630 has_nonprivate_method = 1;
1631 break;
1633 if (!has_nonprivate_method)
1635 warning ("all member functions in class `%T' are private", t);
1636 return;
1640 /* Even if some of the member functions are non-private, the class
1641 won't be useful for much if all the constructors or destructors
1642 are private: such an object can never be created or destroyed. */
1643 if (TYPE_HAS_DESTRUCTOR (t))
1645 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1647 if (TREE_PRIVATE (dtor))
1649 warning ("`%#T' only defines a private destructor and has no friends",
1651 return;
1655 if (TYPE_HAS_CONSTRUCTOR (t))
1657 int nonprivate_ctor = 0;
1659 /* If a non-template class does not define a copy
1660 constructor, one is defined for it, enabling it to avoid
1661 this warning. For a template class, this does not
1662 happen, and so we would normally get a warning on:
1664 template <class T> class C { private: C(); };
1666 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1667 complete non-template or fully instantiated classes have this
1668 flag set. */
1669 if (!TYPE_HAS_INIT_REF (t))
1670 nonprivate_ctor = 1;
1671 else
1672 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1674 fn = OVL_NEXT (fn))
1676 tree ctor = OVL_CURRENT (fn);
1677 /* Ideally, we wouldn't count copy constructors (or, in
1678 fact, any constructor that takes an argument of the
1679 class type as a parameter) because such things cannot
1680 be used to construct an instance of the class unless
1681 you already have one. But, for now at least, we're
1682 more generous. */
1683 if (! TREE_PRIVATE (ctor))
1685 nonprivate_ctor = 1;
1686 break;
1690 if (nonprivate_ctor == 0)
1692 warning ("`%#T' only defines private constructors and has no friends",
1694 return;
1699 /* Function to help qsort sort FIELD_DECLs by name order. */
1701 static int
1702 field_decl_cmp (const void* x_p, const void* y_p)
1704 const tree *const x = x_p;
1705 const tree *const y = y_p;
1706 if (DECL_NAME (*x) == DECL_NAME (*y))
1707 /* A nontype is "greater" than a type. */
1708 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1709 if (DECL_NAME (*x) == NULL_TREE)
1710 return -1;
1711 if (DECL_NAME (*y) == NULL_TREE)
1712 return 1;
1713 if (DECL_NAME (*x) < DECL_NAME (*y))
1714 return -1;
1715 return 1;
1718 static struct {
1719 gt_pointer_operator new_value;
1720 void *cookie;
1721 } resort_data;
1723 /* This routine compares two fields like field_decl_cmp but using the
1724 pointer operator in resort_data. */
1726 static int
1727 resort_field_decl_cmp (const void* x_p, const void* y_p)
1729 const tree *const x = x_p;
1730 const tree *const y = y_p;
1732 if (DECL_NAME (*x) == DECL_NAME (*y))
1733 /* A nontype is "greater" than a type. */
1734 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1735 if (DECL_NAME (*x) == NULL_TREE)
1736 return -1;
1737 if (DECL_NAME (*y) == NULL_TREE)
1738 return 1;
1740 tree d1 = DECL_NAME (*x);
1741 tree d2 = DECL_NAME (*y);
1742 resort_data.new_value (&d1, resort_data.cookie);
1743 resort_data.new_value (&d2, resort_data.cookie);
1744 if (d1 < d2)
1745 return -1;
1747 return 1;
1750 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
1752 void
1753 resort_sorted_fields (void* obj,
1754 void* orig_obj ATTRIBUTE_UNUSED ,
1755 gt_pointer_operator new_value,
1756 void* cookie)
1758 tree sf = obj;
1759 resort_data.new_value = new_value;
1760 resort_data.cookie = cookie;
1761 qsort (&TREE_VEC_ELT (sf, 0), TREE_VEC_LENGTH (sf), sizeof (tree),
1762 resort_field_decl_cmp);
1765 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1767 static int
1768 method_name_cmp (const void* m1_p, const void* m2_p)
1770 const tree *const m1 = m1_p;
1771 const tree *const m2 = m2_p;
1773 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1774 return 0;
1775 if (*m1 == NULL_TREE)
1776 return -1;
1777 if (*m2 == NULL_TREE)
1778 return 1;
1779 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1780 return -1;
1781 return 1;
1784 /* This routine compares two fields like method_name_cmp but using the
1785 pointer operator in resort_field_decl_data. */
1787 static int
1788 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1790 const tree *const m1 = m1_p;
1791 const tree *const m2 = m2_p;
1792 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1793 return 0;
1794 if (*m1 == NULL_TREE)
1795 return -1;
1796 if (*m2 == NULL_TREE)
1797 return 1;
1799 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1800 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1801 resort_data.new_value (&d1, resort_data.cookie);
1802 resort_data.new_value (&d2, resort_data.cookie);
1803 if (d1 < d2)
1804 return -1;
1806 return 1;
1809 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1811 void
1812 resort_type_method_vec (void* obj,
1813 void* orig_obj ATTRIBUTE_UNUSED ,
1814 gt_pointer_operator new_value,
1815 void* cookie)
1817 tree method_vec = obj;
1818 int len = TREE_VEC_LENGTH (method_vec);
1819 int slot;
1821 /* The type conversion ops have to live at the front of the vec, so we
1822 can't sort them. */
1823 for (slot = 2; slot < len; ++slot)
1825 tree fn = TREE_VEC_ELT (method_vec, slot);
1827 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1828 break;
1830 if (len - slot > 1)
1832 resort_data.new_value = new_value;
1833 resort_data.cookie = cookie;
1834 qsort (&TREE_VEC_ELT (method_vec, slot), len - slot, sizeof (tree),
1835 resort_method_name_cmp);
1839 /* Warn about duplicate methods in fn_fields. Also compact method
1840 lists so that lookup can be made faster.
1842 Data Structure: List of method lists. The outer list is a
1843 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1844 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1845 links the entire list of methods for TYPE_METHODS. Friends are
1846 chained in the same way as member functions (? TREE_CHAIN or
1847 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1848 list. That allows them to be quickly deleted, and requires no
1849 extra storage.
1851 Sort methods that are not special (i.e., constructors, destructors,
1852 and type conversion operators) so that we can find them faster in
1853 search. */
1855 static void
1856 finish_struct_methods (tree t)
1858 tree fn_fields;
1859 tree method_vec;
1860 int slot, len;
1862 if (!TYPE_METHODS (t))
1864 /* Clear these for safety; perhaps some parsing error could set
1865 these incorrectly. */
1866 TYPE_HAS_CONSTRUCTOR (t) = 0;
1867 TYPE_HAS_DESTRUCTOR (t) = 0;
1868 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
1869 return;
1872 method_vec = CLASSTYPE_METHOD_VEC (t);
1873 my_friendly_assert (method_vec != NULL_TREE, 19991215);
1874 len = TREE_VEC_LENGTH (method_vec);
1876 /* First fill in entry 0 with the constructors, entry 1 with destructors,
1877 and the next few with type conversion operators (if any). */
1878 for (fn_fields = TYPE_METHODS (t); fn_fields;
1879 fn_fields = TREE_CHAIN (fn_fields))
1880 /* Clear out this flag. */
1881 DECL_IN_AGGR_P (fn_fields) = 0;
1883 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
1884 /* We thought there was a destructor, but there wasn't. Some
1885 parse errors cause this anomalous situation. */
1886 TYPE_HAS_DESTRUCTOR (t) = 0;
1888 /* Issue warnings about private constructors and such. If there are
1889 no methods, then some public defaults are generated. */
1890 maybe_warn_about_overly_private_class (t);
1892 /* Now sort the methods. */
1893 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
1894 len--;
1895 TREE_VEC_LENGTH (method_vec) = len;
1897 /* The type conversion ops have to live at the front of the vec, so we
1898 can't sort them. */
1899 for (slot = 2; slot < len; ++slot)
1901 tree fn = TREE_VEC_ELT (method_vec, slot);
1903 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1904 break;
1906 if (len - slot > 1)
1907 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
1908 method_name_cmp);
1911 /* Emit error when a duplicate definition of a type is seen. Patch up. */
1913 void
1914 duplicate_tag_error (tree t)
1916 error ("redefinition of `%#T'", t);
1917 cp_error_at ("previous definition of `%#T'", t);
1919 /* Pretend we haven't defined this type. */
1921 /* All of the component_decl's were TREE_CHAINed together in the parser.
1922 finish_struct_methods walks these chains and assembles all methods with
1923 the same base name into DECL_CHAINs. Now we don't need the parser chains
1924 anymore, so we unravel them. */
1926 /* This used to be in finish_struct, but it turns out that the
1927 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
1928 things... */
1929 if (CLASSTYPE_METHOD_VEC (t))
1931 tree method_vec = CLASSTYPE_METHOD_VEC (t);
1932 int i, len = TREE_VEC_LENGTH (method_vec);
1933 for (i = 0; i < len; i++)
1935 tree unchain = TREE_VEC_ELT (method_vec, i);
1936 while (unchain != NULL_TREE)
1938 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
1939 unchain = OVL_NEXT (unchain);
1944 if (TYPE_LANG_SPECIFIC (t))
1946 tree binfo = TYPE_BINFO (t);
1947 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
1948 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
1949 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
1950 int use_template = CLASSTYPE_USE_TEMPLATE (t);
1952 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
1953 BINFO_BASETYPES(binfo) = NULL_TREE;
1955 TYPE_LANG_SPECIFIC (t)->u.h.is_lang_type_class = 1;
1956 TYPE_BINFO (t) = binfo;
1957 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1958 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
1959 TYPE_REDEFINED (t) = 1;
1960 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
1961 CLASSTYPE_USE_TEMPLATE (t) = use_template;
1962 CLASSTYPE_DECL_LIST (t) = NULL_TREE;
1964 TYPE_SIZE (t) = NULL_TREE;
1965 TYPE_MODE (t) = VOIDmode;
1966 TYPE_FIELDS (t) = NULL_TREE;
1967 TYPE_METHODS (t) = NULL_TREE;
1968 TYPE_VFIELD (t) = NULL_TREE;
1969 TYPE_CONTEXT (t) = NULL_TREE;
1971 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
1972 TYPE_LANG_FLAG_0 (t) = 0;
1973 TYPE_LANG_FLAG_1 (t) = 0;
1974 TYPE_LANG_FLAG_2 (t) = 0;
1975 TYPE_LANG_FLAG_3 (t) = 0;
1976 TYPE_LANG_FLAG_4 (t) = 0;
1977 TYPE_LANG_FLAG_5 (t) = 0;
1978 TYPE_LANG_FLAG_6 (t) = 0;
1979 /* But not this one. */
1980 SET_IS_AGGR_TYPE (t, 1);
1983 /* Make BINFO's vtable have N entries, including RTTI entries,
1984 vbase and vcall offsets, etc. Set its type and call the backend
1985 to lay it out. */
1987 static void
1988 layout_vtable_decl (tree binfo, int n)
1990 tree atype;
1991 tree vtable;
1993 atype = build_cplus_array_type (vtable_entry_type,
1994 build_index_type (size_int (n - 1)));
1995 layout_type (atype);
1997 /* We may have to grow the vtable. */
1998 vtable = get_vtbl_decl_for_binfo (binfo);
1999 if (!same_type_p (TREE_TYPE (vtable), atype))
2001 TREE_TYPE (vtable) = atype;
2002 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2003 layout_decl (vtable, 0);
2005 /* At one time the vtable info was grabbed 2 words at a time. This
2006 fails on SPARC unless you have 8-byte alignment. */
2007 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2008 DECL_ALIGN (vtable));
2012 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2013 have the same signature. */
2016 same_signature_p (tree fndecl, tree base_fndecl)
2018 /* One destructor overrides another if they are the same kind of
2019 destructor. */
2020 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2021 && special_function_p (base_fndecl) == special_function_p (fndecl))
2022 return 1;
2023 /* But a non-destructor never overrides a destructor, nor vice
2024 versa, nor do different kinds of destructors override
2025 one-another. For example, a complete object destructor does not
2026 override a deleting destructor. */
2027 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2028 return 0;
2030 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2032 tree types, base_types;
2033 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2034 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2035 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2036 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2037 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2038 return 1;
2040 return 0;
2043 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2044 subobject. */
2046 static bool
2047 base_derived_from (tree derived, tree base)
2049 tree probe;
2051 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2053 if (probe == derived)
2054 return true;
2055 else if (TREE_VIA_VIRTUAL (probe))
2056 /* If we meet a virtual base, we can't follow the inheritance
2057 any more. See if the complete type of DERIVED contains
2058 such a virtual base. */
2059 return purpose_member (BINFO_TYPE (probe),
2060 CLASSTYPE_VBASECLASSES (BINFO_TYPE (derived)))
2061 != NULL_TREE;
2063 return false;
2066 typedef struct find_final_overrider_data_s {
2067 /* The function for which we are trying to find a final overrider. */
2068 tree fn;
2069 /* The base class in which the function was declared. */
2070 tree declaring_base;
2071 /* The most derived class in the hierarchy. */
2072 tree most_derived_type;
2073 /* The candidate overriders. */
2074 tree candidates;
2075 /* Binfos which inherited virtually on the currrent path. */
2076 tree vpath;
2077 } find_final_overrider_data;
2079 /* Called from find_final_overrider via dfs_walk. */
2081 static tree
2082 dfs_find_final_overrider (tree binfo, void* data)
2084 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2086 if (binfo == ffod->declaring_base)
2088 /* We've found a path to the declaring base. Walk the path from
2089 derived to base, looking for an overrider for FN. */
2090 tree path, probe, vpath;
2092 /* Build the path, using the inheritance chain and record of
2093 virtual inheritance. */
2094 for (path = NULL_TREE, probe = binfo, vpath = ffod->vpath;;)
2096 path = tree_cons (NULL_TREE, probe, path);
2097 if (same_type_p (BINFO_TYPE (probe), ffod->most_derived_type))
2098 break;
2099 if (TREE_VIA_VIRTUAL (probe))
2101 probe = TREE_VALUE (vpath);
2102 vpath = TREE_CHAIN (vpath);
2104 else
2105 probe = BINFO_INHERITANCE_CHAIN (probe);
2107 /* Now walk path, looking for overrides. */
2108 for (; path; path = TREE_CHAIN (path))
2110 tree method = look_for_overrides_here
2111 (BINFO_TYPE (TREE_VALUE (path)), ffod->fn);
2113 if (method)
2115 tree *candidate = &ffod->candidates;
2116 path = TREE_VALUE (path);
2118 /* Remove any candidates overridden by this new function. */
2119 while (*candidate)
2121 /* If *CANDIDATE overrides METHOD, then METHOD
2122 cannot override anything else on the list. */
2123 if (base_derived_from (TREE_VALUE (*candidate), path))
2124 return NULL_TREE;
2125 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2126 if (base_derived_from (path, TREE_VALUE (*candidate)))
2127 *candidate = TREE_CHAIN (*candidate);
2128 else
2129 candidate = &TREE_CHAIN (*candidate);
2132 /* Add the new function. */
2133 ffod->candidates = tree_cons (method, path, ffod->candidates);
2134 break;
2139 return NULL_TREE;
2142 static tree
2143 dfs_find_final_overrider_q (tree derived, int ix, void *data)
2145 tree binfo = BINFO_BASETYPE (derived, ix);
2146 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2148 if (TREE_VIA_VIRTUAL (binfo))
2149 ffod->vpath = tree_cons (NULL_TREE, derived, ffod->vpath);
2151 return binfo;
2154 static tree
2155 dfs_find_final_overrider_post (tree binfo, void *data)
2157 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2159 if (TREE_VIA_VIRTUAL (binfo) && TREE_CHAIN (ffod->vpath))
2160 ffod->vpath = TREE_CHAIN (ffod->vpath);
2162 return NULL_TREE;
2165 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2166 FN and whose TREE_VALUE is the binfo for the base where the
2167 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2168 DERIVED) is the base object in which FN is declared. */
2170 static tree
2171 find_final_overrider (tree derived, tree binfo, tree fn)
2173 find_final_overrider_data ffod;
2175 /* Getting this right is a little tricky. This is valid:
2177 struct S { virtual void f (); };
2178 struct T { virtual void f (); };
2179 struct U : public S, public T { };
2181 even though calling `f' in `U' is ambiguous. But,
2183 struct R { virtual void f(); };
2184 struct S : virtual public R { virtual void f (); };
2185 struct T : virtual public R { virtual void f (); };
2186 struct U : public S, public T { };
2188 is not -- there's no way to decide whether to put `S::f' or
2189 `T::f' in the vtable for `R'.
2191 The solution is to look at all paths to BINFO. If we find
2192 different overriders along any two, then there is a problem. */
2193 if (DECL_THUNK_P (fn))
2194 fn = THUNK_TARGET (fn);
2196 ffod.fn = fn;
2197 ffod.declaring_base = binfo;
2198 ffod.most_derived_type = BINFO_TYPE (derived);
2199 ffod.candidates = NULL_TREE;
2200 ffod.vpath = NULL_TREE;
2202 dfs_walk_real (derived,
2203 dfs_find_final_overrider,
2204 dfs_find_final_overrider_post,
2205 dfs_find_final_overrider_q,
2206 &ffod);
2208 /* If there was no winner, issue an error message. */
2209 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2211 error ("no unique final overrider for `%D' in `%T'", fn,
2212 BINFO_TYPE (derived));
2213 return error_mark_node;
2216 return ffod.candidates;
2219 /* Return the index of the vcall offset for FN when TYPE is used as a
2220 virtual base. */
2222 static tree
2223 get_vcall_index (tree fn, tree type)
2225 tree v;
2227 for (v = CLASSTYPE_VCALL_INDICES (type); v; v = TREE_CHAIN (v))
2228 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (TREE_PURPOSE (v)))
2229 || same_signature_p (fn, TREE_PURPOSE (v)))
2230 break;
2232 /* There should always be an appropriate index. */
2233 my_friendly_assert (v, 20021103);
2235 return TREE_VALUE (v);
2238 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2239 dominated by T. FN has been overridden in BINFO; VIRTUALS points to the
2240 corresponding position in the BINFO_VIRTUALS list. */
2242 static void
2243 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2244 unsigned ix)
2246 tree b;
2247 tree overrider;
2248 tree delta;
2249 tree virtual_base;
2250 tree first_defn;
2251 tree overrider_fn, overrider_target;
2252 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2253 tree over_return, base_return;
2254 bool lost = false;
2256 /* Find the nearest primary base (possibly binfo itself) which defines
2257 this function; this is the class the caller will convert to when
2258 calling FN through BINFO. */
2259 for (b = binfo; ; b = get_primary_binfo (b))
2261 my_friendly_assert (b, 20021227);
2262 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2263 break;
2265 /* The nearest definition is from a lost primary. */
2266 if (BINFO_LOST_PRIMARY_P (b))
2267 lost = true;
2269 first_defn = b;
2271 /* Find the final overrider. */
2272 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2273 if (overrider == error_mark_node)
2274 return;
2275 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2277 /* Check for adjusting covariant return types. */
2278 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2279 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2281 if (POINTER_TYPE_P (over_return)
2282 && TREE_CODE (over_return) == TREE_CODE (base_return)
2283 && CLASS_TYPE_P (TREE_TYPE (over_return))
2284 && CLASS_TYPE_P (TREE_TYPE (base_return)))
2286 /* If FN is a covariant thunk, we must figure out the adjustment
2287 to the final base FN was converting to. As OVERRIDER_TARGET might
2288 also be converting to the return type of FN, we have to
2289 combine the two conversions here. */
2290 tree fixed_offset, virtual_offset;
2292 if (DECL_THUNK_P (fn))
2294 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2295 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2297 else
2298 fixed_offset = virtual_offset = NULL_TREE;
2300 if (!virtual_offset)
2302 /* There was no existing virtual thunk (which takes
2303 precidence). */
2304 tree thunk_binfo;
2305 base_kind kind;
2307 thunk_binfo = lookup_base (TREE_TYPE (over_return),
2308 TREE_TYPE (base_return),
2309 ba_check | ba_quiet, &kind);
2311 if (thunk_binfo && (kind == bk_via_virtual
2312 || !BINFO_OFFSET_ZEROP (thunk_binfo)))
2314 tree offset = BINFO_OFFSET (thunk_binfo);
2316 if (kind == bk_via_virtual)
2318 /* We convert via virtual base. Find the virtual
2319 base and adjust the fixed offset to be from there. */
2320 while (!TREE_VIA_VIRTUAL (thunk_binfo))
2321 thunk_binfo = BINFO_INHERITANCE_CHAIN (thunk_binfo);
2323 virtual_offset = thunk_binfo;
2324 offset = size_binop (MINUS_EXPR, offset,
2325 BINFO_OFFSET (virtual_offset));
2327 if (fixed_offset)
2328 /* There was an existing fixed offset, this must be
2329 from the base just converted to, and the base the
2330 FN was thunking to. */
2331 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2332 else
2333 fixed_offset = offset;
2337 if (fixed_offset || virtual_offset)
2338 /* Replace the overriding function with a covariant thunk. We
2339 will emit the overriding function in its own slot as
2340 well. */
2341 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2342 fixed_offset, virtual_offset);
2344 else
2345 my_friendly_assert (!DECL_THUNK_P (fn), 20021231);
2347 /* Assume that we will produce a thunk that convert all the way to
2348 the final overrider, and not to an intermediate virtual base. */
2349 virtual_base = NULL_TREE;
2351 /* See if we can convert to an intermediate virtual base first, and then
2352 use the vcall offset located there to finish the conversion. */
2353 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2355 /* If we find the final overrider, then we can stop
2356 walking. */
2357 if (same_type_p (BINFO_TYPE (b),
2358 BINFO_TYPE (TREE_VALUE (overrider))))
2359 break;
2361 /* If we find a virtual base, and we haven't yet found the
2362 overrider, then there is a virtual base between the
2363 declaring base (first_defn) and the final overrider. */
2364 if (TREE_VIA_VIRTUAL (b))
2366 virtual_base = b;
2367 break;
2371 if (overrider_fn != overrider_target && !virtual_base)
2373 /* The ABI specifies that a covariant thunk includes a mangling
2374 for a this pointer adjustment. This-adjusting thunks that
2375 override a function from a virtual base have a vcall
2376 adjustment. When the virtual base in question is a primary
2377 virtual base, we know the adjustments are zero, (and in the
2378 non-covariant case, we would not use the thunk).
2379 Unfortunately we didn't notice this could happen, when
2380 designing the ABI and so never mandated that such a covariant
2381 thunk should be emitted. Because we must use the ABI mandated
2382 name, we must continue searching from the binfo where we
2383 found the most recent definition of the function, towards the
2384 primary binfo which first introduced the function into the
2385 vtable. If that enters a virtual base, we must use a vcall
2386 this-adjusting thunk. Bleah! */
2387 tree probe;
2389 for (probe = first_defn; (probe = get_primary_binfo (probe));)
2391 if (TREE_VIA_VIRTUAL (probe))
2392 virtual_base = probe;
2393 if ((unsigned) list_length (BINFO_VIRTUALS (probe)) <= ix)
2394 break;
2396 if (virtual_base)
2397 /* Even if we find a virtual base, the correct delta is
2398 between the overrider and the binfo we're building a vtable
2399 for. */
2400 goto virtual_covariant;
2403 /* Compute the constant adjustment to the `this' pointer. The
2404 `this' pointer, when this function is called, will point at BINFO
2405 (or one of its primary bases, which are at the same offset). */
2406 if (virtual_base)
2407 /* The `this' pointer needs to be adjusted from the declaration to
2408 the nearest virtual base. */
2409 delta = size_diffop (BINFO_OFFSET (virtual_base),
2410 BINFO_OFFSET (first_defn));
2411 else if (lost)
2412 /* If the nearest definition is in a lost primary, we don't need an
2413 entry in our vtable. Except possibly in a constructor vtable,
2414 if we happen to get our primary back. In that case, the offset
2415 will be zero, as it will be a primary base. */
2416 delta = size_zero_node;
2417 else
2418 /* The `this' pointer needs to be adjusted from pointing to
2419 BINFO to pointing at the base where the final overrider
2420 appears. */
2421 virtual_covariant:
2422 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2423 BINFO_OFFSET (binfo));
2425 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2427 if (virtual_base)
2428 BV_VCALL_INDEX (*virtuals)
2429 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2432 /* Called from modify_all_vtables via dfs_walk. */
2434 static tree
2435 dfs_modify_vtables (tree binfo, void* data)
2437 if (/* There's no need to modify the vtable for a non-virtual
2438 primary base; we're not going to use that vtable anyhow.
2439 We do still need to do this for virtual primary bases, as they
2440 could become non-primary in a construction vtable. */
2441 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2442 /* Similarly, a base without a vtable needs no modification. */
2443 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2445 tree t = (tree) data;
2446 tree virtuals;
2447 tree old_virtuals;
2448 unsigned ix;
2450 make_new_vtable (t, binfo);
2452 /* Now, go through each of the virtual functions in the virtual
2453 function table for BINFO. Find the final overrider, and
2454 update the BINFO_VIRTUALS list appropriately. */
2455 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2456 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2457 virtuals;
2458 ix++, virtuals = TREE_CHAIN (virtuals),
2459 old_virtuals = TREE_CHAIN (old_virtuals))
2460 update_vtable_entry_for_fn (t,
2461 binfo,
2462 BV_FN (old_virtuals),
2463 &virtuals, ix);
2466 BINFO_MARKED (binfo) = 1;
2468 return NULL_TREE;
2471 /* Update all of the primary and secondary vtables for T. Create new
2472 vtables as required, and initialize their RTTI information. Each
2473 of the functions in VIRTUALS is declared in T and may override a
2474 virtual function from a base class; find and modify the appropriate
2475 entries to point to the overriding functions. Returns a list, in
2476 declaration order, of the virtual functions that are declared in T,
2477 but do not appear in the primary base class vtable, and which
2478 should therefore be appended to the end of the vtable for T. */
2480 static tree
2481 modify_all_vtables (tree t, tree virtuals)
2483 tree binfo = TYPE_BINFO (t);
2484 tree *fnsp;
2486 /* Update all of the vtables. */
2487 dfs_walk (binfo, dfs_modify_vtables, unmarkedp, t);
2488 dfs_walk (binfo, dfs_unmark, markedp, t);
2490 /* Add virtual functions not already in our primary vtable. These
2491 will be both those introduced by this class, and those overridden
2492 from secondary bases. It does not include virtuals merely
2493 inherited from secondary bases. */
2494 for (fnsp = &virtuals; *fnsp; )
2496 tree fn = TREE_VALUE (*fnsp);
2498 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2499 || DECL_VINDEX (fn) == error_mark_node)
2501 /* We don't need to adjust the `this' pointer when
2502 calling this function. */
2503 BV_DELTA (*fnsp) = integer_zero_node;
2504 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2506 /* This is a function not already in our vtable. Keep it. */
2507 fnsp = &TREE_CHAIN (*fnsp);
2509 else
2510 /* We've already got an entry for this function. Skip it. */
2511 *fnsp = TREE_CHAIN (*fnsp);
2514 return virtuals;
2517 /* Get the base virtual function declarations in T that have the
2518 indicated NAME. */
2520 static tree
2521 get_basefndecls (tree name, tree t)
2523 tree methods;
2524 tree base_fndecls = NULL_TREE;
2525 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2526 int i;
2528 for (methods = TYPE_METHODS (t); methods; methods = TREE_CHAIN (methods))
2529 if (TREE_CODE (methods) == FUNCTION_DECL
2530 && DECL_VINDEX (methods) != NULL_TREE
2531 && DECL_NAME (methods) == name)
2532 base_fndecls = tree_cons (NULL_TREE, methods, base_fndecls);
2534 if (base_fndecls)
2535 return base_fndecls;
2537 for (i = 0; i < n_baseclasses; i++)
2539 tree basetype = TYPE_BINFO_BASETYPE (t, i);
2540 base_fndecls = chainon (get_basefndecls (name, basetype),
2541 base_fndecls);
2544 return base_fndecls;
2547 /* If this declaration supersedes the declaration of
2548 a method declared virtual in the base class, then
2549 mark this field as being virtual as well. */
2551 static void
2552 check_for_override (tree decl, tree ctype)
2554 if (TREE_CODE (decl) == TEMPLATE_DECL)
2555 /* In [temp.mem] we have:
2557 A specialization of a member function template does not
2558 override a virtual function from a base class. */
2559 return;
2560 if ((DECL_DESTRUCTOR_P (decl)
2561 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2562 && look_for_overrides (ctype, decl)
2563 && !DECL_STATIC_FUNCTION_P (decl))
2564 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2565 the error_mark_node so that we know it is an overriding
2566 function. */
2567 DECL_VINDEX (decl) = decl;
2569 if (DECL_VIRTUAL_P (decl))
2571 if (!DECL_VINDEX (decl))
2572 DECL_VINDEX (decl) = error_mark_node;
2573 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2577 /* Warn about hidden virtual functions that are not overridden in t.
2578 We know that constructors and destructors don't apply. */
2580 void
2581 warn_hidden (tree t)
2583 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2584 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2585 int i;
2587 /* We go through each separately named virtual function. */
2588 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2590 tree fns;
2591 tree name;
2592 tree fndecl;
2593 tree base_fndecls;
2594 int j;
2596 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2597 have the same name. Figure out what name that is. */
2598 name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2599 /* There are no possibly hidden functions yet. */
2600 base_fndecls = NULL_TREE;
2601 /* Iterate through all of the base classes looking for possibly
2602 hidden functions. */
2603 for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
2605 tree basetype = TYPE_BINFO_BASETYPE (t, j);
2606 base_fndecls = chainon (get_basefndecls (name, basetype),
2607 base_fndecls);
2610 /* If there are no functions to hide, continue. */
2611 if (!base_fndecls)
2612 continue;
2614 /* Remove any overridden functions. */
2615 for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
2617 fndecl = OVL_CURRENT (fns);
2618 if (DECL_VINDEX (fndecl))
2620 tree *prev = &base_fndecls;
2622 while (*prev)
2623 /* If the method from the base class has the same
2624 signature as the method from the derived class, it
2625 has been overridden. */
2626 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2627 *prev = TREE_CHAIN (*prev);
2628 else
2629 prev = &TREE_CHAIN (*prev);
2633 /* Now give a warning for all base functions without overriders,
2634 as they are hidden. */
2635 while (base_fndecls)
2637 /* Here we know it is a hider, and no overrider exists. */
2638 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2639 cp_warning_at (" by `%D'",
2640 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2641 base_fndecls = TREE_CHAIN (base_fndecls);
2646 /* Check for things that are invalid. There are probably plenty of other
2647 things we should check for also. */
2649 static void
2650 finish_struct_anon (tree t)
2652 tree field;
2654 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2656 if (TREE_STATIC (field))
2657 continue;
2658 if (TREE_CODE (field) != FIELD_DECL)
2659 continue;
2661 if (DECL_NAME (field) == NULL_TREE
2662 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2664 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2665 for (; elt; elt = TREE_CHAIN (elt))
2667 /* We're generally only interested in entities the user
2668 declared, but we also find nested classes by noticing
2669 the TYPE_DECL that we create implicitly. You're
2670 allowed to put one anonymous union inside another,
2671 though, so we explicitly tolerate that. We use
2672 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2673 we also allow unnamed types used for defining fields. */
2674 if (DECL_ARTIFICIAL (elt)
2675 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2676 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2677 continue;
2679 if (constructor_name_p (DECL_NAME (elt), t))
2680 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2681 elt);
2683 if (TREE_CODE (elt) != FIELD_DECL)
2685 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2686 elt);
2687 continue;
2690 if (TREE_PRIVATE (elt))
2691 cp_pedwarn_at ("private member `%#D' in anonymous union",
2692 elt);
2693 else if (TREE_PROTECTED (elt))
2694 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2695 elt);
2697 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2698 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2704 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2705 will be used later during class template instantiation.
2706 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2707 a non-static member data (FIELD_DECL), a member function
2708 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2709 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2710 When FRIEND_P is nonzero, T is either a friend class
2711 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2712 (FUNCTION_DECL, TEMPLATE_DECL). */
2714 void
2715 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2717 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2718 if (CLASSTYPE_TEMPLATE_INFO (type))
2719 CLASSTYPE_DECL_LIST (type)
2720 = tree_cons (friend_p ? NULL_TREE : type,
2721 t, CLASSTYPE_DECL_LIST (type));
2724 /* Create default constructors, assignment operators, and so forth for
2725 the type indicated by T, if they are needed.
2726 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2727 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2728 class cannot have a default constructor, copy constructor taking a
2729 const reference argument, or an assignment operator taking a const
2730 reference, respectively. If a virtual destructor is created, its
2731 DECL is returned; otherwise the return value is NULL_TREE. */
2733 static void
2734 add_implicitly_declared_members (tree t,
2735 int cant_have_default_ctor,
2736 int cant_have_const_cctor,
2737 int cant_have_const_assignment)
2739 tree default_fn;
2740 tree implicit_fns = NULL_TREE;
2741 tree virtual_dtor = NULL_TREE;
2742 tree *f;
2744 ++adding_implicit_members;
2746 /* Destructor. */
2747 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2749 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2750 check_for_override (default_fn, t);
2752 /* If we couldn't make it work, then pretend we didn't need it. */
2753 if (default_fn == void_type_node)
2754 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2755 else
2757 TREE_CHAIN (default_fn) = implicit_fns;
2758 implicit_fns = default_fn;
2760 if (DECL_VINDEX (default_fn))
2761 virtual_dtor = default_fn;
2764 else
2765 /* Any non-implicit destructor is non-trivial. */
2766 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2768 /* Default constructor. */
2769 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2771 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2772 TREE_CHAIN (default_fn) = implicit_fns;
2773 implicit_fns = default_fn;
2776 /* Copy constructor. */
2777 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2779 /* ARM 12.18: You get either X(X&) or X(const X&), but
2780 not both. --Chip */
2781 default_fn
2782 = implicitly_declare_fn (sfk_copy_constructor, t,
2783 /*const_p=*/!cant_have_const_cctor);
2784 TREE_CHAIN (default_fn) = implicit_fns;
2785 implicit_fns = default_fn;
2788 /* Assignment operator. */
2789 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2791 default_fn
2792 = implicitly_declare_fn (sfk_assignment_operator, t,
2793 /*const_p=*/!cant_have_const_assignment);
2794 TREE_CHAIN (default_fn) = implicit_fns;
2795 implicit_fns = default_fn;
2798 /* Now, hook all of the new functions on to TYPE_METHODS,
2799 and add them to the CLASSTYPE_METHOD_VEC. */
2800 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2802 add_method (t, *f, /*error_p=*/0);
2803 maybe_add_class_template_decl_list (current_class_type, *f, /*friend_p=*/0);
2805 if (abi_version_at_least (2))
2806 /* G++ 3.2 put the implicit destructor at the *beginning* of the
2807 list, which cause the destructor to be emitted in an incorrect
2808 location in the vtable. */
2809 TYPE_METHODS (t) = chainon (TYPE_METHODS (t), implicit_fns);
2810 else
2812 if (warn_abi && virtual_dtor)
2813 warning ("vtable layout for class `%T' may not be ABI-compliant "
2814 "and may change in a future version of GCC due to implicit "
2815 "virtual destructor",
2817 *f = TYPE_METHODS (t);
2818 TYPE_METHODS (t) = implicit_fns;
2821 --adding_implicit_members;
2824 /* Subroutine of finish_struct_1. Recursively count the number of fields
2825 in TYPE, including anonymous union members. */
2827 static int
2828 count_fields (tree fields)
2830 tree x;
2831 int n_fields = 0;
2832 for (x = fields; x; x = TREE_CHAIN (x))
2834 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2835 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2836 else
2837 n_fields += 1;
2839 return n_fields;
2842 /* Subroutine of finish_struct_1. Recursively add all the fields in the
2843 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
2845 static int
2846 add_fields_to_vec (tree fields, tree field_vec, int idx)
2848 tree x;
2849 for (x = fields; x; x = TREE_CHAIN (x))
2851 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2852 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2853 else
2854 TREE_VEC_ELT (field_vec, idx++) = x;
2856 return idx;
2859 /* FIELD is a bit-field. We are finishing the processing for its
2860 enclosing type. Issue any appropriate messages and set appropriate
2861 flags. */
2863 static void
2864 check_bitfield_decl (tree field)
2866 tree type = TREE_TYPE (field);
2867 tree w = NULL_TREE;
2869 /* Detect invalid bit-field type. */
2870 if (DECL_INITIAL (field)
2871 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
2873 cp_error_at ("bit-field `%#D' with non-integral type", field);
2874 w = error_mark_node;
2877 /* Detect and ignore out of range field width. */
2878 if (DECL_INITIAL (field))
2880 w = DECL_INITIAL (field);
2882 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2883 STRIP_NOPS (w);
2885 /* detect invalid field size. */
2886 if (TREE_CODE (w) == CONST_DECL)
2887 w = DECL_INITIAL (w);
2888 else
2889 w = decl_constant_value (w);
2891 if (TREE_CODE (w) != INTEGER_CST)
2893 cp_error_at ("bit-field `%D' width not an integer constant",
2894 field);
2895 w = error_mark_node;
2897 else if (tree_int_cst_sgn (w) < 0)
2899 cp_error_at ("negative width in bit-field `%D'", field);
2900 w = error_mark_node;
2902 else if (integer_zerop (w) && DECL_NAME (field) != 0)
2904 cp_error_at ("zero width for bit-field `%D'", field);
2905 w = error_mark_node;
2907 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
2908 && TREE_CODE (type) != ENUMERAL_TYPE
2909 && TREE_CODE (type) != BOOLEAN_TYPE)
2910 cp_warning_at ("width of `%D' exceeds its type", field);
2911 else if (TREE_CODE (type) == ENUMERAL_TYPE
2912 && (0 > compare_tree_int (w,
2913 min_precision (TYPE_MIN_VALUE (type),
2914 TREE_UNSIGNED (type)))
2915 || 0 > compare_tree_int (w,
2916 min_precision
2917 (TYPE_MAX_VALUE (type),
2918 TREE_UNSIGNED (type)))))
2919 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
2920 field, type);
2923 /* Remove the bit-field width indicator so that the rest of the
2924 compiler does not treat that value as an initializer. */
2925 DECL_INITIAL (field) = NULL_TREE;
2927 if (w != error_mark_node)
2929 DECL_SIZE (field) = convert (bitsizetype, w);
2930 DECL_BIT_FIELD (field) = 1;
2932 if (integer_zerop (w)
2933 && ! (* targetm.ms_bitfield_layout_p) (DECL_FIELD_CONTEXT (field)))
2935 #ifdef EMPTY_FIELD_BOUNDARY
2936 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
2937 EMPTY_FIELD_BOUNDARY);
2938 #endif
2939 #ifdef PCC_BITFIELD_TYPE_MATTERS
2940 if (PCC_BITFIELD_TYPE_MATTERS)
2942 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
2943 TYPE_ALIGN (type));
2944 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
2946 #endif
2949 else
2951 /* Non-bit-fields are aligned for their type. */
2952 DECL_BIT_FIELD (field) = 0;
2953 CLEAR_DECL_C_BIT_FIELD (field);
2954 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
2955 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
2959 /* FIELD is a non bit-field. We are finishing the processing for its
2960 enclosing type T. Issue any appropriate messages and set appropriate
2961 flags. */
2963 static void
2964 check_field_decl (tree field,
2965 tree t,
2966 int* cant_have_const_ctor,
2967 int* cant_have_default_ctor,
2968 int* no_const_asn_ref,
2969 int* any_default_members)
2971 tree type = strip_array_types (TREE_TYPE (field));
2973 /* An anonymous union cannot contain any fields which would change
2974 the settings of CANT_HAVE_CONST_CTOR and friends. */
2975 if (ANON_UNION_TYPE_P (type))
2977 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
2978 structs. So, we recurse through their fields here. */
2979 else if (ANON_AGGR_TYPE_P (type))
2981 tree fields;
2983 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2984 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
2985 check_field_decl (fields, t, cant_have_const_ctor,
2986 cant_have_default_ctor, no_const_asn_ref,
2987 any_default_members);
2989 /* Check members with class type for constructors, destructors,
2990 etc. */
2991 else if (CLASS_TYPE_P (type))
2993 /* Never let anything with uninheritable virtuals
2994 make it through without complaint. */
2995 abstract_virtuals_error (field, type);
2997 if (TREE_CODE (t) == UNION_TYPE)
2999 if (TYPE_NEEDS_CONSTRUCTING (type))
3000 cp_error_at ("member `%#D' with constructor not allowed in union",
3001 field);
3002 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3003 cp_error_at ("member `%#D' with destructor not allowed in union",
3004 field);
3005 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3006 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3007 field);
3009 else
3011 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3012 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3013 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3014 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3015 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3018 if (!TYPE_HAS_CONST_INIT_REF (type))
3019 *cant_have_const_ctor = 1;
3021 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3022 *no_const_asn_ref = 1;
3024 if (TYPE_HAS_CONSTRUCTOR (type)
3025 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3026 *cant_have_default_ctor = 1;
3028 if (DECL_INITIAL (field) != NULL_TREE)
3030 /* `build_class_init_list' does not recognize
3031 non-FIELD_DECLs. */
3032 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3033 cp_error_at ("multiple fields in union `%T' initialized");
3034 *any_default_members = 1;
3037 /* Non-bit-fields are aligned for their type, except packed fields
3038 which require only BITS_PER_UNIT alignment. */
3039 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3040 (DECL_PACKED (field)
3041 ? BITS_PER_UNIT
3042 : TYPE_ALIGN (TREE_TYPE (field))));
3043 if (! DECL_PACKED (field))
3044 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3047 /* Check the data members (both static and non-static), class-scoped
3048 typedefs, etc., appearing in the declaration of T. Issue
3049 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3050 declaration order) of access declarations; each TREE_VALUE in this
3051 list is a USING_DECL.
3053 In addition, set the following flags:
3055 EMPTY_P
3056 The class is empty, i.e., contains no non-static data members.
3058 CANT_HAVE_DEFAULT_CTOR_P
3059 This class cannot have an implicitly generated default
3060 constructor.
3062 CANT_HAVE_CONST_CTOR_P
3063 This class cannot have an implicitly generated copy constructor
3064 taking a const reference.
3066 CANT_HAVE_CONST_ASN_REF
3067 This class cannot have an implicitly generated assignment
3068 operator taking a const reference.
3070 All of these flags should be initialized before calling this
3071 function.
3073 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3074 fields can be added by adding to this chain. */
3076 static void
3077 check_field_decls (tree t, tree *access_decls,
3078 int *cant_have_default_ctor_p,
3079 int *cant_have_const_ctor_p,
3080 int *no_const_asn_ref_p)
3082 tree *field;
3083 tree *next;
3084 int has_pointers;
3085 int any_default_members;
3087 /* First, delete any duplicate fields. */
3088 delete_duplicate_fields (TYPE_FIELDS (t));
3090 /* Assume there are no access declarations. */
3091 *access_decls = NULL_TREE;
3092 /* Assume this class has no pointer members. */
3093 has_pointers = 0;
3094 /* Assume none of the members of this class have default
3095 initializations. */
3096 any_default_members = 0;
3098 for (field = &TYPE_FIELDS (t); *field; field = next)
3100 tree x = *field;
3101 tree type = TREE_TYPE (x);
3103 next = &TREE_CHAIN (x);
3105 if (TREE_CODE (x) == FIELD_DECL)
3107 DECL_PACKED (x) |= TYPE_PACKED (t);
3109 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3110 /* We don't treat zero-width bitfields as making a class
3111 non-empty. */
3113 else
3115 tree element_type;
3117 /* The class is non-empty. */
3118 CLASSTYPE_EMPTY_P (t) = 0;
3119 /* The class is not even nearly empty. */
3120 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3121 /* If one of the data members contains an empty class,
3122 so does T. */
3123 element_type = strip_array_types (type);
3124 if (CLASS_TYPE_P (element_type)
3125 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3126 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3130 if (TREE_CODE (x) == USING_DECL)
3132 /* Prune the access declaration from the list of fields. */
3133 *field = TREE_CHAIN (x);
3135 /* Save the access declarations for our caller. */
3136 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3138 /* Since we've reset *FIELD there's no reason to skip to the
3139 next field. */
3140 next = field;
3141 continue;
3144 if (TREE_CODE (x) == TYPE_DECL
3145 || TREE_CODE (x) == TEMPLATE_DECL)
3146 continue;
3148 /* If we've gotten this far, it's a data member, possibly static,
3149 or an enumerator. */
3151 DECL_CONTEXT (x) = t;
3153 /* ``A local class cannot have static data members.'' ARM 9.4 */
3154 if (current_function_decl && TREE_STATIC (x))
3155 cp_error_at ("field `%D' in local class cannot be static", x);
3157 /* Perform error checking that did not get done in
3158 grokdeclarator. */
3159 if (TREE_CODE (type) == FUNCTION_TYPE)
3161 cp_error_at ("field `%D' invalidly declared function type",
3163 type = build_pointer_type (type);
3164 TREE_TYPE (x) = type;
3166 else if (TREE_CODE (type) == METHOD_TYPE)
3168 cp_error_at ("field `%D' invalidly declared method type", x);
3169 type = build_pointer_type (type);
3170 TREE_TYPE (x) = type;
3172 else if (TREE_CODE (type) == OFFSET_TYPE)
3174 cp_error_at ("field `%D' invalidly declared offset type", x);
3175 type = build_pointer_type (type);
3176 TREE_TYPE (x) = type;
3179 if (type == error_mark_node)
3180 continue;
3182 /* When this goes into scope, it will be a non-local reference. */
3183 DECL_NONLOCAL (x) = 1;
3185 if (TREE_CODE (x) == CONST_DECL)
3186 continue;
3188 if (TREE_CODE (x) == VAR_DECL)
3190 if (TREE_CODE (t) == UNION_TYPE)
3191 /* Unions cannot have static members. */
3192 cp_error_at ("field `%D' declared static in union", x);
3194 continue;
3197 /* Now it can only be a FIELD_DECL. */
3199 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3200 CLASSTYPE_NON_AGGREGATE (t) = 1;
3202 /* If this is of reference type, check if it needs an init.
3203 Also do a little ANSI jig if necessary. */
3204 if (TREE_CODE (type) == REFERENCE_TYPE)
3206 CLASSTYPE_NON_POD_P (t) = 1;
3207 if (DECL_INITIAL (x) == NULL_TREE)
3208 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3210 /* ARM $12.6.2: [A member initializer list] (or, for an
3211 aggregate, initialization by a brace-enclosed list) is the
3212 only way to initialize nonstatic const and reference
3213 members. */
3214 *cant_have_default_ctor_p = 1;
3215 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3217 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3218 && extra_warnings)
3219 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3222 type = strip_array_types (type);
3224 if (TREE_CODE (type) == POINTER_TYPE)
3225 has_pointers = 1;
3227 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3228 CLASSTYPE_HAS_MUTABLE (t) = 1;
3230 if (! pod_type_p (type))
3231 /* DR 148 now allows pointers to members (which are POD themselves),
3232 to be allowed in POD structs. */
3233 CLASSTYPE_NON_POD_P (t) = 1;
3235 if (! zero_init_p (type))
3236 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3238 /* If any field is const, the structure type is pseudo-const. */
3239 if (CP_TYPE_CONST_P (type))
3241 C_TYPE_FIELDS_READONLY (t) = 1;
3242 if (DECL_INITIAL (x) == NULL_TREE)
3243 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3245 /* ARM $12.6.2: [A member initializer list] (or, for an
3246 aggregate, initialization by a brace-enclosed list) is the
3247 only way to initialize nonstatic const and reference
3248 members. */
3249 *cant_have_default_ctor_p = 1;
3250 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3252 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3253 && extra_warnings)
3254 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3256 /* A field that is pseudo-const makes the structure likewise. */
3257 else if (CLASS_TYPE_P (type))
3259 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3260 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3261 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3262 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3265 /* Core issue 80: A nonstatic data member is required to have a
3266 different name from the class iff the class has a
3267 user-defined constructor. */
3268 if (constructor_name_p (x, t) && TYPE_HAS_CONSTRUCTOR (t))
3269 cp_pedwarn_at ("field `%#D' with same name as class", x);
3271 /* We set DECL_C_BIT_FIELD in grokbitfield.
3272 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3273 if (DECL_C_BIT_FIELD (x))
3274 check_bitfield_decl (x);
3275 else
3276 check_field_decl (x, t,
3277 cant_have_const_ctor_p,
3278 cant_have_default_ctor_p,
3279 no_const_asn_ref_p,
3280 &any_default_members);
3283 /* Effective C++ rule 11. */
3284 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3285 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3287 warning ("`%#T' has pointer data members", t);
3289 if (! TYPE_HAS_INIT_REF (t))
3291 warning (" but does not override `%T(const %T&)'", t, t);
3292 if (! TYPE_HAS_ASSIGN_REF (t))
3293 warning (" or `operator=(const %T&)'", t);
3295 else if (! TYPE_HAS_ASSIGN_REF (t))
3296 warning (" but does not override `operator=(const %T&)'", t);
3300 /* Check anonymous struct/anonymous union fields. */
3301 finish_struct_anon (t);
3303 /* We've built up the list of access declarations in reverse order.
3304 Fix that now. */
3305 *access_decls = nreverse (*access_decls);
3308 /* If TYPE is an empty class type, records its OFFSET in the table of
3309 OFFSETS. */
3311 static int
3312 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3314 splay_tree_node n;
3316 if (!is_empty_class (type))
3317 return 0;
3319 /* Record the location of this empty object in OFFSETS. */
3320 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3321 if (!n)
3322 n = splay_tree_insert (offsets,
3323 (splay_tree_key) offset,
3324 (splay_tree_value) NULL_TREE);
3325 n->value = ((splay_tree_value)
3326 tree_cons (NULL_TREE,
3327 type,
3328 (tree) n->value));
3330 return 0;
3333 /* Returns nonzero if TYPE is an empty class type and there is
3334 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3336 static int
3337 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3339 splay_tree_node n;
3340 tree t;
3342 if (!is_empty_class (type))
3343 return 0;
3345 /* Record the location of this empty object in OFFSETS. */
3346 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3347 if (!n)
3348 return 0;
3350 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3351 if (same_type_p (TREE_VALUE (t), type))
3352 return 1;
3354 return 0;
3357 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3358 F for every subobject, passing it the type, offset, and table of
3359 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3360 be traversed.
3362 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3363 than MAX_OFFSET will not be walked.
3365 If F returns a nonzero value, the traversal ceases, and that value
3366 is returned. Otherwise, returns zero. */
3368 static int
3369 walk_subobject_offsets (tree type,
3370 subobject_offset_fn f,
3371 tree offset,
3372 splay_tree offsets,
3373 tree max_offset,
3374 int vbases_p)
3376 int r = 0;
3377 tree type_binfo = NULL_TREE;
3379 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3380 stop. */
3381 if (max_offset && INT_CST_LT (max_offset, offset))
3382 return 0;
3384 if (!TYPE_P (type))
3386 if (abi_version_at_least (2))
3387 type_binfo = type;
3388 type = BINFO_TYPE (type);
3391 if (CLASS_TYPE_P (type))
3393 tree field;
3394 tree binfo;
3395 int i;
3397 /* Avoid recursing into objects that are not interesting. */
3398 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3399 return 0;
3401 /* Record the location of TYPE. */
3402 r = (*f) (type, offset, offsets);
3403 if (r)
3404 return r;
3406 /* Iterate through the direct base classes of TYPE. */
3407 if (!type_binfo)
3408 type_binfo = TYPE_BINFO (type);
3409 for (i = 0; i < BINFO_N_BASETYPES (type_binfo); ++i)
3411 tree binfo_offset;
3413 binfo = BINFO_BASETYPE (type_binfo, i);
3415 if (abi_version_at_least (2)
3416 && TREE_VIA_VIRTUAL (binfo))
3417 continue;
3419 if (!vbases_p
3420 && TREE_VIA_VIRTUAL (binfo)
3421 && !BINFO_PRIMARY_P (binfo))
3422 continue;
3424 if (!abi_version_at_least (2))
3425 binfo_offset = size_binop (PLUS_EXPR,
3426 offset,
3427 BINFO_OFFSET (binfo));
3428 else
3430 tree orig_binfo;
3431 /* We cannot rely on BINFO_OFFSET being set for the base
3432 class yet, but the offsets for direct non-virtual
3433 bases can be calculated by going back to the TYPE. */
3434 orig_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3435 binfo_offset = size_binop (PLUS_EXPR,
3436 offset,
3437 BINFO_OFFSET (orig_binfo));
3440 r = walk_subobject_offsets (binfo,
3442 binfo_offset,
3443 offsets,
3444 max_offset,
3445 (abi_version_at_least (2)
3446 ? /*vbases_p=*/0 : vbases_p));
3447 if (r)
3448 return r;
3451 if (abi_version_at_least (2))
3453 tree vbase;
3455 /* Iterate through the virtual base classes of TYPE. In G++
3456 3.2, we included virtual bases in the direct base class
3457 loop above, which results in incorrect results; the
3458 correct offsets for virtual bases are only known when
3459 working with the most derived type. */
3460 if (vbases_p)
3461 for (vbase = CLASSTYPE_VBASECLASSES (type);
3462 vbase;
3463 vbase = TREE_CHAIN (vbase))
3465 binfo = TREE_VALUE (vbase);
3466 r = walk_subobject_offsets (binfo,
3468 size_binop (PLUS_EXPR,
3469 offset,
3470 BINFO_OFFSET (binfo)),
3471 offsets,
3472 max_offset,
3473 /*vbases_p=*/0);
3474 if (r)
3475 return r;
3477 else
3479 /* We still have to walk the primary base, if it is
3480 virtual. (If it is non-virtual, then it was walked
3481 above.) */
3482 vbase = get_primary_binfo (type_binfo);
3483 if (vbase && TREE_VIA_VIRTUAL (vbase)
3484 && BINFO_PRIMARY_BASE_OF (vbase) == type_binfo)
3486 r = (walk_subobject_offsets
3487 (vbase, f, offset,
3488 offsets, max_offset, /*vbases_p=*/0));
3489 if (r)
3490 return r;
3495 /* Iterate through the fields of TYPE. */
3496 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3497 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3499 tree field_offset;
3501 if (abi_version_at_least (2))
3502 field_offset = byte_position (field);
3503 else
3504 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3505 field_offset = DECL_FIELD_OFFSET (field);
3507 r = walk_subobject_offsets (TREE_TYPE (field),
3509 size_binop (PLUS_EXPR,
3510 offset,
3511 field_offset),
3512 offsets,
3513 max_offset,
3514 /*vbases_p=*/1);
3515 if (r)
3516 return r;
3519 else if (TREE_CODE (type) == ARRAY_TYPE)
3521 tree element_type = strip_array_types (type);
3522 tree domain = TYPE_DOMAIN (type);
3523 tree index;
3525 /* Avoid recursing into objects that are not interesting. */
3526 if (!CLASS_TYPE_P (element_type)
3527 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3528 return 0;
3530 /* Step through each of the elements in the array. */
3531 for (index = size_zero_node;
3532 /* G++ 3.2 had an off-by-one error here. */
3533 (abi_version_at_least (2)
3534 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3535 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3536 index = size_binop (PLUS_EXPR, index, size_one_node))
3538 r = walk_subobject_offsets (TREE_TYPE (type),
3540 offset,
3541 offsets,
3542 max_offset,
3543 /*vbases_p=*/1);
3544 if (r)
3545 return r;
3546 offset = size_binop (PLUS_EXPR, offset,
3547 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3548 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3549 there's no point in iterating through the remaining
3550 elements of the array. */
3551 if (max_offset && INT_CST_LT (max_offset, offset))
3552 break;
3556 return 0;
3559 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3560 OFFSETS. If VBASES_P is nonzero, virtual bases of TYPE are
3561 examined. */
3563 static void
3564 record_subobject_offsets (tree type,
3565 tree offset,
3566 splay_tree offsets,
3567 int vbases_p)
3569 walk_subobject_offsets (type, record_subobject_offset, offset,
3570 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3573 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3574 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3575 virtual bases of TYPE are examined. */
3577 static int
3578 layout_conflict_p (tree type,
3579 tree offset,
3580 splay_tree offsets,
3581 int vbases_p)
3583 splay_tree_node max_node;
3585 /* Get the node in OFFSETS that indicates the maximum offset where
3586 an empty subobject is located. */
3587 max_node = splay_tree_max (offsets);
3588 /* If there aren't any empty subobjects, then there's no point in
3589 performing this check. */
3590 if (!max_node)
3591 return 0;
3593 return walk_subobject_offsets (type, check_subobject_offset, offset,
3594 offsets, (tree) (max_node->key),
3595 vbases_p);
3598 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3599 non-static data member of the type indicated by RLI. BINFO is the
3600 binfo corresponding to the base subobject, OFFSETS maps offsets to
3601 types already located at those offsets. This function determines
3602 the position of the DECL. */
3604 static void
3605 layout_nonempty_base_or_field (record_layout_info rli,
3606 tree decl,
3607 tree binfo,
3608 splay_tree offsets)
3610 tree offset = NULL_TREE;
3611 bool field_p;
3612 tree type;
3614 if (binfo)
3616 /* For the purposes of determining layout conflicts, we want to
3617 use the class type of BINFO; TREE_TYPE (DECL) will be the
3618 CLASSTYPE_AS_BASE version, which does not contain entries for
3619 zero-sized bases. */
3620 type = TREE_TYPE (binfo);
3621 field_p = false;
3623 else
3625 type = TREE_TYPE (decl);
3626 field_p = true;
3629 /* Try to place the field. It may take more than one try if we have
3630 a hard time placing the field without putting two objects of the
3631 same type at the same address. */
3632 while (1)
3634 struct record_layout_info_s old_rli = *rli;
3636 /* Place this field. */
3637 place_field (rli, decl);
3638 offset = byte_position (decl);
3640 /* We have to check to see whether or not there is already
3641 something of the same type at the offset we're about to use.
3642 For example:
3644 struct S {};
3645 struct T : public S { int i; };
3646 struct U : public S, public T {};
3648 Here, we put S at offset zero in U. Then, we can't put T at
3649 offset zero -- its S component would be at the same address
3650 as the S we already allocated. So, we have to skip ahead.
3651 Since all data members, including those whose type is an
3652 empty class, have nonzero size, any overlap can happen only
3653 with a direct or indirect base-class -- it can't happen with
3654 a data member. */
3655 /* G++ 3.2 did not check for overlaps when placing a non-empty
3656 virtual base. */
3657 if (!abi_version_at_least (2) && binfo && TREE_VIA_VIRTUAL (binfo))
3658 break;
3659 if (layout_conflict_p (field_p ? type : binfo, offset,
3660 offsets, field_p))
3662 /* Strip off the size allocated to this field. That puts us
3663 at the first place we could have put the field with
3664 proper alignment. */
3665 *rli = old_rli;
3667 /* Bump up by the alignment required for the type. */
3668 rli->bitpos
3669 = size_binop (PLUS_EXPR, rli->bitpos,
3670 bitsize_int (binfo
3671 ? CLASSTYPE_ALIGN (type)
3672 : TYPE_ALIGN (type)));
3673 normalize_rli (rli);
3675 else
3676 /* There was no conflict. We're done laying out this field. */
3677 break;
3680 /* Now that we know where it will be placed, update its
3681 BINFO_OFFSET. */
3682 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3683 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3684 this point because their BINFO_OFFSET is copied from another
3685 hierarchy. Therefore, we may not need to add the entire
3686 OFFSET. */
3687 propagate_binfo_offsets (binfo,
3688 size_diffop (convert (ssizetype, offset),
3689 convert (ssizetype,
3690 BINFO_OFFSET (binfo))));
3693 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3695 static int
3696 empty_base_at_nonzero_offset_p (tree type,
3697 tree offset,
3698 splay_tree offsets ATTRIBUTE_UNUSED)
3700 return is_empty_class (type) && !integer_zerop (offset);
3703 /* Layout the empty base BINFO. EOC indicates the byte currently just
3704 past the end of the class, and should be correctly aligned for a
3705 class of the type indicated by BINFO; OFFSETS gives the offsets of
3706 the empty bases allocated so far. T is the most derived
3707 type. Return nonzero iff we added it at the end. */
3709 static bool
3710 layout_empty_base (tree binfo, tree eoc, splay_tree offsets)
3712 tree alignment;
3713 tree basetype = BINFO_TYPE (binfo);
3714 bool atend = false;
3716 /* This routine should only be used for empty classes. */
3717 my_friendly_assert (is_empty_class (basetype), 20000321);
3718 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3720 if (abi_version_at_least (2))
3721 BINFO_OFFSET (binfo) = size_zero_node;
3722 if (warn_abi && !integer_zerop (BINFO_OFFSET (binfo)))
3723 warning ("offset of empty base `%T' may not be ABI-compliant and may"
3724 "change in a future version of GCC",
3725 BINFO_TYPE (binfo));
3727 /* This is an empty base class. We first try to put it at offset
3728 zero. */
3729 if (layout_conflict_p (binfo,
3730 BINFO_OFFSET (binfo),
3731 offsets,
3732 /*vbases_p=*/0))
3734 /* That didn't work. Now, we move forward from the next
3735 available spot in the class. */
3736 atend = true;
3737 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3738 while (1)
3740 if (!layout_conflict_p (binfo,
3741 BINFO_OFFSET (binfo),
3742 offsets,
3743 /*vbases_p=*/0))
3744 /* We finally found a spot where there's no overlap. */
3745 break;
3747 /* There's overlap here, too. Bump along to the next spot. */
3748 propagate_binfo_offsets (binfo, alignment);
3751 return atend;
3754 /* Layout the the base given by BINFO in the class indicated by RLI.
3755 *BASE_ALIGN is a running maximum of the alignments of
3756 any base class. OFFSETS gives the location of empty base
3757 subobjects. T is the most derived type. Return nonzero if the new
3758 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3759 *NEXT_FIELD, unless BINFO is for an empty base class.
3761 Returns the location at which the next field should be inserted. */
3763 static tree *
3764 build_base_field (record_layout_info rli, tree binfo,
3765 splay_tree offsets, tree *next_field)
3767 tree t = rli->t;
3768 tree basetype = BINFO_TYPE (binfo);
3770 if (!COMPLETE_TYPE_P (basetype))
3771 /* This error is now reported in xref_tag, thus giving better
3772 location information. */
3773 return next_field;
3775 /* Place the base class. */
3776 if (!is_empty_class (basetype))
3778 tree decl;
3780 /* The containing class is non-empty because it has a non-empty
3781 base class. */
3782 CLASSTYPE_EMPTY_P (t) = 0;
3784 /* Create the FIELD_DECL. */
3785 decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3786 DECL_ARTIFICIAL (decl) = 1;
3787 DECL_FIELD_CONTEXT (decl) = t;
3788 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3789 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3790 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3791 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3792 DECL_IGNORED_P (decl) = 1;
3794 /* Try to place the field. It may take more than one try if we
3795 have a hard time placing the field without putting two
3796 objects of the same type at the same address. */
3797 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3798 /* Add the new FIELD_DECL to the list of fields for T. */
3799 TREE_CHAIN (decl) = *next_field;
3800 *next_field = decl;
3801 next_field = &TREE_CHAIN (decl);
3803 else
3805 tree eoc;
3806 bool atend;
3808 /* On some platforms (ARM), even empty classes will not be
3809 byte-aligned. */
3810 eoc = round_up (rli_size_unit_so_far (rli),
3811 CLASSTYPE_ALIGN_UNIT (basetype));
3812 atend = layout_empty_base (binfo, eoc, offsets);
3813 /* A nearly-empty class "has no proper base class that is empty,
3814 not morally virtual, and at an offset other than zero." */
3815 if (!TREE_VIA_VIRTUAL (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3817 if (atend)
3818 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3819 /* The check above (used in G++ 3.2) is insufficient because
3820 an empty class placed at offset zero might itself have an
3821 empty base at a nonzero offset. */
3822 else if (walk_subobject_offsets (basetype,
3823 empty_base_at_nonzero_offset_p,
3824 size_zero_node,
3825 /*offsets=*/NULL,
3826 /*max_offset=*/NULL_TREE,
3827 /*vbases_p=*/true))
3829 if (abi_version_at_least (2))
3830 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3831 else if (warn_abi)
3832 warning ("class `%T' will be considered nearly empty in a "
3833 "future version of GCC", t);
3837 /* We do not create a FIELD_DECL for empty base classes because
3838 it might overlap some other field. We want to be able to
3839 create CONSTRUCTORs for the class by iterating over the
3840 FIELD_DECLs, and the back end does not handle overlapping
3841 FIELD_DECLs. */
3843 /* An empty virtual base causes a class to be non-empty
3844 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3845 here because that was already done when the virtual table
3846 pointer was created. */
3849 /* Record the offsets of BINFO and its base subobjects. */
3850 record_subobject_offsets (binfo,
3851 BINFO_OFFSET (binfo),
3852 offsets,
3853 /*vbases_p=*/0);
3855 return next_field;
3858 /* Layout all of the non-virtual base classes. Record empty
3859 subobjects in OFFSETS. T is the most derived type. Return nonzero
3860 if the type cannot be nearly empty. The fields created
3861 corresponding to the base classes will be inserted at
3862 *NEXT_FIELD. */
3864 static void
3865 build_base_fields (record_layout_info rli,
3866 splay_tree offsets, tree *next_field)
3868 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3869 subobjects. */
3870 tree t = rli->t;
3871 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
3872 int i;
3874 /* The primary base class is always allocated first. */
3875 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3876 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
3877 offsets, next_field);
3879 /* Now allocate the rest of the bases. */
3880 for (i = 0; i < n_baseclasses; ++i)
3882 tree base_binfo;
3884 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
3886 /* The primary base was already allocated above, so we don't
3887 need to allocate it again here. */
3888 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
3889 continue;
3891 /* Virtual bases are added at the end (a primary virtual base
3892 will have already been added). */
3893 if (TREE_VIA_VIRTUAL (base_binfo))
3894 continue;
3896 next_field = build_base_field (rli, base_binfo,
3897 offsets, next_field);
3901 /* Go through the TYPE_METHODS of T issuing any appropriate
3902 diagnostics, figuring out which methods override which other
3903 methods, and so forth. */
3905 static void
3906 check_methods (tree t)
3908 tree x;
3910 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3912 /* If this was an evil function, don't keep it in class. */
3913 if (DECL_ASSEMBLER_NAME_SET_P (x)
3914 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3915 continue;
3917 check_for_override (x, t);
3918 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3919 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3921 /* The name of the field is the original field name
3922 Save this in auxiliary field for later overloading. */
3923 if (DECL_VINDEX (x))
3925 TYPE_POLYMORPHIC_P (t) = 1;
3926 if (DECL_PURE_VIRTUAL_P (x))
3927 CLASSTYPE_PURE_VIRTUALS (t)
3928 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3933 /* FN is a constructor or destructor. Clone the declaration to create
3934 a specialized in-charge or not-in-charge version, as indicated by
3935 NAME. */
3937 static tree
3938 build_clone (tree fn, tree name)
3940 tree parms;
3941 tree clone;
3943 /* Copy the function. */
3944 clone = copy_decl (fn);
3945 /* Remember where this function came from. */
3946 DECL_CLONED_FUNCTION (clone) = fn;
3947 DECL_ABSTRACT_ORIGIN (clone) = fn;
3948 /* Reset the function name. */
3949 DECL_NAME (clone) = name;
3950 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3951 /* There's no pending inline data for this function. */
3952 DECL_PENDING_INLINE_INFO (clone) = NULL;
3953 DECL_PENDING_INLINE_P (clone) = 0;
3954 /* And it hasn't yet been deferred. */
3955 DECL_DEFERRED_FN (clone) = 0;
3957 /* The base-class destructor is not virtual. */
3958 if (name == base_dtor_identifier)
3960 DECL_VIRTUAL_P (clone) = 0;
3961 if (TREE_CODE (clone) != TEMPLATE_DECL)
3962 DECL_VINDEX (clone) = NULL_TREE;
3965 /* If there was an in-charge parameter, drop it from the function
3966 type. */
3967 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3969 tree basetype;
3970 tree parmtypes;
3971 tree exceptions;
3973 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3974 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3975 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3976 /* Skip the `this' parameter. */
3977 parmtypes = TREE_CHAIN (parmtypes);
3978 /* Skip the in-charge parameter. */
3979 parmtypes = TREE_CHAIN (parmtypes);
3980 /* And the VTT parm, in a complete [cd]tor. */
3981 if (DECL_HAS_VTT_PARM_P (fn)
3982 && ! DECL_NEEDS_VTT_PARM_P (clone))
3983 parmtypes = TREE_CHAIN (parmtypes);
3984 /* If this is subobject constructor or destructor, add the vtt
3985 parameter. */
3986 TREE_TYPE (clone)
3987 = build_cplus_method_type (basetype,
3988 TREE_TYPE (TREE_TYPE (clone)),
3989 parmtypes);
3990 if (exceptions)
3991 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3992 exceptions);
3995 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
3996 aren't function parameters; those are the template parameters. */
3997 if (TREE_CODE (clone) != TEMPLATE_DECL)
3999 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4000 /* Remove the in-charge parameter. */
4001 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4003 TREE_CHAIN (DECL_ARGUMENTS (clone))
4004 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4005 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4007 /* And the VTT parm, in a complete [cd]tor. */
4008 if (DECL_HAS_VTT_PARM_P (fn))
4010 if (DECL_NEEDS_VTT_PARM_P (clone))
4011 DECL_HAS_VTT_PARM_P (clone) = 1;
4012 else
4014 TREE_CHAIN (DECL_ARGUMENTS (clone))
4015 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4016 DECL_HAS_VTT_PARM_P (clone) = 0;
4020 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4022 DECL_CONTEXT (parms) = clone;
4023 cxx_dup_lang_specific_decl (parms);
4027 /* Create the RTL for this function. */
4028 SET_DECL_RTL (clone, NULL_RTX);
4029 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4031 /* Make it easy to find the CLONE given the FN. */
4032 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4033 TREE_CHAIN (fn) = clone;
4035 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4036 if (TREE_CODE (clone) == TEMPLATE_DECL)
4038 tree result;
4040 DECL_TEMPLATE_RESULT (clone)
4041 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4042 result = DECL_TEMPLATE_RESULT (clone);
4043 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4044 DECL_TI_TEMPLATE (result) = clone;
4046 else if (DECL_DEFERRED_FN (fn))
4047 defer_fn (clone);
4049 return clone;
4052 /* Produce declarations for all appropriate clones of FN. If
4053 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4054 CLASTYPE_METHOD_VEC as well. */
4056 void
4057 clone_function_decl (tree fn, int update_method_vec_p)
4059 tree clone;
4061 /* Avoid inappropriate cloning. */
4062 if (TREE_CHAIN (fn)
4063 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4064 return;
4066 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4068 /* For each constructor, we need two variants: an in-charge version
4069 and a not-in-charge version. */
4070 clone = build_clone (fn, complete_ctor_identifier);
4071 if (update_method_vec_p)
4072 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4073 clone = build_clone (fn, base_ctor_identifier);
4074 if (update_method_vec_p)
4075 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4077 else
4079 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4081 /* For each destructor, we need three variants: an in-charge
4082 version, a not-in-charge version, and an in-charge deleting
4083 version. We clone the deleting version first because that
4084 means it will go second on the TYPE_METHODS list -- and that
4085 corresponds to the correct layout order in the virtual
4086 function table.
4088 For a non-virtual destructor, we do not build a deleting
4089 destructor. */
4090 if (DECL_VIRTUAL_P (fn))
4092 clone = build_clone (fn, deleting_dtor_identifier);
4093 if (update_method_vec_p)
4094 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4096 clone = build_clone (fn, complete_dtor_identifier);
4097 if (update_method_vec_p)
4098 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4099 clone = build_clone (fn, base_dtor_identifier);
4100 if (update_method_vec_p)
4101 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4104 /* Note that this is an abstract function that is never emitted. */
4105 DECL_ABSTRACT (fn) = 1;
4108 /* DECL is an in charge constructor, which is being defined. This will
4109 have had an in class declaration, from whence clones were
4110 declared. An out-of-class definition can specify additional default
4111 arguments. As it is the clones that are involved in overload
4112 resolution, we must propagate the information from the DECL to its
4113 clones. */
4115 void
4116 adjust_clone_args (tree decl)
4118 tree clone;
4120 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4121 clone = TREE_CHAIN (clone))
4123 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4124 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4125 tree decl_parms, clone_parms;
4127 clone_parms = orig_clone_parms;
4129 /* Skip the 'this' parameter. */
4130 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4131 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4133 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4134 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4135 if (DECL_HAS_VTT_PARM_P (decl))
4136 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4138 clone_parms = orig_clone_parms;
4139 if (DECL_HAS_VTT_PARM_P (clone))
4140 clone_parms = TREE_CHAIN (clone_parms);
4142 for (decl_parms = orig_decl_parms; decl_parms;
4143 decl_parms = TREE_CHAIN (decl_parms),
4144 clone_parms = TREE_CHAIN (clone_parms))
4146 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4147 TREE_TYPE (clone_parms)), 20010424);
4149 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4151 /* A default parameter has been added. Adjust the
4152 clone's parameters. */
4153 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4154 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4155 tree type;
4157 clone_parms = orig_decl_parms;
4159 if (DECL_HAS_VTT_PARM_P (clone))
4161 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4162 TREE_VALUE (orig_clone_parms),
4163 clone_parms);
4164 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4166 type = build_cplus_method_type (basetype,
4167 TREE_TYPE (TREE_TYPE (clone)),
4168 clone_parms);
4169 if (exceptions)
4170 type = build_exception_variant (type, exceptions);
4171 TREE_TYPE (clone) = type;
4173 clone_parms = NULL_TREE;
4174 break;
4177 my_friendly_assert (!clone_parms, 20010424);
4181 /* For each of the constructors and destructors in T, create an
4182 in-charge and not-in-charge variant. */
4184 static void
4185 clone_constructors_and_destructors (tree t)
4187 tree fns;
4189 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4190 out now. */
4191 if (!CLASSTYPE_METHOD_VEC (t))
4192 return;
4194 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4195 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4196 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4197 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4200 /* Remove all zero-width bit-fields from T. */
4202 static void
4203 remove_zero_width_bit_fields (tree t)
4205 tree *fieldsp;
4207 fieldsp = &TYPE_FIELDS (t);
4208 while (*fieldsp)
4210 if (TREE_CODE (*fieldsp) == FIELD_DECL
4211 && DECL_C_BIT_FIELD (*fieldsp)
4212 && DECL_INITIAL (*fieldsp))
4213 *fieldsp = TREE_CHAIN (*fieldsp);
4214 else
4215 fieldsp = &TREE_CHAIN (*fieldsp);
4219 /* Returns TRUE iff we need a cookie when dynamically allocating an
4220 array whose elements have the indicated class TYPE. */
4222 static bool
4223 type_requires_array_cookie (tree type)
4225 tree fns;
4226 bool has_two_argument_delete_p = false;
4228 my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4230 /* If there's a non-trivial destructor, we need a cookie. In order
4231 to iterate through the array calling the destructor for each
4232 element, we'll have to know how many elements there are. */
4233 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4234 return true;
4236 /* If the usual deallocation function is a two-argument whose second
4237 argument is of type `size_t', then we have to pass the size of
4238 the array to the deallocation function, so we will need to store
4239 a cookie. */
4240 fns = lookup_fnfields (TYPE_BINFO (type),
4241 ansi_opname (VEC_DELETE_EXPR),
4242 /*protect=*/0);
4243 /* If there are no `operator []' members, or the lookup is
4244 ambiguous, then we don't need a cookie. */
4245 if (!fns || fns == error_mark_node)
4246 return false;
4247 /* Loop through all of the functions. */
4248 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4250 tree fn;
4251 tree second_parm;
4253 /* Select the current function. */
4254 fn = OVL_CURRENT (fns);
4255 /* See if this function is a one-argument delete function. If
4256 it is, then it will be the usual deallocation function. */
4257 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4258 if (second_parm == void_list_node)
4259 return false;
4260 /* Otherwise, if we have a two-argument function and the second
4261 argument is `size_t', it will be the usual deallocation
4262 function -- unless there is one-argument function, too. */
4263 if (TREE_CHAIN (second_parm) == void_list_node
4264 && same_type_p (TREE_VALUE (second_parm), sizetype))
4265 has_two_argument_delete_p = true;
4268 return has_two_argument_delete_p;
4271 /* Check the validity of the bases and members declared in T. Add any
4272 implicitly-generated functions (like copy-constructors and
4273 assignment operators). Compute various flag bits (like
4274 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4275 level: i.e., independently of the ABI in use. */
4277 static void
4278 check_bases_and_members (tree t)
4280 /* Nonzero if we are not allowed to generate a default constructor
4281 for this case. */
4282 int cant_have_default_ctor;
4283 /* Nonzero if the implicitly generated copy constructor should take
4284 a non-const reference argument. */
4285 int cant_have_const_ctor;
4286 /* Nonzero if the the implicitly generated assignment operator
4287 should take a non-const reference argument. */
4288 int no_const_asn_ref;
4289 tree access_decls;
4291 /* By default, we use const reference arguments and generate default
4292 constructors. */
4293 cant_have_default_ctor = 0;
4294 cant_have_const_ctor = 0;
4295 no_const_asn_ref = 0;
4297 /* Check all the base-classes. */
4298 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4299 &no_const_asn_ref);
4301 /* Check all the data member declarations. */
4302 check_field_decls (t, &access_decls,
4303 &cant_have_default_ctor,
4304 &cant_have_const_ctor,
4305 &no_const_asn_ref);
4307 /* Check all the method declarations. */
4308 check_methods (t);
4310 /* A nearly-empty class has to be vptr-containing; a nearly empty
4311 class contains just a vptr. */
4312 if (!TYPE_CONTAINS_VPTR_P (t))
4313 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4315 /* Do some bookkeeping that will guide the generation of implicitly
4316 declared member functions. */
4317 TYPE_HAS_COMPLEX_INIT_REF (t)
4318 |= (TYPE_HAS_INIT_REF (t)
4319 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4320 || TYPE_POLYMORPHIC_P (t));
4321 TYPE_NEEDS_CONSTRUCTING (t)
4322 |= (TYPE_HAS_CONSTRUCTOR (t)
4323 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4324 || TYPE_POLYMORPHIC_P (t));
4325 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4326 || TYPE_POLYMORPHIC_P (t));
4327 CLASSTYPE_NON_POD_P (t)
4328 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4329 || TYPE_HAS_ASSIGN_REF (t));
4330 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4331 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4332 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4334 /* Synthesize any needed methods. Note that methods will be synthesized
4335 for anonymous unions; grok_x_components undoes that. */
4336 add_implicitly_declared_members (t, cant_have_default_ctor,
4337 cant_have_const_ctor,
4338 no_const_asn_ref);
4340 /* Create the in-charge and not-in-charge variants of constructors
4341 and destructors. */
4342 clone_constructors_and_destructors (t);
4344 /* Process the using-declarations. */
4345 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4346 handle_using_decl (TREE_VALUE (access_decls), t);
4348 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4349 finish_struct_methods (t);
4351 /* Figure out whether or not we will need a cookie when dynamically
4352 allocating an array of this type. */
4353 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4354 = type_requires_array_cookie (t);
4357 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4358 accordingly. If a new vfield was created (because T doesn't have a
4359 primary base class), then the newly created field is returned. It
4360 is not added to the TYPE_FIELDS list; it is the caller's
4361 responsibility to do that. Accumulate declared virtual functions
4362 on VIRTUALS_P. */
4364 static tree
4365 create_vtable_ptr (tree t, tree* virtuals_p)
4367 tree fn;
4369 /* Collect the virtual functions declared in T. */
4370 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4371 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4372 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4374 tree new_virtual = make_node (TREE_LIST);
4376 BV_FN (new_virtual) = fn;
4377 BV_DELTA (new_virtual) = integer_zero_node;
4379 TREE_CHAIN (new_virtual) = *virtuals_p;
4380 *virtuals_p = new_virtual;
4383 /* If we couldn't find an appropriate base class, create a new field
4384 here. Even if there weren't any new virtual functions, we might need a
4385 new virtual function table if we're supposed to include vptrs in
4386 all classes that need them. */
4387 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
4389 /* We build this decl with vtbl_ptr_type_node, which is a
4390 `vtable_entry_type*'. It might seem more precise to use
4391 `vtable_entry_type (*)[N]' where N is the number of firtual
4392 functions. However, that would require the vtable pointer in
4393 base classes to have a different type than the vtable pointer
4394 in derived classes. We could make that happen, but that
4395 still wouldn't solve all the problems. In particular, the
4396 type-based alias analysis code would decide that assignments
4397 to the base class vtable pointer can't alias assignments to
4398 the derived class vtable pointer, since they have different
4399 types. Thus, in a derived class destructor, where the base
4400 class constructor was inlined, we could generate bad code for
4401 setting up the vtable pointer.
4403 Therefore, we use one type for all vtable pointers. We still
4404 use a type-correct type; it's just doesn't indicate the array
4405 bounds. That's better than using `void*' or some such; it's
4406 cleaner, and it let's the alias analysis code know that these
4407 stores cannot alias stores to void*! */
4408 tree field;
4410 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4411 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4412 DECL_VIRTUAL_P (field) = 1;
4413 DECL_ARTIFICIAL (field) = 1;
4414 DECL_FIELD_CONTEXT (field) = t;
4415 DECL_FCONTEXT (field) = t;
4416 DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4417 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4419 TYPE_VFIELD (t) = field;
4421 /* This class is non-empty. */
4422 CLASSTYPE_EMPTY_P (t) = 0;
4424 if (CLASSTYPE_N_BASECLASSES (t))
4425 /* If there were any baseclasses, they can't possibly be at
4426 offset zero any more, because that's where the vtable
4427 pointer is. So, converting to a base class is going to
4428 take work. */
4429 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4431 return field;
4434 return NULL_TREE;
4437 /* Fixup the inline function given by INFO now that the class is
4438 complete. */
4440 static void
4441 fixup_pending_inline (tree fn)
4443 if (DECL_PENDING_INLINE_INFO (fn))
4445 tree args = DECL_ARGUMENTS (fn);
4446 while (args)
4448 DECL_CONTEXT (args) = fn;
4449 args = TREE_CHAIN (args);
4454 /* Fixup the inline methods and friends in TYPE now that TYPE is
4455 complete. */
4457 static void
4458 fixup_inline_methods (tree type)
4460 tree method = TYPE_METHODS (type);
4462 if (method && TREE_CODE (method) == TREE_VEC)
4464 if (TREE_VEC_ELT (method, 1))
4465 method = TREE_VEC_ELT (method, 1);
4466 else if (TREE_VEC_ELT (method, 0))
4467 method = TREE_VEC_ELT (method, 0);
4468 else
4469 method = TREE_VEC_ELT (method, 2);
4472 /* Do inline member functions. */
4473 for (; method; method = TREE_CHAIN (method))
4474 fixup_pending_inline (method);
4476 /* Do friends. */
4477 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4478 method;
4479 method = TREE_CHAIN (method))
4480 fixup_pending_inline (TREE_VALUE (method));
4481 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4484 /* Add OFFSET to all base types of BINFO which is a base in the
4485 hierarchy dominated by T.
4487 OFFSET, which is a type offset, is number of bytes. */
4489 static void
4490 propagate_binfo_offsets (tree binfo, tree offset)
4492 int i;
4493 tree primary_binfo;
4495 /* Update BINFO's offset. */
4496 BINFO_OFFSET (binfo)
4497 = convert (sizetype,
4498 size_binop (PLUS_EXPR,
4499 convert (ssizetype, BINFO_OFFSET (binfo)),
4500 offset));
4502 /* Find the primary base class. */
4503 primary_binfo = get_primary_binfo (binfo);
4505 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4506 downwards. */
4507 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4509 tree base_binfo;
4511 /* On the first time through the loop, do the primary base.
4512 Because the primary base need not be an immediate base, we
4513 must handle the primary base specially. */
4514 if (i == -1)
4516 if (!primary_binfo)
4517 continue;
4519 base_binfo = primary_binfo;
4521 else
4523 base_binfo = BINFO_BASETYPE (binfo, i);
4524 /* Don't do the primary base twice. */
4525 if (base_binfo == primary_binfo)
4526 continue;
4529 /* Skip virtual bases that aren't our canonical primary base. */
4530 if (TREE_VIA_VIRTUAL (base_binfo)
4531 && BINFO_PRIMARY_BASE_OF (base_binfo) != binfo)
4532 continue;
4534 propagate_binfo_offsets (base_binfo, offset);
4538 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
4539 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4540 empty subobjects of T. */
4542 static void
4543 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
4545 tree vbase;
4546 tree t = rli->t;
4547 bool first_vbase = true;
4548 tree *next_field;
4550 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4551 return;
4553 if (!abi_version_at_least(2))
4555 /* In G++ 3.2, we incorrectly rounded the size before laying out
4556 the virtual bases. */
4557 finish_record_layout (rli, /*free_p=*/false);
4558 #ifdef STRUCTURE_SIZE_BOUNDARY
4559 /* Packed structures don't need to have minimum size. */
4560 if (! TYPE_PACKED (t))
4561 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
4562 #endif
4563 rli->offset = TYPE_SIZE_UNIT (t);
4564 rli->bitpos = bitsize_zero_node;
4565 rli->record_align = TYPE_ALIGN (t);
4568 /* Find the last field. The artificial fields created for virtual
4569 bases will go after the last extant field to date. */
4570 next_field = &TYPE_FIELDS (t);
4571 while (*next_field)
4572 next_field = &TREE_CHAIN (*next_field);
4574 /* Go through the virtual bases, allocating space for each virtual
4575 base that is not already a primary base class. These are
4576 allocated in inheritance graph order. */
4577 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
4579 if (!TREE_VIA_VIRTUAL (vbase))
4580 continue;
4582 if (!BINFO_PRIMARY_P (vbase))
4584 tree basetype = TREE_TYPE (vbase);
4586 /* This virtual base is not a primary base of any class in the
4587 hierarchy, so we have to add space for it. */
4588 next_field = build_base_field (rli, vbase,
4589 offsets, next_field);
4591 /* If the first virtual base might have been placed at a
4592 lower address, had we started from CLASSTYPE_SIZE, rather
4593 than TYPE_SIZE, issue a warning. There can be both false
4594 positives and false negatives from this warning in rare
4595 cases; to deal with all the possibilities would probably
4596 require performing both layout algorithms and comparing
4597 the results which is not particularly tractable. */
4598 if (warn_abi
4599 && first_vbase
4600 && (tree_int_cst_lt
4601 (size_binop (CEIL_DIV_EXPR,
4602 round_up (CLASSTYPE_SIZE (t),
4603 CLASSTYPE_ALIGN (basetype)),
4604 bitsize_unit_node),
4605 BINFO_OFFSET (vbase))))
4606 warning ("offset of virtual base `%T' is not ABI-compliant and may change in a future version of GCC",
4607 basetype);
4609 first_vbase = false;
4614 /* Returns the offset of the byte just past the end of the base class
4615 BINFO. */
4617 static tree
4618 end_of_base (tree binfo)
4620 tree size;
4622 if (is_empty_class (BINFO_TYPE (binfo)))
4623 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4624 allocate some space for it. It cannot have virtual bases, so
4625 TYPE_SIZE_UNIT is fine. */
4626 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4627 else
4628 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4630 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
4633 /* Returns the offset of the byte just past the end of the base class
4634 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4635 only non-virtual bases are included. */
4637 static tree
4638 end_of_class (tree t, int include_virtuals_p)
4640 tree result = size_zero_node;
4641 tree binfo;
4642 tree offset;
4643 int i;
4645 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4647 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4649 if (!include_virtuals_p
4650 && TREE_VIA_VIRTUAL (binfo)
4651 && !BINFO_PRIMARY_P (binfo))
4652 continue;
4654 offset = end_of_base (binfo);
4655 if (INT_CST_LT_UNSIGNED (result, offset))
4656 result = offset;
4659 /* G++ 3.2 did not check indirect virtual bases. */
4660 if (abi_version_at_least (2) && include_virtuals_p)
4661 for (binfo = CLASSTYPE_VBASECLASSES (t);
4662 binfo;
4663 binfo = TREE_CHAIN (binfo))
4665 offset = end_of_base (TREE_VALUE (binfo));
4666 if (INT_CST_LT_UNSIGNED (result, offset))
4667 result = offset;
4670 return result;
4673 /* Warn about bases of T that are inaccessible because they are
4674 ambiguous. For example:
4676 struct S {};
4677 struct T : public S {};
4678 struct U : public S, public T {};
4680 Here, `(S*) new U' is not allowed because there are two `S'
4681 subobjects of U. */
4683 static void
4684 warn_about_ambiguous_bases (tree t)
4686 int i;
4687 tree vbases;
4688 tree basetype;
4690 /* Check direct bases. */
4691 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4693 basetype = TYPE_BINFO_BASETYPE (t, i);
4695 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4696 warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4697 basetype, t);
4700 /* Check for ambiguous virtual bases. */
4701 if (extra_warnings)
4702 for (vbases = CLASSTYPE_VBASECLASSES (t);
4703 vbases;
4704 vbases = TREE_CHAIN (vbases))
4706 basetype = BINFO_TYPE (TREE_VALUE (vbases));
4708 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4709 warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4710 basetype, t);
4714 /* Compare two INTEGER_CSTs K1 and K2. */
4716 static int
4717 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
4719 return tree_int_cst_compare ((tree) k1, (tree) k2);
4722 /* Increase the size indicated in RLI to account for empty classes
4723 that are "off the end" of the class. */
4725 static void
4726 include_empty_classes (record_layout_info rli)
4728 tree eoc;
4729 tree rli_size;
4731 /* It might be the case that we grew the class to allocate a
4732 zero-sized base class. That won't be reflected in RLI, yet,
4733 because we are willing to overlay multiple bases at the same
4734 offset. However, now we need to make sure that RLI is big enough
4735 to reflect the entire class. */
4736 eoc = end_of_class (rli->t,
4737 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
4738 rli_size = rli_size_unit_so_far (rli);
4739 if (TREE_CODE (rli_size) == INTEGER_CST
4740 && INT_CST_LT_UNSIGNED (rli_size, eoc))
4742 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4743 rli->bitpos
4744 = size_binop (PLUS_EXPR,
4745 rli->bitpos,
4746 size_binop (MULT_EXPR,
4747 convert (bitsizetype,
4748 size_binop (MINUS_EXPR,
4749 eoc, rli_size)),
4750 bitsize_int (BITS_PER_UNIT)));
4751 normalize_rli (rli);
4755 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4756 BINFO_OFFSETs for all of the base-classes. Position the vtable
4757 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
4759 static void
4760 layout_class_type (tree t, tree *virtuals_p)
4762 tree non_static_data_members;
4763 tree field;
4764 tree vptr;
4765 record_layout_info rli;
4766 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4767 types that appear at that offset. */
4768 splay_tree empty_base_offsets;
4769 /* True if the last field layed out was a bit-field. */
4770 bool last_field_was_bitfield = false;
4771 /* The location at which the next field should be inserted. */
4772 tree *next_field;
4773 /* T, as a base class. */
4774 tree base_t;
4776 /* Keep track of the first non-static data member. */
4777 non_static_data_members = TYPE_FIELDS (t);
4779 /* Start laying out the record. */
4780 rli = start_record_layout (t);
4782 /* If possible, we reuse the virtual function table pointer from one
4783 of our base classes. */
4784 determine_primary_base (t);
4786 /* Create a pointer to our virtual function table. */
4787 vptr = create_vtable_ptr (t, virtuals_p);
4789 /* The vptr is always the first thing in the class. */
4790 if (vptr)
4792 TREE_CHAIN (vptr) = TYPE_FIELDS (t);
4793 TYPE_FIELDS (t) = vptr;
4794 next_field = &TREE_CHAIN (vptr);
4795 place_field (rli, vptr);
4797 else
4798 next_field = &TYPE_FIELDS (t);
4800 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4801 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4802 NULL, NULL);
4803 build_base_fields (rli, empty_base_offsets, next_field);
4805 /* Layout the non-static data members. */
4806 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4808 tree type;
4809 tree padding;
4810 bool was_unnamed_p = false;
4812 /* We still pass things that aren't non-static data members to
4813 the back-end, in case it wants to do something with them. */
4814 if (TREE_CODE (field) != FIELD_DECL)
4816 place_field (rli, field);
4817 /* If the static data member has incomplete type, keep track
4818 of it so that it can be completed later. (The handling
4819 of pending statics in finish_record_layout is
4820 insufficient; consider:
4822 struct S1;
4823 struct S2 { static S1 s1; };
4825 At this point, finish_record_layout will be called, but
4826 S1 is still incomplete.) */
4827 if (TREE_CODE (field) == VAR_DECL)
4828 maybe_register_incomplete_var (field);
4829 continue;
4832 type = TREE_TYPE (field);
4834 /* If this field is a bit-field whose width is greater than its
4835 type, then there are some special rules for allocating
4836 it. */
4837 if (DECL_C_BIT_FIELD (field)
4838 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4840 integer_type_kind itk;
4841 tree integer_type;
4842 /* We must allocate the bits as if suitably aligned for the
4843 longest integer type that fits in this many bits. type
4844 of the field. Then, we are supposed to use the left over
4845 bits as additional padding. */
4846 for (itk = itk_char; itk != itk_none; ++itk)
4847 if (INT_CST_LT (DECL_SIZE (field),
4848 TYPE_SIZE (integer_types[itk])))
4849 break;
4851 /* ITK now indicates a type that is too large for the
4852 field. We have to back up by one to find the largest
4853 type that fits. */
4854 integer_type = integer_types[itk - 1];
4856 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
4857 /* In a union, the padding field must have the full width
4858 of the bit-field; all fields start at offset zero. */
4859 padding = DECL_SIZE (field);
4860 else
4862 if (warn_abi && TREE_CODE (t) == UNION_TYPE)
4863 warning ("size assigned to `%T' may not be "
4864 "ABI-compliant and may change in a future "
4865 "version of GCC",
4867 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4868 TYPE_SIZE (integer_type));
4870 #ifdef PCC_BITFIELD_TYPE_MATTERS
4871 /* An unnamed bitfield does not normally affect the
4872 alignment of the containing class on a target where
4873 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
4874 make any exceptions for unnamed bitfields when the
4875 bitfields are longer than their types. Therefore, we
4876 temporarily give the field a name. */
4877 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
4879 was_unnamed_p = true;
4880 DECL_NAME (field) = make_anon_name ();
4882 #endif
4883 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4884 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4885 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4887 else
4888 padding = NULL_TREE;
4890 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4891 empty_base_offsets);
4892 /* If the bit-field had no name originally, remove the name
4893 now. */
4894 if (was_unnamed_p)
4895 DECL_NAME (field) = NULL_TREE;
4897 /* Remember the location of any empty classes in FIELD. */
4898 if (abi_version_at_least (2))
4899 record_subobject_offsets (TREE_TYPE (field),
4900 byte_position(field),
4901 empty_base_offsets,
4902 /*vbases_p=*/1);
4904 /* If a bit-field does not immediately follow another bit-field,
4905 and yet it starts in the middle of a byte, we have failed to
4906 comply with the ABI. */
4907 if (warn_abi
4908 && DECL_C_BIT_FIELD (field)
4909 && !last_field_was_bitfield
4910 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
4911 DECL_FIELD_BIT_OFFSET (field),
4912 bitsize_unit_node)))
4913 cp_warning_at ("offset of `%D' is not ABI-compliant and may change in a future version of GCC",
4914 field);
4916 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
4917 offset of the field. */
4918 if (warn_abi
4919 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
4920 byte_position (field))
4921 && contains_empty_class_p (TREE_TYPE (field)))
4922 cp_warning_at ("`%D' contains empty classes which may cause base "
4923 "classes to be placed at different locations in a "
4924 "future version of GCC",
4925 field);
4927 /* If we needed additional padding after this field, add it
4928 now. */
4929 if (padding)
4931 tree padding_field;
4933 padding_field = build_decl (FIELD_DECL,
4934 NULL_TREE,
4935 char_type_node);
4936 DECL_BIT_FIELD (padding_field) = 1;
4937 DECL_SIZE (padding_field) = padding;
4938 DECL_ALIGN (padding_field) = 1;
4939 DECL_USER_ALIGN (padding_field) = 0;
4940 layout_nonempty_base_or_field (rli, padding_field,
4941 NULL_TREE,
4942 empty_base_offsets);
4945 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
4948 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
4950 /* Make sure that we are on a byte boundary so that the size of
4951 the class without virtual bases will always be a round number
4952 of bytes. */
4953 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4954 normalize_rli (rli);
4957 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
4958 padding. */
4959 if (!abi_version_at_least (2))
4960 include_empty_classes(rli);
4962 /* Delete all zero-width bit-fields from the list of fields. Now
4963 that the type is laid out they are no longer important. */
4964 remove_zero_width_bit_fields (t);
4966 /* Create the version of T used for virtual bases. We do not use
4967 make_aggr_type for this version; this is an artificial type. For
4968 a POD type, we just reuse T. */
4969 if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t))
4971 base_t = make_node (TREE_CODE (t));
4973 /* Set the size and alignment for the new type. In G++ 3.2, all
4974 empty classes were considered to have size zero when used as
4975 base classes. */
4976 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
4978 TYPE_SIZE (base_t) = bitsize_zero_node;
4979 TYPE_SIZE_UNIT (base_t) = size_zero_node;
4980 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
4981 warning ("layout of classes derived from empty class `%T' "
4982 "may change in a future version of GCC",
4985 else
4987 tree eoc;
4989 /* If the ABI version is not at least two, and the last
4990 field was a bit-field, RLI may not be on a byte
4991 boundary. In particular, rli_size_unit_so_far might
4992 indicate the last complete byte, while rli_size_so_far
4993 indicates the total number of bits used. Therefore,
4994 rli_size_so_far, rather than rli_size_unit_so_far, is
4995 used to compute TYPE_SIZE_UNIT. */
4996 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4997 TYPE_SIZE_UNIT (base_t)
4998 = size_binop (MAX_EXPR,
4999 convert (sizetype,
5000 size_binop (CEIL_DIV_EXPR,
5001 rli_size_so_far (rli),
5002 bitsize_int (BITS_PER_UNIT))),
5003 eoc);
5004 TYPE_SIZE (base_t)
5005 = size_binop (MAX_EXPR,
5006 rli_size_so_far (rli),
5007 size_binop (MULT_EXPR,
5008 convert (bitsizetype, eoc),
5009 bitsize_int (BITS_PER_UNIT)));
5011 TYPE_ALIGN (base_t) = rli->record_align;
5012 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
5014 /* Copy the fields from T. */
5015 next_field = &TYPE_FIELDS (base_t);
5016 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5017 if (TREE_CODE (field) == FIELD_DECL)
5019 *next_field = build_decl (FIELD_DECL,
5020 DECL_NAME (field),
5021 TREE_TYPE (field));
5022 DECL_CONTEXT (*next_field) = base_t;
5023 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
5024 DECL_FIELD_BIT_OFFSET (*next_field)
5025 = DECL_FIELD_BIT_OFFSET (field);
5026 next_field = &TREE_CHAIN (*next_field);
5029 /* Record the base version of the type. */
5030 CLASSTYPE_AS_BASE (t) = base_t;
5031 TYPE_CONTEXT (base_t) = t;
5033 else
5034 CLASSTYPE_AS_BASE (t) = t;
5036 /* Every empty class contains an empty class. */
5037 if (CLASSTYPE_EMPTY_P (t))
5038 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
5040 /* Set the TYPE_DECL for this type to contain the right
5041 value for DECL_OFFSET, so that we can use it as part
5042 of a COMPONENT_REF for multiple inheritance. */
5043 layout_decl (TYPE_MAIN_DECL (t), 0);
5045 /* Now fix up any virtual base class types that we left lying
5046 around. We must get these done before we try to lay out the
5047 virtual function table. As a side-effect, this will remove the
5048 base subobject fields. */
5049 layout_virtual_bases (rli, empty_base_offsets);
5051 /* Make sure that empty classes are reflected in RLI at this
5052 point. */
5053 include_empty_classes(rli);
5055 /* Make sure not to create any structures with zero size. */
5056 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
5057 place_field (rli,
5058 build_decl (FIELD_DECL, NULL_TREE, char_type_node));
5060 /* Let the back-end lay out the type. */
5061 finish_record_layout (rli, /*free_p=*/true);
5063 /* Warn about bases that can't be talked about due to ambiguity. */
5064 warn_about_ambiguous_bases (t);
5066 /* Clean up. */
5067 splay_tree_delete (empty_base_offsets);
5070 /* Returns the virtual function with which the vtable for TYPE is
5071 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
5073 static tree
5074 key_method (tree type)
5076 tree method;
5078 if (TYPE_FOR_JAVA (type)
5079 || processing_template_decl
5080 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
5081 || CLASSTYPE_INTERFACE_KNOWN (type))
5082 return NULL_TREE;
5084 for (method = TYPE_METHODS (type); method != NULL_TREE;
5085 method = TREE_CHAIN (method))
5086 if (DECL_VINDEX (method) != NULL_TREE
5087 && ! DECL_DECLARED_INLINE_P (method)
5088 && ! DECL_PURE_VIRTUAL_P (method))
5089 return method;
5091 return NULL_TREE;
5094 /* Perform processing required when the definition of T (a class type)
5095 is complete. */
5097 void
5098 finish_struct_1 (tree t)
5100 tree x;
5101 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
5102 tree virtuals = NULL_TREE;
5103 int n_fields = 0;
5104 tree vfield;
5106 if (COMPLETE_TYPE_P (t))
5108 if (IS_AGGR_TYPE (t))
5109 error ("redefinition of `%#T'", t);
5110 else
5111 abort ();
5112 popclass ();
5113 return;
5116 /* If this type was previously laid out as a forward reference,
5117 make sure we lay it out again. */
5118 TYPE_SIZE (t) = NULL_TREE;
5119 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5120 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5122 fixup_inline_methods (t);
5124 /* Make assumptions about the class; we'll reset the flags if
5125 necessary. */
5126 CLASSTYPE_EMPTY_P (t) = 1;
5127 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
5128 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
5130 /* Do end-of-class semantic processing: checking the validity of the
5131 bases and members and add implicitly generated methods. */
5132 check_bases_and_members (t);
5134 /* Find the key method */
5135 if (TYPE_CONTAINS_VPTR_P (t))
5137 CLASSTYPE_KEY_METHOD (t) = key_method (t);
5139 /* If a polymorphic class has no key method, we may emit the vtable
5140 in every translation unit where the class definition appears. */
5141 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
5142 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
5145 /* Layout the class itself. */
5146 layout_class_type (t, &virtuals);
5148 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5149 vfield = TYPE_VFIELD (t);
5150 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5152 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5154 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5155 BINFO_TYPE (primary)),
5156 20010726);
5157 /* The vtable better be at the start. */
5158 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5159 20010726);
5160 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5161 20010726);
5163 vfield = copy_decl (vfield);
5164 DECL_FIELD_CONTEXT (vfield) = t;
5165 TYPE_VFIELD (t) = vfield;
5167 else
5168 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5170 virtuals = modify_all_vtables (t, nreverse (virtuals));
5172 /* If we created a new vtbl pointer for this class, add it to the
5173 list. */
5174 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5175 CLASSTYPE_VFIELDS (t)
5176 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5178 /* If necessary, create the primary vtable for this class. */
5179 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
5181 /* We must enter these virtuals into the table. */
5182 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5183 build_primary_vtable (NULL_TREE, t);
5184 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
5185 /* Here we know enough to change the type of our virtual
5186 function table, but we will wait until later this function. */
5187 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5190 if (TYPE_CONTAINS_VPTR_P (t))
5192 int vindex;
5193 tree fn;
5195 if (TYPE_BINFO_VTABLE (t))
5196 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5197 20000116);
5198 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5199 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5200 20000116);
5202 /* Add entries for virtual functions introduced by this class. */
5203 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), virtuals);
5205 /* Set DECL_VINDEX for all functions declared in this class. */
5206 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
5207 fn;
5208 fn = TREE_CHAIN (fn),
5209 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
5210 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
5212 tree fndecl = BV_FN (fn);
5214 if (DECL_THUNK_P (fndecl))
5215 /* A thunk. We should never be calling this entry directly
5216 from this vtable -- we'd use the entry for the non
5217 thunk base function. */
5218 DECL_VINDEX (fndecl) = NULL_TREE;
5219 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
5220 DECL_VINDEX (fndecl) = build_shared_int_cst (vindex);
5224 finish_struct_bits (t);
5226 /* Complete the rtl for any static member objects of the type we're
5227 working on. */
5228 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5229 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5230 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
5231 DECL_MODE (x) = TYPE_MODE (t);
5233 /* Done with FIELDS...now decide whether to sort these for
5234 faster lookups later.
5236 We use a small number because most searches fail (succeeding
5237 ultimately as the search bores through the inheritance
5238 hierarchy), and we want this failure to occur quickly. */
5240 n_fields = count_fields (TYPE_FIELDS (t));
5241 if (n_fields > 7)
5243 tree field_vec = make_tree_vec (n_fields);
5244 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5245 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5246 field_decl_cmp);
5247 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5248 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5249 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5252 if (TYPE_HAS_CONSTRUCTOR (t))
5254 tree vfields = CLASSTYPE_VFIELDS (t);
5256 for (vfields = CLASSTYPE_VFIELDS (t);
5257 vfields; vfields = TREE_CHAIN (vfields))
5258 /* Mark the fact that constructor for T could affect anybody
5259 inheriting from T who wants to initialize vtables for
5260 VFIELDS's type. */
5261 if (VF_BINFO_VALUE (vfields))
5262 TREE_ADDRESSABLE (vfields) = 1;
5265 /* Make the rtl for any new vtables we have created, and unmark
5266 the base types we marked. */
5267 finish_vtbls (t);
5269 /* Build the VTT for T. */
5270 build_vtt (t);
5272 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5273 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5274 warning ("`%#T' has virtual functions but non-virtual destructor", t);
5276 complete_vars (t);
5278 if (warn_overloaded_virtual)
5279 warn_hidden (t);
5281 maybe_suppress_debug_info (t);
5283 dump_class_hierarchy (t);
5285 /* Finish debugging output for this type. */
5286 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5289 /* When T was built up, the member declarations were added in reverse
5290 order. Rearrange them to declaration order. */
5292 void
5293 unreverse_member_declarations (tree t)
5295 tree next;
5296 tree prev;
5297 tree x;
5299 /* The following lists are all in reverse order. Put them in
5300 declaration order now. */
5301 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5302 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5303 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
5305 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5306 reverse order, so we can't just use nreverse. */
5307 prev = NULL_TREE;
5308 for (x = TYPE_FIELDS (t);
5309 x && TREE_CODE (x) != TYPE_DECL;
5310 x = next)
5312 next = TREE_CHAIN (x);
5313 TREE_CHAIN (x) = prev;
5314 prev = x;
5316 if (prev)
5318 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5319 if (prev)
5320 TYPE_FIELDS (t) = prev;
5324 tree
5325 finish_struct (tree t, tree attributes)
5327 const char *saved_filename = input_filename;
5328 int saved_lineno = lineno;
5330 /* Now that we've got all the field declarations, reverse everything
5331 as necessary. */
5332 unreverse_member_declarations (t);
5334 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5336 /* Nadger the current location so that diagnostics point to the start of
5337 the struct, not the end. */
5338 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5339 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5341 if (processing_template_decl)
5343 finish_struct_methods (t);
5344 TYPE_SIZE (t) = bitsize_zero_node;
5346 else
5347 finish_struct_1 (t);
5349 input_filename = saved_filename;
5350 lineno = saved_lineno;
5352 TYPE_BEING_DEFINED (t) = 0;
5354 if (current_class_type)
5355 popclass ();
5356 else
5357 error ("trying to finish struct, but kicked out due to previous parse errors");
5359 if (processing_template_decl && at_function_scope_p ())
5360 add_stmt (build_min (TAG_DEFN, t));
5362 return t;
5365 /* Return the dynamic type of INSTANCE, if known.
5366 Used to determine whether the virtual function table is needed
5367 or not.
5369 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5370 of our knowledge of its type. *NONNULL should be initialized
5371 before this function is called. */
5373 static tree
5374 fixed_type_or_null (tree instance, int* nonnull, int* cdtorp)
5376 switch (TREE_CODE (instance))
5378 case INDIRECT_REF:
5379 if (POINTER_TYPE_P (TREE_TYPE (instance)))
5380 return NULL_TREE;
5381 else
5382 return fixed_type_or_null (TREE_OPERAND (instance, 0),
5383 nonnull, cdtorp);
5385 case CALL_EXPR:
5386 /* This is a call to a constructor, hence it's never zero. */
5387 if (TREE_HAS_CONSTRUCTOR (instance))
5389 if (nonnull)
5390 *nonnull = 1;
5391 return TREE_TYPE (instance);
5393 return NULL_TREE;
5395 case SAVE_EXPR:
5396 /* This is a call to a constructor, hence it's never zero. */
5397 if (TREE_HAS_CONSTRUCTOR (instance))
5399 if (nonnull)
5400 *nonnull = 1;
5401 return TREE_TYPE (instance);
5403 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5405 case RTL_EXPR:
5406 return NULL_TREE;
5408 case PLUS_EXPR:
5409 case MINUS_EXPR:
5410 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5411 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5412 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5413 /* Propagate nonnull. */
5414 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5415 return NULL_TREE;
5417 case NOP_EXPR:
5418 case CONVERT_EXPR:
5419 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5421 case ADDR_EXPR:
5422 if (nonnull)
5423 *nonnull = 1;
5424 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5426 case COMPONENT_REF:
5427 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5429 case VAR_DECL:
5430 case FIELD_DECL:
5431 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5432 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5434 if (nonnull)
5435 *nonnull = 1;
5436 return TREE_TYPE (TREE_TYPE (instance));
5438 /* fall through... */
5439 case TARGET_EXPR:
5440 case PARM_DECL:
5441 case RESULT_DECL:
5442 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5444 if (nonnull)
5445 *nonnull = 1;
5446 return TREE_TYPE (instance);
5448 else if (instance == current_class_ptr)
5450 if (nonnull)
5451 *nonnull = 1;
5453 /* if we're in a ctor or dtor, we know our type. */
5454 if (DECL_LANG_SPECIFIC (current_function_decl)
5455 && (DECL_CONSTRUCTOR_P (current_function_decl)
5456 || DECL_DESTRUCTOR_P (current_function_decl)))
5458 if (cdtorp)
5459 *cdtorp = 1;
5460 return TREE_TYPE (TREE_TYPE (instance));
5463 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5465 /* Reference variables should be references to objects. */
5466 if (nonnull)
5467 *nonnull = 1;
5469 if (TREE_CODE (instance) == VAR_DECL
5470 && DECL_INITIAL (instance))
5471 return fixed_type_or_null (DECL_INITIAL (instance),
5472 nonnull, cdtorp);
5474 return NULL_TREE;
5476 default:
5477 return NULL_TREE;
5481 /* Return nonzero if the dynamic type of INSTANCE is known, and
5482 equivalent to the static type. We also handle the case where
5483 INSTANCE is really a pointer. Return negative if this is a
5484 ctor/dtor. There the dynamic type is known, but this might not be
5485 the most derived base of the original object, and hence virtual
5486 bases may not be layed out according to this type.
5488 Used to determine whether the virtual function table is needed
5489 or not.
5491 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5492 of our knowledge of its type. *NONNULL should be initialized
5493 before this function is called. */
5496 resolves_to_fixed_type_p (tree instance, int* nonnull)
5498 tree t = TREE_TYPE (instance);
5499 int cdtorp = 0;
5501 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5502 if (fixed == NULL_TREE)
5503 return 0;
5504 if (POINTER_TYPE_P (t))
5505 t = TREE_TYPE (t);
5506 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5507 return 0;
5508 return cdtorp ? -1 : 1;
5512 void
5513 init_class_processing (void)
5515 current_class_depth = 0;
5516 current_class_stack_size = 10;
5517 current_class_stack
5518 = (class_stack_node_t) xmalloc (current_class_stack_size
5519 * sizeof (struct class_stack_node));
5520 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5522 ridpointers[(int) RID_PUBLIC] = access_public_node;
5523 ridpointers[(int) RID_PRIVATE] = access_private_node;
5524 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5527 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
5528 appropriate for TYPE.
5530 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5531 which can be seen locally to the class. They are shadowed by
5532 any subsequent local declaration (including parameter names).
5534 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5535 which have static meaning (i.e., static members, static
5536 member functions, enum declarations, etc).
5538 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5539 which can be seen locally to the class (as in 1), but
5540 know that we are doing this for declaration purposes
5541 (i.e. friend foo::bar (int)).
5543 So that we may avoid calls to lookup_name, we cache the _TYPE
5544 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5546 For multiple inheritance, we perform a two-pass depth-first search
5547 of the type lattice. The first pass performs a pre-order search,
5548 marking types after the type has had its fields installed in
5549 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5550 unmarks the marked types. If a field or member function name
5551 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5552 that name becomes `error_mark_node'. */
5554 void
5555 pushclass (tree type, bool modify)
5557 type = TYPE_MAIN_VARIANT (type);
5559 /* Make sure there is enough room for the new entry on the stack. */
5560 if (current_class_depth + 1 >= current_class_stack_size)
5562 current_class_stack_size *= 2;
5563 current_class_stack
5564 = (class_stack_node_t) xrealloc (current_class_stack,
5565 current_class_stack_size
5566 * sizeof (struct class_stack_node));
5569 /* Insert a new entry on the class stack. */
5570 current_class_stack[current_class_depth].name = current_class_name;
5571 current_class_stack[current_class_depth].type = current_class_type;
5572 current_class_stack[current_class_depth].access = current_access_specifier;
5573 current_class_stack[current_class_depth].names_used = 0;
5574 current_class_depth++;
5576 /* Now set up the new type. */
5577 current_class_name = TYPE_NAME (type);
5578 if (TREE_CODE (current_class_name) == TYPE_DECL)
5579 current_class_name = DECL_NAME (current_class_name);
5580 current_class_type = type;
5582 /* By default, things in classes are private, while things in
5583 structures or unions are public. */
5584 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5585 ? access_private_node
5586 : access_public_node);
5588 if (previous_class_type != NULL_TREE
5589 && (type != previous_class_type
5590 || !COMPLETE_TYPE_P (previous_class_type))
5591 && current_class_depth == 1)
5593 /* Forcibly remove any old class remnants. */
5594 invalidate_class_lookup_cache ();
5597 /* If we're about to enter a nested class, clear
5598 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5599 if (modify && current_class_depth > 1)
5600 clear_identifier_class_values ();
5602 pushlevel_class ();
5604 if (modify)
5606 if (type != previous_class_type || current_class_depth > 1)
5607 push_class_decls (type);
5608 else
5610 tree item;
5612 /* We are re-entering the same class we just left, so we
5613 don't have to search the whole inheritance matrix to find
5614 all the decls to bind again. Instead, we install the
5615 cached class_shadowed list, and walk through it binding
5616 names and setting up IDENTIFIER_TYPE_VALUEs. */
5617 set_class_shadows (previous_class_values);
5618 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5620 tree id = TREE_PURPOSE (item);
5621 tree decl = TREE_TYPE (item);
5623 push_class_binding (id, decl);
5624 if (TREE_CODE (decl) == TYPE_DECL)
5625 set_identifier_type_value (id, TREE_TYPE (decl));
5627 unuse_fields (type);
5630 storetags (CLASSTYPE_TAGS (type));
5634 /* When we exit a toplevel class scope, we save the
5635 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5636 reenter the class. Here, we've entered some other class, so we
5637 must invalidate our cache. */
5639 void
5640 invalidate_class_lookup_cache (void)
5642 tree t;
5644 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5645 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5646 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5648 previous_class_values = NULL_TREE;
5649 previous_class_type = NULL_TREE;
5652 /* Get out of the current class scope. If we were in a class scope
5653 previously, that is the one popped to. */
5655 void
5656 popclass (void)
5658 poplevel_class ();
5659 pop_class_decls ();
5661 current_class_depth--;
5662 current_class_name = current_class_stack[current_class_depth].name;
5663 current_class_type = current_class_stack[current_class_depth].type;
5664 current_access_specifier = current_class_stack[current_class_depth].access;
5665 if (current_class_stack[current_class_depth].names_used)
5666 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5669 /* Returns 1 if current_class_type is either T or a nested type of T.
5670 We start looking from 1 because entry 0 is from global scope, and has
5671 no type. */
5674 currently_open_class (tree t)
5676 int i;
5677 if (current_class_type && same_type_p (t, current_class_type))
5678 return 1;
5679 for (i = 1; i < current_class_depth; ++i)
5680 if (current_class_stack[i].type
5681 && same_type_p (current_class_stack [i].type, t))
5682 return 1;
5683 return 0;
5686 /* If either current_class_type or one of its enclosing classes are derived
5687 from T, return the appropriate type. Used to determine how we found
5688 something via unqualified lookup. */
5690 tree
5691 currently_open_derived_class (tree t)
5693 int i;
5695 /* The bases of a dependent type are unknown. */
5696 if (dependent_type_p (t))
5697 return NULL_TREE;
5699 if (DERIVED_FROM_P (t, current_class_type))
5700 return current_class_type;
5702 for (i = current_class_depth - 1; i > 0; --i)
5703 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5704 return current_class_stack[i].type;
5706 return NULL_TREE;
5709 /* When entering a class scope, all enclosing class scopes' names with
5710 static meaning (static variables, static functions, types and
5711 enumerators) have to be visible. This recursive function calls
5712 pushclass for all enclosing class contexts until global or a local
5713 scope is reached. TYPE is the enclosed class. */
5715 void
5716 push_nested_class (tree type)
5718 tree context;
5720 /* A namespace might be passed in error cases, like A::B:C. */
5721 if (type == NULL_TREE
5722 || type == error_mark_node
5723 || TREE_CODE (type) == NAMESPACE_DECL
5724 || ! IS_AGGR_TYPE (type)
5725 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5726 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5727 return;
5729 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5731 if (context && CLASS_TYPE_P (context))
5732 push_nested_class (context);
5733 pushclass (type, true);
5736 /* Undoes a push_nested_class call. */
5738 void
5739 pop_nested_class (void)
5741 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5743 popclass ();
5744 if (context && CLASS_TYPE_P (context))
5745 pop_nested_class ();
5748 /* Returns the number of extern "LANG" blocks we are nested within. */
5751 current_lang_depth (void)
5753 return VARRAY_ACTIVE_SIZE (current_lang_base);
5756 /* Set global variables CURRENT_LANG_NAME to appropriate value
5757 so that behavior of name-mangling machinery is correct. */
5759 void
5760 push_lang_context (tree name)
5762 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5764 if (name == lang_name_cplusplus)
5766 current_lang_name = name;
5768 else if (name == lang_name_java)
5770 current_lang_name = name;
5771 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5772 (See record_builtin_java_type in decl.c.) However, that causes
5773 incorrect debug entries if these types are actually used.
5774 So we re-enable debug output after extern "Java". */
5775 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5776 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5777 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5778 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5779 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5780 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5781 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5782 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5784 else if (name == lang_name_c)
5786 current_lang_name = name;
5788 else
5789 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5792 /* Get out of the current language scope. */
5794 void
5795 pop_lang_context (void)
5797 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5798 VARRAY_POP (current_lang_base);
5801 /* Type instantiation routines. */
5803 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5804 matches the TARGET_TYPE. If there is no satisfactory match, return
5805 error_mark_node, and issue an error message if COMPLAIN is
5806 nonzero. Permit pointers to member function if PTRMEM is nonzero.
5807 If TEMPLATE_ONLY, the name of the overloaded function
5808 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5809 template arguments. */
5811 static tree
5812 resolve_address_of_overloaded_function (tree target_type,
5813 tree overload,
5814 int complain,
5815 int ptrmem,
5816 int template_only,
5817 tree explicit_targs)
5819 /* Here's what the standard says:
5821 [over.over]
5823 If the name is a function template, template argument deduction
5824 is done, and if the argument deduction succeeds, the deduced
5825 arguments are used to generate a single template function, which
5826 is added to the set of overloaded functions considered.
5828 Non-member functions and static member functions match targets of
5829 type "pointer-to-function" or "reference-to-function." Nonstatic
5830 member functions match targets of type "pointer-to-member
5831 function;" the function type of the pointer to member is used to
5832 select the member function from the set of overloaded member
5833 functions. If a nonstatic member function is selected, the
5834 reference to the overloaded function name is required to have the
5835 form of a pointer to member as described in 5.3.1.
5837 If more than one function is selected, any template functions in
5838 the set are eliminated if the set also contains a non-template
5839 function, and any given template function is eliminated if the
5840 set contains a second template function that is more specialized
5841 than the first according to the partial ordering rules 14.5.5.2.
5842 After such eliminations, if any, there shall remain exactly one
5843 selected function. */
5845 int is_ptrmem = 0;
5846 int is_reference = 0;
5847 /* We store the matches in a TREE_LIST rooted here. The functions
5848 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5849 interoperability with most_specialized_instantiation. */
5850 tree matches = NULL_TREE;
5851 tree fn;
5853 /* By the time we get here, we should be seeing only real
5854 pointer-to-member types, not the internal POINTER_TYPE to
5855 METHOD_TYPE representation. */
5856 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5857 && (TREE_CODE (TREE_TYPE (target_type))
5858 == METHOD_TYPE)), 0);
5860 if (TREE_CODE (overload) == COMPONENT_REF)
5861 overload = TREE_OPERAND (overload, 1);
5863 /* Check that the TARGET_TYPE is reasonable. */
5864 if (TYPE_PTRFN_P (target_type))
5865 /* This is OK. */;
5866 else if (TYPE_PTRMEMFUNC_P (target_type))
5867 /* This is OK, too. */
5868 is_ptrmem = 1;
5869 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5871 /* This is OK, too. This comes from a conversion to reference
5872 type. */
5873 target_type = build_reference_type (target_type);
5874 is_reference = 1;
5876 else
5878 if (complain)
5879 error ("\
5880 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5881 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5882 return error_mark_node;
5885 /* If we can find a non-template function that matches, we can just
5886 use it. There's no point in generating template instantiations
5887 if we're just going to throw them out anyhow. But, of course, we
5888 can only do this when we don't *need* a template function. */
5889 if (!template_only)
5891 tree fns;
5893 for (fns = overload; fns; fns = OVL_NEXT (fns))
5895 tree fn = OVL_CURRENT (fns);
5896 tree fntype;
5898 if (TREE_CODE (fn) == TEMPLATE_DECL)
5899 /* We're not looking for templates just yet. */
5900 continue;
5902 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5903 != is_ptrmem)
5904 /* We're looking for a non-static member, and this isn't
5905 one, or vice versa. */
5906 continue;
5908 /* See if there's a match. */
5909 fntype = TREE_TYPE (fn);
5910 if (is_ptrmem)
5911 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5912 else if (!is_reference)
5913 fntype = build_pointer_type (fntype);
5915 if (can_convert_arg (target_type, fntype, fn))
5916 matches = tree_cons (fn, NULL_TREE, matches);
5920 /* Now, if we've already got a match (or matches), there's no need
5921 to proceed to the template functions. But, if we don't have a
5922 match we need to look at them, too. */
5923 if (!matches)
5925 tree target_fn_type;
5926 tree target_arg_types;
5927 tree target_ret_type;
5928 tree fns;
5930 if (is_ptrmem)
5931 target_fn_type
5932 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5933 else
5934 target_fn_type = TREE_TYPE (target_type);
5935 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5936 target_ret_type = TREE_TYPE (target_fn_type);
5938 /* Never do unification on the 'this' parameter. */
5939 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5940 target_arg_types = TREE_CHAIN (target_arg_types);
5942 for (fns = overload; fns; fns = OVL_NEXT (fns))
5944 tree fn = OVL_CURRENT (fns);
5945 tree instantiation;
5946 tree instantiation_type;
5947 tree targs;
5949 if (TREE_CODE (fn) != TEMPLATE_DECL)
5950 /* We're only looking for templates. */
5951 continue;
5953 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5954 != is_ptrmem)
5955 /* We're not looking for a non-static member, and this is
5956 one, or vice versa. */
5957 continue;
5959 /* Try to do argument deduction. */
5960 targs = make_tree_vec (DECL_NTPARMS (fn));
5961 if (fn_type_unification (fn, explicit_targs, targs,
5962 target_arg_types, target_ret_type,
5963 DEDUCE_EXACT, -1) != 0)
5964 /* Argument deduction failed. */
5965 continue;
5967 /* Instantiate the template. */
5968 instantiation = instantiate_template (fn, targs);
5969 if (instantiation == error_mark_node)
5970 /* Instantiation failed. */
5971 continue;
5973 /* See if there's a match. */
5974 instantiation_type = TREE_TYPE (instantiation);
5975 if (is_ptrmem)
5976 instantiation_type =
5977 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5978 else if (!is_reference)
5979 instantiation_type = build_pointer_type (instantiation_type);
5980 if (can_convert_arg (target_type, instantiation_type, instantiation))
5981 matches = tree_cons (instantiation, fn, matches);
5984 /* Now, remove all but the most specialized of the matches. */
5985 if (matches)
5987 tree match = most_specialized_instantiation (matches);
5989 if (match != error_mark_node)
5990 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5994 /* Now we should have exactly one function in MATCHES. */
5995 if (matches == NULL_TREE)
5997 /* There were *no* matches. */
5998 if (complain)
6000 error ("no matches converting function `%D' to type `%#T'",
6001 DECL_NAME (OVL_FUNCTION (overload)),
6002 target_type);
6004 /* print_candidates expects a chain with the functions in
6005 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6006 so why be clever?). */
6007 for (; overload; overload = OVL_NEXT (overload))
6008 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6009 matches);
6011 print_candidates (matches);
6013 return error_mark_node;
6015 else if (TREE_CHAIN (matches))
6017 /* There were too many matches. */
6019 if (complain)
6021 tree match;
6023 error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6024 DECL_NAME (OVL_FUNCTION (overload)),
6025 target_type);
6027 /* Since print_candidates expects the functions in the
6028 TREE_VALUE slot, we flip them here. */
6029 for (match = matches; match; match = TREE_CHAIN (match))
6030 TREE_VALUE (match) = TREE_PURPOSE (match);
6032 print_candidates (matches);
6035 return error_mark_node;
6038 /* Good, exactly one match. Now, convert it to the correct type. */
6039 fn = TREE_PURPOSE (matches);
6041 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6042 && !ptrmem && !flag_ms_extensions)
6044 static int explained;
6046 if (!complain)
6047 return error_mark_node;
6049 pedwarn ("assuming pointer to member `%D'", fn);
6050 if (!explained)
6052 pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6053 explained = 1;
6056 mark_used (fn);
6058 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6059 return build_unary_op (ADDR_EXPR, fn, 0);
6060 else
6062 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6063 will mark the function as addressed, but here we must do it
6064 explicitly. */
6065 cxx_mark_addressable (fn);
6067 return fn;
6071 /* This function will instantiate the type of the expression given in
6072 RHS to match the type of LHSTYPE. If errors exist, then return
6073 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6074 we complain on errors. If we are not complaining, never modify rhs,
6075 as overload resolution wants to try many possible instantiations, in
6076 the hope that at least one will work.
6078 For non-recursive calls, LHSTYPE should be a function, pointer to
6079 function, or a pointer to member function. */
6081 tree
6082 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
6084 int complain = (flags & tf_error);
6085 int strict = (flags & tf_no_attributes)
6086 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6087 int allow_ptrmem = flags & tf_ptrmem_ok;
6089 flags &= ~tf_ptrmem_ok;
6091 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6093 if (complain)
6094 error ("not enough type information");
6095 return error_mark_node;
6098 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6100 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6101 return rhs;
6102 if (flag_ms_extensions
6103 && TYPE_PTRMEMFUNC_P (lhstype)
6104 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6105 /* Microsoft allows `A::f' to be resolved to a
6106 pointer-to-member. */
6108 else
6110 if (complain)
6111 error ("argument of type `%T' does not match `%T'",
6112 TREE_TYPE (rhs), lhstype);
6113 return error_mark_node;
6117 if (TREE_CODE (rhs) == BASELINK)
6118 rhs = BASELINK_FUNCTIONS (rhs);
6120 /* We don't overwrite rhs if it is an overloaded function.
6121 Copying it would destroy the tree link. */
6122 if (TREE_CODE (rhs) != OVERLOAD)
6123 rhs = copy_node (rhs);
6125 /* This should really only be used when attempting to distinguish
6126 what sort of a pointer to function we have. For now, any
6127 arithmetic operation which is not supported on pointers
6128 is rejected as an error. */
6130 switch (TREE_CODE (rhs))
6132 case TYPE_EXPR:
6133 case CONVERT_EXPR:
6134 case SAVE_EXPR:
6135 case CONSTRUCTOR:
6136 case BUFFER_REF:
6137 abort ();
6138 return error_mark_node;
6140 case INDIRECT_REF:
6141 case ARRAY_REF:
6143 tree new_rhs;
6145 new_rhs = instantiate_type (build_pointer_type (lhstype),
6146 TREE_OPERAND (rhs, 0), flags);
6147 if (new_rhs == error_mark_node)
6148 return error_mark_node;
6150 TREE_TYPE (rhs) = lhstype;
6151 TREE_OPERAND (rhs, 0) = new_rhs;
6152 return rhs;
6155 case NOP_EXPR:
6156 rhs = copy_node (TREE_OPERAND (rhs, 0));
6157 TREE_TYPE (rhs) = unknown_type_node;
6158 return instantiate_type (lhstype, rhs, flags);
6160 case COMPONENT_REF:
6161 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6163 case OFFSET_REF:
6164 rhs = TREE_OPERAND (rhs, 1);
6165 if (BASELINK_P (rhs))
6166 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs),
6167 flags | allow_ptrmem);
6169 /* This can happen if we are forming a pointer-to-member for a
6170 member template. */
6171 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6173 /* Fall through. */
6175 case TEMPLATE_ID_EXPR:
6177 tree fns = TREE_OPERAND (rhs, 0);
6178 tree args = TREE_OPERAND (rhs, 1);
6180 return
6181 resolve_address_of_overloaded_function (lhstype,
6182 fns,
6183 complain,
6184 allow_ptrmem,
6185 /*template_only=*/1,
6186 args);
6189 case OVERLOAD:
6190 case FUNCTION_DECL:
6191 return
6192 resolve_address_of_overloaded_function (lhstype,
6193 rhs,
6194 complain,
6195 allow_ptrmem,
6196 /*template_only=*/0,
6197 /*explicit_targs=*/NULL_TREE);
6199 case TREE_LIST:
6200 /* Now we should have a baselink. */
6201 my_friendly_assert (BASELINK_P (rhs), 990412);
6203 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
6205 case CALL_EXPR:
6206 /* This is too hard for now. */
6207 abort ();
6208 return error_mark_node;
6210 case PLUS_EXPR:
6211 case MINUS_EXPR:
6212 case COMPOUND_EXPR:
6213 TREE_OPERAND (rhs, 0)
6214 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6215 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6216 return error_mark_node;
6217 TREE_OPERAND (rhs, 1)
6218 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6219 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6220 return error_mark_node;
6222 TREE_TYPE (rhs) = lhstype;
6223 return rhs;
6225 case MULT_EXPR:
6226 case TRUNC_DIV_EXPR:
6227 case FLOOR_DIV_EXPR:
6228 case CEIL_DIV_EXPR:
6229 case ROUND_DIV_EXPR:
6230 case RDIV_EXPR:
6231 case TRUNC_MOD_EXPR:
6232 case FLOOR_MOD_EXPR:
6233 case CEIL_MOD_EXPR:
6234 case ROUND_MOD_EXPR:
6235 case FIX_ROUND_EXPR:
6236 case FIX_FLOOR_EXPR:
6237 case FIX_CEIL_EXPR:
6238 case FIX_TRUNC_EXPR:
6239 case FLOAT_EXPR:
6240 case NEGATE_EXPR:
6241 case ABS_EXPR:
6242 case MAX_EXPR:
6243 case MIN_EXPR:
6244 case FFS_EXPR:
6246 case BIT_AND_EXPR:
6247 case BIT_IOR_EXPR:
6248 case BIT_XOR_EXPR:
6249 case LSHIFT_EXPR:
6250 case RSHIFT_EXPR:
6251 case LROTATE_EXPR:
6252 case RROTATE_EXPR:
6254 case PREINCREMENT_EXPR:
6255 case PREDECREMENT_EXPR:
6256 case POSTINCREMENT_EXPR:
6257 case POSTDECREMENT_EXPR:
6258 if (complain)
6259 error ("invalid operation on uninstantiated type");
6260 return error_mark_node;
6262 case TRUTH_AND_EXPR:
6263 case TRUTH_OR_EXPR:
6264 case TRUTH_XOR_EXPR:
6265 case LT_EXPR:
6266 case LE_EXPR:
6267 case GT_EXPR:
6268 case GE_EXPR:
6269 case EQ_EXPR:
6270 case NE_EXPR:
6271 case TRUTH_ANDIF_EXPR:
6272 case TRUTH_ORIF_EXPR:
6273 case TRUTH_NOT_EXPR:
6274 if (complain)
6275 error ("not enough type information");
6276 return error_mark_node;
6278 case COND_EXPR:
6279 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6281 if (complain)
6282 error ("not enough type information");
6283 return error_mark_node;
6285 TREE_OPERAND (rhs, 1)
6286 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6287 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6288 return error_mark_node;
6289 TREE_OPERAND (rhs, 2)
6290 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6291 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6292 return error_mark_node;
6294 TREE_TYPE (rhs) = lhstype;
6295 return rhs;
6297 case MODIFY_EXPR:
6298 TREE_OPERAND (rhs, 1)
6299 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6300 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6301 return error_mark_node;
6303 TREE_TYPE (rhs) = lhstype;
6304 return rhs;
6306 case ADDR_EXPR:
6308 if (PTRMEM_OK_P (rhs))
6309 flags |= tf_ptrmem_ok;
6311 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6313 case ENTRY_VALUE_EXPR:
6314 abort ();
6315 return error_mark_node;
6317 case ERROR_MARK:
6318 return error_mark_node;
6320 default:
6321 abort ();
6322 return error_mark_node;
6326 /* Return the name of the virtual function pointer field
6327 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6328 this may have to look back through base types to find the
6329 ultimate field name. (For single inheritance, these could
6330 all be the same name. Who knows for multiple inheritance). */
6332 static tree
6333 get_vfield_name (tree type)
6335 tree binfo = TYPE_BINFO (type);
6336 char *buf;
6338 while (BINFO_BASETYPES (binfo)
6339 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6340 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6341 binfo = BINFO_BASETYPE (binfo, 0);
6343 type = BINFO_TYPE (binfo);
6344 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6345 + TYPE_NAME_LENGTH (type) + 2);
6346 sprintf (buf, VFIELD_NAME_FORMAT,
6347 IDENTIFIER_POINTER (constructor_name (type)));
6348 return get_identifier (buf);
6351 void
6352 print_class_statistics (void)
6354 #ifdef GATHER_STATISTICS
6355 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6356 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6357 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6358 n_build_method_call, n_inner_fields_searched);
6359 if (n_vtables)
6361 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6362 n_vtables, n_vtable_searches);
6363 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6364 n_vtable_entries, n_vtable_elems);
6366 #endif
6369 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6370 according to [class]:
6371 The class-name is also inserted
6372 into the scope of the class itself. For purposes of access checking,
6373 the inserted class name is treated as if it were a public member name. */
6375 void
6376 build_self_reference (void)
6378 tree name = constructor_name (current_class_type);
6379 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6380 tree saved_cas;
6382 DECL_NONLOCAL (value) = 1;
6383 DECL_CONTEXT (value) = current_class_type;
6384 DECL_ARTIFICIAL (value) = 1;
6386 if (processing_template_decl)
6387 value = push_template_decl (value);
6389 saved_cas = current_access_specifier;
6390 current_access_specifier = access_public_node;
6391 finish_member_declaration (value);
6392 current_access_specifier = saved_cas;
6395 /* Returns 1 if TYPE contains only padding bytes. */
6398 is_empty_class (tree type)
6400 if (type == error_mark_node)
6401 return 0;
6403 if (! IS_AGGR_TYPE (type))
6404 return 0;
6406 /* In G++ 3.2, whether or not a class was empty was determined by
6407 looking at its size. */
6408 if (abi_version_at_least (2))
6409 return CLASSTYPE_EMPTY_P (type);
6410 else
6411 return integer_zerop (CLASSTYPE_SIZE (type));
6414 /* Returns true if TYPE contains an empty class. */
6416 static bool
6417 contains_empty_class_p (tree type)
6419 if (is_empty_class (type))
6420 return true;
6421 if (CLASS_TYPE_P (type))
6423 tree field;
6424 int i;
6426 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
6427 if (contains_empty_class_p (TYPE_BINFO_BASETYPE (type, i)))
6428 return true;
6429 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6430 if (TREE_CODE (field) == FIELD_DECL
6431 && !DECL_ARTIFICIAL (field)
6432 && is_empty_class (TREE_TYPE (field)))
6433 return true;
6435 else if (TREE_CODE (type) == ARRAY_TYPE)
6436 return contains_empty_class_p (TREE_TYPE (type));
6437 return false;
6440 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6441 a *_TYPE node. NODE can also be a local class. */
6443 tree
6444 get_enclosing_class (tree type)
6446 tree node = type;
6448 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6450 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6452 case 'd':
6453 node = DECL_CONTEXT (node);
6454 break;
6456 case 't':
6457 if (node != type)
6458 return node;
6459 node = TYPE_CONTEXT (node);
6460 break;
6462 default:
6463 abort ();
6466 return NULL_TREE;
6469 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6472 is_base_of_enclosing_class (tree base, tree type)
6474 while (type)
6476 if (lookup_base (type, base, ba_any, NULL))
6477 return 1;
6479 type = get_enclosing_class (type);
6481 return 0;
6484 /* Note that NAME was looked up while the current class was being
6485 defined and that the result of that lookup was DECL. */
6487 void
6488 maybe_note_name_used_in_class (tree name, tree decl)
6490 splay_tree names_used;
6492 /* If we're not defining a class, there's nothing to do. */
6493 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6494 return;
6496 /* If there's already a binding for this NAME, then we don't have
6497 anything to worry about. */
6498 if (IDENTIFIER_CLASS_VALUE (name))
6499 return;
6501 if (!current_class_stack[current_class_depth - 1].names_used)
6502 current_class_stack[current_class_depth - 1].names_used
6503 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6504 names_used = current_class_stack[current_class_depth - 1].names_used;
6506 splay_tree_insert (names_used,
6507 (splay_tree_key) name,
6508 (splay_tree_value) decl);
6511 /* Note that NAME was declared (as DECL) in the current class. Check
6512 to see that the declaration is valid. */
6514 void
6515 note_name_declared_in_class (tree name, tree decl)
6517 splay_tree names_used;
6518 splay_tree_node n;
6520 /* Look to see if we ever used this name. */
6521 names_used
6522 = current_class_stack[current_class_depth - 1].names_used;
6523 if (!names_used)
6524 return;
6526 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6527 if (n)
6529 /* [basic.scope.class]
6531 A name N used in a class S shall refer to the same declaration
6532 in its context and when re-evaluated in the completed scope of
6533 S. */
6534 error ("declaration of `%#D'", decl);
6535 cp_error_at ("changes meaning of `%D' from `%+#D'",
6536 DECL_NAME (OVL_CURRENT (decl)),
6537 (tree) n->value);
6541 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6542 Secondary vtables are merged with primary vtables; this function
6543 will return the VAR_DECL for the primary vtable. */
6545 tree
6546 get_vtbl_decl_for_binfo (tree binfo)
6548 tree decl;
6550 decl = BINFO_VTABLE (binfo);
6551 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6553 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6554 2000403);
6555 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6557 if (decl)
6558 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6559 return decl;
6563 /* Returns the binfo for the primary base of BINFO. If the resulting
6564 BINFO is a virtual base, and it is inherited elsewhere in the
6565 hierarchy, then the returned binfo might not be the primary base of
6566 BINFO in the complete object. Check BINFO_PRIMARY_P or
6567 BINFO_LOST_PRIMARY_P to be sure. */
6569 tree
6570 get_primary_binfo (tree binfo)
6572 tree primary_base;
6573 tree result;
6575 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6576 if (!primary_base)
6577 return NULL_TREE;
6579 result = copied_binfo (primary_base, binfo);
6580 return result;
6583 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
6585 static int
6586 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
6588 if (!indented_p)
6589 fprintf (stream, "%*s", indent, "");
6590 return 1;
6593 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
6594 INDENT should be zero when called from the top level; it is
6595 incremented recursively. IGO indicates the next expected BINFO in
6596 inheritance graph ordering. */
6598 static tree
6599 dump_class_hierarchy_r (FILE *stream,
6600 int flags,
6601 tree binfo,
6602 tree igo,
6603 int indent)
6605 int indented = 0;
6606 tree base_binfos;
6608 indented = maybe_indent_hierarchy (stream, indent, 0);
6609 fprintf (stream, "%s (0x%lx) ",
6610 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6611 (unsigned long) binfo);
6612 if (binfo != igo)
6614 fprintf (stream, "alternative-path\n");
6615 return igo;
6617 igo = TREE_CHAIN (binfo);
6619 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6620 tree_low_cst (BINFO_OFFSET (binfo), 0));
6621 if (is_empty_class (BINFO_TYPE (binfo)))
6622 fprintf (stream, " empty");
6623 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6624 fprintf (stream, " nearly-empty");
6625 if (TREE_VIA_VIRTUAL (binfo))
6626 fprintf (stream, " virtual");
6627 fprintf (stream, "\n");
6629 indented = 0;
6630 if (BINFO_PRIMARY_BASE_OF (binfo))
6632 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6633 fprintf (stream, " primary-for %s (0x%lx)",
6634 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6635 TFF_PLAIN_IDENTIFIER),
6636 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6638 if (BINFO_LOST_PRIMARY_P (binfo))
6640 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6641 fprintf (stream, " lost-primary");
6643 if (indented)
6644 fprintf (stream, "\n");
6646 if (!(flags & TDF_SLIM))
6648 int indented = 0;
6650 if (BINFO_SUBVTT_INDEX (binfo))
6652 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6653 fprintf (stream, " subvttidx=%s",
6654 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6655 TFF_PLAIN_IDENTIFIER));
6657 if (BINFO_VPTR_INDEX (binfo))
6659 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6660 fprintf (stream, " vptridx=%s",
6661 expr_as_string (BINFO_VPTR_INDEX (binfo),
6662 TFF_PLAIN_IDENTIFIER));
6664 if (BINFO_VPTR_FIELD (binfo))
6666 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6667 fprintf (stream, " vbaseoffset=%s",
6668 expr_as_string (BINFO_VPTR_FIELD (binfo),
6669 TFF_PLAIN_IDENTIFIER));
6671 if (BINFO_VTABLE (binfo))
6673 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6674 fprintf (stream, " vptr=%s",
6675 expr_as_string (BINFO_VTABLE (binfo),
6676 TFF_PLAIN_IDENTIFIER));
6679 if (indented)
6680 fprintf (stream, "\n");
6683 base_binfos = BINFO_BASETYPES (binfo);
6684 if (base_binfos)
6686 int ix, n;
6688 n = TREE_VEC_LENGTH (base_binfos);
6689 for (ix = 0; ix != n; ix++)
6691 tree base_binfo = TREE_VEC_ELT (base_binfos, ix);
6693 igo = dump_class_hierarchy_r (stream, flags, base_binfo,
6694 igo, indent + 2);
6698 return igo;
6701 /* Dump the BINFO hierarchy for T. */
6703 static void
6704 dump_class_hierarchy (tree t)
6706 int flags;
6707 FILE *stream = dump_begin (TDI_class, &flags);
6709 if (!stream)
6710 return;
6712 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6713 fprintf (stream, " size=%lu align=%lu\n",
6714 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6715 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6716 fprintf (stream, " base size=%lu base align=%lu\n",
6717 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
6718 / BITS_PER_UNIT),
6719 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
6720 / BITS_PER_UNIT));
6721 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
6722 fprintf (stream, "\n");
6723 dump_end (TDI_class, stream);
6726 static void
6727 dump_array (FILE * stream, tree decl)
6729 tree inits;
6730 int ix;
6731 HOST_WIDE_INT elt;
6732 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6734 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6735 / BITS_PER_UNIT);
6736 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6737 fprintf (stream, " %s entries",
6738 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6739 TFF_PLAIN_IDENTIFIER));
6740 fprintf (stream, "\n");
6742 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6743 inits; ix++, inits = TREE_CHAIN (inits))
6744 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
6745 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6748 static void
6749 dump_vtable (tree t, tree binfo, tree vtable)
6751 int flags;
6752 FILE *stream = dump_begin (TDI_class, &flags);
6754 if (!stream)
6755 return;
6757 if (!(flags & TDF_SLIM))
6759 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6761 fprintf (stream, "%s for %s",
6762 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6763 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6764 if (ctor_vtbl_p)
6766 if (!TREE_VIA_VIRTUAL (binfo))
6767 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6768 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6770 fprintf (stream, "\n");
6771 dump_array (stream, vtable);
6772 fprintf (stream, "\n");
6775 dump_end (TDI_class, stream);
6778 static void
6779 dump_vtt (tree t, tree vtt)
6781 int flags;
6782 FILE *stream = dump_begin (TDI_class, &flags);
6784 if (!stream)
6785 return;
6787 if (!(flags & TDF_SLIM))
6789 fprintf (stream, "VTT for %s\n",
6790 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6791 dump_array (stream, vtt);
6792 fprintf (stream, "\n");
6795 dump_end (TDI_class, stream);
6798 /* Virtual function table initialization. */
6800 /* Create all the necessary vtables for T and its base classes. */
6802 static void
6803 finish_vtbls (tree t)
6805 tree list;
6806 tree vbase;
6808 /* We lay out the primary and secondary vtables in one contiguous
6809 vtable. The primary vtable is first, followed by the non-virtual
6810 secondary vtables in inheritance graph order. */
6811 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6812 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6813 TYPE_BINFO (t), t, list);
6815 /* Then come the virtual bases, also in inheritance graph order. */
6816 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6818 if (!TREE_VIA_VIRTUAL (vbase))
6819 continue;
6820 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), t, list);
6823 if (TYPE_BINFO_VTABLE (t))
6824 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6827 /* Initialize the vtable for BINFO with the INITS. */
6829 static void
6830 initialize_vtable (tree binfo, tree inits)
6832 tree decl;
6834 layout_vtable_decl (binfo, list_length (inits));
6835 decl = get_vtbl_decl_for_binfo (binfo);
6836 initialize_array (decl, inits);
6837 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6840 /* Initialize DECL (a declaration for a namespace-scope array) with
6841 the INITS. */
6843 static void
6844 initialize_array (tree decl, tree inits)
6846 tree context;
6848 context = DECL_CONTEXT (decl);
6849 DECL_CONTEXT (decl) = NULL_TREE;
6850 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6851 TREE_HAS_CONSTRUCTOR (DECL_INITIAL (decl)) = 1;
6852 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6853 DECL_CONTEXT (decl) = context;
6856 /* Build the VTT (virtual table table) for T.
6857 A class requires a VTT if it has virtual bases.
6859 This holds
6860 1 - primary virtual pointer for complete object T
6861 2 - secondary VTTs for each direct non-virtual base of T which requires a
6863 3 - secondary virtual pointers for each direct or indirect base of T which
6864 has virtual bases or is reachable via a virtual path from T.
6865 4 - secondary VTTs for each direct or indirect virtual base of T.
6867 Secondary VTTs look like complete object VTTs without part 4. */
6869 static void
6870 build_vtt (tree t)
6872 tree inits;
6873 tree type;
6874 tree vtt;
6875 tree index;
6877 /* Build up the initializers for the VTT. */
6878 inits = NULL_TREE;
6879 index = size_zero_node;
6880 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6882 /* If we didn't need a VTT, we're done. */
6883 if (!inits)
6884 return;
6886 /* Figure out the type of the VTT. */
6887 type = build_index_type (size_int (list_length (inits) - 1));
6888 type = build_cplus_array_type (const_ptr_type_node, type);
6890 /* Now, build the VTT object itself. */
6891 vtt = build_vtable (t, get_vtt_name (t), type);
6892 initialize_array (vtt, inits);
6893 /* Add the VTT to the vtables list. */
6894 TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
6895 TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
6897 dump_vtt (t, vtt);
6900 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
6901 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
6902 and CHAIN the vtable pointer for this binfo after construction is
6903 complete. VALUE can also be another BINFO, in which case we recurse. */
6905 static tree
6906 binfo_ctor_vtable (tree binfo)
6908 tree vt;
6910 while (1)
6912 vt = BINFO_VTABLE (binfo);
6913 if (TREE_CODE (vt) == TREE_LIST)
6914 vt = TREE_VALUE (vt);
6915 if (TREE_CODE (vt) == TREE_VEC)
6916 binfo = vt;
6917 else
6918 break;
6921 return vt;
6924 /* Recursively build the VTT-initializer for BINFO (which is in the
6925 hierarchy dominated by T). INITS points to the end of the initializer
6926 list to date. INDEX is the VTT index where the next element will be
6927 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
6928 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
6929 for virtual bases of T. When it is not so, we build the constructor
6930 vtables for the BINFO-in-T variant. */
6932 static tree *
6933 build_vtt_inits (tree binfo, tree t, tree* inits, tree* index)
6935 int i;
6936 tree b;
6937 tree init;
6938 tree secondary_vptrs;
6939 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
6941 /* We only need VTTs for subobjects with virtual bases. */
6942 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
6943 return inits;
6945 /* We need to use a construction vtable if this is not the primary
6946 VTT. */
6947 if (!top_level_p)
6949 build_ctor_vtbl_group (binfo, t);
6951 /* Record the offset in the VTT where this sub-VTT can be found. */
6952 BINFO_SUBVTT_INDEX (binfo) = *index;
6955 /* Add the address of the primary vtable for the complete object. */
6956 init = binfo_ctor_vtable (binfo);
6957 *inits = build_tree_list (NULL_TREE, init);
6958 inits = &TREE_CHAIN (*inits);
6959 if (top_level_p)
6961 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
6962 BINFO_VPTR_INDEX (binfo) = *index;
6964 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
6966 /* Recursively add the secondary VTTs for non-virtual bases. */
6967 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6969 b = BINFO_BASETYPE (binfo, i);
6970 if (!TREE_VIA_VIRTUAL (b))
6971 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
6972 inits, index);
6975 /* Add secondary virtual pointers for all subobjects of BINFO with
6976 either virtual bases or reachable along a virtual path, except
6977 subobjects that are non-virtual primary bases. */
6978 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
6979 TREE_TYPE (secondary_vptrs) = *index;
6980 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
6981 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
6983 dfs_walk_real (binfo,
6984 dfs_build_secondary_vptr_vtt_inits,
6985 NULL,
6986 dfs_ctor_vtable_bases_queue_p,
6987 secondary_vptrs);
6988 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
6989 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
6990 secondary_vptrs);
6992 *index = TREE_TYPE (secondary_vptrs);
6994 /* The secondary vptrs come back in reverse order. After we reverse
6995 them, and add the INITS, the last init will be the first element
6996 of the chain. */
6997 secondary_vptrs = TREE_VALUE (secondary_vptrs);
6998 if (secondary_vptrs)
7000 *inits = nreverse (secondary_vptrs);
7001 inits = &TREE_CHAIN (secondary_vptrs);
7002 my_friendly_assert (*inits == NULL_TREE, 20000517);
7005 /* Add the secondary VTTs for virtual bases. */
7006 if (top_level_p)
7007 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7009 if (!TREE_VIA_VIRTUAL (b))
7010 continue;
7012 inits = build_vtt_inits (b, t, inits, index);
7015 if (!top_level_p)
7017 tree data = tree_cons (t, binfo, NULL_TREE);
7018 VTT_TOP_LEVEL_P (data) = 0;
7019 VTT_MARKED_BINFO_P (data) = 0;
7021 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7022 dfs_ctor_vtable_bases_queue_p,
7023 data);
7026 return inits;
7029 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7030 for the base in most derived. DATA is a TREE_LIST who's
7031 TREE_CHAIN is the type of the base being
7032 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7033 flag of DATA indicates that this is a constructor vtable. The
7034 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7036 static tree
7037 dfs_build_secondary_vptr_vtt_inits (tree binfo, void* data)
7039 tree l;
7040 tree t;
7041 tree init;
7042 tree index;
7043 int top_level_p;
7045 l = (tree) data;
7046 t = TREE_CHAIN (l);
7047 top_level_p = VTT_TOP_LEVEL_P (l);
7049 BINFO_MARKED (binfo) = 1;
7051 /* We don't care about bases that don't have vtables. */
7052 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7053 return NULL_TREE;
7055 /* We're only interested in proper subobjects of T. */
7056 if (same_type_p (BINFO_TYPE (binfo), t))
7057 return NULL_TREE;
7059 /* We're not interested in non-virtual primary bases. */
7060 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7061 return NULL_TREE;
7063 /* If BINFO has virtual bases or is reachable via a virtual path
7064 from T, it'll have a secondary vptr. */
7065 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7066 && !binfo_via_virtual (binfo, t))
7067 return NULL_TREE;
7069 /* Record the index where this secondary vptr can be found. */
7070 index = TREE_TYPE (l);
7071 if (top_level_p)
7073 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7074 BINFO_VPTR_INDEX (binfo) = index;
7076 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7077 TYPE_SIZE_UNIT (ptr_type_node));
7079 /* Add the initializer for the secondary vptr itself. */
7080 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7082 /* It's a primary virtual base, and this is not the construction
7083 vtable. Find the base this is primary of in the inheritance graph,
7084 and use that base's vtable now. */
7085 while (BINFO_PRIMARY_BASE_OF (binfo))
7086 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7088 init = binfo_ctor_vtable (binfo);
7089 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7091 return NULL_TREE;
7094 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7095 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7096 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7097 hierarchy. */
7099 static tree
7100 dfs_ctor_vtable_bases_queue_p (tree derived, int ix,
7101 void* data)
7103 tree binfo = BINFO_BASETYPE (derived, ix);
7105 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7106 return NULL_TREE;
7107 return binfo;
7110 /* Called from build_vtt_inits via dfs_walk. After building constructor
7111 vtables and generating the sub-vtt from them, we need to restore the
7112 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7113 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7115 static tree
7116 dfs_fixup_binfo_vtbls (tree binfo, void* data)
7118 BINFO_MARKED (binfo) = 0;
7120 /* We don't care about bases that don't have vtables. */
7121 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7122 return NULL_TREE;
7124 /* If we scribbled the construction vtable vptr into BINFO, clear it
7125 out now. */
7126 if (BINFO_VTABLE (binfo)
7127 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7128 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7129 == TREE_VALUE ((tree) data)))
7130 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7132 return NULL_TREE;
7135 /* Build the construction vtable group for BINFO which is in the
7136 hierarchy dominated by T. */
7138 static void
7139 build_ctor_vtbl_group (tree binfo, tree t)
7141 tree list;
7142 tree type;
7143 tree vtbl;
7144 tree inits;
7145 tree id;
7146 tree vbase;
7148 /* See if we've already created this construction vtable group. */
7149 id = mangle_ctor_vtbl_for_type (t, binfo);
7150 if (IDENTIFIER_GLOBAL_VALUE (id))
7151 return;
7153 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7154 /* Build a version of VTBL (with the wrong type) for use in
7155 constructing the addresses of secondary vtables in the
7156 construction vtable group. */
7157 vtbl = build_vtable (t, id, ptr_type_node);
7158 list = build_tree_list (vtbl, NULL_TREE);
7159 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7160 binfo, t, list);
7162 /* Add the vtables for each of our virtual bases using the vbase in T
7163 binfo. */
7164 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7165 vbase;
7166 vbase = TREE_CHAIN (vbase))
7168 tree b;
7170 if (!TREE_VIA_VIRTUAL (vbase))
7171 continue;
7172 b = copied_binfo (vbase, binfo);
7174 accumulate_vtbl_inits (b, vbase, binfo, t, list);
7176 inits = TREE_VALUE (list);
7178 /* Figure out the type of the construction vtable. */
7179 type = build_index_type (size_int (list_length (inits) - 1));
7180 type = build_cplus_array_type (vtable_entry_type, type);
7181 TREE_TYPE (vtbl) = type;
7183 /* Initialize the construction vtable. */
7184 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
7185 initialize_array (vtbl, inits);
7186 dump_vtable (t, binfo, vtbl);
7189 /* Add the vtbl initializers for BINFO (and its bases other than
7190 non-virtual primaries) to the list of INITS. BINFO is in the
7191 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7192 the constructor the vtbl inits should be accumulated for. (If this
7193 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7194 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7195 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7196 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7197 but are not necessarily the same in terms of layout. */
7199 static void
7200 accumulate_vtbl_inits (tree binfo,
7201 tree orig_binfo,
7202 tree rtti_binfo,
7203 tree t,
7204 tree inits)
7206 int i;
7207 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7209 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7210 BINFO_TYPE (orig_binfo)),
7211 20000517);
7213 /* If it doesn't have a vptr, we don't do anything. */
7214 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7215 return;
7217 /* If we're building a construction vtable, we're not interested in
7218 subobjects that don't require construction vtables. */
7219 if (ctor_vtbl_p
7220 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7221 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7222 return;
7224 /* Build the initializers for the BINFO-in-T vtable. */
7225 TREE_VALUE (inits)
7226 = chainon (TREE_VALUE (inits),
7227 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7228 rtti_binfo, t, inits));
7230 /* Walk the BINFO and its bases. We walk in preorder so that as we
7231 initialize each vtable we can figure out at what offset the
7232 secondary vtable lies from the primary vtable. We can't use
7233 dfs_walk here because we need to iterate through bases of BINFO
7234 and RTTI_BINFO simultaneously. */
7235 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7237 tree base_binfo = BINFO_BASETYPE (binfo, i);
7239 /* Skip virtual bases. */
7240 if (TREE_VIA_VIRTUAL (base_binfo))
7241 continue;
7242 accumulate_vtbl_inits (base_binfo,
7243 BINFO_BASETYPE (orig_binfo, i),
7244 rtti_binfo, t,
7245 inits);
7249 /* Called from accumulate_vtbl_inits. Returns the initializers for
7250 the BINFO vtable. */
7252 static tree
7253 dfs_accumulate_vtbl_inits (tree binfo,
7254 tree orig_binfo,
7255 tree rtti_binfo,
7256 tree t,
7257 tree l)
7259 tree inits = NULL_TREE;
7260 tree vtbl = NULL_TREE;
7261 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7263 if (ctor_vtbl_p
7264 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7266 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7267 primary virtual base. If it is not the same primary in
7268 the hierarchy of T, we'll need to generate a ctor vtable
7269 for it, to place at its location in T. If it is the same
7270 primary, we still need a VTT entry for the vtable, but it
7271 should point to the ctor vtable for the base it is a
7272 primary for within the sub-hierarchy of RTTI_BINFO.
7274 There are three possible cases:
7276 1) We are in the same place.
7277 2) We are a primary base within a lost primary virtual base of
7278 RTTI_BINFO.
7279 3) We are primary to something not a base of RTTI_BINFO. */
7281 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7282 tree last = NULL_TREE;
7284 /* First, look through the bases we are primary to for RTTI_BINFO
7285 or a virtual base. */
7286 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7288 last = b;
7289 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7290 break;
7292 /* If we run out of primary links, keep looking down our
7293 inheritance chain; we might be an indirect primary. */
7294 if (b == NULL_TREE)
7295 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7296 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7297 break;
7299 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7300 base B and it is a base of RTTI_BINFO, this is case 2. In
7301 either case, we share our vtable with LAST, i.e. the
7302 derived-most base within B of which we are a primary. */
7303 if (b == rtti_binfo
7304 || (b && purpose_member (BINFO_TYPE (b),
7305 CLASSTYPE_VBASECLASSES (BINFO_TYPE (rtti_binfo)))))
7306 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7307 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7308 binfo_ctor_vtable after everything's been set up. */
7309 vtbl = last;
7311 /* Otherwise, this is case 3 and we get our own. */
7313 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
7314 return inits;
7316 if (!vtbl)
7318 tree index;
7319 int non_fn_entries;
7321 /* Compute the initializer for this vtable. */
7322 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7323 &non_fn_entries);
7325 /* Figure out the position to which the VPTR should point. */
7326 vtbl = TREE_PURPOSE (l);
7327 vtbl = build1 (ADDR_EXPR,
7328 vtbl_ptr_type_node,
7329 vtbl);
7330 TREE_CONSTANT (vtbl) = 1;
7331 index = size_binop (PLUS_EXPR,
7332 size_int (non_fn_entries),
7333 size_int (list_length (TREE_VALUE (l))));
7334 index = size_binop (MULT_EXPR,
7335 TYPE_SIZE_UNIT (vtable_entry_type),
7336 index);
7337 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7338 TREE_CONSTANT (vtbl) = 1;
7341 if (ctor_vtbl_p)
7342 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7343 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7344 straighten this out. */
7345 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7346 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7347 inits = NULL_TREE;
7348 else
7349 /* For an ordinary vtable, set BINFO_VTABLE. */
7350 BINFO_VTABLE (binfo) = vtbl;
7352 return inits;
7355 /* Construct the initializer for BINFO's virtual function table. BINFO
7356 is part of the hierarchy dominated by T. If we're building a
7357 construction vtable, the ORIG_BINFO is the binfo we should use to
7358 find the actual function pointers to put in the vtable - but they
7359 can be overridden on the path to most-derived in the graph that
7360 ORIG_BINFO belongs. Otherwise,
7361 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7362 BINFO that should be indicated by the RTTI information in the
7363 vtable; it will be a base class of T, rather than T itself, if we
7364 are building a construction vtable.
7366 The value returned is a TREE_LIST suitable for wrapping in a
7367 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7368 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7369 number of non-function entries in the vtable.
7371 It might seem that this function should never be called with a
7372 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7373 base is always subsumed by a derived class vtable. However, when
7374 we are building construction vtables, we do build vtables for
7375 primary bases; we need these while the primary base is being
7376 constructed. */
7378 static tree
7379 build_vtbl_initializer (tree binfo,
7380 tree orig_binfo,
7381 tree t,
7382 tree rtti_binfo,
7383 int* non_fn_entries_p)
7385 tree v, b;
7386 tree vfun_inits;
7387 tree vbase;
7388 vtbl_init_data vid;
7390 /* Initialize VID. */
7391 memset (&vid, 0, sizeof (vid));
7392 vid.binfo = binfo;
7393 vid.derived = t;
7394 vid.rtti_binfo = rtti_binfo;
7395 vid.last_init = &vid.inits;
7396 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7397 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7398 vid.generate_vcall_entries = true;
7399 /* The first vbase or vcall offset is at index -3 in the vtable. */
7400 vid.index = ssize_int (-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7402 /* Add entries to the vtable for RTTI. */
7403 build_rtti_vtbl_entries (binfo, &vid);
7405 /* Create an array for keeping track of the functions we've
7406 processed. When we see multiple functions with the same
7407 signature, we share the vcall offsets. */
7408 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7409 /* Add the vcall and vbase offset entries. */
7410 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7411 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7412 build_vbase_offset_vtbl_entries. */
7413 for (vbase = CLASSTYPE_VBASECLASSES (t);
7414 vbase;
7415 vbase = TREE_CHAIN (vbase))
7416 BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase)) = 0;
7418 /* If the target requires padding between data entries, add that now. */
7419 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7421 tree cur, *prev;
7423 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7425 tree add = cur;
7426 int i;
7428 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7429 add = tree_cons (NULL_TREE,
7430 build1 (NOP_EXPR, vtable_entry_type,
7431 null_pointer_node),
7432 add);
7433 *prev = add;
7437 if (non_fn_entries_p)
7438 *non_fn_entries_p = list_length (vid.inits);
7440 /* Go through all the ordinary virtual functions, building up
7441 initializers. */
7442 vfun_inits = NULL_TREE;
7443 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7445 tree delta;
7446 tree vcall_index;
7447 tree fn, fn_original;
7448 tree init = NULL_TREE;
7450 fn = BV_FN (v);
7451 fn_original = fn;
7452 if (DECL_THUNK_P (fn))
7454 if (!DECL_NAME (fn))
7455 finish_thunk (fn);
7456 fn_original = THUNK_TARGET (fn);
7459 /* If the only definition of this function signature along our
7460 primary base chain is from a lost primary, this vtable slot will
7461 never be used, so just zero it out. This is important to avoid
7462 requiring extra thunks which cannot be generated with the function.
7464 We first check this in update_vtable_entry_for_fn, so we handle
7465 restored primary bases properly; we also need to do it here so we
7466 zero out unused slots in ctor vtables, rather than filling themff
7467 with erroneous values (though harmless, apart from relocation
7468 costs). */
7469 for (b = binfo; ; b = get_primary_binfo (b))
7471 /* We found a defn before a lost primary; go ahead as normal. */
7472 if (look_for_overrides_here (BINFO_TYPE (b), fn_original))
7473 break;
7475 /* The nearest definition is from a lost primary; clear the
7476 slot. */
7477 if (BINFO_LOST_PRIMARY_P (b))
7479 init = size_zero_node;
7480 break;
7484 if (! init)
7486 /* Pull the offset for `this', and the function to call, out of
7487 the list. */
7488 delta = BV_DELTA (v);
7489 vcall_index = BV_VCALL_INDEX (v);
7491 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7492 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7494 /* You can't call an abstract virtual function; it's abstract.
7495 So, we replace these functions with __pure_virtual. */
7496 if (DECL_PURE_VIRTUAL_P (fn_original))
7497 fn = abort_fndecl;
7498 else if (!integer_zerop (delta) || vcall_index)
7500 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
7501 if (!DECL_NAME (fn))
7502 finish_thunk (fn);
7504 /* Take the address of the function, considering it to be of an
7505 appropriate generic type. */
7506 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7507 /* The address of a function can't change. */
7508 TREE_CONSTANT (init) = 1;
7511 /* And add it to the chain of initializers. */
7512 if (TARGET_VTABLE_USES_DESCRIPTORS)
7514 int i;
7515 if (init == size_zero_node)
7516 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7517 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7518 else
7519 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7521 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7522 TREE_OPERAND (init, 0),
7523 build_int_2 (i, 0));
7524 TREE_CONSTANT (fdesc) = 1;
7526 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7529 else
7530 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7533 /* The initializers for virtual functions were built up in reverse
7534 order; straighten them out now. */
7535 vfun_inits = nreverse (vfun_inits);
7537 /* The negative offset initializers are also in reverse order. */
7538 vid.inits = nreverse (vid.inits);
7540 /* Chain the two together. */
7541 return chainon (vid.inits, vfun_inits);
7544 /* Adds to vid->inits the initializers for the vbase and vcall
7545 offsets in BINFO, which is in the hierarchy dominated by T. */
7547 static void
7548 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
7550 tree b;
7552 /* If this is a derived class, we must first create entries
7553 corresponding to the primary base class. */
7554 b = get_primary_binfo (binfo);
7555 if (b)
7556 build_vcall_and_vbase_vtbl_entries (b, vid);
7558 /* Add the vbase entries for this base. */
7559 build_vbase_offset_vtbl_entries (binfo, vid);
7560 /* Add the vcall entries for this base. */
7561 build_vcall_offset_vtbl_entries (binfo, vid);
7564 /* Returns the initializers for the vbase offset entries in the vtable
7565 for BINFO (which is part of the class hierarchy dominated by T), in
7566 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7567 where the next vbase offset will go. */
7569 static void
7570 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7572 tree vbase;
7573 tree t;
7574 tree non_primary_binfo;
7576 /* If there are no virtual baseclasses, then there is nothing to
7577 do. */
7578 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7579 return;
7581 t = vid->derived;
7583 /* We might be a primary base class. Go up the inheritance hierarchy
7584 until we find the most derived class of which we are a primary base:
7585 it is the offset of that which we need to use. */
7586 non_primary_binfo = binfo;
7587 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7589 tree b;
7591 /* If we have reached a virtual base, then it must be a primary
7592 base (possibly multi-level) of vid->binfo, or we wouldn't
7593 have called build_vcall_and_vbase_vtbl_entries for it. But it
7594 might be a lost primary, so just skip down to vid->binfo. */
7595 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7597 non_primary_binfo = vid->binfo;
7598 break;
7601 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7602 if (get_primary_binfo (b) != non_primary_binfo)
7603 break;
7604 non_primary_binfo = b;
7607 /* Go through the virtual bases, adding the offsets. */
7608 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7609 vbase;
7610 vbase = TREE_CHAIN (vbase))
7612 tree b;
7613 tree delta;
7615 if (!TREE_VIA_VIRTUAL (vbase))
7616 continue;
7618 /* Find the instance of this virtual base in the complete
7619 object. */
7620 b = copied_binfo (vbase, binfo);
7622 /* If we've already got an offset for this virtual base, we
7623 don't need another one. */
7624 if (BINFO_VTABLE_PATH_MARKED (b))
7625 continue;
7626 BINFO_VTABLE_PATH_MARKED (b) = 1;
7628 /* Figure out where we can find this vbase offset. */
7629 delta = size_binop (MULT_EXPR,
7630 vid->index,
7631 convert (ssizetype,
7632 TYPE_SIZE_UNIT (vtable_entry_type)));
7633 if (vid->primary_vtbl_p)
7634 BINFO_VPTR_FIELD (b) = delta;
7636 if (binfo != TYPE_BINFO (t))
7638 /* The vbase offset had better be the same. */
7639 my_friendly_assert (tree_int_cst_equal (delta,
7640 BINFO_VPTR_FIELD (vbase)),
7641 20030202);
7644 /* The next vbase will come at a more negative offset. */
7645 vid->index = size_binop (MINUS_EXPR, vid->index,
7646 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7648 /* The initializer is the delta from BINFO to this virtual base.
7649 The vbase offsets go in reverse inheritance-graph order, and
7650 we are walking in inheritance graph order so these end up in
7651 the right order. */
7652 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7654 *vid->last_init
7655 = build_tree_list (NULL_TREE,
7656 fold (build1 (NOP_EXPR,
7657 vtable_entry_type,
7658 delta)));
7659 vid->last_init = &TREE_CHAIN (*vid->last_init);
7663 /* Adds the initializers for the vcall offset entries in the vtable
7664 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7665 to VID->INITS. */
7667 static void
7668 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7670 /* We only need these entries if this base is a virtual base. We
7671 compute the indices -- but do not add to the vtable -- when
7672 building the main vtable for a class. */
7673 if (TREE_VIA_VIRTUAL (binfo) || binfo == TYPE_BINFO (vid->derived))
7675 /* We need a vcall offset for each of the virtual functions in this
7676 vtable. For example:
7678 class A { virtual void f (); };
7679 class B1 : virtual public A { virtual void f (); };
7680 class B2 : virtual public A { virtual void f (); };
7681 class C: public B1, public B2 { virtual void f (); };
7683 A C object has a primary base of B1, which has a primary base of A. A
7684 C also has a secondary base of B2, which no longer has a primary base
7685 of A. So the B2-in-C construction vtable needs a secondary vtable for
7686 A, which will adjust the A* to a B2* to call f. We have no way of
7687 knowing what (or even whether) this offset will be when we define B2,
7688 so we store this "vcall offset" in the A sub-vtable and look it up in
7689 a "virtual thunk" for B2::f.
7691 We need entries for all the functions in our primary vtable and
7692 in our non-virtual bases' secondary vtables. */
7693 vid->vbase = binfo;
7694 /* If we are just computing the vcall indices -- but do not need
7695 the actual entries -- not that. */
7696 if (!TREE_VIA_VIRTUAL (binfo))
7697 vid->generate_vcall_entries = false;
7698 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7699 add_vcall_offset_vtbl_entries_r (binfo, vid);
7703 /* Build vcall offsets, starting with those for BINFO. */
7705 static void
7706 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
7708 int i;
7709 tree primary_binfo;
7711 /* Don't walk into virtual bases -- except, of course, for the
7712 virtual base for which we are building vcall offsets. Any
7713 primary virtual base will have already had its offsets generated
7714 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7715 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7716 return;
7718 /* If BINFO has a primary base, process it first. */
7719 primary_binfo = get_primary_binfo (binfo);
7720 if (primary_binfo)
7721 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7723 /* Add BINFO itself to the list. */
7724 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7726 /* Scan the non-primary bases of BINFO. */
7727 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7729 tree base_binfo;
7731 base_binfo = BINFO_BASETYPE (binfo, i);
7732 if (base_binfo != primary_binfo)
7733 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7737 /* Called from build_vcall_offset_vtbl_entries_r. */
7739 static void
7740 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
7742 /* Make entries for the rest of the virtuals. */
7743 if (abi_version_at_least (2))
7745 tree orig_fn;
7747 /* The ABI requires that the methods be processed in declaration
7748 order. G++ 3.2 used the order in the vtable. */
7749 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
7750 orig_fn;
7751 orig_fn = TREE_CHAIN (orig_fn))
7752 if (DECL_VINDEX (orig_fn))
7753 add_vcall_offset (orig_fn, binfo, vid);
7755 else
7757 tree derived_virtuals;
7758 tree base_virtuals;
7759 tree orig_virtuals;
7760 /* If BINFO is a primary base, the most derived class which has
7761 BINFO as a primary base; otherwise, just BINFO. */
7762 tree non_primary_binfo;
7764 /* We might be a primary base class. Go up the inheritance hierarchy
7765 until we find the most derived class of which we are a primary base:
7766 it is the BINFO_VIRTUALS there that we need to consider. */
7767 non_primary_binfo = binfo;
7768 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7770 tree b;
7772 /* If we have reached a virtual base, then it must be vid->vbase,
7773 because we ignore other virtual bases in
7774 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7775 base (possibly multi-level) of vid->binfo, or we wouldn't
7776 have called build_vcall_and_vbase_vtbl_entries for it. But it
7777 might be a lost primary, so just skip down to vid->binfo. */
7778 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7780 if (non_primary_binfo != vid->vbase)
7781 abort ();
7782 non_primary_binfo = vid->binfo;
7783 break;
7786 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7787 if (get_primary_binfo (b) != non_primary_binfo)
7788 break;
7789 non_primary_binfo = b;
7792 if (vid->ctor_vtbl_p)
7793 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7794 where rtti_binfo is the most derived type. */
7795 non_primary_binfo
7796 = original_binfo (non_primary_binfo, vid->rtti_binfo);
7798 for (base_virtuals = BINFO_VIRTUALS (binfo),
7799 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7800 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7801 base_virtuals;
7802 base_virtuals = TREE_CHAIN (base_virtuals),
7803 derived_virtuals = TREE_CHAIN (derived_virtuals),
7804 orig_virtuals = TREE_CHAIN (orig_virtuals))
7806 tree orig_fn;
7808 /* Find the declaration that originally caused this function to
7809 be present in BINFO_TYPE (binfo). */
7810 orig_fn = BV_FN (orig_virtuals);
7812 /* When processing BINFO, we only want to generate vcall slots for
7813 function slots introduced in BINFO. So don't try to generate
7814 one if the function isn't even defined in BINFO. */
7815 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7816 continue;
7818 add_vcall_offset (orig_fn, binfo, vid);
7823 /* Add a vcall offset entry for ORIG_FN to the vtable. */
7825 static void
7826 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
7828 size_t i;
7829 tree vcall_offset;
7831 /* If there is already an entry for a function with the same
7832 signature as FN, then we do not need a second vcall offset.
7833 Check the list of functions already present in the derived
7834 class vtable. */
7835 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7837 tree derived_entry;
7839 derived_entry = VARRAY_TREE (vid->fns, i);
7840 if (same_signature_p (derived_entry, orig_fn)
7841 /* We only use one vcall offset for virtual destructors,
7842 even though there are two virtual table entries. */
7843 || (DECL_DESTRUCTOR_P (derived_entry)
7844 && DECL_DESTRUCTOR_P (orig_fn)))
7845 return;
7848 /* If we are building these vcall offsets as part of building
7849 the vtable for the most derived class, remember the vcall
7850 offset. */
7851 if (vid->binfo == TYPE_BINFO (vid->derived))
7852 CLASSTYPE_VCALL_INDICES (vid->derived)
7853 = tree_cons (orig_fn, vid->index,
7854 CLASSTYPE_VCALL_INDICES (vid->derived));
7856 /* The next vcall offset will be found at a more negative
7857 offset. */
7858 vid->index = size_binop (MINUS_EXPR, vid->index,
7859 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7861 /* Keep track of this function. */
7862 VARRAY_PUSH_TREE (vid->fns, orig_fn);
7864 if (vid->generate_vcall_entries)
7866 tree base;
7867 tree fn;
7869 /* Find the overriding function. */
7870 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
7871 if (fn == error_mark_node)
7872 vcall_offset = build1 (NOP_EXPR, vtable_entry_type,
7873 integer_zero_node);
7874 else
7876 base = TREE_VALUE (fn);
7878 /* The vbase we're working on is a primary base of
7879 vid->binfo. But it might be a lost primary, so its
7880 BINFO_OFFSET might be wrong, so we just use the
7881 BINFO_OFFSET from vid->binfo. */
7882 vcall_offset = size_diffop (BINFO_OFFSET (base),
7883 BINFO_OFFSET (vid->binfo));
7884 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
7885 vcall_offset));
7887 /* Add the intiailizer to the vtable. */
7888 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7889 vid->last_init = &TREE_CHAIN (*vid->last_init);
7893 /* Return vtbl initializers for the RTTI entries coresponding to the
7894 BINFO's vtable. The RTTI entries should indicate the object given
7895 by VID->rtti_binfo. */
7897 static void
7898 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
7900 tree b;
7901 tree t;
7902 tree basetype;
7903 tree offset;
7904 tree decl;
7905 tree init;
7907 basetype = BINFO_TYPE (binfo);
7908 t = BINFO_TYPE (vid->rtti_binfo);
7910 /* To find the complete object, we will first convert to our most
7911 primary base, and then add the offset in the vtbl to that value. */
7912 b = binfo;
7913 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
7914 && !BINFO_LOST_PRIMARY_P (b))
7916 tree primary_base;
7918 primary_base = get_primary_binfo (b);
7919 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
7920 b = primary_base;
7922 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
7924 /* The second entry is the address of the typeinfo object. */
7925 if (flag_rtti)
7926 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
7927 else
7928 decl = integer_zero_node;
7930 /* Convert the declaration to a type that can be stored in the
7931 vtable. */
7932 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
7933 TREE_CONSTANT (init) = 1;
7934 *vid->last_init = build_tree_list (NULL_TREE, init);
7935 vid->last_init = &TREE_CHAIN (*vid->last_init);
7937 /* Add the offset-to-top entry. It comes earlier in the vtable that
7938 the the typeinfo entry. Convert the offset to look like a
7939 function pointer, so that we can put it in the vtable. */
7940 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
7941 TREE_CONSTANT (init) = 1;
7942 *vid->last_init = build_tree_list (NULL_TREE, init);
7943 vid->last_init = &TREE_CHAIN (*vid->last_init);