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 PROTO ((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 PROTO((tree));
76 static void finish_struct_anon PROTO((tree));
77 static tree build_vbase_pointer PROTO((tree, tree));
78 static tree build_vtable_entry PROTO((tree, tree));
79 static tree get_vtable_name PROTO((tree));
80 static tree get_derived_offset PROTO((tree, tree));
81 static tree get_basefndecls PROTO((tree, tree));
82 static void set_rtti_entry PROTO((tree, tree, tree));
83 static void build_vtable PROTO((tree, tree));
84 static void prepare_fresh_vtable PROTO((tree, tree));
85 static tree dfs_fixup_vtable_deltas PROTO((tree, void *));
86 static tree dfs_finish_vtbls PROTO((tree, void *));
87 static void finish_vtbls PROTO((tree));
88 static void modify_vtable_entry PROTO((tree, tree, tree));
89 static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
90 static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
91 static tree delete_duplicate_fields_1 PROTO((tree, tree));
92 static void delete_duplicate_fields PROTO((tree));
93 static void finish_struct_bits PROTO((tree));
94 static int alter_access PROTO((tree, tree, tree, tree));
95 static void handle_using_decl PROTO((tree, tree));
96 static int overrides PROTO((tree, tree));
97 static int strictly_overrides PROTO((tree, tree));
98 static void merge_overrides PROTO((tree, tree, int, tree));
99 static void override_one_vtable PROTO((tree, tree, tree));
100 static void mark_overriders PROTO((tree, tree));
101 static void check_for_override PROTO((tree, tree));
102 static tree dfs_get_class_offset PROTO((tree, void *));
103 static tree get_class_offset PROTO((tree, tree, tree, tree));
104 static void modify_one_vtable PROTO((tree, tree, tree));
105 static tree dfs_modify_vtables PROTO((tree, void *));
106 static void modify_all_vtables PROTO((tree, tree));
107 static void determine_primary_base PROTO((tree, int *));
108 static void finish_struct_methods PROTO((tree));
109 static void maybe_warn_about_overly_private_class PROTO ((tree));
110 static int field_decl_cmp PROTO ((const tree *, const tree *));
111 static int method_name_cmp PROTO ((const tree *, const tree *));
112 static tree add_implicitly_declared_members PROTO((tree, int, int, int));
113 static tree fixed_type_or_null PROTO((tree, int *));
114 static tree resolve_address_of_overloaded_function PROTO((tree, tree, int,
116 static void build_vtable_entry_ref PROTO((tree, tree, tree));
117 static tree build_vtable_entry_for_fn PROTO((tree, tree));
118 static tree build_vtbl_initializer PROTO((tree, tree));
119 static int count_fields PROTO((tree));
120 static int add_fields_to_vec PROTO((tree, tree, int));
121 static void check_bitfield_decl PROTO((tree));
122 static void check_field_decl PROTO((tree, tree, int *, int *, int *, int *));
123 static void check_field_decls PROTO((tree, tree *, int *, int *, int *,
125 static int avoid_overlap PROTO((tree, tree, int *));
126 static tree build_base_field PROTO((tree, tree, int *, int *, unsigned int *));
127 static tree build_base_fields PROTO((tree, int *));
128 static tree build_vbase_pointer_fields PROTO((tree, int *));
129 static tree build_vtbl_or_vbase_field PROTO((tree, tree, tree, tree, int *));
130 static void check_methods PROTO((tree));
131 static void remove_zero_width_bit_fields PROTO((tree));
132 static void check_bases PROTO((tree, int *, int *, int *));
133 static void check_bases_and_members PROTO((tree, int *));
134 static void create_vtable_ptr PROTO((tree, int *, int *, tree *, tree *));
135 static void layout_class_type PROTO((tree, int *, int *, tree *, tree *));
136 static void fixup_pending_inline PROTO((struct pending_inline *));
137 static void fixup_inline_methods PROTO((tree));
138 static void set_primary_base PROTO((tree, int, int *));
139 static tree dfs_propagate_binfo_offsets PROTO((tree, void *));
140 static void propagate_binfo_offsets PROTO((tree, tree));
141 static void layout_basetypes PROTO((tree));
142 static void layout_virtual_bases PROTO((tree));
143 static void remove_base_field PROTO((tree, tree, tree *));
144 static void remove_base_fields PROTO((tree));
145 static tree dfs_set_offset_for_shared_vbases PROTO((tree, void *));
146 static tree dfs_set_offset_for_unshared_vbases PROTO((tree, void *));
147 static tree dfs_build_vbase_offset_vtbl_entries PROTO((tree, void *));
148 static tree build_vbase_offset_vtbl_entries PROTO((tree, tree));
149 static void start_vtable PROTO((tree, int *));
151 /* Variables shared between class.c and call.c. */
153 #ifdef GATHER_STATISTICS
155 int n_vtable_entries = 0;
156 int n_vtable_searches = 0;
157 int n_vtable_elems = 0;
158 int n_convert_harshness = 0;
159 int n_compute_conversion_costs = 0;
160 int n_build_method_call = 0;
161 int n_inner_fields_searched = 0;
164 /* Virtual base class layout. */
166 /* Returns a list of virtual base class pointers as a chain of
170 build_vbase_pointer_fields (rec, empty_p)
174 /* Chain to hold all the new FIELD_DECLs which point at virtual
176 tree vbase_decls = NULL_TREE;
177 tree binfos = TYPE_BINFO_BASETYPES (rec);
178 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
182 /* Under the new ABI, there are no vbase pointers in the object.
183 Instead, the offsets are stored in the vtable. */
184 if (vbase_offsets_in_vtable_p ())
187 /* Loop over the baseclasses, adding vbase pointers as needed. */
188 for (i = 0; i < n_baseclasses; i++)
190 register tree base_binfo = TREE_VEC_ELT (binfos, i);
191 register tree basetype = BINFO_TYPE (base_binfo);
193 if (TYPE_SIZE (basetype) == 0)
194 /* This error is now reported in xref_tag, thus giving better
195 location information. */
198 /* All basetypes are recorded in the association list of the
201 if (TREE_VIA_VIRTUAL (base_binfo))
206 /* The offset for a virtual base class is only used in computing
207 virtual function tables and for initializing virtual base
208 pointers. It is built once `get_vbase_types' is called. */
210 /* If this basetype can come from another vbase pointer
211 without an additional indirection, we will share
212 that pointer. If an indirection is involved, we
213 make our own pointer. */
214 for (j = 0; j < n_baseclasses; j++)
216 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
217 if (! TREE_VIA_VIRTUAL (other_base_binfo)
218 && BINFO_FOR_VBASE (basetype, BINFO_TYPE (other_base_binfo)))
221 FORMAT_VBASE_NAME (name, basetype);
222 decl = build_vtbl_or_vbase_field (get_identifier (name),
223 get_identifier (VTABLE_BASE),
224 build_pointer_type (basetype),
227 BINFO_VPTR_FIELD (base_binfo) = decl;
228 TREE_CHAIN (decl) = vbase_decls;
233 /* The space this decl occupies has already been accounted for. */
241 /* Called from build_vbase_offset_vtbl_entries via dfs_walk. */
244 dfs_build_vbase_offset_vtbl_entries (binfo, data)
248 tree list = (tree) data;
250 if (TREE_TYPE (list) == binfo)
251 /* The TREE_TYPE of LIST is the base class from which we started
252 walking. If that BINFO is virtual it's not a virtual baseclass
255 else if (TREE_VIA_VIRTUAL (binfo))
260 /* Remember the index to the vbase offset for this virtual
262 vbase = BINFO_FOR_VBASE (TREE_TYPE (binfo), TREE_PURPOSE (list));
263 if (!TREE_VALUE (list))
264 BINFO_VPTR_FIELD (vbase) = build_int_2 (-1, 0);
267 BINFO_VPTR_FIELD (vbase) = TREE_PURPOSE (TREE_VALUE (list));
268 BINFO_VPTR_FIELD (vbase) = ssize_binop (MINUS_EXPR,
269 BINFO_VPTR_FIELD (vbase),
273 /* And record the offset at which this virtual base lies in the
275 init = BINFO_OFFSET (binfo);
276 TREE_VALUE (list) = tree_cons (BINFO_VPTR_FIELD (vbase),
281 SET_BINFO_VTABLE_PATH_MARKED (binfo);
286 /* Returns the initializers for the vbase offset entries in the vtable
287 for BINFO (which is part of the class hierarchy dominated by T), in
291 build_vbase_offset_vtbl_entries (binfo, t)
299 /* Under the old ABI, pointers to virtual bases are stored in each
301 if (!vbase_offsets_in_vtable_p ())
304 /* If there are no virtual baseclasses, then there is nothing to
306 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
311 /* The offsets are allocated in the reverse order of a
312 depth-first left-to-right traversal of the hierarchy. We use
313 BINFO_VTABLE_PATH_MARKED because we are ourselves during a
314 dfs_walk, and so BINFO_MARKED is already in use. */
315 list = build_tree_list (t, NULL_TREE);
316 TREE_TYPE (list) = binfo;
318 dfs_build_vbase_offset_vtbl_entries,
319 dfs_vtable_path_unmarked_real_bases_queue_p,
322 dfs_vtable_path_unmark,
323 dfs_vtable_path_marked_real_bases_queue_p,
325 inits = nreverse (TREE_VALUE (list));
327 /* We've now got offsets in the right oder. However, the offsets
328 we've stored are offsets from the beginning of the complete
329 object, and we need offsets from this BINFO. */
330 for (init = inits; init; init = TREE_CHAIN (init))
332 tree exp = TREE_VALUE (init);
334 exp = ssize_binop (MINUS_EXPR, exp, BINFO_OFFSET (binfo));
335 exp = build1 (NOP_EXPR, vtable_entry_type, exp);
337 TREE_CONSTANT (exp) = 1;
338 /* The dfs_build_vbase_offset_vtbl_entries routine uses the
339 TREE_PURPOSE to scribble in. But, we need to clear it now so
340 that the values are not perceived as labeled initializers. */
341 TREE_PURPOSE (init) = NULL_TREE;
342 TREE_VALUE (init) = exp;
348 /* Returns a pointer to the virtual base class of EXP that has the
349 indicated TYPE. EXP is of class type, not a pointer type. */
352 build_vbase_pointer (exp, type)
355 if (vbase_offsets_in_vtable_p ())
360 /* Find the shared copy of TYPE; that's where the vtable offset
362 vbase = BINFO_FOR_VBASE (type, TREE_TYPE (exp));
363 /* Find the virtual function table pointer. */
364 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
365 /* Compute the location where the offset will lie. */
366 vbase_ptr = build_binary_op (PLUS_EXPR,
368 BINFO_VPTR_FIELD (vbase));
369 vbase_ptr = build1 (NOP_EXPR,
370 build_pointer_type (ptrdiff_type_node),
372 /* Add the contents of this location to EXP. */
373 return build (PLUS_EXPR,
374 build_pointer_type (type),
375 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
376 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
381 FORMAT_VBASE_NAME (name, type);
382 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
386 /* Build multi-level access to EXPR using hierarchy path PATH.
387 CODE is PLUS_EXPR if we are going with the grain,
388 and MINUS_EXPR if we are not (in which case, we cannot traverse
389 virtual baseclass links).
391 TYPE is the type we want this path to have on exit.
393 NONNULL is non-zero if we know (for any reason) that EXPR is
394 not, in fact, zero. */
397 build_vbase_path (code, type, expr, path, nonnull)
399 tree type, expr, path;
402 register int changed = 0;
403 tree last = NULL_TREE, last_virtual = NULL_TREE;
405 tree null_expr = 0, nonnull_expr;
407 tree offset = integer_zero_node;
409 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
410 return build1 (NOP_EXPR, type, expr);
412 /* We could do better if we had additional logic to convert back to the
413 unconverted type (the static type of the complete object), and then
414 convert back to the type we want. Until that is done, we only optimize
415 if the complete type is the same type as expr has. */
416 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
418 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
419 expr = save_expr (expr);
422 if (BINFO_INHERITANCE_CHAIN (path))
423 path = reverse_path (path);
425 basetype = BINFO_TYPE (path);
429 if (TREE_VIA_VIRTUAL (path))
431 last_virtual = BINFO_TYPE (path);
432 if (code == PLUS_EXPR)
434 changed = ! fixed_type_p;
440 /* We already check for ambiguous things in the caller, just
444 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
445 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
447 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
448 nonnull_expr = build_vbase_pointer (ind, last_virtual);
450 && TREE_CODE (type) == POINTER_TYPE
451 && null_expr == NULL_TREE)
453 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
454 expr = build (COND_EXPR, build_pointer_type (last_virtual),
455 build (EQ_EXPR, boolean_type_node, expr,
457 null_expr, nonnull_expr);
460 /* else we'll figure out the offset below. */
462 /* Happens in the case of parse errors. */
463 if (nonnull_expr == error_mark_node)
464 return error_mark_node;
468 cp_error ("cannot cast up from virtual baseclass `%T'",
470 return error_mark_node;
474 path = BINFO_INHERITANCE_CHAIN (path);
476 /* LAST is now the last basetype assoc on the path. */
478 /* A pointer to a virtual base member of a non-null object
479 is non-null. Therefore, we only need to test for zeroness once.
480 Make EXPR the canonical expression to deal with here. */
483 TREE_OPERAND (expr, 2) = nonnull_expr;
484 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
485 = TREE_TYPE (nonnull_expr);
490 /* If we go through any virtual base pointers, make sure that
491 casts to BASETYPE from the last virtual base class use
492 the right value for BASETYPE. */
495 tree intype = TREE_TYPE (TREE_TYPE (expr));
496 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
498 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
499 offset = BINFO_OFFSET (binfo);
503 offset = BINFO_OFFSET (last);
505 if (TREE_INT_CST_LOW (offset))
507 /* Bash types to make the backend happy. */
508 offset = cp_convert (type, offset);
510 /* This shouldn't be necessary. (mrs) */
511 expr = build1 (NOP_EXPR, type, expr);
514 /* If expr might be 0, we need to preserve that zeroness. */
518 TREE_TYPE (null_expr) = type;
520 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
521 if (TREE_SIDE_EFFECTS (expr))
522 expr = save_expr (expr);
524 return build (COND_EXPR, type,
525 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
527 build (code, type, expr, offset));
529 else return build (code, type, expr, offset);
532 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
533 be used multiple times in initialization of multiple inheritance. */
536 TREE_TYPE (expr) = type;
540 return build1 (NOP_EXPR, type, expr);
544 /* Virtual function things. */
546 /* Build an entry in the virtual function table.
547 DELTA is the offset for the `this' pointer.
548 PFN is an ADDR_EXPR containing a pointer to the virtual function.
549 Note that the index (DELTA2) in the virtual function table
553 build_vtable_entry (delta, pfn)
556 if (flag_vtable_thunks)
558 HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
559 if (idelta && ! DECL_PURE_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
561 pfn = build1 (ADDR_EXPR, vtable_entry_type,
562 make_thunk (pfn, idelta));
563 TREE_READONLY (pfn) = 1;
564 TREE_CONSTANT (pfn) = 1;
566 #ifdef GATHER_STATISTICS
567 n_vtable_entries += 1;
573 extern int flag_huge_objects;
574 tree elems = tree_cons (NULL_TREE, delta,
575 tree_cons (NULL_TREE, integer_zero_node,
576 build_tree_list (NULL_TREE, pfn)));
577 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
579 /* DELTA used to be constructed by `size_int' and/or size_binop,
580 which caused overflow problems when it was negative. That should
583 if (! int_fits_type_p (delta, delta_type_node))
585 if (flag_huge_objects)
586 sorry ("object size exceeds built-in limit for virtual function table implementation");
588 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
591 TREE_CONSTANT (entry) = 1;
592 TREE_STATIC (entry) = 1;
593 TREE_READONLY (entry) = 1;
595 #ifdef GATHER_STATISTICS
596 n_vtable_entries += 1;
603 /* Build a vtable entry for FNDECL. DELTA is the amount by which we
604 must adjust the this pointer when calling F. */
607 build_vtable_entry_for_fn (delta, fndecl)
613 /* Take the address of the function, considering it to be of an
614 appropriate generic type. */
615 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
616 /* The address of a function can't change. */
617 TREE_CONSTANT (pfn) = 1;
618 /* Now build the vtable entry itself. */
619 return build_vtable_entry (delta, pfn);
622 /* We want to give the assembler the vtable identifier as well as
623 the offset to the function pointer. So we generate
625 __asm__ __volatile__ (".vtable_entry %c0, %c1"
626 : : "s"(&class_vtable),
627 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
630 build_vtable_entry_ref (basetype, vtbl, idx)
631 tree basetype, vtbl, idx;
633 static char asm_stmt[] = ".vtable_entry %c0, %c1";
636 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
637 s = build_tree_list (build_string (1, "s"), s);
639 i = build_array_ref (vtbl, idx);
640 if (!flag_vtable_thunks)
641 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
642 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
643 i2 = build_array_ref (vtbl, build_int_2(0,0));
644 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
645 i = build_binary_op (MINUS_EXPR, i, i2);
646 i = build_tree_list (build_string (1, "i"), i);
648 finish_asm_stmt (ridpointers[RID_VOLATILE],
649 build_string (sizeof(asm_stmt)-1, asm_stmt),
650 NULL_TREE, chainon (s, i), NULL_TREE);
653 /* Given an object INSTANCE, return an expression which yields the
654 virtual function vtable element corresponding to INDEX. There are
655 many special cases for INSTANCE which we take care of here, mainly
656 to avoid creating extra tree nodes when we don't have to. */
659 build_vtbl_ref (instance, idx)
663 tree basetype = TREE_TYPE (instance);
665 if (TREE_CODE (basetype) == REFERENCE_TYPE)
666 basetype = TREE_TYPE (basetype);
668 if (instance == current_class_ref)
669 vtbl = build_vfield_ref (instance, basetype);
674 /* Try to figure out what a reference refers to, and
675 access its virtual function table directly. */
676 tree ref = NULL_TREE;
678 if (TREE_CODE (instance) == INDIRECT_REF
679 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
680 ref = TREE_OPERAND (instance, 0);
681 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
684 if (ref && TREE_CODE (ref) == VAR_DECL
685 && DECL_INITIAL (ref))
687 tree init = DECL_INITIAL (ref);
689 while (TREE_CODE (init) == NOP_EXPR
690 || TREE_CODE (init) == NON_LVALUE_EXPR)
691 init = TREE_OPERAND (init, 0);
692 if (TREE_CODE (init) == ADDR_EXPR)
694 init = TREE_OPERAND (init, 0);
695 if (IS_AGGR_TYPE (TREE_TYPE (init))
696 && (TREE_CODE (init) == PARM_DECL
697 || TREE_CODE (init) == VAR_DECL))
703 if (IS_AGGR_TYPE (TREE_TYPE (instance))
704 && (TREE_CODE (instance) == RESULT_DECL
705 || TREE_CODE (instance) == PARM_DECL
706 || TREE_CODE (instance) == VAR_DECL))
707 vtbl = TYPE_BINFO_VTABLE (basetype);
709 vtbl = build_vfield_ref (instance, basetype);
712 assemble_external (vtbl);
715 build_vtable_entry_ref (basetype, vtbl, idx);
717 aref = build_array_ref (vtbl, idx);
722 /* Given an object INSTANCE, return an expression which yields the
723 virtual function corresponding to INDEX. There are many special
724 cases for INSTANCE which we take care of here, mainly to avoid
725 creating extra tree nodes when we don't have to. */
728 build_vfn_ref (ptr_to_instptr, instance, idx)
729 tree *ptr_to_instptr, instance;
732 tree aref = build_vtbl_ref (instance, idx);
734 /* When using thunks, there is no extra delta, and we get the pfn
736 if (flag_vtable_thunks)
741 /* Save the intermediate result in a SAVE_EXPR so we don't have to
742 compute each component of the virtual function pointer twice. */
743 if (TREE_CODE (aref) == INDIRECT_REF)
744 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
747 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
749 cp_convert (ptrdiff_type_node,
750 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
753 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
756 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
757 for the given TYPE. */
760 get_vtable_name (type)
763 tree type_id = build_typename_overload (type);
764 char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
765 + IDENTIFIER_LENGTH (type_id) + 2);
766 const char *ptr = IDENTIFIER_POINTER (type_id);
768 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
770 /* We don't take off the numbers; prepare_fresh_vtable uses the
771 DECL_ASSEMBLER_NAME for the type, which includes the number
772 in `3foo'. If we were to pull them off here, we'd end up with
773 something like `_vt.foo.3bar', instead of a uniform definition. */
774 while (ptr[i] >= '0' && ptr[i] <= '9')
777 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
778 return get_identifier (buf);
781 /* Return the offset to the main vtable for a given base BINFO. */
784 get_vfield_offset (binfo)
788 = size_binop (FLOOR_DIV_EXPR,
789 DECL_FIELD_BITPOS (TYPE_VFIELD (BINFO_TYPE (binfo))),
790 size_int (BITS_PER_UNIT));
791 tmp = convert (sizetype, tmp);
792 return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
795 /* Get the offset to the start of the original binfo that we derived
796 this binfo from. If we find TYPE first, return the offset only
797 that far. The shortened search is useful because the this pointer
798 on method calling is expected to point to a DECL_CONTEXT (fndecl)
799 object, and not a baseclass of it. */
802 get_derived_offset (binfo, type)
805 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
808 while (BINFO_BASETYPES (binfo)
809 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
811 tree binfos = BINFO_BASETYPES (binfo);
812 if (BINFO_TYPE (binfo) == type)
814 binfo = TREE_VEC_ELT (binfos, i);
816 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
817 return size_binop (MINUS_EXPR, offset1, offset2);
820 /* Update the rtti info for this class. */
823 set_rtti_entry (virtuals, offset, type)
824 tree virtuals, offset, type;
828 if (CLASSTYPE_COM_INTERFACE (type))
832 fn = get_tinfo_fn_unused (type);
834 /* If someone tries to get RTTI information for a type compiled
835 without RTTI, they're out of luck. By calling __pure_virtual
836 in this case, we give a small clue as to what went wrong. We
837 could consider having a __no_typeinfo function as well, for a
838 more specific hint. */
841 if (flag_vtable_thunks)
843 /* The first slot holds the offset. */
844 TREE_PURPOSE (virtuals) = offset;
846 /* The next node holds the function. */
847 virtuals = TREE_CHAIN (virtuals);
848 offset = integer_zero_node;
851 /* This slot holds the function to call. */
852 TREE_PURPOSE (virtuals) = offset;
853 TREE_VALUE (virtuals) = fn;
856 /* Build a virtual function for type TYPE.
857 If BINFO is non-NULL, build the vtable starting with the initial
858 approximation that it is the same as the one which is the head of
859 the association list. */
862 build_vtable (binfo, type)
865 tree name = get_vtable_name (type);
872 if (BINFO_NEW_VTABLE_MARKED (binfo))
873 /* We have already created a vtable for this base, so there's
874 no need to do it again. */
877 virtuals = copy_list (BINFO_VIRTUALS (binfo));
878 decl = build_lang_decl (VAR_DECL, name,
879 TREE_TYPE (BINFO_VTABLE (binfo)));
881 /* Now do rtti stuff. */
882 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
883 offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
884 set_rtti_entry (virtuals, offset, type);
888 virtuals = NULL_TREE;
889 decl = build_lang_decl (VAR_DECL, name, void_type_node);
892 #ifdef GATHER_STATISTICS
894 n_vtable_elems += list_length (virtuals);
897 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
898 import_export_vtable (decl, type, 0);
900 decl = pushdecl_top_level (decl);
901 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
902 /* Initialize the association list for this type, based
903 on our first approximation. */
904 TYPE_BINFO_VTABLE (type) = decl;
905 TYPE_BINFO_VIRTUALS (type) = virtuals;
907 DECL_ARTIFICIAL (decl) = 1;
908 TREE_STATIC (decl) = 1;
909 #ifndef WRITABLE_VTABLES
910 /* Make them READONLY by default. (mrs) */
911 TREE_READONLY (decl) = 1;
913 /* At one time the vtable info was grabbed 2 words at a time. This
914 fails on sparc unless you have 8-byte alignment. (tiemann) */
915 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
918 DECL_VIRTUAL_P (decl) = 1;
919 DECL_CONTEXT (decl) = type;
921 binfo = TYPE_BINFO (type);
922 SET_BINFO_NEW_VTABLE_MARKED (binfo);
925 /* Give TYPE a new virtual function table which is initialized
926 with a skeleton-copy of its original initialization. The only
927 entry that changes is the `delta' entry, so we can really
928 share a lot of structure.
930 FOR_TYPE is the derived type which caused this table to
933 BINFO is the type association which provided TYPE for FOR_TYPE.
935 The order in which vtables are built (by calling this function) for
936 an object must remain the same, otherwise a binary incompatibility
940 prepare_fresh_vtable (binfo, for_type)
941 tree binfo, for_type;
944 tree orig_decl = BINFO_VTABLE (binfo);
957 if (BINFO_NEW_VTABLE_MARKED (binfo))
958 /* We already created a vtable for this base. There's no need to
962 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
964 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
965 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
967 /* We know that the vtable that we are going to create doesn't exist
968 yet in the global namespace, and when we finish, it will be
969 pushed into the global namespace. In complex MI hierarchies, we
970 have to loop while the name we are thinking of adding is globally
971 defined, adding more name components to the vtable name as we
972 loop, until the name is unique. This is because in complex MI
973 cases, we might have the same base more than once. This means
974 that the order in which this function is called for vtables must
975 remain the same, otherwise binary compatibility can be
980 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
984 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
986 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
987 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
988 name = get_identifier (buf);
990 /* If this name doesn't clash, then we can use it, otherwise
991 we add more to the name until it is unique. */
993 if (! IDENTIFIER_GLOBAL_VALUE (name))
996 /* Set values for next loop through, if the name isn't unique. */
998 path = BINFO_INHERITANCE_CHAIN (path);
1000 /* We better not run out of stuff to make it unique. */
1001 my_friendly_assert (path != NULL_TREE, 368);
1003 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
1005 if (for_type == basetype)
1007 /* If we run out of basetypes in the path, we have already
1008 found created a vtable with that name before, we now
1009 resort to tacking on _%d to distinguish them. */
1011 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
1012 buf1 = (char *) alloca (i);
1014 sprintf (buf1, "%s%c%s%c%d",
1015 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
1017 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
1018 + strlen (buf1) + 1);
1019 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1020 name = get_identifier (buf);
1022 /* If this name doesn't clash, then we can use it,
1023 otherwise we add something different to the name until
1025 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
1027 /* Hey, they really like MI don't they? Increase the 3
1028 above to 6, and the 999 to 999999. :-) */
1029 my_friendly_assert (j <= 999, 369);
1034 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
1035 new_buf2 = (char *) alloca (i);
1036 sprintf (new_buf2, "%s%c%s",
1037 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
1041 new_decl = build_lang_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
1042 /* Remember which class this vtable is really for. */
1043 DECL_CONTEXT (new_decl) = for_type;
1045 DECL_ARTIFICIAL (new_decl) = 1;
1046 TREE_STATIC (new_decl) = 1;
1047 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
1048 DECL_VIRTUAL_P (new_decl) = 1;
1049 #ifndef WRITABLE_VTABLES
1050 /* Make them READONLY by default. (mrs) */
1051 TREE_READONLY (new_decl) = 1;
1053 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
1055 /* Make fresh virtual list, so we can smash it later. */
1056 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
1058 if (TREE_VIA_VIRTUAL (binfo))
1060 tree binfo1 = BINFO_FOR_VBASE (BINFO_TYPE (binfo), for_type);
1062 /* XXX - This should never happen, if it does, the caller should
1063 ensure that the binfo is from for_type's binfos, not from any
1064 base type's. We can remove all this code after a while. */
1065 if (binfo1 != binfo)
1066 warning ("internal inconsistency: binfo offset error for rtti");
1068 offset = BINFO_OFFSET (binfo1);
1071 offset = BINFO_OFFSET (binfo);
1073 set_rtti_entry (BINFO_VIRTUALS (binfo),
1074 ssize_binop (MINUS_EXPR, integer_zero_node, offset),
1077 #ifdef GATHER_STATISTICS
1079 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
1082 /* Set TREE_PUBLIC and TREE_EXTERN as appropriate. */
1083 import_export_vtable (new_decl, for_type, 0);
1085 if (TREE_VIA_VIRTUAL (binfo))
1086 my_friendly_assert (binfo == BINFO_FOR_VBASE (BINFO_TYPE (binfo),
1087 current_class_type),
1089 SET_BINFO_NEW_VTABLE_MARKED (binfo);
1092 /* Change the offset for the FNDECL entry to NEW_OFFSET. Also update
1093 DECL_VINDEX (FNDECL). */
1096 modify_vtable_entry (old_entry_in_list, new_offset, fndecl)
1097 tree old_entry_in_list, new_offset, fndecl;
1099 tree base_fndecl = TREE_VALUE (old_entry_in_list);
1101 /* Update the entry. */
1102 TREE_PURPOSE (old_entry_in_list) = new_offset;
1103 TREE_VALUE (old_entry_in_list) = fndecl;
1105 /* Now assign virtual dispatch information, if unset. We can
1106 dispatch this, through any overridden base function. */
1107 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1109 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1110 DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
1114 /* Access the virtual function table entry N. VIRTUALS is the virtual
1115 function table's initializer. */
1118 get_vtable_entry_n (virtuals, n)
1120 unsigned HOST_WIDE_INT n;
1125 virtuals = TREE_CHAIN (virtuals);
1130 /* Call this function whenever its known that a vtable for T is going
1131 to be needed. It's safe to call it more than once. *HAS_VIRTUAL_P
1132 is initialized to the number of slots that are reserved at the
1133 beginning of the vtable for RTTI information. */
1136 start_vtable (t, has_virtual_p)
1140 if (*has_virtual_p == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1142 /* If we are using thunks, use two slots at the front, one
1143 for the offset pointer, one for the tdesc pointer.
1144 For ARM-style vtables, use the same slot for both. */
1145 if (flag_vtable_thunks)
1152 /* Add a virtual function to all the appropriate vtables for the class
1153 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1154 allocate a new slot in our table. If it is error_mark_node, we
1155 know that no other function from another vtable is overridden by X.
1156 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1157 vtable for the type, and we build upon the NEW_VIRTUALS list
1161 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1162 has_virtual, fndecl, t)
1163 tree *new_virtuals_p;
1164 tree *overridden_virtuals_p;
1167 tree t; /* Structure type. */
1169 my_friendly_assert (DECL_CONTEXT (fndecl) == t, 20000116);
1171 /* If this function doesn't override anything from a base class, we
1172 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1173 override something, we keep it around and assign its DECL_VINDEX
1174 later, in modify_all_vtables. */
1175 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1176 /* We've already dealt with this function. */
1178 else if (DECL_VINDEX (fndecl) == error_mark_node)
1180 /* FNDECL is a new virtual function; it doesn't override any
1181 virtual function in a base class. */
1183 /* We remember that this was the base sub-object for rtti. */
1184 CLASSTYPE_RTTI (t) = t;
1186 start_vtable (t, has_virtual);
1188 /* Build a new INT_CST for this DECL_VINDEX. */
1190 static tree index_table[256];
1192 /* We skip a slot for the offset/tdesc entry. */
1193 int i = (*has_virtual)++;
1195 if (i >= 256 || index_table[i] == 0)
1197 idx = build_int_2 (i, 0);
1199 index_table[i] = idx;
1202 idx = index_table[i];
1204 /* Now assign virtual dispatch information. */
1205 DECL_VINDEX (fndecl) = idx;
1207 /* Save the state we've computed on the NEW_VIRTUALS list. */
1208 *new_virtuals_p = tree_cons (integer_zero_node,
1212 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1213 /* FNDECL overrides a function from a base class. */
1214 *overridden_virtuals_p = tree_cons (NULL_TREE,
1216 *overridden_virtuals_p);
1219 extern struct obstack *current_obstack;
1221 /* Add method METHOD to class TYPE.
1223 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1224 the class type where the method should be added. */
1227 add_method (type, fields, method)
1228 tree type, *fields, method;
1230 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1232 DECL_CONTEXT (method) = type;
1233 DECL_CLASS_CONTEXT (method) = type;
1235 if (fields && *fields)
1236 *fields = build_overload (method, *fields);
1243 if (!CLASSTYPE_METHOD_VEC (type))
1244 /* Make a new method vector. We start with 8 entries. We must
1245 allocate at least two (for constructors and destructors), and
1246 we're going to end up with an assignment operator at some
1249 We could use a TREE_LIST for now, and convert it to a
1250 TREE_VEC in finish_struct, but we would probably waste more
1251 memory making the links in the list than we would by
1252 over-allocating the size of the vector here. Furthermore,
1253 we would complicate all the code that expects this to be a
1255 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1257 method_vec = CLASSTYPE_METHOD_VEC (type);
1258 len = TREE_VEC_LENGTH (method_vec);
1260 if (DECL_NAME (method) == constructor_name (type))
1261 /* A new constructor or destructor. Constructors go in
1262 slot 0; destructors go in slot 1. */
1263 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1266 /* See if we already have an entry with this name. */
1267 for (slot = 2; slot < len; ++slot)
1268 if (!TREE_VEC_ELT (method_vec, slot)
1269 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1271 == DECL_NAME (method)))
1276 /* We need a bigger method vector. */
1277 tree new_vec = make_tree_vec (2 * len);
1278 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1279 (PTR) &TREE_VEC_ELT (new_vec, 0),
1280 len * sizeof (tree));
1282 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1285 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1287 /* Type conversion operators have to come before
1288 ordinary methods; add_conversions depends on this to
1289 speed up looking for conversion operators. So, if
1290 necessary, we slide some of the vector elements up.
1291 In theory, this makes this algorithm O(N^2) but we
1292 don't expect many conversion operators. */
1293 for (slot = 2; slot < len; ++slot)
1295 tree fn = TREE_VEC_ELT (method_vec, slot);
1298 /* There are no more entries in the vector, so we
1299 can insert the new conversion operator here. */
1302 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1303 /* We can insert the new function right at the
1308 if (!TREE_VEC_ELT (method_vec, slot))
1309 /* There is nothing in the Ith slot, so we can avoid
1314 /* We know the last slot in the vector is empty
1315 because we know that at this point there's room
1316 for a new function. */
1317 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1318 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1319 (len - slot - 1) * sizeof (tree));
1320 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1325 if (template_class_depth (type))
1326 /* TYPE is a template class. Don't issue any errors now; wait
1327 until instantiation time to complain. */
1333 /* Check to see if we've already got this method. */
1334 for (fns = TREE_VEC_ELT (method_vec, slot);
1336 fns = OVL_NEXT (fns))
1338 tree fn = OVL_CURRENT (fns);
1340 if (TREE_CODE (fn) != TREE_CODE (method))
1343 if (TREE_CODE (method) != TEMPLATE_DECL)
1345 /* [over.load] Member function declarations with the
1346 same name and the same parameter types cannot be
1347 overloaded if any of them is a static member
1348 function declaration. */
1349 if (DECL_STATIC_FUNCTION_P (fn)
1350 != DECL_STATIC_FUNCTION_P (method))
1352 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1353 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1355 if (! DECL_STATIC_FUNCTION_P (fn))
1356 parms1 = TREE_CHAIN (parms1);
1358 parms2 = TREE_CHAIN (parms2);
1360 if (compparms (parms1, parms2))
1361 cp_error ("`%#D' and `%#D' cannot be overloaded",
1365 /* Since this is an ordinary function in a
1366 non-template class, it's mangled name can be used
1367 as a unique identifier. This technique is only
1368 an optimization; we would get the same results if
1369 we just used decls_match here. */
1370 if (DECL_ASSEMBLER_NAME (fn)
1371 != DECL_ASSEMBLER_NAME (method))
1374 else if (!decls_match (fn, method))
1377 /* There has already been a declaration of this method
1378 or member template. */
1379 cp_error_at ("`%D' has already been declared in `%T'",
1382 /* We don't call duplicate_decls here to merge the
1383 declarations because that will confuse things if the
1384 methods have inline definitions. In particular, we
1385 will crash while processing the definitions. */
1390 /* Actually insert the new method. */
1391 TREE_VEC_ELT (method_vec, slot)
1392 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1394 /* Add the new binding. */
1395 if (!DECL_CONSTRUCTOR_P (method)
1396 && !DECL_DESTRUCTOR_P (method))
1397 push_class_level_binding (DECL_NAME (method),
1398 TREE_VEC_ELT (method_vec, slot));
1402 /* Subroutines of finish_struct. */
1404 /* Look through the list of fields for this struct, deleting
1405 duplicates as we go. This must be recursive to handle
1408 FIELD is the field which may not appear anywhere in FIELDS.
1409 FIELD_PTR, if non-null, is the starting point at which
1410 chained deletions may take place.
1411 The value returned is the first acceptable entry found
1414 Note that anonymous fields which are not of UNION_TYPE are
1415 not duplicates, they are just anonymous fields. This happens
1416 when we have unnamed bitfields, for example. */
1419 delete_duplicate_fields_1 (field, fields)
1424 if (DECL_NAME (field) == 0)
1426 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1429 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1430 fields = delete_duplicate_fields_1 (x, fields);
1435 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1437 if (DECL_NAME (x) == 0)
1439 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1441 TYPE_FIELDS (TREE_TYPE (x))
1442 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1443 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1446 fields = TREE_CHAIN (fields);
1448 TREE_CHAIN (prev) = TREE_CHAIN (x);
1451 else if (TREE_CODE (field) == USING_DECL)
1452 /* A using declaration may is allowed to appear more than
1453 once. We'll prune these from the field list later, and
1454 handle_using_decl will complain about invalid multiple
1457 else if (DECL_NAME (field) == DECL_NAME (x))
1459 if (TREE_CODE (field) == CONST_DECL
1460 && TREE_CODE (x) == CONST_DECL)
1461 cp_error_at ("duplicate enum value `%D'", x);
1462 else if (TREE_CODE (field) == CONST_DECL
1463 || TREE_CODE (x) == CONST_DECL)
1464 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1466 else if (DECL_DECLARES_TYPE_P (field)
1467 && DECL_DECLARES_TYPE_P (x))
1469 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1471 cp_error_at ("duplicate nested type `%D'", x);
1473 else if (DECL_DECLARES_TYPE_P (field)
1474 || DECL_DECLARES_TYPE_P (x))
1476 /* Hide tag decls. */
1477 if ((TREE_CODE (field) == TYPE_DECL
1478 && DECL_ARTIFICIAL (field))
1479 || (TREE_CODE (x) == TYPE_DECL
1480 && DECL_ARTIFICIAL (x)))
1482 cp_error_at ("duplicate field `%D' (as type and non-type)",
1486 cp_error_at ("duplicate member `%D'", x);
1488 fields = TREE_CHAIN (fields);
1490 TREE_CHAIN (prev) = TREE_CHAIN (x);
1498 delete_duplicate_fields (fields)
1502 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1503 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1506 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1507 along the path given by BINFO. Return 1 if change was legit,
1508 otherwise return 0. */
1511 alter_access (t, binfo, fdecl, access)
1517 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1520 if (TREE_VALUE (elem) != access)
1522 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1523 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1525 error ("conflicting access specifications for field `%s', ignored",
1526 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1530 /* They're changing the access to the same thing they changed
1531 it to before. That's OK. */
1537 enforce_access (binfo, fdecl);
1538 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1544 /* Process the USING_DECL, which is a member of T. */
1547 handle_using_decl (using_decl, t)
1551 tree ctype = DECL_INITIAL (using_decl);
1552 tree name = DECL_NAME (using_decl);
1554 = TREE_PRIVATE (using_decl) ? access_private_node
1555 : TREE_PROTECTED (using_decl) ? access_protected_node
1556 : access_public_node;
1558 tree flist = NULL_TREE;
1559 tree fields = TYPE_FIELDS (t);
1560 tree method_vec = CLASSTYPE_METHOD_VEC (t);
1565 binfo = binfo_or_else (ctype, t);
1569 if (name == constructor_name (ctype)
1570 || name == constructor_name_full (ctype))
1572 cp_error_at ("using-declaration for constructor", using_decl);
1576 fdecl = lookup_member (binfo, name, 0, 0);
1580 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1584 /* Functions are represented as TREE_LIST, with the purpose
1585 being the type and the value the functions. Other members
1586 come as themselves. */
1587 if (TREE_CODE (fdecl) == TREE_LIST)
1588 /* Ignore base type this came from. */
1589 fdecl = TREE_VALUE (fdecl);
1591 if (TREE_CODE (fdecl) == OVERLOAD)
1593 /* We later iterate over all functions. */
1595 fdecl = OVL_FUNCTION (flist);
1598 name = DECL_NAME (fdecl);
1599 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1600 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1601 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1604 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1605 cp_error_at (" because of local method `%#D' with same name",
1606 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1610 if (! DECL_LANG_SPECIFIC (fdecl))
1611 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1614 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1615 if (DECL_NAME (tmp) == name)
1617 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1618 cp_error_at (" because of local field `%#D' with same name", tmp);
1622 /* Make type T see field decl FDECL with access ACCESS.*/
1627 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1630 flist = OVL_CHAIN (flist);
1634 alter_access (t, binfo, fdecl, access);
1637 /* Run through the base clases of T, updating
1638 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1639 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1643 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1646 int *cant_have_default_ctor_p;
1647 int *cant_have_const_ctor_p;
1648 int *no_const_asn_ref_p;
1652 int seen_nearly_empty_base_p;
1655 binfos = TYPE_BINFO_BASETYPES (t);
1656 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1657 seen_nearly_empty_base_p = 0;
1659 /* An aggregate cannot have baseclasses. */
1660 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1662 for (i = 0; i < n_baseclasses; ++i)
1667 /* Figure out what base we're looking at. */
1668 base_binfo = TREE_VEC_ELT (binfos, i);
1669 basetype = TREE_TYPE (base_binfo);
1671 /* If the type of basetype is incomplete, then we already
1672 complained about that fact (and we should have fixed it up as
1674 if (TYPE_SIZE (basetype) == 0)
1677 /* The base type is of incomplete type. It is
1678 probably best to pretend that it does not
1680 if (i == n_baseclasses-1)
1681 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1682 TREE_VEC_LENGTH (binfos) -= 1;
1684 for (j = i; j+1 < n_baseclasses; j++)
1685 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1689 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1690 here because the case of virtual functions but non-virtual
1691 dtor is handled in finish_struct_1. */
1692 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1693 && TYPE_HAS_DESTRUCTOR (basetype))
1694 cp_warning ("base class `%#T' has a non-virtual destructor",
1697 /* If the base class doesn't have copy constructors or
1698 assignment operators that take const references, then the
1699 derived class cannot have such a member automatically
1701 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1702 *cant_have_const_ctor_p = 1;
1703 if (TYPE_HAS_ASSIGN_REF (basetype)
1704 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1705 *no_const_asn_ref_p = 1;
1706 /* Similarly, if the base class doesn't have a default
1707 constructor, then the derived class won't have an
1708 automatically generated default constructor. */
1709 if (TYPE_HAS_CONSTRUCTOR (basetype)
1710 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1712 *cant_have_default_ctor_p = 1;
1713 if (! TYPE_HAS_CONSTRUCTOR (t))
1715 cp_pedwarn ("base `%T' with only non-default constructor",
1717 cp_pedwarn ("in class without a constructor");
1721 /* If the base class is not empty or nearly empty, then this
1722 class cannot be nearly empty. */
1723 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1724 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1725 /* And if there is more than one nearly empty base, then the
1726 derived class is not nearly empty either. */
1727 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1728 && seen_nearly_empty_base_p)
1729 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1730 /* If this is the first nearly empty base class, then remember
1732 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1733 seen_nearly_empty_base_p = 1;
1735 /* A lot of properties from the bases also apply to the derived
1737 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1738 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1739 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1740 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1741 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1742 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1743 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1744 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1745 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1747 /* Derived classes can implicitly become COMified if their bases
1749 if (CLASSTYPE_COM_INTERFACE (basetype))
1750 CLASSTYPE_COM_INTERFACE (t) = 1;
1751 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1754 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1756 CLASSTYPE_COM_INTERFACE (t) = 0;
1761 /* Make the Ith baseclass of T its primary base. */
1764 set_primary_base (t, i, has_virtual_p)
1771 CLASSTYPE_VFIELD_PARENT (t) = i;
1772 basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
1773 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1774 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1775 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1776 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1777 *has_virtual_p = CLASSTYPE_VSIZE (basetype);
1780 /* Determine the primary class for T. */
1783 determine_primary_base (t, has_virtual_p)
1787 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1789 /* If there are no baseclasses, there is certainly no primary base. */
1790 if (n_baseclasses == 0)
1795 for (i = 0; i < n_baseclasses; i++)
1797 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1798 tree basetype = BINFO_TYPE (base_binfo);
1800 if (TYPE_CONTAINS_VPTR_P (basetype))
1802 /* Even a virtual baseclass can contain our RTTI
1803 information. But, we prefer a non-virtual polymorphic
1805 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1806 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1808 /* A virtual baseclass can't be the primary base under the
1809 old ABI. And under the new ABI we still prefer a
1810 non-virtual base. */
1811 if (TREE_VIA_VIRTUAL (base_binfo))
1814 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1816 set_primary_base (t, i, has_virtual_p);
1817 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1823 /* Only add unique vfields, and flatten them out as we go. */
1824 for (vfields = CLASSTYPE_VFIELDS (basetype);
1826 vfields = TREE_CHAIN (vfields))
1827 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1828 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1829 CLASSTYPE_VFIELDS (t)
1830 = tree_cons (base_binfo,
1831 VF_BASETYPE_VALUE (vfields),
1832 CLASSTYPE_VFIELDS (t));
1834 if (*has_virtual_p == 0)
1835 set_primary_base (t, i, has_virtual_p);
1840 if (!TYPE_VFIELD (t))
1841 CLASSTYPE_VFIELD_PARENT (t) = -1;
1843 /* The new ABI allows for the use of a "nearly-empty" virtual base
1844 class as the primary base class if no non-virtual polymorphic
1845 base can be found. */
1846 if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1847 for (i = 0; i < n_baseclasses; ++i)
1849 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1850 tree basetype = BINFO_TYPE (base_binfo);
1852 if (TREE_VIA_VIRTUAL (base_binfo)
1853 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1855 set_primary_base (t, i, has_virtual_p);
1856 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1861 /* Mark the primary base classes at this point. */
1862 mark_primary_bases (t);
1865 /* Set memoizing fields and bits of T (and its variants) for later
1869 finish_struct_bits (t)
1872 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1874 /* Fix up variants (if any). */
1875 tree variants = TYPE_NEXT_VARIANT (t);
1878 /* These fields are in the _TYPE part of the node, not in
1879 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1880 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1881 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1882 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1883 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1885 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1886 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1887 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1888 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1889 /* Copy whatever these are holding today. */
1890 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1891 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1892 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1893 TYPE_SIZE (variants) = TYPE_SIZE (t);
1894 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1895 variants = TYPE_NEXT_VARIANT (variants);
1898 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1899 /* For a class w/o baseclasses, `finish_struct' has set
1900 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1901 definition). Similarly for a class whose base classes do not
1902 have vtables. When neither of these is true, we might have
1903 removed abstract virtuals (by providing a definition), added
1904 some (by declaring new ones), or redeclared ones from a base
1905 class. We need to recalculate what's really an abstract virtual
1906 at this point (by looking in the vtables). */
1907 get_pure_virtuals (t);
1911 /* Notice whether this class has type conversion functions defined. */
1912 tree binfo = TYPE_BINFO (t);
1913 tree binfos = BINFO_BASETYPES (binfo);
1916 for (i = n_baseclasses-1; i >= 0; i--)
1918 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1920 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1924 /* If this type has a copy constructor, force its mode to be BLKmode, and
1925 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1926 be passed by invisible reference and prevent it from being returned in
1929 Also do this if the class has BLKmode but can still be returned in
1930 registers, since function_cannot_inline_p won't let us inline
1931 functions returning such a type. This affects the HP-PA. */
1932 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1933 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1934 && CLASSTYPE_NON_AGGREGATE (t)))
1937 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1938 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1940 TYPE_MODE (variants) = BLKmode;
1941 TREE_ADDRESSABLE (variants) = 1;
1946 /* Issue warnings about T having private constructors, but no friends,
1949 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1950 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1951 non-private static member functions. */
1954 maybe_warn_about_overly_private_class (t)
1957 int has_member_fn = 0;
1958 int has_nonprivate_method = 0;
1961 if (!warn_ctor_dtor_privacy
1962 /* If the class has friends, those entities might create and
1963 access instances, so we should not warn. */
1964 || (CLASSTYPE_FRIEND_CLASSES (t)
1965 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1966 /* We will have warned when the template was declared; there's
1967 no need to warn on every instantiation. */
1968 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1969 /* There's no reason to even consider warning about this
1973 /* We only issue one warning, if more than one applies, because
1974 otherwise, on code like:
1977 // Oops - forgot `public:'
1983 we warn several times about essentially the same problem. */
1985 /* Check to see if all (non-constructor, non-destructor) member
1986 functions are private. (Since there are no friends or
1987 non-private statics, we can't ever call any of the private member
1989 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1990 /* We're not interested in compiler-generated methods; they don't
1991 provide any way to call private members. */
1992 if (!DECL_ARTIFICIAL (fn))
1994 if (!TREE_PRIVATE (fn))
1996 if (DECL_STATIC_FUNCTION_P (fn))
1997 /* A non-private static member function is just like a
1998 friend; it can create and invoke private member
1999 functions, and be accessed without a class
2003 has_nonprivate_method = 1;
2006 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2010 if (!has_nonprivate_method && has_member_fn)
2012 /* There are no non-private methods, and there's at least one
2013 private member function that isn't a constructor or
2014 destructor. (If all the private members are
2015 constructors/destructors we want to use the code below that
2016 issues error messages specifically referring to
2017 constructors/destructors.) */
2019 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2020 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2021 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2022 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2024 has_nonprivate_method = 1;
2027 if (!has_nonprivate_method)
2029 cp_warning ("all member functions in class `%T' are private", t);
2034 /* Even if some of the member functions are non-private, the class
2035 won't be useful for much if all the constructors or destructors
2036 are private: such an object can never be created or destroyed. */
2037 if (TYPE_HAS_DESTRUCTOR (t))
2039 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2041 if (TREE_PRIVATE (dtor))
2043 cp_warning ("`%#T' only defines a private destructor and has no friends",
2049 if (TYPE_HAS_CONSTRUCTOR (t))
2051 int nonprivate_ctor = 0;
2053 /* If a non-template class does not define a copy
2054 constructor, one is defined for it, enabling it to avoid
2055 this warning. For a template class, this does not
2056 happen, and so we would normally get a warning on:
2058 template <class T> class C { private: C(); };
2060 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2061 complete non-template or fully instantiated classes have this
2063 if (!TYPE_HAS_INIT_REF (t))
2064 nonprivate_ctor = 1;
2066 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2070 tree ctor = OVL_CURRENT (fn);
2071 /* Ideally, we wouldn't count copy constructors (or, in
2072 fact, any constructor that takes an argument of the
2073 class type as a parameter) because such things cannot
2074 be used to construct an instance of the class unless
2075 you already have one. But, for now at least, we're
2077 if (! TREE_PRIVATE (ctor))
2079 nonprivate_ctor = 1;
2084 if (nonprivate_ctor == 0)
2086 cp_warning ("`%#T' only defines private constructors and has no friends",
2093 /* Function to help qsort sort FIELD_DECLs by name order. */
2096 field_decl_cmp (x, y)
2099 if (DECL_NAME (*x) == DECL_NAME (*y))
2100 /* A nontype is "greater" than a type. */
2101 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2102 if (DECL_NAME (*x) == NULL_TREE)
2104 if (DECL_NAME (*y) == NULL_TREE)
2106 if (DECL_NAME (*x) < DECL_NAME (*y))
2111 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2114 method_name_cmp (m1, m2)
2115 const tree *m1, *m2;
2117 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2119 if (*m1 == NULL_TREE)
2121 if (*m2 == NULL_TREE)
2123 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2128 /* Warn about duplicate methods in fn_fields. Also compact method
2129 lists so that lookup can be made faster.
2131 Data Structure: List of method lists. The outer list is a
2132 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2133 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2134 links the entire list of methods for TYPE_METHODS. Friends are
2135 chained in the same way as member functions (? TREE_CHAIN or
2136 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2137 list. That allows them to be quickly deleted, and requires no
2140 If there are any constructors/destructors, they are moved to the
2141 front of the list. This makes pushclass more efficient.
2143 @@ The above comment is obsolete. It mostly describes what add_method
2144 @@ and add_implicitly_declared_members do.
2146 Sort methods that are not special (i.e., constructors, destructors, and
2147 type conversion operators) so that we can find them faster in search. */
2150 finish_struct_methods (t)
2155 tree ctor_name = constructor_name (t);
2158 if (!TYPE_METHODS (t))
2160 /* Clear these for safety; perhaps some parsing error could set
2161 these incorrectly. */
2162 TYPE_HAS_CONSTRUCTOR (t) = 0;
2163 TYPE_HAS_DESTRUCTOR (t) = 0;
2164 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2168 method_vec = CLASSTYPE_METHOD_VEC (t);
2169 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2170 len = TREE_VEC_LENGTH (method_vec);
2172 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2173 and the next few with type conversion operators (if any). */
2174 for (fn_fields = TYPE_METHODS (t); fn_fields;
2175 fn_fields = TREE_CHAIN (fn_fields))
2177 tree fn_name = DECL_NAME (fn_fields);
2179 /* Clear out this flag.
2181 @@ Doug may figure out how to break
2182 @@ this with nested classes and friends. */
2183 DECL_IN_AGGR_P (fn_fields) = 0;
2185 /* Note here that a copy ctor is private, so we don't dare generate
2186 a default copy constructor for a class that has a member
2187 of this type without making sure they have access to it. */
2188 if (fn_name == ctor_name)
2190 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2191 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2193 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2194 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2196 if (TREE_CHAIN (parmtypes) == NULL_TREE
2197 || TREE_CHAIN (parmtypes) == void_list_node
2198 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2200 if (TREE_PROTECTED (fn_fields))
2201 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2202 else if (TREE_PRIVATE (fn_fields))
2203 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2207 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2209 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2211 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2213 if (TREE_PROTECTED (fn_fields))
2214 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2215 else if (TREE_PRIVATE (fn_fields))
2216 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2221 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2222 /* We thought there was a destructor, but there wasn't. Some
2223 parse errors cause this anomalous situation. */
2224 TYPE_HAS_DESTRUCTOR (t) = 0;
2226 /* Issue warnings about private constructors and such. If there are
2227 no methods, then some public defaults are generated. */
2228 maybe_warn_about_overly_private_class (t);
2230 /* Now sort the methods. */
2231 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2233 TREE_VEC_LENGTH (method_vec) = len;
2235 /* The type conversion ops have to live at the front of the vec, so we
2237 for (slot = 2; slot < len; ++slot)
2239 tree fn = TREE_VEC_ELT (method_vec, slot);
2241 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2245 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2246 (int (*)(const void *, const void *))method_name_cmp);
2249 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2252 duplicate_tag_error (t)
2255 cp_error ("redefinition of `%#T'", t);
2256 cp_error_at ("previous definition here", t);
2258 /* Pretend we haven't defined this type. */
2260 /* All of the component_decl's were TREE_CHAINed together in the parser.
2261 finish_struct_methods walks these chains and assembles all methods with
2262 the same base name into DECL_CHAINs. Now we don't need the parser chains
2263 anymore, so we unravel them. */
2265 /* This used to be in finish_struct, but it turns out that the
2266 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2268 if (CLASSTYPE_METHOD_VEC (t))
2270 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2271 int i, len = TREE_VEC_LENGTH (method_vec);
2272 for (i = 0; i < len; i++)
2274 tree unchain = TREE_VEC_ELT (method_vec, i);
2275 while (unchain != NULL_TREE)
2277 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2278 unchain = OVL_NEXT (unchain);
2283 if (TYPE_LANG_SPECIFIC (t))
2285 tree binfo = TYPE_BINFO (t);
2286 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2287 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2288 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2289 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2291 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2292 BINFO_BASETYPES(binfo) = NULL_TREE;
2294 TYPE_BINFO (t) = binfo;
2295 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2296 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2297 TYPE_REDEFINED (t) = 1;
2298 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2299 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2301 TYPE_SIZE (t) = NULL_TREE;
2302 TYPE_MODE (t) = VOIDmode;
2303 TYPE_FIELDS (t) = NULL_TREE;
2304 TYPE_METHODS (t) = NULL_TREE;
2305 TYPE_VFIELD (t) = NULL_TREE;
2306 TYPE_CONTEXT (t) = NULL_TREE;
2307 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2310 /* Returns the number of extra entries (at negative indices) required
2311 for BINFO's vtable. */
2314 num_extra_vtbl_entries (binfo)
2320 /* Under the old ABI, there are no entries at negative offsets. */
2321 if (!vbase_offsets_in_vtable_p ())
2322 return size_zero_node;
2324 type = BINFO_TYPE (binfo);
2327 /* There is an entry for the offset to each virtual base. */
2328 entries += list_length (CLASSTYPE_VBASECLASSES (type));
2330 return size_int (entries);
2333 /* Returns the offset (in bytes) from the beginning of BINFO's vtable
2334 where the vptr should actually point. */
2337 size_extra_vtbl_entries (binfo)
2342 offset = size_binop (EXACT_DIV_EXPR,
2343 TYPE_SIZE (vtable_entry_type),
2344 size_int (BITS_PER_UNIT));
2345 offset = size_binop (MULT_EXPR, offset, num_extra_vtbl_entries (binfo));
2346 return fold (offset);
2349 /* Construct the initializer for BINFOs virtual function table. BINFO
2350 is part of the hierarchy dominated by T. */
2353 build_vtbl_initializer (binfo, t)
2357 tree v = BINFO_VIRTUALS (binfo);
2358 tree inits = NULL_TREE;
2359 tree type = BINFO_TYPE (binfo);
2361 /* Add entries to the vtable for offsets to our virtual bases. */
2362 inits = build_vbase_offset_vtbl_entries (binfo, t);
2364 /* Process the RTTI stuff at the head of the list. If we're not
2365 using vtable thunks, then the RTTI entry is just an ordinary
2366 function, and we can process it just like the other virtual
2367 function entries. */
2368 if (!CLASSTYPE_COM_INTERFACE (type) && flag_vtable_thunks)
2373 /* The first entry is an offset. */
2374 offset = TREE_PURPOSE (v);
2375 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2378 /* Convert the offset to look like a function pointer, so that
2379 we can put it in the vtable. */
2380 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2381 TREE_CONSTANT (init) = 1;
2382 init = build_vtable_entry (integer_zero_node, init);
2383 inits = tree_cons (NULL_TREE, init, inits);
2385 /* Even in this case, the second entry (the tdesc pointer) is
2386 just an ordinary function. */
2390 /* Go through all the ordinary virtual functions, building up
2398 /* Pull the offset for `this', and the function to call, out of
2400 delta = TREE_PURPOSE (v);
2401 fn = TREE_VALUE (v);
2402 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2403 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2405 /* You can't call an abstract virtual function; it's abstract.
2406 So, we replace these functions with __pure_virtual. */
2407 if (DECL_PURE_VIRTUAL_P (fn))
2410 /* Package up that information for the vtable. */
2411 init = build_vtable_entry_for_fn (delta, fn);
2412 /* And add it to the chain of initializers. */
2413 inits = tree_cons (NULL_TREE, init, inits);
2419 /* The initializers were built up in reverse order; straighten them
2421 inits = nreverse (inits);
2422 /* Package all the initializers up as an array initializer. */
2423 return build_nt (CONSTRUCTOR, NULL_TREE, inits);
2426 /* Called from finish_vtbls via dfs_walk. */
2429 dfs_finish_vtbls (binfo, data)
2433 if (!BINFO_PRIMARY_MARKED_P (binfo)
2434 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2435 && BINFO_NEW_VTABLE_MARKED (binfo))
2440 decl = BINFO_VTABLE (binfo);
2441 context = DECL_CONTEXT (decl);
2442 DECL_CONTEXT (decl) = 0;
2443 DECL_INITIAL (decl) = build_vtbl_initializer (binfo, (tree) data);
2444 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
2445 DECL_CONTEXT (decl) = context;
2448 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2449 SET_BINFO_MARKED (binfo);
2454 /* Create all the necessary vtables for T and its base classes. */
2460 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
2461 dfs_unmarked_real_bases_queue_p, t);
2462 dfs_walk (TYPE_BINFO (t), dfs_unmark,
2463 dfs_marked_real_bases_queue_p, t);
2466 /* True if we should override the given BASE_FNDECL with the given
2470 overrides (fndecl, base_fndecl)
2471 tree fndecl, base_fndecl;
2473 /* Destructors have special names. */
2474 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2475 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2477 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2478 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2480 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2482 tree types, base_types;
2484 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2485 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2487 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2488 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2489 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2490 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2491 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2497 /* Returns the BINFO_OFFSET for the base of BINFO that has the same
2501 get_class_offset_1 (parent, binfo, context, t, fndecl)
2502 tree parent, binfo, context, t, fndecl;
2504 tree binfos = BINFO_BASETYPES (binfo);
2505 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2506 tree rval = NULL_TREE;
2508 if (binfo == parent)
2509 return error_mark_node;
2511 for (i = 0; i < n_baselinks; i++)
2513 tree base_binfo = TREE_VEC_ELT (binfos, i);
2516 if (TREE_VIA_VIRTUAL (base_binfo))
2517 base_binfo = BINFO_FOR_VBASE (BINFO_TYPE (base_binfo), t);
2518 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2519 /* See if we have a new value */
2520 if (nrval && (nrval != error_mark_node || rval==0))
2522 /* Only compare if we have two offsets */
2523 if (rval && rval != error_mark_node
2524 && ! tree_int_cst_equal (nrval, rval))
2526 /* Only give error if the two offsets are different */
2527 error ("every virtual function must have a unique final overrider");
2528 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2529 cp_error (" with virtual `%D' from virtual base class", fndecl);
2535 if (rval && BINFO_TYPE (binfo) == context)
2537 my_friendly_assert (rval == error_mark_node
2538 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2539 rval = BINFO_OFFSET (binfo);
2545 /* Called from get_class_offset via dfs_walk. */
2548 dfs_get_class_offset (binfo, data)
2552 tree list = (tree) data;
2553 tree context = TREE_TYPE (list);
2555 if (same_type_p (BINFO_TYPE (binfo), context))
2557 if (TREE_VALUE (list))
2558 return error_mark_node;
2560 TREE_VALUE (list) = BINFO_OFFSET (binfo);
2563 SET_BINFO_MARKED (binfo);
2568 /* Returns the BINFO_OFFSET for the subobject of BINFO that has the
2569 type given by CONTEXT. */
2572 get_class_offset (context, t, binfo, fndecl)
2573 tree context, t, binfo, fndecl;
2580 return integer_zero_node;
2582 if (BINFO_TYPE (binfo) == context)
2583 return BINFO_OFFSET (binfo);
2585 /* Check less derived binfos first. */
2586 while (BINFO_BASETYPES (binfo)
2587 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2589 tree binfos = BINFO_BASETYPES (binfo);
2590 binfo = TREE_VEC_ELT (binfos, i);
2591 if (BINFO_TYPE (binfo) == context)
2592 return BINFO_OFFSET (binfo);
2595 list = build_tree_list (t, NULL_TREE);
2596 TREE_TYPE (list) = context;
2597 offset = dfs_walk (TYPE_BINFO (t),
2598 dfs_get_class_offset,
2599 dfs_unmarked_real_bases_queue_p,
2601 dfs_walk (TYPE_BINFO (t), dfs_unmark, dfs_marked_real_bases_queue_p, t);
2603 if (offset == error_mark_node)
2605 error ("every virtual function must have a unique final overrider");
2606 cp_error (" found two (or more) `%T' class subobjects in `%T'",
2608 cp_error (" with virtual `%D' from virtual base class", fndecl);
2609 offset = integer_zero_node;
2612 offset = TREE_VALUE (list);
2614 my_friendly_assert (offset != NULL_TREE, 999);
2615 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST, 999);
2620 /* Return the BINFO_VIRTUALS list for BINFO, without the RTTI stuff at
2621 the front. If non-NULL, N is set to the number of entries
2625 skip_rtti_stuff (binfo, t, n)
2628 unsigned HOST_WIDE_INT *n;
2632 if (CLASSTYPE_COM_INTERFACE (t))
2637 virtuals = BINFO_VIRTUALS (binfo);
2640 /* We always reserve a slot for the offset/tdesc entry. */
2643 virtuals = TREE_CHAIN (virtuals);
2645 if (flag_vtable_thunks && virtuals)
2647 /* The second slot is reserved for the tdesc pointer when thunks
2651 virtuals = TREE_CHAIN (virtuals);
2658 modify_one_vtable (binfo, t, fndecl)
2659 tree binfo, t, fndecl;
2662 unsigned HOST_WIDE_INT n;
2664 /* update rtti entry */
2667 if (binfo == TYPE_BINFO (t))
2668 build_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))), t);
2670 prepare_fresh_vtable (binfo, t);
2672 if (fndecl == NULL_TREE)
2675 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), &n);
2679 tree current_fndecl = TREE_VALUE (virtuals);
2681 /* We should never have an instance of __pure_virtual on the
2682 BINFO_VIRTUALS list. If we do, then we will never notice
2683 that the function that should have been there instead has
2685 my_friendly_assert (current_fndecl != abort_fndecl,
2688 if (current_fndecl && overrides (fndecl, current_fndecl))
2690 tree base_offset, offset;
2691 tree context = DECL_CLASS_CONTEXT (fndecl);
2692 tree vfield = TYPE_VFIELD (t);
2695 offset = get_class_offset (context, t, binfo, fndecl);
2697 /* Find the right offset for the this pointer based on the
2698 base class we just found. We have to take into
2699 consideration the virtual base class pointers that we
2700 stick in before the virtual function table pointer.
2702 Also, we want just the delta between the most base class
2703 that we derived this vfield from and us. */
2704 base_offset = size_binop (PLUS_EXPR,
2705 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2706 BINFO_OFFSET (binfo));
2707 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2709 if (binfo == TYPE_BINFO (t))
2710 /* In this case, it is *type*'s vtable we are modifying.
2711 We start with the approximation that it's vtable is
2712 that of the immediate base class. */
2713 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2715 /* This is our very own copy of `basetype' to play with.
2716 Later, we will fill in all the virtual functions that
2717 override the virtual functions in these base classes
2718 which are not defined by the current type. */
2719 prepare_fresh_vtable (binfo, t);
2722 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2724 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2729 virtuals = TREE_CHAIN (virtuals);
2733 /* Called from modify_all_vtables via dfs_walk. */
2736 dfs_modify_vtables (binfo, data)
2740 if (/* There's no need to modify the vtable for a primary base;
2741 we're not going to use that vtable anyhow. */
2742 !BINFO_PRIMARY_MARKED_P (binfo)
2743 /* Similarly, a base without a vtable needs no modification. */
2744 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2746 tree list = (tree) data;
2747 modify_one_vtable (binfo, TREE_PURPOSE (list), TREE_VALUE (list));
2750 SET_BINFO_MARKED (binfo);
2756 modify_all_vtables (t, fndecl)
2762 list = build_tree_list (t, fndecl);
2763 dfs_walk (TYPE_BINFO (t), dfs_modify_vtables,
2764 dfs_unmarked_real_bases_queue_p, list);
2765 dfs_walk (TYPE_BINFO (t), dfs_unmark, dfs_marked_real_bases_queue_p, t);
2768 /* Fixup all the delta entries in this one vtable that need updating. */
2771 dfs_fixup_vtable_deltas (binfo, data)
2776 unsigned HOST_WIDE_INT n;
2777 tree t = (tree) data;
2779 while (BINFO_PRIMARY_MARKED_P (binfo))
2781 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2782 /* If BINFO is virtual then we'll handle this base later. */
2783 if (TREE_VIA_VIRTUAL (binfo))
2787 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), &n);
2791 tree fndecl = TREE_VALUE (virtuals);
2792 tree delta = TREE_PURPOSE (virtuals);
2796 tree base_offset, offset;
2797 tree context = DECL_CLASS_CONTEXT (fndecl);
2798 tree vfield = TYPE_VFIELD (t);
2801 offset = get_class_offset (context, t, binfo, fndecl);
2803 /* Find the right offset for the this pointer based on the
2804 base class we just found. We have to take into
2805 consideration the virtual base class pointers that we
2806 stick in before the virtual function table pointer.
2808 Also, we want just the delta between the most base class
2809 that we derived this vfield from and us. */
2810 base_offset = size_binop (PLUS_EXPR,
2811 get_derived_offset (binfo,
2812 DECL_CONTEXT (fndecl)),
2813 BINFO_OFFSET (binfo));
2814 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2816 if (! tree_int_cst_equal (this_offset, delta))
2818 /* Make sure we can modify the derived association with immunity. */
2819 if (binfo == TYPE_BINFO (t))
2820 /* In this case, it is *type*'s vtable we are modifying.
2821 We start with the approximation that it's vtable is that
2822 of the immediate base class. */
2823 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2825 /* This is our very own copy of `basetype' to play
2826 with. Later, we will fill in all the virtual
2827 functions that override the virtual functions in
2828 these base classes which are not defined by the
2830 prepare_fresh_vtable (binfo, t);
2832 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2838 virtuals = TREE_CHAIN (virtuals);
2844 /* Here, we already know that they match in every respect.
2845 All we have to check is where they had their declarations. */
2848 strictly_overrides (fndecl1, fndecl2)
2849 tree fndecl1, fndecl2;
2851 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2852 DECL_CLASS_CONTEXT (fndecl1),
2854 if (distance == -2 || distance > 0)
2859 /* Merge overrides for one vtable.
2860 If we want to merge in same function, we are fine.
2862 if one has a DECL_CLASS_CONTEXT that is a parent of the
2863 other, than choose the more derived one
2865 potentially ill-formed (see 10.3 [class.virtual])
2866 we have to check later to see if there was an
2867 override in this class. If there was ok, if not
2868 then it is ill-formed. (mrs)
2870 We take special care to reuse a vtable, if we can. */
2873 override_one_vtable (binfo, old, t)
2880 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2882 /* Either or both of BINFO or OLD might be primary base classes
2883 because merge_overrides is called with a vbase from the class we
2884 are definining and the corresponding vbase from one of its direct
2887 while (BINFO_PRIMARY_MARKED_P (binfo))
2889 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2890 /* If BINFO is virtual, then we'll handle this virtual base when
2892 if (TREE_VIA_VIRTUAL (binfo))
2895 while (BINFO_PRIMARY_MARKED_P (old))
2896 old = BINFO_INHERITANCE_CHAIN (old);
2898 /* If we have already committed to modifying it, then don't try and
2899 reuse another vtable. */
2900 if (BINFO_NEW_VTABLE_MARKED (binfo))
2903 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), NULL);
2904 old_virtuals = skip_rtti_stuff (old, BINFO_TYPE (binfo), NULL);
2905 orig_virtuals = skip_rtti_stuff (orig_binfo, BINFO_TYPE (binfo), NULL);
2907 while (orig_virtuals)
2909 tree fndecl = TREE_VALUE (virtuals);
2910 tree old_fndecl = TREE_VALUE (old_virtuals);
2912 /* First check to see if they are the same. */
2913 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2915 /* No need to do anything. */
2917 else if (strictly_overrides (fndecl, old_fndecl))
2919 if (choose == UNDECIDED)
2921 else if (choose == REUSE_OLD)
2924 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2926 prepare_fresh_vtable (binfo, t);
2927 override_one_vtable (binfo, old, t);
2932 else if (strictly_overrides (old_fndecl, fndecl))
2934 if (choose == UNDECIDED)
2936 else if (choose == REUSE_NEW)
2939 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2941 prepare_fresh_vtable (binfo, t);
2942 override_one_vtable (binfo, old, t);
2945 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2947 else if (choose == NEITHER)
2949 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2955 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2957 prepare_fresh_vtable (binfo, t);
2958 override_one_vtable (binfo, old, t);
2962 /* This MUST be overridden, or the class is ill-formed. */
2963 tree fndecl = TREE_VALUE (virtuals);
2965 fndecl = copy_node (fndecl);
2966 copy_lang_decl (fndecl);
2967 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
2968 /* Make sure we search for it later. */
2969 if (! CLASSTYPE_PURE_VIRTUALS (t))
2970 CLASSTYPE_PURE_VIRTUALS (t) = error_mark_node;
2972 /* We can use integer_zero_node, as we will core dump
2973 if this is used anyway. */
2974 TREE_PURPOSE (virtuals) = integer_zero_node;
2975 TREE_VALUE (virtuals) = fndecl;
2978 virtuals = TREE_CHAIN (virtuals);
2979 old_virtuals = TREE_CHAIN (old_virtuals);
2980 orig_virtuals = TREE_CHAIN (orig_virtuals);
2983 /* Let's reuse the old vtable. */
2984 if (choose == REUSE_OLD)
2986 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2987 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2991 /* Merge in overrides for virtual bases.
2992 BINFO is the hierarchy we want to modify, and OLD has the potential
2996 merge_overrides (binfo, old, do_self, t)
3001 tree binfos = BINFO_BASETYPES (binfo);
3002 tree old_binfos = BINFO_BASETYPES (old);
3003 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3005 /* Should we use something besides CLASSTYPE_VFIELDS? */
3006 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
3008 override_one_vtable (binfo, old, t);
3011 for (i = 0; i < n_baselinks; i++)
3013 tree base_binfo = TREE_VEC_ELT (binfos, i);
3014 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
3015 int is_not_base_vtable
3016 = !BINFO_PRIMARY_MARKED_P (base_binfo);
3017 if (! TREE_VIA_VIRTUAL (base_binfo))
3018 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
3022 /* Get the base virtual function declarations in T that are either
3023 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
3024 the overrider/hider. */
3027 get_basefndecls (fndecl, t)
3030 tree methods = TYPE_METHODS (t);
3031 tree base_fndecls = NULL_TREE;
3032 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3033 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3037 if (TREE_CODE (methods) == FUNCTION_DECL
3038 && DECL_VINDEX (methods) != NULL_TREE
3039 && DECL_NAME (fndecl) == DECL_NAME (methods))
3040 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
3042 methods = TREE_CHAIN (methods);
3046 return base_fndecls;
3048 for (i = 0; i < n_baseclasses; i++)
3050 tree base_binfo = TREE_VEC_ELT (binfos, i);
3051 tree basetype = BINFO_TYPE (base_binfo);
3053 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3057 return base_fndecls;
3060 /* Mark the functions that have been hidden with their overriders.
3061 Since we start out with all functions already marked with a hider,
3062 no need to mark functions that are just hidden.
3064 Subroutine of warn_hidden. */
3067 mark_overriders (fndecl, base_fndecls)
3068 tree fndecl, base_fndecls;
3070 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3072 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
3073 TREE_PURPOSE (base_fndecls) = fndecl;
3077 /* If this declaration supersedes the declaration of
3078 a method declared virtual in the base class, then
3079 mark this field as being virtual as well. */
3082 check_for_override (decl, ctype)
3085 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
3086 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3087 int virtualp = DECL_VIRTUAL_P (decl);
3088 int found_overriden_fn = 0;
3090 for (i = 0; i < n_baselinks; i++)
3092 tree base_binfo = TREE_VEC_ELT (binfos, i);
3093 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
3095 tree tmp = get_matching_virtual
3097 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
3099 if (tmp && !found_overriden_fn)
3101 /* If this function overrides some virtual in some base
3102 class, then the function itself is also necessarily
3103 virtual, even if the user didn't explicitly say so. */
3104 DECL_VIRTUAL_P (decl) = 1;
3106 /* The TMP we really want is the one from the deepest
3107 baseclass on this path, taking care not to
3108 duplicate if we have already found it (via another
3109 path to its virtual baseclass. */
3110 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
3112 cp_error_at ("`static %#D' cannot be declared", decl);
3113 cp_error_at (" since `virtual %#D' declared in base class",
3119 /* Set DECL_VINDEX to a value that is neither an
3120 INTEGER_CST nor the error_mark_node so that
3121 add_virtual_function will realize this is an
3122 overridden function. */
3124 = tree_cons (tmp, NULL_TREE, DECL_VINDEX (decl));
3126 /* We now know that DECL overrides something,
3127 which is all that is important. But, we must
3128 continue to iterate through all the base-classes
3129 in order to allow get_matching_virtual to check for
3130 various illegal overrides. */
3131 found_overriden_fn = 1;
3137 if (DECL_VINDEX (decl) == NULL_TREE)
3138 DECL_VINDEX (decl) = error_mark_node;
3139 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3143 /* Warn about hidden virtual functions that are not overridden in t.
3144 We know that constructors and destructors don't apply. */
3150 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3151 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3154 /* We go through each separately named virtual function. */
3155 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3157 tree fns = TREE_VEC_ELT (method_vec, i);
3158 tree fndecl = NULL_TREE;
3160 tree base_fndecls = NULL_TREE;
3161 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3162 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3164 /* First see if we have any virtual functions in this batch. */
3165 for (; fns; fns = OVL_NEXT (fns))
3167 fndecl = OVL_CURRENT (fns);
3168 if (DECL_VINDEX (fndecl))
3172 if (fns == NULL_TREE)
3175 /* First we get a list of all possible functions that might be
3176 hidden from each base class. */
3177 for (i = 0; i < n_baseclasses; i++)
3179 tree base_binfo = TREE_VEC_ELT (binfos, i);
3180 tree basetype = BINFO_TYPE (base_binfo);
3182 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3186 fns = OVL_NEXT (fns);
3188 /* ...then mark up all the base functions with overriders, preferring
3189 overriders to hiders. */
3191 for (; fns; fns = OVL_NEXT (fns))
3193 fndecl = OVL_CURRENT (fns);
3194 if (DECL_VINDEX (fndecl))
3195 mark_overriders (fndecl, base_fndecls);
3198 /* Now give a warning for all base functions without overriders,
3199 as they are hidden. */
3200 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3202 if (! overrides (TREE_PURPOSE (base_fndecls),
3203 TREE_VALUE (base_fndecls)))
3205 /* Here we know it is a hider, and no overrider exists. */
3206 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3207 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3213 /* Check for things that are invalid. There are probably plenty of other
3214 things we should check for also. */
3217 finish_struct_anon (t)
3222 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3224 if (TREE_STATIC (field))
3226 if (TREE_CODE (field) != FIELD_DECL)
3229 if (DECL_NAME (field) == NULL_TREE
3230 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3232 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3233 for (; elt; elt = TREE_CHAIN (elt))
3235 if (DECL_ARTIFICIAL (elt))
3238 if (DECL_NAME (elt) == constructor_name (t))
3239 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3242 if (TREE_CODE (elt) != FIELD_DECL)
3244 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3249 if (TREE_PRIVATE (elt))
3250 cp_pedwarn_at ("private member `%#D' in anonymous union",
3252 else if (TREE_PROTECTED (elt))
3253 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3256 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3257 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3263 extern int interface_only, interface_unknown;
3265 /* Create default constructors, assignment operators, and so forth for
3266 the type indicated by T, if they are needed.
3267 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3268 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3269 cannot have a default constructor, copy constructor taking a const
3270 reference argument, or an assignment operator, respectively. If a
3271 virtual destructor is created, its DECL is returned; otherwise the
3272 return value is NULL_TREE. */
3275 add_implicitly_declared_members (t, cant_have_default_ctor,
3276 cant_have_const_cctor,
3277 cant_have_assignment)
3279 int cant_have_default_ctor;
3280 int cant_have_const_cctor;
3281 int cant_have_assignment;
3284 tree implicit_fns = NULL_TREE;
3285 tree name = TYPE_IDENTIFIER (t);
3286 tree virtual_dtor = NULL_TREE;
3290 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3292 default_fn = cons_up_default_function (t, name, 0);
3293 check_for_override (default_fn, t);
3295 /* If we couldn't make it work, then pretend we didn't need it. */
3296 if (default_fn == void_type_node)
3297 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3300 TREE_CHAIN (default_fn) = implicit_fns;
3301 implicit_fns = default_fn;
3303 if (DECL_VINDEX (default_fn))
3304 virtual_dtor = default_fn;
3307 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3309 /* Default constructor. */
3310 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3312 default_fn = cons_up_default_function (t, name, 2);
3313 TREE_CHAIN (default_fn) = implicit_fns;
3314 implicit_fns = default_fn;
3317 /* Copy constructor. */
3318 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3320 /* ARM 12.18: You get either X(X&) or X(const X&), but
3322 default_fn = cons_up_default_function (t, name,
3323 3 + cant_have_const_cctor);
3324 TREE_CHAIN (default_fn) = implicit_fns;
3325 implicit_fns = default_fn;
3328 /* Assignment operator. */
3329 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3331 default_fn = cons_up_default_function (t, name,
3332 5 + cant_have_assignment);
3333 TREE_CHAIN (default_fn) = implicit_fns;
3334 implicit_fns = default_fn;
3337 /* Now, hook all of the new functions on to TYPE_METHODS,
3338 and add them to the CLASSTYPE_METHOD_VEC. */
3339 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3340 add_method (t, 0, *f);
3341 *f = TYPE_METHODS (t);
3342 TYPE_METHODS (t) = implicit_fns;
3344 return virtual_dtor;
3347 /* Subroutine of finish_struct_1. Recursively count the number of fields
3348 in TYPE, including anonymous union members. */
3351 count_fields (fields)
3356 for (x = fields; x; x = TREE_CHAIN (x))
3358 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3359 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3366 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3367 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3370 add_fields_to_vec (fields, field_vec, idx)
3371 tree fields, field_vec;
3375 for (x = fields; x; x = TREE_CHAIN (x))
3377 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3378 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3380 TREE_VEC_ELT (field_vec, idx++) = x;
3385 /* FIELD is a bit-field. We are finishing the processing for its
3386 enclosing type. Issue any appropriate messages and set appropriate
3390 check_bitfield_decl (field)
3393 tree type = TREE_TYPE (field);
3395 /* Invalid bit-field size done by grokfield. */
3396 /* Detect invalid bit-field type. Simply checking if TYPE is
3397 integral is insufficient, as that is the array core of the field
3398 type. If TREE_TYPE (field) is integral, then TYPE must be the same. */
3399 if (DECL_INITIAL (field)
3400 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3402 cp_error_at ("bit-field `%#D' with non-integral type", field);
3403 DECL_INITIAL (field) = NULL;
3406 /* Detect and ignore out of range field width. */
3407 if (DECL_INITIAL (field))
3409 tree w = DECL_INITIAL (field);
3410 register int width = 0;
3412 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3415 /* detect invalid field size. */
3416 if (TREE_CODE (w) == CONST_DECL)
3417 w = DECL_INITIAL (w);
3418 else if (TREE_READONLY_DECL_P (w))
3419 w = decl_constant_value (w);
3421 if (TREE_CODE (w) != INTEGER_CST)
3423 cp_error_at ("bit-field `%D' width not an integer constant",
3425 DECL_INITIAL (field) = NULL_TREE;
3427 else if (width = TREE_INT_CST_LOW (w),
3430 DECL_INITIAL (field) = NULL;
3431 cp_error_at ("negative width in bit-field `%D'", field);
3433 else if (width == 0 && DECL_NAME (field) != 0)
3435 DECL_INITIAL (field) = NULL;
3436 cp_error_at ("zero width for bit-field `%D'", field);
3439 > TYPE_PRECISION (long_long_unsigned_type_node))
3441 /* The backend will dump if you try to use something too
3443 DECL_INITIAL (field) = NULL;
3444 sorry ("bit-fields larger than %d bits",
3445 TYPE_PRECISION (long_long_unsigned_type_node));
3446 cp_error_at (" in declaration of `%D'", field);
3448 else if (width > TYPE_PRECISION (type)
3449 && TREE_CODE (type) != ENUMERAL_TYPE
3450 && TREE_CODE (type) != BOOLEAN_TYPE)
3451 cp_warning_at ("width of `%D' exceeds its type", field);
3452 else if (TREE_CODE (type) == ENUMERAL_TYPE
3453 && ((min_precision (TYPE_MIN_VALUE (type),
3454 TREE_UNSIGNED (type)) > width)
3455 || (min_precision (TYPE_MAX_VALUE (type),
3456 TREE_UNSIGNED (type)) > width)))
3457 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3460 if (DECL_INITIAL (field))
3462 DECL_INITIAL (field) = NULL_TREE;
3463 DECL_FIELD_SIZE (field) = width;
3464 DECL_BIT_FIELD (field) = 1;
3468 #ifdef EMPTY_FIELD_BOUNDARY
3469 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3470 EMPTY_FIELD_BOUNDARY);
3472 #ifdef PCC_BITFIELD_TYPE_MATTERS
3473 if (PCC_BITFIELD_TYPE_MATTERS)
3474 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3481 /* Non-bit-fields are aligned for their type. */
3482 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3485 /* FIELD is a non bit-field. We are finishing the processing for its
3486 enclosing type T. Issue any appropriate messages and set appropriate
3490 check_field_decl (field, t, cant_have_const_ctor,
3491 cant_have_default_ctor, no_const_asn_ref,
3492 any_default_members)
3495 int *cant_have_const_ctor;
3496 int *cant_have_default_ctor;
3497 int *no_const_asn_ref;
3498 int *any_default_members;
3500 tree type = strip_array_types (TREE_TYPE (field));
3502 /* An anonymous union cannot contain any fields which would change
3503 the settings of CANT_HAVE_CONST_CTOR and friends. */
3504 if (ANON_UNION_TYPE_P (type))
3506 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3507 structs. So, we recurse through their fields here. */
3508 else if (ANON_AGGR_TYPE_P (type))
3512 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3513 if (TREE_CODE (field) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3514 check_field_decl (fields, t, cant_have_const_ctor,
3515 cant_have_default_ctor, no_const_asn_ref,
3516 any_default_members);
3518 /* Check members with class type for constructors, destructors,
3520 else if (CLASS_TYPE_P (type))
3522 /* Never let anything with uninheritable virtuals
3523 make it through without complaint. */
3524 abstract_virtuals_error (field, type);
3526 if (TREE_CODE (t) == UNION_TYPE)
3528 if (TYPE_NEEDS_CONSTRUCTING (type))
3529 cp_error_at ("member `%#D' with constructor not allowed in union",
3531 if (TYPE_NEEDS_DESTRUCTOR (type))
3532 cp_error_at ("member `%#D' with destructor not allowed in union",
3534 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3535 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3540 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3541 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3542 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3543 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3546 if (!TYPE_HAS_CONST_INIT_REF (type))
3547 *cant_have_const_ctor = 1;
3549 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3550 *no_const_asn_ref = 1;
3552 if (TYPE_HAS_CONSTRUCTOR (type)
3553 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3554 *cant_have_default_ctor = 1;
3556 if (DECL_INITIAL (field) != NULL_TREE)
3558 /* `build_class_init_list' does not recognize
3560 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3561 cp_error_at ("multiple fields in union `%T' initialized");
3562 *any_default_members = 1;
3565 /* Non-bit-fields are aligned for their type, except packed fields
3566 which require only BITS_PER_UNIT alignment. */
3567 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3568 (DECL_PACKED (field)
3570 : TYPE_ALIGN (TREE_TYPE (field))));
3573 /* Check the data members (both static and non-static), class-scoped
3574 typedefs, etc., appearing in the declaration of T. Issue
3575 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3576 declaration order) of access declarations; each TREE_VALUE in this
3577 list is a USING_DECL.
3579 In addition, set the following flags:
3582 The class is empty, i.e., contains no non-static data members.
3584 CANT_HAVE_DEFAULT_CTOR_P
3585 This class cannot have an implicitly generated default
3588 CANT_HAVE_CONST_CTOR_P
3589 This class cannot have an implicitly generated copy constructor
3590 taking a const reference.
3592 CANT_HAVE_CONST_ASN_REF
3593 This class cannot have an implicitly generated assignment
3594 operator taking a const reference.
3596 All of these flags should be initialized before calling this
3599 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3600 fields can be added by adding to this chain. */
3603 check_field_decls (t, access_decls, empty_p,
3604 cant_have_default_ctor_p, cant_have_const_ctor_p,
3609 int *cant_have_default_ctor_p;
3610 int *cant_have_const_ctor_p;
3611 int *no_const_asn_ref_p;
3616 int any_default_members;
3618 /* First, delete any duplicate fields. */
3619 delete_duplicate_fields (TYPE_FIELDS (t));
3621 /* Assume there are no access declarations. */
3622 *access_decls = NULL_TREE;
3623 /* Assume this class has no pointer members. */
3625 /* Assume none of the members of this class have default
3627 any_default_members = 0;
3629 for (field = &TYPE_FIELDS (t); *field; field = next)
3632 tree type = TREE_TYPE (x);
3634 GNU_xref_member (current_class_name, x);
3636 next = &TREE_CHAIN (x);
3638 if (TREE_CODE (x) == FIELD_DECL)
3640 DECL_PACKED (x) |= TYPE_PACKED (t);
3642 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3643 /* We don't treat zero-width bitfields as making a class
3648 /* The class is non-empty. */
3650 /* The class is not even nearly empty. */
3651 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3655 if (TREE_CODE (x) == USING_DECL)
3657 /* Prune the access declaration from the list of fields. */
3658 *field = TREE_CHAIN (x);
3660 /* Save the access declarations for our caller. */
3661 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3663 /* Since we've reset *FIELD there's no reason to skip to the
3669 if (TREE_CODE (x) == TYPE_DECL
3670 || TREE_CODE (x) == TEMPLATE_DECL)
3673 /* If we've gotten this far, it's a data member, possibly static,
3674 or an enumerator. */
3676 DECL_FIELD_CONTEXT (x) = t;
3678 /* ``A local class cannot have static data members.'' ARM 9.4 */
3679 if (current_function_decl && TREE_STATIC (x))
3680 cp_error_at ("field `%D' in local class cannot be static", x);
3682 /* Perform error checking that did not get done in
3684 if (TREE_CODE (type) == FUNCTION_TYPE)
3686 cp_error_at ("field `%D' invalidly declared function type",
3688 type = build_pointer_type (type);
3689 TREE_TYPE (x) = type;
3691 else if (TREE_CODE (type) == METHOD_TYPE)
3693 cp_error_at ("field `%D' invalidly declared method type", x);
3694 type = build_pointer_type (type);
3695 TREE_TYPE (x) = type;
3697 else if (TREE_CODE (type) == OFFSET_TYPE)
3699 cp_error_at ("field `%D' invalidly declared offset type", x);
3700 type = build_pointer_type (type);
3701 TREE_TYPE (x) = type;
3704 if (type == error_mark_node)
3707 DECL_SAVED_INSNS (x) = 0;
3708 DECL_FIELD_SIZE (x) = 0;
3710 /* When this goes into scope, it will be a non-local reference. */
3711 DECL_NONLOCAL (x) = 1;
3713 if (TREE_CODE (x) == CONST_DECL)
3716 if (TREE_CODE (x) == VAR_DECL)
3718 if (TREE_CODE (t) == UNION_TYPE)
3719 /* Unions cannot have static members. */
3720 cp_error_at ("field `%D' declared static in union", x);
3725 /* Now it can only be a FIELD_DECL. */
3727 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3728 CLASSTYPE_NON_AGGREGATE (t) = 1;
3730 /* If this is of reference type, check if it needs an init.
3731 Also do a little ANSI jig if necessary. */
3732 if (TREE_CODE (type) == REFERENCE_TYPE)
3734 CLASSTYPE_NON_POD_P (t) = 1;
3735 if (DECL_INITIAL (x) == NULL_TREE)
3736 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3738 /* ARM $12.6.2: [A member initializer list] (or, for an
3739 aggregate, initialization by a brace-enclosed list) is the
3740 only way to initialize nonstatic const and reference
3742 *cant_have_default_ctor_p = 1;
3743 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3745 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3748 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3750 cp_warning_at ("non-static reference in class without a constructor", x);
3754 type = strip_array_types (type);
3756 if (TREE_CODE (type) == POINTER_TYPE)
3759 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3760 CLASSTYPE_HAS_MUTABLE (t) = 1;
3762 if (! pod_type_p (type)
3763 /* For some reason, pointers to members are POD types themselves,
3764 but are not allowed in POD structs. Silly. */
3765 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3766 CLASSTYPE_NON_POD_P (t) = 1;
3768 /* If any field is const, the structure type is pseudo-const. */
3769 if (CP_TYPE_CONST_P (type))
3771 C_TYPE_FIELDS_READONLY (t) = 1;
3772 if (DECL_INITIAL (x) == NULL_TREE)
3773 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3775 /* ARM $12.6.2: [A member initializer list] (or, for an
3776 aggregate, initialization by a brace-enclosed list) is the
3777 only way to initialize nonstatic const and reference
3779 *cant_have_default_ctor_p = 1;
3780 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3782 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3785 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3787 cp_warning_at ("non-static const member in class without a constructor", x);
3790 /* A field that is pseudo-const makes the structure likewise. */
3791 else if (IS_AGGR_TYPE (type))
3793 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3794 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3795 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3798 /* We set DECL_C_BIT_FIELD in grokbitfield.
3799 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3800 if (DECL_C_BIT_FIELD (x))
3801 check_bitfield_decl (x);
3803 check_field_decl (x, t,
3804 cant_have_const_ctor_p,
3805 cant_have_default_ctor_p,
3807 &any_default_members);
3810 /* Effective C++ rule 11. */
3811 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3812 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3814 cp_warning ("`%#T' has pointer data members", t);
3816 if (! TYPE_HAS_INIT_REF (t))
3818 cp_warning (" but does not override `%T(const %T&)'", t, t);
3819 if (! TYPE_HAS_ASSIGN_REF (t))
3820 cp_warning (" or `operator=(const %T&)'", t);
3822 else if (! TYPE_HAS_ASSIGN_REF (t))
3823 cp_warning (" but does not override `operator=(const %T&)'", t);
3827 /* Check anonymous struct/anonymous union fields. */
3828 finish_struct_anon (t);
3830 /* We've built up the list of access declarations in reverse order.
3832 *access_decls = nreverse (*access_decls);
3835 /* Return a FIELD_DECL for a pointer-to-virtual-table or
3836 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
3837 field are as indicated. The CLASS_TYPE in which this field occurs
3838 is also indicated. *EMPTY_P is set to a non-zero value by this
3839 function to indicate that a class containing this field is
3843 build_vtbl_or_vbase_field (name, assembler_name, type, class_type,
3846 tree assembler_name;
3853 /* This class is non-empty. */
3856 /* Build the FIELD_DECL. */
3857 field = build_lang_decl (FIELD_DECL, name, type);
3858 DECL_ASSEMBLER_NAME (field) = assembler_name;
3859 DECL_VIRTUAL_P (field) = 1;
3860 DECL_ARTIFICIAL (field) = 1;
3861 DECL_FIELD_CONTEXT (field) = class_type;
3862 DECL_CLASS_CONTEXT (field) = class_type;
3863 DECL_FCONTEXT (field) = class_type;
3864 DECL_SAVED_INSNS (field) = 0;
3865 DECL_FIELD_SIZE (field) = 0;
3866 DECL_ALIGN (field) = TYPE_ALIGN (type);
3872 /* If the empty base field in DECL overlaps with a base of the same type in
3873 NEWDECL, which is either another base field or the first data field of
3874 the class, pad the base just before NEWDECL and return 1. Otherwise,
3878 avoid_overlap (decl, newdecl, empty_p)
3884 if (newdecl == NULL_TREE
3885 || ! types_overlap_p (TREE_TYPE (decl), TREE_TYPE (newdecl)))
3888 for (field = decl; TREE_CHAIN (field) && TREE_CHAIN (field) != newdecl;
3889 field = TREE_CHAIN (field))
3892 DECL_SIZE (field) = integer_one_node;
3893 /* The containing class cannot be empty; this field takes up space. */
3899 /* Build a FIELD_DECL for the base given by BINFO in T. If the new
3900 object is non-empty, clear *EMPTY_P. Otherwise, set *SAW_EMPTY_P.
3901 *BASE_ALIGN is a running maximum of the alignments of any base
3905 build_base_field (t, binfo, empty_p, saw_empty_p, base_align)
3910 unsigned int *base_align;
3912 tree basetype = BINFO_TYPE (binfo);
3915 if (TYPE_SIZE (basetype) == 0)
3916 /* This error is now reported in xref_tag, thus giving better
3917 location information. */
3920 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
3921 DECL_ARTIFICIAL (decl) = 1;
3922 DECL_FIELD_CONTEXT (decl) = DECL_CLASS_CONTEXT (decl) = t;
3923 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3924 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3926 if (flag_new_abi && DECL_SIZE (decl) == integer_zero_node)
3932 /* The containing class is non-empty because it has a non-empty base
3938 /* Brain damage for backwards compatibility. For no good
3939 reason, the old layout_basetypes made every base at least
3940 as large as the alignment for the bases up to that point,
3941 gratuitously wasting space. So we do the same thing
3943 *base_align = MAX (*base_align, DECL_ALIGN (decl));
3945 = size_int (MAX (TREE_INT_CST_LOW (DECL_SIZE (decl)),
3946 (int) (*base_align)));
3952 /* Returns a list of fields to stand in for the base class subobjects
3953 of REC. These fields are later removed by layout_basetypes. */
3956 build_base_fields (rec, empty_p)
3960 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3962 tree base_decls = NULL_TREE;
3963 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3964 tree decl, nextdecl;
3965 int i, saw_empty = 0;
3966 unsigned int base_align = 0;
3968 /* Under the new ABI, the primary base class is always allocated
3970 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
3974 primary_base = CLASSTYPE_PRIMARY_BINFO (rec);
3975 base_decls = chainon (build_base_field (rec,
3983 /* Now allocate the rest of the bases. */
3984 for (i = 0; i < n_baseclasses; ++i)
3988 /* Under the new ABI, the primary base was already allocated
3989 above, so we don't need to allocate it again here. */
3990 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
3993 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3995 /* A primary virtual base class is allocated just like any other
3996 base class, but a non-primary virtual base is allocated
3997 later, in layout_basetypes. */
3998 if (TREE_VIA_VIRTUAL (base_binfo)
3999 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4002 base_decls = chainon (build_base_field (rec, base_binfo,
4009 /* Reverse the list of fields so we allocate the bases in the proper
4011 base_decls = nreverse (base_decls);
4013 /* In the presence of empty base classes, we run the risk of allocating
4014 two objects of the same class on top of one another. Avoid that. */
4015 if (flag_new_abi && saw_empty)
4016 for (decl = base_decls; decl; decl = TREE_CHAIN (decl))
4018 if (DECL_SIZE (decl) == integer_zero_node)
4020 /* First step through the following bases until we find
4021 an overlap or a non-empty base. */
4022 for (nextdecl = TREE_CHAIN (decl); nextdecl;
4023 nextdecl = TREE_CHAIN (nextdecl))
4025 if (avoid_overlap (decl, nextdecl, empty_p)
4026 || DECL_SIZE (nextdecl) != integer_zero_node)
4030 /* If we're still looking, also check against the first
4032 for (nextdecl = TYPE_FIELDS (rec);
4033 nextdecl && TREE_CODE (nextdecl) != FIELD_DECL;
4034 nextdecl = TREE_CHAIN (nextdecl))
4036 avoid_overlap (decl, nextdecl, empty_p);
4044 /* Go through the TYPE_METHODS of T issuing any appropriate
4045 diagnostics, figuring out which methods override which other
4046 methods, and so forth. */
4054 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4056 GNU_xref_member (current_class_name, x);
4058 /* If this was an evil function, don't keep it in class. */
4059 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4062 /* Do both of these, even though they're in the same union;
4063 if the insn `r' member and the size `i' member are
4064 different sizes, as on the alpha, the larger of the two
4065 will end up with garbage in it. */
4066 DECL_SAVED_INSNS (x) = 0;
4067 DECL_FIELD_SIZE (x) = 0;
4069 check_for_override (x, t);
4070 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4071 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4073 /* The name of the field is the original field name
4074 Save this in auxiliary field for later overloading. */
4075 if (DECL_VINDEX (x))
4077 TYPE_POLYMORPHIC_P (t) = 1;
4078 if (DECL_PURE_VIRTUAL_P (x))
4079 CLASSTYPE_PURE_VIRTUALS (t)
4080 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4085 /* Remove all zero-width bit-fields from T. */
4088 remove_zero_width_bit_fields (t)
4093 fieldsp = &TYPE_FIELDS (t);
4096 if (TREE_CODE (*fieldsp) == FIELD_DECL
4097 && DECL_C_BIT_FIELD (*fieldsp)
4098 && DECL_INITIAL (*fieldsp))
4099 *fieldsp = TREE_CHAIN (*fieldsp);
4101 fieldsp = &TREE_CHAIN (*fieldsp);
4105 /* Check the validity of the bases and members declared in T. Add any
4106 implicitly-generated functions (like copy-constructors and
4107 assignment operators). Compute various flag bits (like
4108 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4109 level: i.e., independently of the ABI in use. */
4112 check_bases_and_members (t, empty_p)
4116 /* Nonzero if we are not allowed to generate a default constructor
4118 int cant_have_default_ctor;
4119 /* Nonzero if the implicitly generated copy constructor should take
4120 a non-const reference argument. */
4121 int cant_have_const_ctor;
4122 /* Nonzero if the the implicitly generated assignment operator
4123 should take a non-const reference argument. */
4124 int no_const_asn_ref;
4127 /* By default, we use const reference arguments and generate default
4129 cant_have_default_ctor = 0;
4130 cant_have_const_ctor = 0;
4131 no_const_asn_ref = 0;
4133 /* Assume that the class is nearly empty; we'll clear this flag if
4134 it turns out not to be nearly empty. */
4135 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4137 /* Check all the base-classes. */
4138 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4141 /* Check all the data member declarations. */
4142 check_field_decls (t, &access_decls, empty_p,
4143 &cant_have_default_ctor,
4144 &cant_have_const_ctor,
4147 /* Check all the method declarations. */
4150 /* A nearly-empty class has to be vptr-containing; a nearly empty
4151 class contains just a vptr. */
4152 if (!TYPE_CONTAINS_VPTR_P (t))
4153 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4155 /* Do some bookkeeping that will guide the generation of implicitly
4156 declared member functions. */
4157 TYPE_HAS_COMPLEX_INIT_REF (t)
4158 |= (TYPE_HAS_INIT_REF (t)
4159 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4160 || TYPE_POLYMORPHIC_P (t));
4161 TYPE_NEEDS_CONSTRUCTING (t)
4162 |= (TYPE_HAS_CONSTRUCTOR (t)
4163 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4164 || TYPE_POLYMORPHIC_P (t));
4165 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4166 || TYPE_POLYMORPHIC_P (t));
4167 CLASSTYPE_NON_POD_P (t)
4168 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4169 || TYPE_HAS_ASSIGN_REF (t));
4170 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4171 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4172 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4174 /* Synthesize any needed methods. Note that methods will be synthesized
4175 for anonymous unions; grok_x_components undoes that. */
4176 add_implicitly_declared_members (t, cant_have_default_ctor,
4177 cant_have_const_ctor,
4180 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4181 finish_struct_methods (t);
4183 /* Process the access-declarations. We wait until now to do this
4184 because handle_using_decls requires that the CLASSTYPE_METHOD_VEC
4185 be set up correctly. */
4186 while (access_decls)
4188 handle_using_decl (TREE_VALUE (access_decls), t);
4189 access_decls = TREE_CHAIN (access_decls);
4193 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4194 accordingly, and, if necessary, add the TYPE_VFIELD to the
4195 TYPE_FIELDS list. */
4198 create_vtable_ptr (t, empty_p, has_virtual_p,
4199 new_virtuals_p, overridden_virtuals_p)
4203 tree *new_virtuals_p;
4204 tree *overridden_virtuals_p;
4208 /* Loop over the virtual functions, adding them to our various
4210 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4211 if (DECL_VINDEX (fn))
4212 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4213 has_virtual_p, fn, t);
4215 /* Even if there weren't any new virtual functions, we might need a
4216 new virtual function table if we're supposed to include vptrs in
4217 all classes that need them. */
4218 if (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())
4219 start_vtable (t, has_virtual_p);
4221 /* If we couldn't find an appropriate base class, create a new field
4223 if (*has_virtual_p && !TYPE_VFIELD (t))
4225 /* We build this decl with vtbl_ptr_type_node, which is a
4226 `vtable_entry_type*'. It might seem more precise to use
4227 `vtable_entry_type (*)[N]' where N is the number of firtual
4228 functions. However, that would require the vtable pointer in
4229 base classes to have a different type than the vtable pointer
4230 in derived classes. We could make that happen, but that
4231 still wouldn't solve all the problems. In particular, the
4232 type-based alias analysis code would decide that assignments
4233 to the base class vtable pointer can't alias assignments to
4234 the derived class vtable pointer, since they have different
4235 types. Thus, in an derived class destructor, where the base
4236 class constructor was inlined, we could generate bad code for
4237 setting up the vtable pointer.
4239 Therefore, we use one type for all vtable pointers. We still
4240 use a type-correct type; it's just doesn't indicate the array
4241 bounds. That's better than using `void*' or some such; it's
4242 cleaner, and it let's the alias analysis code know that these
4243 stores cannot alias stores to void*! */
4245 = build_vtbl_or_vbase_field (get_vfield_name (t),
4246 get_identifier (VFIELD_BASE),
4251 /* Add the new field to the list of fields in this class. */
4253 /* In the old ABI, the vtable pointer goes at the end of the
4255 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), TYPE_VFIELD (t));
4258 /* But in the new ABI, the vtable pointer is the first thing
4260 TYPE_FIELDS (t) = chainon (TYPE_VFIELD (t), TYPE_FIELDS (t));
4261 /* If there were any baseclasses, they can't possibly be at
4262 offset zero any more, because that's where the vtable
4263 pointer is. So, converting to a base class is going to
4265 if (CLASSTYPE_N_BASECLASSES (t))
4266 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4269 /* We can't yet add this new field to the list of all virtual
4270 function table pointers in this class. The
4271 modify_all_vtables function depends on this not being done.
4272 So, it is done later, in finish_struct_1. */
4276 /* Fixup the inline function given by INFO now that the class is
4280 fixup_pending_inline (info)
4281 struct pending_inline *info;
4286 tree fn = info->fndecl;
4288 args = DECL_ARGUMENTS (fn);
4291 DECL_CONTEXT (args) = fn;
4292 args = TREE_CHAIN (args);
4297 /* Fixup the inline methods and friends in TYPE now that TYPE is
4301 fixup_inline_methods (type)
4304 tree method = TYPE_METHODS (type);
4306 if (method && TREE_CODE (method) == TREE_VEC)
4308 if (TREE_VEC_ELT (method, 1))
4309 method = TREE_VEC_ELT (method, 1);
4310 else if (TREE_VEC_ELT (method, 0))
4311 method = TREE_VEC_ELT (method, 0);
4313 method = TREE_VEC_ELT (method, 2);
4316 /* Do inline member functions. */
4317 for (; method; method = TREE_CHAIN (method))
4318 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
4321 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4323 method = TREE_CHAIN (method))
4324 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
4325 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4328 /* Called from propagate_binfo_offsets via dfs_walk. */
4331 dfs_propagate_binfo_offsets (binfo, data)
4335 tree offset = (tree) data;
4337 /* Update the BINFO_OFFSET for this base. */
4338 BINFO_OFFSET (binfo)
4339 = size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), offset);
4341 SET_BINFO_MARKED (binfo);
4346 /* Add OFFSET to all base types of BINFO which is a base in the
4347 hierarchy dominated by T.
4349 OFFSET, which is a type offset, is number of bytes.
4351 Note that we don't have to worry about having two paths to the
4352 same base type, since this type owns its association list. */
4355 propagate_binfo_offsets (binfo, offset)
4360 dfs_propagate_binfo_offsets,
4361 dfs_skip_nonprimary_vbases_unmarkedp,
4365 dfs_skip_nonprimary_vbases_markedp,
4369 /* Remove *FIELD (which corresponds to the base given by BINFO) from
4370 the field list for T. */
4373 remove_base_field (t, binfo, field)
4378 tree basetype = BINFO_TYPE (binfo);
4381 my_friendly_assert (TREE_TYPE (*field) == basetype, 23897);
4383 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4384 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4388 = size_int (CEIL (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (*field)),
4390 propagate_binfo_offsets (binfo, offset);
4392 /* Remove this field. */
4393 *field = TREE_CHAIN (*field);
4396 /* Remove the FIELD_DECLs created for T's base classes in
4397 build_base_fields. Simultaneously, update BINFO_OFFSET for all the
4398 bases, except for non-primary virtual baseclasses. */
4401 remove_base_fields (t)
4407 /* Now propagate offset information throughout the lattice.
4408 Simultaneously, remove the temporary FIELD_DECLS we created in
4409 build_base_fields to refer to base types. */
4410 field = &TYPE_FIELDS (t);
4411 if (TYPE_VFIELD (t) == *field)
4413 /* If this class did not have a primary base, we create a
4414 virtual function table pointer. It will be the first thing
4415 in the class, under the new ABI. Skip it; the base fields
4417 my_friendly_assert (flag_new_abi
4418 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t),
4420 field = &TREE_CHAIN (*field);
4423 /* Under the new ABI, the primary base is always allocated first. */
4424 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4425 remove_base_field (t, CLASSTYPE_PRIMARY_BINFO (t), field);
4427 /* Now remove the rest of the bases. */
4428 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
4432 /* Under the new ABI, we've already removed the primary base
4434 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (t))
4437 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4439 /* We treat a primary virtual base class just like an ordinary base
4440 class. But, non-primary virtual bases are laid out later. */
4441 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4444 remove_base_field (t, binfo, field);
4448 /* Called via dfs_walk from layout_virtual bases. */
4451 dfs_set_offset_for_shared_vbases (binfo, data)
4455 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
4457 /* Update the shared copy. */
4460 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4461 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
4467 /* Called via dfs_walk from layout_virtual bases. */
4470 dfs_set_offset_for_unshared_vbases (binfo, data)
4474 /* If this is a virtual base, make sure it has the same offset as
4475 the shared copy. If it's a primary base, then we know it's
4477 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4479 tree t = (tree) data;
4483 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
4484 offset = ssize_binop (MINUS_EXPR,
4485 BINFO_OFFSET (vbase),
4486 BINFO_OFFSET (binfo));
4487 propagate_binfo_offsets (binfo, offset);
4493 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4494 TYPE_ALIGN and TYPE_SIZE for T. */
4497 layout_virtual_bases (t)
4503 /* DSIZE is the size of the class without the virtual bases. */
4504 dsize = TREE_INT_CST_LOW (TYPE_SIZE (t));
4505 /* Make every class have alignment of at least one. */
4506 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4508 /* Go through the virtual bases, allocating space for each virtual
4509 base that is not already a primary base class. */
4510 for (vbase = CLASSTYPE_VBASECLASSES (t);
4512 vbase = TREE_CHAIN (vbase))
4513 if (!BINFO_VBASE_PRIMARY_P (vbase))
4515 /* This virtual base is not a primary base of any class in the
4516 hierarchy, so we have to add space for it. */
4518 unsigned int desired_align;
4520 basetype = BINFO_TYPE (vbase);
4521 desired_align = TYPE_ALIGN (basetype);
4522 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4524 /* Add padding so that we can put the virtual base class at an
4525 appropriately aligned offset. */
4526 dsize = CEIL (dsize, desired_align) * desired_align;
4527 /* And compute the offset of the virtual base. */
4528 propagate_binfo_offsets (vbase,
4529 size_int (CEIL (dsize, BITS_PER_UNIT)));
4530 /* Every virtual baseclass takes a least a UNIT, so that we can
4531 take it's address and get something different for each base. */
4532 dsize += MAX (BITS_PER_UNIT,
4533 TREE_INT_CST_LOW (CLASSTYPE_SIZE (basetype)));
4536 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4537 BINFO_OFFSET set correctly. Those we just allocated certainly
4538 will. The others are primary baseclasses; we walk the hierarchy
4539 to find the primary copies and update the shared copy. */
4540 dfs_walk (TYPE_BINFO (t),
4541 dfs_set_offset_for_shared_vbases,
4542 dfs_unmarked_real_bases_queue_p,
4545 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4546 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4547 bases and their direct and indirect bases. The ambiguity checks
4548 in get_base_distance depend on the BINFO_OFFSETs being set
4550 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4552 /* Now, make sure that the total size of the type is a multiple of
4554 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4555 TYPE_SIZE (t) = size_int (dsize);
4556 TYPE_SIZE_UNIT (t) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (t),
4557 size_int (BITS_PER_UNIT));
4560 /* Finish the work of layout_record, now taking virtual bases into account.
4561 Also compute the actual offsets that our base classes will have.
4562 This must be performed after the fields are laid out, since virtual
4563 baseclasses must lay down at the end of the record. */
4566 layout_basetypes (rec)
4571 #ifdef STRUCTURE_SIZE_BOUNDARY
4572 /* Packed structures don't need to have minimum size. */
4573 if (! TYPE_PACKED (rec))
4574 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), STRUCTURE_SIZE_BOUNDARY);
4577 /* Remove the FIELD_DECLs we created for baseclasses in
4578 build_base_fields. Simultaneously, update the BINFO_OFFSETs for
4579 everything in the hierarcy except non-primary virtual bases. */
4580 remove_base_fields (rec);
4582 /* Allocate the virtual base classes. */
4583 layout_virtual_bases (rec);
4585 /* Get all the virtual base types that this type uses. The
4586 TREE_VALUE slot holds the virtual baseclass type. Note that
4587 get_vbase_types makes copies of the virtual base BINFOs, so that
4588 the vbase_types are unshared. */
4589 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
4590 vbase_types = TREE_CHAIN (vbase_types))
4593 tree basetype = BINFO_TYPE (vbase_types);
4594 if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
4595 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4600 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4601 BINFO_OFFSETs for all of the base-classes. Position the vtable
4605 layout_class_type (t, empty_p, has_virtual_p,
4606 new_virtuals_p, overridden_virtuals_p)
4610 tree *new_virtuals_p;
4611 tree *overridden_virtuals_p;
4613 /* If possible, we reuse the virtual function table pointer from one
4614 of our base classes. */
4615 determine_primary_base (t, has_virtual_p);
4617 /* Add pointers to all of our virtual base-classes. */
4618 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (t, empty_p),
4620 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4621 TYPE_FIELDS (t) = chainon (build_base_fields (t, empty_p),
4624 /* Create a pointer to our virtual function table. */
4625 create_vtable_ptr (t, empty_p, has_virtual_p,
4626 new_virtuals_p, overridden_virtuals_p);
4628 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4629 we have to save this before we start modifying
4630 TYPE_NONCOPIED_PARTS. */
4631 fixup_inline_methods (t);
4633 /* We make all structures have at least one element, so that they
4634 have non-zero size. The field that we add here is fake, in the
4635 sense that, for example, we don't want people to be able to
4636 initialize it later. So, we add it just long enough to let the
4637 back-end lay out the type, and then remove it. */
4640 tree decl = build_lang_decl
4641 (FIELD_DECL, NULL_TREE, char_type_node);
4642 TREE_CHAIN (decl) = TYPE_FIELDS (t);
4643 TYPE_FIELDS (t) = decl;
4644 TYPE_NONCOPIED_PARTS (t)
4645 = tree_cons (NULL_TREE, decl, TYPE_NONCOPIED_PARTS (t));
4646 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4649 /* Let the back-end lay out the type. Note that at this point we
4650 have only included non-virtual base-classes; we will lay out the
4651 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4652 this call are not necessarily correct; they are just the size and
4653 alignment when no virtual base clases are used. */
4656 /* If we added an extra field to make this class non-empty, remove
4659 TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
4661 /* Delete all zero-width bit-fields from the list of fields. Now
4662 that the type is laid out they are no longer important. */
4663 remove_zero_width_bit_fields (t);
4665 /* Remember the size and alignment of the class before adding
4666 the virtual bases. */
4667 if (*empty_p && flag_new_abi)
4668 CLASSTYPE_SIZE (t) = integer_zero_node;
4669 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
4670 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
4671 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4673 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4674 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4676 /* Set the TYPE_DECL for this type to contain the right
4677 value for DECL_OFFSET, so that we can use it as part
4678 of a COMPONENT_REF for multiple inheritance. */
4679 layout_decl (TYPE_MAIN_DECL (t), 0);
4681 /* Now fix up any virtual base class types that we left lying
4682 around. We must get these done before we try to lay out the
4683 virtual function table. */
4684 if (CLASSTYPE_N_BASECLASSES (t))
4685 /* layout_basetypes will remove the base subobject fields. */
4686 layout_basetypes (t);
4689 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
4690 (or C++ class declaration).
4692 For C++, we must handle the building of derived classes.
4693 Also, C++ allows static class members. The way that this is
4694 handled is to keep the field name where it is (as the DECL_NAME
4695 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
4696 of the field. layout_record and layout_union will know about this.
4698 More C++ hair: inline functions have text in their
4699 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
4700 meaningful tree structure. After the struct has been laid out, set
4701 things up so that this can happen.
4703 And still more: virtual functions. In the case of single inheritance,
4704 when a new virtual function is seen which redefines a virtual function
4705 from the base class, the new virtual function is placed into
4706 the virtual function table at exactly the same address that
4707 it had in the base class. When this is extended to multiple
4708 inheritance, the same thing happens, except that multiple virtual
4709 function tables must be maintained. The first virtual function
4710 table is treated in exactly the same way as in the case of single
4711 inheritance. Additional virtual function tables have different
4712 DELTAs, which tell how to adjust `this' to point to the right thing.
4714 ATTRIBUTES is the set of decl attributes to be applied, if any. */
4722 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
4723 a FUNCTION_DECL. Each of these functions is a virtual function
4724 declared in T that does not override any virtual function from a
4726 tree new_virtuals = NULL_TREE;
4727 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
4728 except that each declaration here overrides the declaration from
4730 tree overridden_virtuals = NULL_TREE;
4737 if (IS_AGGR_TYPE (t))
4738 cp_error ("redefinition of `%#T'", t);
4740 my_friendly_abort (172);
4745 GNU_xref_decl (current_function_decl, t);
4747 /* If this type was previously laid out as a forward reference,
4748 make sure we lay it out again. */
4749 TYPE_SIZE (t) = NULL_TREE;
4750 CLASSTYPE_GOT_SEMICOLON (t) = 0;
4751 CLASSTYPE_VFIELD_PARENT (t) = -1;
4753 CLASSTYPE_RTTI (t) = NULL_TREE;
4755 /* Do end-of-class semantic processing: checking the validity of the
4756 bases and members and add implicitly generated methods. */
4757 check_bases_and_members (t, &empty);
4759 /* Layout the class itself. */
4760 layout_class_type (t, &empty, &has_virtual,
4761 &new_virtuals, &overridden_virtuals);
4763 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4767 vbases = CLASSTYPE_VBASECLASSES (t);
4770 /* Now fixup overrides of all functions in vtables from all
4771 direct or indirect virtual base classes. */
4772 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
4773 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
4775 for (i = 0; i < n_baseclasses; i++)
4777 tree base_binfo = TREE_VEC_ELT (binfos, i);
4778 tree basetype = BINFO_TYPE (base_binfo);
4781 vbases = CLASSTYPE_VBASECLASSES (basetype);
4785 tree basetype_vbase;
4788 = find_vbase_instance (BINFO_TYPE (vbases), t);
4790 = find_vbase_instance (BINFO_TYPE (vbases), basetype);
4792 merge_overrides (vbase, basetype_vbase, 1, t);
4793 vbases = TREE_CHAIN (vbases);
4799 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4800 might need to know it for setting up the offsets in the vtable
4801 (or in thunks) below. */
4802 vfield = TYPE_VFIELD (t);
4803 if (vfield != NULL_TREE
4804 && DECL_FIELD_CONTEXT (vfield) != t)
4806 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4807 tree offset = BINFO_OFFSET (binfo);
4809 vfield = copy_node (vfield);
4810 copy_lang_decl (vfield);
4812 if (! integer_zerop (offset))
4813 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4814 DECL_FIELD_CONTEXT (vfield) = t;
4815 DECL_CLASS_CONTEXT (vfield) = t;
4816 DECL_FIELD_BITPOS (vfield)
4817 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4818 TYPE_VFIELD (t) = vfield;
4821 if (flag_rtti && TYPE_CONTAINS_VPTR_P (t) && !overridden_virtuals)
4822 modify_all_vtables (t, NULL_TREE);
4824 for (overridden_virtuals = nreverse (overridden_virtuals);
4825 overridden_virtuals;
4826 overridden_virtuals = TREE_CHAIN (overridden_virtuals))
4827 modify_all_vtables (t, TREE_VALUE (overridden_virtuals));
4829 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4832 /* Now fixup any virtual function entries from virtual bases
4833 that have different deltas. This has to come after we do the
4834 pending hard virtuals, as we might have a function that comes
4835 from multiple virtual base instances that is only overridden
4836 by a hard virtual above. */
4837 vbases = CLASSTYPE_VBASECLASSES (t);
4842 /* We might be able to shorten the amount of work we do by
4843 only doing this for vtables that come from virtual bases
4844 that have differing offsets, but don't want to miss any
4846 vbase = find_vbase_instance (BINFO_TYPE (vbases), t);
4847 dfs_walk (vbase, dfs_fixup_vtable_deltas, dfs_skip_vbases, t);
4848 vbases = TREE_CHAIN (vbases);
4852 /* If necessary, create the vtable for this class. */
4854 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
4856 new_virtuals = nreverse (new_virtuals);
4857 /* We must enter these virtuals into the table. */
4858 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4860 if (! CLASSTYPE_COM_INTERFACE (t))
4862 /* The second slot is for the tdesc pointer when thunks are used. */
4863 if (flag_vtable_thunks)
4864 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
4866 /* The first slot is for the rtti offset. */
4867 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
4869 set_rtti_entry (new_virtuals,
4870 convert (ssizetype, integer_zero_node), t);
4872 build_vtable (NULL_TREE, t);
4874 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4875 /* Here we know enough to change the type of our virtual
4876 function table, but we will wait until later this function. */
4877 build_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
4879 /* If this type has basetypes with constructors, then those
4880 constructors might clobber the virtual function table. But
4881 they don't if the derived class shares the exact vtable of the base
4884 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4886 /* If we didn't need a new vtable, see if we should copy one from
4888 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4890 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
4892 /* This class contributes nothing new to the virtual function
4893 table. However, it may have declared functions which
4894 went into the virtual function table "inherited" from the
4895 base class. If so, we grab a copy of those updated functions,
4896 and pretend they are ours. */
4898 /* See if we should steal the virtual info from base class. */
4899 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4900 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4901 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4902 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4903 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4904 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4907 if (TYPE_CONTAINS_VPTR_P (t))
4909 if (TYPE_BINFO_VTABLE (t))
4910 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
4912 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4913 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
4916 CLASSTYPE_VSIZE (t) = has_virtual;
4917 /* Entries for virtual functions defined in the primary base are
4918 followed by entries for new functions unique to this class. */
4919 TYPE_BINFO_VIRTUALS (t)
4920 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
4923 /* Now lay out the virtual function table. */
4926 /* Use size_int so values are memoized in common cases. */
4930 itype = size_int (has_virtual);
4931 itype = size_binop (PLUS_EXPR,
4933 num_extra_vtbl_entries (TYPE_BINFO (t)));
4934 atype = build_cplus_array_type (vtable_entry_type,
4935 build_index_type (itype));
4936 layout_type (atype);
4938 /* We may have to grow the vtable. */
4939 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4941 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4942 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4943 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4944 /* At one time the vtable info was grabbed 2 words at a time. This
4945 fails on sparc unless you have 8-byte alignment. (tiemann) */
4946 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4947 = MAX (TYPE_ALIGN (double_type_node),
4948 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4952 /* If we created a new vtbl pointer for this class, add it to the
4954 if (TYPE_VFIELD (t) && CLASSTYPE_VFIELD_PARENT (t) == -1)
4955 CLASSTYPE_VFIELDS (t)
4956 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
4958 finish_struct_bits (t);
4960 /* Complete the rtl for any static member objects of the type we're
4962 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
4964 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4965 && TREE_TYPE (x) == t)
4967 DECL_MODE (x) = TYPE_MODE (t);
4968 make_decl_rtl (x, NULL, 0);
4972 /* Done with FIELDS...now decide whether to sort these for
4973 faster lookups later.
4975 The C front-end only does this when n_fields > 15. We use
4976 a smaller number because most searches fail (succeeding
4977 ultimately as the search bores through the inheritance
4978 hierarchy), and we want this failure to occur quickly. */
4980 n_fields = count_fields (TYPE_FIELDS (t));
4983 tree field_vec = make_tree_vec (n_fields);
4984 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
4985 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
4986 (int (*)(const void *, const void *))field_decl_cmp);
4987 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
4988 retrofit_lang_decl (TYPE_MAIN_DECL (t));
4989 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
4992 if (TYPE_HAS_CONSTRUCTOR (t))
4994 tree vfields = CLASSTYPE_VFIELDS (t);
4998 /* Mark the fact that constructor for T
4999 could affect anybody inheriting from T
5000 who wants to initialize vtables for VFIELDS's type. */
5001 if (VF_DERIVED_VALUE (vfields))
5002 TREE_ADDRESSABLE (vfields) = 1;
5003 vfields = TREE_CHAIN (vfields);
5007 if (CLASSTYPE_VSIZE (t) != 0)
5009 /* In addition to this one, all the other vfields should be listed. */
5010 /* Before that can be done, we have to have FIELD_DECLs for them, and
5011 a place to find them. */
5012 TYPE_NONCOPIED_PARTS (t)
5013 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5014 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5016 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5017 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5018 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5022 /* Make the rtl for any new vtables we have created, and unmark
5023 the base types we marked. */
5025 hack_incomplete_structures (t);
5027 if (warn_overloaded_virtual)
5030 maybe_suppress_debug_info (t);
5032 /* Finish debugging output for this type. */
5033 rest_of_type_compilation (t, toplevel_bindings_p ());
5036 /* When T was built up, the member declarations were added in reverse
5037 order. Rearrange them to declaration order. */
5040 unreverse_member_declarations (t)
5047 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5048 reverse order. Put them in declaration order now. */
5049 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5050 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5052 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5053 reverse order, so we can't just use nreverse. */
5055 for (x = TYPE_FIELDS (t);
5056 x && TREE_CODE (x) != TYPE_DECL;
5059 next = TREE_CHAIN (x);
5060 TREE_CHAIN (x) = prev;
5065 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5067 TYPE_FIELDS (t) = prev;
5072 finish_struct (t, attributes)
5075 /* Now that we've got all the field declarations, reverse everything
5077 unreverse_member_declarations (t);
5079 cplus_decl_attributes (t, attributes, NULL_TREE);
5081 if (processing_template_decl)
5083 finish_struct_methods (t);
5084 TYPE_SIZE (t) = integer_zero_node;
5087 finish_struct_1 (t);
5089 TYPE_BEING_DEFINED (t) = 0;
5091 if (current_class_type)
5094 error ("trying to finish struct, but kicked out due to previous parse errors.");
5096 if (processing_template_decl)
5098 tree scope = current_scope ();
5099 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5100 add_tree (build_min (TAG_DEFN, t));
5106 /* Return the dynamic type of INSTANCE, if known.
5107 Used to determine whether the virtual function table is needed
5110 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5111 of our knowledge of its type. */
5114 fixed_type_or_null (instance, nonnull)
5121 switch (TREE_CODE (instance))
5124 /* Check that we are not going through a cast of some sort. */
5125 if (TREE_TYPE (instance)
5126 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5127 instance = TREE_OPERAND (instance, 0);
5128 /* fall through... */
5130 /* This is a call to a constructor, hence it's never zero. */
5131 if (TREE_HAS_CONSTRUCTOR (instance))
5135 return TREE_TYPE (instance);
5140 /* This is a call to a constructor, hence it's never zero. */
5141 if (TREE_HAS_CONSTRUCTOR (instance))
5145 return TREE_TYPE (instance);
5147 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5154 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5155 /* Propagate nonnull. */
5156 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5157 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5158 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5163 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5168 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5171 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5175 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5176 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5180 return TREE_TYPE (TREE_TYPE (instance));
5182 /* fall through... */
5185 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5189 return TREE_TYPE (instance);
5193 if (instance == current_class_ptr
5194 && flag_this_is_variable <= 0)
5196 /* Normally, 'this' must be non-null. */
5197 if (flag_this_is_variable == 0)
5200 /* <0 means we're in a constructor and we know our type. */
5201 if (flag_this_is_variable < 0)
5202 return TREE_TYPE (TREE_TYPE (instance));
5204 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5205 /* Reference variables should be references to objects. */
5215 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5216 to the static type. We also handle the case where INSTANCE is really
5219 Used to determine whether the virtual function table is needed
5222 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5223 of our knowledge of its type. */
5226 resolves_to_fixed_type_p (instance, nonnull)
5230 tree t = TREE_TYPE (instance);
5231 tree fixed = fixed_type_or_null (instance, nonnull);
5232 if (fixed == NULL_TREE)
5234 if (POINTER_TYPE_P (t))
5236 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
5241 init_class_processing ()
5243 current_class_depth = 0;
5244 current_class_stack_size = 10;
5246 = (class_stack_node_t) xmalloc (current_class_stack_size
5247 * sizeof (struct class_stack_node));
5249 access_default_node = build_int_2 (0, 0);
5250 access_public_node = build_int_2 (1, 0);
5251 access_protected_node = build_int_2 (2, 0);
5252 access_private_node = build_int_2 (3, 0);
5253 access_default_virtual_node = build_int_2 (4, 0);
5254 access_public_virtual_node = build_int_2 (5, 0);
5255 access_protected_virtual_node = build_int_2 (6, 0);
5256 access_private_virtual_node = build_int_2 (7, 0);
5259 /* Set current scope to NAME. CODE tells us if this is a
5260 STRUCT, UNION, or ENUM environment.
5262 NAME may end up being NULL_TREE if this is an anonymous or
5263 late-bound struct (as in "struct { ... } foo;") */
5265 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5266 appropriate values, found by looking up the type definition of
5269 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5270 which can be seen locally to the class. They are shadowed by
5271 any subsequent local declaration (including parameter names).
5273 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5274 which have static meaning (i.e., static members, static
5275 member functions, enum declarations, etc).
5277 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5278 which can be seen locally to the class (as in 1), but
5279 know that we are doing this for declaration purposes
5280 (i.e. friend foo::bar (int)).
5282 So that we may avoid calls to lookup_name, we cache the _TYPE
5283 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5285 For multiple inheritance, we perform a two-pass depth-first search
5286 of the type lattice. The first pass performs a pre-order search,
5287 marking types after the type has had its fields installed in
5288 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5289 unmarks the marked types. If a field or member function name
5290 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5291 that name becomes `error_mark_node'. */
5294 pushclass (type, modify)
5298 type = TYPE_MAIN_VARIANT (type);
5300 /* Make sure there is enough room for the new entry on the stack. */
5301 if (current_class_depth + 1 >= current_class_stack_size)
5303 current_class_stack_size *= 2;
5305 = (class_stack_node_t) xrealloc (current_class_stack,
5306 current_class_stack_size
5307 * sizeof (struct class_stack_node));
5310 /* Insert a new entry on the class stack. */
5311 current_class_stack[current_class_depth].name = current_class_name;
5312 current_class_stack[current_class_depth].type = current_class_type;
5313 current_class_stack[current_class_depth].access = current_access_specifier;
5314 current_class_stack[current_class_depth].names_used = 0;
5315 current_class_depth++;
5317 /* Now set up the new type. */
5318 current_class_name = TYPE_NAME (type);
5319 if (TREE_CODE (current_class_name) == TYPE_DECL)
5320 current_class_name = DECL_NAME (current_class_name);
5321 current_class_type = type;
5323 /* By default, things in classes are private, while things in
5324 structures or unions are public. */
5325 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5326 ? access_private_node
5327 : access_public_node);
5329 if (previous_class_type != NULL_TREE
5330 && (type != previous_class_type
5331 || TYPE_SIZE (previous_class_type) == NULL_TREE)
5332 && current_class_depth == 1)
5334 /* Forcibly remove any old class remnants. */
5335 invalidate_class_lookup_cache ();
5338 /* If we're about to enter a nested class, clear
5339 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5340 if (modify && current_class_depth > 1)
5341 clear_identifier_class_values ();
5346 if (CLASSTYPE_TEMPLATE_INFO (type))
5347 overload_template_name (type);
5352 if (type != previous_class_type || current_class_depth > 1)
5353 push_class_decls (type);
5358 /* We are re-entering the same class we just left, so we
5359 don't have to search the whole inheritance matrix to find
5360 all the decls to bind again. Instead, we install the
5361 cached class_shadowed list, and walk through it binding
5362 names and setting up IDENTIFIER_TYPE_VALUEs. */
5363 set_class_shadows (previous_class_values);
5364 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5366 tree id = TREE_PURPOSE (item);
5367 tree decl = TREE_TYPE (item);
5369 push_class_binding (id, decl);
5370 if (TREE_CODE (decl) == TYPE_DECL)
5371 set_identifier_type_value (id, TREE_TYPE (decl));
5373 unuse_fields (type);
5376 storetags (CLASSTYPE_TAGS (type));
5380 /* When we exit a toplevel class scope, we save the
5381 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5382 reenter the class. Here, we've entered some other class, so we
5383 must invalidate our cache. */
5386 invalidate_class_lookup_cache ()
5390 /* This code can be seen as a cache miss. When we've cached a
5391 class' scope's bindings and we can't use them, we need to reset
5392 them. This is it! */
5393 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5394 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5396 previous_class_type = NULL_TREE;
5399 /* Get out of the current class scope. If we were in a class scope
5400 previously, that is the one popped to. */
5406 /* Since poplevel_class does the popping of class decls nowadays,
5407 this really only frees the obstack used for these decls. */
5410 current_class_depth--;
5411 current_class_name = current_class_stack[current_class_depth].name;
5412 current_class_type = current_class_stack[current_class_depth].type;
5413 current_access_specifier = current_class_stack[current_class_depth].access;
5414 if (current_class_stack[current_class_depth].names_used)
5415 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5418 /* Returns 1 if current_class_type is either T or a nested type of T. */
5421 currently_open_class (t)
5425 if (t == current_class_type)
5427 for (i = 0; i < current_class_depth; ++i)
5428 if (current_class_stack [i].type == t)
5433 /* When entering a class scope, all enclosing class scopes' names with
5434 static meaning (static variables, static functions, types and enumerators)
5435 have to be visible. This recursive function calls pushclass for all
5436 enclosing class contexts until global or a local scope is reached.
5437 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5438 formal of the same name. */
5441 push_nested_class (type, modify)
5447 /* A namespace might be passed in error cases, like A::B:C. */
5448 if (type == NULL_TREE
5449 || type == error_mark_node
5450 || TREE_CODE (type) == NAMESPACE_DECL
5451 || ! IS_AGGR_TYPE (type)
5452 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5453 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
5456 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5458 if (context && CLASS_TYPE_P (context))
5459 push_nested_class (context, 2);
5460 pushclass (type, modify);
5463 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5468 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5471 if (context && CLASS_TYPE_P (context))
5472 pop_nested_class ();
5475 /* Set global variables CURRENT_LANG_NAME to appropriate value
5476 so that behavior of name-mangling machinery is correct. */
5479 push_lang_context (name)
5482 *current_lang_stack++ = current_lang_name;
5483 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5484 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5486 size_t old_size = VARRAY_SIZE (current_lang_base);
5488 VARRAY_GROW (current_lang_base, old_size + 10);
5489 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5492 if (name == lang_name_cplusplus)
5494 strict_prototype = strict_prototypes_lang_cplusplus;
5495 current_lang_name = name;
5497 else if (name == lang_name_java)
5499 strict_prototype = strict_prototypes_lang_cplusplus;
5500 current_lang_name = name;
5501 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5502 (See record_builtin_java_type in decl.c.) However, that causes
5503 incorrect debug entries if these types are actually used.
5504 So we re-enable debug output after extern "Java". */
5505 DECL_IGNORED_P (java_byte_type_node) = 0;
5506 DECL_IGNORED_P (java_short_type_node) = 0;
5507 DECL_IGNORED_P (java_int_type_node) = 0;
5508 DECL_IGNORED_P (java_long_type_node) = 0;
5509 DECL_IGNORED_P (java_float_type_node) = 0;
5510 DECL_IGNORED_P (java_double_type_node) = 0;
5511 DECL_IGNORED_P (java_char_type_node) = 0;
5512 DECL_IGNORED_P (java_boolean_type_node) = 0;
5514 else if (name == lang_name_c)
5516 strict_prototype = strict_prototypes_lang_c;
5517 current_lang_name = name;
5520 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5523 /* Get out of the current language scope. */
5528 /* Clear the current entry so that garbage collector won't hold on
5530 *current_lang_stack = NULL_TREE;
5531 current_lang_name = *--current_lang_stack;
5532 if (current_lang_name == lang_name_cplusplus
5533 || current_lang_name == lang_name_java)
5534 strict_prototype = strict_prototypes_lang_cplusplus;
5535 else if (current_lang_name == lang_name_c)
5536 strict_prototype = strict_prototypes_lang_c;
5539 /* Type instantiation routines. */
5541 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5542 matches the TARGET_TYPE. If there is no satisfactory match, return
5543 error_mark_node, and issue an error message if COMPLAIN is
5544 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5545 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5546 template arguments. */
5549 resolve_address_of_overloaded_function (target_type,
5558 tree explicit_targs;
5560 /* Here's what the standard says:
5564 If the name is a function template, template argument deduction
5565 is done, and if the argument deduction succeeds, the deduced
5566 arguments are used to generate a single template function, which
5567 is added to the set of overloaded functions considered.
5569 Non-member functions and static member functions match targets of
5570 type "pointer-to-function" or "reference-to-function." Nonstatic
5571 member functions match targets of type "pointer-to-member
5572 function;" the function type of the pointer to member is used to
5573 select the member function from the set of overloaded member
5574 functions. If a nonstatic member function is selected, the
5575 reference to the overloaded function name is required to have the
5576 form of a pointer to member as described in 5.3.1.
5578 If more than one function is selected, any template functions in
5579 the set are eliminated if the set also contains a non-template
5580 function, and any given template function is eliminated if the
5581 set contains a second template function that is more specialized
5582 than the first according to the partial ordering rules 14.5.5.2.
5583 After such eliminations, if any, there shall remain exactly one
5584 selected function. */
5587 int is_reference = 0;
5588 /* We store the matches in a TREE_LIST rooted here. The functions
5589 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5590 interoperability with most_specialized_instantiation. */
5591 tree matches = NULL_TREE;
5594 /* By the time we get here, we should be seeing only real
5595 pointer-to-member types, not the internal POINTER_TYPE to
5596 METHOD_TYPE representation. */
5597 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5598 && (TREE_CODE (TREE_TYPE (target_type))
5599 == METHOD_TYPE)), 0);
5601 /* Check that the TARGET_TYPE is reasonable. */
5602 if (TYPE_PTRFN_P (target_type))
5605 else if (TYPE_PTRMEMFUNC_P (target_type))
5606 /* This is OK, too. */
5608 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5610 /* This is OK, too. This comes from a conversion to reference
5612 target_type = build_reference_type (target_type);
5618 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5619 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5620 return error_mark_node;
5623 /* If we can find a non-template function that matches, we can just
5624 use it. There's no point in generating template instantiations
5625 if we're just going to throw them out anyhow. But, of course, we
5626 can only do this when we don't *need* a template function. */
5631 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5633 tree fn = OVL_FUNCTION (fns);
5636 if (TREE_CODE (fn) == TEMPLATE_DECL)
5637 /* We're not looking for templates just yet. */
5640 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5642 /* We're looking for a non-static member, and this isn't
5643 one, or vice versa. */
5646 /* See if there's a match. */
5647 fntype = TREE_TYPE (fn);
5649 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5650 else if (!is_reference)
5651 fntype = build_pointer_type (fntype);
5653 if (can_convert_arg (target_type, fntype, fn))
5654 matches = tree_cons (fn, NULL_TREE, matches);
5658 /* Now, if we've already got a match (or matches), there's no need
5659 to proceed to the template functions. But, if we don't have a
5660 match we need to look at them, too. */
5663 tree target_fn_type;
5664 tree target_arg_types;
5669 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5671 target_fn_type = TREE_TYPE (target_type);
5672 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5674 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5676 tree fn = OVL_FUNCTION (fns);
5678 tree instantiation_type;
5681 if (TREE_CODE (fn) != TEMPLATE_DECL)
5682 /* We're only looking for templates. */
5685 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5687 /* We're not looking for a non-static member, and this is
5688 one, or vice versa. */
5691 /* Try to do argument deduction. */
5692 targs = make_tree_vec (DECL_NTPARMS (fn));
5693 if (fn_type_unification (fn, explicit_targs, targs,
5694 target_arg_types, NULL_TREE,
5696 /* Argument deduction failed. */
5699 /* Instantiate the template. */
5700 instantiation = instantiate_template (fn, targs);
5701 if (instantiation == error_mark_node)
5702 /* Instantiation failed. */
5705 /* See if there's a match. */
5706 instantiation_type = TREE_TYPE (instantiation);
5708 instantiation_type =
5709 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5710 else if (!is_reference)
5711 instantiation_type = build_pointer_type (instantiation_type);
5712 if (can_convert_arg (target_type, instantiation_type, instantiation))
5713 matches = tree_cons (instantiation, fn, matches);
5716 /* Now, remove all but the most specialized of the matches. */
5719 tree match = most_specialized_instantiation (matches,
5722 if (match != error_mark_node)
5723 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5727 /* Now we should have exactly one function in MATCHES. */
5728 if (matches == NULL_TREE)
5730 /* There were *no* matches. */
5733 cp_error ("no matches converting function `%D' to type `%#T'",
5734 DECL_NAME (OVL_FUNCTION (overload)),
5737 /* print_candidates expects a chain with the functions in
5738 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5739 so why be clever?). */
5740 for (; overload; overload = OVL_NEXT (overload))
5741 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5744 print_candidates (matches);
5746 return error_mark_node;
5748 else if (TREE_CHAIN (matches))
5750 /* There were too many matches. */
5756 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5757 DECL_NAME (OVL_FUNCTION (overload)),
5760 /* Since print_candidates expects the functions in the
5761 TREE_VALUE slot, we flip them here. */
5762 for (match = matches; match; match = TREE_CHAIN (match))
5763 TREE_VALUE (match) = TREE_PURPOSE (match);
5765 print_candidates (matches);
5768 return error_mark_node;
5771 /* Good, exactly one match. Now, convert it to the correct type. */
5772 fn = TREE_PURPOSE (matches);
5776 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5777 return build_unary_op (ADDR_EXPR, fn, 0);
5780 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5781 will mark the function as addressed, but here we must do it
5783 mark_addressable (fn);
5789 /* This function will instantiate the type of the expression given in
5790 RHS to match the type of LHSTYPE. If errors exist, then return
5791 error_mark_node. We only complain is COMPLAIN is set. If we are
5792 not complaining, never modify rhs, as overload resolution wants to
5793 try many possible instantiations, in hopes that at least one will
5796 FLAGS is a bitmask, as we see at the top of the function.
5798 For non-recursive calls, LHSTYPE should be a function, pointer to
5799 function, or a pointer to member function. */
5802 instantiate_type (lhstype, rhs, flags)
5806 int complain = (flags & 1);
5807 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
5809 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5812 error ("not enough type information");
5813 return error_mark_node;
5816 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5818 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
5821 cp_error ("argument of type `%T' does not match `%T'",
5822 TREE_TYPE (rhs), lhstype);
5823 return error_mark_node;
5826 /* We don't overwrite rhs if it is an overloaded function.
5827 Copying it would destroy the tree link. */
5828 if (TREE_CODE (rhs) != OVERLOAD)
5829 rhs = copy_node (rhs);
5831 /* This should really only be used when attempting to distinguish
5832 what sort of a pointer to function we have. For now, any
5833 arithmetic operation which is not supported on pointers
5834 is rejected as an error. */
5836 switch (TREE_CODE (rhs))
5843 my_friendly_abort (177);
5844 return error_mark_node;
5851 new_rhs = instantiate_type (build_pointer_type (lhstype),
5852 TREE_OPERAND (rhs, 0), flags);
5853 if (new_rhs == error_mark_node)
5854 return error_mark_node;
5856 TREE_TYPE (rhs) = lhstype;
5857 TREE_OPERAND (rhs, 0) = new_rhs;
5862 rhs = copy_node (TREE_OPERAND (rhs, 0));
5863 TREE_TYPE (rhs) = unknown_type_node;
5864 return instantiate_type (lhstype, rhs, flags);
5868 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5870 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
5871 && complain && !flag_ms_extensions)
5873 /* Note: we check this after the recursive call to avoid
5874 complaining about cases where overload resolution fails. */
5876 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5877 tree fn = PTRMEM_CST_MEMBER (r);
5879 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
5882 ("object-dependent reference to `%E' can only be used in a call",
5885 (" to form a pointer to member function, say `&%T::%E'",
5893 rhs = TREE_OPERAND (rhs, 1);
5894 if (BASELINK_P (rhs))
5895 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5897 /* This can happen if we are forming a pointer-to-member for a
5899 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5903 case TEMPLATE_ID_EXPR:
5905 resolve_address_of_overloaded_function (lhstype,
5906 TREE_OPERAND (rhs, 0),
5908 /*template_only=*/1,
5909 TREE_OPERAND (rhs, 1));
5913 resolve_address_of_overloaded_function (lhstype,
5916 /*template_only=*/0,
5917 /*explicit_targs=*/NULL_TREE);
5920 /* Now we should have a baselink. */
5921 my_friendly_assert (BASELINK_P (rhs), 990412);
5923 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5926 /* This is too hard for now. */
5927 my_friendly_abort (183);
5928 return error_mark_node;
5933 TREE_OPERAND (rhs, 0)
5934 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5935 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5936 return error_mark_node;
5937 TREE_OPERAND (rhs, 1)
5938 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5939 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5940 return error_mark_node;
5942 TREE_TYPE (rhs) = lhstype;
5946 case TRUNC_DIV_EXPR:
5947 case FLOOR_DIV_EXPR:
5949 case ROUND_DIV_EXPR:
5951 case TRUNC_MOD_EXPR:
5952 case FLOOR_MOD_EXPR:
5954 case ROUND_MOD_EXPR:
5955 case FIX_ROUND_EXPR:
5956 case FIX_FLOOR_EXPR:
5958 case FIX_TRUNC_EXPR:
5974 case PREINCREMENT_EXPR:
5975 case PREDECREMENT_EXPR:
5976 case POSTINCREMENT_EXPR:
5977 case POSTDECREMENT_EXPR:
5979 error ("invalid operation on uninstantiated type");
5980 return error_mark_node;
5982 case TRUTH_AND_EXPR:
5984 case TRUTH_XOR_EXPR:
5991 case TRUTH_ANDIF_EXPR:
5992 case TRUTH_ORIF_EXPR:
5993 case TRUTH_NOT_EXPR:
5995 error ("not enough type information");
5996 return error_mark_node;
5999 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6002 error ("not enough type information");
6003 return error_mark_node;
6005 TREE_OPERAND (rhs, 1)
6006 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6007 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6008 return error_mark_node;
6009 TREE_OPERAND (rhs, 2)
6010 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6011 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6012 return error_mark_node;
6014 TREE_TYPE (rhs) = lhstype;
6018 TREE_OPERAND (rhs, 1)
6019 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6020 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6021 return error_mark_node;
6023 TREE_TYPE (rhs) = lhstype;
6027 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6029 case ENTRY_VALUE_EXPR:
6030 my_friendly_abort (184);
6031 return error_mark_node;
6034 return error_mark_node;
6037 my_friendly_abort (185);
6038 return error_mark_node;
6042 /* Return the name of the virtual function pointer field
6043 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6044 this may have to look back through base types to find the
6045 ultimate field name. (For single inheritance, these could
6046 all be the same name. Who knows for multiple inheritance). */
6049 get_vfield_name (type)
6052 tree binfo = TYPE_BINFO (type);
6055 while (BINFO_BASETYPES (binfo)
6056 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6057 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6058 binfo = BINFO_BASETYPE (binfo, 0);
6060 type = BINFO_TYPE (binfo);
6061 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6062 + TYPE_NAME_LENGTH (type) + 2);
6063 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6064 return get_identifier (buf);
6068 print_class_statistics ()
6070 #ifdef GATHER_STATISTICS
6071 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6072 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6073 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6074 n_build_method_call, n_inner_fields_searched);
6077 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6078 n_vtables, n_vtable_searches);
6079 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6080 n_vtable_entries, n_vtable_elems);
6085 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6086 according to [class]:
6087 The class-name is also inserted
6088 into the scope of the class itself. For purposes of access checking,
6089 the inserted class name is treated as if it were a public member name. */
6092 build_self_reference ()
6094 tree name = constructor_name (current_class_type);
6095 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6098 DECL_NONLOCAL (value) = 1;
6099 DECL_CONTEXT (value) = current_class_type;
6100 DECL_CLASS_CONTEXT (value) = current_class_type;
6101 DECL_ARTIFICIAL (value) = 1;
6103 if (processing_template_decl)
6104 value = push_template_decl (value);
6106 saved_cas = current_access_specifier;
6107 current_access_specifier = access_public_node;
6108 finish_member_declaration (value);
6109 current_access_specifier = saved_cas;
6112 /* Returns 1 if TYPE contains only padding bytes. */
6115 is_empty_class (type)
6120 if (type == error_mark_node)
6123 if (! IS_AGGR_TYPE (type))
6127 return CLASSTYPE_SIZE (type) == integer_zero_node;
6129 if (TYPE_BINFO_BASETYPES (type))
6131 t = TYPE_FIELDS (type);
6132 while (t && TREE_CODE (t) != FIELD_DECL)
6134 return (t == NULL_TREE);
6137 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6138 a *_TYPE node. NODE can also be a local class. */
6141 get_enclosing_class (type)
6146 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6148 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6151 node = DECL_CONTEXT (node);
6157 node = TYPE_CONTEXT (node);
6161 my_friendly_abort (0);
6167 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6170 is_base_of_enclosing_class (base, type)
6175 if (get_binfo (base, type, 0))
6178 type = get_enclosing_class (type);
6183 /* Note that NAME was looked up while the current class was being
6184 defined and that the result of that lookup was DECL. */
6187 maybe_note_name_used_in_class (name, decl)
6191 splay_tree names_used;
6193 /* If we're not defining a class, there's nothing to do. */
6194 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6197 /* If there's already a binding for this NAME, then we don't have
6198 anything to worry about. */
6199 if (IDENTIFIER_CLASS_VALUE (name))
6202 if (!current_class_stack[current_class_depth - 1].names_used)
6203 current_class_stack[current_class_depth - 1].names_used
6204 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6205 names_used = current_class_stack[current_class_depth - 1].names_used;
6207 splay_tree_insert (names_used,
6208 (splay_tree_key) name,
6209 (splay_tree_value) decl);
6212 /* Note that NAME was declared (as DECL) in the current class. Check
6213 to see that the declaration is legal. */
6216 note_name_declared_in_class (name, decl)
6220 splay_tree names_used;
6223 /* Look to see if we ever used this name. */
6225 = current_class_stack[current_class_depth - 1].names_used;
6229 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6232 /* [basic.scope.class]
6234 A name N used in a class S shall refer to the same declaration
6235 in its context and when re-evaluated in the completed scope of
6237 cp_error ("declaration of `%#D'", decl);
6238 cp_error_at ("changes meaning of `%s' from `%+#D'",
6239 IDENTIFIER_POINTER (DECL_NAME (decl)),
6244 /* Dump the offsets of all the bases rooted at BINFO to stderr.
6245 INDENT should be zero when called from the top level; it is
6246 incremented recursively. */
6249 dump_class_hierarchy (binfo, indent)
6255 fprintf (stderr, "%*s0x%x (%s) %d %s\n", indent, "",
6256 (unsigned int) binfo,
6257 type_as_string (binfo, TS_PLAIN),
6258 TREE_INT_CST_LOW (BINFO_OFFSET (binfo)),
6259 BINFO_PRIMARY_MARKED_P (binfo) ? "primary" : "");
6261 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6262 dump_class_hierarchy (BINFO_BASETYPE (binfo, i), indent + 2);