1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
41 /* This is how we tell when two virtual member functions are really the
43 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
45 extern void set_class_shadows
PARAMS ((tree
));
47 /* The number of nested classes being processed. If we are not in the
48 scope of any class, this is zero. */
50 int current_class_depth
;
52 /* In order to deal with nested classes, we keep a stack of classes.
53 The topmost entry is the innermost class, and is the entry at index
54 CURRENT_CLASS_DEPTH */
56 typedef struct class_stack_node
{
57 /* The name of the class. */
60 /* The _TYPE node for the class. */
63 /* The access specifier pending for new declarations in the scope of
67 /* If were defining TYPE, the names used in this class. */
68 splay_tree names_used
;
69 }* class_stack_node_t
;
71 /* The stack itself. This is an dynamically resized array. The
72 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
73 static int current_class_stack_size
;
74 static class_stack_node_t current_class_stack
;
76 static tree get_vfield_name
PARAMS ((tree
));
77 static void finish_struct_anon
PARAMS ((tree
));
78 static tree build_vbase_pointer
PARAMS ((tree
, tree
));
79 static tree build_vtable_entry
PARAMS ((tree
, tree
, tree
));
80 static tree get_vtable_name
PARAMS ((tree
));
81 static tree get_derived_offset
PARAMS ((tree
, tree
));
82 static tree get_basefndecls
PARAMS ((tree
, tree
));
83 static void set_rtti_entry
PARAMS ((tree
, tree
, tree
));
84 static int build_primary_vtable
PARAMS ((tree
, tree
));
85 static int build_secondary_vtable
PARAMS ((tree
, tree
));
86 static tree dfs_finish_vtbls
PARAMS ((tree
, void *));
87 static tree dfs_accumulate_vtbl_inits
PARAMS ((tree
, void *));
88 static void finish_vtbls
PARAMS ((tree
));
89 static void modify_vtable_entry
PARAMS ((tree
, tree
, tree
, tree
, tree
*));
90 static void add_virtual_function
PARAMS ((tree
*, tree
*, int *, tree
, tree
));
91 static tree delete_duplicate_fields_1
PARAMS ((tree
, tree
));
92 static void delete_duplicate_fields
PARAMS ((tree
));
93 static void finish_struct_bits
PARAMS ((tree
));
94 static int alter_access
PARAMS ((tree
, tree
, tree
));
95 static void handle_using_decl
PARAMS ((tree
, tree
));
96 static int overrides
PARAMS ((tree
, tree
));
97 static int strictly_overrides
PARAMS ((tree
, tree
));
98 static void mark_overriders
PARAMS ((tree
, tree
));
99 static void check_for_override
PARAMS ((tree
, tree
));
100 static tree dfs_modify_vtables
PARAMS ((tree
, void *));
101 static tree modify_all_vtables
PARAMS ((tree
, int *, tree
));
102 static void determine_primary_base
PARAMS ((tree
, int *));
103 static void finish_struct_methods
PARAMS ((tree
));
104 static void maybe_warn_about_overly_private_class
PARAMS ((tree
));
105 static int field_decl_cmp
PARAMS ((const tree
*, const tree
*));
106 static int method_name_cmp
PARAMS ((const tree
*, const tree
*));
107 static tree add_implicitly_declared_members
PARAMS ((tree
, int, int, int));
108 static tree fixed_type_or_null
PARAMS ((tree
, int *));
109 static tree resolve_address_of_overloaded_function
PARAMS ((tree
, tree
, int,
111 static void build_vtable_entry_ref
PARAMS ((tree
, tree
, tree
));
112 static tree build_vtbl_initializer
PARAMS ((tree
, tree
));
113 static int count_fields
PARAMS ((tree
));
114 static int add_fields_to_vec
PARAMS ((tree
, tree
, int));
115 static void check_bitfield_decl
PARAMS ((tree
));
116 static void check_field_decl
PARAMS ((tree
, tree
, int *, int *, int *, int *));
117 static void check_field_decls
PARAMS ((tree
, tree
*, int *, int *, int *,
119 static int avoid_overlap
PARAMS ((tree
, tree
, int *));
120 static tree build_base_field
PARAMS ((tree
, tree
, int *, int *, unsigned int *));
121 static tree build_base_fields
PARAMS ((tree
, int *));
122 static tree build_vbase_pointer_fields
PARAMS ((tree
, int *));
123 static tree build_vtbl_or_vbase_field
PARAMS ((tree
, tree
, tree
, tree
, tree
,
125 static void check_methods
PARAMS ((tree
));
126 static void remove_zero_width_bit_fields
PARAMS ((tree
));
127 static void check_bases
PARAMS ((tree
, int *, int *, int *));
128 static void check_bases_and_members
PARAMS ((tree
, int *));
129 static void create_vtable_ptr
PARAMS ((tree
, int *, int *, tree
*, tree
*));
130 static void layout_class_type
PARAMS ((tree
, int *, int *, tree
*, tree
*));
131 static void fixup_pending_inline
PARAMS ((struct pending_inline
*));
132 static void fixup_inline_methods
PARAMS ((tree
));
133 static void set_primary_base
PARAMS ((tree
, int, int *));
134 static tree dfs_propagate_binfo_offsets
PARAMS ((tree
, void *));
135 static void propagate_binfo_offsets
PARAMS ((tree
, tree
));
136 static void layout_basetypes
PARAMS ((tree
));
137 static void layout_virtual_bases
PARAMS ((tree
));
138 static void remove_base_field
PARAMS ((tree
, tree
, tree
*));
139 static void remove_base_fields
PARAMS ((tree
));
140 static tree dfs_set_offset_for_shared_vbases
PARAMS ((tree
, void *));
141 static tree dfs_set_offset_for_unshared_vbases
PARAMS ((tree
, void *));
142 static tree dfs_build_vbase_offset_vtbl_entries
PARAMS ((tree
, void *));
143 static tree build_vbase_offset_vtbl_entries
PARAMS ((tree
, tree
));
144 static tree dfs_vcall_offset_queue_p
PARAMS ((tree
, void *));
145 static tree dfs_build_vcall_offset_vtbl_entries
PARAMS ((tree
, void *));
146 static tree build_vcall_offset_vtbl_entries
PARAMS ((tree
, tree
));
147 static tree dfs_count_virtuals
PARAMS ((tree
, void *));
148 static void start_vtable
PARAMS ((tree
, int *));
149 static void layout_vtable_decl
PARAMS ((tree
, int));
150 static int num_vfun_entries
PARAMS ((tree
));
151 static tree dfs_find_final_overrider
PARAMS ((tree
, void *));
152 static tree find_final_overrider
PARAMS ((tree
, tree
, tree
));
153 static tree dfs_find_base
PARAMS ((tree
, void *));
154 static int make_new_vtable
PARAMS ((tree
, tree
));
155 extern void dump_class_hierarchy
PARAMS ((tree
, int));
156 static tree build_vtable
PARAMS ((tree
, tree
, tree
));
157 static void initialize_vtable
PARAMS ((tree
, tree
));
159 /* Variables shared between class.c and call.c. */
161 #ifdef GATHER_STATISTICS
163 int n_vtable_entries
= 0;
164 int n_vtable_searches
= 0;
165 int n_vtable_elems
= 0;
166 int n_convert_harshness
= 0;
167 int n_compute_conversion_costs
= 0;
168 int n_build_method_call
= 0;
169 int n_inner_fields_searched
= 0;
172 /* Virtual base class layout. */
174 /* Returns a list of virtual base class pointers as a chain of
178 build_vbase_pointer_fields (rec
, empty_p
)
182 /* Chain to hold all the new FIELD_DECLs which point at virtual
184 tree vbase_decls
= NULL_TREE
;
185 tree binfos
= TYPE_BINFO_BASETYPES (rec
);
186 int n_baseclasses
= CLASSTYPE_N_BASECLASSES (rec
);
190 /* Under the new ABI, there are no vbase pointers in the object.
191 Instead, the offsets are stored in the vtable. */
192 if (vbase_offsets_in_vtable_p ())
195 /* Loop over the baseclasses, adding vbase pointers as needed. */
196 for (i
= 0; i
< n_baseclasses
; i
++)
198 register tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
199 register tree basetype
= BINFO_TYPE (base_binfo
);
201 if (TYPE_SIZE (basetype
) == 0)
202 /* This error is now reported in xref_tag, thus giving better
203 location information. */
206 /* All basetypes are recorded in the association list of the
209 if (TREE_VIA_VIRTUAL (base_binfo
))
214 /* The offset for a virtual base class is only used in computing
215 virtual function tables and for initializing virtual base
216 pointers. It is built once `get_vbase_types' is called. */
218 /* If this basetype can come from another vbase pointer
219 without an additional indirection, we will share
220 that pointer. If an indirection is involved, we
221 make our own pointer. */
222 for (j
= 0; j
< n_baseclasses
; j
++)
224 tree other_base_binfo
= TREE_VEC_ELT (binfos
, j
);
225 if (! TREE_VIA_VIRTUAL (other_base_binfo
)
226 && BINFO_FOR_VBASE (basetype
, BINFO_TYPE (other_base_binfo
)))
229 FORMAT_VBASE_NAME (name
, basetype
);
230 decl
= build_vtbl_or_vbase_field (get_identifier (name
),
231 get_identifier (VTABLE_BASE
),
232 build_pointer_type (basetype
),
236 BINFO_VPTR_FIELD (base_binfo
) = decl
;
237 TREE_CHAIN (decl
) = vbase_decls
;
242 /* The space this decl occupies has already been accounted for. */
250 /* Called from build_vbase_offset_vtbl_entries via dfs_walk. */
253 dfs_build_vbase_offset_vtbl_entries (binfo
, data
)
257 tree list
= (tree
) data
;
259 if (TREE_TYPE (list
) == binfo
)
260 /* The TREE_TYPE of LIST is the base class from which we started
261 walking. If that BINFO is virtual it's not a virtual baseclass
264 else if (TREE_VIA_VIRTUAL (binfo
))
269 /* Remember the index to the vbase offset for this virtual
271 vbase
= BINFO_FOR_VBASE (TREE_TYPE (binfo
), TREE_PURPOSE (list
));
272 if (!TREE_VALUE (list
))
273 BINFO_VPTR_FIELD (vbase
) = build_int_2 (-1, 0);
276 BINFO_VPTR_FIELD (vbase
) = TREE_PURPOSE (TREE_VALUE (list
));
277 BINFO_VPTR_FIELD (vbase
)
278 = fold (build (MINUS_EXPR
, integer_type_node
,
279 BINFO_VPTR_FIELD (vbase
), integer_one_node
));
282 /* And record the offset at which this virtual base lies in the
284 init
= BINFO_OFFSET (binfo
);
285 TREE_VALUE (list
) = tree_cons (BINFO_VPTR_FIELD (vbase
),
286 init
, TREE_VALUE (list
));
289 SET_BINFO_VTABLE_PATH_MARKED (binfo
);
294 /* Returns the initializers for the vbase offset entries in the vtable
295 for BINFO (which is part of the class hierarchy dominated by T), in
299 build_vbase_offset_vtbl_entries (binfo
, t
)
307 /* Under the old ABI, pointers to virtual bases are stored in each
309 if (!vbase_offsets_in_vtable_p ())
312 /* If there are no virtual baseclasses, then there is nothing to
314 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo
)))
319 /* The offsets are allocated in the reverse order of a
320 depth-first left-to-right traversal of the hierarchy. We use
321 BINFO_VTABLE_PATH_MARKED because we are ourselves during a
322 dfs_walk, and so BINFO_MARKED is already in use. */
323 list
= build_tree_list (t
, NULL_TREE
);
324 TREE_TYPE (list
) = binfo
;
326 dfs_build_vbase_offset_vtbl_entries
,
327 dfs_vtable_path_unmarked_real_bases_queue_p
,
330 dfs_vtable_path_unmark
,
331 dfs_vtable_path_marked_real_bases_queue_p
,
333 inits
= nreverse (TREE_VALUE (list
));
335 /* We've now got offsets in the right order. However, the offsets
336 we've stored are offsets from the beginning of the complete
337 object, and we need offsets from this BINFO. */
338 for (init
= inits
; init
; init
= TREE_CHAIN (init
))
340 /* The dfs_build_vbase_offset_vtbl_entries routine uses the
341 TREE_PURPOSE to scribble in. But, we need to clear it now so
342 that the values are not perceived as labeled initializers. */
343 TREE_PURPOSE (init
) = NULL_TREE
;
345 = fold (build1 (NOP_EXPR
, vtable_entry_type
,
346 size_diffop (TREE_VALUE (init
),
347 BINFO_OFFSET (binfo
))));
353 typedef struct vcall_offset_data_s
355 /* The binfo for the most-derived type. */
357 /* The binfo for the virtual base for which we're building
360 /* The vcall offset initializers built up so far. */
362 /* The number of vcall offsets accumulated. */
366 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
369 dfs_vcall_offset_queue_p (binfo
, data
)
373 vcall_offset_data
* vod
= (vcall_offset_data
*) data
;
375 return (binfo
== vod
->vbase
) ? binfo
: dfs_skip_vbases (binfo
, NULL
);
378 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
381 dfs_build_vcall_offset_vtbl_entries (binfo
, data
)
385 vcall_offset_data
* vod
;
389 /* Primary bases are not interesting; all of the virtual
390 function table entries have been overridden. */
391 if (BINFO_PRIMARY_MARKED_P (binfo
))
394 vod
= (vcall_offset_data
*) data
;
395 binfo_inits
= NULL_TREE
;
397 /* We chain the offsets on in reverse order. That's correct --
398 build_vtbl_initializer will straighten them out. */
399 for (virtuals
= skip_rtti_stuff (binfo
,
403 virtuals
= TREE_CHAIN (virtuals
))
405 /* Figure out what function we're looking at. */
406 tree fn
= TREE_VALUE (virtuals
);
407 tree base
= DECL_CONTEXT (fn
);
408 /* The FN comes from BASE. So, we must caculate the adjustment
409 from the virtual base that derived from BINFO to BASE. */
410 tree base_binfo
= get_binfo (base
, vod
->derived
, /*protect=*/0);
413 = tree_cons (NULL_TREE
,
414 fold (build1 (NOP_EXPR
, vtable_entry_type
,
415 size_diffop (BINFO_OFFSET (base_binfo
),
416 BINFO_OFFSET (vod
->vbase
)))),
420 /* Now add the initializers we've just created to the list that will
421 be returned to our caller. */
422 vod
->inits
= chainon (vod
->inits
, binfo_inits
);
427 /* Returns the initializers for the vcall offset entries in the vtable
428 for BINFO (which is part of the class hierarchy dominated by T), in
432 build_vcall_offset_vtbl_entries (binfo
, t
)
436 vcall_offset_data vod
;
438 /* Under the old ABI, the adjustments to the `this' pointer were made
440 if (!vcall_offsets_in_vtable_p ())
443 /* We only need these entries if this base is a virtual base. */
444 if (!TREE_VIA_VIRTUAL (binfo
))
447 /* We need a vcall offset for each of the virtual functions in this
450 class A { virtual void f (); };
451 class B : virtual public A { };
452 class C: virtual public A, public B {};
459 The location of `A' is not at a fixed offset relative to `B'; the
460 offset depends on the complete object derived from `B'. So,
461 `B' vtable contains an entry for `f' that indicates by what
462 amount the `this' pointer for `B' needs to be adjusted to arrive
465 We need entries for all the functions in our primary vtable and
466 in our non-virtual bases vtables. For each base, the entries
467 appear in the same order as in the base; but the bases themselves
468 appear in reverse depth-first, left-to-right order. */
471 vod
.inits
= NULL_TREE
;
473 dfs_build_vcall_offset_vtbl_entries
,
474 dfs_vcall_offset_queue_p
,
480 /* Returns a pointer to the virtual base class of EXP that has the
481 indicated TYPE. EXP is of class type, not a pointer type. */
484 build_vbase_pointer (exp
, type
)
487 if (vbase_offsets_in_vtable_p ())
492 /* Find the shared copy of TYPE; that's where the vtable offset
494 vbase
= BINFO_FOR_VBASE (type
, TREE_TYPE (exp
));
495 /* Find the virtual function table pointer. */
496 vbase_ptr
= build_vfield_ref (exp
, TREE_TYPE (exp
));
497 /* Compute the location where the offset will lie. */
498 vbase_ptr
= build_binary_op (PLUS_EXPR
,
500 BINFO_VPTR_FIELD (vbase
));
501 vbase_ptr
= build1 (NOP_EXPR
,
502 build_pointer_type (ptrdiff_type_node
),
504 /* Add the contents of this location to EXP. */
505 return build (PLUS_EXPR
,
506 build_pointer_type (type
),
507 build_unary_op (ADDR_EXPR
, exp
, /*noconvert=*/0),
508 build1 (INDIRECT_REF
, ptrdiff_type_node
, vbase_ptr
));
513 FORMAT_VBASE_NAME (name
, type
);
514 return build_component_ref (exp
, get_identifier (name
), NULL_TREE
, 0);
518 /* Build multi-level access to EXPR using hierarchy path PATH.
519 CODE is PLUS_EXPR if we are going with the grain,
520 and MINUS_EXPR if we are not (in which case, we cannot traverse
521 virtual baseclass links).
523 TYPE is the type we want this path to have on exit.
525 NONNULL is non-zero if we know (for any reason) that EXPR is
526 not, in fact, zero. */
529 build_vbase_path (code
, type
, expr
, path
, nonnull
)
531 tree type
, expr
, path
;
534 register int changed
= 0;
535 tree last
= NULL_TREE
, last_virtual
= NULL_TREE
;
537 tree null_expr
= 0, nonnull_expr
;
539 tree offset
= integer_zero_node
;
541 if (BINFO_INHERITANCE_CHAIN (path
) == NULL_TREE
)
542 return build1 (NOP_EXPR
, type
, expr
);
544 /* We could do better if we had additional logic to convert back to the
545 unconverted type (the static type of the complete object), and then
546 convert back to the type we want. Until that is done, we only optimize
547 if the complete type is the same type as expr has. */
548 fixed_type_p
= resolves_to_fixed_type_p (expr
, &nonnull
);
550 if (!fixed_type_p
&& TREE_SIDE_EFFECTS (expr
))
551 expr
= save_expr (expr
);
554 path
= reverse_path (path
);
556 basetype
= BINFO_TYPE (path
);
560 if (TREE_VIA_VIRTUAL (TREE_VALUE (path
)))
562 last_virtual
= BINFO_TYPE (TREE_VALUE (path
));
563 if (code
== PLUS_EXPR
)
565 changed
= ! fixed_type_p
;
571 /* We already check for ambiguous things in the caller, just
575 tree binfo
= get_binfo (last
, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr
))), 0);
576 nonnull_expr
= convert_pointer_to_real (binfo
, nonnull_expr
);
578 ind
= build_indirect_ref (nonnull_expr
, NULL_PTR
);
579 nonnull_expr
= build_vbase_pointer (ind
, last_virtual
);
581 && TREE_CODE (type
) == POINTER_TYPE
582 && null_expr
== NULL_TREE
)
584 null_expr
= build1 (NOP_EXPR
, build_pointer_type (last_virtual
), integer_zero_node
);
585 expr
= build (COND_EXPR
, build_pointer_type (last_virtual
),
586 build (EQ_EXPR
, boolean_type_node
, expr
,
588 null_expr
, nonnull_expr
);
591 /* else we'll figure out the offset below. */
593 /* Happens in the case of parse errors. */
594 if (nonnull_expr
== error_mark_node
)
595 return error_mark_node
;
599 cp_error ("cannot cast up from virtual baseclass `%T'",
601 return error_mark_node
;
604 last
= TREE_VALUE (path
);
605 path
= TREE_CHAIN (path
);
607 /* LAST is now the last basetype assoc on the path. */
609 /* A pointer to a virtual base member of a non-null object
610 is non-null. Therefore, we only need to test for zeroness once.
611 Make EXPR the canonical expression to deal with here. */
614 TREE_OPERAND (expr
, 2) = nonnull_expr
;
615 TREE_TYPE (expr
) = TREE_TYPE (TREE_OPERAND (expr
, 1))
616 = TREE_TYPE (nonnull_expr
);
621 /* If we go through any virtual base pointers, make sure that
622 casts to BASETYPE from the last virtual base class use
623 the right value for BASETYPE. */
626 tree intype
= TREE_TYPE (TREE_TYPE (expr
));
628 if (TYPE_MAIN_VARIANT (intype
) != BINFO_TYPE (last
))
630 = BINFO_OFFSET (get_binfo (last
, TYPE_MAIN_VARIANT (intype
), 0));
633 offset
= BINFO_OFFSET (last
);
635 if (TREE_INT_CST_LOW (offset
))
637 /* Bash types to make the backend happy. */
638 offset
= cp_convert (type
, offset
);
640 /* If expr might be 0, we need to preserve that zeroness. */
644 TREE_TYPE (null_expr
) = type
;
646 null_expr
= build1 (NOP_EXPR
, type
, integer_zero_node
);
647 if (TREE_SIDE_EFFECTS (expr
))
648 expr
= save_expr (expr
);
650 return build (COND_EXPR
, type
,
651 build (EQ_EXPR
, boolean_type_node
, expr
, integer_zero_node
),
653 build (code
, type
, expr
, offset
));
655 else return build (code
, type
, expr
, offset
);
658 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
659 be used multiple times in initialization of multiple inheritance. */
662 TREE_TYPE (expr
) = type
;
666 return build1 (NOP_EXPR
, type
, expr
);
670 /* Virtual function things. */
672 /* Build an entry in the virtual function table. DELTA is the offset
673 for the `this' pointer. VCALL_INDEX is the vtable index containing
674 the vcall offset; zero if none. ENTRY is the virtual function
675 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
676 but it may not actually be a virtual function table pointer. (For
677 example, it might be the address of the RTTI object, under the new
681 build_vtable_entry (delta
, vcall_index
, entry
)
686 if (flag_vtable_thunks
)
688 HOST_WIDE_INT idelta
;
689 HOST_WIDE_INT ivindex
;
691 idelta
= TREE_INT_CST_LOW (delta
);
692 ivindex
= TREE_INT_CST_LOW (vcall_index
);
693 if ((idelta
|| ivindex
)
694 && ! DECL_PURE_VIRTUAL_P (TREE_OPERAND (entry
, 0)))
696 entry
= make_thunk (entry
, idelta
, ivindex
);
697 entry
= build1 (ADDR_EXPR
, vtable_entry_type
, entry
);
698 TREE_READONLY (entry
) = 1;
699 TREE_CONSTANT (entry
) = 1;
701 #ifdef GATHER_STATISTICS
702 n_vtable_entries
+= 1;
708 extern int flag_huge_objects
;
709 tree elems
= tree_cons (NULL_TREE
, delta
,
710 tree_cons (NULL_TREE
, integer_zero_node
,
711 build_tree_list (NULL_TREE
, entry
)));
712 tree entry
= build (CONSTRUCTOR
, vtable_entry_type
, NULL_TREE
, elems
);
714 /* We don't use vcall offsets when not using vtable thunks. */
715 my_friendly_assert (integer_zerop (vcall_index
), 20000125);
717 /* DELTA used to be constructed by `size_int' and/or size_binop,
718 which caused overflow problems when it was negative. That should
721 if (! int_fits_type_p (delta
, delta_type_node
))
723 if (flag_huge_objects
)
724 sorry ("object size exceeds built-in limit for virtual function table implementation");
726 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
729 TREE_CONSTANT (entry
) = 1;
730 TREE_STATIC (entry
) = 1;
731 TREE_READONLY (entry
) = 1;
733 #ifdef GATHER_STATISTICS
734 n_vtable_entries
+= 1;
741 /* We want to give the assembler the vtable identifier as well as
742 the offset to the function pointer. So we generate
744 __asm__ __volatile__ (".vtable_entry %c0, %c1"
745 : : "s"(&class_vtable),
746 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
749 build_vtable_entry_ref (basetype
, vtbl
, idx
)
750 tree basetype
, vtbl
, idx
;
752 static char asm_stmt
[] = ".vtable_entry %c0, %c1";
755 s
= build_unary_op (ADDR_EXPR
, TYPE_BINFO_VTABLE (basetype
), 0);
756 s
= build_tree_list (build_string (1, "s"), s
);
758 i
= build_array_ref (vtbl
, idx
);
759 if (!flag_vtable_thunks
)
760 i
= build_component_ref (i
, pfn_identifier
, vtable_entry_type
, 0);
761 i
= build_c_cast (ptrdiff_type_node
, build_unary_op (ADDR_EXPR
, i
, 0));
762 i2
= build_array_ref (vtbl
, build_int_2(0,0));
763 i2
= build_c_cast (ptrdiff_type_node
, build_unary_op (ADDR_EXPR
, i2
, 0));
764 i
= build_binary_op (MINUS_EXPR
, i
, i2
);
765 i
= build_tree_list (build_string (1, "i"), i
);
767 finish_asm_stmt (ridpointers
[RID_VOLATILE
],
768 build_string (sizeof(asm_stmt
)-1, asm_stmt
),
769 NULL_TREE
, chainon (s
, i
), NULL_TREE
);
772 /* Given an object INSTANCE, return an expression which yields the
773 virtual function vtable element corresponding to INDEX. There are
774 many special cases for INSTANCE which we take care of here, mainly
775 to avoid creating extra tree nodes when we don't have to. */
778 build_vtbl_ref (instance
, idx
)
782 tree basetype
= TREE_TYPE (instance
);
784 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
785 basetype
= TREE_TYPE (basetype
);
787 if (instance
== current_class_ref
)
788 vtbl
= build_vfield_ref (instance
, basetype
);
793 /* Try to figure out what a reference refers to, and
794 access its virtual function table directly. */
795 tree ref
= NULL_TREE
;
797 if (TREE_CODE (instance
) == INDIRECT_REF
798 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance
, 0))) == REFERENCE_TYPE
)
799 ref
= TREE_OPERAND (instance
, 0);
800 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
803 if (ref
&& TREE_CODE (ref
) == VAR_DECL
804 && DECL_INITIAL (ref
))
806 tree init
= DECL_INITIAL (ref
);
808 while (TREE_CODE (init
) == NOP_EXPR
809 || TREE_CODE (init
) == NON_LVALUE_EXPR
)
810 init
= TREE_OPERAND (init
, 0);
811 if (TREE_CODE (init
) == ADDR_EXPR
)
813 init
= TREE_OPERAND (init
, 0);
814 if (IS_AGGR_TYPE (TREE_TYPE (init
))
815 && (TREE_CODE (init
) == PARM_DECL
816 || TREE_CODE (init
) == VAR_DECL
))
822 if (IS_AGGR_TYPE (TREE_TYPE (instance
))
823 && (TREE_CODE (instance
) == RESULT_DECL
824 || TREE_CODE (instance
) == PARM_DECL
825 || TREE_CODE (instance
) == VAR_DECL
))
826 vtbl
= TYPE_BINFO_VTABLE (basetype
);
828 vtbl
= build_vfield_ref (instance
, basetype
);
831 assemble_external (vtbl
);
834 build_vtable_entry_ref (basetype
, vtbl
, idx
);
836 aref
= build_array_ref (vtbl
, idx
);
841 /* Given an object INSTANCE, return an expression which yields the
842 virtual function corresponding to INDEX. There are many special
843 cases for INSTANCE which we take care of here, mainly to avoid
844 creating extra tree nodes when we don't have to. */
847 build_vfn_ref (ptr_to_instptr
, instance
, idx
)
848 tree
*ptr_to_instptr
, instance
;
851 tree aref
= build_vtbl_ref (instance
, idx
);
853 /* When using thunks, there is no extra delta, and we get the pfn
855 if (flag_vtable_thunks
)
860 /* Save the intermediate result in a SAVE_EXPR so we don't have to
861 compute each component of the virtual function pointer twice. */
862 if (TREE_CODE (aref
) == INDIRECT_REF
)
863 TREE_OPERAND (aref
, 0) = save_expr (TREE_OPERAND (aref
, 0));
866 = build (PLUS_EXPR
, TREE_TYPE (*ptr_to_instptr
),
868 cp_convert (ptrdiff_type_node
,
869 build_component_ref (aref
, delta_identifier
, NULL_TREE
, 0)));
872 return build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
875 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
876 for the given TYPE. */
879 get_vtable_name (type
)
882 tree type_id
= build_typename_overload (type
);
883 char *buf
= (char *) alloca (strlen (VTABLE_NAME_PREFIX
)
884 + IDENTIFIER_LENGTH (type_id
) + 2);
885 const char *ptr
= IDENTIFIER_POINTER (type_id
);
887 for (i
= 0; ptr
[i
] == OPERATOR_TYPENAME_FORMAT
[i
]; i
++) ;
889 /* We don't take off the numbers; build_secondary_vtable uses the
890 DECL_ASSEMBLER_NAME for the type, which includes the number
891 in `3foo'. If we were to pull them off here, we'd end up with
892 something like `_vt.foo.3bar', instead of a uniform definition. */
893 while (ptr
[i
] >= '0' && ptr
[i
] <= '9')
896 sprintf (buf
, "%s%s", VTABLE_NAME_PREFIX
, ptr
+i
);
897 return get_identifier (buf
);
900 /* Return the offset to the main vtable for a given base BINFO. */
903 get_vfield_offset (binfo
)
907 = size_binop (FLOOR_DIV_EXPR
,
908 DECL_FIELD_BITPOS (TYPE_VFIELD (BINFO_TYPE (binfo
))),
909 bitsize_int (BITS_PER_UNIT
));
911 return size_binop (PLUS_EXPR
, convert (sizetype
, tmp
),
912 BINFO_OFFSET (binfo
));
915 /* Get the offset to the start of the original binfo that we derived
916 this binfo from. If we find TYPE first, return the offset only
917 that far. The shortened search is useful because the this pointer
918 on method calling is expected to point to a DECL_CONTEXT (fndecl)
919 object, and not a baseclass of it. */
923 get_derived_offset (binfo
, type
)
926 tree offset1
= get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo
)));
930 while (BINFO_BASETYPES (binfo
)
931 && (i
= CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo
))) != -1)
933 tree binfos
= BINFO_BASETYPES (binfo
);
934 if (BINFO_TYPE (binfo
) == type
)
936 binfo
= TREE_VEC_ELT (binfos
, i
);
939 offset2
= get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo
)));
940 return size_binop (MINUS_EXPR
, offset1
, offset2
);
943 /* Update the rtti info for this class. */
946 set_rtti_entry (virtuals
, offset
, type
)
947 tree virtuals
, offset
, type
;
951 if (CLASSTYPE_COM_INTERFACE (type
))
955 decl
= get_tinfo_decl (type
);
956 else if (!new_abi_rtti_p ())
957 /* If someone tries to get RTTI information for a type compiled
958 without RTTI, they're out of luck. By calling __pure_virtual
959 in this case, we give a small clue as to what went wrong. We
960 could consider having a __no_typeinfo function as well, for a
961 more specific hint. */
964 /* For the new-abi, we just point to the type_info object. */
967 if (flag_vtable_thunks
)
969 /* The first slot holds the offset. */
970 BV_DELTA (virtuals
) = offset
;
971 BV_VCALL_INDEX (virtuals
) = integer_zero_node
;
973 /* The next node holds the decl. */
974 virtuals
= TREE_CHAIN (virtuals
);
975 offset
= integer_zero_node
;
978 /* This slot holds the function to call. */
979 BV_DELTA (virtuals
) = offset
;
980 BV_VCALL_INDEX (virtuals
) = integer_zero_node
;
981 BV_FN (virtuals
) = decl
;
984 /* Create a VAR_DECL for a primary or secondary vtable for
985 CLASS_TYPE. Use NAME for the name of the vtable, and VTABLE_TYPE
989 build_vtable (class_type
, name
, vtable_type
)
996 decl
= build_lang_decl (VAR_DECL
, name
, vtable_type
);
997 DECL_CONTEXT (decl
) = class_type
;
998 DECL_ARTIFICIAL (decl
) = 1;
999 TREE_STATIC (decl
) = 1;
1000 #ifndef WRITABLE_VTABLES
1001 /* Make them READONLY by default. (mrs) */
1002 TREE_READONLY (decl
) = 1;
1004 DECL_VIRTUAL_P (decl
) = 1;
1005 import_export_vtable (decl
, class_type
, 0);
1010 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
1011 or even complete. If this does not exist, create it. If COMPLETE is
1012 non-zero, then complete the definition of it -- that will render it
1013 impossible to actually build the vtable, but is useful to get at those
1014 which are known to exist in the runtime. */
1017 get_vtable_decl (type
, complete
)
1021 tree name
= get_vtable_name (type
);
1022 tree decl
= IDENTIFIER_GLOBAL_VALUE (name
);
1026 my_friendly_assert (TREE_CODE (decl
) == VAR_DECL
1027 && DECL_VIRTUAL_P (decl
), 20000118);
1031 decl
= build_vtable (type
, name
, void_type_node
);
1032 decl
= pushdecl_top_level (decl
);
1033 SET_IDENTIFIER_GLOBAL_VALUE (name
, decl
);
1035 /* At one time the vtable info was grabbed 2 words at a time. This
1036 fails on sparc unless you have 8-byte alignment. (tiemann) */
1037 DECL_ALIGN (decl
) = MAX (TYPE_ALIGN (double_type_node
),
1041 cp_finish_decl (decl
, NULL_TREE
, NULL_TREE
, 0);
1046 /* Build the primary virtual function table for TYPE. If BINFO is
1047 non-NULL, build the vtable starting with the initial approximation
1048 that it is the same as the one which is the head of the association
1049 list. Returns a non-zero value if a new vtable is actually
1053 build_primary_vtable (binfo
, type
)
1056 tree virtuals
, decl
;
1058 decl
= get_vtable_decl (type
, /*complete=*/0);
1064 if (BINFO_NEW_VTABLE_MARKED (binfo
))
1065 /* We have already created a vtable for this base, so there's
1066 no need to do it again. */
1069 virtuals
= copy_list (BINFO_VIRTUALS (binfo
));
1070 TREE_TYPE (decl
) = TREE_TYPE (BINFO_VTABLE (binfo
));
1071 DECL_SIZE (decl
) = TYPE_SIZE (TREE_TYPE (BINFO_VTABLE (binfo
)));
1072 DECL_SIZE_UNIT (decl
)
1073 = TYPE_SIZE_UNIT (TREE_TYPE (BINFO_VTABLE (binfo
)));
1075 /* Now do rtti stuff. */
1076 offset
= get_derived_offset (TYPE_BINFO (type
), NULL_TREE
);
1077 offset
= size_diffop (size_zero_node
, offset
);
1078 set_rtti_entry (virtuals
, offset
, type
);
1082 my_friendly_assert (TREE_CODE (TREE_TYPE (decl
)) == VOID_TYPE
,
1084 virtuals
= NULL_TREE
;
1087 #ifdef GATHER_STATISTICS
1089 n_vtable_elems
+= list_length (virtuals
);
1092 /* Initialize the association list for this type, based
1093 on our first approximation. */
1094 TYPE_BINFO_VTABLE (type
) = decl
;
1095 TYPE_BINFO_VIRTUALS (type
) = virtuals
;
1097 binfo
= TYPE_BINFO (type
);
1098 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
1102 /* Give TYPE a new virtual function table which is initialized
1103 with a skeleton-copy of its original initialization. The only
1104 entry that changes is the `delta' entry, so we can really
1105 share a lot of structure.
1107 FOR_TYPE is the derived type which caused this table to
1110 BINFO is the type association which provided TYPE for FOR_TYPE.
1112 The order in which vtables are built (by calling this function) for
1113 an object must remain the same, otherwise a binary incompatibility
1117 build_secondary_vtable (binfo
, for_type
)
1118 tree binfo
, for_type
;
1121 tree orig_decl
= BINFO_VTABLE (binfo
);
1134 if (TREE_VIA_VIRTUAL (binfo
))
1135 my_friendly_assert (binfo
== BINFO_FOR_VBASE (BINFO_TYPE (binfo
),
1136 current_class_type
),
1139 if (BINFO_NEW_VTABLE_MARKED (binfo
))
1140 /* We already created a vtable for this base. There's no need to
1144 /* Remember that we've created a vtable for this BINFO, so that we
1145 don't try to do so again. */
1146 SET_BINFO_NEW_VTABLE_MARKED (binfo
);
1148 /* Make fresh virtual list, so we can smash it later. */
1149 BINFO_VIRTUALS (binfo
) = copy_list (BINFO_VIRTUALS (binfo
));
1151 if (TREE_VIA_VIRTUAL (binfo
))
1153 tree binfo1
= BINFO_FOR_VBASE (BINFO_TYPE (binfo
), for_type
);
1155 /* XXX - This should never happen, if it does, the caller should
1156 ensure that the binfo is from for_type's binfos, not from any
1157 base type's. We can remove all this code after a while. */
1158 if (binfo1
!= binfo
)
1159 warning ("internal inconsistency: binfo offset error for rtti");
1161 offset
= BINFO_OFFSET (binfo1
);
1164 offset
= BINFO_OFFSET (binfo
);
1166 set_rtti_entry (BINFO_VIRTUALS (binfo
),
1167 size_diffop (size_zero_node
, offset
),
1170 /* In the new ABI, secondary vtables are laid out as part of the
1171 same structure as the primary vtable. */
1172 if (merge_primary_and_secondary_vtables_p ())
1174 BINFO_VTABLE (binfo
) = NULL_TREE
;
1178 /* Create the declaration for the secondary vtable. */
1179 basetype
= TYPE_MAIN_VARIANT (BINFO_TYPE (binfo
));
1180 buf2
= TYPE_ASSEMBLER_NAME_STRING (basetype
);
1181 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1;
1183 /* We know that the vtable that we are going to create doesn't exist
1184 yet in the global namespace, and when we finish, it will be
1185 pushed into the global namespace. In complex MI hierarchies, we
1186 have to loop while the name we are thinking of adding is globally
1187 defined, adding more name components to the vtable name as we
1188 loop, until the name is unique. This is because in complex MI
1189 cases, we might have the same base more than once. This means
1190 that the order in which this function is called for vtables must
1191 remain the same, otherwise binary compatibility can be
1196 char *buf1
= (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type
)
1200 sprintf (buf1
, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type
), joiner
,
1202 buf
= (char *) alloca (strlen (VTABLE_NAME_PREFIX
) + strlen (buf1
) + 1);
1203 sprintf (buf
, "%s%s", VTABLE_NAME_PREFIX
, buf1
);
1204 name
= get_identifier (buf
);
1206 /* If this name doesn't clash, then we can use it, otherwise
1207 we add more to the name until it is unique. */
1209 if (! IDENTIFIER_GLOBAL_VALUE (name
))
1212 /* Set values for next loop through, if the name isn't unique. */
1214 path
= BINFO_INHERITANCE_CHAIN (path
);
1216 /* We better not run out of stuff to make it unique. */
1217 my_friendly_assert (path
!= NULL_TREE
, 368);
1219 basetype
= TYPE_MAIN_VARIANT (BINFO_TYPE (path
));
1221 if (for_type
== basetype
)
1223 /* If we run out of basetypes in the path, we have already
1224 found created a vtable with that name before, we now
1225 resort to tacking on _%d to distinguish them. */
1227 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1 + i
+ 1 + 3;
1228 buf1
= (char *) alloca (i
);
1230 sprintf (buf1
, "%s%c%s%c%d",
1231 TYPE_ASSEMBLER_NAME_STRING (basetype
), joiner
,
1233 buf
= (char *) alloca (strlen (VTABLE_NAME_PREFIX
)
1234 + strlen (buf1
) + 1);
1235 sprintf (buf
, "%s%s", VTABLE_NAME_PREFIX
, buf1
);
1236 name
= get_identifier (buf
);
1238 /* If this name doesn't clash, then we can use it,
1239 otherwise we add something different to the name until
1241 } while (++j
<= 999 && IDENTIFIER_GLOBAL_VALUE (name
));
1243 /* Hey, they really like MI don't they? Increase the 3
1244 above to 6, and the 999 to 999999. :-) */
1245 my_friendly_assert (j
<= 999, 369);
1250 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1 + i
;
1251 new_buf2
= (char *) alloca (i
);
1252 sprintf (new_buf2
, "%s%c%s",
1253 TYPE_ASSEMBLER_NAME_STRING (basetype
), joiner
, buf2
);
1257 new_decl
= build_vtable (for_type
, name
, TREE_TYPE (orig_decl
));
1258 DECL_ALIGN (new_decl
) = DECL_ALIGN (orig_decl
);
1259 BINFO_VTABLE (binfo
) = pushdecl_top_level (new_decl
);
1261 #ifdef GATHER_STATISTICS
1263 n_vtable_elems
+= list_length (BINFO_VIRTUALS (binfo
));
1269 /* Create a new vtable for BINFO which is the hierarchy dominated by
1273 make_new_vtable (t
, binfo
)
1277 if (binfo
== TYPE_BINFO (t
))
1278 /* In this case, it is *type*'s vtable we are modifying. We start
1279 with the approximation that it's vtable is that of the
1280 immediate base class. */
1281 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t
))),
1284 /* This is our very own copy of `basetype' to play with. Later,
1285 we will fill in all the virtual functions that override the
1286 virtual functions in these base classes which are not defined
1287 by the current type. */
1288 return build_secondary_vtable (binfo
, t
);
1291 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
1292 (which is in the hierarchy dominated by T) list FNDECL as its
1293 BV_FN. DELTA is the required adjustment from the `this' pointer
1294 where the vtable entry appears to the `this' required when the
1295 function is actually called. */
1298 modify_vtable_entry (t
, binfo
, fndecl
, delta
, virtuals
)
1309 vcall_index
= integer_zero_node
;
1311 if (fndecl
!= BV_FN (v
)
1312 || !tree_int_cst_equal (delta
, BV_DELTA (v
))
1313 || !tree_int_cst_equal (vcall_index
, BV_VCALL_INDEX (v
)))
1317 /* We need a new vtable for BINFO. */
1318 if (make_new_vtable (t
, binfo
))
1320 /* If we really did make a new vtable, we also made a copy
1321 of the BINFO_VIRTUALS list. Now, we have to find the
1322 corresponding entry in that list. */
1323 *virtuals
= BINFO_VIRTUALS (binfo
);
1324 while (BV_FN (*virtuals
) != BV_FN (v
))
1325 *virtuals
= TREE_CHAIN (*virtuals
);
1329 base_fndecl
= BV_FN (v
);
1330 BV_DELTA (v
) = delta
;
1331 BV_VCALL_INDEX (v
) = vcall_index
;
1334 /* Now assign virtual dispatch information, if unset. We can
1335 dispatch this, through any overridden base function. */
1336 if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
1338 DECL_VINDEX (fndecl
) = DECL_VINDEX (base_fndecl
);
1339 DECL_VIRTUAL_CONTEXT (fndecl
) = DECL_VIRTUAL_CONTEXT (base_fndecl
);
1344 /* Call this function whenever its known that a vtable for T is going
1345 to be needed. It's safe to call it more than once. *HAS_VIRTUAL_P
1346 is initialized to the number of slots that are reserved at the
1347 beginning of the vtable for RTTI information. */
1350 start_vtable (t
, has_virtual_p
)
1354 if (*has_virtual_p
== 0 && ! CLASSTYPE_COM_INTERFACE (t
))
1356 /* If we are using thunks, use two slots at the front, one
1357 for the offset pointer, one for the tdesc pointer.
1358 For ARM-style vtables, use the same slot for both. */
1359 if (flag_vtable_thunks
)
1366 /* Add a virtual function to all the appropriate vtables for the class
1367 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1368 allocate a new slot in our table. If it is error_mark_node, we
1369 know that no other function from another vtable is overridden by X.
1370 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1371 vtable for the type, and we build upon the NEW_VIRTUALS list
1375 add_virtual_function (new_virtuals_p
, overridden_virtuals_p
,
1376 has_virtual
, fndecl
, t
)
1377 tree
*new_virtuals_p
;
1378 tree
*overridden_virtuals_p
;
1381 tree t
; /* Structure type. */
1385 /* If this function doesn't override anything from a base class, we
1386 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1387 override something, we keep it around and assign its DECL_VINDEX
1388 later, in modify_all_vtables. */
1389 if (TREE_CODE (DECL_VINDEX (fndecl
)) == INTEGER_CST
)
1390 /* We've already dealt with this function. */
1393 new_virtual
= build_tree_list (integer_zero_node
, fndecl
);
1394 BV_VCALL_INDEX (new_virtual
) = integer_zero_node
;
1396 if (DECL_VINDEX (fndecl
) == error_mark_node
)
1398 /* FNDECL is a new virtual function; it doesn't override any
1399 virtual function in a base class. */
1401 /* We remember that this was the base sub-object for rtti. */
1402 CLASSTYPE_RTTI (t
) = t
;
1404 start_vtable (t
, has_virtual
);
1406 /* Now assign virtual dispatch information. */
1407 DECL_VINDEX (fndecl
) = build_shared_int_cst ((*has_virtual
)++);
1408 DECL_VIRTUAL_CONTEXT (fndecl
) = t
;
1410 /* Save the state we've computed on the NEW_VIRTUALS list. */
1411 TREE_CHAIN (new_virtual
) = *new_virtuals_p
;
1412 *new_virtuals_p
= new_virtual
;
1416 /* FNDECL overrides a function from a base class. */
1417 TREE_CHAIN (new_virtual
) = *overridden_virtuals_p
;
1418 *overridden_virtuals_p
= new_virtual
;
1422 extern struct obstack
*current_obstack
;
1424 /* Add method METHOD to class TYPE.
1426 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1427 the class type where the method should be added. */
1430 add_method (type
, fields
, method
)
1431 tree type
, *fields
, method
;
1433 int using = (DECL_CONTEXT (method
) != type
);
1435 if (fields
&& *fields
)
1436 *fields
= build_overload (method
, *fields
);
1443 if (!CLASSTYPE_METHOD_VEC (type
))
1444 /* Make a new method vector. We start with 8 entries. We must
1445 allocate at least two (for constructors and destructors), and
1446 we're going to end up with an assignment operator at some
1449 We could use a TREE_LIST for now, and convert it to a
1450 TREE_VEC in finish_struct, but we would probably waste more
1451 memory making the links in the list than we would by
1452 over-allocating the size of the vector here. Furthermore,
1453 we would complicate all the code that expects this to be a
1455 CLASSTYPE_METHOD_VEC (type
) = make_tree_vec (8);
1457 method_vec
= CLASSTYPE_METHOD_VEC (type
);
1458 len
= TREE_VEC_LENGTH (method_vec
);
1460 if (DECL_NAME (method
) == constructor_name (type
))
1461 /* A new constructor or destructor. Constructors go in
1462 slot 0; destructors go in slot 1. */
1463 slot
= DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method
)) ? 1 : 0;
1466 /* See if we already have an entry with this name. */
1467 for (slot
= 2; slot
< len
; ++slot
)
1468 if (!TREE_VEC_ELT (method_vec
, slot
)
1469 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec
,
1471 == DECL_NAME (method
)))
1476 /* We need a bigger method vector. */
1477 tree new_vec
= make_tree_vec (2 * len
);
1478 bcopy ((PTR
) &TREE_VEC_ELT (method_vec
, 0),
1479 (PTR
) &TREE_VEC_ELT (new_vec
, 0),
1480 len
* sizeof (tree
));
1482 method_vec
= CLASSTYPE_METHOD_VEC (type
) = new_vec
;
1485 if (DECL_CONV_FN_P (method
) && !TREE_VEC_ELT (method_vec
, slot
))
1487 /* Type conversion operators have to come before
1488 ordinary methods; add_conversions depends on this to
1489 speed up looking for conversion operators. So, if
1490 necessary, we slide some of the vector elements up.
1491 In theory, this makes this algorithm O(N^2) but we
1492 don't expect many conversion operators. */
1493 for (slot
= 2; slot
< len
; ++slot
)
1495 tree fn
= TREE_VEC_ELT (method_vec
, slot
);
1498 /* There are no more entries in the vector, so we
1499 can insert the new conversion operator here. */
1502 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
1503 /* We can insert the new function right at the
1508 if (!TREE_VEC_ELT (method_vec
, slot
))
1509 /* There is nothing in the Ith slot, so we can avoid
1514 /* We know the last slot in the vector is empty
1515 because we know that at this point there's room
1516 for a new function. */
1517 bcopy ((PTR
) &TREE_VEC_ELT (method_vec
, slot
),
1518 (PTR
) &TREE_VEC_ELT (method_vec
, slot
+ 1),
1519 (len
- slot
- 1) * sizeof (tree
));
1520 TREE_VEC_ELT (method_vec
, slot
) = NULL_TREE
;
1525 if (template_class_depth (type
))
1526 /* TYPE is a template class. Don't issue any errors now; wait
1527 until instantiation time to complain. */
1533 /* Check to see if we've already got this method. */
1534 for (fns
= TREE_VEC_ELT (method_vec
, slot
);
1536 fns
= OVL_NEXT (fns
))
1538 tree fn
= OVL_CURRENT (fns
);
1540 if (TREE_CODE (fn
) != TREE_CODE (method
))
1543 if (TREE_CODE (method
) != TEMPLATE_DECL
)
1545 /* [over.load] Member function declarations with the
1546 same name and the same parameter types cannot be
1547 overloaded if any of them is a static member
1548 function declaration. */
1549 if ((DECL_STATIC_FUNCTION_P (fn
)
1550 != DECL_STATIC_FUNCTION_P (method
))
1553 tree parms1
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1554 tree parms2
= TYPE_ARG_TYPES (TREE_TYPE (method
));
1556 if (! DECL_STATIC_FUNCTION_P (fn
))
1557 parms1
= TREE_CHAIN (parms1
);
1558 if (! DECL_STATIC_FUNCTION_P (method
))
1559 parms2
= TREE_CHAIN (parms2
);
1561 if (compparms (parms1
, parms2
))
1564 /* Defer to the local function. */
1567 cp_error ("`%#D' and `%#D' cannot be overloaded",
1572 /* Since this is an ordinary function in a
1573 non-template class, it's mangled name can be used
1574 as a unique identifier. This technique is only
1575 an optimization; we would get the same results if
1576 we just used decls_match here. */
1577 if (DECL_ASSEMBLER_NAME (fn
)
1578 != DECL_ASSEMBLER_NAME (method
))
1581 else if (!decls_match (fn
, method
))
1584 /* There has already been a declaration of this method
1585 or member template. */
1586 cp_error_at ("`%D' has already been declared in `%T'",
1589 /* We don't call duplicate_decls here to merge the
1590 declarations because that will confuse things if the
1591 methods have inline definitions. In particular, we
1592 will crash while processing the definitions. */
1597 /* Actually insert the new method. */
1598 TREE_VEC_ELT (method_vec
, slot
)
1599 = build_overload (method
, TREE_VEC_ELT (method_vec
, slot
));
1601 /* Add the new binding. */
1602 if (!DECL_CONSTRUCTOR_P (method
)
1603 && !DECL_DESTRUCTOR_P (method
))
1604 push_class_level_binding (DECL_NAME (method
),
1605 TREE_VEC_ELT (method_vec
, slot
));
1609 /* Subroutines of finish_struct. */
1611 /* Look through the list of fields for this struct, deleting
1612 duplicates as we go. This must be recursive to handle
1615 FIELD is the field which may not appear anywhere in FIELDS.
1616 FIELD_PTR, if non-null, is the starting point at which
1617 chained deletions may take place.
1618 The value returned is the first acceptable entry found
1621 Note that anonymous fields which are not of UNION_TYPE are
1622 not duplicates, they are just anonymous fields. This happens
1623 when we have unnamed bitfields, for example. */
1626 delete_duplicate_fields_1 (field
, fields
)
1631 if (DECL_NAME (field
) == 0)
1633 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1636 for (x
= TYPE_FIELDS (TREE_TYPE (field
)); x
; x
= TREE_CHAIN (x
))
1637 fields
= delete_duplicate_fields_1 (x
, fields
);
1642 for (x
= fields
; x
; prev
= x
, x
= TREE_CHAIN (x
))
1644 if (DECL_NAME (x
) == 0)
1646 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
1648 TYPE_FIELDS (TREE_TYPE (x
))
1649 = delete_duplicate_fields_1 (field
, TYPE_FIELDS (TREE_TYPE (x
)));
1650 if (TYPE_FIELDS (TREE_TYPE (x
)) == 0)
1653 fields
= TREE_CHAIN (fields
);
1655 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1658 else if (TREE_CODE (field
) == USING_DECL
)
1659 /* A using declaration may is allowed to appear more than
1660 once. We'll prune these from the field list later, and
1661 handle_using_decl will complain about invalid multiple
1664 else if (DECL_NAME (field
) == DECL_NAME (x
))
1666 if (TREE_CODE (field
) == CONST_DECL
1667 && TREE_CODE (x
) == CONST_DECL
)
1668 cp_error_at ("duplicate enum value `%D'", x
);
1669 else if (TREE_CODE (field
) == CONST_DECL
1670 || TREE_CODE (x
) == CONST_DECL
)
1671 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1673 else if (DECL_DECLARES_TYPE_P (field
)
1674 && DECL_DECLARES_TYPE_P (x
))
1676 if (same_type_p (TREE_TYPE (field
), TREE_TYPE (x
)))
1678 cp_error_at ("duplicate nested type `%D'", x
);
1680 else if (DECL_DECLARES_TYPE_P (field
)
1681 || DECL_DECLARES_TYPE_P (x
))
1683 /* Hide tag decls. */
1684 if ((TREE_CODE (field
) == TYPE_DECL
1685 && DECL_ARTIFICIAL (field
))
1686 || (TREE_CODE (x
) == TYPE_DECL
1687 && DECL_ARTIFICIAL (x
)))
1689 cp_error_at ("duplicate field `%D' (as type and non-type)",
1693 cp_error_at ("duplicate member `%D'", x
);
1695 fields
= TREE_CHAIN (fields
);
1697 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1705 delete_duplicate_fields (fields
)
1709 for (x
= fields
; x
&& TREE_CHAIN (x
); x
= TREE_CHAIN (x
))
1710 TREE_CHAIN (x
) = delete_duplicate_fields_1 (x
, TREE_CHAIN (x
));
1713 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1714 legit, otherwise return 0. */
1717 alter_access (t
, fdecl
, access
)
1722 tree elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1725 if (TREE_VALUE (elem
) != access
)
1727 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1728 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl
));
1730 error ("conflicting access specifications for field `%s', ignored",
1731 IDENTIFIER_POINTER (DECL_NAME (fdecl
)));
1735 /* They're changing the access to the same thing they changed
1736 it to before. That's OK. */
1742 enforce_access (t
, fdecl
);
1743 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1749 /* Process the USING_DECL, which is a member of T. */
1752 handle_using_decl (using_decl
, t
)
1756 tree ctype
= DECL_INITIAL (using_decl
);
1757 tree name
= DECL_NAME (using_decl
);
1759 = TREE_PRIVATE (using_decl
) ? access_private_node
1760 : TREE_PROTECTED (using_decl
) ? access_protected_node
1761 : access_public_node
;
1763 tree flist
= NULL_TREE
;
1766 binfo
= binfo_or_else (ctype
, t
);
1770 if (name
== constructor_name (ctype
)
1771 || name
== constructor_name_full (ctype
))
1773 cp_error_at ("using-declaration for constructor", using_decl
);
1777 fdecl
= lookup_member (binfo
, name
, 0, 0);
1781 cp_error_at ("no members matching `%D' in `%#T'", using_decl
, ctype
);
1785 if (BASELINK_P (fdecl
))
1786 /* Ignore base type this came from. */
1787 fdecl
= TREE_VALUE (fdecl
);
1789 old_value
= IDENTIFIER_CLASS_VALUE (name
);
1792 if (is_overloaded_fn (old_value
))
1793 old_value
= OVL_CURRENT (old_value
);
1795 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1798 old_value
= NULL_TREE
;
1801 if (is_overloaded_fn (fdecl
))
1803 else if (! DECL_LANG_SPECIFIC (fdecl
))
1804 my_friendly_abort (20000221);
1808 else if (is_overloaded_fn (old_value
))
1811 /* It's OK to use functions from a base when there are functions with
1812 the same name already present in the current class. */;
1815 cp_error ("`%D' invalid in `%#T'", using_decl
, t
);
1816 cp_error_at (" because of local method `%#D' with same name",
1817 OVL_CURRENT (old_value
));
1823 cp_error ("`%D' invalid in `%#T'", using_decl
, t
);
1824 cp_error_at (" because of local field `%#D' with same name", old_value
);
1828 /* Make type T see field decl FDECL with access ACCESS.*/
1830 for (; flist
; flist
= OVL_NEXT (flist
))
1832 add_method (t
, 0, OVL_CURRENT (flist
));
1833 alter_access (t
, OVL_CURRENT (flist
), access
);
1836 alter_access (t
, fdecl
, access
);
1839 /* Run through the base clases of T, updating
1840 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1841 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1845 check_bases (t
, cant_have_default_ctor_p
, cant_have_const_ctor_p
,
1848 int *cant_have_default_ctor_p
;
1849 int *cant_have_const_ctor_p
;
1850 int *no_const_asn_ref_p
;
1854 int seen_nearly_empty_base_p
;
1857 binfos
= TYPE_BINFO_BASETYPES (t
);
1858 n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
1859 seen_nearly_empty_base_p
= 0;
1861 /* An aggregate cannot have baseclasses. */
1862 CLASSTYPE_NON_AGGREGATE (t
) |= (n_baseclasses
!= 0);
1864 for (i
= 0; i
< n_baseclasses
; ++i
)
1869 /* Figure out what base we're looking at. */
1870 base_binfo
= TREE_VEC_ELT (binfos
, i
);
1871 basetype
= TREE_TYPE (base_binfo
);
1873 /* If the type of basetype is incomplete, then we already
1874 complained about that fact (and we should have fixed it up as
1876 if (TYPE_SIZE (basetype
) == 0)
1879 /* The base type is of incomplete type. It is
1880 probably best to pretend that it does not
1882 if (i
== n_baseclasses
-1)
1883 TREE_VEC_ELT (binfos
, i
) = NULL_TREE
;
1884 TREE_VEC_LENGTH (binfos
) -= 1;
1886 for (j
= i
; j
+1 < n_baseclasses
; j
++)
1887 TREE_VEC_ELT (binfos
, j
) = TREE_VEC_ELT (binfos
, j
+1);
1891 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1892 here because the case of virtual functions but non-virtual
1893 dtor is handled in finish_struct_1. */
1894 if (warn_ecpp
&& ! TYPE_POLYMORPHIC_P (basetype
)
1895 && TYPE_HAS_DESTRUCTOR (basetype
))
1896 cp_warning ("base class `%#T' has a non-virtual destructor",
1899 /* If the base class doesn't have copy constructors or
1900 assignment operators that take const references, then the
1901 derived class cannot have such a member automatically
1903 if (! TYPE_HAS_CONST_INIT_REF (basetype
))
1904 *cant_have_const_ctor_p
= 1;
1905 if (TYPE_HAS_ASSIGN_REF (basetype
)
1906 && !TYPE_HAS_CONST_ASSIGN_REF (basetype
))
1907 *no_const_asn_ref_p
= 1;
1908 /* Similarly, if the base class doesn't have a default
1909 constructor, then the derived class won't have an
1910 automatically generated default constructor. */
1911 if (TYPE_HAS_CONSTRUCTOR (basetype
)
1912 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
))
1914 *cant_have_default_ctor_p
= 1;
1915 if (! TYPE_HAS_CONSTRUCTOR (t
))
1916 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1920 /* If the base class is not empty or nearly empty, then this
1921 class cannot be nearly empty. */
1922 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype
) && !is_empty_class (basetype
))
1923 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1924 /* And if there is more than one nearly empty base, then the
1925 derived class is not nearly empty either. */
1926 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
)
1927 && seen_nearly_empty_base_p
)
1928 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1929 /* If this is the first nearly empty base class, then remember
1931 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1932 seen_nearly_empty_base_p
= 1;
1934 /* A lot of properties from the bases also apply to the derived
1936 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1937 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1938 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1939 TYPE_HAS_COMPLEX_ASSIGN_REF (t
)
1940 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype
);
1941 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (basetype
);
1942 TYPE_OVERLOADS_CALL_EXPR (t
) |= TYPE_OVERLOADS_CALL_EXPR (basetype
);
1943 TYPE_OVERLOADS_ARRAY_REF (t
) |= TYPE_OVERLOADS_ARRAY_REF (basetype
);
1944 TYPE_OVERLOADS_ARROW (t
) |= TYPE_OVERLOADS_ARROW (basetype
);
1945 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1947 /* Derived classes can implicitly become COMified if their bases
1949 if (CLASSTYPE_COM_INTERFACE (basetype
))
1950 CLASSTYPE_COM_INTERFACE (t
) = 1;
1951 else if (i
== 0 && CLASSTYPE_COM_INTERFACE (t
))
1954 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1956 CLASSTYPE_COM_INTERFACE (t
) = 0;
1961 /* Make the Ith baseclass of T its primary base. */
1964 set_primary_base (t
, i
, has_virtual_p
)
1971 CLASSTYPE_VFIELD_PARENT (t
) = i
;
1972 basetype
= BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t
));
1973 TYPE_BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (basetype
);
1974 TYPE_BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (basetype
);
1975 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
1976 CLASSTYPE_RTTI (t
) = CLASSTYPE_RTTI (basetype
);
1977 *has_virtual_p
= CLASSTYPE_VSIZE (basetype
);
1980 /* Determine the primary class for T. */
1983 determine_primary_base (t
, has_virtual_p
)
1987 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
1989 /* If there are no baseclasses, there is certainly no primary base. */
1990 if (n_baseclasses
== 0)
1995 for (i
= 0; i
< n_baseclasses
; i
++)
1997 tree base_binfo
= TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t
), i
);
1998 tree basetype
= BINFO_TYPE (base_binfo
);
2000 if (TYPE_CONTAINS_VPTR_P (basetype
))
2002 /* Even a virtual baseclass can contain our RTTI
2003 information. But, we prefer a non-virtual polymorphic
2005 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2006 CLASSTYPE_RTTI (t
) = CLASSTYPE_RTTI (basetype
);
2008 /* A virtual baseclass can't be the primary base under the
2009 old ABI. And under the new ABI we still prefer a
2010 non-virtual base. */
2011 if (TREE_VIA_VIRTUAL (base_binfo
))
2014 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2016 set_primary_base (t
, i
, has_virtual_p
);
2017 CLASSTYPE_VFIELDS (t
) = copy_list (CLASSTYPE_VFIELDS (basetype
));
2023 /* Only add unique vfields, and flatten them out as we go. */
2024 for (vfields
= CLASSTYPE_VFIELDS (basetype
);
2026 vfields
= TREE_CHAIN (vfields
))
2027 if (VF_BINFO_VALUE (vfields
) == NULL_TREE
2028 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields
)))
2029 CLASSTYPE_VFIELDS (t
)
2030 = tree_cons (base_binfo
,
2031 VF_BASETYPE_VALUE (vfields
),
2032 CLASSTYPE_VFIELDS (t
));
2034 if (*has_virtual_p
== 0)
2035 set_primary_base (t
, i
, has_virtual_p
);
2040 if (!TYPE_VFIELD (t
))
2041 CLASSTYPE_VFIELD_PARENT (t
) = -1;
2043 /* The new ABI allows for the use of a "nearly-empty" virtual base
2044 class as the primary base class if no non-virtual polymorphic
2045 base can be found. */
2046 if (flag_new_abi
&& !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
2047 for (i
= 0; i
< n_baseclasses
; ++i
)
2049 tree base_binfo
= TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t
), i
);
2050 tree basetype
= BINFO_TYPE (base_binfo
);
2052 if (TREE_VIA_VIRTUAL (base_binfo
)
2053 && CLASSTYPE_NEARLY_EMPTY_P (basetype
))
2055 set_primary_base (t
, i
, has_virtual_p
);
2056 CLASSTYPE_VFIELDS (t
) = copy_list (CLASSTYPE_VFIELDS (basetype
));
2061 /* Mark the primary base classes at this point. */
2062 mark_primary_bases (t
);
2065 /* Set memoizing fields and bits of T (and its variants) for later
2069 finish_struct_bits (t
)
2072 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
2074 /* Fix up variants (if any). */
2075 tree variants
= TYPE_NEXT_VARIANT (t
);
2078 /* These fields are in the _TYPE part of the node, not in
2079 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2080 TYPE_HAS_CONSTRUCTOR (variants
) = TYPE_HAS_CONSTRUCTOR (t
);
2081 TYPE_HAS_DESTRUCTOR (variants
) = TYPE_HAS_DESTRUCTOR (t
);
2082 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
2083 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants
)
2084 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
2086 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants
)
2087 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t
);
2088 TYPE_POLYMORPHIC_P (variants
) = TYPE_POLYMORPHIC_P (t
);
2089 TYPE_USES_VIRTUAL_BASECLASSES (variants
) = TYPE_USES_VIRTUAL_BASECLASSES (t
);
2090 /* Copy whatever these are holding today. */
2091 TYPE_MIN_VALUE (variants
) = TYPE_MIN_VALUE (t
);
2092 TYPE_MAX_VALUE (variants
) = TYPE_MAX_VALUE (t
);
2093 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
2094 TYPE_SIZE (variants
) = TYPE_SIZE (t
);
2095 TYPE_SIZE_UNIT (variants
) = TYPE_SIZE_UNIT (t
);
2096 variants
= TYPE_NEXT_VARIANT (variants
);
2099 if (n_baseclasses
&& TYPE_POLYMORPHIC_P (t
))
2100 /* For a class w/o baseclasses, `finish_struct' has set
2101 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2102 definition). Similarly for a class whose base classes do not
2103 have vtables. When neither of these is true, we might have
2104 removed abstract virtuals (by providing a definition), added
2105 some (by declaring new ones), or redeclared ones from a base
2106 class. We need to recalculate what's really an abstract virtual
2107 at this point (by looking in the vtables). */
2108 get_pure_virtuals (t
);
2112 /* Notice whether this class has type conversion functions defined. */
2113 tree binfo
= TYPE_BINFO (t
);
2114 tree binfos
= BINFO_BASETYPES (binfo
);
2117 for (i
= n_baseclasses
-1; i
>= 0; i
--)
2119 basetype
= BINFO_TYPE (TREE_VEC_ELT (binfos
, i
));
2121 TYPE_HAS_CONVERSION (t
) |= TYPE_HAS_CONVERSION (basetype
);
2125 /* If this type has a copy constructor, force its mode to be BLKmode, and
2126 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2127 be passed by invisible reference and prevent it from being returned in
2130 Also do this if the class has BLKmode but can still be returned in
2131 registers, since function_cannot_inline_p won't let us inline
2132 functions returning such a type. This affects the HP-PA. */
2133 if (! TYPE_HAS_TRIVIAL_INIT_REF (t
)
2134 || (TYPE_MODE (t
) == BLKmode
&& ! aggregate_value_p (t
)
2135 && CLASSTYPE_NON_AGGREGATE (t
)))
2138 DECL_MODE (TYPE_MAIN_DECL (t
)) = BLKmode
;
2139 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
2141 TYPE_MODE (variants
) = BLKmode
;
2142 TREE_ADDRESSABLE (variants
) = 1;
2147 /* Issue warnings about T having private constructors, but no friends,
2150 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2151 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2152 non-private static member functions. */
2155 maybe_warn_about_overly_private_class (t
)
2158 int has_member_fn
= 0;
2159 int has_nonprivate_method
= 0;
2162 if (!warn_ctor_dtor_privacy
2163 /* If the class has friends, those entities might create and
2164 access instances, so we should not warn. */
2165 || (CLASSTYPE_FRIEND_CLASSES (t
)
2166 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
2167 /* We will have warned when the template was declared; there's
2168 no need to warn on every instantiation. */
2169 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
2170 /* There's no reason to even consider warning about this
2174 /* We only issue one warning, if more than one applies, because
2175 otherwise, on code like:
2178 // Oops - forgot `public:'
2184 we warn several times about essentially the same problem. */
2186 /* Check to see if all (non-constructor, non-destructor) member
2187 functions are private. (Since there are no friends or
2188 non-private statics, we can't ever call any of the private member
2190 for (fn
= TYPE_METHODS (t
); fn
; fn
= TREE_CHAIN (fn
))
2191 /* We're not interested in compiler-generated methods; they don't
2192 provide any way to call private members. */
2193 if (!DECL_ARTIFICIAL (fn
))
2195 if (!TREE_PRIVATE (fn
))
2197 if (DECL_STATIC_FUNCTION_P (fn
))
2198 /* A non-private static member function is just like a
2199 friend; it can create and invoke private member
2200 functions, and be accessed without a class
2204 has_nonprivate_method
= 1;
2207 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
2211 if (!has_nonprivate_method
&& has_member_fn
)
2213 /* There are no non-private methods, and there's at least one
2214 private member function that isn't a constructor or
2215 destructor. (If all the private members are
2216 constructors/destructors we want to use the code below that
2217 issues error messages specifically referring to
2218 constructors/destructors.) */
2220 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
2221 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (t
); i
++)
2222 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos
, i
))
2223 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos
, i
)))
2225 has_nonprivate_method
= 1;
2228 if (!has_nonprivate_method
)
2230 cp_warning ("all member functions in class `%T' are private", t
);
2235 /* Even if some of the member functions are non-private, the class
2236 won't be useful for much if all the constructors or destructors
2237 are private: such an object can never be created or destroyed. */
2238 if (TYPE_HAS_DESTRUCTOR (t
))
2240 tree dtor
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 1);
2242 if (TREE_PRIVATE (dtor
))
2244 cp_warning ("`%#T' only defines a private destructor and has no friends",
2250 if (TYPE_HAS_CONSTRUCTOR (t
))
2252 int nonprivate_ctor
= 0;
2254 /* If a non-template class does not define a copy
2255 constructor, one is defined for it, enabling it to avoid
2256 this warning. For a template class, this does not
2257 happen, and so we would normally get a warning on:
2259 template <class T> class C { private: C(); };
2261 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2262 complete non-template or fully instantiated classes have this
2264 if (!TYPE_HAS_INIT_REF (t
))
2265 nonprivate_ctor
= 1;
2267 for (fn
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 0);
2271 tree ctor
= OVL_CURRENT (fn
);
2272 /* Ideally, we wouldn't count copy constructors (or, in
2273 fact, any constructor that takes an argument of the
2274 class type as a parameter) because such things cannot
2275 be used to construct an instance of the class unless
2276 you already have one. But, for now at least, we're
2278 if (! TREE_PRIVATE (ctor
))
2280 nonprivate_ctor
= 1;
2285 if (nonprivate_ctor
== 0)
2287 cp_warning ("`%#T' only defines private constructors and has no friends",
2294 /* Function to help qsort sort FIELD_DECLs by name order. */
2297 field_decl_cmp (x
, y
)
2300 if (DECL_NAME (*x
) == DECL_NAME (*y
))
2301 /* A nontype is "greater" than a type. */
2302 return DECL_DECLARES_TYPE_P (*y
) - DECL_DECLARES_TYPE_P (*x
);
2303 if (DECL_NAME (*x
) == NULL_TREE
)
2305 if (DECL_NAME (*y
) == NULL_TREE
)
2307 if (DECL_NAME (*x
) < DECL_NAME (*y
))
2312 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2315 method_name_cmp (m1
, m2
)
2316 const tree
*m1
, *m2
;
2318 if (*m1
== NULL_TREE
&& *m2
== NULL_TREE
)
2320 if (*m1
== NULL_TREE
)
2322 if (*m2
== NULL_TREE
)
2324 if (DECL_NAME (OVL_CURRENT (*m1
)) < DECL_NAME (OVL_CURRENT (*m2
)))
2329 /* Warn about duplicate methods in fn_fields. Also compact method
2330 lists so that lookup can be made faster.
2332 Data Structure: List of method lists. The outer list is a
2333 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2334 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2335 links the entire list of methods for TYPE_METHODS. Friends are
2336 chained in the same way as member functions (? TREE_CHAIN or
2337 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2338 list. That allows them to be quickly deleted, and requires no
2341 If there are any constructors/destructors, they are moved to the
2342 front of the list. This makes pushclass more efficient.
2344 @@ The above comment is obsolete. It mostly describes what add_method
2345 @@ and add_implicitly_declared_members do.
2347 Sort methods that are not special (i.e., constructors, destructors, and
2348 type conversion operators) so that we can find them faster in search. */
2351 finish_struct_methods (t
)
2356 tree ctor_name
= constructor_name (t
);
2359 if (!TYPE_METHODS (t
))
2361 /* Clear these for safety; perhaps some parsing error could set
2362 these incorrectly. */
2363 TYPE_HAS_CONSTRUCTOR (t
) = 0;
2364 TYPE_HAS_DESTRUCTOR (t
) = 0;
2365 CLASSTYPE_METHOD_VEC (t
) = NULL_TREE
;
2369 method_vec
= CLASSTYPE_METHOD_VEC (t
);
2370 my_friendly_assert (method_vec
!= NULL_TREE
, 19991215);
2371 len
= TREE_VEC_LENGTH (method_vec
);
2373 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2374 and the next few with type conversion operators (if any). */
2375 for (fn_fields
= TYPE_METHODS (t
); fn_fields
;
2376 fn_fields
= TREE_CHAIN (fn_fields
))
2378 tree fn_name
= DECL_NAME (fn_fields
);
2380 /* Clear out this flag.
2382 @@ Doug may figure out how to break
2383 @@ this with nested classes and friends. */
2384 DECL_IN_AGGR_P (fn_fields
) = 0;
2386 /* Note here that a copy ctor is private, so we don't dare generate
2387 a default copy constructor for a class that has a member
2388 of this type without making sure they have access to it. */
2389 if (fn_name
== ctor_name
)
2391 tree parmtypes
= FUNCTION_ARG_CHAIN (fn_fields
);
2392 tree parmtype
= parmtypes
? TREE_VALUE (parmtypes
) : void_type_node
;
2394 if (TREE_CODE (parmtype
) == REFERENCE_TYPE
2395 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype
)) == t
)
2397 if (TREE_CHAIN (parmtypes
) == NULL_TREE
2398 || TREE_CHAIN (parmtypes
) == void_list_node
2399 || TREE_PURPOSE (TREE_CHAIN (parmtypes
)))
2401 if (TREE_PROTECTED (fn_fields
))
2402 TYPE_HAS_NONPUBLIC_CTOR (t
) = 1;
2403 else if (TREE_PRIVATE (fn_fields
))
2404 TYPE_HAS_NONPUBLIC_CTOR (t
) = 2;
2408 else if (fn_name
== ansi_opname
[(int) MODIFY_EXPR
])
2410 tree parmtype
= TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields
));
2412 if (copy_assignment_arg_p (parmtype
, DECL_VIRTUAL_P (fn_fields
)))
2414 if (TREE_PROTECTED (fn_fields
))
2415 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t
) = 1;
2416 else if (TREE_PRIVATE (fn_fields
))
2417 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t
) = 2;
2422 if (TYPE_HAS_DESTRUCTOR (t
) && !TREE_VEC_ELT (method_vec
, 1))
2423 /* We thought there was a destructor, but there wasn't. Some
2424 parse errors cause this anomalous situation. */
2425 TYPE_HAS_DESTRUCTOR (t
) = 0;
2427 /* Issue warnings about private constructors and such. If there are
2428 no methods, then some public defaults are generated. */
2429 maybe_warn_about_overly_private_class (t
);
2431 /* Now sort the methods. */
2432 while (len
> 2 && TREE_VEC_ELT (method_vec
, len
-1) == NULL_TREE
)
2434 TREE_VEC_LENGTH (method_vec
) = len
;
2436 /* The type conversion ops have to live at the front of the vec, so we
2438 for (slot
= 2; slot
< len
; ++slot
)
2440 tree fn
= TREE_VEC_ELT (method_vec
, slot
);
2442 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
2446 qsort (&TREE_VEC_ELT (method_vec
, slot
), len
-slot
, sizeof (tree
),
2447 (int (*)(const void *, const void *))method_name_cmp
);
2450 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2453 duplicate_tag_error (t
)
2456 cp_error ("redefinition of `%#T'", t
);
2457 cp_error_at ("previous definition here", t
);
2459 /* Pretend we haven't defined this type. */
2461 /* All of the component_decl's were TREE_CHAINed together in the parser.
2462 finish_struct_methods walks these chains and assembles all methods with
2463 the same base name into DECL_CHAINs. Now we don't need the parser chains
2464 anymore, so we unravel them. */
2466 /* This used to be in finish_struct, but it turns out that the
2467 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2469 if (CLASSTYPE_METHOD_VEC (t
))
2471 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2472 int i
, len
= TREE_VEC_LENGTH (method_vec
);
2473 for (i
= 0; i
< len
; i
++)
2475 tree unchain
= TREE_VEC_ELT (method_vec
, i
);
2476 while (unchain
!= NULL_TREE
)
2478 TREE_CHAIN (OVL_CURRENT (unchain
)) = NULL_TREE
;
2479 unchain
= OVL_NEXT (unchain
);
2484 if (TYPE_LANG_SPECIFIC (t
))
2486 tree binfo
= TYPE_BINFO (t
);
2487 int interface_only
= CLASSTYPE_INTERFACE_ONLY (t
);
2488 int interface_unknown
= CLASSTYPE_INTERFACE_UNKNOWN (t
);
2489 tree template_info
= CLASSTYPE_TEMPLATE_INFO (t
);
2490 int use_template
= CLASSTYPE_USE_TEMPLATE (t
);
2492 bzero ((char *) TYPE_LANG_SPECIFIC (t
), sizeof (struct lang_type
));
2493 BINFO_BASETYPES(binfo
) = NULL_TREE
;
2495 TYPE_BINFO (t
) = binfo
;
2496 CLASSTYPE_INTERFACE_ONLY (t
) = interface_only
;
2497 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t
, interface_unknown
);
2498 TYPE_REDEFINED (t
) = 1;
2499 CLASSTYPE_TEMPLATE_INFO (t
) = template_info
;
2500 CLASSTYPE_USE_TEMPLATE (t
) = use_template
;
2502 TYPE_SIZE (t
) = NULL_TREE
;
2503 TYPE_MODE (t
) = VOIDmode
;
2504 TYPE_FIELDS (t
) = NULL_TREE
;
2505 TYPE_METHODS (t
) = NULL_TREE
;
2506 TYPE_VFIELD (t
) = NULL_TREE
;
2507 TYPE_CONTEXT (t
) = NULL_TREE
;
2508 TYPE_NONCOPIED_PARTS (t
) = NULL_TREE
;
2511 /* Make the BINFO's vtablehave N entries, including RTTI entries,
2512 vbase and vcall offsets, etc. Set its type and call the backend
2516 layout_vtable_decl (binfo
, n
)
2523 itype
= size_int (n
);
2524 atype
= build_cplus_array_type (vtable_entry_type
,
2525 build_index_type (itype
));
2526 layout_type (atype
);
2528 /* We may have to grow the vtable. */
2529 if (!same_type_p (TREE_TYPE (BINFO_VTABLE (binfo
)), atype
))
2531 tree vtable
= BINFO_VTABLE (binfo
);
2533 TREE_TYPE (vtable
) = atype
;
2534 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = 0;
2535 layout_decl (vtable
, 0);
2537 /* At one time the vtable info was grabbed 2 words at a time. This
2538 fails on Sparc unless you have 8-byte alignment. */
2539 DECL_ALIGN (vtable
) = MAX (TYPE_ALIGN (double_type_node
),
2540 DECL_ALIGN (vtable
));
2544 /* Returns the number of virtual function table entries (excluding
2545 RTTI information, vbase and vcall offests, etc.) in the vtable for
2549 num_vfun_entries (binfo
)
2552 return list_length (skip_rtti_stuff (binfo
,
2557 /* Called from num_extra_vtbl_entries via dfs_walk. */
2560 dfs_count_virtuals (binfo
, data
)
2564 /* Non-primary bases are not interesting; all of the virtual
2565 function table entries have been overridden. */
2566 if (!BINFO_PRIMARY_MARKED_P (binfo
))
2567 ((vcall_offset_data
*) data
)->offsets
+= num_vfun_entries (binfo
);
2572 /* Returns the number of extra entries (at negative indices) required
2573 for BINFO's vtable. */
2576 num_extra_vtbl_entries (binfo
)
2582 type
= BINFO_TYPE (binfo
);
2585 /* There is an entry for the offset to each virtual base. */
2586 if (vbase_offsets_in_vtable_p ())
2587 entries
+= list_length (CLASSTYPE_VBASECLASSES (type
));
2589 /* If this is a virtual base, there are entries for each virtual
2590 function defined in this class or its bases. */
2591 if (vcall_offsets_in_vtable_p () && TREE_VIA_VIRTUAL (binfo
))
2593 vcall_offset_data vod
;
2599 dfs_vcall_offset_queue_p
,
2601 entries
+= vod
.offsets
;
2604 return entries
? size_int (entries
) : size_zero_node
;
2607 /* Returns the offset (in bytes) from the beginning of BINFO's vtable
2608 where the vptr should actually point. */
2611 size_extra_vtbl_entries (binfo
)
2614 tree offset
= size_binop (MULT_EXPR
, TYPE_SIZE_UNIT (vtable_entry_type
),
2615 num_extra_vtbl_entries (binfo
));
2616 return fold (offset
);
2619 /* Construct the initializer for BINFOs virtual function table. BINFO
2620 is part of the hierarchy dominated by T. The value returned is a
2621 TREE_LIST suitable for wrapping in a CONSTRUCTOR to use as the
2622 DECL_INITIAL for a vtable. */
2625 build_vtbl_initializer (binfo
, t
)
2629 tree v
= BINFO_VIRTUALS (binfo
);
2630 tree inits
= NULL_TREE
;
2631 tree type
= BINFO_TYPE (binfo
);
2633 /* Add entries to the vtable that indicate how to adjust the this
2634 pointer when calling a virtual function in this class. */
2635 inits
= build_vcall_offset_vtbl_entries (binfo
, t
);
2637 /* Add entries to the vtable for offsets to our virtual bases. */
2638 inits
= chainon (build_vbase_offset_vtbl_entries (binfo
, t
),
2641 /* Process the RTTI stuff at the head of the list. If we're not
2642 using vtable thunks, then the RTTI entry is just an ordinary
2643 function, and we can process it just like the other virtual
2644 function entries. */
2645 if (!CLASSTYPE_COM_INTERFACE (type
) && flag_vtable_thunks
)
2650 /* The first entry is an offset. */
2651 offset
= TREE_PURPOSE (v
);
2652 my_friendly_assert (TREE_CODE (offset
) == INTEGER_CST
,
2655 /* Convert the offset to look like a function pointer, so that
2656 we can put it in the vtable. */
2657 init
= build1 (NOP_EXPR
, vfunc_ptr_type_node
, offset
);
2658 TREE_CONSTANT (init
) = 1;
2659 inits
= tree_cons (NULL_TREE
, init
, inits
);
2663 if (new_abi_rtti_p ())
2665 tree decl
= TREE_VALUE (v
);
2668 decl
= build_unary_op (ADDR_EXPR
, decl
, 0);
2670 decl
= integer_zero_node
;
2671 decl
= build1 (NOP_EXPR
, vfunc_ptr_type_node
, decl
);
2672 TREE_CONSTANT (decl
) = 1;
2673 decl
= build_vtable_entry (integer_zero_node
, integer_zero_node
,
2675 inits
= tree_cons (NULL_TREE
, decl
, inits
);
2679 /* In the old abi the second entry (the tdesc pointer) is
2680 just an ordinary function, so it can be dealt with like the
2681 virtual functions. */
2684 /* Go through all the ordinary virtual functions, building up
2694 /* Pull the offset for `this', and the function to call, out of
2696 delta
= BV_DELTA (v
);
2697 vcall_index
= BV_VCALL_INDEX (v
);
2699 my_friendly_assert (TREE_CODE (delta
) == INTEGER_CST
, 19990727);
2700 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 19990727);
2702 /* You can't call an abstract virtual function; it's abstract.
2703 So, we replace these functions with __pure_virtual. */
2704 if (DECL_PURE_VIRTUAL_P (fn
))
2707 /* Take the address of the function, considering it to be of an
2708 appropriate generic type. */
2709 pfn
= build1 (ADDR_EXPR
, vfunc_ptr_type_node
, fn
);
2710 /* The address of a function can't change. */
2711 TREE_CONSTANT (pfn
) = 1;
2712 /* Enter it in the vtable. */
2713 init
= build_vtable_entry (delta
, vcall_index
, pfn
);
2714 /* And add it to the chain of initializers. */
2715 inits
= tree_cons (NULL_TREE
, init
, inits
);
2721 /* The initializers were built up in reverse order; straighten them
2723 return nreverse (inits
);
2726 /* Initialize the vtable for BINFO with the INITS. */
2729 initialize_vtable (binfo
, inits
)
2736 layout_vtable_decl (binfo
, list_length (inits
));
2737 decl
= BINFO_VTABLE (binfo
);
2738 context
= DECL_CONTEXT (decl
);
2739 DECL_CONTEXT (decl
) = 0;
2740 DECL_INITIAL (decl
) = build_nt (CONSTRUCTOR
, NULL_TREE
, inits
);
2741 cp_finish_decl (decl
, DECL_INITIAL (decl
), NULL_TREE
, 0);
2742 DECL_CONTEXT (decl
) = context
;
2745 /* Called from finish_vtbls via dfs_walk. */
2748 dfs_finish_vtbls (binfo
, data
)
2752 if (!BINFO_PRIMARY_MARKED_P (binfo
)
2753 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
))
2754 && BINFO_NEW_VTABLE_MARKED (binfo
))
2755 initialize_vtable (binfo
,
2756 build_vtbl_initializer (binfo
, (tree
) data
));
2758 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
);
2759 SET_BINFO_MARKED (binfo
);
2764 /* Called from finish_vtbls via dfs_walk when using the new ABI.
2765 Accumulates the vtable initializers for all of the vtables into
2766 TREE_VALUE (DATA). */
2769 dfs_accumulate_vtbl_inits (binfo
, data
)
2773 if (!BINFO_PRIMARY_MARKED_P (binfo
)
2774 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
))
2775 && BINFO_NEW_VTABLE_MARKED (binfo
))
2781 t
= TREE_PURPOSE (l
);
2783 /* If this is a secondary vtable, record its location. */
2784 if (binfo
!= TYPE_BINFO (t
))
2788 vtbl
= TYPE_BINFO_VTABLE (t
);
2789 vtbl
= build1 (ADDR_EXPR
,
2790 build_pointer_type (TREE_TYPE (vtbl
)),
2792 BINFO_VTABLE (binfo
)
2793 = build (PLUS_EXPR
, TREE_TYPE (vtbl
), vtbl
,
2794 size_binop (MULT_EXPR
,
2795 TYPE_SIZE_UNIT (TREE_TYPE (vtbl
)),
2796 size_int (list_length (TREE_VALUE (l
)))));
2799 /* Add the initializers for this vtable to the initailizers for
2800 the other vtables we've already got. */
2802 = chainon (TREE_VALUE (l
),
2803 build_vtbl_initializer (binfo
, t
));
2806 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo
);
2807 SET_BINFO_MARKED (binfo
);
2812 /* Create all the necessary vtables for T and its base classes. */
2818 if (merge_primary_and_secondary_vtables_p ())
2822 /* Under the new ABI, we lay out the primary and secondary
2823 vtables in one contiguous vtable. The primary vtable is
2824 first, followed by the secondary vtables as encountered in a
2825 pre-order depth-first left-to-right traversal. */
2826 list
= build_tree_list (t
, NULL_TREE
);
2827 dfs_walk_real (TYPE_BINFO (t
),
2828 dfs_accumulate_vtbl_inits
,
2830 dfs_unmarked_real_bases_queue_p
,
2832 if (TYPE_BINFO_VTABLE (t
))
2833 initialize_vtable (TYPE_BINFO (t
), TREE_VALUE (list
));
2836 dfs_walk (TYPE_BINFO (t
), dfs_finish_vtbls
,
2837 dfs_unmarked_real_bases_queue_p
, t
);
2839 dfs_walk (TYPE_BINFO (t
), dfs_unmark
,
2840 dfs_marked_real_bases_queue_p
, t
);
2843 /* True if we should override the given BASE_FNDECL with the given
2847 overrides (fndecl
, base_fndecl
)
2848 tree fndecl
, base_fndecl
;
2850 /* Destructors have special names. */
2851 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl
))
2852 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl
)))
2854 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl
))
2855 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl
)))
2857 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
))
2859 tree types
, base_types
;
2861 retypes
= TREE_TYPE (TREE_TYPE (fndecl
));
2862 base_retypes
= TREE_TYPE (TREE_TYPE (base_fndecl
));
2864 types
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2865 base_types
= TYPE_ARG_TYPES (TREE_TYPE (base_fndecl
));
2866 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types
)))
2867 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types
))))
2868 && compparms (TREE_CHAIN (base_types
), TREE_CHAIN (types
)))
2874 typedef struct find_final_overrider_data_s
{
2875 /* The function for which we are trying to find a final overrider. */
2877 /* The base class in which the function was declared. */
2878 tree declaring_base
;
2879 /* The most derived class in the hierarchy. */
2880 tree most_derived_type
;
2881 /* The final overriding function. */
2883 /* The BINFO for the class in which the final overriding function
2885 tree overriding_base
;
2886 } find_final_overrider_data
;
2888 /* Called from find_final_overrider via dfs_walk. */
2891 dfs_find_final_overrider (binfo
, data
)
2895 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2897 if (same_type_p (BINFO_TYPE (binfo
),
2898 BINFO_TYPE (ffod
->declaring_base
))
2899 && tree_int_cst_equal (BINFO_OFFSET (binfo
),
2900 BINFO_OFFSET (ffod
->declaring_base
)))
2905 /* We've found a path to the declaring base. Walk down the path
2906 looking for an overrider for FN. */
2907 for (path
= reverse_path (binfo
);
2909 path
= TREE_CHAIN (path
))
2911 for (method
= TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path
)));
2913 method
= TREE_CHAIN (method
))
2914 if (DECL_VIRTUAL_P (method
) && overrides (method
, ffod
->fn
))
2921 /* If we found an overrider, record the overriding function, and
2922 the base from which it came. */
2925 if (ffod
->overriding_fn
&& ffod
->overriding_fn
!= method
)
2927 /* We've found a different overrider along a different
2928 path. That can be OK if the new one overrides the
2931 struct S { virtual void f(); };
2932 struct T : public virtual S { virtual void f(); };
2933 struct U : public virtual S, public virtual T {};
2935 Here `T::f' is the final overrider for `S::f'. */
2936 if (strictly_overrides (method
, ffod
->overriding_fn
))
2938 ffod
->overriding_fn
= method
;
2939 ffod
->overriding_base
= TREE_VALUE (path
);
2941 else if (!strictly_overrides (ffod
->overriding_fn
, method
))
2943 cp_error ("no unique final overrider for `%D' in `%T'",
2944 ffod
->most_derived_type
,
2946 cp_error ("candidates are: `%#D'", ffod
->overriding_fn
);
2947 cp_error (" `%#D'", method
);
2948 return error_mark_node
;
2951 else if (ffod
->overriding_base
2952 && (!tree_int_cst_equal
2953 (BINFO_OFFSET (TREE_VALUE (path
)),
2954 BINFO_OFFSET (ffod
->overriding_base
))))
2956 /* We've found two instances of the same base that
2957 provide overriders. */
2958 cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'",
2960 BINFO_TYPE (ffod
->overriding_base
),
2961 ffod
->most_derived_type
);
2962 return error_mark_node
;
2966 ffod
->overriding_fn
= method
;
2967 ffod
->overriding_base
= TREE_VALUE (path
);
2975 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2976 FN and whose TREE_VALUE is the binfo for the base where the
2977 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2978 base object in which FN is declared. */
2981 find_final_overrider (t
, binfo
, fn
)
2986 find_final_overrider_data ffod
;
2988 /* Getting this right is a little tricky. This is legal:
2990 struct S { virtual void f (); };
2991 struct T { virtual void f (); };
2992 struct U : public S, public T { };
2994 even though calling `f' in `U' is ambiguous. But,
2996 struct R { virtual void f(); };
2997 struct S : virtual public R { virtual void f (); };
2998 struct T : virtual public R { virtual void f (); };
2999 struct U : public S, public T { };
3001 is not -- there's no way to decide whether to put `S::f' or
3002 `T::f' in the vtable for `R'.
3004 The solution is to look at all paths to BINFO. If we find
3005 different overriders along any two, then there is a problem. */
3007 ffod
.declaring_base
= binfo
;
3008 ffod
.most_derived_type
= t
;
3009 ffod
.overriding_fn
= NULL_TREE
;
3010 ffod
.overriding_base
= NULL_TREE
;
3012 if (dfs_walk (TYPE_BINFO (t
),
3013 dfs_find_final_overrider
,
3016 return error_mark_node
;
3018 return build_tree_list (ffod
.overriding_fn
, ffod
.overriding_base
);
3021 /* Return the BINFO_VIRTUALS list for BINFO, without the RTTI stuff at
3022 the front. If non-NULL, N is set to the number of entries
3026 skip_rtti_stuff (binfo
, t
, n
)
3033 if (CLASSTYPE_COM_INTERFACE (t
))
3038 virtuals
= BINFO_VIRTUALS (binfo
);
3041 /* We always reserve a slot for the offset/tdesc entry. */
3044 virtuals
= TREE_CHAIN (virtuals
);
3046 if (flag_vtable_thunks
&& virtuals
)
3048 /* The second slot is reserved for the tdesc pointer when thunks
3052 virtuals
= TREE_CHAIN (virtuals
);
3058 /* Called via dfs_walk. Returns BINFO if BINFO has the same type as
3059 DATA (which is really an _TYPE node). */
3062 dfs_find_base (binfo
, data
)
3066 return (same_type_p (BINFO_TYPE (binfo
), (tree
) data
)
3067 ? binfo
: NULL_TREE
);
3070 /* Called from modify_all_vtables via dfs_walk. */
3073 dfs_modify_vtables (binfo
, data
)
3077 if (/* There's no need to modify the vtable for a primary base;
3078 we're not going to use that vtable anyhow. */
3079 !BINFO_PRIMARY_MARKED_P (binfo
)
3080 /* Similarly, a base without a vtable needs no modification. */
3081 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
3089 /* If we're supporting RTTI then we always need a new vtable to
3090 point to the RTTI information. Under the new ABI we may need
3091 a new vtable to contain vcall and vbase offsets. */
3092 if (flag_rtti
|| flag_new_abi
)
3093 make_new_vtable (t
, binfo
);
3095 /* Now, go through each of the virtual functions in the virtual
3096 function table for BINFO. Find the final overrider, and
3097 update the BINFO_VIRTUALS list appropriately. */
3098 for (virtuals
= skip_rtti_stuff (binfo
, BINFO_TYPE (binfo
), NULL
),
3099 old_virtuals
= skip_rtti_stuff (TYPE_BINFO (BINFO_TYPE (binfo
)),
3103 virtuals
= TREE_CHAIN (virtuals
),
3104 old_virtuals
= TREE_CHAIN (old_virtuals
))
3111 unsigned HOST_WIDE_INT i
;
3113 /* Find the function which originally caused this vtable
3114 entry to be present. */
3115 fn
= BV_FN (old_virtuals
);
3116 vindex
= DECL_VINDEX (fn
);
3117 b
= dfs_walk (binfo
, dfs_find_base
, NULL
, DECL_VIRTUAL_CONTEXT (fn
));
3118 fn
= skip_rtti_stuff (TYPE_BINFO (BINFO_TYPE (b
)),
3121 while (i
< TREE_INT_CST_LOW (vindex
))
3123 fn
= TREE_CHAIN (fn
);
3128 /* Handle the case of a virtual function defined in BINFO
3130 overrider
= find_final_overrider (t
, b
, fn
);
3131 if (overrider
== error_mark_node
)
3134 /* The `this' pointer needs to be adjusted from pointing to
3135 BINFO to pointing at the base where the final overrider
3137 delta
= size_binop (PLUS_EXPR
,
3138 get_derived_offset (binfo
,
3139 DECL_VIRTUAL_CONTEXT (fn
)),
3140 BINFO_OFFSET (binfo
));
3141 delta
= size_diffop (BINFO_OFFSET (TREE_VALUE (overrider
)), delta
);
3143 modify_vtable_entry (t
,
3145 TREE_PURPOSE (overrider
),
3151 SET_BINFO_MARKED (binfo
);
3156 /* Update all of the primary and secondary vtables for T. Create new
3157 vtables as required, and initialize their RTTI information. Each
3158 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
3159 function from a base class; find and modify the appropriate entries
3160 to point to the overriding functions. Returns a list, in
3161 declaration order, of the functions that are overridden in this
3162 class, but do not appear in the primary base class vtable, and
3163 which should therefore be appended to the end of the vtable for T. */
3166 modify_all_vtables (t
, has_virtual_p
, overridden_virtuals
)
3169 tree overridden_virtuals
;
3173 binfo
= TYPE_BINFO (t
);
3175 /* Update all of the vtables. */
3178 dfs_unmarked_real_bases_queue_p
,
3180 dfs_walk (binfo
, dfs_unmark
, dfs_marked_real_bases_queue_p
, t
);
3182 /* If we should include overriding functions for secondary vtables
3183 in our primary vtable, add them now. */
3184 if (all_overridden_vfuns_in_vtables_p ())
3186 tree
*fnsp
= &overridden_virtuals
;
3190 tree fn
= TREE_VALUE (*fnsp
);
3192 if (BINFO_VIRTUALS (binfo
)
3193 && !value_member (fn
, BINFO_VIRTUALS (binfo
)))
3195 /* We know we need a vtable for this class now. */
3196 start_vtable (t
, has_virtual_p
);
3197 /* Set the vtable index. */
3199 = build_shared_int_cst ((*has_virtual_p
)++);
3200 /* We don't need to convert to a base class when calling
3202 DECL_VIRTUAL_CONTEXT (fn
) = t
;
3204 /* We don't need to adjust the `this' pointer when
3205 calling this function. */
3206 BV_DELTA (*fnsp
) = integer_zero_node
;
3207 BV_VCALL_INDEX (*fnsp
) = integer_zero_node
;
3209 /* This is an overridden function not already in our
3211 fnsp
= &TREE_CHAIN (*fnsp
);
3214 /* We've already got an entry for this function. Skip
3216 *fnsp
= TREE_CHAIN (*fnsp
);
3220 overridden_virtuals
= NULL_TREE
;
3222 return overridden_virtuals
;
3225 /* Here, we already know that they match in every respect.
3226 All we have to check is where they had their declarations. */
3229 strictly_overrides (fndecl1
, fndecl2
)
3230 tree fndecl1
, fndecl2
;
3232 int distance
= get_base_distance (DECL_CONTEXT (fndecl2
),
3233 DECL_CONTEXT (fndecl1
),
3235 if (distance
== -2 || distance
> 0)
3240 /* Get the base virtual function declarations in T that are either
3241 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
3242 the overrider/hider. */
3245 get_basefndecls (fndecl
, t
)
3248 tree methods
= TYPE_METHODS (t
);
3249 tree base_fndecls
= NULL_TREE
;
3250 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
3251 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
3255 if (TREE_CODE (methods
) == FUNCTION_DECL
3256 && DECL_VINDEX (methods
) != NULL_TREE
3257 && DECL_NAME (fndecl
) == DECL_NAME (methods
))
3258 base_fndecls
= tree_cons (fndecl
, methods
, base_fndecls
);
3260 methods
= TREE_CHAIN (methods
);
3264 return base_fndecls
;
3266 for (i
= 0; i
< n_baseclasses
; i
++)
3268 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
3269 tree basetype
= BINFO_TYPE (base_binfo
);
3271 base_fndecls
= chainon (get_basefndecls (fndecl
, basetype
),
3275 return base_fndecls
;
3278 /* Mark the functions that have been hidden with their overriders.
3279 Since we start out with all functions already marked with a hider,
3280 no need to mark functions that are just hidden.
3282 Subroutine of warn_hidden. */
3285 mark_overriders (fndecl
, base_fndecls
)
3286 tree fndecl
, base_fndecls
;
3288 for (; base_fndecls
; base_fndecls
= TREE_CHAIN (base_fndecls
))
3290 if (overrides (fndecl
, TREE_VALUE (base_fndecls
)))
3291 TREE_PURPOSE (base_fndecls
) = fndecl
;
3295 /* If this declaration supersedes the declaration of
3296 a method declared virtual in the base class, then
3297 mark this field as being virtual as well. */
3300 check_for_override (decl
, ctype
)
3303 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (ctype
));
3304 int i
, n_baselinks
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
3305 int virtualp
= DECL_VIRTUAL_P (decl
);
3306 int found_overriden_fn
= 0;
3308 for (i
= 0; i
< n_baselinks
; i
++)
3310 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
3311 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo
)))
3313 tree tmp
= get_matching_virtual
3315 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl
)));
3317 if (tmp
&& !found_overriden_fn
)
3319 /* If this function overrides some virtual in some base
3320 class, then the function itself is also necessarily
3321 virtual, even if the user didn't explicitly say so. */
3322 DECL_VIRTUAL_P (decl
) = 1;
3324 /* The TMP we really want is the one from the deepest
3325 baseclass on this path, taking care not to
3326 duplicate if we have already found it (via another
3327 path to its virtual baseclass. */
3328 if (TREE_CODE (TREE_TYPE (decl
)) == FUNCTION_TYPE
)
3330 cp_error_at ("`static %#D' cannot be declared", decl
);
3331 cp_error_at (" since `virtual %#D' declared in base class",
3337 /* Set DECL_VINDEX to a value that is neither an
3338 INTEGER_CST nor the error_mark_node so that
3339 add_virtual_function will realize this is an
3340 overridden function. */
3342 = tree_cons (tmp
, NULL_TREE
, DECL_VINDEX (decl
));
3344 /* We now know that DECL overrides something,
3345 which is all that is important. But, we must
3346 continue to iterate through all the base-classes
3347 in order to allow get_matching_virtual to check for
3348 various illegal overrides. */
3349 found_overriden_fn
= 1;
3355 if (DECL_VINDEX (decl
) == NULL_TREE
)
3356 DECL_VINDEX (decl
) = error_mark_node
;
3357 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = 1;
3361 /* Warn about hidden virtual functions that are not overridden in t.
3362 We know that constructors and destructors don't apply. */
3368 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
3369 int n_methods
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
3372 /* We go through each separately named virtual function. */
3373 for (i
= 2; i
< n_methods
&& TREE_VEC_ELT (method_vec
, i
); ++i
)
3375 tree fns
= TREE_VEC_ELT (method_vec
, i
);
3376 tree fndecl
= NULL_TREE
;
3378 tree base_fndecls
= NULL_TREE
;
3379 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
3380 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
3382 /* First see if we have any virtual functions in this batch. */
3383 for (; fns
; fns
= OVL_NEXT (fns
))
3385 fndecl
= OVL_CURRENT (fns
);
3386 if (DECL_VINDEX (fndecl
))
3390 if (fns
== NULL_TREE
)
3393 /* First we get a list of all possible functions that might be
3394 hidden from each base class. */
3395 for (i
= 0; i
< n_baseclasses
; i
++)
3397 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
3398 tree basetype
= BINFO_TYPE (base_binfo
);
3400 base_fndecls
= chainon (get_basefndecls (fndecl
, basetype
),
3404 fns
= OVL_NEXT (fns
);
3406 /* ...then mark up all the base functions with overriders, preferring
3407 overriders to hiders. */
3409 for (; fns
; fns
= OVL_NEXT (fns
))
3411 fndecl
= OVL_CURRENT (fns
);
3412 if (DECL_VINDEX (fndecl
))
3413 mark_overriders (fndecl
, base_fndecls
);
3416 /* Now give a warning for all base functions without overriders,
3417 as they are hidden. */
3418 for (; base_fndecls
; base_fndecls
= TREE_CHAIN (base_fndecls
))
3420 if (! overrides (TREE_PURPOSE (base_fndecls
),
3421 TREE_VALUE (base_fndecls
)))
3423 /* Here we know it is a hider, and no overrider exists. */
3424 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls
));
3425 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls
));
3431 /* Check for things that are invalid. There are probably plenty of other
3432 things we should check for also. */
3435 finish_struct_anon (t
)
3440 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
3442 if (TREE_STATIC (field
))
3444 if (TREE_CODE (field
) != FIELD_DECL
)
3447 if (DECL_NAME (field
) == NULL_TREE
3448 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
3450 tree elt
= TYPE_FIELDS (TREE_TYPE (field
));
3451 for (; elt
; elt
= TREE_CHAIN (elt
))
3453 if (DECL_ARTIFICIAL (elt
))
3456 if (DECL_NAME (elt
) == constructor_name (t
))
3457 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
3460 if (TREE_CODE (elt
) != FIELD_DECL
)
3462 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3467 if (TREE_PRIVATE (elt
))
3468 cp_pedwarn_at ("private member `%#D' in anonymous union",
3470 else if (TREE_PROTECTED (elt
))
3471 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3474 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
3475 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
3481 extern int interface_only
, interface_unknown
;
3483 /* Create default constructors, assignment operators, and so forth for
3484 the type indicated by T, if they are needed.
3485 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3486 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3487 cannot have a default constructor, copy constructor taking a const
3488 reference argument, or an assignment operator, respectively. If a
3489 virtual destructor is created, its DECL is returned; otherwise the
3490 return value is NULL_TREE. */
3493 add_implicitly_declared_members (t
, cant_have_default_ctor
,
3494 cant_have_const_cctor
,
3495 cant_have_assignment
)
3497 int cant_have_default_ctor
;
3498 int cant_have_const_cctor
;
3499 int cant_have_assignment
;
3502 tree implicit_fns
= NULL_TREE
;
3503 tree name
= TYPE_IDENTIFIER (t
);
3504 tree virtual_dtor
= NULL_TREE
;
3508 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) && !TYPE_HAS_DESTRUCTOR (t
))
3510 default_fn
= cons_up_default_function (t
, name
, 0);
3511 check_for_override (default_fn
, t
);
3513 /* If we couldn't make it work, then pretend we didn't need it. */
3514 if (default_fn
== void_type_node
)
3515 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = 0;
3518 TREE_CHAIN (default_fn
) = implicit_fns
;
3519 implicit_fns
= default_fn
;
3521 if (DECL_VINDEX (default_fn
))
3522 virtual_dtor
= default_fn
;
3526 /* Any non-implicit destructor is non-trivial. */
3527 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) |= TYPE_HAS_DESTRUCTOR (t
);
3529 /* Default constructor. */
3530 if (! TYPE_HAS_CONSTRUCTOR (t
) && ! cant_have_default_ctor
)
3532 default_fn
= cons_up_default_function (t
, name
, 2);
3533 TREE_CHAIN (default_fn
) = implicit_fns
;
3534 implicit_fns
= default_fn
;
3537 /* Copy constructor. */
3538 if (! TYPE_HAS_INIT_REF (t
) && ! TYPE_FOR_JAVA (t
))
3540 /* ARM 12.18: You get either X(X&) or X(const X&), but
3542 default_fn
= cons_up_default_function (t
, name
,
3543 3 + cant_have_const_cctor
);
3544 TREE_CHAIN (default_fn
) = implicit_fns
;
3545 implicit_fns
= default_fn
;
3548 /* Assignment operator. */
3549 if (! TYPE_HAS_ASSIGN_REF (t
) && ! TYPE_FOR_JAVA (t
))
3551 default_fn
= cons_up_default_function (t
, name
,
3552 5 + cant_have_assignment
);
3553 TREE_CHAIN (default_fn
) = implicit_fns
;
3554 implicit_fns
= default_fn
;
3557 /* Now, hook all of the new functions on to TYPE_METHODS,
3558 and add them to the CLASSTYPE_METHOD_VEC. */
3559 for (f
= &implicit_fns
; *f
; f
= &TREE_CHAIN (*f
))
3560 add_method (t
, 0, *f
);
3561 *f
= TYPE_METHODS (t
);
3562 TYPE_METHODS (t
) = implicit_fns
;
3564 return virtual_dtor
;
3567 /* Subroutine of finish_struct_1. Recursively count the number of fields
3568 in TYPE, including anonymous union members. */
3571 count_fields (fields
)
3576 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3578 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3579 n_fields
+= count_fields (TYPE_FIELDS (TREE_TYPE (x
)));
3586 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3587 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3590 add_fields_to_vec (fields
, field_vec
, idx
)
3591 tree fields
, field_vec
;
3595 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3597 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3598 idx
= add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x
)), field_vec
, idx
);
3600 TREE_VEC_ELT (field_vec
, idx
++) = x
;
3605 /* FIELD is a bit-field. We are finishing the processing for its
3606 enclosing type. Issue any appropriate messages and set appropriate
3610 check_bitfield_decl (field
)
3613 tree type
= TREE_TYPE (field
);
3615 /* Invalid bit-field size done by grokfield. */
3616 /* Detect invalid bit-field type. Simply checking if TYPE is
3617 integral is insufficient, as that is the array core of the field
3618 type. If TREE_TYPE (field) is integral, then TYPE must be the same. */
3619 if (DECL_INITIAL (field
)
3620 && ! INTEGRAL_TYPE_P (TREE_TYPE (field
)))
3622 cp_error_at ("bit-field `%#D' with non-integral type", field
);
3623 DECL_INITIAL (field
) = NULL
;
3626 /* Detect and ignore out of range field width. */
3627 if (DECL_INITIAL (field
))
3629 tree w
= DECL_INITIAL (field
);
3631 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3634 /* detect invalid field size. */
3635 if (TREE_CODE (w
) == CONST_DECL
)
3636 w
= DECL_INITIAL (w
);
3637 else if (TREE_READONLY_DECL_P (w
))
3638 w
= decl_constant_value (w
);
3640 if (TREE_CODE (w
) != INTEGER_CST
)
3642 cp_error_at ("bit-field `%D' width not an integer constant",
3644 DECL_INITIAL (field
) = NULL_TREE
;
3646 else if (tree_int_cst_sgn (w
) < 0)
3648 DECL_INITIAL (field
) = NULL
;
3649 cp_error_at ("negative width in bit-field `%D'", field
);
3651 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3653 DECL_INITIAL (field
) = NULL
;
3654 cp_error_at ("zero width for bit-field `%D'", field
);
3656 else if (0 < compare_tree_int (w
,
3658 (long_long_unsigned_type_node
)))
3660 /* The backend will dump if you try to use something too
3662 DECL_INITIAL (field
) = NULL
;
3663 sorry ("bit-fields larger than %d bits",
3664 TYPE_PRECISION (long_long_unsigned_type_node
));
3665 cp_error_at (" in declaration of `%D'", field
);
3667 else if (compare_tree_int (w
, TYPE_PRECISION (type
)) > 0
3668 && TREE_CODE (type
) != ENUMERAL_TYPE
3669 && TREE_CODE (type
) != BOOLEAN_TYPE
)
3670 cp_warning_at ("width of `%D' exceeds its type", field
);
3671 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3672 && (0 > compare_tree_int (w
,
3673 min_precision (TYPE_MIN_VALUE (type
),
3674 TREE_UNSIGNED (type
)))
3675 || 0 > compare_tree_int (w
,
3677 (TYPE_MAX_VALUE (type
),
3678 TREE_UNSIGNED (type
)))))
3679 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3682 if (DECL_INITIAL (field
))
3684 DECL_INITIAL (field
) = NULL_TREE
;
3685 DECL_SIZE (field
) = bitsize_int (TREE_INT_CST_LOW (w
));
3686 DECL_BIT_FIELD (field
) = 1;
3688 if (integer_zerop (w
))
3690 #ifdef EMPTY_FIELD_BOUNDARY
3691 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
),
3692 EMPTY_FIELD_BOUNDARY
);
3694 #ifdef PCC_BITFIELD_TYPE_MATTERS
3695 if (PCC_BITFIELD_TYPE_MATTERS
)
3696 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
),
3703 /* Non-bit-fields are aligned for their type. */
3704 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
), TYPE_ALIGN (type
));
3707 /* FIELD is a non bit-field. We are finishing the processing for its
3708 enclosing type T. Issue any appropriate messages and set appropriate
3712 check_field_decl (field
, t
, cant_have_const_ctor
,
3713 cant_have_default_ctor
, no_const_asn_ref
,
3714 any_default_members
)
3717 int *cant_have_const_ctor
;
3718 int *cant_have_default_ctor
;
3719 int *no_const_asn_ref
;
3720 int *any_default_members
;
3722 tree type
= strip_array_types (TREE_TYPE (field
));
3724 /* An anonymous union cannot contain any fields which would change
3725 the settings of CANT_HAVE_CONST_CTOR and friends. */
3726 if (ANON_UNION_TYPE_P (type
))
3728 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3729 structs. So, we recurse through their fields here. */
3730 else if (ANON_AGGR_TYPE_P (type
))
3734 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3735 if (TREE_CODE (field
) == FIELD_DECL
&& !DECL_C_BIT_FIELD (field
))
3736 check_field_decl (fields
, t
, cant_have_const_ctor
,
3737 cant_have_default_ctor
, no_const_asn_ref
,
3738 any_default_members
);
3740 /* Check members with class type for constructors, destructors,
3742 else if (CLASS_TYPE_P (type
))
3744 /* Never let anything with uninheritable virtuals
3745 make it through without complaint. */
3746 abstract_virtuals_error (field
, type
);
3748 if (TREE_CODE (t
) == UNION_TYPE
)
3750 if (TYPE_NEEDS_CONSTRUCTING (type
))
3751 cp_error_at ("member `%#D' with constructor not allowed in union",
3753 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3754 cp_error_at ("member `%#D' with destructor not allowed in union",
3756 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type
))
3757 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3762 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3763 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3764 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3765 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type
);
3766 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (type
);
3769 if (!TYPE_HAS_CONST_INIT_REF (type
))
3770 *cant_have_const_ctor
= 1;
3772 if (!TYPE_HAS_CONST_ASSIGN_REF (type
))
3773 *no_const_asn_ref
= 1;
3775 if (TYPE_HAS_CONSTRUCTOR (type
)
3776 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
3777 *cant_have_default_ctor
= 1;
3779 if (DECL_INITIAL (field
) != NULL_TREE
)
3781 /* `build_class_init_list' does not recognize
3783 if (TREE_CODE (t
) == UNION_TYPE
&& any_default_members
!= 0)
3784 cp_error_at ("multiple fields in union `%T' initialized");
3785 *any_default_members
= 1;
3788 /* Non-bit-fields are aligned for their type, except packed fields
3789 which require only BITS_PER_UNIT alignment. */
3790 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
),
3791 (DECL_PACKED (field
)
3793 : TYPE_ALIGN (TREE_TYPE (field
))));
3796 /* Check the data members (both static and non-static), class-scoped
3797 typedefs, etc., appearing in the declaration of T. Issue
3798 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3799 declaration order) of access declarations; each TREE_VALUE in this
3800 list is a USING_DECL.
3802 In addition, set the following flags:
3805 The class is empty, i.e., contains no non-static data members.
3807 CANT_HAVE_DEFAULT_CTOR_P
3808 This class cannot have an implicitly generated default
3811 CANT_HAVE_CONST_CTOR_P
3812 This class cannot have an implicitly generated copy constructor
3813 taking a const reference.
3815 CANT_HAVE_CONST_ASN_REF
3816 This class cannot have an implicitly generated assignment
3817 operator taking a const reference.
3819 All of these flags should be initialized before calling this
3822 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3823 fields can be added by adding to this chain. */
3826 check_field_decls (t
, access_decls
, empty_p
,
3827 cant_have_default_ctor_p
, cant_have_const_ctor_p
,
3832 int *cant_have_default_ctor_p
;
3833 int *cant_have_const_ctor_p
;
3834 int *no_const_asn_ref_p
;
3839 int any_default_members
;
3841 /* First, delete any duplicate fields. */
3842 delete_duplicate_fields (TYPE_FIELDS (t
));
3844 /* Assume there are no access declarations. */
3845 *access_decls
= NULL_TREE
;
3846 /* Assume this class has no pointer members. */
3848 /* Assume none of the members of this class have default
3850 any_default_members
= 0;
3852 for (field
= &TYPE_FIELDS (t
); *field
; field
= next
)
3855 tree type
= TREE_TYPE (x
);
3857 GNU_xref_member (current_class_name
, x
);
3859 next
= &TREE_CHAIN (x
);
3861 if (TREE_CODE (x
) == FIELD_DECL
)
3863 DECL_PACKED (x
) |= TYPE_PACKED (t
);
3865 if (DECL_C_BIT_FIELD (x
) && integer_zerop (DECL_INITIAL (x
)))
3866 /* We don't treat zero-width bitfields as making a class
3871 /* The class is non-empty. */
3873 /* The class is not even nearly empty. */
3874 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3878 if (TREE_CODE (x
) == USING_DECL
)
3880 /* Prune the access declaration from the list of fields. */
3881 *field
= TREE_CHAIN (x
);
3883 /* Save the access declarations for our caller. */
3884 *access_decls
= tree_cons (NULL_TREE
, x
, *access_decls
);
3886 /* Since we've reset *FIELD there's no reason to skip to the
3892 if (TREE_CODE (x
) == TYPE_DECL
3893 || TREE_CODE (x
) == TEMPLATE_DECL
)
3896 /* If we've gotten this far, it's a data member, possibly static,
3897 or an enumerator. */
3899 DECL_FIELD_CONTEXT (x
) = t
;
3901 /* ``A local class cannot have static data members.'' ARM 9.4 */
3902 if (current_function_decl
&& TREE_STATIC (x
))
3903 cp_error_at ("field `%D' in local class cannot be static", x
);
3905 /* Perform error checking that did not get done in
3907 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3909 cp_error_at ("field `%D' invalidly declared function type",
3911 type
= build_pointer_type (type
);
3912 TREE_TYPE (x
) = type
;
3914 else if (TREE_CODE (type
) == METHOD_TYPE
)
3916 cp_error_at ("field `%D' invalidly declared method type", x
);
3917 type
= build_pointer_type (type
);
3918 TREE_TYPE (x
) = type
;
3920 else if (TREE_CODE (type
) == OFFSET_TYPE
)
3922 cp_error_at ("field `%D' invalidly declared offset type", x
);
3923 type
= build_pointer_type (type
);
3924 TREE_TYPE (x
) = type
;
3927 if (type
== error_mark_node
)
3930 DECL_SAVED_INSNS (x
) = 0;
3932 /* When this goes into scope, it will be a non-local reference. */
3933 DECL_NONLOCAL (x
) = 1;
3935 if (TREE_CODE (x
) == CONST_DECL
)
3938 if (TREE_CODE (x
) == VAR_DECL
)
3940 if (TREE_CODE (t
) == UNION_TYPE
)
3941 /* Unions cannot have static members. */
3942 cp_error_at ("field `%D' declared static in union", x
);
3947 /* Now it can only be a FIELD_DECL. */
3949 if (TREE_PRIVATE (x
) || TREE_PROTECTED (x
))
3950 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3952 /* If this is of reference type, check if it needs an init.
3953 Also do a little ANSI jig if necessary. */
3954 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3956 CLASSTYPE_NON_POD_P (t
) = 1;
3957 if (DECL_INITIAL (x
) == NULL_TREE
)
3958 CLASSTYPE_REF_FIELDS_NEED_INIT (t
) = 1;
3960 /* ARM $12.6.2: [A member initializer list] (or, for an
3961 aggregate, initialization by a brace-enclosed list) is the
3962 only way to initialize nonstatic const and reference
3964 *cant_have_default_ctor_p
= 1;
3965 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
3967 if (! TYPE_HAS_CONSTRUCTOR (t
) && extra_warnings
)
3970 cp_warning_at ("non-static reference `%#D' in class without a constructor", x
);
3972 cp_warning_at ("non-static reference in class without a constructor", x
);
3976 type
= strip_array_types (type
);
3978 if (TREE_CODE (type
) == POINTER_TYPE
)
3981 if (DECL_MUTABLE_P (x
) || TYPE_HAS_MUTABLE_P (type
))
3982 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3984 if (! pod_type_p (type
)
3985 /* For some reason, pointers to members are POD types themselves,
3986 but are not allowed in POD structs. Silly. */
3987 || TYPE_PTRMEM_P (type
) || TYPE_PTRMEMFUNC_P (type
))
3988 CLASSTYPE_NON_POD_P (t
) = 1;
3990 /* If any field is const, the structure type is pseudo-const. */
3991 if (CP_TYPE_CONST_P (type
))
3993 C_TYPE_FIELDS_READONLY (t
) = 1;
3994 if (DECL_INITIAL (x
) == NULL_TREE
)
3995 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
) = 1;
3997 /* ARM $12.6.2: [A member initializer list] (or, for an
3998 aggregate, initialization by a brace-enclosed list) is the
3999 only way to initialize nonstatic const and reference
4001 *cant_have_default_ctor_p
= 1;
4002 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
4004 if (! TYPE_HAS_CONSTRUCTOR (t
) && extra_warnings
)
4007 cp_warning_at ("non-static const member `%#D' in class without a constructor", x
);
4009 cp_warning_at ("non-static const member in class without a constructor", x
);
4012 /* A field that is pseudo-const makes the structure likewise. */
4013 else if (IS_AGGR_TYPE (type
))
4015 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
4016 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
4017 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
);
4020 /* We set DECL_C_BIT_FIELD in grokbitfield.
4021 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4022 if (DECL_C_BIT_FIELD (x
))
4023 check_bitfield_decl (x
);
4025 check_field_decl (x
, t
,
4026 cant_have_const_ctor_p
,
4027 cant_have_default_ctor_p
,
4029 &any_default_members
);
4032 /* Effective C++ rule 11. */
4033 if (has_pointers
&& warn_ecpp
&& TYPE_HAS_CONSTRUCTOR (t
)
4034 && ! (TYPE_HAS_INIT_REF (t
) && TYPE_HAS_ASSIGN_REF (t
)))
4036 cp_warning ("`%#T' has pointer data members", t
);
4038 if (! TYPE_HAS_INIT_REF (t
))
4040 cp_warning (" but does not override `%T(const %T&)'", t
, t
);
4041 if (! TYPE_HAS_ASSIGN_REF (t
))
4042 cp_warning (" or `operator=(const %T&)'", t
);
4044 else if (! TYPE_HAS_ASSIGN_REF (t
))
4045 cp_warning (" but does not override `operator=(const %T&)'", t
);
4049 /* Check anonymous struct/anonymous union fields. */
4050 finish_struct_anon (t
);
4052 /* We've built up the list of access declarations in reverse order.
4054 *access_decls
= nreverse (*access_decls
);
4057 /* Return a FIELD_DECL for a pointer-to-virtual-table or
4058 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
4059 field are as indicated. The CLASS_TYPE in which this field occurs
4060 is also indicated. FCONTEXT is the type that is needed for the debug
4061 info output routines. *EMPTY_P is set to a non-zero value by this
4062 function to indicate that a class containing this field is
4066 build_vtbl_or_vbase_field (name
, assembler_name
, type
, class_type
, fcontext
,
4069 tree assembler_name
;
4077 /* This class is non-empty. */
4080 /* Build the FIELD_DECL. */
4081 field
= build_lang_decl (FIELD_DECL
, name
, type
);
4082 DECL_ASSEMBLER_NAME (field
) = assembler_name
;
4083 DECL_VIRTUAL_P (field
) = 1;
4084 DECL_ARTIFICIAL (field
) = 1;
4085 DECL_FIELD_CONTEXT (field
) = class_type
;
4086 DECL_FCONTEXT (field
) = fcontext
;
4087 DECL_SAVED_INSNS (field
) = 0;
4088 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
4094 /* If the empty base field in DECL overlaps with a base of the same type in
4095 NEWDECL, which is either another base field or the first data field of
4096 the class, pad the base just before NEWDECL and return 1. Otherwise,
4100 avoid_overlap (decl
, newdecl
, empty_p
)
4106 if (newdecl
== NULL_TREE
4107 || ! types_overlap_p (TREE_TYPE (decl
), TREE_TYPE (newdecl
)))
4110 for (field
= decl
; TREE_CHAIN (field
) && TREE_CHAIN (field
) != newdecl
;
4111 field
= TREE_CHAIN (field
))
4114 DECL_SIZE (field
) = bitsize_int (1);
4115 DECL_SIZE_UNIT (field
) = 0;
4116 /* The containing class cannot be empty; this field takes up space. */
4122 /* Build a FIELD_DECL for the base given by BINFO in T. If the new
4123 object is non-empty, clear *EMPTY_P. Otherwise, set *SAW_EMPTY_P.
4124 *BASE_ALIGN is a running maximum of the alignments of any base
4128 build_base_field (t
, binfo
, empty_p
, saw_empty_p
, base_align
)
4133 unsigned int *base_align
;
4135 tree basetype
= BINFO_TYPE (binfo
);
4138 if (TYPE_SIZE (basetype
) == 0)
4139 /* This error is now reported in xref_tag, thus giving better
4140 location information. */
4143 decl
= build_lang_decl (FIELD_DECL
, NULL_TREE
, basetype
);
4144 DECL_ARTIFICIAL (decl
) = 1;
4145 DECL_FIELD_CONTEXT (decl
) = t
;
4146 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
4147 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
4148 DECL_ALIGN (decl
) = CLASSTYPE_ALIGN (basetype
);
4150 if (flag_new_abi
&& integer_zerop (DECL_SIZE (decl
)))
4156 /* The containing class is non-empty because it has a non-empty base
4162 /* Brain damage for backwards compatibility. For no good
4163 reason, the old layout_basetypes made every base at least
4164 as large as the alignment for the bases up to that point,
4165 gratuitously wasting space. So we do the same thing
4167 *base_align
= MAX (*base_align
, DECL_ALIGN (decl
));
4169 = size_int (MAX ((HOST_WIDE_INT
) TREE_INT_CST_LOW (DECL_SIZE (decl
)),
4170 (int) (*base_align
)));
4171 DECL_SIZE_UNIT (decl
)
4172 = size_int (MAX (((HOST_WIDE_INT
) TREE_INT_CST_LOW
4173 (DECL_SIZE_UNIT (decl
))),
4174 (int) *base_align
/ BITS_PER_UNIT
));
4180 /* Returns a list of fields to stand in for the base class subobjects
4181 of REC. These fields are later removed by layout_basetypes. */
4184 build_base_fields (rec
, empty_p
)
4188 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4190 tree base_decls
= NULL_TREE
;
4191 int n_baseclasses
= CLASSTYPE_N_BASECLASSES (rec
);
4192 tree decl
, nextdecl
;
4193 int i
, saw_empty
= 0;
4194 unsigned int base_align
= 0;
4196 /* Under the new ABI, the primary base class is always allocated
4198 if (flag_new_abi
&& CLASSTYPE_HAS_PRIMARY_BASE_P (rec
))
4202 primary_base
= CLASSTYPE_PRIMARY_BINFO (rec
);
4203 base_decls
= chainon (build_base_field (rec
,
4211 /* Now allocate the rest of the bases. */
4212 for (i
= 0; i
< n_baseclasses
; ++i
)
4216 /* Under the new ABI, the primary base was already allocated
4217 above, so we don't need to allocate it again here. */
4218 if (flag_new_abi
&& i
== CLASSTYPE_VFIELD_PARENT (rec
))
4221 base_binfo
= BINFO_BASETYPE (TYPE_BINFO (rec
), i
);
4223 /* A primary virtual base class is allocated just like any other
4224 base class, but a non-primary virtual base is allocated
4225 later, in layout_basetypes. */
4226 if (TREE_VIA_VIRTUAL (base_binfo
)
4227 && !BINFO_PRIMARY_MARKED_P (base_binfo
))
4230 base_decls
= chainon (build_base_field (rec
, base_binfo
,
4237 /* Reverse the list of fields so we allocate the bases in the proper
4239 base_decls
= nreverse (base_decls
);
4241 /* In the presence of empty base classes, we run the risk of allocating
4242 two objects of the same class on top of one another. Avoid that. */
4243 if (flag_new_abi
&& saw_empty
)
4244 for (decl
= base_decls
; decl
; decl
= TREE_CHAIN (decl
))
4246 if (integer_zerop (DECL_SIZE (decl
)))
4248 /* First step through the following bases until we find
4249 an overlap or a non-empty base. */
4250 for (nextdecl
= TREE_CHAIN (decl
); nextdecl
;
4251 nextdecl
= TREE_CHAIN (nextdecl
))
4252 if (avoid_overlap (decl
, nextdecl
, empty_p
)
4253 || ! integer_zerop (DECL_SIZE (nextdecl
)))
4256 /* If we're still looking, also check against the first
4258 for (nextdecl
= TYPE_FIELDS (rec
);
4259 nextdecl
&& TREE_CODE (nextdecl
) != FIELD_DECL
;
4260 nextdecl
= TREE_CHAIN (nextdecl
))
4262 avoid_overlap (decl
, nextdecl
, empty_p
);
4270 /* Go through the TYPE_METHODS of T issuing any appropriate
4271 diagnostics, figuring out which methods override which other
4272 methods, and so forth. */
4279 int seen_one_arg_array_delete_p
= 0;
4281 for (x
= TYPE_METHODS (t
); x
; x
= TREE_CHAIN (x
))
4283 GNU_xref_member (current_class_name
, x
);
4285 /* If this was an evil function, don't keep it in class. */
4286 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x
)))
4289 DECL_SAVED_INSNS (x
) = 0;
4290 check_for_override (x
, t
);
4291 if (DECL_PURE_VIRTUAL_P (x
) && ! DECL_VINDEX (x
))
4292 cp_error_at ("initializer specified for non-virtual method `%D'", x
);
4294 /* The name of the field is the original field name
4295 Save this in auxiliary field for later overloading. */
4296 if (DECL_VINDEX (x
))
4298 TYPE_POLYMORPHIC_P (t
) = 1;
4299 if (DECL_PURE_VIRTUAL_P (x
))
4300 CLASSTYPE_PURE_VIRTUALS (t
)
4301 = tree_cons (NULL_TREE
, x
, CLASSTYPE_PURE_VIRTUALS (t
));
4304 if (DECL_ARRAY_DELETE_OPERATOR_P (x
))
4308 /* When dynamically allocating an array of this type, we
4309 need a "cookie" to record how many elements we allocated,
4310 even if the array elements have no non-trivial
4311 destructor, if the usual array deallocation function
4312 takes a second argument of type size_t. The standard (in
4313 [class.free]) requires that the second argument be set
4315 second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x
)));
4316 /* This is overly conservative, but we must maintain this
4317 behavior for backwards compatibility. */
4318 if (!flag_new_abi
&& second_parm
!= void_list_node
)
4319 TYPE_VEC_DELETE_TAKES_SIZE (t
) = 1;
4320 /* Under the new ABI, we choose only those function that are
4321 explicitly declared as `operator delete[] (void *,
4323 else if (flag_new_abi
4324 && !seen_one_arg_array_delete_p
4326 && TREE_CHAIN (second_parm
) == void_list_node
4327 && same_type_p (TREE_VALUE (second_parm
), sizetype
))
4328 TYPE_VEC_DELETE_TAKES_SIZE (t
) = 1;
4329 /* If there's no second parameter, then this is the usual
4330 deallocation function. */
4331 else if (second_parm
== void_list_node
)
4332 seen_one_arg_array_delete_p
= 1;
4337 /* Remove all zero-width bit-fields from T. */
4340 remove_zero_width_bit_fields (t
)
4345 fieldsp
= &TYPE_FIELDS (t
);
4348 if (TREE_CODE (*fieldsp
) == FIELD_DECL
4349 && DECL_C_BIT_FIELD (*fieldsp
)
4350 && DECL_INITIAL (*fieldsp
))
4351 *fieldsp
= TREE_CHAIN (*fieldsp
);
4353 fieldsp
= &TREE_CHAIN (*fieldsp
);
4357 /* Check the validity of the bases and members declared in T. Add any
4358 implicitly-generated functions (like copy-constructors and
4359 assignment operators). Compute various flag bits (like
4360 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4361 level: i.e., independently of the ABI in use. */
4364 check_bases_and_members (t
, empty_p
)
4368 /* Nonzero if we are not allowed to generate a default constructor
4370 int cant_have_default_ctor
;
4371 /* Nonzero if the implicitly generated copy constructor should take
4372 a non-const reference argument. */
4373 int cant_have_const_ctor
;
4374 /* Nonzero if the the implicitly generated assignment operator
4375 should take a non-const reference argument. */
4376 int no_const_asn_ref
;
4379 /* By default, we use const reference arguments and generate default
4381 cant_have_default_ctor
= 0;
4382 cant_have_const_ctor
= 0;
4383 no_const_asn_ref
= 0;
4385 /* Assume that the class is nearly empty; we'll clear this flag if
4386 it turns out not to be nearly empty. */
4387 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
4389 /* Check all the base-classes. */
4390 check_bases (t
, &cant_have_default_ctor
, &cant_have_const_ctor
,
4393 /* Check all the data member declarations. */
4394 check_field_decls (t
, &access_decls
, empty_p
,
4395 &cant_have_default_ctor
,
4396 &cant_have_const_ctor
,
4399 /* Check all the method declarations. */
4402 /* A nearly-empty class has to be vptr-containing; a nearly empty
4403 class contains just a vptr. */
4404 if (!TYPE_CONTAINS_VPTR_P (t
))
4405 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4407 /* Do some bookkeeping that will guide the generation of implicitly
4408 declared member functions. */
4409 TYPE_HAS_COMPLEX_INIT_REF (t
)
4410 |= (TYPE_HAS_INIT_REF (t
)
4411 || TYPE_USES_VIRTUAL_BASECLASSES (t
)
4412 || TYPE_POLYMORPHIC_P (t
));
4413 TYPE_NEEDS_CONSTRUCTING (t
)
4414 |= (TYPE_HAS_CONSTRUCTOR (t
)
4415 || TYPE_USES_VIRTUAL_BASECLASSES (t
)
4416 || TYPE_POLYMORPHIC_P (t
));
4417 CLASSTYPE_NON_AGGREGATE (t
) |= (TYPE_HAS_CONSTRUCTOR (t
)
4418 || TYPE_POLYMORPHIC_P (t
));
4419 CLASSTYPE_NON_POD_P (t
)
4420 |= (CLASSTYPE_NON_AGGREGATE (t
) || TYPE_HAS_DESTRUCTOR (t
)
4421 || TYPE_HAS_ASSIGN_REF (t
));
4422 TYPE_HAS_REAL_ASSIGN_REF (t
) |= TYPE_HAS_ASSIGN_REF (t
);
4423 TYPE_HAS_COMPLEX_ASSIGN_REF (t
)
4424 |= TYPE_HAS_ASSIGN_REF (t
) || TYPE_USES_VIRTUAL_BASECLASSES (t
);
4426 /* Synthesize any needed methods. Note that methods will be synthesized
4427 for anonymous unions; grok_x_components undoes that. */
4428 add_implicitly_declared_members (t
, cant_have_default_ctor
,
4429 cant_have_const_ctor
,
4432 /* Process the using-declarations. */
4433 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
4434 handle_using_decl (TREE_VALUE (access_decls
), t
);
4436 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4437 finish_struct_methods (t
);
4440 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4441 accordingly, and, if necessary, add the TYPE_VFIELD to the
4442 TYPE_FIELDS list. */
4445 create_vtable_ptr (t
, empty_p
, has_virtual_p
,
4446 new_virtuals_p
, overridden_virtuals_p
)
4450 tree
*new_virtuals_p
;
4451 tree
*overridden_virtuals_p
;
4455 /* Loop over the virtual functions, adding them to our various
4457 for (fn
= TYPE_METHODS (t
); fn
; fn
= TREE_CHAIN (fn
))
4458 if (DECL_VINDEX (fn
))
4459 add_virtual_function (new_virtuals_p
, overridden_virtuals_p
,
4460 has_virtual_p
, fn
, t
);
4462 /* Even if there weren't any new virtual functions, we might need a
4463 new virtual function table if we're supposed to include vptrs in
4464 all classes that need them. */
4465 if (TYPE_CONTAINS_VPTR_P (t
) && vptrs_present_everywhere_p ())
4466 start_vtable (t
, has_virtual_p
);
4468 /* If we couldn't find an appropriate base class, create a new field
4470 if (*has_virtual_p
&& !TYPE_VFIELD (t
))
4472 /* We build this decl with vtbl_ptr_type_node, which is a
4473 `vtable_entry_type*'. It might seem more precise to use
4474 `vtable_entry_type (*)[N]' where N is the number of firtual
4475 functions. However, that would require the vtable pointer in
4476 base classes to have a different type than the vtable pointer
4477 in derived classes. We could make that happen, but that
4478 still wouldn't solve all the problems. In particular, the
4479 type-based alias analysis code would decide that assignments
4480 to the base class vtable pointer can't alias assignments to
4481 the derived class vtable pointer, since they have different
4482 types. Thus, in an derived class destructor, where the base
4483 class constructor was inlined, we could generate bad code for
4484 setting up the vtable pointer.
4486 Therefore, we use one type for all vtable pointers. We still
4487 use a type-correct type; it's just doesn't indicate the array
4488 bounds. That's better than using `void*' or some such; it's
4489 cleaner, and it let's the alias analysis code know that these
4490 stores cannot alias stores to void*! */
4492 = build_vtbl_or_vbase_field (get_vfield_name (t
),
4493 get_identifier (VFIELD_BASE
),
4499 /* Add the new field to the list of fields in this class. */
4501 /* In the old ABI, the vtable pointer goes at the end of the
4503 TYPE_FIELDS (t
) = chainon (TYPE_FIELDS (t
), TYPE_VFIELD (t
));
4506 /* But in the new ABI, the vtable pointer is the first thing
4508 TYPE_FIELDS (t
) = chainon (TYPE_VFIELD (t
), TYPE_FIELDS (t
));
4509 /* If there were any baseclasses, they can't possibly be at
4510 offset zero any more, because that's where the vtable
4511 pointer is. So, converting to a base class is going to
4513 if (CLASSTYPE_N_BASECLASSES (t
))
4514 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t
) = 1;
4517 /* We can't yet add this new field to the list of all virtual
4518 function table pointers in this class. The
4519 modify_all_vtables function depends on this not being done.
4520 So, it is done later, in finish_struct_1. */
4524 /* Fixup the inline function given by INFO now that the class is
4528 fixup_pending_inline (info
)
4529 struct pending_inline
*info
;
4534 tree fn
= info
->fndecl
;
4536 args
= DECL_ARGUMENTS (fn
);
4539 DECL_CONTEXT (args
) = fn
;
4540 args
= TREE_CHAIN (args
);
4545 /* Fixup the inline methods and friends in TYPE now that TYPE is
4549 fixup_inline_methods (type
)
4552 tree method
= TYPE_METHODS (type
);
4554 if (method
&& TREE_CODE (method
) == TREE_VEC
)
4556 if (TREE_VEC_ELT (method
, 1))
4557 method
= TREE_VEC_ELT (method
, 1);
4558 else if (TREE_VEC_ELT (method
, 0))
4559 method
= TREE_VEC_ELT (method
, 0);
4561 method
= TREE_VEC_ELT (method
, 2);
4564 /* Do inline member functions. */
4565 for (; method
; method
= TREE_CHAIN (method
))
4566 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method
));
4569 for (method
= CLASSTYPE_INLINE_FRIENDS (type
);
4571 method
= TREE_CHAIN (method
))
4572 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method
)));
4573 CLASSTYPE_INLINE_FRIENDS (type
) = NULL_TREE
;
4576 /* Called from propagate_binfo_offsets via dfs_walk. */
4579 dfs_propagate_binfo_offsets (binfo
, data
)
4583 tree offset
= (tree
) data
;
4585 /* Update the BINFO_OFFSET for this base. */
4586 BINFO_OFFSET (binfo
) = size_binop (PLUS_EXPR
, BINFO_OFFSET (binfo
), offset
);
4588 SET_BINFO_MARKED (binfo
);
4593 /* Add OFFSET to all base types of BINFO which is a base in the
4594 hierarchy dominated by T.
4596 OFFSET, which is a type offset, is number of bytes.
4598 Note that we don't have to worry about having two paths to the
4599 same base type, since this type owns its association list. */
4602 propagate_binfo_offsets (binfo
, offset
)
4607 dfs_propagate_binfo_offsets
,
4608 dfs_skip_nonprimary_vbases_unmarkedp
,
4612 dfs_skip_nonprimary_vbases_markedp
,
4616 /* Remove *FIELD (which corresponds to the base given by BINFO) from
4617 the field list for T. */
4620 remove_base_field (t
, binfo
, field
)
4625 tree basetype
= BINFO_TYPE (binfo
);
4628 my_friendly_assert (TREE_TYPE (*field
) == basetype
, 23897);
4630 if (get_base_distance (basetype
, t
, 0, (tree
*)0) == -2)
4631 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4635 = size_int (CEIL (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (*field
)),
4637 propagate_binfo_offsets (binfo
, offset
);
4639 /* Remove this field. */
4640 *field
= TREE_CHAIN (*field
);
4643 /* Remove the FIELD_DECLs created for T's base classes in
4644 build_base_fields. Simultaneously, update BINFO_OFFSET for all the
4645 bases, except for non-primary virtual baseclasses. */
4648 remove_base_fields (t
)
4654 /* Now propagate offset information throughout the lattice.
4655 Simultaneously, remove the temporary FIELD_DECLS we created in
4656 build_base_fields to refer to base types. */
4657 field
= &TYPE_FIELDS (t
);
4658 if (TYPE_VFIELD (t
) == *field
)
4660 /* If this class did not have a primary base, we create a
4661 virtual function table pointer. It will be the first thing
4662 in the class, under the new ABI. Skip it; the base fields
4664 my_friendly_assert (flag_new_abi
4665 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
),
4667 field
= &TREE_CHAIN (*field
);
4670 /* Under the new ABI, the primary base is always allocated first. */
4671 if (flag_new_abi
&& CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
4672 remove_base_field (t
, CLASSTYPE_PRIMARY_BINFO (t
), field
);
4674 /* Now remove the rest of the bases. */
4675 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (t
); i
++)
4679 /* Under the new ABI, we've already removed the primary base
4681 if (flag_new_abi
&& i
== CLASSTYPE_VFIELD_PARENT (t
))
4684 binfo
= BINFO_BASETYPE (TYPE_BINFO (t
), i
);
4686 /* We treat a primary virtual base class just like an ordinary base
4687 class. But, non-primary virtual bases are laid out later. */
4688 if (TREE_VIA_VIRTUAL (binfo
) && !BINFO_PRIMARY_MARKED_P (binfo
))
4691 remove_base_field (t
, binfo
, field
);
4695 /* Called via dfs_walk from layout_virtual bases. */
4698 dfs_set_offset_for_shared_vbases (binfo
, data
)
4702 if (TREE_VIA_VIRTUAL (binfo
) && BINFO_PRIMARY_MARKED_P (binfo
))
4704 /* Update the shared copy. */
4707 shared_binfo
= BINFO_FOR_VBASE (BINFO_TYPE (binfo
), (tree
) data
);
4708 BINFO_OFFSET (shared_binfo
) = BINFO_OFFSET (binfo
);
4714 /* Called via dfs_walk from layout_virtual bases. */
4717 dfs_set_offset_for_unshared_vbases (binfo
, data
)
4721 /* If this is a virtual base, make sure it has the same offset as
4722 the shared copy. If it's a primary base, then we know it's
4724 if (TREE_VIA_VIRTUAL (binfo
) && !BINFO_PRIMARY_MARKED_P (binfo
))
4726 tree t
= (tree
) data
;
4730 vbase
= BINFO_FOR_VBASE (BINFO_TYPE (binfo
), t
);
4731 offset
= size_binop (MINUS_EXPR
,
4732 BINFO_OFFSET (vbase
), BINFO_OFFSET (binfo
));
4733 propagate_binfo_offsets (binfo
, offset
);
4739 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4740 TYPE_ALIGN and TYPE_SIZE for T. */
4743 layout_virtual_bases (t
)
4749 /* DSIZE is the size of the class without the virtual bases. */
4750 dsize
= TREE_INT_CST_LOW (TYPE_SIZE (t
));
4751 /* Make every class have alignment of at least one. */
4752 TYPE_ALIGN (t
) = MAX (TYPE_ALIGN (t
), BITS_PER_UNIT
);
4754 /* Go through the virtual bases, allocating space for each virtual
4755 base that is not already a primary base class. */
4756 for (vbase
= CLASSTYPE_VBASECLASSES (t
);
4758 vbase
= TREE_CHAIN (vbase
))
4759 if (!BINFO_VBASE_PRIMARY_P (vbase
))
4761 /* This virtual base is not a primary base of any class in the
4762 hierarchy, so we have to add space for it. */
4764 unsigned int desired_align
;
4766 basetype
= BINFO_TYPE (vbase
);
4767 desired_align
= TYPE_ALIGN (basetype
);
4768 TYPE_ALIGN (t
) = MAX (TYPE_ALIGN (t
), desired_align
);
4770 /* Add padding so that we can put the virtual base class at an
4771 appropriately aligned offset. */
4772 dsize
= CEIL (dsize
, desired_align
) * desired_align
;
4773 /* And compute the offset of the virtual base. */
4774 propagate_binfo_offsets (vbase
,
4775 size_int (CEIL (dsize
, BITS_PER_UNIT
)));
4776 /* Every virtual baseclass takes a least a UNIT, so that we can
4777 take it's address and get something different for each base. */
4778 dsize
+= MAX (BITS_PER_UNIT
,
4779 TREE_INT_CST_LOW (CLASSTYPE_SIZE (basetype
)));
4782 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4783 BINFO_OFFSET set correctly. Those we just allocated certainly
4784 will. The others are primary baseclasses; we walk the hierarchy
4785 to find the primary copies and update the shared copy. */
4786 dfs_walk (TYPE_BINFO (t
),
4787 dfs_set_offset_for_shared_vbases
,
4788 dfs_unmarked_real_bases_queue_p
,
4791 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4792 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4793 bases and their direct and indirect bases. The ambiguity checks
4794 in get_base_distance depend on the BINFO_OFFSETs being set
4796 dfs_walk (TYPE_BINFO (t
), dfs_set_offset_for_unshared_vbases
, NULL
, t
);
4797 for (vbase
= CLASSTYPE_VBASECLASSES (t
);
4799 vbase
= TREE_CHAIN (vbase
))
4800 dfs_walk (vbase
, dfs_set_offset_for_unshared_vbases
, NULL
, t
);
4802 /* Now, make sure that the total size of the type is a multiple of
4804 dsize
= CEIL (dsize
, TYPE_ALIGN (t
)) * TYPE_ALIGN (t
);
4805 TYPE_SIZE (t
) = bitsize_int (dsize
);
4806 TYPE_SIZE_UNIT (t
) = convert (sizetype
,
4807 size_binop (FLOOR_DIV_EXPR
, TYPE_SIZE (t
),
4808 bitsize_int (BITS_PER_UNIT
)));
4811 /* Finish the work of layout_record, now taking virtual bases into account.
4812 Also compute the actual offsets that our base classes will have.
4813 This must be performed after the fields are laid out, since virtual
4814 baseclasses must lay down at the end of the record. */
4817 layout_basetypes (rec
)
4822 #ifdef STRUCTURE_SIZE_BOUNDARY
4823 /* Packed structures don't need to have minimum size. */
4824 if (! TYPE_PACKED (rec
))
4825 TYPE_ALIGN (rec
) = MAX (TYPE_ALIGN (rec
), STRUCTURE_SIZE_BOUNDARY
);
4828 /* Remove the FIELD_DECLs we created for baseclasses in
4829 build_base_fields. Simultaneously, update the BINFO_OFFSETs for
4830 everything in the hierarcy except non-primary virtual bases. */
4831 remove_base_fields (rec
);
4833 /* Allocate the virtual base classes. */
4834 layout_virtual_bases (rec
);
4836 /* Get all the virtual base types that this type uses. The
4837 TREE_VALUE slot holds the virtual baseclass type. Note that
4838 get_vbase_types makes copies of the virtual base BINFOs, so that
4839 the vbase_types are unshared. */
4840 for (vbase_types
= CLASSTYPE_VBASECLASSES (rec
); vbase_types
;
4841 vbase_types
= TREE_CHAIN (vbase_types
))
4844 tree basetype
= BINFO_TYPE (vbase_types
);
4845 if (get_base_distance (basetype
, rec
, 0, (tree
*)0) == -2)
4846 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4851 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4852 BINFO_OFFSETs for all of the base-classes. Position the vtable
4856 layout_class_type (t
, empty_p
, has_virtual_p
,
4857 new_virtuals_p
, overridden_virtuals_p
)
4861 tree
*new_virtuals_p
;
4862 tree
*overridden_virtuals_p
;
4864 tree padding
= NULL_TREE
;
4866 /* If possible, we reuse the virtual function table pointer from one
4867 of our base classes. */
4868 determine_primary_base (t
, has_virtual_p
);
4870 /* Add pointers to all of our virtual base-classes. */
4871 TYPE_FIELDS (t
) = chainon (build_vbase_pointer_fields (t
, empty_p
),
4873 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4874 TYPE_FIELDS (t
) = chainon (build_base_fields (t
, empty_p
),
4877 /* Create a pointer to our virtual function table. */
4878 create_vtable_ptr (t
, empty_p
, has_virtual_p
,
4879 new_virtuals_p
, overridden_virtuals_p
);
4881 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4882 we have to save this before we start modifying
4883 TYPE_NONCOPIED_PARTS. */
4884 fixup_inline_methods (t
);
4886 /* We make all structures have at least one element, so that they
4887 have non-zero size. The field that we add here is fake, in the
4888 sense that, for example, we don't want people to be able to
4889 initialize it later. So, we add it just long enough to let the
4890 back-end lay out the type, and then remove it. In the new ABI,
4891 the class may be empty even if it has basetypes. Therefore, we
4892 add the fake field at the end of the fields list; if there are
4893 already FIELD_DECLs on the list, their offsets will not be
4897 padding
= build_lang_decl (FIELD_DECL
, NULL_TREE
, char_type_node
);
4898 TYPE_FIELDS (t
) = chainon (TYPE_FIELDS (t
), padding
);
4899 TYPE_NONCOPIED_PARTS (t
)
4900 = tree_cons (NULL_TREE
, padding
, TYPE_NONCOPIED_PARTS (t
));
4901 TREE_STATIC (TYPE_NONCOPIED_PARTS (t
)) = 1;
4904 /* Let the back-end lay out the type. Note that at this point we
4905 have only included non-virtual base-classes; we will lay out the
4906 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4907 this call are not necessarily correct; they are just the size and
4908 alignment when no virtual base clases are used. */
4911 /* If we added an extra field to make this class non-empty, remove
4917 declp
= &TYPE_FIELDS (t
);
4918 while (*declp
!= padding
)
4919 declp
= &TREE_CHAIN (*declp
);
4920 *declp
= TREE_CHAIN (*declp
);
4923 /* Delete all zero-width bit-fields from the list of fields. Now
4924 that the type is laid out they are no longer important. */
4925 remove_zero_width_bit_fields (t
);
4927 /* Remember the size and alignment of the class before adding
4928 the virtual bases. */
4929 if (*empty_p
&& flag_new_abi
)
4931 CLASSTYPE_SIZE (t
) = bitsize_int (0);
4932 CLASSTYPE_SIZE_UNIT (t
) = size_zero_node
;
4934 else if (flag_new_abi
&& TYPE_HAS_COMPLEX_INIT_REF (t
)
4935 && TYPE_HAS_COMPLEX_ASSIGN_REF (t
))
4937 CLASSTYPE_SIZE (t
) = TYPE_BINFO_SIZE (t
);
4938 CLASSTYPE_SIZE_UNIT (t
) = TYPE_BINFO_SIZE_UNIT (t
);
4942 CLASSTYPE_SIZE (t
) = TYPE_SIZE (t
);
4943 CLASSTYPE_SIZE_UNIT (t
) = TYPE_SIZE_UNIT (t
);
4946 CLASSTYPE_ALIGN (t
) = TYPE_ALIGN (t
);
4948 /* Set the TYPE_DECL for this type to contain the right
4949 value for DECL_OFFSET, so that we can use it as part
4950 of a COMPONENT_REF for multiple inheritance. */
4951 layout_decl (TYPE_MAIN_DECL (t
), 0);
4953 /* Now fix up any virtual base class types that we left lying
4954 around. We must get these done before we try to lay out the
4955 virtual function table. */
4956 if (CLASSTYPE_N_BASECLASSES (t
))
4957 /* layout_basetypes will remove the base subobject fields. */
4958 layout_basetypes (t
);
4961 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
4962 (or C++ class declaration).
4964 For C++, we must handle the building of derived classes.
4965 Also, C++ allows static class members. The way that this is
4966 handled is to keep the field name where it is (as the DECL_NAME
4967 of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
4968 of the field. layout_record and layout_union will know about this.
4970 More C++ hair: inline functions have text in their
4971 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
4972 meaningful tree structure. After the struct has been laid out, set
4973 things up so that this can happen.
4975 And still more: virtual functions. In the case of single inheritance,
4976 when a new virtual function is seen which redefines a virtual function
4977 from the base class, the new virtual function is placed into
4978 the virtual function table at exactly the same address that
4979 it had in the base class. When this is extended to multiple
4980 inheritance, the same thing happens, except that multiple virtual
4981 function tables must be maintained. The first virtual function
4982 table is treated in exactly the same way as in the case of single
4983 inheritance. Additional virtual function tables have different
4984 DELTAs, which tell how to adjust `this' to point to the right thing.
4986 ATTRIBUTES is the set of decl attributes to be applied, if any. */
4994 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
4995 a FUNCTION_DECL. Each of these functions is a virtual function
4996 declared in T that does not override any virtual function from a
4998 tree new_virtuals
= NULL_TREE
;
4999 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5000 except that each declaration here overrides the declaration from
5002 tree overridden_virtuals
= NULL_TREE
;
5009 if (IS_AGGR_TYPE (t
))
5010 cp_error ("redefinition of `%#T'", t
);
5012 my_friendly_abort (172);
5017 GNU_xref_decl (current_function_decl
, t
);
5019 /* If this type was previously laid out as a forward reference,
5020 make sure we lay it out again. */
5021 TYPE_SIZE (t
) = NULL_TREE
;
5022 CLASSTYPE_GOT_SEMICOLON (t
) = 0;
5023 CLASSTYPE_VFIELD_PARENT (t
) = -1;
5025 CLASSTYPE_RTTI (t
) = NULL_TREE
;
5027 /* Do end-of-class semantic processing: checking the validity of the
5028 bases and members and add implicitly generated methods. */
5029 check_bases_and_members (t
, &empty
);
5031 /* Layout the class itself. */
5032 layout_class_type (t
, &empty
, &has_virtual
,
5033 &new_virtuals
, &overridden_virtuals
);
5035 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
5036 might need to know it for setting up the offsets in the vtable
5037 (or in thunks) below. */
5038 vfield
= TYPE_VFIELD (t
);
5039 if (vfield
!= NULL_TREE
5040 && DECL_FIELD_CONTEXT (vfield
) != t
)
5042 tree binfo
= get_binfo (DECL_FIELD_CONTEXT (vfield
), t
, 0);
5043 tree offset
= convert (bitsizetype
, BINFO_OFFSET (binfo
));
5045 vfield
= copy_node (vfield
);
5046 copy_lang_decl (vfield
);
5048 if (! integer_zerop (offset
))
5049 offset
= size_binop (MULT_EXPR
, offset
, bitsize_int (BITS_PER_UNIT
));
5051 DECL_FIELD_CONTEXT (vfield
) = t
;
5052 DECL_FIELD_BITPOS (vfield
)
5053 = size_binop (PLUS_EXPR
, offset
, DECL_FIELD_BITPOS (vfield
));
5054 TYPE_VFIELD (t
) = vfield
;
5058 = modify_all_vtables (t
, &has_virtual
, nreverse (overridden_virtuals
));
5060 /* If necessary, create the primary vtable for this class. */
5062 || overridden_virtuals
5063 || (TYPE_CONTAINS_VPTR_P (t
) && vptrs_present_everywhere_p ()))
5065 new_virtuals
= nreverse (new_virtuals
);
5066 /* We must enter these virtuals into the table. */
5067 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5069 if (! CLASSTYPE_COM_INTERFACE (t
))
5071 /* The second slot is for the tdesc pointer when thunks
5073 if (flag_vtable_thunks
)
5074 new_virtuals
= tree_cons (NULL_TREE
, NULL_TREE
, new_virtuals
);
5076 /* The first slot is for the rtti offset. */
5077 new_virtuals
= tree_cons (NULL_TREE
, NULL_TREE
, new_virtuals
);
5079 set_rtti_entry (new_virtuals
,
5080 convert (ssizetype
, integer_zero_node
), t
);
5082 build_primary_vtable (NULL_TREE
, t
);
5084 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
)))
5085 /* Here we know enough to change the type of our virtual
5086 function table, but we will wait until later this function. */
5087 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
5089 /* If this type has basetypes with constructors, then those
5090 constructors might clobber the virtual function table. But
5091 they don't if the derived class shares the exact vtable of the base
5094 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
5096 /* If we didn't need a new vtable, see if we should copy one from
5098 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5100 tree binfo
= CLASSTYPE_PRIMARY_BINFO (t
);
5102 /* This class contributes nothing new to the virtual function
5103 table. However, it may have declared functions which
5104 went into the virtual function table "inherited" from the
5105 base class. If so, we grab a copy of those updated functions,
5106 and pretend they are ours. */
5108 /* See if we should steal the virtual info from base class. */
5109 if (TYPE_BINFO_VTABLE (t
) == NULL_TREE
)
5110 TYPE_BINFO_VTABLE (t
) = BINFO_VTABLE (binfo
);
5111 if (TYPE_BINFO_VIRTUALS (t
) == NULL_TREE
)
5112 TYPE_BINFO_VIRTUALS (t
) = BINFO_VIRTUALS (binfo
);
5113 if (TYPE_BINFO_VTABLE (t
) != BINFO_VTABLE (binfo
))
5114 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
5117 if (TYPE_CONTAINS_VPTR_P (t
))
5119 if (TYPE_BINFO_VTABLE (t
))
5120 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t
)),
5122 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5123 my_friendly_assert (TYPE_BINFO_VIRTUALS (t
) == NULL_TREE
,
5126 CLASSTYPE_VSIZE (t
) = has_virtual
;
5127 /* Entries for virtual functions defined in the primary base are
5128 followed by entries for new functions unique to this class. */
5129 TYPE_BINFO_VIRTUALS (t
)
5130 = chainon (TYPE_BINFO_VIRTUALS (t
), new_virtuals
);
5131 /* Finally, add entries for functions that override virtuals
5132 from non-primary bases. */
5133 TYPE_BINFO_VIRTUALS (t
)
5134 = chainon (TYPE_BINFO_VIRTUALS (t
), overridden_virtuals
);
5137 /* If we created a new vtbl pointer for this class, add it to the
5139 if (TYPE_VFIELD (t
) && CLASSTYPE_VFIELD_PARENT (t
) == -1)
5140 CLASSTYPE_VFIELDS (t
)
5141 = chainon (CLASSTYPE_VFIELDS (t
), build_tree_list (NULL_TREE
, t
));
5143 finish_struct_bits (t
);
5145 /* Complete the rtl for any static member objects of the type we're
5147 for (x
= TYPE_FIELDS (t
); x
; x
= TREE_CHAIN (x
))
5149 if (TREE_CODE (x
) == VAR_DECL
&& TREE_STATIC (x
)
5150 && TREE_TYPE (x
) == t
)
5152 DECL_MODE (x
) = TYPE_MODE (t
);
5153 make_decl_rtl (x
, NULL
, 0);
5157 /* Done with FIELDS...now decide whether to sort these for
5158 faster lookups later.
5160 The C front-end only does this when n_fields > 15. We use
5161 a smaller number because most searches fail (succeeding
5162 ultimately as the search bores through the inheritance
5163 hierarchy), and we want this failure to occur quickly. */
5165 n_fields
= count_fields (TYPE_FIELDS (t
));
5168 tree field_vec
= make_tree_vec (n_fields
);
5169 add_fields_to_vec (TYPE_FIELDS (t
), field_vec
, 0);
5170 qsort (&TREE_VEC_ELT (field_vec
, 0), n_fields
, sizeof (tree
),
5171 (int (*)(const void *, const void *))field_decl_cmp
);
5172 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t
)))
5173 retrofit_lang_decl (TYPE_MAIN_DECL (t
));
5174 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t
)) = field_vec
;
5177 if (TYPE_HAS_CONSTRUCTOR (t
))
5179 tree vfields
= CLASSTYPE_VFIELDS (t
);
5183 /* Mark the fact that constructor for T
5184 could affect anybody inheriting from T
5185 who wants to initialize vtables for VFIELDS's type. */
5186 if (VF_DERIVED_VALUE (vfields
))
5187 TREE_ADDRESSABLE (vfields
) = 1;
5188 vfields
= TREE_CHAIN (vfields
);
5192 /* Make the rtl for any new vtables we have created, and unmark
5193 the base types we marked. */
5196 if (CLASSTYPE_VSIZE (t
) != 0)
5198 /* In addition to this one, all the other vfields should be listed. */
5199 /* Before that can be done, we have to have FIELD_DECLs for them, and
5200 a place to find them. */
5201 TYPE_NONCOPIED_PARTS (t
)
5202 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t
)),
5203 TYPE_VFIELD (t
), TYPE_NONCOPIED_PARTS (t
));
5205 if (warn_nonvdtor
&& TYPE_HAS_DESTRUCTOR (t
)
5206 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 1)) == NULL_TREE
)
5207 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5211 hack_incomplete_structures (t
);
5213 if (warn_overloaded_virtual
)
5216 maybe_suppress_debug_info (t
);
5218 /* Finish debugging output for this type. */
5219 rest_of_type_compilation (t
, toplevel_bindings_p ());
5222 /* When T was built up, the member declarations were added in reverse
5223 order. Rearrange them to declaration order. */
5226 unreverse_member_declarations (t
)
5233 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5234 reverse order. Put them in declaration order now. */
5235 TYPE_METHODS (t
) = nreverse (TYPE_METHODS (t
));
5236 CLASSTYPE_TAGS (t
) = nreverse (CLASSTYPE_TAGS (t
));
5238 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5239 reverse order, so we can't just use nreverse. */
5241 for (x
= TYPE_FIELDS (t
);
5242 x
&& TREE_CODE (x
) != TYPE_DECL
;
5245 next
= TREE_CHAIN (x
);
5246 TREE_CHAIN (x
) = prev
;
5251 TREE_CHAIN (TYPE_FIELDS (t
)) = x
;
5253 TYPE_FIELDS (t
) = prev
;
5258 finish_struct (t
, attributes
)
5261 /* Now that we've got all the field declarations, reverse everything
5263 unreverse_member_declarations (t
);
5265 cplus_decl_attributes (t
, attributes
, NULL_TREE
);
5267 if (processing_template_decl
)
5269 finish_struct_methods (t
);
5270 TYPE_SIZE (t
) = integer_zero_node
;
5273 finish_struct_1 (t
);
5275 TYPE_BEING_DEFINED (t
) = 0;
5277 if (current_class_type
)
5280 error ("trying to finish struct, but kicked out due to previous parse errors.");
5282 if (processing_template_decl
)
5284 tree scope
= current_scope ();
5285 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
5286 add_tree (build_min (TAG_DEFN
, t
));
5292 /* Return the dynamic type of INSTANCE, if known.
5293 Used to determine whether the virtual function table is needed
5296 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5297 of our knowledge of its type. *NONNULL should be initialized
5298 before this function is called. */
5301 fixed_type_or_null (instance
, nonnull
)
5305 switch (TREE_CODE (instance
))
5308 /* Check that we are not going through a cast of some sort. */
5309 if (TREE_TYPE (instance
)
5310 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance
, 0))))
5311 instance
= TREE_OPERAND (instance
, 0);
5312 /* fall through... */
5314 /* This is a call to a constructor, hence it's never zero. */
5315 if (TREE_HAS_CONSTRUCTOR (instance
))
5319 return TREE_TYPE (instance
);
5324 /* This is a call to a constructor, hence it's never zero. */
5325 if (TREE_HAS_CONSTRUCTOR (instance
))
5329 return TREE_TYPE (instance
);
5331 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
5338 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
5339 /* Propagate nonnull. */
5340 fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
5341 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
5342 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
5347 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
5352 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
);
5355 return fixed_type_or_null (TREE_OPERAND (instance
, 1), nonnull
);
5359 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
5360 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance
))))
5364 return TREE_TYPE (TREE_TYPE (instance
));
5366 /* fall through... */
5369 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
5373 return TREE_TYPE (instance
);
5377 if (instance
== current_class_ptr
5378 && flag_this_is_variable
<= 0)
5380 /* Normally, 'this' must be non-null. */
5381 if (flag_this_is_variable
== 0)
5384 /* <0 means we're in a constructor and we know our type. */
5385 if (flag_this_is_variable
< 0)
5386 return TREE_TYPE (TREE_TYPE (instance
));
5388 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
5389 /* Reference variables should be references to objects. */
5399 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5400 to the static type. We also handle the case where INSTANCE is really
5403 Used to determine whether the virtual function table is needed
5406 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5407 of our knowledge of its type. *NONNULL should be initialized
5408 before this function is called. */
5411 resolves_to_fixed_type_p (instance
, nonnull
)
5415 tree t
= TREE_TYPE (instance
);
5416 tree fixed
= fixed_type_or_null (instance
, nonnull
);
5417 if (fixed
== NULL_TREE
)
5419 if (POINTER_TYPE_P (t
))
5421 return same_type_p (TYPE_MAIN_VARIANT (t
), TYPE_MAIN_VARIANT (fixed
));
5426 init_class_processing ()
5428 current_class_depth
= 0;
5429 current_class_stack_size
= 10;
5431 = (class_stack_node_t
) xmalloc (current_class_stack_size
5432 * sizeof (struct class_stack_node
));
5434 access_default_node
= build_int_2 (0, 0);
5435 access_public_node
= build_int_2 (1, 0);
5436 access_protected_node
= build_int_2 (2, 0);
5437 access_private_node
= build_int_2 (3, 0);
5438 access_default_virtual_node
= build_int_2 (4, 0);
5439 access_public_virtual_node
= build_int_2 (5, 0);
5440 access_protected_virtual_node
= build_int_2 (6, 0);
5441 access_private_virtual_node
= build_int_2 (7, 0);
5444 /* Set current scope to NAME. CODE tells us if this is a
5445 STRUCT, UNION, or ENUM environment.
5447 NAME may end up being NULL_TREE if this is an anonymous or
5448 late-bound struct (as in "struct { ... } foo;") */
5450 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5451 appropriate values, found by looking up the type definition of
5454 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5455 which can be seen locally to the class. They are shadowed by
5456 any subsequent local declaration (including parameter names).
5458 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5459 which have static meaning (i.e., static members, static
5460 member functions, enum declarations, etc).
5462 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5463 which can be seen locally to the class (as in 1), but
5464 know that we are doing this for declaration purposes
5465 (i.e. friend foo::bar (int)).
5467 So that we may avoid calls to lookup_name, we cache the _TYPE
5468 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5470 For multiple inheritance, we perform a two-pass depth-first search
5471 of the type lattice. The first pass performs a pre-order search,
5472 marking types after the type has had its fields installed in
5473 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5474 unmarks the marked types. If a field or member function name
5475 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5476 that name becomes `error_mark_node'. */
5479 pushclass (type
, modify
)
5483 type
= TYPE_MAIN_VARIANT (type
);
5485 /* Make sure there is enough room for the new entry on the stack. */
5486 if (current_class_depth
+ 1 >= current_class_stack_size
)
5488 current_class_stack_size
*= 2;
5490 = (class_stack_node_t
) xrealloc (current_class_stack
,
5491 current_class_stack_size
5492 * sizeof (struct class_stack_node
));
5495 /* Insert a new entry on the class stack. */
5496 current_class_stack
[current_class_depth
].name
= current_class_name
;
5497 current_class_stack
[current_class_depth
].type
= current_class_type
;
5498 current_class_stack
[current_class_depth
].access
= current_access_specifier
;
5499 current_class_stack
[current_class_depth
].names_used
= 0;
5500 current_class_depth
++;
5502 /* Now set up the new type. */
5503 current_class_name
= TYPE_NAME (type
);
5504 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
5505 current_class_name
= DECL_NAME (current_class_name
);
5506 current_class_type
= type
;
5508 /* By default, things in classes are private, while things in
5509 structures or unions are public. */
5510 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
5511 ? access_private_node
5512 : access_public_node
);
5514 if (previous_class_type
!= NULL_TREE
5515 && (type
!= previous_class_type
5516 || TYPE_SIZE (previous_class_type
) == NULL_TREE
)
5517 && current_class_depth
== 1)
5519 /* Forcibly remove any old class remnants. */
5520 invalidate_class_lookup_cache ();
5523 /* If we're about to enter a nested class, clear
5524 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5525 if (modify
&& current_class_depth
> 1)
5526 clear_identifier_class_values ();
5531 if (CLASSTYPE_TEMPLATE_INFO (type
))
5532 overload_template_name (type
);
5537 if (type
!= previous_class_type
|| current_class_depth
> 1)
5538 push_class_decls (type
);
5543 /* We are re-entering the same class we just left, so we
5544 don't have to search the whole inheritance matrix to find
5545 all the decls to bind again. Instead, we install the
5546 cached class_shadowed list, and walk through it binding
5547 names and setting up IDENTIFIER_TYPE_VALUEs. */
5548 set_class_shadows (previous_class_values
);
5549 for (item
= previous_class_values
; item
; item
= TREE_CHAIN (item
))
5551 tree id
= TREE_PURPOSE (item
);
5552 tree decl
= TREE_TYPE (item
);
5554 push_class_binding (id
, decl
);
5555 if (TREE_CODE (decl
) == TYPE_DECL
)
5556 set_identifier_type_value (id
, TREE_TYPE (decl
));
5558 unuse_fields (type
);
5561 storetags (CLASSTYPE_TAGS (type
));
5565 /* When we exit a toplevel class scope, we save the
5566 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5567 reenter the class. Here, we've entered some other class, so we
5568 must invalidate our cache. */
5571 invalidate_class_lookup_cache ()
5575 /* This code can be seen as a cache miss. When we've cached a
5576 class' scope's bindings and we can't use them, we need to reset
5577 them. This is it! */
5578 for (t
= previous_class_values
; t
; t
= TREE_CHAIN (t
))
5579 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t
)) = NULL_TREE
;
5581 previous_class_type
= NULL_TREE
;
5584 /* Get out of the current class scope. If we were in a class scope
5585 previously, that is the one popped to. */
5591 /* Since poplevel_class does the popping of class decls nowadays,
5592 this really only frees the obstack used for these decls. */
5595 current_class_depth
--;
5596 current_class_name
= current_class_stack
[current_class_depth
].name
;
5597 current_class_type
= current_class_stack
[current_class_depth
].type
;
5598 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
5599 if (current_class_stack
[current_class_depth
].names_used
)
5600 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
5603 /* Returns 1 if current_class_type is either T or a nested type of T.
5604 We start looking from 1 because entry 0 is from global scope, and has
5608 currently_open_class (t
)
5612 if (t
== current_class_type
)
5614 for (i
= 1; i
< current_class_depth
; ++i
)
5615 if (current_class_stack
[i
].type
== t
)
5620 /* If either current_class_type or one of its enclosing classes are derived
5621 from T, return the appropriate type. Used to determine how we found
5622 something via unqualified lookup. */
5625 currently_open_derived_class (t
)
5630 if (DERIVED_FROM_P (t
, current_class_type
))
5631 return current_class_type
;
5633 for (i
= current_class_depth
- 1; i
> 0; --i
)
5634 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
5635 return current_class_stack
[i
].type
;
5640 /* When entering a class scope, all enclosing class scopes' names with
5641 static meaning (static variables, static functions, types and enumerators)
5642 have to be visible. This recursive function calls pushclass for all
5643 enclosing class contexts until global or a local scope is reached.
5644 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5645 formal of the same name. */
5648 push_nested_class (type
, modify
)
5654 /* A namespace might be passed in error cases, like A::B:C. */
5655 if (type
== NULL_TREE
5656 || type
== error_mark_node
5657 || TREE_CODE (type
) == NAMESPACE_DECL
5658 || ! IS_AGGR_TYPE (type
)
5659 || TREE_CODE (type
) == TEMPLATE_TYPE_PARM
5660 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
5663 context
= DECL_CONTEXT (TYPE_MAIN_DECL (type
));
5665 if (context
&& CLASS_TYPE_P (context
))
5666 push_nested_class (context
, 2);
5667 pushclass (type
, modify
);
5670 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5675 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
5678 if (context
&& CLASS_TYPE_P (context
))
5679 pop_nested_class ();
5682 /* Set global variables CURRENT_LANG_NAME to appropriate value
5683 so that behavior of name-mangling machinery is correct. */
5686 push_lang_context (name
)
5689 *current_lang_stack
++ = current_lang_name
;
5690 if (current_lang_stack
- &VARRAY_TREE (current_lang_base
, 0)
5691 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base
))
5693 size_t old_size
= VARRAY_SIZE (current_lang_base
);
5695 VARRAY_GROW (current_lang_base
, old_size
+ 10);
5696 current_lang_stack
= &VARRAY_TREE (current_lang_base
, old_size
);
5699 if (name
== lang_name_cplusplus
)
5701 strict_prototype
= strict_prototypes_lang_cplusplus
;
5702 current_lang_name
= name
;
5704 else if (name
== lang_name_java
)
5706 strict_prototype
= strict_prototypes_lang_cplusplus
;
5707 current_lang_name
= name
;
5708 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5709 (See record_builtin_java_type in decl.c.) However, that causes
5710 incorrect debug entries if these types are actually used.
5711 So we re-enable debug output after extern "Java". */
5712 DECL_IGNORED_P (java_byte_type_node
) = 0;
5713 DECL_IGNORED_P (java_short_type_node
) = 0;
5714 DECL_IGNORED_P (java_int_type_node
) = 0;
5715 DECL_IGNORED_P (java_long_type_node
) = 0;
5716 DECL_IGNORED_P (java_float_type_node
) = 0;
5717 DECL_IGNORED_P (java_double_type_node
) = 0;
5718 DECL_IGNORED_P (java_char_type_node
) = 0;
5719 DECL_IGNORED_P (java_boolean_type_node
) = 0;
5721 else if (name
== lang_name_c
)
5723 strict_prototype
= strict_prototypes_lang_c
;
5724 current_lang_name
= name
;
5727 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name
));
5730 /* Get out of the current language scope. */
5735 /* Clear the current entry so that garbage collector won't hold on
5737 *current_lang_stack
= NULL_TREE
;
5738 current_lang_name
= *--current_lang_stack
;
5739 if (current_lang_name
== lang_name_cplusplus
5740 || current_lang_name
== lang_name_java
)
5741 strict_prototype
= strict_prototypes_lang_cplusplus
;
5742 else if (current_lang_name
== lang_name_c
)
5743 strict_prototype
= strict_prototypes_lang_c
;
5746 /* Type instantiation routines. */
5748 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5749 matches the TARGET_TYPE. If there is no satisfactory match, return
5750 error_mark_node, and issue an error message if COMPLAIN is
5751 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5752 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5753 template arguments. */
5756 resolve_address_of_overloaded_function (target_type
,
5765 tree explicit_targs
;
5767 /* Here's what the standard says:
5771 If the name is a function template, template argument deduction
5772 is done, and if the argument deduction succeeds, the deduced
5773 arguments are used to generate a single template function, which
5774 is added to the set of overloaded functions considered.
5776 Non-member functions and static member functions match targets of
5777 type "pointer-to-function" or "reference-to-function." Nonstatic
5778 member functions match targets of type "pointer-to-member
5779 function;" the function type of the pointer to member is used to
5780 select the member function from the set of overloaded member
5781 functions. If a nonstatic member function is selected, the
5782 reference to the overloaded function name is required to have the
5783 form of a pointer to member as described in 5.3.1.
5785 If more than one function is selected, any template functions in
5786 the set are eliminated if the set also contains a non-template
5787 function, and any given template function is eliminated if the
5788 set contains a second template function that is more specialized
5789 than the first according to the partial ordering rules 14.5.5.2.
5790 After such eliminations, if any, there shall remain exactly one
5791 selected function. */
5794 int is_reference
= 0;
5795 /* We store the matches in a TREE_LIST rooted here. The functions
5796 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5797 interoperability with most_specialized_instantiation. */
5798 tree matches
= NULL_TREE
;
5801 /* By the time we get here, we should be seeing only real
5802 pointer-to-member types, not the internal POINTER_TYPE to
5803 METHOD_TYPE representation. */
5804 my_friendly_assert (!(TREE_CODE (target_type
) == POINTER_TYPE
5805 && (TREE_CODE (TREE_TYPE (target_type
))
5806 == METHOD_TYPE
)), 0);
5808 /* Check that the TARGET_TYPE is reasonable. */
5809 if (TYPE_PTRFN_P (target_type
))
5812 else if (TYPE_PTRMEMFUNC_P (target_type
))
5813 /* This is OK, too. */
5815 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5817 /* This is OK, too. This comes from a conversion to reference
5819 target_type
= build_reference_type (target_type
);
5825 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5826 DECL_NAME (OVL_FUNCTION (overload
)), target_type
);
5827 return error_mark_node
;
5830 /* If we can find a non-template function that matches, we can just
5831 use it. There's no point in generating template instantiations
5832 if we're just going to throw them out anyhow. But, of course, we
5833 can only do this when we don't *need* a template function. */
5838 for (fns
= overload
; fns
; fns
= OVL_CHAIN (fns
))
5840 tree fn
= OVL_FUNCTION (fns
);
5843 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
5844 /* We're not looking for templates just yet. */
5847 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
5849 /* We're looking for a non-static member, and this isn't
5850 one, or vice versa. */
5853 /* See if there's a match. */
5854 fntype
= TREE_TYPE (fn
);
5856 fntype
= build_ptrmemfunc_type (build_pointer_type (fntype
));
5857 else if (!is_reference
)
5858 fntype
= build_pointer_type (fntype
);
5860 if (can_convert_arg (target_type
, fntype
, fn
))
5861 matches
= tree_cons (fn
, NULL_TREE
, matches
);
5865 /* Now, if we've already got a match (or matches), there's no need
5866 to proceed to the template functions. But, if we don't have a
5867 match we need to look at them, too. */
5870 tree target_fn_type
;
5871 tree target_arg_types
;
5872 tree target_ret_type
;
5877 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type
));
5879 target_fn_type
= TREE_TYPE (target_type
);
5880 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
5881 target_ret_type
= TREE_TYPE (target_fn_type
);
5883 for (fns
= overload
; fns
; fns
= OVL_CHAIN (fns
))
5885 tree fn
= OVL_FUNCTION (fns
);
5887 tree instantiation_type
;
5890 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
5891 /* We're only looking for templates. */
5894 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
5896 /* We're not looking for a non-static member, and this is
5897 one, or vice versa. */
5900 /* Try to do argument deduction. */
5901 targs
= make_tree_vec (DECL_NTPARMS (fn
));
5902 if (fn_type_unification (fn
, explicit_targs
, targs
,
5903 target_arg_types
, target_ret_type
,
5905 /* Argument deduction failed. */
5908 /* Instantiate the template. */
5909 instantiation
= instantiate_template (fn
, targs
);
5910 if (instantiation
== error_mark_node
)
5911 /* Instantiation failed. */
5914 /* See if there's a match. */
5915 instantiation_type
= TREE_TYPE (instantiation
);
5917 instantiation_type
=
5918 build_ptrmemfunc_type (build_pointer_type (instantiation_type
));
5919 else if (!is_reference
)
5920 instantiation_type
= build_pointer_type (instantiation_type
);
5921 if (can_convert_arg (target_type
, instantiation_type
, instantiation
))
5922 matches
= tree_cons (instantiation
, fn
, matches
);
5925 /* Now, remove all but the most specialized of the matches. */
5928 tree match
= most_specialized_instantiation (matches
,
5931 if (match
!= error_mark_node
)
5932 matches
= tree_cons (match
, NULL_TREE
, NULL_TREE
);
5936 /* Now we should have exactly one function in MATCHES. */
5937 if (matches
== NULL_TREE
)
5939 /* There were *no* matches. */
5942 cp_error ("no matches converting function `%D' to type `%#T'",
5943 DECL_NAME (OVL_FUNCTION (overload
)),
5946 /* print_candidates expects a chain with the functions in
5947 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5948 so why be clever?). */
5949 for (; overload
; overload
= OVL_NEXT (overload
))
5950 matches
= tree_cons (NULL_TREE
, OVL_CURRENT (overload
),
5953 print_candidates (matches
);
5955 return error_mark_node
;
5957 else if (TREE_CHAIN (matches
))
5959 /* There were too many matches. */
5965 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5966 DECL_NAME (OVL_FUNCTION (overload
)),
5969 /* Since print_candidates expects the functions in the
5970 TREE_VALUE slot, we flip them here. */
5971 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
5972 TREE_VALUE (match
) = TREE_PURPOSE (match
);
5974 print_candidates (matches
);
5977 return error_mark_node
;
5980 /* Good, exactly one match. Now, convert it to the correct type. */
5981 fn
= TREE_PURPOSE (matches
);
5985 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
5986 return build_unary_op (ADDR_EXPR
, fn
, 0);
5989 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
5990 will mark the function as addressed, but here we must do it
5992 mark_addressable (fn
);
5998 /* This function will instantiate the type of the expression given in
5999 RHS to match the type of LHSTYPE. If errors exist, then return
6000 error_mark_node. We only complain is COMPLAIN is set. If we are
6001 not complaining, never modify rhs, as overload resolution wants to
6002 try many possible instantiations, in hopes that at least one will
6005 FLAGS is a bitmask, as we see at the top of the function.
6007 For non-recursive calls, LHSTYPE should be a function, pointer to
6008 function, or a pointer to member function. */
6011 instantiate_type (lhstype
, rhs
, flags
)
6015 int complain
= (flags
& 1);
6016 int strict
= (flags
& 2) ? COMPARE_NO_ATTRIBUTES
: COMPARE_STRICT
;
6018 if (TREE_CODE (lhstype
) == UNKNOWN_TYPE
)
6021 error ("not enough type information");
6022 return error_mark_node
;
6025 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
6027 if (comptypes (lhstype
, TREE_TYPE (rhs
), strict
))
6030 cp_error ("argument of type `%T' does not match `%T'",
6031 TREE_TYPE (rhs
), lhstype
);
6032 return error_mark_node
;
6035 /* We don't overwrite rhs if it is an overloaded function.
6036 Copying it would destroy the tree link. */
6037 if (TREE_CODE (rhs
) != OVERLOAD
)
6038 rhs
= copy_node (rhs
);
6040 /* This should really only be used when attempting to distinguish
6041 what sort of a pointer to function we have. For now, any
6042 arithmetic operation which is not supported on pointers
6043 is rejected as an error. */
6045 switch (TREE_CODE (rhs
))
6052 my_friendly_abort (177);
6053 return error_mark_node
;
6060 new_rhs
= instantiate_type (build_pointer_type (lhstype
),
6061 TREE_OPERAND (rhs
, 0), flags
);
6062 if (new_rhs
== error_mark_node
)
6063 return error_mark_node
;
6065 TREE_TYPE (rhs
) = lhstype
;
6066 TREE_OPERAND (rhs
, 0) = new_rhs
;
6071 rhs
= copy_node (TREE_OPERAND (rhs
, 0));
6072 TREE_TYPE (rhs
) = unknown_type_node
;
6073 return instantiate_type (lhstype
, rhs
, flags
);
6077 tree r
= instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6079 if (r
!= error_mark_node
&& TYPE_PTRMEMFUNC_P (lhstype
)
6080 && complain
&& !flag_ms_extensions
)
6082 /* Note: we check this after the recursive call to avoid
6083 complaining about cases where overload resolution fails. */
6085 tree t
= TREE_TYPE (TREE_OPERAND (rhs
, 0));
6086 tree fn
= PTRMEM_CST_MEMBER (r
);
6088 my_friendly_assert (TREE_CODE (r
) == PTRMEM_CST
, 990811);
6091 ("object-dependent reference to `%E' can only be used in a call",
6094 (" to form a pointer to member function, say `&%T::%E'",
6102 rhs
= TREE_OPERAND (rhs
, 1);
6103 if (BASELINK_P (rhs
))
6104 return instantiate_type (lhstype
, TREE_VALUE (rhs
), flags
);
6106 /* This can happen if we are forming a pointer-to-member for a
6108 my_friendly_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
, 0);
6112 case TEMPLATE_ID_EXPR
:
6114 resolve_address_of_overloaded_function (lhstype
,
6115 TREE_OPERAND (rhs
, 0),
6117 /*template_only=*/1,
6118 TREE_OPERAND (rhs
, 1));
6122 resolve_address_of_overloaded_function (lhstype
,
6125 /*template_only=*/0,
6126 /*explicit_targs=*/NULL_TREE
);
6129 /* Now we should have a baselink. */
6130 my_friendly_assert (BASELINK_P (rhs
), 990412);
6132 return instantiate_type (lhstype
, TREE_VALUE (rhs
), flags
);
6135 /* This is too hard for now. */
6136 my_friendly_abort (183);
6137 return error_mark_node
;
6142 TREE_OPERAND (rhs
, 0)
6143 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), flags
);
6144 if (TREE_OPERAND (rhs
, 0) == error_mark_node
)
6145 return error_mark_node
;
6146 TREE_OPERAND (rhs
, 1)
6147 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6148 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
6149 return error_mark_node
;
6151 TREE_TYPE (rhs
) = lhstype
;
6155 case TRUNC_DIV_EXPR
:
6156 case FLOOR_DIV_EXPR
:
6158 case ROUND_DIV_EXPR
:
6160 case TRUNC_MOD_EXPR
:
6161 case FLOOR_MOD_EXPR
:
6163 case ROUND_MOD_EXPR
:
6164 case FIX_ROUND_EXPR
:
6165 case FIX_FLOOR_EXPR
:
6167 case FIX_TRUNC_EXPR
:
6183 case PREINCREMENT_EXPR
:
6184 case PREDECREMENT_EXPR
:
6185 case POSTINCREMENT_EXPR
:
6186 case POSTDECREMENT_EXPR
:
6188 error ("invalid operation on uninstantiated type");
6189 return error_mark_node
;
6191 case TRUTH_AND_EXPR
:
6193 case TRUTH_XOR_EXPR
:
6200 case TRUTH_ANDIF_EXPR
:
6201 case TRUTH_ORIF_EXPR
:
6202 case TRUTH_NOT_EXPR
:
6204 error ("not enough type information");
6205 return error_mark_node
;
6208 if (type_unknown_p (TREE_OPERAND (rhs
, 0)))
6211 error ("not enough type information");
6212 return error_mark_node
;
6214 TREE_OPERAND (rhs
, 1)
6215 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6216 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
6217 return error_mark_node
;
6218 TREE_OPERAND (rhs
, 2)
6219 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 2), flags
);
6220 if (TREE_OPERAND (rhs
, 2) == error_mark_node
)
6221 return error_mark_node
;
6223 TREE_TYPE (rhs
) = lhstype
;
6227 TREE_OPERAND (rhs
, 1)
6228 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6229 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
6230 return error_mark_node
;
6232 TREE_TYPE (rhs
) = lhstype
;
6236 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), flags
);
6238 case ENTRY_VALUE_EXPR
:
6239 my_friendly_abort (184);
6240 return error_mark_node
;
6243 return error_mark_node
;
6246 my_friendly_abort (185);
6247 return error_mark_node
;
6251 /* Return the name of the virtual function pointer field
6252 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6253 this may have to look back through base types to find the
6254 ultimate field name. (For single inheritance, these could
6255 all be the same name. Who knows for multiple inheritance). */
6258 get_vfield_name (type
)
6261 tree binfo
= TYPE_BINFO (type
);
6264 while (BINFO_BASETYPES (binfo
)
6265 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo
, 0)))
6266 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo
, 0)))
6267 binfo
= BINFO_BASETYPE (binfo
, 0);
6269 type
= BINFO_TYPE (binfo
);
6270 buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
6271 + TYPE_NAME_LENGTH (type
) + 2);
6272 sprintf (buf
, VFIELD_NAME_FORMAT
, TYPE_NAME_STRING (type
));
6273 return get_identifier (buf
);
6277 print_class_statistics ()
6279 #ifdef GATHER_STATISTICS
6280 fprintf (stderr
, "convert_harshness = %d\n", n_convert_harshness
);
6281 fprintf (stderr
, "compute_conversion_costs = %d\n", n_compute_conversion_costs
);
6282 fprintf (stderr
, "build_method_call = %d (inner = %d)\n",
6283 n_build_method_call
, n_inner_fields_searched
);
6286 fprintf (stderr
, "vtables = %d; vtable searches = %d\n",
6287 n_vtables
, n_vtable_searches
);
6288 fprintf (stderr
, "vtable entries = %d; vtable elems = %d\n",
6289 n_vtable_entries
, n_vtable_elems
);
6294 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6295 according to [class]:
6296 The class-name is also inserted
6297 into the scope of the class itself. For purposes of access checking,
6298 the inserted class name is treated as if it were a public member name. */
6301 build_self_reference ()
6303 tree name
= constructor_name (current_class_type
);
6304 tree value
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
6307 DECL_NONLOCAL (value
) = 1;
6308 DECL_CONTEXT (value
) = current_class_type
;
6309 DECL_ARTIFICIAL (value
) = 1;
6311 if (processing_template_decl
)
6312 value
= push_template_decl (value
);
6314 saved_cas
= current_access_specifier
;
6315 current_access_specifier
= access_public_node
;
6316 finish_member_declaration (value
);
6317 current_access_specifier
= saved_cas
;
6320 /* Returns 1 if TYPE contains only padding bytes. */
6323 is_empty_class (type
)
6328 if (type
== error_mark_node
)
6331 if (! IS_AGGR_TYPE (type
))
6335 return integer_zerop (CLASSTYPE_SIZE (type
));
6337 if (TYPE_BINFO_BASETYPES (type
))
6339 t
= TYPE_FIELDS (type
);
6340 while (t
&& TREE_CODE (t
) != FIELD_DECL
)
6342 return (t
== NULL_TREE
);
6345 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6346 a *_TYPE node. NODE can also be a local class. */
6349 get_enclosing_class (type
)
6354 while (node
&& TREE_CODE (node
) != NAMESPACE_DECL
)
6356 switch (TREE_CODE_CLASS (TREE_CODE (node
)))
6359 node
= DECL_CONTEXT (node
);
6365 node
= TYPE_CONTEXT (node
);
6369 my_friendly_abort (0);
6375 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6378 is_base_of_enclosing_class (base
, type
)
6383 if (get_binfo (base
, type
, 0))
6386 type
= get_enclosing_class (type
);
6391 /* Note that NAME was looked up while the current class was being
6392 defined and that the result of that lookup was DECL. */
6395 maybe_note_name_used_in_class (name
, decl
)
6399 splay_tree names_used
;
6401 /* If we're not defining a class, there's nothing to do. */
6402 if (!current_class_type
|| !TYPE_BEING_DEFINED (current_class_type
))
6405 /* If there's already a binding for this NAME, then we don't have
6406 anything to worry about. */
6407 if (IDENTIFIER_CLASS_VALUE (name
))
6410 if (!current_class_stack
[current_class_depth
- 1].names_used
)
6411 current_class_stack
[current_class_depth
- 1].names_used
6412 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
6413 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
6415 splay_tree_insert (names_used
,
6416 (splay_tree_key
) name
,
6417 (splay_tree_value
) decl
);
6420 /* Note that NAME was declared (as DECL) in the current class. Check
6421 to see that the declaration is legal. */
6424 note_name_declared_in_class (name
, decl
)
6428 splay_tree names_used
;
6431 /* Look to see if we ever used this name. */
6433 = current_class_stack
[current_class_depth
- 1].names_used
;
6437 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
6440 /* [basic.scope.class]
6442 A name N used in a class S shall refer to the same declaration
6443 in its context and when re-evaluated in the completed scope of
6445 cp_error ("declaration of `%#D'", decl
);
6446 cp_error_at ("changes meaning of `%s' from `%+#D'",
6447 IDENTIFIER_POINTER (DECL_NAME (decl
)),
6452 /* Dump the offsets of all the bases rooted at BINFO to stderr.
6453 INDENT should be zero when called from the top level; it is
6454 incremented recursively. */
6457 dump_class_hierarchy (binfo
, indent
)
6463 fprintf (stderr
, "%*s0x%lx (%s) ", indent
, "",
6464 (unsigned long) binfo
,
6465 type_as_string (binfo
, TS_PLAIN
));
6466 fprintf (stderr
, HOST_WIDE_INT_PRINT_DEC
,
6467 TREE_INT_CST_LOW (BINFO_OFFSET (binfo
)));
6468 fprintf (stderr
, " %s\n",
6469 BINFO_PRIMARY_MARKED_P (binfo
) ? "primary" : "");
6471 for (i
= 0; i
< BINFO_N_BASETYPES (binfo
); ++i
)
6472 dump_class_hierarchy (BINFO_BASETYPE (binfo
, i
), indent
+ 2);