1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
41 /* The number of nested classes being processed. If we are not in the
42 scope of any class, this is zero. */
44 int current_class_depth;
46 /* In order to deal with nested classes, we keep a stack of classes.
47 The topmost entry is the innermost class, and is the entry at index
48 CURRENT_CLASS_DEPTH */
50 typedef struct class_stack_node {
51 /* The name of the class. */
54 /* The _TYPE node for the class. */
57 /* The access specifier pending for new declarations in the scope of
61 /* If were defining TYPE, the names used in this class. */
62 splay_tree names_used;
63 }* class_stack_node_t;
65 typedef struct vtbl_init_data_s
67 /* The base for which we're building initializers. */
69 /* The binfo for the most-derived type. */
71 /* The negative-index vtable initializers built up so far. These
72 are in order from least negative index to most negative index. */
74 /* The last (i.e., most negative entry in INITS. */
76 /* The binfo for the virtual base for which we're building
77 vcall offset initializers. */
79 /* The functions in vbase for which we have already provided vcall
82 /* The vtable index of the next vcall or vbase offset. */
84 /* Nonzero if we are building the initializer for the primary
87 /* Nonzero if we are building the initializer for a construction
92 /* The type of a function passed to walk_subobject_offsets. */
93 typedef int (*subobject_offset_fn) PARAMS ((tree, tree, splay_tree));
95 /* The stack itself. This is an dynamically resized array. The
96 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
97 static int current_class_stack_size;
98 static class_stack_node_t current_class_stack;
100 /* An array of all local classes present in this translation unit, in
101 declaration order. */
102 varray_type local_classes;
104 static tree get_vfield_name PARAMS ((tree));
105 static void finish_struct_anon PARAMS ((tree));
106 static tree build_vbase_pointer PARAMS ((tree, tree));
107 static tree build_vtable_entry PARAMS ((tree, tree, tree, int));
108 static tree get_vtable_name PARAMS ((tree));
109 static tree get_derived_offset PARAMS ((tree, tree));
110 static tree get_basefndecls PARAMS ((tree, tree));
111 static int build_primary_vtable PARAMS ((tree, tree));
112 static int build_secondary_vtable PARAMS ((tree, tree));
113 static tree dfs_finish_vtbls PARAMS ((tree, void *));
114 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
116 static void finish_vtbls PARAMS ((tree));
117 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
118 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
119 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
120 static void delete_duplicate_fields PARAMS ((tree));
121 static void finish_struct_bits PARAMS ((tree));
122 static int alter_access PARAMS ((tree, tree, tree));
123 static void handle_using_decl PARAMS ((tree, tree));
124 static int same_signature_p PARAMS ((tree, tree));
125 static int strictly_overrides PARAMS ((tree, tree));
126 static void mark_overriders PARAMS ((tree, tree));
127 static void check_for_override PARAMS ((tree, tree));
128 static tree dfs_modify_vtables PARAMS ((tree, void *));
129 static tree modify_all_vtables PARAMS ((tree, int *, tree));
130 static void determine_primary_base PARAMS ((tree, int *));
131 static void finish_struct_methods PARAMS ((tree));
132 static void maybe_warn_about_overly_private_class PARAMS ((tree));
133 static int field_decl_cmp PARAMS ((const tree *, const tree *));
134 static int method_name_cmp PARAMS ((const tree *, const tree *));
135 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
136 static tree fixed_type_or_null PARAMS ((tree, int *));
137 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
139 static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
140 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
141 static int count_fields PARAMS ((tree));
142 static int add_fields_to_vec PARAMS ((tree, tree, int));
143 static void check_bitfield_decl PARAMS ((tree));
144 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
145 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
147 static void build_base_field PARAMS ((record_layout_info, tree, int *,
148 unsigned int *, splay_tree));
149 static void build_base_fields PARAMS ((record_layout_info, int *,
151 static tree build_vbase_pointer_fields PARAMS ((record_layout_info, int *));
152 static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, tree,
154 static void check_methods PARAMS ((tree));
155 static void remove_zero_width_bit_fields PARAMS ((tree));
156 static void check_bases PARAMS ((tree, int *, int *, int *));
157 static void check_bases_and_members PARAMS ((tree, int *));
158 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
159 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
160 static void fixup_pending_inline PARAMS ((tree));
161 static void fixup_inline_methods PARAMS ((tree));
162 static void set_primary_base PARAMS ((tree, tree, int *));
163 static void propagate_binfo_offsets PARAMS ((tree, tree));
164 static void layout_virtual_bases PARAMS ((tree, splay_tree));
165 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
166 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
167 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
168 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
169 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
170 static void layout_vtable_decl PARAMS ((tree, int));
171 static tree dfs_find_final_overrider PARAMS ((tree, void *));
172 static tree find_final_overrider PARAMS ((tree, tree, tree));
173 static int make_new_vtable PARAMS ((tree, tree));
174 static void dump_class_hierarchy_r PARAMS ((tree, tree, int));
175 extern void dump_class_hierarchy PARAMS ((tree));
176 static tree build_vtable PARAMS ((tree, tree, tree));
177 static void initialize_vtable PARAMS ((tree, tree));
178 static void initialize_array PARAMS ((tree, tree));
179 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
182 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
183 static void layout_empty_base PARAMS ((tree, tree, splay_tree));
184 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
185 static void set_vindex PARAMS ((tree, tree, int *));
186 static void build_rtti_vtbl_entries PARAMS ((tree, tree, vtbl_init_data *));
187 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
189 static tree dfs_mark_primary_bases PARAMS ((tree, void *));
190 static void mark_primary_bases PARAMS ((tree));
191 static void clone_constructors_and_destructors PARAMS ((tree));
192 static tree build_clone PARAMS ((tree, tree));
193 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
194 static tree copy_virtuals PARAMS ((tree));
195 static void build_ctor_vtbl_group PARAMS ((tree, tree));
196 static void build_vtt PARAMS ((tree));
197 static tree *build_vtt_inits PARAMS ((tree, tree, int, tree *, tree *));
198 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
199 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
200 static tree get_matching_base PARAMS ((tree, tree));
201 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
202 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
203 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
204 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
205 tree, splay_tree, int));
206 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
207 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
208 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
211 /* Variables shared between class.c and call.c. */
213 #ifdef GATHER_STATISTICS
215 int n_vtable_entries = 0;
216 int n_vtable_searches = 0;
217 int n_vtable_elems = 0;
218 int n_convert_harshness = 0;
219 int n_compute_conversion_costs = 0;
220 int n_build_method_call = 0;
221 int n_inner_fields_searched = 0;
224 /* Virtual base class layout. */
226 /* Returns a list of virtual base class pointers as a chain of
230 build_vbase_pointer_fields (rli, empty_p)
231 record_layout_info rli;
234 /* Chain to hold all the new FIELD_DECLs which point at virtual
237 tree vbase_decls = NULL_TREE;
238 tree binfos = TYPE_BINFO_BASETYPES (rec);
239 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
243 /* Under the new ABI, there are no vbase pointers in the object.
244 Instead, the offsets are stored in the vtable. */
245 if (vbase_offsets_in_vtable_p ())
248 /* Loop over the baseclasses, adding vbase pointers as needed. */
249 for (i = 0; i < n_baseclasses; i++)
251 register tree base_binfo = TREE_VEC_ELT (binfos, i);
252 register tree basetype = BINFO_TYPE (base_binfo);
254 if (!COMPLETE_TYPE_P (basetype))
255 /* This error is now reported in xref_tag, thus giving better
256 location information. */
259 /* All basetypes are recorded in the association list of the
262 if (TREE_VIA_VIRTUAL (base_binfo))
267 /* The offset for a virtual base class is only used in computing
268 virtual function tables and for initializing virtual base
269 pointers. It is built once `get_vbase_types' is called. */
271 /* If this basetype can come from another vbase pointer
272 without an additional indirection, we will share
273 that pointer. If an indirection is involved, we
274 make our own pointer. */
275 for (j = 0; j < n_baseclasses; j++)
277 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
278 if (! TREE_VIA_VIRTUAL (other_base_binfo)
279 && binfo_for_vbase (basetype, BINFO_TYPE (other_base_binfo)))
282 FORMAT_VBASE_NAME (name, basetype);
283 decl = build_vtbl_or_vbase_field (get_identifier (name),
284 get_identifier (VTABLE_BASE),
285 build_pointer_type (basetype),
289 BINFO_VPTR_FIELD (base_binfo) = decl;
290 TREE_CHAIN (decl) = vbase_decls;
291 place_field (rli, decl);
296 /* The space this decl occupies has already been accounted for. */
304 /* Returns a pointer to the virtual base class of EXP that has the
305 indicated TYPE. EXP is of class type, not a pointer type. */
308 build_vbase_pointer (exp, type)
311 if (vbase_offsets_in_vtable_p ())
316 /* Find the shared copy of TYPE; that's where the vtable offset
318 vbase = binfo_for_vbase (type, TREE_TYPE (exp));
319 /* Find the virtual function table pointer. */
320 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
321 /* Compute the location where the offset will lie. */
322 vbase_ptr = build (PLUS_EXPR,
323 TREE_TYPE (vbase_ptr),
325 BINFO_VPTR_FIELD (vbase));
326 vbase_ptr = build1 (NOP_EXPR,
327 build_pointer_type (ptrdiff_type_node),
329 /* Add the contents of this location to EXP. */
330 return build (PLUS_EXPR,
331 build_pointer_type (type),
332 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
333 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
338 FORMAT_VBASE_NAME (name, type);
339 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
343 /* Build multi-level access to EXPR using hierarchy path PATH.
344 CODE is PLUS_EXPR if we are going with the grain,
345 and MINUS_EXPR if we are not (in which case, we cannot traverse
346 virtual baseclass links).
348 TYPE is the type we want this path to have on exit.
350 NONNULL is non-zero if we know (for any reason) that EXPR is
351 not, in fact, zero. */
354 build_vbase_path (code, type, expr, path, nonnull)
356 tree type, expr, path;
359 register int changed = 0;
360 tree last = NULL_TREE, last_virtual = NULL_TREE;
362 tree null_expr = 0, nonnull_expr;
364 tree offset = integer_zero_node;
366 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
367 return build1 (NOP_EXPR, type, expr);
369 /* We could do better if we had additional logic to convert back to the
370 unconverted type (the static type of the complete object), and then
371 convert back to the type we want. Until that is done, we only optimize
372 if the complete type is the same type as expr has. */
373 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
375 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
376 expr = save_expr (expr);
379 path = reverse_path (path);
381 basetype = BINFO_TYPE (path);
385 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
387 last_virtual = BINFO_TYPE (TREE_VALUE (path));
388 if (code == PLUS_EXPR)
390 changed = ! fixed_type_p;
396 /* We already check for ambiguous things in the caller, just
400 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
401 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
403 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
404 nonnull_expr = build_vbase_pointer (ind, last_virtual);
406 && TREE_CODE (type) == POINTER_TYPE
407 && null_expr == NULL_TREE)
409 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
410 expr = build (COND_EXPR, build_pointer_type (last_virtual),
411 build (EQ_EXPR, boolean_type_node, expr,
413 null_expr, nonnull_expr);
416 /* else we'll figure out the offset below. */
418 /* Happens in the case of parse errors. */
419 if (nonnull_expr == error_mark_node)
420 return error_mark_node;
424 cp_error ("cannot cast up from virtual baseclass `%T'",
426 return error_mark_node;
429 last = TREE_VALUE (path);
430 path = TREE_CHAIN (path);
432 /* LAST is now the last basetype assoc on the path. */
434 /* A pointer to a virtual base member of a non-null object
435 is non-null. Therefore, we only need to test for zeroness once.
436 Make EXPR the canonical expression to deal with here. */
439 TREE_OPERAND (expr, 2) = nonnull_expr;
440 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
441 = TREE_TYPE (nonnull_expr);
446 /* If we go through any virtual base pointers, make sure that
447 casts to BASETYPE from the last virtual base class use
448 the right value for BASETYPE. */
451 tree intype = TREE_TYPE (TREE_TYPE (expr));
453 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
455 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
458 offset = BINFO_OFFSET (last);
460 if (! integer_zerop (offset))
462 /* Bash types to make the backend happy. */
463 offset = cp_convert (type, offset);
465 /* If expr might be 0, we need to preserve that zeroness. */
469 TREE_TYPE (null_expr) = type;
471 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
472 if (TREE_SIDE_EFFECTS (expr))
473 expr = save_expr (expr);
475 return build (COND_EXPR, type,
476 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
478 build (code, type, expr, offset));
480 else return build (code, type, expr, offset);
483 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
484 be used multiple times in initialization of multiple inheritance. */
487 TREE_TYPE (expr) = type;
491 return build1 (NOP_EXPR, type, expr);
495 /* Virtual function things. */
497 /* We want to give the assembler the vtable identifier as well as
498 the offset to the function pointer. So we generate
500 __asm__ __volatile__ (".vtable_entry %c0, %c1"
501 : : "s"(&class_vtable),
502 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
505 build_vtable_entry_ref (basetype, vtbl, idx)
506 tree basetype, vtbl, idx;
508 static char asm_stmt[] = ".vtable_entry %c0, %c1";
511 s = build_unary_op (ADDR_EXPR,
512 get_vtbl_decl_for_binfo (TYPE_BINFO (basetype)),
514 s = build_tree_list (build_string (1, "s"), s);
516 i = build_array_ref (vtbl, idx);
517 if (!flag_vtable_thunks)
518 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
519 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
520 i2 = build_array_ref (vtbl, build_int_2(0,0));
521 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
522 i = cp_build_binary_op (MINUS_EXPR, i, i2);
523 i = build_tree_list (build_string (1, "i"), i);
525 finish_asm_stmt (ridpointers[RID_VOLATILE],
526 build_string (sizeof(asm_stmt)-1, asm_stmt),
527 NULL_TREE, chainon (s, i), NULL_TREE);
530 /* Given an object INSTANCE, return an expression which yields the
531 virtual function vtable element corresponding to INDEX. There are
532 many special cases for INSTANCE which we take care of here, mainly
533 to avoid creating extra tree nodes when we don't have to. */
536 build_vtbl_ref (instance, idx)
540 tree basetype = TREE_TYPE (instance);
542 if (TREE_CODE (basetype) == REFERENCE_TYPE)
543 basetype = TREE_TYPE (basetype);
545 if (instance == current_class_ref)
546 vtbl = build_vfield_ref (instance, basetype);
551 /* Try to figure out what a reference refers to, and
552 access its virtual function table directly. */
553 tree ref = NULL_TREE;
555 if (TREE_CODE (instance) == INDIRECT_REF
556 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
557 ref = TREE_OPERAND (instance, 0);
558 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
561 if (ref && TREE_CODE (ref) == VAR_DECL
562 && DECL_INITIAL (ref))
564 tree init = DECL_INITIAL (ref);
566 while (TREE_CODE (init) == NOP_EXPR
567 || TREE_CODE (init) == NON_LVALUE_EXPR)
568 init = TREE_OPERAND (init, 0);
569 if (TREE_CODE (init) == ADDR_EXPR)
571 init = TREE_OPERAND (init, 0);
572 if (IS_AGGR_TYPE (TREE_TYPE (init))
573 && (TREE_CODE (init) == PARM_DECL
574 || TREE_CODE (init) == VAR_DECL))
580 if (IS_AGGR_TYPE (TREE_TYPE (instance))
581 && (TREE_CODE (instance) == RESULT_DECL
582 || TREE_CODE (instance) == PARM_DECL
583 || TREE_CODE (instance) == VAR_DECL))
585 vtbl = TYPE_BINFO_VTABLE (basetype);
586 /* Knowing the dynamic type of INSTANCE we can easily obtain
587 the correct vtable entry. In the new ABI, we resolve
588 this back to be in terms of the primary vtable. */
589 if (TREE_CODE (vtbl) == PLUS_EXPR)
591 idx = fold (build (PLUS_EXPR,
594 build (EXACT_DIV_EXPR,
596 TREE_OPERAND (vtbl, 1),
597 TYPE_SIZE_UNIT (vtable_entry_type))));
598 vtbl = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
602 vtbl = build_vfield_ref (instance, basetype);
605 assemble_external (vtbl);
608 build_vtable_entry_ref (basetype, vtbl, idx);
610 aref = build_array_ref (vtbl, idx);
615 /* Given an object INSTANCE, return an expression which yields the
616 virtual function corresponding to INDEX. There are many special
617 cases for INSTANCE which we take care of here, mainly to avoid
618 creating extra tree nodes when we don't have to. */
621 build_vfn_ref (ptr_to_instptr, instance, idx)
622 tree *ptr_to_instptr, instance;
625 tree aref = build_vtbl_ref (instance, idx);
627 /* When using thunks, there is no extra delta, and we get the pfn
629 if (flag_vtable_thunks)
634 /* Save the intermediate result in a SAVE_EXPR so we don't have to
635 compute each component of the virtual function pointer twice. */
636 if (TREE_CODE (aref) == INDIRECT_REF)
637 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
640 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
642 cp_convert (ptrdiff_type_node,
643 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
646 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
649 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
650 for the given TYPE. */
653 get_vtable_name (type)
657 return mangle_vtbl_for_type (type);
659 return build_overload_with_type (get_identifier (VTABLE_NAME_PREFIX),
663 /* Return an IDENTIFIER_NODE for the name of the virtual table table
671 return mangle_vtt_for_type (type);
673 return build_overload_with_type (get_identifier (VTT_NAME_PREFIX),
677 /* Return the offset to the main vtable for a given base BINFO. */
680 get_vfield_offset (binfo)
684 size_binop (PLUS_EXPR, byte_position (TYPE_VFIELD (BINFO_TYPE (binfo))),
685 BINFO_OFFSET (binfo));
688 /* Get the offset to the start of the original binfo that we derived
689 this binfo from. If we find TYPE first, return the offset only
690 that far. The shortened search is useful because the this pointer
691 on method calling is expected to point to a DECL_CONTEXT (fndecl)
692 object, and not a baseclass of it. */
695 get_derived_offset (binfo, type)
698 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
701 while (!same_type_p (BINFO_TYPE (binfo), type))
702 binfo = get_primary_binfo (binfo);
704 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
705 return size_binop (MINUS_EXPR, offset1, offset2);
708 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
709 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
710 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
713 build_vtable (class_type, name, vtable_type)
720 decl = build_lang_decl (VAR_DECL, name, vtable_type);
721 DECL_CONTEXT (decl) = class_type;
722 DECL_ARTIFICIAL (decl) = 1;
723 TREE_STATIC (decl) = 1;
724 #ifndef WRITABLE_VTABLES
725 /* Make them READONLY by default. (mrs) */
726 TREE_READONLY (decl) = 1;
728 DECL_VIRTUAL_P (decl) = 1;
729 import_export_vtable (decl, class_type, 0);
734 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
735 or even complete. If this does not exist, create it. If COMPLETE is
736 non-zero, then complete the definition of it -- that will render it
737 impossible to actually build the vtable, but is useful to get at those
738 which are known to exist in the runtime. */
741 get_vtable_decl (type, complete)
745 tree name = get_vtable_name (type);
746 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
750 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
751 && DECL_VIRTUAL_P (decl), 20000118);
755 decl = build_vtable (type, name, void_type_node);
756 decl = pushdecl_top_level (decl);
757 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
760 /* At one time the vtable info was grabbed 2 words at a time. This
761 fails on sparc unless you have 8-byte alignment. (tiemann) */
762 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
767 DECL_EXTERNAL (decl) = 1;
768 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
774 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
775 BV_VCALL_INDEX for each entry is cleared. */
778 copy_virtuals (binfo)
784 copies = copy_list (BINFO_VIRTUALS (binfo));
785 for (t = copies; t; t = TREE_CHAIN (t))
787 BV_VCALL_INDEX (t) = NULL_TREE;
788 BV_USE_VCALL_INDEX_P (t) = 0;
789 BV_GENERATE_THUNK_WITH_VTABLE_P (t) = 0;
795 /* Build the primary virtual function table for TYPE. If BINFO is
796 non-NULL, build the vtable starting with the initial approximation
797 that it is the same as the one which is the head of the association
798 list. Returns a non-zero value if a new vtable is actually
802 build_primary_vtable (binfo, type)
808 decl = get_vtable_decl (type, /*complete=*/0);
812 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
813 /* We have already created a vtable for this base, so there's
814 no need to do it again. */
817 virtuals = copy_virtuals (binfo);
818 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
819 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
820 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
824 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
826 virtuals = NULL_TREE;
829 #ifdef GATHER_STATISTICS
831 n_vtable_elems += list_length (virtuals);
834 /* Initialize the association list for this type, based
835 on our first approximation. */
836 TYPE_BINFO_VTABLE (type) = decl;
837 TYPE_BINFO_VIRTUALS (type) = virtuals;
838 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
842 /* Give TYPE a new virtual function table which is initialized
843 with a skeleton-copy of its original initialization. The only
844 entry that changes is the `delta' entry, so we can really
845 share a lot of structure.
847 FOR_TYPE is the derived type which caused this table to
850 BINFO is the type association which provided TYPE for FOR_TYPE.
852 The order in which vtables are built (by calling this function) for
853 an object must remain the same, otherwise a binary incompatibility
857 build_secondary_vtable (binfo, for_type)
858 tree binfo, for_type;
861 tree orig_decl = BINFO_VTABLE (binfo);
875 if (TREE_VIA_VIRTUAL (binfo))
876 my_friendly_assert (binfo == binfo_for_vbase (BINFO_TYPE (binfo),
880 if (BINFO_NEW_VTABLE_MARKED (binfo, current_class_type))
881 /* We already created a vtable for this base. There's no need to
885 /* Remember that we've created a vtable for this BINFO, so that we
886 don't try to do so again. */
887 SET_BINFO_NEW_VTABLE_MARKED (binfo, current_class_type);
889 /* Make fresh virtual list, so we can smash it later. */
890 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
892 if (TREE_VIA_VIRTUAL (binfo))
894 tree binfo1 = binfo_for_vbase (BINFO_TYPE (binfo), for_type);
896 /* XXX - This should never happen, if it does, the caller should
897 ensure that the binfo is from for_type's binfos, not from any
898 base type's. We can remove all this code after a while. */
900 warning ("internal inconsistency: binfo offset error for rtti");
902 offset = BINFO_OFFSET (binfo1);
905 offset = BINFO_OFFSET (binfo);
907 /* In the new ABI, secondary vtables are laid out as part of the
908 same structure as the primary vtable. */
909 if (merge_primary_and_secondary_vtables_p ())
911 BINFO_VTABLE (binfo) = NULL_TREE;
915 /* Create the declaration for the secondary vtable. */
916 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
917 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
918 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
920 /* We know that the vtable that we are going to create doesn't exist
921 yet in the global namespace, and when we finish, it will be
922 pushed into the global namespace. In complex MI hierarchies, we
923 have to loop while the name we are thinking of adding is globally
924 defined, adding more name components to the vtable name as we
925 loop, until the name is unique. This is because in complex MI
926 cases, we might have the same base more than once. This means
927 that the order in which this function is called for vtables must
928 remain the same, otherwise binary compatibility can be
933 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
937 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
939 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
940 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
941 name = get_identifier (buf);
943 /* If this name doesn't clash, then we can use it, otherwise
944 we add more to the name until it is unique. */
946 if (! IDENTIFIER_GLOBAL_VALUE (name))
949 /* Set values for next loop through, if the name isn't unique. */
951 path = BINFO_INHERITANCE_CHAIN (path);
953 /* We better not run out of stuff to make it unique. */
954 my_friendly_assert (path != NULL_TREE, 368);
956 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
958 if (for_type == basetype)
960 /* If we run out of basetypes in the path, we have already
961 found created a vtable with that name before, we now
962 resort to tacking on _%d to distinguish them. */
964 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
965 buf1 = (char *) alloca (i);
967 sprintf (buf1, "%s%c%s%c%d",
968 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
970 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
971 + strlen (buf1) + 1);
972 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
973 name = get_identifier (buf);
975 /* If this name doesn't clash, then we can use it,
976 otherwise we add something different to the name until
978 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
980 /* Hey, they really like MI don't they? Increase the 3
981 above to 6, and the 999 to 999999. :-) */
982 my_friendly_assert (j <= 999, 369);
987 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
988 new_buf2 = (char *) alloca (i);
989 sprintf (new_buf2, "%s%c%s",
990 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
994 new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
995 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
996 DECL_USER_ALIGN (new_decl) = DECL_USER_ALIGN (orig_decl);
997 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
999 #ifdef GATHER_STATISTICS
1001 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
1007 /* Create a new vtable for BINFO which is the hierarchy dominated by
1011 make_new_vtable (t, binfo)
1015 if (binfo == TYPE_BINFO (t))
1016 /* In this case, it is *type*'s vtable we are modifying. We start
1017 with the approximation that it's vtable is that of the
1018 immediate base class. */
1019 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
1022 /* This is our very own copy of `basetype' to play with. Later,
1023 we will fill in all the virtual functions that override the
1024 virtual functions in these base classes which are not defined
1025 by the current type. */
1026 return build_secondary_vtable (binfo, t);
1029 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
1030 (which is in the hierarchy dominated by T) list FNDECL as its
1031 BV_FN. DELTA is the required constant adjustment from the `this'
1032 pointer where the vtable entry appears to the `this' required when
1033 the function is actually called. */
1036 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
1047 if (fndecl != BV_FN (v)
1048 || !tree_int_cst_equal (delta, BV_DELTA (v)))
1052 /* We need a new vtable for BINFO. */
1053 if (make_new_vtable (t, binfo))
1055 /* If we really did make a new vtable, we also made a copy
1056 of the BINFO_VIRTUALS list. Now, we have to find the
1057 corresponding entry in that list. */
1058 *virtuals = BINFO_VIRTUALS (binfo);
1059 while (BV_FN (*virtuals) != BV_FN (v))
1060 *virtuals = TREE_CHAIN (*virtuals);
1064 base_fndecl = BV_FN (v);
1065 BV_DELTA (v) = delta;
1066 BV_VCALL_INDEX (v) = NULL_TREE;
1069 /* Now assign virtual dispatch information, if unset. We can
1070 dispatch this, through any overridden base function. */
1071 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1073 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1074 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1079 /* Return the index (in the virtual function table) of the first
1080 virtual function. */
1083 first_vfun_index (t)
1086 /* Under the old ABI, the offset-to-top and RTTI entries are at
1087 indices zero and one; under the new ABI, the first virtual
1088 function is at index zero. */
1089 if (!CLASSTYPE_COM_INTERFACE (t) && !flag_new_abi)
1090 return flag_vtable_thunks ? 2 : 1;
1095 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
1096 functions present in the vtable so far. */
1099 set_vindex (t, decl, vfuns_p)
1106 vindex = (*vfuns_p)++;
1107 vindex += first_vfun_index (t);
1108 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
1111 /* Add a virtual function to all the appropriate vtables for the class
1112 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1113 allocate a new slot in our table. If it is error_mark_node, we
1114 know that no other function from another vtable is overridden by X.
1115 VFUNS_P keeps track of how many virtuals there are in our
1116 main vtable for the type, and we build upon the NEW_VIRTUALS list
1120 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1122 tree *new_virtuals_p;
1123 tree *overridden_virtuals_p;
1126 tree t; /* Structure type. */
1130 /* If this function doesn't override anything from a base class, we
1131 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1132 override something, we keep it around and assign its DECL_VINDEX
1133 later, in modify_all_vtables. */
1134 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1135 /* We've already dealt with this function. */
1138 new_virtual = make_node (TREE_LIST);
1139 BV_FN (new_virtual) = fndecl;
1140 BV_DELTA (new_virtual) = integer_zero_node;
1142 if (DECL_VINDEX (fndecl) == error_mark_node)
1144 /* FNDECL is a new virtual function; it doesn't override any
1145 virtual function in a base class. */
1147 /* We remember that this was the base sub-object for rtti. */
1148 CLASSTYPE_RTTI (t) = t;
1150 /* Now assign virtual dispatch information. */
1151 set_vindex (t, fndecl, vfuns_p);
1152 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1154 /* Save the state we've computed on the NEW_VIRTUALS list. */
1155 TREE_CHAIN (new_virtual) = *new_virtuals_p;
1156 *new_virtuals_p = new_virtual;
1160 /* FNDECL overrides a function from a base class. */
1161 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1162 *overridden_virtuals_p = new_virtual;
1166 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
1167 the method after the class has already been defined because a
1168 declaration for it was seen. (Even though that is erroneous, we
1169 add the method for improved error recovery.) */
1172 add_method (type, method, error_p)
1177 int using = (DECL_CONTEXT (method) != type);
1182 if (!CLASSTYPE_METHOD_VEC (type))
1183 /* Make a new method vector. We start with 8 entries. We must
1184 allocate at least two (for constructors and destructors), and
1185 we're going to end up with an assignment operator at some point
1188 We could use a TREE_LIST for now, and convert it to a TREE_VEC
1189 in finish_struct, but we would probably waste more memory
1190 making the links in the list than we would by over-allocating
1191 the size of the vector here. Furthermore, we would complicate
1192 all the code that expects this to be a vector. */
1193 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1195 method_vec = CLASSTYPE_METHOD_VEC (type);
1196 len = TREE_VEC_LENGTH (method_vec);
1198 /* Constructors and destructors go in special slots. */
1199 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1200 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1201 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1202 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1205 /* See if we already have an entry with this name. */
1206 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
1207 if (!TREE_VEC_ELT (method_vec, slot)
1208 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1210 == DECL_NAME (method)))
1215 /* We need a bigger method vector. */
1219 /* In the non-error case, we are processing a class
1220 definition. Double the size of the vector to give room
1224 /* In the error case, the vector is already complete. We
1225 don't expect many errors, and the rest of the front-end
1226 will get confused if there are empty slots in the vector. */
1230 new_vec = make_tree_vec (new_len);
1231 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1232 (PTR) &TREE_VEC_ELT (new_vec, 0),
1233 len * sizeof (tree));
1235 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1238 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1240 /* Type conversion operators have to come before ordinary
1241 methods; add_conversions depends on this to speed up
1242 looking for conversion operators. So, if necessary, we
1243 slide some of the vector elements up. In theory, this
1244 makes this algorithm O(N^2) but we don't expect many
1245 conversion operators. */
1246 for (slot = 2; slot < len; ++slot)
1248 tree fn = TREE_VEC_ELT (method_vec, slot);
1251 /* There are no more entries in the vector, so we
1252 can insert the new conversion operator here. */
1255 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1256 /* We can insert the new function right at the
1261 if (!TREE_VEC_ELT (method_vec, slot))
1262 /* There is nothing in the Ith slot, so we can avoid
1267 /* We know the last slot in the vector is empty
1268 because we know that at this point there's room
1269 for a new function. */
1270 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1271 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1272 (len - slot - 1) * sizeof (tree));
1273 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1278 if (template_class_depth (type))
1279 /* TYPE is a template class. Don't issue any errors now; wait
1280 until instantiation time to complain. */
1286 /* Check to see if we've already got this method. */
1287 for (fns = TREE_VEC_ELT (method_vec, slot);
1289 fns = OVL_NEXT (fns))
1291 tree fn = OVL_CURRENT (fns);
1293 if (TREE_CODE (fn) != TREE_CODE (method))
1296 if (TREE_CODE (method) != TEMPLATE_DECL)
1298 /* [over.load] Member function declarations with the
1299 same name and the same parameter types cannot be
1300 overloaded if any of them is a static member
1301 function declaration. */
1302 if ((DECL_STATIC_FUNCTION_P (fn)
1303 != DECL_STATIC_FUNCTION_P (method))
1306 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1307 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1309 if (! DECL_STATIC_FUNCTION_P (fn))
1310 parms1 = TREE_CHAIN (parms1);
1311 if (! DECL_STATIC_FUNCTION_P (method))
1312 parms2 = TREE_CHAIN (parms2);
1314 if (compparms (parms1, parms2))
1317 /* Defer to the local function. */
1320 cp_error ("`%#D' and `%#D' cannot be overloaded",
1325 /* Since this is an ordinary function in a
1326 non-template class, it's mangled name can be used
1327 as a unique identifier. This technique is only
1328 an optimization; we would get the same results if
1329 we just used decls_match here. */
1330 if (DECL_ASSEMBLER_NAME (fn)
1331 != DECL_ASSEMBLER_NAME (method))
1334 else if (!decls_match (fn, method))
1337 /* There has already been a declaration of this method
1338 or member template. */
1339 cp_error_at ("`%D' has already been declared in `%T'",
1342 /* We don't call duplicate_decls here to merge the
1343 declarations because that will confuse things if the
1344 methods have inline definitions. In particular, we
1345 will crash while processing the definitions. */
1350 /* Actually insert the new method. */
1351 TREE_VEC_ELT (method_vec, slot)
1352 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1354 /* Add the new binding. */
1355 if (!DECL_CONSTRUCTOR_P (method)
1356 && !DECL_DESTRUCTOR_P (method))
1357 push_class_level_binding (DECL_NAME (method),
1358 TREE_VEC_ELT (method_vec, slot));
1361 /* Subroutines of finish_struct. */
1363 /* Look through the list of fields for this struct, deleting
1364 duplicates as we go. This must be recursive to handle
1367 FIELD is the field which may not appear anywhere in FIELDS.
1368 FIELD_PTR, if non-null, is the starting point at which
1369 chained deletions may take place.
1370 The value returned is the first acceptable entry found
1373 Note that anonymous fields which are not of UNION_TYPE are
1374 not duplicates, they are just anonymous fields. This happens
1375 when we have unnamed bitfields, for example. */
1378 delete_duplicate_fields_1 (field, fields)
1383 if (DECL_NAME (field) == 0)
1385 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1388 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1389 fields = delete_duplicate_fields_1 (x, fields);
1394 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1396 if (DECL_NAME (x) == 0)
1398 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1400 TYPE_FIELDS (TREE_TYPE (x))
1401 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1402 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1405 fields = TREE_CHAIN (fields);
1407 TREE_CHAIN (prev) = TREE_CHAIN (x);
1410 else if (TREE_CODE (field) == USING_DECL)
1411 /* A using declaration may is allowed to appear more than
1412 once. We'll prune these from the field list later, and
1413 handle_using_decl will complain about invalid multiple
1416 else if (DECL_NAME (field) == DECL_NAME (x))
1418 if (TREE_CODE (field) == CONST_DECL
1419 && TREE_CODE (x) == CONST_DECL)
1420 cp_error_at ("duplicate enum value `%D'", x);
1421 else if (TREE_CODE (field) == CONST_DECL
1422 || TREE_CODE (x) == CONST_DECL)
1423 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1425 else if (DECL_DECLARES_TYPE_P (field)
1426 && DECL_DECLARES_TYPE_P (x))
1428 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1430 cp_error_at ("duplicate nested type `%D'", x);
1432 else if (DECL_DECLARES_TYPE_P (field)
1433 || DECL_DECLARES_TYPE_P (x))
1435 /* Hide tag decls. */
1436 if ((TREE_CODE (field) == TYPE_DECL
1437 && DECL_ARTIFICIAL (field))
1438 || (TREE_CODE (x) == TYPE_DECL
1439 && DECL_ARTIFICIAL (x)))
1441 cp_error_at ("duplicate field `%D' (as type and non-type)",
1445 cp_error_at ("duplicate member `%D'", x);
1447 fields = TREE_CHAIN (fields);
1449 TREE_CHAIN (prev) = TREE_CHAIN (x);
1457 delete_duplicate_fields (fields)
1461 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1462 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1465 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1466 legit, otherwise return 0. */
1469 alter_access (t, fdecl, access)
1476 if (!DECL_LANG_SPECIFIC (fdecl))
1477 retrofit_lang_decl (fdecl);
1479 elem = purpose_member (t, DECL_ACCESS (fdecl));
1482 if (TREE_VALUE (elem) != access)
1484 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1485 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1487 error ("conflicting access specifications for field `%s', ignored",
1488 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1492 /* They're changing the access to the same thing they changed
1493 it to before. That's OK. */
1499 enforce_access (t, fdecl);
1500 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1506 /* Process the USING_DECL, which is a member of T. */
1509 handle_using_decl (using_decl, t)
1513 tree ctype = DECL_INITIAL (using_decl);
1514 tree name = DECL_NAME (using_decl);
1516 = TREE_PRIVATE (using_decl) ? access_private_node
1517 : TREE_PROTECTED (using_decl) ? access_protected_node
1518 : access_public_node;
1520 tree flist = NULL_TREE;
1523 binfo = binfo_or_else (ctype, t);
1527 if (name == constructor_name (ctype)
1528 || name == constructor_name_full (ctype))
1530 cp_error_at ("using-declaration for constructor", using_decl);
1534 fdecl = lookup_member (binfo, name, 0, 0);
1538 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1542 if (BASELINK_P (fdecl))
1543 /* Ignore base type this came from. */
1544 fdecl = TREE_VALUE (fdecl);
1546 old_value = IDENTIFIER_CLASS_VALUE (name);
1549 if (is_overloaded_fn (old_value))
1550 old_value = OVL_CURRENT (old_value);
1552 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1555 old_value = NULL_TREE;
1558 if (is_overloaded_fn (fdecl))
1563 else if (is_overloaded_fn (old_value))
1566 /* It's OK to use functions from a base when there are functions with
1567 the same name already present in the current class. */;
1570 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1571 cp_error_at (" because of local method `%#D' with same name",
1572 OVL_CURRENT (old_value));
1578 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1579 cp_error_at (" because of local field `%#D' with same name", old_value);
1583 /* Make type T see field decl FDECL with access ACCESS.*/
1585 for (; flist; flist = OVL_NEXT (flist))
1587 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1588 alter_access (t, OVL_CURRENT (flist), access);
1591 alter_access (t, fdecl, access);
1594 /* Run through the base clases of T, updating
1595 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1596 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1600 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1603 int *cant_have_default_ctor_p;
1604 int *cant_have_const_ctor_p;
1605 int *no_const_asn_ref_p;
1609 int seen_nearly_empty_base_p;
1612 binfos = TYPE_BINFO_BASETYPES (t);
1613 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1614 seen_nearly_empty_base_p = 0;
1616 /* An aggregate cannot have baseclasses. */
1617 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1619 for (i = 0; i < n_baseclasses; ++i)
1624 /* Figure out what base we're looking at. */
1625 base_binfo = TREE_VEC_ELT (binfos, i);
1626 basetype = TREE_TYPE (base_binfo);
1628 /* If the type of basetype is incomplete, then we already
1629 complained about that fact (and we should have fixed it up as
1631 if (!COMPLETE_TYPE_P (basetype))
1634 /* The base type is of incomplete type. It is
1635 probably best to pretend that it does not
1637 if (i == n_baseclasses-1)
1638 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1639 TREE_VEC_LENGTH (binfos) -= 1;
1641 for (j = i; j+1 < n_baseclasses; j++)
1642 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1646 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1647 here because the case of virtual functions but non-virtual
1648 dtor is handled in finish_struct_1. */
1649 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1650 && TYPE_HAS_DESTRUCTOR (basetype))
1651 cp_warning ("base class `%#T' has a non-virtual destructor",
1654 /* If the base class doesn't have copy constructors or
1655 assignment operators that take const references, then the
1656 derived class cannot have such a member automatically
1658 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1659 *cant_have_const_ctor_p = 1;
1660 if (TYPE_HAS_ASSIGN_REF (basetype)
1661 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1662 *no_const_asn_ref_p = 1;
1663 /* Similarly, if the base class doesn't have a default
1664 constructor, then the derived class won't have an
1665 automatically generated default constructor. */
1666 if (TYPE_HAS_CONSTRUCTOR (basetype)
1667 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1669 *cant_have_default_ctor_p = 1;
1670 if (! TYPE_HAS_CONSTRUCTOR (t))
1671 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1675 /* If the base class is not empty or nearly empty, then this
1676 class cannot be nearly empty. */
1677 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1678 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1679 /* And if there is more than one nearly empty base, then the
1680 derived class is not nearly empty either. */
1681 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1682 && seen_nearly_empty_base_p)
1683 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1684 /* If this is the first nearly empty base class, then remember
1686 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1687 seen_nearly_empty_base_p = 1;
1689 /* A lot of properties from the bases also apply to the derived
1691 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1692 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1693 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1694 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1695 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1696 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1697 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1698 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1699 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1700 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1702 /* Derived classes can implicitly become COMified if their bases
1704 if (CLASSTYPE_COM_INTERFACE (basetype))
1705 CLASSTYPE_COM_INTERFACE (t) = 1;
1706 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1709 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1711 CLASSTYPE_COM_INTERFACE (t) = 0;
1716 /* Called via dfs_walk from mark_primary_bases. Sets
1717 BINFO_PRIMARY_MARKED_P for BINFO, if appropriate. */
1720 dfs_mark_primary_bases (binfo, data)
1726 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1729 base_binfo = get_primary_binfo (binfo);
1731 if (TREE_VIA_VIRTUAL (base_binfo))
1737 shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1739 /* If this virtual base is not already primary somewhere else in
1740 the hiearchy, then we'll be using this copy. */
1741 if (!BINFO_PRIMARY_MARKED_P (shared_binfo))
1743 /* Make sure the CLASSTYPE_VBASECLASSES list contains the
1744 primary copy; it's the one that really exists. */
1745 if (base_binfo != shared_binfo)
1746 TREE_VALUE (purpose_member (BINFO_TYPE (base_binfo),
1747 CLASSTYPE_VBASECLASSES (type)))
1751 base_binfo = NULL_TREE;
1755 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1760 /* Set BINFO_PRIMARY_MARKED_P for all binfos in the hierarchy
1761 dominated by BINFO that are primary bases. */
1764 mark_primary_bases (type)
1769 /* Mark the TYPE_BINFO hierarchy. We need to mark primary bases in
1770 pre-order to deal with primary virtual bases. (The virtual base
1771 would be skipped if it were not marked as primary, and that
1772 requires getting to dfs_mark_primary_bases before
1773 dfs_skip_nonprimary_vbases_unmarkedp has a chance to skip the
1775 dfs_walk_real (TYPE_BINFO (type), dfs_mark_primary_bases, NULL,
1776 dfs_skip_nonprimary_vbases_unmarkedp, type);
1778 /* Now go through the virtual base classes in inheritance graph
1779 order. Any that are not already primary will need to be
1780 allocated in TYPE, and so we need to mark their primary bases. */
1781 for (vbases = TYPE_BINFO (type); vbases; vbases = TREE_CHAIN (vbases))
1785 /* Make sure that only BINFOs appear on this list.
1786 Historically, the TREE_CHAIN was used for other purposes, and
1787 we want to make sure that none of those uses remain. */
1788 my_friendly_assert (TREE_CODE (vbases) == TREE_VEC, 20000402);
1790 if (!TREE_VIA_VIRTUAL (vbases))
1793 vbase = binfo_for_vbase (BINFO_TYPE (vbases), type);
1794 if (BINFO_PRIMARY_MARKED_P (vbase))
1795 /* This virtual base was already included in the hierarchy, so
1796 there's nothing to do here. */
1799 /* Now, walk its bases. */
1800 dfs_walk_real (vbase, dfs_mark_primary_bases, NULL,
1801 dfs_skip_nonprimary_vbases_unmarkedp, type);
1805 /* Make the BINFO the primary base of T. */
1808 set_primary_base (t, binfo, vfuns_p)
1815 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1816 basetype = BINFO_TYPE (binfo);
1817 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1818 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1819 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1820 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1821 *vfuns_p = CLASSTYPE_VSIZE (basetype);
1824 /* Determine the primary class for T. */
1827 determine_primary_base (t, vfuns_p)
1831 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1835 /* If there are no baseclasses, there is certainly no primary base. */
1836 if (n_baseclasses == 0)
1839 type_binfo = TYPE_BINFO (t);
1841 for (i = 0; i < n_baseclasses; i++)
1843 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1844 tree basetype = BINFO_TYPE (base_binfo);
1846 if (TYPE_CONTAINS_VPTR_P (basetype))
1848 /* Even a virtual baseclass can contain our RTTI
1849 information. But, we prefer a non-virtual polymorphic
1851 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1852 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1854 /* A virtual baseclass can't be the primary base under the
1855 old ABI. And under the new ABI we still prefer a
1856 non-virtual base. */
1857 if (TREE_VIA_VIRTUAL (base_binfo))
1860 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1862 set_primary_base (t, base_binfo, vfuns_p);
1863 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1869 /* Only add unique vfields, and flatten them out as we go. */
1870 for (vfields = CLASSTYPE_VFIELDS (basetype);
1872 vfields = TREE_CHAIN (vfields))
1873 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1874 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1875 CLASSTYPE_VFIELDS (t)
1876 = tree_cons (base_binfo,
1877 VF_BASETYPE_VALUE (vfields),
1878 CLASSTYPE_VFIELDS (t));
1880 if (!flag_new_abi && *vfuns_p == 0)
1881 set_primary_base (t, base_binfo, vfuns_p);
1886 if (!TYPE_VFIELD (t))
1887 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1889 /* Mark the indirect primary bases. */
1890 for (vbases = CLASSTYPE_VBASECLASSES (t);
1892 vbases = TREE_CHAIN (vbases))
1894 tree binfo = TREE_VALUE (vbases);
1896 /* See if this virtual base is an indirect primary base. If so,
1897 it must be either a primary base or an indirect primary base
1898 in one of the direct bases. */
1899 for (i = 0; i < n_baseclasses; ++i)
1904 basetype = TYPE_BINFO_BASETYPE (t, i);
1905 for (v = CLASSTYPE_VBASECLASSES (basetype);
1909 tree b = TREE_VALUE (v);
1910 if ((BINFO_PRIMARY_MARKED_P (b)
1911 || BINFO_INDIRECT_PRIMARY_P (b))
1912 && same_type_p (BINFO_TYPE (b), BINFO_TYPE (binfo)))
1914 BINFO_INDIRECT_PRIMARY_P (binfo) = 1;
1919 /* If we've discovered that this virtual base is an indirect
1920 primary base, then we can move on to the next virtual
1922 if (BINFO_INDIRECT_PRIMARY_P (binfo))
1927 /* The new ABI allows for the use of a "nearly-empty" virtual base
1928 class as the primary base class if no non-virtual polymorphic
1929 base can be found. */
1930 if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1932 /* If not NULL, this is the best primary base candidate we have
1934 tree candidate = NULL_TREE;
1937 /* Loop over the baseclasses. */
1938 for (base_binfo = TYPE_BINFO (t);
1940 base_binfo = TREE_CHAIN (base_binfo))
1942 tree basetype = BINFO_TYPE (base_binfo);
1944 if (TREE_VIA_VIRTUAL (base_binfo)
1945 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1947 /* If this is not an indirect primary base, then it's
1948 definitely our primary base. */
1949 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1951 candidate = base_binfo;
1954 /* If this was an indirect primary base, it's still our
1955 primary base -- unless there's another nearly-empty
1956 virtual base that isn't an indirect primary base. */
1957 else if (!candidate)
1958 candidate = base_binfo;
1962 /* If we've got a primary base, use it. */
1965 set_primary_base (t, candidate, vfuns_p);
1966 CLASSTYPE_VFIELDS (t)
1967 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1971 /* Mark the primary base classes at this point. */
1972 mark_primary_bases (t);
1975 /* Set memoizing fields and bits of T (and its variants) for later
1979 finish_struct_bits (t)
1982 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1984 /* Fix up variants (if any). */
1985 tree variants = TYPE_NEXT_VARIANT (t);
1988 /* These fields are in the _TYPE part of the node, not in
1989 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1990 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1991 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1992 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1993 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1994 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1996 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1997 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1998 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1999 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2000 /* Copy whatever these are holding today. */
2001 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2002 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
2003 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2004 TYPE_SIZE (variants) = TYPE_SIZE (t);
2005 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
2006 variants = TYPE_NEXT_VARIANT (variants);
2009 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
2010 /* For a class w/o baseclasses, `finish_struct' has set
2011 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2012 definition). Similarly for a class whose base classes do not
2013 have vtables. When neither of these is true, we might have
2014 removed abstract virtuals (by providing a definition), added
2015 some (by declaring new ones), or redeclared ones from a base
2016 class. We need to recalculate what's really an abstract virtual
2017 at this point (by looking in the vtables). */
2018 get_pure_virtuals (t);
2022 /* Notice whether this class has type conversion functions defined. */
2023 tree binfo = TYPE_BINFO (t);
2024 tree binfos = BINFO_BASETYPES (binfo);
2027 for (i = n_baseclasses-1; i >= 0; i--)
2029 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2031 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
2035 /* If this type has a copy constructor, force its mode to be BLKmode, and
2036 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2037 be passed by invisible reference and prevent it from being returned in
2040 Also do this if the class has BLKmode but can still be returned in
2041 registers, since function_cannot_inline_p won't let us inline
2042 functions returning such a type. This affects the HP-PA. */
2043 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
2044 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
2045 && CLASSTYPE_NON_AGGREGATE (t)))
2048 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2049 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2051 TYPE_MODE (variants) = BLKmode;
2052 TREE_ADDRESSABLE (variants) = 1;
2057 /* Issue warnings about T having private constructors, but no friends,
2060 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2061 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2062 non-private static member functions. */
2065 maybe_warn_about_overly_private_class (t)
2068 int has_member_fn = 0;
2069 int has_nonprivate_method = 0;
2072 if (!warn_ctor_dtor_privacy
2073 /* If the class has friends, those entities might create and
2074 access instances, so we should not warn. */
2075 || (CLASSTYPE_FRIEND_CLASSES (t)
2076 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2077 /* We will have warned when the template was declared; there's
2078 no need to warn on every instantiation. */
2079 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2080 /* There's no reason to even consider warning about this
2084 /* We only issue one warning, if more than one applies, because
2085 otherwise, on code like:
2088 // Oops - forgot `public:'
2094 we warn several times about essentially the same problem. */
2096 /* Check to see if all (non-constructor, non-destructor) member
2097 functions are private. (Since there are no friends or
2098 non-private statics, we can't ever call any of the private member
2100 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2101 /* We're not interested in compiler-generated methods; they don't
2102 provide any way to call private members. */
2103 if (!DECL_ARTIFICIAL (fn))
2105 if (!TREE_PRIVATE (fn))
2107 if (DECL_STATIC_FUNCTION_P (fn))
2108 /* A non-private static member function is just like a
2109 friend; it can create and invoke private member
2110 functions, and be accessed without a class
2114 has_nonprivate_method = 1;
2117 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2121 if (!has_nonprivate_method && has_member_fn)
2123 /* There are no non-private methods, and there's at least one
2124 private member function that isn't a constructor or
2125 destructor. (If all the private members are
2126 constructors/destructors we want to use the code below that
2127 issues error messages specifically referring to
2128 constructors/destructors.) */
2130 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2131 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2132 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2133 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2135 has_nonprivate_method = 1;
2138 if (!has_nonprivate_method)
2140 cp_warning ("all member functions in class `%T' are private", t);
2145 /* Even if some of the member functions are non-private, the class
2146 won't be useful for much if all the constructors or destructors
2147 are private: such an object can never be created or destroyed. */
2148 if (TYPE_HAS_DESTRUCTOR (t))
2150 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2152 if (TREE_PRIVATE (dtor))
2154 cp_warning ("`%#T' only defines a private destructor and has no friends",
2160 if (TYPE_HAS_CONSTRUCTOR (t))
2162 int nonprivate_ctor = 0;
2164 /* If a non-template class does not define a copy
2165 constructor, one is defined for it, enabling it to avoid
2166 this warning. For a template class, this does not
2167 happen, and so we would normally get a warning on:
2169 template <class T> class C { private: C(); };
2171 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2172 complete non-template or fully instantiated classes have this
2174 if (!TYPE_HAS_INIT_REF (t))
2175 nonprivate_ctor = 1;
2177 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2181 tree ctor = OVL_CURRENT (fn);
2182 /* Ideally, we wouldn't count copy constructors (or, in
2183 fact, any constructor that takes an argument of the
2184 class type as a parameter) because such things cannot
2185 be used to construct an instance of the class unless
2186 you already have one. But, for now at least, we're
2188 if (! TREE_PRIVATE (ctor))
2190 nonprivate_ctor = 1;
2195 if (nonprivate_ctor == 0)
2197 cp_warning ("`%#T' only defines private constructors and has no friends",
2204 /* Function to help qsort sort FIELD_DECLs by name order. */
2207 field_decl_cmp (x, y)
2210 if (DECL_NAME (*x) == DECL_NAME (*y))
2211 /* A nontype is "greater" than a type. */
2212 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2213 if (DECL_NAME (*x) == NULL_TREE)
2215 if (DECL_NAME (*y) == NULL_TREE)
2217 if (DECL_NAME (*x) < DECL_NAME (*y))
2222 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2225 method_name_cmp (m1, m2)
2226 const tree *m1, *m2;
2228 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2230 if (*m1 == NULL_TREE)
2232 if (*m2 == NULL_TREE)
2234 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2239 /* Warn about duplicate methods in fn_fields. Also compact method
2240 lists so that lookup can be made faster.
2242 Data Structure: List of method lists. The outer list is a
2243 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2244 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2245 links the entire list of methods for TYPE_METHODS. Friends are
2246 chained in the same way as member functions (? TREE_CHAIN or
2247 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2248 list. That allows them to be quickly deleted, and requires no
2251 Sort methods that are not special (i.e., constructors, destructors,
2252 and type conversion operators) so that we can find them faster in
2256 finish_struct_methods (t)
2263 if (!TYPE_METHODS (t))
2265 /* Clear these for safety; perhaps some parsing error could set
2266 these incorrectly. */
2267 TYPE_HAS_CONSTRUCTOR (t) = 0;
2268 TYPE_HAS_DESTRUCTOR (t) = 0;
2269 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2273 method_vec = CLASSTYPE_METHOD_VEC (t);
2274 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2275 len = TREE_VEC_LENGTH (method_vec);
2277 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2278 and the next few with type conversion operators (if any). */
2279 for (fn_fields = TYPE_METHODS (t); fn_fields;
2280 fn_fields = TREE_CHAIN (fn_fields))
2281 /* Clear out this flag. */
2282 DECL_IN_AGGR_P (fn_fields) = 0;
2284 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2285 /* We thought there was a destructor, but there wasn't. Some
2286 parse errors cause this anomalous situation. */
2287 TYPE_HAS_DESTRUCTOR (t) = 0;
2289 /* Issue warnings about private constructors and such. If there are
2290 no methods, then some public defaults are generated. */
2291 maybe_warn_about_overly_private_class (t);
2293 /* Now sort the methods. */
2294 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2296 TREE_VEC_LENGTH (method_vec) = len;
2298 /* The type conversion ops have to live at the front of the vec, so we
2300 for (slot = 2; slot < len; ++slot)
2302 tree fn = TREE_VEC_ELT (method_vec, slot);
2304 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2308 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2309 (int (*)(const void *, const void *))method_name_cmp);
2312 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2315 duplicate_tag_error (t)
2318 cp_error ("redefinition of `%#T'", t);
2319 cp_error_at ("previous definition here", t);
2321 /* Pretend we haven't defined this type. */
2323 /* All of the component_decl's were TREE_CHAINed together in the parser.
2324 finish_struct_methods walks these chains and assembles all methods with
2325 the same base name into DECL_CHAINs. Now we don't need the parser chains
2326 anymore, so we unravel them. */
2328 /* This used to be in finish_struct, but it turns out that the
2329 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2331 if (CLASSTYPE_METHOD_VEC (t))
2333 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2334 int i, len = TREE_VEC_LENGTH (method_vec);
2335 for (i = 0; i < len; i++)
2337 tree unchain = TREE_VEC_ELT (method_vec, i);
2338 while (unchain != NULL_TREE)
2340 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2341 unchain = OVL_NEXT (unchain);
2346 if (TYPE_LANG_SPECIFIC (t))
2348 tree binfo = TYPE_BINFO (t);
2349 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2350 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2351 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2352 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2354 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2355 BINFO_BASETYPES(binfo) = NULL_TREE;
2357 TYPE_BINFO (t) = binfo;
2358 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2359 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2360 TYPE_REDEFINED (t) = 1;
2361 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2362 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2364 TYPE_SIZE (t) = NULL_TREE;
2365 TYPE_MODE (t) = VOIDmode;
2366 TYPE_FIELDS (t) = NULL_TREE;
2367 TYPE_METHODS (t) = NULL_TREE;
2368 TYPE_VFIELD (t) = NULL_TREE;
2369 TYPE_CONTEXT (t) = NULL_TREE;
2370 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2373 /* Make the BINFO's vtablehave N entries, including RTTI entries,
2374 vbase and vcall offsets, etc. Set its type and call the backend
2378 layout_vtable_decl (binfo, n)
2386 itype = size_int (n);
2387 atype = build_cplus_array_type (vtable_entry_type,
2388 build_index_type (itype));
2389 layout_type (atype);
2391 /* We may have to grow the vtable. */
2392 vtable = get_vtbl_decl_for_binfo (binfo);
2393 if (!same_type_p (TREE_TYPE (vtable), atype))
2395 TREE_TYPE (vtable) = atype;
2396 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2397 layout_decl (vtable, 0);
2399 /* At one time the vtable info was grabbed 2 words at a time. This
2400 fails on Sparc unless you have 8-byte alignment. */
2401 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2402 DECL_ALIGN (vtable));
2406 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2407 have the same signature. */
2410 same_signature_p (fndecl, base_fndecl)
2411 tree fndecl, base_fndecl;
2413 /* One destructor overrides another if they are the same kind of
2415 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2416 && special_function_p (base_fndecl) == special_function_p (fndecl))
2418 /* But a non-destructor never overrides a destructor, nor vice
2419 versa, nor do different kinds of destructors override
2420 one-another. For example, a complete object destructor does not
2421 override a deleting destructor. */
2422 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2425 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2427 tree types, base_types;
2428 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2429 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2430 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2431 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2432 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2438 typedef struct find_final_overrider_data_s {
2439 /* The function for which we are trying to find a final overrider. */
2441 /* The base class in which the function was declared. */
2442 tree declaring_base;
2443 /* The most derived class in the hierarchy. */
2444 tree most_derived_type;
2445 /* The final overriding function. */
2447 /* The BINFO for the class in which the final overriding function
2449 tree overriding_base;
2450 } find_final_overrider_data;
2452 /* Called from find_final_overrider via dfs_walk. */
2455 dfs_find_final_overrider (binfo, data)
2459 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2461 if (same_type_p (BINFO_TYPE (binfo),
2462 BINFO_TYPE (ffod->declaring_base))
2463 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2464 BINFO_OFFSET (ffod->declaring_base)))
2469 /* We haven't found an overrider yet. */
2471 /* We've found a path to the declaring base. Walk down the path
2472 looking for an overrider for FN. */
2473 for (path = reverse_path (binfo);
2475 path = TREE_CHAIN (path))
2477 for (method = TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path)));
2479 method = TREE_CHAIN (method))
2480 if (DECL_VIRTUAL_P (method)
2481 && same_signature_p (method, ffod->fn))
2488 /* If we found an overrider, record the overriding function, and
2489 the base from which it came. */
2494 /* Assume the path is non-virtual. See if there are any
2495 virtual bases from (but not including) the overrider up
2496 to and including the base where the function is
2498 for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
2499 if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
2501 base = ffod->declaring_base;
2502 while (BINFO_PRIMARY_MARKED_P (base))
2504 BINFO_OVERRIDE_ALONG_VIRTUAL_PATH_P (base) = 1;
2505 base = BINFO_INHERITANCE_CHAIN (base);
2507 BINFO_OVERRIDE_ALONG_VIRTUAL_PATH_P (base) = 1;
2511 if (ffod->overriding_fn && ffod->overriding_fn != method)
2513 /* We've found a different overrider along a different
2514 path. That can be OK if the new one overrides the
2517 struct S { virtual void f(); };
2518 struct T : public virtual S { virtual void f(); };
2519 struct U : public virtual S, public virtual T {};
2521 Here `T::f' is the final overrider for `S::f'. */
2522 if (strictly_overrides (method, ffod->overriding_fn))
2524 ffod->overriding_fn = method;
2525 ffod->overriding_base = TREE_VALUE (path);
2527 else if (!strictly_overrides (ffod->overriding_fn, method))
2529 cp_error ("no unique final overrider for `%D' in `%T'",
2530 ffod->most_derived_type,
2532 cp_error ("candidates are: `%#D'", ffod->overriding_fn);
2533 cp_error (" `%#D'", method);
2534 return error_mark_node;
2537 else if (ffod->overriding_base
2538 && (!tree_int_cst_equal
2539 (BINFO_OFFSET (TREE_VALUE (path)),
2540 BINFO_OFFSET (ffod->overriding_base))))
2542 /* We've found two instances of the same base that
2543 provide overriders. */
2544 cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'",
2546 BINFO_TYPE (ffod->overriding_base),
2547 ffod->most_derived_type);
2548 return error_mark_node;
2552 ffod->overriding_fn = method;
2553 ffod->overriding_base = TREE_VALUE (path);
2561 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2562 FN and whose TREE_VALUE is the binfo for the base where the
2563 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2564 base object in which FN is declared. */
2567 find_final_overrider (t, binfo, fn)
2572 find_final_overrider_data ffod;
2574 /* Getting this right is a little tricky. This is legal:
2576 struct S { virtual void f (); };
2577 struct T { virtual void f (); };
2578 struct U : public S, public T { };
2580 even though calling `f' in `U' is ambiguous. But,
2582 struct R { virtual void f(); };
2583 struct S : virtual public R { virtual void f (); };
2584 struct T : virtual public R { virtual void f (); };
2585 struct U : public S, public T { };
2587 is not -- there's no way to decide whether to put `S::f' or
2588 `T::f' in the vtable for `R'.
2590 The solution is to look at all paths to BINFO. If we find
2591 different overriders along any two, then there is a problem. */
2593 ffod.declaring_base = binfo;
2594 ffod.most_derived_type = t;
2595 ffod.overriding_fn = NULL_TREE;
2596 ffod.overriding_base = NULL_TREE;
2598 if (dfs_walk (TYPE_BINFO (t),
2599 dfs_find_final_overrider,
2602 return error_mark_node;
2604 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2607 /* Update a entry in the vtable for BINFO, which is in the hierarchy
2608 dominated by T. FN has been overridden in BINFO; VIRTUALS points
2609 to the corresponding position in the BINFO_VIRTUALS list. */
2612 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2622 int generate_thunk_with_vtable_p;
2624 /* Find the function which originally caused this vtable
2625 entry to be present. */
2632 primary_base = get_primary_binfo (b);
2636 for (f = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (primary_base)));
2639 if (same_signature_p (BV_FN (f), fn))
2649 /* Find the final overrider. */
2650 overrider = find_final_overrider (t, b, fn);
2651 if (overrider == error_mark_node)
2654 /* Compute the constant adjustment to the `this' pointer. The
2655 `this' pointer, when this function is called, will point at the
2656 class whose vtable this is. */
2657 delta = size_binop (PLUS_EXPR,
2658 get_derived_offset (binfo,
2659 DECL_VIRTUAL_CONTEXT (fn)),
2660 BINFO_OFFSET (binfo));
2662 /* Assume that we will produce a thunk that convert all the way to
2663 the final overrider, and not to an intermediate virtual base. */
2664 virtual_base = NULL_TREE;
2666 /* Assume that we will always generate thunks with the vtables that
2668 generate_thunk_with_vtable_p = 1;
2670 /* Under the new ABI, we will convert to an intermediate virtual
2671 base first, and then use the vcall offset located there to finish
2677 /* If we find BINFO, then the final overrider is in a class
2678 derived from BINFO, so the thunks can be generated with
2679 the final overrider. */
2681 && same_type_p (BINFO_TYPE (b), BINFO_TYPE (binfo)))
2682 generate_thunk_with_vtable_p = 0;
2684 /* If we find the final overrider, then we can stop
2686 if (same_type_p (BINFO_TYPE (b),
2687 BINFO_TYPE (TREE_VALUE (overrider))))
2690 /* If we find a virtual base, and we haven't yet found the
2691 overrider, then there is a virtual base between the
2692 declaring base and the final overrider. */
2693 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2695 generate_thunk_with_vtable_p = 1;
2699 b = BINFO_INHERITANCE_CHAIN (b);
2703 virtual_base = NULL_TREE;
2706 /* The `this' pointer needs to be adjusted to the nearest virtual
2708 delta = size_diffop (BINFO_OFFSET (virtual_base), delta);
2710 /* The `this' pointer needs to be adjusted from pointing to
2711 BINFO to pointing at the base where the final overrider
2713 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)), delta);
2715 modify_vtable_entry (t,
2717 TREE_PURPOSE (overrider),
2722 BV_USE_VCALL_INDEX_P (*virtuals) = 1;
2723 if (generate_thunk_with_vtable_p)
2724 BV_GENERATE_THUNK_WITH_VTABLE_P (*virtuals) = 1;
2727 /* Called from modify_all_vtables via dfs_walk. */
2730 dfs_modify_vtables (binfo, data)
2734 if (/* There's no need to modify the vtable for a primary base;
2735 we're not going to use that vtable anyhow. */
2736 !BINFO_PRIMARY_MARKED_P (binfo)
2737 /* Similarly, a base without a vtable needs no modification. */
2738 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2746 /* If we're supporting RTTI then we always need a new vtable to
2747 point to the RTTI information. Under the new ABI we may need
2748 a new vtable to contain vcall and vbase offsets. */
2749 if (flag_rtti || flag_new_abi)
2750 make_new_vtable (t, binfo);
2752 /* Now, go through each of the virtual functions in the virtual
2753 function table for BINFO. Find the final overrider, and
2754 update the BINFO_VIRTUALS list appropriately. */
2755 for (virtuals = BINFO_VIRTUALS (binfo),
2756 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2758 virtuals = TREE_CHAIN (virtuals),
2759 old_virtuals = TREE_CHAIN (old_virtuals))
2760 update_vtable_entry_for_fn (t,
2762 BV_FN (old_virtuals),
2766 SET_BINFO_MARKED (binfo);
2771 /* Update all of the primary and secondary vtables for T. Create new
2772 vtables as required, and initialize their RTTI information. Each
2773 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2774 function from a base class; find and modify the appropriate entries
2775 to point to the overriding functions. Returns a list, in
2776 declaration order, of the functions that are overridden in this
2777 class, but do not appear in the primary base class vtable, and
2778 which should therefore be appended to the end of the vtable for T. */
2781 modify_all_vtables (t, vfuns_p, overridden_virtuals)
2784 tree overridden_virtuals;
2788 binfo = TYPE_BINFO (t);
2790 /* Update all of the vtables. */
2793 dfs_unmarked_real_bases_queue_p,
2795 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2797 /* If we should include overriding functions for secondary vtables
2798 in our primary vtable, add them now. */
2799 if (all_overridden_vfuns_in_vtables_p ())
2801 tree *fnsp = &overridden_virtuals;
2805 tree fn = TREE_VALUE (*fnsp);
2807 if (!BINFO_VIRTUALS (binfo)
2808 || !value_member (fn, BINFO_VIRTUALS (binfo)))
2810 /* Set the vtable index. */
2811 set_vindex (t, fn, vfuns_p);
2812 /* We don't need to convert to a base class when calling
2814 DECL_VIRTUAL_CONTEXT (fn) = t;
2816 /* We don't need to adjust the `this' pointer when
2817 calling this function. */
2818 BV_DELTA (*fnsp) = integer_zero_node;
2819 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2821 /* This is an overridden function not already in our
2823 fnsp = &TREE_CHAIN (*fnsp);
2826 /* We've already got an entry for this function. Skip
2828 *fnsp = TREE_CHAIN (*fnsp);
2832 overridden_virtuals = NULL_TREE;
2834 return overridden_virtuals;
2837 /* Here, we already know that they match in every respect.
2838 All we have to check is where they had their declarations. */
2841 strictly_overrides (fndecl1, fndecl2)
2842 tree fndecl1, fndecl2;
2844 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
2845 DECL_CONTEXT (fndecl1),
2847 if (distance == -2 || distance > 0)
2852 /* Get the base virtual function declarations in T that are either
2853 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2854 the overrider/hider. */
2857 get_basefndecls (fndecl, t)
2860 tree methods = TYPE_METHODS (t);
2861 tree base_fndecls = NULL_TREE;
2862 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2863 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2867 if (TREE_CODE (methods) == FUNCTION_DECL
2868 && DECL_VINDEX (methods) != NULL_TREE
2869 && DECL_NAME (fndecl) == DECL_NAME (methods))
2870 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
2872 methods = TREE_CHAIN (methods);
2876 return base_fndecls;
2878 for (i = 0; i < n_baseclasses; i++)
2880 tree base_binfo = TREE_VEC_ELT (binfos, i);
2881 tree basetype = BINFO_TYPE (base_binfo);
2883 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2887 return base_fndecls;
2890 /* Mark the functions that have been hidden with their overriders.
2891 Since we start out with all functions already marked with a hider,
2892 no need to mark functions that are just hidden.
2894 Subroutine of warn_hidden. */
2897 mark_overriders (fndecl, base_fndecls)
2898 tree fndecl, base_fndecls;
2900 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2901 if (same_signature_p (fndecl, TREE_VALUE (base_fndecls)))
2902 TREE_PURPOSE (base_fndecls) = fndecl;
2905 /* If this declaration supersedes the declaration of
2906 a method declared virtual in the base class, then
2907 mark this field as being virtual as well. */
2910 check_for_override (decl, ctype)
2913 if (TREE_CODE (decl) == TEMPLATE_DECL)
2914 /* In [temp.mem] we have:
2916 A specialization of a member function template does not
2917 override a virtual function from a base class. */
2919 if ((DECL_DESTRUCTOR_P (decl)
2920 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2921 && look_for_overrides (ctype, decl)
2922 && !DECL_STATIC_FUNCTION_P (decl))
2924 /* Set DECL_VINDEX to a value that is neither an
2925 INTEGER_CST nor the error_mark_node so that
2926 add_virtual_function will realize this is an
2927 overriding function. */
2928 DECL_VINDEX (decl) = decl;
2930 if (DECL_VIRTUAL_P (decl))
2932 if (DECL_VINDEX (decl) == NULL_TREE)
2933 DECL_VINDEX (decl) = error_mark_node;
2934 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2938 /* Warn about hidden virtual functions that are not overridden in t.
2939 We know that constructors and destructors don't apply. */
2945 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2946 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2949 /* We go through each separately named virtual function. */
2950 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2952 tree fns = TREE_VEC_ELT (method_vec, i);
2953 tree fndecl = NULL_TREE;
2955 tree base_fndecls = NULL_TREE;
2956 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2957 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2959 /* First see if we have any virtual functions in this batch. */
2960 for (; fns; fns = OVL_NEXT (fns))
2962 fndecl = OVL_CURRENT (fns);
2963 if (DECL_VINDEX (fndecl))
2967 if (fns == NULL_TREE)
2970 /* First we get a list of all possible functions that might be
2971 hidden from each base class. */
2972 for (i = 0; i < n_baseclasses; i++)
2974 tree base_binfo = TREE_VEC_ELT (binfos, i);
2975 tree basetype = BINFO_TYPE (base_binfo);
2977 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2981 fns = OVL_NEXT (fns);
2983 /* ...then mark up all the base functions with overriders, preferring
2984 overriders to hiders. */
2986 for (; fns; fns = OVL_NEXT (fns))
2988 fndecl = OVL_CURRENT (fns);
2989 if (DECL_VINDEX (fndecl))
2990 mark_overriders (fndecl, base_fndecls);
2993 /* Now give a warning for all base functions without overriders,
2994 as they are hidden. */
2995 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2996 if (!same_signature_p (TREE_PURPOSE (base_fndecls),
2997 TREE_VALUE (base_fndecls)))
2999 /* Here we know it is a hider, and no overrider exists. */
3000 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3001 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3006 /* Check for things that are invalid. There are probably plenty of other
3007 things we should check for also. */
3010 finish_struct_anon (t)
3015 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3017 if (TREE_STATIC (field))
3019 if (TREE_CODE (field) != FIELD_DECL)
3022 if (DECL_NAME (field) == NULL_TREE
3023 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3025 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3026 for (; elt; elt = TREE_CHAIN (elt))
3028 if (DECL_ARTIFICIAL (elt))
3031 if (DECL_NAME (elt) == constructor_name (t))
3032 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
3035 if (TREE_CODE (elt) != FIELD_DECL)
3037 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3042 if (TREE_PRIVATE (elt))
3043 cp_pedwarn_at ("private member `%#D' in anonymous union",
3045 else if (TREE_PROTECTED (elt))
3046 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3049 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3050 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3056 /* Create default constructors, assignment operators, and so forth for
3057 the type indicated by T, if they are needed.
3058 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3059 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
3060 class cannot have a default constructor, copy constructor taking a
3061 const reference argument, or an assignment operator taking a const
3062 reference, respectively. If a virtual destructor is created, its
3063 DECL is returned; otherwise the return value is NULL_TREE. */
3066 add_implicitly_declared_members (t, cant_have_default_ctor,
3067 cant_have_const_cctor,
3068 cant_have_const_assignment)
3070 int cant_have_default_ctor;
3071 int cant_have_const_cctor;
3072 int cant_have_const_assignment;
3075 tree implicit_fns = NULL_TREE;
3076 tree virtual_dtor = NULL_TREE;
3080 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3082 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
3083 check_for_override (default_fn, t);
3085 /* If we couldn't make it work, then pretend we didn't need it. */
3086 if (default_fn == void_type_node)
3087 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
3090 TREE_CHAIN (default_fn) = implicit_fns;
3091 implicit_fns = default_fn;
3093 if (DECL_VINDEX (default_fn))
3094 virtual_dtor = default_fn;
3098 /* Any non-implicit destructor is non-trivial. */
3099 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3101 /* Default constructor. */
3102 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3104 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
3105 TREE_CHAIN (default_fn) = implicit_fns;
3106 implicit_fns = default_fn;
3109 /* Copy constructor. */
3110 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3112 /* ARM 12.18: You get either X(X&) or X(const X&), but
3115 = implicitly_declare_fn (sfk_copy_constructor, t,
3116 /*const_p=*/!cant_have_const_cctor);
3117 TREE_CHAIN (default_fn) = implicit_fns;
3118 implicit_fns = default_fn;
3121 /* Assignment operator. */
3122 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3125 = implicitly_declare_fn (sfk_assignment_operator, t,
3126 /*const_p=*/!cant_have_const_assignment);
3127 TREE_CHAIN (default_fn) = implicit_fns;
3128 implicit_fns = default_fn;
3131 /* Now, hook all of the new functions on to TYPE_METHODS,
3132 and add them to the CLASSTYPE_METHOD_VEC. */
3133 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3134 add_method (t, *f, /*error_p=*/0);
3135 *f = TYPE_METHODS (t);
3136 TYPE_METHODS (t) = implicit_fns;
3138 return virtual_dtor;
3141 /* Subroutine of finish_struct_1. Recursively count the number of fields
3142 in TYPE, including anonymous union members. */
3145 count_fields (fields)
3150 for (x = fields; x; x = TREE_CHAIN (x))
3152 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3153 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3160 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3161 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3164 add_fields_to_vec (fields, field_vec, idx)
3165 tree fields, field_vec;
3169 for (x = fields; x; x = TREE_CHAIN (x))
3171 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3172 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3174 TREE_VEC_ELT (field_vec, idx++) = x;
3179 /* FIELD is a bit-field. We are finishing the processing for its
3180 enclosing type. Issue any appropriate messages and set appropriate
3184 check_bitfield_decl (field)
3187 tree type = TREE_TYPE (field);
3190 /* Detect invalid bit-field type. */
3191 if (DECL_INITIAL (field)
3192 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3194 cp_error_at ("bit-field `%#D' with non-integral type", field);
3195 w = error_mark_node;
3198 /* Detect and ignore out of range field width. */
3199 if (DECL_INITIAL (field))
3201 w = DECL_INITIAL (field);
3203 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3206 /* detect invalid field size. */
3207 if (TREE_CODE (w) == CONST_DECL)
3208 w = DECL_INITIAL (w);
3210 w = decl_constant_value (w);
3212 if (TREE_CODE (w) != INTEGER_CST)
3214 cp_error_at ("bit-field `%D' width not an integer constant",
3216 w = error_mark_node;
3218 else if (tree_int_cst_sgn (w) < 0)
3220 cp_error_at ("negative width in bit-field `%D'", field);
3221 w = error_mark_node;
3223 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3225 cp_error_at ("zero width for bit-field `%D'", field);
3226 w = error_mark_node;
3228 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3229 && TREE_CODE (type) != ENUMERAL_TYPE
3230 && TREE_CODE (type) != BOOLEAN_TYPE)
3231 cp_warning_at ("width of `%D' exceeds its type", field);
3232 else if (TREE_CODE (type) == ENUMERAL_TYPE
3233 && (0 > compare_tree_int (w,
3234 min_precision (TYPE_MIN_VALUE (type),
3235 TREE_UNSIGNED (type)))
3236 || 0 > compare_tree_int (w,
3238 (TYPE_MAX_VALUE (type),
3239 TREE_UNSIGNED (type)))))
3240 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3244 /* Remove the bit-field width indicator so that the rest of the
3245 compiler does not treat that value as an initializer. */
3246 DECL_INITIAL (field) = NULL_TREE;
3248 if (w != error_mark_node)
3250 DECL_SIZE (field) = convert (bitsizetype, w);
3251 DECL_BIT_FIELD (field) = 1;
3253 if (integer_zerop (w))
3255 #ifdef EMPTY_FIELD_BOUNDARY
3256 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3257 EMPTY_FIELD_BOUNDARY);
3259 #ifdef PCC_BITFIELD_TYPE_MATTERS
3260 if (PCC_BITFIELD_TYPE_MATTERS)
3262 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3264 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3271 /* Non-bit-fields are aligned for their type. */
3272 DECL_BIT_FIELD (field) = 0;
3273 CLEAR_DECL_C_BIT_FIELD (field);
3274 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3275 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3279 /* FIELD is a non bit-field. We are finishing the processing for its
3280 enclosing type T. Issue any appropriate messages and set appropriate
3284 check_field_decl (field, t, cant_have_const_ctor,
3285 cant_have_default_ctor, no_const_asn_ref,
3286 any_default_members)
3289 int *cant_have_const_ctor;
3290 int *cant_have_default_ctor;
3291 int *no_const_asn_ref;
3292 int *any_default_members;
3294 tree type = strip_array_types (TREE_TYPE (field));
3296 /* An anonymous union cannot contain any fields which would change
3297 the settings of CANT_HAVE_CONST_CTOR and friends. */
3298 if (ANON_UNION_TYPE_P (type))
3300 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3301 structs. So, we recurse through their fields here. */
3302 else if (ANON_AGGR_TYPE_P (type))
3306 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3307 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3308 check_field_decl (fields, t, cant_have_const_ctor,
3309 cant_have_default_ctor, no_const_asn_ref,
3310 any_default_members);
3312 /* Check members with class type for constructors, destructors,
3314 else if (CLASS_TYPE_P (type))
3316 /* Never let anything with uninheritable virtuals
3317 make it through without complaint. */
3318 abstract_virtuals_error (field, type);
3320 if (TREE_CODE (t) == UNION_TYPE)
3322 if (TYPE_NEEDS_CONSTRUCTING (type))
3323 cp_error_at ("member `%#D' with constructor not allowed in union",
3325 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3326 cp_error_at ("member `%#D' with destructor not allowed in union",
3328 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3329 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3334 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3335 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3336 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3337 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3338 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3341 if (!TYPE_HAS_CONST_INIT_REF (type))
3342 *cant_have_const_ctor = 1;
3344 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3345 *no_const_asn_ref = 1;
3347 if (TYPE_HAS_CONSTRUCTOR (type)
3348 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3349 *cant_have_default_ctor = 1;
3351 if (DECL_INITIAL (field) != NULL_TREE)
3353 /* `build_class_init_list' does not recognize
3355 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3356 cp_error_at ("multiple fields in union `%T' initialized");
3357 *any_default_members = 1;
3360 /* Non-bit-fields are aligned for their type, except packed fields
3361 which require only BITS_PER_UNIT alignment. */
3362 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3363 (DECL_PACKED (field)
3365 : TYPE_ALIGN (TREE_TYPE (field))));
3366 if (! DECL_PACKED (field))
3367 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3370 /* Check the data members (both static and non-static), class-scoped
3371 typedefs, etc., appearing in the declaration of T. Issue
3372 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3373 declaration order) of access declarations; each TREE_VALUE in this
3374 list is a USING_DECL.
3376 In addition, set the following flags:
3379 The class is empty, i.e., contains no non-static data members.
3381 CANT_HAVE_DEFAULT_CTOR_P
3382 This class cannot have an implicitly generated default
3385 CANT_HAVE_CONST_CTOR_P
3386 This class cannot have an implicitly generated copy constructor
3387 taking a const reference.
3389 CANT_HAVE_CONST_ASN_REF
3390 This class cannot have an implicitly generated assignment
3391 operator taking a const reference.
3393 All of these flags should be initialized before calling this
3396 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3397 fields can be added by adding to this chain. */
3400 check_field_decls (t, access_decls, empty_p,
3401 cant_have_default_ctor_p, cant_have_const_ctor_p,
3406 int *cant_have_default_ctor_p;
3407 int *cant_have_const_ctor_p;
3408 int *no_const_asn_ref_p;
3413 int any_default_members;
3415 /* First, delete any duplicate fields. */
3416 delete_duplicate_fields (TYPE_FIELDS (t));
3418 /* Assume there are no access declarations. */
3419 *access_decls = NULL_TREE;
3420 /* Assume this class has no pointer members. */
3422 /* Assume none of the members of this class have default
3424 any_default_members = 0;
3426 for (field = &TYPE_FIELDS (t); *field; field = next)
3429 tree type = TREE_TYPE (x);
3431 GNU_xref_member (current_class_name, x);
3433 next = &TREE_CHAIN (x);
3435 if (TREE_CODE (x) == FIELD_DECL)
3437 DECL_PACKED (x) |= TYPE_PACKED (t);
3439 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3440 /* We don't treat zero-width bitfields as making a class
3445 /* The class is non-empty. */
3447 /* The class is not even nearly empty. */
3448 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3452 if (TREE_CODE (x) == USING_DECL)
3454 /* Prune the access declaration from the list of fields. */
3455 *field = TREE_CHAIN (x);
3457 /* Save the access declarations for our caller. */
3458 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3460 /* Since we've reset *FIELD there's no reason to skip to the
3466 if (TREE_CODE (x) == TYPE_DECL
3467 || TREE_CODE (x) == TEMPLATE_DECL)
3470 /* If we've gotten this far, it's a data member, possibly static,
3471 or an enumerator. */
3473 DECL_CONTEXT (x) = t;
3475 /* ``A local class cannot have static data members.'' ARM 9.4 */
3476 if (current_function_decl && TREE_STATIC (x))
3477 cp_error_at ("field `%D' in local class cannot be static", x);
3479 /* Perform error checking that did not get done in
3481 if (TREE_CODE (type) == FUNCTION_TYPE)
3483 cp_error_at ("field `%D' invalidly declared function type",
3485 type = build_pointer_type (type);
3486 TREE_TYPE (x) = type;
3488 else if (TREE_CODE (type) == METHOD_TYPE)
3490 cp_error_at ("field `%D' invalidly declared method type", x);
3491 type = build_pointer_type (type);
3492 TREE_TYPE (x) = type;
3494 else if (TREE_CODE (type) == OFFSET_TYPE)
3496 cp_error_at ("field `%D' invalidly declared offset type", x);
3497 type = build_pointer_type (type);
3498 TREE_TYPE (x) = type;
3501 if (type == error_mark_node)
3504 /* When this goes into scope, it will be a non-local reference. */
3505 DECL_NONLOCAL (x) = 1;
3507 if (TREE_CODE (x) == CONST_DECL)
3510 if (TREE_CODE (x) == VAR_DECL)
3512 if (TREE_CODE (t) == UNION_TYPE)
3513 /* Unions cannot have static members. */
3514 cp_error_at ("field `%D' declared static in union", x);
3519 /* Now it can only be a FIELD_DECL. */
3521 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3522 CLASSTYPE_NON_AGGREGATE (t) = 1;
3524 /* If this is of reference type, check if it needs an init.
3525 Also do a little ANSI jig if necessary. */
3526 if (TREE_CODE (type) == REFERENCE_TYPE)
3528 CLASSTYPE_NON_POD_P (t) = 1;
3529 if (DECL_INITIAL (x) == NULL_TREE)
3530 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3532 /* ARM $12.6.2: [A member initializer list] (or, for an
3533 aggregate, initialization by a brace-enclosed list) is the
3534 only way to initialize nonstatic const and reference
3536 *cant_have_default_ctor_p = 1;
3537 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3539 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3540 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3543 type = strip_array_types (type);
3545 if (TREE_CODE (type) == POINTER_TYPE)
3548 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3549 CLASSTYPE_HAS_MUTABLE (t) = 1;
3551 if (! pod_type_p (type)
3552 /* For some reason, pointers to members are POD types themselves,
3553 but are not allowed in POD structs. Silly. */
3554 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3555 CLASSTYPE_NON_POD_P (t) = 1;
3557 /* If any field is const, the structure type is pseudo-const. */
3558 if (CP_TYPE_CONST_P (type))
3560 C_TYPE_FIELDS_READONLY (t) = 1;
3561 if (DECL_INITIAL (x) == NULL_TREE)
3562 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3564 /* ARM $12.6.2: [A member initializer list] (or, for an
3565 aggregate, initialization by a brace-enclosed list) is the
3566 only way to initialize nonstatic const and reference
3568 *cant_have_default_ctor_p = 1;
3569 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3571 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3572 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3574 /* A field that is pseudo-const makes the structure likewise. */
3575 else if (IS_AGGR_TYPE (type))
3577 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3578 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3579 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3582 /* Core issue 80: A nonstatic data member is required to have a
3583 different name from the class iff the class has a
3584 user-defined constructor. */
3585 if (DECL_NAME (x) == constructor_name (t)
3586 && TYPE_HAS_CONSTRUCTOR (t))
3587 cp_pedwarn_at ("field `%#D' with same name as class", x);
3589 /* We set DECL_C_BIT_FIELD in grokbitfield.
3590 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3591 if (DECL_C_BIT_FIELD (x))
3592 check_bitfield_decl (x);
3594 check_field_decl (x, t,
3595 cant_have_const_ctor_p,
3596 cant_have_default_ctor_p,
3598 &any_default_members);
3601 /* Effective C++ rule 11. */
3602 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3603 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3605 cp_warning ("`%#T' has pointer data members", t);
3607 if (! TYPE_HAS_INIT_REF (t))
3609 cp_warning (" but does not override `%T(const %T&)'", t, t);
3610 if (! TYPE_HAS_ASSIGN_REF (t))
3611 cp_warning (" or `operator=(const %T&)'", t);
3613 else if (! TYPE_HAS_ASSIGN_REF (t))
3614 cp_warning (" but does not override `operator=(const %T&)'", t);
3618 /* Check anonymous struct/anonymous union fields. */
3619 finish_struct_anon (t);
3621 /* We've built up the list of access declarations in reverse order.
3623 *access_decls = nreverse (*access_decls);
3626 /* Return a FIELD_DECL for a pointer-to-virtual-table or
3627 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
3628 field are as indicated. The CLASS_TYPE in which this field occurs
3629 is also indicated. FCONTEXT is the type that is needed for the debug
3630 info output routines. *EMPTY_P is set to a non-zero value by this
3631 function to indicate that a class containing this field is
3635 build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
3638 tree assembler_name;
3646 /* This class is non-empty. */
3649 /* Build the FIELD_DECL. */
3650 field = build_decl (FIELD_DECL, name, type);
3651 DECL_ASSEMBLER_NAME (field) = assembler_name;
3652 DECL_VIRTUAL_P (field) = 1;
3653 DECL_ARTIFICIAL (field) = 1;
3654 DECL_FIELD_CONTEXT (field) = class_type;
3655 DECL_FCONTEXT (field) = fcontext;
3656 DECL_ALIGN (field) = TYPE_ALIGN (type);
3657 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (type);
3663 /* If TYPE is an empty class type, records its OFFSET in the table of
3667 record_subobject_offset (type, offset, offsets)
3674 if (!is_empty_class (type))
3677 /* Record the location of this empty object in OFFSETS. */
3678 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3680 n = splay_tree_insert (offsets,
3681 (splay_tree_key) offset,
3682 (splay_tree_value) NULL_TREE);
3683 n->value = ((splay_tree_value)
3684 tree_cons (NULL_TREE,
3691 /* Returns non-zero if TYPE is an empty class type and there is
3692 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3695 check_subobject_offset (type, offset, offsets)
3703 if (!is_empty_class (type))
3706 /* Record the location of this empty object in OFFSETS. */
3707 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3711 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3712 if (same_type_p (TREE_VALUE (t), type))
3718 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3719 F for every subobject, passing it the type, offset, and table of
3720 OFFSETS. If VBASES_P is non-zero, then even non-virtual primary
3721 bases should be traversed; otherwise, they are ignored. If F
3722 returns a non-zero value, the traversal ceases, and that value is
3723 returned. Otherwise, returns zero. */
3726 walk_subobject_offsets (type, f, offset, offsets, vbases_p)
3728 subobject_offset_fn f;
3735 if (CLASS_TYPE_P (type))
3740 /* Record the location of TYPE. */
3741 r = (*f) (type, offset, offsets);
3745 /* Iterate through the direct base classes of TYPE. */
3746 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3748 tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3751 && TREE_VIA_VIRTUAL (binfo)
3752 && !BINFO_PRIMARY_MARKED_P (binfo))
3755 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3757 size_binop (PLUS_EXPR,
3759 BINFO_OFFSET (binfo)),
3766 /* Iterate through the fields of TYPE. */
3767 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3768 if (TREE_CODE (field) == FIELD_DECL)
3770 r = walk_subobject_offsets (TREE_TYPE (field),
3772 size_binop (PLUS_EXPR,
3774 DECL_FIELD_OFFSET (field)),
3781 else if (TREE_CODE (type) == ARRAY_TYPE)
3783 tree domain = TYPE_DOMAIN (type);
3786 /* Step through each of the elements in the array. */
3787 for (index = size_zero_node;
3788 INT_CST_LT (index, TYPE_MAX_VALUE (domain));
3789 index = size_binop (PLUS_EXPR, index, size_one_node))
3791 r = walk_subobject_offsets (TREE_TYPE (type),
3798 offset = size_binop (PLUS_EXPR, offset,
3799 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3806 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3807 OFFSETS. If VBASES_P is non-zero, virtual bases of TYPE are
3811 record_subobject_offsets (type, offset, offsets, vbases_p)
3817 walk_subobject_offsets (type, record_subobject_offset, offset,
3821 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3822 OFFSET) conflict with entries in OFFSETS. If VBASES_P is non-zero,
3823 virtual bases of TYPE are examined. */
3826 layout_conflict_p (type, offset, offsets, vbases_p)
3832 return walk_subobject_offsets (type, check_subobject_offset, offset,
3836 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3837 non-static data member of the type indicated by RLI. BINFO is the
3838 binfo corresponding to the base subobject, OFFSETS maps offsets to
3839 types already located at those offsets. This function determines
3840 the position of the DECL. */
3843 layout_nonempty_base_or_field (rli, decl, binfo, offsets)
3844 record_layout_info rli;
3849 tree offset = NULL_TREE;
3850 tree type = TREE_TYPE (decl);
3851 /* If we are laying out a base class, rather than a field, then
3852 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
3853 int field_p = !DECL_ARTIFICIAL (decl);
3855 /* Try to place the field. It may take more than one try if we have
3856 a hard time placing the field without putting two objects of the
3857 same type at the same address. */
3860 struct record_layout_info_s old_rli = *rli;
3862 /* Place this field. */
3863 place_field (rli, decl);
3864 offset = byte_position (decl);
3866 /* We have to check to see whether or not there is already
3867 something of the same type at the offset we're about to use.
3871 struct T : public S { int i; };
3872 struct U : public S, public T {};
3874 Here, we put S at offset zero in U. Then, we can't put T at
3875 offset zero -- its S component would be at the same address
3876 as the S we already allocated. So, we have to skip ahead.
3877 Since all data members, including those whose type is an
3878 empty class, have non-zero size, any overlap can happen only
3879 with a direct or indirect base-class -- it can't happen with
3881 if (flag_new_abi && layout_conflict_p (TREE_TYPE (decl),
3886 /* Strip off the size allocated to this field. That puts us
3887 at the first place we could have put the field with
3888 proper alignment. */
3891 /* Bump up by the alignment required for the type. */
3893 = size_binop (PLUS_EXPR, rli->bitpos,
3895 ? CLASSTYPE_ALIGN (type)
3896 : TYPE_ALIGN (type)));
3897 normalize_rli (rli);
3900 /* There was no conflict. We're done laying out this field. */
3904 /* Now that we know where it wil be placed, update its
3906 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3907 propagate_binfo_offsets (binfo,
3908 convert (ssizetype, offset));
3911 /* Layout the empty base BINFO. EOC indicates the byte currently just
3912 past the end of the class, and should be correctly aligned for a
3913 class of the type indicated by BINFO; OFFSETS gives the offsets of
3914 the empty bases allocated so far. */
3917 layout_empty_base (binfo, eoc, offsets)
3923 tree basetype = BINFO_TYPE (binfo);
3925 /* This routine should only be used for empty classes. */
3926 my_friendly_assert (is_empty_class (basetype), 20000321);
3927 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3929 /* This is an empty base class. We first try to put it at offset
3931 if (layout_conflict_p (BINFO_TYPE (binfo),
3932 BINFO_OFFSET (binfo),
3936 /* That didn't work. Now, we move forward from the next
3937 available spot in the class. */
3938 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3941 if (!layout_conflict_p (BINFO_TYPE (binfo),
3942 BINFO_OFFSET (binfo),
3945 /* We finally found a spot where there's no overlap. */
3948 /* There's overlap here, too. Bump along to the next spot. */
3949 propagate_binfo_offsets (binfo, alignment);
3954 /* Build a FIELD_DECL for the base given by BINFO in the class
3955 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
3956 *BASE_ALIGN is a running maximum of the alignments of any base
3957 class. OFFSETS gives the location of empty base subobjects. */
3960 build_base_field (rli, binfo, empty_p, base_align, offsets)
3961 record_layout_info rli;
3964 unsigned int *base_align;
3967 tree basetype = BINFO_TYPE (binfo);
3970 if (!COMPLETE_TYPE_P (basetype))
3971 /* This error is now reported in xref_tag, thus giving better
3972 location information. */
3975 decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
3976 DECL_ARTIFICIAL (decl) = 1;
3977 DECL_FIELD_CONTEXT (decl) = rli->t;
3978 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3979 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3980 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3981 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3985 /* Brain damage for backwards compatibility. For no good
3986 reason, the old basetype layout made every base have at least
3987 as large as the alignment for the bases up to that point,
3988 gratuitously wasting space. So we do the same thing here. */
3989 *base_align = MAX (*base_align, DECL_ALIGN (decl));
3991 = size_binop (MAX_EXPR, DECL_SIZE (decl), bitsize_int (*base_align));
3992 DECL_SIZE_UNIT (decl)
3993 = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
3994 size_int (*base_align / BITS_PER_UNIT));
3997 if (!integer_zerop (DECL_SIZE (decl)))
3999 /* The containing class is non-empty because it has a non-empty
4003 /* Try to place the field. It may take more than one try if we
4004 have a hard time placing the field without putting two
4005 objects of the same type at the same address. */
4006 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4010 unsigned HOST_WIDE_INT eoc;
4012 /* On some platforms (ARM), even empty classes will not be
4014 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
4015 eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
4016 layout_empty_base (binfo, size_int (eoc), offsets);
4019 /* Check for inaccessible base classes. If the same base class
4020 appears more than once in the hierarchy, but isn't virtual, then
4022 if (get_base_distance (basetype, rli->t, 0, NULL) == -2)
4023 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4026 /* Record the offsets of BINFO and its base subobjects. */
4027 record_subobject_offsets (BINFO_TYPE (binfo),
4028 BINFO_OFFSET (binfo),
4033 /* Layout all of the non-virtual base classes. Record empty
4034 subobjects in OFFSETS. */
4037 build_base_fields (rli, empty_p, offsets)
4038 record_layout_info rli;
4042 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4045 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
4047 unsigned int base_align = 0;
4049 /* Under the new ABI, the primary base class is always allocated
4051 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
4052 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
4053 empty_p, &base_align, offsets);
4055 /* Now allocate the rest of the bases. */
4056 for (i = 0; i < n_baseclasses; ++i)
4060 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4062 /* Under the new ABI, the primary base was already allocated
4063 above, so we don't need to allocate it again here. */
4064 if (flag_new_abi && base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
4067 /* A primary virtual base class is allocated just like any other
4068 base class, but a non-primary virtual base is allocated
4069 later, in layout_virtual_bases. */
4070 if (TREE_VIA_VIRTUAL (base_binfo)
4071 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4074 build_base_field (rli, base_binfo, empty_p, &base_align, offsets);
4078 /* Go through the TYPE_METHODS of T issuing any appropriate
4079 diagnostics, figuring out which methods override which other
4080 methods, and so forth. */
4087 int seen_one_arg_array_delete_p = 0;
4089 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4091 GNU_xref_member (current_class_name, x);
4093 /* If this was an evil function, don't keep it in class. */
4094 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4097 check_for_override (x, t);
4098 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4099 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4101 /* The name of the field is the original field name
4102 Save this in auxiliary field for later overloading. */
4103 if (DECL_VINDEX (x))
4105 TYPE_POLYMORPHIC_P (t) = 1;
4106 if (DECL_PURE_VIRTUAL_P (x))
4107 CLASSTYPE_PURE_VIRTUALS (t)
4108 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4111 if (DECL_ARRAY_DELETE_OPERATOR_P (x))
4115 /* When dynamically allocating an array of this type, we
4116 need a "cookie" to record how many elements we allocated,
4117 even if the array elements have no non-trivial
4118 destructor, if the usual array deallocation function
4119 takes a second argument of type size_t. The standard (in
4120 [class.free]) requires that the second argument be set
4122 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
4123 /* This is overly conservative, but we must maintain this
4124 behavior for backwards compatibility. */
4125 if (!flag_new_abi && second_parm != void_list_node)
4126 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4127 /* Under the new ABI, we choose only those function that are
4128 explicitly declared as `operator delete[] (void *,
4130 else if (flag_new_abi
4131 && !seen_one_arg_array_delete_p
4133 && TREE_CHAIN (second_parm) == void_list_node
4134 && same_type_p (TREE_VALUE (second_parm), sizetype))
4135 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4136 /* If there's no second parameter, then this is the usual
4137 deallocation function. */
4138 else if (second_parm == void_list_node)
4139 seen_one_arg_array_delete_p = 1;
4144 /* FN is a constructor or destructor. Clone the declaration to create
4145 a specialized in-charge or not-in-charge version, as indicated by
4149 build_clone (fn, name)
4156 /* Copy the function. */
4157 clone = copy_decl (fn);
4158 /* Remember where this function came from. */
4159 DECL_CLONED_FUNCTION (clone) = fn;
4160 /* Reset the function name. */
4161 DECL_NAME (clone) = name;
4162 DECL_ASSEMBLER_NAME (clone) = DECL_NAME (clone);
4163 /* There's no pending inline data for this function. */
4164 DECL_PENDING_INLINE_INFO (clone) = NULL;
4165 DECL_PENDING_INLINE_P (clone) = 0;
4166 /* And it hasn't yet been deferred. */
4167 DECL_DEFERRED_FN (clone) = 0;
4168 /* There's no magic VTT parameter in the clone. */
4169 DECL_VTT_PARM (clone) = NULL_TREE;
4171 /* The base-class destructor is not virtual. */
4172 if (name == base_dtor_identifier)
4174 DECL_VIRTUAL_P (clone) = 0;
4175 if (TREE_CODE (clone) != TEMPLATE_DECL)
4176 DECL_VINDEX (clone) = NULL_TREE;
4179 /* If there was an in-charge parameter, drop it from the function
4181 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4187 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4188 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4189 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4190 /* Skip the `this' parameter. */
4191 parmtypes = TREE_CHAIN (parmtypes);
4192 /* Skip the in-charge parameter. */
4193 parmtypes = TREE_CHAIN (parmtypes);
4194 /* If this is subobject constructor or destructor, add the vtt
4196 if (DECL_NEEDS_VTT_PARM_P (clone))
4197 parmtypes = hash_tree_chain (vtt_parm_type, parmtypes);
4199 = build_cplus_method_type (basetype,
4200 TREE_TYPE (TREE_TYPE (clone)),
4203 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4207 /* Copy the function parameters. But, DECL_ARGUMENTS aren't
4208 function parameters; instead, those are the template parameters. */
4209 if (TREE_CODE (clone) != TEMPLATE_DECL)
4211 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4212 /* Remove the in-charge parameter. */
4213 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4215 TREE_CHAIN (DECL_ARGUMENTS (clone))
4216 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4217 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4220 /* Add the VTT parameter. */
4221 if (DECL_NEEDS_VTT_PARM_P (clone))
4225 parm = build_artificial_parm (vtt_parm_identifier,
4227 TREE_CHAIN (parm) = TREE_CHAIN (DECL_ARGUMENTS (clone));
4228 TREE_CHAIN (DECL_ARGUMENTS (clone)) = parm;
4231 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4233 DECL_CONTEXT (parms) = clone;
4234 copy_lang_decl (parms);
4238 /* Mangle the function name. */
4239 set_mangled_name_for_decl (clone);
4241 /* Create the RTL for this function. */
4242 DECL_RTL (clone) = NULL_RTX;
4243 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4245 /* Make it easy to find the CLONE given the FN. */
4246 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4247 TREE_CHAIN (fn) = clone;
4249 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4250 if (TREE_CODE (clone) == TEMPLATE_DECL)
4254 DECL_TEMPLATE_RESULT (clone)
4255 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4256 result = DECL_TEMPLATE_RESULT (clone);
4257 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4258 DECL_TI_TEMPLATE (result) = clone;
4260 else if (DECL_DEFERRED_FN (fn))
4266 /* Produce declarations for all appropriate clones of FN. If
4267 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4268 CLASTYPE_METHOD_VEC as well. */
4271 clone_function_decl (fn, update_method_vec_p)
4273 int update_method_vec_p;
4277 /* Avoid inappropriate cloning. */
4280 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn))))
4283 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4285 /* For each constructor, we need two variants: an in-charge version
4286 and a not-in-charge version. */
4287 clone = build_clone (fn, complete_ctor_identifier);
4288 if (update_method_vec_p)
4289 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4290 clone = build_clone (fn, base_ctor_identifier);
4291 if (update_method_vec_p)
4292 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4296 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4298 /* For each destructor, we need three variants: an in-charge
4299 version, a not-in-charge version, and an in-charge deleting
4300 version. We clone the deleting version first because that
4301 means it will go second on the TYPE_METHODS list -- and that
4302 corresponds to the correct layout order in the virtual
4304 clone = build_clone (fn, deleting_dtor_identifier);
4305 if (update_method_vec_p)
4306 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4307 clone = build_clone (fn, complete_dtor_identifier);
4308 if (update_method_vec_p)
4309 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4310 clone = build_clone (fn, base_dtor_identifier);
4311 if (update_method_vec_p)
4312 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4316 /* For each of the constructors and destructors in T, create an
4317 in-charge and not-in-charge variant. */
4320 clone_constructors_and_destructors (t)
4325 /* We only clone constructors and destructors under the new ABI. */
4329 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4331 if (!CLASSTYPE_METHOD_VEC (t))
4334 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4335 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4336 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4337 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4340 /* Remove all zero-width bit-fields from T. */
4343 remove_zero_width_bit_fields (t)
4348 fieldsp = &TYPE_FIELDS (t);
4351 if (TREE_CODE (*fieldsp) == FIELD_DECL
4352 && DECL_C_BIT_FIELD (*fieldsp)
4353 && DECL_INITIAL (*fieldsp))
4354 *fieldsp = TREE_CHAIN (*fieldsp);
4356 fieldsp = &TREE_CHAIN (*fieldsp);
4360 /* Check the validity of the bases and members declared in T. Add any
4361 implicitly-generated functions (like copy-constructors and
4362 assignment operators). Compute various flag bits (like
4363 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4364 level: i.e., independently of the ABI in use. */
4367 check_bases_and_members (t, empty_p)
4371 /* Nonzero if we are not allowed to generate a default constructor
4373 int cant_have_default_ctor;
4374 /* Nonzero if the implicitly generated copy constructor should take
4375 a non-const reference argument. */
4376 int cant_have_const_ctor;
4377 /* Nonzero if the the implicitly generated assignment operator
4378 should take a non-const reference argument. */
4379 int no_const_asn_ref;
4382 /* By default, we use const reference arguments and generate default
4384 cant_have_default_ctor = 0;
4385 cant_have_const_ctor = 0;
4386 no_const_asn_ref = 0;
4388 /* Assume that the class is nearly empty; we'll clear this flag if
4389 it turns out not to be nearly empty. */
4390 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4392 /* Check all the base-classes. */
4393 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4396 /* Check all the data member declarations. */
4397 check_field_decls (t, &access_decls, empty_p,
4398 &cant_have_default_ctor,
4399 &cant_have_const_ctor,
4402 /* Check all the method declarations. */
4405 /* A nearly-empty class has to be vptr-containing; a nearly empty
4406 class contains just a vptr. */
4407 if (!TYPE_CONTAINS_VPTR_P (t))
4408 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4410 /* Do some bookkeeping that will guide the generation of implicitly
4411 declared member functions. */
4412 TYPE_HAS_COMPLEX_INIT_REF (t)
4413 |= (TYPE_HAS_INIT_REF (t)
4414 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4415 || TYPE_POLYMORPHIC_P (t));
4416 TYPE_NEEDS_CONSTRUCTING (t)
4417 |= (TYPE_HAS_CONSTRUCTOR (t)
4418 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4419 || TYPE_POLYMORPHIC_P (t));
4420 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4421 || TYPE_POLYMORPHIC_P (t));
4422 CLASSTYPE_NON_POD_P (t)
4423 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4424 || TYPE_HAS_ASSIGN_REF (t));
4425 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4426 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4427 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4429 /* Synthesize any needed methods. Note that methods will be synthesized
4430 for anonymous unions; grok_x_components undoes that. */
4431 add_implicitly_declared_members (t, cant_have_default_ctor,
4432 cant_have_const_ctor,
4435 /* Create the in-charge and not-in-charge variants of constructors
4437 clone_constructors_and_destructors (t);
4439 /* Process the using-declarations. */
4440 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4441 handle_using_decl (TREE_VALUE (access_decls), t);
4443 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4444 finish_struct_methods (t);
4447 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4448 accordingly. If a new vfield was created (because T doesn't have a
4449 primary base class), then the newly created field is returned. It
4450 is not added to the TYPE_FIELDS list; it is the caller's
4451 responsibility to do that. */
4454 create_vtable_ptr (t, empty_p, vfuns_p,
4455 new_virtuals_p, overridden_virtuals_p)
4459 tree *new_virtuals_p;
4460 tree *overridden_virtuals_p;
4464 /* Loop over the virtual functions, adding them to our various
4466 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4467 if (DECL_VINDEX (fn)
4468 && !(flag_new_abi && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)))
4469 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4472 /* If we couldn't find an appropriate base class, create a new field
4473 here. Even if there weren't any new virtual functions, we might need a
4474 new virtual function table if we're supposed to include vptrs in
4475 all classes that need them. */
4476 if (!TYPE_VFIELD (t)
4478 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())))
4480 /* We build this decl with vtbl_ptr_type_node, which is a
4481 `vtable_entry_type*'. It might seem more precise to use
4482 `vtable_entry_type (*)[N]' where N is the number of firtual
4483 functions. However, that would require the vtable pointer in
4484 base classes to have a different type than the vtable pointer
4485 in derived classes. We could make that happen, but that
4486 still wouldn't solve all the problems. In particular, the
4487 type-based alias analysis code would decide that assignments
4488 to the base class vtable pointer can't alias assignments to
4489 the derived class vtable pointer, since they have different
4490 types. Thus, in an derived class destructor, where the base
4491 class constructor was inlined, we could generate bad code for
4492 setting up the vtable pointer.
4494 Therefore, we use one type for all vtable pointers. We still
4495 use a type-correct type; it's just doesn't indicate the array
4496 bounds. That's better than using `void*' or some such; it's
4497 cleaner, and it let's the alias analysis code know that these
4498 stores cannot alias stores to void*! */
4500 = build_vtbl_or_vbase_field (get_vfield_name (t),
4501 get_identifier (VFIELD_BASE),
4507 if (flag_new_abi && CLASSTYPE_N_BASECLASSES (t))
4508 /* If there were any baseclasses, they can't possibly be at
4509 offset zero any more, because that's where the vtable
4510 pointer is. So, converting to a base class is going to
4512 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4514 return TYPE_VFIELD (t);
4520 /* Fixup the inline function given by INFO now that the class is
4524 fixup_pending_inline (fn)
4527 if (DECL_PENDING_INLINE_INFO (fn))
4529 tree args = DECL_ARGUMENTS (fn);
4532 DECL_CONTEXT (args) = fn;
4533 args = TREE_CHAIN (args);
4538 /* Fixup the inline methods and friends in TYPE now that TYPE is
4542 fixup_inline_methods (type)
4545 tree method = TYPE_METHODS (type);
4547 if (method && TREE_CODE (method) == TREE_VEC)
4549 if (TREE_VEC_ELT (method, 1))
4550 method = TREE_VEC_ELT (method, 1);
4551 else if (TREE_VEC_ELT (method, 0))
4552 method = TREE_VEC_ELT (method, 0);
4554 method = TREE_VEC_ELT (method, 2);
4557 /* Do inline member functions. */
4558 for (; method; method = TREE_CHAIN (method))
4559 fixup_pending_inline (method);
4562 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4564 method = TREE_CHAIN (method))
4565 fixup_pending_inline (TREE_VALUE (method));
4566 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4569 /* Add OFFSET to all base types of BINFO which is a base in the
4570 hierarchy dominated by T.
4572 OFFSET, which is a type offset, is number of bytes. */
4575 propagate_binfo_offsets (binfo, offset)
4582 /* Update BINFO's offset. */
4583 BINFO_OFFSET (binfo)
4584 = convert (sizetype,
4585 size_binop (PLUS_EXPR,
4586 convert (ssizetype, BINFO_OFFSET (binfo)),
4589 /* Find the primary base class. */
4590 primary_binfo = get_primary_binfo (binfo);
4592 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4594 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4598 /* On the first through the loop, do the primary base. Because
4599 the primary base need not be an immediate base, we must
4600 handle the primary base specially. */
4606 base_binfo = primary_binfo;
4610 base_binfo = BINFO_BASETYPE (binfo, i);
4611 /* Don't do the primary base twice. */
4612 if (base_binfo == primary_binfo)
4616 /* Skip virtual bases that aren't our primary base. */
4617 if (TREE_VIA_VIRTUAL (base_binfo)
4618 && BINFO_PRIMARY_BASE_OF (base_binfo) != binfo)
4621 propagate_binfo_offsets (base_binfo, offset);
4625 /* Called via dfs_walk from layout_virtual bases. */
4628 dfs_set_offset_for_unshared_vbases (binfo, data)
4632 /* If this is a virtual base, make sure it has the same offset as
4633 the shared copy. If it's a primary base, then we know it's
4635 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4637 tree t = (tree) data;
4641 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4642 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4643 propagate_binfo_offsets (binfo, offset);
4649 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4650 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4651 empty subobjects of T. */
4654 layout_virtual_bases (t, offsets)
4659 unsigned HOST_WIDE_INT dsize;
4660 unsigned HOST_WIDE_INT eoc;
4662 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4665 #ifdef STRUCTURE_SIZE_BOUNDARY
4666 /* Packed structures don't need to have minimum size. */
4667 if (! TYPE_PACKED (t))
4668 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4671 /* DSIZE is the size of the class without the virtual bases. */
4672 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4674 /* Make every class have alignment of at least one. */
4675 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4677 /* Go through the virtual bases, allocating space for each virtual
4678 base that is not already a primary base class. Under the new
4679 ABI, these are allocated according to a depth-first left-to-right
4680 postorder traversal; in the new ABI, inheritance graph order is
4682 for (vbases = (flag_new_abi
4684 : CLASSTYPE_VBASECLASSES (t));
4686 vbases = TREE_CHAIN (vbases))
4692 if (!TREE_VIA_VIRTUAL (vbases))
4694 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4697 vbase = TREE_VALUE (vbases);
4699 if (!BINFO_PRIMARY_MARKED_P (vbase))
4701 /* This virtual base is not a primary base of any class in the
4702 hierarchy, so we have to add space for it. */
4704 unsigned int desired_align;
4706 basetype = BINFO_TYPE (vbase);
4709 desired_align = CLASSTYPE_ALIGN (basetype);
4711 /* Under the old ABI, virtual bases were aligned as for the
4712 entire base object (including its virtual bases). That's
4713 wasteful, in general. */
4714 desired_align = TYPE_ALIGN (basetype);
4715 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4717 /* Add padding so that we can put the virtual base class at an
4718 appropriately aligned offset. */
4719 dsize = CEIL (dsize, desired_align) * desired_align;
4721 /* Under the new ABI, we try to squish empty virtual bases in
4722 just like ordinary empty bases. */
4723 if (flag_new_abi && is_empty_class (basetype))
4724 layout_empty_base (vbase,
4725 size_int (CEIL (dsize, BITS_PER_UNIT)),
4731 offset = ssize_int (CEIL (dsize, BITS_PER_UNIT));
4732 offset = size_diffop (offset,
4734 BINFO_OFFSET (vbase)));
4736 /* And compute the offset of the virtual base. */
4737 propagate_binfo_offsets (vbase, offset);
4738 /* Every virtual baseclass takes a least a UNIT, so that
4739 we can take it's address and get something different
4741 dsize += MAX (BITS_PER_UNIT,
4742 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4745 /* Keep track of the offsets assigned to this virtual base. */
4746 record_subobject_offsets (BINFO_TYPE (vbase),
4747 BINFO_OFFSET (vbase),
4753 /* Now, go through the TYPE_BINFO hierarchy, setting the
4754 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4755 bases and their direct and indirect bases. The ambiguity checks
4756 in get_base_distance depend on the BINFO_OFFSETs being set
4758 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4760 /* If we had empty base classes that protruded beyond the end of the
4761 class, we didn't update DSIZE above; we were hoping to overlay
4762 multiple such bases at the same location. */
4763 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4764 if (eoc * BITS_PER_UNIT > dsize)
4765 dsize = (eoc + 1) * BITS_PER_UNIT;
4767 /* Now, make sure that the total size of the type is a multiple of
4769 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4770 TYPE_SIZE (t) = bitsize_int (dsize);
4771 TYPE_SIZE_UNIT (t) = convert (sizetype,
4772 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4773 bitsize_unit_node));
4775 /* Check for ambiguous virtual bases. */
4777 for (vbases = CLASSTYPE_VBASECLASSES (t);
4779 vbases = TREE_CHAIN (vbases))
4781 tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
4782 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4783 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4788 /* Returns the offset of the byte just past the end of the base class
4789 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4790 only non-virtual bases are included. */
4792 static unsigned HOST_WIDE_INT
4793 end_of_class (t, include_virtuals_p)
4795 int include_virtuals_p;
4797 unsigned HOST_WIDE_INT result = 0;
4800 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4804 unsigned HOST_WIDE_INT end_of_base;
4806 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4808 if (!include_virtuals_p
4809 && TREE_VIA_VIRTUAL (base_binfo)
4810 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4813 offset = size_binop (PLUS_EXPR,
4814 BINFO_OFFSET (base_binfo),
4815 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
4816 end_of_base = tree_low_cst (offset, /*pos=*/1);
4817 if (end_of_base > result)
4818 result = end_of_base;
4824 /* Compare two INTEGER_CSTs K1 and K2. */
4827 splay_tree_compare_integer_csts (k1, k2)
4831 return tree_int_cst_compare ((tree) k1, (tree) k2);
4834 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4835 BINFO_OFFSETs for all of the base-classes. Position the vtable
4839 layout_class_type (t, empty_p, vfuns_p,
4840 new_virtuals_p, overridden_virtuals_p)
4844 tree *new_virtuals_p;
4845 tree *overridden_virtuals_p;
4847 tree non_static_data_members;
4850 record_layout_info rli;
4851 unsigned HOST_WIDE_INT eoc;
4852 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4853 types that appear at that offset. */
4854 splay_tree empty_base_offsets;
4856 /* Keep track of the first non-static data member. */
4857 non_static_data_members = TYPE_FIELDS (t);
4859 /* Start laying out the record. */
4860 rli = start_record_layout (t);
4862 /* If possible, we reuse the virtual function table pointer from one
4863 of our base classes. */
4864 determine_primary_base (t, vfuns_p);
4866 /* Create a pointer to our virtual function table. */
4867 vptr = create_vtable_ptr (t, empty_p, vfuns_p,
4868 new_virtuals_p, overridden_virtuals_p);
4870 /* Under the new ABI, the vptr is always the first thing in the
4872 if (flag_new_abi && vptr)
4874 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4875 place_field (rli, vptr);
4878 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4879 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4881 build_base_fields (rli, empty_p, empty_base_offsets);
4882 /* Add pointers to all of our virtual base-classes. */
4883 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (rli, empty_p),
4886 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4887 we have to save this before we start modifying
4888 TYPE_NONCOPIED_PARTS. */
4889 fixup_inline_methods (t);
4891 /* Layout the non-static data members. */
4892 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4897 /* We still pass things that aren't non-static data members to
4898 the back-end, in case it wants to do something with them. */
4899 if (TREE_CODE (field) != FIELD_DECL)
4901 place_field (rli, field);
4905 type = TREE_TYPE (field);
4907 /* If this field is a bit-field whose width is greater than its
4908 type, then there are some special rules for allocating it
4909 under the new ABI. Under the old ABI, there were no special
4910 rules, but the back-end can't handle bitfields longer than a
4911 `long long', so we use the same mechanism. */
4912 if (DECL_C_BIT_FIELD (field)
4914 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4916 && 0 < compare_tree_int (DECL_SIZE (field),
4918 (long_long_unsigned_type_node)))))
4920 integer_type_kind itk;
4923 /* We must allocate the bits as if suitably aligned for the
4924 longest integer type that fits in this many bits. type
4925 of the field. Then, we are supposed to use the left over
4926 bits as additional padding. */
4927 for (itk = itk_char; itk != itk_none; ++itk)
4928 if (INT_CST_LT (DECL_SIZE (field),
4929 TYPE_SIZE (integer_types[itk])))
4932 /* ITK now indicates a type that is too large for the
4933 field. We have to back up by one to find the largest
4935 integer_type = integer_types[itk - 1];
4936 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4937 TYPE_SIZE (integer_type));
4938 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4939 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4940 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4943 padding = NULL_TREE;
4945 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4946 empty_base_offsets);
4948 /* If we needed additional padding after this field, add it
4954 padding_field = build_decl (FIELD_DECL,
4957 DECL_BIT_FIELD (padding_field) = 1;
4958 DECL_SIZE (padding_field) = padding;
4959 DECL_ALIGN (padding_field) = 1;
4960 DECL_USER_ALIGN (padding_field) = 0;
4961 layout_nonempty_base_or_field (rli, padding_field,
4963 empty_base_offsets);
4967 /* It might be the case that we grew the class to allocate a
4968 zero-sized base class. That won't be reflected in RLI, yet,
4969 because we are willing to overlay multiple bases at the same
4970 offset. However, now we need to make sure that RLI is big enough
4971 to reflect the entire class. */
4972 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4973 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4974 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
4976 /* We don't handle zero-sized base classes specially under the
4977 old ABI, so if we get here, we had better be operating under
4978 the new ABI rules. */
4979 my_friendly_assert (flag_new_abi, 20000321);
4980 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc + 1));
4981 rli->bitpos = bitsize_zero_node;
4984 /* We make all structures have at least one element, so that they
4985 have non-zero size. In the new ABI, the class may be empty even
4986 if it has basetypes. Therefore, we add the fake field after all
4987 the other fields; if there are already FIELD_DECLs on the list,
4988 their offsets will not be disturbed. */
4993 padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
4994 place_field (rli, padding);
4995 TYPE_NONCOPIED_PARTS (t)
4996 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
4997 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
5000 /* Under the old ABI, the vptr comes at the very end of the
5002 if (!flag_new_abi && vptr)
5004 place_field (rli, vptr);
5005 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), vptr);
5008 /* Let the back-end lay out the type. Note that at this point we
5009 have only included non-virtual base-classes; we will lay out the
5010 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
5011 this call are not necessarily correct; they are just the size and
5012 alignment when no virtual base clases are used. */
5013 finish_record_layout (rli);
5015 /* Delete all zero-width bit-fields from the list of fields. Now
5016 that the type is laid out they are no longer important. */
5017 remove_zero_width_bit_fields (t);
5019 /* Remember the size and alignment of the class before adding
5020 the virtual bases. */
5021 if (*empty_p && flag_new_abi)
5023 CLASSTYPE_SIZE (t) = bitsize_zero_node;
5024 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
5026 else if (flag_new_abi)
5028 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
5029 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
5033 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
5034 CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
5037 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5038 CLASSTYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (t);
5040 /* Set the TYPE_DECL for this type to contain the right
5041 value for DECL_OFFSET, so that we can use it as part
5042 of a COMPONENT_REF for multiple inheritance. */
5043 layout_decl (TYPE_MAIN_DECL (t), 0);
5045 /* Now fix up any virtual base class types that we left lying
5046 around. We must get these done before we try to lay out the
5047 virtual function table. As a side-effect, this will remove the
5048 base subobject fields. */
5049 layout_virtual_bases (t, empty_base_offsets);
5052 splay_tree_delete (empty_base_offsets);
5055 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5056 (or C++ class declaration).
5058 For C++, we must handle the building of derived classes.
5059 Also, C++ allows static class members. The way that this is
5060 handled is to keep the field name where it is (as the DECL_NAME
5061 of the field), and place the overloaded decl in the bit position
5062 of the field. layout_record and layout_union will know about this.
5064 More C++ hair: inline functions have text in their
5065 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5066 meaningful tree structure. After the struct has been laid out, set
5067 things up so that this can happen.
5069 And still more: virtual functions. In the case of single inheritance,
5070 when a new virtual function is seen which redefines a virtual function
5071 from the base class, the new virtual function is placed into
5072 the virtual function table at exactly the same address that
5073 it had in the base class. When this is extended to multiple
5074 inheritance, the same thing happens, except that multiple virtual
5075 function tables must be maintained. The first virtual function
5076 table is treated in exactly the same way as in the case of single
5077 inheritance. Additional virtual function tables have different
5078 DELTAs, which tell how to adjust `this' to point to the right thing.
5080 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5088 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5089 a FUNCTION_DECL. Each of these functions is a virtual function
5090 declared in T that does not override any virtual function from a
5092 tree new_virtuals = NULL_TREE;
5093 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5094 except that each declaration here overrides the declaration from
5096 tree overridden_virtuals = NULL_TREE;
5101 if (COMPLETE_TYPE_P (t))
5103 if (IS_AGGR_TYPE (t))
5104 cp_error ("redefinition of `%#T'", t);
5106 my_friendly_abort (172);
5111 GNU_xref_decl (current_function_decl, t);
5113 /* If this type was previously laid out as a forward reference,
5114 make sure we lay it out again. */
5115 TYPE_SIZE (t) = NULL_TREE;
5116 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5117 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5119 CLASSTYPE_RTTI (t) = NULL_TREE;
5121 /* Do end-of-class semantic processing: checking the validity of the
5122 bases and members and add implicitly generated methods. */
5123 check_bases_and_members (t, &empty);
5125 /* Layout the class itself. */
5126 layout_class_type (t, &empty, &vfuns,
5127 &new_virtuals, &overridden_virtuals);
5129 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
5130 might need to know it for setting up the offsets in the vtable
5131 (or in thunks) below. */
5132 vfield = TYPE_VFIELD (t);
5133 if (vfield != NULL_TREE
5134 && DECL_FIELD_CONTEXT (vfield) != t)
5136 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
5138 vfield = copy_decl (vfield);
5140 DECL_FIELD_CONTEXT (vfield) = t;
5141 DECL_FIELD_OFFSET (vfield)
5142 = size_binop (PLUS_EXPR,
5143 BINFO_OFFSET (binfo),
5144 DECL_FIELD_OFFSET (vfield));
5145 TYPE_VFIELD (t) = vfield;
5149 = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
5151 /* If we created a new vtbl pointer for this class, add it to the
5153 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5154 CLASSTYPE_VFIELDS (t)
5155 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5157 /* If necessary, create the primary vtable for this class. */
5159 || overridden_virtuals
5160 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
5162 new_virtuals = nreverse (new_virtuals);
5163 /* We must enter these virtuals into the table. */
5164 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5165 build_primary_vtable (NULL_TREE, t);
5166 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5167 /* Here we know enough to change the type of our virtual
5168 function table, but we will wait until later this function. */
5169 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5171 /* If this type has basetypes with constructors, then those
5172 constructors might clobber the virtual function table. But
5173 they don't if the derived class shares the exact vtable of the base
5176 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5178 /* If we didn't need a new vtable, see if we should copy one from
5180 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5182 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5184 /* If this class uses a different vtable than its primary base
5185 then when we will need to initialize our vptr after the base
5186 class constructor runs. */
5187 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5188 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5191 if (TYPE_CONTAINS_VPTR_P (t))
5193 if (TYPE_BINFO_VTABLE (t))
5194 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5196 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5197 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5200 CLASSTYPE_VSIZE (t) = vfuns;
5201 /* Entries for virtual functions defined in the primary base are
5202 followed by entries for new functions unique to this class. */
5203 TYPE_BINFO_VIRTUALS (t)
5204 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5205 /* Finally, add entries for functions that override virtuals
5206 from non-primary bases. */
5207 TYPE_BINFO_VIRTUALS (t)
5208 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5211 finish_struct_bits (t);
5213 /* Complete the rtl for any static member objects of the type we're
5215 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5217 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5218 && TREE_TYPE (x) == t)
5220 DECL_MODE (x) = TYPE_MODE (t);
5221 make_decl_rtl (x, NULL, 0);
5225 /* Done with FIELDS...now decide whether to sort these for
5226 faster lookups later.
5228 The C front-end only does this when n_fields > 15. We use
5229 a smaller number because most searches fail (succeeding
5230 ultimately as the search bores through the inheritance
5231 hierarchy), and we want this failure to occur quickly. */
5233 n_fields = count_fields (TYPE_FIELDS (t));
5236 tree field_vec = make_tree_vec (n_fields);
5237 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5238 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5239 (int (*)(const void *, const void *))field_decl_cmp);
5240 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5241 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5242 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5245 if (TYPE_HAS_CONSTRUCTOR (t))
5247 tree vfields = CLASSTYPE_VFIELDS (t);
5251 /* Mark the fact that constructor for T
5252 could affect anybody inheriting from T
5253 who wants to initialize vtables for VFIELDS's type. */
5254 if (VF_DERIVED_VALUE (vfields))
5255 TREE_ADDRESSABLE (vfields) = 1;
5256 vfields = TREE_CHAIN (vfields);
5260 /* Make the rtl for any new vtables we have created, and unmark
5261 the base types we marked. */
5263 /* Build the VTT for T. */
5266 if (TYPE_VFIELD (t))
5268 /* In addition to this one, all the other vfields should be listed. */
5269 /* Before that can be done, we have to have FIELD_DECLs for them, and
5270 a place to find them. */
5271 TYPE_NONCOPIED_PARTS (t)
5272 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5273 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5275 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5276 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5277 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5281 hack_incomplete_structures (t);
5283 if (warn_overloaded_virtual)
5286 maybe_suppress_debug_info (t);
5288 /* Finish debugging output for this type. */
5289 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5292 /* When T was built up, the member declarations were added in reverse
5293 order. Rearrange them to declaration order. */
5296 unreverse_member_declarations (t)
5303 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5304 reverse order. Put them in declaration order now. */
5305 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5306 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5308 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5309 reverse order, so we can't just use nreverse. */
5311 for (x = TYPE_FIELDS (t);
5312 x && TREE_CODE (x) != TYPE_DECL;
5315 next = TREE_CHAIN (x);
5316 TREE_CHAIN (x) = prev;
5321 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5323 TYPE_FIELDS (t) = prev;
5328 finish_struct (t, attributes)
5331 /* Now that we've got all the field declarations, reverse everything
5333 unreverse_member_declarations (t);
5335 cplus_decl_attributes (t, attributes, NULL_TREE);
5337 if (processing_template_decl)
5339 finish_struct_methods (t);
5340 TYPE_SIZE (t) = bitsize_zero_node;
5343 finish_struct_1 (t);
5345 TYPE_BEING_DEFINED (t) = 0;
5347 if (current_class_type)
5350 error ("trying to finish struct, but kicked out due to previous parse errors.");
5352 if (processing_template_decl)
5354 tree scope = current_scope ();
5355 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5356 add_stmt (build_min (TAG_DEFN, t));
5362 /* Return the dynamic type of INSTANCE, if known.
5363 Used to determine whether the virtual function table is needed
5366 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5367 of our knowledge of its type. *NONNULL should be initialized
5368 before this function is called. */
5371 fixed_type_or_null (instance, nonnull)
5375 switch (TREE_CODE (instance))
5378 /* Check that we are not going through a cast of some sort. */
5379 if (TREE_TYPE (instance)
5380 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5381 instance = TREE_OPERAND (instance, 0);
5382 /* fall through... */
5384 /* This is a call to a constructor, hence it's never zero. */
5385 if (TREE_HAS_CONSTRUCTOR (instance))
5389 return TREE_TYPE (instance);
5394 /* This is a call to a constructor, hence it's never zero. */
5395 if (TREE_HAS_CONSTRUCTOR (instance))
5399 return TREE_TYPE (instance);
5401 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5408 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5409 /* Propagate nonnull. */
5410 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5411 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5412 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5417 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5422 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5425 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5429 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5430 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5434 return TREE_TYPE (TREE_TYPE (instance));
5436 /* fall through... */
5439 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5443 return TREE_TYPE (instance);
5447 if (instance == current_class_ptr
5448 && flag_this_is_variable <= 0)
5450 /* Normally, 'this' must be non-null. */
5451 if (flag_this_is_variable == 0)
5454 /* <0 means we're in a constructor and we know our type. */
5455 if (flag_this_is_variable < 0)
5456 return TREE_TYPE (TREE_TYPE (instance));
5458 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5459 /* Reference variables should be references to objects. */
5469 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5470 to the static type. We also handle the case where INSTANCE is really
5473 Used to determine whether the virtual function table is needed
5476 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5477 of our knowledge of its type. *NONNULL should be initialized
5478 before this function is called. */
5481 resolves_to_fixed_type_p (instance, nonnull)
5485 tree t = TREE_TYPE (instance);
5486 tree fixed = fixed_type_or_null (instance, nonnull);
5487 if (fixed == NULL_TREE)
5489 if (POINTER_TYPE_P (t))
5491 return same_type_ignoring_top_level_qualifiers_p (t, fixed);
5496 init_class_processing ()
5498 current_class_depth = 0;
5499 current_class_stack_size = 10;
5501 = (class_stack_node_t) xmalloc (current_class_stack_size
5502 * sizeof (struct class_stack_node));
5503 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5504 ggc_add_tree_varray_root (&local_classes, 1);
5506 access_default_node = build_int_2 (0, 0);
5507 access_public_node = build_int_2 (ak_public, 0);
5508 access_protected_node = build_int_2 (ak_protected, 0);
5509 access_private_node = build_int_2 (ak_private, 0);
5510 access_default_virtual_node = build_int_2 (4, 0);
5511 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5512 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5513 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5515 ridpointers[(int) RID_PUBLIC] = access_public_node;
5516 ridpointers[(int) RID_PRIVATE] = access_private_node;
5517 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5520 /* Set current scope to NAME. CODE tells us if this is a
5521 STRUCT, UNION, or ENUM environment.
5523 NAME may end up being NULL_TREE if this is an anonymous or
5524 late-bound struct (as in "struct { ... } foo;") */
5526 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5527 appropriate values, found by looking up the type definition of
5530 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5531 which can be seen locally to the class. They are shadowed by
5532 any subsequent local declaration (including parameter names).
5534 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5535 which have static meaning (i.e., static members, static
5536 member functions, enum declarations, etc).
5538 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5539 which can be seen locally to the class (as in 1), but
5540 know that we are doing this for declaration purposes
5541 (i.e. friend foo::bar (int)).
5543 So that we may avoid calls to lookup_name, we cache the _TYPE
5544 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5546 For multiple inheritance, we perform a two-pass depth-first search
5547 of the type lattice. The first pass performs a pre-order search,
5548 marking types after the type has had its fields installed in
5549 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5550 unmarks the marked types. If a field or member function name
5551 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5552 that name becomes `error_mark_node'. */
5555 pushclass (type, modify)
5559 type = TYPE_MAIN_VARIANT (type);
5561 /* Make sure there is enough room for the new entry on the stack. */
5562 if (current_class_depth + 1 >= current_class_stack_size)
5564 current_class_stack_size *= 2;
5566 = (class_stack_node_t) xrealloc (current_class_stack,
5567 current_class_stack_size
5568 * sizeof (struct class_stack_node));
5571 /* Insert a new entry on the class stack. */
5572 current_class_stack[current_class_depth].name = current_class_name;
5573 current_class_stack[current_class_depth].type = current_class_type;
5574 current_class_stack[current_class_depth].access = current_access_specifier;
5575 current_class_stack[current_class_depth].names_used = 0;
5576 current_class_depth++;
5578 /* Now set up the new type. */
5579 current_class_name = TYPE_NAME (type);
5580 if (TREE_CODE (current_class_name) == TYPE_DECL)
5581 current_class_name = DECL_NAME (current_class_name);
5582 current_class_type = type;
5584 /* By default, things in classes are private, while things in
5585 structures or unions are public. */
5586 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5587 ? access_private_node
5588 : access_public_node);
5590 if (previous_class_type != NULL_TREE
5591 && (type != previous_class_type
5592 || !COMPLETE_TYPE_P (previous_class_type))
5593 && current_class_depth == 1)
5595 /* Forcibly remove any old class remnants. */
5596 invalidate_class_lookup_cache ();
5599 /* If we're about to enter a nested class, clear
5600 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5601 if (modify && current_class_depth > 1)
5602 clear_identifier_class_values ();
5607 if (CLASSTYPE_TEMPLATE_INFO (type))
5608 overload_template_name (type);
5613 if (type != previous_class_type || current_class_depth > 1)
5614 push_class_decls (type);
5619 /* We are re-entering the same class we just left, so we
5620 don't have to search the whole inheritance matrix to find
5621 all the decls to bind again. Instead, we install the
5622 cached class_shadowed list, and walk through it binding
5623 names and setting up IDENTIFIER_TYPE_VALUEs. */
5624 set_class_shadows (previous_class_values);
5625 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5627 tree id = TREE_PURPOSE (item);
5628 tree decl = TREE_TYPE (item);
5630 push_class_binding (id, decl);
5631 if (TREE_CODE (decl) == TYPE_DECL)
5632 set_identifier_type_value (id, TREE_TYPE (decl));
5634 unuse_fields (type);
5637 storetags (CLASSTYPE_TAGS (type));
5641 /* When we exit a toplevel class scope, we save the
5642 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5643 reenter the class. Here, we've entered some other class, so we
5644 must invalidate our cache. */
5647 invalidate_class_lookup_cache ()
5651 /* This code can be seen as a cache miss. When we've cached a
5652 class' scope's bindings and we can't use them, we need to reset
5653 them. This is it! */
5654 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5655 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5657 previous_class_type = NULL_TREE;
5660 /* Get out of the current class scope. If we were in a class scope
5661 previously, that is the one popped to. */
5667 /* Since poplevel_class does the popping of class decls nowadays,
5668 this really only frees the obstack used for these decls. */
5671 current_class_depth--;
5672 current_class_name = current_class_stack[current_class_depth].name;
5673 current_class_type = current_class_stack[current_class_depth].type;
5674 current_access_specifier = current_class_stack[current_class_depth].access;
5675 if (current_class_stack[current_class_depth].names_used)
5676 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5679 /* Returns 1 if current_class_type is either T or a nested type of T.
5680 We start looking from 1 because entry 0 is from global scope, and has
5684 currently_open_class (t)
5688 if (t == current_class_type)
5690 for (i = 1; i < current_class_depth; ++i)
5691 if (current_class_stack [i].type == t)
5696 /* If either current_class_type or one of its enclosing classes are derived
5697 from T, return the appropriate type. Used to determine how we found
5698 something via unqualified lookup. */
5701 currently_open_derived_class (t)
5706 if (DERIVED_FROM_P (t, current_class_type))
5707 return current_class_type;
5709 for (i = current_class_depth - 1; i > 0; --i)
5710 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5711 return current_class_stack[i].type;
5716 /* When entering a class scope, all enclosing class scopes' names with
5717 static meaning (static variables, static functions, types and enumerators)
5718 have to be visible. This recursive function calls pushclass for all
5719 enclosing class contexts until global or a local scope is reached.
5720 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5721 formal of the same name. */
5724 push_nested_class (type, modify)
5730 /* A namespace might be passed in error cases, like A::B:C. */
5731 if (type == NULL_TREE
5732 || type == error_mark_node
5733 || TREE_CODE (type) == NAMESPACE_DECL
5734 || ! IS_AGGR_TYPE (type)
5735 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5736 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5739 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5741 if (context && CLASS_TYPE_P (context))
5742 push_nested_class (context, 2);
5743 pushclass (type, modify);
5746 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5751 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5754 if (context && CLASS_TYPE_P (context))
5755 pop_nested_class ();
5758 /* Set global variables CURRENT_LANG_NAME to appropriate value
5759 so that behavior of name-mangling machinery is correct. */
5762 push_lang_context (name)
5765 *current_lang_stack++ = current_lang_name;
5766 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5767 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5769 size_t old_size = VARRAY_SIZE (current_lang_base);
5771 VARRAY_GROW (current_lang_base, old_size + 10);
5772 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5775 if (name == lang_name_cplusplus)
5777 current_lang_name = name;
5779 else if (name == lang_name_java)
5781 current_lang_name = name;
5782 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5783 (See record_builtin_java_type in decl.c.) However, that causes
5784 incorrect debug entries if these types are actually used.
5785 So we re-enable debug output after extern "Java". */
5786 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5787 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5788 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5789 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5790 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5791 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5792 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5793 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5795 else if (name == lang_name_c)
5797 current_lang_name = name;
5800 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5803 /* Get out of the current language scope. */
5808 /* Clear the current entry so that garbage collector won't hold on
5810 *current_lang_stack = NULL_TREE;
5811 current_lang_name = *--current_lang_stack;
5814 /* Type instantiation routines. */
5816 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5817 matches the TARGET_TYPE. If there is no satisfactory match, return
5818 error_mark_node, and issue an error message if COMPLAIN is
5819 non-zero. Permit pointers to member function if PTRMEM is non-zero.
5820 If TEMPLATE_ONLY, the name of the overloaded function
5821 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5822 template arguments. */
5825 resolve_address_of_overloaded_function (target_type,
5836 tree explicit_targs;
5838 /* Here's what the standard says:
5842 If the name is a function template, template argument deduction
5843 is done, and if the argument deduction succeeds, the deduced
5844 arguments are used to generate a single template function, which
5845 is added to the set of overloaded functions considered.
5847 Non-member functions and static member functions match targets of
5848 type "pointer-to-function" or "reference-to-function." Nonstatic
5849 member functions match targets of type "pointer-to-member
5850 function;" the function type of the pointer to member is used to
5851 select the member function from the set of overloaded member
5852 functions. If a nonstatic member function is selected, the
5853 reference to the overloaded function name is required to have the
5854 form of a pointer to member as described in 5.3.1.
5856 If more than one function is selected, any template functions in
5857 the set are eliminated if the set also contains a non-template
5858 function, and any given template function is eliminated if the
5859 set contains a second template function that is more specialized
5860 than the first according to the partial ordering rules 14.5.5.2.
5861 After such eliminations, if any, there shall remain exactly one
5862 selected function. */
5865 int is_reference = 0;
5866 /* We store the matches in a TREE_LIST rooted here. The functions
5867 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5868 interoperability with most_specialized_instantiation. */
5869 tree matches = NULL_TREE;
5872 /* By the time we get here, we should be seeing only real
5873 pointer-to-member types, not the internal POINTER_TYPE to
5874 METHOD_TYPE representation. */
5875 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5876 && (TREE_CODE (TREE_TYPE (target_type))
5877 == METHOD_TYPE)), 0);
5879 if (TREE_CODE (overload) == COMPONENT_REF)
5880 overload = TREE_OPERAND (overload, 1);
5882 /* Check that the TARGET_TYPE is reasonable. */
5883 if (TYPE_PTRFN_P (target_type))
5886 else if (TYPE_PTRMEMFUNC_P (target_type))
5887 /* This is OK, too. */
5889 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5891 /* This is OK, too. This comes from a conversion to reference
5893 target_type = build_reference_type (target_type);
5899 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5900 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5901 return error_mark_node;
5904 /* If we can find a non-template function that matches, we can just
5905 use it. There's no point in generating template instantiations
5906 if we're just going to throw them out anyhow. But, of course, we
5907 can only do this when we don't *need* a template function. */
5912 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5914 tree fn = OVL_FUNCTION (fns);
5917 if (TREE_CODE (fn) == TEMPLATE_DECL)
5918 /* We're not looking for templates just yet. */
5921 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5923 /* We're looking for a non-static member, and this isn't
5924 one, or vice versa. */
5927 /* See if there's a match. */
5928 fntype = TREE_TYPE (fn);
5930 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5931 else if (!is_reference)
5932 fntype = build_pointer_type (fntype);
5934 if (can_convert_arg (target_type, fntype, fn))
5935 matches = tree_cons (fn, NULL_TREE, matches);
5939 /* Now, if we've already got a match (or matches), there's no need
5940 to proceed to the template functions. But, if we don't have a
5941 match we need to look at them, too. */
5944 tree target_fn_type;
5945 tree target_arg_types;
5946 tree target_ret_type;
5951 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5953 target_fn_type = TREE_TYPE (target_type);
5954 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5955 target_ret_type = TREE_TYPE (target_fn_type);
5957 /* Never do unification on the 'this' parameter. */
5958 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5959 target_arg_types = TREE_CHAIN (target_arg_types);
5961 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5963 tree fn = OVL_FUNCTION (fns);
5965 tree instantiation_type;
5968 if (TREE_CODE (fn) != TEMPLATE_DECL)
5969 /* We're only looking for templates. */
5972 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5974 /* We're not looking for a non-static member, and this is
5975 one, or vice versa. */
5978 /* Try to do argument deduction. */
5979 targs = make_tree_vec (DECL_NTPARMS (fn));
5980 if (fn_type_unification (fn, explicit_targs, targs,
5981 target_arg_types, target_ret_type,
5982 DEDUCE_EXACT, -1) != 0)
5983 /* Argument deduction failed. */
5986 /* Instantiate the template. */
5987 instantiation = instantiate_template (fn, targs);
5988 if (instantiation == error_mark_node)
5989 /* Instantiation failed. */
5992 /* See if there's a match. */
5993 instantiation_type = TREE_TYPE (instantiation);
5995 instantiation_type =
5996 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5997 else if (!is_reference)
5998 instantiation_type = build_pointer_type (instantiation_type);
5999 if (can_convert_arg (target_type, instantiation_type, instantiation))
6000 matches = tree_cons (instantiation, fn, matches);
6003 /* Now, remove all but the most specialized of the matches. */
6006 tree match = most_specialized_instantiation (matches);
6008 if (match != error_mark_node)
6009 matches = tree_cons (match, NULL_TREE, NULL_TREE);
6013 /* Now we should have exactly one function in MATCHES. */
6014 if (matches == NULL_TREE)
6016 /* There were *no* matches. */
6019 cp_error ("no matches converting function `%D' to type `%#T'",
6020 DECL_NAME (OVL_FUNCTION (overload)),
6023 /* print_candidates expects a chain with the functions in
6024 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6025 so why be clever?). */
6026 for (; overload; overload = OVL_NEXT (overload))
6027 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6030 print_candidates (matches);
6032 return error_mark_node;
6034 else if (TREE_CHAIN (matches))
6036 /* There were too many matches. */
6042 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6043 DECL_NAME (OVL_FUNCTION (overload)),
6046 /* Since print_candidates expects the functions in the
6047 TREE_VALUE slot, we flip them here. */
6048 for (match = matches; match; match = TREE_CHAIN (match))
6049 TREE_VALUE (match) = TREE_PURPOSE (match);
6051 print_candidates (matches);
6054 return error_mark_node;
6057 /* Good, exactly one match. Now, convert it to the correct type. */
6058 fn = TREE_PURPOSE (matches);
6060 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6061 && !ptrmem && !flag_ms_extensions)
6063 static int explained;
6066 return error_mark_node;
6068 cp_pedwarn ("assuming pointer to member `%D'", fn);
6071 cp_pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6077 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6078 return build_unary_op (ADDR_EXPR, fn, 0);
6081 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6082 will mark the function as addressed, but here we must do it
6084 mark_addressable (fn);
6090 /* This function will instantiate the type of the expression given in
6091 RHS to match the type of LHSTYPE. If errors exist, then return
6092 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6093 we complain on errors. If we are not complaining, never modify rhs,
6094 as overload resolution wants to try many possible instantiations, in
6095 the hope that at least one will work.
6097 For non-recursive calls, LHSTYPE should be a function, pointer to
6098 function, or a pointer to member function. */
6101 instantiate_type (lhstype, rhs, flags)
6103 enum instantiate_type_flags flags;
6105 int complain = (flags & itf_complain);
6106 int strict = (flags & itf_no_attributes)
6107 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6108 int allow_ptrmem = flags & itf_ptrmem_ok;
6110 flags &= ~itf_ptrmem_ok;
6112 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6115 error ("not enough type information");
6116 return error_mark_node;
6119 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6121 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6124 cp_error ("argument of type `%T' does not match `%T'",
6125 TREE_TYPE (rhs), lhstype);
6126 return error_mark_node;
6129 /* We don't overwrite rhs if it is an overloaded function.
6130 Copying it would destroy the tree link. */
6131 if (TREE_CODE (rhs) != OVERLOAD)
6132 rhs = copy_node (rhs);
6134 /* This should really only be used when attempting to distinguish
6135 what sort of a pointer to function we have. For now, any
6136 arithmetic operation which is not supported on pointers
6137 is rejected as an error. */
6139 switch (TREE_CODE (rhs))
6146 my_friendly_abort (177);
6147 return error_mark_node;
6154 new_rhs = instantiate_type (build_pointer_type (lhstype),
6155 TREE_OPERAND (rhs, 0), flags);
6156 if (new_rhs == error_mark_node)
6157 return error_mark_node;
6159 TREE_TYPE (rhs) = lhstype;
6160 TREE_OPERAND (rhs, 0) = new_rhs;
6165 rhs = copy_node (TREE_OPERAND (rhs, 0));
6166 TREE_TYPE (rhs) = unknown_type_node;
6167 return instantiate_type (lhstype, rhs, flags);
6170 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6173 rhs = TREE_OPERAND (rhs, 1);
6174 if (BASELINK_P (rhs))
6175 return instantiate_type (lhstype, TREE_VALUE (rhs),
6176 flags | allow_ptrmem);
6178 /* This can happen if we are forming a pointer-to-member for a
6180 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6184 case TEMPLATE_ID_EXPR:
6186 tree fns = TREE_OPERAND (rhs, 0);
6187 tree args = TREE_OPERAND (rhs, 1);
6190 resolve_address_of_overloaded_function (lhstype,
6194 /*template_only=*/1,
6200 resolve_address_of_overloaded_function (lhstype,
6204 /*template_only=*/0,
6205 /*explicit_targs=*/NULL_TREE);
6208 /* Now we should have a baselink. */
6209 my_friendly_assert (BASELINK_P (rhs), 990412);
6211 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6214 /* This is too hard for now. */
6215 my_friendly_abort (183);
6216 return error_mark_node;
6221 TREE_OPERAND (rhs, 0)
6222 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6223 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6224 return error_mark_node;
6225 TREE_OPERAND (rhs, 1)
6226 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6227 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6228 return error_mark_node;
6230 TREE_TYPE (rhs) = lhstype;
6234 case TRUNC_DIV_EXPR:
6235 case FLOOR_DIV_EXPR:
6237 case ROUND_DIV_EXPR:
6239 case TRUNC_MOD_EXPR:
6240 case FLOOR_MOD_EXPR:
6242 case ROUND_MOD_EXPR:
6243 case FIX_ROUND_EXPR:
6244 case FIX_FLOOR_EXPR:
6246 case FIX_TRUNC_EXPR:
6262 case PREINCREMENT_EXPR:
6263 case PREDECREMENT_EXPR:
6264 case POSTINCREMENT_EXPR:
6265 case POSTDECREMENT_EXPR:
6267 error ("invalid operation on uninstantiated type");
6268 return error_mark_node;
6270 case TRUTH_AND_EXPR:
6272 case TRUTH_XOR_EXPR:
6279 case TRUTH_ANDIF_EXPR:
6280 case TRUTH_ORIF_EXPR:
6281 case TRUTH_NOT_EXPR:
6283 error ("not enough type information");
6284 return error_mark_node;
6287 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6290 error ("not enough type information");
6291 return error_mark_node;
6293 TREE_OPERAND (rhs, 1)
6294 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6295 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6296 return error_mark_node;
6297 TREE_OPERAND (rhs, 2)
6298 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6299 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6300 return error_mark_node;
6302 TREE_TYPE (rhs) = lhstype;
6306 TREE_OPERAND (rhs, 1)
6307 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6308 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6309 return error_mark_node;
6311 TREE_TYPE (rhs) = lhstype;
6316 if (PTRMEM_OK_P (rhs))
6317 flags |= itf_ptrmem_ok;
6319 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6321 case ENTRY_VALUE_EXPR:
6322 my_friendly_abort (184);
6323 return error_mark_node;
6326 return error_mark_node;
6329 my_friendly_abort (185);
6330 return error_mark_node;
6334 /* Return the name of the virtual function pointer field
6335 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6336 this may have to look back through base types to find the
6337 ultimate field name. (For single inheritance, these could
6338 all be the same name. Who knows for multiple inheritance). */
6341 get_vfield_name (type)
6344 tree binfo = TYPE_BINFO (type);
6347 while (BINFO_BASETYPES (binfo)
6348 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6349 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6350 binfo = BINFO_BASETYPE (binfo, 0);
6352 type = BINFO_TYPE (binfo);
6353 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6354 + TYPE_NAME_LENGTH (type) + 2);
6355 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6356 return get_identifier (buf);
6360 print_class_statistics ()
6362 #ifdef GATHER_STATISTICS
6363 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6364 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6365 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6366 n_build_method_call, n_inner_fields_searched);
6369 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6370 n_vtables, n_vtable_searches);
6371 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6372 n_vtable_entries, n_vtable_elems);
6377 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6378 according to [class]:
6379 The class-name is also inserted
6380 into the scope of the class itself. For purposes of access checking,
6381 the inserted class name is treated as if it were a public member name. */
6384 build_self_reference ()
6386 tree name = constructor_name (current_class_type);
6387 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6390 DECL_NONLOCAL (value) = 1;
6391 DECL_CONTEXT (value) = current_class_type;
6392 DECL_ARTIFICIAL (value) = 1;
6394 if (processing_template_decl)
6395 value = push_template_decl (value);
6397 saved_cas = current_access_specifier;
6398 current_access_specifier = access_public_node;
6399 finish_member_declaration (value);
6400 current_access_specifier = saved_cas;
6403 /* Returns 1 if TYPE contains only padding bytes. */
6406 is_empty_class (type)
6411 if (type == error_mark_node)
6414 if (! IS_AGGR_TYPE (type))
6418 return integer_zerop (CLASSTYPE_SIZE (type));
6420 if (TYPE_BINFO_BASETYPES (type))
6422 t = TYPE_FIELDS (type);
6423 while (t && TREE_CODE (t) != FIELD_DECL)
6425 return (t == NULL_TREE);
6428 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6429 a *_TYPE node. NODE can also be a local class. */
6432 get_enclosing_class (type)
6437 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6439 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6442 node = DECL_CONTEXT (node);
6448 node = TYPE_CONTEXT (node);
6452 my_friendly_abort (0);
6458 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6461 is_base_of_enclosing_class (base, type)
6466 if (get_binfo (base, type, 0))
6469 type = get_enclosing_class (type);
6474 /* Note that NAME was looked up while the current class was being
6475 defined and that the result of that lookup was DECL. */
6478 maybe_note_name_used_in_class (name, decl)
6482 splay_tree names_used;
6484 /* If we're not defining a class, there's nothing to do. */
6485 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6488 /* If there's already a binding for this NAME, then we don't have
6489 anything to worry about. */
6490 if (IDENTIFIER_CLASS_VALUE (name))
6493 if (!current_class_stack[current_class_depth - 1].names_used)
6494 current_class_stack[current_class_depth - 1].names_used
6495 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6496 names_used = current_class_stack[current_class_depth - 1].names_used;
6498 splay_tree_insert (names_used,
6499 (splay_tree_key) name,
6500 (splay_tree_value) decl);
6503 /* Note that NAME was declared (as DECL) in the current class. Check
6504 to see that the declaration is legal. */
6507 note_name_declared_in_class (name, decl)
6511 splay_tree names_used;
6514 /* Look to see if we ever used this name. */
6516 = current_class_stack[current_class_depth - 1].names_used;
6520 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6523 /* [basic.scope.class]
6525 A name N used in a class S shall refer to the same declaration
6526 in its context and when re-evaluated in the completed scope of
6528 cp_error ("declaration of `%#D'", decl);
6529 cp_error_at ("changes meaning of `%D' from `%+#D'",
6530 DECL_NAME (OVL_CURRENT (decl)),
6535 /* Returns the VAR_DECL for the complete vtable associated with
6536 BINFO. (Under the new ABI, secondary vtables are merged with
6537 primary vtables; this function will return the VAR_DECL for the
6541 get_vtbl_decl_for_binfo (binfo)
6546 decl = BINFO_VTABLE (binfo);
6547 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6549 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6551 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6554 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6558 /* Called from get_primary_binfo via dfs_walk. */
6561 dfs_get_primary_binfo (binfo, data)
6565 tree primary_base = (tree) data;
6567 if (TREE_VIA_VIRTUAL (binfo)
6568 && same_type_p (TREE_TYPE (binfo), TREE_TYPE (primary_base)))
6574 /* Returns the binfo for the primary base of BINFO. Note that in a
6575 complex hierarchy the resulting BINFO may not actually *be*
6576 primary. In particular if the resulting BINFO is a virtual base,
6577 and it occurs elsewhere in the hierarchy, then this occurrence may
6578 not actually be a primary base in the complete object. Check
6579 BINFO_PRIMARY_MARKED_P to be sure. */
6582 get_primary_binfo (binfo)
6588 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6592 /* A non-virtual primary base is always a direct base, and easy to
6594 if (!TREE_VIA_VIRTUAL (primary_base))
6598 /* Scan the direct basetypes until we find a base with the same
6599 type as the primary base. */
6600 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6602 tree base_binfo = BINFO_BASETYPE (binfo, i);
6604 if (same_type_p (BINFO_TYPE (base_binfo),
6605 BINFO_TYPE (primary_base)))
6609 /* We should always find the primary base. */
6610 my_friendly_abort (20000729);
6613 /* For a primary virtual base, we have to scan the entire hierarchy
6614 rooted at BINFO; the virtual base could be an indirect virtual
6616 result = dfs_walk (binfo, dfs_get_primary_binfo, NULL, primary_base);
6617 my_friendly_assert (result != NULL_TREE, 20000730);
6621 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6622 dominated by T) to stderr. INDENT should be zero when called from
6623 the top level; it is incremented recursively. */
6626 dump_class_hierarchy_r (t, binfo, indent)
6633 fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
6634 (unsigned long) binfo,
6635 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6636 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
6637 tree_low_cst (BINFO_OFFSET (binfo), 0));
6638 if (TREE_VIA_VIRTUAL (binfo))
6639 fprintf (stderr, " virtual");
6640 if (BINFO_PRIMARY_MARKED_P (binfo)
6641 || (TREE_VIA_VIRTUAL (binfo)
6642 && BINFO_PRIMARY_MARKED_P (binfo_for_vbase (BINFO_TYPE (binfo),
6644 fprintf (stderr, " primary");
6645 fprintf (stderr, "\n");
6647 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6648 dump_class_hierarchy_r (t, BINFO_BASETYPE (binfo, i), indent + 2);
6651 /* Dump the BINFO hierarchy for T. */
6654 dump_class_hierarchy (t)
6657 dump_class_hierarchy_r (t, TYPE_BINFO (t), 0);
6660 /* Virtual function table initialization. */
6662 /* Create all the necessary vtables for T and its base classes. */
6668 if (merge_primary_and_secondary_vtables_p ())
6673 /* Under the new ABI, we lay out the primary and secondary
6674 vtables in one contiguous vtable. The primary vtable is
6675 first, followed by the non-virtual secondary vtables in
6676 inheritance graph order. */
6677 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6678 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6679 TYPE_BINFO (t), t, list);
6680 /* Then come the virtual bases, also in inheritance graph
6682 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6684 if (!TREE_VIA_VIRTUAL (vbase))
6687 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), t, list);
6690 if (TYPE_BINFO_VTABLE (t))
6691 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6695 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
6696 dfs_unmarked_real_bases_queue_p, t);
6697 dfs_walk (TYPE_BINFO (t), dfs_unmark,
6698 dfs_marked_real_bases_queue_p, t);
6702 /* Called from finish_vtbls via dfs_walk. */
6705 dfs_finish_vtbls (binfo, data)
6709 tree t = (tree) data;
6711 if (BINFO_NEW_VTABLE_MARKED (binfo, t))
6712 initialize_vtable (binfo,
6713 build_vtbl_initializer (binfo, binfo, t,
6714 TYPE_BINFO (t), NULL));
6716 SET_BINFO_MARKED (binfo);
6721 /* Initialize the vtable for BINFO with the INITS. */
6724 initialize_vtable (binfo, inits)
6730 layout_vtable_decl (binfo, list_length (inits));
6731 decl = get_vtbl_decl_for_binfo (binfo);
6732 initialize_array (decl, inits);
6735 /* Initialize DECL (a declaration for a namespace-scope array) with
6739 initialize_array (decl, inits)
6745 context = DECL_CONTEXT (decl);
6746 DECL_CONTEXT (decl) = NULL_TREE;
6747 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6748 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6749 DECL_CONTEXT (decl) = context;
6752 /* Build the VTT (virtual table table) for T. */
6763 /* Under the old ABI, we don't use VTTs. */
6767 /* Build up the initializers for the VTT. */
6769 index = size_zero_node;
6770 build_vtt_inits (TYPE_BINFO (t), t, /*virtual_vtts_p=*/1,
6773 /* If we didn't need a VTT, we're done. */
6777 /* Figure out the type of the VTT. */
6778 type = build_index_type (size_int (list_length (inits)));
6779 type = build_cplus_array_type (const_ptr_type_node, type);
6781 /* Now, build the VTT object itself. */
6782 vtt = build_vtable (t, get_vtt_name (t), type);
6783 pushdecl_top_level (vtt);
6784 initialize_array (vtt, inits);
6787 /* The type corresponding to BINFO is a base class of T, but BINFO is
6788 in the base class hierarchy of a class derived from T. Return the
6789 base, in T's hierarchy, that corresponds to BINFO. */
6792 get_matching_base (binfo, t)
6799 if (same_type_p (BINFO_TYPE (binfo), t))
6802 if (TREE_VIA_VIRTUAL (binfo))
6803 return binfo_for_vbase (BINFO_TYPE (binfo), t);
6805 derived = get_matching_base (BINFO_INHERITANCE_CHAIN (binfo), t);
6806 for (i = 0; i < BINFO_N_BASETYPES (derived); ++i)
6807 if (same_type_p (BINFO_TYPE (BINFO_BASETYPE (derived, i)),
6808 BINFO_TYPE (binfo)))
6809 return BINFO_BASETYPE (derived, i);
6811 my_friendly_abort (20000628);
6815 /* Recursively build the VTT-initializer for BINFO (which is in the
6816 hierarchy dominated by T). If VIRTUAL_VTTS_P is non-zero, then
6817 sub-VTTs for virtual bases are included. INITS points to the end
6818 of the initializer list to date. INDEX is the VTT index where the
6819 next element will be placed. */
6822 build_vtt_inits (binfo, t, virtual_vtts_p, inits, index)
6832 tree secondary_vptrs;
6835 /* We only need VTTs for subobjects with virtual bases. */
6836 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
6839 /* We need to use a construction vtable if this is not the primary
6841 ctor_vtbl_p = !same_type_p (TREE_TYPE (binfo), t);
6844 build_ctor_vtbl_group (binfo, t);
6846 /* Record the offset in the VTT where this sub-VTT can be found. */
6847 BINFO_SUBVTT_INDEX (binfo) = *index;
6850 /* Add the address of the primary vtable for the complete object. */
6851 init = BINFO_VTABLE (binfo);
6852 if (TREE_CODE (init) == TREE_LIST)
6853 init = TREE_VALUE (init);
6854 *inits = build_tree_list (NULL_TREE, init);
6855 inits = &TREE_CHAIN (*inits);
6856 BINFO_VPTR_INDEX (binfo) = *index;
6857 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
6859 /* Recursively add the secondary VTTs for non-virtual bases. */
6860 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6862 b = BINFO_BASETYPE (binfo, i);
6863 if (!TREE_VIA_VIRTUAL (b))
6864 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
6865 /*virtuals_vtts_p=*/0,
6869 /* Add secondary virtual pointers for all subobjects of BINFO with
6870 either virtual bases or virtual functions overridden along a
6871 virtual path between the declaration and D, except subobjects
6872 that are non-virtual primary bases. */
6873 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
6874 TREE_TYPE (secondary_vptrs) = *index;
6875 dfs_walk_real (binfo,
6876 dfs_build_secondary_vptr_vtt_inits,
6878 dfs_unmarked_real_bases_queue_p,
6880 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
6881 *index = TREE_TYPE (secondary_vptrs);
6883 /* The secondary vptrs come back in reverse order. After we reverse
6884 them, and add the INITS, the last init will be the first element
6886 secondary_vptrs = TREE_VALUE (secondary_vptrs);
6887 if (secondary_vptrs)
6889 *inits = nreverse (secondary_vptrs);
6890 inits = &TREE_CHAIN (secondary_vptrs);
6891 my_friendly_assert (*inits == NULL_TREE, 20000517);
6894 /* Add the secondary VTTs for virtual bases. */
6896 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
6900 if (!TREE_VIA_VIRTUAL (b))
6903 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
6904 inits = build_vtt_inits (vbase, t, /*virtual_vtts_p=*/0,
6908 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
6909 dfs_unmarked_real_bases_queue_p,
6910 build_tree_list (t, binfo));
6915 /* Called from build_vtt_inits via dfs_walk. */
6918 dfs_build_secondary_vptr_vtt_inits (binfo, data)
6930 SET_BINFO_MARKED (binfo);
6932 /* We don't care about bases that don't have vtables. */
6933 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
6936 /* We're only interested in proper subobjects of T. */
6937 if (same_type_p (BINFO_TYPE (binfo), t))
6940 /* We're not interested in non-virtual primary bases. */
6941 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
6944 /* If BINFO doesn't have virtual bases, then we have to look to see
6945 whether or not any virtual functions were overidden along a
6946 virtual path. The point is that given:
6948 struct V { virtual void f(); int i; };
6949 struct C : public virtual V { void f (); };
6951 when we constrct C we need a secondary vptr for V-in-C because we
6952 don't know what the vcall offset for `f' should be. If `V' ends
6953 up in a different place in the complete object, then we'll need a
6954 different vcall offset than that present in the normal V-in-C
6956 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
6957 && !BINFO_OVERRIDE_ALONG_VIRTUAL_PATH_P (get_matching_base (binfo, t)))
6960 /* Record the index where this secondary vptr can be found. */
6961 index = TREE_TYPE (l);
6962 BINFO_VPTR_INDEX (binfo) = index;
6963 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
6964 TYPE_SIZE_UNIT (ptr_type_node));
6966 /* Add the initializer for the secondary vptr itself. */
6967 init = BINFO_VTABLE (binfo);
6968 if (TREE_CODE (init) == TREE_LIST)
6969 init = TREE_VALUE (init);
6970 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
6975 /* Called from build_vtt_inits via dfs_walk. */
6978 dfs_fixup_binfo_vtbls (binfo, data)
6982 CLEAR_BINFO_MARKED (binfo);
6984 /* We don't care about bases that don't have vtables. */
6985 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
6988 /* If we scribbled the construction vtable vptr into BINFO, clear it
6990 if (TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
6991 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
6992 == TREE_VALUE ((tree) data)))
6993 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
6998 /* Build the construction vtable group for BINFO which is in the
6999 hierarchy dominated by T. */
7002 build_ctor_vtbl_group (binfo, t)
7013 /* See if we've already create this construction vtable group. */
7015 id = mangle_ctor_vtbl_for_type (t, binfo);
7017 id = get_ctor_vtbl_name (t, binfo);
7018 if (IDENTIFIER_GLOBAL_VALUE (id))
7021 /* Build a version of VTBL (with the wrong type) for use in
7022 constructing the addresses of secondary vtables in the
7023 construction vtable group. */
7024 vtbl = build_vtable (t, id, ptr_type_node);
7025 list = build_tree_list (vtbl, NULL_TREE);
7026 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7028 for (vbase = TYPE_BINFO (TREE_TYPE (binfo));
7030 vbase = TREE_CHAIN (vbase))
7034 if (!TREE_VIA_VIRTUAL (vbase))
7037 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7038 accumulate_vtbl_inits (b, vbase, binfo, t, list);
7041 inits = TREE_VALUE (list);
7043 /* Figure out the type of the construction vtable. */
7044 type = build_index_type (size_int (list_length (inits)));
7045 type = build_cplus_array_type (vtable_entry_type, type);
7046 TREE_TYPE (vtbl) = type;
7048 /* Initialize the construction vtable. */
7049 pushdecl_top_level (vtbl);
7050 initialize_array (vtbl, inits);
7053 /* Add the vtbl initializers for BINFO (and its non-primary,
7054 non-virtual bases) to the list of INITS. BINFO is in the hierarchy
7055 dominated by T. ORIG_BINFO must have the same type as BINFO, but
7056 may be different from BINFO if we are building a construction
7057 vtable. RTTI_BINFO gives the object that should be used as the
7058 complete object for BINFO. */
7061 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7071 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7072 BINFO_TYPE (orig_binfo)),
7075 /* This is a construction vtable if the RTTI type is not the most
7076 derived type in the hierarchy. */
7077 ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7079 /* If we're building a construction vtable, we're not interested in
7080 subobjects that don't require construction vtables. */
7082 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7083 && !(BINFO_OVERRIDE_ALONG_VIRTUAL_PATH_P
7084 (get_matching_base (binfo, BINFO_TYPE (rtti_binfo)))))
7087 /* Build the initializers for the BINFO-in-T vtable. */
7089 = chainon (TREE_VALUE (inits),
7090 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7091 rtti_binfo, t, inits));
7093 /* Walk the BINFO and its bases. We walk in preorder so that as we
7094 initialize each vtable we can figure out at what offset the
7095 secondary vtable lies from the primary vtable. We can't use
7096 dfs_walk here because we need to iterate through bases of BINFO
7097 and RTTI_BINFO simultaneously. */
7098 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7102 base_binfo = BINFO_BASETYPE (binfo, i);
7103 /* Skip virtual bases. */
7104 if (TREE_VIA_VIRTUAL (base_binfo))
7106 accumulate_vtbl_inits (base_binfo,
7107 BINFO_BASETYPE (orig_binfo, i),
7114 /* Called from finish_vtbls via dfs_walk when using the new ABI.
7115 Accumulates the vtable initializers for all of the vtables into
7116 TREE_VALUE (DATA). Returns the initializers for the BINFO vtable. */
7119 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7126 tree inits = NULL_TREE;
7128 if (BINFO_NEW_VTABLE_MARKED (orig_binfo, t))
7134 /* Compute the initializer for this vtable. */
7135 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7138 /* Figure out the position to which the VPTR should point. */
7139 vtbl = TREE_PURPOSE (l);
7140 vtbl = build1 (ADDR_EXPR,
7143 index = size_binop (PLUS_EXPR,
7144 size_int (non_fn_entries),
7145 size_int (list_length (TREE_VALUE (l))));
7146 index = size_binop (MULT_EXPR,
7147 TYPE_SIZE_UNIT (vtable_entry_type),
7149 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7150 TREE_CONSTANT (vtbl) = 1;
7152 /* For an ordinary vtable, set BINFO_VTABLE. */
7153 if (same_type_p (BINFO_TYPE (rtti_binfo), t))
7154 BINFO_VTABLE (binfo) = vtbl;
7155 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7156 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7157 straighten this out. */
7159 BINFO_VTABLE (binfo) =
7160 tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7166 /* Construct the initializer for BINFOs virtual function table. BINFO
7167 is part of the hierarchy dominated by T. If we're building a
7168 construction vtable, the ORIG_BINFO is the binfo we should use to
7169 find the actual function pointers to put in the vtable. Otherwise,
7170 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7171 BINFO that should be indicated by the RTTI information in the
7172 vtable; it will be a base class of T, rather than T itself, if we
7173 are building a construction vtable.
7175 The value returned is a TREE_LIST suitable for wrapping in a
7176 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7177 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7178 number of non-function entries in the vtable.
7180 It might seem that this function should never be called with a
7181 BINFO for which BINFO_PRIMARY_MARKED_P holds, the vtable for such a
7182 base is always subsumed by a derived class vtable. However, when
7183 we are building construction vtables we do build vtables for
7184 primary bases; we need these while the primary base is being
7188 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7193 int *non_fn_entries_p;
7200 /* Initialize VID. */
7201 memset (&vid, 0, sizeof (vid));
7204 vid.last_init = &vid.inits;
7205 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7206 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7207 /* The first vbase or vcall offset is at index -3 in the vtable. */
7208 vid.index = ssize_int (-3);
7210 /* Add entries to the vtable for RTTI. */
7211 build_rtti_vtbl_entries (binfo, rtti_binfo, &vid);
7213 /* Create an array for keeping track of the functions we've
7214 processed. When we see multiple functions with the same
7215 signature, we share the vcall offsets. */
7216 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7217 /* Add the vcall and vbase offset entries. */
7218 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7220 VARRAY_FREE (vid.fns);
7221 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7222 build_vbase_offset_vtbl_entries. */
7223 for (vbase = CLASSTYPE_VBASECLASSES (t);
7225 vbase = TREE_CHAIN (vbase))
7226 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7228 if (non_fn_entries_p)
7229 *non_fn_entries_p = list_length (vid.inits);
7231 /* Go through all the ordinary virtual functions, building up
7233 vfun_inits = NULL_TREE;
7234 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7242 /* Pull the offset for `this', and the function to call, out of
7244 delta = BV_DELTA (v);
7246 if (BV_USE_VCALL_INDEX_P (v))
7248 vcall_index = BV_VCALL_INDEX (v);
7249 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
7252 vcall_index = NULL_TREE;
7255 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7256 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7258 /* You can't call an abstract virtual function; it's abstract.
7259 So, we replace these functions with __pure_virtual. */
7260 if (DECL_PURE_VIRTUAL_P (fn))
7263 /* Take the address of the function, considering it to be of an
7264 appropriate generic type. */
7265 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7266 /* The address of a function can't change. */
7267 TREE_CONSTANT (pfn) = 1;
7268 /* Enter it in the vtable. */
7269 init = build_vtable_entry (delta, vcall_index, pfn,
7270 BV_GENERATE_THUNK_WITH_VTABLE_P (v));
7271 /* And add it to the chain of initializers. */
7272 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7275 /* The initializers for virtual functions were built up in reverse
7276 order; straighten them out now. */
7277 vfun_inits = nreverse (vfun_inits);
7279 /* The negative offset initializers are also in reverse order. */
7280 vid.inits = nreverse (vid.inits);
7282 /* Chain the two together. */
7283 return chainon (vid.inits, vfun_inits);
7286 /* Sets vid->inits to be the initializers for the vbase and vcall
7287 offsets in BINFO, which is in the hierarchy dominated by T. */
7290 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7292 vtbl_init_data *vid;
7296 /* If this is a derived class, we must first create entries
7297 corresponding to the primary base class. */
7298 b = get_primary_binfo (binfo);
7300 build_vcall_and_vbase_vtbl_entries (b, vid);
7302 /* Add the vbase entries for this base. */
7303 build_vbase_offset_vtbl_entries (binfo, vid);
7304 /* Add the vcall entries for this base. */
7305 build_vcall_offset_vtbl_entries (binfo, vid);
7308 /* Returns the initializers for the vbase offset entries in the vtable
7309 for BINFO (which is part of the class hierarchy dominated by T), in
7310 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7311 where the next vbase offset will go. */
7314 build_vbase_offset_vtbl_entries (binfo, vid)
7316 vtbl_init_data *vid;
7321 /* Under the old ABI, pointers to virtual bases are stored in each
7323 if (!vbase_offsets_in_vtable_p ())
7326 /* If there are no virtual baseclasses, then there is nothing to
7328 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7333 /* Go through the virtual bases, adding the offsets. */
7334 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7336 vbase = TREE_CHAIN (vbase))
7341 if (!TREE_VIA_VIRTUAL (vbase))
7344 /* Find the instance of this virtual base in the complete
7346 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7348 /* If we've already got an offset for this virtual base, we
7349 don't need another one. */
7350 if (BINFO_VTABLE_PATH_MARKED (b))
7352 SET_BINFO_VTABLE_PATH_MARKED (b);
7354 /* Figure out where we can find this vbase offset. */
7355 delta = size_binop (MULT_EXPR,
7358 TYPE_SIZE_UNIT (vtable_entry_type)));
7359 if (vid->primary_vtbl_p)
7360 BINFO_VPTR_FIELD (b) = delta;
7362 if (binfo != TYPE_BINFO (t))
7366 /* Find the instance of this virtual base in the type of BINFO. */
7367 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7368 BINFO_TYPE (binfo));
7370 /* The vbase offset had better be the same. */
7371 if (!tree_int_cst_equal (delta,
7372 BINFO_VPTR_FIELD (orig_vbase)))
7373 my_friendly_abort (20000403);
7376 /* The next vbase will come at a more negative offset. */
7377 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7379 /* The initializer is the delta from BINFO to this virtual base.
7380 The vbase offsets go in reverse inheritance-graph order, and
7381 we are walking in inheritance graph order so these end up in
7383 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (binfo));
7385 = build_tree_list (NULL_TREE,
7386 fold (build1 (NOP_EXPR,
7389 vid->last_init = &TREE_CHAIN (*vid->last_init);
7393 /* Adds the initializers for the vcall offset entries in the vtable
7394 for BINFO (which is part of the class hierarchy dominated by T) to
7398 build_vcall_offset_vtbl_entries (binfo, vid)
7400 vtbl_init_data *vid;
7402 /* Under the old ABI, the adjustments to the `this' pointer were made
7404 if (!vcall_offsets_in_vtable_p ())
7407 /* We only need these entries if this base is a virtual base. */
7408 if (!TREE_VIA_VIRTUAL (binfo))
7411 /* We need a vcall offset for each of the virtual functions in this
7412 vtable. For example:
7414 class A { virtual void f (); };
7415 class B : virtual public A { };
7416 class C: virtual public A, public B {};
7423 The location of `A' is not at a fixed offset relative to `B'; the
7424 offset depends on the complete object derived from `B'. So,
7425 `B' vtable contains an entry for `f' that indicates by what
7426 amount the `this' pointer for `B' needs to be adjusted to arrive
7429 We need entries for all the functions in our primary vtable and
7430 in our non-virtual bases vtables. */
7432 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7433 add_vcall_offset_vtbl_entries_r (binfo, vid);
7436 /* Build vcall offsets, starting with those for BINFO. */
7439 add_vcall_offset_vtbl_entries_r (binfo, vid)
7441 vtbl_init_data *vid;
7446 /* Don't walk into virtual bases -- except, of course, for the
7447 virtual base for which we are building vcall offsets. */
7448 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7451 /* If BINFO has a primary base, process it first. */
7452 primary_binfo = get_primary_binfo (binfo);
7454 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7456 /* Add BINFO itself to the list. */
7457 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7459 /* Scan the non-primary bases of BINFO. */
7460 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7464 base_binfo = BINFO_BASETYPE (binfo, i);
7465 if (base_binfo != primary_binfo)
7466 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7470 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
7473 add_vcall_offset_vtbl_entries_1 (binfo, vid)
7475 vtbl_init_data* vid;
7477 tree derived_virtuals;
7481 /* If BINFO is a primary base, this is the least derived class of
7482 BINFO that is not a primary base. */
7483 tree non_primary_binfo;
7485 binfo_inits = NULL_TREE;
7487 /* We might be a primary base class. Go up the inheritance
7488 hierarchy until we find the class of which we are a primary base:
7489 it is the BINFO_VIRTUALS there that we need to consider. */
7490 non_primary_binfo = binfo;
7491 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7495 /* If we have reached a virtual base, then it must be the
7496 virtual base for which we are building vcall offsets. In
7497 turn, the virtual base must be a (possibly indirect) primary
7498 base of the class that we are initializing, or we wouldn't
7499 care about its vtable offsets. */
7500 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7502 non_primary_binfo = vid->binfo;
7506 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7507 if (get_primary_binfo (b) != non_primary_binfo)
7509 non_primary_binfo = b;
7512 /* Make entries for the rest of the virtuals. */
7513 for (base_virtuals = BINFO_VIRTUALS (binfo),
7514 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7515 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7517 base_virtuals = TREE_CHAIN (base_virtuals),
7518 derived_virtuals = TREE_CHAIN (derived_virtuals),
7519 orig_virtuals = TREE_CHAIN (orig_virtuals))
7527 /* Find the declaration that originally caused this function to
7529 orig_fn = BV_FN (orig_virtuals);
7531 /* We do not need an entry if this function is declared in a
7532 virtual base (or one of its virtual bases), and not
7533 overridden in the section of the hierarchy dominated by the
7534 virtual base for which we are building vcall offsets. */
7535 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7538 /* Find the overriding function. */
7539 fn = BV_FN (derived_virtuals);
7541 /* If there is already an entry for a function with the same
7542 signature as FN, then we do not need a second vcall offset.
7543 Check the list of functions already present in the derived
7545 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7549 derived_entry = VARRAY_TREE (vid->fns, i);
7550 if (same_signature_p (BV_FN (derived_entry), fn))
7552 BV_VCALL_INDEX (derived_virtuals)
7553 = BV_VCALL_INDEX (derived_entry);
7557 if (i != VARRAY_ACTIVE_SIZE (vid->fns))
7560 /* The FN comes from BASE. So, we must caculate the adjustment
7561 from the virtual base that derived from BINFO to BASE. */
7562 base = DECL_CONTEXT (fn);
7563 base_binfo = get_binfo (base, vid->derived, /*protect=*/0);
7565 /* Compute the vcall offset. */
7569 fold (build1 (NOP_EXPR, vtable_entry_type,
7570 size_diffop (BINFO_OFFSET (base_binfo),
7571 BINFO_OFFSET (vid->vbase))))));
7572 vid->last_init = &TREE_CHAIN (*vid->last_init);
7574 /* Keep track of the vtable index where this vcall offset can be
7575 found. For a construction vtable, we already made this
7576 annotation when we build the original vtable. */
7577 if (!vid->ctor_vtbl_p)
7578 BV_VCALL_INDEX (derived_virtuals) = vid->index;
7580 /* The next vcall offset will be found at a more negative
7582 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7584 /* Keep track of this function. */
7585 VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
7589 /* Return vtbl initializers for the RTTI entries coresponding to the
7590 BINFO's vtable. The RTTI entries should indicate the object given
7594 build_rtti_vtbl_entries (binfo, rtti_binfo, vid)
7597 vtbl_init_data *vid;
7606 basetype = BINFO_TYPE (binfo);
7607 t = BINFO_TYPE (rtti_binfo);
7609 /* For a COM object there is no RTTI entry. */
7610 if (CLASSTYPE_COM_INTERFACE (basetype))
7613 /* To find the complete object, we will first convert to our most
7614 primary base, and then add the offset in the vtbl to that value. */
7616 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b)))
7620 primary_base = get_primary_binfo (b);
7621 if (!BINFO_PRIMARY_MARKED_P (primary_base))
7625 offset = size_diffop (BINFO_OFFSET (rtti_binfo), BINFO_OFFSET (b));
7627 /* The second entry is, in the case of the new ABI, the address of
7628 the typeinfo object, or, in the case of the old ABI, a function
7629 which returns a typeinfo object. */
7630 if (new_abi_rtti_p ())
7633 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
7635 decl = integer_zero_node;
7637 /* Convert the declaration to a type that can be stored in the
7639 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
7640 TREE_CONSTANT (init) = 1;
7645 decl = get_tinfo_decl (t);
7647 decl = abort_fndecl;
7649 /* Convert the declaration to a type that can be stored in the
7651 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, decl);
7652 TREE_CONSTANT (init) = 1;
7653 init = build_vtable_entry (offset, NULL_TREE, init,
7654 /*generate_with_vtable_p=*/0);
7656 *vid->last_init = build_tree_list (NULL_TREE, init);
7657 vid->last_init = &TREE_CHAIN (*vid->last_init);
7659 /* Add the offset-to-top entry. It comes earlier in the vtable that
7660 the the typeinfo entry. */
7661 if (flag_vtable_thunks)
7663 /* Convert the offset to look like a function pointer, so that
7664 we can put it in the vtable. */
7665 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
7666 TREE_CONSTANT (init) = 1;
7667 *vid->last_init = build_tree_list (NULL_TREE, init);
7668 vid->last_init = &TREE_CHAIN (*vid->last_init);
7672 /* Build an entry in the virtual function table. DELTA is the offset
7673 for the `this' pointer. VCALL_INDEX is the vtable index containing
7674 the vcall offset; zero if none. ENTRY is the virtual function
7675 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
7676 but it may not actually be a virtual function table pointer. (For
7677 example, it might be the address of the RTTI object, under the new
7681 build_vtable_entry (delta, vcall_index, entry, generate_with_vtable_p)
7685 int generate_with_vtable_p;
7687 if (flag_vtable_thunks)
7691 fn = TREE_OPERAND (entry, 0);
7692 if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
7693 && fn != abort_fndecl
7694 && !DECL_TINFO_FN_P (fn))
7696 entry = make_thunk (entry, delta, vcall_index,
7697 generate_with_vtable_p);
7698 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
7699 TREE_READONLY (entry) = 1;
7700 TREE_CONSTANT (entry) = 1;
7702 #ifdef GATHER_STATISTICS
7703 n_vtable_entries += 1;
7709 tree elems = tree_cons (NULL_TREE, delta,
7710 tree_cons (NULL_TREE, integer_zero_node,
7711 build_tree_list (NULL_TREE, entry)));
7712 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
7714 /* We don't use vcall offsets when not using vtable thunks. */
7715 my_friendly_assert (vcall_index == NULL_TREE, 20000125);
7717 /* DELTA used to be constructed by `size_int' and/or size_binop,
7718 which caused overflow problems when it was negative. That should
7721 if (! int_fits_type_p (delta, delta_type_node))
7723 if (flag_huge_objects)
7724 sorry ("object size exceeds built-in limit for virtual function table implementation");
7726 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
7729 TREE_CONSTANT (entry) = 1;
7730 TREE_STATIC (entry) = 1;
7731 TREE_READONLY (entry) = 1;
7733 #ifdef GATHER_STATISTICS
7734 n_vtable_entries += 1;