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 tree modify_all_vtables PROTO((tree, int *, 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_decl (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_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_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 /* If this function doesn't override anything from a base class, we
1170 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1171 override something, we keep it around and assign its DECL_VINDEX
1172 later, in modify_all_vtables. */
1173 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1174 /* We've already dealt with this function. */
1176 else if (DECL_VINDEX (fndecl) == error_mark_node)
1178 /* FNDECL is a new virtual function; it doesn't override any
1179 virtual function in a base class. */
1181 /* We remember that this was the base sub-object for rtti. */
1182 CLASSTYPE_RTTI (t) = t;
1184 start_vtable (t, has_virtual);
1186 /* Now assign virtual dispatch information. */
1187 DECL_VINDEX (fndecl) = build_shared_int_cst ((*has_virtual)++);
1188 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1190 /* Save the state we've computed on the NEW_VIRTUALS list. */
1191 *new_virtuals_p = tree_cons (integer_zero_node,
1195 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1196 /* FNDECL overrides a function from a base class. */
1197 *overridden_virtuals_p = tree_cons (NULL_TREE,
1199 *overridden_virtuals_p);
1202 extern struct obstack *current_obstack;
1204 /* Add method METHOD to class TYPE.
1206 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1207 the class type where the method should be added. */
1210 add_method (type, fields, method)
1211 tree type, *fields, method;
1213 /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1215 DECL_CONTEXT (method) = type;
1216 DECL_CLASS_CONTEXT (method) = type;
1218 if (fields && *fields)
1219 *fields = build_overload (method, *fields);
1226 if (!CLASSTYPE_METHOD_VEC (type))
1227 /* Make a new method vector. We start with 8 entries. We must
1228 allocate at least two (for constructors and destructors), and
1229 we're going to end up with an assignment operator at some
1232 We could use a TREE_LIST for now, and convert it to a
1233 TREE_VEC in finish_struct, but we would probably waste more
1234 memory making the links in the list than we would by
1235 over-allocating the size of the vector here. Furthermore,
1236 we would complicate all the code that expects this to be a
1238 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1240 method_vec = CLASSTYPE_METHOD_VEC (type);
1241 len = TREE_VEC_LENGTH (method_vec);
1243 if (DECL_NAME (method) == constructor_name (type))
1244 /* A new constructor or destructor. Constructors go in
1245 slot 0; destructors go in slot 1. */
1246 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1249 /* See if we already have an entry with this name. */
1250 for (slot = 2; slot < len; ++slot)
1251 if (!TREE_VEC_ELT (method_vec, slot)
1252 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1254 == DECL_NAME (method)))
1259 /* We need a bigger method vector. */
1260 tree new_vec = make_tree_vec (2 * len);
1261 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1262 (PTR) &TREE_VEC_ELT (new_vec, 0),
1263 len * sizeof (tree));
1265 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1268 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1270 /* Type conversion operators have to come before
1271 ordinary methods; add_conversions depends on this to
1272 speed up looking for conversion operators. So, if
1273 necessary, we slide some of the vector elements up.
1274 In theory, this makes this algorithm O(N^2) but we
1275 don't expect many conversion operators. */
1276 for (slot = 2; slot < len; ++slot)
1278 tree fn = TREE_VEC_ELT (method_vec, slot);
1281 /* There are no more entries in the vector, so we
1282 can insert the new conversion operator here. */
1285 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1286 /* We can insert the new function right at the
1291 if (!TREE_VEC_ELT (method_vec, slot))
1292 /* There is nothing in the Ith slot, so we can avoid
1297 /* We know the last slot in the vector is empty
1298 because we know that at this point there's room
1299 for a new function. */
1300 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1301 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1302 (len - slot - 1) * sizeof (tree));
1303 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1308 if (template_class_depth (type))
1309 /* TYPE is a template class. Don't issue any errors now; wait
1310 until instantiation time to complain. */
1316 /* Check to see if we've already got this method. */
1317 for (fns = TREE_VEC_ELT (method_vec, slot);
1319 fns = OVL_NEXT (fns))
1321 tree fn = OVL_CURRENT (fns);
1323 if (TREE_CODE (fn) != TREE_CODE (method))
1326 if (TREE_CODE (method) != TEMPLATE_DECL)
1328 /* [over.load] Member function declarations with the
1329 same name and the same parameter types cannot be
1330 overloaded if any of them is a static member
1331 function declaration. */
1332 if (DECL_STATIC_FUNCTION_P (fn)
1333 != DECL_STATIC_FUNCTION_P (method))
1335 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1336 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1338 if (! DECL_STATIC_FUNCTION_P (fn))
1339 parms1 = TREE_CHAIN (parms1);
1341 parms2 = TREE_CHAIN (parms2);
1343 if (compparms (parms1, parms2))
1344 cp_error ("`%#D' and `%#D' cannot be overloaded",
1348 /* Since this is an ordinary function in a
1349 non-template class, it's mangled name can be used
1350 as a unique identifier. This technique is only
1351 an optimization; we would get the same results if
1352 we just used decls_match here. */
1353 if (DECL_ASSEMBLER_NAME (fn)
1354 != DECL_ASSEMBLER_NAME (method))
1357 else if (!decls_match (fn, method))
1360 /* There has already been a declaration of this method
1361 or member template. */
1362 cp_error_at ("`%D' has already been declared in `%T'",
1365 /* We don't call duplicate_decls here to merge the
1366 declarations because that will confuse things if the
1367 methods have inline definitions. In particular, we
1368 will crash while processing the definitions. */
1373 /* Actually insert the new method. */
1374 TREE_VEC_ELT (method_vec, slot)
1375 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1377 /* Add the new binding. */
1378 if (!DECL_CONSTRUCTOR_P (method)
1379 && !DECL_DESTRUCTOR_P (method))
1380 push_class_level_binding (DECL_NAME (method),
1381 TREE_VEC_ELT (method_vec, slot));
1385 /* Subroutines of finish_struct. */
1387 /* Look through the list of fields for this struct, deleting
1388 duplicates as we go. This must be recursive to handle
1391 FIELD is the field which may not appear anywhere in FIELDS.
1392 FIELD_PTR, if non-null, is the starting point at which
1393 chained deletions may take place.
1394 The value returned is the first acceptable entry found
1397 Note that anonymous fields which are not of UNION_TYPE are
1398 not duplicates, they are just anonymous fields. This happens
1399 when we have unnamed bitfields, for example. */
1402 delete_duplicate_fields_1 (field, fields)
1407 if (DECL_NAME (field) == 0)
1409 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1412 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1413 fields = delete_duplicate_fields_1 (x, fields);
1418 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1420 if (DECL_NAME (x) == 0)
1422 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1424 TYPE_FIELDS (TREE_TYPE (x))
1425 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1426 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1429 fields = TREE_CHAIN (fields);
1431 TREE_CHAIN (prev) = TREE_CHAIN (x);
1434 else if (TREE_CODE (field) == USING_DECL)
1435 /* A using declaration may is allowed to appear more than
1436 once. We'll prune these from the field list later, and
1437 handle_using_decl will complain about invalid multiple
1440 else if (DECL_NAME (field) == DECL_NAME (x))
1442 if (TREE_CODE (field) == CONST_DECL
1443 && TREE_CODE (x) == CONST_DECL)
1444 cp_error_at ("duplicate enum value `%D'", x);
1445 else if (TREE_CODE (field) == CONST_DECL
1446 || TREE_CODE (x) == CONST_DECL)
1447 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1449 else if (DECL_DECLARES_TYPE_P (field)
1450 && DECL_DECLARES_TYPE_P (x))
1452 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1454 cp_error_at ("duplicate nested type `%D'", x);
1456 else if (DECL_DECLARES_TYPE_P (field)
1457 || DECL_DECLARES_TYPE_P (x))
1459 /* Hide tag decls. */
1460 if ((TREE_CODE (field) == TYPE_DECL
1461 && DECL_ARTIFICIAL (field))
1462 || (TREE_CODE (x) == TYPE_DECL
1463 && DECL_ARTIFICIAL (x)))
1465 cp_error_at ("duplicate field `%D' (as type and non-type)",
1469 cp_error_at ("duplicate member `%D'", x);
1471 fields = TREE_CHAIN (fields);
1473 TREE_CHAIN (prev) = TREE_CHAIN (x);
1481 delete_duplicate_fields (fields)
1485 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1486 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1489 /* Change the access of FDECL to ACCESS in T. The access to FDECL is
1490 along the path given by BINFO. Return 1 if change was legit,
1491 otherwise return 0. */
1494 alter_access (t, binfo, fdecl, access)
1500 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1503 if (TREE_VALUE (elem) != access)
1505 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1506 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1508 error ("conflicting access specifications for field `%s', ignored",
1509 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1513 /* They're changing the access to the same thing they changed
1514 it to before. That's OK. */
1520 enforce_access (binfo, fdecl);
1521 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1527 /* Process the USING_DECL, which is a member of T. */
1530 handle_using_decl (using_decl, t)
1534 tree ctype = DECL_INITIAL (using_decl);
1535 tree name = DECL_NAME (using_decl);
1537 = TREE_PRIVATE (using_decl) ? access_private_node
1538 : TREE_PROTECTED (using_decl) ? access_protected_node
1539 : access_public_node;
1541 tree flist = NULL_TREE;
1542 tree fields = TYPE_FIELDS (t);
1543 tree method_vec = CLASSTYPE_METHOD_VEC (t);
1548 binfo = binfo_or_else (ctype, t);
1552 if (name == constructor_name (ctype)
1553 || name == constructor_name_full (ctype))
1555 cp_error_at ("using-declaration for constructor", using_decl);
1559 fdecl = lookup_member (binfo, name, 0, 0);
1563 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1567 /* Functions are represented as TREE_LIST, with the purpose
1568 being the type and the value the functions. Other members
1569 come as themselves. */
1570 if (TREE_CODE (fdecl) == TREE_LIST)
1571 /* Ignore base type this came from. */
1572 fdecl = TREE_VALUE (fdecl);
1574 if (TREE_CODE (fdecl) == OVERLOAD)
1576 /* We later iterate over all functions. */
1578 fdecl = OVL_FUNCTION (flist);
1581 name = DECL_NAME (fdecl);
1582 n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1583 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1584 if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1587 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1588 cp_error_at (" because of local method `%#D' with same name",
1589 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1593 if (! DECL_LANG_SPECIFIC (fdecl))
1594 /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return. */
1597 for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1598 if (DECL_NAME (tmp) == name)
1600 cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1601 cp_error_at (" because of local field `%#D' with same name", tmp);
1605 /* Make type T see field decl FDECL with access ACCESS.*/
1610 if (alter_access (t, binfo, OVL_FUNCTION (flist),
1613 flist = OVL_CHAIN (flist);
1617 alter_access (t, binfo, fdecl, access);
1620 /* Run through the base clases of T, updating
1621 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1622 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1626 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1629 int *cant_have_default_ctor_p;
1630 int *cant_have_const_ctor_p;
1631 int *no_const_asn_ref_p;
1635 int seen_nearly_empty_base_p;
1638 binfos = TYPE_BINFO_BASETYPES (t);
1639 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1640 seen_nearly_empty_base_p = 0;
1642 /* An aggregate cannot have baseclasses. */
1643 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1645 for (i = 0; i < n_baseclasses; ++i)
1650 /* Figure out what base we're looking at. */
1651 base_binfo = TREE_VEC_ELT (binfos, i);
1652 basetype = TREE_TYPE (base_binfo);
1654 /* If the type of basetype is incomplete, then we already
1655 complained about that fact (and we should have fixed it up as
1657 if (TYPE_SIZE (basetype) == 0)
1660 /* The base type is of incomplete type. It is
1661 probably best to pretend that it does not
1663 if (i == n_baseclasses-1)
1664 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1665 TREE_VEC_LENGTH (binfos) -= 1;
1667 for (j = i; j+1 < n_baseclasses; j++)
1668 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1672 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1673 here because the case of virtual functions but non-virtual
1674 dtor is handled in finish_struct_1. */
1675 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1676 && TYPE_HAS_DESTRUCTOR (basetype))
1677 cp_warning ("base class `%#T' has a non-virtual destructor",
1680 /* If the base class doesn't have copy constructors or
1681 assignment operators that take const references, then the
1682 derived class cannot have such a member automatically
1684 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1685 *cant_have_const_ctor_p = 1;
1686 if (TYPE_HAS_ASSIGN_REF (basetype)
1687 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1688 *no_const_asn_ref_p = 1;
1689 /* Similarly, if the base class doesn't have a default
1690 constructor, then the derived class won't have an
1691 automatically generated default constructor. */
1692 if (TYPE_HAS_CONSTRUCTOR (basetype)
1693 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1695 *cant_have_default_ctor_p = 1;
1696 if (! TYPE_HAS_CONSTRUCTOR (t))
1698 cp_pedwarn ("base `%T' with only non-default constructor",
1700 cp_pedwarn ("in class without a constructor");
1704 /* If the base class is not empty or nearly empty, then this
1705 class cannot be nearly empty. */
1706 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1707 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1708 /* And if there is more than one nearly empty base, then the
1709 derived class is not nearly empty either. */
1710 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1711 && seen_nearly_empty_base_p)
1712 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1713 /* If this is the first nearly empty base class, then remember
1715 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1716 seen_nearly_empty_base_p = 1;
1718 /* A lot of properties from the bases also apply to the derived
1720 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1721 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1722 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1723 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1724 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1725 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1726 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1727 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1728 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1730 /* Derived classes can implicitly become COMified if their bases
1732 if (CLASSTYPE_COM_INTERFACE (basetype))
1733 CLASSTYPE_COM_INTERFACE (t) = 1;
1734 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1737 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1739 CLASSTYPE_COM_INTERFACE (t) = 0;
1744 /* Make the Ith baseclass of T its primary base. */
1747 set_primary_base (t, i, has_virtual_p)
1754 CLASSTYPE_VFIELD_PARENT (t) = i;
1755 basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
1756 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1757 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1758 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1759 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1760 *has_virtual_p = CLASSTYPE_VSIZE (basetype);
1763 /* Determine the primary class for T. */
1766 determine_primary_base (t, has_virtual_p)
1770 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1772 /* If there are no baseclasses, there is certainly no primary base. */
1773 if (n_baseclasses == 0)
1778 for (i = 0; i < n_baseclasses; i++)
1780 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1781 tree basetype = BINFO_TYPE (base_binfo);
1783 if (TYPE_CONTAINS_VPTR_P (basetype))
1785 /* Even a virtual baseclass can contain our RTTI
1786 information. But, we prefer a non-virtual polymorphic
1788 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1789 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1791 /* A virtual baseclass can't be the primary base under the
1792 old ABI. And under the new ABI we still prefer a
1793 non-virtual base. */
1794 if (TREE_VIA_VIRTUAL (base_binfo))
1797 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1799 set_primary_base (t, i, has_virtual_p);
1800 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1806 /* Only add unique vfields, and flatten them out as we go. */
1807 for (vfields = CLASSTYPE_VFIELDS (basetype);
1809 vfields = TREE_CHAIN (vfields))
1810 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1811 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1812 CLASSTYPE_VFIELDS (t)
1813 = tree_cons (base_binfo,
1814 VF_BASETYPE_VALUE (vfields),
1815 CLASSTYPE_VFIELDS (t));
1817 if (*has_virtual_p == 0)
1818 set_primary_base (t, i, has_virtual_p);
1823 if (!TYPE_VFIELD (t))
1824 CLASSTYPE_VFIELD_PARENT (t) = -1;
1826 /* The new ABI allows for the use of a "nearly-empty" virtual base
1827 class as the primary base class if no non-virtual polymorphic
1828 base can be found. */
1829 if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1830 for (i = 0; i < n_baseclasses; ++i)
1832 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1833 tree basetype = BINFO_TYPE (base_binfo);
1835 if (TREE_VIA_VIRTUAL (base_binfo)
1836 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1838 set_primary_base (t, i, has_virtual_p);
1839 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1844 /* Mark the primary base classes at this point. */
1845 mark_primary_bases (t);
1848 /* Set memoizing fields and bits of T (and its variants) for later
1852 finish_struct_bits (t)
1855 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1857 /* Fix up variants (if any). */
1858 tree variants = TYPE_NEXT_VARIANT (t);
1861 /* These fields are in the _TYPE part of the node, not in
1862 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1863 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1864 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1865 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1866 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1868 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1869 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1870 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1871 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1872 /* Copy whatever these are holding today. */
1873 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1874 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1875 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1876 TYPE_SIZE (variants) = TYPE_SIZE (t);
1877 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1878 variants = TYPE_NEXT_VARIANT (variants);
1881 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1882 /* For a class w/o baseclasses, `finish_struct' has set
1883 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1884 definition). Similarly for a class whose base classes do not
1885 have vtables. When neither of these is true, we might have
1886 removed abstract virtuals (by providing a definition), added
1887 some (by declaring new ones), or redeclared ones from a base
1888 class. We need to recalculate what's really an abstract virtual
1889 at this point (by looking in the vtables). */
1890 get_pure_virtuals (t);
1894 /* Notice whether this class has type conversion functions defined. */
1895 tree binfo = TYPE_BINFO (t);
1896 tree binfos = BINFO_BASETYPES (binfo);
1899 for (i = n_baseclasses-1; i >= 0; i--)
1901 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1903 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1907 /* If this type has a copy constructor, force its mode to be BLKmode, and
1908 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1909 be passed by invisible reference and prevent it from being returned in
1912 Also do this if the class has BLKmode but can still be returned in
1913 registers, since function_cannot_inline_p won't let us inline
1914 functions returning such a type. This affects the HP-PA. */
1915 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1916 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1917 && CLASSTYPE_NON_AGGREGATE (t)))
1920 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1921 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1923 TYPE_MODE (variants) = BLKmode;
1924 TREE_ADDRESSABLE (variants) = 1;
1929 /* Issue warnings about T having private constructors, but no friends,
1932 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1933 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1934 non-private static member functions. */
1937 maybe_warn_about_overly_private_class (t)
1940 int has_member_fn = 0;
1941 int has_nonprivate_method = 0;
1944 if (!warn_ctor_dtor_privacy
1945 /* If the class has friends, those entities might create and
1946 access instances, so we should not warn. */
1947 || (CLASSTYPE_FRIEND_CLASSES (t)
1948 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1949 /* We will have warned when the template was declared; there's
1950 no need to warn on every instantiation. */
1951 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1952 /* There's no reason to even consider warning about this
1956 /* We only issue one warning, if more than one applies, because
1957 otherwise, on code like:
1960 // Oops - forgot `public:'
1966 we warn several times about essentially the same problem. */
1968 /* Check to see if all (non-constructor, non-destructor) member
1969 functions are private. (Since there are no friends or
1970 non-private statics, we can't ever call any of the private member
1972 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1973 /* We're not interested in compiler-generated methods; they don't
1974 provide any way to call private members. */
1975 if (!DECL_ARTIFICIAL (fn))
1977 if (!TREE_PRIVATE (fn))
1979 if (DECL_STATIC_FUNCTION_P (fn))
1980 /* A non-private static member function is just like a
1981 friend; it can create and invoke private member
1982 functions, and be accessed without a class
1986 has_nonprivate_method = 1;
1989 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1993 if (!has_nonprivate_method && has_member_fn)
1995 /* There are no non-private methods, and there's at least one
1996 private member function that isn't a constructor or
1997 destructor. (If all the private members are
1998 constructors/destructors we want to use the code below that
1999 issues error messages specifically referring to
2000 constructors/destructors.) */
2002 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2003 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2004 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2005 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2007 has_nonprivate_method = 1;
2010 if (!has_nonprivate_method)
2012 cp_warning ("all member functions in class `%T' are private", t);
2017 /* Even if some of the member functions are non-private, the class
2018 won't be useful for much if all the constructors or destructors
2019 are private: such an object can never be created or destroyed. */
2020 if (TYPE_HAS_DESTRUCTOR (t))
2022 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2024 if (TREE_PRIVATE (dtor))
2026 cp_warning ("`%#T' only defines a private destructor and has no friends",
2032 if (TYPE_HAS_CONSTRUCTOR (t))
2034 int nonprivate_ctor = 0;
2036 /* If a non-template class does not define a copy
2037 constructor, one is defined for it, enabling it to avoid
2038 this warning. For a template class, this does not
2039 happen, and so we would normally get a warning on:
2041 template <class T> class C { private: C(); };
2043 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2044 complete non-template or fully instantiated classes have this
2046 if (!TYPE_HAS_INIT_REF (t))
2047 nonprivate_ctor = 1;
2049 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2053 tree ctor = OVL_CURRENT (fn);
2054 /* Ideally, we wouldn't count copy constructors (or, in
2055 fact, any constructor that takes an argument of the
2056 class type as a parameter) because such things cannot
2057 be used to construct an instance of the class unless
2058 you already have one. But, for now at least, we're
2060 if (! TREE_PRIVATE (ctor))
2062 nonprivate_ctor = 1;
2067 if (nonprivate_ctor == 0)
2069 cp_warning ("`%#T' only defines private constructors and has no friends",
2076 /* Function to help qsort sort FIELD_DECLs by name order. */
2079 field_decl_cmp (x, y)
2082 if (DECL_NAME (*x) == DECL_NAME (*y))
2083 /* A nontype is "greater" than a type. */
2084 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2085 if (DECL_NAME (*x) == NULL_TREE)
2087 if (DECL_NAME (*y) == NULL_TREE)
2089 if (DECL_NAME (*x) < DECL_NAME (*y))
2094 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2097 method_name_cmp (m1, m2)
2098 const tree *m1, *m2;
2100 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2102 if (*m1 == NULL_TREE)
2104 if (*m2 == NULL_TREE)
2106 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2111 /* Warn about duplicate methods in fn_fields. Also compact method
2112 lists so that lookup can be made faster.
2114 Data Structure: List of method lists. The outer list is a
2115 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2116 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2117 links the entire list of methods for TYPE_METHODS. Friends are
2118 chained in the same way as member functions (? TREE_CHAIN or
2119 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2120 list. That allows them to be quickly deleted, and requires no
2123 If there are any constructors/destructors, they are moved to the
2124 front of the list. This makes pushclass more efficient.
2126 @@ The above comment is obsolete. It mostly describes what add_method
2127 @@ and add_implicitly_declared_members do.
2129 Sort methods that are not special (i.e., constructors, destructors, and
2130 type conversion operators) so that we can find them faster in search. */
2133 finish_struct_methods (t)
2138 tree ctor_name = constructor_name (t);
2141 if (!TYPE_METHODS (t))
2143 /* Clear these for safety; perhaps some parsing error could set
2144 these incorrectly. */
2145 TYPE_HAS_CONSTRUCTOR (t) = 0;
2146 TYPE_HAS_DESTRUCTOR (t) = 0;
2147 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2151 method_vec = CLASSTYPE_METHOD_VEC (t);
2152 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2153 len = TREE_VEC_LENGTH (method_vec);
2155 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2156 and the next few with type conversion operators (if any). */
2157 for (fn_fields = TYPE_METHODS (t); fn_fields;
2158 fn_fields = TREE_CHAIN (fn_fields))
2160 tree fn_name = DECL_NAME (fn_fields);
2162 /* Clear out this flag.
2164 @@ Doug may figure out how to break
2165 @@ this with nested classes and friends. */
2166 DECL_IN_AGGR_P (fn_fields) = 0;
2168 /* Note here that a copy ctor is private, so we don't dare generate
2169 a default copy constructor for a class that has a member
2170 of this type without making sure they have access to it. */
2171 if (fn_name == ctor_name)
2173 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2174 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2176 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2177 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2179 if (TREE_CHAIN (parmtypes) == NULL_TREE
2180 || TREE_CHAIN (parmtypes) == void_list_node
2181 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2183 if (TREE_PROTECTED (fn_fields))
2184 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2185 else if (TREE_PRIVATE (fn_fields))
2186 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2190 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2192 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2194 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2196 if (TREE_PROTECTED (fn_fields))
2197 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2198 else if (TREE_PRIVATE (fn_fields))
2199 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2204 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2205 /* We thought there was a destructor, but there wasn't. Some
2206 parse errors cause this anomalous situation. */
2207 TYPE_HAS_DESTRUCTOR (t) = 0;
2209 /* Issue warnings about private constructors and such. If there are
2210 no methods, then some public defaults are generated. */
2211 maybe_warn_about_overly_private_class (t);
2213 /* Now sort the methods. */
2214 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2216 TREE_VEC_LENGTH (method_vec) = len;
2218 /* The type conversion ops have to live at the front of the vec, so we
2220 for (slot = 2; slot < len; ++slot)
2222 tree fn = TREE_VEC_ELT (method_vec, slot);
2224 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2228 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2229 (int (*)(const void *, const void *))method_name_cmp);
2232 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2235 duplicate_tag_error (t)
2238 cp_error ("redefinition of `%#T'", t);
2239 cp_error_at ("previous definition here", t);
2241 /* Pretend we haven't defined this type. */
2243 /* All of the component_decl's were TREE_CHAINed together in the parser.
2244 finish_struct_methods walks these chains and assembles all methods with
2245 the same base name into DECL_CHAINs. Now we don't need the parser chains
2246 anymore, so we unravel them. */
2248 /* This used to be in finish_struct, but it turns out that the
2249 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2251 if (CLASSTYPE_METHOD_VEC (t))
2253 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2254 int i, len = TREE_VEC_LENGTH (method_vec);
2255 for (i = 0; i < len; i++)
2257 tree unchain = TREE_VEC_ELT (method_vec, i);
2258 while (unchain != NULL_TREE)
2260 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2261 unchain = OVL_NEXT (unchain);
2266 if (TYPE_LANG_SPECIFIC (t))
2268 tree binfo = TYPE_BINFO (t);
2269 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2270 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2271 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2272 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2274 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2275 BINFO_BASETYPES(binfo) = NULL_TREE;
2277 TYPE_BINFO (t) = binfo;
2278 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2279 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2280 TYPE_REDEFINED (t) = 1;
2281 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2282 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2284 TYPE_SIZE (t) = NULL_TREE;
2285 TYPE_MODE (t) = VOIDmode;
2286 TYPE_FIELDS (t) = NULL_TREE;
2287 TYPE_METHODS (t) = NULL_TREE;
2288 TYPE_VFIELD (t) = NULL_TREE;
2289 TYPE_CONTEXT (t) = NULL_TREE;
2290 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2293 /* Returns the number of extra entries (at negative indices) required
2294 for BINFO's vtable. */
2297 num_extra_vtbl_entries (binfo)
2303 /* Under the old ABI, there are no entries at negative offsets. */
2304 if (!vbase_offsets_in_vtable_p ())
2305 return size_zero_node;
2307 type = BINFO_TYPE (binfo);
2310 /* There is an entry for the offset to each virtual base. */
2311 entries += list_length (CLASSTYPE_VBASECLASSES (type));
2313 return size_int (entries);
2316 /* Returns the offset (in bytes) from the beginning of BINFO's vtable
2317 where the vptr should actually point. */
2320 size_extra_vtbl_entries (binfo)
2325 offset = size_binop (EXACT_DIV_EXPR,
2326 TYPE_SIZE (vtable_entry_type),
2327 size_int (BITS_PER_UNIT));
2328 offset = size_binop (MULT_EXPR, offset, num_extra_vtbl_entries (binfo));
2329 return fold (offset);
2332 /* Construct the initializer for BINFOs virtual function table. BINFO
2333 is part of the hierarchy dominated by T. */
2336 build_vtbl_initializer (binfo, t)
2340 tree v = BINFO_VIRTUALS (binfo);
2341 tree inits = NULL_TREE;
2342 tree type = BINFO_TYPE (binfo);
2344 /* Add entries to the vtable for offsets to our virtual bases. */
2345 inits = build_vbase_offset_vtbl_entries (binfo, t);
2347 /* Process the RTTI stuff at the head of the list. If we're not
2348 using vtable thunks, then the RTTI entry is just an ordinary
2349 function, and we can process it just like the other virtual
2350 function entries. */
2351 if (!CLASSTYPE_COM_INTERFACE (type) && flag_vtable_thunks)
2356 /* The first entry is an offset. */
2357 offset = TREE_PURPOSE (v);
2358 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2361 /* Convert the offset to look like a function pointer, so that
2362 we can put it in the vtable. */
2363 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2364 TREE_CONSTANT (init) = 1;
2365 init = build_vtable_entry (integer_zero_node, init);
2366 inits = tree_cons (NULL_TREE, init, inits);
2368 /* Even in this case, the second entry (the tdesc pointer) is
2369 just an ordinary function. */
2373 /* Go through all the ordinary virtual functions, building up
2381 /* Pull the offset for `this', and the function to call, out of
2383 delta = TREE_PURPOSE (v);
2384 fn = TREE_VALUE (v);
2385 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2386 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2388 /* You can't call an abstract virtual function; it's abstract.
2389 So, we replace these functions with __pure_virtual. */
2390 if (DECL_PURE_VIRTUAL_P (fn))
2393 /* Package up that information for the vtable. */
2394 init = build_vtable_entry_for_fn (delta, fn);
2395 /* And add it to the chain of initializers. */
2396 inits = tree_cons (NULL_TREE, init, inits);
2402 /* The initializers were built up in reverse order; straighten them
2404 inits = nreverse (inits);
2405 /* Package all the initializers up as an array initializer. */
2406 return build_nt (CONSTRUCTOR, NULL_TREE, inits);
2409 /* Called from finish_vtbls via dfs_walk. */
2412 dfs_finish_vtbls (binfo, data)
2416 if (!BINFO_PRIMARY_MARKED_P (binfo)
2417 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2418 && BINFO_NEW_VTABLE_MARKED (binfo))
2423 decl = BINFO_VTABLE (binfo);
2424 context = DECL_CONTEXT (decl);
2425 DECL_CONTEXT (decl) = 0;
2426 DECL_INITIAL (decl) = build_vtbl_initializer (binfo, (tree) data);
2427 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
2428 DECL_CONTEXT (decl) = context;
2431 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2432 SET_BINFO_MARKED (binfo);
2437 /* Create all the necessary vtables for T and its base classes. */
2443 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
2444 dfs_unmarked_real_bases_queue_p, t);
2445 dfs_walk (TYPE_BINFO (t), dfs_unmark,
2446 dfs_marked_real_bases_queue_p, t);
2449 /* True if we should override the given BASE_FNDECL with the given
2453 overrides (fndecl, base_fndecl)
2454 tree fndecl, base_fndecl;
2456 /* Destructors have special names. */
2457 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2458 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2460 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2461 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2463 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2465 tree types, base_types;
2467 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2468 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2470 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2471 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2472 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2473 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2474 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2480 /* Returns the BINFO_OFFSET for the base of BINFO that has the same
2484 get_class_offset_1 (parent, binfo, context, t, fndecl)
2485 tree parent, binfo, context, t, fndecl;
2487 tree binfos = BINFO_BASETYPES (binfo);
2488 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2489 tree rval = NULL_TREE;
2491 if (binfo == parent)
2492 return error_mark_node;
2494 for (i = 0; i < n_baselinks; i++)
2496 tree base_binfo = TREE_VEC_ELT (binfos, i);
2499 if (TREE_VIA_VIRTUAL (base_binfo))
2500 base_binfo = BINFO_FOR_VBASE (BINFO_TYPE (base_binfo), t);
2501 nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2502 /* See if we have a new value */
2503 if (nrval && (nrval != error_mark_node || rval==0))
2505 /* Only compare if we have two offsets */
2506 if (rval && rval != error_mark_node
2507 && ! tree_int_cst_equal (nrval, rval))
2509 /* Only give error if the two offsets are different */
2510 error ("every virtual function must have a unique final overrider");
2511 cp_error (" found two (or more) `%T' class subobjects in `%T'", context, t);
2512 cp_error (" with virtual `%D' from virtual base class", fndecl);
2518 if (rval && BINFO_TYPE (binfo) == context)
2520 my_friendly_assert (rval == error_mark_node
2521 || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2522 rval = BINFO_OFFSET (binfo);
2528 /* Called from get_class_offset via dfs_walk. */
2531 dfs_get_class_offset (binfo, data)
2535 tree list = (tree) data;
2536 tree context = TREE_TYPE (list);
2538 if (same_type_p (BINFO_TYPE (binfo), context))
2540 if (TREE_VALUE (list))
2541 return error_mark_node;
2543 TREE_VALUE (list) = BINFO_OFFSET (binfo);
2546 SET_BINFO_MARKED (binfo);
2551 /* Returns the BINFO_OFFSET for the subobject of BINFO that has the
2552 type given by CONTEXT. */
2555 get_class_offset (context, t, binfo, fndecl)
2556 tree context, t, binfo, fndecl;
2563 return integer_zero_node;
2565 if (BINFO_TYPE (binfo) == context)
2566 return BINFO_OFFSET (binfo);
2568 /* Check less derived binfos first. */
2569 while (BINFO_BASETYPES (binfo)
2570 && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2572 tree binfos = BINFO_BASETYPES (binfo);
2573 binfo = TREE_VEC_ELT (binfos, i);
2574 if (BINFO_TYPE (binfo) == context)
2575 return BINFO_OFFSET (binfo);
2578 list = build_tree_list (t, NULL_TREE);
2579 TREE_TYPE (list) = context;
2580 offset = dfs_walk (TYPE_BINFO (t),
2581 dfs_get_class_offset,
2582 dfs_unmarked_real_bases_queue_p,
2584 dfs_walk (TYPE_BINFO (t), dfs_unmark, dfs_marked_real_bases_queue_p, t);
2586 if (offset == error_mark_node)
2588 error ("every virtual function must have a unique final overrider");
2589 cp_error (" found two (or more) `%T' class subobjects in `%T'",
2591 cp_error (" with virtual `%D' from virtual base class", fndecl);
2592 offset = integer_zero_node;
2595 offset = TREE_VALUE (list);
2597 my_friendly_assert (offset != NULL_TREE, 999);
2598 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST, 999);
2603 /* Return the BINFO_VIRTUALS list for BINFO, without the RTTI stuff at
2604 the front. If non-NULL, N is set to the number of entries
2608 skip_rtti_stuff (binfo, t, n)
2611 unsigned HOST_WIDE_INT *n;
2615 if (CLASSTYPE_COM_INTERFACE (t))
2620 virtuals = BINFO_VIRTUALS (binfo);
2623 /* We always reserve a slot for the offset/tdesc entry. */
2626 virtuals = TREE_CHAIN (virtuals);
2628 if (flag_vtable_thunks && virtuals)
2630 /* The second slot is reserved for the tdesc pointer when thunks
2634 virtuals = TREE_CHAIN (virtuals);
2641 modify_one_vtable (binfo, t, fndecl)
2642 tree binfo, t, fndecl;
2645 unsigned HOST_WIDE_INT n;
2647 /* update rtti entry */
2650 if (binfo == TYPE_BINFO (t))
2651 build_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))), t);
2653 prepare_fresh_vtable (binfo, t);
2655 if (fndecl == NULL_TREE)
2658 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), &n);
2662 tree current_fndecl = TREE_VALUE (virtuals);
2664 /* We should never have an instance of __pure_virtual on the
2665 BINFO_VIRTUALS list. If we do, then we will never notice
2666 that the function that should have been there instead has
2668 my_friendly_assert (current_fndecl != abort_fndecl,
2671 if (current_fndecl && overrides (fndecl, current_fndecl))
2673 tree base_offset, offset;
2674 tree context = DECL_CLASS_CONTEXT (fndecl);
2675 tree vfield = TYPE_VFIELD (t);
2678 offset = get_class_offset (context, t, binfo, fndecl);
2680 /* Find the right offset for the this pointer based on the
2681 base class we just found. We have to take into
2682 consideration the virtual base class pointers that we
2683 stick in before the virtual function table pointer.
2685 Also, we want just the delta between the most base class
2686 that we derived this vfield from and us. */
2687 base_offset = size_binop (PLUS_EXPR,
2688 get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2689 BINFO_OFFSET (binfo));
2690 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2692 if (binfo == TYPE_BINFO (t))
2693 /* In this case, it is *type*'s vtable we are modifying.
2694 We start with the approximation that it's vtable is
2695 that of the immediate base class. */
2696 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2698 /* This is our very own copy of `basetype' to play with.
2699 Later, we will fill in all the virtual functions that
2700 override the virtual functions in these base classes
2701 which are not defined by the current type. */
2702 prepare_fresh_vtable (binfo, t);
2705 cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2707 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2712 virtuals = TREE_CHAIN (virtuals);
2716 /* Called from modify_all_vtables via dfs_walk. */
2719 dfs_modify_vtables (binfo, data)
2723 if (/* There's no need to modify the vtable for a primary base;
2724 we're not going to use that vtable anyhow. */
2725 !BINFO_PRIMARY_MARKED_P (binfo)
2726 /* Similarly, a base without a vtable needs no modification. */
2727 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2729 tree list = (tree) data;
2730 modify_one_vtable (binfo, TREE_PURPOSE (list), TREE_VALUE (list));
2733 SET_BINFO_MARKED (binfo);
2738 /* Update all of the primary and secondary vtables for T. Create new
2739 vtables as required, and initialize their RTTI information. Each
2740 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2741 function from a base class; find and modify the appropriate entries
2742 to point to the overriding functions. Returns a list, in
2743 declaration order, of the functions that are overridden in this
2744 class, but do not appear in the primary base class vtable, and
2745 which should therefore be appended to the end of the vtable for T. */
2748 modify_all_vtables (t, has_virtual_p, overridden_virtuals)
2751 tree overridden_virtuals;
2756 binfo = TYPE_BINFO (t);
2758 /* Even if there are no overridden virtuals, we want to go through
2759 the hierarchy updating RTTI information. */
2760 if (!overridden_virtuals && TYPE_CONTAINS_VPTR_P (t) && flag_rtti)
2761 overridden_virtuals = build_tree_list (NULL_TREE, NULL_TREE);
2763 /* Iterate through each of the overriding functions, updating the
2765 for (fns = overridden_virtuals; fns; fns = TREE_CHAIN (fns))
2768 list = build_tree_list (t, TREE_VALUE (fns));
2769 dfs_walk (binfo, dfs_modify_vtables,
2770 dfs_unmarked_real_bases_queue_p, list);
2771 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2774 /* If we should include overriding functions for secondary vtables
2775 in our primary vtable, add them now. */
2776 if (all_overridden_vfuns_in_vtables_p ())
2778 tree *fnsp = &overridden_virtuals;
2782 tree fn = TREE_VALUE (*fnsp);
2784 if (BINFO_VIRTUALS (binfo)
2785 && !value_member (fn, BINFO_VIRTUALS (binfo)))
2787 /* We know we need a vtable for this class now. */
2788 start_vtable (t, has_virtual_p);
2789 /* Set the vtable index. */
2791 = build_shared_int_cst ((*has_virtual_p)++);
2792 /* We don't need to convert to a base class when calling
2794 DECL_VIRTUAL_CONTEXT (fn) = t;
2795 /* We don't need to adjust the `this' pointer when
2796 calling this function. */
2797 TREE_PURPOSE (*fnsp) = integer_zero_node;
2799 /* This is an overridden function not already in our
2801 fnsp = &TREE_CHAIN (*fnsp);
2804 /* We've already got an entry for this function. Skip
2806 *fnsp = TREE_CHAIN (*fnsp);
2810 overridden_virtuals = NULL_TREE;
2812 return overridden_virtuals;
2815 /* Fixup all the delta entries in this one vtable that need updating. */
2818 dfs_fixup_vtable_deltas (binfo, data)
2823 unsigned HOST_WIDE_INT n;
2824 tree t = (tree) data;
2826 while (BINFO_PRIMARY_MARKED_P (binfo))
2828 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2829 /* If BINFO is virtual then we'll handle this base later. */
2830 if (TREE_VIA_VIRTUAL (binfo))
2834 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), &n);
2838 tree fndecl = TREE_VALUE (virtuals);
2839 tree delta = TREE_PURPOSE (virtuals);
2843 tree base_offset, offset;
2844 tree context = DECL_CLASS_CONTEXT (fndecl);
2845 tree vfield = TYPE_VFIELD (t);
2848 offset = get_class_offset (context, t, binfo, fndecl);
2850 /* Find the right offset for the this pointer based on the
2851 base class we just found. We have to take into
2852 consideration the virtual base class pointers that we
2853 stick in before the virtual function table pointer.
2855 Also, we want just the delta between the most base class
2856 that we derived this vfield from and us. */
2857 base_offset = size_binop (PLUS_EXPR,
2858 get_derived_offset (binfo,
2859 DECL_CONTEXT (fndecl)),
2860 BINFO_OFFSET (binfo));
2861 this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2863 if (! tree_int_cst_equal (this_offset, delta))
2865 /* Make sure we can modify the derived association with immunity. */
2866 if (binfo == TYPE_BINFO (t))
2867 /* In this case, it is *type*'s vtable we are modifying.
2868 We start with the approximation that it's vtable is that
2869 of the immediate base class. */
2870 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2872 /* This is our very own copy of `basetype' to play
2873 with. Later, we will fill in all the virtual
2874 functions that override the virtual functions in
2875 these base classes which are not defined by the
2877 prepare_fresh_vtable (binfo, t);
2879 modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2885 virtuals = TREE_CHAIN (virtuals);
2891 /* Here, we already know that they match in every respect.
2892 All we have to check is where they had their declarations. */
2895 strictly_overrides (fndecl1, fndecl2)
2896 tree fndecl1, fndecl2;
2898 int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2899 DECL_CLASS_CONTEXT (fndecl1),
2901 if (distance == -2 || distance > 0)
2906 /* Merge overrides for one vtable.
2907 If we want to merge in same function, we are fine.
2909 if one has a DECL_CLASS_CONTEXT that is a parent of the
2910 other, than choose the more derived one
2912 potentially ill-formed (see 10.3 [class.virtual])
2913 we have to check later to see if there was an
2914 override in this class. If there was ok, if not
2915 then it is ill-formed. (mrs)
2917 We take special care to reuse a vtable, if we can. */
2920 override_one_vtable (binfo, old, t)
2927 enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2929 /* Either or both of BINFO or OLD might be primary base classes
2930 because merge_overrides is called with a vbase from the class we
2931 are definining and the corresponding vbase from one of its direct
2934 while (BINFO_PRIMARY_MARKED_P (binfo))
2936 binfo = BINFO_INHERITANCE_CHAIN (binfo);
2937 /* If BINFO is virtual, then we'll handle this virtual base when
2939 if (TREE_VIA_VIRTUAL (binfo))
2942 while (BINFO_PRIMARY_MARKED_P (old))
2943 old = BINFO_INHERITANCE_CHAIN (old);
2945 /* If we have already committed to modifying it, then don't try and
2946 reuse another vtable. */
2947 if (BINFO_NEW_VTABLE_MARKED (binfo))
2950 virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), NULL);
2951 old_virtuals = skip_rtti_stuff (old, BINFO_TYPE (binfo), NULL);
2952 orig_virtuals = skip_rtti_stuff (orig_binfo, BINFO_TYPE (binfo), NULL);
2954 while (orig_virtuals)
2956 tree fndecl = TREE_VALUE (virtuals);
2957 tree old_fndecl = TREE_VALUE (old_virtuals);
2959 /* First check to see if they are the same. */
2960 if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2962 /* No need to do anything. */
2964 else if (strictly_overrides (fndecl, old_fndecl))
2966 if (choose == UNDECIDED)
2968 else if (choose == REUSE_OLD)
2971 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2973 prepare_fresh_vtable (binfo, t);
2974 override_one_vtable (binfo, old, t);
2979 else if (strictly_overrides (old_fndecl, fndecl))
2981 if (choose == UNDECIDED)
2983 else if (choose == REUSE_NEW)
2986 if (! BINFO_NEW_VTABLE_MARKED (binfo))
2988 prepare_fresh_vtable (binfo, t);
2989 override_one_vtable (binfo, old, t);
2992 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2994 else if (choose == NEITHER)
2996 TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
3002 if (! BINFO_NEW_VTABLE_MARKED (binfo))
3004 prepare_fresh_vtable (binfo, t);
3005 override_one_vtable (binfo, old, t);
3009 /* This MUST be overridden, or the class is ill-formed. */
3010 tree fndecl = TREE_VALUE (virtuals);
3012 fndecl = copy_node (fndecl);
3013 copy_lang_decl (fndecl);
3014 DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
3015 /* Make sure we search for it later. */
3016 if (! CLASSTYPE_PURE_VIRTUALS (t))
3017 CLASSTYPE_PURE_VIRTUALS (t) = error_mark_node;
3019 /* We can use integer_zero_node, as we will core dump
3020 if this is used anyway. */
3021 TREE_PURPOSE (virtuals) = integer_zero_node;
3022 TREE_VALUE (virtuals) = fndecl;
3025 virtuals = TREE_CHAIN (virtuals);
3026 old_virtuals = TREE_CHAIN (old_virtuals);
3027 orig_virtuals = TREE_CHAIN (orig_virtuals);
3030 /* Let's reuse the old vtable. */
3031 if (choose == REUSE_OLD)
3033 BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
3034 BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
3038 /* Merge in overrides for virtual bases.
3039 BINFO is the hierarchy we want to modify, and OLD has the potential
3043 merge_overrides (binfo, old, do_self, t)
3048 tree binfos = BINFO_BASETYPES (binfo);
3049 tree old_binfos = BINFO_BASETYPES (old);
3050 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3052 /* Should we use something besides CLASSTYPE_VFIELDS? */
3053 if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
3055 override_one_vtable (binfo, old, t);
3058 for (i = 0; i < n_baselinks; i++)
3060 tree base_binfo = TREE_VEC_ELT (binfos, i);
3061 tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
3062 int is_not_base_vtable
3063 = !BINFO_PRIMARY_MARKED_P (base_binfo);
3064 if (! TREE_VIA_VIRTUAL (base_binfo))
3065 merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
3069 /* Get the base virtual function declarations in T that are either
3070 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
3071 the overrider/hider. */
3074 get_basefndecls (fndecl, t)
3077 tree methods = TYPE_METHODS (t);
3078 tree base_fndecls = NULL_TREE;
3079 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3080 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3084 if (TREE_CODE (methods) == FUNCTION_DECL
3085 && DECL_VINDEX (methods) != NULL_TREE
3086 && DECL_NAME (fndecl) == DECL_NAME (methods))
3087 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
3089 methods = TREE_CHAIN (methods);
3093 return base_fndecls;
3095 for (i = 0; i < n_baseclasses; i++)
3097 tree base_binfo = TREE_VEC_ELT (binfos, i);
3098 tree basetype = BINFO_TYPE (base_binfo);
3100 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3104 return base_fndecls;
3107 /* Mark the functions that have been hidden with their overriders.
3108 Since we start out with all functions already marked with a hider,
3109 no need to mark functions that are just hidden.
3111 Subroutine of warn_hidden. */
3114 mark_overriders (fndecl, base_fndecls)
3115 tree fndecl, base_fndecls;
3117 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3119 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
3120 TREE_PURPOSE (base_fndecls) = fndecl;
3124 /* If this declaration supersedes the declaration of
3125 a method declared virtual in the base class, then
3126 mark this field as being virtual as well. */
3129 check_for_override (decl, ctype)
3132 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
3133 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3134 int virtualp = DECL_VIRTUAL_P (decl);
3135 int found_overriden_fn = 0;
3137 for (i = 0; i < n_baselinks; i++)
3139 tree base_binfo = TREE_VEC_ELT (binfos, i);
3140 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
3142 tree tmp = get_matching_virtual
3144 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
3146 if (tmp && !found_overriden_fn)
3148 /* If this function overrides some virtual in some base
3149 class, then the function itself is also necessarily
3150 virtual, even if the user didn't explicitly say so. */
3151 DECL_VIRTUAL_P (decl) = 1;
3153 /* The TMP we really want is the one from the deepest
3154 baseclass on this path, taking care not to
3155 duplicate if we have already found it (via another
3156 path to its virtual baseclass. */
3157 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
3159 cp_error_at ("`static %#D' cannot be declared", decl);
3160 cp_error_at (" since `virtual %#D' declared in base class",
3166 /* Set DECL_VINDEX to a value that is neither an
3167 INTEGER_CST nor the error_mark_node so that
3168 add_virtual_function will realize this is an
3169 overridden function. */
3171 = tree_cons (tmp, NULL_TREE, DECL_VINDEX (decl));
3173 /* We now know that DECL overrides something,
3174 which is all that is important. But, we must
3175 continue to iterate through all the base-classes
3176 in order to allow get_matching_virtual to check for
3177 various illegal overrides. */
3178 found_overriden_fn = 1;
3184 if (DECL_VINDEX (decl) == NULL_TREE)
3185 DECL_VINDEX (decl) = error_mark_node;
3186 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3190 /* Warn about hidden virtual functions that are not overridden in t.
3191 We know that constructors and destructors don't apply. */
3197 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3198 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3201 /* We go through each separately named virtual function. */
3202 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3204 tree fns = TREE_VEC_ELT (method_vec, i);
3205 tree fndecl = NULL_TREE;
3207 tree base_fndecls = NULL_TREE;
3208 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3209 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3211 /* First see if we have any virtual functions in this batch. */
3212 for (; fns; fns = OVL_NEXT (fns))
3214 fndecl = OVL_CURRENT (fns);
3215 if (DECL_VINDEX (fndecl))
3219 if (fns == NULL_TREE)
3222 /* First we get a list of all possible functions that might be
3223 hidden from each base class. */
3224 for (i = 0; i < n_baseclasses; i++)
3226 tree base_binfo = TREE_VEC_ELT (binfos, i);
3227 tree basetype = BINFO_TYPE (base_binfo);
3229 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3233 fns = OVL_NEXT (fns);
3235 /* ...then mark up all the base functions with overriders, preferring
3236 overriders to hiders. */
3238 for (; fns; fns = OVL_NEXT (fns))
3240 fndecl = OVL_CURRENT (fns);
3241 if (DECL_VINDEX (fndecl))
3242 mark_overriders (fndecl, base_fndecls);
3245 /* Now give a warning for all base functions without overriders,
3246 as they are hidden. */
3247 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3249 if (! overrides (TREE_PURPOSE (base_fndecls),
3250 TREE_VALUE (base_fndecls)))
3252 /* Here we know it is a hider, and no overrider exists. */
3253 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3254 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3260 /* Check for things that are invalid. There are probably plenty of other
3261 things we should check for also. */
3264 finish_struct_anon (t)
3269 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3271 if (TREE_STATIC (field))
3273 if (TREE_CODE (field) != FIELD_DECL)
3276 if (DECL_NAME (field) == NULL_TREE
3277 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3279 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3280 for (; elt; elt = TREE_CHAIN (elt))
3282 if (DECL_ARTIFICIAL (elt))
3285 if (DECL_NAME (elt) == constructor_name (t))
3286 cp_pedwarn_at ("ANSI C++ forbids member `%D' with same name as enclosing class",
3289 if (TREE_CODE (elt) != FIELD_DECL)
3291 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3296 if (TREE_PRIVATE (elt))
3297 cp_pedwarn_at ("private member `%#D' in anonymous union",
3299 else if (TREE_PROTECTED (elt))
3300 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3303 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3304 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3310 extern int interface_only, interface_unknown;
3312 /* Create default constructors, assignment operators, and so forth for
3313 the type indicated by T, if they are needed.
3314 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3315 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3316 cannot have a default constructor, copy constructor taking a const
3317 reference argument, or an assignment operator, respectively. If a
3318 virtual destructor is created, its DECL is returned; otherwise the
3319 return value is NULL_TREE. */
3322 add_implicitly_declared_members (t, cant_have_default_ctor,
3323 cant_have_const_cctor,
3324 cant_have_assignment)
3326 int cant_have_default_ctor;
3327 int cant_have_const_cctor;
3328 int cant_have_assignment;
3331 tree implicit_fns = NULL_TREE;
3332 tree name = TYPE_IDENTIFIER (t);
3333 tree virtual_dtor = NULL_TREE;
3337 if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3339 default_fn = cons_up_default_function (t, name, 0);
3340 check_for_override (default_fn, t);
3342 /* If we couldn't make it work, then pretend we didn't need it. */
3343 if (default_fn == void_type_node)
3344 TYPE_NEEDS_DESTRUCTOR (t) = 0;
3347 TREE_CHAIN (default_fn) = implicit_fns;
3348 implicit_fns = default_fn;
3350 if (DECL_VINDEX (default_fn))
3351 virtual_dtor = default_fn;
3354 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3356 /* Default constructor. */
3357 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3359 default_fn = cons_up_default_function (t, name, 2);
3360 TREE_CHAIN (default_fn) = implicit_fns;
3361 implicit_fns = default_fn;
3364 /* Copy constructor. */
3365 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3367 /* ARM 12.18: You get either X(X&) or X(const X&), but
3369 default_fn = cons_up_default_function (t, name,
3370 3 + cant_have_const_cctor);
3371 TREE_CHAIN (default_fn) = implicit_fns;
3372 implicit_fns = default_fn;
3375 /* Assignment operator. */
3376 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3378 default_fn = cons_up_default_function (t, name,
3379 5 + cant_have_assignment);
3380 TREE_CHAIN (default_fn) = implicit_fns;
3381 implicit_fns = default_fn;
3384 /* Now, hook all of the new functions on to TYPE_METHODS,
3385 and add them to the CLASSTYPE_METHOD_VEC. */
3386 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3387 add_method (t, 0, *f);
3388 *f = TYPE_METHODS (t);
3389 TYPE_METHODS (t) = implicit_fns;
3391 return virtual_dtor;
3394 /* Subroutine of finish_struct_1. Recursively count the number of fields
3395 in TYPE, including anonymous union members. */
3398 count_fields (fields)
3403 for (x = fields; x; x = TREE_CHAIN (x))
3405 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3406 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3413 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3414 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3417 add_fields_to_vec (fields, field_vec, idx)
3418 tree fields, field_vec;
3422 for (x = fields; x; x = TREE_CHAIN (x))
3424 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3425 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3427 TREE_VEC_ELT (field_vec, idx++) = x;
3432 /* FIELD is a bit-field. We are finishing the processing for its
3433 enclosing type. Issue any appropriate messages and set appropriate
3437 check_bitfield_decl (field)
3440 tree type = TREE_TYPE (field);
3442 /* Invalid bit-field size done by grokfield. */
3443 /* Detect invalid bit-field type. Simply checking if TYPE is
3444 integral is insufficient, as that is the array core of the field
3445 type. If TREE_TYPE (field) is integral, then TYPE must be the same. */
3446 if (DECL_INITIAL (field)
3447 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3449 cp_error_at ("bit-field `%#D' with non-integral type", field);
3450 DECL_INITIAL (field) = NULL;
3453 /* Detect and ignore out of range field width. */
3454 if (DECL_INITIAL (field))
3456 tree w = DECL_INITIAL (field);
3457 register int width = 0;
3459 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3462 /* detect invalid field size. */
3463 if (TREE_CODE (w) == CONST_DECL)
3464 w = DECL_INITIAL (w);
3465 else if (TREE_READONLY_DECL_P (w))
3466 w = decl_constant_value (w);
3468 if (TREE_CODE (w) != INTEGER_CST)
3470 cp_error_at ("bit-field `%D' width not an integer constant",
3472 DECL_INITIAL (field) = NULL_TREE;
3474 else if (width = TREE_INT_CST_LOW (w),
3477 DECL_INITIAL (field) = NULL;
3478 cp_error_at ("negative width in bit-field `%D'", field);
3480 else if (width == 0 && DECL_NAME (field) != 0)
3482 DECL_INITIAL (field) = NULL;
3483 cp_error_at ("zero width for bit-field `%D'", field);
3486 > TYPE_PRECISION (long_long_unsigned_type_node))
3488 /* The backend will dump if you try to use something too
3490 DECL_INITIAL (field) = NULL;
3491 sorry ("bit-fields larger than %d bits",
3492 TYPE_PRECISION (long_long_unsigned_type_node));
3493 cp_error_at (" in declaration of `%D'", field);
3495 else if (width > TYPE_PRECISION (type)
3496 && TREE_CODE (type) != ENUMERAL_TYPE
3497 && TREE_CODE (type) != BOOLEAN_TYPE)
3498 cp_warning_at ("width of `%D' exceeds its type", field);
3499 else if (TREE_CODE (type) == ENUMERAL_TYPE
3500 && ((min_precision (TYPE_MIN_VALUE (type),
3501 TREE_UNSIGNED (type)) > width)
3502 || (min_precision (TYPE_MAX_VALUE (type),
3503 TREE_UNSIGNED (type)) > width)))
3504 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3507 if (DECL_INITIAL (field))
3509 DECL_INITIAL (field) = NULL_TREE;
3510 DECL_FIELD_SIZE (field) = width;
3511 DECL_BIT_FIELD (field) = 1;
3515 #ifdef EMPTY_FIELD_BOUNDARY
3516 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3517 EMPTY_FIELD_BOUNDARY);
3519 #ifdef PCC_BITFIELD_TYPE_MATTERS
3520 if (PCC_BITFIELD_TYPE_MATTERS)
3521 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3528 /* Non-bit-fields are aligned for their type. */
3529 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3532 /* FIELD is a non bit-field. We are finishing the processing for its
3533 enclosing type T. Issue any appropriate messages and set appropriate
3537 check_field_decl (field, t, cant_have_const_ctor,
3538 cant_have_default_ctor, no_const_asn_ref,
3539 any_default_members)
3542 int *cant_have_const_ctor;
3543 int *cant_have_default_ctor;
3544 int *no_const_asn_ref;
3545 int *any_default_members;
3547 tree type = strip_array_types (TREE_TYPE (field));
3549 /* An anonymous union cannot contain any fields which would change
3550 the settings of CANT_HAVE_CONST_CTOR and friends. */
3551 if (ANON_UNION_TYPE_P (type))
3553 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3554 structs. So, we recurse through their fields here. */
3555 else if (ANON_AGGR_TYPE_P (type))
3559 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3560 if (TREE_CODE (field) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3561 check_field_decl (fields, t, cant_have_const_ctor,
3562 cant_have_default_ctor, no_const_asn_ref,
3563 any_default_members);
3565 /* Check members with class type for constructors, destructors,
3567 else if (CLASS_TYPE_P (type))
3569 /* Never let anything with uninheritable virtuals
3570 make it through without complaint. */
3571 abstract_virtuals_error (field, type);
3573 if (TREE_CODE (t) == UNION_TYPE)
3575 if (TYPE_NEEDS_CONSTRUCTING (type))
3576 cp_error_at ("member `%#D' with constructor not allowed in union",
3578 if (TYPE_NEEDS_DESTRUCTOR (type))
3579 cp_error_at ("member `%#D' with destructor not allowed in union",
3581 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3582 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3587 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3588 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3589 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3590 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3593 if (!TYPE_HAS_CONST_INIT_REF (type))
3594 *cant_have_const_ctor = 1;
3596 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3597 *no_const_asn_ref = 1;
3599 if (TYPE_HAS_CONSTRUCTOR (type)
3600 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3601 *cant_have_default_ctor = 1;
3603 if (DECL_INITIAL (field) != NULL_TREE)
3605 /* `build_class_init_list' does not recognize
3607 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3608 cp_error_at ("multiple fields in union `%T' initialized");
3609 *any_default_members = 1;
3612 /* Non-bit-fields are aligned for their type, except packed fields
3613 which require only BITS_PER_UNIT alignment. */
3614 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3615 (DECL_PACKED (field)
3617 : TYPE_ALIGN (TREE_TYPE (field))));
3620 /* Check the data members (both static and non-static), class-scoped
3621 typedefs, etc., appearing in the declaration of T. Issue
3622 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3623 declaration order) of access declarations; each TREE_VALUE in this
3624 list is a USING_DECL.
3626 In addition, set the following flags:
3629 The class is empty, i.e., contains no non-static data members.
3631 CANT_HAVE_DEFAULT_CTOR_P
3632 This class cannot have an implicitly generated default
3635 CANT_HAVE_CONST_CTOR_P
3636 This class cannot have an implicitly generated copy constructor
3637 taking a const reference.
3639 CANT_HAVE_CONST_ASN_REF
3640 This class cannot have an implicitly generated assignment
3641 operator taking a const reference.
3643 All of these flags should be initialized before calling this
3646 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3647 fields can be added by adding to this chain. */
3650 check_field_decls (t, access_decls, empty_p,
3651 cant_have_default_ctor_p, cant_have_const_ctor_p,
3656 int *cant_have_default_ctor_p;
3657 int *cant_have_const_ctor_p;
3658 int *no_const_asn_ref_p;
3663 int any_default_members;
3665 /* First, delete any duplicate fields. */
3666 delete_duplicate_fields (TYPE_FIELDS (t));
3668 /* Assume there are no access declarations. */
3669 *access_decls = NULL_TREE;
3670 /* Assume this class has no pointer members. */
3672 /* Assume none of the members of this class have default
3674 any_default_members = 0;
3676 for (field = &TYPE_FIELDS (t); *field; field = next)
3679 tree type = TREE_TYPE (x);
3681 GNU_xref_member (current_class_name, x);
3683 next = &TREE_CHAIN (x);
3685 if (TREE_CODE (x) == FIELD_DECL)
3687 DECL_PACKED (x) |= TYPE_PACKED (t);
3689 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3690 /* We don't treat zero-width bitfields as making a class
3695 /* The class is non-empty. */
3697 /* The class is not even nearly empty. */
3698 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3702 if (TREE_CODE (x) == USING_DECL)
3704 /* Prune the access declaration from the list of fields. */
3705 *field = TREE_CHAIN (x);
3707 /* Save the access declarations for our caller. */
3708 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3710 /* Since we've reset *FIELD there's no reason to skip to the
3716 if (TREE_CODE (x) == TYPE_DECL
3717 || TREE_CODE (x) == TEMPLATE_DECL)
3720 /* If we've gotten this far, it's a data member, possibly static,
3721 or an enumerator. */
3723 DECL_FIELD_CONTEXT (x) = t;
3725 /* ``A local class cannot have static data members.'' ARM 9.4 */
3726 if (current_function_decl && TREE_STATIC (x))
3727 cp_error_at ("field `%D' in local class cannot be static", x);
3729 /* Perform error checking that did not get done in
3731 if (TREE_CODE (type) == FUNCTION_TYPE)
3733 cp_error_at ("field `%D' invalidly declared function type",
3735 type = build_pointer_type (type);
3736 TREE_TYPE (x) = type;
3738 else if (TREE_CODE (type) == METHOD_TYPE)
3740 cp_error_at ("field `%D' invalidly declared method type", x);
3741 type = build_pointer_type (type);
3742 TREE_TYPE (x) = type;
3744 else if (TREE_CODE (type) == OFFSET_TYPE)
3746 cp_error_at ("field `%D' invalidly declared offset type", x);
3747 type = build_pointer_type (type);
3748 TREE_TYPE (x) = type;
3751 if (type == error_mark_node)
3754 DECL_SAVED_INSNS (x) = 0;
3755 DECL_FIELD_SIZE (x) = 0;
3757 /* When this goes into scope, it will be a non-local reference. */
3758 DECL_NONLOCAL (x) = 1;
3760 if (TREE_CODE (x) == CONST_DECL)
3763 if (TREE_CODE (x) == VAR_DECL)
3765 if (TREE_CODE (t) == UNION_TYPE)
3766 /* Unions cannot have static members. */
3767 cp_error_at ("field `%D' declared static in union", x);
3772 /* Now it can only be a FIELD_DECL. */
3774 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3775 CLASSTYPE_NON_AGGREGATE (t) = 1;
3777 /* If this is of reference type, check if it needs an init.
3778 Also do a little ANSI jig if necessary. */
3779 if (TREE_CODE (type) == REFERENCE_TYPE)
3781 CLASSTYPE_NON_POD_P (t) = 1;
3782 if (DECL_INITIAL (x) == NULL_TREE)
3783 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3785 /* ARM $12.6.2: [A member initializer list] (or, for an
3786 aggregate, initialization by a brace-enclosed list) is the
3787 only way to initialize nonstatic const and reference
3789 *cant_have_default_ctor_p = 1;
3790 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3792 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3795 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3797 cp_warning_at ("non-static reference in class without a constructor", x);
3801 type = strip_array_types (type);
3803 if (TREE_CODE (type) == POINTER_TYPE)
3806 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3807 CLASSTYPE_HAS_MUTABLE (t) = 1;
3809 if (! pod_type_p (type)
3810 /* For some reason, pointers to members are POD types themselves,
3811 but are not allowed in POD structs. Silly. */
3812 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3813 CLASSTYPE_NON_POD_P (t) = 1;
3815 /* If any field is const, the structure type is pseudo-const. */
3816 if (CP_TYPE_CONST_P (type))
3818 C_TYPE_FIELDS_READONLY (t) = 1;
3819 if (DECL_INITIAL (x) == NULL_TREE)
3820 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3822 /* ARM $12.6.2: [A member initializer list] (or, for an
3823 aggregate, initialization by a brace-enclosed list) is the
3824 only way to initialize nonstatic const and reference
3826 *cant_have_default_ctor_p = 1;
3827 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3829 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3832 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3834 cp_warning_at ("non-static const member in class without a constructor", x);
3837 /* A field that is pseudo-const makes the structure likewise. */
3838 else if (IS_AGGR_TYPE (type))
3840 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3841 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3842 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3845 /* We set DECL_C_BIT_FIELD in grokbitfield.
3846 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3847 if (DECL_C_BIT_FIELD (x))
3848 check_bitfield_decl (x);
3850 check_field_decl (x, t,
3851 cant_have_const_ctor_p,
3852 cant_have_default_ctor_p,
3854 &any_default_members);
3857 /* Effective C++ rule 11. */
3858 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3859 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3861 cp_warning ("`%#T' has pointer data members", t);
3863 if (! TYPE_HAS_INIT_REF (t))
3865 cp_warning (" but does not override `%T(const %T&)'", t, t);
3866 if (! TYPE_HAS_ASSIGN_REF (t))
3867 cp_warning (" or `operator=(const %T&)'", t);
3869 else if (! TYPE_HAS_ASSIGN_REF (t))
3870 cp_warning (" but does not override `operator=(const %T&)'", t);
3874 /* Check anonymous struct/anonymous union fields. */
3875 finish_struct_anon (t);
3877 /* We've built up the list of access declarations in reverse order.
3879 *access_decls = nreverse (*access_decls);
3882 /* Return a FIELD_DECL for a pointer-to-virtual-table or
3883 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
3884 field are as indicated. The CLASS_TYPE in which this field occurs
3885 is also indicated. *EMPTY_P is set to a non-zero value by this
3886 function to indicate that a class containing this field is
3890 build_vtbl_or_vbase_field (name, assembler_name, type, class_type,
3893 tree assembler_name;
3900 /* This class is non-empty. */
3903 /* Build the FIELD_DECL. */
3904 field = build_lang_decl (FIELD_DECL, name, type);
3905 DECL_ASSEMBLER_NAME (field) = assembler_name;
3906 DECL_VIRTUAL_P (field) = 1;
3907 DECL_ARTIFICIAL (field) = 1;
3908 DECL_FIELD_CONTEXT (field) = class_type;
3909 DECL_CLASS_CONTEXT (field) = class_type;
3910 DECL_FCONTEXT (field) = class_type;
3911 DECL_SAVED_INSNS (field) = 0;
3912 DECL_FIELD_SIZE (field) = 0;
3913 DECL_ALIGN (field) = TYPE_ALIGN (type);
3919 /* If the empty base field in DECL overlaps with a base of the same type in
3920 NEWDECL, which is either another base field or the first data field of
3921 the class, pad the base just before NEWDECL and return 1. Otherwise,
3925 avoid_overlap (decl, newdecl, empty_p)
3931 if (newdecl == NULL_TREE
3932 || ! types_overlap_p (TREE_TYPE (decl), TREE_TYPE (newdecl)))
3935 for (field = decl; TREE_CHAIN (field) && TREE_CHAIN (field) != newdecl;
3936 field = TREE_CHAIN (field))
3939 DECL_SIZE (field) = integer_one_node;
3940 /* The containing class cannot be empty; this field takes up space. */
3946 /* Build a FIELD_DECL for the base given by BINFO in T. If the new
3947 object is non-empty, clear *EMPTY_P. Otherwise, set *SAW_EMPTY_P.
3948 *BASE_ALIGN is a running maximum of the alignments of any base
3952 build_base_field (t, binfo, empty_p, saw_empty_p, base_align)
3957 unsigned int *base_align;
3959 tree basetype = BINFO_TYPE (binfo);
3962 if (TYPE_SIZE (basetype) == 0)
3963 /* This error is now reported in xref_tag, thus giving better
3964 location information. */
3967 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
3968 DECL_ARTIFICIAL (decl) = 1;
3969 DECL_FIELD_CONTEXT (decl) = DECL_CLASS_CONTEXT (decl) = t;
3970 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3971 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3973 if (flag_new_abi && DECL_SIZE (decl) == integer_zero_node)
3979 /* The containing class is non-empty because it has a non-empty base
3985 /* Brain damage for backwards compatibility. For no good
3986 reason, the old layout_basetypes made every base at least
3987 as large as the alignment for the bases up to that point,
3988 gratuitously wasting space. So we do the same thing
3990 *base_align = MAX (*base_align, DECL_ALIGN (decl));
3992 = size_int (MAX (TREE_INT_CST_LOW (DECL_SIZE (decl)),
3993 (int) (*base_align)));
3999 /* Returns a list of fields to stand in for the base class subobjects
4000 of REC. These fields are later removed by layout_basetypes. */
4003 build_base_fields (rec, empty_p)
4007 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4009 tree base_decls = NULL_TREE;
4010 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
4011 tree decl, nextdecl;
4012 int i, saw_empty = 0;
4013 unsigned int base_align = 0;
4015 /* Under the new ABI, the primary base class is always allocated
4017 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
4021 primary_base = CLASSTYPE_PRIMARY_BINFO (rec);
4022 base_decls = chainon (build_base_field (rec,
4030 /* Now allocate the rest of the bases. */
4031 for (i = 0; i < n_baseclasses; ++i)
4035 /* Under the new ABI, the primary base was already allocated
4036 above, so we don't need to allocate it again here. */
4037 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
4040 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4042 /* A primary virtual base class is allocated just like any other
4043 base class, but a non-primary virtual base is allocated
4044 later, in layout_basetypes. */
4045 if (TREE_VIA_VIRTUAL (base_binfo)
4046 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4049 base_decls = chainon (build_base_field (rec, base_binfo,
4056 /* Reverse the list of fields so we allocate the bases in the proper
4058 base_decls = nreverse (base_decls);
4060 /* In the presence of empty base classes, we run the risk of allocating
4061 two objects of the same class on top of one another. Avoid that. */
4062 if (flag_new_abi && saw_empty)
4063 for (decl = base_decls; decl; decl = TREE_CHAIN (decl))
4065 if (DECL_SIZE (decl) == integer_zero_node)
4067 /* First step through the following bases until we find
4068 an overlap or a non-empty base. */
4069 for (nextdecl = TREE_CHAIN (decl); nextdecl;
4070 nextdecl = TREE_CHAIN (nextdecl))
4072 if (avoid_overlap (decl, nextdecl, empty_p)
4073 || DECL_SIZE (nextdecl) != integer_zero_node)
4077 /* If we're still looking, also check against the first
4079 for (nextdecl = TYPE_FIELDS (rec);
4080 nextdecl && TREE_CODE (nextdecl) != FIELD_DECL;
4081 nextdecl = TREE_CHAIN (nextdecl))
4083 avoid_overlap (decl, nextdecl, empty_p);
4091 /* Go through the TYPE_METHODS of T issuing any appropriate
4092 diagnostics, figuring out which methods override which other
4093 methods, and so forth. */
4101 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4103 GNU_xref_member (current_class_name, x);
4105 /* If this was an evil function, don't keep it in class. */
4106 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4109 /* Do both of these, even though they're in the same union;
4110 if the insn `r' member and the size `i' member are
4111 different sizes, as on the alpha, the larger of the two
4112 will end up with garbage in it. */
4113 DECL_SAVED_INSNS (x) = 0;
4114 DECL_FIELD_SIZE (x) = 0;
4116 check_for_override (x, t);
4117 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4118 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4120 /* The name of the field is the original field name
4121 Save this in auxiliary field for later overloading. */
4122 if (DECL_VINDEX (x))
4124 TYPE_POLYMORPHIC_P (t) = 1;
4125 if (DECL_PURE_VIRTUAL_P (x))
4126 CLASSTYPE_PURE_VIRTUALS (t)
4127 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4132 /* Remove all zero-width bit-fields from T. */
4135 remove_zero_width_bit_fields (t)
4140 fieldsp = &TYPE_FIELDS (t);
4143 if (TREE_CODE (*fieldsp) == FIELD_DECL
4144 && DECL_C_BIT_FIELD (*fieldsp)
4145 && DECL_INITIAL (*fieldsp))
4146 *fieldsp = TREE_CHAIN (*fieldsp);
4148 fieldsp = &TREE_CHAIN (*fieldsp);
4152 /* Check the validity of the bases and members declared in T. Add any
4153 implicitly-generated functions (like copy-constructors and
4154 assignment operators). Compute various flag bits (like
4155 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4156 level: i.e., independently of the ABI in use. */
4159 check_bases_and_members (t, empty_p)
4163 /* Nonzero if we are not allowed to generate a default constructor
4165 int cant_have_default_ctor;
4166 /* Nonzero if the implicitly generated copy constructor should take
4167 a non-const reference argument. */
4168 int cant_have_const_ctor;
4169 /* Nonzero if the the implicitly generated assignment operator
4170 should take a non-const reference argument. */
4171 int no_const_asn_ref;
4174 /* By default, we use const reference arguments and generate default
4176 cant_have_default_ctor = 0;
4177 cant_have_const_ctor = 0;
4178 no_const_asn_ref = 0;
4180 /* Assume that the class is nearly empty; we'll clear this flag if
4181 it turns out not to be nearly empty. */
4182 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4184 /* Check all the base-classes. */
4185 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4188 /* Check all the data member declarations. */
4189 check_field_decls (t, &access_decls, empty_p,
4190 &cant_have_default_ctor,
4191 &cant_have_const_ctor,
4194 /* Check all the method declarations. */
4197 /* A nearly-empty class has to be vptr-containing; a nearly empty
4198 class contains just a vptr. */
4199 if (!TYPE_CONTAINS_VPTR_P (t))
4200 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4202 /* Do some bookkeeping that will guide the generation of implicitly
4203 declared member functions. */
4204 TYPE_HAS_COMPLEX_INIT_REF (t)
4205 |= (TYPE_HAS_INIT_REF (t)
4206 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4207 || TYPE_POLYMORPHIC_P (t));
4208 TYPE_NEEDS_CONSTRUCTING (t)
4209 |= (TYPE_HAS_CONSTRUCTOR (t)
4210 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4211 || TYPE_POLYMORPHIC_P (t));
4212 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4213 || TYPE_POLYMORPHIC_P (t));
4214 CLASSTYPE_NON_POD_P (t)
4215 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4216 || TYPE_HAS_ASSIGN_REF (t));
4217 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4218 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4219 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4221 /* Synthesize any needed methods. Note that methods will be synthesized
4222 for anonymous unions; grok_x_components undoes that. */
4223 add_implicitly_declared_members (t, cant_have_default_ctor,
4224 cant_have_const_ctor,
4227 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4228 finish_struct_methods (t);
4230 /* Process the access-declarations. We wait until now to do this
4231 because handle_using_decls requires that the CLASSTYPE_METHOD_VEC
4232 be set up correctly. */
4233 while (access_decls)
4235 handle_using_decl (TREE_VALUE (access_decls), t);
4236 access_decls = TREE_CHAIN (access_decls);
4240 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4241 accordingly, and, if necessary, add the TYPE_VFIELD to the
4242 TYPE_FIELDS list. */
4245 create_vtable_ptr (t, empty_p, has_virtual_p,
4246 new_virtuals_p, overridden_virtuals_p)
4250 tree *new_virtuals_p;
4251 tree *overridden_virtuals_p;
4255 /* Loop over the virtual functions, adding them to our various
4257 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4258 if (DECL_VINDEX (fn))
4259 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4260 has_virtual_p, fn, t);
4262 /* Even if there weren't any new virtual functions, we might need a
4263 new virtual function table if we're supposed to include vptrs in
4264 all classes that need them. */
4265 if (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())
4266 start_vtable (t, has_virtual_p);
4268 /* If we couldn't find an appropriate base class, create a new field
4270 if (*has_virtual_p && !TYPE_VFIELD (t))
4272 /* We build this decl with vtbl_ptr_type_node, which is a
4273 `vtable_entry_type*'. It might seem more precise to use
4274 `vtable_entry_type (*)[N]' where N is the number of firtual
4275 functions. However, that would require the vtable pointer in
4276 base classes to have a different type than the vtable pointer
4277 in derived classes. We could make that happen, but that
4278 still wouldn't solve all the problems. In particular, the
4279 type-based alias analysis code would decide that assignments
4280 to the base class vtable pointer can't alias assignments to
4281 the derived class vtable pointer, since they have different
4282 types. Thus, in an derived class destructor, where the base
4283 class constructor was inlined, we could generate bad code for
4284 setting up the vtable pointer.
4286 Therefore, we use one type for all vtable pointers. We still
4287 use a type-correct type; it's just doesn't indicate the array
4288 bounds. That's better than using `void*' or some such; it's
4289 cleaner, and it let's the alias analysis code know that these
4290 stores cannot alias stores to void*! */
4292 = build_vtbl_or_vbase_field (get_vfield_name (t),
4293 get_identifier (VFIELD_BASE),
4298 /* Add the new field to the list of fields in this class. */
4300 /* In the old ABI, the vtable pointer goes at the end of the
4302 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), TYPE_VFIELD (t));
4305 /* But in the new ABI, the vtable pointer is the first thing
4307 TYPE_FIELDS (t) = chainon (TYPE_VFIELD (t), TYPE_FIELDS (t));
4308 /* If there were any baseclasses, they can't possibly be at
4309 offset zero any more, because that's where the vtable
4310 pointer is. So, converting to a base class is going to
4312 if (CLASSTYPE_N_BASECLASSES (t))
4313 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4316 /* We can't yet add this new field to the list of all virtual
4317 function table pointers in this class. The
4318 modify_all_vtables function depends on this not being done.
4319 So, it is done later, in finish_struct_1. */
4323 /* Fixup the inline function given by INFO now that the class is
4327 fixup_pending_inline (info)
4328 struct pending_inline *info;
4333 tree fn = info->fndecl;
4335 args = DECL_ARGUMENTS (fn);
4338 DECL_CONTEXT (args) = fn;
4339 args = TREE_CHAIN (args);
4344 /* Fixup the inline methods and friends in TYPE now that TYPE is
4348 fixup_inline_methods (type)
4351 tree method = TYPE_METHODS (type);
4353 if (method && TREE_CODE (method) == TREE_VEC)
4355 if (TREE_VEC_ELT (method, 1))
4356 method = TREE_VEC_ELT (method, 1);
4357 else if (TREE_VEC_ELT (method, 0))
4358 method = TREE_VEC_ELT (method, 0);
4360 method = TREE_VEC_ELT (method, 2);
4363 /* Do inline member functions. */
4364 for (; method; method = TREE_CHAIN (method))
4365 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
4368 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4370 method = TREE_CHAIN (method))
4371 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
4372 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4375 /* Called from propagate_binfo_offsets via dfs_walk. */
4378 dfs_propagate_binfo_offsets (binfo, data)
4382 tree offset = (tree) data;
4384 /* Update the BINFO_OFFSET for this base. */
4385 BINFO_OFFSET (binfo)
4386 = size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), offset);
4388 SET_BINFO_MARKED (binfo);
4393 /* Add OFFSET to all base types of BINFO which is a base in the
4394 hierarchy dominated by T.
4396 OFFSET, which is a type offset, is number of bytes.
4398 Note that we don't have to worry about having two paths to the
4399 same base type, since this type owns its association list. */
4402 propagate_binfo_offsets (binfo, offset)
4407 dfs_propagate_binfo_offsets,
4408 dfs_skip_nonprimary_vbases_unmarkedp,
4412 dfs_skip_nonprimary_vbases_markedp,
4416 /* Remove *FIELD (which corresponds to the base given by BINFO) from
4417 the field list for T. */
4420 remove_base_field (t, binfo, field)
4425 tree basetype = BINFO_TYPE (binfo);
4428 my_friendly_assert (TREE_TYPE (*field) == basetype, 23897);
4430 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4431 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4435 = size_int (CEIL (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (*field)),
4437 propagate_binfo_offsets (binfo, offset);
4439 /* Remove this field. */
4440 *field = TREE_CHAIN (*field);
4443 /* Remove the FIELD_DECLs created for T's base classes in
4444 build_base_fields. Simultaneously, update BINFO_OFFSET for all the
4445 bases, except for non-primary virtual baseclasses. */
4448 remove_base_fields (t)
4454 /* Now propagate offset information throughout the lattice.
4455 Simultaneously, remove the temporary FIELD_DECLS we created in
4456 build_base_fields to refer to base types. */
4457 field = &TYPE_FIELDS (t);
4458 if (TYPE_VFIELD (t) == *field)
4460 /* If this class did not have a primary base, we create a
4461 virtual function table pointer. It will be the first thing
4462 in the class, under the new ABI. Skip it; the base fields
4464 my_friendly_assert (flag_new_abi
4465 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t),
4467 field = &TREE_CHAIN (*field);
4470 /* Under the new ABI, the primary base is always allocated first. */
4471 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4472 remove_base_field (t, CLASSTYPE_PRIMARY_BINFO (t), field);
4474 /* Now remove the rest of the bases. */
4475 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
4479 /* Under the new ABI, we've already removed the primary base
4481 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (t))
4484 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4486 /* We treat a primary virtual base class just like an ordinary base
4487 class. But, non-primary virtual bases are laid out later. */
4488 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4491 remove_base_field (t, binfo, field);
4495 /* Called via dfs_walk from layout_virtual bases. */
4498 dfs_set_offset_for_shared_vbases (binfo, data)
4502 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
4504 /* Update the shared copy. */
4507 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4508 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
4514 /* Called via dfs_walk from layout_virtual bases. */
4517 dfs_set_offset_for_unshared_vbases (binfo, data)
4521 /* If this is a virtual base, make sure it has the same offset as
4522 the shared copy. If it's a primary base, then we know it's
4524 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4526 tree t = (tree) data;
4530 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
4531 offset = ssize_binop (MINUS_EXPR,
4532 BINFO_OFFSET (vbase),
4533 BINFO_OFFSET (binfo));
4534 propagate_binfo_offsets (binfo, offset);
4540 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4541 TYPE_ALIGN and TYPE_SIZE for T. */
4544 layout_virtual_bases (t)
4550 /* DSIZE is the size of the class without the virtual bases. */
4551 dsize = TREE_INT_CST_LOW (TYPE_SIZE (t));
4552 /* Make every class have alignment of at least one. */
4553 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4555 /* Go through the virtual bases, allocating space for each virtual
4556 base that is not already a primary base class. */
4557 for (vbase = CLASSTYPE_VBASECLASSES (t);
4559 vbase = TREE_CHAIN (vbase))
4560 if (!BINFO_VBASE_PRIMARY_P (vbase))
4562 /* This virtual base is not a primary base of any class in the
4563 hierarchy, so we have to add space for it. */
4565 unsigned int desired_align;
4567 basetype = BINFO_TYPE (vbase);
4568 desired_align = TYPE_ALIGN (basetype);
4569 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4571 /* Add padding so that we can put the virtual base class at an
4572 appropriately aligned offset. */
4573 dsize = CEIL (dsize, desired_align) * desired_align;
4574 /* And compute the offset of the virtual base. */
4575 propagate_binfo_offsets (vbase,
4576 size_int (CEIL (dsize, BITS_PER_UNIT)));
4577 /* Every virtual baseclass takes a least a UNIT, so that we can
4578 take it's address and get something different for each base. */
4579 dsize += MAX (BITS_PER_UNIT,
4580 TREE_INT_CST_LOW (CLASSTYPE_SIZE (basetype)));
4583 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4584 BINFO_OFFSET set correctly. Those we just allocated certainly
4585 will. The others are primary baseclasses; we walk the hierarchy
4586 to find the primary copies and update the shared copy. */
4587 dfs_walk (TYPE_BINFO (t),
4588 dfs_set_offset_for_shared_vbases,
4589 dfs_unmarked_real_bases_queue_p,
4592 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4593 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4594 bases and their direct and indirect bases. The ambiguity checks
4595 in get_base_distance depend on the BINFO_OFFSETs being set
4597 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4599 /* Now, make sure that the total size of the type is a multiple of
4601 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4602 TYPE_SIZE (t) = size_int (dsize);
4603 TYPE_SIZE_UNIT (t) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (t),
4604 size_int (BITS_PER_UNIT));
4607 /* Finish the work of layout_record, now taking virtual bases into account.
4608 Also compute the actual offsets that our base classes will have.
4609 This must be performed after the fields are laid out, since virtual
4610 baseclasses must lay down at the end of the record. */
4613 layout_basetypes (rec)
4618 #ifdef STRUCTURE_SIZE_BOUNDARY
4619 /* Packed structures don't need to have minimum size. */
4620 if (! TYPE_PACKED (rec))
4621 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), STRUCTURE_SIZE_BOUNDARY);
4624 /* Remove the FIELD_DECLs we created for baseclasses in
4625 build_base_fields. Simultaneously, update the BINFO_OFFSETs for
4626 everything in the hierarcy except non-primary virtual bases. */
4627 remove_base_fields (rec);
4629 /* Allocate the virtual base classes. */
4630 layout_virtual_bases (rec);
4632 /* Get all the virtual base types that this type uses. The
4633 TREE_VALUE slot holds the virtual baseclass type. Note that
4634 get_vbase_types makes copies of the virtual base BINFOs, so that
4635 the vbase_types are unshared. */
4636 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
4637 vbase_types = TREE_CHAIN (vbase_types))
4640 tree basetype = BINFO_TYPE (vbase_types);
4641 if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
4642 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4647 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4648 BINFO_OFFSETs for all of the base-classes. Position the vtable
4652 layout_class_type (t, empty_p, has_virtual_p,
4653 new_virtuals_p, overridden_virtuals_p)
4657 tree *new_virtuals_p;
4658 tree *overridden_virtuals_p;
4660 /* If possible, we reuse the virtual function table pointer from one
4661 of our base classes. */
4662 determine_primary_base (t, has_virtual_p);
4664 /* Add pointers to all of our virtual base-classes. */
4665 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (t, empty_p),
4667 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4668 TYPE_FIELDS (t) = chainon (build_base_fields (t, empty_p),
4671 /* Create a pointer to our virtual function table. */
4672 create_vtable_ptr (t, empty_p, has_virtual_p,
4673 new_virtuals_p, overridden_virtuals_p);
4675 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4676 we have to save this before we start modifying
4677 TYPE_NONCOPIED_PARTS. */
4678 fixup_inline_methods (t);
4680 /* We make all structures have at least one element, so that they
4681 have non-zero size. The field that we add here is fake, in the
4682 sense that, for example, we don't want people to be able to
4683 initialize it later. So, we add it just long enough to let the
4684 back-end lay out the type, and then remove it. */
4687 tree decl = build_lang_decl
4688 (FIELD_DECL, NULL_TREE, char_type_node);
4689 TREE_CHAIN (decl) = TYPE_FIELDS (t);
4690 TYPE_FIELDS (t) = decl;
4691 TYPE_NONCOPIED_PARTS (t)
4692 = tree_cons (NULL_TREE, decl, TYPE_NONCOPIED_PARTS (t));
4693 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4696 /* Let the back-end lay out the type. Note that at this point we
4697 have only included non-virtual base-classes; we will lay out the
4698 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4699 this call are not necessarily correct; they are just the size and
4700 alignment when no virtual base clases are used. */
4703 /* If we added an extra field to make this class non-empty, remove
4706 TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
4708 /* Delete all zero-width bit-fields from the list of fields. Now
4709 that the type is laid out they are no longer important. */
4710 remove_zero_width_bit_fields (t);
4712 /* Remember the size and alignment of the class before adding
4713 the virtual bases. */
4714 if (*empty_p && flag_new_abi)
4715 CLASSTYPE_SIZE (t) = integer_zero_node;
4716 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
4717 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
4718 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4720 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4721 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4723 /* Set the TYPE_DECL for this type to contain the right
4724 value for DECL_OFFSET, so that we can use it as part
4725 of a COMPONENT_REF for multiple inheritance. */
4726 layout_decl (TYPE_MAIN_DECL (t), 0);
4728 /* Now fix up any virtual base class types that we left lying
4729 around. We must get these done before we try to lay out the
4730 virtual function table. */
4731 if (CLASSTYPE_N_BASECLASSES (t))
4732 /* layout_basetypes will remove the base subobject fields. */
4733 layout_basetypes (t);
4736 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
4737 (or C++ class declaration).
4739 For C++, we must handle the building of derived classes.
4740 Also, C++ allows static class members. The way that this is
4741 handled is to keep the field name where it is (as the DECL_NAME
4742 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
4743 of the field. layout_record and layout_union will know about this.
4745 More C++ hair: inline functions have text in their
4746 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
4747 meaningful tree structure. After the struct has been laid out, set
4748 things up so that this can happen.
4750 And still more: virtual functions. In the case of single inheritance,
4751 when a new virtual function is seen which redefines a virtual function
4752 from the base class, the new virtual function is placed into
4753 the virtual function table at exactly the same address that
4754 it had in the base class. When this is extended to multiple
4755 inheritance, the same thing happens, except that multiple virtual
4756 function tables must be maintained. The first virtual function
4757 table is treated in exactly the same way as in the case of single
4758 inheritance. Additional virtual function tables have different
4759 DELTAs, which tell how to adjust `this' to point to the right thing.
4761 ATTRIBUTES is the set of decl attributes to be applied, if any. */
4769 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
4770 a FUNCTION_DECL. Each of these functions is a virtual function
4771 declared in T that does not override any virtual function from a
4773 tree new_virtuals = NULL_TREE;
4774 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
4775 except that each declaration here overrides the declaration from
4777 tree overridden_virtuals = NULL_TREE;
4784 if (IS_AGGR_TYPE (t))
4785 cp_error ("redefinition of `%#T'", t);
4787 my_friendly_abort (172);
4792 GNU_xref_decl (current_function_decl, t);
4794 /* If this type was previously laid out as a forward reference,
4795 make sure we lay it out again. */
4796 TYPE_SIZE (t) = NULL_TREE;
4797 CLASSTYPE_GOT_SEMICOLON (t) = 0;
4798 CLASSTYPE_VFIELD_PARENT (t) = -1;
4800 CLASSTYPE_RTTI (t) = NULL_TREE;
4802 /* Do end-of-class semantic processing: checking the validity of the
4803 bases and members and add implicitly generated methods. */
4804 check_bases_and_members (t, &empty);
4806 /* Layout the class itself. */
4807 layout_class_type (t, &empty, &has_virtual,
4808 &new_virtuals, &overridden_virtuals);
4810 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4814 vbases = CLASSTYPE_VBASECLASSES (t);
4817 /* Now fixup overrides of all functions in vtables from all
4818 direct or indirect virtual base classes. */
4819 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
4820 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
4822 for (i = 0; i < n_baseclasses; i++)
4824 tree base_binfo = TREE_VEC_ELT (binfos, i);
4825 tree basetype = BINFO_TYPE (base_binfo);
4828 vbases = CLASSTYPE_VBASECLASSES (basetype);
4832 tree basetype_vbase;
4835 = find_vbase_instance (BINFO_TYPE (vbases), t);
4837 = find_vbase_instance (BINFO_TYPE (vbases), basetype);
4839 merge_overrides (vbase, basetype_vbase, 1, t);
4840 vbases = TREE_CHAIN (vbases);
4846 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4847 might need to know it for setting up the offsets in the vtable
4848 (or in thunks) below. */
4849 vfield = TYPE_VFIELD (t);
4850 if (vfield != NULL_TREE
4851 && DECL_FIELD_CONTEXT (vfield) != t)
4853 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4854 tree offset = BINFO_OFFSET (binfo);
4856 vfield = copy_node (vfield);
4857 copy_lang_decl (vfield);
4859 if (! integer_zerop (offset))
4860 offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4861 DECL_FIELD_CONTEXT (vfield) = t;
4862 DECL_CLASS_CONTEXT (vfield) = t;
4863 DECL_FIELD_BITPOS (vfield)
4864 = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4865 TYPE_VFIELD (t) = vfield;
4869 = modify_all_vtables (t, &has_virtual, nreverse (overridden_virtuals));
4871 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4874 /* Now fixup any virtual function entries from virtual bases
4875 that have different deltas. This has to come after we do the
4876 overridden virtuals. */
4877 vbases = CLASSTYPE_VBASECLASSES (t);
4882 /* We might be able to shorten the amount of work we do by
4883 only doing this for vtables that come from virtual bases
4884 that have differing offsets, but don't want to miss any
4886 vbase = find_vbase_instance (BINFO_TYPE (vbases), t);
4887 dfs_walk (vbase, dfs_fixup_vtable_deltas, dfs_skip_vbases, t);
4888 vbases = TREE_CHAIN (vbases);
4892 /* If necessary, create the vtable for this class. */
4894 || overridden_virtuals
4895 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
4897 new_virtuals = nreverse (new_virtuals);
4898 /* We must enter these virtuals into the table. */
4899 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4901 if (! CLASSTYPE_COM_INTERFACE (t))
4903 /* The second slot is for the tdesc pointer when thunks
4905 if (flag_vtable_thunks)
4906 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
4908 /* The first slot is for the rtti offset. */
4909 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
4911 set_rtti_entry (new_virtuals,
4912 convert (ssizetype, integer_zero_node), t);
4914 build_vtable (NULL_TREE, t);
4916 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4917 /* Here we know enough to change the type of our virtual
4918 function table, but we will wait until later this function. */
4919 build_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
4921 /* If this type has basetypes with constructors, then those
4922 constructors might clobber the virtual function table. But
4923 they don't if the derived class shares the exact vtable of the base
4926 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4928 /* If we didn't need a new vtable, see if we should copy one from
4930 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4932 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
4934 /* This class contributes nothing new to the virtual function
4935 table. However, it may have declared functions which
4936 went into the virtual function table "inherited" from the
4937 base class. If so, we grab a copy of those updated functions,
4938 and pretend they are ours. */
4940 /* See if we should steal the virtual info from base class. */
4941 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4942 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4943 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4944 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4945 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4946 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4949 if (TYPE_CONTAINS_VPTR_P (t))
4951 if (TYPE_BINFO_VTABLE (t))
4952 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
4954 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4955 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
4958 CLASSTYPE_VSIZE (t) = has_virtual;
4959 /* Entries for virtual functions defined in the primary base are
4960 followed by entries for new functions unique to this class. */
4961 TYPE_BINFO_VIRTUALS (t)
4962 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
4963 /* Finally, add entries for functions that override virtuals
4964 from non-primary bases. */
4965 TYPE_BINFO_VIRTUALS (t)
4966 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
4969 /* Now lay out the virtual function table. */
4972 /* Use size_int so values are memoized in common cases. */
4976 itype = size_int (has_virtual);
4977 itype = size_binop (PLUS_EXPR,
4979 num_extra_vtbl_entries (TYPE_BINFO (t)));
4980 atype = build_cplus_array_type (vtable_entry_type,
4981 build_index_type (itype));
4982 layout_type (atype);
4984 /* We may have to grow the vtable. */
4985 if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4987 TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4988 DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4989 layout_decl (TYPE_BINFO_VTABLE (t), 0);
4990 /* At one time the vtable info was grabbed 2 words at a time. This
4991 fails on sparc unless you have 8-byte alignment. (tiemann) */
4992 DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4993 = MAX (TYPE_ALIGN (double_type_node),
4994 DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4998 /* If we created a new vtbl pointer for this class, add it to the
5000 if (TYPE_VFIELD (t) && CLASSTYPE_VFIELD_PARENT (t) == -1)
5001 CLASSTYPE_VFIELDS (t)
5002 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5004 finish_struct_bits (t);
5006 /* Complete the rtl for any static member objects of the type we're
5008 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5010 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5011 && TREE_TYPE (x) == t)
5013 DECL_MODE (x) = TYPE_MODE (t);
5014 make_decl_rtl (x, NULL, 0);
5018 /* Done with FIELDS...now decide whether to sort these for
5019 faster lookups later.
5021 The C front-end only does this when n_fields > 15. We use
5022 a smaller number because most searches fail (succeeding
5023 ultimately as the search bores through the inheritance
5024 hierarchy), and we want this failure to occur quickly. */
5026 n_fields = count_fields (TYPE_FIELDS (t));
5029 tree field_vec = make_tree_vec (n_fields);
5030 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5031 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5032 (int (*)(const void *, const void *))field_decl_cmp);
5033 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5034 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5035 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5038 if (TYPE_HAS_CONSTRUCTOR (t))
5040 tree vfields = CLASSTYPE_VFIELDS (t);
5044 /* Mark the fact that constructor for T
5045 could affect anybody inheriting from T
5046 who wants to initialize vtables for VFIELDS's type. */
5047 if (VF_DERIVED_VALUE (vfields))
5048 TREE_ADDRESSABLE (vfields) = 1;
5049 vfields = TREE_CHAIN (vfields);
5053 if (CLASSTYPE_VSIZE (t) != 0)
5055 /* In addition to this one, all the other vfields should be listed. */
5056 /* Before that can be done, we have to have FIELD_DECLs for them, and
5057 a place to find them. */
5058 TYPE_NONCOPIED_PARTS (t)
5059 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5060 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5062 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5063 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5064 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5068 /* Make the rtl for any new vtables we have created, and unmark
5069 the base types we marked. */
5071 hack_incomplete_structures (t);
5073 if (warn_overloaded_virtual)
5076 maybe_suppress_debug_info (t);
5078 /* Finish debugging output for this type. */
5079 rest_of_type_compilation (t, toplevel_bindings_p ());
5082 /* When T was built up, the member declarations were added in reverse
5083 order. Rearrange them to declaration order. */
5086 unreverse_member_declarations (t)
5093 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5094 reverse order. Put them in declaration order now. */
5095 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5096 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5098 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5099 reverse order, so we can't just use nreverse. */
5101 for (x = TYPE_FIELDS (t);
5102 x && TREE_CODE (x) != TYPE_DECL;
5105 next = TREE_CHAIN (x);
5106 TREE_CHAIN (x) = prev;
5111 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5113 TYPE_FIELDS (t) = prev;
5118 finish_struct (t, attributes)
5121 /* Now that we've got all the field declarations, reverse everything
5123 unreverse_member_declarations (t);
5125 cplus_decl_attributes (t, attributes, NULL_TREE);
5127 if (processing_template_decl)
5129 finish_struct_methods (t);
5130 TYPE_SIZE (t) = integer_zero_node;
5133 finish_struct_1 (t);
5135 TYPE_BEING_DEFINED (t) = 0;
5137 if (current_class_type)
5140 error ("trying to finish struct, but kicked out due to previous parse errors.");
5142 if (processing_template_decl)
5144 tree scope = current_scope ();
5145 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5146 add_tree (build_min (TAG_DEFN, t));
5152 /* Return the dynamic type of INSTANCE, if known.
5153 Used to determine whether the virtual function table is needed
5156 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5157 of our knowledge of its type. *NONNULL should be initialized
5158 before this function is called. */
5161 fixed_type_or_null (instance, nonnull)
5165 switch (TREE_CODE (instance))
5168 /* Check that we are not going through a cast of some sort. */
5169 if (TREE_TYPE (instance)
5170 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5171 instance = TREE_OPERAND (instance, 0);
5172 /* fall through... */
5174 /* This is a call to a constructor, hence it's never zero. */
5175 if (TREE_HAS_CONSTRUCTOR (instance))
5179 return TREE_TYPE (instance);
5184 /* This is a call to a constructor, hence it's never zero. */
5185 if (TREE_HAS_CONSTRUCTOR (instance))
5189 return TREE_TYPE (instance);
5191 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5198 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5199 /* Propagate nonnull. */
5200 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5201 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5202 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5207 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5212 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5215 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5219 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5220 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5224 return TREE_TYPE (TREE_TYPE (instance));
5226 /* fall through... */
5229 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5233 return TREE_TYPE (instance);
5237 if (instance == current_class_ptr
5238 && flag_this_is_variable <= 0)
5240 /* Normally, 'this' must be non-null. */
5241 if (flag_this_is_variable == 0)
5244 /* <0 means we're in a constructor and we know our type. */
5245 if (flag_this_is_variable < 0)
5246 return TREE_TYPE (TREE_TYPE (instance));
5248 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5249 /* Reference variables should be references to objects. */
5259 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5260 to the static type. We also handle the case where INSTANCE is really
5263 Used to determine whether the virtual function table is needed
5266 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5267 of our knowledge of its type. *NONNULL should be initialized
5268 before this function is called. */
5271 resolves_to_fixed_type_p (instance, nonnull)
5275 tree t = TREE_TYPE (instance);
5276 tree fixed = fixed_type_or_null (instance, nonnull);
5277 if (fixed == NULL_TREE)
5279 if (POINTER_TYPE_P (t))
5281 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
5286 init_class_processing ()
5288 current_class_depth = 0;
5289 current_class_stack_size = 10;
5291 = (class_stack_node_t) xmalloc (current_class_stack_size
5292 * sizeof (struct class_stack_node));
5294 access_default_node = build_int_2 (0, 0);
5295 access_public_node = build_int_2 (1, 0);
5296 access_protected_node = build_int_2 (2, 0);
5297 access_private_node = build_int_2 (3, 0);
5298 access_default_virtual_node = build_int_2 (4, 0);
5299 access_public_virtual_node = build_int_2 (5, 0);
5300 access_protected_virtual_node = build_int_2 (6, 0);
5301 access_private_virtual_node = build_int_2 (7, 0);
5304 /* Set current scope to NAME. CODE tells us if this is a
5305 STRUCT, UNION, or ENUM environment.
5307 NAME may end up being NULL_TREE if this is an anonymous or
5308 late-bound struct (as in "struct { ... } foo;") */
5310 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5311 appropriate values, found by looking up the type definition of
5314 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5315 which can be seen locally to the class. They are shadowed by
5316 any subsequent local declaration (including parameter names).
5318 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5319 which have static meaning (i.e., static members, static
5320 member functions, enum declarations, etc).
5322 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5323 which can be seen locally to the class (as in 1), but
5324 know that we are doing this for declaration purposes
5325 (i.e. friend foo::bar (int)).
5327 So that we may avoid calls to lookup_name, we cache the _TYPE
5328 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5330 For multiple inheritance, we perform a two-pass depth-first search
5331 of the type lattice. The first pass performs a pre-order search,
5332 marking types after the type has had its fields installed in
5333 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5334 unmarks the marked types. If a field or member function name
5335 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5336 that name becomes `error_mark_node'. */
5339 pushclass (type, modify)
5343 type = TYPE_MAIN_VARIANT (type);
5345 /* Make sure there is enough room for the new entry on the stack. */
5346 if (current_class_depth + 1 >= current_class_stack_size)
5348 current_class_stack_size *= 2;
5350 = (class_stack_node_t) xrealloc (current_class_stack,
5351 current_class_stack_size
5352 * sizeof (struct class_stack_node));
5355 /* Insert a new entry on the class stack. */
5356 current_class_stack[current_class_depth].name = current_class_name;
5357 current_class_stack[current_class_depth].type = current_class_type;
5358 current_class_stack[current_class_depth].access = current_access_specifier;
5359 current_class_stack[current_class_depth].names_used = 0;
5360 current_class_depth++;
5362 /* Now set up the new type. */
5363 current_class_name = TYPE_NAME (type);
5364 if (TREE_CODE (current_class_name) == TYPE_DECL)
5365 current_class_name = DECL_NAME (current_class_name);
5366 current_class_type = type;
5368 /* By default, things in classes are private, while things in
5369 structures or unions are public. */
5370 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5371 ? access_private_node
5372 : access_public_node);
5374 if (previous_class_type != NULL_TREE
5375 && (type != previous_class_type
5376 || TYPE_SIZE (previous_class_type) == NULL_TREE)
5377 && current_class_depth == 1)
5379 /* Forcibly remove any old class remnants. */
5380 invalidate_class_lookup_cache ();
5383 /* If we're about to enter a nested class, clear
5384 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5385 if (modify && current_class_depth > 1)
5386 clear_identifier_class_values ();
5391 if (CLASSTYPE_TEMPLATE_INFO (type))
5392 overload_template_name (type);
5397 if (type != previous_class_type || current_class_depth > 1)
5398 push_class_decls (type);
5403 /* We are re-entering the same class we just left, so we
5404 don't have to search the whole inheritance matrix to find
5405 all the decls to bind again. Instead, we install the
5406 cached class_shadowed list, and walk through it binding
5407 names and setting up IDENTIFIER_TYPE_VALUEs. */
5408 set_class_shadows (previous_class_values);
5409 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5411 tree id = TREE_PURPOSE (item);
5412 tree decl = TREE_TYPE (item);
5414 push_class_binding (id, decl);
5415 if (TREE_CODE (decl) == TYPE_DECL)
5416 set_identifier_type_value (id, TREE_TYPE (decl));
5418 unuse_fields (type);
5421 storetags (CLASSTYPE_TAGS (type));
5425 /* When we exit a toplevel class scope, we save the
5426 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5427 reenter the class. Here, we've entered some other class, so we
5428 must invalidate our cache. */
5431 invalidate_class_lookup_cache ()
5435 /* This code can be seen as a cache miss. When we've cached a
5436 class' scope's bindings and we can't use them, we need to reset
5437 them. This is it! */
5438 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5439 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5441 previous_class_type = NULL_TREE;
5444 /* Get out of the current class scope. If we were in a class scope
5445 previously, that is the one popped to. */
5451 /* Since poplevel_class does the popping of class decls nowadays,
5452 this really only frees the obstack used for these decls. */
5455 current_class_depth--;
5456 current_class_name = current_class_stack[current_class_depth].name;
5457 current_class_type = current_class_stack[current_class_depth].type;
5458 current_access_specifier = current_class_stack[current_class_depth].access;
5459 if (current_class_stack[current_class_depth].names_used)
5460 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5463 /* Returns 1 if current_class_type is either T or a nested type of T. */
5466 currently_open_class (t)
5470 if (t == current_class_type)
5472 for (i = 0; i < current_class_depth; ++i)
5473 if (current_class_stack [i].type == t)
5478 /* When entering a class scope, all enclosing class scopes' names with
5479 static meaning (static variables, static functions, types and enumerators)
5480 have to be visible. This recursive function calls pushclass for all
5481 enclosing class contexts until global or a local scope is reached.
5482 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5483 formal of the same name. */
5486 push_nested_class (type, modify)
5492 /* A namespace might be passed in error cases, like A::B:C. */
5493 if (type == NULL_TREE
5494 || type == error_mark_node
5495 || TREE_CODE (type) == NAMESPACE_DECL
5496 || ! IS_AGGR_TYPE (type)
5497 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5498 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
5501 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5503 if (context && CLASS_TYPE_P (context))
5504 push_nested_class (context, 2);
5505 pushclass (type, modify);
5508 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5513 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5516 if (context && CLASS_TYPE_P (context))
5517 pop_nested_class ();
5520 /* Set global variables CURRENT_LANG_NAME to appropriate value
5521 so that behavior of name-mangling machinery is correct. */
5524 push_lang_context (name)
5527 *current_lang_stack++ = current_lang_name;
5528 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5529 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5531 size_t old_size = VARRAY_SIZE (current_lang_base);
5533 VARRAY_GROW (current_lang_base, old_size + 10);
5534 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5537 if (name == lang_name_cplusplus)
5539 strict_prototype = strict_prototypes_lang_cplusplus;
5540 current_lang_name = name;
5542 else if (name == lang_name_java)
5544 strict_prototype = strict_prototypes_lang_cplusplus;
5545 current_lang_name = name;
5546 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5547 (See record_builtin_java_type in decl.c.) However, that causes
5548 incorrect debug entries if these types are actually used.
5549 So we re-enable debug output after extern "Java". */
5550 DECL_IGNORED_P (java_byte_type_node) = 0;
5551 DECL_IGNORED_P (java_short_type_node) = 0;
5552 DECL_IGNORED_P (java_int_type_node) = 0;
5553 DECL_IGNORED_P (java_long_type_node) = 0;
5554 DECL_IGNORED_P (java_float_type_node) = 0;
5555 DECL_IGNORED_P (java_double_type_node) = 0;
5556 DECL_IGNORED_P (java_char_type_node) = 0;
5557 DECL_IGNORED_P (java_boolean_type_node) = 0;
5559 else if (name == lang_name_c)
5561 strict_prototype = strict_prototypes_lang_c;
5562 current_lang_name = name;
5565 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5568 /* Get out of the current language scope. */
5573 /* Clear the current entry so that garbage collector won't hold on
5575 *current_lang_stack = NULL_TREE;
5576 current_lang_name = *--current_lang_stack;
5577 if (current_lang_name == lang_name_cplusplus
5578 || current_lang_name == lang_name_java)
5579 strict_prototype = strict_prototypes_lang_cplusplus;
5580 else if (current_lang_name == lang_name_c)
5581 strict_prototype = strict_prototypes_lang_c;
5584 /* Type instantiation routines. */
5586 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5587 matches the TARGET_TYPE. If there is no satisfactory match, return
5588 error_mark_node, and issue an error message if COMPLAIN is
5589 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5590 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5591 template arguments. */
5594 resolve_address_of_overloaded_function (target_type,
5603 tree explicit_targs;
5605 /* Here's what the standard says:
5609 If the name is a function template, template argument deduction
5610 is done, and if the argument deduction succeeds, the deduced
5611 arguments are used to generate a single template function, which
5612 is added to the set of overloaded functions considered.
5614 Non-member functions and static member functions match targets of
5615 type "pointer-to-function" or "reference-to-function." Nonstatic
5616 member functions match targets of type "pointer-to-member
5617 function;" the function type of the pointer to member is used to
5618 select the member function from the set of overloaded member
5619 functions. If a nonstatic member function is selected, the
5620 reference to the overloaded function name is required to have the
5621 form of a pointer to member as described in 5.3.1.
5623 If more than one function is selected, any template functions in
5624 the set are eliminated if the set also contains a non-template
5625 function, and any given template function is eliminated if the
5626 set contains a second template function that is more specialized
5627 than the first according to the partial ordering rules 14.5.5.2.
5628 After such eliminations, if any, there shall remain exactly one
5629 selected function. */
5632 int is_reference = 0;
5633 /* We store the matches in a TREE_LIST rooted here. The functions
5634 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5635 interoperability with most_specialized_instantiation. */
5636 tree matches = NULL_TREE;
5639 /* By the time we get here, we should be seeing only real
5640 pointer-to-member types, not the internal POINTER_TYPE to
5641 METHOD_TYPE representation. */
5642 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5643 && (TREE_CODE (TREE_TYPE (target_type))
5644 == METHOD_TYPE)), 0);
5646 /* Check that the TARGET_TYPE is reasonable. */
5647 if (TYPE_PTRFN_P (target_type))
5650 else if (TYPE_PTRMEMFUNC_P (target_type))
5651 /* This is OK, too. */
5653 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5655 /* This is OK, too. This comes from a conversion to reference
5657 target_type = build_reference_type (target_type);
5663 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5664 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5665 return error_mark_node;
5668 /* If we can find a non-template function that matches, we can just
5669 use it. There's no point in generating template instantiations
5670 if we're just going to throw them out anyhow. But, of course, we
5671 can only do this when we don't *need* a template function. */
5676 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5678 tree fn = OVL_FUNCTION (fns);
5681 if (TREE_CODE (fn) == TEMPLATE_DECL)
5682 /* We're not looking for templates just yet. */
5685 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5687 /* We're looking for a non-static member, and this isn't
5688 one, or vice versa. */
5691 /* See if there's a match. */
5692 fntype = TREE_TYPE (fn);
5694 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5695 else if (!is_reference)
5696 fntype = build_pointer_type (fntype);
5698 if (can_convert_arg (target_type, fntype, fn))
5699 matches = tree_cons (fn, NULL_TREE, matches);
5703 /* Now, if we've already got a match (or matches), there's no need
5704 to proceed to the template functions. But, if we don't have a
5705 match we need to look at them, too. */
5708 tree target_fn_type;
5709 tree target_arg_types;
5714 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5716 target_fn_type = TREE_TYPE (target_type);
5717 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5719 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5721 tree fn = OVL_FUNCTION (fns);
5723 tree instantiation_type;
5726 if (TREE_CODE (fn) != TEMPLATE_DECL)
5727 /* We're only looking for templates. */
5730 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5732 /* We're not looking for a non-static member, and this is
5733 one, or vice versa. */
5736 /* Try to do argument deduction. */
5737 targs = make_tree_vec (DECL_NTPARMS (fn));
5738 if (fn_type_unification (fn, explicit_targs, targs,
5739 target_arg_types, NULL_TREE,
5741 /* Argument deduction failed. */
5744 /* Instantiate the template. */
5745 instantiation = instantiate_template (fn, targs);
5746 if (instantiation == error_mark_node)
5747 /* Instantiation failed. */
5750 /* See if there's a match. */
5751 instantiation_type = TREE_TYPE (instantiation);
5753 instantiation_type =
5754 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5755 else if (!is_reference)
5756 instantiation_type = build_pointer_type (instantiation_type);
5757 if (can_convert_arg (target_type, instantiation_type, instantiation))
5758 matches = tree_cons (instantiation, fn, matches);
5761 /* Now, remove all but the most specialized of the matches. */
5764 tree match = most_specialized_instantiation (matches,
5767 if (match != error_mark_node)
5768 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5772 /* Now we should have exactly one function in MATCHES. */
5773 if (matches == NULL_TREE)
5775 /* There were *no* matches. */
5778 cp_error ("no matches converting function `%D' to type `%#T'",
5779 DECL_NAME (OVL_FUNCTION (overload)),
5782 /* print_candidates expects a chain with the functions in
5783 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5784 so why be clever?). */
5785 for (; overload; overload = OVL_NEXT (overload))
5786 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5789 print_candidates (matches);
5791 return error_mark_node;
5793 else if (TREE_CHAIN (matches))
5795 /* There were too many matches. */
5801 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5802 DECL_NAME (OVL_FUNCTION (overload)),
5805 /* Since print_candidates expects the functions in the
5806 TREE_VALUE slot, we flip them here. */
5807 for (match = matches; match; match = TREE_CHAIN (match))
5808 TREE_VALUE (match) = TREE_PURPOSE (match);
5810 print_candidates (matches);
5813 return error_mark_node;
5816 /* Good, exactly one match. Now, convert it to the correct type. */
5817 fn = TREE_PURPOSE (matches);
5821 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5822 return build_unary_op (ADDR_EXPR, fn, 0);
5825 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5826 will mark the function as addressed, but here we must do it
5828 mark_addressable (fn);
5834 /* This function will instantiate the type of the expression given in
5835 RHS to match the type of LHSTYPE. If errors exist, then return
5836 error_mark_node. We only complain is COMPLAIN is set. If we are
5837 not complaining, never modify rhs, as overload resolution wants to
5838 try many possible instantiations, in hopes that at least one will
5841 FLAGS is a bitmask, as we see at the top of the function.
5843 For non-recursive calls, LHSTYPE should be a function, pointer to
5844 function, or a pointer to member function. */
5847 instantiate_type (lhstype, rhs, flags)
5851 int complain = (flags & 1);
5852 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
5854 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5857 error ("not enough type information");
5858 return error_mark_node;
5861 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5863 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
5866 cp_error ("argument of type `%T' does not match `%T'",
5867 TREE_TYPE (rhs), lhstype);
5868 return error_mark_node;
5871 /* We don't overwrite rhs if it is an overloaded function.
5872 Copying it would destroy the tree link. */
5873 if (TREE_CODE (rhs) != OVERLOAD)
5874 rhs = copy_node (rhs);
5876 /* This should really only be used when attempting to distinguish
5877 what sort of a pointer to function we have. For now, any
5878 arithmetic operation which is not supported on pointers
5879 is rejected as an error. */
5881 switch (TREE_CODE (rhs))
5888 my_friendly_abort (177);
5889 return error_mark_node;
5896 new_rhs = instantiate_type (build_pointer_type (lhstype),
5897 TREE_OPERAND (rhs, 0), flags);
5898 if (new_rhs == error_mark_node)
5899 return error_mark_node;
5901 TREE_TYPE (rhs) = lhstype;
5902 TREE_OPERAND (rhs, 0) = new_rhs;
5907 rhs = copy_node (TREE_OPERAND (rhs, 0));
5908 TREE_TYPE (rhs) = unknown_type_node;
5909 return instantiate_type (lhstype, rhs, flags);
5913 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5915 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
5916 && complain && !flag_ms_extensions)
5918 /* Note: we check this after the recursive call to avoid
5919 complaining about cases where overload resolution fails. */
5921 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5922 tree fn = PTRMEM_CST_MEMBER (r);
5924 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
5927 ("object-dependent reference to `%E' can only be used in a call",
5930 (" to form a pointer to member function, say `&%T::%E'",
5938 rhs = TREE_OPERAND (rhs, 1);
5939 if (BASELINK_P (rhs))
5940 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5942 /* This can happen if we are forming a pointer-to-member for a
5944 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5948 case TEMPLATE_ID_EXPR:
5950 resolve_address_of_overloaded_function (lhstype,
5951 TREE_OPERAND (rhs, 0),
5953 /*template_only=*/1,
5954 TREE_OPERAND (rhs, 1));
5958 resolve_address_of_overloaded_function (lhstype,
5961 /*template_only=*/0,
5962 /*explicit_targs=*/NULL_TREE);
5965 /* Now we should have a baselink. */
5966 my_friendly_assert (BASELINK_P (rhs), 990412);
5968 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5971 /* This is too hard for now. */
5972 my_friendly_abort (183);
5973 return error_mark_node;
5978 TREE_OPERAND (rhs, 0)
5979 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5980 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5981 return error_mark_node;
5982 TREE_OPERAND (rhs, 1)
5983 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5984 if (TREE_OPERAND (rhs, 1) == error_mark_node)
5985 return error_mark_node;
5987 TREE_TYPE (rhs) = lhstype;
5991 case TRUNC_DIV_EXPR:
5992 case FLOOR_DIV_EXPR:
5994 case ROUND_DIV_EXPR:
5996 case TRUNC_MOD_EXPR:
5997 case FLOOR_MOD_EXPR:
5999 case ROUND_MOD_EXPR:
6000 case FIX_ROUND_EXPR:
6001 case FIX_FLOOR_EXPR:
6003 case FIX_TRUNC_EXPR:
6019 case PREINCREMENT_EXPR:
6020 case PREDECREMENT_EXPR:
6021 case POSTINCREMENT_EXPR:
6022 case POSTDECREMENT_EXPR:
6024 error ("invalid operation on uninstantiated type");
6025 return error_mark_node;
6027 case TRUTH_AND_EXPR:
6029 case TRUTH_XOR_EXPR:
6036 case TRUTH_ANDIF_EXPR:
6037 case TRUTH_ORIF_EXPR:
6038 case TRUTH_NOT_EXPR:
6040 error ("not enough type information");
6041 return error_mark_node;
6044 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6047 error ("not enough type information");
6048 return error_mark_node;
6050 TREE_OPERAND (rhs, 1)
6051 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6052 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6053 return error_mark_node;
6054 TREE_OPERAND (rhs, 2)
6055 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6056 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6057 return error_mark_node;
6059 TREE_TYPE (rhs) = lhstype;
6063 TREE_OPERAND (rhs, 1)
6064 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6065 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6066 return error_mark_node;
6068 TREE_TYPE (rhs) = lhstype;
6072 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6074 case ENTRY_VALUE_EXPR:
6075 my_friendly_abort (184);
6076 return error_mark_node;
6079 return error_mark_node;
6082 my_friendly_abort (185);
6083 return error_mark_node;
6087 /* Return the name of the virtual function pointer field
6088 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6089 this may have to look back through base types to find the
6090 ultimate field name. (For single inheritance, these could
6091 all be the same name. Who knows for multiple inheritance). */
6094 get_vfield_name (type)
6097 tree binfo = TYPE_BINFO (type);
6100 while (BINFO_BASETYPES (binfo)
6101 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6102 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6103 binfo = BINFO_BASETYPE (binfo, 0);
6105 type = BINFO_TYPE (binfo);
6106 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6107 + TYPE_NAME_LENGTH (type) + 2);
6108 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6109 return get_identifier (buf);
6113 print_class_statistics ()
6115 #ifdef GATHER_STATISTICS
6116 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6117 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6118 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6119 n_build_method_call, n_inner_fields_searched);
6122 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6123 n_vtables, n_vtable_searches);
6124 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6125 n_vtable_entries, n_vtable_elems);
6130 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6131 according to [class]:
6132 The class-name is also inserted
6133 into the scope of the class itself. For purposes of access checking,
6134 the inserted class name is treated as if it were a public member name. */
6137 build_self_reference ()
6139 tree name = constructor_name (current_class_type);
6140 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6143 DECL_NONLOCAL (value) = 1;
6144 DECL_CONTEXT (value) = current_class_type;
6145 DECL_CLASS_CONTEXT (value) = current_class_type;
6146 DECL_ARTIFICIAL (value) = 1;
6148 if (processing_template_decl)
6149 value = push_template_decl (value);
6151 saved_cas = current_access_specifier;
6152 current_access_specifier = access_public_node;
6153 finish_member_declaration (value);
6154 current_access_specifier = saved_cas;
6157 /* Returns 1 if TYPE contains only padding bytes. */
6160 is_empty_class (type)
6165 if (type == error_mark_node)
6168 if (! IS_AGGR_TYPE (type))
6172 return CLASSTYPE_SIZE (type) == integer_zero_node;
6174 if (TYPE_BINFO_BASETYPES (type))
6176 t = TYPE_FIELDS (type);
6177 while (t && TREE_CODE (t) != FIELD_DECL)
6179 return (t == NULL_TREE);
6182 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6183 a *_TYPE node. NODE can also be a local class. */
6186 get_enclosing_class (type)
6191 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6193 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6196 node = DECL_CONTEXT (node);
6202 node = TYPE_CONTEXT (node);
6206 my_friendly_abort (0);
6212 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6215 is_base_of_enclosing_class (base, type)
6220 if (get_binfo (base, type, 0))
6223 type = get_enclosing_class (type);
6228 /* Note that NAME was looked up while the current class was being
6229 defined and that the result of that lookup was DECL. */
6232 maybe_note_name_used_in_class (name, decl)
6236 splay_tree names_used;
6238 /* If we're not defining a class, there's nothing to do. */
6239 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6242 /* If there's already a binding for this NAME, then we don't have
6243 anything to worry about. */
6244 if (IDENTIFIER_CLASS_VALUE (name))
6247 if (!current_class_stack[current_class_depth - 1].names_used)
6248 current_class_stack[current_class_depth - 1].names_used
6249 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6250 names_used = current_class_stack[current_class_depth - 1].names_used;
6252 splay_tree_insert (names_used,
6253 (splay_tree_key) name,
6254 (splay_tree_value) decl);
6257 /* Note that NAME was declared (as DECL) in the current class. Check
6258 to see that the declaration is legal. */
6261 note_name_declared_in_class (name, decl)
6265 splay_tree names_used;
6268 /* Look to see if we ever used this name. */
6270 = current_class_stack[current_class_depth - 1].names_used;
6274 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6277 /* [basic.scope.class]
6279 A name N used in a class S shall refer to the same declaration
6280 in its context and when re-evaluated in the completed scope of
6282 cp_error ("declaration of `%#D'", decl);
6283 cp_error_at ("changes meaning of `%s' from `%+#D'",
6284 IDENTIFIER_POINTER (DECL_NAME (decl)),
6289 /* Dump the offsets of all the bases rooted at BINFO to stderr.
6290 INDENT should be zero when called from the top level; it is
6291 incremented recursively. */
6294 dump_class_hierarchy (binfo, indent)
6300 fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
6301 (unsigned long) binfo,
6302 type_as_string (binfo, TS_PLAIN));
6303 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
6304 TREE_INT_CST_LOW (BINFO_OFFSET (binfo)));
6305 fprintf (stderr, " %s\n",
6306 BINFO_PRIMARY_MARKED_P (binfo) ? "primary" : "");
6308 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6309 dump_class_hierarchy (BINFO_BASETYPE (binfo, i), indent + 2);