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));
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));
89 static tree get_vtable_entry_n PARAMS ((tree, unsigned HOST_WIDE_INT));
90 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
91 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
92 static void delete_duplicate_fields PARAMS ((tree));
93 static void finish_struct_bits PARAMS ((tree));
94 static int alter_access PARAMS ((tree, tree, tree, tree));
95 static void handle_using_decl PARAMS ((tree, tree));
96 static int overrides PARAMS ((tree, tree));
97 static int strictly_overrides PARAMS ((tree, tree));
98 static void merge_overrides PARAMS ((tree, tree, int, tree));
99 static void override_one_vtable PARAMS ((tree, tree, tree));
100 static void mark_overriders PARAMS ((tree, tree));
101 static void check_for_override PARAMS ((tree, tree));
102 static tree dfs_get_class_offset PARAMS ((tree, void *));
103 static tree get_class_offset PARAMS ((tree, tree, tree, tree));
104 static void modify_one_vtable PARAMS ((tree, tree, tree));
105 static tree dfs_modify_vtables PARAMS ((tree, void *));
106 static tree modify_all_vtables PARAMS ((tree, int *, tree));
107 static void determine_primary_base PARAMS ((tree, int *));
108 static void finish_struct_methods PARAMS ((tree));
109 static void maybe_warn_about_overly_private_class PARAMS ((tree));
110 static int field_decl_cmp PARAMS ((const tree *, const tree *));
111 static int method_name_cmp PARAMS ((const tree *, const tree *));
112 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
113 static tree fixed_type_or_null PARAMS ((tree, int *));
114 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
116 static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
117 static tree build_vtable_entry_for_fn PARAMS ((tree, tree));
118 static tree build_vtbl_initializer PARAMS ((tree, tree));
119 static int count_fields PARAMS ((tree));
120 static int add_fields_to_vec PARAMS ((tree, tree, int));
121 static void check_bitfield_decl PARAMS ((tree));
122 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
123 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
125 static int avoid_overlap PARAMS ((tree, tree, int *));
126 static tree build_base_field PARAMS ((tree, tree, int *, int *, unsigned int *));
127 static tree build_base_fields PARAMS ((tree, int *));
128 static tree build_vbase_pointer_fields PARAMS ((tree, int *));
129 static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, int *));
130 static void check_methods PARAMS ((tree));
131 static void remove_zero_width_bit_fields PARAMS ((tree));
132 static void check_bases PARAMS ((tree, int *, int *, int *));
133 static void check_bases_and_members PARAMS ((tree, int *));
134 static void create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
135 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
136 static void fixup_pending_inline PARAMS ((struct pending_inline *));
137 static void fixup_inline_methods PARAMS ((tree));
138 static void set_primary_base PARAMS ((tree, int, int *));
139 static tree dfs_propagate_binfo_offsets PARAMS ((tree, void *));
140 static void propagate_binfo_offsets PARAMS ((tree, tree));
141 static void layout_basetypes PARAMS ((tree));
142 static void layout_virtual_bases PARAMS ((tree));
143 static void remove_base_field PARAMS ((tree, tree, tree *));
144 static void remove_base_fields PARAMS ((tree));
145 static tree dfs_set_offset_for_shared_vbases PARAMS ((tree, void *));
146 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
147 static tree dfs_build_vbase_offset_vtbl_entries PARAMS ((tree, void *));
148 static tree build_vbase_offset_vtbl_entries PARAMS ((tree, tree));
149 static tree dfs_vcall_offset_queue_p PARAMS ((tree, void *));
150 static tree dfs_build_vcall_offset_vtbl_entries PARAMS ((tree, void *));
151 static tree build_vcall_offset_vtbl_entries PARAMS ((tree, tree));
152 static tree dfs_count_virtuals PARAMS ((tree, void *));
153 static void start_vtable PARAMS ((tree, int *));
154 static void layout_vtable_decl PARAMS ((tree, int));
155 static int num_vfun_entries PARAMS ((tree));
157 /* Variables shared between class.c and call.c. */
159 #ifdef GATHER_STATISTICS
161 int n_vtable_entries = 0;
162 int n_vtable_searches = 0;
163 int n_vtable_elems = 0;
164 int n_convert_harshness = 0;
165 int n_compute_conversion_costs = 0;
166 int n_build_method_call = 0;
167 int n_inner_fields_searched = 0;
170 /* Virtual base class layout. */
172 /* Returns a list of virtual base class pointers as a chain of
176 build_vbase_pointer_fields (rec, empty_p)
180 /* Chain to hold all the new FIELD_DECLs which point at virtual
182 tree vbase_decls = NULL_TREE;
183 tree binfos = TYPE_BINFO_BASETYPES (rec);
184 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
188 /* Under the new ABI, there are no vbase pointers in the object.
189 Instead, the offsets are stored in the vtable. */
190 if (vbase_offsets_in_vtable_p ())
193 /* Loop over the baseclasses, adding vbase pointers as needed. */
194 for (i = 0; i < n_baseclasses; i++)
196 register tree base_binfo = TREE_VEC_ELT (binfos, i);
197 register tree basetype = BINFO_TYPE (base_binfo);
199 if (TYPE_SIZE (basetype) == 0)
200 /* This error is now reported in xref_tag, thus giving better
201 location information. */
204 /* All basetypes are recorded in the association list of the
207 if (TREE_VIA_VIRTUAL (base_binfo))
212 /* The offset for a virtual base class is only used in computing
213 virtual function tables and for initializing virtual base
214 pointers. It is built once `get_vbase_types' is called. */
216 /* If this basetype can come from another vbase pointer
217 without an additional indirection, we will share
218 that pointer. If an indirection is involved, we
219 make our own pointer. */
220 for (j = 0; j < n_baseclasses; j++)
222 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
223 if (! TREE_VIA_VIRTUAL (other_base_binfo)
224 && BINFO_FOR_VBASE (basetype, BINFO_TYPE (other_base_binfo)))
227 FORMAT_VBASE_NAME (name, basetype);
228 decl = build_vtbl_or_vbase_field (get_identifier (name),
229 get_identifier (VTABLE_BASE),
230 build_pointer_type (basetype),
233 BINFO_VPTR_FIELD (base_binfo) = decl;
234 TREE_CHAIN (decl) = vbase_decls;
239 /* The space this decl occupies has already been accounted for. */
247 /* Called from build_vbase_offset_vtbl_entries via dfs_walk. */
250 dfs_build_vbase_offset_vtbl_entries (binfo, data)
254 tree list = (tree) data;
256 if (TREE_TYPE (list) == binfo)
257 /* The TREE_TYPE of LIST is the base class from which we started
258 walking. If that BINFO is virtual it's not a virtual baseclass
261 else if (TREE_VIA_VIRTUAL (binfo))
266 /* Remember the index to the vbase offset for this virtual
268 vbase = BINFO_FOR_VBASE (TREE_TYPE (binfo), TREE_PURPOSE (list));
269 if (!TREE_VALUE (list))
270 BINFO_VPTR_FIELD (vbase) = build_int_2 (-1, 0);
273 BINFO_VPTR_FIELD (vbase) = TREE_PURPOSE (TREE_VALUE (list));
274 BINFO_VPTR_FIELD (vbase) = ssize_binop (MINUS_EXPR,
275 BINFO_VPTR_FIELD (vbase),
279 /* And record the offset at which this virtual base lies in the
281 init = BINFO_OFFSET (binfo);
282 TREE_VALUE (list) = tree_cons (BINFO_VPTR_FIELD (vbase),
287 SET_BINFO_VTABLE_PATH_MARKED (binfo);
292 /* Returns the initializers for the vbase offset entries in the vtable
293 for BINFO (which is part of the class hierarchy dominated by T), in
297 build_vbase_offset_vtbl_entries (binfo, t)
305 /* Under the old ABI, pointers to virtual bases are stored in each
307 if (!vbase_offsets_in_vtable_p ())
310 /* If there are no virtual baseclasses, then there is nothing to
312 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
317 /* The offsets are allocated in the reverse order of a
318 depth-first left-to-right traversal of the hierarchy. We use
319 BINFO_VTABLE_PATH_MARKED because we are ourselves during a
320 dfs_walk, and so BINFO_MARKED is already in use. */
321 list = build_tree_list (t, NULL_TREE);
322 TREE_TYPE (list) = binfo;
324 dfs_build_vbase_offset_vtbl_entries,
325 dfs_vtable_path_unmarked_real_bases_queue_p,
328 dfs_vtable_path_unmark,
329 dfs_vtable_path_marked_real_bases_queue_p,
331 inits = nreverse (TREE_VALUE (list));
333 /* We've now got offsets in the right order. However, the offsets
334 we've stored are offsets from the beginning of the complete
335 object, and we need offsets from this BINFO. */
336 for (init = inits; init; init = TREE_CHAIN (init))
338 tree exp = TREE_VALUE (init);
340 exp = ssize_binop (MINUS_EXPR, exp, BINFO_OFFSET (binfo));
341 exp = build1 (NOP_EXPR, vtable_entry_type, exp);
343 TREE_CONSTANT (exp) = 1;
344 /* The dfs_build_vbase_offset_vtbl_entries routine uses the
345 TREE_PURPOSE to scribble in. But, we need to clear it now so
346 that the values are not perceived as labeled initializers. */
347 TREE_PURPOSE (init) = NULL_TREE;
348 TREE_VALUE (init) = exp;
354 typedef struct vcall_offset_data_s
356 /* The binfo for the most-derived type. */
358 /* The binfo for the virtual base for which we're building
361 /* The vcall offset initializers built up so far. */
363 /* The number of vcall offsets accumulated. */
367 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
370 dfs_vcall_offset_queue_p (binfo, data)
374 vcall_offset_data* vod = (vcall_offset_data *) data;
376 return (binfo == vod->vbase) ? binfo : dfs_skip_vbases (binfo, NULL);
379 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
382 dfs_build_vcall_offset_vtbl_entries (binfo, data)
386 vcall_offset_data* vod;
390 /* Primary bases are not interesting; all of the virtual
391 function table entries have been overridden. */
392 if (BINFO_PRIMARY_MARKED_P (binfo))
395 vod = (vcall_offset_data *) data;
396 binfo_inits = NULL_TREE;
398 /* We chain the offsets on in reverse order. That's correct --
399 build_vtbl_initializer will straighten them out. */
400 for (virtuals = skip_rtti_stuff (binfo,
404 virtuals = TREE_CHAIN (virtuals))
411 /* Figure out what function we're looking at. */
412 fn = TREE_VALUE (virtuals);
413 base = DECL_CLASS_CONTEXT (fn);
415 /* The FN is comes from BASE. So, we must caculate the
416 adjustment from the virtual base that derived from BINFO to
418 base_binfo = get_binfo (base, vod->derived, /*protect=*/0);
419 offset = ssize_binop (MINUS_EXPR,
420 BINFO_OFFSET (base_binfo),
421 BINFO_OFFSET (vod->vbase));
422 offset = build1 (NOP_EXPR, vtable_entry_type, offset);
423 offset = fold (offset);
424 TREE_CONSTANT (offset) = 1;
425 binfo_inits = tree_cons (NULL_TREE, offset, binfo_inits);
428 /* Now add the initializers we've just created to the list that will
429 be returned to our caller. */
430 vod->inits = chainon (vod->inits, binfo_inits);
435 /* Returns the initializers for the vcall offset entries in the vtable
436 for BINFO (which is part of the class hierarchy dominated by T), in
440 build_vcall_offset_vtbl_entries (binfo, t)
444 vcall_offset_data vod;
446 /* Under the old ABI, the adjustments to the `this' pointer were made
448 if (!vcall_offsets_in_vtable_p ())
451 /* We only need these entries if this base is a virtual base. */
452 if (!TREE_VIA_VIRTUAL (binfo))
455 /* We need a vcall offset for each of the virtual functions in this
458 class A { virtual void f (); };
459 class B : virtual public A { };
460 class C: virtual public A, public B {};
467 The location of `A' is not at a fixed offset relative to `B'; the
468 offset depends on the complete object derived from `B'. So,
469 `B' vtable contains an entry for `f' that indicates by what
470 amount the `this' pointer for `B' needs to be adjusted to arrive
473 We need entries for all the functions in our primary vtable and
474 in our non-virtual bases vtables. For each base, the entries
475 appear in the same order as in the base; but the bases themselves
476 appear in reverse depth-first, left-to-right order. */
479 vod.inits = NULL_TREE;
481 dfs_build_vcall_offset_vtbl_entries,
482 dfs_vcall_offset_queue_p,
488 /* Returns a pointer to the virtual base class of EXP that has the
489 indicated TYPE. EXP is of class type, not a pointer type. */
492 build_vbase_pointer (exp, type)
495 if (vbase_offsets_in_vtable_p ())
500 /* Find the shared copy of TYPE; that's where the vtable offset
502 vbase = BINFO_FOR_VBASE (type, TREE_TYPE (exp));
503 /* Find the virtual function table pointer. */
504 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
505 /* Compute the location where the offset will lie. */
506 vbase_ptr = build_binary_op (PLUS_EXPR,
508 BINFO_VPTR_FIELD (vbase));
509 vbase_ptr = build1 (NOP_EXPR,
510 build_pointer_type (ptrdiff_type_node),
512 /* Add the contents of this location to EXP. */
513 return build (PLUS_EXPR,
514 build_pointer_type (type),
515 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
516 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
521 FORMAT_VBASE_NAME (name, type);
522 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
526 /* Build multi-level access to EXPR using hierarchy path PATH.
527 CODE is PLUS_EXPR if we are going with the grain,
528 and MINUS_EXPR if we are not (in which case, we cannot traverse
529 virtual baseclass links).
531 TYPE is the type we want this path to have on exit.
533 NONNULL is non-zero if we know (for any reason) that EXPR is
534 not, in fact, zero. */
537 build_vbase_path (code, type, expr, path, nonnull)
539 tree type, expr, path;
542 register int changed = 0;
543 tree last = NULL_TREE, last_virtual = NULL_TREE;
545 tree null_expr = 0, nonnull_expr;
547 tree offset = integer_zero_node;
549 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
550 return build1 (NOP_EXPR, type, expr);
552 /* We could do better if we had additional logic to convert back to the
553 unconverted type (the static type of the complete object), and then
554 convert back to the type we want. Until that is done, we only optimize
555 if the complete type is the same type as expr has. */
556 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
558 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
559 expr = save_expr (expr);
562 if (BINFO_INHERITANCE_CHAIN (path))
563 path = reverse_path (path);
565 basetype = BINFO_TYPE (path);
569 if (TREE_VIA_VIRTUAL (path))
571 last_virtual = BINFO_TYPE (path);
572 if (code == PLUS_EXPR)
574 changed = ! fixed_type_p;
580 /* We already check for ambiguous things in the caller, just
584 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
585 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
587 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
588 nonnull_expr = build_vbase_pointer (ind, last_virtual);
590 && TREE_CODE (type) == POINTER_TYPE
591 && null_expr == NULL_TREE)
593 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
594 expr = build (COND_EXPR, build_pointer_type (last_virtual),
595 build (EQ_EXPR, boolean_type_node, expr,
597 null_expr, nonnull_expr);
600 /* else we'll figure out the offset below. */
602 /* Happens in the case of parse errors. */
603 if (nonnull_expr == error_mark_node)
604 return error_mark_node;
608 cp_error ("cannot cast up from virtual baseclass `%T'",
610 return error_mark_node;
614 path = BINFO_INHERITANCE_CHAIN (path);
616 /* LAST is now the last basetype assoc on the path. */
618 /* A pointer to a virtual base member of a non-null object
619 is non-null. Therefore, we only need to test for zeroness once.
620 Make EXPR the canonical expression to deal with here. */
623 TREE_OPERAND (expr, 2) = nonnull_expr;
624 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
625 = TREE_TYPE (nonnull_expr);
630 /* If we go through any virtual base pointers, make sure that
631 casts to BASETYPE from the last virtual base class use
632 the right value for BASETYPE. */
635 tree intype = TREE_TYPE (TREE_TYPE (expr));
636 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
638 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
639 offset = BINFO_OFFSET (binfo);
643 offset = BINFO_OFFSET (last);
645 if (TREE_INT_CST_LOW (offset))
647 /* Bash types to make the backend happy. */
648 offset = cp_convert (type, offset);
650 /* This shouldn't be necessary. (mrs) */
651 expr = build1 (NOP_EXPR, type, expr);
654 /* If expr might be 0, we need to preserve that zeroness. */
658 TREE_TYPE (null_expr) = type;
660 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
661 if (TREE_SIDE_EFFECTS (expr))
662 expr = save_expr (expr);
664 return build (COND_EXPR, type,
665 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
667 build (code, type, expr, offset));
669 else return build (code, type, expr, offset);
672 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
673 be used multiple times in initialization of multiple inheritance. */
676 TREE_TYPE (expr) = type;
680 return build1 (NOP_EXPR, type, expr);
684 /* Virtual function things. */
686 /* Build an entry in the virtual function table.
687 DELTA is the offset for the `this' pointer.
688 PFN is an ADDR_EXPR containing a pointer to the virtual function.
689 Note that the index (DELTA2) in the virtual function table
693 build_vtable_entry (delta, pfn)
696 if (flag_vtable_thunks)
698 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
699 if (idelta && ! DECL_PURE_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
701 pfn = build1 (ADDR_EXPR, vtable_entry_type,
702 make_thunk (pfn, idelta));
703 TREE_READONLY (pfn) = 1;
704 TREE_CONSTANT (pfn) = 1;
706 #ifdef GATHER_STATISTICS
707 n_vtable_entries += 1;
713 extern int flag_huge_objects;
714 tree elems = tree_cons (NULL_TREE, delta,
715 tree_cons (NULL_TREE, integer_zero_node,
716 build_tree_list (NULL_TREE, pfn)));
717 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
719 /* DELTA used to be constructed by `size_int' and/or size_binop,
720 which caused overflow problems when it was negative. That should
723 if (! int_fits_type_p (delta, delta_type_node))
725 if (flag_huge_objects)
726 sorry ("object size exceeds built-in limit for virtual function table implementation");
728 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
731 TREE_CONSTANT (entry) = 1;
732 TREE_STATIC (entry) = 1;
733 TREE_READONLY (entry) = 1;
735 #ifdef GATHER_STATISTICS
736 n_vtable_entries += 1;
743 /* Build a vtable entry for FNDECL. DELTA is the amount by which we
744 must adjust the this pointer when calling F. */
747 build_vtable_entry_for_fn (delta, fndecl)
753 /* Take the address of the function, considering it to be of an
754 appropriate generic type. */
755 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
756 /* The address of a function can't change. */
757 TREE_CONSTANT (pfn) = 1;
758 /* Now build the vtable entry itself. */
759 return build_vtable_entry (delta, pfn);
762 /* We want to give the assembler the vtable identifier as well as
763 the offset to the function pointer. So we generate
765 __asm__ __volatile__ (".vtable_entry %c0, %c1"
766 : : "s"(&class_vtable),
767 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
770 build_vtable_entry_ref (basetype, vtbl, idx)
771 tree basetype, vtbl, idx;
773 static char asm_stmt[] = ".vtable_entry %c0, %c1";
776 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
777 s = build_tree_list (build_string (1, "s"), s);
779 i = build_array_ref (vtbl, idx);
780 if (!flag_vtable_thunks)
781 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
782 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
783 i2 = build_array_ref (vtbl, build_int_2(0,0));
784 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
785 i = build_binary_op (MINUS_EXPR, i, i2);
786 i = build_tree_list (build_string (1, "i"), i);
788 finish_asm_stmt (ridpointers[RID_VOLATILE],
789 build_string (sizeof(asm_stmt)-1, asm_stmt),
790 NULL_TREE, chainon (s, i), NULL_TREE);
793 /* Given an object INSTANCE, return an expression which yields the
794 virtual function vtable element corresponding to INDEX. There are
795 many special cases for INSTANCE which we take care of here, mainly
796 to avoid creating extra tree nodes when we don't have to. */
799 build_vtbl_ref (instance, idx)
803 tree basetype = TREE_TYPE (instance);
805 if (TREE_CODE (basetype) == REFERENCE_TYPE)
806 basetype = TREE_TYPE (basetype);
808 if (instance == current_class_ref)
809 vtbl = build_vfield_ref (instance, basetype);
814 /* Try to figure out what a reference refers to, and
815 access its virtual function table directly. */
816 tree ref = NULL_TREE;
818 if (TREE_CODE (instance) == INDIRECT_REF
819 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
820 ref = TREE_OPERAND (instance, 0);
821 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
824 if (ref && TREE_CODE (ref) == VAR_DECL
825 && DECL_INITIAL (ref))
827 tree init = DECL_INITIAL (ref);
829 while (TREE_CODE (init) == NOP_EXPR
830 || TREE_CODE (init) == NON_LVALUE_EXPR)
831 init = TREE_OPERAND (init, 0);
832 if (TREE_CODE (init) == ADDR_EXPR)
834 init = TREE_OPERAND (init, 0);
835 if (IS_AGGR_TYPE (TREE_TYPE (init))
836 && (TREE_CODE (init) == PARM_DECL
837 || TREE_CODE (init) == VAR_DECL))
843 if (IS_AGGR_TYPE (TREE_TYPE (instance))
844 && (TREE_CODE (instance) == RESULT_DECL
845 || TREE_CODE (instance) == PARM_DECL
846 || TREE_CODE (instance) == VAR_DECL))
847 vtbl = TYPE_BINFO_VTABLE (basetype);
849 vtbl = build_vfield_ref (instance, basetype);
852 assemble_external (vtbl);
855 build_vtable_entry_ref (basetype, vtbl, idx);
857 aref = build_array_ref (vtbl, idx);
862 /* Given an object INSTANCE, return an expression which yields the
863 virtual function corresponding to INDEX. There are many special
864 cases for INSTANCE which we take care of here, mainly to avoid
865 creating extra tree nodes when we don't have to. */
868 build_vfn_ref (ptr_to_instptr, instance, idx)
869 tree *ptr_to_instptr, instance;
872 tree aref = build_vtbl_ref (instance, idx);
874 /* When using thunks, there is no extra delta, and we get the pfn
876 if (flag_vtable_thunks)
881 /* Save the intermediate result in a SAVE_EXPR so we don't have to
882 compute each component of the virtual function pointer twice. */
883 if (TREE_CODE (aref) == INDIRECT_REF)
884 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
887 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
889 cp_convert (ptrdiff_type_node,
890 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
893 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
896 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
897 for the given TYPE. */
900 get_vtable_name (type)
903 tree type_id = build_typename_overload (type);
904 char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
905 + IDENTIFIER_LENGTH (type_id) + 2);
906 const char *ptr = IDENTIFIER_POINTER (type_id);
908 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
910 /* We don't take off the numbers; prepare_fresh_vtable uses the
911 DECL_ASSEMBLER_NAME for the type, which includes the number
912 in `3foo'. If we were to pull them off here, we'd end up with
913 something like `_vt.foo.3bar', instead of a uniform definition. */
914 while (ptr[i] >= '0' && ptr[i] <= '9')
917 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
918 return get_identifier (buf);
921 /* Return the offset to the main vtable for a given base BINFO. */
924 get_vfield_offset (binfo)
928 = size_binop (FLOOR_DIV_EXPR,
929 DECL_FIELD_BITPOS (TYPE_VFIELD (BINFO_TYPE (binfo))),
930 size_int (BITS_PER_UNIT));
931 tmp = convert (sizetype, tmp);
932 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
935 /* Get the offset to the start of the original binfo that we derived
936 this binfo from. If we find TYPE first, return the offset only
937 that far. The shortened search is useful because the this pointer
938 on method calling is expected to point to a DECL_CONTEXT (fndecl)
939 object, and not a baseclass of it. */
942 get_derived_offset (binfo, type)
945 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
948 while (BINFO_BASETYPES (binfo)
949 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
951 tree binfos = BINFO_BASETYPES (binfo);
952 if (BINFO_TYPE (binfo) == type)
954 binfo = TREE_VEC_ELT (binfos, i);
956 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
957 return size_binop (MINUS_EXPR, offset1, offset2);
960 /* Update the rtti info for this class. */
963 set_rtti_entry (virtuals, offset, type)
964 tree virtuals, offset, type;
968 if (CLASSTYPE_COM_INTERFACE (type))
972 decl = get_tinfo_decl (type);
973 else if (!new_abi_rtti_p ())
974 /* If someone tries to get RTTI information for a type compiled
975 without RTTI, they're out of luck. By calling __pure_virtual
976 in this case, we give a small clue as to what went wrong. We
977 could consider having a __no_typeinfo function as well, for a
978 more specific hint. */
981 /* For the new-abi, we just point to the type_info object. */
984 if (flag_vtable_thunks)
986 /* The first slot holds the offset. */
987 TREE_PURPOSE (virtuals) = offset;
989 /* The next node holds the decl. */
990 virtuals = TREE_CHAIN (virtuals);
991 offset = integer_zero_node;
994 /* This slot holds the decl. */
995 TREE_PURPOSE (virtuals) = offset;
996 TREE_VALUE (virtuals) = decl;
999 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
1000 or even complete. If this does not exist, create it. If COMPLETE is
1001 non-zero, then complete the definition of it -- that will render it
1002 impossible to actually build the vtable, but is useful to get at those
1003 which are known to exist in the runtime. */
1005 tree get_vtable_decl (type, complete)
1009 tree name = get_vtable_name (type);
1010 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1014 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
1015 && DECL_VIRTUAL_P (decl), 20000118);
1019 decl = build_lang_decl (VAR_DECL, name, void_type_node);
1021 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
1022 import_export_vtable (decl, type, 0);
1024 decl = pushdecl_top_level (decl);
1025 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
1027 DECL_ARTIFICIAL (decl) = 1;
1028 TREE_STATIC (decl) = 1;
1029 #ifndef WRITABLE_VTABLES
1030 /* Make them READONLY by default. (mrs) */
1031 TREE_READONLY (decl) = 1;
1033 /* At one time the vtable info was grabbed 2 words at a time. This
1034 fails on sparc unless you have 8-byte alignment. (tiemann) */
1035 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
1038 DECL_VIRTUAL_P (decl) = 1;
1041 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
1043 DECL_CONTEXT (decl) = type;
1047 /* Build a virtual function for type TYPE.
1048 If BINFO is non-NULL, build the vtable starting with the initial
1049 approximation that it is the same as the one which is the head of
1050 the association list. */
1053 build_vtable (binfo, type)
1056 tree virtuals, decl;
1058 decl = get_vtable_decl (type, /*complete=*/0);
1064 if (BINFO_NEW_VTABLE_MARKED (binfo))
1065 /* We have already created a vtable for this base, so there's
1066 no need to do it again. */
1069 virtuals = copy_list (BINFO_VIRTUALS (binfo));
1070 TREE_TYPE (decl) = TREE_TYPE (BINFO_VTABLE (binfo));
1071 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (BINFO_VTABLE (binfo)));
1073 /* Now do rtti stuff. */
1074 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
1075 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
1076 set_rtti_entry (virtuals, offset, type);
1080 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
1082 virtuals = NULL_TREE;
1085 #ifdef GATHER_STATISTICS
1087 n_vtable_elems += list_length (virtuals);
1090 /* Initialize the association list for this type, based
1091 on our first approximation. */
1092 TYPE_BINFO_VTABLE (type) = decl;
1093 TYPE_BINFO_VIRTUALS (type) = virtuals;
1095 binfo = TYPE_BINFO (type);
1096 SET_BINFO_NEW_VTABLE_MARKED (binfo);
1099 /* Give TYPE a new virtual function table which is initialized
1100 with a skeleton-copy of its original initialization. The only
1101 entry that changes is the `delta' entry, so we can really
1102 share a lot of structure.
1104 FOR_TYPE is the derived type which caused this table to
1107 BINFO is the type association which provided TYPE for FOR_TYPE.
1109 The order in which vtables are built (by calling this function) for
1110 an object must remain the same, otherwise a binary incompatibility
1114 prepare_fresh_vtable (binfo, for_type)
1115 tree binfo, for_type;
1118 tree orig_decl = BINFO_VTABLE (binfo);
1131 if (BINFO_NEW_VTABLE_MARKED (binfo))
1132 /* We already created a vtable for this base. There's no need to
1136 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
1138 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
1139 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
1141 /* We know that the vtable that we are going to create doesn't exist
1142 yet in the global namespace, and when we finish, it will be
1143 pushed into the global namespace. In complex MI hierarchies, we
1144 have to loop while the name we are thinking of adding is globally
1145 defined, adding more name components to the vtable name as we
1146 loop, until the name is unique. This is because in complex MI
1147 cases, we might have the same base more than once. This means
1148 that the order in which this function is called for vtables must
1149 remain the same, otherwise binary compatibility can be
1154 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
1158 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
1160 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
1161 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1162 name = get_identifier (buf);
1164 /* If this name doesn't clash, then we can use it, otherwise
1165 we add more to the name until it is unique. */
1167 if (! IDENTIFIER_GLOBAL_VALUE (name))
1170 /* Set values for next loop through, if the name isn't unique. */
1172 path = BINFO_INHERITANCE_CHAIN (path);
1174 /* We better not run out of stuff to make it unique. */
1175 my_friendly_assert (path != NULL_TREE, 368);
1177 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
1179 if (for_type == basetype)
1181 /* If we run out of basetypes in the path, we have already
1182 found created a vtable with that name before, we now
1183 resort to tacking on _%d to distinguish them. */
1185 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
1186 buf1 = (char *) alloca (i);
1188 sprintf (buf1, "%s%c%s%c%d",
1189 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
1191 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
1192 + strlen (buf1) + 1);
1193 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1194 name = get_identifier (buf);
1196 /* If this name doesn't clash, then we can use it,
1197 otherwise we add something different to the name until
1199 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
1201 /* Hey, they really like MI don't they? Increase the 3
1202 above to 6, and the 999 to 999999. :-) */
1203 my_friendly_assert (j <= 999, 369);
1208 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
1209 new_buf2 = (char *) alloca (i);
1210 sprintf (new_buf2, "%s%c%s",
1211 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
1215 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
1216 /* Remember which class this vtable is really for. */
1217 DECL_CONTEXT (new_decl) = for_type;
1219 DECL_ARTIFICIAL (new_decl) = 1;
1220 TREE_STATIC (new_decl) = 1;
1221 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
1222 DECL_VIRTUAL_P (new_decl) = 1;
1223 #ifndef WRITABLE_VTABLES
1224 /* Make them READONLY by default. (mrs) */
1225 TREE_READONLY (new_decl) = 1;
1227 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
1229 /* Make fresh virtual list, so we can smash it later. */
1230 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
1232 if (TREE_VIA_VIRTUAL (binfo))
1234 tree binfo1 = BINFO_FOR_VBASE (BINFO_TYPE (binfo), for_type);
1236 /* XXX - This should never happen, if it does, the caller should
1237 ensure that the binfo is from for_type's binfos, not from any
1238 base type's. We can remove all this code after a while. */
1239 if (binfo1 != binfo)
1240 warning ("internal inconsistency: binfo offset error for rtti");
1242 offset = BINFO_OFFSET (binfo1);
1245 offset = BINFO_OFFSET (binfo);
1247 set_rtti_entry (BINFO_VIRTUALS (binfo),
1248 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
1251 #ifdef GATHER_STATISTICS
1253 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
1256 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
1257 import_export_vtable (new_decl, for_type, 0);
1259 if (TREE_VIA_VIRTUAL (binfo))
1260 my_friendly_assert (binfo == BINFO_FOR_VBASE (BINFO_TYPE (binfo),
1261 current_class_type),
1263 SET_BINFO_NEW_VTABLE_MARKED (binfo);
1266 /* Change the offset for the FNDECL entry to NEW_OFFSET. Also update
1267 DECL_VINDEX (FNDECL). */
1270 modify_vtable_entry (old_entry_in_list, new_offset, fndecl)
1271 tree old_entry_in_list, new_offset, fndecl;
1273 tree base_fndecl = TREE_VALUE (old_entry_in_list);
1275 /* Update the entry. */
1276 TREE_PURPOSE (old_entry_in_list) = new_offset;
1277 TREE_VALUE (old_entry_in_list) = fndecl;
1279 /* Now assign virtual dispatch information, if unset. We can
1280 dispatch this, through any overridden base function. */
1281 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1283 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1284 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1288 /* Access the virtual function table entry N. VIRTUALS is the virtual
1289 function table's initializer. */
1292 get_vtable_entry_n (virtuals, n)
1294 unsigned HOST_WIDE_INT n;
1299 virtuals = TREE_CHAIN (virtuals);
1304 /* Call this function whenever its known that a vtable for T is going
1305 to be needed. It's safe to call it more than once. *HAS_VIRTUAL_P
1306 is initialized to the number of slots that are reserved at the
1307 beginning of the vtable for RTTI information. */
1310 start_vtable (t, has_virtual_p)
1314 if (*has_virtual_p == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1316 /* If we are using thunks, use two slots at the front, one
1317 for the offset pointer, one for the tdesc pointer.
1318 For ARM-style vtables, use the same slot for both. */
1319 if (flag_vtable_thunks)
1326 /* Add a virtual function to all the appropriate vtables for the class
1327 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1328 allocate a new slot in our table. If it is error_mark_node, we
1329 know that no other function from another vtable is overridden by X.
1330 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1331 vtable for the type, and we build upon the NEW_VIRTUALS list
1335 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1336 has_virtual, fndecl, t)
1337 tree *new_virtuals_p;
1338 tree *overridden_virtuals_p;
1341 tree t; /* Structure type. */
1343 /* If this function doesn't override anything from a base class, we
1344 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1345 override something, we keep it around and assign its DECL_VINDEX
1346 later, in modify_all_vtables. */
1347 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1348 /* We've already dealt with this function. */
1350 else if (DECL_VINDEX (fndecl) == error_mark_node)
1352 /* FNDECL is a new virtual function; it doesn't override any
1353 virtual function in a base class. */
1355 /* We remember that this was the base sub-object for rtti. */
1356 CLASSTYPE_RTTI (t) = t;
1358 start_vtable (t, has_virtual);
1360 /* Now assign virtual dispatch information. */
1361 DECL_VINDEX (fndecl) = build_shared_int_cst ((*has_virtual)++);
1362 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1364 /* Save the state we've computed on the NEW_VIRTUALS list. */
1365 *new_virtuals_p = tree_cons (integer_zero_node,
1369 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1370 /* FNDECL overrides a function from a base class. */
1371 *overridden_virtuals_p = tree_cons (NULL_TREE,
1373 *overridden_virtuals_p);
1376 extern struct obstack *current_obstack;
1378 /* Add method METHOD to class TYPE.
1380 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1381 the class type where the method should be added. */
1384 add_method (type, fields, method)
1385 tree type, *fields, method;
1387 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1389 DECL_CONTEXT (method) = type;
1390 DECL_CLASS_CONTEXT (method) = type;
1392 if (fields && *fields)
1393 *fields = build_overload (method, *fields);
1400 if (!CLASSTYPE_METHOD_VEC (type))
1401 /* Make a new method vector. We start with 8 entries. We must
1402 allocate at least two (for constructors and destructors), and
1403 we're going to end up with an assignment operator at some
1406 We could use a TREE_LIST for now, and convert it to a
1407 TREE_VEC in finish_struct, but we would probably waste more
1408 memory making the links in the list than we would by
1409 over-allocating the size of the vector here. Furthermore,
1410 we would complicate all the code that expects this to be a
1412 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1414 method_vec = CLASSTYPE_METHOD_VEC (type);
1415 len = TREE_VEC_LENGTH (method_vec);
1417 if (DECL_NAME (method) == constructor_name (type))
1418 /* A new constructor or destructor. Constructors go in
1419 slot 0; destructors go in slot 1. */
1420 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1423 /* See if we already have an entry with this name. */
1424 for (slot = 2; slot < len; ++slot)
1425 if (!TREE_VEC_ELT (method_vec, slot)
1426 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1428 == DECL_NAME (method)))
1433 /* We need a bigger method vector. */
1434 tree new_vec = make_tree_vec (2 * len);
1435 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1436 (PTR) &TREE_VEC_ELT (new_vec, 0),
1437 len * sizeof (tree));
1439 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1442 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1444 /* Type conversion operators have to come before
1445 ordinary methods; add_conversions depends on this to
1446 speed up looking for conversion operators. So, if
1447 necessary, we slide some of the vector elements up.
1448 In theory, this makes this algorithm O(N^2) but we
1449 don't expect many conversion operators. */
1450 for (slot = 2; slot < len; ++slot)
1452 tree fn = TREE_VEC_ELT (method_vec, slot);
1455 /* There are no more entries in the vector, so we
1456 can insert the new conversion operator here. */
1459 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1460 /* We can insert the new function right at the
1465 if (!TREE_VEC_ELT (method_vec, slot))
1466 /* There is nothing in the Ith slot, so we can avoid
1471 /* We know the last slot in the vector is empty
1472 because we know that at this point there's room
1473 for a new function. */
1474 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1475 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1476 (len - slot - 1) * sizeof (tree));
1477 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1482 if (template_class_depth (type))
1483 /* TYPE is a template class. Don't issue any errors now; wait
1484 until instantiation time to complain. */
1490 /* Check to see if we've already got this method. */
1491 for (fns = TREE_VEC_ELT (method_vec, slot);
1493 fns = OVL_NEXT (fns))
1495 tree fn = OVL_CURRENT (fns);
1497 if (TREE_CODE (fn) != TREE_CODE (method))
1500 if (TREE_CODE (method) != TEMPLATE_DECL)
1502 /* [over.load] Member function declarations with the
1503 same name and the same parameter types cannot be
1504 overloaded if any of them is a static member
1505 function declaration. */
1506 if (DECL_STATIC_FUNCTION_P (fn)
1507 != DECL_STATIC_FUNCTION_P (method))
1509 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1510 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1512 if (! DECL_STATIC_FUNCTION_P (fn))
1513 parms1 = TREE_CHAIN (parms1);
1515 parms2 = TREE_CHAIN (parms2);
1517 if (compparms (parms1, parms2))
1518 cp_error ("`%#D' and `%#D' cannot be overloaded",
1522 /* Since this is an ordinary function in a
1523 non-template class, it's mangled name can be used
1524 as a unique identifier. This technique is only
1525 an optimization; we would get the same results if
1526 we just used decls_match here. */
1527 if (DECL_ASSEMBLER_NAME (fn)
1528 != DECL_ASSEMBLER_NAME (method))
1531 else if (!decls_match (fn, method))
1534 /* There has already been a declaration of this method
1535 or member template. */
1536 cp_error_at ("`%D' has already been declared in `%T'",
1539 /* We don't call duplicate_decls here to merge the
1540 declarations because that will confuse things if the
1541 methods have inline definitions. In particular, we
1542 will crash while processing the definitions. */
1547 /* Actually insert the new method. */
1548 TREE_VEC_ELT (method_vec, slot)
1549 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1551 /* Add the new binding. */
1552 if (!DECL_CONSTRUCTOR_P (method)
1553 && !DECL_DESTRUCTOR_P (method))
1554 push_class_level_binding (DECL_NAME (method),
1555 TREE_VEC_ELT (method_vec, slot));
1559 /* Subroutines of finish_struct. */
1561 /* Look through the list of fields for this struct, deleting
1562 duplicates as we go. This must be recursive to handle
1565 FIELD is the field which may not appear anywhere in FIELDS.
1566 FIELD_PTR, if non-null, is the starting point at which
1567 chained deletions may take place.
1568 The value returned is the first acceptable entry found
1571 Note that anonymous fields which are not of UNION_TYPE are
1572 not duplicates, they are just anonymous fields. This happens
1573 when we have unnamed bitfields, for example. */
1576 delete_duplicate_fields_1 (field, fields)
1581 if (DECL_NAME (field) == 0)
1583 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1586 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1587 fields = delete_duplicate_fields_1 (x, fields);
1592 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1594 if (DECL_NAME (x) == 0)
1596 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1598 TYPE_FIELDS (TREE_TYPE (x))
1599 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1600 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1603 fields = TREE_CHAIN (fields);
1605 TREE_CHAIN (prev) = TREE_CHAIN (x);
1608 else if (TREE_CODE (field) == USING_DECL)
1609 /* A using declaration may is allowed to appear more than
1610 once. We'll prune these from the field list later, and
1611 handle_using_decl will complain about invalid multiple
1614 else if (DECL_NAME (field) == DECL_NAME (x))
1616 if (TREE_CODE (field) == CONST_DECL
1617 && TREE_CODE (x) == CONST_DECL)
1618 cp_error_at ("duplicate enum value `%D'", x);
1619 else if (TREE_CODE (field) == CONST_DECL
1620 || TREE_CODE (x) == CONST_DECL)
1621 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1623 else if (DECL_DECLARES_TYPE_P (field)
1624 && DECL_DECLARES_TYPE_P (x))
1626 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1628 cp_error_at ("duplicate nested type `%D'", x);
1630 else if (DECL_DECLARES_TYPE_P (field)
1631 || DECL_DECLARES_TYPE_P (x))
1633 /* Hide tag decls. */
1634 if ((TREE_CODE (field) == TYPE_DECL
1635 && DECL_ARTIFICIAL (field))
1636 || (TREE_CODE (x) == TYPE_DECL
1637 && DECL_ARTIFICIAL (x)))
1639 cp_error_at ("duplicate field `%D' (as type and non-type)",
1643 cp_error_at ("duplicate member `%D'", x);
1645 fields = TREE_CHAIN (fields);
1647 TREE_CHAIN (prev) = TREE_CHAIN (x);
1655 delete_duplicate_fields (fields)
1659 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1660 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1663 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1664 along the path given by BINFO. Return 1 if change was legit,
1665 otherwise return 0. */
1668 alter_access (t, binfo, fdecl, access)
1674 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1677 if (TREE_VALUE (elem) != access)
1679 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1680 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1682 error ("conflicting access specifications for field `%s', ignored",
1683 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1687 /* They're changing the access to the same thing they changed
1688 it to before. That's OK. */
1694 enforce_access (binfo, fdecl);
1695 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1701 /* Process the USING_DECL, which is a member of T. */
1704 handle_using_decl (using_decl, t)
1708 tree ctype = DECL_INITIAL (using_decl);
1709 tree name = DECL_NAME (using_decl);
1711 = TREE_PRIVATE (using_decl) ? access_private_node
1712 : TREE_PROTECTED (using_decl) ? access_protected_node
1713 : access_public_node;
1715 tree flist = NULL_TREE;
1716 tree fields = TYPE_FIELDS (t);
1717 tree method_vec = CLASSTYPE_METHOD_VEC (t);
1722 binfo = binfo_or_else (ctype, t);
1726 if (name == constructor_name (ctype)
1727 || name == constructor_name_full (ctype))
1729 cp_error_at ("using-declaration for constructor", using_decl);
1733 fdecl = lookup_member (binfo, name, 0, 0);
1737 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1741 /* Functions are represented as TREE_LIST, with the purpose
1742 being the type and the value the functions. Other members
1743 come as themselves. */
1744 if (TREE_CODE (fdecl) == TREE_LIST)
1745 /* Ignore base type this came from. */
1746 fdecl = TREE_VALUE (fdecl);
1748 if (TREE_CODE (fdecl) == OVERLOAD)
1750 /* We later iterate over all functions. */
1752 fdecl = OVL_FUNCTION (flist);
1755 name = DECL_NAME (fdecl);
1756 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1757 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1758 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1761 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1762 cp_error_at (" because of local method `%#D' with same name",
1763 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1767 if (! DECL_LANG_SPECIFIC (fdecl))
1768 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1771 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1772 if (DECL_NAME (tmp) == name)
1774 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1775 cp_error_at (" because of local field `%#D' with same name", tmp);
1779 /* Make type T see field decl FDECL with access ACCESS.*/
1784 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1787 flist = OVL_CHAIN (flist);
1791 alter_access (t, binfo, fdecl, access);
1794 /* Run through the base clases of T, updating
1795 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1796 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1800 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1803 int *cant_have_default_ctor_p;
1804 int *cant_have_const_ctor_p;
1805 int *no_const_asn_ref_p;
1809 int seen_nearly_empty_base_p;
1812 binfos = TYPE_BINFO_BASETYPES (t);
1813 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1814 seen_nearly_empty_base_p = 0;
1816 /* An aggregate cannot have baseclasses. */
1817 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1819 for (i = 0; i < n_baseclasses; ++i)
1824 /* Figure out what base we're looking at. */
1825 base_binfo = TREE_VEC_ELT (binfos, i);
1826 basetype = TREE_TYPE (base_binfo);
1828 /* If the type of basetype is incomplete, then we already
1829 complained about that fact (and we should have fixed it up as
1831 if (TYPE_SIZE (basetype) == 0)
1834 /* The base type is of incomplete type. It is
1835 probably best to pretend that it does not
1837 if (i == n_baseclasses-1)
1838 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1839 TREE_VEC_LENGTH (binfos) -= 1;
1841 for (j = i; j+1 < n_baseclasses; j++)
1842 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1846 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1847 here because the case of virtual functions but non-virtual
1848 dtor is handled in finish_struct_1. */
1849 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1850 && TYPE_HAS_DESTRUCTOR (basetype))
1851 cp_warning ("base class `%#T' has a non-virtual destructor",
1854 /* If the base class doesn't have copy constructors or
1855 assignment operators that take const references, then the
1856 derived class cannot have such a member automatically
1858 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1859 *cant_have_const_ctor_p = 1;
1860 if (TYPE_HAS_ASSIGN_REF (basetype)
1861 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1862 *no_const_asn_ref_p = 1;
1863 /* Similarly, if the base class doesn't have a default
1864 constructor, then the derived class won't have an
1865 automatically generated default constructor. */
1866 if (TYPE_HAS_CONSTRUCTOR (basetype)
1867 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1869 *cant_have_default_ctor_p = 1;
1870 if (! TYPE_HAS_CONSTRUCTOR (t))
1872 cp_pedwarn ("base `%T' with only non-default constructor",
1874 cp_pedwarn ("in class without a constructor");
1878 /* If the base class is not empty or nearly empty, then this
1879 class cannot be nearly empty. */
1880 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1881 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1882 /* And if there is more than one nearly empty base, then the
1883 derived class is not nearly empty either. */
1884 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1885 && seen_nearly_empty_base_p)
1886 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1887 /* If this is the first nearly empty base class, then remember
1889 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1890 seen_nearly_empty_base_p = 1;
1892 /* A lot of properties from the bases also apply to the derived
1894 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1895 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1896 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1897 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1898 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1899 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1900 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1901 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1902 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1904 /* Derived classes can implicitly become COMified if their bases
1906 if (CLASSTYPE_COM_INTERFACE (basetype))
1907 CLASSTYPE_COM_INTERFACE (t) = 1;
1908 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1911 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1913 CLASSTYPE_COM_INTERFACE (t) = 0;
1918 /* Make the Ith baseclass of T its primary base. */
1921 set_primary_base (t, i, has_virtual_p)
1928 CLASSTYPE_VFIELD_PARENT (t) = i;
1929 basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
1930 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1931 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1932 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1933 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1934 *has_virtual_p = CLASSTYPE_VSIZE (basetype);
1937 /* Determine the primary class for T. */
1940 determine_primary_base (t, has_virtual_p)
1944 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1946 /* If there are no baseclasses, there is certainly no primary base. */
1947 if (n_baseclasses == 0)
1952 for (i = 0; i < n_baseclasses; i++)
1954 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1955 tree basetype = BINFO_TYPE (base_binfo);
1957 if (TYPE_CONTAINS_VPTR_P (basetype))
1959 /* Even a virtual baseclass can contain our RTTI
1960 information. But, we prefer a non-virtual polymorphic
1962 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1963 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1965 /* A virtual baseclass can't be the primary base under the
1966 old ABI. And under the new ABI we still prefer a
1967 non-virtual base. */
1968 if (TREE_VIA_VIRTUAL (base_binfo))
1971 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1973 set_primary_base (t, i, has_virtual_p);
1974 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1980 /* Only add unique vfields, and flatten them out as we go. */
1981 for (vfields = CLASSTYPE_VFIELDS (basetype);
1983 vfields = TREE_CHAIN (vfields))
1984 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1985 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1986 CLASSTYPE_VFIELDS (t)
1987 = tree_cons (base_binfo,
1988 VF_BASETYPE_VALUE (vfields),
1989 CLASSTYPE_VFIELDS (t));
1991 if (*has_virtual_p == 0)
1992 set_primary_base (t, i, has_virtual_p);
1997 if (!TYPE_VFIELD (t))
1998 CLASSTYPE_VFIELD_PARENT (t) = -1;
2000 /* The new ABI allows for the use of a "nearly-empty" virtual base
2001 class as the primary base class if no non-virtual polymorphic
2002 base can be found. */
2003 if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2004 for (i = 0; i < n_baseclasses; ++i)
2006 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
2007 tree basetype = BINFO_TYPE (base_binfo);
2009 if (TREE_VIA_VIRTUAL (base_binfo)
2010 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
2012 set_primary_base (t, i, has_virtual_p);
2013 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2018 /* Mark the primary base classes at this point. */
2019 mark_primary_bases (t);
2022 /* Set memoizing fields and bits of T (and its variants) for later
2026 finish_struct_bits (t)
2029 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2031 /* Fix up variants (if any). */
2032 tree variants = TYPE_NEXT_VARIANT (t);
2035 /* These fields are in the _TYPE part of the node, not in
2036 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2037 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
2038 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
2039 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2040 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
2042 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
2043 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
2044 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2045 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2046 /* Copy whatever these are holding today. */
2047 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2048 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
2049 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2050 TYPE_SIZE (variants) = TYPE_SIZE (t);
2051 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
2052 variants = TYPE_NEXT_VARIANT (variants);
2055 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
2056 /* For a class w/o baseclasses, `finish_struct' has set
2057 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2058 definition). Similarly for a class whose base classes do not
2059 have vtables. When neither of these is true, we might have
2060 removed abstract virtuals (by providing a definition), added
2061 some (by declaring new ones), or redeclared ones from a base
2062 class. We need to recalculate what's really an abstract virtual
2063 at this point (by looking in the vtables). */
2064 get_pure_virtuals (t);
2068 /* Notice whether this class has type conversion functions defined. */
2069 tree binfo = TYPE_BINFO (t);
2070 tree binfos = BINFO_BASETYPES (binfo);
2073 for (i = n_baseclasses-1; i >= 0; i--)
2075 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2077 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
2081 /* If this type has a copy constructor, force its mode to be BLKmode, and
2082 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2083 be passed by invisible reference and prevent it from being returned in
2086 Also do this if the class has BLKmode but can still be returned in
2087 registers, since function_cannot_inline_p won't let us inline
2088 functions returning such a type. This affects the HP-PA. */
2089 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
2090 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
2091 && CLASSTYPE_NON_AGGREGATE (t)))
2094 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2095 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2097 TYPE_MODE (variants) = BLKmode;
2098 TREE_ADDRESSABLE (variants) = 1;
2103 /* Issue warnings about T having private constructors, but no friends,
2106 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2107 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2108 non-private static member functions. */
2111 maybe_warn_about_overly_private_class (t)
2114 int has_member_fn = 0;
2115 int has_nonprivate_method = 0;
2118 if (!warn_ctor_dtor_privacy
2119 /* If the class has friends, those entities might create and
2120 access instances, so we should not warn. */
2121 || (CLASSTYPE_FRIEND_CLASSES (t)
2122 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2123 /* We will have warned when the template was declared; there's
2124 no need to warn on every instantiation. */
2125 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2126 /* There's no reason to even consider warning about this
2130 /* We only issue one warning, if more than one applies, because
2131 otherwise, on code like:
2134 // Oops - forgot `public:'
2140 we warn several times about essentially the same problem. */
2142 /* Check to see if all (non-constructor, non-destructor) member
2143 functions are private. (Since there are no friends or
2144 non-private statics, we can't ever call any of the private member
2146 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2147 /* We're not interested in compiler-generated methods; they don't
2148 provide any way to call private members. */
2149 if (!DECL_ARTIFICIAL (fn))
2151 if (!TREE_PRIVATE (fn))
2153 if (DECL_STATIC_FUNCTION_P (fn))
2154 /* A non-private static member function is just like a
2155 friend; it can create and invoke private member
2156 functions, and be accessed without a class
2160 has_nonprivate_method = 1;
2163 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2167 if (!has_nonprivate_method && has_member_fn)
2169 /* There are no non-private methods, and there's at least one
2170 private member function that isn't a constructor or
2171 destructor. (If all the private members are
2172 constructors/destructors we want to use the code below that
2173 issues error messages specifically referring to
2174 constructors/destructors.) */
2176 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2177 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2178 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2179 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2181 has_nonprivate_method = 1;
2184 if (!has_nonprivate_method)
2186 cp_warning ("all member functions in class `%T' are private", t);
2191 /* Even if some of the member functions are non-private, the class
2192 won't be useful for much if all the constructors or destructors
2193 are private: such an object can never be created or destroyed. */
2194 if (TYPE_HAS_DESTRUCTOR (t))
2196 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2198 if (TREE_PRIVATE (dtor))
2200 cp_warning ("`%#T' only defines a private destructor and has no friends",
2206 if (TYPE_HAS_CONSTRUCTOR (t))
2208 int nonprivate_ctor = 0;
2210 /* If a non-template class does not define a copy
2211 constructor, one is defined for it, enabling it to avoid
2212 this warning. For a template class, this does not
2213 happen, and so we would normally get a warning on:
2215 template <class T> class C { private: C(); };
2217 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2218 complete non-template or fully instantiated classes have this
2220 if (!TYPE_HAS_INIT_REF (t))
2221 nonprivate_ctor = 1;
2223 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2227 tree ctor = OVL_CURRENT (fn);
2228 /* Ideally, we wouldn't count copy constructors (or, in
2229 fact, any constructor that takes an argument of the
2230 class type as a parameter) because such things cannot
2231 be used to construct an instance of the class unless
2232 you already have one. But, for now at least, we're
2234 if (! TREE_PRIVATE (ctor))
2236 nonprivate_ctor = 1;
2241 if (nonprivate_ctor == 0)
2243 cp_warning ("`%#T' only defines private constructors and has no friends",
2250 /* Function to help qsort sort FIELD_DECLs by name order. */
2253 field_decl_cmp (x, y)
2256 if (DECL_NAME (*x) == DECL_NAME (*y))
2257 /* A nontype is "greater" than a type. */
2258 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2259 if (DECL_NAME (*x) == NULL_TREE)
2261 if (DECL_NAME (*y) == NULL_TREE)
2263 if (DECL_NAME (*x) < DECL_NAME (*y))
2268 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2271 method_name_cmp (m1, m2)
2272 const tree *m1, *m2;
2274 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2276 if (*m1 == NULL_TREE)
2278 if (*m2 == NULL_TREE)
2280 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2285 /* Warn about duplicate methods in fn_fields. Also compact method
2286 lists so that lookup can be made faster.
2288 Data Structure: List of method lists. The outer list is a
2289 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2290 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2291 links the entire list of methods for TYPE_METHODS. Friends are
2292 chained in the same way as member functions (? TREE_CHAIN or
2293 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2294 list. That allows them to be quickly deleted, and requires no
2297 If there are any constructors/destructors, they are moved to the
2298 front of the list. This makes pushclass more efficient.
2300 @@ The above comment is obsolete. It mostly describes what add_method
2301 @@ and add_implicitly_declared_members do.
2303 Sort methods that are not special (i.e., constructors, destructors, and
2304 type conversion operators) so that we can find them faster in search. */
2307 finish_struct_methods (t)
2312 tree ctor_name = constructor_name (t);
2315 if (!TYPE_METHODS (t))
2317 /* Clear these for safety; perhaps some parsing error could set
2318 these incorrectly. */
2319 TYPE_HAS_CONSTRUCTOR (t) = 0;
2320 TYPE_HAS_DESTRUCTOR (t) = 0;
2321 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2325 method_vec = CLASSTYPE_METHOD_VEC (t);
2326 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2327 len = TREE_VEC_LENGTH (method_vec);
2329 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2330 and the next few with type conversion operators (if any). */
2331 for (fn_fields = TYPE_METHODS (t); fn_fields;
2332 fn_fields = TREE_CHAIN (fn_fields))
2334 tree fn_name = DECL_NAME (fn_fields);
2336 /* Clear out this flag.
2338 @@ Doug may figure out how to break
2339 @@ this with nested classes and friends. */
2340 DECL_IN_AGGR_P (fn_fields) = 0;
2342 /* Note here that a copy ctor is private, so we don't dare generate
2343 a default copy constructor for a class that has a member
2344 of this type without making sure they have access to it. */
2345 if (fn_name == ctor_name)
2347 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2348 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2350 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2351 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2353 if (TREE_CHAIN (parmtypes) == NULL_TREE
2354 || TREE_CHAIN (parmtypes) == void_list_node
2355 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2357 if (TREE_PROTECTED (fn_fields))
2358 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2359 else if (TREE_PRIVATE (fn_fields))
2360 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2364 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2366 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2368 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2370 if (TREE_PROTECTED (fn_fields))
2371 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2372 else if (TREE_PRIVATE (fn_fields))
2373 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2378 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2379 /* We thought there was a destructor, but there wasn't. Some
2380 parse errors cause this anomalous situation. */
2381 TYPE_HAS_DESTRUCTOR (t) = 0;
2383 /* Issue warnings about private constructors and such. If there are
2384 no methods, then some public defaults are generated. */
2385 maybe_warn_about_overly_private_class (t);
2387 /* Now sort the methods. */
2388 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2390 TREE_VEC_LENGTH (method_vec) = len;
2392 /* The type conversion ops have to live at the front of the vec, so we
2394 for (slot = 2; slot < len; ++slot)
2396 tree fn = TREE_VEC_ELT (method_vec, slot);
2398 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2402 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2403 (int (*)(const void *, const void *))method_name_cmp);
2406 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2409 duplicate_tag_error (t)
2412 cp_error ("redefinition of `%#T'", t);
2413 cp_error_at ("previous definition here", t);
2415 /* Pretend we haven't defined this type. */
2417 /* All of the component_decl's were TREE_CHAINed together in the parser.
2418 finish_struct_methods walks these chains and assembles all methods with
2419 the same base name into DECL_CHAINs. Now we don't need the parser chains
2420 anymore, so we unravel them. */
2422 /* This used to be in finish_struct, but it turns out that the
2423 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2425 if (CLASSTYPE_METHOD_VEC (t))
2427 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2428 int i, len = TREE_VEC_LENGTH (method_vec);
2429 for (i = 0; i < len; i++)
2431 tree unchain = TREE_VEC_ELT (method_vec, i);
2432 while (unchain != NULL_TREE)
2434 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2435 unchain = OVL_NEXT (unchain);
2440 if (TYPE_LANG_SPECIFIC (t))
2442 tree binfo = TYPE_BINFO (t);
2443 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2444 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2445 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2446 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2448 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2449 BINFO_BASETYPES(binfo) = NULL_TREE;
2451 TYPE_BINFO (t) = binfo;
2452 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2453 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2454 TYPE_REDEFINED (t) = 1;
2455 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2456 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2458 TYPE_SIZE (t) = NULL_TREE;
2459 TYPE_MODE (t) = VOIDmode;
2460 TYPE_FIELDS (t) = NULL_TREE;
2461 TYPE_METHODS (t) = NULL_TREE;
2462 TYPE_VFIELD (t) = NULL_TREE;
2463 TYPE_CONTEXT (t) = NULL_TREE;
2464 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2467 /* Make the BINFO's vtablehave N entries, including RTTI entries, but
2468 not including vbase and vcall offsets. Set its type and call the
2469 backend to lay it out. */
2472 layout_vtable_decl (binfo, n)
2479 itype = size_int (n);
2480 itype = size_binop (PLUS_EXPR,
2482 num_extra_vtbl_entries (binfo));
2483 atype = build_cplus_array_type (vtable_entry_type,
2484 build_index_type (itype));
2485 layout_type (atype);
2487 /* We may have to grow the vtable. */
2488 if (!same_type_p (TREE_TYPE (BINFO_VTABLE (binfo)), atype))
2490 TREE_TYPE (BINFO_VTABLE (binfo)) = atype;
2491 DECL_SIZE (BINFO_VTABLE (binfo)) = 0;
2492 layout_decl (BINFO_VTABLE (binfo), 0);
2493 /* At one time the vtable info was grabbed 2 words at a time. This
2494 fails on sparc unless you have 8-byte alignment. (tiemann) */
2495 DECL_ALIGN (BINFO_VTABLE (binfo))
2496 = MAX (TYPE_ALIGN (double_type_node),
2497 DECL_ALIGN (BINFO_VTABLE (binfo)));
2501 /* Returns the number of virtual function table entries (excluding
2502 RTTI information, vbase and vcall offests, etc.) in the vtable for
2506 num_vfun_entries (binfo)
2509 return list_length (skip_rtti_stuff (binfo,
2514 /* Called from num_extra_vtbl_entries via dfs_walk. */
2517 dfs_count_virtuals (binfo, data)
2521 /* Non-primary bases are not interesting; all of the virtual
2522 function table entries have been overridden. */
2523 if (!BINFO_PRIMARY_MARKED_P (binfo))
2524 ((vcall_offset_data *) data)->offsets += num_vfun_entries (binfo);
2529 /* Returns the number of extra entries (at negative indices) required
2530 for BINFO's vtable. */
2533 num_extra_vtbl_entries (binfo)
2539 type = BINFO_TYPE (binfo);
2542 /* There is an entry for the offset to each virtual base. */
2543 if (vbase_offsets_in_vtable_p ())
2544 entries += list_length (CLASSTYPE_VBASECLASSES (type));
2546 /* If this is a virtual base, there are entries for each virtual
2547 function defined in this class or its bases. */
2548 if (vcall_offsets_in_vtable_p () && TREE_VIA_VIRTUAL (binfo))
2550 vcall_offset_data vod;
2556 dfs_vcall_offset_queue_p,
2558 entries += vod.offsets;
2561 return entries ? size_int (entries) : size_zero_node;
2564 /* Returns the offset (in bytes) from the beginning of BINFO's vtable
2565 where the vptr should actually point. */
2568 size_extra_vtbl_entries (binfo)
2573 offset = size_binop (EXACT_DIV_EXPR,
2574 TYPE_SIZE (vtable_entry_type),
2575 size_int (BITS_PER_UNIT));
2576 offset = size_binop (MULT_EXPR, offset, num_extra_vtbl_entries (binfo));
2577 return fold (offset);
2580 /* Construct the initializer for BINFOs virtual function table. BINFO
2581 is part of the hierarchy dominated by T. */
2584 build_vtbl_initializer (binfo, t)
2588 tree v = BINFO_VIRTUALS (binfo);
2589 tree inits = NULL_TREE;
2590 tree type = BINFO_TYPE (binfo);
2592 /* Add entries to the vtable that indicate how to adjust the this
2593 pointer when calling a virtual function in this class. */
2594 inits = build_vcall_offset_vtbl_entries (binfo, t);
2596 /* Add entries to the vtable for offsets to our virtual bases. */
2597 inits = chainon (build_vbase_offset_vtbl_entries (binfo, t),
2600 /* Process the RTTI stuff at the head of the list. If we're not
2601 using vtable thunks, then the RTTI entry is just an ordinary
2602 function, and we can process it just like the other virtual
2603 function entries. */
2604 if (!CLASSTYPE_COM_INTERFACE (type) && flag_vtable_thunks)
2609 /* The first entry is an offset. */
2610 offset = TREE_PURPOSE (v);
2611 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2614 /* Convert the offset to look like a function pointer, so that
2615 we can put it in the vtable. */
2616 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2617 TREE_CONSTANT (init) = 1;
2618 init = build_vtable_entry (integer_zero_node, init);
2619 inits = tree_cons (NULL_TREE, init, inits);
2623 if (new_abi_rtti_p ())
2625 tree decl = TREE_VALUE (v);
2628 decl = build_unary_op (ADDR_EXPR, decl, 0);
2630 decl = integer_zero_node;
2631 decl = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
2632 TREE_CONSTANT (decl) = 1;
2633 decl = build_vtable_entry (integer_zero_node, decl);
2634 inits = tree_cons (NULL_TREE, decl, inits);
2638 /* In the old abi the second entry (the tdesc pointer) is
2639 just an ordinary function, so it can be dealt with like the
2640 virtual functions. */
2643 /* Go through all the ordinary virtual functions, building up
2651 /* Pull the offset for `this', and the function to call, out of
2653 delta = TREE_PURPOSE (v);
2654 fn = TREE_VALUE (v);
2655 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2656 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2658 /* You can't call an abstract virtual function; it's abstract.
2659 So, we replace these functions with __pure_virtual. */
2660 if (DECL_PURE_VIRTUAL_P (fn))
2663 /* Package up that information for the vtable. */
2664 init = build_vtable_entry_for_fn (delta, fn);
2665 /* And add it to the chain of initializers. */
2666 inits = tree_cons (NULL_TREE, init, inits);
2672 /* The initializers were built up in reverse order; straighten them
2674 inits = nreverse (inits);
2675 /* Package all the initializers up as an array initializer. */
2676 return build_nt (CONSTRUCTOR, NULL_TREE, inits);
2679 /* Called from finish_vtbls via dfs_walk. */
2682 dfs_finish_vtbls (binfo, data)
2686 if (!BINFO_PRIMARY_MARKED_P (binfo)
2687 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2688 && BINFO_NEW_VTABLE_MARKED (binfo))
2693 layout_vtable_decl (binfo, list_length (BINFO_VIRTUALS (binfo)));
2694 decl = BINFO_VTABLE (binfo);
2695 context = DECL_CONTEXT (decl);
2696 DECL_CONTEXT (decl) = 0;
2697 DECL_INITIAL (decl) = build_vtbl_initializer (binfo, (tree) data);
2698 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
2699 DECL_CONTEXT (decl) = context;
2702 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2703 SET_BINFO_MARKED (binfo);
2708 /* Create all the necessary vtables for T and its base classes. */
2714 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
2715 dfs_unmarked_real_bases_queue_p, t);
2716 dfs_walk (TYPE_BINFO (t), dfs_unmark,
2717 dfs_marked_real_bases_queue_p, t);
2720 /* True if we should override the given BASE_FNDECL with the given
2724 overrides (fndecl, base_fndecl)
2725 tree fndecl, base_fndecl;
2727 /* Destructors have special names. */
2728 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2729 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2731 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2732 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2734 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2736 tree types, base_types;
2738 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2739 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2741 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2742 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2743 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2744 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2745 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2751 /* Returns the BINFO_OFFSET for the base of BINFO that has the same
2755 get_class_offset_1 (parent, binfo, context, t, fndecl)
2756 tree parent, binfo, context, t, fndecl;
2758 tree binfos = BINFO_BASETYPES (binfo);
2759 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2760 tree rval = NULL_TREE;
2762 if (binfo == parent)
2763 return error_mark_node;
2765 for (i = 0; i < n_baselinks; i++)
2767 tree base_binfo = TREE_VEC_ELT (binfos, i);
2770 if (TREE_VIA_VIRTUAL (base_binfo))
2771 base_binfo = BINFO_FOR_VBASE (BINFO_TYPE (base_binfo), t);
2772 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2773 /* See if we have a new value */
2774 if (nrval && (nrval != error_mark_node || rval==0))
2776 /* Only compare if we have two offsets */
2777 if (rval && rval != error_mark_node
2778 && ! tree_int_cst_equal (nrval, rval))
2780 /* Only give error if the two offsets are different */
2781 error ("every virtual function must have a unique final overrider");
2782 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2783 cp_error (" with virtual `%D' from virtual base class", fndecl);
2789 if (rval && BINFO_TYPE (binfo) == context)
2791 my_friendly_assert (rval == error_mark_node
2792 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2793 rval = BINFO_OFFSET (binfo);
2799 /* Called from get_class_offset via dfs_walk. */
2802 dfs_get_class_offset (binfo, data)
2806 tree list = (tree) data;
2807 tree context = TREE_TYPE (list);
2809 if (same_type_p (BINFO_TYPE (binfo), context))
2811 if (TREE_VALUE (list))
2812 return error_mark_node;
2814 TREE_VALUE (list) = BINFO_OFFSET (binfo);
2817 SET_BINFO_MARKED (binfo);
2822 /* Returns the BINFO_OFFSET for the subobject of BINFO that has the
2823 type given by CONTEXT. */
2826 get_class_offset (context, t, binfo, fndecl)
2827 tree context, t, binfo, fndecl;
2834 return integer_zero_node;
2836 if (BINFO_TYPE (binfo) == context)
2837 return BINFO_OFFSET (binfo);
2839 /* Check less derived binfos first. */
2840 while (BINFO_BASETYPES (binfo)
2841 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2843 tree binfos = BINFO_BASETYPES (binfo);
2844 binfo = TREE_VEC_ELT (binfos, i);
2845 if (BINFO_TYPE (binfo) == context)
2846 return BINFO_OFFSET (binfo);
2849 list = build_tree_list (t, NULL_TREE);
2850 TREE_TYPE (list) = context;
2851 offset = dfs_walk (TYPE_BINFO (t),
2852 dfs_get_class_offset,
2853 dfs_unmarked_real_bases_queue_p,
2855 dfs_walk (TYPE_BINFO (t), dfs_unmark, dfs_marked_real_bases_queue_p, t);
2857 if (offset == error_mark_node)
2859 error ("every virtual function must have a unique final overrider");
2860 cp_error (" found two (or more) `%T' class subobjects in `%T'",
2862 cp_error (" with virtual `%D' from virtual base class", fndecl);
2863 offset = integer_zero_node;
2866 offset = TREE_VALUE (list);
2868 my_friendly_assert (offset != NULL_TREE, 999);
2869 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST, 999);
2874 /* Return the BINFO_VIRTUALS list for BINFO, without the RTTI stuff at
2875 the front. If non-NULL, N is set to the number of entries
2879 skip_rtti_stuff (binfo, t, n)
2882 unsigned HOST_WIDE_INT *n;
2886 if (CLASSTYPE_COM_INTERFACE (t))
2891 virtuals = BINFO_VIRTUALS (binfo);
2894 /* We always reserve a slot for the offset/tdesc entry. */
2897 virtuals = TREE_CHAIN (virtuals);
2899 if (flag_vtable_thunks && virtuals)
2901 /* The second slot is reserved for the tdesc pointer when thunks
2905 virtuals = TREE_CHAIN (virtuals);
2912 modify_one_vtable (binfo, t, fndecl)
2913 tree binfo, t, fndecl;
2916 unsigned HOST_WIDE_INT n;
2918 /* If we're support RTTI then we always need a new vtable to point
2919 to the RTTI information. Under the new ABI we may need a new
2920 vtable to contain vcall and vbase offsets. */
2921 if (flag_rtti || flag_new_abi)
2923 if (binfo == TYPE_BINFO (t))
2924 build_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))), t);
2926 prepare_fresh_vtable (binfo, t);
2928 if (fndecl == NULL_TREE)
2931 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), &n);
2935 tree current_fndecl = TREE_VALUE (virtuals);
2937 /* We should never have an instance of __pure_virtual on the
2938 BINFO_VIRTUALS list. If we do, then we will never notice
2939 that the function that should have been there instead has
2941 my_friendly_assert (current_fndecl != abort_fndecl,
2944 if (current_fndecl && overrides (fndecl, current_fndecl))
2946 tree base_offset, offset;
2947 tree context = DECL_CLASS_CONTEXT (fndecl);
2948 tree vfield = TYPE_VFIELD (t);
2951 offset = get_class_offset (context, t, binfo, fndecl);
2953 /* Find the right offset for the this pointer based on the
2954 base class we just found. We have to take into
2955 consideration the virtual base class pointers that we
2956 stick in before the virtual function table pointer.
2958 Also, we want just the delta between the most base class
2959 that we derived this vfield from and us. */
2960 base_offset = size_binop (PLUS_EXPR,
2961 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2962 BINFO_OFFSET (binfo));
2963 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2965 if (binfo == TYPE_BINFO (t))
2966 /* In this case, it is *type*'s vtable we are modifying.
2967 We start with the approximation that it's vtable is
2968 that of the immediate base class. */
2969 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2971 /* This is our very own copy of `basetype' to play with.
2972 Later, we will fill in all the virtual functions that
2973 override the virtual functions in these base classes
2974 which are not defined by the current type. */
2975 prepare_fresh_vtable (binfo, t);
2978 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2980 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2985 virtuals = TREE_CHAIN (virtuals);
2989 /* Called from modify_all_vtables via dfs_walk. */
2992 dfs_modify_vtables (binfo, data)
2996 if (/* There's no need to modify the vtable for a primary base;
2997 we're not going to use that vtable anyhow. */
2998 !BINFO_PRIMARY_MARKED_P (binfo)
2999 /* Similarly, a base without a vtable needs no modification. */
3000 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
3002 tree list = (tree) data;
3003 modify_one_vtable (binfo, TREE_PURPOSE (list), TREE_VALUE (list));
3006 SET_BINFO_MARKED (binfo);
3011 /* Update all of the primary and secondary vtables for T. Create new
3012 vtables as required, and initialize their RTTI information. Each
3013 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
3014 function from a base class; find and modify the appropriate entries
3015 to point to the overriding functions. Returns a list, in
3016 declaration order, of the functions that are overridden in this
3017 class, but do not appear in the primary base class vtable, and
3018 which should therefore be appended to the end of the vtable for T. */
3021 modify_all_vtables (t, has_virtual_p, overridden_virtuals)
3024 tree overridden_virtuals;
3029 binfo = TYPE_BINFO (t);
3031 /* Even if there are no overridden virtuals, we want to go through
3032 the hierarchy updating RTTI information. */
3033 if (!overridden_virtuals && TYPE_CONTAINS_VPTR_P (t) && flag_rtti)
3034 overridden_virtuals = build_tree_list (NULL_TREE, NULL_TREE);
3036 /* Iterate through each of the overriding functions, updating the
3038 for (fns = overridden_virtuals; fns; fns = TREE_CHAIN (fns))
3041 list = build_tree_list (t, TREE_VALUE (fns));
3042 dfs_walk (binfo, dfs_modify_vtables,
3043 dfs_unmarked_real_bases_queue_p, list);
3044 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
3047 /* If we should include overriding functions for secondary vtables
3048 in our primary vtable, add them now. */
3049 if (all_overridden_vfuns_in_vtables_p ())
3051 tree *fnsp = &overridden_virtuals;
3055 tree fn = TREE_VALUE (*fnsp);
3057 if (BINFO_VIRTUALS (binfo)
3058 && !value_member (fn, BINFO_VIRTUALS (binfo)))
3060 /* We know we need a vtable for this class now. */
3061 start_vtable (t, has_virtual_p);
3062 /* Set the vtable index. */
3064 = build_shared_int_cst ((*has_virtual_p)++);
3065 /* We don't need to convert to a base class when calling
3067 DECL_VIRTUAL_CONTEXT (fn) = t;
3068 /* We don't need to adjust the `this' pointer when
3069 calling this function. */
3070 TREE_PURPOSE (*fnsp) = integer_zero_node;
3072 /* This is an overridden function not already in our
3074 fnsp = &TREE_CHAIN (*fnsp);
3077 /* We've already got an entry for this function. Skip
3079 *fnsp = TREE_CHAIN (*fnsp);
3083 overridden_virtuals = NULL_TREE;
3085 return overridden_virtuals;
3088 /* Fixup all the delta entries in this one vtable that need updating. */
3091 dfs_fixup_vtable_deltas (binfo, data)
3096 unsigned HOST_WIDE_INT n;
3097 tree t = (tree) data;
3099 while (BINFO_PRIMARY_MARKED_P (binfo))
3101 binfo = BINFO_INHERITANCE_CHAIN (binfo);
3102 /* If BINFO is virtual then we'll handle this base later. */
3103 if (TREE_VIA_VIRTUAL (binfo))
3107 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), &n);
3111 tree fndecl = TREE_VALUE (virtuals);
3112 tree delta = TREE_PURPOSE (virtuals);
3116 tree base_offset, offset;
3117 tree context = DECL_CLASS_CONTEXT (fndecl);
3118 tree vfield = TYPE_VFIELD (t);
3121 offset = get_class_offset (context, t, binfo, fndecl);
3123 /* Find the right offset for the this pointer based on the
3124 base class we just found. We have to take into
3125 consideration the virtual base class pointers that we
3126 stick in before the virtual function table pointer.
3128 Also, we want just the delta between the most base class
3129 that we derived this vfield from and us. */
3130 base_offset = size_binop (PLUS_EXPR,
3131 get_derived_offset (binfo,
3132 DECL_CONTEXT (fndecl)),
3133 BINFO_OFFSET (binfo));
3134 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
3136 if (! tree_int_cst_equal (this_offset, delta))
3138 /* Make sure we can modify the derived association with immunity. */
3139 if (binfo == TYPE_BINFO (t))
3140 /* In this case, it is *type*'s vtable we are modifying.
3141 We start with the approximation that it's vtable is that
3142 of the immediate base class. */
3143 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
3145 /* This is our very own copy of `basetype' to play
3146 with. Later, we will fill in all the virtual
3147 functions that override the virtual functions in
3148 these base classes which are not defined by the
3150 prepare_fresh_vtable (binfo, t);
3152 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
3158 virtuals = TREE_CHAIN (virtuals);
3164 /* Here, we already know that they match in every respect.
3165 All we have to check is where they had their declarations. */
3168 strictly_overrides (fndecl1, fndecl2)
3169 tree fndecl1, fndecl2;
3171 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
3172 DECL_CLASS_CONTEXT (fndecl1),
3174 if (distance == -2 || distance > 0)
3179 /* Merge overrides for one vtable.
3180 If we want to merge in same function, we are fine.
3182 if one has a DECL_CLASS_CONTEXT that is a parent of the
3183 other, than choose the more derived one
3185 potentially ill-formed (see 10.3 [class.virtual])
3186 we have to check later to see if there was an
3187 override in this class. If there was ok, if not
3188 then it is ill-formed. (mrs)
3190 We take special care to reuse a vtable, if we can. */
3193 override_one_vtable (binfo, old, t)
3200 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
3202 /* Either or both of BINFO or OLD might be primary base classes
3203 because merge_overrides is called with a vbase from the class we
3204 are definining and the corresponding vbase from one of its direct
3207 while (BINFO_PRIMARY_MARKED_P (binfo))
3209 binfo = BINFO_INHERITANCE_CHAIN (binfo);
3210 /* If BINFO is virtual, then we'll handle this virtual base when
3212 if (TREE_VIA_VIRTUAL (binfo))
3215 while (BINFO_PRIMARY_MARKED_P (old))
3216 old = BINFO_INHERITANCE_CHAIN (old);
3218 /* If we have already committed to modifying it, then don't try and
3219 reuse another vtable. */
3220 if (BINFO_NEW_VTABLE_MARKED (binfo))
3223 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), NULL);
3224 old_virtuals = skip_rtti_stuff (old, BINFO_TYPE (binfo), NULL);
3225 orig_virtuals = skip_rtti_stuff (orig_binfo, BINFO_TYPE (binfo), NULL);
3227 while (orig_virtuals)
3229 tree fndecl = TREE_VALUE (virtuals);
3230 tree old_fndecl = TREE_VALUE (old_virtuals);
3232 /* First check to see if they are the same. */
3233 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
3235 /* No need to do anything. */
3237 else if (strictly_overrides (fndecl, old_fndecl))
3239 if (choose == UNDECIDED)
3241 else if (choose == REUSE_OLD)
3244 if (! BINFO_NEW_VTABLE_MARKED (binfo))
3246 prepare_fresh_vtable (binfo, t);
3247 override_one_vtable (binfo, old, t);
3252 else if (strictly_overrides (old_fndecl, fndecl))
3254 if (choose == UNDECIDED)
3256 else if (choose == REUSE_NEW)
3259 if (! BINFO_NEW_VTABLE_MARKED (binfo))
3261 prepare_fresh_vtable (binfo, t);
3262 override_one_vtable (binfo, old, t);
3265 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
3267 else if (choose == NEITHER)
3269 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
3275 if (! BINFO_NEW_VTABLE_MARKED (binfo))
3277 prepare_fresh_vtable (binfo, t);
3278 override_one_vtable (binfo, old, t);
3282 /* This MUST be overridden, or the class is ill-formed. */
3283 tree fndecl = TREE_VALUE (virtuals);
3285 fndecl = copy_node (fndecl);
3286 copy_lang_decl (fndecl);
3287 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
3288 /* Make sure we search for it later. */
3289 if (! CLASSTYPE_PURE_VIRTUALS (t))
3290 CLASSTYPE_PURE_VIRTUALS (t) = error_mark_node;
3292 /* We can use integer_zero_node, as we will core dump
3293 if this is used anyway. */
3294 TREE_PURPOSE (virtuals) = integer_zero_node;
3295 TREE_VALUE (virtuals) = fndecl;
3298 virtuals = TREE_CHAIN (virtuals);
3299 old_virtuals = TREE_CHAIN (old_virtuals);
3300 orig_virtuals = TREE_CHAIN (orig_virtuals);
3303 /* Let's reuse the old vtable. */
3304 if (choose == REUSE_OLD)
3306 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
3307 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
3311 /* Merge in overrides for virtual bases.
3312 BINFO is the hierarchy we want to modify, and OLD has the potential
3316 merge_overrides (binfo, old, do_self, t)
3321 tree binfos = BINFO_BASETYPES (binfo);
3322 tree old_binfos = BINFO_BASETYPES (old);
3323 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3325 /* Should we use something besides CLASSTYPE_VFIELDS? */
3326 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
3328 override_one_vtable (binfo, old, t);
3331 for (i = 0; i < n_baselinks; i++)
3333 tree base_binfo = TREE_VEC_ELT (binfos, i);
3334 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
3335 int is_not_base_vtable
3336 = !BINFO_PRIMARY_MARKED_P (base_binfo);
3337 if (! TREE_VIA_VIRTUAL (base_binfo))
3338 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
3342 /* Get the base virtual function declarations in T that are either
3343 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
3344 the overrider/hider. */
3347 get_basefndecls (fndecl, t)
3350 tree methods = TYPE_METHODS (t);
3351 tree base_fndecls = NULL_TREE;
3352 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3353 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3357 if (TREE_CODE (methods) == FUNCTION_DECL
3358 && DECL_VINDEX (methods) != NULL_TREE
3359 && DECL_NAME (fndecl) == DECL_NAME (methods))
3360 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
3362 methods = TREE_CHAIN (methods);
3366 return base_fndecls;
3368 for (i = 0; i < n_baseclasses; i++)
3370 tree base_binfo = TREE_VEC_ELT (binfos, i);
3371 tree basetype = BINFO_TYPE (base_binfo);
3373 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3377 return base_fndecls;
3380 /* Mark the functions that have been hidden with their overriders.
3381 Since we start out with all functions already marked with a hider,
3382 no need to mark functions that are just hidden.
3384 Subroutine of warn_hidden. */
3387 mark_overriders (fndecl, base_fndecls)
3388 tree fndecl, base_fndecls;
3390 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3392 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
3393 TREE_PURPOSE (base_fndecls) = fndecl;
3397 /* If this declaration supersedes the declaration of
3398 a method declared virtual in the base class, then
3399 mark this field as being virtual as well. */
3402 check_for_override (decl, ctype)
3405 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
3406 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3407 int virtualp = DECL_VIRTUAL_P (decl);
3408 int found_overriden_fn = 0;
3410 for (i = 0; i < n_baselinks; i++)
3412 tree base_binfo = TREE_VEC_ELT (binfos, i);
3413 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
3415 tree tmp = get_matching_virtual
3417 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
3419 if (tmp && !found_overriden_fn)
3421 /* If this function overrides some virtual in some base
3422 class, then the function itself is also necessarily
3423 virtual, even if the user didn't explicitly say so. */
3424 DECL_VIRTUAL_P (decl) = 1;
3426 /* The TMP we really want is the one from the deepest
3427 baseclass on this path, taking care not to
3428 duplicate if we have already found it (via another
3429 path to its virtual baseclass. */
3430 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
3432 cp_error_at ("`static %#D' cannot be declared", decl);
3433 cp_error_at (" since `virtual %#D' declared in base class",
3439 /* Set DECL_VINDEX to a value that is neither an
3440 INTEGER_CST nor the error_mark_node so that
3441 add_virtual_function will realize this is an
3442 overridden function. */
3444 = tree_cons (tmp, NULL_TREE, DECL_VINDEX (decl));
3446 /* We now know that DECL overrides something,
3447 which is all that is important. But, we must
3448 continue to iterate through all the base-classes
3449 in order to allow get_matching_virtual to check for
3450 various illegal overrides. */
3451 found_overriden_fn = 1;
3457 if (DECL_VINDEX (decl) == NULL_TREE)
3458 DECL_VINDEX (decl) = error_mark_node;
3459 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3463 /* Warn about hidden virtual functions that are not overridden in t.
3464 We know that constructors and destructors don't apply. */
3470 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3471 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3474 /* We go through each separately named virtual function. */
3475 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3477 tree fns = TREE_VEC_ELT (method_vec, i);
3478 tree fndecl = NULL_TREE;
3480 tree base_fndecls = NULL_TREE;
3481 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3482 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3484 /* First see if we have any virtual functions in this batch. */
3485 for (; fns; fns = OVL_NEXT (fns))
3487 fndecl = OVL_CURRENT (fns);
3488 if (DECL_VINDEX (fndecl))
3492 if (fns == NULL_TREE)
3495 /* First we get a list of all possible functions that might be
3496 hidden from each base class. */
3497 for (i = 0; i < n_baseclasses; i++)
3499 tree base_binfo = TREE_VEC_ELT (binfos, i);
3500 tree basetype = BINFO_TYPE (base_binfo);
3502 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3506 fns = OVL_NEXT (fns);
3508 /* ...then mark up all the base functions with overriders, preferring
3509 overriders to hiders. */
3511 for (; fns; fns = OVL_NEXT (fns))
3513 fndecl = OVL_CURRENT (fns);
3514 if (DECL_VINDEX (fndecl))
3515 mark_overriders (fndecl, base_fndecls);
3518 /* Now give a warning for all base functions without overriders,
3519 as they are hidden. */
3520 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3522 if (! overrides (TREE_PURPOSE (base_fndecls),
3523 TREE_VALUE (base_fndecls)))
3525 /* Here we know it is a hider, and no overrider exists. */
3526 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3527 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3533 /* Check for things that are invalid. There are probably plenty of other
3534 things we should check for also. */
3537 finish_struct_anon (t)
3542 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3544 if (TREE_STATIC (field))
3546 if (TREE_CODE (field) != FIELD_DECL)
3549 if (DECL_NAME (field) == NULL_TREE
3550 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3552 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3553 for (; elt; elt = TREE_CHAIN (elt))
3555 if (DECL_ARTIFICIAL (elt))
3558 if (DECL_NAME (elt) == constructor_name (t))
3559 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3562 if (TREE_CODE (elt) != FIELD_DECL)
3564 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3569 if (TREE_PRIVATE (elt))
3570 cp_pedwarn_at ("private member `%#D' in anonymous union",
3572 else if (TREE_PROTECTED (elt))
3573 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3576 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3577 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3583 extern int interface_only, interface_unknown;
3585 /* Create default constructors, assignment operators, and so forth for
3586 the type indicated by T, if they are needed.
3587 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3588 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3589 cannot have a default constructor, copy constructor taking a const
3590 reference argument, or an assignment operator, respectively. If a
3591 virtual destructor is created, its DECL is returned; otherwise the
3592 return value is NULL_TREE. */
3595 add_implicitly_declared_members (t, cant_have_default_ctor,
3596 cant_have_const_cctor,
3597 cant_have_assignment)
3599 int cant_have_default_ctor;
3600 int cant_have_const_cctor;
3601 int cant_have_assignment;
3604 tree implicit_fns = NULL_TREE;
3605 tree name = TYPE_IDENTIFIER (t);
3606 tree virtual_dtor = NULL_TREE;
3610 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3612 default_fn = cons_up_default_function (t, name, 0);
3613 check_for_override (default_fn, t);
3615 /* If we couldn't make it work, then pretend we didn't need it. */
3616 if (default_fn == void_type_node)
3617 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3620 TREE_CHAIN (default_fn) = implicit_fns;
3621 implicit_fns = default_fn;
3623 if (DECL_VINDEX (default_fn))
3624 virtual_dtor = default_fn;
3627 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3629 /* Default constructor. */
3630 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3632 default_fn = cons_up_default_function (t, name, 2);
3633 TREE_CHAIN (default_fn) = implicit_fns;
3634 implicit_fns = default_fn;
3637 /* Copy constructor. */
3638 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3640 /* ARM 12.18: You get either X(X&) or X(const X&), but
3642 default_fn = cons_up_default_function (t, name,
3643 3 + cant_have_const_cctor);
3644 TREE_CHAIN (default_fn) = implicit_fns;
3645 implicit_fns = default_fn;
3648 /* Assignment operator. */
3649 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3651 default_fn = cons_up_default_function (t, name,
3652 5 + cant_have_assignment);
3653 TREE_CHAIN (default_fn) = implicit_fns;
3654 implicit_fns = default_fn;
3657 /* Now, hook all of the new functions on to TYPE_METHODS,
3658 and add them to the CLASSTYPE_METHOD_VEC. */
3659 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3660 add_method (t, 0, *f);
3661 *f = TYPE_METHODS (t);
3662 TYPE_METHODS (t) = implicit_fns;
3664 return virtual_dtor;
3667 /* Subroutine of finish_struct_1. Recursively count the number of fields
3668 in TYPE, including anonymous union members. */
3671 count_fields (fields)
3676 for (x = fields; x; x = TREE_CHAIN (x))
3678 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3679 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3686 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3687 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3690 add_fields_to_vec (fields, field_vec, idx)
3691 tree fields, field_vec;
3695 for (x = fields; x; x = TREE_CHAIN (x))
3697 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3698 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3700 TREE_VEC_ELT (field_vec, idx++) = x;
3705 /* FIELD is a bit-field. We are finishing the processing for its
3706 enclosing type. Issue any appropriate messages and set appropriate
3710 check_bitfield_decl (field)
3713 tree type = TREE_TYPE (field);
3715 /* Invalid bit-field size done by grokfield. */
3716 /* Detect invalid bit-field type. Simply checking if TYPE is
3717 integral is insufficient, as that is the array core of the field
3718 type. If TREE_TYPE (field) is integral, then TYPE must be the same. */
3719 if (DECL_INITIAL (field)
3720 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3722 cp_error_at ("bit-field `%#D' with non-integral type", field);
3723 DECL_INITIAL (field) = NULL;
3726 /* Detect and ignore out of range field width. */
3727 if (DECL_INITIAL (field))
3729 tree w = DECL_INITIAL (field);
3730 register int width = 0;
3732 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3735 /* detect invalid field size. */
3736 if (TREE_CODE (w) == CONST_DECL)
3737 w = DECL_INITIAL (w);
3738 else if (TREE_READONLY_DECL_P (w))
3739 w = decl_constant_value (w);
3741 if (TREE_CODE (w) != INTEGER_CST)
3743 cp_error_at ("bit-field `%D' width not an integer constant",
3745 DECL_INITIAL (field) = NULL_TREE;
3747 else if (width = TREE_INT_CST_LOW (w),
3750 DECL_INITIAL (field) = NULL;
3751 cp_error_at ("negative width in bit-field `%D'", field);
3753 else if (width == 0 && DECL_NAME (field) != 0)
3755 DECL_INITIAL (field) = NULL;
3756 cp_error_at ("zero width for bit-field `%D'", field);
3759 > TYPE_PRECISION (long_long_unsigned_type_node))
3761 /* The backend will dump if you try to use something too
3763 DECL_INITIAL (field) = NULL;
3764 sorry ("bit-fields larger than %d bits",
3765 TYPE_PRECISION (long_long_unsigned_type_node));
3766 cp_error_at (" in declaration of `%D'", field);
3768 else if (width > TYPE_PRECISION (type)
3769 && TREE_CODE (type) != ENUMERAL_TYPE
3770 && TREE_CODE (type) != BOOLEAN_TYPE)
3771 cp_warning_at ("width of `%D' exceeds its type", field);
3772 else if (TREE_CODE (type) == ENUMERAL_TYPE
3773 && ((min_precision (TYPE_MIN_VALUE (type),
3774 TREE_UNSIGNED (type)) > width)
3775 || (min_precision (TYPE_MAX_VALUE (type),
3776 TREE_UNSIGNED (type)) > width)))
3777 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3780 if (DECL_INITIAL (field))
3782 DECL_INITIAL (field) = NULL_TREE;
3783 DECL_FIELD_SIZE (field) = width;
3784 DECL_BIT_FIELD (field) = 1;
3788 #ifdef EMPTY_FIELD_BOUNDARY
3789 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3790 EMPTY_FIELD_BOUNDARY);
3792 #ifdef PCC_BITFIELD_TYPE_MATTERS
3793 if (PCC_BITFIELD_TYPE_MATTERS)
3794 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3801 /* Non-bit-fields are aligned for their type. */
3802 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3805 /* FIELD is a non bit-field. We are finishing the processing for its
3806 enclosing type T. Issue any appropriate messages and set appropriate
3810 check_field_decl (field, t, cant_have_const_ctor,
3811 cant_have_default_ctor, no_const_asn_ref,
3812 any_default_members)
3815 int *cant_have_const_ctor;
3816 int *cant_have_default_ctor;
3817 int *no_const_asn_ref;
3818 int *any_default_members;
3820 tree type = strip_array_types (TREE_TYPE (field));
3822 /* An anonymous union cannot contain any fields which would change
3823 the settings of CANT_HAVE_CONST_CTOR and friends. */
3824 if (ANON_UNION_TYPE_P (type))
3826 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3827 structs. So, we recurse through their fields here. */
3828 else if (ANON_AGGR_TYPE_P (type))
3832 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3833 if (TREE_CODE (field) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3834 check_field_decl (fields, t, cant_have_const_ctor,
3835 cant_have_default_ctor, no_const_asn_ref,
3836 any_default_members);
3838 /* Check members with class type for constructors, destructors,
3840 else if (CLASS_TYPE_P (type))
3842 /* Never let anything with uninheritable virtuals
3843 make it through without complaint. */
3844 abstract_virtuals_error (field, type);
3846 if (TREE_CODE (t) == UNION_TYPE)
3848 if (TYPE_NEEDS_CONSTRUCTING (type))
3849 cp_error_at ("member `%#D' with constructor not allowed in union",
3851 if (TYPE_NEEDS_DESTRUCTOR (type))
3852 cp_error_at ("member `%#D' with destructor not allowed in union",
3854 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3855 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3860 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3861 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3862 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3863 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3866 if (!TYPE_HAS_CONST_INIT_REF (type))
3867 *cant_have_const_ctor = 1;
3869 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3870 *no_const_asn_ref = 1;
3872 if (TYPE_HAS_CONSTRUCTOR (type)
3873 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3874 *cant_have_default_ctor = 1;
3876 if (DECL_INITIAL (field) != NULL_TREE)
3878 /* `build_class_init_list' does not recognize
3880 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3881 cp_error_at ("multiple fields in union `%T' initialized");
3882 *any_default_members = 1;
3885 /* Non-bit-fields are aligned for their type, except packed fields
3886 which require only BITS_PER_UNIT alignment. */
3887 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3888 (DECL_PACKED (field)
3890 : TYPE_ALIGN (TREE_TYPE (field))));
3893 /* Check the data members (both static and non-static), class-scoped
3894 typedefs, etc., appearing in the declaration of T. Issue
3895 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3896 declaration order) of access declarations; each TREE_VALUE in this
3897 list is a USING_DECL.
3899 In addition, set the following flags:
3902 The class is empty, i.e., contains no non-static data members.
3904 CANT_HAVE_DEFAULT_CTOR_P
3905 This class cannot have an implicitly generated default
3908 CANT_HAVE_CONST_CTOR_P
3909 This class cannot have an implicitly generated copy constructor
3910 taking a const reference.
3912 CANT_HAVE_CONST_ASN_REF
3913 This class cannot have an implicitly generated assignment
3914 operator taking a const reference.
3916 All of these flags should be initialized before calling this
3919 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3920 fields can be added by adding to this chain. */
3923 check_field_decls (t, access_decls, empty_p,
3924 cant_have_default_ctor_p, cant_have_const_ctor_p,
3929 int *cant_have_default_ctor_p;
3930 int *cant_have_const_ctor_p;
3931 int *no_const_asn_ref_p;
3936 int any_default_members;
3938 /* First, delete any duplicate fields. */
3939 delete_duplicate_fields (TYPE_FIELDS (t));
3941 /* Assume there are no access declarations. */
3942 *access_decls = NULL_TREE;
3943 /* Assume this class has no pointer members. */
3945 /* Assume none of the members of this class have default
3947 any_default_members = 0;
3949 for (field = &TYPE_FIELDS (t); *field; field = next)
3952 tree type = TREE_TYPE (x);
3954 GNU_xref_member (current_class_name, x);
3956 next = &TREE_CHAIN (x);
3958 if (TREE_CODE (x) == FIELD_DECL)
3960 DECL_PACKED (x) |= TYPE_PACKED (t);
3962 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3963 /* We don't treat zero-width bitfields as making a class
3968 /* The class is non-empty. */
3970 /* The class is not even nearly empty. */
3971 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3975 if (TREE_CODE (x) == USING_DECL)
3977 /* Prune the access declaration from the list of fields. */
3978 *field = TREE_CHAIN (x);
3980 /* Save the access declarations for our caller. */
3981 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3983 /* Since we've reset *FIELD there's no reason to skip to the
3989 if (TREE_CODE (x) == TYPE_DECL
3990 || TREE_CODE (x) == TEMPLATE_DECL)
3993 /* If we've gotten this far, it's a data member, possibly static,
3994 or an enumerator. */
3996 DECL_FIELD_CONTEXT (x) = t;
3998 /* ``A local class cannot have static data members.'' ARM 9.4 */
3999 if (current_function_decl && TREE_STATIC (x))
4000 cp_error_at ("field `%D' in local class cannot be static", x);
4002 /* Perform error checking that did not get done in
4004 if (TREE_CODE (type) == FUNCTION_TYPE)
4006 cp_error_at ("field `%D' invalidly declared function type",
4008 type = build_pointer_type (type);
4009 TREE_TYPE (x) = type;
4011 else if (TREE_CODE (type) == METHOD_TYPE)
4013 cp_error_at ("field `%D' invalidly declared method type", x);
4014 type = build_pointer_type (type);
4015 TREE_TYPE (x) = type;
4017 else if (TREE_CODE (type) == OFFSET_TYPE)
4019 cp_error_at ("field `%D' invalidly declared offset type", x);
4020 type = build_pointer_type (type);
4021 TREE_TYPE (x) = type;
4024 if (type == error_mark_node)
4027 DECL_SAVED_INSNS (x) = 0;
4028 DECL_FIELD_SIZE (x) = 0;
4030 /* When this goes into scope, it will be a non-local reference. */
4031 DECL_NONLOCAL (x) = 1;
4033 if (TREE_CODE (x) == CONST_DECL)
4036 if (TREE_CODE (x) == VAR_DECL)
4038 if (TREE_CODE (t) == UNION_TYPE)
4039 /* Unions cannot have static members. */
4040 cp_error_at ("field `%D' declared static in union", x);
4045 /* Now it can only be a FIELD_DECL. */
4047 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
4048 CLASSTYPE_NON_AGGREGATE (t) = 1;
4050 /* If this is of reference type, check if it needs an init.
4051 Also do a little ANSI jig if necessary. */
4052 if (TREE_CODE (type) == REFERENCE_TYPE)
4054 CLASSTYPE_NON_POD_P (t) = 1;
4055 if (DECL_INITIAL (x) == NULL_TREE)
4056 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
4058 /* ARM $12.6.2: [A member initializer list] (or, for an
4059 aggregate, initialization by a brace-enclosed list) is the
4060 only way to initialize nonstatic const and reference
4062 *cant_have_default_ctor_p = 1;
4063 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
4065 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
4068 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
4070 cp_warning_at ("non-static reference in class without a constructor", x);
4074 type = strip_array_types (type);
4076 if (TREE_CODE (type) == POINTER_TYPE)
4079 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
4080 CLASSTYPE_HAS_MUTABLE (t) = 1;
4082 if (! pod_type_p (type)
4083 /* For some reason, pointers to members are POD types themselves,
4084 but are not allowed in POD structs. Silly. */
4085 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
4086 CLASSTYPE_NON_POD_P (t) = 1;
4088 /* If any field is const, the structure type is pseudo-const. */
4089 if (CP_TYPE_CONST_P (type))
4091 C_TYPE_FIELDS_READONLY (t) = 1;
4092 if (DECL_INITIAL (x) == NULL_TREE)
4093 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
4095 /* ARM $12.6.2: [A member initializer list] (or, for an
4096 aggregate, initialization by a brace-enclosed list) is the
4097 only way to initialize nonstatic const and reference
4099 *cant_have_default_ctor_p = 1;
4100 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
4102 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
4105 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
4107 cp_warning_at ("non-static const member in class without a constructor", x);
4110 /* A field that is pseudo-const makes the structure likewise. */
4111 else if (IS_AGGR_TYPE (type))
4113 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4114 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4115 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
4118 /* We set DECL_C_BIT_FIELD in grokbitfield.
4119 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4120 if (DECL_C_BIT_FIELD (x))
4121 check_bitfield_decl (x);
4123 check_field_decl (x, t,
4124 cant_have_const_ctor_p,
4125 cant_have_default_ctor_p,
4127 &any_default_members);
4130 /* Effective C++ rule 11. */
4131 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
4132 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
4134 cp_warning ("`%#T' has pointer data members", t);
4136 if (! TYPE_HAS_INIT_REF (t))
4138 cp_warning (" but does not override `%T(const %T&)'", t, t);
4139 if (! TYPE_HAS_ASSIGN_REF (t))
4140 cp_warning (" or `operator=(const %T&)'", t);
4142 else if (! TYPE_HAS_ASSIGN_REF (t))
4143 cp_warning (" but does not override `operator=(const %T&)'", t);
4147 /* Check anonymous struct/anonymous union fields. */
4148 finish_struct_anon (t);
4150 /* We've built up the list of access declarations in reverse order.
4152 *access_decls = nreverse (*access_decls);
4155 /* Return a FIELD_DECL for a pointer-to-virtual-table or
4156 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
4157 field are as indicated. The CLASS_TYPE in which this field occurs
4158 is also indicated. *EMPTY_P is set to a non-zero value by this
4159 function to indicate that a class containing this field is
4163 build_vtbl_or_vbase_field (name, assembler_name, type, class_type,
4166 tree assembler_name;
4173 /* This class is non-empty. */
4176 /* Build the FIELD_DECL. */
4177 field = build_lang_decl (FIELD_DECL, name, type);
4178 DECL_ASSEMBLER_NAME (field) = assembler_name;
4179 DECL_VIRTUAL_P (field) = 1;
4180 DECL_ARTIFICIAL (field) = 1;
4181 DECL_FIELD_CONTEXT (field) = class_type;
4182 DECL_CLASS_CONTEXT (field) = class_type;
4183 DECL_FCONTEXT (field) = class_type;
4184 DECL_SAVED_INSNS (field) = 0;
4185 DECL_FIELD_SIZE (field) = 0;
4186 DECL_ALIGN (field) = TYPE_ALIGN (type);
4192 /* If the empty base field in DECL overlaps with a base of the same type in
4193 NEWDECL, which is either another base field or the first data field of
4194 the class, pad the base just before NEWDECL and return 1. Otherwise,
4198 avoid_overlap (decl, newdecl, empty_p)
4204 if (newdecl == NULL_TREE
4205 || ! types_overlap_p (TREE_TYPE (decl), TREE_TYPE (newdecl)))
4208 for (field = decl; TREE_CHAIN (field) && TREE_CHAIN (field) != newdecl;
4209 field = TREE_CHAIN (field))
4212 DECL_SIZE (field) = integer_one_node;
4213 /* The containing class cannot be empty; this field takes up space. */
4219 /* Build a FIELD_DECL for the base given by BINFO in T. If the new
4220 object is non-empty, clear *EMPTY_P. Otherwise, set *SAW_EMPTY_P.
4221 *BASE_ALIGN is a running maximum of the alignments of any base
4225 build_base_field (t, binfo, empty_p, saw_empty_p, base_align)
4230 unsigned int *base_align;
4232 tree basetype = BINFO_TYPE (binfo);
4235 if (TYPE_SIZE (basetype) == 0)
4236 /* This error is now reported in xref_tag, thus giving better
4237 location information. */
4240 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
4241 DECL_ARTIFICIAL (decl) = 1;
4242 DECL_FIELD_CONTEXT (decl) = DECL_CLASS_CONTEXT (decl) = t;
4243 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4244 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4246 if (flag_new_abi && DECL_SIZE (decl) == integer_zero_node)
4252 /* The containing class is non-empty because it has a non-empty base
4258 /* Brain damage for backwards compatibility. For no good
4259 reason, the old layout_basetypes made every base at least
4260 as large as the alignment for the bases up to that point,
4261 gratuitously wasting space. So we do the same thing
4263 *base_align = MAX (*base_align, DECL_ALIGN (decl));
4265 = size_int (MAX (TREE_INT_CST_LOW (DECL_SIZE (decl)),
4266 (int) (*base_align)));
4272 /* Returns a list of fields to stand in for the base class subobjects
4273 of REC. These fields are later removed by layout_basetypes. */
4276 build_base_fields (rec, empty_p)
4280 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4282 tree base_decls = NULL_TREE;
4283 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
4284 tree decl, nextdecl;
4285 int i, saw_empty = 0;
4286 unsigned int base_align = 0;
4288 /* Under the new ABI, the primary base class is always allocated
4290 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
4294 primary_base = CLASSTYPE_PRIMARY_BINFO (rec);
4295 base_decls = chainon (build_base_field (rec,
4303 /* Now allocate the rest of the bases. */
4304 for (i = 0; i < n_baseclasses; ++i)
4308 /* Under the new ABI, the primary base was already allocated
4309 above, so we don't need to allocate it again here. */
4310 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
4313 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4315 /* A primary virtual base class is allocated just like any other
4316 base class, but a non-primary virtual base is allocated
4317 later, in layout_basetypes. */
4318 if (TREE_VIA_VIRTUAL (base_binfo)
4319 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4322 base_decls = chainon (build_base_field (rec, base_binfo,
4329 /* Reverse the list of fields so we allocate the bases in the proper
4331 base_decls = nreverse (base_decls);
4333 /* In the presence of empty base classes, we run the risk of allocating
4334 two objects of the same class on top of one another. Avoid that. */
4335 if (flag_new_abi && saw_empty)
4336 for (decl = base_decls; decl; decl = TREE_CHAIN (decl))
4338 if (DECL_SIZE (decl) == integer_zero_node)
4340 /* First step through the following bases until we find
4341 an overlap or a non-empty base. */
4342 for (nextdecl = TREE_CHAIN (decl); nextdecl;
4343 nextdecl = TREE_CHAIN (nextdecl))
4345 if (avoid_overlap (decl, nextdecl, empty_p)
4346 || DECL_SIZE (nextdecl) != integer_zero_node)
4350 /* If we're still looking, also check against the first
4352 for (nextdecl = TYPE_FIELDS (rec);
4353 nextdecl && TREE_CODE (nextdecl) != FIELD_DECL;
4354 nextdecl = TREE_CHAIN (nextdecl))
4356 avoid_overlap (decl, nextdecl, empty_p);
4364 /* Go through the TYPE_METHODS of T issuing any appropriate
4365 diagnostics, figuring out which methods override which other
4366 methods, and so forth. */
4374 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4376 GNU_xref_member (current_class_name, x);
4378 /* If this was an evil function, don't keep it in class. */
4379 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4382 /* Do both of these, even though they're in the same union;
4383 if the insn `r' member and the size `i' member are
4384 different sizes, as on the alpha, the larger of the two
4385 will end up with garbage in it. */
4386 DECL_SAVED_INSNS (x) = 0;
4387 DECL_FIELD_SIZE (x) = 0;
4389 check_for_override (x, t);
4390 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4391 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4393 /* The name of the field is the original field name
4394 Save this in auxiliary field for later overloading. */
4395 if (DECL_VINDEX (x))
4397 TYPE_POLYMORPHIC_P (t) = 1;
4398 if (DECL_PURE_VIRTUAL_P (x))
4399 CLASSTYPE_PURE_VIRTUALS (t)
4400 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4405 /* Remove all zero-width bit-fields from T. */
4408 remove_zero_width_bit_fields (t)
4413 fieldsp = &TYPE_FIELDS (t);
4416 if (TREE_CODE (*fieldsp) == FIELD_DECL
4417 && DECL_C_BIT_FIELD (*fieldsp)
4418 && DECL_INITIAL (*fieldsp))
4419 *fieldsp = TREE_CHAIN (*fieldsp);
4421 fieldsp = &TREE_CHAIN (*fieldsp);
4425 /* Check the validity of the bases and members declared in T. Add any
4426 implicitly-generated functions (like copy-constructors and
4427 assignment operators). Compute various flag bits (like
4428 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4429 level: i.e., independently of the ABI in use. */
4432 check_bases_and_members (t, empty_p)
4436 /* Nonzero if we are not allowed to generate a default constructor
4438 int cant_have_default_ctor;
4439 /* Nonzero if the implicitly generated copy constructor should take
4440 a non-const reference argument. */
4441 int cant_have_const_ctor;
4442 /* Nonzero if the the implicitly generated assignment operator
4443 should take a non-const reference argument. */
4444 int no_const_asn_ref;
4447 /* By default, we use const reference arguments and generate default
4449 cant_have_default_ctor = 0;
4450 cant_have_const_ctor = 0;
4451 no_const_asn_ref = 0;
4453 /* Assume that the class is nearly empty; we'll clear this flag if
4454 it turns out not to be nearly empty. */
4455 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4457 /* Check all the base-classes. */
4458 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4461 /* Check all the data member declarations. */
4462 check_field_decls (t, &access_decls, empty_p,
4463 &cant_have_default_ctor,
4464 &cant_have_const_ctor,
4467 /* Check all the method declarations. */
4470 /* A nearly-empty class has to be vptr-containing; a nearly empty
4471 class contains just a vptr. */
4472 if (!TYPE_CONTAINS_VPTR_P (t))
4473 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4475 /* Do some bookkeeping that will guide the generation of implicitly
4476 declared member functions. */
4477 TYPE_HAS_COMPLEX_INIT_REF (t)
4478 |= (TYPE_HAS_INIT_REF (t)
4479 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4480 || TYPE_POLYMORPHIC_P (t));
4481 TYPE_NEEDS_CONSTRUCTING (t)
4482 |= (TYPE_HAS_CONSTRUCTOR (t)
4483 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4484 || TYPE_POLYMORPHIC_P (t));
4485 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4486 || TYPE_POLYMORPHIC_P (t));
4487 CLASSTYPE_NON_POD_P (t)
4488 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4489 || TYPE_HAS_ASSIGN_REF (t));
4490 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4491 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4492 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4494 /* Synthesize any needed methods. Note that methods will be synthesized
4495 for anonymous unions; grok_x_components undoes that. */
4496 add_implicitly_declared_members (t, cant_have_default_ctor,
4497 cant_have_const_ctor,
4500 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4501 finish_struct_methods (t);
4503 /* Process the access-declarations. We wait until now to do this
4504 because handle_using_decls requires that the CLASSTYPE_METHOD_VEC
4505 be set up correctly. */
4506 while (access_decls)
4508 handle_using_decl (TREE_VALUE (access_decls), t);
4509 access_decls = TREE_CHAIN (access_decls);
4513 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4514 accordingly, and, if necessary, add the TYPE_VFIELD to the
4515 TYPE_FIELDS list. */
4518 create_vtable_ptr (t, empty_p, has_virtual_p,
4519 new_virtuals_p, overridden_virtuals_p)
4523 tree *new_virtuals_p;
4524 tree *overridden_virtuals_p;
4528 /* Loop over the virtual functions, adding them to our various
4530 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4531 if (DECL_VINDEX (fn))
4532 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4533 has_virtual_p, fn, t);
4535 /* Even if there weren't any new virtual functions, we might need a
4536 new virtual function table if we're supposed to include vptrs in
4537 all classes that need them. */
4538 if (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())
4539 start_vtable (t, has_virtual_p);
4541 /* If we couldn't find an appropriate base class, create a new field
4543 if (*has_virtual_p && !TYPE_VFIELD (t))
4545 /* We build this decl with vtbl_ptr_type_node, which is a
4546 `vtable_entry_type*'. It might seem more precise to use
4547 `vtable_entry_type (*)[N]' where N is the number of firtual
4548 functions. However, that would require the vtable pointer in
4549 base classes to have a different type than the vtable pointer
4550 in derived classes. We could make that happen, but that
4551 still wouldn't solve all the problems. In particular, the
4552 type-based alias analysis code would decide that assignments
4553 to the base class vtable pointer can't alias assignments to
4554 the derived class vtable pointer, since they have different
4555 types. Thus, in an derived class destructor, where the base
4556 class constructor was inlined, we could generate bad code for
4557 setting up the vtable pointer.
4559 Therefore, we use one type for all vtable pointers. We still
4560 use a type-correct type; it's just doesn't indicate the array
4561 bounds. That's better than using `void*' or some such; it's
4562 cleaner, and it let's the alias analysis code know that these
4563 stores cannot alias stores to void*! */
4565 = build_vtbl_or_vbase_field (get_vfield_name (t),
4566 get_identifier (VFIELD_BASE),
4571 /* Add the new field to the list of fields in this class. */
4573 /* In the old ABI, the vtable pointer goes at the end of the
4575 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), TYPE_VFIELD (t));
4578 /* But in the new ABI, the vtable pointer is the first thing
4580 TYPE_FIELDS (t) = chainon (TYPE_VFIELD (t), TYPE_FIELDS (t));
4581 /* If there were any baseclasses, they can't possibly be at
4582 offset zero any more, because that's where the vtable
4583 pointer is. So, converting to a base class is going to
4585 if (CLASSTYPE_N_BASECLASSES (t))
4586 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4589 /* We can't yet add this new field to the list of all virtual
4590 function table pointers in this class. The
4591 modify_all_vtables function depends on this not being done.
4592 So, it is done later, in finish_struct_1. */
4596 /* Fixup the inline function given by INFO now that the class is
4600 fixup_pending_inline (info)
4601 struct pending_inline *info;
4606 tree fn = info->fndecl;
4608 args = DECL_ARGUMENTS (fn);
4611 DECL_CONTEXT (args) = fn;
4612 args = TREE_CHAIN (args);
4617 /* Fixup the inline methods and friends in TYPE now that TYPE is
4621 fixup_inline_methods (type)
4624 tree method = TYPE_METHODS (type);
4626 if (method && TREE_CODE (method) == TREE_VEC)
4628 if (TREE_VEC_ELT (method, 1))
4629 method = TREE_VEC_ELT (method, 1);
4630 else if (TREE_VEC_ELT (method, 0))
4631 method = TREE_VEC_ELT (method, 0);
4633 method = TREE_VEC_ELT (method, 2);
4636 /* Do inline member functions. */
4637 for (; method; method = TREE_CHAIN (method))
4638 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
4641 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4643 method = TREE_CHAIN (method))
4644 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
4645 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4648 /* Called from propagate_binfo_offsets via dfs_walk. */
4651 dfs_propagate_binfo_offsets (binfo, data)
4655 tree offset = (tree) data;
4657 /* Update the BINFO_OFFSET for this base. */
4658 BINFO_OFFSET (binfo)
4659 = size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), offset);
4661 SET_BINFO_MARKED (binfo);
4666 /* Add OFFSET to all base types of BINFO which is a base in the
4667 hierarchy dominated by T.
4669 OFFSET, which is a type offset, is number of bytes.
4671 Note that we don't have to worry about having two paths to the
4672 same base type, since this type owns its association list. */
4675 propagate_binfo_offsets (binfo, offset)
4680 dfs_propagate_binfo_offsets,
4681 dfs_skip_nonprimary_vbases_unmarkedp,
4685 dfs_skip_nonprimary_vbases_markedp,
4689 /* Remove *FIELD (which corresponds to the base given by BINFO) from
4690 the field list for T. */
4693 remove_base_field (t, binfo, field)
4698 tree basetype = BINFO_TYPE (binfo);
4701 my_friendly_assert (TREE_TYPE (*field) == basetype, 23897);
4703 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4704 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4708 = size_int (CEIL (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (*field)),
4710 propagate_binfo_offsets (binfo, offset);
4712 /* Remove this field. */
4713 *field = TREE_CHAIN (*field);
4716 /* Remove the FIELD_DECLs created for T's base classes in
4717 build_base_fields. Simultaneously, update BINFO_OFFSET for all the
4718 bases, except for non-primary virtual baseclasses. */
4721 remove_base_fields (t)
4727 /* Now propagate offset information throughout the lattice.
4728 Simultaneously, remove the temporary FIELD_DECLS we created in
4729 build_base_fields to refer to base types. */
4730 field = &TYPE_FIELDS (t);
4731 if (TYPE_VFIELD (t) == *field)
4733 /* If this class did not have a primary base, we create a
4734 virtual function table pointer. It will be the first thing
4735 in the class, under the new ABI. Skip it; the base fields
4737 my_friendly_assert (flag_new_abi
4738 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t),
4740 field = &TREE_CHAIN (*field);
4743 /* Under the new ABI, the primary base is always allocated first. */
4744 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4745 remove_base_field (t, CLASSTYPE_PRIMARY_BINFO (t), field);
4747 /* Now remove the rest of the bases. */
4748 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
4752 /* Under the new ABI, we've already removed the primary base
4754 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (t))
4757 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4759 /* We treat a primary virtual base class just like an ordinary base
4760 class. But, non-primary virtual bases are laid out later. */
4761 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4764 remove_base_field (t, binfo, field);
4768 /* Called via dfs_walk from layout_virtual bases. */
4771 dfs_set_offset_for_shared_vbases (binfo, data)
4775 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
4777 /* Update the shared copy. */
4780 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4781 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
4787 /* Called via dfs_walk from layout_virtual bases. */
4790 dfs_set_offset_for_unshared_vbases (binfo, data)
4794 /* If this is a virtual base, make sure it has the same offset as
4795 the shared copy. If it's a primary base, then we know it's
4797 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4799 tree t = (tree) data;
4803 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
4804 offset = ssize_binop (MINUS_EXPR,
4805 BINFO_OFFSET (vbase),
4806 BINFO_OFFSET (binfo));
4807 propagate_binfo_offsets (binfo, offset);
4813 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4814 TYPE_ALIGN and TYPE_SIZE for T. */
4817 layout_virtual_bases (t)
4823 /* DSIZE is the size of the class without the virtual bases. */
4824 dsize = TREE_INT_CST_LOW (TYPE_SIZE (t));
4825 /* Make every class have alignment of at least one. */
4826 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4828 /* Go through the virtual bases, allocating space for each virtual
4829 base that is not already a primary base class. */
4830 for (vbase = CLASSTYPE_VBASECLASSES (t);
4832 vbase = TREE_CHAIN (vbase))
4833 if (!BINFO_VBASE_PRIMARY_P (vbase))
4835 /* This virtual base is not a primary base of any class in the
4836 hierarchy, so we have to add space for it. */
4838 unsigned int desired_align;
4840 basetype = BINFO_TYPE (vbase);
4841 desired_align = TYPE_ALIGN (basetype);
4842 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4844 /* Add padding so that we can put the virtual base class at an
4845 appropriately aligned offset. */
4846 dsize = CEIL (dsize, desired_align) * desired_align;
4847 /* And compute the offset of the virtual base. */
4848 propagate_binfo_offsets (vbase,
4849 size_int (CEIL (dsize, BITS_PER_UNIT)));
4850 /* Every virtual baseclass takes a least a UNIT, so that we can
4851 take it's address and get something different for each base. */
4852 dsize += MAX (BITS_PER_UNIT,
4853 TREE_INT_CST_LOW (CLASSTYPE_SIZE (basetype)));
4856 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4857 BINFO_OFFSET set correctly. Those we just allocated certainly
4858 will. The others are primary baseclasses; we walk the hierarchy
4859 to find the primary copies and update the shared copy. */
4860 dfs_walk (TYPE_BINFO (t),
4861 dfs_set_offset_for_shared_vbases,
4862 dfs_unmarked_real_bases_queue_p,
4865 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4866 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4867 bases and their direct and indirect bases. The ambiguity checks
4868 in get_base_distance depend on the BINFO_OFFSETs being set
4870 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4872 /* Now, make sure that the total size of the type is a multiple of
4874 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4875 TYPE_SIZE (t) = size_int (dsize);
4876 TYPE_SIZE_UNIT (t) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (t),
4877 size_int (BITS_PER_UNIT));
4880 /* Finish the work of layout_record, now taking virtual bases into account.
4881 Also compute the actual offsets that our base classes will have.
4882 This must be performed after the fields are laid out, since virtual
4883 baseclasses must lay down at the end of the record. */
4886 layout_basetypes (rec)
4891 #ifdef STRUCTURE_SIZE_BOUNDARY
4892 /* Packed structures don't need to have minimum size. */
4893 if (! TYPE_PACKED (rec))
4894 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), STRUCTURE_SIZE_BOUNDARY);
4897 /* Remove the FIELD_DECLs we created for baseclasses in
4898 build_base_fields. Simultaneously, update the BINFO_OFFSETs for
4899 everything in the hierarcy except non-primary virtual bases. */
4900 remove_base_fields (rec);
4902 /* Allocate the virtual base classes. */
4903 layout_virtual_bases (rec);
4905 /* Get all the virtual base types that this type uses. The
4906 TREE_VALUE slot holds the virtual baseclass type. Note that
4907 get_vbase_types makes copies of the virtual base BINFOs, so that
4908 the vbase_types are unshared. */
4909 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
4910 vbase_types = TREE_CHAIN (vbase_types))
4913 tree basetype = BINFO_TYPE (vbase_types);
4914 if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
4915 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4920 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4921 BINFO_OFFSETs for all of the base-classes. Position the vtable
4925 layout_class_type (t, empty_p, has_virtual_p,
4926 new_virtuals_p, overridden_virtuals_p)
4930 tree *new_virtuals_p;
4931 tree *overridden_virtuals_p;
4933 tree padding = NULL_TREE;
4935 /* If possible, we reuse the virtual function table pointer from one
4936 of our base classes. */
4937 determine_primary_base (t, has_virtual_p);
4939 /* Add pointers to all of our virtual base-classes. */
4940 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (t, empty_p),
4942 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4943 TYPE_FIELDS (t) = chainon (build_base_fields (t, empty_p),
4946 /* Create a pointer to our virtual function table. */
4947 create_vtable_ptr (t, empty_p, has_virtual_p,
4948 new_virtuals_p, overridden_virtuals_p);
4950 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4951 we have to save this before we start modifying
4952 TYPE_NONCOPIED_PARTS. */
4953 fixup_inline_methods (t);
4955 /* We make all structures have at least one element, so that they
4956 have non-zero size. The field that we add here is fake, in the
4957 sense that, for example, we don't want people to be able to
4958 initialize it later. So, we add it just long enough to let the
4959 back-end lay out the type, and then remove it. In the new ABI,
4960 the class may be empty even if it has basetypes. Therefore, we
4961 add the fake field at the end of the fields list; if there are
4962 already FIELD_DECLs on the list, their offsets will not be
4966 padding = build_lang_decl (FIELD_DECL, NULL_TREE, char_type_node);
4967 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), padding);
4968 TYPE_NONCOPIED_PARTS (t)
4969 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
4970 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4973 /* Let the back-end lay out the type. Note that at this point we
4974 have only included non-virtual base-classes; we will lay out the
4975 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4976 this call are not necessarily correct; they are just the size and
4977 alignment when no virtual base clases are used. */
4980 /* If we added an extra field to make this class non-empty, remove
4986 declp = &TYPE_FIELDS (t);
4987 while (*declp != padding)
4988 declp = &TREE_CHAIN (*declp);
4989 *declp = TREE_CHAIN (*declp);
4992 /* Delete all zero-width bit-fields from the list of fields. Now
4993 that the type is laid out they are no longer important. */
4994 remove_zero_width_bit_fields (t);
4996 /* Remember the size and alignment of the class before adding
4997 the virtual bases. */
4998 if (*empty_p && flag_new_abi)
4999 CLASSTYPE_SIZE (t) = integer_zero_node;
5000 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
5001 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
5002 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
5004 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
5005 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5007 /* Set the TYPE_DECL for this type to contain the right
5008 value for DECL_OFFSET, so that we can use it as part
5009 of a COMPONENT_REF for multiple inheritance. */
5010 layout_decl (TYPE_MAIN_DECL (t), 0);
5012 /* Now fix up any virtual base class types that we left lying
5013 around. We must get these done before we try to lay out the
5014 virtual function table. */
5015 if (CLASSTYPE_N_BASECLASSES (t))
5016 /* layout_basetypes will remove the base subobject fields. */
5017 layout_basetypes (t);
5020 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5021 (or C++ class declaration).
5023 For C++, we must handle the building of derived classes.
5024 Also, C++ allows static class members. The way that this is
5025 handled is to keep the field name where it is (as the DECL_NAME
5026 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
5027 of the field. layout_record and layout_union will know about this.
5029 More C++ hair: inline functions have text in their
5030 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5031 meaningful tree structure. After the struct has been laid out, set
5032 things up so that this can happen.
5034 And still more: virtual functions. In the case of single inheritance,
5035 when a new virtual function is seen which redefines a virtual function
5036 from the base class, the new virtual function is placed into
5037 the virtual function table at exactly the same address that
5038 it had in the base class. When this is extended to multiple
5039 inheritance, the same thing happens, except that multiple virtual
5040 function tables must be maintained. The first virtual function
5041 table is treated in exactly the same way as in the case of single
5042 inheritance. Additional virtual function tables have different
5043 DELTAs, which tell how to adjust `this' to point to the right thing.
5045 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5053 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5054 a FUNCTION_DECL. Each of these functions is a virtual function
5055 declared in T that does not override any virtual function from a
5057 tree new_virtuals = NULL_TREE;
5058 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5059 except that each declaration here overrides the declaration from
5061 tree overridden_virtuals = NULL_TREE;
5068 if (IS_AGGR_TYPE (t))
5069 cp_error ("redefinition of `%#T'", t);
5071 my_friendly_abort (172);
5076 GNU_xref_decl (current_function_decl, t);
5078 /* If this type was previously laid out as a forward reference,
5079 make sure we lay it out again. */
5080 TYPE_SIZE (t) = NULL_TREE;
5081 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5082 CLASSTYPE_VFIELD_PARENT (t) = -1;
5084 CLASSTYPE_RTTI (t) = NULL_TREE;
5086 /* Do end-of-class semantic processing: checking the validity of the
5087 bases and members and add implicitly generated methods. */
5088 check_bases_and_members (t, &empty);
5090 /* Layout the class itself. */
5091 layout_class_type (t, &empty, &has_virtual,
5092 &new_virtuals, &overridden_virtuals);
5094 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
5098 vbases = CLASSTYPE_VBASECLASSES (t);
5101 /* Now fixup overrides of all functions in vtables from all
5102 direct or indirect virtual base classes. */
5103 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
5104 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
5106 for (i = 0; i < n_baseclasses; i++)
5108 tree base_binfo = TREE_VEC_ELT (binfos, i);
5109 tree basetype = BINFO_TYPE (base_binfo);
5112 vbases = CLASSTYPE_VBASECLASSES (basetype);
5116 tree basetype_vbase;
5119 = find_vbase_instance (BINFO_TYPE (vbases), t);
5121 = find_vbase_instance (BINFO_TYPE (vbases), basetype);
5123 merge_overrides (vbase, basetype_vbase, 1, t);
5124 vbases = TREE_CHAIN (vbases);
5130 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
5131 might need to know it for setting up the offsets in the vtable
5132 (or in thunks) below. */
5133 vfield = TYPE_VFIELD (t);
5134 if (vfield != NULL_TREE
5135 && DECL_FIELD_CONTEXT (vfield) != t)
5137 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
5138 tree offset = BINFO_OFFSET (binfo);
5140 vfield = copy_node (vfield);
5141 copy_lang_decl (vfield);
5143 if (! integer_zerop (offset))
5144 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
5145 DECL_FIELD_CONTEXT (vfield) = t;
5146 DECL_CLASS_CONTEXT (vfield) = t;
5147 DECL_FIELD_BITPOS (vfield)
5148 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
5149 TYPE_VFIELD (t) = vfield;
5153 = modify_all_vtables (t, &has_virtual, nreverse (overridden_virtuals));
5155 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
5158 /* Now fixup any virtual function entries from virtual bases
5159 that have different deltas. This has to come after we do the
5160 overridden virtuals. */
5161 vbases = CLASSTYPE_VBASECLASSES (t);
5166 /* We might be able to shorten the amount of work we do by
5167 only doing this for vtables that come from virtual bases
5168 that have differing offsets, but don't want to miss any
5170 vbase = find_vbase_instance (BINFO_TYPE (vbases), t);
5171 dfs_walk (vbase, dfs_fixup_vtable_deltas, dfs_skip_vbases, t);
5172 vbases = TREE_CHAIN (vbases);
5176 /* If necessary, create the vtable for this class. */
5178 || overridden_virtuals
5179 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
5181 new_virtuals = nreverse (new_virtuals);
5182 /* We must enter these virtuals into the table. */
5183 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5185 if (! CLASSTYPE_COM_INTERFACE (t))
5187 /* The second slot is for the tdesc pointer when thunks
5189 if (flag_vtable_thunks)
5190 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
5192 /* The first slot is for the rtti offset. */
5193 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
5195 set_rtti_entry (new_virtuals,
5196 convert (ssizetype, integer_zero_node), t);
5198 build_vtable (NULL_TREE, t);
5200 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
5201 /* Here we know enough to change the type of our virtual
5202 function table, but we will wait until later this function. */
5203 build_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5205 /* If this type has basetypes with constructors, then those
5206 constructors might clobber the virtual function table. But
5207 they don't if the derived class shares the exact vtable of the base
5210 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5212 /* If we didn't need a new vtable, see if we should copy one from
5214 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5216 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5218 /* This class contributes nothing new to the virtual function
5219 table. However, it may have declared functions which
5220 went into the virtual function table "inherited" from the
5221 base class. If so, we grab a copy of those updated functions,
5222 and pretend they are ours. */
5224 /* See if we should steal the virtual info from base class. */
5225 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
5226 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
5227 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
5228 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
5229 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5230 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5233 if (TYPE_CONTAINS_VPTR_P (t))
5235 if (TYPE_BINFO_VTABLE (t))
5236 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5238 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5239 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5242 CLASSTYPE_VSIZE (t) = has_virtual;
5243 /* Entries for virtual functions defined in the primary base are
5244 followed by entries for new functions unique to this class. */
5245 TYPE_BINFO_VIRTUALS (t)
5246 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5247 /* Finally, add entries for functions that override virtuals
5248 from non-primary bases. */
5249 TYPE_BINFO_VIRTUALS (t)
5250 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5253 /* Now lay out the virtual function table. */
5255 layout_vtable_decl (TYPE_BINFO (t), has_virtual);
5257 /* If we created a new vtbl pointer for this class, add it to the
5259 if (TYPE_VFIELD (t) && CLASSTYPE_VFIELD_PARENT (t) == -1)
5260 CLASSTYPE_VFIELDS (t)
5261 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5263 finish_struct_bits (t);
5265 /* Complete the rtl for any static member objects of the type we're
5267 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5269 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5270 && TREE_TYPE (x) == t)
5272 DECL_MODE (x) = TYPE_MODE (t);
5273 make_decl_rtl (x, NULL, 0);
5277 /* Done with FIELDS...now decide whether to sort these for
5278 faster lookups later.
5280 The C front-end only does this when n_fields > 15. We use
5281 a smaller number because most searches fail (succeeding
5282 ultimately as the search bores through the inheritance
5283 hierarchy), and we want this failure to occur quickly. */
5285 n_fields = count_fields (TYPE_FIELDS (t));
5288 tree field_vec = make_tree_vec (n_fields);
5289 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5290 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5291 (int (*)(const void *, const void *))field_decl_cmp);
5292 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5293 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5294 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5297 if (TYPE_HAS_CONSTRUCTOR (t))
5299 tree vfields = CLASSTYPE_VFIELDS (t);
5303 /* Mark the fact that constructor for T
5304 could affect anybody inheriting from T
5305 who wants to initialize vtables for VFIELDS's type. */
5306 if (VF_DERIVED_VALUE (vfields))
5307 TREE_ADDRESSABLE (vfields) = 1;
5308 vfields = TREE_CHAIN (vfields);
5312 if (CLASSTYPE_VSIZE (t) != 0)
5314 /* In addition to this one, all the other vfields should be listed. */
5315 /* Before that can be done, we have to have FIELD_DECLs for them, and
5316 a place to find them. */
5317 TYPE_NONCOPIED_PARTS (t)
5318 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5319 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5321 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5322 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5323 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5327 /* Make the rtl for any new vtables we have created, and unmark
5328 the base types we marked. */
5330 hack_incomplete_structures (t);
5332 if (warn_overloaded_virtual)
5335 maybe_suppress_debug_info (t);
5337 /* Finish debugging output for this type. */
5338 rest_of_type_compilation (t, toplevel_bindings_p ());
5341 /* When T was built up, the member declarations were added in reverse
5342 order. Rearrange them to declaration order. */
5345 unreverse_member_declarations (t)
5352 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5353 reverse order. Put them in declaration order now. */
5354 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5355 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5357 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5358 reverse order, so we can't just use nreverse. */
5360 for (x = TYPE_FIELDS (t);
5361 x && TREE_CODE (x) != TYPE_DECL;
5364 next = TREE_CHAIN (x);
5365 TREE_CHAIN (x) = prev;
5370 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5372 TYPE_FIELDS (t) = prev;
5377 finish_struct (t, attributes)
5380 /* Now that we've got all the field declarations, reverse everything
5382 unreverse_member_declarations (t);
5384 cplus_decl_attributes (t, attributes, NULL_TREE);
5386 if (processing_template_decl)
5388 finish_struct_methods (t);
5389 TYPE_SIZE (t) = integer_zero_node;
5392 finish_struct_1 (t);
5394 TYPE_BEING_DEFINED (t) = 0;
5396 if (current_class_type)
5399 error ("trying to finish struct, but kicked out due to previous parse errors.");
5401 if (processing_template_decl)
5403 tree scope = current_scope ();
5404 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5405 add_tree (build_min (TAG_DEFN, t));
5411 /* Return the dynamic type of INSTANCE, if known.
5412 Used to determine whether the virtual function table is needed
5415 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5416 of our knowledge of its type. *NONNULL should be initialized
5417 before this function is called. */
5420 fixed_type_or_null (instance, nonnull)
5424 switch (TREE_CODE (instance))
5427 /* Check that we are not going through a cast of some sort. */
5428 if (TREE_TYPE (instance)
5429 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5430 instance = TREE_OPERAND (instance, 0);
5431 /* fall through... */
5433 /* This is a call to a constructor, hence it's never zero. */
5434 if (TREE_HAS_CONSTRUCTOR (instance))
5438 return TREE_TYPE (instance);
5443 /* This is a call to a constructor, hence it's never zero. */
5444 if (TREE_HAS_CONSTRUCTOR (instance))
5448 return TREE_TYPE (instance);
5450 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5457 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5458 /* Propagate nonnull. */
5459 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5460 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5461 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5466 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5471 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5474 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5478 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5479 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5483 return TREE_TYPE (TREE_TYPE (instance));
5485 /* fall through... */
5488 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5492 return TREE_TYPE (instance);
5496 if (instance == current_class_ptr
5497 && flag_this_is_variable <= 0)
5499 /* Normally, 'this' must be non-null. */
5500 if (flag_this_is_variable == 0)
5503 /* <0 means we're in a constructor and we know our type. */
5504 if (flag_this_is_variable < 0)
5505 return TREE_TYPE (TREE_TYPE (instance));
5507 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5508 /* Reference variables should be references to objects. */
5518 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5519 to the static type. We also handle the case where INSTANCE is really
5522 Used to determine whether the virtual function table is needed
5525 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5526 of our knowledge of its type. *NONNULL should be initialized
5527 before this function is called. */
5530 resolves_to_fixed_type_p (instance, nonnull)
5534 tree t = TREE_TYPE (instance);
5535 tree fixed = fixed_type_or_null (instance, nonnull);
5536 if (fixed == NULL_TREE)
5538 if (POINTER_TYPE_P (t))
5540 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
5545 init_class_processing ()
5547 current_class_depth = 0;
5548 current_class_stack_size = 10;
5550 = (class_stack_node_t) xmalloc (current_class_stack_size
5551 * sizeof (struct class_stack_node));
5553 access_default_node = build_int_2 (0, 0);
5554 access_public_node = build_int_2 (1, 0);
5555 access_protected_node = build_int_2 (2, 0);
5556 access_private_node = build_int_2 (3, 0);
5557 access_default_virtual_node = build_int_2 (4, 0);
5558 access_public_virtual_node = build_int_2 (5, 0);
5559 access_protected_virtual_node = build_int_2 (6, 0);
5560 access_private_virtual_node = build_int_2 (7, 0);
5563 /* Set current scope to NAME. CODE tells us if this is a
5564 STRUCT, UNION, or ENUM environment.
5566 NAME may end up being NULL_TREE if this is an anonymous or
5567 late-bound struct (as in "struct { ... } foo;") */
5569 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5570 appropriate values, found by looking up the type definition of
5573 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5574 which can be seen locally to the class. They are shadowed by
5575 any subsequent local declaration (including parameter names).
5577 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5578 which have static meaning (i.e., static members, static
5579 member functions, enum declarations, etc).
5581 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5582 which can be seen locally to the class (as in 1), but
5583 know that we are doing this for declaration purposes
5584 (i.e. friend foo::bar (int)).
5586 So that we may avoid calls to lookup_name, we cache the _TYPE
5587 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5589 For multiple inheritance, we perform a two-pass depth-first search
5590 of the type lattice. The first pass performs a pre-order search,
5591 marking types after the type has had its fields installed in
5592 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5593 unmarks the marked types. If a field or member function name
5594 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5595 that name becomes `error_mark_node'. */
5598 pushclass (type, modify)
5602 type = TYPE_MAIN_VARIANT (type);
5604 /* Make sure there is enough room for the new entry on the stack. */
5605 if (current_class_depth + 1 >= current_class_stack_size)
5607 current_class_stack_size *= 2;
5609 = (class_stack_node_t) xrealloc (current_class_stack,
5610 current_class_stack_size
5611 * sizeof (struct class_stack_node));
5614 /* Insert a new entry on the class stack. */
5615 current_class_stack[current_class_depth].name = current_class_name;
5616 current_class_stack[current_class_depth].type = current_class_type;
5617 current_class_stack[current_class_depth].access = current_access_specifier;
5618 current_class_stack[current_class_depth].names_used = 0;
5619 current_class_depth++;
5621 /* Now set up the new type. */
5622 current_class_name = TYPE_NAME (type);
5623 if (TREE_CODE (current_class_name) == TYPE_DECL)
5624 current_class_name = DECL_NAME (current_class_name);
5625 current_class_type = type;
5627 /* By default, things in classes are private, while things in
5628 structures or unions are public. */
5629 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5630 ? access_private_node
5631 : access_public_node);
5633 if (previous_class_type != NULL_TREE
5634 && (type != previous_class_type
5635 || TYPE_SIZE (previous_class_type) == NULL_TREE)
5636 && current_class_depth == 1)
5638 /* Forcibly remove any old class remnants. */
5639 invalidate_class_lookup_cache ();
5642 /* If we're about to enter a nested class, clear
5643 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5644 if (modify && current_class_depth > 1)
5645 clear_identifier_class_values ();
5650 if (CLASSTYPE_TEMPLATE_INFO (type))
5651 overload_template_name (type);
5656 if (type != previous_class_type || current_class_depth > 1)
5657 push_class_decls (type);
5662 /* We are re-entering the same class we just left, so we
5663 don't have to search the whole inheritance matrix to find
5664 all the decls to bind again. Instead, we install the
5665 cached class_shadowed list, and walk through it binding
5666 names and setting up IDENTIFIER_TYPE_VALUEs. */
5667 set_class_shadows (previous_class_values);
5668 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5670 tree id = TREE_PURPOSE (item);
5671 tree decl = TREE_TYPE (item);
5673 push_class_binding (id, decl);
5674 if (TREE_CODE (decl) == TYPE_DECL)
5675 set_identifier_type_value (id, TREE_TYPE (decl));
5677 unuse_fields (type);
5680 storetags (CLASSTYPE_TAGS (type));
5684 /* When we exit a toplevel class scope, we save the
5685 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5686 reenter the class. Here, we've entered some other class, so we
5687 must invalidate our cache. */
5690 invalidate_class_lookup_cache ()
5694 /* This code can be seen as a cache miss. When we've cached a
5695 class' scope's bindings and we can't use them, we need to reset
5696 them. This is it! */
5697 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5698 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5700 previous_class_type = NULL_TREE;
5703 /* Get out of the current class scope. If we were in a class scope
5704 previously, that is the one popped to. */
5710 /* Since poplevel_class does the popping of class decls nowadays,
5711 this really only frees the obstack used for these decls. */
5714 current_class_depth--;
5715 current_class_name = current_class_stack[current_class_depth].name;
5716 current_class_type = current_class_stack[current_class_depth].type;
5717 current_access_specifier = current_class_stack[current_class_depth].access;
5718 if (current_class_stack[current_class_depth].names_used)
5719 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5722 /* Returns 1 if current_class_type is either T or a nested type of T. */
5725 currently_open_class (t)
5729 if (t == current_class_type)
5731 for (i = 0; i < current_class_depth; ++i)
5732 if (current_class_stack [i].type == t)
5737 /* When entering a class scope, all enclosing class scopes' names with
5738 static meaning (static variables, static functions, types and enumerators)
5739 have to be visible. This recursive function calls pushclass for all
5740 enclosing class contexts until global or a local scope is reached.
5741 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5742 formal of the same name. */
5745 push_nested_class (type, modify)
5751 /* A namespace might be passed in error cases, like A::B:C. */
5752 if (type == NULL_TREE
5753 || type == error_mark_node
5754 || TREE_CODE (type) == NAMESPACE_DECL
5755 || ! IS_AGGR_TYPE (type)
5756 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5757 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
5760 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5762 if (context && CLASS_TYPE_P (context))
5763 push_nested_class (context, 2);
5764 pushclass (type, modify);
5767 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5772 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5775 if (context && CLASS_TYPE_P (context))
5776 pop_nested_class ();
5779 /* Set global variables CURRENT_LANG_NAME to appropriate value
5780 so that behavior of name-mangling machinery is correct. */
5783 push_lang_context (name)
5786 *current_lang_stack++ = current_lang_name;
5787 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5788 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5790 size_t old_size = VARRAY_SIZE (current_lang_base);
5792 VARRAY_GROW (current_lang_base, old_size + 10);
5793 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5796 if (name == lang_name_cplusplus)
5798 strict_prototype = strict_prototypes_lang_cplusplus;
5799 current_lang_name = name;
5801 else if (name == lang_name_java)
5803 strict_prototype = strict_prototypes_lang_cplusplus;
5804 current_lang_name = name;
5805 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5806 (See record_builtin_java_type in decl.c.) However, that causes
5807 incorrect debug entries if these types are actually used.
5808 So we re-enable debug output after extern "Java". */
5809 DECL_IGNORED_P (java_byte_type_node) = 0;
5810 DECL_IGNORED_P (java_short_type_node) = 0;
5811 DECL_IGNORED_P (java_int_type_node) = 0;
5812 DECL_IGNORED_P (java_long_type_node) = 0;
5813 DECL_IGNORED_P (java_float_type_node) = 0;
5814 DECL_IGNORED_P (java_double_type_node) = 0;
5815 DECL_IGNORED_P (java_char_type_node) = 0;
5816 DECL_IGNORED_P (java_boolean_type_node) = 0;
5818 else if (name == lang_name_c)
5820 strict_prototype = strict_prototypes_lang_c;
5821 current_lang_name = name;
5824 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5827 /* Get out of the current language scope. */
5832 /* Clear the current entry so that garbage collector won't hold on
5834 *current_lang_stack = NULL_TREE;
5835 current_lang_name = *--current_lang_stack;
5836 if (current_lang_name == lang_name_cplusplus
5837 || current_lang_name == lang_name_java)
5838 strict_prototype = strict_prototypes_lang_cplusplus;
5839 else if (current_lang_name == lang_name_c)
5840 strict_prototype = strict_prototypes_lang_c;
5843 /* Type instantiation routines. */
5845 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5846 matches the TARGET_TYPE. If there is no satisfactory match, return
5847 error_mark_node, and issue an error message if COMPLAIN is
5848 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5849 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5850 template arguments. */
5853 resolve_address_of_overloaded_function (target_type,
5862 tree explicit_targs;
5864 /* Here's what the standard says:
5868 If the name is a function template, template argument deduction
5869 is done, and if the argument deduction succeeds, the deduced
5870 arguments are used to generate a single template function, which
5871 is added to the set of overloaded functions considered.
5873 Non-member functions and static member functions match targets of
5874 type "pointer-to-function" or "reference-to-function." Nonstatic
5875 member functions match targets of type "pointer-to-member
5876 function;" the function type of the pointer to member is used to
5877 select the member function from the set of overloaded member
5878 functions. If a nonstatic member function is selected, the
5879 reference to the overloaded function name is required to have the
5880 form of a pointer to member as described in 5.3.1.
5882 If more than one function is selected, any template functions in
5883 the set are eliminated if the set also contains a non-template
5884 function, and any given template function is eliminated if the
5885 set contains a second template function that is more specialized
5886 than the first according to the partial ordering rules 14.5.5.2.
5887 After such eliminations, if any, there shall remain exactly one
5888 selected function. */
5891 int is_reference = 0;
5892 /* We store the matches in a TREE_LIST rooted here. The functions
5893 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5894 interoperability with most_specialized_instantiation. */
5895 tree matches = NULL_TREE;
5898 /* By the time we get here, we should be seeing only real
5899 pointer-to-member types, not the internal POINTER_TYPE to
5900 METHOD_TYPE representation. */
5901 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5902 && (TREE_CODE (TREE_TYPE (target_type))
5903 == METHOD_TYPE)), 0);
5905 /* Check that the TARGET_TYPE is reasonable. */
5906 if (TYPE_PTRFN_P (target_type))
5909 else if (TYPE_PTRMEMFUNC_P (target_type))
5910 /* This is OK, too. */
5912 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5914 /* This is OK, too. This comes from a conversion to reference
5916 target_type = build_reference_type (target_type);
5922 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5923 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5924 return error_mark_node;
5927 /* If we can find a non-template function that matches, we can just
5928 use it. There's no point in generating template instantiations
5929 if we're just going to throw them out anyhow. But, of course, we
5930 can only do this when we don't *need* a template function. */
5935 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5937 tree fn = OVL_FUNCTION (fns);
5940 if (TREE_CODE (fn) == TEMPLATE_DECL)
5941 /* We're not looking for templates just yet. */
5944 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5946 /* We're looking for a non-static member, and this isn't
5947 one, or vice versa. */
5950 /* See if there's a match. */
5951 fntype = TREE_TYPE (fn);
5953 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5954 else if (!is_reference)
5955 fntype = build_pointer_type (fntype);
5957 if (can_convert_arg (target_type, fntype, fn))
5958 matches = tree_cons (fn, NULL_TREE, matches);
5962 /* Now, if we've already got a match (or matches), there's no need
5963 to proceed to the template functions. But, if we don't have a
5964 match we need to look at them, too. */
5967 tree target_fn_type;
5968 tree target_arg_types;
5973 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5975 target_fn_type = TREE_TYPE (target_type);
5976 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5978 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5980 tree fn = OVL_FUNCTION (fns);
5982 tree instantiation_type;
5985 if (TREE_CODE (fn) != TEMPLATE_DECL)
5986 /* We're only looking for templates. */
5989 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5991 /* We're not looking for a non-static member, and this is
5992 one, or vice versa. */
5995 /* Try to do argument deduction. */
5996 targs = make_tree_vec (DECL_NTPARMS (fn));
5997 if (fn_type_unification (fn, explicit_targs, targs,
5998 target_arg_types, NULL_TREE,
6000 /* Argument deduction failed. */
6003 /* Instantiate the template. */
6004 instantiation = instantiate_template (fn, targs);
6005 if (instantiation == error_mark_node)
6006 /* Instantiation failed. */
6009 /* See if there's a match. */
6010 instantiation_type = TREE_TYPE (instantiation);
6012 instantiation_type =
6013 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
6014 else if (!is_reference)
6015 instantiation_type = build_pointer_type (instantiation_type);
6016 if (can_convert_arg (target_type, instantiation_type, instantiation))
6017 matches = tree_cons (instantiation, fn, matches);
6020 /* Now, remove all but the most specialized of the matches. */
6023 tree match = most_specialized_instantiation (matches,
6026 if (match != error_mark_node)
6027 matches = tree_cons (match, NULL_TREE, NULL_TREE);
6031 /* Now we should have exactly one function in MATCHES. */
6032 if (matches == NULL_TREE)
6034 /* There were *no* matches. */
6037 cp_error ("no matches converting function `%D' to type `%#T'",
6038 DECL_NAME (OVL_FUNCTION (overload)),
6041 /* print_candidates expects a chain with the functions in
6042 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6043 so why be clever?). */
6044 for (; overload; overload = OVL_NEXT (overload))
6045 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6048 print_candidates (matches);
6050 return error_mark_node;
6052 else if (TREE_CHAIN (matches))
6054 /* There were too many matches. */
6060 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6061 DECL_NAME (OVL_FUNCTION (overload)),
6064 /* Since print_candidates expects the functions in the
6065 TREE_VALUE slot, we flip them here. */
6066 for (match = matches; match; match = TREE_CHAIN (match))
6067 TREE_VALUE (match) = TREE_PURPOSE (match);
6069 print_candidates (matches);
6072 return error_mark_node;
6075 /* Good, exactly one match. Now, convert it to the correct type. */
6076 fn = TREE_PURPOSE (matches);
6080 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6081 return build_unary_op (ADDR_EXPR, fn, 0);
6084 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6085 will mark the function as addressed, but here we must do it
6087 mark_addressable (fn);
6093 /* This function will instantiate the type of the expression given in
6094 RHS to match the type of LHSTYPE. If errors exist, then return
6095 error_mark_node. We only complain is COMPLAIN is set. If we are
6096 not complaining, never modify rhs, as overload resolution wants to
6097 try many possible instantiations, in hopes that at least one will
6100 FLAGS is a bitmask, as we see at the top of the function.
6102 For non-recursive calls, LHSTYPE should be a function, pointer to
6103 function, or a pointer to member function. */
6106 instantiate_type (lhstype, rhs, flags)
6110 int complain = (flags & 1);
6111 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6113 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6116 error ("not enough type information");
6117 return error_mark_node;
6120 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6122 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6125 cp_error ("argument of type `%T' does not match `%T'",
6126 TREE_TYPE (rhs), lhstype);
6127 return error_mark_node;
6130 /* We don't overwrite rhs if it is an overloaded function.
6131 Copying it would destroy the tree link. */
6132 if (TREE_CODE (rhs) != OVERLOAD)
6133 rhs = copy_node (rhs);
6135 /* This should really only be used when attempting to distinguish
6136 what sort of a pointer to function we have. For now, any
6137 arithmetic operation which is not supported on pointers
6138 is rejected as an error. */
6140 switch (TREE_CODE (rhs))
6147 my_friendly_abort (177);
6148 return error_mark_node;
6155 new_rhs = instantiate_type (build_pointer_type (lhstype),
6156 TREE_OPERAND (rhs, 0), flags);
6157 if (new_rhs == error_mark_node)
6158 return error_mark_node;
6160 TREE_TYPE (rhs) = lhstype;
6161 TREE_OPERAND (rhs, 0) = new_rhs;
6166 rhs = copy_node (TREE_OPERAND (rhs, 0));
6167 TREE_TYPE (rhs) = unknown_type_node;
6168 return instantiate_type (lhstype, rhs, flags);
6172 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6174 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
6175 && complain && !flag_ms_extensions)
6177 /* Note: we check this after the recursive call to avoid
6178 complaining about cases where overload resolution fails. */
6180 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
6181 tree fn = PTRMEM_CST_MEMBER (r);
6183 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
6186 ("object-dependent reference to `%E' can only be used in a call",
6189 (" to form a pointer to member function, say `&%T::%E'",
6197 rhs = TREE_OPERAND (rhs, 1);
6198 if (BASELINK_P (rhs))
6199 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6201 /* This can happen if we are forming a pointer-to-member for a
6203 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6207 case TEMPLATE_ID_EXPR:
6209 resolve_address_of_overloaded_function (lhstype,
6210 TREE_OPERAND (rhs, 0),
6212 /*template_only=*/1,
6213 TREE_OPERAND (rhs, 1));
6217 resolve_address_of_overloaded_function (lhstype,
6220 /*template_only=*/0,
6221 /*explicit_targs=*/NULL_TREE);
6224 /* Now we should have a baselink. */
6225 my_friendly_assert (BASELINK_P (rhs), 990412);
6227 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6230 /* This is too hard for now. */
6231 my_friendly_abort (183);
6232 return error_mark_node;
6237 TREE_OPERAND (rhs, 0)
6238 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6239 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6240 return error_mark_node;
6241 TREE_OPERAND (rhs, 1)
6242 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6243 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6244 return error_mark_node;
6246 TREE_TYPE (rhs) = lhstype;
6250 case TRUNC_DIV_EXPR:
6251 case FLOOR_DIV_EXPR:
6253 case ROUND_DIV_EXPR:
6255 case TRUNC_MOD_EXPR:
6256 case FLOOR_MOD_EXPR:
6258 case ROUND_MOD_EXPR:
6259 case FIX_ROUND_EXPR:
6260 case FIX_FLOOR_EXPR:
6262 case FIX_TRUNC_EXPR:
6278 case PREINCREMENT_EXPR:
6279 case PREDECREMENT_EXPR:
6280 case POSTINCREMENT_EXPR:
6281 case POSTDECREMENT_EXPR:
6283 error ("invalid operation on uninstantiated type");
6284 return error_mark_node;
6286 case TRUTH_AND_EXPR:
6288 case TRUTH_XOR_EXPR:
6295 case TRUTH_ANDIF_EXPR:
6296 case TRUTH_ORIF_EXPR:
6297 case TRUTH_NOT_EXPR:
6299 error ("not enough type information");
6300 return error_mark_node;
6303 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6306 error ("not enough type information");
6307 return error_mark_node;
6309 TREE_OPERAND (rhs, 1)
6310 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6311 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6312 return error_mark_node;
6313 TREE_OPERAND (rhs, 2)
6314 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6315 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6316 return error_mark_node;
6318 TREE_TYPE (rhs) = lhstype;
6322 TREE_OPERAND (rhs, 1)
6323 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6324 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6325 return error_mark_node;
6327 TREE_TYPE (rhs) = lhstype;
6331 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6333 case ENTRY_VALUE_EXPR:
6334 my_friendly_abort (184);
6335 return error_mark_node;
6338 return error_mark_node;
6341 my_friendly_abort (185);
6342 return error_mark_node;
6346 /* Return the name of the virtual function pointer field
6347 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6348 this may have to look back through base types to find the
6349 ultimate field name. (For single inheritance, these could
6350 all be the same name. Who knows for multiple inheritance). */
6353 get_vfield_name (type)
6356 tree binfo = TYPE_BINFO (type);
6359 while (BINFO_BASETYPES (binfo)
6360 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6361 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6362 binfo = BINFO_BASETYPE (binfo, 0);
6364 type = BINFO_TYPE (binfo);
6365 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6366 + TYPE_NAME_LENGTH (type) + 2);
6367 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6368 return get_identifier (buf);
6372 print_class_statistics ()
6374 #ifdef GATHER_STATISTICS
6375 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6376 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6377 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6378 n_build_method_call, n_inner_fields_searched);
6381 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6382 n_vtables, n_vtable_searches);
6383 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6384 n_vtable_entries, n_vtable_elems);
6389 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6390 according to [class]:
6391 The class-name is also inserted
6392 into the scope of the class itself. For purposes of access checking,
6393 the inserted class name is treated as if it were a public member name. */
6396 build_self_reference ()
6398 tree name = constructor_name (current_class_type);
6399 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6402 DECL_NONLOCAL (value) = 1;
6403 DECL_CONTEXT (value) = current_class_type;
6404 DECL_CLASS_CONTEXT (value) = current_class_type;
6405 DECL_ARTIFICIAL (value) = 1;
6407 if (processing_template_decl)
6408 value = push_template_decl (value);
6410 saved_cas = current_access_specifier;
6411 current_access_specifier = access_public_node;
6412 finish_member_declaration (value);
6413 current_access_specifier = saved_cas;
6416 /* Returns 1 if TYPE contains only padding bytes. */
6419 is_empty_class (type)
6424 if (type == error_mark_node)
6427 if (! IS_AGGR_TYPE (type))
6431 return CLASSTYPE_SIZE (type) == integer_zero_node;
6433 if (TYPE_BINFO_BASETYPES (type))
6435 t = TYPE_FIELDS (type);
6436 while (t && TREE_CODE (t) != FIELD_DECL)
6438 return (t == NULL_TREE);
6441 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6442 a *_TYPE node. NODE can also be a local class. */
6445 get_enclosing_class (type)
6450 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6452 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6455 node = DECL_CONTEXT (node);
6461 node = TYPE_CONTEXT (node);
6465 my_friendly_abort (0);
6471 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6474 is_base_of_enclosing_class (base, type)
6479 if (get_binfo (base, type, 0))
6482 type = get_enclosing_class (type);
6487 /* Note that NAME was looked up while the current class was being
6488 defined and that the result of that lookup was DECL. */
6491 maybe_note_name_used_in_class (name, decl)
6495 splay_tree names_used;
6497 /* If we're not defining a class, there's nothing to do. */
6498 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6501 /* If there's already a binding for this NAME, then we don't have
6502 anything to worry about. */
6503 if (IDENTIFIER_CLASS_VALUE (name))
6506 if (!current_class_stack[current_class_depth - 1].names_used)
6507 current_class_stack[current_class_depth - 1].names_used
6508 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6509 names_used = current_class_stack[current_class_depth - 1].names_used;
6511 splay_tree_insert (names_used,
6512 (splay_tree_key) name,
6513 (splay_tree_value) decl);
6516 /* Note that NAME was declared (as DECL) in the current class. Check
6517 to see that the declaration is legal. */
6520 note_name_declared_in_class (name, decl)
6524 splay_tree names_used;
6527 /* Look to see if we ever used this name. */
6529 = current_class_stack[current_class_depth - 1].names_used;
6533 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6536 /* [basic.scope.class]
6538 A name N used in a class S shall refer to the same declaration
6539 in its context and when re-evaluated in the completed scope of
6541 cp_error ("declaration of `%#D'", decl);
6542 cp_error_at ("changes meaning of `%s' from `%+#D'",
6543 IDENTIFIER_POINTER (DECL_NAME (decl)),
6548 /* Dump the offsets of all the bases rooted at BINFO to stderr.
6549 INDENT should be zero when called from the top level; it is
6550 incremented recursively. */
6553 dump_class_hierarchy (binfo, indent)
6559 fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
6560 (unsigned long) binfo,
6561 type_as_string (binfo, TS_PLAIN));
6562 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
6563 TREE_INT_CST_LOW (BINFO_OFFSET (binfo)));
6564 fprintf (stderr, " %s\n",
6565 BINFO_PRIMARY_MARKED_P (binfo) ? "primary" : "");
6567 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6568 dump_class_hierarchy (BINFO_BASETYPE (binfo, i), indent + 2);