PR c++/11667
[official-gcc.git] / gcc / cp / class.c
blobb447f7013ee067775604499deba53f8580d280ed
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"
38 #include "convert.h"
40 /* The number of nested classes being processed. If we are not in the
41 scope of any class, this is zero. */
43 int current_class_depth;
45 /* In order to deal with nested classes, we keep a stack of classes.
46 The topmost entry is the innermost class, and is the entry at index
47 CURRENT_CLASS_DEPTH */
49 typedef struct class_stack_node {
50 /* The name of the class. */
51 tree name;
53 /* The _TYPE node for the class. */
54 tree type;
56 /* The access specifier pending for new declarations in the scope of
57 this class. */
58 tree access;
60 /* If were defining TYPE, the names used in this class. */
61 splay_tree names_used;
62 }* class_stack_node_t;
64 typedef struct vtbl_init_data_s
66 /* The base for which we're building initializers. */
67 tree binfo;
68 /* The type of the most-derived type. */
69 tree derived;
70 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
71 unless ctor_vtbl_p is true. */
72 tree rtti_binfo;
73 /* The negative-index vtable initializers built up so far. These
74 are in order from least negative index to most negative index. */
75 tree inits;
76 /* The last (i.e., most negative) entry in INITS. */
77 tree* last_init;
78 /* The binfo for the virtual base for which we're building
79 vcall offset initializers. */
80 tree vbase;
81 /* The functions in vbase for which we have already provided vcall
82 offsets. */
83 varray_type fns;
84 /* The vtable index of the next vcall or vbase offset. */
85 tree index;
86 /* Nonzero if we are building the initializer for the primary
87 vtable. */
88 int primary_vtbl_p;
89 /* Nonzero if we are building the initializer for a construction
90 vtable. */
91 int ctor_vtbl_p;
92 /* True when adding vcall offset entries to the vtable. False when
93 merely computing the indices. */
94 bool generate_vcall_entries;
95 } vtbl_init_data;
97 /* The type of a function passed to walk_subobject_offsets. */
98 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
100 /* The stack itself. This is a dynamically resized array. The
101 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
102 static int current_class_stack_size;
103 static class_stack_node_t current_class_stack;
105 /* An array of all local classes present in this translation unit, in
106 declaration order. */
107 varray_type local_classes;
109 static tree get_vfield_name (tree);
110 static void finish_struct_anon (tree);
111 static tree get_vtable_name (tree);
112 static tree get_basefndecls (tree, tree);
113 static int build_primary_vtable (tree, tree);
114 static int build_secondary_vtable (tree);
115 static void finish_vtbls (tree);
116 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
117 static tree delete_duplicate_fields_1 (tree, tree);
118 static void delete_duplicate_fields (tree);
119 static void finish_struct_bits (tree);
120 static int alter_access (tree, tree, tree);
121 static void handle_using_decl (tree, tree);
122 static void check_for_override (tree, tree);
123 static tree dfs_modify_vtables (tree, void *);
124 static tree modify_all_vtables (tree, tree);
125 static void determine_primary_base (tree);
126 static void finish_struct_methods (tree);
127 static void maybe_warn_about_overly_private_class (tree);
128 static int method_name_cmp (const void *, const void *);
129 static int resort_method_name_cmp (const void *, const void *);
130 static void add_implicitly_declared_members (tree, int, int, int);
131 static tree fixed_type_or_null (tree, int *, int *);
132 static tree resolve_address_of_overloaded_function (tree, tree, int,
133 int, int, tree);
134 static tree build_vtable_entry_ref (tree, tree, tree);
135 static tree build_vtbl_ref_1 (tree, tree);
136 static tree build_vtbl_initializer (tree, tree, tree, tree, int *);
137 static int count_fields (tree);
138 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
139 static void check_bitfield_decl (tree);
140 static void check_field_decl (tree, tree, int *, int *, int *, int *);
141 static void check_field_decls (tree, tree *, int *, int *, int *);
142 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
143 static void build_base_fields (record_layout_info, splay_tree, tree *);
144 static void check_methods (tree);
145 static void remove_zero_width_bit_fields (tree);
146 static void check_bases (tree, int *, int *, int *);
147 static void check_bases_and_members (tree);
148 static tree create_vtable_ptr (tree, tree *);
149 static void include_empty_classes (record_layout_info);
150 static void layout_class_type (tree, tree *);
151 static void fixup_pending_inline (tree);
152 static void fixup_inline_methods (tree);
153 static void set_primary_base (tree, tree);
154 static void propagate_binfo_offsets (tree, tree);
155 static void layout_virtual_bases (record_layout_info, splay_tree);
156 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
157 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
158 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
159 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
160 static void add_vcall_offset (tree, tree, vtbl_init_data *);
161 static void layout_vtable_decl (tree, int);
162 static tree dfs_find_final_overrider (tree, void *);
163 static tree dfs_find_final_overrider_post (tree, void *);
164 static tree dfs_find_final_overrider_q (tree, int, void *);
165 static tree find_final_overrider (tree, tree, tree);
166 static int make_new_vtable (tree, tree);
167 static int maybe_indent_hierarchy (FILE *, int, int);
168 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
169 static void dump_class_hierarchy (tree);
170 static void dump_array (FILE *, tree);
171 static void dump_vtable (tree, tree, tree);
172 static void dump_vtt (tree, tree);
173 static tree build_vtable (tree, tree, tree);
174 static void initialize_vtable (tree, tree);
175 static void initialize_array (tree, tree);
176 static void layout_nonempty_base_or_field (record_layout_info,
177 tree, tree, splay_tree);
178 static tree end_of_class (tree, int);
179 static bool layout_empty_base (tree, tree, splay_tree);
180 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree);
181 static tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree,
182 tree);
183 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
184 static void build_vcall_and_vbase_vtbl_entries (tree,
185 vtbl_init_data *);
186 static void mark_primary_bases (tree);
187 static void clone_constructors_and_destructors (tree);
188 static tree build_clone (tree, tree);
189 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
190 static tree copy_virtuals (tree);
191 static void build_ctor_vtbl_group (tree, tree);
192 static void build_vtt (tree);
193 static tree binfo_ctor_vtable (tree);
194 static tree *build_vtt_inits (tree, tree, tree *, tree *);
195 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
196 static tree dfs_ctor_vtable_bases_queue_p (tree, int, void *data);
197 static tree dfs_fixup_binfo_vtbls (tree, void *);
198 static int record_subobject_offset (tree, tree, splay_tree);
199 static int check_subobject_offset (tree, tree, splay_tree);
200 static int walk_subobject_offsets (tree, subobject_offset_fn,
201 tree, splay_tree, tree, int);
202 static void record_subobject_offsets (tree, tree, splay_tree, int);
203 static int layout_conflict_p (tree, tree, splay_tree, int);
204 static int splay_tree_compare_integer_csts (splay_tree_key k1,
205 splay_tree_key k2);
206 static void warn_about_ambiguous_bases (tree);
207 static bool type_requires_array_cookie (tree);
208 static bool contains_empty_class_p (tree);
209 static bool base_derived_from (tree, tree);
210 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
211 static tree end_of_base (tree);
212 static tree get_vcall_index (tree, tree);
214 /* Macros for dfs walking during vtt construction. See
215 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
216 and dfs_fixup_binfo_vtbls. */
217 #define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
218 #define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
220 /* Variables shared between class.c and call.c. */
222 #ifdef GATHER_STATISTICS
223 int n_vtables = 0;
224 int n_vtable_entries = 0;
225 int n_vtable_searches = 0;
226 int n_vtable_elems = 0;
227 int n_convert_harshness = 0;
228 int n_compute_conversion_costs = 0;
229 int n_build_method_call = 0;
230 int n_inner_fields_searched = 0;
231 #endif
233 /* Convert to or from a base subobject. EXPR is an expression of type
234 `A' or `A*', an expression of type `B' or `B*' is returned. To
235 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
236 the B base instance within A. To convert base A to derived B, CODE
237 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
238 In this latter case, A must not be a morally virtual base of B.
239 NONNULL is true if EXPR is known to be non-NULL (this is only
240 needed when EXPR is of pointer type). CV qualifiers are preserved
241 from EXPR. */
243 tree
244 build_base_path (enum tree_code code,
245 tree expr,
246 tree binfo,
247 int nonnull)
249 tree v_binfo = NULL_TREE;
250 tree d_binfo = NULL_TREE;
251 tree probe;
252 tree offset;
253 tree target_type;
254 tree null_test = NULL;
255 tree ptr_target_type;
256 int fixed_type_p;
257 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
259 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
260 return error_mark_node;
262 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
264 d_binfo = probe;
265 if (!v_binfo && TREE_VIA_VIRTUAL (probe))
266 v_binfo = probe;
269 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
270 if (want_pointer)
271 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
273 my_friendly_assert (code == MINUS_EXPR
274 ? same_type_p (BINFO_TYPE (binfo), probe)
275 : code == PLUS_EXPR
276 ? same_type_p (BINFO_TYPE (d_binfo), probe)
277 : false, 20010723);
279 if (code == MINUS_EXPR && v_binfo)
281 error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
282 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
283 return error_mark_node;
286 if (!want_pointer)
287 /* This must happen before the call to save_expr. */
288 expr = build_unary_op (ADDR_EXPR, expr, 0);
290 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
291 if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
292 expr = save_expr (expr);
294 if (want_pointer && !nonnull)
295 null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
297 offset = BINFO_OFFSET (binfo);
299 if (v_binfo && fixed_type_p <= 0)
301 /* Going via virtual base V_BINFO. We need the static offset
302 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
303 V_BINFO. That offset is an entry in D_BINFO's vtable. */
304 tree v_offset;
306 if (fixed_type_p < 0 && in_base_initializer)
308 /* In a base member initializer, we cannot rely on
309 the vtable being set up. We have to use the vtt_parm. */
310 tree derived = BINFO_INHERITANCE_CHAIN (v_binfo);
312 v_offset = build (PLUS_EXPR, TREE_TYPE (current_vtt_parm),
313 current_vtt_parm, BINFO_VPTR_INDEX (derived));
315 v_offset = build1 (INDIRECT_REF,
316 TREE_TYPE (TYPE_VFIELD (BINFO_TYPE (derived))),
317 v_offset);
320 else
321 v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
322 TREE_TYPE (TREE_TYPE (expr)));
324 v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
325 v_offset, BINFO_VPTR_FIELD (v_binfo));
326 v_offset = build1 (NOP_EXPR,
327 build_pointer_type (ptrdiff_type_node),
328 v_offset);
329 v_offset = build_indirect_ref (v_offset, NULL);
330 TREE_CONSTANT (v_offset) = 1;
332 offset = convert_to_integer (ptrdiff_type_node,
333 size_diffop (offset,
334 BINFO_OFFSET (v_binfo)));
336 if (!integer_zerop (offset))
337 v_offset = build (code, ptrdiff_type_node, v_offset, offset);
339 if (fixed_type_p < 0)
340 /* Negative fixed_type_p means this is a constructor or destructor;
341 virtual base layout is fixed in in-charge [cd]tors, but not in
342 base [cd]tors. */
343 offset = build (COND_EXPR, ptrdiff_type_node,
344 build (EQ_EXPR, boolean_type_node,
345 current_in_charge_parm, integer_zero_node),
346 v_offset,
347 BINFO_OFFSET (binfo));
348 else
349 offset = v_offset;
352 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
354 target_type = cp_build_qualified_type
355 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
356 ptr_target_type = build_pointer_type (target_type);
357 if (want_pointer)
358 target_type = ptr_target_type;
360 expr = build1 (NOP_EXPR, ptr_target_type, expr);
362 if (!integer_zerop (offset))
363 expr = build (code, ptr_target_type, expr, offset);
364 else
365 null_test = NULL;
367 if (!want_pointer)
368 expr = build_indirect_ref (expr, NULL);
370 if (null_test)
371 expr = build (COND_EXPR, target_type, null_test,
372 build1 (NOP_EXPR, target_type, integer_zero_node),
373 expr);
375 return expr;
378 /* Convert OBJECT to the base TYPE. If CHECK_ACCESS is true, an error
379 message is emitted if TYPE is inaccessible. OBJECT is assumed to
380 be non-NULL. */
382 tree
383 convert_to_base (tree object, tree type, bool check_access)
385 tree binfo;
387 binfo = lookup_base (TREE_TYPE (object), type,
388 check_access ? ba_check : ba_ignore,
389 NULL);
390 if (!binfo || binfo == error_mark_node)
391 return error_mark_node;
393 return build_base_path (PLUS_EXPR, object, binfo, /*nonnull=*/1);
396 /* EXPR is an expression with class type. BASE is a base class (a
397 BINFO) of that class type. Returns EXPR, converted to the BASE
398 type. This function assumes that EXPR is the most derived class;
399 therefore virtual bases can be found at their static offsets. */
401 tree
402 convert_to_base_statically (tree expr, tree base)
404 tree expr_type;
406 expr_type = TREE_TYPE (expr);
407 if (!same_type_p (expr_type, BINFO_TYPE (base)))
409 tree pointer_type;
411 pointer_type = build_pointer_type (expr_type);
412 expr = build_unary_op (ADDR_EXPR, expr, /*noconvert=*/1);
413 if (!integer_zerop (BINFO_OFFSET (base)))
414 expr = build (PLUS_EXPR, pointer_type, expr,
415 build_nop (pointer_type, BINFO_OFFSET (base)));
416 expr = build_nop (build_pointer_type (BINFO_TYPE (base)), expr);
417 expr = build1 (INDIRECT_REF, BINFO_TYPE (base), expr);
420 return expr;
424 /* Virtual function things. */
426 static tree
427 build_vtable_entry_ref (tree array_ref, tree instance, tree idx)
429 tree i, i2, vtable, first_fn, basetype;
431 basetype = non_reference (TREE_TYPE (instance));
433 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
434 first_fn = TYPE_BINFO_VTABLE (basetype);
436 i = fold (build_array_ref (first_fn, idx));
437 i = fold (build_c_cast (ptrdiff_type_node,
438 build_unary_op (ADDR_EXPR, i, 0)));
439 i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
440 i2 = fold (build_c_cast (ptrdiff_type_node,
441 build_unary_op (ADDR_EXPR, i2, 0)));
442 i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
444 if (TREE_CODE (i) != INTEGER_CST)
445 abort ();
447 return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
450 /* Given an object INSTANCE, return an expression which yields the
451 vtable element corresponding to INDEX. There are many special
452 cases for INSTANCE which we take care of here, mainly to avoid
453 creating extra tree nodes when we don't have to. */
455 static tree
456 build_vtbl_ref_1 (tree instance, tree idx)
458 tree aref;
459 tree vtbl = NULL_TREE;
461 /* Try to figure out what a reference refers to, and
462 access its virtual function table directly. */
464 int cdtorp = 0;
465 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
467 tree basetype = non_reference (TREE_TYPE (instance));
469 if (fixed_type && !cdtorp)
471 tree binfo = lookup_base (fixed_type, basetype,
472 ba_ignore|ba_quiet, NULL);
473 if (binfo)
474 vtbl = BINFO_VTABLE (binfo);
477 if (!vtbl)
478 vtbl = build_vfield_ref (instance, basetype);
480 assemble_external (vtbl);
482 aref = build_array_ref (vtbl, idx);
483 TREE_CONSTANT (aref) = 1;
485 return aref;
488 tree
489 build_vtbl_ref (tree instance, tree idx)
491 tree aref = build_vtbl_ref_1 (instance, idx);
493 if (flag_vtable_gc)
494 aref = build_vtable_entry_ref (aref, instance, idx);
496 return aref;
499 /* Given an object INSTANCE, return an expression which yields a
500 function pointer corresponding to vtable element INDEX. */
502 tree
503 build_vfn_ref (tree instance, tree idx)
505 tree aref = build_vtbl_ref_1 (instance, idx);
507 /* When using function descriptors, the address of the
508 vtable entry is treated as a function pointer. */
509 if (TARGET_VTABLE_USES_DESCRIPTORS)
510 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
511 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
513 if (flag_vtable_gc)
514 aref = build_vtable_entry_ref (aref, instance, idx);
516 return aref;
519 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
520 for the given TYPE. */
522 static tree
523 get_vtable_name (tree type)
525 return mangle_vtbl_for_type (type);
528 /* Return an IDENTIFIER_NODE for the name of the virtual table table
529 for TYPE. */
531 tree
532 get_vtt_name (tree type)
534 return mangle_vtt_for_type (type);
537 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
538 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
539 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
541 static tree
542 build_vtable (tree class_type, tree name, tree vtable_type)
544 tree decl;
546 decl = build_lang_decl (VAR_DECL, name, vtable_type);
547 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
548 now to avoid confusion in mangle_decl. */
549 SET_DECL_ASSEMBLER_NAME (decl, name);
550 DECL_CONTEXT (decl) = class_type;
551 DECL_ARTIFICIAL (decl) = 1;
552 TREE_STATIC (decl) = 1;
553 TREE_READONLY (decl) = 1;
554 DECL_VIRTUAL_P (decl) = 1;
555 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
556 DECL_VTABLE_OR_VTT_P (decl) = 1;
558 /* At one time the vtable info was grabbed 2 words at a time. This
559 fails on sparc unless you have 8-byte alignment. (tiemann) */
560 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
561 DECL_ALIGN (decl));
563 import_export_vtable (decl, class_type, 0);
565 return decl;
568 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
569 or even complete. If this does not exist, create it. If COMPLETE is
570 nonzero, then complete the definition of it -- that will render it
571 impossible to actually build the vtable, but is useful to get at those
572 which are known to exist in the runtime. */
574 tree
575 get_vtable_decl (tree type, int complete)
577 tree decl;
579 if (CLASSTYPE_VTABLES (type))
580 return CLASSTYPE_VTABLES (type);
582 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
583 CLASSTYPE_VTABLES (type) = decl;
585 if (complete)
587 DECL_EXTERNAL (decl) = 1;
588 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
591 return decl;
594 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
595 BV_VCALL_INDEX for each entry is cleared. */
597 static tree
598 copy_virtuals (tree binfo)
600 tree copies;
601 tree t;
603 copies = copy_list (BINFO_VIRTUALS (binfo));
604 for (t = copies; t; t = TREE_CHAIN (t))
605 BV_VCALL_INDEX (t) = NULL_TREE;
607 return copies;
610 /* Build the primary virtual function table for TYPE. If BINFO is
611 non-NULL, build the vtable starting with the initial approximation
612 that it is the same as the one which is the head of the association
613 list. Returns a nonzero value if a new vtable is actually
614 created. */
616 static int
617 build_primary_vtable (tree binfo, tree type)
619 tree decl;
620 tree virtuals;
622 decl = get_vtable_decl (type, /*complete=*/0);
624 if (binfo)
626 if (BINFO_NEW_VTABLE_MARKED (binfo))
627 /* We have already created a vtable for this base, so there's
628 no need to do it again. */
629 return 0;
631 virtuals = copy_virtuals (binfo);
632 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
633 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
634 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
636 else
638 my_friendly_assert (TREE_TYPE (decl) == vtbl_type_node, 20000118);
639 virtuals = NULL_TREE;
642 #ifdef GATHER_STATISTICS
643 n_vtables += 1;
644 n_vtable_elems += list_length (virtuals);
645 #endif
647 /* Initialize the association list for this type, based
648 on our first approximation. */
649 TYPE_BINFO_VTABLE (type) = decl;
650 TYPE_BINFO_VIRTUALS (type) = virtuals;
651 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
652 return 1;
655 /* Give BINFO a new virtual function table which is initialized
656 with a skeleton-copy of its original initialization. The only
657 entry that changes is the `delta' entry, so we can really
658 share a lot of structure.
660 FOR_TYPE is the most derived type which caused this table to
661 be needed.
663 Returns nonzero if we haven't met BINFO before.
665 The order in which vtables are built (by calling this function) for
666 an object must remain the same, otherwise a binary incompatibility
667 can result. */
669 static int
670 build_secondary_vtable (tree binfo)
672 if (BINFO_NEW_VTABLE_MARKED (binfo))
673 /* We already created a vtable for this base. There's no need to
674 do it again. */
675 return 0;
677 /* Remember that we've created a vtable for this BINFO, so that we
678 don't try to do so again. */
679 SET_BINFO_NEW_VTABLE_MARKED (binfo);
681 /* Make fresh virtual list, so we can smash it later. */
682 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
684 /* Secondary vtables are laid out as part of the same structure as
685 the primary vtable. */
686 BINFO_VTABLE (binfo) = NULL_TREE;
687 return 1;
690 /* Create a new vtable for BINFO which is the hierarchy dominated by
691 T. Return nonzero if we actually created a new vtable. */
693 static int
694 make_new_vtable (tree t, tree binfo)
696 if (binfo == TYPE_BINFO (t))
697 /* In this case, it is *type*'s vtable we are modifying. We start
698 with the approximation that its vtable is that of the
699 immediate base class. */
700 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
701 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
702 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
704 else
705 /* This is our very own copy of `basetype' to play with. Later,
706 we will fill in all the virtual functions that override the
707 virtual functions in these base classes which are not defined
708 by the current type. */
709 return build_secondary_vtable (binfo);
712 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
713 (which is in the hierarchy dominated by T) list FNDECL as its
714 BV_FN. DELTA is the required constant adjustment from the `this'
715 pointer where the vtable entry appears to the `this' required when
716 the function is actually called. */
718 static void
719 modify_vtable_entry (tree t,
720 tree binfo,
721 tree fndecl,
722 tree delta,
723 tree *virtuals)
725 tree v;
727 v = *virtuals;
729 if (fndecl != BV_FN (v)
730 || !tree_int_cst_equal (delta, BV_DELTA (v)))
732 /* We need a new vtable for BINFO. */
733 if (make_new_vtable (t, binfo))
735 /* If we really did make a new vtable, we also made a copy
736 of the BINFO_VIRTUALS list. Now, we have to find the
737 corresponding entry in that list. */
738 *virtuals = BINFO_VIRTUALS (binfo);
739 while (BV_FN (*virtuals) != BV_FN (v))
740 *virtuals = TREE_CHAIN (*virtuals);
741 v = *virtuals;
744 BV_DELTA (v) = delta;
745 BV_VCALL_INDEX (v) = NULL_TREE;
746 BV_FN (v) = fndecl;
751 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
752 the method after the class has already been defined because a
753 declaration for it was seen. (Even though that is erroneous, we
754 add the method for improved error recovery.) */
756 void
757 add_method (tree type, tree method, int error_p)
759 int using = (DECL_CONTEXT (method) != type);
760 int len;
761 int slot;
762 tree method_vec;
763 int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
764 && DECL_TEMPLATE_CONV_FN_P (method));
766 if (!CLASSTYPE_METHOD_VEC (type))
767 /* Make a new method vector. We start with 8 entries. We must
768 allocate at least two (for constructors and destructors), and
769 we're going to end up with an assignment operator at some point
770 as well.
772 We could use a TREE_LIST for now, and convert it to a TREE_VEC
773 in finish_struct, but we would probably waste more memory
774 making the links in the list than we would by over-allocating
775 the size of the vector here. Furthermore, we would complicate
776 all the code that expects this to be a vector. */
777 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
779 method_vec = CLASSTYPE_METHOD_VEC (type);
780 len = TREE_VEC_LENGTH (method_vec);
782 /* Constructors and destructors go in special slots. */
783 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
784 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
785 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
786 slot = CLASSTYPE_DESTRUCTOR_SLOT;
787 else
789 int have_template_convs_p = 0;
791 /* See if we already have an entry with this name. */
792 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
794 tree m = TREE_VEC_ELT (method_vec, slot);
796 if (!m)
797 break;
798 m = OVL_CURRENT (m);
800 if (template_conv_p)
802 have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
803 && DECL_TEMPLATE_CONV_FN_P (m));
805 /* If we need to move things up, see if there's
806 space. */
807 if (!have_template_convs_p)
809 slot = len - 1;
810 if (TREE_VEC_ELT (method_vec, slot))
811 slot++;
813 break;
815 if (DECL_NAME (m) == DECL_NAME (method))
816 break;
819 if (slot == len)
821 /* We need a bigger method vector. */
822 int new_len;
823 tree new_vec;
825 /* In the non-error case, we are processing a class
826 definition. Double the size of the vector to give room
827 for new methods. */
828 if (!error_p)
829 new_len = 2 * len;
830 /* In the error case, the vector is already complete. We
831 don't expect many errors, and the rest of the front-end
832 will get confused if there are empty slots in the vector. */
833 else
834 new_len = len + 1;
836 new_vec = make_tree_vec (new_len);
837 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
838 len * sizeof (tree));
839 len = new_len;
840 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
843 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
845 /* Type conversion operators have to come before ordinary
846 methods; add_conversions depends on this to speed up
847 looking for conversion operators. So, if necessary, we
848 slide some of the vector elements up. In theory, this
849 makes this algorithm O(N^2) but we don't expect many
850 conversion operators. */
851 if (template_conv_p)
852 slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
853 else
854 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
856 tree fn = TREE_VEC_ELT (method_vec, slot);
858 if (!fn)
859 /* There are no more entries in the vector, so we
860 can insert the new conversion operator here. */
861 break;
863 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
864 /* We can insert the new function right at the
865 SLOTth position. */
866 break;
869 if (template_conv_p && have_template_convs_p)
870 /*OK*/;
871 else if (!TREE_VEC_ELT (method_vec, slot))
872 /* There is nothing in the Ith slot, so we can avoid
873 moving anything. */
875 else
877 /* We know the last slot in the vector is empty
878 because we know that at this point there's room
879 for a new function. */
880 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
881 &TREE_VEC_ELT (method_vec, slot),
882 (len - slot - 1) * sizeof (tree));
883 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
888 if (template_class_depth (type))
889 /* TYPE is a template class. Don't issue any errors now; wait
890 until instantiation time to complain. */
892 else
894 tree fns;
896 /* Check to see if we've already got this method. */
897 for (fns = TREE_VEC_ELT (method_vec, slot);
898 fns;
899 fns = OVL_NEXT (fns))
901 tree fn = OVL_CURRENT (fns);
902 tree parms1;
903 tree parms2;
904 bool same = 1;
906 if (TREE_CODE (fn) != TREE_CODE (method))
907 continue;
909 /* [over.load] Member function declarations with the
910 same name and the same parameter types cannot be
911 overloaded if any of them is a static member
912 function declaration.
914 [namespace.udecl] When a using-declaration brings names
915 from a base class into a derived class scope, member
916 functions in the derived class override and/or hide member
917 functions with the same name and parameter types in a base
918 class (rather than conflicting). */
919 parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
920 parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
922 /* Compare the quals on the 'this' parm. Don't compare
923 the whole types, as used functions are treated as
924 coming from the using class in overload resolution. */
925 if (! DECL_STATIC_FUNCTION_P (fn)
926 && ! DECL_STATIC_FUNCTION_P (method)
927 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
928 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
929 same = 0;
931 /* For templates, the template parms must be identical. */
932 if (TREE_CODE (fn) == TEMPLATE_DECL
933 && !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
934 DECL_TEMPLATE_PARMS (method)))
935 same = 0;
937 if (! DECL_STATIC_FUNCTION_P (fn))
938 parms1 = TREE_CHAIN (parms1);
939 if (! DECL_STATIC_FUNCTION_P (method))
940 parms2 = TREE_CHAIN (parms2);
942 if (same && compparms (parms1, parms2)
943 && (!DECL_CONV_FN_P (fn)
944 || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
945 TREE_TYPE (TREE_TYPE (method)))))
947 if (using && DECL_CONTEXT (fn) == type)
948 /* Defer to the local function. */
949 return;
950 else
952 cp_error_at ("`%#D' and `%#D' cannot be overloaded",
953 method, fn);
955 /* We don't call duplicate_decls here to merge
956 the declarations because that will confuse
957 things if the methods have inline
958 definitions. In particular, we will crash
959 while processing the definitions. */
960 return;
966 /* Actually insert the new method. */
967 TREE_VEC_ELT (method_vec, slot)
968 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
970 /* Add the new binding. */
971 if (!DECL_CONSTRUCTOR_P (method)
972 && !DECL_DESTRUCTOR_P (method))
973 push_class_level_binding (DECL_NAME (method),
974 TREE_VEC_ELT (method_vec, slot));
977 /* Subroutines of finish_struct. */
979 /* Look through the list of fields for this struct, deleting
980 duplicates as we go. This must be recursive to handle
981 anonymous unions.
983 FIELD is the field which may not appear anywhere in FIELDS.
984 FIELD_PTR, if non-null, is the starting point at which
985 chained deletions may take place.
986 The value returned is the first acceptable entry found
987 in FIELDS.
989 Note that anonymous fields which are not of UNION_TYPE are
990 not duplicates, they are just anonymous fields. This happens
991 when we have unnamed bitfields, for example. */
993 static tree
994 delete_duplicate_fields_1 (tree field, tree fields)
996 tree x;
997 tree prev = 0;
998 if (DECL_NAME (field) == 0)
1000 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1001 return fields;
1003 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1004 fields = delete_duplicate_fields_1 (x, fields);
1005 return fields;
1007 else
1009 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1011 if (DECL_NAME (x) == 0)
1013 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1014 continue;
1015 TYPE_FIELDS (TREE_TYPE (x))
1016 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1017 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1019 if (prev == 0)
1020 fields = TREE_CHAIN (fields);
1021 else
1022 TREE_CHAIN (prev) = TREE_CHAIN (x);
1025 else if (TREE_CODE (field) == USING_DECL)
1026 /* A using declaration is allowed to appear more than
1027 once. We'll prune these from the field list later, and
1028 handle_using_decl will complain about invalid multiple
1029 uses. */
1031 else if (DECL_NAME (field) == DECL_NAME (x))
1033 if (TREE_CODE (field) == CONST_DECL
1034 && TREE_CODE (x) == CONST_DECL)
1035 cp_error_at ("duplicate enum value `%D'", x);
1036 else if (TREE_CODE (field) == CONST_DECL
1037 || TREE_CODE (x) == CONST_DECL)
1038 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1040 else if (DECL_DECLARES_TYPE_P (field)
1041 && DECL_DECLARES_TYPE_P (x))
1043 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1044 continue;
1045 cp_error_at ("duplicate nested type `%D'", x);
1047 else if (DECL_DECLARES_TYPE_P (field)
1048 || DECL_DECLARES_TYPE_P (x))
1050 /* Hide tag decls. */
1051 if ((TREE_CODE (field) == TYPE_DECL
1052 && DECL_ARTIFICIAL (field))
1053 || (TREE_CODE (x) == TYPE_DECL
1054 && DECL_ARTIFICIAL (x)))
1055 continue;
1056 cp_error_at ("duplicate field `%D' (as type and non-type)",
1059 else
1060 cp_error_at ("duplicate member `%D'", x);
1061 if (prev == 0)
1062 fields = TREE_CHAIN (fields);
1063 else
1064 TREE_CHAIN (prev) = TREE_CHAIN (x);
1068 return fields;
1071 static void
1072 delete_duplicate_fields (tree fields)
1074 tree x;
1075 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1076 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1079 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1080 legit, otherwise return 0. */
1082 static int
1083 alter_access (tree t, tree fdecl, tree access)
1085 tree elem;
1087 if (!DECL_LANG_SPECIFIC (fdecl))
1088 retrofit_lang_decl (fdecl);
1090 my_friendly_assert (!DECL_DISCRIMINATOR_P (fdecl), 20030624);
1092 elem = purpose_member (t, DECL_ACCESS (fdecl));
1093 if (elem)
1095 if (TREE_VALUE (elem) != access)
1097 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1098 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1099 else
1100 error ("conflicting access specifications for field `%s', ignored",
1101 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1103 else
1105 /* They're changing the access to the same thing they changed
1106 it to before. That's OK. */
1110 else
1112 perform_or_defer_access_check (TYPE_BINFO (t), fdecl);
1113 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1114 return 1;
1116 return 0;
1119 /* Process the USING_DECL, which is a member of T. */
1121 static void
1122 handle_using_decl (tree using_decl, tree t)
1124 tree ctype = DECL_INITIAL (using_decl);
1125 tree name = DECL_NAME (using_decl);
1126 tree access
1127 = TREE_PRIVATE (using_decl) ? access_private_node
1128 : TREE_PROTECTED (using_decl) ? access_protected_node
1129 : access_public_node;
1130 tree fdecl, binfo;
1131 tree flist = NULL_TREE;
1132 tree old_value;
1134 if (ctype == error_mark_node)
1135 return;
1137 binfo = lookup_base (t, ctype, ba_any, NULL);
1138 if (! binfo)
1140 error_not_base_type (t, ctype);
1141 return;
1144 if (constructor_name_p (name, ctype))
1146 cp_error_at ("`%D' names constructor", using_decl);
1147 return;
1149 if (constructor_name_p (name, t))
1151 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1152 return;
1155 fdecl = lookup_member (binfo, name, 0, false);
1157 if (!fdecl)
1159 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1160 return;
1163 if (BASELINK_P (fdecl))
1164 /* Ignore base type this came from. */
1165 fdecl = BASELINK_FUNCTIONS (fdecl);
1167 old_value = IDENTIFIER_CLASS_VALUE (name);
1168 if (old_value)
1170 if (is_overloaded_fn (old_value))
1171 old_value = OVL_CURRENT (old_value);
1173 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1174 /* OK */;
1175 else
1176 old_value = NULL_TREE;
1179 if (is_overloaded_fn (fdecl))
1180 flist = fdecl;
1182 if (! old_value)
1184 else if (is_overloaded_fn (old_value))
1186 if (flist)
1187 /* It's OK to use functions from a base when there are functions with
1188 the same name already present in the current class. */;
1189 else
1191 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1192 cp_error_at (" because of local method `%#D' with same name",
1193 OVL_CURRENT (old_value));
1194 return;
1197 else if (!DECL_ARTIFICIAL (old_value))
1199 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1200 cp_error_at (" because of local member `%#D' with same name", old_value);
1201 return;
1204 /* Make type T see field decl FDECL with access ACCESS.*/
1205 if (flist)
1206 for (; flist; flist = OVL_NEXT (flist))
1208 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1209 alter_access (t, OVL_CURRENT (flist), access);
1211 else
1212 alter_access (t, fdecl, access);
1215 /* Run through the base clases of T, updating
1216 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1217 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1218 the bases. */
1220 static void
1221 check_bases (tree t,
1222 int* cant_have_default_ctor_p,
1223 int* cant_have_const_ctor_p,
1224 int* no_const_asn_ref_p)
1226 int n_baseclasses;
1227 int i;
1228 int seen_non_virtual_nearly_empty_base_p;
1229 tree binfos;
1231 binfos = TYPE_BINFO_BASETYPES (t);
1232 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1233 seen_non_virtual_nearly_empty_base_p = 0;
1235 /* An aggregate cannot have baseclasses. */
1236 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1238 for (i = 0; i < n_baseclasses; ++i)
1240 tree base_binfo;
1241 tree basetype;
1243 /* Figure out what base we're looking at. */
1244 base_binfo = TREE_VEC_ELT (binfos, i);
1245 basetype = TREE_TYPE (base_binfo);
1247 /* If the type of basetype is incomplete, then we already
1248 complained about that fact (and we should have fixed it up as
1249 well). */
1250 if (!COMPLETE_TYPE_P (basetype))
1252 int j;
1253 /* The base type is of incomplete type. It is
1254 probably best to pretend that it does not
1255 exist. */
1256 if (i == n_baseclasses-1)
1257 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1258 TREE_VEC_LENGTH (binfos) -= 1;
1259 n_baseclasses -= 1;
1260 for (j = i; j+1 < n_baseclasses; j++)
1261 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1262 continue;
1265 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1266 here because the case of virtual functions but non-virtual
1267 dtor is handled in finish_struct_1. */
1268 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1269 && TYPE_HAS_DESTRUCTOR (basetype))
1270 warning ("base class `%#T' has a non-virtual destructor",
1271 basetype);
1273 /* If the base class doesn't have copy constructors or
1274 assignment operators that take const references, then the
1275 derived class cannot have such a member automatically
1276 generated. */
1277 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1278 *cant_have_const_ctor_p = 1;
1279 if (TYPE_HAS_ASSIGN_REF (basetype)
1280 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1281 *no_const_asn_ref_p = 1;
1282 /* Similarly, if the base class doesn't have a default
1283 constructor, then the derived class won't have an
1284 automatically generated default constructor. */
1285 if (TYPE_HAS_CONSTRUCTOR (basetype)
1286 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1288 *cant_have_default_ctor_p = 1;
1289 if (! TYPE_HAS_CONSTRUCTOR (t))
1290 pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1291 basetype);
1294 if (TREE_VIA_VIRTUAL (base_binfo))
1295 /* A virtual base does not effect nearly emptiness. */
1297 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1299 if (seen_non_virtual_nearly_empty_base_p)
1300 /* And if there is more than one nearly empty base, then the
1301 derived class is not nearly empty either. */
1302 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1303 else
1304 /* Remember we've seen one. */
1305 seen_non_virtual_nearly_empty_base_p = 1;
1307 else if (!is_empty_class (basetype))
1308 /* If the base class is not empty or nearly empty, then this
1309 class cannot be nearly empty. */
1310 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1312 /* A lot of properties from the bases also apply to the derived
1313 class. */
1314 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1315 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1316 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1317 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1318 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1319 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1320 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1321 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1322 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1323 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1324 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1325 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1329 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1330 dominated by TYPE that are primary bases. */
1332 static void
1333 mark_primary_bases (tree type)
1335 tree binfo;
1337 /* Walk the bases in inheritance graph order. */
1338 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1340 tree base_binfo = get_primary_binfo (binfo);
1342 if (!base_binfo)
1343 /* Not a dynamic base. */;
1344 else if (BINFO_PRIMARY_P (base_binfo))
1345 BINFO_LOST_PRIMARY_P (binfo) = 1;
1346 else
1348 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1349 /* A virtual binfo might have been copied from within
1350 another hierarchy. As we're about to use it as a primary
1351 base, make sure the offsets match. */
1352 if (TREE_VIA_VIRTUAL (base_binfo))
1354 tree delta = size_diffop (convert (ssizetype,
1355 BINFO_OFFSET (binfo)),
1356 convert (ssizetype,
1357 BINFO_OFFSET (base_binfo)));
1359 propagate_binfo_offsets (base_binfo, delta);
1365 /* Make the BINFO the primary base of T. */
1367 static void
1368 set_primary_base (tree t, tree binfo)
1370 tree basetype;
1372 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1373 basetype = BINFO_TYPE (binfo);
1374 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1375 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1376 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1379 /* Determine the primary class for T. */
1381 static void
1382 determine_primary_base (tree t)
1384 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1385 tree vbases;
1386 tree type_binfo;
1388 /* If there are no baseclasses, there is certainly no primary base. */
1389 if (n_baseclasses == 0)
1390 return;
1392 type_binfo = TYPE_BINFO (t);
1394 for (i = 0; i < n_baseclasses; i++)
1396 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1397 tree basetype = BINFO_TYPE (base_binfo);
1399 if (TYPE_CONTAINS_VPTR_P (basetype))
1401 /* We prefer a non-virtual base, although a virtual one will
1402 do. */
1403 if (TREE_VIA_VIRTUAL (base_binfo))
1404 continue;
1406 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1408 set_primary_base (t, base_binfo);
1409 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1411 else
1413 tree vfields;
1415 /* Only add unique vfields, and flatten them out as we go. */
1416 for (vfields = CLASSTYPE_VFIELDS (basetype);
1417 vfields;
1418 vfields = TREE_CHAIN (vfields))
1419 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1420 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1421 CLASSTYPE_VFIELDS (t)
1422 = tree_cons (base_binfo,
1423 VF_BASETYPE_VALUE (vfields),
1424 CLASSTYPE_VFIELDS (t));
1429 if (!TYPE_VFIELD (t))
1430 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1432 /* Find the indirect primary bases - those virtual bases which are primary
1433 bases of something else in this hierarchy. */
1434 for (vbases = CLASSTYPE_VBASECLASSES (t);
1435 vbases;
1436 vbases = TREE_CHAIN (vbases))
1438 tree vbase_binfo = TREE_VALUE (vbases);
1440 /* See if this virtual base is an indirect primary base. To be so,
1441 it must be a primary base within the hierarchy of one of our
1442 direct bases. */
1443 for (i = 0; i < n_baseclasses; ++i)
1445 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1446 tree v;
1448 for (v = CLASSTYPE_VBASECLASSES (basetype);
1450 v = TREE_CHAIN (v))
1452 tree base_vbase = TREE_VALUE (v);
1454 if (BINFO_PRIMARY_P (base_vbase)
1455 && same_type_p (BINFO_TYPE (base_vbase),
1456 BINFO_TYPE (vbase_binfo)))
1458 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1459 break;
1463 /* If we've discovered that this virtual base is an indirect
1464 primary base, then we can move on to the next virtual
1465 base. */
1466 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1467 break;
1471 /* A "nearly-empty" virtual base class can be the primary base
1472 class, if no non-virtual polymorphic base can be found. */
1473 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1475 /* If not NULL, this is the best primary base candidate we have
1476 found so far. */
1477 tree candidate = NULL_TREE;
1478 tree base_binfo;
1480 /* Loop over the baseclasses. */
1481 for (base_binfo = TYPE_BINFO (t);
1482 base_binfo;
1483 base_binfo = TREE_CHAIN (base_binfo))
1485 tree basetype = BINFO_TYPE (base_binfo);
1487 if (TREE_VIA_VIRTUAL (base_binfo)
1488 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1490 /* If this is not an indirect primary base, then it's
1491 definitely our primary base. */
1492 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1494 candidate = base_binfo;
1495 break;
1498 /* If this is an indirect primary base, it still could be
1499 our primary base -- unless we later find there's another
1500 nearly-empty virtual base that isn't an indirect
1501 primary base. */
1502 if (!candidate)
1503 candidate = base_binfo;
1507 /* If we've got a primary base, use it. */
1508 if (candidate)
1510 set_primary_base (t, candidate);
1511 CLASSTYPE_VFIELDS (t)
1512 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1516 /* Mark the primary base classes at this point. */
1517 mark_primary_bases (t);
1520 /* Set memoizing fields and bits of T (and its variants) for later
1521 use. */
1523 static void
1524 finish_struct_bits (tree t)
1526 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1528 /* Fix up variants (if any). */
1529 tree variants = TYPE_NEXT_VARIANT (t);
1530 while (variants)
1532 /* These fields are in the _TYPE part of the node, not in
1533 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1534 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1535 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1536 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1537 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1538 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1540 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1541 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1542 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1543 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1544 /* Copy whatever these are holding today. */
1545 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1546 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1547 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1548 TYPE_SIZE (variants) = TYPE_SIZE (t);
1549 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1550 variants = TYPE_NEXT_VARIANT (variants);
1553 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1554 /* For a class w/o baseclasses, `finish_struct' has set
1555 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1556 definition). Similarly for a class whose base classes do not
1557 have vtables. When neither of these is true, we might have
1558 removed abstract virtuals (by providing a definition), added
1559 some (by declaring new ones), or redeclared ones from a base
1560 class. We need to recalculate what's really an abstract virtual
1561 at this point (by looking in the vtables). */
1562 get_pure_virtuals (t);
1564 if (n_baseclasses)
1566 /* Notice whether this class has type conversion functions defined. */
1567 tree binfo = TYPE_BINFO (t);
1568 tree binfos = BINFO_BASETYPES (binfo);
1569 tree basetype;
1571 for (i = n_baseclasses-1; i >= 0; i--)
1573 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1575 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1579 /* If this type has a copy constructor or a destructor, force its mode to
1580 be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero. This
1581 will cause it to be passed by invisible reference and prevent it from
1582 being returned in a register. */
1583 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1585 tree variants;
1586 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1587 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1589 TYPE_MODE (variants) = BLKmode;
1590 TREE_ADDRESSABLE (variants) = 1;
1595 /* Issue warnings about T having private constructors, but no friends,
1596 and so forth.
1598 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1599 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1600 non-private static member functions. */
1602 static void
1603 maybe_warn_about_overly_private_class (tree t)
1605 int has_member_fn = 0;
1606 int has_nonprivate_method = 0;
1607 tree fn;
1609 if (!warn_ctor_dtor_privacy
1610 /* If the class has friends, those entities might create and
1611 access instances, so we should not warn. */
1612 || (CLASSTYPE_FRIEND_CLASSES (t)
1613 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1614 /* We will have warned when the template was declared; there's
1615 no need to warn on every instantiation. */
1616 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1617 /* There's no reason to even consider warning about this
1618 class. */
1619 return;
1621 /* We only issue one warning, if more than one applies, because
1622 otherwise, on code like:
1624 class A {
1625 // Oops - forgot `public:'
1626 A();
1627 A(const A&);
1628 ~A();
1631 we warn several times about essentially the same problem. */
1633 /* Check to see if all (non-constructor, non-destructor) member
1634 functions are private. (Since there are no friends or
1635 non-private statics, we can't ever call any of the private member
1636 functions.) */
1637 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1638 /* We're not interested in compiler-generated methods; they don't
1639 provide any way to call private members. */
1640 if (!DECL_ARTIFICIAL (fn))
1642 if (!TREE_PRIVATE (fn))
1644 if (DECL_STATIC_FUNCTION_P (fn))
1645 /* A non-private static member function is just like a
1646 friend; it can create and invoke private member
1647 functions, and be accessed without a class
1648 instance. */
1649 return;
1651 has_nonprivate_method = 1;
1652 /* Keep searching for a static member function. */
1654 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1655 has_member_fn = 1;
1658 if (!has_nonprivate_method && has_member_fn)
1660 /* There are no non-private methods, and there's at least one
1661 private member function that isn't a constructor or
1662 destructor. (If all the private members are
1663 constructors/destructors we want to use the code below that
1664 issues error messages specifically referring to
1665 constructors/destructors.) */
1666 int i;
1667 tree binfo = TYPE_BINFO (t);
1669 for (i = 0; i < BINFO_N_BASETYPES (binfo); i++)
1670 if (BINFO_BASEACCESS (binfo, i) != access_private_node)
1672 has_nonprivate_method = 1;
1673 break;
1675 if (!has_nonprivate_method)
1677 warning ("all member functions in class `%T' are private", t);
1678 return;
1682 /* Even if some of the member functions are non-private, the class
1683 won't be useful for much if all the constructors or destructors
1684 are private: such an object can never be created or destroyed. */
1685 if (TYPE_HAS_DESTRUCTOR (t))
1687 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1689 if (TREE_PRIVATE (dtor))
1691 warning ("`%#T' only defines a private destructor and has no friends",
1693 return;
1697 if (TYPE_HAS_CONSTRUCTOR (t))
1699 int nonprivate_ctor = 0;
1701 /* If a non-template class does not define a copy
1702 constructor, one is defined for it, enabling it to avoid
1703 this warning. For a template class, this does not
1704 happen, and so we would normally get a warning on:
1706 template <class T> class C { private: C(); };
1708 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1709 complete non-template or fully instantiated classes have this
1710 flag set. */
1711 if (!TYPE_HAS_INIT_REF (t))
1712 nonprivate_ctor = 1;
1713 else
1714 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1716 fn = OVL_NEXT (fn))
1718 tree ctor = OVL_CURRENT (fn);
1719 /* Ideally, we wouldn't count copy constructors (or, in
1720 fact, any constructor that takes an argument of the
1721 class type as a parameter) because such things cannot
1722 be used to construct an instance of the class unless
1723 you already have one. But, for now at least, we're
1724 more generous. */
1725 if (! TREE_PRIVATE (ctor))
1727 nonprivate_ctor = 1;
1728 break;
1732 if (nonprivate_ctor == 0)
1734 warning ("`%#T' only defines private constructors and has no friends",
1736 return;
1741 static struct {
1742 gt_pointer_operator new_value;
1743 void *cookie;
1744 } resort_data;
1746 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1748 static int
1749 method_name_cmp (const void* m1_p, const void* m2_p)
1751 const tree *const m1 = m1_p;
1752 const tree *const m2 = m2_p;
1754 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1755 return 0;
1756 if (*m1 == NULL_TREE)
1757 return -1;
1758 if (*m2 == NULL_TREE)
1759 return 1;
1760 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1761 return -1;
1762 return 1;
1765 /* This routine compares two fields like method_name_cmp but using the
1766 pointer operator in resort_field_decl_data. */
1768 static int
1769 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1771 const tree *const m1 = m1_p;
1772 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;
1780 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1781 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1782 resort_data.new_value (&d1, resort_data.cookie);
1783 resort_data.new_value (&d2, resort_data.cookie);
1784 if (d1 < d2)
1785 return -1;
1787 return 1;
1790 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1792 void
1793 resort_type_method_vec (void* obj,
1794 void* orig_obj ATTRIBUTE_UNUSED ,
1795 gt_pointer_operator new_value,
1796 void* cookie)
1798 tree method_vec = obj;
1799 int len = TREE_VEC_LENGTH (method_vec);
1800 int slot;
1802 /* The type conversion ops have to live at the front of the vec, so we
1803 can't sort them. */
1804 for (slot = 2; slot < len; ++slot)
1806 tree fn = TREE_VEC_ELT (method_vec, slot);
1808 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1809 break;
1811 if (len - slot > 1)
1813 resort_data.new_value = new_value;
1814 resort_data.cookie = cookie;
1815 qsort (&TREE_VEC_ELT (method_vec, slot), len - slot, sizeof (tree),
1816 resort_method_name_cmp);
1820 /* Warn about duplicate methods in fn_fields. Also compact method
1821 lists so that lookup can be made faster.
1823 Data Structure: List of method lists. The outer list is a
1824 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1825 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1826 links the entire list of methods for TYPE_METHODS. Friends are
1827 chained in the same way as member functions (? TREE_CHAIN or
1828 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1829 list. That allows them to be quickly deleted, and requires no
1830 extra storage.
1832 Sort methods that are not special (i.e., constructors, destructors,
1833 and type conversion operators) so that we can find them faster in
1834 search. */
1836 static void
1837 finish_struct_methods (tree t)
1839 tree fn_fields;
1840 tree method_vec;
1841 int slot, len;
1843 if (!TYPE_METHODS (t))
1845 /* Clear these for safety; perhaps some parsing error could set
1846 these incorrectly. */
1847 TYPE_HAS_CONSTRUCTOR (t) = 0;
1848 TYPE_HAS_DESTRUCTOR (t) = 0;
1849 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
1850 return;
1853 method_vec = CLASSTYPE_METHOD_VEC (t);
1854 my_friendly_assert (method_vec != NULL_TREE, 19991215);
1855 len = TREE_VEC_LENGTH (method_vec);
1857 /* First fill in entry 0 with the constructors, entry 1 with destructors,
1858 and the next few with type conversion operators (if any). */
1859 for (fn_fields = TYPE_METHODS (t); fn_fields;
1860 fn_fields = TREE_CHAIN (fn_fields))
1861 /* Clear out this flag. */
1862 DECL_IN_AGGR_P (fn_fields) = 0;
1864 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
1865 /* We thought there was a destructor, but there wasn't. Some
1866 parse errors cause this anomalous situation. */
1867 TYPE_HAS_DESTRUCTOR (t) = 0;
1869 /* Issue warnings about private constructors and such. If there are
1870 no methods, then some public defaults are generated. */
1871 maybe_warn_about_overly_private_class (t);
1873 /* Now sort the methods. */
1874 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
1875 len--;
1876 TREE_VEC_LENGTH (method_vec) = len;
1878 /* The type conversion ops have to live at the front of the vec, so we
1879 can't sort them. */
1880 for (slot = 2; slot < len; ++slot)
1882 tree fn = TREE_VEC_ELT (method_vec, slot);
1884 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1885 break;
1887 if (len - slot > 1)
1888 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
1889 method_name_cmp);
1892 /* Make BINFO's vtable have N entries, including RTTI entries,
1893 vbase and vcall offsets, etc. Set its type and call the backend
1894 to lay it out. */
1896 static void
1897 layout_vtable_decl (tree binfo, int n)
1899 tree atype;
1900 tree vtable;
1902 atype = build_cplus_array_type (vtable_entry_type,
1903 build_index_type (size_int (n - 1)));
1904 layout_type (atype);
1906 /* We may have to grow the vtable. */
1907 vtable = get_vtbl_decl_for_binfo (binfo);
1908 if (!same_type_p (TREE_TYPE (vtable), atype))
1910 TREE_TYPE (vtable) = atype;
1911 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
1912 layout_decl (vtable, 0);
1916 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
1917 have the same signature. */
1920 same_signature_p (tree fndecl, tree base_fndecl)
1922 /* One destructor overrides another if they are the same kind of
1923 destructor. */
1924 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
1925 && special_function_p (base_fndecl) == special_function_p (fndecl))
1926 return 1;
1927 /* But a non-destructor never overrides a destructor, nor vice
1928 versa, nor do different kinds of destructors override
1929 one-another. For example, a complete object destructor does not
1930 override a deleting destructor. */
1931 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
1932 return 0;
1934 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
1936 tree types, base_types;
1937 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1938 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
1939 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
1940 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
1941 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
1942 return 1;
1944 return 0;
1947 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
1948 subobject. */
1950 static bool
1951 base_derived_from (tree derived, tree base)
1953 tree probe;
1955 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1957 if (probe == derived)
1958 return true;
1959 else if (TREE_VIA_VIRTUAL (probe))
1960 /* If we meet a virtual base, we can't follow the inheritance
1961 any more. See if the complete type of DERIVED contains
1962 such a virtual base. */
1963 return purpose_member (BINFO_TYPE (probe),
1964 CLASSTYPE_VBASECLASSES (BINFO_TYPE (derived)))
1965 != NULL_TREE;
1967 return false;
1970 typedef struct find_final_overrider_data_s {
1971 /* The function for which we are trying to find a final overrider. */
1972 tree fn;
1973 /* The base class in which the function was declared. */
1974 tree declaring_base;
1975 /* The most derived class in the hierarchy. */
1976 tree most_derived_type;
1977 /* The candidate overriders. */
1978 tree candidates;
1979 /* Binfos which inherited virtually on the currrent path. */
1980 tree vpath;
1981 } find_final_overrider_data;
1983 /* Called from find_final_overrider via dfs_walk. */
1985 static tree
1986 dfs_find_final_overrider (tree binfo, void* data)
1988 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
1990 if (binfo == ffod->declaring_base)
1992 /* We've found a path to the declaring base. Walk the path from
1993 derived to base, looking for an overrider for FN. */
1994 tree path, probe, vpath;
1996 /* Build the path, using the inheritance chain and record of
1997 virtual inheritance. */
1998 for (path = NULL_TREE, probe = binfo, vpath = ffod->vpath;;)
2000 path = tree_cons (NULL_TREE, probe, path);
2001 if (same_type_p (BINFO_TYPE (probe), ffod->most_derived_type))
2002 break;
2003 if (TREE_VIA_VIRTUAL (probe))
2005 probe = TREE_VALUE (vpath);
2006 vpath = TREE_CHAIN (vpath);
2008 else
2009 probe = BINFO_INHERITANCE_CHAIN (probe);
2011 /* Now walk path, looking for overrides. */
2012 for (; path; path = TREE_CHAIN (path))
2014 tree method = look_for_overrides_here
2015 (BINFO_TYPE (TREE_VALUE (path)), ffod->fn);
2017 if (method)
2019 tree *candidate = &ffod->candidates;
2020 path = TREE_VALUE (path);
2022 /* Remove any candidates overridden by this new function. */
2023 while (*candidate)
2025 /* If *CANDIDATE overrides METHOD, then METHOD
2026 cannot override anything else on the list. */
2027 if (base_derived_from (TREE_VALUE (*candidate), path))
2028 return NULL_TREE;
2029 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2030 if (base_derived_from (path, TREE_VALUE (*candidate)))
2031 *candidate = TREE_CHAIN (*candidate);
2032 else
2033 candidate = &TREE_CHAIN (*candidate);
2036 /* Add the new function. */
2037 ffod->candidates = tree_cons (method, path, ffod->candidates);
2038 break;
2043 return NULL_TREE;
2046 static tree
2047 dfs_find_final_overrider_q (tree derived, int ix, void *data)
2049 tree binfo = BINFO_BASETYPE (derived, ix);
2050 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2052 if (TREE_VIA_VIRTUAL (binfo))
2053 ffod->vpath = tree_cons (NULL_TREE, derived, ffod->vpath);
2055 return binfo;
2058 static tree
2059 dfs_find_final_overrider_post (tree binfo, void *data)
2061 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2063 if (TREE_VIA_VIRTUAL (binfo) && TREE_CHAIN (ffod->vpath))
2064 ffod->vpath = TREE_CHAIN (ffod->vpath);
2066 return NULL_TREE;
2069 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2070 FN and whose TREE_VALUE is the binfo for the base where the
2071 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2072 DERIVED) is the base object in which FN is declared. */
2074 static tree
2075 find_final_overrider (tree derived, tree binfo, tree fn)
2077 find_final_overrider_data ffod;
2079 /* Getting this right is a little tricky. This is valid:
2081 struct S { virtual void f (); };
2082 struct T { virtual void f (); };
2083 struct U : public S, public T { };
2085 even though calling `f' in `U' is ambiguous. But,
2087 struct R { virtual void f(); };
2088 struct S : virtual public R { virtual void f (); };
2089 struct T : virtual public R { virtual void f (); };
2090 struct U : public S, public T { };
2092 is not -- there's no way to decide whether to put `S::f' or
2093 `T::f' in the vtable for `R'.
2095 The solution is to look at all paths to BINFO. If we find
2096 different overriders along any two, then there is a problem. */
2097 if (DECL_THUNK_P (fn))
2098 fn = THUNK_TARGET (fn);
2100 ffod.fn = fn;
2101 ffod.declaring_base = binfo;
2102 ffod.most_derived_type = BINFO_TYPE (derived);
2103 ffod.candidates = NULL_TREE;
2104 ffod.vpath = NULL_TREE;
2106 dfs_walk_real (derived,
2107 dfs_find_final_overrider,
2108 dfs_find_final_overrider_post,
2109 dfs_find_final_overrider_q,
2110 &ffod);
2112 /* If there was no winner, issue an error message. */
2113 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2115 error ("no unique final overrider for `%D' in `%T'", fn,
2116 BINFO_TYPE (derived));
2117 return error_mark_node;
2120 return ffod.candidates;
2123 /* Return the index of the vcall offset for FN when TYPE is used as a
2124 virtual base. */
2126 static tree
2127 get_vcall_index (tree fn, tree type)
2129 tree v;
2131 for (v = CLASSTYPE_VCALL_INDICES (type); v; v = TREE_CHAIN (v))
2132 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (TREE_PURPOSE (v)))
2133 || same_signature_p (fn, TREE_PURPOSE (v)))
2134 break;
2136 /* There should always be an appropriate index. */
2137 my_friendly_assert (v, 20021103);
2139 return TREE_VALUE (v);
2142 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2143 dominated by T. FN has been overridden in BINFO; VIRTUALS points to the
2144 corresponding position in the BINFO_VIRTUALS list. */
2146 static void
2147 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2148 unsigned ix)
2150 tree b;
2151 tree overrider;
2152 tree delta;
2153 tree virtual_base;
2154 tree first_defn;
2155 tree overrider_fn, overrider_target;
2156 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2157 tree over_return, base_return;
2158 bool lost = false;
2160 /* Find the nearest primary base (possibly binfo itself) which defines
2161 this function; this is the class the caller will convert to when
2162 calling FN through BINFO. */
2163 for (b = binfo; ; b = get_primary_binfo (b))
2165 my_friendly_assert (b, 20021227);
2166 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2167 break;
2169 /* The nearest definition is from a lost primary. */
2170 if (BINFO_LOST_PRIMARY_P (b))
2171 lost = true;
2173 first_defn = b;
2175 /* Find the final overrider. */
2176 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2177 if (overrider == error_mark_node)
2178 return;
2179 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2181 /* Check for adjusting covariant return types. */
2182 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2183 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2185 if (POINTER_TYPE_P (over_return)
2186 && TREE_CODE (over_return) == TREE_CODE (base_return)
2187 && CLASS_TYPE_P (TREE_TYPE (over_return))
2188 && CLASS_TYPE_P (TREE_TYPE (base_return)))
2190 /* If FN is a covariant thunk, we must figure out the adjustment
2191 to the final base FN was converting to. As OVERRIDER_TARGET might
2192 also be converting to the return type of FN, we have to
2193 combine the two conversions here. */
2194 tree fixed_offset, virtual_offset;
2196 if (DECL_THUNK_P (fn))
2198 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2199 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2201 else
2202 fixed_offset = virtual_offset = NULL_TREE;
2204 if (!virtual_offset)
2206 /* There was no existing virtual thunk (which takes
2207 precidence). */
2208 tree thunk_binfo;
2209 base_kind kind;
2211 thunk_binfo = lookup_base (TREE_TYPE (over_return),
2212 TREE_TYPE (base_return),
2213 ba_check | ba_quiet, &kind);
2215 if (thunk_binfo && (kind == bk_via_virtual
2216 || !BINFO_OFFSET_ZEROP (thunk_binfo)))
2218 tree offset = BINFO_OFFSET (thunk_binfo);
2220 if (kind == bk_via_virtual)
2222 /* We convert via virtual base. Find the virtual
2223 base and adjust the fixed offset to be from there. */
2224 while (!TREE_VIA_VIRTUAL (thunk_binfo))
2225 thunk_binfo = BINFO_INHERITANCE_CHAIN (thunk_binfo);
2227 virtual_offset = thunk_binfo;
2228 offset = size_binop (MINUS_EXPR, offset,
2229 BINFO_OFFSET (virtual_offset));
2231 if (fixed_offset)
2232 /* There was an existing fixed offset, this must be
2233 from the base just converted to, and the base the
2234 FN was thunking to. */
2235 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2236 else
2237 fixed_offset = offset;
2241 if (fixed_offset || virtual_offset)
2242 /* Replace the overriding function with a covariant thunk. We
2243 will emit the overriding function in its own slot as
2244 well. */
2245 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2246 fixed_offset, virtual_offset);
2248 else
2249 my_friendly_assert (!DECL_THUNK_P (fn), 20021231);
2251 /* Assume that we will produce a thunk that convert all the way to
2252 the final overrider, and not to an intermediate virtual base. */
2253 virtual_base = NULL_TREE;
2255 /* See if we can convert to an intermediate virtual base first, and then
2256 use the vcall offset located there to finish the conversion. */
2257 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2259 /* If we find the final overrider, then we can stop
2260 walking. */
2261 if (same_type_p (BINFO_TYPE (b),
2262 BINFO_TYPE (TREE_VALUE (overrider))))
2263 break;
2265 /* If we find a virtual base, and we haven't yet found the
2266 overrider, then there is a virtual base between the
2267 declaring base (first_defn) and the final overrider. */
2268 if (TREE_VIA_VIRTUAL (b))
2270 virtual_base = b;
2271 break;
2275 if (overrider_fn != overrider_target && !virtual_base)
2277 /* The ABI specifies that a covariant thunk includes a mangling
2278 for a this pointer adjustment. This-adjusting thunks that
2279 override a function from a virtual base have a vcall
2280 adjustment. When the virtual base in question is a primary
2281 virtual base, we know the adjustments are zero, (and in the
2282 non-covariant case, we would not use the thunk).
2283 Unfortunately we didn't notice this could happen, when
2284 designing the ABI and so never mandated that such a covariant
2285 thunk should be emitted. Because we must use the ABI mandated
2286 name, we must continue searching from the binfo where we
2287 found the most recent definition of the function, towards the
2288 primary binfo which first introduced the function into the
2289 vtable. If that enters a virtual base, we must use a vcall
2290 this-adjusting thunk. Bleah! */
2291 tree probe;
2293 for (probe = first_defn; (probe = get_primary_binfo (probe));)
2295 if (TREE_VIA_VIRTUAL (probe))
2296 virtual_base = probe;
2297 if ((unsigned) list_length (BINFO_VIRTUALS (probe)) <= ix)
2298 break;
2300 if (virtual_base)
2301 /* Even if we find a virtual base, the correct delta is
2302 between the overrider and the binfo we're building a vtable
2303 for. */
2304 goto virtual_covariant;
2307 /* Compute the constant adjustment to the `this' pointer. The
2308 `this' pointer, when this function is called, will point at BINFO
2309 (or one of its primary bases, which are at the same offset). */
2310 if (virtual_base)
2311 /* The `this' pointer needs to be adjusted from the declaration to
2312 the nearest virtual base. */
2313 delta = size_diffop (BINFO_OFFSET (virtual_base),
2314 BINFO_OFFSET (first_defn));
2315 else if (lost)
2316 /* If the nearest definition is in a lost primary, we don't need an
2317 entry in our vtable. Except possibly in a constructor vtable,
2318 if we happen to get our primary back. In that case, the offset
2319 will be zero, as it will be a primary base. */
2320 delta = size_zero_node;
2321 else
2322 /* The `this' pointer needs to be adjusted from pointing to
2323 BINFO to pointing at the base where the final overrider
2324 appears. */
2325 virtual_covariant:
2326 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2327 BINFO_OFFSET (binfo));
2329 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2331 if (virtual_base)
2332 BV_VCALL_INDEX (*virtuals)
2333 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2336 /* Called from modify_all_vtables via dfs_walk. */
2338 static tree
2339 dfs_modify_vtables (tree binfo, void* data)
2341 if (/* There's no need to modify the vtable for a non-virtual
2342 primary base; we're not going to use that vtable anyhow.
2343 We do still need to do this for virtual primary bases, as they
2344 could become non-primary in a construction vtable. */
2345 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2346 /* Similarly, a base without a vtable needs no modification. */
2347 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2349 tree t = (tree) data;
2350 tree virtuals;
2351 tree old_virtuals;
2352 unsigned ix;
2354 make_new_vtable (t, binfo);
2356 /* Now, go through each of the virtual functions in the virtual
2357 function table for BINFO. Find the final overrider, and
2358 update the BINFO_VIRTUALS list appropriately. */
2359 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2360 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2361 virtuals;
2362 ix++, virtuals = TREE_CHAIN (virtuals),
2363 old_virtuals = TREE_CHAIN (old_virtuals))
2364 update_vtable_entry_for_fn (t,
2365 binfo,
2366 BV_FN (old_virtuals),
2367 &virtuals, ix);
2370 BINFO_MARKED (binfo) = 1;
2372 return NULL_TREE;
2375 /* Update all of the primary and secondary vtables for T. Create new
2376 vtables as required, and initialize their RTTI information. Each
2377 of the functions in VIRTUALS is declared in T and may override a
2378 virtual function from a base class; find and modify the appropriate
2379 entries to point to the overriding functions. Returns a list, in
2380 declaration order, of the virtual functions that are declared in T,
2381 but do not appear in the primary base class vtable, and which
2382 should therefore be appended to the end of the vtable for T. */
2384 static tree
2385 modify_all_vtables (tree t, tree virtuals)
2387 tree binfo = TYPE_BINFO (t);
2388 tree *fnsp;
2390 /* Update all of the vtables. */
2391 dfs_walk (binfo, dfs_modify_vtables, unmarkedp, t);
2392 dfs_walk (binfo, dfs_unmark, markedp, t);
2394 /* Add virtual functions not already in our primary vtable. These
2395 will be both those introduced by this class, and those overridden
2396 from secondary bases. It does not include virtuals merely
2397 inherited from secondary bases. */
2398 for (fnsp = &virtuals; *fnsp; )
2400 tree fn = TREE_VALUE (*fnsp);
2402 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2403 || DECL_VINDEX (fn) == error_mark_node)
2405 /* We don't need to adjust the `this' pointer when
2406 calling this function. */
2407 BV_DELTA (*fnsp) = integer_zero_node;
2408 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2410 /* This is a function not already in our vtable. Keep it. */
2411 fnsp = &TREE_CHAIN (*fnsp);
2413 else
2414 /* We've already got an entry for this function. Skip it. */
2415 *fnsp = TREE_CHAIN (*fnsp);
2418 return virtuals;
2421 /* Get the base virtual function declarations in T that have the
2422 indicated NAME. */
2424 static tree
2425 get_basefndecls (tree name, tree t)
2427 tree methods;
2428 tree base_fndecls = NULL_TREE;
2429 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2430 int i;
2432 /* Find virtual functions in T with the indicated NAME. */
2433 i = lookup_fnfields_1 (t, name);
2434 if (i != -1)
2435 for (methods = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), i);
2436 methods;
2437 methods = OVL_NEXT (methods))
2439 tree method = OVL_CURRENT (methods);
2441 if (TREE_CODE (method) == FUNCTION_DECL
2442 && DECL_VINDEX (method))
2443 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2446 if (base_fndecls)
2447 return base_fndecls;
2449 for (i = 0; i < n_baseclasses; i++)
2451 tree basetype = TYPE_BINFO_BASETYPE (t, i);
2452 base_fndecls = chainon (get_basefndecls (name, basetype),
2453 base_fndecls);
2456 return base_fndecls;
2459 /* If this declaration supersedes the declaration of
2460 a method declared virtual in the base class, then
2461 mark this field as being virtual as well. */
2463 static void
2464 check_for_override (tree decl, tree ctype)
2466 if (TREE_CODE (decl) == TEMPLATE_DECL)
2467 /* In [temp.mem] we have:
2469 A specialization of a member function template does not
2470 override a virtual function from a base class. */
2471 return;
2472 if ((DECL_DESTRUCTOR_P (decl)
2473 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2474 && look_for_overrides (ctype, decl)
2475 && !DECL_STATIC_FUNCTION_P (decl))
2476 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2477 the error_mark_node so that we know it is an overriding
2478 function. */
2479 DECL_VINDEX (decl) = decl;
2481 if (DECL_VIRTUAL_P (decl))
2483 if (!DECL_VINDEX (decl))
2484 DECL_VINDEX (decl) = error_mark_node;
2485 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2489 /* Warn about hidden virtual functions that are not overridden in t.
2490 We know that constructors and destructors don't apply. */
2492 void
2493 warn_hidden (tree t)
2495 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2496 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2497 int i;
2499 /* We go through each separately named virtual function. */
2500 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2502 tree fns;
2503 tree name;
2504 tree fndecl;
2505 tree base_fndecls;
2506 int j;
2508 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2509 have the same name. Figure out what name that is. */
2510 name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2511 /* There are no possibly hidden functions yet. */
2512 base_fndecls = NULL_TREE;
2513 /* Iterate through all of the base classes looking for possibly
2514 hidden functions. */
2515 for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
2517 tree basetype = TYPE_BINFO_BASETYPE (t, j);
2518 base_fndecls = chainon (get_basefndecls (name, basetype),
2519 base_fndecls);
2522 /* If there are no functions to hide, continue. */
2523 if (!base_fndecls)
2524 continue;
2526 /* Remove any overridden functions. */
2527 for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
2529 fndecl = OVL_CURRENT (fns);
2530 if (DECL_VINDEX (fndecl))
2532 tree *prev = &base_fndecls;
2534 while (*prev)
2535 /* If the method from the base class has the same
2536 signature as the method from the derived class, it
2537 has been overridden. */
2538 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2539 *prev = TREE_CHAIN (*prev);
2540 else
2541 prev = &TREE_CHAIN (*prev);
2545 /* Now give a warning for all base functions without overriders,
2546 as they are hidden. */
2547 while (base_fndecls)
2549 /* Here we know it is a hider, and no overrider exists. */
2550 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2551 cp_warning_at (" by `%D'",
2552 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2553 base_fndecls = TREE_CHAIN (base_fndecls);
2558 /* Check for things that are invalid. There are probably plenty of other
2559 things we should check for also. */
2561 static void
2562 finish_struct_anon (tree t)
2564 tree field;
2566 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2568 if (TREE_STATIC (field))
2569 continue;
2570 if (TREE_CODE (field) != FIELD_DECL)
2571 continue;
2573 if (DECL_NAME (field) == NULL_TREE
2574 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2576 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2577 for (; elt; elt = TREE_CHAIN (elt))
2579 /* We're generally only interested in entities the user
2580 declared, but we also find nested classes by noticing
2581 the TYPE_DECL that we create implicitly. You're
2582 allowed to put one anonymous union inside another,
2583 though, so we explicitly tolerate that. We use
2584 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2585 we also allow unnamed types used for defining fields. */
2586 if (DECL_ARTIFICIAL (elt)
2587 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2588 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2589 continue;
2591 if (constructor_name_p (DECL_NAME (elt), t))
2592 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2593 elt);
2595 if (TREE_CODE (elt) != FIELD_DECL)
2597 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2598 elt);
2599 continue;
2602 if (TREE_PRIVATE (elt))
2603 cp_pedwarn_at ("private member `%#D' in anonymous union",
2604 elt);
2605 else if (TREE_PROTECTED (elt))
2606 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2607 elt);
2609 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2610 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2616 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2617 will be used later during class template instantiation.
2618 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2619 a non-static member data (FIELD_DECL), a member function
2620 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2621 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2622 When FRIEND_P is nonzero, T is either a friend class
2623 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2624 (FUNCTION_DECL, TEMPLATE_DECL). */
2626 void
2627 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2629 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2630 if (CLASSTYPE_TEMPLATE_INFO (type))
2631 CLASSTYPE_DECL_LIST (type)
2632 = tree_cons (friend_p ? NULL_TREE : type,
2633 t, CLASSTYPE_DECL_LIST (type));
2636 /* Create default constructors, assignment operators, and so forth for
2637 the type indicated by T, if they are needed.
2638 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2639 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2640 class cannot have a default constructor, copy constructor taking a
2641 const reference argument, or an assignment operator taking a const
2642 reference, respectively. If a virtual destructor is created, its
2643 DECL is returned; otherwise the return value is NULL_TREE. */
2645 static void
2646 add_implicitly_declared_members (tree t,
2647 int cant_have_default_ctor,
2648 int cant_have_const_cctor,
2649 int cant_have_const_assignment)
2651 tree default_fn;
2652 tree implicit_fns = NULL_TREE;
2653 tree virtual_dtor = NULL_TREE;
2654 tree *f;
2656 ++adding_implicit_members;
2658 /* Destructor. */
2659 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2661 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2662 check_for_override (default_fn, t);
2664 /* If we couldn't make it work, then pretend we didn't need it. */
2665 if (default_fn == void_type_node)
2666 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2667 else
2669 TREE_CHAIN (default_fn) = implicit_fns;
2670 implicit_fns = default_fn;
2672 if (DECL_VINDEX (default_fn))
2673 virtual_dtor = default_fn;
2676 else
2677 /* Any non-implicit destructor is non-trivial. */
2678 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2680 /* Default constructor. */
2681 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2683 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2684 TREE_CHAIN (default_fn) = implicit_fns;
2685 implicit_fns = default_fn;
2688 /* Copy constructor. */
2689 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2691 /* ARM 12.18: You get either X(X&) or X(const X&), but
2692 not both. --Chip */
2693 default_fn
2694 = implicitly_declare_fn (sfk_copy_constructor, t,
2695 /*const_p=*/!cant_have_const_cctor);
2696 TREE_CHAIN (default_fn) = implicit_fns;
2697 implicit_fns = default_fn;
2700 /* Assignment operator. */
2701 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2703 default_fn
2704 = implicitly_declare_fn (sfk_assignment_operator, t,
2705 /*const_p=*/!cant_have_const_assignment);
2706 TREE_CHAIN (default_fn) = implicit_fns;
2707 implicit_fns = default_fn;
2710 /* Now, hook all of the new functions on to TYPE_METHODS,
2711 and add them to the CLASSTYPE_METHOD_VEC. */
2712 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2714 add_method (t, *f, /*error_p=*/0);
2715 maybe_add_class_template_decl_list (current_class_type, *f, /*friend_p=*/0);
2717 if (abi_version_at_least (2))
2718 /* G++ 3.2 put the implicit destructor at the *beginning* of the
2719 list, which cause the destructor to be emitted in an incorrect
2720 location in the vtable. */
2721 TYPE_METHODS (t) = chainon (TYPE_METHODS (t), implicit_fns);
2722 else
2724 if (warn_abi && virtual_dtor)
2725 warning ("vtable layout for class `%T' may not be ABI-compliant "
2726 "and may change in a future version of GCC due to implicit "
2727 "virtual destructor",
2729 *f = TYPE_METHODS (t);
2730 TYPE_METHODS (t) = implicit_fns;
2733 --adding_implicit_members;
2736 /* Subroutine of finish_struct_1. Recursively count the number of fields
2737 in TYPE, including anonymous union members. */
2739 static int
2740 count_fields (tree fields)
2742 tree x;
2743 int n_fields = 0;
2744 for (x = fields; x; x = TREE_CHAIN (x))
2746 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2747 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2748 else
2749 n_fields += 1;
2751 return n_fields;
2754 /* Subroutine of finish_struct_1. Recursively add all the fields in the
2755 TREE_LIST FIELDS to the SORTED_FIELDS_TYPE elts, starting at offset IDX. */
2757 static int
2758 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
2760 tree x;
2761 for (x = fields; x; x = TREE_CHAIN (x))
2763 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2764 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2765 else
2766 field_vec->elts[idx++] = x;
2768 return idx;
2771 /* FIELD is a bit-field. We are finishing the processing for its
2772 enclosing type. Issue any appropriate messages and set appropriate
2773 flags. */
2775 static void
2776 check_bitfield_decl (tree field)
2778 tree type = TREE_TYPE (field);
2779 tree w = NULL_TREE;
2781 /* Detect invalid bit-field type. */
2782 if (DECL_INITIAL (field)
2783 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
2785 cp_error_at ("bit-field `%#D' with non-integral type", field);
2786 w = error_mark_node;
2789 /* Detect and ignore out of range field width. */
2790 if (DECL_INITIAL (field))
2792 w = DECL_INITIAL (field);
2794 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2795 STRIP_NOPS (w);
2797 /* detect invalid field size. */
2798 if (TREE_CODE (w) == CONST_DECL)
2799 w = DECL_INITIAL (w);
2800 else
2801 w = decl_constant_value (w);
2803 if (TREE_CODE (w) != INTEGER_CST)
2805 cp_error_at ("bit-field `%D' width not an integer constant",
2806 field);
2807 w = error_mark_node;
2809 else if (tree_int_cst_sgn (w) < 0)
2811 cp_error_at ("negative width in bit-field `%D'", field);
2812 w = error_mark_node;
2814 else if (integer_zerop (w) && DECL_NAME (field) != 0)
2816 cp_error_at ("zero width for bit-field `%D'", field);
2817 w = error_mark_node;
2819 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
2820 && TREE_CODE (type) != ENUMERAL_TYPE
2821 && TREE_CODE (type) != BOOLEAN_TYPE)
2822 cp_warning_at ("width of `%D' exceeds its type", field);
2823 else if (TREE_CODE (type) == ENUMERAL_TYPE
2824 && (0 > compare_tree_int (w,
2825 min_precision (TYPE_MIN_VALUE (type),
2826 TREE_UNSIGNED (type)))
2827 || 0 > compare_tree_int (w,
2828 min_precision
2829 (TYPE_MAX_VALUE (type),
2830 TREE_UNSIGNED (type)))))
2831 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
2832 field, type);
2835 /* Remove the bit-field width indicator so that the rest of the
2836 compiler does not treat that value as an initializer. */
2837 DECL_INITIAL (field) = NULL_TREE;
2839 if (w != error_mark_node)
2841 DECL_SIZE (field) = convert (bitsizetype, w);
2842 DECL_BIT_FIELD (field) = 1;
2844 else
2846 /* Non-bit-fields are aligned for their type. */
2847 DECL_BIT_FIELD (field) = 0;
2848 CLEAR_DECL_C_BIT_FIELD (field);
2852 /* FIELD is a non bit-field. We are finishing the processing for its
2853 enclosing type T. Issue any appropriate messages and set appropriate
2854 flags. */
2856 static void
2857 check_field_decl (tree field,
2858 tree t,
2859 int* cant_have_const_ctor,
2860 int* cant_have_default_ctor,
2861 int* no_const_asn_ref,
2862 int* any_default_members)
2864 tree type = strip_array_types (TREE_TYPE (field));
2866 /* An anonymous union cannot contain any fields which would change
2867 the settings of CANT_HAVE_CONST_CTOR and friends. */
2868 if (ANON_UNION_TYPE_P (type))
2870 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
2871 structs. So, we recurse through their fields here. */
2872 else if (ANON_AGGR_TYPE_P (type))
2874 tree fields;
2876 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2877 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
2878 check_field_decl (fields, t, cant_have_const_ctor,
2879 cant_have_default_ctor, no_const_asn_ref,
2880 any_default_members);
2882 /* Check members with class type for constructors, destructors,
2883 etc. */
2884 else if (CLASS_TYPE_P (type))
2886 /* Never let anything with uninheritable virtuals
2887 make it through without complaint. */
2888 abstract_virtuals_error (field, type);
2890 if (TREE_CODE (t) == UNION_TYPE)
2892 if (TYPE_NEEDS_CONSTRUCTING (type))
2893 cp_error_at ("member `%#D' with constructor not allowed in union",
2894 field);
2895 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2896 cp_error_at ("member `%#D' with destructor not allowed in union",
2897 field);
2898 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
2899 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
2900 field);
2902 else
2904 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
2905 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2906 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2907 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
2908 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
2911 if (!TYPE_HAS_CONST_INIT_REF (type))
2912 *cant_have_const_ctor = 1;
2914 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
2915 *no_const_asn_ref = 1;
2917 if (TYPE_HAS_CONSTRUCTOR (type)
2918 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2919 *cant_have_default_ctor = 1;
2921 if (DECL_INITIAL (field) != NULL_TREE)
2923 /* `build_class_init_list' does not recognize
2924 non-FIELD_DECLs. */
2925 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
2926 error ("multiple fields in union `%T' initialized", t);
2927 *any_default_members = 1;
2931 /* Check the data members (both static and non-static), class-scoped
2932 typedefs, etc., appearing in the declaration of T. Issue
2933 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
2934 declaration order) of access declarations; each TREE_VALUE in this
2935 list is a USING_DECL.
2937 In addition, set the following flags:
2939 EMPTY_P
2940 The class is empty, i.e., contains no non-static data members.
2942 CANT_HAVE_DEFAULT_CTOR_P
2943 This class cannot have an implicitly generated default
2944 constructor.
2946 CANT_HAVE_CONST_CTOR_P
2947 This class cannot have an implicitly generated copy constructor
2948 taking a const reference.
2950 CANT_HAVE_CONST_ASN_REF
2951 This class cannot have an implicitly generated assignment
2952 operator taking a const reference.
2954 All of these flags should be initialized before calling this
2955 function.
2957 Returns a pointer to the end of the TYPE_FIELDs chain; additional
2958 fields can be added by adding to this chain. */
2960 static void
2961 check_field_decls (tree t, tree *access_decls,
2962 int *cant_have_default_ctor_p,
2963 int *cant_have_const_ctor_p,
2964 int *no_const_asn_ref_p)
2966 tree *field;
2967 tree *next;
2968 int has_pointers;
2969 int any_default_members;
2971 /* First, delete any duplicate fields. */
2972 delete_duplicate_fields (TYPE_FIELDS (t));
2974 /* Assume there are no access declarations. */
2975 *access_decls = NULL_TREE;
2976 /* Assume this class has no pointer members. */
2977 has_pointers = 0;
2978 /* Assume none of the members of this class have default
2979 initializations. */
2980 any_default_members = 0;
2982 for (field = &TYPE_FIELDS (t); *field; field = next)
2984 tree x = *field;
2985 tree type = TREE_TYPE (x);
2987 next = &TREE_CHAIN (x);
2989 if (TREE_CODE (x) == FIELD_DECL)
2991 if (TYPE_PACKED (t))
2993 if (!pod_type_p (TREE_TYPE (x)) && !TYPE_PACKED (TREE_TYPE (x)))
2994 cp_warning_at
2995 ("ignoring packed attribute on unpacked non-POD field `%#D'",
2997 else
2998 DECL_PACKED (x) = 1;
3001 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3002 /* We don't treat zero-width bitfields as making a class
3003 non-empty. */
3005 else
3007 tree element_type;
3009 /* The class is non-empty. */
3010 CLASSTYPE_EMPTY_P (t) = 0;
3011 /* The class is not even nearly empty. */
3012 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3013 /* If one of the data members contains an empty class,
3014 so does T. */
3015 element_type = strip_array_types (type);
3016 if (CLASS_TYPE_P (element_type)
3017 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3018 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3022 if (TREE_CODE (x) == USING_DECL)
3024 /* Prune the access declaration from the list of fields. */
3025 *field = TREE_CHAIN (x);
3027 /* Save the access declarations for our caller. */
3028 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3030 /* Since we've reset *FIELD there's no reason to skip to the
3031 next field. */
3032 next = field;
3033 continue;
3036 if (TREE_CODE (x) == TYPE_DECL
3037 || TREE_CODE (x) == TEMPLATE_DECL)
3038 continue;
3040 /* If we've gotten this far, it's a data member, possibly static,
3041 or an enumerator. */
3043 DECL_CONTEXT (x) = t;
3045 /* ``A local class cannot have static data members.'' ARM 9.4 */
3046 if (current_function_decl && TREE_STATIC (x))
3047 cp_error_at ("field `%D' in local class cannot be static", x);
3049 /* Perform error checking that did not get done in
3050 grokdeclarator. */
3051 if (TREE_CODE (type) == FUNCTION_TYPE)
3053 cp_error_at ("field `%D' invalidly declared function type",
3055 type = build_pointer_type (type);
3056 TREE_TYPE (x) = type;
3058 else if (TREE_CODE (type) == METHOD_TYPE)
3060 cp_error_at ("field `%D' invalidly declared method type", x);
3061 type = build_pointer_type (type);
3062 TREE_TYPE (x) = type;
3065 if (type == error_mark_node)
3066 continue;
3068 /* When this goes into scope, it will be a non-local reference. */
3069 DECL_NONLOCAL (x) = 1;
3071 if (TREE_CODE (x) == CONST_DECL)
3072 continue;
3074 if (TREE_CODE (x) == VAR_DECL)
3076 if (TREE_CODE (t) == UNION_TYPE)
3077 /* Unions cannot have static members. */
3078 cp_error_at ("field `%D' declared static in union", x);
3080 continue;
3083 /* Now it can only be a FIELD_DECL. */
3085 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3086 CLASSTYPE_NON_AGGREGATE (t) = 1;
3088 /* If this is of reference type, check if it needs an init.
3089 Also do a little ANSI jig if necessary. */
3090 if (TREE_CODE (type) == REFERENCE_TYPE)
3092 CLASSTYPE_NON_POD_P (t) = 1;
3093 if (DECL_INITIAL (x) == NULL_TREE)
3094 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3096 /* ARM $12.6.2: [A member initializer list] (or, for an
3097 aggregate, initialization by a brace-enclosed list) is the
3098 only way to initialize nonstatic const and reference
3099 members. */
3100 *cant_have_default_ctor_p = 1;
3101 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3103 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3104 && extra_warnings)
3105 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3108 type = strip_array_types (type);
3110 if (TYPE_PTR_P (type))
3111 has_pointers = 1;
3113 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3114 CLASSTYPE_HAS_MUTABLE (t) = 1;
3116 if (! pod_type_p (type))
3117 /* DR 148 now allows pointers to members (which are POD themselves),
3118 to be allowed in POD structs. */
3119 CLASSTYPE_NON_POD_P (t) = 1;
3121 if (! zero_init_p (type))
3122 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3124 /* If any field is const, the structure type is pseudo-const. */
3125 if (CP_TYPE_CONST_P (type))
3127 C_TYPE_FIELDS_READONLY (t) = 1;
3128 if (DECL_INITIAL (x) == NULL_TREE)
3129 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3131 /* ARM $12.6.2: [A member initializer list] (or, for an
3132 aggregate, initialization by a brace-enclosed list) is the
3133 only way to initialize nonstatic const and reference
3134 members. */
3135 *cant_have_default_ctor_p = 1;
3136 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3138 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3139 && extra_warnings)
3140 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3142 /* A field that is pseudo-const makes the structure likewise. */
3143 else if (CLASS_TYPE_P (type))
3145 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3146 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3147 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3148 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3151 /* Core issue 80: A nonstatic data member is required to have a
3152 different name from the class iff the class has a
3153 user-defined constructor. */
3154 if (constructor_name_p (DECL_NAME (x), t) && TYPE_HAS_CONSTRUCTOR (t))
3155 cp_pedwarn_at ("field `%#D' with same name as class", x);
3157 /* We set DECL_C_BIT_FIELD in grokbitfield.
3158 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3159 if (DECL_C_BIT_FIELD (x))
3160 check_bitfield_decl (x);
3161 else
3162 check_field_decl (x, t,
3163 cant_have_const_ctor_p,
3164 cant_have_default_ctor_p,
3165 no_const_asn_ref_p,
3166 &any_default_members);
3169 /* Effective C++ rule 11. */
3170 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3171 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3173 warning ("`%#T' has pointer data members", t);
3175 if (! TYPE_HAS_INIT_REF (t))
3177 warning (" but does not override `%T(const %T&)'", t, t);
3178 if (! TYPE_HAS_ASSIGN_REF (t))
3179 warning (" or `operator=(const %T&)'", t);
3181 else if (! TYPE_HAS_ASSIGN_REF (t))
3182 warning (" but does not override `operator=(const %T&)'", t);
3186 /* Check anonymous struct/anonymous union fields. */
3187 finish_struct_anon (t);
3189 /* We've built up the list of access declarations in reverse order.
3190 Fix that now. */
3191 *access_decls = nreverse (*access_decls);
3194 /* If TYPE is an empty class type, records its OFFSET in the table of
3195 OFFSETS. */
3197 static int
3198 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3200 splay_tree_node n;
3202 if (!is_empty_class (type))
3203 return 0;
3205 /* Record the location of this empty object in OFFSETS. */
3206 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3207 if (!n)
3208 n = splay_tree_insert (offsets,
3209 (splay_tree_key) offset,
3210 (splay_tree_value) NULL_TREE);
3211 n->value = ((splay_tree_value)
3212 tree_cons (NULL_TREE,
3213 type,
3214 (tree) n->value));
3216 return 0;
3219 /* Returns nonzero if TYPE is an empty class type and there is
3220 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3222 static int
3223 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3225 splay_tree_node n;
3226 tree t;
3228 if (!is_empty_class (type))
3229 return 0;
3231 /* Record the location of this empty object in OFFSETS. */
3232 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3233 if (!n)
3234 return 0;
3236 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3237 if (same_type_p (TREE_VALUE (t), type))
3238 return 1;
3240 return 0;
3243 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3244 F for every subobject, passing it the type, offset, and table of
3245 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3246 be traversed.
3248 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3249 than MAX_OFFSET will not be walked.
3251 If F returns a nonzero value, the traversal ceases, and that value
3252 is returned. Otherwise, returns zero. */
3254 static int
3255 walk_subobject_offsets (tree type,
3256 subobject_offset_fn f,
3257 tree offset,
3258 splay_tree offsets,
3259 tree max_offset,
3260 int vbases_p)
3262 int r = 0;
3263 tree type_binfo = NULL_TREE;
3265 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3266 stop. */
3267 if (max_offset && INT_CST_LT (max_offset, offset))
3268 return 0;
3270 if (!TYPE_P (type))
3272 if (abi_version_at_least (2))
3273 type_binfo = type;
3274 type = BINFO_TYPE (type);
3277 if (CLASS_TYPE_P (type))
3279 tree field;
3280 tree binfo;
3281 int i;
3283 /* Avoid recursing into objects that are not interesting. */
3284 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3285 return 0;
3287 /* Record the location of TYPE. */
3288 r = (*f) (type, offset, offsets);
3289 if (r)
3290 return r;
3292 /* Iterate through the direct base classes of TYPE. */
3293 if (!type_binfo)
3294 type_binfo = TYPE_BINFO (type);
3295 for (i = 0; i < BINFO_N_BASETYPES (type_binfo); ++i)
3297 tree binfo_offset;
3299 binfo = BINFO_BASETYPE (type_binfo, i);
3301 if (abi_version_at_least (2)
3302 && TREE_VIA_VIRTUAL (binfo))
3303 continue;
3305 if (!vbases_p
3306 && TREE_VIA_VIRTUAL (binfo)
3307 && !BINFO_PRIMARY_P (binfo))
3308 continue;
3310 if (!abi_version_at_least (2))
3311 binfo_offset = size_binop (PLUS_EXPR,
3312 offset,
3313 BINFO_OFFSET (binfo));
3314 else
3316 tree orig_binfo;
3317 /* We cannot rely on BINFO_OFFSET being set for the base
3318 class yet, but the offsets for direct non-virtual
3319 bases can be calculated by going back to the TYPE. */
3320 orig_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3321 binfo_offset = size_binop (PLUS_EXPR,
3322 offset,
3323 BINFO_OFFSET (orig_binfo));
3326 r = walk_subobject_offsets (binfo,
3328 binfo_offset,
3329 offsets,
3330 max_offset,
3331 (abi_version_at_least (2)
3332 ? /*vbases_p=*/0 : vbases_p));
3333 if (r)
3334 return r;
3337 if (abi_version_at_least (2))
3339 tree vbase;
3341 /* Iterate through the virtual base classes of TYPE. In G++
3342 3.2, we included virtual bases in the direct base class
3343 loop above, which results in incorrect results; the
3344 correct offsets for virtual bases are only known when
3345 working with the most derived type. */
3346 if (vbases_p)
3347 for (vbase = CLASSTYPE_VBASECLASSES (type);
3348 vbase;
3349 vbase = TREE_CHAIN (vbase))
3351 binfo = TREE_VALUE (vbase);
3352 r = walk_subobject_offsets (binfo,
3354 size_binop (PLUS_EXPR,
3355 offset,
3356 BINFO_OFFSET (binfo)),
3357 offsets,
3358 max_offset,
3359 /*vbases_p=*/0);
3360 if (r)
3361 return r;
3363 else
3365 /* We still have to walk the primary base, if it is
3366 virtual. (If it is non-virtual, then it was walked
3367 above.) */
3368 vbase = get_primary_binfo (type_binfo);
3369 if (vbase && TREE_VIA_VIRTUAL (vbase)
3370 && BINFO_PRIMARY_BASE_OF (vbase) == type_binfo)
3372 r = (walk_subobject_offsets
3373 (vbase, f, offset,
3374 offsets, max_offset, /*vbases_p=*/0));
3375 if (r)
3376 return r;
3381 /* Iterate through the fields of TYPE. */
3382 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3383 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3385 tree field_offset;
3387 if (abi_version_at_least (2))
3388 field_offset = byte_position (field);
3389 else
3390 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3391 field_offset = DECL_FIELD_OFFSET (field);
3393 r = walk_subobject_offsets (TREE_TYPE (field),
3395 size_binop (PLUS_EXPR,
3396 offset,
3397 field_offset),
3398 offsets,
3399 max_offset,
3400 /*vbases_p=*/1);
3401 if (r)
3402 return r;
3405 else if (TREE_CODE (type) == ARRAY_TYPE)
3407 tree element_type = strip_array_types (type);
3408 tree domain = TYPE_DOMAIN (type);
3409 tree index;
3411 /* Avoid recursing into objects that are not interesting. */
3412 if (!CLASS_TYPE_P (element_type)
3413 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3414 return 0;
3416 /* Step through each of the elements in the array. */
3417 for (index = size_zero_node;
3418 /* G++ 3.2 had an off-by-one error here. */
3419 (abi_version_at_least (2)
3420 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3421 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3422 index = size_binop (PLUS_EXPR, index, size_one_node))
3424 r = walk_subobject_offsets (TREE_TYPE (type),
3426 offset,
3427 offsets,
3428 max_offset,
3429 /*vbases_p=*/1);
3430 if (r)
3431 return r;
3432 offset = size_binop (PLUS_EXPR, offset,
3433 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3434 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3435 there's no point in iterating through the remaining
3436 elements of the array. */
3437 if (max_offset && INT_CST_LT (max_offset, offset))
3438 break;
3442 return 0;
3445 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3446 OFFSETS. If VBASES_P is nonzero, virtual bases of TYPE are
3447 examined. */
3449 static void
3450 record_subobject_offsets (tree type,
3451 tree offset,
3452 splay_tree offsets,
3453 int vbases_p)
3455 walk_subobject_offsets (type, record_subobject_offset, offset,
3456 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3459 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3460 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3461 virtual bases of TYPE are examined. */
3463 static int
3464 layout_conflict_p (tree type,
3465 tree offset,
3466 splay_tree offsets,
3467 int vbases_p)
3469 splay_tree_node max_node;
3471 /* Get the node in OFFSETS that indicates the maximum offset where
3472 an empty subobject is located. */
3473 max_node = splay_tree_max (offsets);
3474 /* If there aren't any empty subobjects, then there's no point in
3475 performing this check. */
3476 if (!max_node)
3477 return 0;
3479 return walk_subobject_offsets (type, check_subobject_offset, offset,
3480 offsets, (tree) (max_node->key),
3481 vbases_p);
3484 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3485 non-static data member of the type indicated by RLI. BINFO is the
3486 binfo corresponding to the base subobject, OFFSETS maps offsets to
3487 types already located at those offsets. This function determines
3488 the position of the DECL. */
3490 static void
3491 layout_nonempty_base_or_field (record_layout_info rli,
3492 tree decl,
3493 tree binfo,
3494 splay_tree offsets)
3496 tree offset = NULL_TREE;
3497 bool field_p;
3498 tree type;
3500 if (binfo)
3502 /* For the purposes of determining layout conflicts, we want to
3503 use the class type of BINFO; TREE_TYPE (DECL) will be the
3504 CLASSTYPE_AS_BASE version, which does not contain entries for
3505 zero-sized bases. */
3506 type = TREE_TYPE (binfo);
3507 field_p = false;
3509 else
3511 type = TREE_TYPE (decl);
3512 field_p = true;
3515 /* Try to place the field. It may take more than one try if we have
3516 a hard time placing the field without putting two objects of the
3517 same type at the same address. */
3518 while (1)
3520 struct record_layout_info_s old_rli = *rli;
3522 /* Place this field. */
3523 place_field (rli, decl);
3524 offset = byte_position (decl);
3526 /* We have to check to see whether or not there is already
3527 something of the same type at the offset we're about to use.
3528 For example:
3530 struct S {};
3531 struct T : public S { int i; };
3532 struct U : public S, public T {};
3534 Here, we put S at offset zero in U. Then, we can't put T at
3535 offset zero -- its S component would be at the same address
3536 as the S we already allocated. So, we have to skip ahead.
3537 Since all data members, including those whose type is an
3538 empty class, have nonzero size, any overlap can happen only
3539 with a direct or indirect base-class -- it can't happen with
3540 a data member. */
3541 /* G++ 3.2 did not check for overlaps when placing a non-empty
3542 virtual base. */
3543 if (!abi_version_at_least (2) && binfo && TREE_VIA_VIRTUAL (binfo))
3544 break;
3545 if (layout_conflict_p (field_p ? type : binfo, offset,
3546 offsets, field_p))
3548 /* Strip off the size allocated to this field. That puts us
3549 at the first place we could have put the field with
3550 proper alignment. */
3551 *rli = old_rli;
3553 /* Bump up by the alignment required for the type. */
3554 rli->bitpos
3555 = size_binop (PLUS_EXPR, rli->bitpos,
3556 bitsize_int (binfo
3557 ? CLASSTYPE_ALIGN (type)
3558 : TYPE_ALIGN (type)));
3559 normalize_rli (rli);
3561 else
3562 /* There was no conflict. We're done laying out this field. */
3563 break;
3566 /* Now that we know where it will be placed, update its
3567 BINFO_OFFSET. */
3568 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3569 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3570 this point because their BINFO_OFFSET is copied from another
3571 hierarchy. Therefore, we may not need to add the entire
3572 OFFSET. */
3573 propagate_binfo_offsets (binfo,
3574 size_diffop (convert (ssizetype, offset),
3575 convert (ssizetype,
3576 BINFO_OFFSET (binfo))));
3579 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3581 static int
3582 empty_base_at_nonzero_offset_p (tree type,
3583 tree offset,
3584 splay_tree offsets ATTRIBUTE_UNUSED)
3586 return is_empty_class (type) && !integer_zerop (offset);
3589 /* Layout the empty base BINFO. EOC indicates the byte currently just
3590 past the end of the class, and should be correctly aligned for a
3591 class of the type indicated by BINFO; OFFSETS gives the offsets of
3592 the empty bases allocated so far. T is the most derived
3593 type. Return nonzero iff we added it at the end. */
3595 static bool
3596 layout_empty_base (tree binfo, tree eoc, splay_tree offsets)
3598 tree alignment;
3599 tree basetype = BINFO_TYPE (binfo);
3600 bool atend = false;
3602 /* This routine should only be used for empty classes. */
3603 my_friendly_assert (is_empty_class (basetype), 20000321);
3604 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3606 if (abi_version_at_least (2))
3607 BINFO_OFFSET (binfo) = size_zero_node;
3608 if (warn_abi && !integer_zerop (BINFO_OFFSET (binfo)))
3609 warning ("offset of empty base `%T' may not be ABI-compliant and may"
3610 "change in a future version of GCC",
3611 BINFO_TYPE (binfo));
3613 /* This is an empty base class. We first try to put it at offset
3614 zero. */
3615 if (layout_conflict_p (binfo,
3616 BINFO_OFFSET (binfo),
3617 offsets,
3618 /*vbases_p=*/0))
3620 /* That didn't work. Now, we move forward from the next
3621 available spot in the class. */
3622 atend = true;
3623 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3624 while (1)
3626 if (!layout_conflict_p (binfo,
3627 BINFO_OFFSET (binfo),
3628 offsets,
3629 /*vbases_p=*/0))
3630 /* We finally found a spot where there's no overlap. */
3631 break;
3633 /* There's overlap here, too. Bump along to the next spot. */
3634 propagate_binfo_offsets (binfo, alignment);
3637 return atend;
3640 /* Layout the the base given by BINFO in the class indicated by RLI.
3641 *BASE_ALIGN is a running maximum of the alignments of
3642 any base class. OFFSETS gives the location of empty base
3643 subobjects. T is the most derived type. Return nonzero if the new
3644 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3645 *NEXT_FIELD, unless BINFO is for an empty base class.
3647 Returns the location at which the next field should be inserted. */
3649 static tree *
3650 build_base_field (record_layout_info rli, tree binfo,
3651 splay_tree offsets, tree *next_field)
3653 tree t = rli->t;
3654 tree basetype = BINFO_TYPE (binfo);
3656 if (!COMPLETE_TYPE_P (basetype))
3657 /* This error is now reported in xref_tag, thus giving better
3658 location information. */
3659 return next_field;
3661 /* Place the base class. */
3662 if (!is_empty_class (basetype))
3664 tree decl;
3666 /* The containing class is non-empty because it has a non-empty
3667 base class. */
3668 CLASSTYPE_EMPTY_P (t) = 0;
3670 /* Create the FIELD_DECL. */
3671 decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3672 DECL_ARTIFICIAL (decl) = 1;
3673 DECL_FIELD_CONTEXT (decl) = t;
3674 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3675 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3676 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3677 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3678 DECL_IGNORED_P (decl) = 1;
3680 /* Try to place the field. It may take more than one try if we
3681 have a hard time placing the field without putting two
3682 objects of the same type at the same address. */
3683 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3684 /* Add the new FIELD_DECL to the list of fields for T. */
3685 TREE_CHAIN (decl) = *next_field;
3686 *next_field = decl;
3687 next_field = &TREE_CHAIN (decl);
3689 else
3691 tree eoc;
3692 bool atend;
3694 /* On some platforms (ARM), even empty classes will not be
3695 byte-aligned. */
3696 eoc = round_up (rli_size_unit_so_far (rli),
3697 CLASSTYPE_ALIGN_UNIT (basetype));
3698 atend = layout_empty_base (binfo, eoc, offsets);
3699 /* A nearly-empty class "has no proper base class that is empty,
3700 not morally virtual, and at an offset other than zero." */
3701 if (!TREE_VIA_VIRTUAL (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3703 if (atend)
3704 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3705 /* The check above (used in G++ 3.2) is insufficient because
3706 an empty class placed at offset zero might itself have an
3707 empty base at a nonzero offset. */
3708 else if (walk_subobject_offsets (basetype,
3709 empty_base_at_nonzero_offset_p,
3710 size_zero_node,
3711 /*offsets=*/NULL,
3712 /*max_offset=*/NULL_TREE,
3713 /*vbases_p=*/true))
3715 if (abi_version_at_least (2))
3716 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3717 else if (warn_abi)
3718 warning ("class `%T' will be considered nearly empty in a "
3719 "future version of GCC", t);
3723 /* We do not create a FIELD_DECL for empty base classes because
3724 it might overlap some other field. We want to be able to
3725 create CONSTRUCTORs for the class by iterating over the
3726 FIELD_DECLs, and the back end does not handle overlapping
3727 FIELD_DECLs. */
3729 /* An empty virtual base causes a class to be non-empty
3730 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3731 here because that was already done when the virtual table
3732 pointer was created. */
3735 /* Record the offsets of BINFO and its base subobjects. */
3736 record_subobject_offsets (binfo,
3737 BINFO_OFFSET (binfo),
3738 offsets,
3739 /*vbases_p=*/0);
3741 return next_field;
3744 /* Layout all of the non-virtual base classes. Record empty
3745 subobjects in OFFSETS. T is the most derived type. Return nonzero
3746 if the type cannot be nearly empty. The fields created
3747 corresponding to the base classes will be inserted at
3748 *NEXT_FIELD. */
3750 static void
3751 build_base_fields (record_layout_info rli,
3752 splay_tree offsets, tree *next_field)
3754 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3755 subobjects. */
3756 tree t = rli->t;
3757 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
3758 int i;
3760 /* The primary base class is always allocated first. */
3761 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3762 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
3763 offsets, next_field);
3765 /* Now allocate the rest of the bases. */
3766 for (i = 0; i < n_baseclasses; ++i)
3768 tree base_binfo;
3770 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
3772 /* The primary base was already allocated above, so we don't
3773 need to allocate it again here. */
3774 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
3775 continue;
3777 /* Virtual bases are added at the end (a primary virtual base
3778 will have already been added). */
3779 if (TREE_VIA_VIRTUAL (base_binfo))
3780 continue;
3782 next_field = build_base_field (rli, base_binfo,
3783 offsets, next_field);
3787 /* Go through the TYPE_METHODS of T issuing any appropriate
3788 diagnostics, figuring out which methods override which other
3789 methods, and so forth. */
3791 static void
3792 check_methods (tree t)
3794 tree x;
3796 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3798 /* If this was an evil function, don't keep it in class. */
3799 if (DECL_ASSEMBLER_NAME_SET_P (x)
3800 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3801 continue;
3803 check_for_override (x, t);
3804 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3805 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3807 /* The name of the field is the original field name
3808 Save this in auxiliary field for later overloading. */
3809 if (DECL_VINDEX (x))
3811 TYPE_POLYMORPHIC_P (t) = 1;
3812 if (DECL_PURE_VIRTUAL_P (x))
3813 CLASSTYPE_PURE_VIRTUALS (t)
3814 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3819 /* FN is a constructor or destructor. Clone the declaration to create
3820 a specialized in-charge or not-in-charge version, as indicated by
3821 NAME. */
3823 static tree
3824 build_clone (tree fn, tree name)
3826 tree parms;
3827 tree clone;
3829 /* Copy the function. */
3830 clone = copy_decl (fn);
3831 /* Remember where this function came from. */
3832 DECL_CLONED_FUNCTION (clone) = fn;
3833 DECL_ABSTRACT_ORIGIN (clone) = fn;
3834 /* Reset the function name. */
3835 DECL_NAME (clone) = name;
3836 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3837 /* There's no pending inline data for this function. */
3838 DECL_PENDING_INLINE_INFO (clone) = NULL;
3839 DECL_PENDING_INLINE_P (clone) = 0;
3840 /* And it hasn't yet been deferred. */
3841 DECL_DEFERRED_FN (clone) = 0;
3843 /* The base-class destructor is not virtual. */
3844 if (name == base_dtor_identifier)
3846 DECL_VIRTUAL_P (clone) = 0;
3847 if (TREE_CODE (clone) != TEMPLATE_DECL)
3848 DECL_VINDEX (clone) = NULL_TREE;
3851 /* If there was an in-charge parameter, drop it from the function
3852 type. */
3853 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3855 tree basetype;
3856 tree parmtypes;
3857 tree exceptions;
3859 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3860 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3861 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3862 /* Skip the `this' parameter. */
3863 parmtypes = TREE_CHAIN (parmtypes);
3864 /* Skip the in-charge parameter. */
3865 parmtypes = TREE_CHAIN (parmtypes);
3866 /* And the VTT parm, in a complete [cd]tor. */
3867 if (DECL_HAS_VTT_PARM_P (fn)
3868 && ! DECL_NEEDS_VTT_PARM_P (clone))
3869 parmtypes = TREE_CHAIN (parmtypes);
3870 /* If this is subobject constructor or destructor, add the vtt
3871 parameter. */
3872 TREE_TYPE (clone)
3873 = build_cplus_method_type (basetype,
3874 TREE_TYPE (TREE_TYPE (clone)),
3875 parmtypes);
3876 if (exceptions)
3877 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3878 exceptions);
3881 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
3882 aren't function parameters; those are the template parameters. */
3883 if (TREE_CODE (clone) != TEMPLATE_DECL)
3885 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
3886 /* Remove the in-charge parameter. */
3887 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3889 TREE_CHAIN (DECL_ARGUMENTS (clone))
3890 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3891 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
3893 /* And the VTT parm, in a complete [cd]tor. */
3894 if (DECL_HAS_VTT_PARM_P (fn))
3896 if (DECL_NEEDS_VTT_PARM_P (clone))
3897 DECL_HAS_VTT_PARM_P (clone) = 1;
3898 else
3900 TREE_CHAIN (DECL_ARGUMENTS (clone))
3901 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3902 DECL_HAS_VTT_PARM_P (clone) = 0;
3906 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
3908 DECL_CONTEXT (parms) = clone;
3909 cxx_dup_lang_specific_decl (parms);
3913 /* Create the RTL for this function. */
3914 SET_DECL_RTL (clone, NULL_RTX);
3915 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
3917 /* Make it easy to find the CLONE given the FN. */
3918 TREE_CHAIN (clone) = TREE_CHAIN (fn);
3919 TREE_CHAIN (fn) = clone;
3921 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
3922 if (TREE_CODE (clone) == TEMPLATE_DECL)
3924 tree result;
3926 DECL_TEMPLATE_RESULT (clone)
3927 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
3928 result = DECL_TEMPLATE_RESULT (clone);
3929 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
3930 DECL_TI_TEMPLATE (result) = clone;
3932 else if (DECL_DEFERRED_FN (fn))
3933 defer_fn (clone);
3935 return clone;
3938 /* Produce declarations for all appropriate clones of FN. If
3939 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
3940 CLASTYPE_METHOD_VEC as well. */
3942 void
3943 clone_function_decl (tree fn, int update_method_vec_p)
3945 tree clone;
3947 /* Avoid inappropriate cloning. */
3948 if (TREE_CHAIN (fn)
3949 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
3950 return;
3952 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
3954 /* For each constructor, we need two variants: an in-charge version
3955 and a not-in-charge version. */
3956 clone = build_clone (fn, complete_ctor_identifier);
3957 if (update_method_vec_p)
3958 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
3959 clone = build_clone (fn, base_ctor_identifier);
3960 if (update_method_vec_p)
3961 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
3963 else
3965 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
3967 /* For each destructor, we need three variants: an in-charge
3968 version, a not-in-charge version, and an in-charge deleting
3969 version. We clone the deleting version first because that
3970 means it will go second on the TYPE_METHODS list -- and that
3971 corresponds to the correct layout order in the virtual
3972 function table.
3974 For a non-virtual destructor, we do not build a deleting
3975 destructor. */
3976 if (DECL_VIRTUAL_P (fn))
3978 clone = build_clone (fn, deleting_dtor_identifier);
3979 if (update_method_vec_p)
3980 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
3982 clone = build_clone (fn, complete_dtor_identifier);
3983 if (update_method_vec_p)
3984 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
3985 clone = build_clone (fn, base_dtor_identifier);
3986 if (update_method_vec_p)
3987 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
3990 /* Note that this is an abstract function that is never emitted. */
3991 DECL_ABSTRACT (fn) = 1;
3994 /* DECL is an in charge constructor, which is being defined. This will
3995 have had an in class declaration, from whence clones were
3996 declared. An out-of-class definition can specify additional default
3997 arguments. As it is the clones that are involved in overload
3998 resolution, we must propagate the information from the DECL to its
3999 clones. */
4001 void
4002 adjust_clone_args (tree decl)
4004 tree clone;
4006 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4007 clone = TREE_CHAIN (clone))
4009 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4010 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4011 tree decl_parms, clone_parms;
4013 clone_parms = orig_clone_parms;
4015 /* Skip the 'this' parameter. */
4016 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4017 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4019 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4020 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4021 if (DECL_HAS_VTT_PARM_P (decl))
4022 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4024 clone_parms = orig_clone_parms;
4025 if (DECL_HAS_VTT_PARM_P (clone))
4026 clone_parms = TREE_CHAIN (clone_parms);
4028 for (decl_parms = orig_decl_parms; decl_parms;
4029 decl_parms = TREE_CHAIN (decl_parms),
4030 clone_parms = TREE_CHAIN (clone_parms))
4032 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4033 TREE_TYPE (clone_parms)), 20010424);
4035 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4037 /* A default parameter has been added. Adjust the
4038 clone's parameters. */
4039 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4040 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4041 tree type;
4043 clone_parms = orig_decl_parms;
4045 if (DECL_HAS_VTT_PARM_P (clone))
4047 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4048 TREE_VALUE (orig_clone_parms),
4049 clone_parms);
4050 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4052 type = build_cplus_method_type (basetype,
4053 TREE_TYPE (TREE_TYPE (clone)),
4054 clone_parms);
4055 if (exceptions)
4056 type = build_exception_variant (type, exceptions);
4057 TREE_TYPE (clone) = type;
4059 clone_parms = NULL_TREE;
4060 break;
4063 my_friendly_assert (!clone_parms, 20010424);
4067 /* For each of the constructors and destructors in T, create an
4068 in-charge and not-in-charge variant. */
4070 static void
4071 clone_constructors_and_destructors (tree t)
4073 tree fns;
4075 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4076 out now. */
4077 if (!CLASSTYPE_METHOD_VEC (t))
4078 return;
4080 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4081 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4082 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4083 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4086 /* Remove all zero-width bit-fields from T. */
4088 static void
4089 remove_zero_width_bit_fields (tree t)
4091 tree *fieldsp;
4093 fieldsp = &TYPE_FIELDS (t);
4094 while (*fieldsp)
4096 if (TREE_CODE (*fieldsp) == FIELD_DECL
4097 && DECL_C_BIT_FIELD (*fieldsp)
4098 && DECL_INITIAL (*fieldsp))
4099 *fieldsp = TREE_CHAIN (*fieldsp);
4100 else
4101 fieldsp = &TREE_CHAIN (*fieldsp);
4105 /* Returns TRUE iff we need a cookie when dynamically allocating an
4106 array whose elements have the indicated class TYPE. */
4108 static bool
4109 type_requires_array_cookie (tree type)
4111 tree fns;
4112 bool has_two_argument_delete_p = false;
4114 my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4116 /* If there's a non-trivial destructor, we need a cookie. In order
4117 to iterate through the array calling the destructor for each
4118 element, we'll have to know how many elements there are. */
4119 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4120 return true;
4122 /* If the usual deallocation function is a two-argument whose second
4123 argument is of type `size_t', then we have to pass the size of
4124 the array to the deallocation function, so we will need to store
4125 a cookie. */
4126 fns = lookup_fnfields (TYPE_BINFO (type),
4127 ansi_opname (VEC_DELETE_EXPR),
4128 /*protect=*/0);
4129 /* If there are no `operator []' members, or the lookup is
4130 ambiguous, then we don't need a cookie. */
4131 if (!fns || fns == error_mark_node)
4132 return false;
4133 /* Loop through all of the functions. */
4134 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4136 tree fn;
4137 tree second_parm;
4139 /* Select the current function. */
4140 fn = OVL_CURRENT (fns);
4141 /* See if this function is a one-argument delete function. If
4142 it is, then it will be the usual deallocation function. */
4143 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4144 if (second_parm == void_list_node)
4145 return false;
4146 /* Otherwise, if we have a two-argument function and the second
4147 argument is `size_t', it will be the usual deallocation
4148 function -- unless there is one-argument function, too. */
4149 if (TREE_CHAIN (second_parm) == void_list_node
4150 && same_type_p (TREE_VALUE (second_parm), sizetype))
4151 has_two_argument_delete_p = true;
4154 return has_two_argument_delete_p;
4157 /* Check the validity of the bases and members declared in T. Add any
4158 implicitly-generated functions (like copy-constructors and
4159 assignment operators). Compute various flag bits (like
4160 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4161 level: i.e., independently of the ABI in use. */
4163 static void
4164 check_bases_and_members (tree t)
4166 /* Nonzero if we are not allowed to generate a default constructor
4167 for this case. */
4168 int cant_have_default_ctor;
4169 /* Nonzero if the implicitly generated copy constructor should take
4170 a non-const reference argument. */
4171 int cant_have_const_ctor;
4172 /* Nonzero if the the implicitly generated assignment operator
4173 should take a non-const reference argument. */
4174 int no_const_asn_ref;
4175 tree access_decls;
4177 /* By default, we use const reference arguments and generate default
4178 constructors. */
4179 cant_have_default_ctor = 0;
4180 cant_have_const_ctor = 0;
4181 no_const_asn_ref = 0;
4183 /* Check all the base-classes. */
4184 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4185 &no_const_asn_ref);
4187 /* Check all the data member declarations. */
4188 check_field_decls (t, &access_decls,
4189 &cant_have_default_ctor,
4190 &cant_have_const_ctor,
4191 &no_const_asn_ref);
4193 /* Check all the method declarations. */
4194 check_methods (t);
4196 /* A nearly-empty class has to be vptr-containing; a nearly empty
4197 class contains just a vptr. */
4198 if (!TYPE_CONTAINS_VPTR_P (t))
4199 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4201 /* Do some bookkeeping that will guide the generation of implicitly
4202 declared member functions. */
4203 TYPE_HAS_COMPLEX_INIT_REF (t)
4204 |= (TYPE_HAS_INIT_REF (t)
4205 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4206 || TYPE_POLYMORPHIC_P (t));
4207 TYPE_NEEDS_CONSTRUCTING (t)
4208 |= (TYPE_HAS_CONSTRUCTOR (t)
4209 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4210 || TYPE_POLYMORPHIC_P (t));
4211 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4212 || TYPE_POLYMORPHIC_P (t));
4213 CLASSTYPE_NON_POD_P (t)
4214 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4215 || TYPE_HAS_ASSIGN_REF (t));
4216 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4217 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4218 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4220 /* Synthesize any needed methods. Note that methods will be synthesized
4221 for anonymous unions; grok_x_components undoes that. */
4222 add_implicitly_declared_members (t, cant_have_default_ctor,
4223 cant_have_const_ctor,
4224 no_const_asn_ref);
4226 /* Create the in-charge and not-in-charge variants of constructors
4227 and destructors. */
4228 clone_constructors_and_destructors (t);
4230 /* Process the using-declarations. */
4231 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4232 handle_using_decl (TREE_VALUE (access_decls), t);
4234 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4235 finish_struct_methods (t);
4237 /* Figure out whether or not we will need a cookie when dynamically
4238 allocating an array of this type. */
4239 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4240 = type_requires_array_cookie (t);
4243 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4244 accordingly. If a new vfield was created (because T doesn't have a
4245 primary base class), then the newly created field is returned. It
4246 is not added to the TYPE_FIELDS list; it is the caller's
4247 responsibility to do that. Accumulate declared virtual functions
4248 on VIRTUALS_P. */
4250 static tree
4251 create_vtable_ptr (tree t, tree* virtuals_p)
4253 tree fn;
4255 /* Collect the virtual functions declared in T. */
4256 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4257 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4258 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4260 tree new_virtual = make_node (TREE_LIST);
4262 BV_FN (new_virtual) = fn;
4263 BV_DELTA (new_virtual) = integer_zero_node;
4265 TREE_CHAIN (new_virtual) = *virtuals_p;
4266 *virtuals_p = new_virtual;
4269 /* If we couldn't find an appropriate base class, create a new field
4270 here. Even if there weren't any new virtual functions, we might need a
4271 new virtual function table if we're supposed to include vptrs in
4272 all classes that need them. */
4273 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
4275 /* We build this decl with vtbl_ptr_type_node, which is a
4276 `vtable_entry_type*'. It might seem more precise to use
4277 `vtable_entry_type (*)[N]' where N is the number of firtual
4278 functions. However, that would require the vtable pointer in
4279 base classes to have a different type than the vtable pointer
4280 in derived classes. We could make that happen, but that
4281 still wouldn't solve all the problems. In particular, the
4282 type-based alias analysis code would decide that assignments
4283 to the base class vtable pointer can't alias assignments to
4284 the derived class vtable pointer, since they have different
4285 types. Thus, in a derived class destructor, where the base
4286 class constructor was inlined, we could generate bad code for
4287 setting up the vtable pointer.
4289 Therefore, we use one type for all vtable pointers. We still
4290 use a type-correct type; it's just doesn't indicate the array
4291 bounds. That's better than using `void*' or some such; it's
4292 cleaner, and it let's the alias analysis code know that these
4293 stores cannot alias stores to void*! */
4294 tree field;
4296 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4297 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4298 DECL_VIRTUAL_P (field) = 1;
4299 DECL_ARTIFICIAL (field) = 1;
4300 DECL_FIELD_CONTEXT (field) = t;
4301 DECL_FCONTEXT (field) = t;
4303 TYPE_VFIELD (t) = field;
4305 /* This class is non-empty. */
4306 CLASSTYPE_EMPTY_P (t) = 0;
4308 if (CLASSTYPE_N_BASECLASSES (t))
4309 /* If there were any baseclasses, they can't possibly be at
4310 offset zero any more, because that's where the vtable
4311 pointer is. So, converting to a base class is going to
4312 take work. */
4313 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4315 return field;
4318 return NULL_TREE;
4321 /* Fixup the inline function given by INFO now that the class is
4322 complete. */
4324 static void
4325 fixup_pending_inline (tree fn)
4327 if (DECL_PENDING_INLINE_INFO (fn))
4329 tree args = DECL_ARGUMENTS (fn);
4330 while (args)
4332 DECL_CONTEXT (args) = fn;
4333 args = TREE_CHAIN (args);
4338 /* Fixup the inline methods and friends in TYPE now that TYPE is
4339 complete. */
4341 static void
4342 fixup_inline_methods (tree type)
4344 tree method = TYPE_METHODS (type);
4346 if (method && TREE_CODE (method) == TREE_VEC)
4348 if (TREE_VEC_ELT (method, 1))
4349 method = TREE_VEC_ELT (method, 1);
4350 else if (TREE_VEC_ELT (method, 0))
4351 method = TREE_VEC_ELT (method, 0);
4352 else
4353 method = TREE_VEC_ELT (method, 2);
4356 /* Do inline member functions. */
4357 for (; method; method = TREE_CHAIN (method))
4358 fixup_pending_inline (method);
4360 /* Do friends. */
4361 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4362 method;
4363 method = TREE_CHAIN (method))
4364 fixup_pending_inline (TREE_VALUE (method));
4365 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4368 /* Add OFFSET to all base types of BINFO which is a base in the
4369 hierarchy dominated by T.
4371 OFFSET, which is a type offset, is number of bytes. */
4373 static void
4374 propagate_binfo_offsets (tree binfo, tree offset)
4376 int i;
4377 tree primary_binfo;
4379 /* Update BINFO's offset. */
4380 BINFO_OFFSET (binfo)
4381 = convert (sizetype,
4382 size_binop (PLUS_EXPR,
4383 convert (ssizetype, BINFO_OFFSET (binfo)),
4384 offset));
4386 /* Find the primary base class. */
4387 primary_binfo = get_primary_binfo (binfo);
4389 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4390 downwards. */
4391 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4393 tree base_binfo;
4395 /* On the first time through the loop, do the primary base.
4396 Because the primary base need not be an immediate base, we
4397 must handle the primary base specially. */
4398 if (i == -1)
4400 if (!primary_binfo)
4401 continue;
4403 base_binfo = primary_binfo;
4405 else
4407 base_binfo = BINFO_BASETYPE (binfo, i);
4408 /* Don't do the primary base twice. */
4409 if (base_binfo == primary_binfo)
4410 continue;
4413 /* Skip virtual bases that aren't our canonical primary base. */
4414 if (TREE_VIA_VIRTUAL (base_binfo)
4415 && BINFO_PRIMARY_BASE_OF (base_binfo) != binfo)
4416 continue;
4418 propagate_binfo_offsets (base_binfo, offset);
4422 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
4423 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4424 empty subobjects of T. */
4426 static void
4427 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
4429 tree vbase;
4430 tree t = rli->t;
4431 bool first_vbase = true;
4432 tree *next_field;
4434 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4435 return;
4437 if (!abi_version_at_least(2))
4439 /* In G++ 3.2, we incorrectly rounded the size before laying out
4440 the virtual bases. */
4441 finish_record_layout (rli, /*free_p=*/false);
4442 #ifdef STRUCTURE_SIZE_BOUNDARY
4443 /* Packed structures don't need to have minimum size. */
4444 if (! TYPE_PACKED (t))
4445 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
4446 #endif
4447 rli->offset = TYPE_SIZE_UNIT (t);
4448 rli->bitpos = bitsize_zero_node;
4449 rli->record_align = TYPE_ALIGN (t);
4452 /* Find the last field. The artificial fields created for virtual
4453 bases will go after the last extant field to date. */
4454 next_field = &TYPE_FIELDS (t);
4455 while (*next_field)
4456 next_field = &TREE_CHAIN (*next_field);
4458 /* Go through the virtual bases, allocating space for each virtual
4459 base that is not already a primary base class. These are
4460 allocated in inheritance graph order. */
4461 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
4463 if (!TREE_VIA_VIRTUAL (vbase))
4464 continue;
4466 if (!BINFO_PRIMARY_P (vbase))
4468 tree basetype = TREE_TYPE (vbase);
4470 /* This virtual base is not a primary base of any class in the
4471 hierarchy, so we have to add space for it. */
4472 next_field = build_base_field (rli, vbase,
4473 offsets, next_field);
4475 /* If the first virtual base might have been placed at a
4476 lower address, had we started from CLASSTYPE_SIZE, rather
4477 than TYPE_SIZE, issue a warning. There can be both false
4478 positives and false negatives from this warning in rare
4479 cases; to deal with all the possibilities would probably
4480 require performing both layout algorithms and comparing
4481 the results which is not particularly tractable. */
4482 if (warn_abi
4483 && first_vbase
4484 && (tree_int_cst_lt
4485 (size_binop (CEIL_DIV_EXPR,
4486 round_up (CLASSTYPE_SIZE (t),
4487 CLASSTYPE_ALIGN (basetype)),
4488 bitsize_unit_node),
4489 BINFO_OFFSET (vbase))))
4490 warning ("offset of virtual base `%T' is not ABI-compliant and may change in a future version of GCC",
4491 basetype);
4493 first_vbase = false;
4498 /* Returns the offset of the byte just past the end of the base class
4499 BINFO. */
4501 static tree
4502 end_of_base (tree binfo)
4504 tree size;
4506 if (is_empty_class (BINFO_TYPE (binfo)))
4507 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4508 allocate some space for it. It cannot have virtual bases, so
4509 TYPE_SIZE_UNIT is fine. */
4510 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4511 else
4512 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4514 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
4517 /* Returns the offset of the byte just past the end of the base class
4518 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4519 only non-virtual bases are included. */
4521 static tree
4522 end_of_class (tree t, int include_virtuals_p)
4524 tree result = size_zero_node;
4525 tree binfo;
4526 tree offset;
4527 int i;
4529 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4531 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4533 if (!include_virtuals_p
4534 && TREE_VIA_VIRTUAL (binfo)
4535 && BINFO_PRIMARY_BASE_OF (binfo) != TYPE_BINFO (t))
4536 continue;
4538 offset = end_of_base (binfo);
4539 if (INT_CST_LT_UNSIGNED (result, offset))
4540 result = offset;
4543 /* G++ 3.2 did not check indirect virtual bases. */
4544 if (abi_version_at_least (2) && include_virtuals_p)
4545 for (binfo = CLASSTYPE_VBASECLASSES (t);
4546 binfo;
4547 binfo = TREE_CHAIN (binfo))
4549 offset = end_of_base (TREE_VALUE (binfo));
4550 if (INT_CST_LT_UNSIGNED (result, offset))
4551 result = offset;
4554 return result;
4557 /* Warn about bases of T that are inaccessible because they are
4558 ambiguous. For example:
4560 struct S {};
4561 struct T : public S {};
4562 struct U : public S, public T {};
4564 Here, `(S*) new U' is not allowed because there are two `S'
4565 subobjects of U. */
4567 static void
4568 warn_about_ambiguous_bases (tree t)
4570 int i;
4571 tree vbases;
4572 tree basetype;
4574 /* Check direct bases. */
4575 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4577 basetype = TYPE_BINFO_BASETYPE (t, i);
4579 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4580 warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4581 basetype, t);
4584 /* Check for ambiguous virtual bases. */
4585 if (extra_warnings)
4586 for (vbases = CLASSTYPE_VBASECLASSES (t);
4587 vbases;
4588 vbases = TREE_CHAIN (vbases))
4590 basetype = BINFO_TYPE (TREE_VALUE (vbases));
4592 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4593 warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4594 basetype, t);
4598 /* Compare two INTEGER_CSTs K1 and K2. */
4600 static int
4601 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
4603 return tree_int_cst_compare ((tree) k1, (tree) k2);
4606 /* Increase the size indicated in RLI to account for empty classes
4607 that are "off the end" of the class. */
4609 static void
4610 include_empty_classes (record_layout_info rli)
4612 tree eoc;
4613 tree rli_size;
4615 /* It might be the case that we grew the class to allocate a
4616 zero-sized base class. That won't be reflected in RLI, yet,
4617 because we are willing to overlay multiple bases at the same
4618 offset. However, now we need to make sure that RLI is big enough
4619 to reflect the entire class. */
4620 eoc = end_of_class (rli->t,
4621 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
4622 rli_size = rli_size_unit_so_far (rli);
4623 if (TREE_CODE (rli_size) == INTEGER_CST
4624 && INT_CST_LT_UNSIGNED (rli_size, eoc))
4626 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4627 rli->bitpos
4628 = size_binop (PLUS_EXPR,
4629 rli->bitpos,
4630 size_binop (MULT_EXPR,
4631 convert (bitsizetype,
4632 size_binop (MINUS_EXPR,
4633 eoc, rli_size)),
4634 bitsize_int (BITS_PER_UNIT)));
4635 normalize_rli (rli);
4639 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4640 BINFO_OFFSETs for all of the base-classes. Position the vtable
4641 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
4643 static void
4644 layout_class_type (tree t, tree *virtuals_p)
4646 tree non_static_data_members;
4647 tree field;
4648 tree vptr;
4649 record_layout_info rli;
4650 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4651 types that appear at that offset. */
4652 splay_tree empty_base_offsets;
4653 /* True if the last field layed out was a bit-field. */
4654 bool last_field_was_bitfield = false;
4655 /* The location at which the next field should be inserted. */
4656 tree *next_field;
4657 /* T, as a base class. */
4658 tree base_t;
4660 /* Keep track of the first non-static data member. */
4661 non_static_data_members = TYPE_FIELDS (t);
4663 /* Start laying out the record. */
4664 rli = start_record_layout (t);
4666 /* If possible, we reuse the virtual function table pointer from one
4667 of our base classes. */
4668 determine_primary_base (t);
4670 /* Create a pointer to our virtual function table. */
4671 vptr = create_vtable_ptr (t, virtuals_p);
4673 /* The vptr is always the first thing in the class. */
4674 if (vptr)
4676 TREE_CHAIN (vptr) = TYPE_FIELDS (t);
4677 TYPE_FIELDS (t) = vptr;
4678 next_field = &TREE_CHAIN (vptr);
4679 place_field (rli, vptr);
4681 else
4682 next_field = &TYPE_FIELDS (t);
4684 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4685 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4686 NULL, NULL);
4687 build_base_fields (rli, empty_base_offsets, next_field);
4689 /* Layout the non-static data members. */
4690 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4692 tree type;
4693 tree padding;
4695 /* We still pass things that aren't non-static data members to
4696 the back-end, in case it wants to do something with them. */
4697 if (TREE_CODE (field) != FIELD_DECL)
4699 place_field (rli, field);
4700 /* If the static data member has incomplete type, keep track
4701 of it so that it can be completed later. (The handling
4702 of pending statics in finish_record_layout is
4703 insufficient; consider:
4705 struct S1;
4706 struct S2 { static S1 s1; };
4708 At this point, finish_record_layout will be called, but
4709 S1 is still incomplete.) */
4710 if (TREE_CODE (field) == VAR_DECL)
4711 maybe_register_incomplete_var (field);
4712 continue;
4715 type = TREE_TYPE (field);
4717 padding = NULL_TREE;
4719 /* If this field is a bit-field whose width is greater than its
4720 type, then there are some special rules for allocating
4721 it. */
4722 if (DECL_C_BIT_FIELD (field)
4723 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4725 integer_type_kind itk;
4726 tree integer_type;
4727 bool was_unnamed_p = false;
4728 /* We must allocate the bits as if suitably aligned for the
4729 longest integer type that fits in this many bits. type
4730 of the field. Then, we are supposed to use the left over
4731 bits as additional padding. */
4732 for (itk = itk_char; itk != itk_none; ++itk)
4733 if (INT_CST_LT (DECL_SIZE (field),
4734 TYPE_SIZE (integer_types[itk])))
4735 break;
4737 /* ITK now indicates a type that is too large for the
4738 field. We have to back up by one to find the largest
4739 type that fits. */
4740 integer_type = integer_types[itk - 1];
4742 /* Figure out how much additional padding is required. GCC
4743 3.2 always created a padding field, even if it had zero
4744 width. */
4745 if (!abi_version_at_least (2)
4746 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
4748 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
4749 /* In a union, the padding field must have the full width
4750 of the bit-field; all fields start at offset zero. */
4751 padding = DECL_SIZE (field);
4752 else
4754 if (warn_abi && TREE_CODE (t) == UNION_TYPE)
4755 warning ("size assigned to `%T' may not be "
4756 "ABI-compliant and may change in a future "
4757 "version of GCC",
4759 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4760 TYPE_SIZE (integer_type));
4763 #ifdef PCC_BITFIELD_TYPE_MATTERS
4764 /* An unnamed bitfield does not normally affect the
4765 alignment of the containing class on a target where
4766 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
4767 make any exceptions for unnamed bitfields when the
4768 bitfields are longer than their types. Therefore, we
4769 temporarily give the field a name. */
4770 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
4772 was_unnamed_p = true;
4773 DECL_NAME (field) = make_anon_name ();
4775 #endif
4776 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4777 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4778 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4779 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4780 empty_base_offsets);
4781 if (was_unnamed_p)
4782 DECL_NAME (field) = NULL_TREE;
4783 /* Now that layout has been performed, set the size of the
4784 field to the size of its declared type; the rest of the
4785 field is effectively invisible. */
4786 DECL_SIZE (field) = TYPE_SIZE (type);
4788 else
4789 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4790 empty_base_offsets);
4792 /* Remember the location of any empty classes in FIELD. */
4793 if (abi_version_at_least (2))
4794 record_subobject_offsets (TREE_TYPE (field),
4795 byte_position(field),
4796 empty_base_offsets,
4797 /*vbases_p=*/1);
4799 /* If a bit-field does not immediately follow another bit-field,
4800 and yet it starts in the middle of a byte, we have failed to
4801 comply with the ABI. */
4802 if (warn_abi
4803 && DECL_C_BIT_FIELD (field)
4804 && !last_field_was_bitfield
4805 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
4806 DECL_FIELD_BIT_OFFSET (field),
4807 bitsize_unit_node)))
4808 cp_warning_at ("offset of `%D' is not ABI-compliant and may change in a future version of GCC",
4809 field);
4811 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
4812 offset of the field. */
4813 if (warn_abi
4814 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
4815 byte_position (field))
4816 && contains_empty_class_p (TREE_TYPE (field)))
4817 cp_warning_at ("`%D' contains empty classes which may cause base "
4818 "classes to be placed at different locations in a "
4819 "future version of GCC",
4820 field);
4822 /* If we needed additional padding after this field, add it
4823 now. */
4824 if (padding)
4826 tree padding_field;
4828 padding_field = build_decl (FIELD_DECL,
4829 NULL_TREE,
4830 char_type_node);
4831 DECL_BIT_FIELD (padding_field) = 1;
4832 DECL_SIZE (padding_field) = padding;
4833 DECL_CONTEXT (padding_field) = t;
4834 layout_nonempty_base_or_field (rli, padding_field,
4835 NULL_TREE,
4836 empty_base_offsets);
4839 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
4842 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
4844 /* Make sure that we are on a byte boundary so that the size of
4845 the class without virtual bases will always be a round number
4846 of bytes. */
4847 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4848 normalize_rli (rli);
4851 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
4852 padding. */
4853 if (!abi_version_at_least (2))
4854 include_empty_classes(rli);
4856 /* Delete all zero-width bit-fields from the list of fields. Now
4857 that the type is laid out they are no longer important. */
4858 remove_zero_width_bit_fields (t);
4860 /* Create the version of T used for virtual bases. We do not use
4861 make_aggr_type for this version; this is an artificial type. For
4862 a POD type, we just reuse T. */
4863 if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t))
4865 base_t = make_node (TREE_CODE (t));
4867 /* Set the size and alignment for the new type. In G++ 3.2, all
4868 empty classes were considered to have size zero when used as
4869 base classes. */
4870 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
4872 TYPE_SIZE (base_t) = bitsize_zero_node;
4873 TYPE_SIZE_UNIT (base_t) = size_zero_node;
4874 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
4875 warning ("layout of classes derived from empty class `%T' "
4876 "may change in a future version of GCC",
4879 else
4881 tree eoc;
4883 /* If the ABI version is not at least two, and the last
4884 field was a bit-field, RLI may not be on a byte
4885 boundary. In particular, rli_size_unit_so_far might
4886 indicate the last complete byte, while rli_size_so_far
4887 indicates the total number of bits used. Therefore,
4888 rli_size_so_far, rather than rli_size_unit_so_far, is
4889 used to compute TYPE_SIZE_UNIT. */
4890 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4891 TYPE_SIZE_UNIT (base_t)
4892 = size_binop (MAX_EXPR,
4893 convert (sizetype,
4894 size_binop (CEIL_DIV_EXPR,
4895 rli_size_so_far (rli),
4896 bitsize_int (BITS_PER_UNIT))),
4897 eoc);
4898 TYPE_SIZE (base_t)
4899 = size_binop (MAX_EXPR,
4900 rli_size_so_far (rli),
4901 size_binop (MULT_EXPR,
4902 convert (bitsizetype, eoc),
4903 bitsize_int (BITS_PER_UNIT)));
4905 TYPE_ALIGN (base_t) = rli->record_align;
4906 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
4908 /* Copy the fields from T. */
4909 next_field = &TYPE_FIELDS (base_t);
4910 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
4911 if (TREE_CODE (field) == FIELD_DECL)
4913 *next_field = build_decl (FIELD_DECL,
4914 DECL_NAME (field),
4915 TREE_TYPE (field));
4916 DECL_CONTEXT (*next_field) = base_t;
4917 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
4918 DECL_FIELD_BIT_OFFSET (*next_field)
4919 = DECL_FIELD_BIT_OFFSET (field);
4920 next_field = &TREE_CHAIN (*next_field);
4923 /* Record the base version of the type. */
4924 CLASSTYPE_AS_BASE (t) = base_t;
4925 TYPE_CONTEXT (base_t) = t;
4927 else
4928 CLASSTYPE_AS_BASE (t) = t;
4930 /* Every empty class contains an empty class. */
4931 if (CLASSTYPE_EMPTY_P (t))
4932 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
4934 /* Set the TYPE_DECL for this type to contain the right
4935 value for DECL_OFFSET, so that we can use it as part
4936 of a COMPONENT_REF for multiple inheritance. */
4937 layout_decl (TYPE_MAIN_DECL (t), 0);
4939 /* Now fix up any virtual base class types that we left lying
4940 around. We must get these done before we try to lay out the
4941 virtual function table. As a side-effect, this will remove the
4942 base subobject fields. */
4943 layout_virtual_bases (rli, empty_base_offsets);
4945 /* Make sure that empty classes are reflected in RLI at this
4946 point. */
4947 include_empty_classes(rli);
4949 /* Make sure not to create any structures with zero size. */
4950 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
4951 place_field (rli,
4952 build_decl (FIELD_DECL, NULL_TREE, char_type_node));
4954 /* Let the back-end lay out the type. */
4955 finish_record_layout (rli, /*free_p=*/true);
4957 /* Warn about bases that can't be talked about due to ambiguity. */
4958 warn_about_ambiguous_bases (t);
4960 /* Clean up. */
4961 splay_tree_delete (empty_base_offsets);
4964 /* Returns the virtual function with which the vtable for TYPE is
4965 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
4967 static tree
4968 key_method (tree type)
4970 tree method;
4972 if (TYPE_FOR_JAVA (type)
4973 || processing_template_decl
4974 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
4975 || CLASSTYPE_INTERFACE_KNOWN (type))
4976 return NULL_TREE;
4978 for (method = TYPE_METHODS (type); method != NULL_TREE;
4979 method = TREE_CHAIN (method))
4980 if (DECL_VINDEX (method) != NULL_TREE
4981 && ! DECL_DECLARED_INLINE_P (method)
4982 && ! DECL_PURE_VIRTUAL_P (method))
4983 return method;
4985 return NULL_TREE;
4988 /* Perform processing required when the definition of T (a class type)
4989 is complete. */
4991 void
4992 finish_struct_1 (tree t)
4994 tree x;
4995 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
4996 tree virtuals = NULL_TREE;
4997 int n_fields = 0;
4998 tree vfield;
5000 if (COMPLETE_TYPE_P (t))
5002 if (IS_AGGR_TYPE (t))
5003 error ("redefinition of `%#T'", t);
5004 else
5005 abort ();
5006 popclass ();
5007 return;
5010 /* If this type was previously laid out as a forward reference,
5011 make sure we lay it out again. */
5012 TYPE_SIZE (t) = NULL_TREE;
5013 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5014 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5016 fixup_inline_methods (t);
5018 /* Make assumptions about the class; we'll reset the flags if
5019 necessary. */
5020 CLASSTYPE_EMPTY_P (t) = 1;
5021 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
5022 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
5024 /* Do end-of-class semantic processing: checking the validity of the
5025 bases and members and add implicitly generated methods. */
5026 check_bases_and_members (t);
5028 /* Find the key method */
5029 if (TYPE_CONTAINS_VPTR_P (t))
5031 CLASSTYPE_KEY_METHOD (t) = key_method (t);
5033 /* If a polymorphic class has no key method, we may emit the vtable
5034 in every translation unit where the class definition appears. */
5035 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
5036 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
5039 /* Layout the class itself. */
5040 layout_class_type (t, &virtuals);
5042 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5043 vfield = TYPE_VFIELD (t);
5044 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5046 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5048 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5049 BINFO_TYPE (primary)),
5050 20010726);
5051 /* The vtable better be at the start. */
5052 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5053 20010726);
5054 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5055 20010726);
5057 vfield = copy_decl (vfield);
5058 DECL_FIELD_CONTEXT (vfield) = t;
5059 TYPE_VFIELD (t) = vfield;
5061 else
5062 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5064 virtuals = modify_all_vtables (t, nreverse (virtuals));
5066 /* If we created a new vtbl pointer for this class, add it to the
5067 list. */
5068 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5069 CLASSTYPE_VFIELDS (t)
5070 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5072 /* If necessary, create the primary vtable for this class. */
5073 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
5075 /* We must enter these virtuals into the table. */
5076 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5077 build_primary_vtable (NULL_TREE, t);
5078 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
5079 /* Here we know enough to change the type of our virtual
5080 function table, but we will wait until later this function. */
5081 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5084 if (TYPE_CONTAINS_VPTR_P (t))
5086 int vindex;
5087 tree fn;
5089 if (TYPE_BINFO_VTABLE (t))
5090 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5091 20000116);
5092 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5093 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5094 20000116);
5096 /* Add entries for virtual functions introduced by this class. */
5097 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), virtuals);
5099 /* Set DECL_VINDEX for all functions declared in this class. */
5100 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
5101 fn;
5102 fn = TREE_CHAIN (fn),
5103 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
5104 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
5106 tree fndecl = BV_FN (fn);
5108 if (DECL_THUNK_P (fndecl))
5109 /* A thunk. We should never be calling this entry directly
5110 from this vtable -- we'd use the entry for the non
5111 thunk base function. */
5112 DECL_VINDEX (fndecl) = NULL_TREE;
5113 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
5114 DECL_VINDEX (fndecl) = build_shared_int_cst (vindex);
5118 finish_struct_bits (t);
5120 /* Complete the rtl for any static member objects of the type we're
5121 working on. */
5122 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5123 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5124 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
5125 DECL_MODE (x) = TYPE_MODE (t);
5127 /* Done with FIELDS...now decide whether to sort these for
5128 faster lookups later.
5130 We use a small number because most searches fail (succeeding
5131 ultimately as the search bores through the inheritance
5132 hierarchy), and we want this failure to occur quickly. */
5134 n_fields = count_fields (TYPE_FIELDS (t));
5135 if (n_fields > 7)
5137 struct sorted_fields_type *field_vec = ggc_alloc (sizeof (struct sorted_fields_type)
5138 + n_fields * sizeof (tree));
5139 field_vec->len = n_fields;
5140 add_fields_to_record_type (TYPE_FIELDS (t), field_vec, 0);
5141 qsort (field_vec->elts, n_fields, sizeof (tree),
5142 field_decl_cmp);
5143 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5144 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5145 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5148 if (TYPE_HAS_CONSTRUCTOR (t))
5150 tree vfields = CLASSTYPE_VFIELDS (t);
5152 for (vfields = CLASSTYPE_VFIELDS (t);
5153 vfields; vfields = TREE_CHAIN (vfields))
5154 /* Mark the fact that constructor for T could affect anybody
5155 inheriting from T who wants to initialize vtables for
5156 VFIELDS's type. */
5157 if (VF_BINFO_VALUE (vfields))
5158 TREE_ADDRESSABLE (vfields) = 1;
5161 /* Make the rtl for any new vtables we have created, and unmark
5162 the base types we marked. */
5163 finish_vtbls (t);
5165 /* Build the VTT for T. */
5166 build_vtt (t);
5168 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5169 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5170 warning ("`%#T' has virtual functions but non-virtual destructor", t);
5172 complete_vars (t);
5174 if (warn_overloaded_virtual)
5175 warn_hidden (t);
5177 maybe_suppress_debug_info (t);
5179 dump_class_hierarchy (t);
5181 /* Finish debugging output for this type. */
5182 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5185 /* When T was built up, the member declarations were added in reverse
5186 order. Rearrange them to declaration order. */
5188 void
5189 unreverse_member_declarations (tree t)
5191 tree next;
5192 tree prev;
5193 tree x;
5195 /* The following lists are all in reverse order. Put them in
5196 declaration order now. */
5197 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5198 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
5200 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5201 reverse order, so we can't just use nreverse. */
5202 prev = NULL_TREE;
5203 for (x = TYPE_FIELDS (t);
5204 x && TREE_CODE (x) != TYPE_DECL;
5205 x = next)
5207 next = TREE_CHAIN (x);
5208 TREE_CHAIN (x) = prev;
5209 prev = x;
5211 if (prev)
5213 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5214 if (prev)
5215 TYPE_FIELDS (t) = prev;
5219 tree
5220 finish_struct (tree t, tree attributes)
5222 location_t saved_loc = input_location;
5224 /* Now that we've got all the field declarations, reverse everything
5225 as necessary. */
5226 unreverse_member_declarations (t);
5228 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5230 /* Nadger the current location so that diagnostics point to the start of
5231 the struct, not the end. */
5232 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
5234 if (processing_template_decl)
5236 finish_struct_methods (t);
5237 TYPE_SIZE (t) = bitsize_zero_node;
5239 else
5240 finish_struct_1 (t);
5242 input_location = saved_loc;
5244 TYPE_BEING_DEFINED (t) = 0;
5246 if (current_class_type)
5247 popclass ();
5248 else
5249 error ("trying to finish struct, but kicked out due to previous parse errors");
5251 if (processing_template_decl && at_function_scope_p ())
5252 add_stmt (build_min (TAG_DEFN, t));
5254 return t;
5257 /* Return the dynamic type of INSTANCE, if known.
5258 Used to determine whether the virtual function table is needed
5259 or not.
5261 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5262 of our knowledge of its type. *NONNULL should be initialized
5263 before this function is called. */
5265 static tree
5266 fixed_type_or_null (tree instance, int* nonnull, int* cdtorp)
5268 switch (TREE_CODE (instance))
5270 case INDIRECT_REF:
5271 if (POINTER_TYPE_P (TREE_TYPE (instance)))
5272 return NULL_TREE;
5273 else
5274 return fixed_type_or_null (TREE_OPERAND (instance, 0),
5275 nonnull, cdtorp);
5277 case CALL_EXPR:
5278 /* This is a call to a constructor, hence it's never zero. */
5279 if (TREE_HAS_CONSTRUCTOR (instance))
5281 if (nonnull)
5282 *nonnull = 1;
5283 return TREE_TYPE (instance);
5285 return NULL_TREE;
5287 case SAVE_EXPR:
5288 /* This is a call to a constructor, hence it's never zero. */
5289 if (TREE_HAS_CONSTRUCTOR (instance))
5291 if (nonnull)
5292 *nonnull = 1;
5293 return TREE_TYPE (instance);
5295 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5297 case RTL_EXPR:
5298 return NULL_TREE;
5300 case PLUS_EXPR:
5301 case MINUS_EXPR:
5302 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5303 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5304 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5305 /* Propagate nonnull. */
5306 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5307 return NULL_TREE;
5309 case NOP_EXPR:
5310 case CONVERT_EXPR:
5311 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5313 case ADDR_EXPR:
5314 if (nonnull)
5315 *nonnull = 1;
5316 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5318 case COMPONENT_REF:
5319 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5321 case VAR_DECL:
5322 case FIELD_DECL:
5323 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5324 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5326 if (nonnull)
5327 *nonnull = 1;
5328 return TREE_TYPE (TREE_TYPE (instance));
5330 /* fall through... */
5331 case TARGET_EXPR:
5332 case PARM_DECL:
5333 case RESULT_DECL:
5334 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5336 if (nonnull)
5337 *nonnull = 1;
5338 return TREE_TYPE (instance);
5340 else if (instance == current_class_ptr)
5342 if (nonnull)
5343 *nonnull = 1;
5345 /* if we're in a ctor or dtor, we know our type. */
5346 if (DECL_LANG_SPECIFIC (current_function_decl)
5347 && (DECL_CONSTRUCTOR_P (current_function_decl)
5348 || DECL_DESTRUCTOR_P (current_function_decl)))
5350 if (cdtorp)
5351 *cdtorp = 1;
5352 return TREE_TYPE (TREE_TYPE (instance));
5355 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5357 /* Reference variables should be references to objects. */
5358 if (nonnull)
5359 *nonnull = 1;
5361 /* DECL_VAR_MARKED_P is used to prevent recursion; a
5362 variable's initializer may refer to the variable
5363 itself. */
5364 if (TREE_CODE (instance) == VAR_DECL
5365 && DECL_INITIAL (instance)
5366 && !DECL_VAR_MARKED_P (instance))
5368 tree type;
5369 DECL_VAR_MARKED_P (instance) = 1;
5370 type = fixed_type_or_null (DECL_INITIAL (instance),
5371 nonnull, cdtorp);
5372 DECL_VAR_MARKED_P (instance) = 0;
5373 return type;
5376 return NULL_TREE;
5378 default:
5379 return NULL_TREE;
5383 /* Return nonzero if the dynamic type of INSTANCE is known, and
5384 equivalent to the static type. We also handle the case where
5385 INSTANCE is really a pointer. Return negative if this is a
5386 ctor/dtor. There the dynamic type is known, but this might not be
5387 the most derived base of the original object, and hence virtual
5388 bases may not be layed out according to this type.
5390 Used to determine whether the virtual function table is needed
5391 or not.
5393 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5394 of our knowledge of its type. *NONNULL should be initialized
5395 before this function is called. */
5398 resolves_to_fixed_type_p (tree instance, int* nonnull)
5400 tree t = TREE_TYPE (instance);
5401 int cdtorp = 0;
5403 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5404 if (fixed == NULL_TREE)
5405 return 0;
5406 if (POINTER_TYPE_P (t))
5407 t = TREE_TYPE (t);
5408 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5409 return 0;
5410 return cdtorp ? -1 : 1;
5414 void
5415 init_class_processing (void)
5417 current_class_depth = 0;
5418 current_class_stack_size = 10;
5419 current_class_stack
5420 = xmalloc (current_class_stack_size * sizeof (struct class_stack_node));
5421 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5423 ridpointers[(int) RID_PUBLIC] = access_public_node;
5424 ridpointers[(int) RID_PRIVATE] = access_private_node;
5425 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5428 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
5429 appropriate for TYPE.
5431 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5432 which can be seen locally to the class. They are shadowed by
5433 any subsequent local declaration (including parameter names).
5435 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5436 which have static meaning (i.e., static members, static
5437 member functions, enum declarations, etc).
5439 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5440 which can be seen locally to the class (as in 1), but
5441 know that we are doing this for declaration purposes
5442 (i.e. friend foo::bar (int)).
5444 So that we may avoid calls to lookup_name, we cache the _TYPE
5445 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5447 For multiple inheritance, we perform a two-pass depth-first search
5448 of the type lattice. The first pass performs a pre-order search,
5449 marking types after the type has had its fields installed in
5450 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5451 unmarks the marked types. If a field or member function name
5452 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5453 that name becomes `error_mark_node'. */
5455 void
5456 pushclass (tree type, bool modify)
5458 type = TYPE_MAIN_VARIANT (type);
5460 /* Make sure there is enough room for the new entry on the stack. */
5461 if (current_class_depth + 1 >= current_class_stack_size)
5463 current_class_stack_size *= 2;
5464 current_class_stack
5465 = xrealloc (current_class_stack,
5466 current_class_stack_size
5467 * sizeof (struct class_stack_node));
5470 /* Insert a new entry on the class stack. */
5471 current_class_stack[current_class_depth].name = current_class_name;
5472 current_class_stack[current_class_depth].type = current_class_type;
5473 current_class_stack[current_class_depth].access = current_access_specifier;
5474 current_class_stack[current_class_depth].names_used = 0;
5475 current_class_depth++;
5477 /* Now set up the new type. */
5478 current_class_name = TYPE_NAME (type);
5479 if (TREE_CODE (current_class_name) == TYPE_DECL)
5480 current_class_name = DECL_NAME (current_class_name);
5481 current_class_type = type;
5483 /* By default, things in classes are private, while things in
5484 structures or unions are public. */
5485 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5486 ? access_private_node
5487 : access_public_node);
5489 if (previous_class_type != NULL_TREE
5490 && (type != previous_class_type
5491 || !COMPLETE_TYPE_P (previous_class_type))
5492 && current_class_depth == 1)
5494 /* Forcibly remove any old class remnants. */
5495 invalidate_class_lookup_cache ();
5498 /* If we're about to enter a nested class, clear
5499 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5500 if (modify && current_class_depth > 1)
5501 clear_identifier_class_values ();
5503 pushlevel_class ();
5505 if (modify)
5507 if (type != previous_class_type || current_class_depth > 1)
5508 push_class_decls (type);
5509 else
5511 tree item;
5513 /* We are re-entering the same class we just left, so we
5514 don't have to search the whole inheritance matrix to find
5515 all the decls to bind again. Instead, we install the
5516 cached class_shadowed list, and walk through it binding
5517 names and setting up IDENTIFIER_TYPE_VALUEs. */
5518 set_class_shadows (previous_class_values);
5519 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5521 tree id = TREE_PURPOSE (item);
5522 tree decl = TREE_TYPE (item);
5524 push_class_binding (id, decl);
5525 if (TREE_CODE (decl) == TYPE_DECL)
5526 set_identifier_type_value (id, TREE_TYPE (decl));
5528 unuse_fields (type);
5531 cxx_remember_type_decls (CLASSTYPE_NESTED_UTDS (type));
5535 /* When we exit a toplevel class scope, we save the
5536 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5537 reenter the class. Here, we've entered some other class, so we
5538 must invalidate our cache. */
5540 void
5541 invalidate_class_lookup_cache (void)
5543 tree t;
5545 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5546 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5547 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5549 previous_class_values = NULL_TREE;
5550 previous_class_type = NULL_TREE;
5553 /* Get out of the current class scope. If we were in a class scope
5554 previously, that is the one popped to. */
5556 void
5557 popclass (void)
5559 poplevel_class ();
5560 pop_class_decls ();
5562 current_class_depth--;
5563 current_class_name = current_class_stack[current_class_depth].name;
5564 current_class_type = current_class_stack[current_class_depth].type;
5565 current_access_specifier = current_class_stack[current_class_depth].access;
5566 if (current_class_stack[current_class_depth].names_used)
5567 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5570 /* Returns 1 if current_class_type is either T or a nested type of T.
5571 We start looking from 1 because entry 0 is from global scope, and has
5572 no type. */
5575 currently_open_class (tree t)
5577 int i;
5578 if (current_class_type && same_type_p (t, current_class_type))
5579 return 1;
5580 for (i = 1; i < current_class_depth; ++i)
5581 if (current_class_stack[i].type
5582 && same_type_p (current_class_stack [i].type, t))
5583 return 1;
5584 return 0;
5587 /* If either current_class_type or one of its enclosing classes are derived
5588 from T, return the appropriate type. Used to determine how we found
5589 something via unqualified lookup. */
5591 tree
5592 currently_open_derived_class (tree t)
5594 int i;
5596 /* The bases of a dependent type are unknown. */
5597 if (dependent_type_p (t))
5598 return NULL_TREE;
5600 if (DERIVED_FROM_P (t, current_class_type))
5601 return current_class_type;
5603 for (i = current_class_depth - 1; i > 0; --i)
5604 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5605 return current_class_stack[i].type;
5607 return NULL_TREE;
5610 /* When entering a class scope, all enclosing class scopes' names with
5611 static meaning (static variables, static functions, types and
5612 enumerators) have to be visible. This recursive function calls
5613 pushclass for all enclosing class contexts until global or a local
5614 scope is reached. TYPE is the enclosed class. */
5616 void
5617 push_nested_class (tree type)
5619 tree context;
5621 /* A namespace might be passed in error cases, like A::B:C. */
5622 if (type == NULL_TREE
5623 || type == error_mark_node
5624 || TREE_CODE (type) == NAMESPACE_DECL
5625 || ! IS_AGGR_TYPE (type)
5626 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5627 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5628 return;
5630 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5632 if (context && CLASS_TYPE_P (context))
5633 push_nested_class (context);
5634 pushclass (type, true);
5637 /* Undoes a push_nested_class call. */
5639 void
5640 pop_nested_class (void)
5642 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5644 popclass ();
5645 if (context && CLASS_TYPE_P (context))
5646 pop_nested_class ();
5649 /* Returns the number of extern "LANG" blocks we are nested within. */
5652 current_lang_depth (void)
5654 return VARRAY_ACTIVE_SIZE (current_lang_base);
5657 /* Set global variables CURRENT_LANG_NAME to appropriate value
5658 so that behavior of name-mangling machinery is correct. */
5660 void
5661 push_lang_context (tree name)
5663 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5665 if (name == lang_name_cplusplus)
5667 current_lang_name = name;
5669 else if (name == lang_name_java)
5671 current_lang_name = name;
5672 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5673 (See record_builtin_java_type in decl.c.) However, that causes
5674 incorrect debug entries if these types are actually used.
5675 So we re-enable debug output after extern "Java". */
5676 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5677 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5678 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5679 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5680 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5681 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5682 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5683 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5685 else if (name == lang_name_c)
5687 current_lang_name = name;
5689 else
5690 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5693 /* Get out of the current language scope. */
5695 void
5696 pop_lang_context (void)
5698 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5699 VARRAY_POP (current_lang_base);
5702 /* Type instantiation routines. */
5704 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5705 matches the TARGET_TYPE. If there is no satisfactory match, return
5706 error_mark_node, and issue an error message if COMPLAIN is
5707 nonzero. Permit pointers to member function if PTRMEM is nonzero.
5708 If TEMPLATE_ONLY, the name of the overloaded function
5709 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5710 template arguments. */
5712 static tree
5713 resolve_address_of_overloaded_function (tree target_type,
5714 tree overload,
5715 int complain,
5716 int ptrmem,
5717 int template_only,
5718 tree explicit_targs)
5720 /* Here's what the standard says:
5722 [over.over]
5724 If the name is a function template, template argument deduction
5725 is done, and if the argument deduction succeeds, the deduced
5726 arguments are used to generate a single template function, which
5727 is added to the set of overloaded functions considered.
5729 Non-member functions and static member functions match targets of
5730 type "pointer-to-function" or "reference-to-function." Nonstatic
5731 member functions match targets of type "pointer-to-member
5732 function;" the function type of the pointer to member is used to
5733 select the member function from the set of overloaded member
5734 functions. If a nonstatic member function is selected, the
5735 reference to the overloaded function name is required to have the
5736 form of a pointer to member as described in 5.3.1.
5738 If more than one function is selected, any template functions in
5739 the set are eliminated if the set also contains a non-template
5740 function, and any given template function is eliminated if the
5741 set contains a second template function that is more specialized
5742 than the first according to the partial ordering rules 14.5.5.2.
5743 After such eliminations, if any, there shall remain exactly one
5744 selected function. */
5746 int is_ptrmem = 0;
5747 int is_reference = 0;
5748 /* We store the matches in a TREE_LIST rooted here. The functions
5749 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5750 interoperability with most_specialized_instantiation. */
5751 tree matches = NULL_TREE;
5752 tree fn;
5754 /* By the time we get here, we should be seeing only real
5755 pointer-to-member types, not the internal POINTER_TYPE to
5756 METHOD_TYPE representation. */
5757 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5758 && (TREE_CODE (TREE_TYPE (target_type))
5759 == METHOD_TYPE)), 0);
5761 if (TREE_CODE (overload) == COMPONENT_REF)
5762 overload = TREE_OPERAND (overload, 1);
5764 /* Check that the TARGET_TYPE is reasonable. */
5765 if (TYPE_PTRFN_P (target_type))
5766 /* This is OK. */;
5767 else if (TYPE_PTRMEMFUNC_P (target_type))
5768 /* This is OK, too. */
5769 is_ptrmem = 1;
5770 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5772 /* This is OK, too. This comes from a conversion to reference
5773 type. */
5774 target_type = build_reference_type (target_type);
5775 is_reference = 1;
5777 else
5779 if (complain)
5780 error ("\
5781 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5782 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5783 return error_mark_node;
5786 /* If we can find a non-template function that matches, we can just
5787 use it. There's no point in generating template instantiations
5788 if we're just going to throw them out anyhow. But, of course, we
5789 can only do this when we don't *need* a template function. */
5790 if (!template_only)
5792 tree fns;
5794 for (fns = overload; fns; fns = OVL_NEXT (fns))
5796 tree fn = OVL_CURRENT (fns);
5797 tree fntype;
5799 if (TREE_CODE (fn) == TEMPLATE_DECL)
5800 /* We're not looking for templates just yet. */
5801 continue;
5803 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5804 != is_ptrmem)
5805 /* We're looking for a non-static member, and this isn't
5806 one, or vice versa. */
5807 continue;
5809 /* See if there's a match. */
5810 fntype = TREE_TYPE (fn);
5811 if (is_ptrmem)
5812 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5813 else if (!is_reference)
5814 fntype = build_pointer_type (fntype);
5816 if (can_convert_arg (target_type, fntype, fn))
5817 matches = tree_cons (fn, NULL_TREE, matches);
5821 /* Now, if we've already got a match (or matches), there's no need
5822 to proceed to the template functions. But, if we don't have a
5823 match we need to look at them, too. */
5824 if (!matches)
5826 tree target_fn_type;
5827 tree target_arg_types;
5828 tree target_ret_type;
5829 tree fns;
5831 if (is_ptrmem)
5832 target_fn_type
5833 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5834 else
5835 target_fn_type = TREE_TYPE (target_type);
5836 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5837 target_ret_type = TREE_TYPE (target_fn_type);
5839 /* Never do unification on the 'this' parameter. */
5840 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5841 target_arg_types = TREE_CHAIN (target_arg_types);
5843 for (fns = overload; fns; fns = OVL_NEXT (fns))
5845 tree fn = OVL_CURRENT (fns);
5846 tree instantiation;
5847 tree instantiation_type;
5848 tree targs;
5850 if (TREE_CODE (fn) != TEMPLATE_DECL)
5851 /* We're only looking for templates. */
5852 continue;
5854 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5855 != is_ptrmem)
5856 /* We're not looking for a non-static member, and this is
5857 one, or vice versa. */
5858 continue;
5860 /* Try to do argument deduction. */
5861 targs = make_tree_vec (DECL_NTPARMS (fn));
5862 if (fn_type_unification (fn, explicit_targs, targs,
5863 target_arg_types, target_ret_type,
5864 DEDUCE_EXACT, -1) != 0)
5865 /* Argument deduction failed. */
5866 continue;
5868 /* Instantiate the template. */
5869 instantiation = instantiate_template (fn, targs,
5870 complain ? tf_error : tf_none);
5871 if (instantiation == error_mark_node)
5872 /* Instantiation failed. */
5873 continue;
5875 /* See if there's a match. */
5876 instantiation_type = TREE_TYPE (instantiation);
5877 if (is_ptrmem)
5878 instantiation_type =
5879 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5880 else if (!is_reference)
5881 instantiation_type = build_pointer_type (instantiation_type);
5882 if (can_convert_arg (target_type, instantiation_type, instantiation))
5883 matches = tree_cons (instantiation, fn, matches);
5886 /* Now, remove all but the most specialized of the matches. */
5887 if (matches)
5889 tree match = most_specialized_instantiation (matches);
5891 if (match != error_mark_node)
5892 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5896 /* Now we should have exactly one function in MATCHES. */
5897 if (matches == NULL_TREE)
5899 /* There were *no* matches. */
5900 if (complain)
5902 error ("no matches converting function `%D' to type `%#T'",
5903 DECL_NAME (OVL_FUNCTION (overload)),
5904 target_type);
5906 /* print_candidates expects a chain with the functions in
5907 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5908 so why be clever?). */
5909 for (; overload; overload = OVL_NEXT (overload))
5910 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5911 matches);
5913 print_candidates (matches);
5915 return error_mark_node;
5917 else if (TREE_CHAIN (matches))
5919 /* There were too many matches. */
5921 if (complain)
5923 tree match;
5925 error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5926 DECL_NAME (OVL_FUNCTION (overload)),
5927 target_type);
5929 /* Since print_candidates expects the functions in the
5930 TREE_VALUE slot, we flip them here. */
5931 for (match = matches; match; match = TREE_CHAIN (match))
5932 TREE_VALUE (match) = TREE_PURPOSE (match);
5934 print_candidates (matches);
5937 return error_mark_node;
5940 /* Good, exactly one match. Now, convert it to the correct type. */
5941 fn = TREE_PURPOSE (matches);
5943 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5944 && !ptrmem && !flag_ms_extensions)
5946 static int explained;
5948 if (!complain)
5949 return error_mark_node;
5951 pedwarn ("assuming pointer to member `%D'", fn);
5952 if (!explained)
5954 pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
5955 explained = 1;
5958 mark_used (fn);
5960 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5961 return build_unary_op (ADDR_EXPR, fn, 0);
5962 else
5964 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5965 will mark the function as addressed, but here we must do it
5966 explicitly. */
5967 cxx_mark_addressable (fn);
5969 return fn;
5973 /* This function will instantiate the type of the expression given in
5974 RHS to match the type of LHSTYPE. If errors exist, then return
5975 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
5976 we complain on errors. If we are not complaining, never modify rhs,
5977 as overload resolution wants to try many possible instantiations, in
5978 the hope that at least one will work.
5980 For non-recursive calls, LHSTYPE should be a function, pointer to
5981 function, or a pointer to member function. */
5983 tree
5984 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
5986 int complain = (flags & tf_error);
5987 int allow_ptrmem = flags & tf_ptrmem_ok;
5989 flags &= ~tf_ptrmem_ok;
5991 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5993 if (complain)
5994 error ("not enough type information");
5995 return error_mark_node;
5998 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6000 if (same_type_p (lhstype, TREE_TYPE (rhs)))
6001 return rhs;
6002 if (flag_ms_extensions
6003 && TYPE_PTRMEMFUNC_P (lhstype)
6004 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6005 /* Microsoft allows `A::f' to be resolved to a
6006 pointer-to-member. */
6008 else
6010 if (complain)
6011 error ("argument of type `%T' does not match `%T'",
6012 TREE_TYPE (rhs), lhstype);
6013 return error_mark_node;
6017 if (TREE_CODE (rhs) == BASELINK)
6018 rhs = BASELINK_FUNCTIONS (rhs);
6020 /* We don't overwrite rhs if it is an overloaded function.
6021 Copying it would destroy the tree link. */
6022 if (TREE_CODE (rhs) != OVERLOAD)
6023 rhs = copy_node (rhs);
6025 /* This should really only be used when attempting to distinguish
6026 what sort of a pointer to function we have. For now, any
6027 arithmetic operation which is not supported on pointers
6028 is rejected as an error. */
6030 switch (TREE_CODE (rhs))
6032 case TYPE_EXPR:
6033 case CONVERT_EXPR:
6034 case SAVE_EXPR:
6035 case CONSTRUCTOR:
6036 case BUFFER_REF:
6037 abort ();
6038 return error_mark_node;
6040 case INDIRECT_REF:
6041 case ARRAY_REF:
6043 tree new_rhs;
6045 new_rhs = instantiate_type (build_pointer_type (lhstype),
6046 TREE_OPERAND (rhs, 0), flags);
6047 if (new_rhs == error_mark_node)
6048 return error_mark_node;
6050 TREE_TYPE (rhs) = lhstype;
6051 TREE_OPERAND (rhs, 0) = new_rhs;
6052 return rhs;
6055 case NOP_EXPR:
6056 rhs = copy_node (TREE_OPERAND (rhs, 0));
6057 TREE_TYPE (rhs) = unknown_type_node;
6058 return instantiate_type (lhstype, rhs, flags);
6060 case COMPONENT_REF:
6061 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6063 case OFFSET_REF:
6064 rhs = TREE_OPERAND (rhs, 1);
6065 if (BASELINK_P (rhs))
6066 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs),
6067 flags | allow_ptrmem);
6069 /* This can happen if we are forming a pointer-to-member for a
6070 member template. */
6071 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6073 /* Fall through. */
6075 case TEMPLATE_ID_EXPR:
6077 tree fns = TREE_OPERAND (rhs, 0);
6078 tree args = TREE_OPERAND (rhs, 1);
6080 return
6081 resolve_address_of_overloaded_function (lhstype,
6082 fns,
6083 complain,
6084 allow_ptrmem,
6085 /*template_only=*/1,
6086 args);
6089 case OVERLOAD:
6090 case FUNCTION_DECL:
6091 return
6092 resolve_address_of_overloaded_function (lhstype,
6093 rhs,
6094 complain,
6095 allow_ptrmem,
6096 /*template_only=*/0,
6097 /*explicit_targs=*/NULL_TREE);
6099 case TREE_LIST:
6100 /* Now we should have a baselink. */
6101 my_friendly_assert (BASELINK_P (rhs), 990412);
6103 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
6105 case CALL_EXPR:
6106 /* This is too hard for now. */
6107 abort ();
6108 return error_mark_node;
6110 case PLUS_EXPR:
6111 case MINUS_EXPR:
6112 case COMPOUND_EXPR:
6113 TREE_OPERAND (rhs, 0)
6114 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6115 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6116 return error_mark_node;
6117 TREE_OPERAND (rhs, 1)
6118 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6119 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6120 return error_mark_node;
6122 TREE_TYPE (rhs) = lhstype;
6123 return rhs;
6125 case MULT_EXPR:
6126 case TRUNC_DIV_EXPR:
6127 case FLOOR_DIV_EXPR:
6128 case CEIL_DIV_EXPR:
6129 case ROUND_DIV_EXPR:
6130 case RDIV_EXPR:
6131 case TRUNC_MOD_EXPR:
6132 case FLOOR_MOD_EXPR:
6133 case CEIL_MOD_EXPR:
6134 case ROUND_MOD_EXPR:
6135 case FIX_ROUND_EXPR:
6136 case FIX_FLOOR_EXPR:
6137 case FIX_CEIL_EXPR:
6138 case FIX_TRUNC_EXPR:
6139 case FLOAT_EXPR:
6140 case NEGATE_EXPR:
6141 case ABS_EXPR:
6142 case MAX_EXPR:
6143 case MIN_EXPR:
6144 case FFS_EXPR:
6146 case BIT_AND_EXPR:
6147 case BIT_IOR_EXPR:
6148 case BIT_XOR_EXPR:
6149 case LSHIFT_EXPR:
6150 case RSHIFT_EXPR:
6151 case LROTATE_EXPR:
6152 case RROTATE_EXPR:
6154 case PREINCREMENT_EXPR:
6155 case PREDECREMENT_EXPR:
6156 case POSTINCREMENT_EXPR:
6157 case POSTDECREMENT_EXPR:
6158 if (complain)
6159 error ("invalid operation on uninstantiated type");
6160 return error_mark_node;
6162 case TRUTH_AND_EXPR:
6163 case TRUTH_OR_EXPR:
6164 case TRUTH_XOR_EXPR:
6165 case LT_EXPR:
6166 case LE_EXPR:
6167 case GT_EXPR:
6168 case GE_EXPR:
6169 case EQ_EXPR:
6170 case NE_EXPR:
6171 case TRUTH_ANDIF_EXPR:
6172 case TRUTH_ORIF_EXPR:
6173 case TRUTH_NOT_EXPR:
6174 if (complain)
6175 error ("not enough type information");
6176 return error_mark_node;
6178 case COND_EXPR:
6179 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6181 if (complain)
6182 error ("not enough type information");
6183 return error_mark_node;
6185 TREE_OPERAND (rhs, 1)
6186 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6187 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6188 return error_mark_node;
6189 TREE_OPERAND (rhs, 2)
6190 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6191 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6192 return error_mark_node;
6194 TREE_TYPE (rhs) = lhstype;
6195 return rhs;
6197 case MODIFY_EXPR:
6198 TREE_OPERAND (rhs, 1)
6199 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6200 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6201 return error_mark_node;
6203 TREE_TYPE (rhs) = lhstype;
6204 return rhs;
6206 case ADDR_EXPR:
6208 if (PTRMEM_OK_P (rhs))
6209 flags |= tf_ptrmem_ok;
6211 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6213 case ENTRY_VALUE_EXPR:
6214 abort ();
6215 return error_mark_node;
6217 case ERROR_MARK:
6218 return error_mark_node;
6220 default:
6221 abort ();
6222 return error_mark_node;
6226 /* Return the name of the virtual function pointer field
6227 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6228 this may have to look back through base types to find the
6229 ultimate field name. (For single inheritance, these could
6230 all be the same name. Who knows for multiple inheritance). */
6232 static tree
6233 get_vfield_name (tree type)
6235 tree binfo = TYPE_BINFO (type);
6236 char *buf;
6238 while (BINFO_BASETYPES (binfo)
6239 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6240 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6241 binfo = BINFO_BASETYPE (binfo, 0);
6243 type = BINFO_TYPE (binfo);
6244 buf = alloca (sizeof (VFIELD_NAME_FORMAT) + TYPE_NAME_LENGTH (type) + 2);
6245 sprintf (buf, VFIELD_NAME_FORMAT,
6246 IDENTIFIER_POINTER (constructor_name (type)));
6247 return get_identifier (buf);
6250 void
6251 print_class_statistics (void)
6253 #ifdef GATHER_STATISTICS
6254 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6255 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6256 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6257 n_build_method_call, n_inner_fields_searched);
6258 if (n_vtables)
6260 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6261 n_vtables, n_vtable_searches);
6262 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6263 n_vtable_entries, n_vtable_elems);
6265 #endif
6268 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6269 according to [class]:
6270 The class-name is also inserted
6271 into the scope of the class itself. For purposes of access checking,
6272 the inserted class name is treated as if it were a public member name. */
6274 void
6275 build_self_reference (void)
6277 tree name = constructor_name (current_class_type);
6278 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6279 tree saved_cas;
6281 DECL_NONLOCAL (value) = 1;
6282 DECL_CONTEXT (value) = current_class_type;
6283 DECL_ARTIFICIAL (value) = 1;
6284 SET_DECL_SELF_REFERENCE_P (value);
6286 if (processing_template_decl)
6287 value = push_template_decl (value);
6289 saved_cas = current_access_specifier;
6290 current_access_specifier = access_public_node;
6291 finish_member_declaration (value);
6292 current_access_specifier = saved_cas;
6295 /* Returns 1 if TYPE contains only padding bytes. */
6298 is_empty_class (tree type)
6300 if (type == error_mark_node)
6301 return 0;
6303 if (! IS_AGGR_TYPE (type))
6304 return 0;
6306 /* In G++ 3.2, whether or not a class was empty was determined by
6307 looking at its size. */
6308 if (abi_version_at_least (2))
6309 return CLASSTYPE_EMPTY_P (type);
6310 else
6311 return integer_zerop (CLASSTYPE_SIZE (type));
6314 /* Returns true if TYPE contains an empty class. */
6316 static bool
6317 contains_empty_class_p (tree type)
6319 if (is_empty_class (type))
6320 return true;
6321 if (CLASS_TYPE_P (type))
6323 tree field;
6324 int i;
6326 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
6327 if (contains_empty_class_p (TYPE_BINFO_BASETYPE (type, i)))
6328 return true;
6329 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6330 if (TREE_CODE (field) == FIELD_DECL
6331 && !DECL_ARTIFICIAL (field)
6332 && is_empty_class (TREE_TYPE (field)))
6333 return true;
6335 else if (TREE_CODE (type) == ARRAY_TYPE)
6336 return contains_empty_class_p (TREE_TYPE (type));
6337 return false;
6340 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6341 a *_TYPE node. NODE can also be a local class. */
6343 tree
6344 get_enclosing_class (tree type)
6346 tree node = type;
6348 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6350 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6352 case 'd':
6353 node = DECL_CONTEXT (node);
6354 break;
6356 case 't':
6357 if (node != type)
6358 return node;
6359 node = TYPE_CONTEXT (node);
6360 break;
6362 default:
6363 abort ();
6366 return NULL_TREE;
6369 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6372 is_base_of_enclosing_class (tree base, tree type)
6374 while (type)
6376 if (lookup_base (type, base, ba_any, NULL))
6377 return 1;
6379 type = get_enclosing_class (type);
6381 return 0;
6384 /* Note that NAME was looked up while the current class was being
6385 defined and that the result of that lookup was DECL. */
6387 void
6388 maybe_note_name_used_in_class (tree name, tree decl)
6390 splay_tree names_used;
6392 /* If we're not defining a class, there's nothing to do. */
6393 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6394 return;
6396 /* If there's already a binding for this NAME, then we don't have
6397 anything to worry about. */
6398 if (IDENTIFIER_CLASS_VALUE (name))
6399 return;
6401 if (!current_class_stack[current_class_depth - 1].names_used)
6402 current_class_stack[current_class_depth - 1].names_used
6403 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6404 names_used = current_class_stack[current_class_depth - 1].names_used;
6406 splay_tree_insert (names_used,
6407 (splay_tree_key) name,
6408 (splay_tree_value) decl);
6411 /* Note that NAME was declared (as DECL) in the current class. Check
6412 to see that the declaration is valid. */
6414 void
6415 note_name_declared_in_class (tree name, tree decl)
6417 splay_tree names_used;
6418 splay_tree_node n;
6420 /* Look to see if we ever used this name. */
6421 names_used
6422 = current_class_stack[current_class_depth - 1].names_used;
6423 if (!names_used)
6424 return;
6426 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6427 if (n)
6429 /* [basic.scope.class]
6431 A name N used in a class S shall refer to the same declaration
6432 in its context and when re-evaluated in the completed scope of
6433 S. */
6434 error ("declaration of `%#D'", decl);
6435 cp_error_at ("changes meaning of `%D' from `%+#D'",
6436 DECL_NAME (OVL_CURRENT (decl)),
6437 (tree) n->value);
6441 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6442 Secondary vtables are merged with primary vtables; this function
6443 will return the VAR_DECL for the primary vtable. */
6445 tree
6446 get_vtbl_decl_for_binfo (tree binfo)
6448 tree decl;
6450 decl = BINFO_VTABLE (binfo);
6451 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6453 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6454 2000403);
6455 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6457 if (decl)
6458 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6459 return decl;
6463 /* Returns the binfo for the primary base of BINFO. If the resulting
6464 BINFO is a virtual base, and it is inherited elsewhere in the
6465 hierarchy, then the returned binfo might not be the primary base of
6466 BINFO in the complete object. Check BINFO_PRIMARY_P or
6467 BINFO_LOST_PRIMARY_P to be sure. */
6469 tree
6470 get_primary_binfo (tree binfo)
6472 tree primary_base;
6473 tree result;
6475 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6476 if (!primary_base)
6477 return NULL_TREE;
6479 result = copied_binfo (primary_base, binfo);
6480 return result;
6483 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
6485 static int
6486 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
6488 if (!indented_p)
6489 fprintf (stream, "%*s", indent, "");
6490 return 1;
6493 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
6494 INDENT should be zero when called from the top level; it is
6495 incremented recursively. IGO indicates the next expected BINFO in
6496 inheritance graph ordering. */
6498 static tree
6499 dump_class_hierarchy_r (FILE *stream,
6500 int flags,
6501 tree binfo,
6502 tree igo,
6503 int indent)
6505 int indented = 0;
6506 tree base_binfos;
6508 indented = maybe_indent_hierarchy (stream, indent, 0);
6509 fprintf (stream, "%s (0x%lx) ",
6510 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6511 (unsigned long) binfo);
6512 if (binfo != igo)
6514 fprintf (stream, "alternative-path\n");
6515 return igo;
6517 igo = TREE_CHAIN (binfo);
6519 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6520 tree_low_cst (BINFO_OFFSET (binfo), 0));
6521 if (is_empty_class (BINFO_TYPE (binfo)))
6522 fprintf (stream, " empty");
6523 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6524 fprintf (stream, " nearly-empty");
6525 if (TREE_VIA_VIRTUAL (binfo))
6526 fprintf (stream, " virtual");
6527 fprintf (stream, "\n");
6529 indented = 0;
6530 if (BINFO_PRIMARY_BASE_OF (binfo))
6532 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6533 fprintf (stream, " primary-for %s (0x%lx)",
6534 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6535 TFF_PLAIN_IDENTIFIER),
6536 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6538 if (BINFO_LOST_PRIMARY_P (binfo))
6540 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6541 fprintf (stream, " lost-primary");
6543 if (indented)
6544 fprintf (stream, "\n");
6546 if (!(flags & TDF_SLIM))
6548 int indented = 0;
6550 if (BINFO_SUBVTT_INDEX (binfo))
6552 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6553 fprintf (stream, " subvttidx=%s",
6554 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6555 TFF_PLAIN_IDENTIFIER));
6557 if (BINFO_VPTR_INDEX (binfo))
6559 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6560 fprintf (stream, " vptridx=%s",
6561 expr_as_string (BINFO_VPTR_INDEX (binfo),
6562 TFF_PLAIN_IDENTIFIER));
6564 if (BINFO_VPTR_FIELD (binfo))
6566 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6567 fprintf (stream, " vbaseoffset=%s",
6568 expr_as_string (BINFO_VPTR_FIELD (binfo),
6569 TFF_PLAIN_IDENTIFIER));
6571 if (BINFO_VTABLE (binfo))
6573 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6574 fprintf (stream, " vptr=%s",
6575 expr_as_string (BINFO_VTABLE (binfo),
6576 TFF_PLAIN_IDENTIFIER));
6579 if (indented)
6580 fprintf (stream, "\n");
6583 base_binfos = BINFO_BASETYPES (binfo);
6584 if (base_binfos)
6586 int ix, n;
6588 n = TREE_VEC_LENGTH (base_binfos);
6589 for (ix = 0; ix != n; ix++)
6591 tree base_binfo = TREE_VEC_ELT (base_binfos, ix);
6593 igo = dump_class_hierarchy_r (stream, flags, base_binfo,
6594 igo, indent + 2);
6598 return igo;
6601 /* Dump the BINFO hierarchy for T. */
6603 static void
6604 dump_class_hierarchy (tree t)
6606 int flags;
6607 FILE *stream = dump_begin (TDI_class, &flags);
6609 if (!stream)
6610 return;
6612 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6613 fprintf (stream, " size=%lu align=%lu\n",
6614 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6615 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6616 fprintf (stream, " base size=%lu base align=%lu\n",
6617 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
6618 / BITS_PER_UNIT),
6619 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
6620 / BITS_PER_UNIT));
6621 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
6622 fprintf (stream, "\n");
6623 dump_end (TDI_class, stream);
6626 static void
6627 dump_array (FILE * stream, tree decl)
6629 tree inits;
6630 int ix;
6631 HOST_WIDE_INT elt;
6632 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6634 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6635 / BITS_PER_UNIT);
6636 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6637 fprintf (stream, " %s entries",
6638 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6639 TFF_PLAIN_IDENTIFIER));
6640 fprintf (stream, "\n");
6642 for (ix = 0, inits = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
6643 inits; ix++, inits = TREE_CHAIN (inits))
6644 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
6645 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6648 static void
6649 dump_vtable (tree t, tree binfo, tree vtable)
6651 int flags;
6652 FILE *stream = dump_begin (TDI_class, &flags);
6654 if (!stream)
6655 return;
6657 if (!(flags & TDF_SLIM))
6659 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6661 fprintf (stream, "%s for %s",
6662 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6663 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6664 if (ctor_vtbl_p)
6666 if (!TREE_VIA_VIRTUAL (binfo))
6667 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6668 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6670 fprintf (stream, "\n");
6671 dump_array (stream, vtable);
6672 fprintf (stream, "\n");
6675 dump_end (TDI_class, stream);
6678 static void
6679 dump_vtt (tree t, tree vtt)
6681 int flags;
6682 FILE *stream = dump_begin (TDI_class, &flags);
6684 if (!stream)
6685 return;
6687 if (!(flags & TDF_SLIM))
6689 fprintf (stream, "VTT for %s\n",
6690 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6691 dump_array (stream, vtt);
6692 fprintf (stream, "\n");
6695 dump_end (TDI_class, stream);
6698 /* Virtual function table initialization. */
6700 /* Create all the necessary vtables for T and its base classes. */
6702 static void
6703 finish_vtbls (tree t)
6705 tree list;
6706 tree vbase;
6708 /* We lay out the primary and secondary vtables in one contiguous
6709 vtable. The primary vtable is first, followed by the non-virtual
6710 secondary vtables in inheritance graph order. */
6711 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6712 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6713 TYPE_BINFO (t), t, list);
6715 /* Then come the virtual bases, also in inheritance graph order. */
6716 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6718 if (!TREE_VIA_VIRTUAL (vbase))
6719 continue;
6720 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), t, list);
6723 if (TYPE_BINFO_VTABLE (t))
6724 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6727 /* Initialize the vtable for BINFO with the INITS. */
6729 static void
6730 initialize_vtable (tree binfo, tree inits)
6732 tree decl;
6734 layout_vtable_decl (binfo, list_length (inits));
6735 decl = get_vtbl_decl_for_binfo (binfo);
6736 initialize_array (decl, inits);
6737 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6740 /* Initialize DECL (a declaration for a namespace-scope array) with
6741 the INITS. */
6743 static void
6744 initialize_array (tree decl, tree inits)
6746 tree context;
6748 context = DECL_CONTEXT (decl);
6749 DECL_CONTEXT (decl) = NULL_TREE;
6750 DECL_INITIAL (decl) = build_constructor (NULL_TREE, inits);
6751 TREE_HAS_CONSTRUCTOR (DECL_INITIAL (decl)) = 1;
6752 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6753 DECL_CONTEXT (decl) = context;
6756 /* Build the VTT (virtual table table) for T.
6757 A class requires a VTT if it has virtual bases.
6759 This holds
6760 1 - primary virtual pointer for complete object T
6761 2 - secondary VTTs for each direct non-virtual base of T which requires a
6763 3 - secondary virtual pointers for each direct or indirect base of T which
6764 has virtual bases or is reachable via a virtual path from T.
6765 4 - secondary VTTs for each direct or indirect virtual base of T.
6767 Secondary VTTs look like complete object VTTs without part 4. */
6769 static void
6770 build_vtt (tree t)
6772 tree inits;
6773 tree type;
6774 tree vtt;
6775 tree index;
6777 /* Build up the initializers for the VTT. */
6778 inits = NULL_TREE;
6779 index = size_zero_node;
6780 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6782 /* If we didn't need a VTT, we're done. */
6783 if (!inits)
6784 return;
6786 /* Figure out the type of the VTT. */
6787 type = build_index_type (size_int (list_length (inits) - 1));
6788 type = build_cplus_array_type (const_ptr_type_node, type);
6790 /* Now, build the VTT object itself. */
6791 vtt = build_vtable (t, get_vtt_name (t), type);
6792 initialize_array (vtt, inits);
6793 /* Add the VTT to the vtables list. */
6794 TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
6795 TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
6797 dump_vtt (t, vtt);
6800 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
6801 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
6802 and CHAIN the vtable pointer for this binfo after construction is
6803 complete. VALUE can also be another BINFO, in which case we recurse. */
6805 static tree
6806 binfo_ctor_vtable (tree binfo)
6808 tree vt;
6810 while (1)
6812 vt = BINFO_VTABLE (binfo);
6813 if (TREE_CODE (vt) == TREE_LIST)
6814 vt = TREE_VALUE (vt);
6815 if (TREE_CODE (vt) == TREE_VEC)
6816 binfo = vt;
6817 else
6818 break;
6821 return vt;
6824 /* Recursively build the VTT-initializer for BINFO (which is in the
6825 hierarchy dominated by T). INITS points to the end of the initializer
6826 list to date. INDEX is the VTT index where the next element will be
6827 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
6828 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
6829 for virtual bases of T. When it is not so, we build the constructor
6830 vtables for the BINFO-in-T variant. */
6832 static tree *
6833 build_vtt_inits (tree binfo, tree t, tree* inits, tree* index)
6835 int i;
6836 tree b;
6837 tree init;
6838 tree secondary_vptrs;
6839 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
6841 /* We only need VTTs for subobjects with virtual bases. */
6842 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
6843 return inits;
6845 /* We need to use a construction vtable if this is not the primary
6846 VTT. */
6847 if (!top_level_p)
6849 build_ctor_vtbl_group (binfo, t);
6851 /* Record the offset in the VTT where this sub-VTT can be found. */
6852 BINFO_SUBVTT_INDEX (binfo) = *index;
6855 /* Add the address of the primary vtable for the complete object. */
6856 init = binfo_ctor_vtable (binfo);
6857 *inits = build_tree_list (NULL_TREE, init);
6858 inits = &TREE_CHAIN (*inits);
6859 if (top_level_p)
6861 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
6862 BINFO_VPTR_INDEX (binfo) = *index;
6864 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
6866 /* Recursively add the secondary VTTs for non-virtual bases. */
6867 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6869 b = BINFO_BASETYPE (binfo, i);
6870 if (!TREE_VIA_VIRTUAL (b))
6871 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
6872 inits, index);
6875 /* Add secondary virtual pointers for all subobjects of BINFO with
6876 either virtual bases or reachable along a virtual path, except
6877 subobjects that are non-virtual primary bases. */
6878 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
6879 TREE_TYPE (secondary_vptrs) = *index;
6880 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
6881 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
6883 dfs_walk_real (binfo,
6884 dfs_build_secondary_vptr_vtt_inits,
6885 NULL,
6886 dfs_ctor_vtable_bases_queue_p,
6887 secondary_vptrs);
6888 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
6889 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
6890 secondary_vptrs);
6892 *index = TREE_TYPE (secondary_vptrs);
6894 /* The secondary vptrs come back in reverse order. After we reverse
6895 them, and add the INITS, the last init will be the first element
6896 of the chain. */
6897 secondary_vptrs = TREE_VALUE (secondary_vptrs);
6898 if (secondary_vptrs)
6900 *inits = nreverse (secondary_vptrs);
6901 inits = &TREE_CHAIN (secondary_vptrs);
6902 my_friendly_assert (*inits == NULL_TREE, 20000517);
6905 /* Add the secondary VTTs for virtual bases. */
6906 if (top_level_p)
6907 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
6909 if (!TREE_VIA_VIRTUAL (b))
6910 continue;
6912 inits = build_vtt_inits (b, t, inits, index);
6915 if (!top_level_p)
6917 tree data = tree_cons (t, binfo, NULL_TREE);
6918 VTT_TOP_LEVEL_P (data) = 0;
6919 VTT_MARKED_BINFO_P (data) = 0;
6921 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
6922 dfs_ctor_vtable_bases_queue_p,
6923 data);
6926 return inits;
6929 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
6930 for the base in most derived. DATA is a TREE_LIST who's
6931 TREE_CHAIN is the type of the base being
6932 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
6933 flag of DATA indicates that this is a constructor vtable. The
6934 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
6936 static tree
6937 dfs_build_secondary_vptr_vtt_inits (tree binfo, void* data)
6939 tree l;
6940 tree t;
6941 tree init;
6942 tree index;
6943 int top_level_p;
6945 l = (tree) data;
6946 t = TREE_CHAIN (l);
6947 top_level_p = VTT_TOP_LEVEL_P (l);
6949 BINFO_MARKED (binfo) = 1;
6951 /* We don't care about bases that don't have vtables. */
6952 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
6953 return NULL_TREE;
6955 /* We're only interested in proper subobjects of T. */
6956 if (same_type_p (BINFO_TYPE (binfo), t))
6957 return NULL_TREE;
6959 /* We're not interested in non-virtual primary bases. */
6960 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
6961 return NULL_TREE;
6963 /* If BINFO has virtual bases or is reachable via a virtual path
6964 from T, it'll have a secondary vptr. */
6965 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
6966 && !binfo_via_virtual (binfo, t))
6967 return NULL_TREE;
6969 /* Record the index where this secondary vptr can be found. */
6970 index = TREE_TYPE (l);
6971 if (top_level_p)
6973 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
6974 BINFO_VPTR_INDEX (binfo) = index;
6976 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
6977 TYPE_SIZE_UNIT (ptr_type_node));
6979 /* Add the initializer for the secondary vptr itself. */
6980 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
6982 /* It's a primary virtual base, and this is not the construction
6983 vtable. Find the base this is primary of in the inheritance graph,
6984 and use that base's vtable now. */
6985 while (BINFO_PRIMARY_BASE_OF (binfo))
6986 binfo = BINFO_PRIMARY_BASE_OF (binfo);
6988 init = binfo_ctor_vtable (binfo);
6989 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
6991 return NULL_TREE;
6994 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
6995 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
6996 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
6997 hierarchy. */
6999 static tree
7000 dfs_ctor_vtable_bases_queue_p (tree derived, int ix,
7001 void* data)
7003 tree binfo = BINFO_BASETYPE (derived, ix);
7005 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7006 return NULL_TREE;
7007 return binfo;
7010 /* Called from build_vtt_inits via dfs_walk. After building constructor
7011 vtables and generating the sub-vtt from them, we need to restore the
7012 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7013 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7015 static tree
7016 dfs_fixup_binfo_vtbls (tree binfo, void* data)
7018 BINFO_MARKED (binfo) = 0;
7020 /* We don't care about bases that don't have vtables. */
7021 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7022 return NULL_TREE;
7024 /* If we scribbled the construction vtable vptr into BINFO, clear it
7025 out now. */
7026 if (BINFO_VTABLE (binfo)
7027 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7028 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7029 == TREE_VALUE ((tree) data)))
7030 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7032 return NULL_TREE;
7035 /* Build the construction vtable group for BINFO which is in the
7036 hierarchy dominated by T. */
7038 static void
7039 build_ctor_vtbl_group (tree binfo, tree t)
7041 tree list;
7042 tree type;
7043 tree vtbl;
7044 tree inits;
7045 tree id;
7046 tree vbase;
7048 /* See if we've already created this construction vtable group. */
7049 id = mangle_ctor_vtbl_for_type (t, binfo);
7050 if (IDENTIFIER_GLOBAL_VALUE (id))
7051 return;
7053 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7054 /* Build a version of VTBL (with the wrong type) for use in
7055 constructing the addresses of secondary vtables in the
7056 construction vtable group. */
7057 vtbl = build_vtable (t, id, ptr_type_node);
7058 list = build_tree_list (vtbl, NULL_TREE);
7059 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7060 binfo, t, list);
7062 /* Add the vtables for each of our virtual bases using the vbase in T
7063 binfo. */
7064 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7065 vbase;
7066 vbase = TREE_CHAIN (vbase))
7068 tree b;
7070 if (!TREE_VIA_VIRTUAL (vbase))
7071 continue;
7072 b = copied_binfo (vbase, binfo);
7074 accumulate_vtbl_inits (b, vbase, binfo, t, list);
7076 inits = TREE_VALUE (list);
7078 /* Figure out the type of the construction vtable. */
7079 type = build_index_type (size_int (list_length (inits) - 1));
7080 type = build_cplus_array_type (vtable_entry_type, type);
7081 TREE_TYPE (vtbl) = type;
7083 /* Initialize the construction vtable. */
7084 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
7085 initialize_array (vtbl, inits);
7086 dump_vtable (t, binfo, vtbl);
7089 /* Add the vtbl initializers for BINFO (and its bases other than
7090 non-virtual primaries) to the list of INITS. BINFO is in the
7091 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7092 the constructor the vtbl inits should be accumulated for. (If this
7093 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7094 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7095 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7096 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7097 but are not necessarily the same in terms of layout. */
7099 static void
7100 accumulate_vtbl_inits (tree binfo,
7101 tree orig_binfo,
7102 tree rtti_binfo,
7103 tree t,
7104 tree inits)
7106 int i;
7107 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7109 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7110 BINFO_TYPE (orig_binfo)),
7111 20000517);
7113 /* If it doesn't have a vptr, we don't do anything. */
7114 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7115 return;
7117 /* If we're building a construction vtable, we're not interested in
7118 subobjects that don't require construction vtables. */
7119 if (ctor_vtbl_p
7120 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7121 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7122 return;
7124 /* Build the initializers for the BINFO-in-T vtable. */
7125 TREE_VALUE (inits)
7126 = chainon (TREE_VALUE (inits),
7127 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7128 rtti_binfo, t, inits));
7130 /* Walk the BINFO and its bases. We walk in preorder so that as we
7131 initialize each vtable we can figure out at what offset the
7132 secondary vtable lies from the primary vtable. We can't use
7133 dfs_walk here because we need to iterate through bases of BINFO
7134 and RTTI_BINFO simultaneously. */
7135 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7137 tree base_binfo = BINFO_BASETYPE (binfo, i);
7139 /* Skip virtual bases. */
7140 if (TREE_VIA_VIRTUAL (base_binfo))
7141 continue;
7142 accumulate_vtbl_inits (base_binfo,
7143 BINFO_BASETYPE (orig_binfo, i),
7144 rtti_binfo, t,
7145 inits);
7149 /* Called from accumulate_vtbl_inits. Returns the initializers for
7150 the BINFO vtable. */
7152 static tree
7153 dfs_accumulate_vtbl_inits (tree binfo,
7154 tree orig_binfo,
7155 tree rtti_binfo,
7156 tree t,
7157 tree l)
7159 tree inits = NULL_TREE;
7160 tree vtbl = NULL_TREE;
7161 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7163 if (ctor_vtbl_p
7164 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7166 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7167 primary virtual base. If it is not the same primary in
7168 the hierarchy of T, we'll need to generate a ctor vtable
7169 for it, to place at its location in T. If it is the same
7170 primary, we still need a VTT entry for the vtable, but it
7171 should point to the ctor vtable for the base it is a
7172 primary for within the sub-hierarchy of RTTI_BINFO.
7174 There are three possible cases:
7176 1) We are in the same place.
7177 2) We are a primary base within a lost primary virtual base of
7178 RTTI_BINFO.
7179 3) We are primary to something not a base of RTTI_BINFO. */
7181 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7182 tree last = NULL_TREE;
7184 /* First, look through the bases we are primary to for RTTI_BINFO
7185 or a virtual base. */
7186 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7188 last = b;
7189 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7190 break;
7192 /* If we run out of primary links, keep looking down our
7193 inheritance chain; we might be an indirect primary. */
7194 if (b == NULL_TREE)
7195 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7196 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7197 break;
7199 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7200 base B and it is a base of RTTI_BINFO, this is case 2. In
7201 either case, we share our vtable with LAST, i.e. the
7202 derived-most base within B of which we are a primary. */
7203 if (b == rtti_binfo
7204 || (b && purpose_member (BINFO_TYPE (b),
7205 CLASSTYPE_VBASECLASSES (BINFO_TYPE (rtti_binfo)))))
7206 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7207 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7208 binfo_ctor_vtable after everything's been set up. */
7209 vtbl = last;
7211 /* Otherwise, this is case 3 and we get our own. */
7213 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
7214 return inits;
7216 if (!vtbl)
7218 tree index;
7219 int non_fn_entries;
7221 /* Compute the initializer for this vtable. */
7222 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7223 &non_fn_entries);
7225 /* Figure out the position to which the VPTR should point. */
7226 vtbl = TREE_PURPOSE (l);
7227 vtbl = build1 (ADDR_EXPR,
7228 vtbl_ptr_type_node,
7229 vtbl);
7230 TREE_CONSTANT (vtbl) = 1;
7231 index = size_binop (PLUS_EXPR,
7232 size_int (non_fn_entries),
7233 size_int (list_length (TREE_VALUE (l))));
7234 index = size_binop (MULT_EXPR,
7235 TYPE_SIZE_UNIT (vtable_entry_type),
7236 index);
7237 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7238 TREE_CONSTANT (vtbl) = 1;
7241 if (ctor_vtbl_p)
7242 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7243 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7244 straighten this out. */
7245 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7246 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7247 inits = NULL_TREE;
7248 else
7249 /* For an ordinary vtable, set BINFO_VTABLE. */
7250 BINFO_VTABLE (binfo) = vtbl;
7252 return inits;
7255 /* Construct the initializer for BINFO's virtual function table. BINFO
7256 is part of the hierarchy dominated by T. If we're building a
7257 construction vtable, the ORIG_BINFO is the binfo we should use to
7258 find the actual function pointers to put in the vtable - but they
7259 can be overridden on the path to most-derived in the graph that
7260 ORIG_BINFO belongs. Otherwise,
7261 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7262 BINFO that should be indicated by the RTTI information in the
7263 vtable; it will be a base class of T, rather than T itself, if we
7264 are building a construction vtable.
7266 The value returned is a TREE_LIST suitable for wrapping in a
7267 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7268 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7269 number of non-function entries in the vtable.
7271 It might seem that this function should never be called with a
7272 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7273 base is always subsumed by a derived class vtable. However, when
7274 we are building construction vtables, we do build vtables for
7275 primary bases; we need these while the primary base is being
7276 constructed. */
7278 static tree
7279 build_vtbl_initializer (tree binfo,
7280 tree orig_binfo,
7281 tree t,
7282 tree rtti_binfo,
7283 int* non_fn_entries_p)
7285 tree v, b;
7286 tree vfun_inits;
7287 tree vbase;
7288 vtbl_init_data vid;
7290 /* Initialize VID. */
7291 memset (&vid, 0, sizeof (vid));
7292 vid.binfo = binfo;
7293 vid.derived = t;
7294 vid.rtti_binfo = rtti_binfo;
7295 vid.last_init = &vid.inits;
7296 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7297 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7298 vid.generate_vcall_entries = true;
7299 /* The first vbase or vcall offset is at index -3 in the vtable. */
7300 vid.index = ssize_int (-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7302 /* Add entries to the vtable for RTTI. */
7303 build_rtti_vtbl_entries (binfo, &vid);
7305 /* Create an array for keeping track of the functions we've
7306 processed. When we see multiple functions with the same
7307 signature, we share the vcall offsets. */
7308 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7309 /* Add the vcall and vbase offset entries. */
7310 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7311 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7312 build_vbase_offset_vtbl_entries. */
7313 for (vbase = CLASSTYPE_VBASECLASSES (t);
7314 vbase;
7315 vbase = TREE_CHAIN (vbase))
7316 BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase)) = 0;
7318 /* If the target requires padding between data entries, add that now. */
7319 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7321 tree cur, *prev;
7323 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7325 tree add = cur;
7326 int i;
7328 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7329 add = tree_cons (NULL_TREE,
7330 build1 (NOP_EXPR, vtable_entry_type,
7331 null_pointer_node),
7332 add);
7333 *prev = add;
7337 if (non_fn_entries_p)
7338 *non_fn_entries_p = list_length (vid.inits);
7340 /* Go through all the ordinary virtual functions, building up
7341 initializers. */
7342 vfun_inits = NULL_TREE;
7343 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7345 tree delta;
7346 tree vcall_index;
7347 tree fn, fn_original;
7348 tree init = NULL_TREE;
7350 fn = BV_FN (v);
7351 fn_original = fn;
7352 if (DECL_THUNK_P (fn))
7354 if (!DECL_NAME (fn))
7355 finish_thunk (fn);
7356 fn_original = THUNK_TARGET (fn);
7359 /* If the only definition of this function signature along our
7360 primary base chain is from a lost primary, this vtable slot will
7361 never be used, so just zero it out. This is important to avoid
7362 requiring extra thunks which cannot be generated with the function.
7364 We first check this in update_vtable_entry_for_fn, so we handle
7365 restored primary bases properly; we also need to do it here so we
7366 zero out unused slots in ctor vtables, rather than filling themff
7367 with erroneous values (though harmless, apart from relocation
7368 costs). */
7369 for (b = binfo; ; b = get_primary_binfo (b))
7371 /* We found a defn before a lost primary; go ahead as normal. */
7372 if (look_for_overrides_here (BINFO_TYPE (b), fn_original))
7373 break;
7375 /* The nearest definition is from a lost primary; clear the
7376 slot. */
7377 if (BINFO_LOST_PRIMARY_P (b))
7379 init = size_zero_node;
7380 break;
7384 if (! init)
7386 /* Pull the offset for `this', and the function to call, out of
7387 the list. */
7388 delta = BV_DELTA (v);
7389 vcall_index = BV_VCALL_INDEX (v);
7391 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7392 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7394 /* You can't call an abstract virtual function; it's abstract.
7395 So, we replace these functions with __pure_virtual. */
7396 if (DECL_PURE_VIRTUAL_P (fn_original))
7397 fn = abort_fndecl;
7398 else if (!integer_zerop (delta) || vcall_index)
7400 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
7401 if (!DECL_NAME (fn))
7402 finish_thunk (fn);
7404 /* Take the address of the function, considering it to be of an
7405 appropriate generic type. */
7406 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7407 /* The address of a function can't change. */
7408 TREE_CONSTANT (init) = 1;
7411 /* And add it to the chain of initializers. */
7412 if (TARGET_VTABLE_USES_DESCRIPTORS)
7414 int i;
7415 if (init == size_zero_node)
7416 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7417 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7418 else
7419 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7421 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7422 TREE_OPERAND (init, 0),
7423 build_int_2 (i, 0));
7424 TREE_CONSTANT (fdesc) = 1;
7426 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7429 else
7430 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7433 /* The initializers for virtual functions were built up in reverse
7434 order; straighten them out now. */
7435 vfun_inits = nreverse (vfun_inits);
7437 /* The negative offset initializers are also in reverse order. */
7438 vid.inits = nreverse (vid.inits);
7440 /* Chain the two together. */
7441 return chainon (vid.inits, vfun_inits);
7444 /* Adds to vid->inits the initializers for the vbase and vcall
7445 offsets in BINFO, which is in the hierarchy dominated by T. */
7447 static void
7448 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
7450 tree b;
7452 /* If this is a derived class, we must first create entries
7453 corresponding to the primary base class. */
7454 b = get_primary_binfo (binfo);
7455 if (b)
7456 build_vcall_and_vbase_vtbl_entries (b, vid);
7458 /* Add the vbase entries for this base. */
7459 build_vbase_offset_vtbl_entries (binfo, vid);
7460 /* Add the vcall entries for this base. */
7461 build_vcall_offset_vtbl_entries (binfo, vid);
7464 /* Returns the initializers for the vbase offset entries in the vtable
7465 for BINFO (which is part of the class hierarchy dominated by T), in
7466 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7467 where the next vbase offset will go. */
7469 static void
7470 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7472 tree vbase;
7473 tree t;
7474 tree non_primary_binfo;
7476 /* If there are no virtual baseclasses, then there is nothing to
7477 do. */
7478 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7479 return;
7481 t = vid->derived;
7483 /* We might be a primary base class. Go up the inheritance hierarchy
7484 until we find the most derived class of which we are a primary base:
7485 it is the offset of that which we need to use. */
7486 non_primary_binfo = binfo;
7487 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7489 tree b;
7491 /* If we have reached a virtual base, then it must be a primary
7492 base (possibly multi-level) of vid->binfo, or we wouldn't
7493 have called build_vcall_and_vbase_vtbl_entries for it. But it
7494 might be a lost primary, so just skip down to vid->binfo. */
7495 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7497 non_primary_binfo = vid->binfo;
7498 break;
7501 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7502 if (get_primary_binfo (b) != non_primary_binfo)
7503 break;
7504 non_primary_binfo = b;
7507 /* Go through the virtual bases, adding the offsets. */
7508 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7509 vbase;
7510 vbase = TREE_CHAIN (vbase))
7512 tree b;
7513 tree delta;
7515 if (!TREE_VIA_VIRTUAL (vbase))
7516 continue;
7518 /* Find the instance of this virtual base in the complete
7519 object. */
7520 b = copied_binfo (vbase, binfo);
7522 /* If we've already got an offset for this virtual base, we
7523 don't need another one. */
7524 if (BINFO_VTABLE_PATH_MARKED (b))
7525 continue;
7526 BINFO_VTABLE_PATH_MARKED (b) = 1;
7528 /* Figure out where we can find this vbase offset. */
7529 delta = size_binop (MULT_EXPR,
7530 vid->index,
7531 convert (ssizetype,
7532 TYPE_SIZE_UNIT (vtable_entry_type)));
7533 if (vid->primary_vtbl_p)
7534 BINFO_VPTR_FIELD (b) = delta;
7536 if (binfo != TYPE_BINFO (t))
7538 /* The vbase offset had better be the same. */
7539 my_friendly_assert (tree_int_cst_equal (delta,
7540 BINFO_VPTR_FIELD (vbase)),
7541 20030202);
7544 /* The next vbase will come at a more negative offset. */
7545 vid->index = size_binop (MINUS_EXPR, vid->index,
7546 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7548 /* The initializer is the delta from BINFO to this virtual base.
7549 The vbase offsets go in reverse inheritance-graph order, and
7550 we are walking in inheritance graph order so these end up in
7551 the right order. */
7552 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7554 *vid->last_init
7555 = build_tree_list (NULL_TREE,
7556 fold (build1 (NOP_EXPR,
7557 vtable_entry_type,
7558 delta)));
7559 vid->last_init = &TREE_CHAIN (*vid->last_init);
7563 /* Adds the initializers for the vcall offset entries in the vtable
7564 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7565 to VID->INITS. */
7567 static void
7568 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7570 /* We only need these entries if this base is a virtual base. We
7571 compute the indices -- but do not add to the vtable -- when
7572 building the main vtable for a class. */
7573 if (TREE_VIA_VIRTUAL (binfo) || binfo == TYPE_BINFO (vid->derived))
7575 /* We need a vcall offset for each of the virtual functions in this
7576 vtable. For example:
7578 class A { virtual void f (); };
7579 class B1 : virtual public A { virtual void f (); };
7580 class B2 : virtual public A { virtual void f (); };
7581 class C: public B1, public B2 { virtual void f (); };
7583 A C object has a primary base of B1, which has a primary base of A. A
7584 C also has a secondary base of B2, which no longer has a primary base
7585 of A. So the B2-in-C construction vtable needs a secondary vtable for
7586 A, which will adjust the A* to a B2* to call f. We have no way of
7587 knowing what (or even whether) this offset will be when we define B2,
7588 so we store this "vcall offset" in the A sub-vtable and look it up in
7589 a "virtual thunk" for B2::f.
7591 We need entries for all the functions in our primary vtable and
7592 in our non-virtual bases' secondary vtables. */
7593 vid->vbase = binfo;
7594 /* If we are just computing the vcall indices -- but do not need
7595 the actual entries -- not that. */
7596 if (!TREE_VIA_VIRTUAL (binfo))
7597 vid->generate_vcall_entries = false;
7598 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7599 add_vcall_offset_vtbl_entries_r (binfo, vid);
7603 /* Build vcall offsets, starting with those for BINFO. */
7605 static void
7606 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
7608 int i;
7609 tree primary_binfo;
7611 /* Don't walk into virtual bases -- except, of course, for the
7612 virtual base for which we are building vcall offsets. Any
7613 primary virtual base will have already had its offsets generated
7614 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7615 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7616 return;
7618 /* If BINFO has a primary base, process it first. */
7619 primary_binfo = get_primary_binfo (binfo);
7620 if (primary_binfo)
7621 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7623 /* Add BINFO itself to the list. */
7624 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7626 /* Scan the non-primary bases of BINFO. */
7627 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7629 tree base_binfo;
7631 base_binfo = BINFO_BASETYPE (binfo, i);
7632 if (base_binfo != primary_binfo)
7633 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7637 /* Called from build_vcall_offset_vtbl_entries_r. */
7639 static void
7640 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
7642 /* Make entries for the rest of the virtuals. */
7643 if (abi_version_at_least (2))
7645 tree orig_fn;
7647 /* The ABI requires that the methods be processed in declaration
7648 order. G++ 3.2 used the order in the vtable. */
7649 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
7650 orig_fn;
7651 orig_fn = TREE_CHAIN (orig_fn))
7652 if (DECL_VINDEX (orig_fn))
7653 add_vcall_offset (orig_fn, binfo, vid);
7655 else
7657 tree derived_virtuals;
7658 tree base_virtuals;
7659 tree orig_virtuals;
7660 /* If BINFO is a primary base, the most derived class which has
7661 BINFO as a primary base; otherwise, just BINFO. */
7662 tree non_primary_binfo;
7664 /* We might be a primary base class. Go up the inheritance hierarchy
7665 until we find the most derived class of which we are a primary base:
7666 it is the BINFO_VIRTUALS there that we need to consider. */
7667 non_primary_binfo = binfo;
7668 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7670 tree b;
7672 /* If we have reached a virtual base, then it must be vid->vbase,
7673 because we ignore other virtual bases in
7674 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7675 base (possibly multi-level) of vid->binfo, or we wouldn't
7676 have called build_vcall_and_vbase_vtbl_entries for it. But it
7677 might be a lost primary, so just skip down to vid->binfo. */
7678 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7680 if (non_primary_binfo != vid->vbase)
7681 abort ();
7682 non_primary_binfo = vid->binfo;
7683 break;
7686 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7687 if (get_primary_binfo (b) != non_primary_binfo)
7688 break;
7689 non_primary_binfo = b;
7692 if (vid->ctor_vtbl_p)
7693 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7694 where rtti_binfo is the most derived type. */
7695 non_primary_binfo
7696 = original_binfo (non_primary_binfo, vid->rtti_binfo);
7698 for (base_virtuals = BINFO_VIRTUALS (binfo),
7699 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7700 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7701 base_virtuals;
7702 base_virtuals = TREE_CHAIN (base_virtuals),
7703 derived_virtuals = TREE_CHAIN (derived_virtuals),
7704 orig_virtuals = TREE_CHAIN (orig_virtuals))
7706 tree orig_fn;
7708 /* Find the declaration that originally caused this function to
7709 be present in BINFO_TYPE (binfo). */
7710 orig_fn = BV_FN (orig_virtuals);
7712 /* When processing BINFO, we only want to generate vcall slots for
7713 function slots introduced in BINFO. So don't try to generate
7714 one if the function isn't even defined in BINFO. */
7715 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7716 continue;
7718 add_vcall_offset (orig_fn, binfo, vid);
7723 /* Add a vcall offset entry for ORIG_FN to the vtable. */
7725 static void
7726 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
7728 size_t i;
7729 tree vcall_offset;
7731 /* If there is already an entry for a function with the same
7732 signature as FN, then we do not need a second vcall offset.
7733 Check the list of functions already present in the derived
7734 class vtable. */
7735 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7737 tree derived_entry;
7739 derived_entry = VARRAY_TREE (vid->fns, i);
7740 if (same_signature_p (derived_entry, orig_fn)
7741 /* We only use one vcall offset for virtual destructors,
7742 even though there are two virtual table entries. */
7743 || (DECL_DESTRUCTOR_P (derived_entry)
7744 && DECL_DESTRUCTOR_P (orig_fn)))
7745 return;
7748 /* If we are building these vcall offsets as part of building
7749 the vtable for the most derived class, remember the vcall
7750 offset. */
7751 if (vid->binfo == TYPE_BINFO (vid->derived))
7752 CLASSTYPE_VCALL_INDICES (vid->derived)
7753 = tree_cons (orig_fn, vid->index,
7754 CLASSTYPE_VCALL_INDICES (vid->derived));
7756 /* The next vcall offset will be found at a more negative
7757 offset. */
7758 vid->index = size_binop (MINUS_EXPR, vid->index,
7759 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7761 /* Keep track of this function. */
7762 VARRAY_PUSH_TREE (vid->fns, orig_fn);
7764 if (vid->generate_vcall_entries)
7766 tree base;
7767 tree fn;
7769 /* Find the overriding function. */
7770 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
7771 if (fn == error_mark_node)
7772 vcall_offset = build1 (NOP_EXPR, vtable_entry_type,
7773 integer_zero_node);
7774 else
7776 base = TREE_VALUE (fn);
7778 /* The vbase we're working on is a primary base of
7779 vid->binfo. But it might be a lost primary, so its
7780 BINFO_OFFSET might be wrong, so we just use the
7781 BINFO_OFFSET from vid->binfo. */
7782 vcall_offset = size_diffop (BINFO_OFFSET (base),
7783 BINFO_OFFSET (vid->binfo));
7784 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
7785 vcall_offset));
7787 /* Add the initializer to the vtable. */
7788 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7789 vid->last_init = &TREE_CHAIN (*vid->last_init);
7793 /* Return vtbl initializers for the RTTI entries corresponding to the
7794 BINFO's vtable. The RTTI entries should indicate the object given
7795 by VID->rtti_binfo. */
7797 static void
7798 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
7800 tree b;
7801 tree t;
7802 tree basetype;
7803 tree offset;
7804 tree decl;
7805 tree init;
7807 basetype = BINFO_TYPE (binfo);
7808 t = BINFO_TYPE (vid->rtti_binfo);
7810 /* To find the complete object, we will first convert to our most
7811 primary base, and then add the offset in the vtbl to that value. */
7812 b = binfo;
7813 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
7814 && !BINFO_LOST_PRIMARY_P (b))
7816 tree primary_base;
7818 primary_base = get_primary_binfo (b);
7819 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
7820 b = primary_base;
7822 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
7824 /* The second entry is the address of the typeinfo object. */
7825 if (flag_rtti)
7826 decl = build_address (get_tinfo_decl (t));
7827 else
7828 decl = integer_zero_node;
7830 /* Convert the declaration to a type that can be stored in the
7831 vtable. */
7832 init = build_nop (vfunc_ptr_type_node, decl);
7833 *vid->last_init = build_tree_list (NULL_TREE, init);
7834 vid->last_init = &TREE_CHAIN (*vid->last_init);
7836 /* Add the offset-to-top entry. It comes earlier in the vtable that
7837 the the typeinfo entry. Convert the offset to look like a
7838 function pointer, so that we can put it in the vtable. */
7839 init = build_nop (vfunc_ptr_type_node, offset);
7840 *vid->last_init = build_tree_list (NULL_TREE, init);
7841 vid->last_init = &TREE_CHAIN (*vid->last_init);