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 vcall_offset_data_s
67 /* The binfo for the most-derived type. */
69 /* The binfo for the virtual base for which we're building
72 /* The vcall offset initializers built up so far. */
74 /* The vtable index of the next vcall or vbase offset. */
76 /* Nonzero if we are building the initializer for the primary
81 /* The stack itself. This is an dynamically resized array. The
82 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
83 static int current_class_stack_size;
84 static class_stack_node_t current_class_stack;
86 static tree get_vfield_name PARAMS ((tree));
87 static void finish_struct_anon PARAMS ((tree));
88 static tree build_vbase_pointer PARAMS ((tree, tree));
89 static tree build_vtable_entry PARAMS ((tree, tree, tree));
90 static tree get_vtable_name PARAMS ((tree));
91 static tree get_derived_offset PARAMS ((tree, tree));
92 static tree get_basefndecls PARAMS ((tree, tree));
93 static int build_primary_vtable PARAMS ((tree, tree));
94 static int build_secondary_vtable PARAMS ((tree, tree));
95 static tree dfs_finish_vtbls PARAMS ((tree, void *));
96 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, void *));
97 static void finish_vtbls PARAMS ((tree));
98 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
99 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
100 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
101 static void delete_duplicate_fields PARAMS ((tree));
102 static void finish_struct_bits PARAMS ((tree));
103 static int alter_access PARAMS ((tree, tree, tree));
104 static void handle_using_decl PARAMS ((tree, tree));
105 static int overrides PARAMS ((tree, tree));
106 static int strictly_overrides PARAMS ((tree, tree));
107 static void mark_overriders PARAMS ((tree, tree));
108 static void check_for_override PARAMS ((tree, tree));
109 static tree dfs_modify_vtables PARAMS ((tree, void *));
110 static tree modify_all_vtables PARAMS ((tree, int *, tree));
111 static void determine_primary_base PARAMS ((tree, int *));
112 static void finish_struct_methods PARAMS ((tree));
113 static void maybe_warn_about_overly_private_class PARAMS ((tree));
114 static int field_decl_cmp PARAMS ((const tree *, const tree *));
115 static int method_name_cmp PARAMS ((const tree *, const tree *));
116 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
117 static tree fixed_type_or_null PARAMS ((tree, int *));
118 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
120 static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
121 static tree build_vtbl_initializer PARAMS ((tree, tree, int *));
122 static int count_fields PARAMS ((tree));
123 static int add_fields_to_vec PARAMS ((tree, tree, int));
124 static void check_bitfield_decl PARAMS ((tree));
125 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
126 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
128 static void build_base_field PARAMS ((record_layout_info, tree, int *,
129 unsigned int *, varray_type *));
130 static varray_type build_base_fields PARAMS ((record_layout_info, int *));
131 static tree build_vbase_pointer_fields PARAMS ((record_layout_info, int *));
132 static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, tree,
134 static void check_methods PARAMS ((tree));
135 static void remove_zero_width_bit_fields PARAMS ((tree));
136 static void check_bases PARAMS ((tree, int *, int *, int *));
137 static void check_bases_and_members PARAMS ((tree, int *));
138 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
139 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
140 static void fixup_pending_inline PARAMS ((struct pending_inline *));
141 static void fixup_inline_methods PARAMS ((tree));
142 static void set_primary_base PARAMS ((tree, int, int *));
143 static tree dfs_propagate_binfo_offsets PARAMS ((tree, void *));
144 static void propagate_binfo_offsets PARAMS ((tree, tree));
145 static void layout_virtual_bases PARAMS ((tree, varray_type *));
146 static tree dfs_set_offset_for_shared_vbases PARAMS ((tree, void *));
147 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
148 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vcall_offset_data *));
149 static tree dfs_build_vcall_offset_vtbl_entries PARAMS ((tree, void *));
150 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vcall_offset_data *));
151 static void layout_vtable_decl PARAMS ((tree, int));
152 static tree dfs_find_final_overrider PARAMS ((tree, void *));
153 static tree find_final_overrider PARAMS ((tree, tree, tree));
154 static tree dfs_find_base PARAMS ((tree, void *));
155 static int make_new_vtable PARAMS ((tree, tree));
156 static void dump_class_hierarchy_r PARAMS ((tree, tree, int));
157 extern void dump_class_hierarchy PARAMS ((tree));
158 static tree build_vtable PARAMS ((tree, tree, tree));
159 static void initialize_vtable PARAMS ((tree, tree));
160 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
163 static tree dfs_record_base_offsets PARAMS ((tree, void *));
164 static void record_base_offsets PARAMS ((tree, varray_type *));
165 static tree dfs_search_base_offsets PARAMS ((tree, void *));
166 static int layout_conflict_p PARAMS ((tree, varray_type));
167 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
168 static void layout_empty_base PARAMS ((tree, tree, varray_type));
169 static void accumulate_vtbl_inits PARAMS ((tree, tree));
170 static void set_vindex PARAMS ((tree, tree, int *));
171 static tree build_rtti_vtbl_entries PARAMS ((tree, tree));
172 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
173 vcall_offset_data *));
174 static tree dfs_mark_primary_bases PARAMS ((tree, void *));
175 static void mark_primary_bases PARAMS ((tree));
176 static void clone_constructors_and_destructors PARAMS ((tree));
177 static tree build_clone PARAMS ((tree, tree));
179 /* Variables shared between class.c and call.c. */
181 #ifdef GATHER_STATISTICS
183 int n_vtable_entries = 0;
184 int n_vtable_searches = 0;
185 int n_vtable_elems = 0;
186 int n_convert_harshness = 0;
187 int n_compute_conversion_costs = 0;
188 int n_build_method_call = 0;
189 int n_inner_fields_searched = 0;
192 /* Virtual base class layout. */
194 /* Returns a list of virtual base class pointers as a chain of
198 build_vbase_pointer_fields (rli, empty_p)
199 record_layout_info rli;
202 /* Chain to hold all the new FIELD_DECLs which point at virtual
205 tree vbase_decls = NULL_TREE;
206 tree binfos = TYPE_BINFO_BASETYPES (rec);
207 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
211 /* Under the new ABI, there are no vbase pointers in the object.
212 Instead, the offsets are stored in the vtable. */
213 if (vbase_offsets_in_vtable_p ())
216 /* Loop over the baseclasses, adding vbase pointers as needed. */
217 for (i = 0; i < n_baseclasses; i++)
219 register tree base_binfo = TREE_VEC_ELT (binfos, i);
220 register tree basetype = BINFO_TYPE (base_binfo);
222 if (!COMPLETE_TYPE_P (basetype))
223 /* This error is now reported in xref_tag, thus giving better
224 location information. */
227 /* All basetypes are recorded in the association list of the
230 if (TREE_VIA_VIRTUAL (base_binfo))
235 /* The offset for a virtual base class is only used in computing
236 virtual function tables and for initializing virtual base
237 pointers. It is built once `get_vbase_types' is called. */
239 /* If this basetype can come from another vbase pointer
240 without an additional indirection, we will share
241 that pointer. If an indirection is involved, we
242 make our own pointer. */
243 for (j = 0; j < n_baseclasses; j++)
245 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
246 if (! TREE_VIA_VIRTUAL (other_base_binfo)
247 && BINFO_FOR_VBASE (basetype, BINFO_TYPE (other_base_binfo)))
250 FORMAT_VBASE_NAME (name, basetype);
251 decl = build_vtbl_or_vbase_field (get_identifier (name),
252 get_identifier (VTABLE_BASE),
253 build_pointer_type (basetype),
257 BINFO_VPTR_FIELD (base_binfo) = decl;
258 TREE_CHAIN (decl) = vbase_decls;
259 place_field (rli, decl);
264 /* The space this decl occupies has already been accounted for. */
272 /* Returns a pointer to the virtual base class of EXP that has the
273 indicated TYPE. EXP is of class type, not a pointer type. */
276 build_vbase_pointer (exp, type)
279 if (vbase_offsets_in_vtable_p ())
284 /* Find the shared copy of TYPE; that's where the vtable offset
286 vbase = BINFO_FOR_VBASE (type, TREE_TYPE (exp));
287 /* Find the virtual function table pointer. */
288 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
289 /* Compute the location where the offset will lie. */
290 vbase_ptr = build (PLUS_EXPR,
291 TREE_TYPE (vbase_ptr),
293 BINFO_VPTR_FIELD (vbase));
294 vbase_ptr = build1 (NOP_EXPR,
295 build_pointer_type (ptrdiff_type_node),
297 /* Add the contents of this location to EXP. */
298 return build (PLUS_EXPR,
299 build_pointer_type (type),
300 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
301 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
306 FORMAT_VBASE_NAME (name, type);
307 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
311 /* Build multi-level access to EXPR using hierarchy path PATH.
312 CODE is PLUS_EXPR if we are going with the grain,
313 and MINUS_EXPR if we are not (in which case, we cannot traverse
314 virtual baseclass links).
316 TYPE is the type we want this path to have on exit.
318 NONNULL is non-zero if we know (for any reason) that EXPR is
319 not, in fact, zero. */
322 build_vbase_path (code, type, expr, path, nonnull)
324 tree type, expr, path;
327 register int changed = 0;
328 tree last = NULL_TREE, last_virtual = NULL_TREE;
330 tree null_expr = 0, nonnull_expr;
332 tree offset = integer_zero_node;
334 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
335 return build1 (NOP_EXPR, type, expr);
337 /* We could do better if we had additional logic to convert back to the
338 unconverted type (the static type of the complete object), and then
339 convert back to the type we want. Until that is done, we only optimize
340 if the complete type is the same type as expr has. */
341 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
343 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
344 expr = save_expr (expr);
347 path = reverse_path (path);
349 basetype = BINFO_TYPE (path);
353 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
355 last_virtual = BINFO_TYPE (TREE_VALUE (path));
356 if (code == PLUS_EXPR)
358 changed = ! fixed_type_p;
364 /* We already check for ambiguous things in the caller, just
368 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
369 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
371 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
372 nonnull_expr = build_vbase_pointer (ind, last_virtual);
374 && TREE_CODE (type) == POINTER_TYPE
375 && null_expr == NULL_TREE)
377 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
378 expr = build (COND_EXPR, build_pointer_type (last_virtual),
379 build (EQ_EXPR, boolean_type_node, expr,
381 null_expr, nonnull_expr);
384 /* else we'll figure out the offset below. */
386 /* Happens in the case of parse errors. */
387 if (nonnull_expr == error_mark_node)
388 return error_mark_node;
392 cp_error ("cannot cast up from virtual baseclass `%T'",
394 return error_mark_node;
397 last = TREE_VALUE (path);
398 path = TREE_CHAIN (path);
400 /* LAST is now the last basetype assoc on the path. */
402 /* A pointer to a virtual base member of a non-null object
403 is non-null. Therefore, we only need to test for zeroness once.
404 Make EXPR the canonical expression to deal with here. */
407 TREE_OPERAND (expr, 2) = nonnull_expr;
408 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
409 = TREE_TYPE (nonnull_expr);
414 /* If we go through any virtual base pointers, make sure that
415 casts to BASETYPE from the last virtual base class use
416 the right value for BASETYPE. */
419 tree intype = TREE_TYPE (TREE_TYPE (expr));
421 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
423 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
426 offset = BINFO_OFFSET (last);
428 if (! integer_zerop (offset))
430 /* Bash types to make the backend happy. */
431 offset = cp_convert (type, offset);
433 /* If expr might be 0, we need to preserve that zeroness. */
437 TREE_TYPE (null_expr) = type;
439 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
440 if (TREE_SIDE_EFFECTS (expr))
441 expr = save_expr (expr);
443 return build (COND_EXPR, type,
444 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
446 build (code, type, expr, offset));
448 else return build (code, type, expr, offset);
451 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
452 be used multiple times in initialization of multiple inheritance. */
455 TREE_TYPE (expr) = type;
459 return build1 (NOP_EXPR, type, expr);
463 /* Virtual function things. */
465 /* We want to give the assembler the vtable identifier as well as
466 the offset to the function pointer. So we generate
468 __asm__ __volatile__ (".vtable_entry %c0, %c1"
469 : : "s"(&class_vtable),
470 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
473 build_vtable_entry_ref (basetype, vtbl, idx)
474 tree basetype, vtbl, idx;
476 static char asm_stmt[] = ".vtable_entry %c0, %c1";
479 s = build_unary_op (ADDR_EXPR, get_vtbl_decl_for_binfo (basetype), 0);
480 s = build_tree_list (build_string (1, "s"), s);
482 i = build_array_ref (vtbl, idx);
483 if (!flag_vtable_thunks)
484 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
485 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
486 i2 = build_array_ref (vtbl, build_int_2(0,0));
487 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
488 i = build_binary_op (MINUS_EXPR, i, i2);
489 i = build_tree_list (build_string (1, "i"), i);
491 finish_asm_stmt (ridpointers[RID_VOLATILE],
492 build_string (sizeof(asm_stmt)-1, asm_stmt),
493 NULL_TREE, chainon (s, i), NULL_TREE);
496 /* Given an object INSTANCE, return an expression which yields the
497 virtual function vtable element corresponding to INDEX. There are
498 many special cases for INSTANCE which we take care of here, mainly
499 to avoid creating extra tree nodes when we don't have to. */
502 build_vtbl_ref (instance, idx)
506 tree basetype = TREE_TYPE (instance);
508 if (TREE_CODE (basetype) == REFERENCE_TYPE)
509 basetype = TREE_TYPE (basetype);
511 if (instance == current_class_ref)
512 vtbl = build_vfield_ref (instance, basetype);
517 /* Try to figure out what a reference refers to, and
518 access its virtual function table directly. */
519 tree ref = NULL_TREE;
521 if (TREE_CODE (instance) == INDIRECT_REF
522 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
523 ref = TREE_OPERAND (instance, 0);
524 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
527 if (ref && TREE_CODE (ref) == VAR_DECL
528 && DECL_INITIAL (ref))
530 tree init = DECL_INITIAL (ref);
532 while (TREE_CODE (init) == NOP_EXPR
533 || TREE_CODE (init) == NON_LVALUE_EXPR)
534 init = TREE_OPERAND (init, 0);
535 if (TREE_CODE (init) == ADDR_EXPR)
537 init = TREE_OPERAND (init, 0);
538 if (IS_AGGR_TYPE (TREE_TYPE (init))
539 && (TREE_CODE (init) == PARM_DECL
540 || TREE_CODE (init) == VAR_DECL))
546 if (IS_AGGR_TYPE (TREE_TYPE (instance))
547 && (TREE_CODE (instance) == RESULT_DECL
548 || TREE_CODE (instance) == PARM_DECL
549 || TREE_CODE (instance) == VAR_DECL))
551 vtbl = TYPE_BINFO_VTABLE (basetype);
552 /* Knowing the dynamic type of INSTANCE we can easily obtain
553 the correct vtable entry. In the new ABI, we resolve
554 this back to be in terms of the primary vtable. */
555 if (TREE_CODE (vtbl) == PLUS_EXPR)
557 idx = fold (build (PLUS_EXPR,
560 build (EXACT_DIV_EXPR,
562 TREE_OPERAND (vtbl, 1),
563 TYPE_SIZE_UNIT (vtable_entry_type))));
564 vtbl = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
568 vtbl = build_vfield_ref (instance, basetype);
571 assemble_external (vtbl);
574 build_vtable_entry_ref (basetype, vtbl, idx);
576 aref = build_array_ref (vtbl, idx);
581 /* Given an object INSTANCE, return an expression which yields the
582 virtual function corresponding to INDEX. There are many special
583 cases for INSTANCE which we take care of here, mainly to avoid
584 creating extra tree nodes when we don't have to. */
587 build_vfn_ref (ptr_to_instptr, instance, idx)
588 tree *ptr_to_instptr, instance;
591 tree aref = build_vtbl_ref (instance, idx);
593 /* When using thunks, there is no extra delta, and we get the pfn
595 if (flag_vtable_thunks)
600 /* Save the intermediate result in a SAVE_EXPR so we don't have to
601 compute each component of the virtual function pointer twice. */
602 if (TREE_CODE (aref) == INDIRECT_REF)
603 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
606 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
608 cp_convert (ptrdiff_type_node,
609 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
612 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
615 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
616 for the given TYPE. */
619 get_vtable_name (type)
622 tree type_id = build_typename_overload (type);
623 char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
624 + IDENTIFIER_LENGTH (type_id) + 2);
625 const char *ptr = IDENTIFIER_POINTER (type_id);
627 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
629 /* We don't take off the numbers; build_secondary_vtable uses the
630 DECL_ASSEMBLER_NAME for the type, which includes the number
631 in `3foo'. If we were to pull them off here, we'd end up with
632 something like `_vt.foo.3bar', instead of a uniform definition. */
633 while (ptr[i] >= '0' && ptr[i] <= '9')
636 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
637 return get_identifier (buf);
640 /* Return the offset to the main vtable for a given base BINFO. */
643 get_vfield_offset (binfo)
647 size_binop (PLUS_EXPR, byte_position (TYPE_VFIELD (BINFO_TYPE (binfo))),
648 BINFO_OFFSET (binfo));
651 /* Get the offset to the start of the original binfo that we derived
652 this binfo from. If we find TYPE first, return the offset only
653 that far. The shortened search is useful because the this pointer
654 on method calling is expected to point to a DECL_CONTEXT (fndecl)
655 object, and not a baseclass of it. */
659 get_derived_offset (binfo, type)
662 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
666 while (BINFO_BASETYPES (binfo)
667 && (i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
669 tree binfos = BINFO_BASETYPES (binfo);
670 if (BINFO_TYPE (binfo) == type)
672 binfo = TREE_VEC_ELT (binfos, i);
675 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
676 return size_binop (MINUS_EXPR, offset1, offset2);
679 /* Create a VAR_DECL for a primary or secondary vtable for
680 CLASS_TYPE. Use NAME for the name of the vtable, and VTABLE_TYPE
684 build_vtable (class_type, name, vtable_type)
691 decl = build_lang_decl (VAR_DECL, name, vtable_type);
692 DECL_CONTEXT (decl) = class_type;
693 DECL_ARTIFICIAL (decl) = 1;
694 TREE_STATIC (decl) = 1;
695 #ifndef WRITABLE_VTABLES
696 /* Make them READONLY by default. (mrs) */
697 TREE_READONLY (decl) = 1;
699 DECL_VIRTUAL_P (decl) = 1;
700 import_export_vtable (decl, class_type, 0);
705 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
706 or even complete. If this does not exist, create it. If COMPLETE is
707 non-zero, then complete the definition of it -- that will render it
708 impossible to actually build the vtable, but is useful to get at those
709 which are known to exist in the runtime. */
712 get_vtable_decl (type, complete)
716 tree name = get_vtable_name (type);
717 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
721 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
722 && DECL_VIRTUAL_P (decl), 20000118);
726 decl = build_vtable (type, name, void_type_node);
727 decl = pushdecl_top_level (decl);
728 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
730 /* At one time the vtable info was grabbed 2 words at a time. This
731 fails on sparc unless you have 8-byte alignment. (tiemann) */
732 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
737 DECL_EXTERNAL (decl) = 1;
738 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
744 /* Build the primary virtual function table for TYPE. If BINFO is
745 non-NULL, build the vtable starting with the initial approximation
746 that it is the same as the one which is the head of the association
747 list. Returns a non-zero value if a new vtable is actually
751 build_primary_vtable (binfo, type)
756 decl = get_vtable_decl (type, /*complete=*/0);
760 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
761 /* We have already created a vtable for this base, so there's
762 no need to do it again. */
765 virtuals = copy_list (BINFO_VIRTUALS (binfo));
766 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
767 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
768 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
772 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
774 virtuals = NULL_TREE;
777 #ifdef GATHER_STATISTICS
779 n_vtable_elems += list_length (virtuals);
782 /* Initialize the association list for this type, based
783 on our first approximation. */
784 TYPE_BINFO_VTABLE (type) = decl;
785 TYPE_BINFO_VIRTUALS (type) = virtuals;
787 binfo = TYPE_BINFO (type);
788 SET_BINFO_NEW_VTABLE_MARKED (binfo, type);
792 /* Give TYPE a new virtual function table which is initialized
793 with a skeleton-copy of its original initialization. The only
794 entry that changes is the `delta' entry, so we can really
795 share a lot of structure.
797 FOR_TYPE is the derived type which caused this table to
800 BINFO is the type association which provided TYPE for FOR_TYPE.
802 The order in which vtables are built (by calling this function) for
803 an object must remain the same, otherwise a binary incompatibility
807 build_secondary_vtable (binfo, for_type)
808 tree binfo, for_type;
811 tree orig_decl = BINFO_VTABLE (binfo);
824 if (TREE_VIA_VIRTUAL (binfo))
825 my_friendly_assert (binfo == BINFO_FOR_VBASE (BINFO_TYPE (binfo),
829 if (BINFO_NEW_VTABLE_MARKED (binfo, current_class_type))
830 /* We already created a vtable for this base. There's no need to
834 /* Remember that we've created a vtable for this BINFO, so that we
835 don't try to do so again. */
836 SET_BINFO_NEW_VTABLE_MARKED (binfo, current_class_type);
838 /* Make fresh virtual list, so we can smash it later. */
839 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
841 if (TREE_VIA_VIRTUAL (binfo))
843 tree binfo1 = BINFO_FOR_VBASE (BINFO_TYPE (binfo), for_type);
845 /* XXX - This should never happen, if it does, the caller should
846 ensure that the binfo is from for_type's binfos, not from any
847 base type's. We can remove all this code after a while. */
849 warning ("internal inconsistency: binfo offset error for rtti");
851 offset = BINFO_OFFSET (binfo1);
854 offset = BINFO_OFFSET (binfo);
856 /* In the new ABI, secondary vtables are laid out as part of the
857 same structure as the primary vtable. */
858 if (merge_primary_and_secondary_vtables_p ())
860 BINFO_VTABLE (binfo) = NULL_TREE;
864 /* Create the declaration for the secondary vtable. */
865 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
866 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
867 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
869 /* We know that the vtable that we are going to create doesn't exist
870 yet in the global namespace, and when we finish, it will be
871 pushed into the global namespace. In complex MI hierarchies, we
872 have to loop while the name we are thinking of adding is globally
873 defined, adding more name components to the vtable name as we
874 loop, until the name is unique. This is because in complex MI
875 cases, we might have the same base more than once. This means
876 that the order in which this function is called for vtables must
877 remain the same, otherwise binary compatibility can be
882 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
886 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
888 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
889 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
890 name = get_identifier (buf);
892 /* If this name doesn't clash, then we can use it, otherwise
893 we add more to the name until it is unique. */
895 if (! IDENTIFIER_GLOBAL_VALUE (name))
898 /* Set values for next loop through, if the name isn't unique. */
900 path = BINFO_INHERITANCE_CHAIN (path);
902 /* We better not run out of stuff to make it unique. */
903 my_friendly_assert (path != NULL_TREE, 368);
905 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
907 if (for_type == basetype)
909 /* If we run out of basetypes in the path, we have already
910 found created a vtable with that name before, we now
911 resort to tacking on _%d to distinguish them. */
913 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
914 buf1 = (char *) alloca (i);
916 sprintf (buf1, "%s%c%s%c%d",
917 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
919 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
920 + strlen (buf1) + 1);
921 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
922 name = get_identifier (buf);
924 /* If this name doesn't clash, then we can use it,
925 otherwise we add something different to the name until
927 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
929 /* Hey, they really like MI don't they? Increase the 3
930 above to 6, and the 999 to 999999. :-) */
931 my_friendly_assert (j <= 999, 369);
936 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
937 new_buf2 = (char *) alloca (i);
938 sprintf (new_buf2, "%s%c%s",
939 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
943 new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
944 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
945 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
947 #ifdef GATHER_STATISTICS
949 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
955 /* Create a new vtable for BINFO which is the hierarchy dominated by
959 make_new_vtable (t, binfo)
963 if (binfo == TYPE_BINFO (t))
964 /* In this case, it is *type*'s vtable we are modifying. We start
965 with the approximation that it's vtable is that of the
966 immediate base class. */
967 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
970 /* This is our very own copy of `basetype' to play with. Later,
971 we will fill in all the virtual functions that override the
972 virtual functions in these base classes which are not defined
973 by the current type. */
974 return build_secondary_vtable (binfo, t);
977 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
978 (which is in the hierarchy dominated by T) list FNDECL as its
979 BV_FN. DELTA is the required constant adjustment from the `this'
980 pointer where the vtable entry appears to the `this' required when
981 the function is actually called. */
984 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
995 if (fndecl != BV_FN (v)
996 || !tree_int_cst_equal (delta, BV_DELTA (v)))
1000 /* We need a new vtable for BINFO. */
1001 if (make_new_vtable (t, binfo))
1003 /* If we really did make a new vtable, we also made a copy
1004 of the BINFO_VIRTUALS list. Now, we have to find the
1005 corresponding entry in that list. */
1006 *virtuals = BINFO_VIRTUALS (binfo);
1007 while (BV_FN (*virtuals) != BV_FN (v))
1008 *virtuals = TREE_CHAIN (*virtuals);
1012 base_fndecl = BV_FN (v);
1013 BV_DELTA (v) = delta;
1014 BV_VCALL_INDEX (v) = integer_zero_node;
1017 /* Now assign virtual dispatch information, if unset. We can
1018 dispatch this, through any overridden base function. */
1019 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1021 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1022 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1027 /* Return the index (in the virtual function table) of the first
1028 virtual function. */
1031 first_vfun_index (t)
1034 /* Under the old ABI, the offset-to-top and RTTI entries are at
1035 indices zero and one; under the new ABI, the first virtual
1036 function is at index zero. */
1037 if (!CLASSTYPE_COM_INTERFACE (t) && !flag_new_abi)
1038 return flag_vtable_thunks ? 2 : 1;
1043 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
1044 functions present in the vtable so far. */
1047 set_vindex (t, decl, vfuns_p)
1054 vindex = (*vfuns_p)++;
1055 vindex += first_vfun_index (t);
1056 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
1059 /* Add a virtual function to all the appropriate vtables for the class
1060 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1061 allocate a new slot in our table. If it is error_mark_node, we
1062 know that no other function from another vtable is overridden by X.
1063 VFUNS_P keeps track of how many virtuals there are in our
1064 main vtable for the type, and we build upon the NEW_VIRTUALS list
1068 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1070 tree *new_virtuals_p;
1071 tree *overridden_virtuals_p;
1074 tree t; /* Structure type. */
1078 /* If this function doesn't override anything from a base class, we
1079 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1080 override something, we keep it around and assign its DECL_VINDEX
1081 later, in modify_all_vtables. */
1082 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1083 /* We've already dealt with this function. */
1086 new_virtual = build_tree_list (NULL_TREE, fndecl);
1087 BV_DELTA (new_virtual) = integer_zero_node;
1088 BV_VCALL_INDEX (new_virtual) = integer_zero_node;
1090 if (DECL_VINDEX (fndecl) == error_mark_node)
1092 /* FNDECL is a new virtual function; it doesn't override any
1093 virtual function in a base class. */
1095 /* We remember that this was the base sub-object for rtti. */
1096 CLASSTYPE_RTTI (t) = t;
1098 /* Now assign virtual dispatch information. */
1099 set_vindex (t, fndecl, vfuns_p);
1100 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1102 /* Save the state we've computed on the NEW_VIRTUALS list. */
1103 TREE_CHAIN (new_virtual) = *new_virtuals_p;
1104 *new_virtuals_p = new_virtual;
1108 /* FNDECL overrides a function from a base class. */
1109 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1110 *overridden_virtuals_p = new_virtual;
1114 extern struct obstack *current_obstack;
1116 /* Add method METHOD to class TYPE.
1118 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1119 the class type where the method should be added. */
1122 add_method (type, fields, method)
1123 tree type, *fields, method;
1125 int using = (DECL_CONTEXT (method) != type);
1127 if (fields && *fields)
1128 *fields = build_overload (method, *fields);
1135 if (!CLASSTYPE_METHOD_VEC (type))
1136 /* Make a new method vector. We start with 8 entries. We must
1137 allocate at least two (for constructors and destructors), and
1138 we're going to end up with an assignment operator at some
1141 We could use a TREE_LIST for now, and convert it to a
1142 TREE_VEC in finish_struct, but we would probably waste more
1143 memory making the links in the list than we would by
1144 over-allocating the size of the vector here. Furthermore,
1145 we would complicate all the code that expects this to be a
1147 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1149 method_vec = CLASSTYPE_METHOD_VEC (type);
1150 len = TREE_VEC_LENGTH (method_vec);
1152 /* Constructors and destructors go in special slots. */
1153 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1154 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1155 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1156 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1159 /* See if we already have an entry with this name. */
1160 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
1161 if (!TREE_VEC_ELT (method_vec, slot)
1162 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1164 == DECL_NAME (method)))
1169 /* We need a bigger method vector. */
1170 tree new_vec = make_tree_vec (2 * len);
1171 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1172 (PTR) &TREE_VEC_ELT (new_vec, 0),
1173 len * sizeof (tree));
1175 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1178 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1180 /* Type conversion operators have to come before
1181 ordinary methods; add_conversions depends on this to
1182 speed up looking for conversion operators. So, if
1183 necessary, we slide some of the vector elements up.
1184 In theory, this makes this algorithm O(N^2) but we
1185 don't expect many conversion operators. */
1186 for (slot = 2; slot < len; ++slot)
1188 tree fn = TREE_VEC_ELT (method_vec, slot);
1191 /* There are no more entries in the vector, so we
1192 can insert the new conversion operator here. */
1195 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1196 /* We can insert the new function right at the
1201 if (!TREE_VEC_ELT (method_vec, slot))
1202 /* There is nothing in the Ith slot, so we can avoid
1207 /* We know the last slot in the vector is empty
1208 because we know that at this point there's room
1209 for a new function. */
1210 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1211 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1212 (len - slot - 1) * sizeof (tree));
1213 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1218 if (template_class_depth (type))
1219 /* TYPE is a template class. Don't issue any errors now; wait
1220 until instantiation time to complain. */
1226 /* Check to see if we've already got this method. */
1227 for (fns = TREE_VEC_ELT (method_vec, slot);
1229 fns = OVL_NEXT (fns))
1231 tree fn = OVL_CURRENT (fns);
1233 if (TREE_CODE (fn) != TREE_CODE (method))
1236 if (TREE_CODE (method) != TEMPLATE_DECL)
1238 /* [over.load] Member function declarations with the
1239 same name and the same parameter types cannot be
1240 overloaded if any of them is a static member
1241 function declaration. */
1242 if ((DECL_STATIC_FUNCTION_P (fn)
1243 != DECL_STATIC_FUNCTION_P (method))
1246 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1247 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1249 if (! DECL_STATIC_FUNCTION_P (fn))
1250 parms1 = TREE_CHAIN (parms1);
1251 if (! DECL_STATIC_FUNCTION_P (method))
1252 parms2 = TREE_CHAIN (parms2);
1254 if (compparms (parms1, parms2))
1257 /* Defer to the local function. */
1260 cp_error ("`%#D' and `%#D' cannot be overloaded",
1265 /* Since this is an ordinary function in a
1266 non-template class, it's mangled name can be used
1267 as a unique identifier. This technique is only
1268 an optimization; we would get the same results if
1269 we just used decls_match here. */
1270 if (DECL_ASSEMBLER_NAME (fn)
1271 != DECL_ASSEMBLER_NAME (method))
1274 else if (!decls_match (fn, method))
1277 /* There has already been a declaration of this method
1278 or member template. */
1279 cp_error_at ("`%D' has already been declared in `%T'",
1282 /* We don't call duplicate_decls here to merge the
1283 declarations because that will confuse things if the
1284 methods have inline definitions. In particular, we
1285 will crash while processing the definitions. */
1290 /* Actually insert the new method. */
1291 TREE_VEC_ELT (method_vec, slot)
1292 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1294 /* Add the new binding. */
1295 if (!DECL_CONSTRUCTOR_P (method)
1296 && !DECL_DESTRUCTOR_P (method))
1297 push_class_level_binding (DECL_NAME (method),
1298 TREE_VEC_ELT (method_vec, slot));
1302 /* Subroutines of finish_struct. */
1304 /* Look through the list of fields for this struct, deleting
1305 duplicates as we go. This must be recursive to handle
1308 FIELD is the field which may not appear anywhere in FIELDS.
1309 FIELD_PTR, if non-null, is the starting point at which
1310 chained deletions may take place.
1311 The value returned is the first acceptable entry found
1314 Note that anonymous fields which are not of UNION_TYPE are
1315 not duplicates, they are just anonymous fields. This happens
1316 when we have unnamed bitfields, for example. */
1319 delete_duplicate_fields_1 (field, fields)
1324 if (DECL_NAME (field) == 0)
1326 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1329 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1330 fields = delete_duplicate_fields_1 (x, fields);
1335 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1337 if (DECL_NAME (x) == 0)
1339 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1341 TYPE_FIELDS (TREE_TYPE (x))
1342 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1343 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1346 fields = TREE_CHAIN (fields);
1348 TREE_CHAIN (prev) = TREE_CHAIN (x);
1351 else if (TREE_CODE (field) == USING_DECL)
1352 /* A using declaration may is allowed to appear more than
1353 once. We'll prune these from the field list later, and
1354 handle_using_decl will complain about invalid multiple
1357 else if (DECL_NAME (field) == DECL_NAME (x))
1359 if (TREE_CODE (field) == CONST_DECL
1360 && TREE_CODE (x) == CONST_DECL)
1361 cp_error_at ("duplicate enum value `%D'", x);
1362 else if (TREE_CODE (field) == CONST_DECL
1363 || TREE_CODE (x) == CONST_DECL)
1364 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1366 else if (DECL_DECLARES_TYPE_P (field)
1367 && DECL_DECLARES_TYPE_P (x))
1369 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1371 cp_error_at ("duplicate nested type `%D'", x);
1373 else if (DECL_DECLARES_TYPE_P (field)
1374 || DECL_DECLARES_TYPE_P (x))
1376 /* Hide tag decls. */
1377 if ((TREE_CODE (field) == TYPE_DECL
1378 && DECL_ARTIFICIAL (field))
1379 || (TREE_CODE (x) == TYPE_DECL
1380 && DECL_ARTIFICIAL (x)))
1382 cp_error_at ("duplicate field `%D' (as type and non-type)",
1386 cp_error_at ("duplicate member `%D'", x);
1388 fields = TREE_CHAIN (fields);
1390 TREE_CHAIN (prev) = TREE_CHAIN (x);
1398 delete_duplicate_fields (fields)
1402 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1403 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1406 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1407 legit, otherwise return 0. */
1410 alter_access (t, fdecl, access)
1415 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1418 if (TREE_VALUE (elem) != access)
1420 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1421 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1423 error ("conflicting access specifications for field `%s', ignored",
1424 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1428 /* They're changing the access to the same thing they changed
1429 it to before. That's OK. */
1435 enforce_access (t, fdecl);
1436 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1442 /* Process the USING_DECL, which is a member of T. */
1445 handle_using_decl (using_decl, t)
1449 tree ctype = DECL_INITIAL (using_decl);
1450 tree name = DECL_NAME (using_decl);
1452 = TREE_PRIVATE (using_decl) ? access_private_node
1453 : TREE_PROTECTED (using_decl) ? access_protected_node
1454 : access_public_node;
1456 tree flist = NULL_TREE;
1459 binfo = binfo_or_else (ctype, t);
1463 if (name == constructor_name (ctype)
1464 || name == constructor_name_full (ctype))
1466 cp_error_at ("using-declaration for constructor", using_decl);
1470 fdecl = lookup_member (binfo, name, 0, 0);
1474 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1478 if (BASELINK_P (fdecl))
1479 /* Ignore base type this came from. */
1480 fdecl = TREE_VALUE (fdecl);
1482 old_value = IDENTIFIER_CLASS_VALUE (name);
1485 if (is_overloaded_fn (old_value))
1486 old_value = OVL_CURRENT (old_value);
1488 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1491 old_value = NULL_TREE;
1494 if (is_overloaded_fn (fdecl))
1496 else if (! DECL_LANG_SPECIFIC (fdecl))
1497 my_friendly_abort (20000221);
1501 else if (is_overloaded_fn (old_value))
1504 /* It's OK to use functions from a base when there are functions with
1505 the same name already present in the current class. */;
1508 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1509 cp_error_at (" because of local method `%#D' with same name",
1510 OVL_CURRENT (old_value));
1516 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1517 cp_error_at (" because of local field `%#D' with same name", old_value);
1521 /* Make type T see field decl FDECL with access ACCESS.*/
1523 for (; flist; flist = OVL_NEXT (flist))
1525 add_method (t, 0, OVL_CURRENT (flist));
1526 alter_access (t, OVL_CURRENT (flist), access);
1529 alter_access (t, fdecl, access);
1532 /* Run through the base clases of T, updating
1533 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1534 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1538 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1541 int *cant_have_default_ctor_p;
1542 int *cant_have_const_ctor_p;
1543 int *no_const_asn_ref_p;
1547 int seen_nearly_empty_base_p;
1550 binfos = TYPE_BINFO_BASETYPES (t);
1551 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1552 seen_nearly_empty_base_p = 0;
1554 /* An aggregate cannot have baseclasses. */
1555 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1557 for (i = 0; i < n_baseclasses; ++i)
1562 /* Figure out what base we're looking at. */
1563 base_binfo = TREE_VEC_ELT (binfos, i);
1564 basetype = TREE_TYPE (base_binfo);
1566 /* If the type of basetype is incomplete, then we already
1567 complained about that fact (and we should have fixed it up as
1569 if (!COMPLETE_TYPE_P (basetype))
1572 /* The base type is of incomplete type. It is
1573 probably best to pretend that it does not
1575 if (i == n_baseclasses-1)
1576 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1577 TREE_VEC_LENGTH (binfos) -= 1;
1579 for (j = i; j+1 < n_baseclasses; j++)
1580 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1584 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1585 here because the case of virtual functions but non-virtual
1586 dtor is handled in finish_struct_1. */
1587 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1588 && TYPE_HAS_DESTRUCTOR (basetype))
1589 cp_warning ("base class `%#T' has a non-virtual destructor",
1592 /* If the base class doesn't have copy constructors or
1593 assignment operators that take const references, then the
1594 derived class cannot have such a member automatically
1596 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1597 *cant_have_const_ctor_p = 1;
1598 if (TYPE_HAS_ASSIGN_REF (basetype)
1599 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1600 *no_const_asn_ref_p = 1;
1601 /* Similarly, if the base class doesn't have a default
1602 constructor, then the derived class won't have an
1603 automatically generated default constructor. */
1604 if (TYPE_HAS_CONSTRUCTOR (basetype)
1605 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1607 *cant_have_default_ctor_p = 1;
1608 if (! TYPE_HAS_CONSTRUCTOR (t))
1609 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1613 /* If the base class is not empty or nearly empty, then this
1614 class cannot be nearly empty. */
1615 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1616 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1617 /* And if there is more than one nearly empty base, then the
1618 derived class is not nearly empty either. */
1619 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1620 && seen_nearly_empty_base_p)
1621 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1622 /* If this is the first nearly empty base class, then remember
1624 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1625 seen_nearly_empty_base_p = 1;
1627 /* A lot of properties from the bases also apply to the derived
1629 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1630 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1631 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1632 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1633 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1634 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1635 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1636 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1637 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1638 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1640 /* Derived classes can implicitly become COMified if their bases
1642 if (CLASSTYPE_COM_INTERFACE (basetype))
1643 CLASSTYPE_COM_INTERFACE (t) = 1;
1644 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1647 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1649 CLASSTYPE_COM_INTERFACE (t) = 0;
1654 /* Called via dfs_walk from mark_primary_bases. Sets
1655 BINFO_PRIMARY_MARKED_P for BINFO, if appropriate. */
1658 dfs_mark_primary_bases (binfo, data)
1665 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1668 i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
1669 base_binfo = BINFO_BASETYPE (binfo, i);
1671 if (!TREE_VIA_VIRTUAL (base_binfo))
1672 /* Non-virtual base classes are easy. */
1673 BINFO_PRIMARY_MARKED_P (base_binfo) = 1;
1679 = BINFO_FOR_VBASE (BINFO_TYPE (base_binfo), (tree) data);
1681 /* If this virtual base is not already primary somewhere else in
1682 the hiearchy, then we'll be using this copy. */
1683 if (!BINFO_VBASE_PRIMARY_P (shared_binfo))
1685 BINFO_VBASE_PRIMARY_P (shared_binfo) = 1;
1686 BINFO_PRIMARY_MARKED_P (base_binfo) = 1;
1693 /* Set BINFO_PRIMARY_MARKED_P for all binfos in the hierarchy
1694 dominated by BINFO that are primary bases. */
1697 mark_primary_bases (type)
1702 /* Mark the TYPE_BINFO hierarchy. We need to mark primary bases in
1703 pre-order to deal with primary virtual bases. (The virtual base
1704 would be skipped if it were not marked as primary, and that
1705 requires getting to dfs_mark_primary_bases before
1706 dfs_skip_nonprimary_vbases_unmarkedp has a chance to skip the
1708 dfs_walk_real (TYPE_BINFO (type), dfs_mark_primary_bases, NULL,
1709 dfs_skip_nonprimary_vbases_unmarkedp, type);
1711 /* Now go through the virtual base classes in inheritance graph
1712 order. Any that are not already primary will need to be
1713 allocated in TYPE, and so we need to mark their primary bases. */
1714 for (vbases = TYPE_BINFO (type); vbases; vbases = TREE_CHAIN (vbases))
1718 /* Make sure that only BINFOs appear on this list.
1719 Historically, the TREE_CHAIN was used for other purposes, and
1720 we want to make sure that none of those uses remain. */
1721 my_friendly_assert (TREE_CODE (vbases) == TREE_VEC, 20000402);
1723 if (!TREE_VIA_VIRTUAL (vbases))
1726 vbase = BINFO_FOR_VBASE (BINFO_TYPE (vbases), type);
1727 if (BINFO_VBASE_PRIMARY_P (vbase))
1728 /* This virtual base was already included in the hierarchy, so
1729 there's nothing to do here. */
1732 /* Temporarily pretend that VBASE is primary so that its bases
1733 will be walked; this is the real copy of VBASE. */
1734 BINFO_PRIMARY_MARKED_P (vbase) = 1;
1736 /* Now, walk its bases. */
1737 dfs_walk_real (vbase, dfs_mark_primary_bases, NULL,
1738 dfs_skip_nonprimary_vbases_unmarkedp, type);
1740 /* VBASE wasn't really primary. */
1741 BINFO_PRIMARY_MARKED_P (vbase) = 0;
1745 /* Make the Ith baseclass of T its primary base. */
1748 set_primary_base (t, i, vfuns_p)
1755 CLASSTYPE_VFIELD_PARENT (t) = i;
1756 basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
1757 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1758 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1759 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1760 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1761 *vfuns_p = CLASSTYPE_VSIZE (basetype);
1764 /* Determine the primary class for T. */
1767 determine_primary_base (t, vfuns_p)
1771 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1773 /* If there are no baseclasses, there is certainly no primary base. */
1774 if (n_baseclasses == 0)
1779 for (i = 0; i < n_baseclasses; i++)
1781 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1782 tree basetype = BINFO_TYPE (base_binfo);
1784 if (TYPE_CONTAINS_VPTR_P (basetype))
1786 /* Even a virtual baseclass can contain our RTTI
1787 information. But, we prefer a non-virtual polymorphic
1789 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1790 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1792 /* A virtual baseclass can't be the primary base under the
1793 old ABI. And under the new ABI we still prefer a
1794 non-virtual base. */
1795 if (TREE_VIA_VIRTUAL (base_binfo))
1798 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1800 set_primary_base (t, i, vfuns_p);
1801 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1807 /* Only add unique vfields, and flatten them out as we go. */
1808 for (vfields = CLASSTYPE_VFIELDS (basetype);
1810 vfields = TREE_CHAIN (vfields))
1811 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1812 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1813 CLASSTYPE_VFIELDS (t)
1814 = tree_cons (base_binfo,
1815 VF_BASETYPE_VALUE (vfields),
1816 CLASSTYPE_VFIELDS (t));
1818 if (!flag_new_abi && *vfuns_p == 0)
1819 set_primary_base (t, i, vfuns_p);
1824 if (!TYPE_VFIELD (t))
1825 CLASSTYPE_VFIELD_PARENT (t) = -1;
1827 /* The new ABI allows for the use of a "nearly-empty" virtual base
1828 class as the primary base class if no non-virtual polymorphic
1829 base can be found. */
1830 if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1831 for (i = 0; i < n_baseclasses; ++i)
1833 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1834 tree basetype = BINFO_TYPE (base_binfo);
1836 if (TREE_VIA_VIRTUAL (base_binfo)
1837 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1839 set_primary_base (t, i, vfuns_p);
1840 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1845 /* Mark the primary base classes at this point. */
1846 mark_primary_bases (t);
1849 /* Set memoizing fields and bits of T (and its variants) for later
1853 finish_struct_bits (t)
1856 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1858 /* Fix up variants (if any). */
1859 tree variants = TYPE_NEXT_VARIANT (t);
1862 /* These fields are in the _TYPE part of the node, not in
1863 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1864 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1865 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1866 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1867 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1868 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1870 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1871 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1872 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1873 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1874 /* Copy whatever these are holding today. */
1875 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1876 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1877 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1878 TYPE_SIZE (variants) = TYPE_SIZE (t);
1879 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1880 variants = TYPE_NEXT_VARIANT (variants);
1883 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1884 /* For a class w/o baseclasses, `finish_struct' has set
1885 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1886 definition). Similarly for a class whose base classes do not
1887 have vtables. When neither of these is true, we might have
1888 removed abstract virtuals (by providing a definition), added
1889 some (by declaring new ones), or redeclared ones from a base
1890 class. We need to recalculate what's really an abstract virtual
1891 at this point (by looking in the vtables). */
1892 get_pure_virtuals (t);
1896 /* Notice whether this class has type conversion functions defined. */
1897 tree binfo = TYPE_BINFO (t);
1898 tree binfos = BINFO_BASETYPES (binfo);
1901 for (i = n_baseclasses-1; i >= 0; i--)
1903 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1905 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1909 /* If this type has a copy constructor, force its mode to be BLKmode, and
1910 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1911 be passed by invisible reference and prevent it from being returned in
1914 Also do this if the class has BLKmode but can still be returned in
1915 registers, since function_cannot_inline_p won't let us inline
1916 functions returning such a type. This affects the HP-PA. */
1917 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1918 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1919 && CLASSTYPE_NON_AGGREGATE (t)))
1922 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1923 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1925 TYPE_MODE (variants) = BLKmode;
1926 TREE_ADDRESSABLE (variants) = 1;
1931 /* Issue warnings about T having private constructors, but no friends,
1934 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1935 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1936 non-private static member functions. */
1939 maybe_warn_about_overly_private_class (t)
1942 int has_member_fn = 0;
1943 int has_nonprivate_method = 0;
1946 if (!warn_ctor_dtor_privacy
1947 /* If the class has friends, those entities might create and
1948 access instances, so we should not warn. */
1949 || (CLASSTYPE_FRIEND_CLASSES (t)
1950 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1951 /* We will have warned when the template was declared; there's
1952 no need to warn on every instantiation. */
1953 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1954 /* There's no reason to even consider warning about this
1958 /* We only issue one warning, if more than one applies, because
1959 otherwise, on code like:
1962 // Oops - forgot `public:'
1968 we warn several times about essentially the same problem. */
1970 /* Check to see if all (non-constructor, non-destructor) member
1971 functions are private. (Since there are no friends or
1972 non-private statics, we can't ever call any of the private member
1974 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1975 /* We're not interested in compiler-generated methods; they don't
1976 provide any way to call private members. */
1977 if (!DECL_ARTIFICIAL (fn))
1979 if (!TREE_PRIVATE (fn))
1981 if (DECL_STATIC_FUNCTION_P (fn))
1982 /* A non-private static member function is just like a
1983 friend; it can create and invoke private member
1984 functions, and be accessed without a class
1988 has_nonprivate_method = 1;
1991 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1995 if (!has_nonprivate_method && has_member_fn)
1997 /* There are no non-private methods, and there's at least one
1998 private member function that isn't a constructor or
1999 destructor. (If all the private members are
2000 constructors/destructors we want to use the code below that
2001 issues error messages specifically referring to
2002 constructors/destructors.) */
2004 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2005 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2006 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2007 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2009 has_nonprivate_method = 1;
2012 if (!has_nonprivate_method)
2014 cp_warning ("all member functions in class `%T' are private", t);
2019 /* Even if some of the member functions are non-private, the class
2020 won't be useful for much if all the constructors or destructors
2021 are private: such an object can never be created or destroyed. */
2022 if (TYPE_HAS_DESTRUCTOR (t))
2024 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2026 if (TREE_PRIVATE (dtor))
2028 cp_warning ("`%#T' only defines a private destructor and has no friends",
2034 if (TYPE_HAS_CONSTRUCTOR (t))
2036 int nonprivate_ctor = 0;
2038 /* If a non-template class does not define a copy
2039 constructor, one is defined for it, enabling it to avoid
2040 this warning. For a template class, this does not
2041 happen, and so we would normally get a warning on:
2043 template <class T> class C { private: C(); };
2045 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2046 complete non-template or fully instantiated classes have this
2048 if (!TYPE_HAS_INIT_REF (t))
2049 nonprivate_ctor = 1;
2051 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2055 tree ctor = OVL_CURRENT (fn);
2056 /* Ideally, we wouldn't count copy constructors (or, in
2057 fact, any constructor that takes an argument of the
2058 class type as a parameter) because such things cannot
2059 be used to construct an instance of the class unless
2060 you already have one. But, for now at least, we're
2062 if (! TREE_PRIVATE (ctor))
2064 nonprivate_ctor = 1;
2069 if (nonprivate_ctor == 0)
2071 cp_warning ("`%#T' only defines private constructors and has no friends",
2078 /* Function to help qsort sort FIELD_DECLs by name order. */
2081 field_decl_cmp (x, y)
2084 if (DECL_NAME (*x) == DECL_NAME (*y))
2085 /* A nontype is "greater" than a type. */
2086 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2087 if (DECL_NAME (*x) == NULL_TREE)
2089 if (DECL_NAME (*y) == NULL_TREE)
2091 if (DECL_NAME (*x) < DECL_NAME (*y))
2096 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2099 method_name_cmp (m1, m2)
2100 const tree *m1, *m2;
2102 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2104 if (*m1 == NULL_TREE)
2106 if (*m2 == NULL_TREE)
2108 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2113 /* Warn about duplicate methods in fn_fields. Also compact method
2114 lists so that lookup can be made faster.
2116 Data Structure: List of method lists. The outer list is a
2117 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2118 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2119 links the entire list of methods for TYPE_METHODS. Friends are
2120 chained in the same way as member functions (? TREE_CHAIN or
2121 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2122 list. That allows them to be quickly deleted, and requires no
2125 Sort methods that are not special (i.e., constructors, destructors,
2126 and type conversion operators) so that we can find them faster in
2130 finish_struct_methods (t)
2137 if (!TYPE_METHODS (t))
2139 /* Clear these for safety; perhaps some parsing error could set
2140 these incorrectly. */
2141 TYPE_HAS_CONSTRUCTOR (t) = 0;
2142 TYPE_HAS_DESTRUCTOR (t) = 0;
2143 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2147 method_vec = CLASSTYPE_METHOD_VEC (t);
2148 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2149 len = TREE_VEC_LENGTH (method_vec);
2151 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2152 and the next few with type conversion operators (if any). */
2153 for (fn_fields = TYPE_METHODS (t); fn_fields;
2154 fn_fields = TREE_CHAIN (fn_fields))
2155 /* Clear out this flag. */
2156 DECL_IN_AGGR_P (fn_fields) = 0;
2158 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2159 /* We thought there was a destructor, but there wasn't. Some
2160 parse errors cause this anomalous situation. */
2161 TYPE_HAS_DESTRUCTOR (t) = 0;
2163 /* Issue warnings about private constructors and such. If there are
2164 no methods, then some public defaults are generated. */
2165 maybe_warn_about_overly_private_class (t);
2167 /* Now sort the methods. */
2168 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2170 TREE_VEC_LENGTH (method_vec) = len;
2172 /* The type conversion ops have to live at the front of the vec, so we
2174 for (slot = 2; slot < len; ++slot)
2176 tree fn = TREE_VEC_ELT (method_vec, slot);
2178 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2182 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2183 (int (*)(const void *, const void *))method_name_cmp);
2186 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2189 duplicate_tag_error (t)
2192 cp_error ("redefinition of `%#T'", t);
2193 cp_error_at ("previous definition here", t);
2195 /* Pretend we haven't defined this type. */
2197 /* All of the component_decl's were TREE_CHAINed together in the parser.
2198 finish_struct_methods walks these chains and assembles all methods with
2199 the same base name into DECL_CHAINs. Now we don't need the parser chains
2200 anymore, so we unravel them. */
2202 /* This used to be in finish_struct, but it turns out that the
2203 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2205 if (CLASSTYPE_METHOD_VEC (t))
2207 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2208 int i, len = TREE_VEC_LENGTH (method_vec);
2209 for (i = 0; i < len; i++)
2211 tree unchain = TREE_VEC_ELT (method_vec, i);
2212 while (unchain != NULL_TREE)
2214 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2215 unchain = OVL_NEXT (unchain);
2220 if (TYPE_LANG_SPECIFIC (t))
2222 tree binfo = TYPE_BINFO (t);
2223 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2224 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2225 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2226 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2228 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2229 BINFO_BASETYPES(binfo) = NULL_TREE;
2231 TYPE_BINFO (t) = binfo;
2232 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2233 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2234 TYPE_REDEFINED (t) = 1;
2235 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2236 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2238 TYPE_SIZE (t) = NULL_TREE;
2239 TYPE_MODE (t) = VOIDmode;
2240 TYPE_FIELDS (t) = NULL_TREE;
2241 TYPE_METHODS (t) = NULL_TREE;
2242 TYPE_VFIELD (t) = NULL_TREE;
2243 TYPE_CONTEXT (t) = NULL_TREE;
2244 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2247 /* Make the BINFO's vtablehave N entries, including RTTI entries,
2248 vbase and vcall offsets, etc. Set its type and call the backend
2252 layout_vtable_decl (binfo, n)
2260 itype = size_int (n);
2261 atype = build_cplus_array_type (vtable_entry_type,
2262 build_index_type (itype));
2263 layout_type (atype);
2265 /* We may have to grow the vtable. */
2266 vtable = get_vtbl_decl_for_binfo (binfo);
2267 if (!same_type_p (TREE_TYPE (vtable), atype))
2269 TREE_TYPE (vtable) = atype;
2270 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2271 layout_decl (vtable, 0);
2273 /* At one time the vtable info was grabbed 2 words at a time. This
2274 fails on Sparc unless you have 8-byte alignment. */
2275 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2276 DECL_ALIGN (vtable));
2280 /* True if we should override the given BASE_FNDECL with the given
2284 overrides (fndecl, base_fndecl)
2285 tree fndecl, base_fndecl;
2287 /* Destructors have special names. */
2288 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl))
2290 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2292 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2294 tree types, base_types;
2296 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2297 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2299 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2300 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2301 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2302 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2303 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2309 typedef struct find_final_overrider_data_s {
2310 /* The function for which we are trying to find a final overrider. */
2312 /* The base class in which the function was declared. */
2313 tree declaring_base;
2314 /* The most derived class in the hierarchy. */
2315 tree most_derived_type;
2316 /* The final overriding function. */
2318 /* The BINFO for the class in which the final overriding function
2320 tree overriding_base;
2321 } find_final_overrider_data;
2323 /* Called from find_final_overrider via dfs_walk. */
2326 dfs_find_final_overrider (binfo, data)
2330 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2332 if (same_type_p (BINFO_TYPE (binfo),
2333 BINFO_TYPE (ffod->declaring_base))
2334 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2335 BINFO_OFFSET (ffod->declaring_base)))
2340 /* We've found a path to the declaring base. Walk down the path
2341 looking for an overrider for FN. */
2342 for (path = reverse_path (binfo);
2344 path = TREE_CHAIN (path))
2346 for (method = TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path)));
2348 method = TREE_CHAIN (method))
2349 if (DECL_VIRTUAL_P (method) && overrides (method, ffod->fn))
2356 /* If we found an overrider, record the overriding function, and
2357 the base from which it came. */
2360 if (ffod->overriding_fn && ffod->overriding_fn != method)
2362 /* We've found a different overrider along a different
2363 path. That can be OK if the new one overrides the
2366 struct S { virtual void f(); };
2367 struct T : public virtual S { virtual void f(); };
2368 struct U : public virtual S, public virtual T {};
2370 Here `T::f' is the final overrider for `S::f'. */
2371 if (strictly_overrides (method, ffod->overriding_fn))
2373 ffod->overriding_fn = method;
2374 ffod->overriding_base = TREE_VALUE (path);
2376 else if (!strictly_overrides (ffod->overriding_fn, method))
2378 cp_error ("no unique final overrider for `%D' in `%T'",
2379 ffod->most_derived_type,
2381 cp_error ("candidates are: `%#D'", ffod->overriding_fn);
2382 cp_error (" `%#D'", method);
2383 return error_mark_node;
2386 else if (ffod->overriding_base
2387 && (!tree_int_cst_equal
2388 (BINFO_OFFSET (TREE_VALUE (path)),
2389 BINFO_OFFSET (ffod->overriding_base))))
2391 /* We've found two instances of the same base that
2392 provide overriders. */
2393 cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'",
2395 BINFO_TYPE (ffod->overriding_base),
2396 ffod->most_derived_type);
2397 return error_mark_node;
2401 ffod->overriding_fn = method;
2402 ffod->overriding_base = TREE_VALUE (path);
2410 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2411 FN and whose TREE_VALUE is the binfo for the base where the
2412 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2413 base object in which FN is declared. */
2416 find_final_overrider (t, binfo, fn)
2421 find_final_overrider_data ffod;
2423 /* Getting this right is a little tricky. This is legal:
2425 struct S { virtual void f (); };
2426 struct T { virtual void f (); };
2427 struct U : public S, public T { };
2429 even though calling `f' in `U' is ambiguous. But,
2431 struct R { virtual void f(); };
2432 struct S : virtual public R { virtual void f (); };
2433 struct T : virtual public R { virtual void f (); };
2434 struct U : public S, public T { };
2436 is not -- there's no way to decide whether to put `S::f' or
2437 `T::f' in the vtable for `R'.
2439 The solution is to look at all paths to BINFO. If we find
2440 different overriders along any two, then there is a problem. */
2442 ffod.declaring_base = binfo;
2443 ffod.most_derived_type = t;
2444 ffod.overriding_fn = NULL_TREE;
2445 ffod.overriding_base = NULL_TREE;
2447 if (dfs_walk (TYPE_BINFO (t),
2448 dfs_find_final_overrider,
2451 return error_mark_node;
2453 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2456 /* Called via dfs_walk. Returns BINFO if BINFO has the same type as
2457 DATA (which is really an _TYPE node). */
2460 dfs_find_base (binfo, data)
2464 return (same_type_p (BINFO_TYPE (binfo), (tree) data)
2465 ? binfo : NULL_TREE);
2468 /* Update a entry in the vtable for BINFO, which is in the hierarchy
2469 dominated by T. FN has been overridden in BINFO; VIRTUALS points
2470 to the corresponding position in the BINFO_VIRTUALS list. */
2473 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2483 HOST_WIDE_INT vindex_val;
2486 /* Find the function which originally caused this vtable
2487 entry to be present. */
2488 vindex = DECL_VINDEX (fn);
2489 b = dfs_walk (binfo, dfs_find_base, NULL, DECL_VIRTUAL_CONTEXT (fn));
2490 fn = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (b)));
2491 i = first_vfun_index (BINFO_TYPE (b));
2492 vindex_val = tree_low_cst (vindex, 0);
2493 while (i < vindex_val)
2495 fn = TREE_CHAIN (fn);
2500 /* Handle the case of a virtual function defined in BINFO itself. */
2501 overrider = find_final_overrider (t, b, fn);
2502 if (overrider == error_mark_node)
2505 /* Compute the constant adjustment to the `this' pointer. The
2506 `this' pointer, when this function is called, will point at the
2507 class whose vtable this is. */
2508 delta = size_binop (PLUS_EXPR,
2509 get_derived_offset (binfo,
2510 DECL_VIRTUAL_CONTEXT (fn)),
2511 BINFO_OFFSET (binfo));
2514 /* Under the new ABI, we only need to adjust as far as the
2515 nearest virtual base. Then we use the vcall offset in the
2516 virtual bases vtable. */
2517 for (b = binfo; b; b = BINFO_INHERITANCE_CHAIN (b))
2519 if (TREE_VIA_VIRTUAL (b))
2521 if (same_type_p (BINFO_TYPE (b),
2522 BINFO_TYPE (TREE_VALUE (overrider))))
2529 if (b && TREE_VIA_VIRTUAL (b))
2530 /* The `this' pointer needs to be adjusted to the nearest virtual
2532 delta = size_diffop (BINFO_OFFSET (b), delta);
2534 /* The `this' pointer needs to be adjusted from pointing to
2535 BINFO to pointing at the base where the final overrider
2537 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)), delta);
2539 modify_vtable_entry (t,
2541 TREE_PURPOSE (overrider),
2546 /* Called from modify_all_vtables via dfs_walk. */
2549 dfs_modify_vtables (binfo, data)
2553 if (/* There's no need to modify the vtable for a primary base;
2554 we're not going to use that vtable anyhow. */
2555 !BINFO_PRIMARY_MARKED_P (binfo)
2556 /* Similarly, a base without a vtable needs no modification. */
2557 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2565 /* If we're supporting RTTI then we always need a new vtable to
2566 point to the RTTI information. Under the new ABI we may need
2567 a new vtable to contain vcall and vbase offsets. */
2568 if (flag_rtti || flag_new_abi)
2569 make_new_vtable (t, binfo);
2571 /* Now, go through each of the virtual functions in the virtual
2572 function table for BINFO. Find the final overrider, and
2573 update the BINFO_VIRTUALS list appropriately. */
2574 for (virtuals = BINFO_VIRTUALS (binfo),
2575 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2577 virtuals = TREE_CHAIN (virtuals),
2578 old_virtuals = TREE_CHAIN (old_virtuals))
2579 update_vtable_entry_for_fn (t,
2581 BV_FN (old_virtuals),
2585 SET_BINFO_MARKED (binfo);
2590 /* Update all of the primary and secondary vtables for T. Create new
2591 vtables as required, and initialize their RTTI information. Each
2592 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2593 function from a base class; find and modify the appropriate entries
2594 to point to the overriding functions. Returns a list, in
2595 declaration order, of the functions that are overridden in this
2596 class, but do not appear in the primary base class vtable, and
2597 which should therefore be appended to the end of the vtable for T. */
2600 modify_all_vtables (t, vfuns_p, overridden_virtuals)
2603 tree overridden_virtuals;
2607 binfo = TYPE_BINFO (t);
2609 /* Update all of the vtables. */
2612 dfs_unmarked_real_bases_queue_p,
2614 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2616 /* If we should include overriding functions for secondary vtables
2617 in our primary vtable, add them now. */
2618 if (all_overridden_vfuns_in_vtables_p ())
2620 tree *fnsp = &overridden_virtuals;
2624 tree fn = TREE_VALUE (*fnsp);
2626 if (!BINFO_VIRTUALS (binfo)
2627 || !value_member (fn, BINFO_VIRTUALS (binfo)))
2629 /* Set the vtable index. */
2630 set_vindex (t, fn, vfuns_p);
2631 /* We don't need to convert to a base class when calling
2633 DECL_VIRTUAL_CONTEXT (fn) = t;
2635 /* We don't need to adjust the `this' pointer when
2636 calling this function. */
2637 BV_DELTA (*fnsp) = integer_zero_node;
2638 BV_VCALL_INDEX (*fnsp) = integer_zero_node;
2640 /* This is an overridden function not already in our
2642 fnsp = &TREE_CHAIN (*fnsp);
2645 /* We've already got an entry for this function. Skip
2647 *fnsp = TREE_CHAIN (*fnsp);
2651 overridden_virtuals = NULL_TREE;
2653 return overridden_virtuals;
2656 /* Here, we already know that they match in every respect.
2657 All we have to check is where they had their declarations. */
2660 strictly_overrides (fndecl1, fndecl2)
2661 tree fndecl1, fndecl2;
2663 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
2664 DECL_CONTEXT (fndecl1),
2666 if (distance == -2 || distance > 0)
2671 /* Get the base virtual function declarations in T that are either
2672 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2673 the overrider/hider. */
2676 get_basefndecls (fndecl, t)
2679 tree methods = TYPE_METHODS (t);
2680 tree base_fndecls = NULL_TREE;
2681 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2682 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2686 if (TREE_CODE (methods) == FUNCTION_DECL
2687 && DECL_VINDEX (methods) != NULL_TREE
2688 && DECL_NAME (fndecl) == DECL_NAME (methods))
2689 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
2691 methods = TREE_CHAIN (methods);
2695 return base_fndecls;
2697 for (i = 0; i < n_baseclasses; i++)
2699 tree base_binfo = TREE_VEC_ELT (binfos, i);
2700 tree basetype = BINFO_TYPE (base_binfo);
2702 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2706 return base_fndecls;
2709 /* Mark the functions that have been hidden with their overriders.
2710 Since we start out with all functions already marked with a hider,
2711 no need to mark functions that are just hidden.
2713 Subroutine of warn_hidden. */
2716 mark_overriders (fndecl, base_fndecls)
2717 tree fndecl, base_fndecls;
2719 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2721 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
2722 TREE_PURPOSE (base_fndecls) = fndecl;
2726 /* If this declaration supersedes the declaration of
2727 a method declared virtual in the base class, then
2728 mark this field as being virtual as well. */
2731 check_for_override (decl, ctype)
2734 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2735 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2736 int virtualp = DECL_VIRTUAL_P (decl);
2737 int found_overriden_fn = 0;
2739 for (i = 0; i < n_baselinks; i++)
2741 tree base_binfo = TREE_VEC_ELT (binfos, i);
2742 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
2744 tree tmp = get_matching_virtual
2745 (base_binfo, decl, DECL_DESTRUCTOR_P (decl));
2747 if (tmp && !found_overriden_fn)
2749 /* If this function overrides some virtual in some base
2750 class, then the function itself is also necessarily
2751 virtual, even if the user didn't explicitly say so. */
2752 DECL_VIRTUAL_P (decl) = 1;
2754 /* The TMP we really want is the one from the deepest
2755 baseclass on this path, taking care not to
2756 duplicate if we have already found it (via another
2757 path to its virtual baseclass. */
2758 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2760 cp_error_at ("`static %#D' cannot be declared", decl);
2761 cp_error_at (" since `virtual %#D' declared in base class",
2767 /* Set DECL_VINDEX to a value that is neither an
2768 INTEGER_CST nor the error_mark_node so that
2769 add_virtual_function will realize this is an
2770 overridden function. */
2772 = tree_cons (tmp, NULL_TREE, DECL_VINDEX (decl));
2774 /* We now know that DECL overrides something,
2775 which is all that is important. But, we must
2776 continue to iterate through all the base-classes
2777 in order to allow get_matching_virtual to check for
2778 various illegal overrides. */
2779 found_overriden_fn = 1;
2785 if (DECL_VINDEX (decl) == NULL_TREE)
2786 DECL_VINDEX (decl) = error_mark_node;
2787 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2791 /* Warn about hidden virtual functions that are not overridden in t.
2792 We know that constructors and destructors don't apply. */
2798 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2799 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2802 /* We go through each separately named virtual function. */
2803 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2805 tree fns = TREE_VEC_ELT (method_vec, i);
2806 tree fndecl = NULL_TREE;
2808 tree base_fndecls = NULL_TREE;
2809 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2810 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2812 /* First see if we have any virtual functions in this batch. */
2813 for (; fns; fns = OVL_NEXT (fns))
2815 fndecl = OVL_CURRENT (fns);
2816 if (DECL_VINDEX (fndecl))
2820 if (fns == NULL_TREE)
2823 /* First we get a list of all possible functions that might be
2824 hidden from each base class. */
2825 for (i = 0; i < n_baseclasses; i++)
2827 tree base_binfo = TREE_VEC_ELT (binfos, i);
2828 tree basetype = BINFO_TYPE (base_binfo);
2830 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2834 fns = OVL_NEXT (fns);
2836 /* ...then mark up all the base functions with overriders, preferring
2837 overriders to hiders. */
2839 for (; fns; fns = OVL_NEXT (fns))
2841 fndecl = OVL_CURRENT (fns);
2842 if (DECL_VINDEX (fndecl))
2843 mark_overriders (fndecl, base_fndecls);
2846 /* Now give a warning for all base functions without overriders,
2847 as they are hidden. */
2848 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2850 if (! overrides (TREE_PURPOSE (base_fndecls),
2851 TREE_VALUE (base_fndecls)))
2853 /* Here we know it is a hider, and no overrider exists. */
2854 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2855 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
2861 /* Check for things that are invalid. There are probably plenty of other
2862 things we should check for also. */
2865 finish_struct_anon (t)
2870 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2872 if (TREE_STATIC (field))
2874 if (TREE_CODE (field) != FIELD_DECL)
2877 if (DECL_NAME (field) == NULL_TREE
2878 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2880 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2881 for (; elt; elt = TREE_CHAIN (elt))
2883 if (DECL_ARTIFICIAL (elt))
2886 if (DECL_NAME (elt) == constructor_name (t))
2887 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2890 if (TREE_CODE (elt) != FIELD_DECL)
2892 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2897 if (TREE_PRIVATE (elt))
2898 cp_pedwarn_at ("private member `%#D' in anonymous union",
2900 else if (TREE_PROTECTED (elt))
2901 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2904 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2905 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2911 /* Create default constructors, assignment operators, and so forth for
2912 the type indicated by T, if they are needed.
2913 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2914 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2915 class cannot have a default constructor, copy constructor taking a
2916 const reference argument, or an assignment operator taking a const
2917 reference, respectively. If a virtual destructor is created, its
2918 DECL is returned; otherwise the return value is NULL_TREE. */
2921 add_implicitly_declared_members (t, cant_have_default_ctor,
2922 cant_have_const_cctor,
2923 cant_have_const_assignment)
2925 int cant_have_default_ctor;
2926 int cant_have_const_cctor;
2927 int cant_have_const_assignment;
2930 tree implicit_fns = NULL_TREE;
2931 tree virtual_dtor = NULL_TREE;
2935 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2937 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2938 check_for_override (default_fn, t);
2940 /* If we couldn't make it work, then pretend we didn't need it. */
2941 if (default_fn == void_type_node)
2942 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2945 TREE_CHAIN (default_fn) = implicit_fns;
2946 implicit_fns = default_fn;
2948 if (DECL_VINDEX (default_fn))
2949 virtual_dtor = default_fn;
2953 /* Any non-implicit destructor is non-trivial. */
2954 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2956 /* Default constructor. */
2957 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2959 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2960 TREE_CHAIN (default_fn) = implicit_fns;
2961 implicit_fns = default_fn;
2964 /* Copy constructor. */
2965 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2967 /* ARM 12.18: You get either X(X&) or X(const X&), but
2970 = implicitly_declare_fn (sfk_copy_constructor, t,
2971 /*const_p=*/!cant_have_const_cctor);
2972 TREE_CHAIN (default_fn) = implicit_fns;
2973 implicit_fns = default_fn;
2976 /* Assignment operator. */
2977 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2980 = implicitly_declare_fn (sfk_assignment_operator, t,
2981 /*const_p=*/!cant_have_const_assignment);
2982 TREE_CHAIN (default_fn) = implicit_fns;
2983 implicit_fns = default_fn;
2986 /* Now, hook all of the new functions on to TYPE_METHODS,
2987 and add them to the CLASSTYPE_METHOD_VEC. */
2988 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2989 add_method (t, 0, *f);
2990 *f = TYPE_METHODS (t);
2991 TYPE_METHODS (t) = implicit_fns;
2993 return virtual_dtor;
2996 /* Subroutine of finish_struct_1. Recursively count the number of fields
2997 in TYPE, including anonymous union members. */
3000 count_fields (fields)
3005 for (x = fields; x; x = TREE_CHAIN (x))
3007 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3008 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3015 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3016 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3019 add_fields_to_vec (fields, field_vec, idx)
3020 tree fields, field_vec;
3024 for (x = fields; x; x = TREE_CHAIN (x))
3026 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3027 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3029 TREE_VEC_ELT (field_vec, idx++) = x;
3034 /* FIELD is a bit-field. We are finishing the processing for its
3035 enclosing type. Issue any appropriate messages and set appropriate
3039 check_bitfield_decl (field)
3042 tree type = TREE_TYPE (field);
3045 /* Detect invalid bit-field type. */
3046 if (DECL_INITIAL (field)
3047 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3049 cp_error_at ("bit-field `%#D' with non-integral type", field);
3050 w = error_mark_node;
3053 /* Detect and ignore out of range field width. */
3054 if (DECL_INITIAL (field))
3056 w = DECL_INITIAL (field);
3058 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3061 /* detect invalid field size. */
3062 if (TREE_CODE (w) == CONST_DECL)
3063 w = DECL_INITIAL (w);
3065 w = decl_constant_value (w);
3067 if (TREE_CODE (w) != INTEGER_CST)
3069 cp_error_at ("bit-field `%D' width not an integer constant",
3071 w = error_mark_node;
3073 else if (tree_int_cst_sgn (w) < 0)
3075 cp_error_at ("negative width in bit-field `%D'", field);
3076 w = error_mark_node;
3078 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3080 cp_error_at ("zero width for bit-field `%D'", field);
3081 w = error_mark_node;
3083 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3084 && TREE_CODE (type) != ENUMERAL_TYPE
3085 && TREE_CODE (type) != BOOLEAN_TYPE)
3086 cp_warning_at ("width of `%D' exceeds its type", field);
3087 else if (TREE_CODE (type) == ENUMERAL_TYPE
3088 && (0 > compare_tree_int (w,
3089 min_precision (TYPE_MIN_VALUE (type),
3090 TREE_UNSIGNED (type)))
3091 || 0 > compare_tree_int (w,
3093 (TYPE_MAX_VALUE (type),
3094 TREE_UNSIGNED (type)))))
3095 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3099 /* Remove the bit-field width indicator so that the rest of the
3100 compiler does not treat that value as an initializer. */
3101 DECL_INITIAL (field) = NULL_TREE;
3103 if (w != error_mark_node)
3105 DECL_SIZE (field) = convert (bitsizetype, w);
3106 DECL_BIT_FIELD (field) = 1;
3108 if (integer_zerop (w))
3110 #ifdef EMPTY_FIELD_BOUNDARY
3111 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3112 EMPTY_FIELD_BOUNDARY);
3114 #ifdef PCC_BITFIELD_TYPE_MATTERS
3115 if (PCC_BITFIELD_TYPE_MATTERS)
3116 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3123 /* Non-bit-fields are aligned for their type. */
3124 DECL_BIT_FIELD (field) = 0;
3125 CLEAR_DECL_C_BIT_FIELD (field);
3126 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3130 /* FIELD is a non bit-field. We are finishing the processing for its
3131 enclosing type T. Issue any appropriate messages and set appropriate
3135 check_field_decl (field, t, cant_have_const_ctor,
3136 cant_have_default_ctor, no_const_asn_ref,
3137 any_default_members)
3140 int *cant_have_const_ctor;
3141 int *cant_have_default_ctor;
3142 int *no_const_asn_ref;
3143 int *any_default_members;
3145 tree type = strip_array_types (TREE_TYPE (field));
3147 /* An anonymous union cannot contain any fields which would change
3148 the settings of CANT_HAVE_CONST_CTOR and friends. */
3149 if (ANON_UNION_TYPE_P (type))
3151 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3152 structs. So, we recurse through their fields here. */
3153 else if (ANON_AGGR_TYPE_P (type))
3157 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3158 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3159 check_field_decl (fields, t, cant_have_const_ctor,
3160 cant_have_default_ctor, no_const_asn_ref,
3161 any_default_members);
3163 /* Check members with class type for constructors, destructors,
3165 else if (CLASS_TYPE_P (type))
3167 /* Never let anything with uninheritable virtuals
3168 make it through without complaint. */
3169 abstract_virtuals_error (field, type);
3171 if (TREE_CODE (t) == UNION_TYPE)
3173 if (TYPE_NEEDS_CONSTRUCTING (type))
3174 cp_error_at ("member `%#D' with constructor not allowed in union",
3176 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3177 cp_error_at ("member `%#D' with destructor not allowed in union",
3179 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3180 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3185 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3186 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3187 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3188 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3189 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3192 if (!TYPE_HAS_CONST_INIT_REF (type))
3193 *cant_have_const_ctor = 1;
3195 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3196 *no_const_asn_ref = 1;
3198 if (TYPE_HAS_CONSTRUCTOR (type)
3199 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3200 *cant_have_default_ctor = 1;
3202 if (DECL_INITIAL (field) != NULL_TREE)
3204 /* `build_class_init_list' does not recognize
3206 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3207 cp_error_at ("multiple fields in union `%T' initialized");
3208 *any_default_members = 1;
3211 /* Non-bit-fields are aligned for their type, except packed fields
3212 which require only BITS_PER_UNIT alignment. */
3213 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3214 (DECL_PACKED (field)
3216 : TYPE_ALIGN (TREE_TYPE (field))));
3219 /* Check the data members (both static and non-static), class-scoped
3220 typedefs, etc., appearing in the declaration of T. Issue
3221 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3222 declaration order) of access declarations; each TREE_VALUE in this
3223 list is a USING_DECL.
3225 In addition, set the following flags:
3228 The class is empty, i.e., contains no non-static data members.
3230 CANT_HAVE_DEFAULT_CTOR_P
3231 This class cannot have an implicitly generated default
3234 CANT_HAVE_CONST_CTOR_P
3235 This class cannot have an implicitly generated copy constructor
3236 taking a const reference.
3238 CANT_HAVE_CONST_ASN_REF
3239 This class cannot have an implicitly generated assignment
3240 operator taking a const reference.
3242 All of these flags should be initialized before calling this
3245 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3246 fields can be added by adding to this chain. */
3249 check_field_decls (t, access_decls, empty_p,
3250 cant_have_default_ctor_p, cant_have_const_ctor_p,
3255 int *cant_have_default_ctor_p;
3256 int *cant_have_const_ctor_p;
3257 int *no_const_asn_ref_p;
3262 int any_default_members;
3264 /* First, delete any duplicate fields. */
3265 delete_duplicate_fields (TYPE_FIELDS (t));
3267 /* Assume there are no access declarations. */
3268 *access_decls = NULL_TREE;
3269 /* Assume this class has no pointer members. */
3271 /* Assume none of the members of this class have default
3273 any_default_members = 0;
3275 for (field = &TYPE_FIELDS (t); *field; field = next)
3278 tree type = TREE_TYPE (x);
3280 GNU_xref_member (current_class_name, x);
3282 next = &TREE_CHAIN (x);
3284 if (TREE_CODE (x) == FIELD_DECL)
3286 DECL_PACKED (x) |= TYPE_PACKED (t);
3288 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3289 /* We don't treat zero-width bitfields as making a class
3294 /* The class is non-empty. */
3296 /* The class is not even nearly empty. */
3297 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3301 if (TREE_CODE (x) == USING_DECL)
3303 /* Prune the access declaration from the list of fields. */
3304 *field = TREE_CHAIN (x);
3306 /* Save the access declarations for our caller. */
3307 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3309 /* Since we've reset *FIELD there's no reason to skip to the
3315 if (TREE_CODE (x) == TYPE_DECL
3316 || TREE_CODE (x) == TEMPLATE_DECL)
3319 /* If we've gotten this far, it's a data member, possibly static,
3320 or an enumerator. */
3322 DECL_CONTEXT (x) = t;
3324 /* ``A local class cannot have static data members.'' ARM 9.4 */
3325 if (current_function_decl && TREE_STATIC (x))
3326 cp_error_at ("field `%D' in local class cannot be static", x);
3328 /* Perform error checking that did not get done in
3330 if (TREE_CODE (type) == FUNCTION_TYPE)
3332 cp_error_at ("field `%D' invalidly declared function type",
3334 type = build_pointer_type (type);
3335 TREE_TYPE (x) = type;
3337 else if (TREE_CODE (type) == METHOD_TYPE)
3339 cp_error_at ("field `%D' invalidly declared method type", x);
3340 type = build_pointer_type (type);
3341 TREE_TYPE (x) = type;
3343 else if (TREE_CODE (type) == OFFSET_TYPE)
3345 cp_error_at ("field `%D' invalidly declared offset type", x);
3346 type = build_pointer_type (type);
3347 TREE_TYPE (x) = type;
3350 if (type == error_mark_node)
3353 /* When this goes into scope, it will be a non-local reference. */
3354 DECL_NONLOCAL (x) = 1;
3356 if (TREE_CODE (x) == CONST_DECL)
3359 if (TREE_CODE (x) == VAR_DECL)
3361 if (TREE_CODE (t) == UNION_TYPE)
3362 /* Unions cannot have static members. */
3363 cp_error_at ("field `%D' declared static in union", x);
3368 /* Now it can only be a FIELD_DECL. */
3370 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3371 CLASSTYPE_NON_AGGREGATE (t) = 1;
3373 /* If this is of reference type, check if it needs an init.
3374 Also do a little ANSI jig if necessary. */
3375 if (TREE_CODE (type) == REFERENCE_TYPE)
3377 CLASSTYPE_NON_POD_P (t) = 1;
3378 if (DECL_INITIAL (x) == NULL_TREE)
3379 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3381 /* ARM $12.6.2: [A member initializer list] (or, for an
3382 aggregate, initialization by a brace-enclosed list) is the
3383 only way to initialize nonstatic const and reference
3385 *cant_have_default_ctor_p = 1;
3386 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3388 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3391 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3393 cp_warning_at ("non-static reference in class without a constructor", x);
3397 type = strip_array_types (type);
3399 if (TREE_CODE (type) == POINTER_TYPE)
3402 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3403 CLASSTYPE_HAS_MUTABLE (t) = 1;
3405 if (! pod_type_p (type)
3406 /* For some reason, pointers to members are POD types themselves,
3407 but are not allowed in POD structs. Silly. */
3408 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3409 CLASSTYPE_NON_POD_P (t) = 1;
3411 /* If any field is const, the structure type is pseudo-const. */
3412 if (CP_TYPE_CONST_P (type))
3414 C_TYPE_FIELDS_READONLY (t) = 1;
3415 if (DECL_INITIAL (x) == NULL_TREE)
3416 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3418 /* ARM $12.6.2: [A member initializer list] (or, for an
3419 aggregate, initialization by a brace-enclosed list) is the
3420 only way to initialize nonstatic const and reference
3422 *cant_have_default_ctor_p = 1;
3423 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3425 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3428 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3430 cp_warning_at ("non-static const member in class without a constructor", x);
3433 /* A field that is pseudo-const makes the structure likewise. */
3434 else if (IS_AGGR_TYPE (type))
3436 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3437 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3438 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3441 /* We set DECL_C_BIT_FIELD in grokbitfield.
3442 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3443 if (DECL_C_BIT_FIELD (x))
3444 check_bitfield_decl (x);
3446 check_field_decl (x, t,
3447 cant_have_const_ctor_p,
3448 cant_have_default_ctor_p,
3450 &any_default_members);
3453 /* Effective C++ rule 11. */
3454 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3455 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3457 cp_warning ("`%#T' has pointer data members", t);
3459 if (! TYPE_HAS_INIT_REF (t))
3461 cp_warning (" but does not override `%T(const %T&)'", t, t);
3462 if (! TYPE_HAS_ASSIGN_REF (t))
3463 cp_warning (" or `operator=(const %T&)'", t);
3465 else if (! TYPE_HAS_ASSIGN_REF (t))
3466 cp_warning (" but does not override `operator=(const %T&)'", t);
3470 /* Check anonymous struct/anonymous union fields. */
3471 finish_struct_anon (t);
3473 /* We've built up the list of access declarations in reverse order.
3475 *access_decls = nreverse (*access_decls);
3478 /* Return a FIELD_DECL for a pointer-to-virtual-table or
3479 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
3480 field are as indicated. The CLASS_TYPE in which this field occurs
3481 is also indicated. FCONTEXT is the type that is needed for the debug
3482 info output routines. *EMPTY_P is set to a non-zero value by this
3483 function to indicate that a class containing this field is
3487 build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
3490 tree assembler_name;
3498 /* This class is non-empty. */
3501 /* Build the FIELD_DECL. */
3502 field = build_lang_decl (FIELD_DECL, name, type);
3503 DECL_ASSEMBLER_NAME (field) = assembler_name;
3504 DECL_VIRTUAL_P (field) = 1;
3505 DECL_ARTIFICIAL (field) = 1;
3506 DECL_FIELD_CONTEXT (field) = class_type;
3507 DECL_FCONTEXT (field) = fcontext;
3508 DECL_ALIGN (field) = TYPE_ALIGN (type);
3514 /* Record the type of BINFO in the slot in DATA (which is really a
3515 `varray_type *') corresponding to the BINFO_OFFSET. */
3518 dfs_record_base_offsets (binfo, data)
3523 unsigned HOST_WIDE_INT offset = tree_low_cst (BINFO_OFFSET (binfo), 1);
3525 v = (varray_type *) data;
3526 while (VARRAY_SIZE (*v) <= offset)
3527 VARRAY_GROW (*v, 2 * VARRAY_SIZE (*v));
3528 VARRAY_TREE (*v, offset) = tree_cons (NULL_TREE,
3530 VARRAY_TREE (*v, offset));
3535 /* Add the offset of BINFO and its bases to BASE_OFFSETS. */
3538 record_base_offsets (binfo, base_offsets)
3540 varray_type *base_offsets;
3543 dfs_record_base_offsets,
3548 /* Returns non-NULL if there is already an entry in DATA (which is
3549 really a `varray_type') indicating that an object with the same
3550 type of BINFO is already at the BINFO_OFFSET for BINFO. */
3553 dfs_search_base_offsets (binfo, data)
3557 if (is_empty_class (BINFO_TYPE (binfo)))
3559 varray_type v = (varray_type) data;
3560 /* Find the offset for this BINFO. */
3561 unsigned HOST_WIDE_INT offset = tree_low_cst (BINFO_OFFSET (binfo), 1);
3564 /* If we haven't yet encountered any objects at offsets that
3565 big, then there's no conflict. */
3566 if (VARRAY_SIZE (v) <= offset)
3568 /* Otherwise, go through the objects already allocated at this
3570 for (t = VARRAY_TREE (v, offset); t; t = TREE_CHAIN (t))
3571 if (same_type_p (TREE_VALUE (t), BINFO_TYPE (binfo)))
3578 /* Returns non-zero if there's a conflict between BINFO and a base
3579 already mentioned in BASE_OFFSETS if BINFO is placed at its current
3583 layout_conflict_p (binfo, base_offsets)
3585 varray_type base_offsets;
3587 return dfs_walk (binfo, dfs_search_base_offsets, dfs_skip_vbases,
3588 base_offsets) != NULL_TREE;
3591 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3592 non-static data member of the type indicated by RLI. BINFO is the
3593 binfo corresponding to the base subobject, or, if this is a
3594 non-static data-member, a dummy BINFO for the type of the data
3595 member. BINFO may be NULL if checks to see if the field overlaps
3596 an existing field with the same type are not required. V maps
3597 offsets to types already located at those offsets. This function
3598 determines the position of the DECL. */
3601 layout_nonempty_base_or_field (rli, decl, binfo, v)
3602 record_layout_info rli;
3607 /* Try to place the field. It may take more than one try if we have
3608 a hard time placing the field without putting two objects of the
3609 same type at the same address. */
3613 struct record_layout_info old_rli = *rli;
3615 /* Place this field. */
3616 place_field (rli, decl);
3618 /* Now that we know where it wil be placed, update its
3620 offset = byte_position (decl);
3622 propagate_binfo_offsets (binfo,
3623 convert (ssizetype, offset));
3625 /* We have to check to see whether or not there is already
3626 something of the same type at the offset we're about to use.
3630 struct T : public S { int i; };
3631 struct U : public S, public T {};
3633 Here, we put S at offset zero in U. Then, we can't put T at
3634 offset zero -- its S component would be at the same address
3635 as the S we already allocated. So, we have to skip ahead.
3636 Since all data members, including those whose type is an
3637 empty class, have non-zero size, any overlap can happen only
3638 with a direct or indirect base-class -- it can't happen with
3640 if (binfo && flag_new_abi && layout_conflict_p (binfo, v))
3642 /* Undo the propogate_binfo_offsets call. */
3643 offset = size_diffop (size_zero_node, offset);
3644 propagate_binfo_offsets (binfo, convert (ssizetype, offset));
3646 /* Strip off the size allocated to this field. That puts us
3647 at the first place we could have put the field with
3648 proper alignment. */
3651 /* Bump up by the alignment required for the type, without
3652 virtual base classes. */
3654 = size_binop (PLUS_EXPR, rli->bitpos,
3655 bitsize_int (CLASSTYPE_ALIGN (BINFO_TYPE (binfo))));
3656 normalize_rli (rli);
3659 /* There was no conflict. We're done laying out this field. */
3664 /* Layout the empty base BINFO. EOC indicates the byte currently just
3665 past the end of the class, and should be correctly aligned for a
3666 class of the type indicated by BINFO; BINFO_OFFSETS gives the
3667 offsets of the other bases allocated so far. */
3670 layout_empty_base (binfo, eoc, binfo_offsets)
3673 varray_type binfo_offsets;
3676 tree basetype = BINFO_TYPE (binfo);
3678 /* This routine should only be used for empty classes. */
3679 my_friendly_assert (is_empty_class (basetype), 20000321);
3680 alignment = ssize_int (CLASSTYPE_ALIGN (basetype));
3682 /* This is an empty base class. We first try to put it at offset
3684 if (layout_conflict_p (binfo, binfo_offsets))
3686 /* That didn't work. Now, we move forward from the next
3687 available spot in the class. */
3688 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3691 if (!layout_conflict_p (binfo, binfo_offsets))
3692 /* We finally found a spot where there's no overlap. */
3695 /* There's overlap here, too. Bump along to the next spot. */
3696 propagate_binfo_offsets (binfo, alignment);
3701 /* Build a FIELD_DECL for the base given by BINFO in the class
3702 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
3703 *BASE_ALIGN is a running maximum of the alignments of any base
3707 build_base_field (rli, binfo, empty_p, base_align, v)
3708 record_layout_info rli;
3711 unsigned int *base_align;
3714 tree basetype = BINFO_TYPE (binfo);
3717 if (!COMPLETE_TYPE_P (basetype))
3718 /* This error is now reported in xref_tag, thus giving better
3719 location information. */
3722 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
3723 DECL_ARTIFICIAL (decl) = 1;
3724 DECL_FIELD_CONTEXT (decl) = rli->t;
3725 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3726 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3727 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3731 /* Brain damage for backwards compatibility. For no good
3732 reason, the old basetype layout made every base have at least
3733 as large as the alignment for the bases up to that point,
3734 gratuitously wasting space. So we do the same thing here. */
3735 *base_align = MAX (*base_align, DECL_ALIGN (decl));
3737 = size_binop (MAX_EXPR, DECL_SIZE (decl), bitsize_int (*base_align));
3738 DECL_SIZE_UNIT (decl)
3739 = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
3740 size_int (*base_align / BITS_PER_UNIT));
3743 if (!integer_zerop (DECL_SIZE (decl)))
3745 /* The containing class is non-empty because it has a non-empty
3749 /* Try to place the field. It may take more than one try if we
3750 have a hard time placing the field without putting two
3751 objects of the same type at the same address. */
3752 layout_nonempty_base_or_field (rli, decl, binfo, *v);
3756 unsigned HOST_WIDE_INT eoc;
3758 /* On some platforms (ARM), even empty classes will not be
3760 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
3761 eoc = CEIL (eoc, DECL_ALIGN (decl)) * DECL_ALIGN (decl);
3762 layout_empty_base (binfo, size_int (eoc), *v);
3765 /* Check for inaccessible base classes. If the same base class
3766 appears more than once in the hierarchy, but isn't virtual, then
3768 if (get_base_distance (basetype, rli->t, 0, NULL) == -2)
3769 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
3772 /* Record the offsets of BINFO and its base subobjects. */
3773 record_base_offsets (binfo, v);
3776 /* Layout all of the non-virtual base classes. Returns a map from
3777 offsets to types present at those offsets. */
3780 build_base_fields (rli, empty_p)
3781 record_layout_info rli;
3784 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3787 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3790 unsigned int base_align = 0;
3792 /* Create the table mapping offsets to empty base classes. */
3793 VARRAY_TREE_INIT (v, 32, "v");
3795 /* Under the new ABI, the primary base class is always allocated
3797 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
3798 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
3799 empty_p, &base_align, &v);
3801 /* Now allocate the rest of the bases. */
3802 for (i = 0; i < n_baseclasses; ++i)
3806 /* Under the new ABI, the primary base was already allocated
3807 above, so we don't need to allocate it again here. */
3808 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
3811 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3813 /* A primary virtual base class is allocated just like any other
3814 base class, but a non-primary virtual base is allocated
3815 later, in layout_virtual_bases. */
3816 if (TREE_VIA_VIRTUAL (base_binfo)
3817 && !BINFO_PRIMARY_MARKED_P (base_binfo))
3820 build_base_field (rli, base_binfo, empty_p, &base_align, &v);
3826 /* Go through the TYPE_METHODS of T issuing any appropriate
3827 diagnostics, figuring out which methods override which other
3828 methods, and so forth. */
3835 int seen_one_arg_array_delete_p = 0;
3837 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3839 GNU_xref_member (current_class_name, x);
3841 /* If this was an evil function, don't keep it in class. */
3842 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3845 check_for_override (x, t);
3846 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3847 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3849 /* The name of the field is the original field name
3850 Save this in auxiliary field for later overloading. */
3851 if (DECL_VINDEX (x))
3853 TYPE_POLYMORPHIC_P (t) = 1;
3854 if (DECL_PURE_VIRTUAL_P (x))
3855 CLASSTYPE_PURE_VIRTUALS (t)
3856 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3859 if (DECL_ARRAY_DELETE_OPERATOR_P (x))
3863 /* When dynamically allocating an array of this type, we
3864 need a "cookie" to record how many elements we allocated,
3865 even if the array elements have no non-trivial
3866 destructor, if the usual array deallocation function
3867 takes a second argument of type size_t. The standard (in
3868 [class.free]) requires that the second argument be set
3870 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
3871 /* This is overly conservative, but we must maintain this
3872 behavior for backwards compatibility. */
3873 if (!flag_new_abi && second_parm != void_list_node)
3874 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
3875 /* Under the new ABI, we choose only those function that are
3876 explicitly declared as `operator delete[] (void *,
3878 else if (flag_new_abi
3879 && !seen_one_arg_array_delete_p
3881 && TREE_CHAIN (second_parm) == void_list_node
3882 && same_type_p (TREE_VALUE (second_parm), sizetype))
3883 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
3884 /* If there's no second parameter, then this is the usual
3885 deallocation function. */
3886 else if (second_parm == void_list_node)
3887 seen_one_arg_array_delete_p = 1;
3892 /* FN is a constructor or destructor. Clone the declaration to create
3893 a specialized in-charge or not-in-charge version, as indicated by
3897 build_clone (fn, name)
3904 /* Copy the function. */
3905 clone = copy_decl (fn);
3906 /* Remember where this function came from. */
3907 DECL_CLONED_FUNCTION (clone) = fn;
3908 /* Reset the function name. */
3909 DECL_NAME (clone) = name;
3910 DECL_ASSEMBLER_NAME (clone) = DECL_NAME (clone);
3911 /* There's no pending inline data for this function. */
3912 DECL_PENDING_INLINE_INFO (clone) = NULL;
3913 DECL_PENDING_INLINE_P (clone) = 0;
3914 /* And it hasn't yet been deferred. */
3915 DECL_DEFERRED_FN (clone) = 0;
3917 /* The base-class destructor is not virtual. */
3918 if (name == base_dtor_identifier)
3920 DECL_VIRTUAL_P (clone) = 0;
3921 if (TREE_CODE (clone) != TEMPLATE_DECL)
3922 DECL_VINDEX (clone) = NULL_TREE;
3925 /* If there was an in-charge parameter, drop it from the function
3927 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3933 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3934 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3935 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3936 /* Skip the `this' parameter. */
3937 parmtypes = TREE_CHAIN (parmtypes);
3938 /* Skip the in-charge parameter. */
3939 parmtypes = TREE_CHAIN (parmtypes);
3941 = build_cplus_method_type (basetype,
3942 TREE_TYPE (TREE_TYPE (clone)),
3945 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3949 /* Copy the function parameters. But, DECL_ARGUMENTS aren't
3950 function parameters; instead, those are the template parameters. */
3951 if (TREE_CODE (clone) != TEMPLATE_DECL)
3953 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
3954 /* Remove the in-charge parameter. */
3955 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3957 TREE_CHAIN (DECL_ARGUMENTS (clone))
3958 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3959 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
3961 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
3963 DECL_CONTEXT (parms) = clone;
3964 copy_lang_decl (parms);
3968 /* Mangle the function name. */
3969 set_mangled_name_for_decl (clone);
3971 /* Create the RTL for this function. */
3972 DECL_RTL (clone) = NULL_RTX;
3973 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
3975 /* Make it easy to find the CLONE given the FN. */
3976 TREE_CHAIN (clone) = TREE_CHAIN (fn);
3977 TREE_CHAIN (fn) = clone;
3979 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
3980 if (TREE_CODE (clone) == TEMPLATE_DECL)
3984 DECL_TEMPLATE_RESULT (clone)
3985 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
3986 result = DECL_TEMPLATE_RESULT (clone);
3987 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
3988 DECL_TI_TEMPLATE (result) = clone;
3990 else if (DECL_DEFERRED_FN (fn))
3996 /* Produce declarations for all appropriate clones of FN. If
3997 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
3998 CLASTYPE_METHOD_VEC as well. */
4001 clone_function_decl (fn, update_method_vec_p)
4003 int update_method_vec_p;
4007 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4009 /* For each constructor, we need two variants: an in-charge version
4010 and a not-in-charge version. */
4011 clone = build_clone (fn, complete_ctor_identifier);
4012 if (update_method_vec_p)
4013 add_method (DECL_CONTEXT (clone), NULL, clone);
4014 clone = build_clone (fn, base_ctor_identifier);
4015 if (update_method_vec_p)
4016 add_method (DECL_CONTEXT (clone), NULL, clone);
4020 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4022 /* For each destructor, we need two variants: an in-charge
4023 version, a not-in-charge version, and an in-charge deleting
4024 version. We clone the deleting version first because that
4025 means it will go second on the TYPE_METHODS list -- and that
4026 corresponds to the correct layout order in the virtual
4028 clone = build_clone (fn, deleting_dtor_identifier);
4029 if (update_method_vec_p)
4030 add_method (DECL_CONTEXT (clone), NULL, clone);
4031 clone = build_clone (fn, complete_dtor_identifier);
4032 if (update_method_vec_p)
4033 add_method (DECL_CONTEXT (clone), NULL, clone);
4034 clone = build_clone (fn, base_dtor_identifier);
4035 if (update_method_vec_p)
4036 add_method (DECL_CONTEXT (clone), NULL, clone);
4040 /* For each of the constructors and destructors in T, create an
4041 in-charge and not-in-charge variant. */
4044 clone_constructors_and_destructors (t)
4049 /* We only clone constructors and destructors under the new ABI. */
4053 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4055 if (!CLASSTYPE_METHOD_VEC (t))
4058 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4059 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4060 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4061 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4064 /* Remove all zero-width bit-fields from T. */
4067 remove_zero_width_bit_fields (t)
4072 fieldsp = &TYPE_FIELDS (t);
4075 if (TREE_CODE (*fieldsp) == FIELD_DECL
4076 && DECL_C_BIT_FIELD (*fieldsp)
4077 && DECL_INITIAL (*fieldsp))
4078 *fieldsp = TREE_CHAIN (*fieldsp);
4080 fieldsp = &TREE_CHAIN (*fieldsp);
4084 /* Check the validity of the bases and members declared in T. Add any
4085 implicitly-generated functions (like copy-constructors and
4086 assignment operators). Compute various flag bits (like
4087 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4088 level: i.e., independently of the ABI in use. */
4091 check_bases_and_members (t, empty_p)
4095 /* Nonzero if we are not allowed to generate a default constructor
4097 int cant_have_default_ctor;
4098 /* Nonzero if the implicitly generated copy constructor should take
4099 a non-const reference argument. */
4100 int cant_have_const_ctor;
4101 /* Nonzero if the the implicitly generated assignment operator
4102 should take a non-const reference argument. */
4103 int no_const_asn_ref;
4106 /* By default, we use const reference arguments and generate default
4108 cant_have_default_ctor = 0;
4109 cant_have_const_ctor = 0;
4110 no_const_asn_ref = 0;
4112 /* Assume that the class is nearly empty; we'll clear this flag if
4113 it turns out not to be nearly empty. */
4114 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4116 /* Check all the base-classes. */
4117 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4120 /* Check all the data member declarations. */
4121 check_field_decls (t, &access_decls, empty_p,
4122 &cant_have_default_ctor,
4123 &cant_have_const_ctor,
4126 /* Check all the method declarations. */
4129 /* A nearly-empty class has to be vptr-containing; a nearly empty
4130 class contains just a vptr. */
4131 if (!TYPE_CONTAINS_VPTR_P (t))
4132 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4134 /* Do some bookkeeping that will guide the generation of implicitly
4135 declared member functions. */
4136 TYPE_HAS_COMPLEX_INIT_REF (t)
4137 |= (TYPE_HAS_INIT_REF (t)
4138 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4139 || TYPE_POLYMORPHIC_P (t));
4140 TYPE_NEEDS_CONSTRUCTING (t)
4141 |= (TYPE_HAS_CONSTRUCTOR (t)
4142 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4143 || TYPE_POLYMORPHIC_P (t));
4144 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4145 || TYPE_POLYMORPHIC_P (t));
4146 CLASSTYPE_NON_POD_P (t)
4147 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4148 || TYPE_HAS_ASSIGN_REF (t));
4149 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4150 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4151 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4153 /* Synthesize any needed methods. Note that methods will be synthesized
4154 for anonymous unions; grok_x_components undoes that. */
4155 add_implicitly_declared_members (t, cant_have_default_ctor,
4156 cant_have_const_ctor,
4159 /* Create the in-charge and not-in-charge variants of constructors
4161 clone_constructors_and_destructors (t);
4163 /* Process the using-declarations. */
4164 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4165 handle_using_decl (TREE_VALUE (access_decls), t);
4167 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4168 finish_struct_methods (t);
4171 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4172 accordingly. If a new vfield was created (because T doesn't have a
4173 primary base class), then the newly created field is returned. It
4174 is not added to the TYPE_FIELDS list; it is the caller's
4175 responsibility to do that. */
4178 create_vtable_ptr (t, empty_p, vfuns_p,
4179 new_virtuals_p, overridden_virtuals_p)
4183 tree *new_virtuals_p;
4184 tree *overridden_virtuals_p;
4188 /* Loop over the virtual functions, adding them to our various
4190 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4191 if (DECL_VINDEX (fn)
4192 && !(flag_new_abi && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)))
4193 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4196 /* If we couldn't find an appropriate base class, create a new field
4197 here. Even if there weren't any new virtual functions, we might need a
4198 new virtual function table if we're supposed to include vptrs in
4199 all classes that need them. */
4200 if (!TYPE_VFIELD (t)
4202 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())))
4204 /* We build this decl with vtbl_ptr_type_node, which is a
4205 `vtable_entry_type*'. It might seem more precise to use
4206 `vtable_entry_type (*)[N]' where N is the number of firtual
4207 functions. However, that would require the vtable pointer in
4208 base classes to have a different type than the vtable pointer
4209 in derived classes. We could make that happen, but that
4210 still wouldn't solve all the problems. In particular, the
4211 type-based alias analysis code would decide that assignments
4212 to the base class vtable pointer can't alias assignments to
4213 the derived class vtable pointer, since they have different
4214 types. Thus, in an derived class destructor, where the base
4215 class constructor was inlined, we could generate bad code for
4216 setting up the vtable pointer.
4218 Therefore, we use one type for all vtable pointers. We still
4219 use a type-correct type; it's just doesn't indicate the array
4220 bounds. That's better than using `void*' or some such; it's
4221 cleaner, and it let's the alias analysis code know that these
4222 stores cannot alias stores to void*! */
4224 = build_vtbl_or_vbase_field (get_vfield_name (t),
4225 get_identifier (VFIELD_BASE),
4231 if (flag_new_abi && CLASSTYPE_N_BASECLASSES (t))
4232 /* If there were any baseclasses, they can't possibly be at
4233 offset zero any more, because that's where the vtable
4234 pointer is. So, converting to a base class is going to
4236 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4238 return TYPE_VFIELD (t);
4244 /* Fixup the inline function given by INFO now that the class is
4248 fixup_pending_inline (info)
4249 struct pending_inline *info;
4254 tree fn = info->fndecl;
4256 args = DECL_ARGUMENTS (fn);
4259 DECL_CONTEXT (args) = fn;
4260 args = TREE_CHAIN (args);
4265 /* Fixup the inline methods and friends in TYPE now that TYPE is
4269 fixup_inline_methods (type)
4272 tree method = TYPE_METHODS (type);
4274 if (method && TREE_CODE (method) == TREE_VEC)
4276 if (TREE_VEC_ELT (method, 1))
4277 method = TREE_VEC_ELT (method, 1);
4278 else if (TREE_VEC_ELT (method, 0))
4279 method = TREE_VEC_ELT (method, 0);
4281 method = TREE_VEC_ELT (method, 2);
4284 /* Do inline member functions. */
4285 for (; method; method = TREE_CHAIN (method))
4286 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
4289 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4291 method = TREE_CHAIN (method))
4292 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
4293 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4296 /* Called from propagate_binfo_offsets via dfs_walk. */
4299 dfs_propagate_binfo_offsets (binfo, data)
4303 tree offset = (tree) data;
4305 /* Update the BINFO_OFFSET for this base. Allow for the case where it
4306 might be negative. */
4307 BINFO_OFFSET (binfo)
4308 = convert (sizetype, size_binop (PLUS_EXPR,
4309 convert (ssizetype, BINFO_OFFSET (binfo)),
4311 SET_BINFO_MARKED (binfo);
4316 /* Add OFFSET to all base types of BINFO which is a base in the
4317 hierarchy dominated by T.
4319 OFFSET, which is a type offset, is number of bytes.
4321 Note that we don't have to worry about having two paths to the
4322 same base type, since this type owns its association list. */
4325 propagate_binfo_offsets (binfo, offset)
4330 dfs_propagate_binfo_offsets,
4331 dfs_skip_nonprimary_vbases_unmarkedp,
4335 dfs_skip_nonprimary_vbases_markedp,
4339 /* Called via dfs_walk from layout_virtual bases. */
4342 dfs_set_offset_for_shared_vbases (binfo, data)
4346 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
4348 /* Update the shared copy. */
4351 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4352 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
4358 /* Called via dfs_walk from layout_virtual bases. */
4361 dfs_set_offset_for_unshared_vbases (binfo, data)
4365 /* If this is a virtual base, make sure it has the same offset as
4366 the shared copy. If it's a primary base, then we know it's
4368 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4370 tree t = (tree) data;
4374 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
4375 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4376 propagate_binfo_offsets (binfo, offset);
4382 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4383 TYPE_ALIGN and TYPE_SIZE for T. BASE_OFFSETS is a varray mapping
4384 offsets to the types at those offsets. */
4387 layout_virtual_bases (t, base_offsets)
4389 varray_type *base_offsets;
4392 unsigned HOST_WIDE_INT dsize;
4393 unsigned HOST_WIDE_INT eoc;
4395 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4398 #ifdef STRUCTURE_SIZE_BOUNDARY
4399 /* Packed structures don't need to have minimum size. */
4400 if (! TYPE_PACKED (t))
4401 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4404 /* DSIZE is the size of the class without the virtual bases. */
4405 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4407 /* Make every class have alignment of at least one. */
4408 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4410 /* Go through the virtual bases, allocating space for each virtual
4411 base that is not already a primary base class. Under the new
4412 ABI, these are allocated according to a depth-first left-to-right
4413 postorder traversal; in the new ABI, inheritance graph order is
4415 for (vbases = (flag_new_abi
4417 : CLASSTYPE_VBASECLASSES (t));
4419 vbases = TREE_CHAIN (vbases))
4423 if (!TREE_VIA_VIRTUAL (vbases))
4427 vbase = BINFO_FOR_VBASE (BINFO_TYPE (vbases), t);
4431 if (!BINFO_VBASE_PRIMARY_P (vbase))
4433 /* This virtual base is not a primary base of any class in the
4434 hierarchy, so we have to add space for it. */
4436 unsigned int desired_align;
4438 basetype = BINFO_TYPE (vbase);
4441 desired_align = CLASSTYPE_ALIGN (basetype);
4443 /* Under the old ABI, virtual bases were aligned as for the
4444 entire base object (including its virtual bases). That's
4445 wasteful, in general. */
4446 desired_align = TYPE_ALIGN (basetype);
4447 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4449 /* Add padding so that we can put the virtual base class at an
4450 appropriately aligned offset. */
4451 dsize = CEIL (dsize, desired_align) * desired_align;
4453 /* Under the new ABI, we try to squish empty virtual bases in
4454 just like ordinary empty bases. */
4455 if (flag_new_abi && is_empty_class (basetype))
4456 layout_empty_base (vbase,
4457 size_int (CEIL (dsize, BITS_PER_UNIT)),
4461 /* And compute the offset of the virtual base. */
4462 propagate_binfo_offsets (vbase,
4463 ssize_int (CEIL (dsize, BITS_PER_UNIT)));
4464 /* Every virtual baseclass takes a least a UNIT, so that
4465 we can take it's address and get something different
4467 dsize += MAX (BITS_PER_UNIT,
4468 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4471 /* Keep track of the offsets assigned to this virtual base. */
4472 record_base_offsets (vbase, base_offsets);
4476 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4477 BINFO_OFFSET set correctly. Those we just allocated certainly
4478 will. The others are primary baseclasses; we walk the hierarchy
4479 to find the primary copies and update the shared copy. */
4480 dfs_walk (TYPE_BINFO (t),
4481 dfs_set_offset_for_shared_vbases,
4482 dfs_unmarked_real_bases_queue_p,
4485 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4486 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4487 bases and their direct and indirect bases. The ambiguity checks
4488 in get_base_distance depend on the BINFO_OFFSETs being set
4490 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4491 for (vbases = CLASSTYPE_VBASECLASSES (t);
4493 vbases = TREE_CHAIN (vbases))
4494 dfs_walk (vbases, dfs_set_offset_for_unshared_vbases, NULL, t);
4496 /* If we had empty base classes that protruded beyond the end of the
4497 class, we didn't update DSIZE above; we were hoping to overlay
4498 multiple such bases at the same location. */
4499 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4500 if (eoc * BITS_PER_UNIT > dsize)
4501 dsize = (eoc + 1) * BITS_PER_UNIT;
4503 /* Now, make sure that the total size of the type is a multiple of
4505 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4506 TYPE_SIZE (t) = bitsize_int (dsize);
4507 TYPE_SIZE_UNIT (t) = convert (sizetype,
4508 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4509 bitsize_unit_node));
4511 /* Check for ambiguous virtual bases. */
4513 for (vbases = CLASSTYPE_VBASECLASSES (t);
4515 vbases = TREE_CHAIN (vbases))
4517 tree basetype = BINFO_TYPE (vbases);
4518 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4519 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4524 /* Returns the offset of the byte just past the end of the base class
4525 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4526 only non-virtual bases are included. */
4528 static unsigned HOST_WIDE_INT
4529 end_of_class (t, include_virtuals_p)
4531 int include_virtuals_p;
4533 unsigned HOST_WIDE_INT result = 0;
4536 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4540 unsigned HOST_WIDE_INT end_of_base;
4542 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4544 if (!include_virtuals_p
4545 && TREE_VIA_VIRTUAL (base_binfo)
4546 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4549 offset = size_binop (PLUS_EXPR,
4550 BINFO_OFFSET (base_binfo),
4551 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
4552 end_of_base = tree_low_cst (offset, /*pos=*/1);
4553 if (end_of_base > result)
4554 result = end_of_base;
4560 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4561 BINFO_OFFSETs for all of the base-classes. Position the vtable
4565 layout_class_type (t, empty_p, vfuns_p,
4566 new_virtuals_p, overridden_virtuals_p)
4570 tree *new_virtuals_p;
4571 tree *overridden_virtuals_p;
4573 tree non_static_data_members;
4576 record_layout_info rli;
4578 unsigned HOST_WIDE_INT eoc;
4580 /* Keep track of the first non-static data member. */
4581 non_static_data_members = TYPE_FIELDS (t);
4583 /* Start laying out the record. */
4584 rli = start_record_layout (t);
4586 /* If possible, we reuse the virtual function table pointer from one
4587 of our base classes. */
4588 determine_primary_base (t, vfuns_p);
4590 /* Create a pointer to our virtual function table. */
4591 vptr = create_vtable_ptr (t, empty_p, vfuns_p,
4592 new_virtuals_p, overridden_virtuals_p);
4594 /* Under the new ABI, the vptr is always the first thing in the
4596 if (flag_new_abi && vptr)
4598 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4599 place_field (rli, vptr);
4602 /* Add pointers to all of our virtual base-classes. */
4603 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (rli, empty_p),
4605 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4606 v = build_base_fields (rli, empty_p);
4608 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4609 we have to save this before we start modifying
4610 TYPE_NONCOPIED_PARTS. */
4611 fixup_inline_methods (t);
4613 /* Layout the non-static data members. */
4614 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4620 /* We still pass things that aren't non-static data members to
4621 the back-end, in case it wants to do something with them. */
4622 if (TREE_CODE (field) != FIELD_DECL)
4624 place_field (rli, field);
4628 type = TREE_TYPE (field);
4630 /* If this field is a bit-field whose width is greater than its
4631 type, then there are some special rules for allocating it
4632 under the new ABI. Under the old ABI, there were no special
4633 rules, but the back-end can't handle bitfields longer than a
4634 `long long', so we use the same mechanism. */
4635 if (DECL_C_BIT_FIELD (field)
4637 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4639 && 0 < compare_tree_int (DECL_SIZE (field),
4641 (long_long_unsigned_type_node)))))
4643 integer_type_kind itk;
4646 /* We must allocate the bits as if suitably aligned for the
4647 longest integer type that fits in this many bits. type
4648 of the field. Then, we are supposed to use the left over
4649 bits as additional padding. */
4650 for (itk = itk_char; itk != itk_none; ++itk)
4651 if (INT_CST_LT (DECL_SIZE (field),
4652 TYPE_SIZE (integer_types[itk])))
4655 /* ITK now indicates a type that is too large for the
4656 field. We have to back up by one to find the largest
4658 integer_type = integer_types[itk - 1];
4659 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4660 TYPE_SIZE (integer_type));
4661 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4662 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4665 padding = NULL_TREE;
4667 /* Create a dummy BINFO corresponding to this field. */
4668 binfo = make_binfo (size_zero_node, type, NULL_TREE, NULL_TREE);
4669 unshare_base_binfos (binfo);
4670 layout_nonempty_base_or_field (rli, field, binfo, v);
4672 /* If we needed additional padding after this field, add it
4678 padding_field = build_decl (FIELD_DECL,
4681 DECL_BIT_FIELD (padding_field) = 1;
4682 DECL_SIZE (padding_field) = padding;
4683 DECL_ALIGN (padding_field) = 1;
4684 layout_nonempty_base_or_field (rli, padding_field, NULL_TREE, v);
4688 /* It might be the case that we grew the class to allocate a
4689 zero-sized base class. That won't be reflected in RLI, yet,
4690 because we are willing to overlay multiple bases at the same
4691 offset. However, now we need to make sure that RLI is big enough
4692 to reflect the entire class. */
4693 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4694 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4695 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
4697 /* We don't handle zero-sized base classes specially under the
4698 old ABI, so if we get here, we had better be operating under
4699 the new ABI rules. */
4700 my_friendly_assert (flag_new_abi, 20000321);
4701 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc + 1));
4702 rli->bitpos = bitsize_zero_node;
4705 /* We make all structures have at least one element, so that they
4706 have non-zero size. In the new ABI, the class may be empty even
4707 if it has basetypes. Therefore, we add the fake field after all
4708 the other fields; if there are already FIELD_DECLs on the list,
4709 their offsets will not be disturbed. */
4714 padding = build_lang_decl (FIELD_DECL, NULL_TREE, char_type_node);
4715 place_field (rli, padding);
4716 TYPE_NONCOPIED_PARTS (t)
4717 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
4718 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4721 /* Under the old ABI, the vptr comes at the very end of the
4723 if (!flag_new_abi && vptr)
4725 place_field (rli, vptr);
4726 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), vptr);
4729 /* Let the back-end lay out the type. Note that at this point we
4730 have only included non-virtual base-classes; we will lay out the
4731 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4732 this call are not necessarily correct; they are just the size and
4733 alignment when no virtual base clases are used. */
4734 finish_record_layout (rli);
4736 /* Delete all zero-width bit-fields from the list of fields. Now
4737 that the type is laid out they are no longer important. */
4738 remove_zero_width_bit_fields (t);
4740 /* Remember the size and alignment of the class before adding
4741 the virtual bases. */
4742 if (*empty_p && flag_new_abi)
4744 CLASSTYPE_SIZE (t) = bitsize_zero_node;
4745 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
4747 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
4748 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
4750 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4751 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
4755 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4756 CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
4759 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4761 /* Set the TYPE_DECL for this type to contain the right
4762 value for DECL_OFFSET, so that we can use it as part
4763 of a COMPONENT_REF for multiple inheritance. */
4764 layout_decl (TYPE_MAIN_DECL (t), 0);
4766 /* Now fix up any virtual base class types that we left lying
4767 around. We must get these done before we try to lay out the
4768 virtual function table. As a side-effect, this will remove the
4769 base subobject fields. */
4770 layout_virtual_bases (t, &v);
4776 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
4777 (or C++ class declaration).
4779 For C++, we must handle the building of derived classes.
4780 Also, C++ allows static class members. The way that this is
4781 handled is to keep the field name where it is (as the DECL_NAME
4782 of the field), and place the overloaded decl in the bit position
4783 of the field. layout_record and layout_union will know about this.
4785 More C++ hair: inline functions have text in their
4786 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
4787 meaningful tree structure. After the struct has been laid out, set
4788 things up so that this can happen.
4790 And still more: virtual functions. In the case of single inheritance,
4791 when a new virtual function is seen which redefines a virtual function
4792 from the base class, the new virtual function is placed into
4793 the virtual function table at exactly the same address that
4794 it had in the base class. When this is extended to multiple
4795 inheritance, the same thing happens, except that multiple virtual
4796 function tables must be maintained. The first virtual function
4797 table is treated in exactly the same way as in the case of single
4798 inheritance. Additional virtual function tables have different
4799 DELTAs, which tell how to adjust `this' to point to the right thing.
4801 ATTRIBUTES is the set of decl attributes to be applied, if any. */
4809 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
4810 a FUNCTION_DECL. Each of these functions is a virtual function
4811 declared in T that does not override any virtual function from a
4813 tree new_virtuals = NULL_TREE;
4814 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
4815 except that each declaration here overrides the declaration from
4817 tree overridden_virtuals = NULL_TREE;
4822 if (COMPLETE_TYPE_P (t))
4824 if (IS_AGGR_TYPE (t))
4825 cp_error ("redefinition of `%#T'", t);
4827 my_friendly_abort (172);
4832 GNU_xref_decl (current_function_decl, t);
4834 /* If this type was previously laid out as a forward reference,
4835 make sure we lay it out again. */
4836 TYPE_SIZE (t) = NULL_TREE;
4837 CLASSTYPE_GOT_SEMICOLON (t) = 0;
4838 CLASSTYPE_VFIELD_PARENT (t) = -1;
4840 CLASSTYPE_RTTI (t) = NULL_TREE;
4842 /* Do end-of-class semantic processing: checking the validity of the
4843 bases and members and add implicitly generated methods. */
4844 check_bases_and_members (t, &empty);
4846 /* Layout the class itself. */
4847 layout_class_type (t, &empty, &vfuns,
4848 &new_virtuals, &overridden_virtuals);
4850 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4851 might need to know it for setting up the offsets in the vtable
4852 (or in thunks) below. */
4853 vfield = TYPE_VFIELD (t);
4854 if (vfield != NULL_TREE
4855 && DECL_FIELD_CONTEXT (vfield) != t)
4857 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4859 vfield = copy_decl (vfield);
4861 DECL_FIELD_CONTEXT (vfield) = t;
4862 DECL_FIELD_OFFSET (vfield)
4863 = size_binop (PLUS_EXPR,
4864 BINFO_OFFSET (binfo),
4865 DECL_FIELD_OFFSET (vfield));
4866 TYPE_VFIELD (t) = vfield;
4870 = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
4872 /* If necessary, create the primary vtable for this class. */
4874 || overridden_virtuals
4875 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
4877 new_virtuals = nreverse (new_virtuals);
4878 /* We must enter these virtuals into the table. */
4879 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4880 build_primary_vtable (NULL_TREE, t);
4881 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
4882 /* Here we know enough to change the type of our virtual
4883 function table, but we will wait until later this function. */
4884 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
4886 /* If this type has basetypes with constructors, then those
4887 constructors might clobber the virtual function table. But
4888 they don't if the derived class shares the exact vtable of the base
4891 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4893 /* If we didn't need a new vtable, see if we should copy one from
4895 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4897 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
4899 /* This class contributes nothing new to the virtual function
4900 table. However, it may have declared functions which
4901 went into the virtual function table "inherited" from the
4902 base class. If so, we grab a copy of those updated functions,
4903 and pretend they are ours. */
4905 /* See if we should steal the virtual info from base class. */
4906 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4907 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4908 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4909 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4910 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4911 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4914 if (TYPE_CONTAINS_VPTR_P (t))
4916 if (TYPE_BINFO_VTABLE (t))
4917 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
4919 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4920 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
4923 CLASSTYPE_VSIZE (t) = vfuns;
4924 /* Entries for virtual functions defined in the primary base are
4925 followed by entries for new functions unique to this class. */
4926 TYPE_BINFO_VIRTUALS (t)
4927 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
4928 /* Finally, add entries for functions that override virtuals
4929 from non-primary bases. */
4930 TYPE_BINFO_VIRTUALS (t)
4931 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
4934 /* If we created a new vtbl pointer for this class, add it to the
4936 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4937 CLASSTYPE_VFIELDS (t)
4938 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
4940 finish_struct_bits (t);
4942 /* Complete the rtl for any static member objects of the type we're
4944 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
4946 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4947 && TREE_TYPE (x) == t)
4949 DECL_MODE (x) = TYPE_MODE (t);
4950 make_decl_rtl (x, NULL, 0);
4954 /* Done with FIELDS...now decide whether to sort these for
4955 faster lookups later.
4957 The C front-end only does this when n_fields > 15. We use
4958 a smaller number because most searches fail (succeeding
4959 ultimately as the search bores through the inheritance
4960 hierarchy), and we want this failure to occur quickly. */
4962 n_fields = count_fields (TYPE_FIELDS (t));
4965 tree field_vec = make_tree_vec (n_fields);
4966 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
4967 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
4968 (int (*)(const void *, const void *))field_decl_cmp);
4969 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
4970 retrofit_lang_decl (TYPE_MAIN_DECL (t));
4971 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
4974 if (TYPE_HAS_CONSTRUCTOR (t))
4976 tree vfields = CLASSTYPE_VFIELDS (t);
4980 /* Mark the fact that constructor for T
4981 could affect anybody inheriting from T
4982 who wants to initialize vtables for VFIELDS's type. */
4983 if (VF_DERIVED_VALUE (vfields))
4984 TREE_ADDRESSABLE (vfields) = 1;
4985 vfields = TREE_CHAIN (vfields);
4989 /* Make the rtl for any new vtables we have created, and unmark
4990 the base types we marked. */
4993 if (TYPE_VFIELD (t))
4995 /* In addition to this one, all the other vfields should be listed. */
4996 /* Before that can be done, we have to have FIELD_DECLs for them, and
4997 a place to find them. */
4998 TYPE_NONCOPIED_PARTS (t)
4999 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5000 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5002 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5003 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5004 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5008 hack_incomplete_structures (t);
5010 if (warn_overloaded_virtual)
5013 maybe_suppress_debug_info (t);
5015 /* Finish debugging output for this type. */
5016 rest_of_type_compilation (t, toplevel_bindings_p ());
5019 /* When T was built up, the member declarations were added in reverse
5020 order. Rearrange them to declaration order. */
5023 unreverse_member_declarations (t)
5030 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5031 reverse order. Put them in declaration order now. */
5032 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5033 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5035 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5036 reverse order, so we can't just use nreverse. */
5038 for (x = TYPE_FIELDS (t);
5039 x && TREE_CODE (x) != TYPE_DECL;
5042 next = TREE_CHAIN (x);
5043 TREE_CHAIN (x) = prev;
5048 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5050 TYPE_FIELDS (t) = prev;
5055 finish_struct (t, attributes)
5058 /* Now that we've got all the field declarations, reverse everything
5060 unreverse_member_declarations (t);
5062 cplus_decl_attributes (t, attributes, NULL_TREE);
5064 if (processing_template_decl)
5066 finish_struct_methods (t);
5067 TYPE_SIZE (t) = bitsize_zero_node;
5070 finish_struct_1 (t);
5072 TYPE_BEING_DEFINED (t) = 0;
5074 if (current_class_type)
5077 error ("trying to finish struct, but kicked out due to previous parse errors.");
5079 if (processing_template_decl)
5081 tree scope = current_scope ();
5082 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5083 add_tree (build_min (TAG_DEFN, t));
5089 /* Return the dynamic type of INSTANCE, if known.
5090 Used to determine whether the virtual function table is needed
5093 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5094 of our knowledge of its type. *NONNULL should be initialized
5095 before this function is called. */
5098 fixed_type_or_null (instance, nonnull)
5102 switch (TREE_CODE (instance))
5105 /* Check that we are not going through a cast of some sort. */
5106 if (TREE_TYPE (instance)
5107 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5108 instance = TREE_OPERAND (instance, 0);
5109 /* fall through... */
5111 /* This is a call to a constructor, hence it's never zero. */
5112 if (TREE_HAS_CONSTRUCTOR (instance))
5116 return TREE_TYPE (instance);
5121 /* This is a call to a constructor, hence it's never zero. */
5122 if (TREE_HAS_CONSTRUCTOR (instance))
5126 return TREE_TYPE (instance);
5128 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5135 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5136 /* Propagate nonnull. */
5137 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5138 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5139 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5144 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5149 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5152 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5156 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5157 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5161 return TREE_TYPE (TREE_TYPE (instance));
5163 /* fall through... */
5166 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5170 return TREE_TYPE (instance);
5174 if (instance == current_class_ptr
5175 && flag_this_is_variable <= 0)
5177 /* Normally, 'this' must be non-null. */
5178 if (flag_this_is_variable == 0)
5181 /* <0 means we're in a constructor and we know our type. */
5182 if (flag_this_is_variable < 0)
5183 return TREE_TYPE (TREE_TYPE (instance));
5185 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5186 /* Reference variables should be references to objects. */
5196 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5197 to the static type. We also handle the case where INSTANCE is really
5200 Used to determine whether the virtual function table is needed
5203 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5204 of our knowledge of its type. *NONNULL should be initialized
5205 before this function is called. */
5208 resolves_to_fixed_type_p (instance, nonnull)
5212 tree t = TREE_TYPE (instance);
5213 tree fixed = fixed_type_or_null (instance, nonnull);
5214 if (fixed == NULL_TREE)
5216 if (POINTER_TYPE_P (t))
5218 return same_type_ignoring_top_level_qualifiers_p (t, fixed);
5223 init_class_processing ()
5225 current_class_depth = 0;
5226 current_class_stack_size = 10;
5228 = (class_stack_node_t) xmalloc (current_class_stack_size
5229 * sizeof (struct class_stack_node));
5231 access_default_node = build_int_2 (0, 0);
5232 access_public_node = build_int_2 (ak_public, 0);
5233 access_protected_node = build_int_2 (ak_protected, 0);
5234 access_private_node = build_int_2 (ak_private, 0);
5235 access_default_virtual_node = build_int_2 (4, 0);
5236 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5237 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5238 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5241 /* Set current scope to NAME. CODE tells us if this is a
5242 STRUCT, UNION, or ENUM environment.
5244 NAME may end up being NULL_TREE if this is an anonymous or
5245 late-bound struct (as in "struct { ... } foo;") */
5247 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5248 appropriate values, found by looking up the type definition of
5251 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5252 which can be seen locally to the class. They are shadowed by
5253 any subsequent local declaration (including parameter names).
5255 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5256 which have static meaning (i.e., static members, static
5257 member functions, enum declarations, etc).
5259 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5260 which can be seen locally to the class (as in 1), but
5261 know that we are doing this for declaration purposes
5262 (i.e. friend foo::bar (int)).
5264 So that we may avoid calls to lookup_name, we cache the _TYPE
5265 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5267 For multiple inheritance, we perform a two-pass depth-first search
5268 of the type lattice. The first pass performs a pre-order search,
5269 marking types after the type has had its fields installed in
5270 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5271 unmarks the marked types. If a field or member function name
5272 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5273 that name becomes `error_mark_node'. */
5276 pushclass (type, modify)
5280 type = TYPE_MAIN_VARIANT (type);
5282 /* Make sure there is enough room for the new entry on the stack. */
5283 if (current_class_depth + 1 >= current_class_stack_size)
5285 current_class_stack_size *= 2;
5287 = (class_stack_node_t) xrealloc (current_class_stack,
5288 current_class_stack_size
5289 * sizeof (struct class_stack_node));
5292 /* Insert a new entry on the class stack. */
5293 current_class_stack[current_class_depth].name = current_class_name;
5294 current_class_stack[current_class_depth].type = current_class_type;
5295 current_class_stack[current_class_depth].access = current_access_specifier;
5296 current_class_stack[current_class_depth].names_used = 0;
5297 current_class_depth++;
5299 /* Now set up the new type. */
5300 current_class_name = TYPE_NAME (type);
5301 if (TREE_CODE (current_class_name) == TYPE_DECL)
5302 current_class_name = DECL_NAME (current_class_name);
5303 current_class_type = type;
5305 /* By default, things in classes are private, while things in
5306 structures or unions are public. */
5307 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5308 ? access_private_node
5309 : access_public_node);
5311 if (previous_class_type != NULL_TREE
5312 && (type != previous_class_type
5313 || !COMPLETE_TYPE_P (previous_class_type))
5314 && current_class_depth == 1)
5316 /* Forcibly remove any old class remnants. */
5317 invalidate_class_lookup_cache ();
5320 /* If we're about to enter a nested class, clear
5321 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5322 if (modify && current_class_depth > 1)
5323 clear_identifier_class_values ();
5328 if (CLASSTYPE_TEMPLATE_INFO (type))
5329 overload_template_name (type);
5334 if (type != previous_class_type || current_class_depth > 1)
5335 push_class_decls (type);
5340 /* We are re-entering the same class we just left, so we
5341 don't have to search the whole inheritance matrix to find
5342 all the decls to bind again. Instead, we install the
5343 cached class_shadowed list, and walk through it binding
5344 names and setting up IDENTIFIER_TYPE_VALUEs. */
5345 set_class_shadows (previous_class_values);
5346 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5348 tree id = TREE_PURPOSE (item);
5349 tree decl = TREE_TYPE (item);
5351 push_class_binding (id, decl);
5352 if (TREE_CODE (decl) == TYPE_DECL)
5353 set_identifier_type_value (id, TREE_TYPE (decl));
5355 unuse_fields (type);
5358 storetags (CLASSTYPE_TAGS (type));
5362 /* When we exit a toplevel class scope, we save the
5363 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5364 reenter the class. Here, we've entered some other class, so we
5365 must invalidate our cache. */
5368 invalidate_class_lookup_cache ()
5372 /* This code can be seen as a cache miss. When we've cached a
5373 class' scope's bindings and we can't use them, we need to reset
5374 them. This is it! */
5375 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5376 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5378 previous_class_type = NULL_TREE;
5381 /* Get out of the current class scope. If we were in a class scope
5382 previously, that is the one popped to. */
5388 /* Since poplevel_class does the popping of class decls nowadays,
5389 this really only frees the obstack used for these decls. */
5392 current_class_depth--;
5393 current_class_name = current_class_stack[current_class_depth].name;
5394 current_class_type = current_class_stack[current_class_depth].type;
5395 current_access_specifier = current_class_stack[current_class_depth].access;
5396 if (current_class_stack[current_class_depth].names_used)
5397 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5400 /* Returns 1 if current_class_type is either T or a nested type of T.
5401 We start looking from 1 because entry 0 is from global scope, and has
5405 currently_open_class (t)
5409 if (t == current_class_type)
5411 for (i = 1; i < current_class_depth; ++i)
5412 if (current_class_stack [i].type == t)
5417 /* If either current_class_type or one of its enclosing classes are derived
5418 from T, return the appropriate type. Used to determine how we found
5419 something via unqualified lookup. */
5422 currently_open_derived_class (t)
5427 if (DERIVED_FROM_P (t, current_class_type))
5428 return current_class_type;
5430 for (i = current_class_depth - 1; i > 0; --i)
5431 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5432 return current_class_stack[i].type;
5437 /* When entering a class scope, all enclosing class scopes' names with
5438 static meaning (static variables, static functions, types and enumerators)
5439 have to be visible. This recursive function calls pushclass for all
5440 enclosing class contexts until global or a local scope is reached.
5441 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5442 formal of the same name. */
5445 push_nested_class (type, modify)
5451 /* A namespace might be passed in error cases, like A::B:C. */
5452 if (type == NULL_TREE
5453 || type == error_mark_node
5454 || TREE_CODE (type) == NAMESPACE_DECL
5455 || ! IS_AGGR_TYPE (type)
5456 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5457 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
5460 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5462 if (context && CLASS_TYPE_P (context))
5463 push_nested_class (context, 2);
5464 pushclass (type, modify);
5467 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5472 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5475 if (context && CLASS_TYPE_P (context))
5476 pop_nested_class ();
5479 /* Set global variables CURRENT_LANG_NAME to appropriate value
5480 so that behavior of name-mangling machinery is correct. */
5483 push_lang_context (name)
5486 *current_lang_stack++ = current_lang_name;
5487 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5488 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5490 size_t old_size = VARRAY_SIZE (current_lang_base);
5492 VARRAY_GROW (current_lang_base, old_size + 10);
5493 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5496 if (name == lang_name_cplusplus)
5498 strict_prototype = strict_prototypes_lang_cplusplus;
5499 current_lang_name = name;
5501 else if (name == lang_name_java)
5503 strict_prototype = strict_prototypes_lang_cplusplus;
5504 current_lang_name = name;
5505 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5506 (See record_builtin_java_type in decl.c.) However, that causes
5507 incorrect debug entries if these types are actually used.
5508 So we re-enable debug output after extern "Java". */
5509 DECL_IGNORED_P (java_byte_type_node) = 0;
5510 DECL_IGNORED_P (java_short_type_node) = 0;
5511 DECL_IGNORED_P (java_int_type_node) = 0;
5512 DECL_IGNORED_P (java_long_type_node) = 0;
5513 DECL_IGNORED_P (java_float_type_node) = 0;
5514 DECL_IGNORED_P (java_double_type_node) = 0;
5515 DECL_IGNORED_P (java_char_type_node) = 0;
5516 DECL_IGNORED_P (java_boolean_type_node) = 0;
5518 else if (name == lang_name_c)
5520 strict_prototype = strict_prototypes_lang_c;
5521 current_lang_name = name;
5524 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5527 /* Get out of the current language scope. */
5532 /* Clear the current entry so that garbage collector won't hold on
5534 *current_lang_stack = NULL_TREE;
5535 current_lang_name = *--current_lang_stack;
5536 if (current_lang_name == lang_name_cplusplus
5537 || current_lang_name == lang_name_java)
5538 strict_prototype = strict_prototypes_lang_cplusplus;
5539 else if (current_lang_name == lang_name_c)
5540 strict_prototype = strict_prototypes_lang_c;
5543 /* Type instantiation routines. */
5545 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5546 matches the TARGET_TYPE. If there is no satisfactory match, return
5547 error_mark_node, and issue an error message if COMPLAIN is
5548 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5549 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5550 template arguments. */
5553 resolve_address_of_overloaded_function (target_type,
5562 tree explicit_targs;
5564 /* Here's what the standard says:
5568 If the name is a function template, template argument deduction
5569 is done, and if the argument deduction succeeds, the deduced
5570 arguments are used to generate a single template function, which
5571 is added to the set of overloaded functions considered.
5573 Non-member functions and static member functions match targets of
5574 type "pointer-to-function" or "reference-to-function." Nonstatic
5575 member functions match targets of type "pointer-to-member
5576 function;" the function type of the pointer to member is used to
5577 select the member function from the set of overloaded member
5578 functions. If a nonstatic member function is selected, the
5579 reference to the overloaded function name is required to have the
5580 form of a pointer to member as described in 5.3.1.
5582 If more than one function is selected, any template functions in
5583 the set are eliminated if the set also contains a non-template
5584 function, and any given template function is eliminated if the
5585 set contains a second template function that is more specialized
5586 than the first according to the partial ordering rules 14.5.5.2.
5587 After such eliminations, if any, there shall remain exactly one
5588 selected function. */
5591 int is_reference = 0;
5592 /* We store the matches in a TREE_LIST rooted here. The functions
5593 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5594 interoperability with most_specialized_instantiation. */
5595 tree matches = NULL_TREE;
5598 /* By the time we get here, we should be seeing only real
5599 pointer-to-member types, not the internal POINTER_TYPE to
5600 METHOD_TYPE representation. */
5601 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5602 && (TREE_CODE (TREE_TYPE (target_type))
5603 == METHOD_TYPE)), 0);
5605 if (TREE_CODE (overload) == COMPONENT_REF)
5606 overload = TREE_OPERAND (overload, 1);
5608 /* Check that the TARGET_TYPE is reasonable. */
5609 if (TYPE_PTRFN_P (target_type))
5612 else if (TYPE_PTRMEMFUNC_P (target_type))
5613 /* This is OK, too. */
5615 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5617 /* This is OK, too. This comes from a conversion to reference
5619 target_type = build_reference_type (target_type);
5625 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5626 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5627 return error_mark_node;
5630 /* If we can find a non-template function that matches, we can just
5631 use it. There's no point in generating template instantiations
5632 if we're just going to throw them out anyhow. But, of course, we
5633 can only do this when we don't *need* a template function. */
5638 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5640 tree fn = OVL_FUNCTION (fns);
5643 if (TREE_CODE (fn) == TEMPLATE_DECL)
5644 /* We're not looking for templates just yet. */
5647 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5649 /* We're looking for a non-static member, and this isn't
5650 one, or vice versa. */
5653 /* See if there's a match. */
5654 fntype = TREE_TYPE (fn);
5656 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5657 else if (!is_reference)
5658 fntype = build_pointer_type (fntype);
5660 if (can_convert_arg (target_type, fntype, fn))
5661 matches = tree_cons (fn, NULL_TREE, matches);
5665 /* Now, if we've already got a match (or matches), there's no need
5666 to proceed to the template functions. But, if we don't have a
5667 match we need to look at them, too. */
5670 tree target_fn_type;
5671 tree target_arg_types;
5672 tree target_ret_type;
5677 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5679 target_fn_type = TREE_TYPE (target_type);
5680 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5681 target_ret_type = TREE_TYPE (target_fn_type);
5683 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5685 tree fn = OVL_FUNCTION (fns);
5687 tree instantiation_type;
5690 if (TREE_CODE (fn) != TEMPLATE_DECL)
5691 /* We're only looking for templates. */
5694 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5696 /* We're not looking for a non-static member, and this is
5697 one, or vice versa. */
5700 /* Try to do argument deduction. */
5701 targs = make_tree_vec (DECL_NTPARMS (fn));
5702 if (fn_type_unification (fn, explicit_targs, targs,
5703 target_arg_types, target_ret_type,
5705 /* Argument deduction failed. */
5708 /* Instantiate the template. */
5709 instantiation = instantiate_template (fn, targs);
5710 if (instantiation == error_mark_node)
5711 /* Instantiation failed. */
5714 /* See if there's a match. */
5715 instantiation_type = TREE_TYPE (instantiation);
5717 instantiation_type =
5718 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5719 else if (!is_reference)
5720 instantiation_type = build_pointer_type (instantiation_type);
5721 if (can_convert_arg (target_type, instantiation_type, instantiation))
5722 matches = tree_cons (instantiation, fn, matches);
5725 /* Now, remove all but the most specialized of the matches. */
5728 tree match = most_specialized_instantiation (matches,
5731 if (match != error_mark_node)
5732 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5736 /* Now we should have exactly one function in MATCHES. */
5737 if (matches == NULL_TREE)
5739 /* There were *no* matches. */
5742 cp_error ("no matches converting function `%D' to type `%#T'",
5743 DECL_NAME (OVL_FUNCTION (overload)),
5746 /* print_candidates expects a chain with the functions in
5747 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5748 so why be clever?). */
5749 for (; overload; overload = OVL_NEXT (overload))
5750 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5753 print_candidates (matches);
5755 return error_mark_node;
5757 else if (TREE_CHAIN (matches))
5759 /* There were too many matches. */
5765 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5766 DECL_NAME (OVL_FUNCTION (overload)),
5769 /* Since print_candidates expects the functions in the
5770 TREE_VALUE slot, we flip them here. */
5771 for (match = matches; match; match = TREE_CHAIN (match))
5772 TREE_VALUE (match) = TREE_PURPOSE (match);
5774 print_candidates (matches);
5777 return error_mark_node;
5780 /* Good, exactly one match. Now, convert it to the correct type. */
5781 fn = TREE_PURPOSE (matches);
5785 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5786 return build_unary_op (ADDR_EXPR, fn, 0);
5789 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5790 will mark the function as addressed, but here we must do it
5792 mark_addressable (fn);
5798 /* This function will instantiate the type of the expression given in
5799 RHS to match the type of LHSTYPE. If errors exist, then return
5800 error_mark_node. We only complain is COMPLAIN is set. If we are
5801 not complaining, never modify rhs, as overload resolution wants to
5802 try many possible instantiations, in hopes that at least one will
5805 FLAGS is a bitmask, as we see at the top of the function.
5807 For non-recursive calls, LHSTYPE should be a function, pointer to
5808 function, or a pointer to member function. */
5811 instantiate_type (lhstype, rhs, flags)
5815 int complain = (flags & 1);
5816 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
5819 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5822 error ("not enough type information");
5823 return error_mark_node;
5826 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5828 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
5831 cp_error ("argument of type `%T' does not match `%T'",
5832 TREE_TYPE (rhs), lhstype);
5833 return error_mark_node;
5836 /* We don't overwrite rhs if it is an overloaded function.
5837 Copying it would destroy the tree link. */
5838 if (TREE_CODE (rhs) != OVERLOAD)
5839 rhs = copy_node (rhs);
5841 /* This should really only be used when attempting to distinguish
5842 what sort of a pointer to function we have. For now, any
5843 arithmetic operation which is not supported on pointers
5844 is rejected as an error. */
5846 switch (TREE_CODE (rhs))
5853 my_friendly_abort (177);
5854 return error_mark_node;
5861 new_rhs = instantiate_type (build_pointer_type (lhstype),
5862 TREE_OPERAND (rhs, 0), flags);
5863 if (new_rhs == error_mark_node)
5864 return error_mark_node;
5866 TREE_TYPE (rhs) = lhstype;
5867 TREE_OPERAND (rhs, 0) = new_rhs;
5872 rhs = copy_node (TREE_OPERAND (rhs, 0));
5873 TREE_TYPE (rhs) = unknown_type_node;
5874 return instantiate_type (lhstype, rhs, flags);
5878 r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5881 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
5882 && complain && !flag_ms_extensions)
5884 /* Note: we check this after the recursive call to avoid
5885 complaining about cases where overload resolution fails. */
5887 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5888 tree fn = PTRMEM_CST_MEMBER (r);
5890 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
5893 ("object-dependent reference to `%E' can only be used in a call",
5896 (" to form a pointer to member function, say `&%T::%E'",
5904 rhs = TREE_OPERAND (rhs, 1);
5905 if (BASELINK_P (rhs))
5906 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5908 /* This can happen if we are forming a pointer-to-member for a
5910 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5914 case TEMPLATE_ID_EXPR:
5916 tree fns = TREE_OPERAND (rhs, 0);
5917 tree args = TREE_OPERAND (rhs, 1);
5920 resolve_address_of_overloaded_function (lhstype,
5923 /*template_only=*/1,
5925 if (TREE_CODE (fns) == COMPONENT_REF)
5935 resolve_address_of_overloaded_function (lhstype,
5938 /*template_only=*/0,
5939 /*explicit_targs=*/NULL_TREE);
5942 /* Now we should have a baselink. */
5943 my_friendly_assert (BASELINK_P (rhs), 990412);
5945 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5948 /* This is too hard for now. */
5949 my_friendly_abort (183);
5950 return error_mark_node;
5955 TREE_OPERAND (rhs, 0)
5956 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5957 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5958 return error_mark_node;
5959 TREE_OPERAND (rhs, 1)
5960 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5961 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5962 return error_mark_node;
5964 TREE_TYPE (rhs) = lhstype;
5968 case TRUNC_DIV_EXPR:
5969 case FLOOR_DIV_EXPR:
5971 case ROUND_DIV_EXPR:
5973 case TRUNC_MOD_EXPR:
5974 case FLOOR_MOD_EXPR:
5976 case ROUND_MOD_EXPR:
5977 case FIX_ROUND_EXPR:
5978 case FIX_FLOOR_EXPR:
5980 case FIX_TRUNC_EXPR:
5996 case PREINCREMENT_EXPR:
5997 case PREDECREMENT_EXPR:
5998 case POSTINCREMENT_EXPR:
5999 case POSTDECREMENT_EXPR:
6001 error ("invalid operation on uninstantiated type");
6002 return error_mark_node;
6004 case TRUTH_AND_EXPR:
6006 case TRUTH_XOR_EXPR:
6013 case TRUTH_ANDIF_EXPR:
6014 case TRUTH_ORIF_EXPR:
6015 case TRUTH_NOT_EXPR:
6017 error ("not enough type information");
6018 return error_mark_node;
6021 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6024 error ("not enough type information");
6025 return error_mark_node;
6027 TREE_OPERAND (rhs, 1)
6028 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6029 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6030 return error_mark_node;
6031 TREE_OPERAND (rhs, 2)
6032 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6033 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6034 return error_mark_node;
6036 TREE_TYPE (rhs) = lhstype;
6040 TREE_OPERAND (rhs, 1)
6041 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6042 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6043 return error_mark_node;
6045 TREE_TYPE (rhs) = lhstype;
6049 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6051 case ENTRY_VALUE_EXPR:
6052 my_friendly_abort (184);
6053 return error_mark_node;
6056 return error_mark_node;
6059 my_friendly_abort (185);
6060 return error_mark_node;
6064 /* Return the name of the virtual function pointer field
6065 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6066 this may have to look back through base types to find the
6067 ultimate field name. (For single inheritance, these could
6068 all be the same name. Who knows for multiple inheritance). */
6071 get_vfield_name (type)
6074 tree binfo = TYPE_BINFO (type);
6077 while (BINFO_BASETYPES (binfo)
6078 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6079 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6080 binfo = BINFO_BASETYPE (binfo, 0);
6082 type = BINFO_TYPE (binfo);
6083 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6084 + TYPE_NAME_LENGTH (type) + 2);
6085 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6086 return get_identifier (buf);
6090 print_class_statistics ()
6092 #ifdef GATHER_STATISTICS
6093 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6094 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6095 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6096 n_build_method_call, n_inner_fields_searched);
6099 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6100 n_vtables, n_vtable_searches);
6101 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6102 n_vtable_entries, n_vtable_elems);
6107 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6108 according to [class]:
6109 The class-name is also inserted
6110 into the scope of the class itself. For purposes of access checking,
6111 the inserted class name is treated as if it were a public member name. */
6114 build_self_reference ()
6116 tree name = constructor_name (current_class_type);
6117 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6120 DECL_NONLOCAL (value) = 1;
6121 DECL_CONTEXT (value) = current_class_type;
6122 DECL_ARTIFICIAL (value) = 1;
6124 if (processing_template_decl)
6125 value = push_template_decl (value);
6127 saved_cas = current_access_specifier;
6128 current_access_specifier = access_public_node;
6129 finish_member_declaration (value);
6130 current_access_specifier = saved_cas;
6133 /* Returns 1 if TYPE contains only padding bytes. */
6136 is_empty_class (type)
6141 if (type == error_mark_node)
6144 if (! IS_AGGR_TYPE (type))
6148 return integer_zerop (CLASSTYPE_SIZE (type));
6150 if (TYPE_BINFO_BASETYPES (type))
6152 t = TYPE_FIELDS (type);
6153 while (t && TREE_CODE (t) != FIELD_DECL)
6155 return (t == NULL_TREE);
6158 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6159 a *_TYPE node. NODE can also be a local class. */
6162 get_enclosing_class (type)
6167 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6169 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6172 node = DECL_CONTEXT (node);
6178 node = TYPE_CONTEXT (node);
6182 my_friendly_abort (0);
6188 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6191 is_base_of_enclosing_class (base, type)
6196 if (get_binfo (base, type, 0))
6199 type = get_enclosing_class (type);
6204 /* Note that NAME was looked up while the current class was being
6205 defined and that the result of that lookup was DECL. */
6208 maybe_note_name_used_in_class (name, decl)
6212 splay_tree names_used;
6214 /* If we're not defining a class, there's nothing to do. */
6215 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6218 /* If there's already a binding for this NAME, then we don't have
6219 anything to worry about. */
6220 if (IDENTIFIER_CLASS_VALUE (name))
6223 if (!current_class_stack[current_class_depth - 1].names_used)
6224 current_class_stack[current_class_depth - 1].names_used
6225 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6226 names_used = current_class_stack[current_class_depth - 1].names_used;
6228 splay_tree_insert (names_used,
6229 (splay_tree_key) name,
6230 (splay_tree_value) decl);
6233 /* Note that NAME was declared (as DECL) in the current class. Check
6234 to see that the declaration is legal. */
6237 note_name_declared_in_class (name, decl)
6241 splay_tree names_used;
6244 /* Look to see if we ever used this name. */
6246 = current_class_stack[current_class_depth - 1].names_used;
6250 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6253 /* [basic.scope.class]
6255 A name N used in a class S shall refer to the same declaration
6256 in its context and when re-evaluated in the completed scope of
6258 cp_error ("declaration of `%#D'", decl);
6259 cp_error_at ("changes meaning of `%s' from `%+#D'",
6260 IDENTIFIER_POINTER (DECL_NAME (OVL_CURRENT (decl))),
6265 /* Returns the VAR_DECL for the complete vtable associated with
6266 BINFO. (Under the new ABI, secondary vtables are merged with
6267 primary vtables; this function will return the VAR_DECL for the
6271 get_vtbl_decl_for_binfo (binfo)
6276 decl = BINFO_VTABLE (binfo);
6277 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6279 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6281 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6284 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6288 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6289 dominated by T) to stderr. INDENT should be zero when called from
6290 the top level; it is incremented recursively. */
6293 dump_class_hierarchy_r (t, binfo, indent)
6300 fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
6301 (unsigned long) binfo,
6302 type_as_string (binfo, TS_PLAIN));
6303 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
6304 tree_low_cst (BINFO_OFFSET (binfo), 0));
6305 if (TREE_VIA_VIRTUAL (binfo))
6306 fprintf (stderr, " virtual");
6307 if (BINFO_PRIMARY_MARKED_P (binfo)
6308 || (TREE_VIA_VIRTUAL (binfo)
6309 && BINFO_VBASE_PRIMARY_P (BINFO_FOR_VBASE (BINFO_TYPE (binfo),
6311 fprintf (stderr, " primary");
6312 fprintf (stderr, "\n");
6314 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6315 dump_class_hierarchy_r (t, BINFO_BASETYPE (binfo, i), indent + 2);
6318 /* Dump the BINFO hierarchy for T. */
6321 dump_class_hierarchy (t)
6326 dump_class_hierarchy_r (t, TYPE_BINFO (t), 0);
6327 fprintf (stderr, "virtual bases\n");
6328 for (vbase = CLASSTYPE_VBASECLASSES (t); vbase; vbase = TREE_CHAIN (vbase))
6329 dump_class_hierarchy_r (t, vbase, 0);
6332 /* Virtual function table initialization. */
6334 /* Create all the necessary vtables for T and its base classes. */
6340 if (merge_primary_and_secondary_vtables_p ())
6345 /* Under the new ABI, we lay out the primary and secondary
6346 vtables in one contiguous vtable. The primary vtable is
6347 first, followed by the non-virtual secondary vtables in
6348 inheritance graph order. */
6349 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6350 TREE_TYPE (list) = t;
6351 accumulate_vtbl_inits (TYPE_BINFO (t), list);
6352 /* Then come the virtual bases, also in inheritance graph
6354 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6356 if (!TREE_VIA_VIRTUAL (vbase))
6358 accumulate_vtbl_inits (BINFO_FOR_VBASE (BINFO_TYPE (vbase), t),
6362 if (TYPE_BINFO_VTABLE (t))
6363 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6367 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
6368 dfs_unmarked_real_bases_queue_p, t);
6369 dfs_walk (TYPE_BINFO (t), dfs_unmark,
6370 dfs_marked_real_bases_queue_p, t);
6374 /* Called from finish_vtbls via dfs_walk. */
6377 dfs_finish_vtbls (binfo, data)
6381 tree t = (tree) data;
6383 if (!BINFO_PRIMARY_MARKED_P (binfo)
6384 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
6385 && BINFO_NEW_VTABLE_MARKED (binfo, t))
6386 initialize_vtable (binfo,
6387 build_vtbl_initializer (binfo, t, NULL));
6389 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo, t);
6390 SET_BINFO_MARKED (binfo);
6395 /* Initialize the vtable for BINFO with the INITS. */
6398 initialize_vtable (binfo, inits)
6405 layout_vtable_decl (binfo, list_length (inits));
6406 decl = get_vtbl_decl_for_binfo (binfo);
6407 context = DECL_CONTEXT (decl);
6408 DECL_CONTEXT (decl) = 0;
6409 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6410 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6411 DECL_CONTEXT (decl) = context;
6414 /* Add the vtbl initializers for BINFO (and its non-primary,
6415 non-virtual bases) to the list of INITS. */
6418 accumulate_vtbl_inits (binfo, inits)
6422 /* Walk the BINFO and its bases. We walk in preorder so that as we
6423 initialize each vtable we can figure out at what offset the
6424 secondary vtable lies from the primary vtable. */
6425 dfs_walk_real (binfo,
6426 dfs_accumulate_vtbl_inits,
6432 /* Called from finish_vtbls via dfs_walk when using the new ABI.
6433 Accumulates the vtable initializers for all of the vtables into
6434 TREE_VALUE (DATA). */
6437 dfs_accumulate_vtbl_inits (binfo, data)
6447 if (!BINFO_PRIMARY_MARKED_P (binfo)
6448 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
6449 && BINFO_NEW_VTABLE_MARKED (binfo, t))
6456 /* Compute the initializer for this vtable. */
6457 inits = build_vtbl_initializer (binfo, t, &non_fn_entries);
6459 /* Set BINFO_VTABLE to the address where the VPTR should point. */
6460 vtbl = TREE_PURPOSE (l);
6461 vtbl = build1 (ADDR_EXPR,
6462 build_pointer_type (TREE_TYPE (vtbl)),
6464 index = size_binop (PLUS_EXPR,
6465 size_int (non_fn_entries),
6466 size_int (list_length (TREE_VALUE (l))));
6467 BINFO_VTABLE (binfo)
6468 = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
6469 size_binop (MULT_EXPR,
6470 TYPE_SIZE_UNIT (TREE_TYPE (vtbl)),
6473 /* Add the initializers for this vtable to the initializers for
6474 the other vtables we've already got. */
6475 TREE_VALUE (l) = chainon (TREE_VALUE (l), inits);
6478 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo, t);
6483 /* Construct the initializer for BINFOs virtual function table. BINFO
6484 is part of the hierarchy dominated by T. The value returned is a
6485 TREE_LIST suitable for wrapping in a CONSTRUCTOR to use as the
6486 DECL_INITIAL for a vtable. If NON_FN_ENTRIES_P is not NULL,
6487 *NON_FN_ENTRIES_P is set to the number of non-function entries in
6491 build_vtbl_initializer (binfo, t, non_fn_entries_p)
6494 int *non_fn_entries_p;
6496 tree v = BINFO_VIRTUALS (binfo);
6497 tree inits = NULL_TREE;
6500 vcall_offset_data vod;
6502 /* Initialize those parts of VOD that matter. */
6504 vod.inits = NULL_TREE;
6505 vod.primary_p = (binfo == TYPE_BINFO (t));
6506 /* The first vbase or vcall offset is at index -3 in the vtable. */
6507 vod.index = build_int_2 (-3, -1);
6509 /* Add the vcall and vbase offset entries. */
6510 build_vcall_and_vbase_vtbl_entries (binfo, &vod);
6512 /* Clear BINFO_VTABLE_PAATH_MARKED; it's set by
6513 build_vbase_offset_vtbl_entries. */
6514 for (vbase = CLASSTYPE_VBASECLASSES (t);
6516 vbase = TREE_CHAIN (vbase))
6517 CLEAR_BINFO_VTABLE_PATH_MARKED (vbase);
6519 /* Add entries to the vtable for RTTI. */
6520 inits = chainon (inits, build_rtti_vtbl_entries (binfo, t));
6522 if (non_fn_entries_p)
6523 *non_fn_entries_p = list_length (inits);
6525 /* Go through all the ordinary virtual functions, building up
6527 vfun_inits = NULL_TREE;
6536 /* Pull the offset for `this', and the function to call, out of
6538 delta = BV_DELTA (v);
6539 vcall_index = BV_VCALL_INDEX (v);
6541 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
6542 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
6544 /* You can't call an abstract virtual function; it's abstract.
6545 So, we replace these functions with __pure_virtual. */
6546 if (DECL_PURE_VIRTUAL_P (fn))
6549 /* Take the address of the function, considering it to be of an
6550 appropriate generic type. */
6551 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
6552 /* The address of a function can't change. */
6553 TREE_CONSTANT (pfn) = 1;
6554 /* Enter it in the vtable. */
6555 init = build_vtable_entry (delta, vcall_index, pfn);
6556 /* And add it to the chain of initializers. */
6557 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
6563 /* The initializers for virtual functions were built up in reverse
6564 order; straighten them out now. */
6565 vfun_inits = nreverse (vfun_inits);
6567 /* The complete initializer is the INITS, followed by the
6569 return chainon (inits, vfun_inits);
6572 /* Sets vod->inits to be the initializers for the vbase and vcall
6573 offsets in BINFO, which is in the hierarchy dominated by T. */
6576 build_vcall_and_vbase_vtbl_entries (binfo, vod)
6578 vcall_offset_data *vod;
6583 /* If this is a derived class, we must first create entries
6584 corresponding to the base class. These entries must go closer to
6585 the vptr, so we save them up and add them to the end of the list
6588 vod->inits = NULL_TREE;
6589 b = BINFO_PRIMARY_BINFO (binfo);
6591 build_vcall_and_vbase_vtbl_entries (b, vod);
6593 /* Add the vbase entries for this base. */
6594 build_vbase_offset_vtbl_entries (binfo, vod);
6595 /* Add the vcall entries for this base. */
6596 build_vcall_offset_vtbl_entries (binfo, vod);
6598 vod->inits = chainon (vod->inits, inits);
6601 /* Returns the initializers for the vbase offset entries in the vtable
6602 for BINFO (which is part of the class hierarchy dominated by T), in
6603 reverse order. VBASE_OFFSET_INDEX gives the vtable index
6604 where the next vbase offset will go. */
6607 build_vbase_offset_vtbl_entries (binfo, vod)
6609 vcall_offset_data *vod;
6614 /* Under the old ABI, pointers to virtual bases are stored in each
6616 if (!vbase_offsets_in_vtable_p ())
6619 /* If there are no virtual baseclasses, then there is nothing to
6621 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
6626 /* Go through the virtual bases, adding the offsets. */
6627 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
6629 vbase = TREE_CHAIN (vbase))
6634 if (!TREE_VIA_VIRTUAL (vbase))
6637 /* Find the instance of this virtual base in the complete
6639 b = BINFO_FOR_VBASE (BINFO_TYPE (vbase), t);
6641 /* If we've already got an offset for this virtual base, we
6642 don't need another one. */
6643 if (BINFO_VTABLE_PATH_MARKED (b))
6645 SET_BINFO_VTABLE_PATH_MARKED (b);
6647 /* Figure out where we can find this vbase offset. */
6648 delta = size_binop (MULT_EXPR,
6649 convert (ssizetype, vod->index),
6651 TYPE_SIZE_UNIT (vtable_entry_type)));
6653 BINFO_VPTR_FIELD (b) = delta;
6655 if (binfo != TYPE_BINFO (t))
6659 /* Find the instance of this virtual base in the type of BINFO. */
6660 orig_vbase = BINFO_FOR_VBASE (BINFO_TYPE (vbase),
6661 BINFO_TYPE (binfo));
6663 /* The vbase offset had better be the same. */
6664 if (!tree_int_cst_equal (delta,
6665 BINFO_VPTR_FIELD (orig_vbase)))
6666 my_friendly_abort (20000403);
6669 /* The next vbase will come at a more negative offset. */
6670 vod->index = fold (build (MINUS_EXPR, integer_type_node,
6671 vod->index, integer_one_node));
6673 /* The initializer is the delta from BINFO to this virtual base.
6674 The vbase offsets go in reverse inheritance-graph order, and
6675 we are walking in inheritance graph order so these end up in
6677 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (binfo));
6678 vod->inits = tree_cons (NULL_TREE,
6679 fold (build1 (NOP_EXPR,
6686 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
6689 dfs_build_vcall_offset_vtbl_entries (binfo, data)
6693 vcall_offset_data* vod;
6694 tree derived_virtuals;
6697 tree non_primary_binfo;
6701 vod = (vcall_offset_data *) data;
6702 binfo_inits = NULL_TREE;
6704 /* We might be a primary base class. Go up the inheritance
6705 hierarchy until we find the class of which we are a primary base:
6706 it is the BINFO_VIRTUALS there that we need to consider. */
6707 non_primary_binfo = binfo;
6708 while (BINFO_PRIMARY_MARKED_P (non_primary_binfo))
6709 non_primary_binfo = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
6711 /* Skip virtuals that we have already handled in a primary base
6713 base_virtuals = BINFO_VIRTUALS (binfo);
6714 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo);
6715 b = BINFO_PRIMARY_BINFO (binfo);
6717 for (i = 0; i < CLASSTYPE_VSIZE (BINFO_TYPE (b)); ++i)
6719 base_virtuals = TREE_CHAIN (base_virtuals);
6720 derived_virtuals = TREE_CHAIN (derived_virtuals);
6723 /* Make entries for the rest of the virtuals. */
6724 while (base_virtuals)
6726 /* Figure out what function we're looking at. */
6727 tree fn = TREE_VALUE (derived_virtuals);
6728 tree base = DECL_CONTEXT (fn);
6729 /* The FN comes from BASE. So, we must caculate the adjustment
6730 from the virtual base that derived from BINFO to BASE. */
6731 tree base_binfo = get_binfo (base, vod->derived, /*protect=*/0);
6733 /* Compute the vcall offset. */
6735 = tree_cons (NULL_TREE,
6736 fold (build1 (NOP_EXPR, vtable_entry_type,
6737 size_diffop (BINFO_OFFSET (base_binfo),
6738 BINFO_OFFSET (vod->vbase)))),
6741 /* Keep track of the vtable index where this vcall offset can be
6743 BV_VCALL_INDEX (derived_virtuals) = vod->index;
6744 /* The next vcall offset will be found at a more negative
6746 vod->index = fold (build (MINUS_EXPR, integer_type_node,
6747 vod->index, integer_one_node));
6749 /* Go to the next entries in the list. */
6750 derived_virtuals = TREE_CHAIN (derived_virtuals);
6751 base_virtuals = TREE_CHAIN (base_virtuals);
6754 /* The offests are built up in reverse order, so we straighten them
6755 here. We simultaneously add them to VOD->INITS; we're walking
6756 the bases in inheritance graph order, and the initializers are
6757 supposed to appear in reverse inheritance order, so that's
6763 next = TREE_CHAIN (binfo_inits);
6764 TREE_CHAIN (binfo_inits) = vod->inits;
6765 vod->inits = binfo_inits;
6772 /* Adds the initializers for the vcall offset entries in the vtable
6773 for BINFO (which is part of the class hierarchy dominated by T) to
6777 build_vcall_offset_vtbl_entries (binfo, vod)
6779 vcall_offset_data *vod;
6783 /* Under the old ABI, the adjustments to the `this' pointer were made
6785 if (!vcall_offsets_in_vtable_p ())
6788 /* We only need these entries if this base is a virtual base. */
6789 if (!TREE_VIA_VIRTUAL (binfo))
6792 /* We need a vcall offset for each of the virtual functions in this
6793 vtable. For example:
6795 class A { virtual void f (); };
6796 class B : virtual public A { };
6797 class C: virtual public A, public B {};
6804 The location of `A' is not at a fixed offset relative to `B'; the
6805 offset depends on the complete object derived from `B'. So,
6806 `B' vtable contains an entry for `f' that indicates by what
6807 amount the `this' pointer for `B' needs to be adjusted to arrive
6810 We need entries for all the functions in our primary vtable and
6811 in our non-virtual bases vtables. For each base, the entries
6812 appear in the same order as in the base; but the bases themselves
6813 appear in reverse depth-first, left-to-right order. */
6816 vod->inits = NULL_TREE;
6817 dfs_walk_real (binfo,
6818 dfs_build_vcall_offset_vtbl_entries,
6822 vod->inits = chainon (vod->inits, inits);
6825 /* Return vtbl initializers for the RTTI entries coresponding to the
6826 BINFO's vtable. BINFO is a part of the hierarchy dominated by
6830 build_rtti_vtbl_entries (binfo, t)
6841 basetype = BINFO_TYPE (binfo);
6844 /* For a COM object there is no RTTI entry. */
6845 if (CLASSTYPE_COM_INTERFACE (basetype))
6848 /* To find the complete object, we will first convert to our most
6849 primary base, and then add the offset in the vtbl to that value. */
6851 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b)))
6855 primary_base = BINFO_PRIMARY_BINFO (b);
6856 if (!BINFO_PRIMARY_MARKED_P (primary_base))
6860 offset = size_diffop (size_zero_node, BINFO_OFFSET (b));
6862 /* The second entry is, in the case of the new ABI, the address of
6863 the typeinfo object, or, in the case of the old ABI, a function
6864 which returns a typeinfo object. */
6865 if (new_abi_rtti_p ())
6868 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
6870 decl = integer_zero_node;
6872 /* Convert the declaration to a type that can be stored in the
6874 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
6875 TREE_CONSTANT (init) = 1;
6880 decl = get_tinfo_decl (t);
6882 decl = abort_fndecl;
6884 /* Convert the declaration to a type that can be stored in the
6886 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, decl);
6887 TREE_CONSTANT (init) = 1;
6888 init = build_vtable_entry (offset, integer_zero_node, init);
6890 inits = tree_cons (NULL_TREE, init, inits);
6892 /* Add the offset-to-top entry. It comes earlier in the vtable that
6893 the the typeinfo entry. */
6894 if (flag_vtable_thunks)
6896 /* Convert the offset to look like a function pointer, so that
6897 we can put it in the vtable. */
6898 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
6899 TREE_CONSTANT (init) = 1;
6900 inits = tree_cons (NULL_TREE, init, inits);
6906 /* Build an entry in the virtual function table. DELTA is the offset
6907 for the `this' pointer. VCALL_INDEX is the vtable index containing
6908 the vcall offset; zero if none. ENTRY is the virtual function
6909 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
6910 but it may not actually be a virtual function table pointer. (For
6911 example, it might be the address of the RTTI object, under the new
6915 build_vtable_entry (delta, vcall_index, entry)
6920 if (flag_vtable_thunks)
6922 HOST_WIDE_INT idelta;
6923 HOST_WIDE_INT ivindex;
6926 idelta = tree_low_cst (delta, 0);
6927 ivindex = tree_low_cst (vcall_index, 0);
6928 fn = TREE_OPERAND (entry, 0);
6929 if ((idelta || ivindex)
6930 && fn != abort_fndecl
6931 && !DECL_TINFO_FN_P (fn))
6933 entry = make_thunk (entry, idelta, ivindex);
6934 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
6935 TREE_READONLY (entry) = 1;
6936 TREE_CONSTANT (entry) = 1;
6938 #ifdef GATHER_STATISTICS
6939 n_vtable_entries += 1;
6945 tree elems = tree_cons (NULL_TREE, delta,
6946 tree_cons (NULL_TREE, integer_zero_node,
6947 build_tree_list (NULL_TREE, entry)));
6948 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
6950 /* We don't use vcall offsets when not using vtable thunks. */
6951 my_friendly_assert (integer_zerop (vcall_index), 20000125);
6953 /* DELTA used to be constructed by `size_int' and/or size_binop,
6954 which caused overflow problems when it was negative. That should
6957 if (! int_fits_type_p (delta, delta_type_node))
6959 if (flag_huge_objects)
6960 sorry ("object size exceeds built-in limit for virtual function table implementation");
6962 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
6965 TREE_CONSTANT (entry) = 1;
6966 TREE_STATIC (entry) = 1;
6967 TREE_READONLY (entry) = 1;
6969 #ifdef GATHER_STATISTICS
6970 n_vtable_entries += 1;