1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 92-99, 2000 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* High-level class interface. */
37 #define obstack_chunk_alloc xmalloc
38 #define obstack_chunk_free free
40 /* This is how we tell when two virtual member functions are really the
42 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
44 extern void set_class_shadows PARAMS ((tree));
46 /* The number of nested classes being processed. If we are not in the
47 scope of any class, this is zero. */
49 int current_class_depth;
51 /* In order to deal with nested classes, we keep a stack of classes.
52 The topmost entry is the innermost class, and is the entry at index
53 CURRENT_CLASS_DEPTH */
55 typedef struct class_stack_node {
56 /* The name of the class. */
59 /* The _TYPE node for the class. */
62 /* The access specifier pending for new declarations in the scope of
66 /* If were defining TYPE, the names used in this class. */
67 splay_tree names_used;
68 }* class_stack_node_t;
70 /* The stack itself. This is an dynamically resized array. The
71 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
72 static int current_class_stack_size;
73 static class_stack_node_t current_class_stack;
75 static tree get_vfield_name PARAMS ((tree));
76 static void finish_struct_anon PARAMS ((tree));
77 static tree build_vbase_pointer PARAMS ((tree, tree));
78 static tree build_vtable_entry PARAMS ((tree, tree, tree));
79 static tree get_vtable_name PARAMS ((tree));
80 static tree get_derived_offset PARAMS ((tree, tree));
81 static tree get_basefndecls PARAMS ((tree, tree));
82 static void set_rtti_entry PARAMS ((tree, tree, tree));
83 static void build_vtable PARAMS ((tree, tree));
84 static void prepare_fresh_vtable PARAMS ((tree, tree));
85 static tree dfs_fixup_vtable_deltas PARAMS ((tree, void *));
86 static tree dfs_finish_vtbls PARAMS ((tree, void *));
87 static void finish_vtbls PARAMS ((tree));
88 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree));
89 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
90 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
91 static void delete_duplicate_fields PARAMS ((tree));
92 static void finish_struct_bits PARAMS ((tree));
93 static int alter_access PARAMS ((tree, tree, tree, tree));
94 static void handle_using_decl PARAMS ((tree, tree));
95 static int overrides PARAMS ((tree, tree));
96 static int strictly_overrides PARAMS ((tree, tree));
97 static void merge_overrides PARAMS ((tree, tree, int, tree));
98 static void override_one_vtable PARAMS ((tree, tree, tree));
99 static void mark_overriders PARAMS ((tree, tree));
100 static void check_for_override PARAMS ((tree, tree));
101 static tree dfs_get_class_offset PARAMS ((tree, void *));
102 static tree get_class_offset PARAMS ((tree, tree, tree, tree));
103 static void modify_one_vtable PARAMS ((tree, tree, tree));
104 static tree dfs_modify_vtables PARAMS ((tree, void *));
105 static tree modify_all_vtables PARAMS ((tree, int *, tree));
106 static void determine_primary_base PARAMS ((tree, int *));
107 static void finish_struct_methods PARAMS ((tree));
108 static void maybe_warn_about_overly_private_class PARAMS ((tree));
109 static int field_decl_cmp PARAMS ((const tree *, const tree *));
110 static int method_name_cmp PARAMS ((const tree *, const tree *));
111 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
112 static tree fixed_type_or_null PARAMS ((tree, int *));
113 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
115 static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
116 static tree build_vtbl_initializer PARAMS ((tree, tree));
117 static int count_fields PARAMS ((tree));
118 static int add_fields_to_vec PARAMS ((tree, tree, int));
119 static void check_bitfield_decl PARAMS ((tree));
120 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
121 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
123 static int avoid_overlap PARAMS ((tree, tree, int *));
124 static tree build_base_field PARAMS ((tree, tree, int *, int *, unsigned int *));
125 static tree build_base_fields PARAMS ((tree, int *));
126 static tree build_vbase_pointer_fields PARAMS ((tree, int *));
127 static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, int *));
128 static void check_methods PARAMS ((tree));
129 static void remove_zero_width_bit_fields PARAMS ((tree));
130 static void check_bases PARAMS ((tree, int *, int *, int *));
131 static void check_bases_and_members PARAMS ((tree, int *));
132 static void create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
133 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
134 static void fixup_pending_inline PARAMS ((struct pending_inline *));
135 static void fixup_inline_methods PARAMS ((tree));
136 static void set_primary_base PARAMS ((tree, int, int *));
137 static tree dfs_propagate_binfo_offsets PARAMS ((tree, void *));
138 static void propagate_binfo_offsets PARAMS ((tree, tree));
139 static void layout_basetypes PARAMS ((tree));
140 static void layout_virtual_bases PARAMS ((tree));
141 static void remove_base_field PARAMS ((tree, tree, tree *));
142 static void remove_base_fields PARAMS ((tree));
143 static tree dfs_set_offset_for_shared_vbases PARAMS ((tree, void *));
144 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
145 static tree dfs_build_vbase_offset_vtbl_entries PARAMS ((tree, void *));
146 static tree build_vbase_offset_vtbl_entries PARAMS ((tree, tree));
147 static tree dfs_vcall_offset_queue_p PARAMS ((tree, void *));
148 static tree dfs_build_vcall_offset_vtbl_entries PARAMS ((tree, void *));
149 static tree build_vcall_offset_vtbl_entries PARAMS ((tree, tree));
150 static tree dfs_count_virtuals PARAMS ((tree, void *));
151 static void start_vtable PARAMS ((tree, int *));
152 static void layout_vtable_decl PARAMS ((tree, int));
153 static int num_vfun_entries PARAMS ((tree));
155 /* Variables shared between class.c and call.c. */
157 #ifdef GATHER_STATISTICS
159 int n_vtable_entries = 0;
160 int n_vtable_searches = 0;
161 int n_vtable_elems = 0;
162 int n_convert_harshness = 0;
163 int n_compute_conversion_costs = 0;
164 int n_build_method_call = 0;
165 int n_inner_fields_searched = 0;
168 /* Virtual base class layout. */
170 /* Returns a list of virtual base class pointers as a chain of
174 build_vbase_pointer_fields (rec, empty_p)
178 /* Chain to hold all the new FIELD_DECLs which point at virtual
180 tree vbase_decls = NULL_TREE;
181 tree binfos = TYPE_BINFO_BASETYPES (rec);
182 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
186 /* Under the new ABI, there are no vbase pointers in the object.
187 Instead, the offsets are stored in the vtable. */
188 if (vbase_offsets_in_vtable_p ())
191 /* Loop over the baseclasses, adding vbase pointers as needed. */
192 for (i = 0; i < n_baseclasses; i++)
194 register tree base_binfo = TREE_VEC_ELT (binfos, i);
195 register tree basetype = BINFO_TYPE (base_binfo);
197 if (TYPE_SIZE (basetype) == 0)
198 /* This error is now reported in xref_tag, thus giving better
199 location information. */
202 /* All basetypes are recorded in the association list of the
205 if (TREE_VIA_VIRTUAL (base_binfo))
210 /* The offset for a virtual base class is only used in computing
211 virtual function tables and for initializing virtual base
212 pointers. It is built once `get_vbase_types' is called. */
214 /* If this basetype can come from another vbase pointer
215 without an additional indirection, we will share
216 that pointer. If an indirection is involved, we
217 make our own pointer. */
218 for (j = 0; j < n_baseclasses; j++)
220 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
221 if (! TREE_VIA_VIRTUAL (other_base_binfo)
222 && BINFO_FOR_VBASE (basetype, BINFO_TYPE (other_base_binfo)))
225 FORMAT_VBASE_NAME (name, basetype);
226 decl = build_vtbl_or_vbase_field (get_identifier (name),
227 get_identifier (VTABLE_BASE),
228 build_pointer_type (basetype),
231 BINFO_VPTR_FIELD (base_binfo) = decl;
232 TREE_CHAIN (decl) = vbase_decls;
237 /* The space this decl occupies has already been accounted for. */
245 /* Called from build_vbase_offset_vtbl_entries via dfs_walk. */
248 dfs_build_vbase_offset_vtbl_entries (binfo, data)
252 tree list = (tree) data;
254 if (TREE_TYPE (list) == binfo)
255 /* The TREE_TYPE of LIST is the base class from which we started
256 walking. If that BINFO is virtual it's not a virtual baseclass
259 else if (TREE_VIA_VIRTUAL (binfo))
264 /* Remember the index to the vbase offset for this virtual
266 vbase = BINFO_FOR_VBASE (TREE_TYPE (binfo), TREE_PURPOSE (list));
267 if (!TREE_VALUE (list))
268 BINFO_VPTR_FIELD (vbase) = build_int_2 (-1, 0);
271 BINFO_VPTR_FIELD (vbase) = TREE_PURPOSE (TREE_VALUE (list));
272 BINFO_VPTR_FIELD (vbase) = ssize_binop (MINUS_EXPR,
273 BINFO_VPTR_FIELD (vbase),
277 /* And record the offset at which this virtual base lies in the
279 init = BINFO_OFFSET (binfo);
280 TREE_VALUE (list) = tree_cons (BINFO_VPTR_FIELD (vbase),
285 SET_BINFO_VTABLE_PATH_MARKED (binfo);
290 /* Returns the initializers for the vbase offset entries in the vtable
291 for BINFO (which is part of the class hierarchy dominated by T), in
295 build_vbase_offset_vtbl_entries (binfo, t)
303 /* Under the old ABI, pointers to virtual bases are stored in each
305 if (!vbase_offsets_in_vtable_p ())
308 /* If there are no virtual baseclasses, then there is nothing to
310 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
315 /* The offsets are allocated in the reverse order of a
316 depth-first left-to-right traversal of the hierarchy. We use
317 BINFO_VTABLE_PATH_MARKED because we are ourselves during a
318 dfs_walk, and so BINFO_MARKED is already in use. */
319 list = build_tree_list (t, NULL_TREE);
320 TREE_TYPE (list) = binfo;
322 dfs_build_vbase_offset_vtbl_entries,
323 dfs_vtable_path_unmarked_real_bases_queue_p,
326 dfs_vtable_path_unmark,
327 dfs_vtable_path_marked_real_bases_queue_p,
329 inits = nreverse (TREE_VALUE (list));
331 /* We've now got offsets in the right order. However, the offsets
332 we've stored are offsets from the beginning of the complete
333 object, and we need offsets from this BINFO. */
334 for (init = inits; init; init = TREE_CHAIN (init))
336 tree exp = TREE_VALUE (init);
338 exp = ssize_binop (MINUS_EXPR, exp, BINFO_OFFSET (binfo));
339 exp = build1 (NOP_EXPR, vtable_entry_type, exp);
341 TREE_CONSTANT (exp) = 1;
342 /* The dfs_build_vbase_offset_vtbl_entries routine uses the
343 TREE_PURPOSE to scribble in. But, we need to clear it now so
344 that the values are not perceived as labeled initializers. */
345 TREE_PURPOSE (init) = NULL_TREE;
346 TREE_VALUE (init) = exp;
352 typedef struct vcall_offset_data_s
354 /* The binfo for the most-derived type. */
356 /* The binfo for the virtual base for which we're building
359 /* The vcall offset initializers built up so far. */
361 /* The number of vcall offsets accumulated. */
365 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
368 dfs_vcall_offset_queue_p (binfo, data)
372 vcall_offset_data* vod = (vcall_offset_data *) data;
374 return (binfo == vod->vbase) ? binfo : dfs_skip_vbases (binfo, NULL);
377 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
380 dfs_build_vcall_offset_vtbl_entries (binfo, data)
384 vcall_offset_data* vod;
388 /* Primary bases are not interesting; all of the virtual
389 function table entries have been overridden. */
390 if (BINFO_PRIMARY_MARKED_P (binfo))
393 vod = (vcall_offset_data *) data;
394 binfo_inits = NULL_TREE;
396 /* We chain the offsets on in reverse order. That's correct --
397 build_vtbl_initializer will straighten them out. */
398 for (virtuals = skip_rtti_stuff (binfo,
402 virtuals = TREE_CHAIN (virtuals))
409 /* Figure out what function we're looking at. */
410 fn = TREE_VALUE (virtuals);
411 base = DECL_CLASS_CONTEXT (fn);
413 /* The FN is comes from BASE. So, we must caculate the
414 adjustment from the virtual base that derived from BINFO to
416 base_binfo = get_binfo (base, vod->derived, /*protect=*/0);
417 offset = ssize_binop (MINUS_EXPR,
418 BINFO_OFFSET (base_binfo),
419 BINFO_OFFSET (vod->vbase));
420 offset = build1 (NOP_EXPR, vtable_entry_type, offset);
421 offset = fold (offset);
422 TREE_CONSTANT (offset) = 1;
423 binfo_inits = tree_cons (NULL_TREE, offset, binfo_inits);
426 /* Now add the initializers we've just created to the list that will
427 be returned to our caller. */
428 vod->inits = chainon (vod->inits, binfo_inits);
433 /* Returns the initializers for the vcall offset entries in the vtable
434 for BINFO (which is part of the class hierarchy dominated by T), in
438 build_vcall_offset_vtbl_entries (binfo, t)
442 vcall_offset_data vod;
444 /* Under the old ABI, the adjustments to the `this' pointer were made
446 if (!vcall_offsets_in_vtable_p ())
449 /* We only need these entries if this base is a virtual base. */
450 if (!TREE_VIA_VIRTUAL (binfo))
453 /* We need a vcall offset for each of the virtual functions in this
456 class A { virtual void f (); };
457 class B : virtual public A { };
458 class C: virtual public A, public B {};
465 The location of `A' is not at a fixed offset relative to `B'; the
466 offset depends on the complete object derived from `B'. So,
467 `B' vtable contains an entry for `f' that indicates by what
468 amount the `this' pointer for `B' needs to be adjusted to arrive
471 We need entries for all the functions in our primary vtable and
472 in our non-virtual bases vtables. For each base, the entries
473 appear in the same order as in the base; but the bases themselves
474 appear in reverse depth-first, left-to-right order. */
477 vod.inits = NULL_TREE;
479 dfs_build_vcall_offset_vtbl_entries,
480 dfs_vcall_offset_queue_p,
486 /* Returns a pointer to the virtual base class of EXP that has the
487 indicated TYPE. EXP is of class type, not a pointer type. */
490 build_vbase_pointer (exp, type)
493 if (vbase_offsets_in_vtable_p ())
498 /* Find the shared copy of TYPE; that's where the vtable offset
500 vbase = BINFO_FOR_VBASE (type, TREE_TYPE (exp));
501 /* Find the virtual function table pointer. */
502 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
503 /* Compute the location where the offset will lie. */
504 vbase_ptr = build_binary_op (PLUS_EXPR,
506 BINFO_VPTR_FIELD (vbase));
507 vbase_ptr = build1 (NOP_EXPR,
508 build_pointer_type (ptrdiff_type_node),
510 /* Add the contents of this location to EXP. */
511 return build (PLUS_EXPR,
512 build_pointer_type (type),
513 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
514 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
519 FORMAT_VBASE_NAME (name, type);
520 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
524 /* Build multi-level access to EXPR using hierarchy path PATH.
525 CODE is PLUS_EXPR if we are going with the grain,
526 and MINUS_EXPR if we are not (in which case, we cannot traverse
527 virtual baseclass links).
529 TYPE is the type we want this path to have on exit.
531 NONNULL is non-zero if we know (for any reason) that EXPR is
532 not, in fact, zero. */
535 build_vbase_path (code, type, expr, path, nonnull)
537 tree type, expr, path;
540 register int changed = 0;
541 tree last = NULL_TREE, last_virtual = NULL_TREE;
543 tree null_expr = 0, nonnull_expr;
545 tree offset = integer_zero_node;
547 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
548 return build1 (NOP_EXPR, type, expr);
550 /* We could do better if we had additional logic to convert back to the
551 unconverted type (the static type of the complete object), and then
552 convert back to the type we want. Until that is done, we only optimize
553 if the complete type is the same type as expr has. */
554 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
556 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
557 expr = save_expr (expr);
560 if (BINFO_INHERITANCE_CHAIN (path))
561 path = reverse_path (path);
563 basetype = BINFO_TYPE (path);
567 if (TREE_VIA_VIRTUAL (path))
569 last_virtual = BINFO_TYPE (path);
570 if (code == PLUS_EXPR)
572 changed = ! fixed_type_p;
578 /* We already check for ambiguous things in the caller, just
582 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
583 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
585 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
586 nonnull_expr = build_vbase_pointer (ind, last_virtual);
588 && TREE_CODE (type) == POINTER_TYPE
589 && null_expr == NULL_TREE)
591 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
592 expr = build (COND_EXPR, build_pointer_type (last_virtual),
593 build (EQ_EXPR, boolean_type_node, expr,
595 null_expr, nonnull_expr);
598 /* else we'll figure out the offset below. */
600 /* Happens in the case of parse errors. */
601 if (nonnull_expr == error_mark_node)
602 return error_mark_node;
606 cp_error ("cannot cast up from virtual baseclass `%T'",
608 return error_mark_node;
612 path = BINFO_INHERITANCE_CHAIN (path);
614 /* LAST is now the last basetype assoc on the path. */
616 /* A pointer to a virtual base member of a non-null object
617 is non-null. Therefore, we only need to test for zeroness once.
618 Make EXPR the canonical expression to deal with here. */
621 TREE_OPERAND (expr, 2) = nonnull_expr;
622 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
623 = TREE_TYPE (nonnull_expr);
628 /* If we go through any virtual base pointers, make sure that
629 casts to BASETYPE from the last virtual base class use
630 the right value for BASETYPE. */
633 tree intype = TREE_TYPE (TREE_TYPE (expr));
634 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
636 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
637 offset = BINFO_OFFSET (binfo);
641 offset = BINFO_OFFSET (last);
643 if (TREE_INT_CST_LOW (offset))
645 /* Bash types to make the backend happy. */
646 offset = cp_convert (type, offset);
648 /* This shouldn't be necessary. (mrs) */
649 expr = build1 (NOP_EXPR, type, expr);
652 /* If expr might be 0, we need to preserve that zeroness. */
656 TREE_TYPE (null_expr) = type;
658 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
659 if (TREE_SIDE_EFFECTS (expr))
660 expr = save_expr (expr);
662 return build (COND_EXPR, type,
663 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
665 build (code, type, expr, offset));
667 else return build (code, type, expr, offset);
670 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
671 be used multiple times in initialization of multiple inheritance. */
674 TREE_TYPE (expr) = type;
678 return build1 (NOP_EXPR, type, expr);
682 /* Virtual function things. */
684 /* Build an entry in the virtual function table. DELTA is the offset
685 for the `this' pointer. VCALL_INDEX is the vtable index containing
686 the vcall offset; zero if none. FNDECL is the virtual function
690 build_vtable_entry (delta, vcall_index, fndecl)
697 /* Take the address of the function, considering it to be of an
698 appropriate generic type. */
699 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
700 /* The address of a function can't change. */
701 TREE_CONSTANT (pfn) = 1;
703 if (flag_vtable_thunks)
705 HOST_WIDE_INT idelta;
706 HOST_WIDE_INT ivindex;
708 idelta = TREE_INT_CST_LOW (delta);
709 ivindex = TREE_INT_CST_LOW (vcall_index);
710 if ((idelta || ivindex)
711 && ! DECL_PURE_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
713 pfn = make_thunk (pfn, idelta, ivindex);
714 pfn = build1 (ADDR_EXPR, vtable_entry_type, pfn);
715 TREE_READONLY (pfn) = 1;
716 TREE_CONSTANT (pfn) = 1;
718 #ifdef GATHER_STATISTICS
719 n_vtable_entries += 1;
725 extern int flag_huge_objects;
726 tree elems = tree_cons (NULL_TREE, delta,
727 tree_cons (NULL_TREE, integer_zero_node,
728 build_tree_list (NULL_TREE, pfn)));
729 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
731 /* We don't use vcall offsets when not using vtable thunks. */
732 my_friendly_assert (integer_zerop (vcall_index), 20000125);
734 /* DELTA used to be constructed by `size_int' and/or size_binop,
735 which caused overflow problems when it was negative. That should
738 if (! int_fits_type_p (delta, delta_type_node))
740 if (flag_huge_objects)
741 sorry ("object size exceeds built-in limit for virtual function table implementation");
743 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
746 TREE_CONSTANT (entry) = 1;
747 TREE_STATIC (entry) = 1;
748 TREE_READONLY (entry) = 1;
750 #ifdef GATHER_STATISTICS
751 n_vtable_entries += 1;
758 /* We want to give the assembler the vtable identifier as well as
759 the offset to the function pointer. So we generate
761 __asm__ __volatile__ (".vtable_entry %c0, %c1"
762 : : "s"(&class_vtable),
763 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
766 build_vtable_entry_ref (basetype, vtbl, idx)
767 tree basetype, vtbl, idx;
769 static char asm_stmt[] = ".vtable_entry %c0, %c1";
772 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
773 s = build_tree_list (build_string (1, "s"), s);
775 i = build_array_ref (vtbl, idx);
776 if (!flag_vtable_thunks)
777 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
778 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
779 i2 = build_array_ref (vtbl, build_int_2(0,0));
780 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
781 i = build_binary_op (MINUS_EXPR, i, i2);
782 i = build_tree_list (build_string (1, "i"), i);
784 finish_asm_stmt (ridpointers[RID_VOLATILE],
785 build_string (sizeof(asm_stmt)-1, asm_stmt),
786 NULL_TREE, chainon (s, i), NULL_TREE);
789 /* Given an object INSTANCE, return an expression which yields the
790 virtual function vtable element corresponding to INDEX. There are
791 many special cases for INSTANCE which we take care of here, mainly
792 to avoid creating extra tree nodes when we don't have to. */
795 build_vtbl_ref (instance, idx)
799 tree basetype = TREE_TYPE (instance);
801 if (TREE_CODE (basetype) == REFERENCE_TYPE)
802 basetype = TREE_TYPE (basetype);
804 if (instance == current_class_ref)
805 vtbl = build_vfield_ref (instance, basetype);
810 /* Try to figure out what a reference refers to, and
811 access its virtual function table directly. */
812 tree ref = NULL_TREE;
814 if (TREE_CODE (instance) == INDIRECT_REF
815 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
816 ref = TREE_OPERAND (instance, 0);
817 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
820 if (ref && TREE_CODE (ref) == VAR_DECL
821 && DECL_INITIAL (ref))
823 tree init = DECL_INITIAL (ref);
825 while (TREE_CODE (init) == NOP_EXPR
826 || TREE_CODE (init) == NON_LVALUE_EXPR)
827 init = TREE_OPERAND (init, 0);
828 if (TREE_CODE (init) == ADDR_EXPR)
830 init = TREE_OPERAND (init, 0);
831 if (IS_AGGR_TYPE (TREE_TYPE (init))
832 && (TREE_CODE (init) == PARM_DECL
833 || TREE_CODE (init) == VAR_DECL))
839 if (IS_AGGR_TYPE (TREE_TYPE (instance))
840 && (TREE_CODE (instance) == RESULT_DECL
841 || TREE_CODE (instance) == PARM_DECL
842 || TREE_CODE (instance) == VAR_DECL))
843 vtbl = TYPE_BINFO_VTABLE (basetype);
845 vtbl = build_vfield_ref (instance, basetype);
848 assemble_external (vtbl);
851 build_vtable_entry_ref (basetype, vtbl, idx);
853 aref = build_array_ref (vtbl, idx);
858 /* Given an object INSTANCE, return an expression which yields the
859 virtual function corresponding to INDEX. There are many special
860 cases for INSTANCE which we take care of here, mainly to avoid
861 creating extra tree nodes when we don't have to. */
864 build_vfn_ref (ptr_to_instptr, instance, idx)
865 tree *ptr_to_instptr, instance;
868 tree aref = build_vtbl_ref (instance, idx);
870 /* When using thunks, there is no extra delta, and we get the pfn
872 if (flag_vtable_thunks)
877 /* Save the intermediate result in a SAVE_EXPR so we don't have to
878 compute each component of the virtual function pointer twice. */
879 if (TREE_CODE (aref) == INDIRECT_REF)
880 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
883 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
885 cp_convert (ptrdiff_type_node,
886 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
889 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
892 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
893 for the given TYPE. */
896 get_vtable_name (type)
899 tree type_id = build_typename_overload (type);
900 char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
901 + IDENTIFIER_LENGTH (type_id) + 2);
902 const char *ptr = IDENTIFIER_POINTER (type_id);
904 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
906 /* We don't take off the numbers; prepare_fresh_vtable uses the
907 DECL_ASSEMBLER_NAME for the type, which includes the number
908 in `3foo'. If we were to pull them off here, we'd end up with
909 something like `_vt.foo.3bar', instead of a uniform definition. */
910 while (ptr[i] >= '0' && ptr[i] <= '9')
913 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
914 return get_identifier (buf);
917 /* Return the offset to the main vtable for a given base BINFO. */
920 get_vfield_offset (binfo)
924 = size_binop (FLOOR_DIV_EXPR,
925 DECL_FIELD_BITPOS (TYPE_VFIELD (BINFO_TYPE (binfo))),
926 size_int (BITS_PER_UNIT));
927 tmp = convert (sizetype, tmp);
928 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
931 /* Get the offset to the start of the original binfo that we derived
932 this binfo from. If we find TYPE first, return the offset only
933 that far. The shortened search is useful because the this pointer
934 on method calling is expected to point to a DECL_CONTEXT (fndecl)
935 object, and not a baseclass of it. */
938 get_derived_offset (binfo, type)
941 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
944 while (BINFO_BASETYPES (binfo)
945 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
947 tree binfos = BINFO_BASETYPES (binfo);
948 if (BINFO_TYPE (binfo) == type)
950 binfo = TREE_VEC_ELT (binfos, i);
952 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
953 return size_binop (MINUS_EXPR, offset1, offset2);
956 /* Update the rtti info for this class. */
959 set_rtti_entry (virtuals, offset, type)
960 tree virtuals, offset, type;
964 if (CLASSTYPE_COM_INTERFACE (type))
968 decl = get_tinfo_decl (type);
969 else if (!new_abi_rtti_p ())
970 /* If someone tries to get RTTI information for a type compiled
971 without RTTI, they're out of luck. By calling __pure_virtual
972 in this case, we give a small clue as to what went wrong. We
973 could consider having a __no_typeinfo function as well, for a
974 more specific hint. */
977 /* For the new-abi, we just point to the type_info object. */
980 if (flag_vtable_thunks)
982 /* The first slot holds the offset. */
983 BF_DELTA (virtuals) = offset;
984 BF_VCALL_INDEX (virtuals) = integer_zero_node;
986 /* The next node holds the decl. */
987 virtuals = TREE_CHAIN (virtuals);
988 offset = integer_zero_node;
991 /* This slot holds the function to call. */
992 BF_DELTA (virtuals) = offset;
993 BF_VCALL_INDEX (virtuals) = integer_zero_node;
994 BF_FN (virtuals) = decl;
997 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
998 or even complete. If this does not exist, create it. If COMPLETE is
999 non-zero, then complete the definition of it -- that will render it
1000 impossible to actually build the vtable, but is useful to get at those
1001 which are known to exist in the runtime. */
1003 tree get_vtable_decl (type, complete)
1007 tree name = get_vtable_name (type);
1008 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1012 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
1013 && DECL_VIRTUAL_P (decl), 20000118);
1017 decl = build_lang_decl (VAR_DECL, name, void_type_node);
1019 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
1020 import_export_vtable (decl, type, 0);
1022 decl = pushdecl_top_level (decl);
1023 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
1025 DECL_ARTIFICIAL (decl) = 1;
1026 TREE_STATIC (decl) = 1;
1027 #ifndef WRITABLE_VTABLES
1028 /* Make them READONLY by default. (mrs) */
1029 TREE_READONLY (decl) = 1;
1031 /* At one time the vtable info was grabbed 2 words at a time. This
1032 fails on sparc unless you have 8-byte alignment. (tiemann) */
1033 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
1036 DECL_VIRTUAL_P (decl) = 1;
1039 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
1041 DECL_CONTEXT (decl) = type;
1045 /* Build a virtual function for type TYPE.
1046 If BINFO is non-NULL, build the vtable starting with the initial
1047 approximation that it is the same as the one which is the head of
1048 the association list. */
1051 build_vtable (binfo, type)
1054 tree virtuals, decl;
1056 decl = get_vtable_decl (type, /*complete=*/0);
1062 if (BINFO_NEW_VTABLE_MARKED (binfo))
1063 /* We have already created a vtable for this base, so there's
1064 no need to do it again. */
1067 virtuals = copy_list (BINFO_VIRTUALS (binfo));
1068 TREE_TYPE (decl) = TREE_TYPE (BINFO_VTABLE (binfo));
1069 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (BINFO_VTABLE (binfo)));
1071 /* Now do rtti stuff. */
1072 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
1073 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
1074 set_rtti_entry (virtuals, offset, type);
1078 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
1080 virtuals = NULL_TREE;
1083 #ifdef GATHER_STATISTICS
1085 n_vtable_elems += list_length (virtuals);
1088 /* Initialize the association list for this type, based
1089 on our first approximation. */
1090 TYPE_BINFO_VTABLE (type) = decl;
1091 TYPE_BINFO_VIRTUALS (type) = virtuals;
1093 binfo = TYPE_BINFO (type);
1094 SET_BINFO_NEW_VTABLE_MARKED (binfo);
1097 /* Give TYPE a new virtual function table which is initialized
1098 with a skeleton-copy of its original initialization. The only
1099 entry that changes is the `delta' entry, so we can really
1100 share a lot of structure.
1102 FOR_TYPE is the derived type which caused this table to
1105 BINFO is the type association which provided TYPE for FOR_TYPE.
1107 The order in which vtables are built (by calling this function) for
1108 an object must remain the same, otherwise a binary incompatibility
1112 prepare_fresh_vtable (binfo, for_type)
1113 tree binfo, for_type;
1116 tree orig_decl = BINFO_VTABLE (binfo);
1129 if (BINFO_NEW_VTABLE_MARKED (binfo))
1130 /* We already created a vtable for this base. There's no need to
1134 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
1136 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
1137 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
1139 /* We know that the vtable that we are going to create doesn't exist
1140 yet in the global namespace, and when we finish, it will be
1141 pushed into the global namespace. In complex MI hierarchies, we
1142 have to loop while the name we are thinking of adding is globally
1143 defined, adding more name components to the vtable name as we
1144 loop, until the name is unique. This is because in complex MI
1145 cases, we might have the same base more than once. This means
1146 that the order in which this function is called for vtables must
1147 remain the same, otherwise binary compatibility can be
1152 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
1156 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
1158 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
1159 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1160 name = get_identifier (buf);
1162 /* If this name doesn't clash, then we can use it, otherwise
1163 we add more to the name until it is unique. */
1165 if (! IDENTIFIER_GLOBAL_VALUE (name))
1168 /* Set values for next loop through, if the name isn't unique. */
1170 path = BINFO_INHERITANCE_CHAIN (path);
1172 /* We better not run out of stuff to make it unique. */
1173 my_friendly_assert (path != NULL_TREE, 368);
1175 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
1177 if (for_type == basetype)
1179 /* If we run out of basetypes in the path, we have already
1180 found created a vtable with that name before, we now
1181 resort to tacking on _%d to distinguish them. */
1183 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
1184 buf1 = (char *) alloca (i);
1186 sprintf (buf1, "%s%c%s%c%d",
1187 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
1189 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
1190 + strlen (buf1) + 1);
1191 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1192 name = get_identifier (buf);
1194 /* If this name doesn't clash, then we can use it,
1195 otherwise we add something different to the name until
1197 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
1199 /* Hey, they really like MI don't they? Increase the 3
1200 above to 6, and the 999 to 999999. :-) */
1201 my_friendly_assert (j <= 999, 369);
1206 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
1207 new_buf2 = (char *) alloca (i);
1208 sprintf (new_buf2, "%s%c%s",
1209 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
1213 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
1214 /* Remember which class this vtable is really for. */
1215 DECL_CONTEXT (new_decl) = for_type;
1217 DECL_ARTIFICIAL (new_decl) = 1;
1218 TREE_STATIC (new_decl) = 1;
1219 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
1220 DECL_VIRTUAL_P (new_decl) = 1;
1221 #ifndef WRITABLE_VTABLES
1222 /* Make them READONLY by default. (mrs) */
1223 TREE_READONLY (new_decl) = 1;
1225 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
1227 /* Make fresh virtual list, so we can smash it later. */
1228 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
1230 if (TREE_VIA_VIRTUAL (binfo))
1232 tree binfo1 = BINFO_FOR_VBASE (BINFO_TYPE (binfo), for_type);
1234 /* XXX - This should never happen, if it does, the caller should
1235 ensure that the binfo is from for_type's binfos, not from any
1236 base type's. We can remove all this code after a while. */
1237 if (binfo1 != binfo)
1238 warning ("internal inconsistency: binfo offset error for rtti");
1240 offset = BINFO_OFFSET (binfo1);
1243 offset = BINFO_OFFSET (binfo);
1245 set_rtti_entry (BINFO_VIRTUALS (binfo),
1246 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
1249 #ifdef GATHER_STATISTICS
1251 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
1254 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
1255 import_export_vtable (new_decl, for_type, 0);
1257 if (TREE_VIA_VIRTUAL (binfo))
1258 my_friendly_assert (binfo == BINFO_FOR_VBASE (BINFO_TYPE (binfo),
1259 current_class_type),
1261 SET_BINFO_NEW_VTABLE_MARKED (binfo);
1264 /* Make V, an entry on the BINFO_VIRTUALS list for BINFO (which is in
1265 the hierarchy dominated by T) list FNDECL as its BF_FN. */
1268 modify_vtable_entry (t, binfo, fndecl, v)
1274 tree base_offset, offset;
1275 tree context = DECL_CLASS_CONTEXT (fndecl);
1276 tree vfield = TYPE_VFIELD (t);
1280 offset = get_class_offset (context, t, binfo, fndecl);
1282 /* Find the right offset for ythe this pointer based on the
1283 base class we just found. We have to take into
1284 consideration the virtual base class pointers that we
1285 stick in before the virtual function table pointer.
1287 Also, we want just the delta between the most base class
1288 that we derived this vfield from and us. */
1290 = size_binop (PLUS_EXPR,
1291 get_derived_offset (binfo,
1292 DECL_VIRTUAL_CONTEXT (BF_FN (v))),
1293 BINFO_OFFSET (binfo));
1294 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
1295 vcall_index = integer_zero_node;
1297 if (fndecl != BF_FN (v)
1298 || !tree_int_cst_equal (this_offset, BF_DELTA (v))
1299 || !tree_int_cst_equal (vcall_index, BF_VCALL_INDEX (v)))
1303 /* Make sure we can modify the derived association with immunity. */
1304 if (binfo == TYPE_BINFO (t))
1305 /* In this case, it is *type*'s vtable we are modifying. We
1306 start with the approximation that it's vtable is that of
1307 the immediate base class. */
1308 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
1310 /* This is our very own copy of `basetype' to play with.
1311 Later, we will fill in all the virtual functions that
1312 override the virtual functions in these base classes which
1313 are not defined by the current type. */
1314 prepare_fresh_vtable (binfo, t);
1316 base_fndecl = BF_FN (v);
1317 BF_DELTA (v) = this_offset;
1318 BF_VCALL_INDEX (v) = vcall_index;
1321 /* Now assign virtual dispatch information, if unset. We can
1322 dispatch this, through any overridden base function. */
1323 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1325 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1326 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1331 /* Call this function whenever its known that a vtable for T is going
1332 to be needed. It's safe to call it more than once. *HAS_VIRTUAL_P
1333 is initialized to the number of slots that are reserved at the
1334 beginning of the vtable for RTTI information. */
1337 start_vtable (t, has_virtual_p)
1341 if (*has_virtual_p == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1343 /* If we are using thunks, use two slots at the front, one
1344 for the offset pointer, one for the tdesc pointer.
1345 For ARM-style vtables, use the same slot for both. */
1346 if (flag_vtable_thunks)
1353 /* Add a virtual function to all the appropriate vtables for the class
1354 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1355 allocate a new slot in our table. If it is error_mark_node, we
1356 know that no other function from another vtable is overridden by X.
1357 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1358 vtable for the type, and we build upon the NEW_VIRTUALS list
1362 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1363 has_virtual, fndecl, t)
1364 tree *new_virtuals_p;
1365 tree *overridden_virtuals_p;
1368 tree t; /* Structure type. */
1372 /* If this function doesn't override anything from a base class, we
1373 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1374 override something, we keep it around and assign its DECL_VINDEX
1375 later, in modify_all_vtables. */
1376 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1377 /* We've already dealt with this function. */
1380 new_virtual = build_tree_list (integer_zero_node, fndecl);
1381 BF_VCALL_INDEX (new_virtual) = integer_zero_node;
1383 if (DECL_VINDEX (fndecl) == error_mark_node)
1385 /* FNDECL is a new virtual function; it doesn't override any
1386 virtual function in a base class. */
1388 /* We remember that this was the base sub-object for rtti. */
1389 CLASSTYPE_RTTI (t) = t;
1391 start_vtable (t, has_virtual);
1393 /* Now assign virtual dispatch information. */
1394 DECL_VINDEX (fndecl) = build_shared_int_cst ((*has_virtual)++);
1395 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1397 /* Save the state we've computed on the NEW_VIRTUALS list. */
1398 TREE_CHAIN (new_virtual) = *new_virtuals_p;
1399 *new_virtuals_p = new_virtual;
1403 /* FNDECL overrides a function from a base class. */
1404 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1405 *overridden_virtuals_p = new_virtual;
1409 extern struct obstack *current_obstack;
1411 /* Add method METHOD to class TYPE.
1413 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1414 the class type where the method should be added. */
1417 add_method (type, fields, method)
1418 tree type, *fields, method;
1420 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1422 DECL_CONTEXT (method) = type;
1423 DECL_CLASS_CONTEXT (method) = type;
1425 if (fields && *fields)
1426 *fields = build_overload (method, *fields);
1433 if (!CLASSTYPE_METHOD_VEC (type))
1434 /* Make a new method vector. We start with 8 entries. We must
1435 allocate at least two (for constructors and destructors), and
1436 we're going to end up with an assignment operator at some
1439 We could use a TREE_LIST for now, and convert it to a
1440 TREE_VEC in finish_struct, but we would probably waste more
1441 memory making the links in the list than we would by
1442 over-allocating the size of the vector here. Furthermore,
1443 we would complicate all the code that expects this to be a
1445 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1447 method_vec = CLASSTYPE_METHOD_VEC (type);
1448 len = TREE_VEC_LENGTH (method_vec);
1450 if (DECL_NAME (method) == constructor_name (type))
1451 /* A new constructor or destructor. Constructors go in
1452 slot 0; destructors go in slot 1. */
1453 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1456 /* See if we already have an entry with this name. */
1457 for (slot = 2; slot < len; ++slot)
1458 if (!TREE_VEC_ELT (method_vec, slot)
1459 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1461 == DECL_NAME (method)))
1466 /* We need a bigger method vector. */
1467 tree new_vec = make_tree_vec (2 * len);
1468 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1469 (PTR) &TREE_VEC_ELT (new_vec, 0),
1470 len * sizeof (tree));
1472 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1475 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1477 /* Type conversion operators have to come before
1478 ordinary methods; add_conversions depends on this to
1479 speed up looking for conversion operators. So, if
1480 necessary, we slide some of the vector elements up.
1481 In theory, this makes this algorithm O(N^2) but we
1482 don't expect many conversion operators. */
1483 for (slot = 2; slot < len; ++slot)
1485 tree fn = TREE_VEC_ELT (method_vec, slot);
1488 /* There are no more entries in the vector, so we
1489 can insert the new conversion operator here. */
1492 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1493 /* We can insert the new function right at the
1498 if (!TREE_VEC_ELT (method_vec, slot))
1499 /* There is nothing in the Ith slot, so we can avoid
1504 /* We know the last slot in the vector is empty
1505 because we know that at this point there's room
1506 for a new function. */
1507 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1508 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1509 (len - slot - 1) * sizeof (tree));
1510 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1515 if (template_class_depth (type))
1516 /* TYPE is a template class. Don't issue any errors now; wait
1517 until instantiation time to complain. */
1523 /* Check to see if we've already got this method. */
1524 for (fns = TREE_VEC_ELT (method_vec, slot);
1526 fns = OVL_NEXT (fns))
1528 tree fn = OVL_CURRENT (fns);
1530 if (TREE_CODE (fn) != TREE_CODE (method))
1533 if (TREE_CODE (method) != TEMPLATE_DECL)
1535 /* [over.load] Member function declarations with the
1536 same name and the same parameter types cannot be
1537 overloaded if any of them is a static member
1538 function declaration. */
1539 if (DECL_STATIC_FUNCTION_P (fn)
1540 != DECL_STATIC_FUNCTION_P (method))
1542 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1543 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1545 if (! DECL_STATIC_FUNCTION_P (fn))
1546 parms1 = TREE_CHAIN (parms1);
1548 parms2 = TREE_CHAIN (parms2);
1550 if (compparms (parms1, parms2))
1551 cp_error ("`%#D' and `%#D' cannot be overloaded",
1555 /* Since this is an ordinary function in a
1556 non-template class, it's mangled name can be used
1557 as a unique identifier. This technique is only
1558 an optimization; we would get the same results if
1559 we just used decls_match here. */
1560 if (DECL_ASSEMBLER_NAME (fn)
1561 != DECL_ASSEMBLER_NAME (method))
1564 else if (!decls_match (fn, method))
1567 /* There has already been a declaration of this method
1568 or member template. */
1569 cp_error_at ("`%D' has already been declared in `%T'",
1572 /* We don't call duplicate_decls here to merge the
1573 declarations because that will confuse things if the
1574 methods have inline definitions. In particular, we
1575 will crash while processing the definitions. */
1580 /* Actually insert the new method. */
1581 TREE_VEC_ELT (method_vec, slot)
1582 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1584 /* Add the new binding. */
1585 if (!DECL_CONSTRUCTOR_P (method)
1586 && !DECL_DESTRUCTOR_P (method))
1587 push_class_level_binding (DECL_NAME (method),
1588 TREE_VEC_ELT (method_vec, slot));
1592 /* Subroutines of finish_struct. */
1594 /* Look through the list of fields for this struct, deleting
1595 duplicates as we go. This must be recursive to handle
1598 FIELD is the field which may not appear anywhere in FIELDS.
1599 FIELD_PTR, if non-null, is the starting point at which
1600 chained deletions may take place.
1601 The value returned is the first acceptable entry found
1604 Note that anonymous fields which are not of UNION_TYPE are
1605 not duplicates, they are just anonymous fields. This happens
1606 when we have unnamed bitfields, for example. */
1609 delete_duplicate_fields_1 (field, fields)
1614 if (DECL_NAME (field) == 0)
1616 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1619 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1620 fields = delete_duplicate_fields_1 (x, fields);
1625 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1627 if (DECL_NAME (x) == 0)
1629 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1631 TYPE_FIELDS (TREE_TYPE (x))
1632 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1633 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1636 fields = TREE_CHAIN (fields);
1638 TREE_CHAIN (prev) = TREE_CHAIN (x);
1641 else if (TREE_CODE (field) == USING_DECL)
1642 /* A using declaration may is allowed to appear more than
1643 once. We'll prune these from the field list later, and
1644 handle_using_decl will complain about invalid multiple
1647 else if (DECL_NAME (field) == DECL_NAME (x))
1649 if (TREE_CODE (field) == CONST_DECL
1650 && TREE_CODE (x) == CONST_DECL)
1651 cp_error_at ("duplicate enum value `%D'", x);
1652 else if (TREE_CODE (field) == CONST_DECL
1653 || TREE_CODE (x) == CONST_DECL)
1654 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1656 else if (DECL_DECLARES_TYPE_P (field)
1657 && DECL_DECLARES_TYPE_P (x))
1659 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1661 cp_error_at ("duplicate nested type `%D'", x);
1663 else if (DECL_DECLARES_TYPE_P (field)
1664 || DECL_DECLARES_TYPE_P (x))
1666 /* Hide tag decls. */
1667 if ((TREE_CODE (field) == TYPE_DECL
1668 && DECL_ARTIFICIAL (field))
1669 || (TREE_CODE (x) == TYPE_DECL
1670 && DECL_ARTIFICIAL (x)))
1672 cp_error_at ("duplicate field `%D' (as type and non-type)",
1676 cp_error_at ("duplicate member `%D'", x);
1678 fields = TREE_CHAIN (fields);
1680 TREE_CHAIN (prev) = TREE_CHAIN (x);
1688 delete_duplicate_fields (fields)
1692 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1693 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1696 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1697 along the path given by BINFO. Return 1 if change was legit,
1698 otherwise return 0. */
1701 alter_access (t, binfo, fdecl, access)
1707 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1710 if (TREE_VALUE (elem) != access)
1712 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1713 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1715 error ("conflicting access specifications for field `%s', ignored",
1716 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1720 /* They're changing the access to the same thing they changed
1721 it to before. That's OK. */
1727 enforce_access (binfo, fdecl);
1728 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1734 /* Process the USING_DECL, which is a member of T. */
1737 handle_using_decl (using_decl, t)
1741 tree ctype = DECL_INITIAL (using_decl);
1742 tree name = DECL_NAME (using_decl);
1744 = TREE_PRIVATE (using_decl) ? access_private_node
1745 : TREE_PROTECTED (using_decl) ? access_protected_node
1746 : access_public_node;
1748 tree flist = NULL_TREE;
1749 tree fields = TYPE_FIELDS (t);
1750 tree method_vec = CLASSTYPE_METHOD_VEC (t);
1755 binfo = binfo_or_else (ctype, t);
1759 if (name == constructor_name (ctype)
1760 || name == constructor_name_full (ctype))
1762 cp_error_at ("using-declaration for constructor", using_decl);
1766 fdecl = lookup_member (binfo, name, 0, 0);
1770 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1774 /* Functions are represented as TREE_LIST, with the purpose
1775 being the type and the value the functions. Other members
1776 come as themselves. */
1777 if (TREE_CODE (fdecl) == TREE_LIST)
1778 /* Ignore base type this came from. */
1779 fdecl = TREE_VALUE (fdecl);
1781 if (TREE_CODE (fdecl) == OVERLOAD)
1783 /* We later iterate over all functions. */
1785 fdecl = OVL_FUNCTION (flist);
1788 name = DECL_NAME (fdecl);
1789 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1790 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1791 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1794 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1795 cp_error_at (" because of local method `%#D' with same name",
1796 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1800 if (! DECL_LANG_SPECIFIC (fdecl))
1801 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1804 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1805 if (DECL_NAME (tmp) == name)
1807 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1808 cp_error_at (" because of local field `%#D' with same name", tmp);
1812 /* Make type T see field decl FDECL with access ACCESS.*/
1817 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1820 flist = OVL_CHAIN (flist);
1824 alter_access (t, binfo, fdecl, access);
1827 /* Run through the base clases of T, updating
1828 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1829 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1833 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1836 int *cant_have_default_ctor_p;
1837 int *cant_have_const_ctor_p;
1838 int *no_const_asn_ref_p;
1842 int seen_nearly_empty_base_p;
1845 binfos = TYPE_BINFO_BASETYPES (t);
1846 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1847 seen_nearly_empty_base_p = 0;
1849 /* An aggregate cannot have baseclasses. */
1850 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1852 for (i = 0; i < n_baseclasses; ++i)
1857 /* Figure out what base we're looking at. */
1858 base_binfo = TREE_VEC_ELT (binfos, i);
1859 basetype = TREE_TYPE (base_binfo);
1861 /* If the type of basetype is incomplete, then we already
1862 complained about that fact (and we should have fixed it up as
1864 if (TYPE_SIZE (basetype) == 0)
1867 /* The base type is of incomplete type. It is
1868 probably best to pretend that it does not
1870 if (i == n_baseclasses-1)
1871 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1872 TREE_VEC_LENGTH (binfos) -= 1;
1874 for (j = i; j+1 < n_baseclasses; j++)
1875 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1879 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1880 here because the case of virtual functions but non-virtual
1881 dtor is handled in finish_struct_1. */
1882 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1883 && TYPE_HAS_DESTRUCTOR (basetype))
1884 cp_warning ("base class `%#T' has a non-virtual destructor",
1887 /* If the base class doesn't have copy constructors or
1888 assignment operators that take const references, then the
1889 derived class cannot have such a member automatically
1891 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1892 *cant_have_const_ctor_p = 1;
1893 if (TYPE_HAS_ASSIGN_REF (basetype)
1894 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1895 *no_const_asn_ref_p = 1;
1896 /* Similarly, if the base class doesn't have a default
1897 constructor, then the derived class won't have an
1898 automatically generated default constructor. */
1899 if (TYPE_HAS_CONSTRUCTOR (basetype)
1900 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1902 *cant_have_default_ctor_p = 1;
1903 if (! TYPE_HAS_CONSTRUCTOR (t))
1905 cp_pedwarn ("base `%T' with only non-default constructor",
1907 cp_pedwarn ("in class without a constructor");
1911 /* If the base class is not empty or nearly empty, then this
1912 class cannot be nearly empty. */
1913 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1914 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1915 /* And if there is more than one nearly empty base, then the
1916 derived class is not nearly empty either. */
1917 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1918 && seen_nearly_empty_base_p)
1919 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1920 /* If this is the first nearly empty base class, then remember
1922 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1923 seen_nearly_empty_base_p = 1;
1925 /* A lot of properties from the bases also apply to the derived
1927 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1928 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1929 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1930 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1931 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1932 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1933 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1934 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1935 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1937 /* Derived classes can implicitly become COMified if their bases
1939 if (CLASSTYPE_COM_INTERFACE (basetype))
1940 CLASSTYPE_COM_INTERFACE (t) = 1;
1941 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1944 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1946 CLASSTYPE_COM_INTERFACE (t) = 0;
1951 /* Make the Ith baseclass of T its primary base. */
1954 set_primary_base (t, i, has_virtual_p)
1961 CLASSTYPE_VFIELD_PARENT (t) = i;
1962 basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
1963 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1964 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1965 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1966 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1967 *has_virtual_p = CLASSTYPE_VSIZE (basetype);
1970 /* Determine the primary class for T. */
1973 determine_primary_base (t, has_virtual_p)
1977 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1979 /* If there are no baseclasses, there is certainly no primary base. */
1980 if (n_baseclasses == 0)
1985 for (i = 0; i < n_baseclasses; i++)
1987 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1988 tree basetype = BINFO_TYPE (base_binfo);
1990 if (TYPE_CONTAINS_VPTR_P (basetype))
1992 /* Even a virtual baseclass can contain our RTTI
1993 information. But, we prefer a non-virtual polymorphic
1995 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1996 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1998 /* A virtual baseclass can't be the primary base under the
1999 old ABI. And under the new ABI we still prefer a
2000 non-virtual base. */
2001 if (TREE_VIA_VIRTUAL (base_binfo))
2004 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2006 set_primary_base (t, i, has_virtual_p);
2007 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2013 /* Only add unique vfields, and flatten them out as we go. */
2014 for (vfields = CLASSTYPE_VFIELDS (basetype);
2016 vfields = TREE_CHAIN (vfields))
2017 if (VF_BINFO_VALUE (vfields) == NULL_TREE
2018 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
2019 CLASSTYPE_VFIELDS (t)
2020 = tree_cons (base_binfo,
2021 VF_BASETYPE_VALUE (vfields),
2022 CLASSTYPE_VFIELDS (t));
2024 if (*has_virtual_p == 0)
2025 set_primary_base (t, i, has_virtual_p);
2030 if (!TYPE_VFIELD (t))
2031 CLASSTYPE_VFIELD_PARENT (t) = -1;
2033 /* The new ABI allows for the use of a "nearly-empty" virtual base
2034 class as the primary base class if no non-virtual polymorphic
2035 base can be found. */
2036 if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2037 for (i = 0; i < n_baseclasses; ++i)
2039 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
2040 tree basetype = BINFO_TYPE (base_binfo);
2042 if (TREE_VIA_VIRTUAL (base_binfo)
2043 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
2045 set_primary_base (t, i, has_virtual_p);
2046 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2051 /* Mark the primary base classes at this point. */
2052 mark_primary_bases (t);
2055 /* Set memoizing fields and bits of T (and its variants) for later
2059 finish_struct_bits (t)
2062 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2064 /* Fix up variants (if any). */
2065 tree variants = TYPE_NEXT_VARIANT (t);
2068 /* These fields are in the _TYPE part of the node, not in
2069 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2070 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
2071 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
2072 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2073 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
2075 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
2076 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
2077 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2078 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2079 /* Copy whatever these are holding today. */
2080 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2081 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
2082 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2083 TYPE_SIZE (variants) = TYPE_SIZE (t);
2084 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
2085 variants = TYPE_NEXT_VARIANT (variants);
2088 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
2089 /* For a class w/o baseclasses, `finish_struct' has set
2090 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2091 definition). Similarly for a class whose base classes do not
2092 have vtables. When neither of these is true, we might have
2093 removed abstract virtuals (by providing a definition), added
2094 some (by declaring new ones), or redeclared ones from a base
2095 class. We need to recalculate what's really an abstract virtual
2096 at this point (by looking in the vtables). */
2097 get_pure_virtuals (t);
2101 /* Notice whether this class has type conversion functions defined. */
2102 tree binfo = TYPE_BINFO (t);
2103 tree binfos = BINFO_BASETYPES (binfo);
2106 for (i = n_baseclasses-1; i >= 0; i--)
2108 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2110 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
2114 /* If this type has a copy constructor, force its mode to be BLKmode, and
2115 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2116 be passed by invisible reference and prevent it from being returned in
2119 Also do this if the class has BLKmode but can still be returned in
2120 registers, since function_cannot_inline_p won't let us inline
2121 functions returning such a type. This affects the HP-PA. */
2122 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
2123 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
2124 && CLASSTYPE_NON_AGGREGATE (t)))
2127 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2128 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2130 TYPE_MODE (variants) = BLKmode;
2131 TREE_ADDRESSABLE (variants) = 1;
2136 /* Issue warnings about T having private constructors, but no friends,
2139 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2140 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2141 non-private static member functions. */
2144 maybe_warn_about_overly_private_class (t)
2147 int has_member_fn = 0;
2148 int has_nonprivate_method = 0;
2151 if (!warn_ctor_dtor_privacy
2152 /* If the class has friends, those entities might create and
2153 access instances, so we should not warn. */
2154 || (CLASSTYPE_FRIEND_CLASSES (t)
2155 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2156 /* We will have warned when the template was declared; there's
2157 no need to warn on every instantiation. */
2158 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2159 /* There's no reason to even consider warning about this
2163 /* We only issue one warning, if more than one applies, because
2164 otherwise, on code like:
2167 // Oops - forgot `public:'
2173 we warn several times about essentially the same problem. */
2175 /* Check to see if all (non-constructor, non-destructor) member
2176 functions are private. (Since there are no friends or
2177 non-private statics, we can't ever call any of the private member
2179 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2180 /* We're not interested in compiler-generated methods; they don't
2181 provide any way to call private members. */
2182 if (!DECL_ARTIFICIAL (fn))
2184 if (!TREE_PRIVATE (fn))
2186 if (DECL_STATIC_FUNCTION_P (fn))
2187 /* A non-private static member function is just like a
2188 friend; it can create and invoke private member
2189 functions, and be accessed without a class
2193 has_nonprivate_method = 1;
2196 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2200 if (!has_nonprivate_method && has_member_fn)
2202 /* There are no non-private methods, and there's at least one
2203 private member function that isn't a constructor or
2204 destructor. (If all the private members are
2205 constructors/destructors we want to use the code below that
2206 issues error messages specifically referring to
2207 constructors/destructors.) */
2209 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2210 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2211 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2212 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2214 has_nonprivate_method = 1;
2217 if (!has_nonprivate_method)
2219 cp_warning ("all member functions in class `%T' are private", t);
2224 /* Even if some of the member functions are non-private, the class
2225 won't be useful for much if all the constructors or destructors
2226 are private: such an object can never be created or destroyed. */
2227 if (TYPE_HAS_DESTRUCTOR (t))
2229 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2231 if (TREE_PRIVATE (dtor))
2233 cp_warning ("`%#T' only defines a private destructor and has no friends",
2239 if (TYPE_HAS_CONSTRUCTOR (t))
2241 int nonprivate_ctor = 0;
2243 /* If a non-template class does not define a copy
2244 constructor, one is defined for it, enabling it to avoid
2245 this warning. For a template class, this does not
2246 happen, and so we would normally get a warning on:
2248 template <class T> class C { private: C(); };
2250 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2251 complete non-template or fully instantiated classes have this
2253 if (!TYPE_HAS_INIT_REF (t))
2254 nonprivate_ctor = 1;
2256 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2260 tree ctor = OVL_CURRENT (fn);
2261 /* Ideally, we wouldn't count copy constructors (or, in
2262 fact, any constructor that takes an argument of the
2263 class type as a parameter) because such things cannot
2264 be used to construct an instance of the class unless
2265 you already have one. But, for now at least, we're
2267 if (! TREE_PRIVATE (ctor))
2269 nonprivate_ctor = 1;
2274 if (nonprivate_ctor == 0)
2276 cp_warning ("`%#T' only defines private constructors and has no friends",
2283 /* Function to help qsort sort FIELD_DECLs by name order. */
2286 field_decl_cmp (x, y)
2289 if (DECL_NAME (*x) == DECL_NAME (*y))
2290 /* A nontype is "greater" than a type. */
2291 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2292 if (DECL_NAME (*x) == NULL_TREE)
2294 if (DECL_NAME (*y) == NULL_TREE)
2296 if (DECL_NAME (*x) < DECL_NAME (*y))
2301 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2304 method_name_cmp (m1, m2)
2305 const tree *m1, *m2;
2307 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2309 if (*m1 == NULL_TREE)
2311 if (*m2 == NULL_TREE)
2313 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2318 /* Warn about duplicate methods in fn_fields. Also compact method
2319 lists so that lookup can be made faster.
2321 Data Structure: List of method lists. The outer list is a
2322 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2323 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2324 links the entire list of methods for TYPE_METHODS. Friends are
2325 chained in the same way as member functions (? TREE_CHAIN or
2326 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2327 list. That allows them to be quickly deleted, and requires no
2330 If there are any constructors/destructors, they are moved to the
2331 front of the list. This makes pushclass more efficient.
2333 @@ The above comment is obsolete. It mostly describes what add_method
2334 @@ and add_implicitly_declared_members do.
2336 Sort methods that are not special (i.e., constructors, destructors, and
2337 type conversion operators) so that we can find them faster in search. */
2340 finish_struct_methods (t)
2345 tree ctor_name = constructor_name (t);
2348 if (!TYPE_METHODS (t))
2350 /* Clear these for safety; perhaps some parsing error could set
2351 these incorrectly. */
2352 TYPE_HAS_CONSTRUCTOR (t) = 0;
2353 TYPE_HAS_DESTRUCTOR (t) = 0;
2354 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2358 method_vec = CLASSTYPE_METHOD_VEC (t);
2359 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2360 len = TREE_VEC_LENGTH (method_vec);
2362 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2363 and the next few with type conversion operators (if any). */
2364 for (fn_fields = TYPE_METHODS (t); fn_fields;
2365 fn_fields = TREE_CHAIN (fn_fields))
2367 tree fn_name = DECL_NAME (fn_fields);
2369 /* Clear out this flag.
2371 @@ Doug may figure out how to break
2372 @@ this with nested classes and friends. */
2373 DECL_IN_AGGR_P (fn_fields) = 0;
2375 /* Note here that a copy ctor is private, so we don't dare generate
2376 a default copy constructor for a class that has a member
2377 of this type without making sure they have access to it. */
2378 if (fn_name == ctor_name)
2380 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2381 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2383 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2384 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2386 if (TREE_CHAIN (parmtypes) == NULL_TREE
2387 || TREE_CHAIN (parmtypes) == void_list_node
2388 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2390 if (TREE_PROTECTED (fn_fields))
2391 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2392 else if (TREE_PRIVATE (fn_fields))
2393 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2397 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2399 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2401 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2403 if (TREE_PROTECTED (fn_fields))
2404 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2405 else if (TREE_PRIVATE (fn_fields))
2406 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2411 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2412 /* We thought there was a destructor, but there wasn't. Some
2413 parse errors cause this anomalous situation. */
2414 TYPE_HAS_DESTRUCTOR (t) = 0;
2416 /* Issue warnings about private constructors and such. If there are
2417 no methods, then some public defaults are generated. */
2418 maybe_warn_about_overly_private_class (t);
2420 /* Now sort the methods. */
2421 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2423 TREE_VEC_LENGTH (method_vec) = len;
2425 /* The type conversion ops have to live at the front of the vec, so we
2427 for (slot = 2; slot < len; ++slot)
2429 tree fn = TREE_VEC_ELT (method_vec, slot);
2431 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2435 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2436 (int (*)(const void *, const void *))method_name_cmp);
2439 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2442 duplicate_tag_error (t)
2445 cp_error ("redefinition of `%#T'", t);
2446 cp_error_at ("previous definition here", t);
2448 /* Pretend we haven't defined this type. */
2450 /* All of the component_decl's were TREE_CHAINed together in the parser.
2451 finish_struct_methods walks these chains and assembles all methods with
2452 the same base name into DECL_CHAINs. Now we don't need the parser chains
2453 anymore, so we unravel them. */
2455 /* This used to be in finish_struct, but it turns out that the
2456 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2458 if (CLASSTYPE_METHOD_VEC (t))
2460 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2461 int i, len = TREE_VEC_LENGTH (method_vec);
2462 for (i = 0; i < len; i++)
2464 tree unchain = TREE_VEC_ELT (method_vec, i);
2465 while (unchain != NULL_TREE)
2467 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2468 unchain = OVL_NEXT (unchain);
2473 if (TYPE_LANG_SPECIFIC (t))
2475 tree binfo = TYPE_BINFO (t);
2476 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2477 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2478 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2479 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2481 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2482 BINFO_BASETYPES(binfo) = NULL_TREE;
2484 TYPE_BINFO (t) = binfo;
2485 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2486 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2487 TYPE_REDEFINED (t) = 1;
2488 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2489 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2491 TYPE_SIZE (t) = NULL_TREE;
2492 TYPE_MODE (t) = VOIDmode;
2493 TYPE_FIELDS (t) = NULL_TREE;
2494 TYPE_METHODS (t) = NULL_TREE;
2495 TYPE_VFIELD (t) = NULL_TREE;
2496 TYPE_CONTEXT (t) = NULL_TREE;
2497 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2500 /* Make the BINFO's vtablehave N entries, including RTTI entries, but
2501 not including vbase and vcall offsets. Set its type and call the
2502 backend to lay it out. */
2505 layout_vtable_decl (binfo, n)
2512 itype = size_int (n);
2513 itype = size_binop (PLUS_EXPR,
2515 num_extra_vtbl_entries (binfo));
2516 atype = build_cplus_array_type (vtable_entry_type,
2517 build_index_type (itype));
2518 layout_type (atype);
2520 /* We may have to grow the vtable. */
2521 if (!same_type_p (TREE_TYPE (BINFO_VTABLE (binfo)), atype))
2523 TREE_TYPE (BINFO_VTABLE (binfo)) = atype;
2524 DECL_SIZE (BINFO_VTABLE (binfo)) = 0;
2525 layout_decl (BINFO_VTABLE (binfo), 0);
2526 /* At one time the vtable info was grabbed 2 words at a time. This
2527 fails on sparc unless you have 8-byte alignment. (tiemann) */
2528 DECL_ALIGN (BINFO_VTABLE (binfo))
2529 = MAX (TYPE_ALIGN (double_type_node),
2530 DECL_ALIGN (BINFO_VTABLE (binfo)));
2534 /* Returns the number of virtual function table entries (excluding
2535 RTTI information, vbase and vcall offests, etc.) in the vtable for
2539 num_vfun_entries (binfo)
2542 return list_length (skip_rtti_stuff (binfo,
2547 /* Called from num_extra_vtbl_entries via dfs_walk. */
2550 dfs_count_virtuals (binfo, data)
2554 /* Non-primary bases are not interesting; all of the virtual
2555 function table entries have been overridden. */
2556 if (!BINFO_PRIMARY_MARKED_P (binfo))
2557 ((vcall_offset_data *) data)->offsets += num_vfun_entries (binfo);
2562 /* Returns the number of extra entries (at negative indices) required
2563 for BINFO's vtable. */
2566 num_extra_vtbl_entries (binfo)
2572 type = BINFO_TYPE (binfo);
2575 /* There is an entry for the offset to each virtual base. */
2576 if (vbase_offsets_in_vtable_p ())
2577 entries += list_length (CLASSTYPE_VBASECLASSES (type));
2579 /* If this is a virtual base, there are entries for each virtual
2580 function defined in this class or its bases. */
2581 if (vcall_offsets_in_vtable_p () && TREE_VIA_VIRTUAL (binfo))
2583 vcall_offset_data vod;
2589 dfs_vcall_offset_queue_p,
2591 entries += vod.offsets;
2594 return entries ? size_int (entries) : size_zero_node;
2597 /* Returns the offset (in bytes) from the beginning of BINFO's vtable
2598 where the vptr should actually point. */
2601 size_extra_vtbl_entries (binfo)
2606 offset = size_binop (EXACT_DIV_EXPR,
2607 TYPE_SIZE (vtable_entry_type),
2608 size_int (BITS_PER_UNIT));
2609 offset = size_binop (MULT_EXPR, offset, num_extra_vtbl_entries (binfo));
2610 return fold (offset);
2613 /* Construct the initializer for BINFOs virtual function table. BINFO
2614 is part of the hierarchy dominated by T. */
2617 build_vtbl_initializer (binfo, t)
2621 tree v = BINFO_VIRTUALS (binfo);
2622 tree inits = NULL_TREE;
2623 tree type = BINFO_TYPE (binfo);
2625 /* Add entries to the vtable that indicate how to adjust the this
2626 pointer when calling a virtual function in this class. */
2627 inits = build_vcall_offset_vtbl_entries (binfo, t);
2629 /* Add entries to the vtable for offsets to our virtual bases. */
2630 inits = chainon (build_vbase_offset_vtbl_entries (binfo, t),
2633 /* Process the RTTI stuff at the head of the list. If we're not
2634 using vtable thunks, then the RTTI entry is just an ordinary
2635 function, and we can process it just like the other virtual
2636 function entries. */
2637 if (!CLASSTYPE_COM_INTERFACE (type) && flag_vtable_thunks)
2642 /* The first entry is an offset. */
2643 offset = TREE_PURPOSE (v);
2644 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2647 /* Convert the offset to look like a function pointer, so that
2648 we can put it in the vtable. */
2649 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2650 TREE_CONSTANT (init) = 1;
2651 inits = tree_cons (NULL_TREE, init, inits);
2655 if (new_abi_rtti_p ())
2657 tree decl = TREE_VALUE (v);
2660 decl = build_unary_op (ADDR_EXPR, decl, 0);
2662 decl = integer_zero_node;
2663 decl = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
2664 TREE_CONSTANT (decl) = 1;
2665 decl = build_vtable_entry (integer_zero_node, integer_zero_node,
2667 inits = tree_cons (NULL_TREE, decl, inits);
2671 /* In the old abi the second entry (the tdesc pointer) is
2672 just an ordinary function, so it can be dealt with like the
2673 virtual functions. */
2676 /* Go through all the ordinary virtual functions, building up
2685 /* Pull the offset for `this', and the function to call, out of
2687 delta = BF_DELTA (v);
2688 vcall_index = BF_VCALL_INDEX (v);
2690 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2691 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2693 /* You can't call an abstract virtual function; it's abstract.
2694 So, we replace these functions with __pure_virtual. */
2695 if (DECL_PURE_VIRTUAL_P (fn))
2698 /* Package up that information for the vtable. */
2699 init = build_vtable_entry (delta, vcall_index, fn);
2700 /* And add it to the chain of initializers. */
2701 inits = tree_cons (NULL_TREE, init, inits);
2707 /* The initializers were built up in reverse order; straighten them
2709 inits = nreverse (inits);
2710 /* Package all the initializers up as an array initializer. */
2711 return build_nt (CONSTRUCTOR, NULL_TREE, inits);
2714 /* Called from finish_vtbls via dfs_walk. */
2717 dfs_finish_vtbls (binfo, data)
2721 if (!BINFO_PRIMARY_MARKED_P (binfo)
2722 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2723 && BINFO_NEW_VTABLE_MARKED (binfo))
2728 layout_vtable_decl (binfo, list_length (BINFO_VIRTUALS (binfo)));
2729 decl = BINFO_VTABLE (binfo);
2730 context = DECL_CONTEXT (decl);
2731 DECL_CONTEXT (decl) = 0;
2732 DECL_INITIAL (decl) = build_vtbl_initializer (binfo, (tree) data);
2733 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
2734 DECL_CONTEXT (decl) = context;
2737 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2738 SET_BINFO_MARKED (binfo);
2743 /* Create all the necessary vtables for T and its base classes. */
2749 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
2750 dfs_unmarked_real_bases_queue_p, t);
2751 dfs_walk (TYPE_BINFO (t), dfs_unmark,
2752 dfs_marked_real_bases_queue_p, t);
2755 /* True if we should override the given BASE_FNDECL with the given
2759 overrides (fndecl, base_fndecl)
2760 tree fndecl, base_fndecl;
2762 /* Destructors have special names. */
2763 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2764 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2766 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2767 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2769 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2771 tree types, base_types;
2773 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2774 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2776 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2777 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2778 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2779 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2780 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2786 /* Returns the BINFO_OFFSET for the base of BINFO that has the same
2790 get_class_offset_1 (parent, binfo, context, t, fndecl)
2791 tree parent, binfo, context, t, fndecl;
2793 tree binfos = BINFO_BASETYPES (binfo);
2794 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2795 tree rval = NULL_TREE;
2797 if (binfo == parent)
2798 return error_mark_node;
2800 for (i = 0; i < n_baselinks; i++)
2802 tree base_binfo = TREE_VEC_ELT (binfos, i);
2805 if (TREE_VIA_VIRTUAL (base_binfo))
2806 base_binfo = BINFO_FOR_VBASE (BINFO_TYPE (base_binfo), t);
2807 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2808 /* See if we have a new value */
2809 if (nrval && (nrval != error_mark_node || rval==0))
2811 /* Only compare if we have two offsets */
2812 if (rval && rval != error_mark_node
2813 && ! tree_int_cst_equal (nrval, rval))
2815 /* Only give error if the two offsets are different */
2816 error ("every virtual function must have a unique final overrider");
2817 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2818 cp_error (" with virtual `%D' from virtual base class", fndecl);
2824 if (rval && BINFO_TYPE (binfo) == context)
2826 my_friendly_assert (rval == error_mark_node
2827 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2828 rval = BINFO_OFFSET (binfo);
2834 /* Called from get_class_offset via dfs_walk. */
2837 dfs_get_class_offset (binfo, data)
2841 tree list = (tree) data;
2842 tree context = TREE_TYPE (list);
2844 if (same_type_p (BINFO_TYPE (binfo), context))
2846 if (TREE_VALUE (list))
2847 return error_mark_node;
2849 TREE_VALUE (list) = BINFO_OFFSET (binfo);
2852 SET_BINFO_MARKED (binfo);
2857 /* Returns the BINFO_OFFSET for the subobject of BINFO that has the
2858 type given by CONTEXT. */
2861 get_class_offset (context, t, binfo, fndecl)
2862 tree context, t, binfo, fndecl;
2869 return integer_zero_node;
2871 if (BINFO_TYPE (binfo) == context)
2872 return BINFO_OFFSET (binfo);
2874 /* Check less derived binfos first. */
2875 while (BINFO_BASETYPES (binfo)
2876 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2878 tree binfos = BINFO_BASETYPES (binfo);
2879 binfo = TREE_VEC_ELT (binfos, i);
2880 if (BINFO_TYPE (binfo) == context)
2881 return BINFO_OFFSET (binfo);
2884 list = build_tree_list (t, NULL_TREE);
2885 TREE_TYPE (list) = context;
2886 offset = dfs_walk (TYPE_BINFO (t),
2887 dfs_get_class_offset,
2888 dfs_unmarked_real_bases_queue_p,
2890 dfs_walk (TYPE_BINFO (t), dfs_unmark, dfs_marked_real_bases_queue_p, t);
2892 if (offset == error_mark_node)
2894 error ("every virtual function must have a unique final overrider");
2895 cp_error (" found two (or more) `%T' class subobjects in `%T'",
2897 cp_error (" with virtual `%D' from virtual base class", fndecl);
2898 offset = integer_zero_node;
2901 offset = TREE_VALUE (list);
2903 my_friendly_assert (offset != NULL_TREE, 999);
2904 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST, 999);
2909 /* Return the BINFO_VIRTUALS list for BINFO, without the RTTI stuff at
2910 the front. If non-NULL, N is set to the number of entries
2914 skip_rtti_stuff (binfo, t, n)
2917 unsigned HOST_WIDE_INT *n;
2921 if (CLASSTYPE_COM_INTERFACE (t))
2926 virtuals = BINFO_VIRTUALS (binfo);
2929 /* We always reserve a slot for the offset/tdesc entry. */
2932 virtuals = TREE_CHAIN (virtuals);
2934 if (flag_vtable_thunks && virtuals)
2936 /* The second slot is reserved for the tdesc pointer when thunks
2940 virtuals = TREE_CHAIN (virtuals);
2947 modify_one_vtable (binfo, t, fndecl)
2948 tree binfo, t, fndecl;
2951 unsigned HOST_WIDE_INT n;
2953 /* If we're support RTTI then we always need a new vtable to point
2954 to the RTTI information. Under the new ABI we may need a new
2955 vtable to contain vcall and vbase offsets. */
2956 if (flag_rtti || flag_new_abi)
2958 if (binfo == TYPE_BINFO (t))
2959 build_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))), t);
2961 prepare_fresh_vtable (binfo, t);
2963 if (fndecl == NULL_TREE)
2966 for (virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), &n);
2968 virtuals = TREE_CHAIN (virtuals), ++n)
2970 tree current_fndecl = BF_FN (virtuals);
2972 /* We should never have an instance of __pure_virtual on the
2973 BINFO_VIRTUALS list. If we do, then we will never notice
2974 that the function that should have been there instead has
2976 my_friendly_assert (current_fndecl != abort_fndecl,
2979 if (current_fndecl && overrides (fndecl, current_fndecl))
2980 modify_vtable_entry (t, binfo, fndecl, virtuals);
2984 /* Called from modify_all_vtables via dfs_walk. */
2987 dfs_modify_vtables (binfo, data)
2991 if (/* There's no need to modify the vtable for a primary base;
2992 we're not going to use that vtable anyhow. */
2993 !BINFO_PRIMARY_MARKED_P (binfo)
2994 /* Similarly, a base without a vtable needs no modification. */
2995 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2997 tree list = (tree) data;
2998 modify_one_vtable (binfo, TREE_PURPOSE (list), TREE_VALUE (list));
3001 SET_BINFO_MARKED (binfo);
3006 /* Update all of the primary and secondary vtables for T. Create new
3007 vtables as required, and initialize their RTTI information. Each
3008 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
3009 function from a base class; find and modify the appropriate entries
3010 to point to the overriding functions. Returns a list, in
3011 declaration order, of the functions that are overridden in this
3012 class, but do not appear in the primary base class vtable, and
3013 which should therefore be appended to the end of the vtable for T. */
3016 modify_all_vtables (t, has_virtual_p, overridden_virtuals)
3019 tree overridden_virtuals;
3024 binfo = TYPE_BINFO (t);
3026 /* Even if there are no overridden virtuals, we want to go through
3027 the hierarchy updating RTTI information. */
3028 if (!overridden_virtuals && TYPE_CONTAINS_VPTR_P (t) && flag_rtti)
3029 overridden_virtuals = build_tree_list (NULL_TREE, NULL_TREE);
3031 /* Iterate through each of the overriding functions, updating the
3033 for (fns = overridden_virtuals; fns; fns = TREE_CHAIN (fns))
3036 list = build_tree_list (t, TREE_VALUE (fns));
3037 dfs_walk (binfo, dfs_modify_vtables,
3038 dfs_unmarked_real_bases_queue_p, list);
3039 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
3042 /* If we should include overriding functions for secondary vtables
3043 in our primary vtable, add them now. */
3044 if (all_overridden_vfuns_in_vtables_p ())
3046 tree *fnsp = &overridden_virtuals;
3050 tree fn = TREE_VALUE (*fnsp);
3052 if (BINFO_VIRTUALS (binfo)
3053 && !value_member (fn, BINFO_VIRTUALS (binfo)))
3055 /* We know we need a vtable for this class now. */
3056 start_vtable (t, has_virtual_p);
3057 /* Set the vtable index. */
3059 = build_shared_int_cst ((*has_virtual_p)++);
3060 /* We don't need to convert to a base class when calling
3062 DECL_VIRTUAL_CONTEXT (fn) = t;
3063 /* We don't need to adjust the `this' pointer when
3064 calling this function. */
3065 TREE_PURPOSE (*fnsp) = integer_zero_node;
3067 /* This is an overridden function not already in our
3069 fnsp = &TREE_CHAIN (*fnsp);
3072 /* We've already got an entry for this function. Skip
3074 *fnsp = TREE_CHAIN (*fnsp);
3078 overridden_virtuals = NULL_TREE;
3080 return overridden_virtuals;
3083 /* Fixup all the delta entries in this one vtable that need updating. */
3086 dfs_fixup_vtable_deltas (binfo, data)
3091 unsigned HOST_WIDE_INT n;
3092 tree t = (tree) data;
3094 while (BINFO_PRIMARY_MARKED_P (binfo))
3096 binfo = BINFO_INHERITANCE_CHAIN (binfo);
3097 /* If BINFO is virtual then we'll handle this base later. */
3098 if (TREE_VIA_VIRTUAL (binfo))
3102 for (virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), &n);
3104 virtuals = TREE_CHAIN (virtuals), ++n)
3106 tree fndecl = BF_FN (virtuals);
3109 modify_vtable_entry (t, binfo, fndecl, virtuals);
3115 /* Here, we already know that they match in every respect.
3116 All we have to check is where they had their declarations. */
3119 strictly_overrides (fndecl1, fndecl2)
3120 tree fndecl1, fndecl2;
3122 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
3123 DECL_CLASS_CONTEXT (fndecl1),
3125 if (distance == -2 || distance > 0)
3130 /* Merge overrides for one vtable.
3131 If we want to merge in same function, we are fine.
3133 if one has a DECL_CLASS_CONTEXT that is a parent of the
3134 other, than choose the more derived one
3136 potentially ill-formed (see 10.3 [class.virtual])
3137 we have to check later to see if there was an
3138 override in this class. If there was ok, if not
3139 then it is ill-formed. (mrs)
3141 We take special care to reuse a vtable, if we can. */
3144 override_one_vtable (binfo, old, t)
3151 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
3153 /* Either or both of BINFO or OLD might be primary base classes
3154 because merge_overrides is called with a vbase from the class we
3155 are definining and the corresponding vbase from one of its direct
3158 while (BINFO_PRIMARY_MARKED_P (binfo))
3160 binfo = BINFO_INHERITANCE_CHAIN (binfo);
3161 /* If BINFO is virtual, then we'll handle this virtual base when
3163 if (TREE_VIA_VIRTUAL (binfo))
3166 while (BINFO_PRIMARY_MARKED_P (old))
3167 old = BINFO_INHERITANCE_CHAIN (old);
3169 /* If we have already committed to modifying it, then don't try and
3170 reuse another vtable. */
3171 if (BINFO_NEW_VTABLE_MARKED (binfo))
3174 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), NULL);
3175 old_virtuals = skip_rtti_stuff (old, BINFO_TYPE (binfo), NULL);
3176 orig_virtuals = skip_rtti_stuff (orig_binfo, BINFO_TYPE (binfo), NULL);
3178 while (orig_virtuals)
3180 tree fndecl = BF_FN (virtuals);
3181 tree old_fndecl = BF_FN (old_virtuals);
3183 /* First check to see if they are the same. */
3184 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
3186 /* No need to do anything. */
3188 else if (strictly_overrides (fndecl, old_fndecl))
3190 if (choose == UNDECIDED)
3192 else if (choose == REUSE_OLD)
3195 if (! BINFO_NEW_VTABLE_MARKED (binfo))
3197 prepare_fresh_vtable (binfo, t);
3198 override_one_vtable (binfo, old, t);
3203 else if (strictly_overrides (old_fndecl, fndecl))
3205 if (choose == UNDECIDED)
3207 else if (choose == REUSE_NEW)
3210 if (! BINFO_NEW_VTABLE_MARKED (binfo))
3212 prepare_fresh_vtable (binfo, t);
3213 override_one_vtable (binfo, old, t);
3216 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
3218 else if (choose == NEITHER)
3220 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
3226 if (! BINFO_NEW_VTABLE_MARKED (binfo))
3228 prepare_fresh_vtable (binfo, t);
3229 override_one_vtable (binfo, old, t);
3233 /* This MUST be overridden, or the class is ill-formed. */
3234 tree fndecl = BF_FN (virtuals);
3236 fndecl = copy_node (fndecl);
3237 copy_lang_decl (fndecl);
3238 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
3239 /* Make sure we search for it later. */
3240 if (! CLASSTYPE_PURE_VIRTUALS (t))
3241 CLASSTYPE_PURE_VIRTUALS (t) = error_mark_node;
3243 /* We can use integer_zero_node, as we will core dump
3244 if this is used anyway. */
3245 BF_DELTA (virtuals) = integer_zero_node;
3246 BF_FN (virtuals) = fndecl;
3249 virtuals = TREE_CHAIN (virtuals);
3250 old_virtuals = TREE_CHAIN (old_virtuals);
3251 orig_virtuals = TREE_CHAIN (orig_virtuals);
3254 /* Let's reuse the old vtable. */
3255 if (choose == REUSE_OLD)
3257 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
3258 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
3262 /* Merge in overrides for virtual bases.
3263 BINFO is the hierarchy we want to modify, and OLD has the potential
3267 merge_overrides (binfo, old, do_self, t)
3272 tree binfos = BINFO_BASETYPES (binfo);
3273 tree old_binfos = BINFO_BASETYPES (old);
3274 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3276 /* Should we use something besides CLASSTYPE_VFIELDS? */
3277 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
3279 override_one_vtable (binfo, old, t);
3282 for (i = 0; i < n_baselinks; i++)
3284 tree base_binfo = TREE_VEC_ELT (binfos, i);
3285 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
3286 int is_not_base_vtable
3287 = !BINFO_PRIMARY_MARKED_P (base_binfo);
3288 if (! TREE_VIA_VIRTUAL (base_binfo))
3289 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
3293 /* Get the base virtual function declarations in T that are either
3294 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
3295 the overrider/hider. */
3298 get_basefndecls (fndecl, t)
3301 tree methods = TYPE_METHODS (t);
3302 tree base_fndecls = NULL_TREE;
3303 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3304 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3308 if (TREE_CODE (methods) == FUNCTION_DECL
3309 && DECL_VINDEX (methods) != NULL_TREE
3310 && DECL_NAME (fndecl) == DECL_NAME (methods))
3311 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
3313 methods = TREE_CHAIN (methods);
3317 return base_fndecls;
3319 for (i = 0; i < n_baseclasses; i++)
3321 tree base_binfo = TREE_VEC_ELT (binfos, i);
3322 tree basetype = BINFO_TYPE (base_binfo);
3324 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3328 return base_fndecls;
3331 /* Mark the functions that have been hidden with their overriders.
3332 Since we start out with all functions already marked with a hider,
3333 no need to mark functions that are just hidden.
3335 Subroutine of warn_hidden. */
3338 mark_overriders (fndecl, base_fndecls)
3339 tree fndecl, base_fndecls;
3341 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3343 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
3344 TREE_PURPOSE (base_fndecls) = fndecl;
3348 /* If this declaration supersedes the declaration of
3349 a method declared virtual in the base class, then
3350 mark this field as being virtual as well. */
3353 check_for_override (decl, ctype)
3356 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
3357 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3358 int virtualp = DECL_VIRTUAL_P (decl);
3359 int found_overriden_fn = 0;
3361 for (i = 0; i < n_baselinks; i++)
3363 tree base_binfo = TREE_VEC_ELT (binfos, i);
3364 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
3366 tree tmp = get_matching_virtual
3368 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
3370 if (tmp && !found_overriden_fn)
3372 /* If this function overrides some virtual in some base
3373 class, then the function itself is also necessarily
3374 virtual, even if the user didn't explicitly say so. */
3375 DECL_VIRTUAL_P (decl) = 1;
3377 /* The TMP we really want is the one from the deepest
3378 baseclass on this path, taking care not to
3379 duplicate if we have already found it (via another
3380 path to its virtual baseclass. */
3381 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
3383 cp_error_at ("`static %#D' cannot be declared", decl);
3384 cp_error_at (" since `virtual %#D' declared in base class",
3390 /* Set DECL_VINDEX to a value that is neither an
3391 INTEGER_CST nor the error_mark_node so that
3392 add_virtual_function will realize this is an
3393 overridden function. */
3395 = tree_cons (tmp, NULL_TREE, DECL_VINDEX (decl));
3397 /* We now know that DECL overrides something,
3398 which is all that is important. But, we must
3399 continue to iterate through all the base-classes
3400 in order to allow get_matching_virtual to check for
3401 various illegal overrides. */
3402 found_overriden_fn = 1;
3408 if (DECL_VINDEX (decl) == NULL_TREE)
3409 DECL_VINDEX (decl) = error_mark_node;
3410 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3414 /* Warn about hidden virtual functions that are not overridden in t.
3415 We know that constructors and destructors don't apply. */
3421 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3422 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3425 /* We go through each separately named virtual function. */
3426 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3428 tree fns = TREE_VEC_ELT (method_vec, i);
3429 tree fndecl = NULL_TREE;
3431 tree base_fndecls = NULL_TREE;
3432 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3433 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3435 /* First see if we have any virtual functions in this batch. */
3436 for (; fns; fns = OVL_NEXT (fns))
3438 fndecl = OVL_CURRENT (fns);
3439 if (DECL_VINDEX (fndecl))
3443 if (fns == NULL_TREE)
3446 /* First we get a list of all possible functions that might be
3447 hidden from each base class. */
3448 for (i = 0; i < n_baseclasses; i++)
3450 tree base_binfo = TREE_VEC_ELT (binfos, i);
3451 tree basetype = BINFO_TYPE (base_binfo);
3453 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3457 fns = OVL_NEXT (fns);
3459 /* ...then mark up all the base functions with overriders, preferring
3460 overriders to hiders. */
3462 for (; fns; fns = OVL_NEXT (fns))
3464 fndecl = OVL_CURRENT (fns);
3465 if (DECL_VINDEX (fndecl))
3466 mark_overriders (fndecl, base_fndecls);
3469 /* Now give a warning for all base functions without overriders,
3470 as they are hidden. */
3471 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3473 if (! overrides (TREE_PURPOSE (base_fndecls),
3474 TREE_VALUE (base_fndecls)))
3476 /* Here we know it is a hider, and no overrider exists. */
3477 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3478 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3484 /* Check for things that are invalid. There are probably plenty of other
3485 things we should check for also. */
3488 finish_struct_anon (t)
3493 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3495 if (TREE_STATIC (field))
3497 if (TREE_CODE (field) != FIELD_DECL)
3500 if (DECL_NAME (field) == NULL_TREE
3501 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3503 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3504 for (; elt; elt = TREE_CHAIN (elt))
3506 if (DECL_ARTIFICIAL (elt))
3509 if (DECL_NAME (elt) == constructor_name (t))
3510 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3513 if (TREE_CODE (elt) != FIELD_DECL)
3515 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3520 if (TREE_PRIVATE (elt))
3521 cp_pedwarn_at ("private member `%#D' in anonymous union",
3523 else if (TREE_PROTECTED (elt))
3524 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3527 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3528 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3534 extern int interface_only, interface_unknown;
3536 /* Create default constructors, assignment operators, and so forth for
3537 the type indicated by T, if they are needed.
3538 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3539 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3540 cannot have a default constructor, copy constructor taking a const
3541 reference argument, or an assignment operator, respectively. If a
3542 virtual destructor is created, its DECL is returned; otherwise the
3543 return value is NULL_TREE. */
3546 add_implicitly_declared_members (t, cant_have_default_ctor,
3547 cant_have_const_cctor,
3548 cant_have_assignment)
3550 int cant_have_default_ctor;
3551 int cant_have_const_cctor;
3552 int cant_have_assignment;
3555 tree implicit_fns = NULL_TREE;
3556 tree name = TYPE_IDENTIFIER (t);
3557 tree virtual_dtor = NULL_TREE;
3561 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3563 default_fn = cons_up_default_function (t, name, 0);
3564 check_for_override (default_fn, t);
3566 /* If we couldn't make it work, then pretend we didn't need it. */
3567 if (default_fn == void_type_node)
3568 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3571 TREE_CHAIN (default_fn) = implicit_fns;
3572 implicit_fns = default_fn;
3574 if (DECL_VINDEX (default_fn))
3575 virtual_dtor = default_fn;
3578 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3580 /* Default constructor. */
3581 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3583 default_fn = cons_up_default_function (t, name, 2);
3584 TREE_CHAIN (default_fn) = implicit_fns;
3585 implicit_fns = default_fn;
3588 /* Copy constructor. */
3589 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3591 /* ARM 12.18: You get either X(X&) or X(const X&), but
3593 default_fn = cons_up_default_function (t, name,
3594 3 + cant_have_const_cctor);
3595 TREE_CHAIN (default_fn) = implicit_fns;
3596 implicit_fns = default_fn;
3599 /* Assignment operator. */
3600 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3602 default_fn = cons_up_default_function (t, name,
3603 5 + cant_have_assignment);
3604 TREE_CHAIN (default_fn) = implicit_fns;
3605 implicit_fns = default_fn;
3608 /* Now, hook all of the new functions on to TYPE_METHODS,
3609 and add them to the CLASSTYPE_METHOD_VEC. */
3610 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3611 add_method (t, 0, *f);
3612 *f = TYPE_METHODS (t);
3613 TYPE_METHODS (t) = implicit_fns;
3615 return virtual_dtor;
3618 /* Subroutine of finish_struct_1. Recursively count the number of fields
3619 in TYPE, including anonymous union members. */
3622 count_fields (fields)
3627 for (x = fields; x; x = TREE_CHAIN (x))
3629 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3630 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3637 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3638 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3641 add_fields_to_vec (fields, field_vec, idx)
3642 tree fields, field_vec;
3646 for (x = fields; x; x = TREE_CHAIN (x))
3648 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3649 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3651 TREE_VEC_ELT (field_vec, idx++) = x;
3656 /* FIELD is a bit-field. We are finishing the processing for its
3657 enclosing type. Issue any appropriate messages and set appropriate
3661 check_bitfield_decl (field)
3664 tree type = TREE_TYPE (field);
3666 /* Invalid bit-field size done by grokfield. */
3667 /* Detect invalid bit-field type. Simply checking if TYPE is
3668 integral is insufficient, as that is the array core of the field
3669 type. If TREE_TYPE (field) is integral, then TYPE must be the same. */
3670 if (DECL_INITIAL (field)
3671 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3673 cp_error_at ("bit-field `%#D' with non-integral type", field);
3674 DECL_INITIAL (field) = NULL;
3677 /* Detect and ignore out of range field width. */
3678 if (DECL_INITIAL (field))
3680 tree w = DECL_INITIAL (field);
3681 register int width = 0;
3683 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3686 /* detect invalid field size. */
3687 if (TREE_CODE (w) == CONST_DECL)
3688 w = DECL_INITIAL (w);
3689 else if (TREE_READONLY_DECL_P (w))
3690 w = decl_constant_value (w);
3692 if (TREE_CODE (w) != INTEGER_CST)
3694 cp_error_at ("bit-field `%D' width not an integer constant",
3696 DECL_INITIAL (field) = NULL_TREE;
3698 else if (width = TREE_INT_CST_LOW (w),
3701 DECL_INITIAL (field) = NULL;
3702 cp_error_at ("negative width in bit-field `%D'", field);
3704 else if (width == 0 && DECL_NAME (field) != 0)
3706 DECL_INITIAL (field) = NULL;
3707 cp_error_at ("zero width for bit-field `%D'", field);
3710 > TYPE_PRECISION (long_long_unsigned_type_node))
3712 /* The backend will dump if you try to use something too
3714 DECL_INITIAL (field) = NULL;
3715 sorry ("bit-fields larger than %d bits",
3716 TYPE_PRECISION (long_long_unsigned_type_node));
3717 cp_error_at (" in declaration of `%D'", field);
3719 else if (width > TYPE_PRECISION (type)
3720 && TREE_CODE (type) != ENUMERAL_TYPE
3721 && TREE_CODE (type) != BOOLEAN_TYPE)
3722 cp_warning_at ("width of `%D' exceeds its type", field);
3723 else if (TREE_CODE (type) == ENUMERAL_TYPE
3724 && ((min_precision (TYPE_MIN_VALUE (type),
3725 TREE_UNSIGNED (type)) > width)
3726 || (min_precision (TYPE_MAX_VALUE (type),
3727 TREE_UNSIGNED (type)) > width)))
3728 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3731 if (DECL_INITIAL (field))
3733 DECL_INITIAL (field) = NULL_TREE;
3734 DECL_FIELD_SIZE (field) = width;
3735 DECL_BIT_FIELD (field) = 1;
3739 #ifdef EMPTY_FIELD_BOUNDARY
3740 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3741 EMPTY_FIELD_BOUNDARY);
3743 #ifdef PCC_BITFIELD_TYPE_MATTERS
3744 if (PCC_BITFIELD_TYPE_MATTERS)
3745 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3752 /* Non-bit-fields are aligned for their type. */
3753 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3756 /* FIELD is a non bit-field. We are finishing the processing for its
3757 enclosing type T. Issue any appropriate messages and set appropriate
3761 check_field_decl (field, t, cant_have_const_ctor,
3762 cant_have_default_ctor, no_const_asn_ref,
3763 any_default_members)
3766 int *cant_have_const_ctor;
3767 int *cant_have_default_ctor;
3768 int *no_const_asn_ref;
3769 int *any_default_members;
3771 tree type = strip_array_types (TREE_TYPE (field));
3773 /* An anonymous union cannot contain any fields which would change
3774 the settings of CANT_HAVE_CONST_CTOR and friends. */
3775 if (ANON_UNION_TYPE_P (type))
3777 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3778 structs. So, we recurse through their fields here. */
3779 else if (ANON_AGGR_TYPE_P (type))
3783 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3784 if (TREE_CODE (field) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3785 check_field_decl (fields, t, cant_have_const_ctor,
3786 cant_have_default_ctor, no_const_asn_ref,
3787 any_default_members);
3789 /* Check members with class type for constructors, destructors,
3791 else if (CLASS_TYPE_P (type))
3793 /* Never let anything with uninheritable virtuals
3794 make it through without complaint. */
3795 abstract_virtuals_error (field, type);
3797 if (TREE_CODE (t) == UNION_TYPE)
3799 if (TYPE_NEEDS_CONSTRUCTING (type))
3800 cp_error_at ("member `%#D' with constructor not allowed in union",
3802 if (TYPE_NEEDS_DESTRUCTOR (type))
3803 cp_error_at ("member `%#D' with destructor not allowed in union",
3805 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3806 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3811 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3812 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3813 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3814 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3817 if (!TYPE_HAS_CONST_INIT_REF (type))
3818 *cant_have_const_ctor = 1;
3820 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3821 *no_const_asn_ref = 1;
3823 if (TYPE_HAS_CONSTRUCTOR (type)
3824 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3825 *cant_have_default_ctor = 1;
3827 if (DECL_INITIAL (field) != NULL_TREE)
3829 /* `build_class_init_list' does not recognize
3831 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3832 cp_error_at ("multiple fields in union `%T' initialized");
3833 *any_default_members = 1;
3836 /* Non-bit-fields are aligned for their type, except packed fields
3837 which require only BITS_PER_UNIT alignment. */
3838 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3839 (DECL_PACKED (field)
3841 : TYPE_ALIGN (TREE_TYPE (field))));
3844 /* Check the data members (both static and non-static), class-scoped
3845 typedefs, etc., appearing in the declaration of T. Issue
3846 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3847 declaration order) of access declarations; each TREE_VALUE in this
3848 list is a USING_DECL.
3850 In addition, set the following flags:
3853 The class is empty, i.e., contains no non-static data members.
3855 CANT_HAVE_DEFAULT_CTOR_P
3856 This class cannot have an implicitly generated default
3859 CANT_HAVE_CONST_CTOR_P
3860 This class cannot have an implicitly generated copy constructor
3861 taking a const reference.
3863 CANT_HAVE_CONST_ASN_REF
3864 This class cannot have an implicitly generated assignment
3865 operator taking a const reference.
3867 All of these flags should be initialized before calling this
3870 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3871 fields can be added by adding to this chain. */
3874 check_field_decls (t, access_decls, empty_p,
3875 cant_have_default_ctor_p, cant_have_const_ctor_p,
3880 int *cant_have_default_ctor_p;
3881 int *cant_have_const_ctor_p;
3882 int *no_const_asn_ref_p;
3887 int any_default_members;
3889 /* First, delete any duplicate fields. */
3890 delete_duplicate_fields (TYPE_FIELDS (t));
3892 /* Assume there are no access declarations. */
3893 *access_decls = NULL_TREE;
3894 /* Assume this class has no pointer members. */
3896 /* Assume none of the members of this class have default
3898 any_default_members = 0;
3900 for (field = &TYPE_FIELDS (t); *field; field = next)
3903 tree type = TREE_TYPE (x);
3905 GNU_xref_member (current_class_name, x);
3907 next = &TREE_CHAIN (x);
3909 if (TREE_CODE (x) == FIELD_DECL)
3911 DECL_PACKED (x) |= TYPE_PACKED (t);
3913 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3914 /* We don't treat zero-width bitfields as making a class
3919 /* The class is non-empty. */
3921 /* The class is not even nearly empty. */
3922 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3926 if (TREE_CODE (x) == USING_DECL)
3928 /* Prune the access declaration from the list of fields. */
3929 *field = TREE_CHAIN (x);
3931 /* Save the access declarations for our caller. */
3932 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3934 /* Since we've reset *FIELD there's no reason to skip to the
3940 if (TREE_CODE (x) == TYPE_DECL
3941 || TREE_CODE (x) == TEMPLATE_DECL)
3944 /* If we've gotten this far, it's a data member, possibly static,
3945 or an enumerator. */
3947 DECL_FIELD_CONTEXT (x) = t;
3949 /* ``A local class cannot have static data members.'' ARM 9.4 */
3950 if (current_function_decl && TREE_STATIC (x))
3951 cp_error_at ("field `%D' in local class cannot be static", x);
3953 /* Perform error checking that did not get done in
3955 if (TREE_CODE (type) == FUNCTION_TYPE)
3957 cp_error_at ("field `%D' invalidly declared function type",
3959 type = build_pointer_type (type);
3960 TREE_TYPE (x) = type;
3962 else if (TREE_CODE (type) == METHOD_TYPE)
3964 cp_error_at ("field `%D' invalidly declared method type", x);
3965 type = build_pointer_type (type);
3966 TREE_TYPE (x) = type;
3968 else if (TREE_CODE (type) == OFFSET_TYPE)
3970 cp_error_at ("field `%D' invalidly declared offset type", x);
3971 type = build_pointer_type (type);
3972 TREE_TYPE (x) = type;
3975 if (type == error_mark_node)
3978 DECL_SAVED_INSNS (x) = 0;
3979 DECL_FIELD_SIZE (x) = 0;
3981 /* When this goes into scope, it will be a non-local reference. */
3982 DECL_NONLOCAL (x) = 1;
3984 if (TREE_CODE (x) == CONST_DECL)
3987 if (TREE_CODE (x) == VAR_DECL)
3989 if (TREE_CODE (t) == UNION_TYPE)
3990 /* Unions cannot have static members. */
3991 cp_error_at ("field `%D' declared static in union", x);
3996 /* Now it can only be a FIELD_DECL. */
3998 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3999 CLASSTYPE_NON_AGGREGATE (t) = 1;
4001 /* If this is of reference type, check if it needs an init.
4002 Also do a little ANSI jig if necessary. */
4003 if (TREE_CODE (type) == REFERENCE_TYPE)
4005 CLASSTYPE_NON_POD_P (t) = 1;
4006 if (DECL_INITIAL (x) == NULL_TREE)
4007 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
4009 /* ARM $12.6.2: [A member initializer list] (or, for an
4010 aggregate, initialization by a brace-enclosed list) is the
4011 only way to initialize nonstatic const and reference
4013 *cant_have_default_ctor_p = 1;
4014 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
4016 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
4019 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
4021 cp_warning_at ("non-static reference in class without a constructor", x);
4025 type = strip_array_types (type);
4027 if (TREE_CODE (type) == POINTER_TYPE)
4030 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
4031 CLASSTYPE_HAS_MUTABLE (t) = 1;
4033 if (! pod_type_p (type)
4034 /* For some reason, pointers to members are POD types themselves,
4035 but are not allowed in POD structs. Silly. */
4036 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
4037 CLASSTYPE_NON_POD_P (t) = 1;
4039 /* If any field is const, the structure type is pseudo-const. */
4040 if (CP_TYPE_CONST_P (type))
4042 C_TYPE_FIELDS_READONLY (t) = 1;
4043 if (DECL_INITIAL (x) == NULL_TREE)
4044 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
4046 /* ARM $12.6.2: [A member initializer list] (or, for an
4047 aggregate, initialization by a brace-enclosed list) is the
4048 only way to initialize nonstatic const and reference
4050 *cant_have_default_ctor_p = 1;
4051 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
4053 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
4056 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
4058 cp_warning_at ("non-static const member in class without a constructor", x);
4061 /* A field that is pseudo-const makes the structure likewise. */
4062 else if (IS_AGGR_TYPE (type))
4064 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4065 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4066 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
4069 /* We set DECL_C_BIT_FIELD in grokbitfield.
4070 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4071 if (DECL_C_BIT_FIELD (x))
4072 check_bitfield_decl (x);
4074 check_field_decl (x, t,
4075 cant_have_const_ctor_p,
4076 cant_have_default_ctor_p,
4078 &any_default_members);
4081 /* Effective C++ rule 11. */
4082 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
4083 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
4085 cp_warning ("`%#T' has pointer data members", t);
4087 if (! TYPE_HAS_INIT_REF (t))
4089 cp_warning (" but does not override `%T(const %T&)'", t, t);
4090 if (! TYPE_HAS_ASSIGN_REF (t))
4091 cp_warning (" or `operator=(const %T&)'", t);
4093 else if (! TYPE_HAS_ASSIGN_REF (t))
4094 cp_warning (" but does not override `operator=(const %T&)'", t);
4098 /* Check anonymous struct/anonymous union fields. */
4099 finish_struct_anon (t);
4101 /* We've built up the list of access declarations in reverse order.
4103 *access_decls = nreverse (*access_decls);
4106 /* Return a FIELD_DECL for a pointer-to-virtual-table or
4107 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
4108 field are as indicated. The CLASS_TYPE in which this field occurs
4109 is also indicated. *EMPTY_P is set to a non-zero value by this
4110 function to indicate that a class containing this field is
4114 build_vtbl_or_vbase_field (name, assembler_name, type, class_type,
4117 tree assembler_name;
4124 /* This class is non-empty. */
4127 /* Build the FIELD_DECL. */
4128 field = build_lang_decl (FIELD_DECL, name, type);
4129 DECL_ASSEMBLER_NAME (field) = assembler_name;
4130 DECL_VIRTUAL_P (field) = 1;
4131 DECL_ARTIFICIAL (field) = 1;
4132 DECL_FIELD_CONTEXT (field) = class_type;
4133 DECL_CLASS_CONTEXT (field) = class_type;
4134 DECL_FCONTEXT (field) = class_type;
4135 DECL_SAVED_INSNS (field) = 0;
4136 DECL_FIELD_SIZE (field) = 0;
4137 DECL_ALIGN (field) = TYPE_ALIGN (type);
4143 /* If the empty base field in DECL overlaps with a base of the same type in
4144 NEWDECL, which is either another base field or the first data field of
4145 the class, pad the base just before NEWDECL and return 1. Otherwise,
4149 avoid_overlap (decl, newdecl, empty_p)
4155 if (newdecl == NULL_TREE
4156 || ! types_overlap_p (TREE_TYPE (decl), TREE_TYPE (newdecl)))
4159 for (field = decl; TREE_CHAIN (field) && TREE_CHAIN (field) != newdecl;
4160 field = TREE_CHAIN (field))
4163 DECL_SIZE (field) = integer_one_node;
4164 /* The containing class cannot be empty; this field takes up space. */
4170 /* Build a FIELD_DECL for the base given by BINFO in T. If the new
4171 object is non-empty, clear *EMPTY_P. Otherwise, set *SAW_EMPTY_P.
4172 *BASE_ALIGN is a running maximum of the alignments of any base
4176 build_base_field (t, binfo, empty_p, saw_empty_p, base_align)
4181 unsigned int *base_align;
4183 tree basetype = BINFO_TYPE (binfo);
4186 if (TYPE_SIZE (basetype) == 0)
4187 /* This error is now reported in xref_tag, thus giving better
4188 location information. */
4191 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
4192 DECL_ARTIFICIAL (decl) = 1;
4193 DECL_FIELD_CONTEXT (decl) = DECL_CLASS_CONTEXT (decl) = t;
4194 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4195 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4197 if (flag_new_abi && DECL_SIZE (decl) == integer_zero_node)
4203 /* The containing class is non-empty because it has a non-empty base
4209 /* Brain damage for backwards compatibility. For no good
4210 reason, the old layout_basetypes made every base at least
4211 as large as the alignment for the bases up to that point,
4212 gratuitously wasting space. So we do the same thing
4214 *base_align = MAX (*base_align, DECL_ALIGN (decl));
4216 = size_int (MAX (TREE_INT_CST_LOW (DECL_SIZE (decl)),
4217 (int) (*base_align)));
4223 /* Returns a list of fields to stand in for the base class subobjects
4224 of REC. These fields are later removed by layout_basetypes. */
4227 build_base_fields (rec, empty_p)
4231 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4233 tree base_decls = NULL_TREE;
4234 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
4235 tree decl, nextdecl;
4236 int i, saw_empty = 0;
4237 unsigned int base_align = 0;
4239 /* Under the new ABI, the primary base class is always allocated
4241 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
4245 primary_base = CLASSTYPE_PRIMARY_BINFO (rec);
4246 base_decls = chainon (build_base_field (rec,
4254 /* Now allocate the rest of the bases. */
4255 for (i = 0; i < n_baseclasses; ++i)
4259 /* Under the new ABI, the primary base was already allocated
4260 above, so we don't need to allocate it again here. */
4261 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
4264 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4266 /* A primary virtual base class is allocated just like any other
4267 base class, but a non-primary virtual base is allocated
4268 later, in layout_basetypes. */
4269 if (TREE_VIA_VIRTUAL (base_binfo)
4270 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4273 base_decls = chainon (build_base_field (rec, base_binfo,
4280 /* Reverse the list of fields so we allocate the bases in the proper
4282 base_decls = nreverse (base_decls);
4284 /* In the presence of empty base classes, we run the risk of allocating
4285 two objects of the same class on top of one another. Avoid that. */
4286 if (flag_new_abi && saw_empty)
4287 for (decl = base_decls; decl; decl = TREE_CHAIN (decl))
4289 if (DECL_SIZE (decl) == integer_zero_node)
4291 /* First step through the following bases until we find
4292 an overlap or a non-empty base. */
4293 for (nextdecl = TREE_CHAIN (decl); nextdecl;
4294 nextdecl = TREE_CHAIN (nextdecl))
4296 if (avoid_overlap (decl, nextdecl, empty_p)
4297 || DECL_SIZE (nextdecl) != integer_zero_node)
4301 /* If we're still looking, also check against the first
4303 for (nextdecl = TYPE_FIELDS (rec);
4304 nextdecl && TREE_CODE (nextdecl) != FIELD_DECL;
4305 nextdecl = TREE_CHAIN (nextdecl))
4307 avoid_overlap (decl, nextdecl, empty_p);
4315 /* Go through the TYPE_METHODS of T issuing any appropriate
4316 diagnostics, figuring out which methods override which other
4317 methods, and so forth. */
4325 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4327 GNU_xref_member (current_class_name, x);
4329 /* If this was an evil function, don't keep it in class. */
4330 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4333 /* Do both of these, even though they're in the same union;
4334 if the insn `r' member and the size `i' member are
4335 different sizes, as on the alpha, the larger of the two
4336 will end up with garbage in it. */
4337 DECL_SAVED_INSNS (x) = 0;
4338 DECL_FIELD_SIZE (x) = 0;
4340 check_for_override (x, t);
4341 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4342 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4344 /* The name of the field is the original field name
4345 Save this in auxiliary field for later overloading. */
4346 if (DECL_VINDEX (x))
4348 TYPE_POLYMORPHIC_P (t) = 1;
4349 if (DECL_PURE_VIRTUAL_P (x))
4350 CLASSTYPE_PURE_VIRTUALS (t)
4351 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4356 /* Remove all zero-width bit-fields from T. */
4359 remove_zero_width_bit_fields (t)
4364 fieldsp = &TYPE_FIELDS (t);
4367 if (TREE_CODE (*fieldsp) == FIELD_DECL
4368 && DECL_C_BIT_FIELD (*fieldsp)
4369 && DECL_INITIAL (*fieldsp))
4370 *fieldsp = TREE_CHAIN (*fieldsp);
4372 fieldsp = &TREE_CHAIN (*fieldsp);
4376 /* Check the validity of the bases and members declared in T. Add any
4377 implicitly-generated functions (like copy-constructors and
4378 assignment operators). Compute various flag bits (like
4379 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4380 level: i.e., independently of the ABI in use. */
4383 check_bases_and_members (t, empty_p)
4387 /* Nonzero if we are not allowed to generate a default constructor
4389 int cant_have_default_ctor;
4390 /* Nonzero if the implicitly generated copy constructor should take
4391 a non-const reference argument. */
4392 int cant_have_const_ctor;
4393 /* Nonzero if the the implicitly generated assignment operator
4394 should take a non-const reference argument. */
4395 int no_const_asn_ref;
4398 /* By default, we use const reference arguments and generate default
4400 cant_have_default_ctor = 0;
4401 cant_have_const_ctor = 0;
4402 no_const_asn_ref = 0;
4404 /* Assume that the class is nearly empty; we'll clear this flag if
4405 it turns out not to be nearly empty. */
4406 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4408 /* Check all the base-classes. */
4409 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4412 /* Check all the data member declarations. */
4413 check_field_decls (t, &access_decls, empty_p,
4414 &cant_have_default_ctor,
4415 &cant_have_const_ctor,
4418 /* Check all the method declarations. */
4421 /* A nearly-empty class has to be vptr-containing; a nearly empty
4422 class contains just a vptr. */
4423 if (!TYPE_CONTAINS_VPTR_P (t))
4424 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4426 /* Do some bookkeeping that will guide the generation of implicitly
4427 declared member functions. */
4428 TYPE_HAS_COMPLEX_INIT_REF (t)
4429 |= (TYPE_HAS_INIT_REF (t)
4430 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4431 || TYPE_POLYMORPHIC_P (t));
4432 TYPE_NEEDS_CONSTRUCTING (t)
4433 |= (TYPE_HAS_CONSTRUCTOR (t)
4434 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4435 || TYPE_POLYMORPHIC_P (t));
4436 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4437 || TYPE_POLYMORPHIC_P (t));
4438 CLASSTYPE_NON_POD_P (t)
4439 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4440 || TYPE_HAS_ASSIGN_REF (t));
4441 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4442 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4443 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4445 /* Synthesize any needed methods. Note that methods will be synthesized
4446 for anonymous unions; grok_x_components undoes that. */
4447 add_implicitly_declared_members (t, cant_have_default_ctor,
4448 cant_have_const_ctor,
4451 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4452 finish_struct_methods (t);
4454 /* Process the access-declarations. We wait until now to do this
4455 because handle_using_decls requires that the CLASSTYPE_METHOD_VEC
4456 be set up correctly. */
4457 while (access_decls)
4459 handle_using_decl (TREE_VALUE (access_decls), t);
4460 access_decls = TREE_CHAIN (access_decls);
4464 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4465 accordingly, and, if necessary, add the TYPE_VFIELD to the
4466 TYPE_FIELDS list. */
4469 create_vtable_ptr (t, empty_p, has_virtual_p,
4470 new_virtuals_p, overridden_virtuals_p)
4474 tree *new_virtuals_p;
4475 tree *overridden_virtuals_p;
4479 /* Loop over the virtual functions, adding them to our various
4481 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4482 if (DECL_VINDEX (fn))
4483 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4484 has_virtual_p, fn, t);
4486 /* Even if there weren't any new virtual functions, we might need a
4487 new virtual function table if we're supposed to include vptrs in
4488 all classes that need them. */
4489 if (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())
4490 start_vtable (t, has_virtual_p);
4492 /* If we couldn't find an appropriate base class, create a new field
4494 if (*has_virtual_p && !TYPE_VFIELD (t))
4496 /* We build this decl with vtbl_ptr_type_node, which is a
4497 `vtable_entry_type*'. It might seem more precise to use
4498 `vtable_entry_type (*)[N]' where N is the number of firtual
4499 functions. However, that would require the vtable pointer in
4500 base classes to have a different type than the vtable pointer
4501 in derived classes. We could make that happen, but that
4502 still wouldn't solve all the problems. In particular, the
4503 type-based alias analysis code would decide that assignments
4504 to the base class vtable pointer can't alias assignments to
4505 the derived class vtable pointer, since they have different
4506 types. Thus, in an derived class destructor, where the base
4507 class constructor was inlined, we could generate bad code for
4508 setting up the vtable pointer.
4510 Therefore, we use one type for all vtable pointers. We still
4511 use a type-correct type; it's just doesn't indicate the array
4512 bounds. That's better than using `void*' or some such; it's
4513 cleaner, and it let's the alias analysis code know that these
4514 stores cannot alias stores to void*! */
4516 = build_vtbl_or_vbase_field (get_vfield_name (t),
4517 get_identifier (VFIELD_BASE),
4522 /* Add the new field to the list of fields in this class. */
4524 /* In the old ABI, the vtable pointer goes at the end of the
4526 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), TYPE_VFIELD (t));
4529 /* But in the new ABI, the vtable pointer is the first thing
4531 TYPE_FIELDS (t) = chainon (TYPE_VFIELD (t), TYPE_FIELDS (t));
4532 /* If there were any baseclasses, they can't possibly be at
4533 offset zero any more, because that's where the vtable
4534 pointer is. So, converting to a base class is going to
4536 if (CLASSTYPE_N_BASECLASSES (t))
4537 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4540 /* We can't yet add this new field to the list of all virtual
4541 function table pointers in this class. The
4542 modify_all_vtables function depends on this not being done.
4543 So, it is done later, in finish_struct_1. */
4547 /* Fixup the inline function given by INFO now that the class is
4551 fixup_pending_inline (info)
4552 struct pending_inline *info;
4557 tree fn = info->fndecl;
4559 args = DECL_ARGUMENTS (fn);
4562 DECL_CONTEXT (args) = fn;
4563 args = TREE_CHAIN (args);
4568 /* Fixup the inline methods and friends in TYPE now that TYPE is
4572 fixup_inline_methods (type)
4575 tree method = TYPE_METHODS (type);
4577 if (method && TREE_CODE (method) == TREE_VEC)
4579 if (TREE_VEC_ELT (method, 1))
4580 method = TREE_VEC_ELT (method, 1);
4581 else if (TREE_VEC_ELT (method, 0))
4582 method = TREE_VEC_ELT (method, 0);
4584 method = TREE_VEC_ELT (method, 2);
4587 /* Do inline member functions. */
4588 for (; method; method = TREE_CHAIN (method))
4589 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
4592 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4594 method = TREE_CHAIN (method))
4595 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
4596 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4599 /* Called from propagate_binfo_offsets via dfs_walk. */
4602 dfs_propagate_binfo_offsets (binfo, data)
4606 tree offset = (tree) data;
4608 /* Update the BINFO_OFFSET for this base. */
4609 BINFO_OFFSET (binfo)
4610 = size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), offset);
4612 SET_BINFO_MARKED (binfo);
4617 /* Add OFFSET to all base types of BINFO which is a base in the
4618 hierarchy dominated by T.
4620 OFFSET, which is a type offset, is number of bytes.
4622 Note that we don't have to worry about having two paths to the
4623 same base type, since this type owns its association list. */
4626 propagate_binfo_offsets (binfo, offset)
4631 dfs_propagate_binfo_offsets,
4632 dfs_skip_nonprimary_vbases_unmarkedp,
4636 dfs_skip_nonprimary_vbases_markedp,
4640 /* Remove *FIELD (which corresponds to the base given by BINFO) from
4641 the field list for T. */
4644 remove_base_field (t, binfo, field)
4649 tree basetype = BINFO_TYPE (binfo);
4652 my_friendly_assert (TREE_TYPE (*field) == basetype, 23897);
4654 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4655 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4659 = size_int (CEIL (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (*field)),
4661 propagate_binfo_offsets (binfo, offset);
4663 /* Remove this field. */
4664 *field = TREE_CHAIN (*field);
4667 /* Remove the FIELD_DECLs created for T's base classes in
4668 build_base_fields. Simultaneously, update BINFO_OFFSET for all the
4669 bases, except for non-primary virtual baseclasses. */
4672 remove_base_fields (t)
4678 /* Now propagate offset information throughout the lattice.
4679 Simultaneously, remove the temporary FIELD_DECLS we created in
4680 build_base_fields to refer to base types. */
4681 field = &TYPE_FIELDS (t);
4682 if (TYPE_VFIELD (t) == *field)
4684 /* If this class did not have a primary base, we create a
4685 virtual function table pointer. It will be the first thing
4686 in the class, under the new ABI. Skip it; the base fields
4688 my_friendly_assert (flag_new_abi
4689 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t),
4691 field = &TREE_CHAIN (*field);
4694 /* Under the new ABI, the primary base is always allocated first. */
4695 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4696 remove_base_field (t, CLASSTYPE_PRIMARY_BINFO (t), field);
4698 /* Now remove the rest of the bases. */
4699 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
4703 /* Under the new ABI, we've already removed the primary base
4705 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (t))
4708 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4710 /* We treat a primary virtual base class just like an ordinary base
4711 class. But, non-primary virtual bases are laid out later. */
4712 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4715 remove_base_field (t, binfo, field);
4719 /* Called via dfs_walk from layout_virtual bases. */
4722 dfs_set_offset_for_shared_vbases (binfo, data)
4726 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
4728 /* Update the shared copy. */
4731 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4732 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
4738 /* Called via dfs_walk from layout_virtual bases. */
4741 dfs_set_offset_for_unshared_vbases (binfo, data)
4745 /* If this is a virtual base, make sure it has the same offset as
4746 the shared copy. If it's a primary base, then we know it's
4748 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4750 tree t = (tree) data;
4754 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
4755 offset = ssize_binop (MINUS_EXPR,
4756 BINFO_OFFSET (vbase),
4757 BINFO_OFFSET (binfo));
4758 propagate_binfo_offsets (binfo, offset);
4764 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4765 TYPE_ALIGN and TYPE_SIZE for T. */
4768 layout_virtual_bases (t)
4774 /* DSIZE is the size of the class without the virtual bases. */
4775 dsize = TREE_INT_CST_LOW (TYPE_SIZE (t));
4776 /* Make every class have alignment of at least one. */
4777 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4779 /* Go through the virtual bases, allocating space for each virtual
4780 base that is not already a primary base class. */
4781 for (vbase = CLASSTYPE_VBASECLASSES (t);
4783 vbase = TREE_CHAIN (vbase))
4784 if (!BINFO_VBASE_PRIMARY_P (vbase))
4786 /* This virtual base is not a primary base of any class in the
4787 hierarchy, so we have to add space for it. */
4789 unsigned int desired_align;
4791 basetype = BINFO_TYPE (vbase);
4792 desired_align = TYPE_ALIGN (basetype);
4793 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4795 /* Add padding so that we can put the virtual base class at an
4796 appropriately aligned offset. */
4797 dsize = CEIL (dsize, desired_align) * desired_align;
4798 /* And compute the offset of the virtual base. */
4799 propagate_binfo_offsets (vbase,
4800 size_int (CEIL (dsize, BITS_PER_UNIT)));
4801 /* Every virtual baseclass takes a least a UNIT, so that we can
4802 take it's address and get something different for each base. */
4803 dsize += MAX (BITS_PER_UNIT,
4804 TREE_INT_CST_LOW (CLASSTYPE_SIZE (basetype)));
4807 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4808 BINFO_OFFSET set correctly. Those we just allocated certainly
4809 will. The others are primary baseclasses; we walk the hierarchy
4810 to find the primary copies and update the shared copy. */
4811 dfs_walk (TYPE_BINFO (t),
4812 dfs_set_offset_for_shared_vbases,
4813 dfs_unmarked_real_bases_queue_p,
4816 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4817 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4818 bases and their direct and indirect bases. The ambiguity checks
4819 in get_base_distance depend on the BINFO_OFFSETs being set
4821 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4823 /* Now, make sure that the total size of the type is a multiple of
4825 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4826 TYPE_SIZE (t) = size_int (dsize);
4827 TYPE_SIZE_UNIT (t) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (t),
4828 size_int (BITS_PER_UNIT));
4831 /* Finish the work of layout_record, now taking virtual bases into account.
4832 Also compute the actual offsets that our base classes will have.
4833 This must be performed after the fields are laid out, since virtual
4834 baseclasses must lay down at the end of the record. */
4837 layout_basetypes (rec)
4842 #ifdef STRUCTURE_SIZE_BOUNDARY
4843 /* Packed structures don't need to have minimum size. */
4844 if (! TYPE_PACKED (rec))
4845 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), STRUCTURE_SIZE_BOUNDARY);
4848 /* Remove the FIELD_DECLs we created for baseclasses in
4849 build_base_fields. Simultaneously, update the BINFO_OFFSETs for
4850 everything in the hierarcy except non-primary virtual bases. */
4851 remove_base_fields (rec);
4853 /* Allocate the virtual base classes. */
4854 layout_virtual_bases (rec);
4856 /* Get all the virtual base types that this type uses. The
4857 TREE_VALUE slot holds the virtual baseclass type. Note that
4858 get_vbase_types makes copies of the virtual base BINFOs, so that
4859 the vbase_types are unshared. */
4860 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
4861 vbase_types = TREE_CHAIN (vbase_types))
4864 tree basetype = BINFO_TYPE (vbase_types);
4865 if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
4866 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4871 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4872 BINFO_OFFSETs for all of the base-classes. Position the vtable
4876 layout_class_type (t, empty_p, has_virtual_p,
4877 new_virtuals_p, overridden_virtuals_p)
4881 tree *new_virtuals_p;
4882 tree *overridden_virtuals_p;
4884 tree padding = NULL_TREE;
4886 /* If possible, we reuse the virtual function table pointer from one
4887 of our base classes. */
4888 determine_primary_base (t, has_virtual_p);
4890 /* Add pointers to all of our virtual base-classes. */
4891 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (t, empty_p),
4893 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4894 TYPE_FIELDS (t) = chainon (build_base_fields (t, empty_p),
4897 /* Create a pointer to our virtual function table. */
4898 create_vtable_ptr (t, empty_p, has_virtual_p,
4899 new_virtuals_p, overridden_virtuals_p);
4901 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4902 we have to save this before we start modifying
4903 TYPE_NONCOPIED_PARTS. */
4904 fixup_inline_methods (t);
4906 /* We make all structures have at least one element, so that they
4907 have non-zero size. The field that we add here is fake, in the
4908 sense that, for example, we don't want people to be able to
4909 initialize it later. So, we add it just long enough to let the
4910 back-end lay out the type, and then remove it. In the new ABI,
4911 the class may be empty even if it has basetypes. Therefore, we
4912 add the fake field at the end of the fields list; if there are
4913 already FIELD_DECLs on the list, their offsets will not be
4917 padding = build_lang_decl (FIELD_DECL, NULL_TREE, char_type_node);
4918 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), padding);
4919 TYPE_NONCOPIED_PARTS (t)
4920 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
4921 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4924 /* Let the back-end lay out the type. Note that at this point we
4925 have only included non-virtual base-classes; we will lay out the
4926 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4927 this call are not necessarily correct; they are just the size and
4928 alignment when no virtual base clases are used. */
4931 /* If we added an extra field to make this class non-empty, remove
4937 declp = &TYPE_FIELDS (t);
4938 while (*declp != padding)
4939 declp = &TREE_CHAIN (*declp);
4940 *declp = TREE_CHAIN (*declp);
4943 /* Delete all zero-width bit-fields from the list of fields. Now
4944 that the type is laid out they are no longer important. */
4945 remove_zero_width_bit_fields (t);
4947 /* Remember the size and alignment of the class before adding
4948 the virtual bases. */
4949 if (*empty_p && flag_new_abi)
4950 CLASSTYPE_SIZE (t) = integer_zero_node;
4951 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
4952 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
4953 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4955 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4956 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4958 /* Set the TYPE_DECL for this type to contain the right
4959 value for DECL_OFFSET, so that we can use it as part
4960 of a COMPONENT_REF for multiple inheritance. */
4961 layout_decl (TYPE_MAIN_DECL (t), 0);
4963 /* Now fix up any virtual base class types that we left lying
4964 around. We must get these done before we try to lay out the
4965 virtual function table. */
4966 if (CLASSTYPE_N_BASECLASSES (t))
4967 /* layout_basetypes will remove the base subobject fields. */
4968 layout_basetypes (t);
4971 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
4972 (or C++ class declaration).
4974 For C++, we must handle the building of derived classes.
4975 Also, C++ allows static class members. The way that this is
4976 handled is to keep the field name where it is (as the DECL_NAME
4977 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
4978 of the field. layout_record and layout_union will know about this.
4980 More C++ hair: inline functions have text in their
4981 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
4982 meaningful tree structure. After the struct has been laid out, set
4983 things up so that this can happen.
4985 And still more: virtual functions. In the case of single inheritance,
4986 when a new virtual function is seen which redefines a virtual function
4987 from the base class, the new virtual function is placed into
4988 the virtual function table at exactly the same address that
4989 it had in the base class. When this is extended to multiple
4990 inheritance, the same thing happens, except that multiple virtual
4991 function tables must be maintained. The first virtual function
4992 table is treated in exactly the same way as in the case of single
4993 inheritance. Additional virtual function tables have different
4994 DELTAs, which tell how to adjust `this' to point to the right thing.
4996 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5004 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5005 a FUNCTION_DECL. Each of these functions is a virtual function
5006 declared in T that does not override any virtual function from a
5008 tree new_virtuals = NULL_TREE;
5009 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5010 except that each declaration here overrides the declaration from
5012 tree overridden_virtuals = NULL_TREE;
5019 if (IS_AGGR_TYPE (t))
5020 cp_error ("redefinition of `%#T'", t);
5022 my_friendly_abort (172);
5027 GNU_xref_decl (current_function_decl, t);
5029 /* If this type was previously laid out as a forward reference,
5030 make sure we lay it out again. */
5031 TYPE_SIZE (t) = NULL_TREE;
5032 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5033 CLASSTYPE_VFIELD_PARENT (t) = -1;
5035 CLASSTYPE_RTTI (t) = NULL_TREE;
5037 /* Do end-of-class semantic processing: checking the validity of the
5038 bases and members and add implicitly generated methods. */
5039 check_bases_and_members (t, &empty);
5041 /* Layout the class itself. */
5042 layout_class_type (t, &empty, &has_virtual,
5043 &new_virtuals, &overridden_virtuals);
5045 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
5049 vbases = CLASSTYPE_VBASECLASSES (t);
5052 /* Now fixup overrides of all functions in vtables from all
5053 direct or indirect virtual base classes. */
5054 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
5055 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
5057 for (i = 0; i < n_baseclasses; i++)
5059 tree base_binfo = TREE_VEC_ELT (binfos, i);
5060 tree basetype = BINFO_TYPE (base_binfo);
5063 vbases = CLASSTYPE_VBASECLASSES (basetype);
5067 tree basetype_vbase;
5070 = find_vbase_instance (BINFO_TYPE (vbases), t);
5072 = find_vbase_instance (BINFO_TYPE (vbases), basetype);
5074 merge_overrides (vbase, basetype_vbase, 1, t);
5075 vbases = TREE_CHAIN (vbases);
5081 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
5082 might need to know it for setting up the offsets in the vtable
5083 (or in thunks) below. */
5084 vfield = TYPE_VFIELD (t);
5085 if (vfield != NULL_TREE
5086 && DECL_FIELD_CONTEXT (vfield) != t)
5088 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
5089 tree offset = BINFO_OFFSET (binfo);
5091 vfield = copy_node (vfield);
5092 copy_lang_decl (vfield);
5094 if (! integer_zerop (offset))
5095 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
5096 DECL_FIELD_CONTEXT (vfield) = t;
5097 DECL_CLASS_CONTEXT (vfield) = t;
5098 DECL_FIELD_BITPOS (vfield)
5099 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
5100 TYPE_VFIELD (t) = vfield;
5104 = modify_all_vtables (t, &has_virtual, nreverse (overridden_virtuals));
5106 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
5109 /* Now fixup any virtual function entries from virtual bases
5110 that have different deltas. This has to come after we do the
5111 overridden virtuals. */
5112 vbases = CLASSTYPE_VBASECLASSES (t);
5117 /* We might be able to shorten the amount of work we do by
5118 only doing this for vtables that come from virtual bases
5119 that have differing offsets, but don't want to miss any
5121 vbase = find_vbase_instance (BINFO_TYPE (vbases), t);
5122 dfs_walk (vbase, dfs_fixup_vtable_deltas, dfs_skip_vbases, t);
5123 vbases = TREE_CHAIN (vbases);
5127 /* If necessary, create the vtable for this class. */
5129 || overridden_virtuals
5130 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
5132 new_virtuals = nreverse (new_virtuals);
5133 /* We must enter these virtuals into the table. */
5134 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5136 if (! CLASSTYPE_COM_INTERFACE (t))
5138 /* The second slot is for the tdesc pointer when thunks
5140 if (flag_vtable_thunks)
5141 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
5143 /* The first slot is for the rtti offset. */
5144 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
5146 set_rtti_entry (new_virtuals,
5147 convert (ssizetype, integer_zero_node), t);
5149 build_vtable (NULL_TREE, t);
5151 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
5152 /* Here we know enough to change the type of our virtual
5153 function table, but we will wait until later this function. */
5154 build_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5156 /* If this type has basetypes with constructors, then those
5157 constructors might clobber the virtual function table. But
5158 they don't if the derived class shares the exact vtable of the base
5161 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5163 /* If we didn't need a new vtable, see if we should copy one from
5165 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5167 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5169 /* This class contributes nothing new to the virtual function
5170 table. However, it may have declared functions which
5171 went into the virtual function table "inherited" from the
5172 base class. If so, we grab a copy of those updated functions,
5173 and pretend they are ours. */
5175 /* See if we should steal the virtual info from base class. */
5176 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
5177 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
5178 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
5179 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
5180 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5181 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5184 if (TYPE_CONTAINS_VPTR_P (t))
5186 if (TYPE_BINFO_VTABLE (t))
5187 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5189 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5190 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5193 CLASSTYPE_VSIZE (t) = has_virtual;
5194 /* Entries for virtual functions defined in the primary base are
5195 followed by entries for new functions unique to this class. */
5196 TYPE_BINFO_VIRTUALS (t)
5197 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5198 /* Finally, add entries for functions that override virtuals
5199 from non-primary bases. */
5200 TYPE_BINFO_VIRTUALS (t)
5201 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5204 /* Now lay out the virtual function table. */
5206 layout_vtable_decl (TYPE_BINFO (t), has_virtual);
5208 /* If we created a new vtbl pointer for this class, add it to the
5210 if (TYPE_VFIELD (t) && CLASSTYPE_VFIELD_PARENT (t) == -1)
5211 CLASSTYPE_VFIELDS (t)
5212 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5214 finish_struct_bits (t);
5216 /* Complete the rtl for any static member objects of the type we're
5218 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5220 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5221 && TREE_TYPE (x) == t)
5223 DECL_MODE (x) = TYPE_MODE (t);
5224 make_decl_rtl (x, NULL, 0);
5228 /* Done with FIELDS...now decide whether to sort these for
5229 faster lookups later.
5231 The C front-end only does this when n_fields > 15. We use
5232 a smaller number because most searches fail (succeeding
5233 ultimately as the search bores through the inheritance
5234 hierarchy), and we want this failure to occur quickly. */
5236 n_fields = count_fields (TYPE_FIELDS (t));
5239 tree field_vec = make_tree_vec (n_fields);
5240 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5241 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5242 (int (*)(const void *, const void *))field_decl_cmp);
5243 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5244 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5245 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5248 if (TYPE_HAS_CONSTRUCTOR (t))
5250 tree vfields = CLASSTYPE_VFIELDS (t);
5254 /* Mark the fact that constructor for T
5255 could affect anybody inheriting from T
5256 who wants to initialize vtables for VFIELDS's type. */
5257 if (VF_DERIVED_VALUE (vfields))
5258 TREE_ADDRESSABLE (vfields) = 1;
5259 vfields = TREE_CHAIN (vfields);
5263 if (CLASSTYPE_VSIZE (t) != 0)
5265 /* In addition to this one, all the other vfields should be listed. */
5266 /* Before that can be done, we have to have FIELD_DECLs for them, and
5267 a place to find them. */
5268 TYPE_NONCOPIED_PARTS (t)
5269 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5270 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5272 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5273 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5274 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5278 /* Make the rtl for any new vtables we have created, and unmark
5279 the base types we marked. */
5281 hack_incomplete_structures (t);
5283 if (warn_overloaded_virtual)
5286 maybe_suppress_debug_info (t);
5288 /* Finish debugging output for this type. */
5289 rest_of_type_compilation (t, toplevel_bindings_p ());
5292 /* When T was built up, the member declarations were added in reverse
5293 order. Rearrange them to declaration order. */
5296 unreverse_member_declarations (t)
5303 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5304 reverse order. Put them in declaration order now. */
5305 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5306 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5308 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5309 reverse order, so we can't just use nreverse. */
5311 for (x = TYPE_FIELDS (t);
5312 x && TREE_CODE (x) != TYPE_DECL;
5315 next = TREE_CHAIN (x);
5316 TREE_CHAIN (x) = prev;
5321 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5323 TYPE_FIELDS (t) = prev;
5328 finish_struct (t, attributes)
5331 /* Now that we've got all the field declarations, reverse everything
5333 unreverse_member_declarations (t);
5335 cplus_decl_attributes (t, attributes, NULL_TREE);
5337 if (processing_template_decl)
5339 finish_struct_methods (t);
5340 TYPE_SIZE (t) = integer_zero_node;
5343 finish_struct_1 (t);
5345 TYPE_BEING_DEFINED (t) = 0;
5347 if (current_class_type)
5350 error ("trying to finish struct, but kicked out due to previous parse errors.");
5352 if (processing_template_decl)
5354 tree scope = current_scope ();
5355 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5356 add_tree (build_min (TAG_DEFN, t));
5362 /* Return the dynamic type of INSTANCE, if known.
5363 Used to determine whether the virtual function table is needed
5366 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5367 of our knowledge of its type. *NONNULL should be initialized
5368 before this function is called. */
5371 fixed_type_or_null (instance, nonnull)
5375 switch (TREE_CODE (instance))
5378 /* Check that we are not going through a cast of some sort. */
5379 if (TREE_TYPE (instance)
5380 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5381 instance = TREE_OPERAND (instance, 0);
5382 /* fall through... */
5384 /* This is a call to a constructor, hence it's never zero. */
5385 if (TREE_HAS_CONSTRUCTOR (instance))
5389 return TREE_TYPE (instance);
5394 /* This is a call to a constructor, hence it's never zero. */
5395 if (TREE_HAS_CONSTRUCTOR (instance))
5399 return TREE_TYPE (instance);
5401 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5408 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5409 /* Propagate nonnull. */
5410 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5411 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5412 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5417 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5422 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5425 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5429 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5430 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5434 return TREE_TYPE (TREE_TYPE (instance));
5436 /* fall through... */
5439 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5443 return TREE_TYPE (instance);
5447 if (instance == current_class_ptr
5448 && flag_this_is_variable <= 0)
5450 /* Normally, 'this' must be non-null. */
5451 if (flag_this_is_variable == 0)
5454 /* <0 means we're in a constructor and we know our type. */
5455 if (flag_this_is_variable < 0)
5456 return TREE_TYPE (TREE_TYPE (instance));
5458 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5459 /* Reference variables should be references to objects. */
5469 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5470 to the static type. We also handle the case where INSTANCE is really
5473 Used to determine whether the virtual function table is needed
5476 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5477 of our knowledge of its type. *NONNULL should be initialized
5478 before this function is called. */
5481 resolves_to_fixed_type_p (instance, nonnull)
5485 tree t = TREE_TYPE (instance);
5486 tree fixed = fixed_type_or_null (instance, nonnull);
5487 if (fixed == NULL_TREE)
5489 if (POINTER_TYPE_P (t))
5491 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
5496 init_class_processing ()
5498 current_class_depth = 0;
5499 current_class_stack_size = 10;
5501 = (class_stack_node_t) xmalloc (current_class_stack_size
5502 * sizeof (struct class_stack_node));
5504 access_default_node = build_int_2 (0, 0);
5505 access_public_node = build_int_2 (1, 0);
5506 access_protected_node = build_int_2 (2, 0);
5507 access_private_node = build_int_2 (3, 0);
5508 access_default_virtual_node = build_int_2 (4, 0);
5509 access_public_virtual_node = build_int_2 (5, 0);
5510 access_protected_virtual_node = build_int_2 (6, 0);
5511 access_private_virtual_node = build_int_2 (7, 0);
5514 /* Set current scope to NAME. CODE tells us if this is a
5515 STRUCT, UNION, or ENUM environment.
5517 NAME may end up being NULL_TREE if this is an anonymous or
5518 late-bound struct (as in "struct { ... } foo;") */
5520 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5521 appropriate values, found by looking up the type definition of
5524 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5525 which can be seen locally to the class. They are shadowed by
5526 any subsequent local declaration (including parameter names).
5528 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5529 which have static meaning (i.e., static members, static
5530 member functions, enum declarations, etc).
5532 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5533 which can be seen locally to the class (as in 1), but
5534 know that we are doing this for declaration purposes
5535 (i.e. friend foo::bar (int)).
5537 So that we may avoid calls to lookup_name, we cache the _TYPE
5538 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5540 For multiple inheritance, we perform a two-pass depth-first search
5541 of the type lattice. The first pass performs a pre-order search,
5542 marking types after the type has had its fields installed in
5543 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5544 unmarks the marked types. If a field or member function name
5545 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5546 that name becomes `error_mark_node'. */
5549 pushclass (type, modify)
5553 type = TYPE_MAIN_VARIANT (type);
5555 /* Make sure there is enough room for the new entry on the stack. */
5556 if (current_class_depth + 1 >= current_class_stack_size)
5558 current_class_stack_size *= 2;
5560 = (class_stack_node_t) xrealloc (current_class_stack,
5561 current_class_stack_size
5562 * sizeof (struct class_stack_node));
5565 /* Insert a new entry on the class stack. */
5566 current_class_stack[current_class_depth].name = current_class_name;
5567 current_class_stack[current_class_depth].type = current_class_type;
5568 current_class_stack[current_class_depth].access = current_access_specifier;
5569 current_class_stack[current_class_depth].names_used = 0;
5570 current_class_depth++;
5572 /* Now set up the new type. */
5573 current_class_name = TYPE_NAME (type);
5574 if (TREE_CODE (current_class_name) == TYPE_DECL)
5575 current_class_name = DECL_NAME (current_class_name);
5576 current_class_type = type;
5578 /* By default, things in classes are private, while things in
5579 structures or unions are public. */
5580 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5581 ? access_private_node
5582 : access_public_node);
5584 if (previous_class_type != NULL_TREE
5585 && (type != previous_class_type
5586 || TYPE_SIZE (previous_class_type) == NULL_TREE)
5587 && current_class_depth == 1)
5589 /* Forcibly remove any old class remnants. */
5590 invalidate_class_lookup_cache ();
5593 /* If we're about to enter a nested class, clear
5594 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5595 if (modify && current_class_depth > 1)
5596 clear_identifier_class_values ();
5601 if (CLASSTYPE_TEMPLATE_INFO (type))
5602 overload_template_name (type);
5607 if (type != previous_class_type || current_class_depth > 1)
5608 push_class_decls (type);
5613 /* We are re-entering the same class we just left, so we
5614 don't have to search the whole inheritance matrix to find
5615 all the decls to bind again. Instead, we install the
5616 cached class_shadowed list, and walk through it binding
5617 names and setting up IDENTIFIER_TYPE_VALUEs. */
5618 set_class_shadows (previous_class_values);
5619 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5621 tree id = TREE_PURPOSE (item);
5622 tree decl = TREE_TYPE (item);
5624 push_class_binding (id, decl);
5625 if (TREE_CODE (decl) == TYPE_DECL)
5626 set_identifier_type_value (id, TREE_TYPE (decl));
5628 unuse_fields (type);
5631 storetags (CLASSTYPE_TAGS (type));
5635 /* When we exit a toplevel class scope, we save the
5636 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5637 reenter the class. Here, we've entered some other class, so we
5638 must invalidate our cache. */
5641 invalidate_class_lookup_cache ()
5645 /* This code can be seen as a cache miss. When we've cached a
5646 class' scope's bindings and we can't use them, we need to reset
5647 them. This is it! */
5648 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5649 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5651 previous_class_type = NULL_TREE;
5654 /* Get out of the current class scope. If we were in a class scope
5655 previously, that is the one popped to. */
5661 /* Since poplevel_class does the popping of class decls nowadays,
5662 this really only frees the obstack used for these decls. */
5665 current_class_depth--;
5666 current_class_name = current_class_stack[current_class_depth].name;
5667 current_class_type = current_class_stack[current_class_depth].type;
5668 current_access_specifier = current_class_stack[current_class_depth].access;
5669 if (current_class_stack[current_class_depth].names_used)
5670 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5673 /* Returns 1 if current_class_type is either T or a nested type of T. */
5676 currently_open_class (t)
5680 if (t == current_class_type)
5682 for (i = 0; i < current_class_depth; ++i)
5683 if (current_class_stack [i].type == t)
5688 /* When entering a class scope, all enclosing class scopes' names with
5689 static meaning (static variables, static functions, types and enumerators)
5690 have to be visible. This recursive function calls pushclass for all
5691 enclosing class contexts until global or a local scope is reached.
5692 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5693 formal of the same name. */
5696 push_nested_class (type, modify)
5702 /* A namespace might be passed in error cases, like A::B:C. */
5703 if (type == NULL_TREE
5704 || type == error_mark_node
5705 || TREE_CODE (type) == NAMESPACE_DECL
5706 || ! IS_AGGR_TYPE (type)
5707 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5708 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
5711 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5713 if (context && CLASS_TYPE_P (context))
5714 push_nested_class (context, 2);
5715 pushclass (type, modify);
5718 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5723 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5726 if (context && CLASS_TYPE_P (context))
5727 pop_nested_class ();
5730 /* Set global variables CURRENT_LANG_NAME to appropriate value
5731 so that behavior of name-mangling machinery is correct. */
5734 push_lang_context (name)
5737 *current_lang_stack++ = current_lang_name;
5738 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5739 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5741 size_t old_size = VARRAY_SIZE (current_lang_base);
5743 VARRAY_GROW (current_lang_base, old_size + 10);
5744 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5747 if (name == lang_name_cplusplus)
5749 strict_prototype = strict_prototypes_lang_cplusplus;
5750 current_lang_name = name;
5752 else if (name == lang_name_java)
5754 strict_prototype = strict_prototypes_lang_cplusplus;
5755 current_lang_name = name;
5756 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5757 (See record_builtin_java_type in decl.c.) However, that causes
5758 incorrect debug entries if these types are actually used.
5759 So we re-enable debug output after extern "Java". */
5760 DECL_IGNORED_P (java_byte_type_node) = 0;
5761 DECL_IGNORED_P (java_short_type_node) = 0;
5762 DECL_IGNORED_P (java_int_type_node) = 0;
5763 DECL_IGNORED_P (java_long_type_node) = 0;
5764 DECL_IGNORED_P (java_float_type_node) = 0;
5765 DECL_IGNORED_P (java_double_type_node) = 0;
5766 DECL_IGNORED_P (java_char_type_node) = 0;
5767 DECL_IGNORED_P (java_boolean_type_node) = 0;
5769 else if (name == lang_name_c)
5771 strict_prototype = strict_prototypes_lang_c;
5772 current_lang_name = name;
5775 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5778 /* Get out of the current language scope. */
5783 /* Clear the current entry so that garbage collector won't hold on
5785 *current_lang_stack = NULL_TREE;
5786 current_lang_name = *--current_lang_stack;
5787 if (current_lang_name == lang_name_cplusplus
5788 || current_lang_name == lang_name_java)
5789 strict_prototype = strict_prototypes_lang_cplusplus;
5790 else if (current_lang_name == lang_name_c)
5791 strict_prototype = strict_prototypes_lang_c;
5794 /* Type instantiation routines. */
5796 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5797 matches the TARGET_TYPE. If there is no satisfactory match, return
5798 error_mark_node, and issue an error message if COMPLAIN is
5799 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5800 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5801 template arguments. */
5804 resolve_address_of_overloaded_function (target_type,
5813 tree explicit_targs;
5815 /* Here's what the standard says:
5819 If the name is a function template, template argument deduction
5820 is done, and if the argument deduction succeeds, the deduced
5821 arguments are used to generate a single template function, which
5822 is added to the set of overloaded functions considered.
5824 Non-member functions and static member functions match targets of
5825 type "pointer-to-function" or "reference-to-function." Nonstatic
5826 member functions match targets of type "pointer-to-member
5827 function;" the function type of the pointer to member is used to
5828 select the member function from the set of overloaded member
5829 functions. If a nonstatic member function is selected, the
5830 reference to the overloaded function name is required to have the
5831 form of a pointer to member as described in 5.3.1.
5833 If more than one function is selected, any template functions in
5834 the set are eliminated if the set also contains a non-template
5835 function, and any given template function is eliminated if the
5836 set contains a second template function that is more specialized
5837 than the first according to the partial ordering rules 14.5.5.2.
5838 After such eliminations, if any, there shall remain exactly one
5839 selected function. */
5842 int is_reference = 0;
5843 /* We store the matches in a TREE_LIST rooted here. The functions
5844 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5845 interoperability with most_specialized_instantiation. */
5846 tree matches = NULL_TREE;
5849 /* By the time we get here, we should be seeing only real
5850 pointer-to-member types, not the internal POINTER_TYPE to
5851 METHOD_TYPE representation. */
5852 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5853 && (TREE_CODE (TREE_TYPE (target_type))
5854 == METHOD_TYPE)), 0);
5856 /* Check that the TARGET_TYPE is reasonable. */
5857 if (TYPE_PTRFN_P (target_type))
5860 else if (TYPE_PTRMEMFUNC_P (target_type))
5861 /* This is OK, too. */
5863 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5865 /* This is OK, too. This comes from a conversion to reference
5867 target_type = build_reference_type (target_type);
5873 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5874 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5875 return error_mark_node;
5878 /* If we can find a non-template function that matches, we can just
5879 use it. There's no point in generating template instantiations
5880 if we're just going to throw them out anyhow. But, of course, we
5881 can only do this when we don't *need* a template function. */
5886 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5888 tree fn = OVL_FUNCTION (fns);
5891 if (TREE_CODE (fn) == TEMPLATE_DECL)
5892 /* We're not looking for templates just yet. */
5895 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5897 /* We're looking for a non-static member, and this isn't
5898 one, or vice versa. */
5901 /* See if there's a match. */
5902 fntype = TREE_TYPE (fn);
5904 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5905 else if (!is_reference)
5906 fntype = build_pointer_type (fntype);
5908 if (can_convert_arg (target_type, fntype, fn))
5909 matches = tree_cons (fn, NULL_TREE, matches);
5913 /* Now, if we've already got a match (or matches), there's no need
5914 to proceed to the template functions. But, if we don't have a
5915 match we need to look at them, too. */
5918 tree target_fn_type;
5919 tree target_arg_types;
5924 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5926 target_fn_type = TREE_TYPE (target_type);
5927 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5929 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5931 tree fn = OVL_FUNCTION (fns);
5933 tree instantiation_type;
5936 if (TREE_CODE (fn) != TEMPLATE_DECL)
5937 /* We're only looking for templates. */
5940 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5942 /* We're not looking for a non-static member, and this is
5943 one, or vice versa. */
5946 /* Try to do argument deduction. */
5947 targs = make_tree_vec (DECL_NTPARMS (fn));
5948 if (fn_type_unification (fn, explicit_targs, targs,
5949 target_arg_types, NULL_TREE,
5951 /* Argument deduction failed. */
5954 /* Instantiate the template. */
5955 instantiation = instantiate_template (fn, targs);
5956 if (instantiation == error_mark_node)
5957 /* Instantiation failed. */
5960 /* See if there's a match. */
5961 instantiation_type = TREE_TYPE (instantiation);
5963 instantiation_type =
5964 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5965 else if (!is_reference)
5966 instantiation_type = build_pointer_type (instantiation_type);
5967 if (can_convert_arg (target_type, instantiation_type, instantiation))
5968 matches = tree_cons (instantiation, fn, matches);
5971 /* Now, remove all but the most specialized of the matches. */
5974 tree match = most_specialized_instantiation (matches,
5977 if (match != error_mark_node)
5978 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5982 /* Now we should have exactly one function in MATCHES. */
5983 if (matches == NULL_TREE)
5985 /* There were *no* matches. */
5988 cp_error ("no matches converting function `%D' to type `%#T'",
5989 DECL_NAME (OVL_FUNCTION (overload)),
5992 /* print_candidates expects a chain with the functions in
5993 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5994 so why be clever?). */
5995 for (; overload; overload = OVL_NEXT (overload))
5996 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5999 print_candidates (matches);
6001 return error_mark_node;
6003 else if (TREE_CHAIN (matches))
6005 /* There were too many matches. */
6011 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6012 DECL_NAME (OVL_FUNCTION (overload)),
6015 /* Since print_candidates expects the functions in the
6016 TREE_VALUE slot, we flip them here. */
6017 for (match = matches; match; match = TREE_CHAIN (match))
6018 TREE_VALUE (match) = TREE_PURPOSE (match);
6020 print_candidates (matches);
6023 return error_mark_node;
6026 /* Good, exactly one match. Now, convert it to the correct type. */
6027 fn = TREE_PURPOSE (matches);
6031 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6032 return build_unary_op (ADDR_EXPR, fn, 0);
6035 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6036 will mark the function as addressed, but here we must do it
6038 mark_addressable (fn);
6044 /* This function will instantiate the type of the expression given in
6045 RHS to match the type of LHSTYPE. If errors exist, then return
6046 error_mark_node. We only complain is COMPLAIN is set. If we are
6047 not complaining, never modify rhs, as overload resolution wants to
6048 try many possible instantiations, in hopes that at least one will
6051 FLAGS is a bitmask, as we see at the top of the function.
6053 For non-recursive calls, LHSTYPE should be a function, pointer to
6054 function, or a pointer to member function. */
6057 instantiate_type (lhstype, rhs, flags)
6061 int complain = (flags & 1);
6062 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6064 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6067 error ("not enough type information");
6068 return error_mark_node;
6071 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6073 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6076 cp_error ("argument of type `%T' does not match `%T'",
6077 TREE_TYPE (rhs), lhstype);
6078 return error_mark_node;
6081 /* We don't overwrite rhs if it is an overloaded function.
6082 Copying it would destroy the tree link. */
6083 if (TREE_CODE (rhs) != OVERLOAD)
6084 rhs = copy_node (rhs);
6086 /* This should really only be used when attempting to distinguish
6087 what sort of a pointer to function we have. For now, any
6088 arithmetic operation which is not supported on pointers
6089 is rejected as an error. */
6091 switch (TREE_CODE (rhs))
6098 my_friendly_abort (177);
6099 return error_mark_node;
6106 new_rhs = instantiate_type (build_pointer_type (lhstype),
6107 TREE_OPERAND (rhs, 0), flags);
6108 if (new_rhs == error_mark_node)
6109 return error_mark_node;
6111 TREE_TYPE (rhs) = lhstype;
6112 TREE_OPERAND (rhs, 0) = new_rhs;
6117 rhs = copy_node (TREE_OPERAND (rhs, 0));
6118 TREE_TYPE (rhs) = unknown_type_node;
6119 return instantiate_type (lhstype, rhs, flags);
6123 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6125 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
6126 && complain && !flag_ms_extensions)
6128 /* Note: we check this after the recursive call to avoid
6129 complaining about cases where overload resolution fails. */
6131 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
6132 tree fn = PTRMEM_CST_MEMBER (r);
6134 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
6137 ("object-dependent reference to `%E' can only be used in a call",
6140 (" to form a pointer to member function, say `&%T::%E'",
6148 rhs = TREE_OPERAND (rhs, 1);
6149 if (BASELINK_P (rhs))
6150 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6152 /* This can happen if we are forming a pointer-to-member for a
6154 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6158 case TEMPLATE_ID_EXPR:
6160 resolve_address_of_overloaded_function (lhstype,
6161 TREE_OPERAND (rhs, 0),
6163 /*template_only=*/1,
6164 TREE_OPERAND (rhs, 1));
6168 resolve_address_of_overloaded_function (lhstype,
6171 /*template_only=*/0,
6172 /*explicit_targs=*/NULL_TREE);
6175 /* Now we should have a baselink. */
6176 my_friendly_assert (BASELINK_P (rhs), 990412);
6178 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6181 /* This is too hard for now. */
6182 my_friendly_abort (183);
6183 return error_mark_node;
6188 TREE_OPERAND (rhs, 0)
6189 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6190 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6191 return error_mark_node;
6192 TREE_OPERAND (rhs, 1)
6193 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6194 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6195 return error_mark_node;
6197 TREE_TYPE (rhs) = lhstype;
6201 case TRUNC_DIV_EXPR:
6202 case FLOOR_DIV_EXPR:
6204 case ROUND_DIV_EXPR:
6206 case TRUNC_MOD_EXPR:
6207 case FLOOR_MOD_EXPR:
6209 case ROUND_MOD_EXPR:
6210 case FIX_ROUND_EXPR:
6211 case FIX_FLOOR_EXPR:
6213 case FIX_TRUNC_EXPR:
6229 case PREINCREMENT_EXPR:
6230 case PREDECREMENT_EXPR:
6231 case POSTINCREMENT_EXPR:
6232 case POSTDECREMENT_EXPR:
6234 error ("invalid operation on uninstantiated type");
6235 return error_mark_node;
6237 case TRUTH_AND_EXPR:
6239 case TRUTH_XOR_EXPR:
6246 case TRUTH_ANDIF_EXPR:
6247 case TRUTH_ORIF_EXPR:
6248 case TRUTH_NOT_EXPR:
6250 error ("not enough type information");
6251 return error_mark_node;
6254 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6257 error ("not enough type information");
6258 return error_mark_node;
6260 TREE_OPERAND (rhs, 1)
6261 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6262 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6263 return error_mark_node;
6264 TREE_OPERAND (rhs, 2)
6265 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6266 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6267 return error_mark_node;
6269 TREE_TYPE (rhs) = lhstype;
6273 TREE_OPERAND (rhs, 1)
6274 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6275 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6276 return error_mark_node;
6278 TREE_TYPE (rhs) = lhstype;
6282 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6284 case ENTRY_VALUE_EXPR:
6285 my_friendly_abort (184);
6286 return error_mark_node;
6289 return error_mark_node;
6292 my_friendly_abort (185);
6293 return error_mark_node;
6297 /* Return the name of the virtual function pointer field
6298 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6299 this may have to look back through base types to find the
6300 ultimate field name. (For single inheritance, these could
6301 all be the same name. Who knows for multiple inheritance). */
6304 get_vfield_name (type)
6307 tree binfo = TYPE_BINFO (type);
6310 while (BINFO_BASETYPES (binfo)
6311 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6312 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6313 binfo = BINFO_BASETYPE (binfo, 0);
6315 type = BINFO_TYPE (binfo);
6316 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6317 + TYPE_NAME_LENGTH (type) + 2);
6318 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6319 return get_identifier (buf);
6323 print_class_statistics ()
6325 #ifdef GATHER_STATISTICS
6326 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6327 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6328 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6329 n_build_method_call, n_inner_fields_searched);
6332 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6333 n_vtables, n_vtable_searches);
6334 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6335 n_vtable_entries, n_vtable_elems);
6340 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6341 according to [class]:
6342 The class-name is also inserted
6343 into the scope of the class itself. For purposes of access checking,
6344 the inserted class name is treated as if it were a public member name. */
6347 build_self_reference ()
6349 tree name = constructor_name (current_class_type);
6350 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6353 DECL_NONLOCAL (value) = 1;
6354 DECL_CONTEXT (value) = current_class_type;
6355 DECL_CLASS_CONTEXT (value) = current_class_type;
6356 DECL_ARTIFICIAL (value) = 1;
6358 if (processing_template_decl)
6359 value = push_template_decl (value);
6361 saved_cas = current_access_specifier;
6362 current_access_specifier = access_public_node;
6363 finish_member_declaration (value);
6364 current_access_specifier = saved_cas;
6367 /* Returns 1 if TYPE contains only padding bytes. */
6370 is_empty_class (type)
6375 if (type == error_mark_node)
6378 if (! IS_AGGR_TYPE (type))
6382 return CLASSTYPE_SIZE (type) == integer_zero_node;
6384 if (TYPE_BINFO_BASETYPES (type))
6386 t = TYPE_FIELDS (type);
6387 while (t && TREE_CODE (t) != FIELD_DECL)
6389 return (t == NULL_TREE);
6392 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6393 a *_TYPE node. NODE can also be a local class. */
6396 get_enclosing_class (type)
6401 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6403 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6406 node = DECL_CONTEXT (node);
6412 node = TYPE_CONTEXT (node);
6416 my_friendly_abort (0);
6422 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6425 is_base_of_enclosing_class (base, type)
6430 if (get_binfo (base, type, 0))
6433 type = get_enclosing_class (type);
6438 /* Note that NAME was looked up while the current class was being
6439 defined and that the result of that lookup was DECL. */
6442 maybe_note_name_used_in_class (name, decl)
6446 splay_tree names_used;
6448 /* If we're not defining a class, there's nothing to do. */
6449 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6452 /* If there's already a binding for this NAME, then we don't have
6453 anything to worry about. */
6454 if (IDENTIFIER_CLASS_VALUE (name))
6457 if (!current_class_stack[current_class_depth - 1].names_used)
6458 current_class_stack[current_class_depth - 1].names_used
6459 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6460 names_used = current_class_stack[current_class_depth - 1].names_used;
6462 splay_tree_insert (names_used,
6463 (splay_tree_key) name,
6464 (splay_tree_value) decl);
6467 /* Note that NAME was declared (as DECL) in the current class. Check
6468 to see that the declaration is legal. */
6471 note_name_declared_in_class (name, decl)
6475 splay_tree names_used;
6478 /* Look to see if we ever used this name. */
6480 = current_class_stack[current_class_depth - 1].names_used;
6484 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6487 /* [basic.scope.class]
6489 A name N used in a class S shall refer to the same declaration
6490 in its context and when re-evaluated in the completed scope of
6492 cp_error ("declaration of `%#D'", decl);
6493 cp_error_at ("changes meaning of `%s' from `%+#D'",
6494 IDENTIFIER_POINTER (DECL_NAME (decl)),
6499 /* Dump the offsets of all the bases rooted at BINFO to stderr.
6500 INDENT should be zero when called from the top level; it is
6501 incremented recursively. */
6504 dump_class_hierarchy (binfo, indent)
6510 fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
6511 (unsigned long) binfo,
6512 type_as_string (binfo, TS_PLAIN));
6513 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
6514 TREE_INT_CST_LOW (BINFO_OFFSET (binfo)));
6515 fprintf (stderr, " %s\n",
6516 BINFO_PRIMARY_MARKED_P (binfo) ? "primary" : "");
6518 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6519 dump_class_hierarchy (BINFO_BASETYPE (binfo, i), indent + 2);