1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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 /* The number of nested classes being processed. If we are not in the
42 scope of any class, this is zero. */
44 int current_class_depth
;
46 /* In order to deal with nested classes, we keep a stack of classes.
47 The topmost entry is the innermost class, and is the entry at index
48 CURRENT_CLASS_DEPTH */
50 typedef struct class_stack_node
{
51 /* The name of the class. */
54 /* The _TYPE node for the class. */
57 /* The access specifier pending for new declarations in the scope of
61 /* If were defining TYPE, the names used in this class. */
62 splay_tree names_used
;
63 }* class_stack_node_t
;
65 typedef struct vtbl_init_data_s
67 /* The base for which we're building initializers. */
69 /* The type of the most-derived type. */
71 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
72 unless ctor_vtbl_p is true. */
74 /* The negative-index vtable initializers built up so far. These
75 are in order from least negative index to most negative index. */
77 /* The last (i.e., most negative entry in INITS. */
79 /* The binfo for the virtual base for which we're building
80 vcall offset initializers. */
82 /* The functions in vbase for which we have already provided vcall
85 /* The vtable index of the next vcall or vbase offset. */
87 /* Nonzero if we are building the initializer for the primary
90 /* Nonzero if we are building the initializer for a construction
95 /* The type of a function passed to walk_subobject_offsets. */
96 typedef int (*subobject_offset_fn
) PARAMS ((tree
, tree
, splay_tree
));
98 /* The stack itself. This is an dynamically resized array. The
99 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
100 static int current_class_stack_size
;
101 static class_stack_node_t current_class_stack
;
103 /* An array of all local classes present in this translation unit, in
104 declaration order. */
105 varray_type local_classes
;
107 static tree get_vfield_name
PARAMS ((tree
));
108 static void finish_struct_anon
PARAMS ((tree
));
109 static tree build_vbase_pointer
PARAMS ((tree
, tree
));
110 static tree build_vtable_entry
PARAMS ((tree
, tree
, tree
, int));
111 static tree get_vtable_name
PARAMS ((tree
));
112 static tree get_derived_offset
PARAMS ((tree
, tree
));
113 static tree get_basefndecls
PARAMS ((tree
, tree
));
114 static int build_primary_vtable
PARAMS ((tree
, tree
));
115 static int build_secondary_vtable
PARAMS ((tree
, tree
));
116 static tree dfs_finish_vtbls
PARAMS ((tree
, void *));
117 static void finish_vtbls
PARAMS ((tree
));
118 static void modify_vtable_entry
PARAMS ((tree
, tree
, tree
, tree
, tree
*));
119 static void add_virtual_function
PARAMS ((tree
*, tree
*, int *, tree
, tree
));
120 static tree delete_duplicate_fields_1
PARAMS ((tree
, tree
));
121 static void delete_duplicate_fields
PARAMS ((tree
));
122 static void finish_struct_bits
PARAMS ((tree
));
123 static int alter_access
PARAMS ((tree
, tree
, tree
));
124 static void handle_using_decl
PARAMS ((tree
, tree
));
125 static int strictly_overrides
PARAMS ((tree
, tree
));
126 static void mark_overriders
PARAMS ((tree
, tree
));
127 static void check_for_override
PARAMS ((tree
, tree
));
128 static tree dfs_modify_vtables
PARAMS ((tree
, void *));
129 static tree modify_all_vtables
PARAMS ((tree
, int *, tree
));
130 static void determine_primary_base
PARAMS ((tree
, int *));
131 static void finish_struct_methods
PARAMS ((tree
));
132 static void maybe_warn_about_overly_private_class
PARAMS ((tree
));
133 static int field_decl_cmp
PARAMS ((const tree
*, const tree
*));
134 static int method_name_cmp
PARAMS ((const tree
*, const tree
*));
135 static tree add_implicitly_declared_members
PARAMS ((tree
, int, int, int));
136 static tree fixed_type_or_null
PARAMS ((tree
, int *, int *));
137 static tree resolve_address_of_overloaded_function
PARAMS ((tree
, tree
, int,
139 static void build_vtable_entry_ref
PARAMS ((tree
, tree
, tree
));
140 static tree build_vtbl_initializer
PARAMS ((tree
, tree
, tree
, tree
, int *));
141 static int count_fields
PARAMS ((tree
));
142 static int add_fields_to_vec
PARAMS ((tree
, tree
, int));
143 static void check_bitfield_decl
PARAMS ((tree
));
144 static void check_field_decl
PARAMS ((tree
, tree
, int *, int *, int *, int *));
145 static void check_field_decls
PARAMS ((tree
, tree
*, int *, int *, int *,
147 static void build_base_field
PARAMS ((record_layout_info
, tree
, int *,
149 static void build_base_fields
PARAMS ((record_layout_info
, int *,
151 static tree build_vbase_pointer_fields
PARAMS ((record_layout_info
, int *));
152 static tree build_vtbl_or_vbase_field
PARAMS ((tree
, tree
, tree
, tree
, tree
,
154 static void check_methods
PARAMS ((tree
));
155 static void remove_zero_width_bit_fields
PARAMS ((tree
));
156 static void check_bases
PARAMS ((tree
, int *, int *, int *));
157 static void check_bases_and_members
PARAMS ((tree
, int *));
158 static tree create_vtable_ptr
PARAMS ((tree
, int *, int *, tree
*, tree
*));
159 static void layout_class_type
PARAMS ((tree
, int *, int *, tree
*, tree
*));
160 static void fixup_pending_inline
PARAMS ((tree
));
161 static void fixup_inline_methods
PARAMS ((tree
));
162 static void set_primary_base
PARAMS ((tree
, tree
, int *));
163 static void propagate_binfo_offsets
PARAMS ((tree
, tree
));
164 static void layout_virtual_bases
PARAMS ((tree
, splay_tree
));
165 static tree dfs_set_offset_for_unshared_vbases
PARAMS ((tree
, void *));
166 static void build_vbase_offset_vtbl_entries
PARAMS ((tree
, vtbl_init_data
*));
167 static void add_vcall_offset_vtbl_entries_r
PARAMS ((tree
, vtbl_init_data
*));
168 static void add_vcall_offset_vtbl_entries_1
PARAMS ((tree
, vtbl_init_data
*));
169 static void build_vcall_offset_vtbl_entries
PARAMS ((tree
, vtbl_init_data
*));
170 static void layout_vtable_decl
PARAMS ((tree
, int));
171 static tree dfs_find_final_overrider
PARAMS ((tree
, void *));
172 static tree find_final_overrider
PARAMS ((tree
, tree
, tree
));
173 static int make_new_vtable
PARAMS ((tree
, tree
));
174 static void dump_class_hierarchy_r
PARAMS ((FILE *, tree
, tree
, int));
175 extern void dump_class_hierarchy
PARAMS ((const char *, tree
));
176 static tree build_vtable
PARAMS ((tree
, tree
, tree
));
177 static void initialize_vtable
PARAMS ((tree
, tree
));
178 static void initialize_array
PARAMS ((tree
, tree
));
179 static void layout_nonempty_base_or_field
PARAMS ((record_layout_info
,
182 static unsigned HOST_WIDE_INT end_of_class
PARAMS ((tree
, int));
183 static void layout_empty_base
PARAMS ((tree
, tree
, splay_tree
));
184 static void accumulate_vtbl_inits
PARAMS ((tree
, tree
, tree
, tree
, tree
));
185 static tree dfs_accumulate_vtbl_inits
PARAMS ((tree
, tree
, tree
, tree
,
187 static void set_vindex
PARAMS ((tree
, int *));
188 static void build_rtti_vtbl_entries
PARAMS ((tree
, vtbl_init_data
*));
189 static void build_vcall_and_vbase_vtbl_entries
PARAMS ((tree
,
191 static void force_canonical_binfo_r
PARAMS ((tree
, tree
, tree
, tree
));
192 static void force_canonical_binfo
PARAMS ((tree
, tree
, tree
, tree
));
193 static void mark_primary_bases
PARAMS ((tree
));
194 static tree mark_primary_virtual_base
PARAMS ((tree
, tree
, tree
));
195 static void clone_constructors_and_destructors
PARAMS ((tree
));
196 static tree build_clone
PARAMS ((tree
, tree
));
197 static void update_vtable_entry_for_fn
PARAMS ((tree
, tree
, tree
, tree
*));
198 static tree copy_virtuals
PARAMS ((tree
));
199 static void build_ctor_vtbl_group
PARAMS ((tree
, tree
));
200 static void build_vtt
PARAMS ((tree
));
201 static tree
*build_vtt_inits
PARAMS ((tree
, tree
, tree
*, tree
*));
202 static tree dfs_build_secondary_vptr_vtt_inits
PARAMS ((tree
, void *));
203 static tree dfs_ctor_vtable_bases_queue_p
PARAMS ((tree
, void *data
));
204 static tree dfs_fixup_binfo_vtbls
PARAMS ((tree
, void *));
205 static tree get_original_base
PARAMS ((tree
, tree
));
206 static tree dfs_get_primary_binfo
PARAMS ((tree
, void*));
207 static int record_subobject_offset
PARAMS ((tree
, tree
, splay_tree
));
208 static int check_subobject_offset
PARAMS ((tree
, tree
, splay_tree
));
209 static int walk_subobject_offsets
PARAMS ((tree
, subobject_offset_fn
,
210 tree
, splay_tree
, int));
211 static void record_subobject_offsets
PARAMS ((tree
, tree
, splay_tree
, int));
212 static int layout_conflict_p
PARAMS ((tree
, tree
, splay_tree
, int));
213 static int splay_tree_compare_integer_csts
PARAMS ((splay_tree_key k1
,
217 /* Macros for dfs walking during vtt construction. See
218 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
219 and dfs_fixup_binfo_vtbls. */
220 #define VTT_TOP_LEVEL_P(node) TREE_UNSIGNED(node)
221 #define VTT_MARKED_BINFO_P(node) TREE_USED(node)
223 /* Variables shared between class.c and call.c. */
225 #ifdef GATHER_STATISTICS
227 int n_vtable_entries
= 0;
228 int n_vtable_searches
= 0;
229 int n_vtable_elems
= 0;
230 int n_convert_harshness
= 0;
231 int n_compute_conversion_costs
= 0;
232 int n_build_method_call
= 0;
233 int n_inner_fields_searched
= 0;
236 /* Virtual base class layout. */
238 /* Returns a list of virtual base class pointers as a chain of
242 build_vbase_pointer_fields (rli
, empty_p
)
243 record_layout_info rli
;
246 /* Chain to hold all the new FIELD_DECLs which point at virtual
249 tree vbase_decls
= NULL_TREE
;
250 tree binfos
= TYPE_BINFO_BASETYPES (rec
);
251 int n_baseclasses
= CLASSTYPE_N_BASECLASSES (rec
);
255 /* Under the new ABI, there are no vbase pointers in the object.
256 Instead, the offsets are stored in the vtable. */
257 if (vbase_offsets_in_vtable_p ())
260 /* Loop over the baseclasses, adding vbase pointers as needed. */
261 for (i
= 0; i
< n_baseclasses
; i
++)
263 register tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
264 register tree basetype
= BINFO_TYPE (base_binfo
);
266 if (!COMPLETE_TYPE_P (basetype
))
267 /* This error is now reported in xref_tag, thus giving better
268 location information. */
271 /* All basetypes are recorded in the association list of the
274 if (TREE_VIA_VIRTUAL (base_binfo
))
279 /* The offset for a virtual base class is only used in computing
280 virtual function tables and for initializing virtual base
281 pointers. It is built once `get_vbase_types' is called. */
283 /* If this basetype can come from another vbase pointer
284 without an additional indirection, we will share
285 that pointer. If an indirection is involved, we
286 make our own pointer. */
287 for (j
= 0; j
< n_baseclasses
; j
++)
289 tree other_base_binfo
= TREE_VEC_ELT (binfos
, j
);
290 if (! TREE_VIA_VIRTUAL (other_base_binfo
)
291 && binfo_for_vbase (basetype
, BINFO_TYPE (other_base_binfo
)))
294 FORMAT_VBASE_NAME (name
, basetype
);
295 decl
= build_vtbl_or_vbase_field (get_identifier (name
),
296 get_identifier (VTABLE_BASE
),
297 build_pointer_type (basetype
),
301 BINFO_VPTR_FIELD (base_binfo
) = decl
;
302 TREE_CHAIN (decl
) = vbase_decls
;
303 place_field (rli
, decl
);
308 /* The space this decl occupies has already been accounted for. */
316 /* Returns a pointer to the virtual base class of EXP that has the
317 indicated TYPE. EXP is of class type, not a pointer type. */
320 build_vbase_pointer (exp
, type
)
323 if (vbase_offsets_in_vtable_p ())
328 /* Find the shared copy of TYPE; that's where the vtable offset
330 vbase
= binfo_for_vbase (type
, TREE_TYPE (exp
));
331 /* Find the virtual function table pointer. */
332 vbase_ptr
= build_vfield_ref (exp
, TREE_TYPE (exp
));
333 /* Compute the location where the offset will lie. */
334 vbase_ptr
= build (PLUS_EXPR
,
335 TREE_TYPE (vbase_ptr
),
337 BINFO_VPTR_FIELD (vbase
));
338 vbase_ptr
= build1 (NOP_EXPR
,
339 build_pointer_type (ptrdiff_type_node
),
341 /* Add the contents of this location to EXP. */
342 return build (PLUS_EXPR
,
343 build_pointer_type (type
),
344 build_unary_op (ADDR_EXPR
, exp
, /*noconvert=*/0),
345 build1 (INDIRECT_REF
, ptrdiff_type_node
, vbase_ptr
));
350 FORMAT_VBASE_NAME (name
, type
);
351 return build_component_ref (exp
, get_identifier (name
), NULL_TREE
, 0);
355 /* Build multi-level access to EXPR using hierarchy path PATH.
356 CODE is PLUS_EXPR if we are going with the grain,
357 and MINUS_EXPR if we are not (in which case, we cannot traverse
358 virtual baseclass links).
360 TYPE is the type we want this path to have on exit.
362 NONNULL is non-zero if we know (for any reason) that EXPR is
363 not, in fact, zero. */
366 build_vbase_path (code
, type
, expr
, path
, nonnull
)
368 tree type
, expr
, path
;
371 register int changed
= 0;
372 tree last
= NULL_TREE
, last_virtual
= NULL_TREE
;
374 tree null_expr
= 0, nonnull_expr
;
376 tree offset
= integer_zero_node
;
378 if (BINFO_INHERITANCE_CHAIN (path
) == NULL_TREE
)
379 return build1 (NOP_EXPR
, type
, expr
);
381 /* We could do better if we had additional logic to convert back to the
382 unconverted type (the static type of the complete object), and then
383 convert back to the type we want. Until that is done, we only optimize
384 if the complete type is the same type as expr has. */
385 fixed_type_p
= resolves_to_fixed_type_p (expr
, &nonnull
);
386 if (fixed_type_p
< 0)
387 /* Virtual base layout is not fixed, even in ctors and dtors. */
390 if (!fixed_type_p
&& TREE_SIDE_EFFECTS (expr
))
391 expr
= save_expr (expr
);
394 path
= reverse_path (path
);
396 basetype
= BINFO_TYPE (path
);
400 if (TREE_VIA_VIRTUAL (TREE_VALUE (path
)))
402 last_virtual
= BINFO_TYPE (TREE_VALUE (path
));
403 if (code
== PLUS_EXPR
)
405 changed
= ! fixed_type_p
;
411 /* We already check for ambiguous things in the caller, just
415 tree binfo
= get_binfo (last
, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr
))), 0);
416 nonnull_expr
= convert_pointer_to_real (binfo
, nonnull_expr
);
418 ind
= build_indirect_ref (nonnull_expr
, NULL_PTR
);
419 nonnull_expr
= build_vbase_pointer (ind
, last_virtual
);
421 && TREE_CODE (type
) == POINTER_TYPE
422 && null_expr
== NULL_TREE
)
424 null_expr
= build1 (NOP_EXPR
, build_pointer_type (last_virtual
), integer_zero_node
);
425 expr
= build (COND_EXPR
, build_pointer_type (last_virtual
),
426 build (EQ_EXPR
, boolean_type_node
, expr
,
428 null_expr
, nonnull_expr
);
431 /* else we'll figure out the offset below. */
433 /* Happens in the case of parse errors. */
434 if (nonnull_expr
== error_mark_node
)
435 return error_mark_node
;
439 cp_error ("cannot cast up from virtual baseclass `%T'",
441 return error_mark_node
;
444 last
= TREE_VALUE (path
);
445 path
= TREE_CHAIN (path
);
447 /* LAST is now the last basetype assoc on the path. */
449 /* A pointer to a virtual base member of a non-null object
450 is non-null. Therefore, we only need to test for zeroness once.
451 Make EXPR the canonical expression to deal with here. */
454 TREE_OPERAND (expr
, 2) = nonnull_expr
;
455 TREE_TYPE (expr
) = TREE_TYPE (TREE_OPERAND (expr
, 1))
456 = TREE_TYPE (nonnull_expr
);
461 /* If we go through any virtual base pointers, make sure that
462 casts to BASETYPE from the last virtual base class use
463 the right value for BASETYPE. */
466 tree intype
= TREE_TYPE (TREE_TYPE (expr
));
468 if (TYPE_MAIN_VARIANT (intype
) != BINFO_TYPE (last
))
470 = BINFO_OFFSET (get_binfo (last
, TYPE_MAIN_VARIANT (intype
), 0));
473 offset
= BINFO_OFFSET (last
);
475 if (! integer_zerop (offset
))
477 /* Bash types to make the backend happy. */
478 offset
= cp_convert (type
, offset
);
480 /* If expr might be 0, we need to preserve that zeroness. */
484 TREE_TYPE (null_expr
) = type
;
486 null_expr
= build1 (NOP_EXPR
, type
, integer_zero_node
);
487 if (TREE_SIDE_EFFECTS (expr
))
488 expr
= save_expr (expr
);
490 return build (COND_EXPR
, type
,
491 build (EQ_EXPR
, boolean_type_node
, expr
, integer_zero_node
),
493 build (code
, type
, expr
, offset
));
495 else return build (code
, type
, expr
, offset
);
498 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
499 be used multiple times in initialization of multiple inheritance. */
502 TREE_TYPE (expr
) = type
;
506 return build1 (NOP_EXPR
, type
, expr
);
510 /* Virtual function things. */
512 /* We want to give the assembler the vtable identifier as well as
513 the offset to the function pointer. So we generate
515 __asm__ __volatile__ (".vtable_entry %c0, %c1"
516 : : "s"(&class_vtable),
517 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
520 build_vtable_entry_ref (basetype
, vtbl
, idx
)
521 tree basetype
, vtbl
, idx
;
523 static char asm_stmt
[] = ".vtable_entry %c0, %c1";
526 s
= build_unary_op (ADDR_EXPR
,
527 get_vtbl_decl_for_binfo (TYPE_BINFO (basetype
)),
529 s
= build_tree_list (build_string (1, "s"), s
);
531 i
= build_array_ref (vtbl
, idx
);
532 if (!flag_vtable_thunks
)
533 i
= build_component_ref (i
, pfn_identifier
, vtable_entry_type
, 0);
534 i
= build_c_cast (ptrdiff_type_node
, build_unary_op (ADDR_EXPR
, i
, 0));
535 i2
= build_array_ref (vtbl
, build_int_2(0,0));
536 i2
= build_c_cast (ptrdiff_type_node
, build_unary_op (ADDR_EXPR
, i2
, 0));
537 i
= cp_build_binary_op (MINUS_EXPR
, i
, i2
);
538 i
= build_tree_list (build_string (1, "i"), i
);
540 finish_asm_stmt (ridpointers
[RID_VOLATILE
],
541 build_string (sizeof(asm_stmt
)-1, asm_stmt
),
542 NULL_TREE
, chainon (s
, i
), NULL_TREE
);
545 /* Given an object INSTANCE, return an expression which yields the
546 virtual function vtable element corresponding to INDEX. There are
547 many special cases for INSTANCE which we take care of here, mainly
548 to avoid creating extra tree nodes when we don't have to. */
551 build_vtbl_ref (instance
, idx
)
555 tree basetype
= TREE_TYPE (instance
);
557 if (TREE_CODE (basetype
) == REFERENCE_TYPE
)
558 basetype
= TREE_TYPE (basetype
);
560 if (instance
== current_class_ref
)
561 vtbl
= build_vfield_ref (instance
, basetype
);
566 /* Try to figure out what a reference refers to, and
567 access its virtual function table directly. */
568 tree ref
= NULL_TREE
;
570 if (TREE_CODE (instance
) == INDIRECT_REF
571 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance
, 0))) == REFERENCE_TYPE
)
572 ref
= TREE_OPERAND (instance
, 0);
573 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
576 if (ref
&& TREE_CODE (ref
) == VAR_DECL
577 && DECL_INITIAL (ref
))
579 tree init
= DECL_INITIAL (ref
);
581 while (TREE_CODE (init
) == NOP_EXPR
582 || TREE_CODE (init
) == NON_LVALUE_EXPR
)
583 init
= TREE_OPERAND (init
, 0);
584 if (TREE_CODE (init
) == ADDR_EXPR
)
586 init
= TREE_OPERAND (init
, 0);
587 if (IS_AGGR_TYPE (TREE_TYPE (init
))
588 && (TREE_CODE (init
) == PARM_DECL
589 || TREE_CODE (init
) == VAR_DECL
))
595 if (IS_AGGR_TYPE (TREE_TYPE (instance
))
596 && (TREE_CODE (instance
) == RESULT_DECL
597 || TREE_CODE (instance
) == PARM_DECL
598 || TREE_CODE (instance
) == VAR_DECL
))
600 vtbl
= TYPE_BINFO_VTABLE (basetype
);
601 /* Knowing the dynamic type of INSTANCE we can easily obtain
602 the correct vtable entry. In the new ABI, we resolve
603 this back to be in terms of the primary vtable. */
604 if (TREE_CODE (vtbl
) == PLUS_EXPR
)
606 idx
= fold (build (PLUS_EXPR
,
609 build (EXACT_DIV_EXPR
,
611 TREE_OPERAND (vtbl
, 1),
612 TYPE_SIZE_UNIT (vtable_entry_type
))));
613 vtbl
= get_vtbl_decl_for_binfo (TYPE_BINFO (basetype
));
617 vtbl
= build_vfield_ref (instance
, basetype
);
620 assemble_external (vtbl
);
623 build_vtable_entry_ref (basetype
, vtbl
, idx
);
625 aref
= build_array_ref (vtbl
, idx
);
630 /* Given an object INSTANCE, return an expression which yields the
631 virtual function corresponding to INDEX. There are many special
632 cases for INSTANCE which we take care of here, mainly to avoid
633 creating extra tree nodes when we don't have to. */
636 build_vfn_ref (ptr_to_instptr
, instance
, idx
)
637 tree
*ptr_to_instptr
, instance
;
640 tree aref
= build_vtbl_ref (instance
, idx
);
642 /* When using thunks, there is no extra delta, and we get the pfn
644 if (flag_vtable_thunks
)
649 /* Save the intermediate result in a SAVE_EXPR so we don't have to
650 compute each component of the virtual function pointer twice. */
651 if (TREE_CODE (aref
) == INDIRECT_REF
)
652 TREE_OPERAND (aref
, 0) = save_expr (TREE_OPERAND (aref
, 0));
655 = build (PLUS_EXPR
, TREE_TYPE (*ptr_to_instptr
),
657 cp_convert (ptrdiff_type_node
,
658 build_component_ref (aref
, delta_identifier
, NULL_TREE
, 0)));
661 return build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
664 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
665 for the given TYPE. */
668 get_vtable_name (type
)
671 return mangle_vtbl_for_type (type
);
674 /* Return an IDENTIFIER_NODE for the name of the virtual table table
681 return mangle_vtt_for_type (type
);
684 /* Return the offset to the main vtable for a given base BINFO. */
687 get_vfield_offset (binfo
)
691 size_binop (PLUS_EXPR
, byte_position (TYPE_VFIELD (BINFO_TYPE (binfo
))),
692 BINFO_OFFSET (binfo
));
695 /* Get the offset to the start of the original binfo that we derived
696 this binfo from. If we find TYPE first, return the offset only
697 that far. The shortened search is useful because the this pointer
698 on method calling is expected to point to a DECL_CONTEXT (fndecl)
699 object, and not a baseclass of it. */
702 get_derived_offset (binfo
, type
)
705 tree offset1
= get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo
)));
708 while (!same_type_p (BINFO_TYPE (binfo
), type
))
709 binfo
= get_primary_binfo (binfo
);
711 offset2
= get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo
)));
712 return size_binop (MINUS_EXPR
, offset1
, offset2
);
715 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
716 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
717 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
720 build_vtable (class_type
, name
, vtable_type
)
727 decl
= build_lang_decl (VAR_DECL
, name
, vtable_type
);
728 DECL_CONTEXT (decl
) = class_type
;
729 DECL_ARTIFICIAL (decl
) = 1;
730 TREE_STATIC (decl
) = 1;
731 #ifndef WRITABLE_VTABLES
732 /* Make them READONLY by default. (mrs) */
733 TREE_READONLY (decl
) = 1;
735 DECL_VIRTUAL_P (decl
) = 1;
736 import_export_vtable (decl
, class_type
, 0);
741 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
742 or even complete. If this does not exist, create it. If COMPLETE is
743 non-zero, then complete the definition of it -- that will render it
744 impossible to actually build the vtable, but is useful to get at those
745 which are known to exist in the runtime. */
748 get_vtable_decl (type
, complete
)
752 tree name
= get_vtable_name (type
);
753 tree decl
= IDENTIFIER_GLOBAL_VALUE (name
);
757 my_friendly_assert (TREE_CODE (decl
) == VAR_DECL
758 && DECL_VIRTUAL_P (decl
), 20000118);
762 decl
= build_vtable (type
, name
, void_type_node
);
763 decl
= pushdecl_top_level (decl
);
764 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name
) == decl
,
767 /* At one time the vtable info was grabbed 2 words at a time. This
768 fails on sparc unless you have 8-byte alignment. (tiemann) */
769 DECL_ALIGN (decl
) = MAX (TYPE_ALIGN (double_type_node
),
774 DECL_EXTERNAL (decl
) = 1;
775 cp_finish_decl (decl
, NULL_TREE
, NULL_TREE
, 0);
781 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
782 BV_VCALL_INDEX for each entry is cleared. */
785 copy_virtuals (binfo
)
791 copies
= copy_list (BINFO_VIRTUALS (binfo
));
792 for (t
= copies
; t
; t
= TREE_CHAIN (t
))
794 BV_VCALL_INDEX (t
) = NULL_TREE
;
795 BV_USE_VCALL_INDEX_P (t
) = 0;
796 BV_GENERATE_THUNK_WITH_VTABLE_P (t
) = 0;
802 /* Build the primary virtual function table for TYPE. If BINFO is
803 non-NULL, build the vtable starting with the initial approximation
804 that it is the same as the one which is the head of the association
805 list. Returns a non-zero value if a new vtable is actually
809 build_primary_vtable (binfo
, type
)
815 decl
= get_vtable_decl (type
, /*complete=*/0);
819 if (BINFO_NEW_VTABLE_MARKED (binfo
, type
))
820 /* We have already created a vtable for this base, so there's
821 no need to do it again. */
824 virtuals
= copy_virtuals (binfo
);
825 TREE_TYPE (decl
) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo
));
826 DECL_SIZE (decl
) = TYPE_SIZE (TREE_TYPE (decl
));
827 DECL_SIZE_UNIT (decl
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
831 my_friendly_assert (TREE_CODE (TREE_TYPE (decl
)) == VOID_TYPE
,
833 virtuals
= NULL_TREE
;
836 #ifdef GATHER_STATISTICS
838 n_vtable_elems
+= list_length (virtuals
);
841 /* Initialize the association list for this type, based
842 on our first approximation. */
843 TYPE_BINFO_VTABLE (type
) = decl
;
844 TYPE_BINFO_VIRTUALS (type
) = virtuals
;
845 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type
), type
);
849 /* Give TYPE a new virtual function table which is initialized
850 with a skeleton-copy of its original initialization. The only
851 entry that changes is the `delta' entry, so we can really
852 share a lot of structure.
854 FOR_TYPE is the derived type which caused this table to
857 BINFO is the type association which provided TYPE for FOR_TYPE.
859 The order in which vtables are built (by calling this function) for
860 an object must remain the same, otherwise a binary incompatibility
864 build_secondary_vtable (binfo
, for_type
)
865 tree binfo
, for_type
;
868 tree orig_decl
= BINFO_VTABLE (binfo
);
882 if (TREE_VIA_VIRTUAL (binfo
))
883 my_friendly_assert (binfo
== binfo_for_vbase (BINFO_TYPE (binfo
),
887 if (BINFO_NEW_VTABLE_MARKED (binfo
, current_class_type
))
888 /* We already created a vtable for this base. There's no need to
892 /* Remember that we've created a vtable for this BINFO, so that we
893 don't try to do so again. */
894 SET_BINFO_NEW_VTABLE_MARKED (binfo
, current_class_type
);
896 /* Make fresh virtual list, so we can smash it later. */
897 BINFO_VIRTUALS (binfo
) = copy_virtuals (binfo
);
899 if (TREE_VIA_VIRTUAL (binfo
))
901 tree binfo1
= binfo_for_vbase (BINFO_TYPE (binfo
), for_type
);
903 /* XXX - This should never happen, if it does, the caller should
904 ensure that the binfo is from for_type's binfos, not from any
905 base type's. We can remove all this code after a while. */
907 warning ("internal inconsistency: binfo offset error for rtti");
909 offset
= BINFO_OFFSET (binfo1
);
912 offset
= BINFO_OFFSET (binfo
);
914 /* In the new ABI, secondary vtables are laid out as part of the
915 same structure as the primary vtable. */
916 if (merge_primary_and_secondary_vtables_p ())
918 BINFO_VTABLE (binfo
) = NULL_TREE
;
922 /* Create the declaration for the secondary vtable. */
923 basetype
= TYPE_MAIN_VARIANT (BINFO_TYPE (binfo
));
924 buf2
= TYPE_ASSEMBLER_NAME_STRING (basetype
);
925 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1;
927 /* We know that the vtable that we are going to create doesn't exist
928 yet in the global namespace, and when we finish, it will be
929 pushed into the global namespace. In complex MI hierarchies, we
930 have to loop while the name we are thinking of adding is globally
931 defined, adding more name components to the vtable name as we
932 loop, until the name is unique. This is because in complex MI
933 cases, we might have the same base more than once. This means
934 that the order in which this function is called for vtables must
935 remain the same, otherwise binary compatibility can be
940 char *buf1
= (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type
)
944 sprintf (buf1
, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type
), joiner
,
946 buf
= (char *) alloca (strlen (VTABLE_NAME_PREFIX
) + strlen (buf1
) + 1);
947 sprintf (buf
, "%s%s", VTABLE_NAME_PREFIX
, buf1
);
948 name
= get_identifier (buf
);
950 /* If this name doesn't clash, then we can use it, otherwise
951 we add more to the name until it is unique. */
953 if (! IDENTIFIER_GLOBAL_VALUE (name
))
956 /* Set values for next loop through, if the name isn't unique. */
958 path
= BINFO_INHERITANCE_CHAIN (path
);
960 /* We better not run out of stuff to make it unique. */
961 my_friendly_assert (path
!= NULL_TREE
, 368);
963 basetype
= TYPE_MAIN_VARIANT (BINFO_TYPE (path
));
965 if (for_type
== basetype
)
967 /* If we run out of basetypes in the path, we have already
968 found created a vtable with that name before, we now
969 resort to tacking on _%d to distinguish them. */
971 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1 + i
+ 1 + 3;
972 buf1
= (char *) alloca (i
);
974 sprintf (buf1
, "%s%c%s%c%d",
975 TYPE_ASSEMBLER_NAME_STRING (basetype
), joiner
,
977 buf
= (char *) alloca (strlen (VTABLE_NAME_PREFIX
)
978 + strlen (buf1
) + 1);
979 sprintf (buf
, "%s%s", VTABLE_NAME_PREFIX
, buf1
);
980 name
= get_identifier (buf
);
982 /* If this name doesn't clash, then we can use it,
983 otherwise we add something different to the name until
985 } while (++j
<= 999 && IDENTIFIER_GLOBAL_VALUE (name
));
987 /* Hey, they really like MI don't they? Increase the 3
988 above to 6, and the 999 to 999999. :-) */
989 my_friendly_assert (j
<= 999, 369);
994 i
= TYPE_ASSEMBLER_NAME_LENGTH (basetype
) + 1 + i
;
995 new_buf2
= (char *) alloca (i
);
996 sprintf (new_buf2
, "%s%c%s",
997 TYPE_ASSEMBLER_NAME_STRING (basetype
), joiner
, buf2
);
1001 new_decl
= build_vtable (for_type
, name
, TREE_TYPE (orig_decl
));
1002 DECL_ALIGN (new_decl
) = DECL_ALIGN (orig_decl
);
1003 DECL_USER_ALIGN (new_decl
) = DECL_USER_ALIGN (orig_decl
);
1004 BINFO_VTABLE (binfo
) = pushdecl_top_level (new_decl
);
1006 #ifdef GATHER_STATISTICS
1008 n_vtable_elems
+= list_length (BINFO_VIRTUALS (binfo
));
1014 /* Create a new vtable for BINFO which is the hierarchy dominated by
1018 make_new_vtable (t
, binfo
)
1022 if (binfo
== TYPE_BINFO (t
))
1023 /* In this case, it is *type*'s vtable we are modifying. We start
1024 with the approximation that it's vtable is that of the
1025 immediate base class. */
1026 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t
))),
1029 /* This is our very own copy of `basetype' to play with. Later,
1030 we will fill in all the virtual functions that override the
1031 virtual functions in these base classes which are not defined
1032 by the current type. */
1033 return build_secondary_vtable (binfo
, t
);
1036 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
1037 (which is in the hierarchy dominated by T) list FNDECL as its
1038 BV_FN. DELTA is the required constant adjustment from the `this'
1039 pointer where the vtable entry appears to the `this' required when
1040 the function is actually called. */
1043 modify_vtable_entry (t
, binfo
, fndecl
, delta
, virtuals
)
1054 if (fndecl
!= BV_FN (v
)
1055 || !tree_int_cst_equal (delta
, BV_DELTA (v
)))
1059 /* We need a new vtable for BINFO. */
1060 if (make_new_vtable (t
, binfo
))
1062 /* If we really did make a new vtable, we also made a copy
1063 of the BINFO_VIRTUALS list. Now, we have to find the
1064 corresponding entry in that list. */
1065 *virtuals
= BINFO_VIRTUALS (binfo
);
1066 while (BV_FN (*virtuals
) != BV_FN (v
))
1067 *virtuals
= TREE_CHAIN (*virtuals
);
1071 base_fndecl
= BV_FN (v
);
1072 BV_DELTA (v
) = delta
;
1073 BV_VCALL_INDEX (v
) = NULL_TREE
;
1076 /* Now assign virtual dispatch information, if unset. We can
1077 dispatch this, through any overridden base function. */
1078 if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
1080 DECL_VINDEX (fndecl
) = DECL_VINDEX (base_fndecl
);
1081 DECL_VIRTUAL_CONTEXT (fndecl
) = DECL_VIRTUAL_CONTEXT (base_fndecl
);
1086 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
1087 functions present in the vtable so far. */
1090 set_vindex (decl
, vfuns_p
)
1096 vindex
= (*vfuns_p
)++;
1097 DECL_VINDEX (decl
) = build_shared_int_cst (vindex
);
1100 /* Add a virtual function to all the appropriate vtables for the class
1101 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1102 allocate a new slot in our table. If it is error_mark_node, we
1103 know that no other function from another vtable is overridden by X.
1104 VFUNS_P keeps track of how many virtuals there are in our
1105 main vtable for the type, and we build upon the NEW_VIRTUALS list
1109 add_virtual_function (new_virtuals_p
, overridden_virtuals_p
,
1111 tree
*new_virtuals_p
;
1112 tree
*overridden_virtuals_p
;
1115 tree t
; /* Structure type. */
1119 /* If this function doesn't override anything from a base class, we
1120 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1121 override something, we keep it around and assign its DECL_VINDEX
1122 later, in modify_all_vtables. */
1123 if (TREE_CODE (DECL_VINDEX (fndecl
)) == INTEGER_CST
)
1124 /* We've already dealt with this function. */
1127 new_virtual
= make_node (TREE_LIST
);
1128 BV_FN (new_virtual
) = fndecl
;
1129 BV_DELTA (new_virtual
) = integer_zero_node
;
1131 if (DECL_VINDEX (fndecl
) == error_mark_node
)
1133 /* FNDECL is a new virtual function; it doesn't override any
1134 virtual function in a base class. */
1136 /* We remember that this was the base sub-object for rtti. */
1137 CLASSTYPE_RTTI (t
) = t
;
1139 /* Now assign virtual dispatch information. */
1140 set_vindex (fndecl
, vfuns_p
);
1141 DECL_VIRTUAL_CONTEXT (fndecl
) = t
;
1143 /* Save the state we've computed on the NEW_VIRTUALS list. */
1144 TREE_CHAIN (new_virtual
) = *new_virtuals_p
;
1145 *new_virtuals_p
= new_virtual
;
1149 /* FNDECL overrides a function from a base class. */
1150 TREE_CHAIN (new_virtual
) = *overridden_virtuals_p
;
1151 *overridden_virtuals_p
= new_virtual
;
1155 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
1156 the method after the class has already been defined because a
1157 declaration for it was seen. (Even though that is erroneous, we
1158 add the method for improved error recovery.) */
1161 add_method (type
, method
, error_p
)
1166 int using = (DECL_CONTEXT (method
) != type
);
1171 if (!CLASSTYPE_METHOD_VEC (type
))
1172 /* Make a new method vector. We start with 8 entries. We must
1173 allocate at least two (for constructors and destructors), and
1174 we're going to end up with an assignment operator at some point
1177 We could use a TREE_LIST for now, and convert it to a TREE_VEC
1178 in finish_struct, but we would probably waste more memory
1179 making the links in the list than we would by over-allocating
1180 the size of the vector here. Furthermore, we would complicate
1181 all the code that expects this to be a vector. */
1182 CLASSTYPE_METHOD_VEC (type
) = make_tree_vec (8);
1184 method_vec
= CLASSTYPE_METHOD_VEC (type
);
1185 len
= TREE_VEC_LENGTH (method_vec
);
1187 /* Constructors and destructors go in special slots. */
1188 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method
))
1189 slot
= CLASSTYPE_CONSTRUCTOR_SLOT
;
1190 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method
))
1191 slot
= CLASSTYPE_DESTRUCTOR_SLOT
;
1194 /* See if we already have an entry with this name. */
1195 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
; slot
< len
; ++slot
)
1196 if (!TREE_VEC_ELT (method_vec
, slot
)
1197 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec
,
1199 == DECL_NAME (method
)))
1204 /* We need a bigger method vector. */
1208 /* In the non-error case, we are processing a class
1209 definition. Double the size of the vector to give room
1213 /* In the error case, the vector is already complete. We
1214 don't expect many errors, and the rest of the front-end
1215 will get confused if there are empty slots in the vector. */
1219 new_vec
= make_tree_vec (new_len
);
1220 bcopy ((PTR
) &TREE_VEC_ELT (method_vec
, 0),
1221 (PTR
) &TREE_VEC_ELT (new_vec
, 0),
1222 len
* sizeof (tree
));
1224 method_vec
= CLASSTYPE_METHOD_VEC (type
) = new_vec
;
1227 if (DECL_CONV_FN_P (method
) && !TREE_VEC_ELT (method_vec
, slot
))
1229 /* Type conversion operators have to come before ordinary
1230 methods; add_conversions depends on this to speed up
1231 looking for conversion operators. So, if necessary, we
1232 slide some of the vector elements up. In theory, this
1233 makes this algorithm O(N^2) but we don't expect many
1234 conversion operators. */
1235 for (slot
= 2; slot
< len
; ++slot
)
1237 tree fn
= TREE_VEC_ELT (method_vec
, slot
);
1240 /* There are no more entries in the vector, so we
1241 can insert the new conversion operator here. */
1244 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
1245 /* We can insert the new function right at the
1250 if (!TREE_VEC_ELT (method_vec
, slot
))
1251 /* There is nothing in the Ith slot, so we can avoid
1256 /* We know the last slot in the vector is empty
1257 because we know that at this point there's room
1258 for a new function. */
1259 bcopy ((PTR
) &TREE_VEC_ELT (method_vec
, slot
),
1260 (PTR
) &TREE_VEC_ELT (method_vec
, slot
+ 1),
1261 (len
- slot
- 1) * sizeof (tree
));
1262 TREE_VEC_ELT (method_vec
, slot
) = NULL_TREE
;
1267 if (template_class_depth (type
))
1268 /* TYPE is a template class. Don't issue any errors now; wait
1269 until instantiation time to complain. */
1275 /* Check to see if we've already got this method. */
1276 for (fns
= TREE_VEC_ELT (method_vec
, slot
);
1278 fns
= OVL_NEXT (fns
))
1280 tree fn
= OVL_CURRENT (fns
);
1282 if (TREE_CODE (fn
) != TREE_CODE (method
))
1285 if (TREE_CODE (method
) != TEMPLATE_DECL
)
1287 /* [over.load] Member function declarations with the
1288 same name and the same parameter types cannot be
1289 overloaded if any of them is a static member
1290 function declaration. */
1291 if ((DECL_STATIC_FUNCTION_P (fn
)
1292 != DECL_STATIC_FUNCTION_P (method
))
1295 tree parms1
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1296 tree parms2
= TYPE_ARG_TYPES (TREE_TYPE (method
));
1298 if (! DECL_STATIC_FUNCTION_P (fn
))
1299 parms1
= TREE_CHAIN (parms1
);
1300 if (! DECL_STATIC_FUNCTION_P (method
))
1301 parms2
= TREE_CHAIN (parms2
);
1303 if (compparms (parms1
, parms2
))
1306 /* Defer to the local function. */
1309 cp_error ("`%#D' and `%#D' cannot be overloaded",
1314 /* Since this is an ordinary function in a
1315 non-template class, it's mangled name can be used
1316 as a unique identifier. This technique is only
1317 an optimization; we would get the same results if
1318 we just used decls_match here. */
1319 if (DECL_ASSEMBLER_NAME (fn
)
1320 != DECL_ASSEMBLER_NAME (method
))
1323 else if (!decls_match (fn
, method
))
1326 /* There has already been a declaration of this method
1327 or member template. */
1328 cp_error_at ("`%D' has already been declared in `%T'",
1331 /* We don't call duplicate_decls here to merge the
1332 declarations because that will confuse things if the
1333 methods have inline definitions. In particular, we
1334 will crash while processing the definitions. */
1339 /* Actually insert the new method. */
1340 TREE_VEC_ELT (method_vec
, slot
)
1341 = build_overload (method
, TREE_VEC_ELT (method_vec
, slot
));
1343 /* Add the new binding. */
1344 if (!DECL_CONSTRUCTOR_P (method
)
1345 && !DECL_DESTRUCTOR_P (method
))
1346 push_class_level_binding (DECL_NAME (method
),
1347 TREE_VEC_ELT (method_vec
, slot
));
1350 /* Subroutines of finish_struct. */
1352 /* Look through the list of fields for this struct, deleting
1353 duplicates as we go. This must be recursive to handle
1356 FIELD is the field which may not appear anywhere in FIELDS.
1357 FIELD_PTR, if non-null, is the starting point at which
1358 chained deletions may take place.
1359 The value returned is the first acceptable entry found
1362 Note that anonymous fields which are not of UNION_TYPE are
1363 not duplicates, they are just anonymous fields. This happens
1364 when we have unnamed bitfields, for example. */
1367 delete_duplicate_fields_1 (field
, fields
)
1372 if (DECL_NAME (field
) == 0)
1374 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1377 for (x
= TYPE_FIELDS (TREE_TYPE (field
)); x
; x
= TREE_CHAIN (x
))
1378 fields
= delete_duplicate_fields_1 (x
, fields
);
1383 for (x
= fields
; x
; prev
= x
, x
= TREE_CHAIN (x
))
1385 if (DECL_NAME (x
) == 0)
1387 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
1389 TYPE_FIELDS (TREE_TYPE (x
))
1390 = delete_duplicate_fields_1 (field
, TYPE_FIELDS (TREE_TYPE (x
)));
1391 if (TYPE_FIELDS (TREE_TYPE (x
)) == 0)
1394 fields
= TREE_CHAIN (fields
);
1396 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1399 else if (TREE_CODE (field
) == USING_DECL
)
1400 /* A using declaration may is allowed to appear more than
1401 once. We'll prune these from the field list later, and
1402 handle_using_decl will complain about invalid multiple
1405 else if (DECL_NAME (field
) == DECL_NAME (x
))
1407 if (TREE_CODE (field
) == CONST_DECL
1408 && TREE_CODE (x
) == CONST_DECL
)
1409 cp_error_at ("duplicate enum value `%D'", x
);
1410 else if (TREE_CODE (field
) == CONST_DECL
1411 || TREE_CODE (x
) == CONST_DECL
)
1412 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1414 else if (DECL_DECLARES_TYPE_P (field
)
1415 && DECL_DECLARES_TYPE_P (x
))
1417 if (same_type_p (TREE_TYPE (field
), TREE_TYPE (x
)))
1419 cp_error_at ("duplicate nested type `%D'", x
);
1421 else if (DECL_DECLARES_TYPE_P (field
)
1422 || DECL_DECLARES_TYPE_P (x
))
1424 /* Hide tag decls. */
1425 if ((TREE_CODE (field
) == TYPE_DECL
1426 && DECL_ARTIFICIAL (field
))
1427 || (TREE_CODE (x
) == TYPE_DECL
1428 && DECL_ARTIFICIAL (x
)))
1430 cp_error_at ("duplicate field `%D' (as type and non-type)",
1434 cp_error_at ("duplicate member `%D'", x
);
1436 fields
= TREE_CHAIN (fields
);
1438 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1446 delete_duplicate_fields (fields
)
1450 for (x
= fields
; x
&& TREE_CHAIN (x
); x
= TREE_CHAIN (x
))
1451 TREE_CHAIN (x
) = delete_duplicate_fields_1 (x
, TREE_CHAIN (x
));
1454 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1455 legit, otherwise return 0. */
1458 alter_access (t
, fdecl
, access
)
1465 if (!DECL_LANG_SPECIFIC (fdecl
))
1466 retrofit_lang_decl (fdecl
);
1468 elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1471 if (TREE_VALUE (elem
) != access
)
1473 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1474 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl
));
1476 error ("conflicting access specifications for field `%s', ignored",
1477 IDENTIFIER_POINTER (DECL_NAME (fdecl
)));
1481 /* They're changing the access to the same thing they changed
1482 it to before. That's OK. */
1488 enforce_access (t
, fdecl
);
1489 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1495 /* Process the USING_DECL, which is a member of T. */
1498 handle_using_decl (using_decl
, t
)
1502 tree ctype
= DECL_INITIAL (using_decl
);
1503 tree name
= DECL_NAME (using_decl
);
1505 = TREE_PRIVATE (using_decl
) ? access_private_node
1506 : TREE_PROTECTED (using_decl
) ? access_protected_node
1507 : access_public_node
;
1509 tree flist
= NULL_TREE
;
1512 binfo
= binfo_or_else (ctype
, t
);
1516 if (name
== constructor_name (ctype
)
1517 || name
== constructor_name_full (ctype
))
1519 cp_error_at ("`%D' names constructor", using_decl
);
1522 if (name
== constructor_name (t
)
1523 || name
== constructor_name_full (t
))
1525 cp_error_at ("`%D' invalid in `%T'", using_decl
, t
);
1529 fdecl
= lookup_member (binfo
, name
, 0, 0);
1533 cp_error_at ("no members matching `%D' in `%#T'", using_decl
, ctype
);
1537 if (BASELINK_P (fdecl
))
1538 /* Ignore base type this came from. */
1539 fdecl
= TREE_VALUE (fdecl
);
1541 old_value
= IDENTIFIER_CLASS_VALUE (name
);
1544 if (is_overloaded_fn (old_value
))
1545 old_value
= OVL_CURRENT (old_value
);
1547 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1550 old_value
= NULL_TREE
;
1553 if (is_overloaded_fn (fdecl
))
1558 else if (is_overloaded_fn (old_value
))
1561 /* It's OK to use functions from a base when there are functions with
1562 the same name already present in the current class. */;
1565 cp_error_at ("`%D' invalid in `%#T'", using_decl
, t
);
1566 cp_error_at (" because of local method `%#D' with same name",
1567 OVL_CURRENT (old_value
));
1571 else if (!DECL_ARTIFICIAL (old_value
))
1573 cp_error_at ("`%D' invalid in `%#T'", using_decl
, t
);
1574 cp_error_at (" because of local member `%#D' with same name", old_value
);
1578 /* Make type T see field decl FDECL with access ACCESS.*/
1580 for (; flist
; flist
= OVL_NEXT (flist
))
1582 add_method (t
, OVL_CURRENT (flist
), /*error_p=*/0);
1583 alter_access (t
, OVL_CURRENT (flist
), access
);
1586 alter_access (t
, fdecl
, access
);
1589 /* Run through the base clases of T, updating
1590 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1591 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1595 check_bases (t
, cant_have_default_ctor_p
, cant_have_const_ctor_p
,
1598 int *cant_have_default_ctor_p
;
1599 int *cant_have_const_ctor_p
;
1600 int *no_const_asn_ref_p
;
1604 int seen_nearly_empty_base_p
;
1607 binfos
= TYPE_BINFO_BASETYPES (t
);
1608 n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
1609 seen_nearly_empty_base_p
= 0;
1611 /* An aggregate cannot have baseclasses. */
1612 CLASSTYPE_NON_AGGREGATE (t
) |= (n_baseclasses
!= 0);
1614 for (i
= 0; i
< n_baseclasses
; ++i
)
1619 /* Figure out what base we're looking at. */
1620 base_binfo
= TREE_VEC_ELT (binfos
, i
);
1621 basetype
= TREE_TYPE (base_binfo
);
1623 /* If the type of basetype is incomplete, then we already
1624 complained about that fact (and we should have fixed it up as
1626 if (!COMPLETE_TYPE_P (basetype
))
1629 /* The base type is of incomplete type. It is
1630 probably best to pretend that it does not
1632 if (i
== n_baseclasses
-1)
1633 TREE_VEC_ELT (binfos
, i
) = NULL_TREE
;
1634 TREE_VEC_LENGTH (binfos
) -= 1;
1636 for (j
= i
; j
+1 < n_baseclasses
; j
++)
1637 TREE_VEC_ELT (binfos
, j
) = TREE_VEC_ELT (binfos
, j
+1);
1641 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1642 here because the case of virtual functions but non-virtual
1643 dtor is handled in finish_struct_1. */
1644 if (warn_ecpp
&& ! TYPE_POLYMORPHIC_P (basetype
)
1645 && TYPE_HAS_DESTRUCTOR (basetype
))
1646 cp_warning ("base class `%#T' has a non-virtual destructor",
1649 /* If the base class doesn't have copy constructors or
1650 assignment operators that take const references, then the
1651 derived class cannot have such a member automatically
1653 if (! TYPE_HAS_CONST_INIT_REF (basetype
))
1654 *cant_have_const_ctor_p
= 1;
1655 if (TYPE_HAS_ASSIGN_REF (basetype
)
1656 && !TYPE_HAS_CONST_ASSIGN_REF (basetype
))
1657 *no_const_asn_ref_p
= 1;
1658 /* Similarly, if the base class doesn't have a default
1659 constructor, then the derived class won't have an
1660 automatically generated default constructor. */
1661 if (TYPE_HAS_CONSTRUCTOR (basetype
)
1662 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
))
1664 *cant_have_default_ctor_p
= 1;
1665 if (! TYPE_HAS_CONSTRUCTOR (t
))
1666 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1670 /* If the base class is not empty or nearly empty, then this
1671 class cannot be nearly empty. */
1672 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype
) && !is_empty_class (basetype
))
1673 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1674 /* And if there is more than one nearly empty base, then the
1675 derived class is not nearly empty either. */
1676 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
)
1677 && seen_nearly_empty_base_p
)
1678 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1679 /* If this is the first nearly empty base class, then remember
1681 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1682 seen_nearly_empty_base_p
= 1;
1684 /* A lot of properties from the bases also apply to the derived
1686 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1687 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1688 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1689 TYPE_HAS_COMPLEX_ASSIGN_REF (t
)
1690 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype
);
1691 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (basetype
);
1692 TYPE_OVERLOADS_CALL_EXPR (t
) |= TYPE_OVERLOADS_CALL_EXPR (basetype
);
1693 TYPE_OVERLOADS_ARRAY_REF (t
) |= TYPE_OVERLOADS_ARRAY_REF (basetype
);
1694 TYPE_OVERLOADS_ARROW (t
) |= TYPE_OVERLOADS_ARROW (basetype
);
1695 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1697 /* Derived classes can implicitly become COMified if their bases
1699 if (CLASSTYPE_COM_INTERFACE (basetype
))
1700 CLASSTYPE_COM_INTERFACE (t
) = 1;
1701 else if (i
== 0 && CLASSTYPE_COM_INTERFACE (t
))
1704 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1706 CLASSTYPE_COM_INTERFACE (t
) = 0;
1711 /* Binfo FROM is within a virtual heirarchy which is being reseated to
1712 TO. Move primary information from FROM to TO, and recursively traverse
1713 into FROM's bases. The heirarchy is dominated by TYPE. MAPPINGS is an
1714 assoc list of binfos that have already been reseated. */
1717 force_canonical_binfo_r (to
, from
, type
, mappings
)
1723 int i
, n_baseclasses
= BINFO_N_BASETYPES (from
);
1725 BINFO_INDIRECT_PRIMARY_P (to
)
1726 = BINFO_INDIRECT_PRIMARY_P (from
);
1727 BINFO_INDIRECT_PRIMARY_P (from
) = 0;
1728 if (BINFO_PRIMARY_P (from
))
1730 tree primary
= BINFO_PRIMARY_BASE_OF (from
);
1733 /* We might have just moved the primary base too, see if it's on our
1735 assoc
= purpose_member (primary
, mappings
);
1737 primary
= TREE_VALUE (assoc
);
1738 BINFO_PRIMARY_BASE_OF (to
) = primary
;
1739 BINFO_PRIMARY_BASE_OF (from
) = NULL_TREE
;
1741 my_friendly_assert (same_type_p (BINFO_TYPE (to
), BINFO_TYPE (from
)), 20010104);
1742 mappings
= tree_cons (from
, to
, mappings
);
1743 for (i
= 0; i
!= n_baseclasses
; i
++)
1745 tree from_binfo
= BINFO_BASETYPE (from
, i
);
1746 tree to_binfo
= BINFO_BASETYPE (to
, i
);
1748 if (TREE_VIA_VIRTUAL (from_binfo
))
1750 tree shared_binfo
= binfo_for_vbase (BINFO_TYPE (from_binfo
), type
);
1752 if (shared_binfo
== from_binfo
)
1753 force_canonical_binfo (to_binfo
, from_binfo
, type
, mappings
);
1756 force_canonical_binfo_r (to_binfo
, from_binfo
, type
, mappings
);
1760 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1761 make TO the canonical binfo, within the heirarchy dominated by TYPE.
1762 MAPPINGS is an assoc list of binfos that have already been reseated.
1763 Adjust any non-virtual bases within FROM, and also move any virtual bases
1764 which are canonical. This complication arises because selecting primary
1765 bases walks in inheritance graph order, but we don't share binfos for
1766 virtual bases, hence we can fill in the primaries for a virtual base,
1767 and then discover that a later base requires the virtual as its
1771 force_canonical_binfo (to
, from
, type
, mappings
)
1777 tree assoc
= purpose_member (BINFO_TYPE (to
),
1778 CLASSTYPE_VBASECLASSES (type
));
1779 TREE_VALUE (assoc
) = to
;
1780 force_canonical_binfo_r (to
, from
, type
, mappings
);
1783 /* Make BASE_BINFO the primary virtual base of BINFO within the hierarchy
1784 dominated by TYPE. Returns BASE_BINFO, if it can be made so, NULL
1785 otherwise (because something else has already made it primary). */
1788 mark_primary_virtual_base (binfo
, base_binfo
, type
)
1793 tree shared_binfo
= binfo_for_vbase (BINFO_TYPE (base_binfo
), type
);
1796 if (BINFO_PRIMARY_P (shared_binfo
))
1798 /* It's already allocated in the hierarchy. BINFO won't have a
1799 primary base in this hierachy, even though the complete object
1800 BINFO is for, would do. */
1801 BINFO_LOST_PRIMARY_P (binfo
) = 1;
1806 /* We need to make sure that the assoc list
1807 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1808 primary BINFO for the virtual base, as this is the one
1809 that'll really exist. */
1810 if (base_binfo
!= shared_binfo
)
1811 force_canonical_binfo (base_binfo
, shared_binfo
, type
, NULL
);
1813 delta
= size_diffop (BINFO_OFFSET (binfo
), BINFO_OFFSET (base_binfo
));
1814 if (!integer_zerop (delta
))
1816 propagate_binfo_offsets (base_binfo
, delta
);
1817 BINFO_OFFSET (base_binfo
) = BINFO_OFFSET (binfo
);
1822 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1823 dominated by TYPE that are primary bases. */
1826 mark_primary_bases (type
)
1831 /* Walk the bases in inheritance graph order. */
1832 for (binfo
= TYPE_BINFO (type
); binfo
; binfo
= TREE_CHAIN (binfo
))
1836 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo
)))
1837 /* Not a dynamic base. */
1840 base_binfo
= get_primary_binfo (binfo
);
1842 if (TREE_VIA_VIRTUAL (base_binfo
))
1843 base_binfo
= mark_primary_virtual_base (binfo
, base_binfo
, type
);
1846 BINFO_PRIMARY_BASE_OF (base_binfo
) = binfo
;
1850 /* Make the BINFO the primary base of T. */
1853 set_primary_base (t
, binfo
, vfuns_p
)
1860 CLASSTYPE_PRIMARY_BINFO (t
) = binfo
;
1861 basetype
= BINFO_TYPE (binfo
);
1862 TYPE_BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (basetype
);
1863 TYPE_BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (basetype
);
1864 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
1865 CLASSTYPE_RTTI (t
) = CLASSTYPE_RTTI (basetype
);
1866 *vfuns_p
= CLASSTYPE_VSIZE (basetype
);
1869 /* Determine the primary class for T. */
1872 determine_primary_base (t
, vfuns_p
)
1876 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
1880 /* If there are no baseclasses, there is certainly no primary base. */
1881 if (n_baseclasses
== 0)
1884 type_binfo
= TYPE_BINFO (t
);
1886 for (i
= 0; i
< n_baseclasses
; i
++)
1888 tree base_binfo
= BINFO_BASETYPE (type_binfo
, i
);
1889 tree basetype
= BINFO_TYPE (base_binfo
);
1891 if (TYPE_CONTAINS_VPTR_P (basetype
))
1893 /* Even a virtual baseclass can contain our RTTI
1894 information. But, we prefer a non-virtual polymorphic
1896 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
1897 CLASSTYPE_RTTI (t
) = CLASSTYPE_RTTI (basetype
);
1899 /* A virtual baseclass can't be the primary base under the
1900 old ABI. And under the new ABI we still prefer a
1901 non-virtual base. */
1902 if (TREE_VIA_VIRTUAL (base_binfo
))
1905 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
1907 set_primary_base (t
, base_binfo
, vfuns_p
);
1908 CLASSTYPE_VFIELDS (t
) = copy_list (CLASSTYPE_VFIELDS (basetype
));
1914 /* Only add unique vfields, and flatten them out as we go. */
1915 for (vfields
= CLASSTYPE_VFIELDS (basetype
);
1917 vfields
= TREE_CHAIN (vfields
))
1918 if (VF_BINFO_VALUE (vfields
) == NULL_TREE
1919 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields
)))
1920 CLASSTYPE_VFIELDS (t
)
1921 = tree_cons (base_binfo
,
1922 VF_BASETYPE_VALUE (vfields
),
1923 CLASSTYPE_VFIELDS (t
));
1928 if (!TYPE_VFIELD (t
))
1929 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
1931 /* Find the indirect primary bases - those virtual bases which are primary
1932 bases of something else in this hierarchy. */
1933 for (vbases
= CLASSTYPE_VBASECLASSES (t
);
1935 vbases
= TREE_CHAIN (vbases
))
1937 tree vbase_binfo
= TREE_VALUE (vbases
);
1939 /* See if this virtual base is an indirect primary base. To be so,
1940 it must be a primary base within the hierarchy of one of our
1942 for (i
= 0; i
< n_baseclasses
; ++i
)
1944 tree basetype
= TYPE_BINFO_BASETYPE (t
, i
);
1947 for (v
= CLASSTYPE_VBASECLASSES (basetype
);
1951 tree base_vbase
= TREE_VALUE (v
);
1953 if (BINFO_PRIMARY_P (base_vbase
)
1954 && same_type_p (BINFO_TYPE (base_vbase
),
1955 BINFO_TYPE (vbase_binfo
)))
1957 BINFO_INDIRECT_PRIMARY_P (vbase_binfo
) = 1;
1962 /* If we've discovered that this virtual base is an indirect
1963 primary base, then we can move on to the next virtual
1965 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo
))
1970 /* The new ABI allows for the use of a "nearly-empty" virtual base
1971 class as the primary base class if no non-virtual polymorphic
1972 base can be found. */
1973 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
1975 /* If not NULL, this is the best primary base candidate we have
1977 tree candidate
= NULL_TREE
;
1980 /* Loop over the baseclasses. */
1981 for (base_binfo
= TYPE_BINFO (t
);
1983 base_binfo
= TREE_CHAIN (base_binfo
))
1985 tree basetype
= BINFO_TYPE (base_binfo
);
1987 if (TREE_VIA_VIRTUAL (base_binfo
)
1988 && CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1990 /* If this is not an indirect primary base, then it's
1991 definitely our primary base. */
1992 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo
))
1994 candidate
= base_binfo
;
1998 /* If this is an indirect primary base, it still could be
1999 our primary base -- unless we later find there's another
2000 nearly-empty virtual base that isn't an indirect
2003 candidate
= base_binfo
;
2007 /* If we've got a primary base, use it. */
2010 set_primary_base (t
, candidate
, vfuns_p
);
2011 CLASSTYPE_VFIELDS (t
)
2012 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate
)));
2016 /* Mark the primary base classes at this point. */
2017 mark_primary_bases (t
);
2020 /* Set memoizing fields and bits of T (and its variants) for later
2024 finish_struct_bits (t
)
2027 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
2029 /* Fix up variants (if any). */
2030 tree variants
= TYPE_NEXT_VARIANT (t
);
2033 /* These fields are in the _TYPE part of the node, not in
2034 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2035 TYPE_HAS_CONSTRUCTOR (variants
) = TYPE_HAS_CONSTRUCTOR (t
);
2036 TYPE_HAS_DESTRUCTOR (variants
) = TYPE_HAS_DESTRUCTOR (t
);
2037 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
2038 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants
)
2039 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
2041 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants
)
2042 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t
);
2043 TYPE_POLYMORPHIC_P (variants
) = TYPE_POLYMORPHIC_P (t
);
2044 TYPE_USES_VIRTUAL_BASECLASSES (variants
) = TYPE_USES_VIRTUAL_BASECLASSES (t
);
2045 /* Copy whatever these are holding today. */
2046 TYPE_MIN_VALUE (variants
) = TYPE_MIN_VALUE (t
);
2047 TYPE_MAX_VALUE (variants
) = TYPE_MAX_VALUE (t
);
2048 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
2049 TYPE_SIZE (variants
) = TYPE_SIZE (t
);
2050 TYPE_SIZE_UNIT (variants
) = TYPE_SIZE_UNIT (t
);
2051 variants
= TYPE_NEXT_VARIANT (variants
);
2054 if (n_baseclasses
&& TYPE_POLYMORPHIC_P (t
))
2055 /* For a class w/o baseclasses, `finish_struct' has set
2056 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2057 definition). Similarly for a class whose base classes do not
2058 have vtables. When neither of these is true, we might have
2059 removed abstract virtuals (by providing a definition), added
2060 some (by declaring new ones), or redeclared ones from a base
2061 class. We need to recalculate what's really an abstract virtual
2062 at this point (by looking in the vtables). */
2063 get_pure_virtuals (t
);
2067 /* Notice whether this class has type conversion functions defined. */
2068 tree binfo
= TYPE_BINFO (t
);
2069 tree binfos
= BINFO_BASETYPES (binfo
);
2072 for (i
= n_baseclasses
-1; i
>= 0; i
--)
2074 basetype
= BINFO_TYPE (TREE_VEC_ELT (binfos
, i
));
2076 TYPE_HAS_CONVERSION (t
) |= TYPE_HAS_CONVERSION (basetype
);
2080 /* If this type has a copy constructor, force its mode to be BLKmode, and
2081 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2082 be passed by invisible reference and prevent it from being returned in
2085 Also do this if the class has BLKmode but can still be returned in
2086 registers, since function_cannot_inline_p won't let us inline
2087 functions returning such a type. This affects the HP-PA. */
2088 if (! TYPE_HAS_TRIVIAL_INIT_REF (t
)
2089 || (TYPE_MODE (t
) == BLKmode
&& ! aggregate_value_p (t
)
2090 && CLASSTYPE_NON_AGGREGATE (t
)))
2093 DECL_MODE (TYPE_MAIN_DECL (t
)) = BLKmode
;
2094 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
2096 TYPE_MODE (variants
) = BLKmode
;
2097 TREE_ADDRESSABLE (variants
) = 1;
2102 /* Issue warnings about T having private constructors, but no friends,
2105 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2106 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2107 non-private static member functions. */
2110 maybe_warn_about_overly_private_class (t
)
2113 int has_member_fn
= 0;
2114 int has_nonprivate_method
= 0;
2117 if (!warn_ctor_dtor_privacy
2118 /* If the class has friends, those entities might create and
2119 access instances, so we should not warn. */
2120 || (CLASSTYPE_FRIEND_CLASSES (t
)
2121 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
2122 /* We will have warned when the template was declared; there's
2123 no need to warn on every instantiation. */
2124 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
2125 /* There's no reason to even consider warning about this
2129 /* We only issue one warning, if more than one applies, because
2130 otherwise, on code like:
2133 // Oops - forgot `public:'
2139 we warn several times about essentially the same problem. */
2141 /* Check to see if all (non-constructor, non-destructor) member
2142 functions are private. (Since there are no friends or
2143 non-private statics, we can't ever call any of the private member
2145 for (fn
= TYPE_METHODS (t
); fn
; fn
= TREE_CHAIN (fn
))
2146 /* We're not interested in compiler-generated methods; they don't
2147 provide any way to call private members. */
2148 if (!DECL_ARTIFICIAL (fn
))
2150 if (!TREE_PRIVATE (fn
))
2152 if (DECL_STATIC_FUNCTION_P (fn
))
2153 /* A non-private static member function is just like a
2154 friend; it can create and invoke private member
2155 functions, and be accessed without a class
2159 has_nonprivate_method
= 1;
2162 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
2166 if (!has_nonprivate_method
&& has_member_fn
)
2168 /* There are no non-private methods, and there's at least one
2169 private member function that isn't a constructor or
2170 destructor. (If all the private members are
2171 constructors/destructors we want to use the code below that
2172 issues error messages specifically referring to
2173 constructors/destructors.) */
2175 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
2176 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (t
); i
++)
2177 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos
, i
))
2178 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos
, i
)))
2180 has_nonprivate_method
= 1;
2183 if (!has_nonprivate_method
)
2185 cp_warning ("all member functions in class `%T' are private", t
);
2190 /* Even if some of the member functions are non-private, the class
2191 won't be useful for much if all the constructors or destructors
2192 are private: such an object can never be created or destroyed. */
2193 if (TYPE_HAS_DESTRUCTOR (t
))
2195 tree dtor
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 1);
2197 if (TREE_PRIVATE (dtor
))
2199 cp_warning ("`%#T' only defines a private destructor and has no friends",
2205 if (TYPE_HAS_CONSTRUCTOR (t
))
2207 int nonprivate_ctor
= 0;
2209 /* If a non-template class does not define a copy
2210 constructor, one is defined for it, enabling it to avoid
2211 this warning. For a template class, this does not
2212 happen, and so we would normally get a warning on:
2214 template <class T> class C { private: C(); };
2216 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2217 complete non-template or fully instantiated classes have this
2219 if (!TYPE_HAS_INIT_REF (t
))
2220 nonprivate_ctor
= 1;
2222 for (fn
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 0);
2226 tree ctor
= OVL_CURRENT (fn
);
2227 /* Ideally, we wouldn't count copy constructors (or, in
2228 fact, any constructor that takes an argument of the
2229 class type as a parameter) because such things cannot
2230 be used to construct an instance of the class unless
2231 you already have one. But, for now at least, we're
2233 if (! TREE_PRIVATE (ctor
))
2235 nonprivate_ctor
= 1;
2240 if (nonprivate_ctor
== 0)
2242 cp_warning ("`%#T' only defines private constructors and has no friends",
2249 /* Function to help qsort sort FIELD_DECLs by name order. */
2252 field_decl_cmp (x
, y
)
2255 if (DECL_NAME (*x
) == DECL_NAME (*y
))
2256 /* A nontype is "greater" than a type. */
2257 return DECL_DECLARES_TYPE_P (*y
) - DECL_DECLARES_TYPE_P (*x
);
2258 if (DECL_NAME (*x
) == NULL_TREE
)
2260 if (DECL_NAME (*y
) == NULL_TREE
)
2262 if (DECL_NAME (*x
) < DECL_NAME (*y
))
2267 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2270 method_name_cmp (m1
, m2
)
2271 const tree
*m1
, *m2
;
2273 if (*m1
== NULL_TREE
&& *m2
== NULL_TREE
)
2275 if (*m1
== NULL_TREE
)
2277 if (*m2
== NULL_TREE
)
2279 if (DECL_NAME (OVL_CURRENT (*m1
)) < DECL_NAME (OVL_CURRENT (*m2
)))
2284 /* Warn about duplicate methods in fn_fields. Also compact method
2285 lists so that lookup can be made faster.
2287 Data Structure: List of method lists. The outer list is a
2288 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2289 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2290 links the entire list of methods for TYPE_METHODS. Friends are
2291 chained in the same way as member functions (? TREE_CHAIN or
2292 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2293 list. That allows them to be quickly deleted, and requires no
2296 Sort methods that are not special (i.e., constructors, destructors,
2297 and type conversion operators) so that we can find them faster in
2301 finish_struct_methods (t
)
2308 if (!TYPE_METHODS (t
))
2310 /* Clear these for safety; perhaps some parsing error could set
2311 these incorrectly. */
2312 TYPE_HAS_CONSTRUCTOR (t
) = 0;
2313 TYPE_HAS_DESTRUCTOR (t
) = 0;
2314 CLASSTYPE_METHOD_VEC (t
) = NULL_TREE
;
2318 method_vec
= CLASSTYPE_METHOD_VEC (t
);
2319 my_friendly_assert (method_vec
!= NULL_TREE
, 19991215);
2320 len
= TREE_VEC_LENGTH (method_vec
);
2322 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2323 and the next few with type conversion operators (if any). */
2324 for (fn_fields
= TYPE_METHODS (t
); fn_fields
;
2325 fn_fields
= TREE_CHAIN (fn_fields
))
2326 /* Clear out this flag. */
2327 DECL_IN_AGGR_P (fn_fields
) = 0;
2329 if (TYPE_HAS_DESTRUCTOR (t
) && !CLASSTYPE_DESTRUCTORS (t
))
2330 /* We thought there was a destructor, but there wasn't. Some
2331 parse errors cause this anomalous situation. */
2332 TYPE_HAS_DESTRUCTOR (t
) = 0;
2334 /* Issue warnings about private constructors and such. If there are
2335 no methods, then some public defaults are generated. */
2336 maybe_warn_about_overly_private_class (t
);
2338 /* Now sort the methods. */
2339 while (len
> 2 && TREE_VEC_ELT (method_vec
, len
-1) == NULL_TREE
)
2341 TREE_VEC_LENGTH (method_vec
) = len
;
2343 /* The type conversion ops have to live at the front of the vec, so we
2345 for (slot
= 2; slot
< len
; ++slot
)
2347 tree fn
= TREE_VEC_ELT (method_vec
, slot
);
2349 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
2353 qsort (&TREE_VEC_ELT (method_vec
, slot
), len
-slot
, sizeof (tree
),
2354 (int (*)(const void *, const void *))method_name_cmp
);
2357 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2360 duplicate_tag_error (t
)
2363 cp_error ("redefinition of `%#T'", t
);
2364 cp_error_at ("previous definition of `%#T'", t
);
2366 /* Pretend we haven't defined this type. */
2368 /* All of the component_decl's were TREE_CHAINed together in the parser.
2369 finish_struct_methods walks these chains and assembles all methods with
2370 the same base name into DECL_CHAINs. Now we don't need the parser chains
2371 anymore, so we unravel them. */
2373 /* This used to be in finish_struct, but it turns out that the
2374 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2376 if (CLASSTYPE_METHOD_VEC (t
))
2378 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2379 int i
, len
= TREE_VEC_LENGTH (method_vec
);
2380 for (i
= 0; i
< len
; i
++)
2382 tree unchain
= TREE_VEC_ELT (method_vec
, i
);
2383 while (unchain
!= NULL_TREE
)
2385 TREE_CHAIN (OVL_CURRENT (unchain
)) = NULL_TREE
;
2386 unchain
= OVL_NEXT (unchain
);
2391 if (TYPE_LANG_SPECIFIC (t
))
2393 tree binfo
= TYPE_BINFO (t
);
2394 int interface_only
= CLASSTYPE_INTERFACE_ONLY (t
);
2395 int interface_unknown
= CLASSTYPE_INTERFACE_UNKNOWN (t
);
2396 tree template_info
= CLASSTYPE_TEMPLATE_INFO (t
);
2397 int use_template
= CLASSTYPE_USE_TEMPLATE (t
);
2399 memset ((char *) TYPE_LANG_SPECIFIC (t
), 0, sizeof (struct lang_type
));
2400 BINFO_BASETYPES(binfo
) = NULL_TREE
;
2402 TYPE_BINFO (t
) = binfo
;
2403 CLASSTYPE_INTERFACE_ONLY (t
) = interface_only
;
2404 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t
, interface_unknown
);
2405 TYPE_REDEFINED (t
) = 1;
2406 CLASSTYPE_TEMPLATE_INFO (t
) = template_info
;
2407 CLASSTYPE_USE_TEMPLATE (t
) = use_template
;
2409 TYPE_SIZE (t
) = NULL_TREE
;
2410 TYPE_MODE (t
) = VOIDmode
;
2411 TYPE_FIELDS (t
) = NULL_TREE
;
2412 TYPE_METHODS (t
) = NULL_TREE
;
2413 TYPE_VFIELD (t
) = NULL_TREE
;
2414 TYPE_CONTEXT (t
) = NULL_TREE
;
2415 TYPE_NONCOPIED_PARTS (t
) = NULL_TREE
;
2417 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2418 TYPE_LANG_FLAG_0 (t
) = 0;
2419 TYPE_LANG_FLAG_1 (t
) = 0;
2420 TYPE_LANG_FLAG_2 (t
) = 0;
2421 TYPE_LANG_FLAG_3 (t
) = 0;
2422 TYPE_LANG_FLAG_4 (t
) = 0;
2423 TYPE_LANG_FLAG_5 (t
) = 0;
2424 TYPE_LANG_FLAG_6 (t
) = 0;
2425 /* But not this one. */
2426 SET_IS_AGGR_TYPE (t
, 1);
2429 /* Make the BINFO's vtablehave N entries, including RTTI entries,
2430 vbase and vcall offsets, etc. Set its type and call the backend
2434 layout_vtable_decl (binfo
, n
)
2442 itype
= size_int (n
);
2443 atype
= build_cplus_array_type (vtable_entry_type
,
2444 build_index_type (itype
));
2445 layout_type (atype
);
2447 /* We may have to grow the vtable. */
2448 vtable
= get_vtbl_decl_for_binfo (binfo
);
2449 if (!same_type_p (TREE_TYPE (vtable
), atype
))
2451 TREE_TYPE (vtable
) = atype
;
2452 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = NULL_TREE
;
2453 layout_decl (vtable
, 0);
2455 /* At one time the vtable info was grabbed 2 words at a time. This
2456 fails on Sparc unless you have 8-byte alignment. */
2457 DECL_ALIGN (vtable
) = MAX (TYPE_ALIGN (double_type_node
),
2458 DECL_ALIGN (vtable
));
2462 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2463 have the same signature. */
2466 same_signature_p (fndecl
, base_fndecl
)
2467 tree fndecl
, base_fndecl
;
2469 /* One destructor overrides another if they are the same kind of
2471 if (DECL_DESTRUCTOR_P (base_fndecl
) && DECL_DESTRUCTOR_P (fndecl
)
2472 && special_function_p (base_fndecl
) == special_function_p (fndecl
))
2474 /* But a non-destructor never overrides a destructor, nor vice
2475 versa, nor do different kinds of destructors override
2476 one-another. For example, a complete object destructor does not
2477 override a deleting destructor. */
2478 if (DECL_DESTRUCTOR_P (base_fndecl
) || DECL_DESTRUCTOR_P (fndecl
))
2481 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
))
2483 tree types
, base_types
;
2484 types
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2485 base_types
= TYPE_ARG_TYPES (TREE_TYPE (base_fndecl
));
2486 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types
)))
2487 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types
))))
2488 && compparms (TREE_CHAIN (base_types
), TREE_CHAIN (types
)))
2494 typedef struct find_final_overrider_data_s
{
2495 /* The function for which we are trying to find a final overrider. */
2497 /* The base class in which the function was declared. */
2498 tree declaring_base
;
2499 /* The most derived class in the hierarchy. */
2500 tree most_derived_type
;
2501 /* The final overriding function. */
2503 /* The BINFO for the class in which the final overriding function
2505 tree overriding_base
;
2506 } find_final_overrider_data
;
2508 /* Called from find_final_overrider via dfs_walk. */
2511 dfs_find_final_overrider (binfo
, data
)
2515 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2517 if (same_type_p (BINFO_TYPE (binfo
),
2518 BINFO_TYPE (ffod
->declaring_base
))
2519 && tree_int_cst_equal (BINFO_OFFSET (binfo
),
2520 BINFO_OFFSET (ffod
->declaring_base
)))
2525 /* We haven't found an overrider yet. */
2527 /* We've found a path to the declaring base. Walk down the path
2528 looking for an overrider for FN. */
2529 for (path
= reverse_path (binfo
);
2531 path
= TREE_CHAIN (path
))
2533 for (method
= TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path
)));
2535 method
= TREE_CHAIN (method
))
2536 if (DECL_VIRTUAL_P (method
)
2537 && same_signature_p (method
, ffod
->fn
))
2544 /* If we found an overrider, record the overriding function, and
2545 the base from which it came. */
2550 /* Assume the path is non-virtual. See if there are any
2551 virtual bases from (but not including) the overrider up
2552 to and including the base where the function is
2554 for (base
= TREE_CHAIN (path
); base
; base
= TREE_CHAIN (base
))
2555 if (TREE_VIA_VIRTUAL (TREE_VALUE (base
)))
2557 base
= ffod
->declaring_base
;
2561 if (ffod
->overriding_fn
&& ffod
->overriding_fn
!= method
)
2563 /* We've found a different overrider along a different
2564 path. That can be OK if the new one overrides the
2567 struct S { virtual void f(); };
2568 struct T : public virtual S { virtual void f(); };
2569 struct U : public virtual S, public virtual T {};
2571 Here `T::f' is the final overrider for `S::f'. */
2572 if (strictly_overrides (method
, ffod
->overriding_fn
))
2574 ffod
->overriding_fn
= method
;
2575 ffod
->overriding_base
= TREE_VALUE (path
);
2577 else if (!strictly_overrides (ffod
->overriding_fn
, method
))
2579 cp_error ("no unique final overrider for `%D' in `%T'",
2580 ffod
->most_derived_type
,
2582 cp_error ("candidates are: `%#D'", ffod
->overriding_fn
);
2583 cp_error (" `%#D'", method
);
2584 return error_mark_node
;
2587 else if (ffod
->overriding_base
2588 && (!tree_int_cst_equal
2589 (BINFO_OFFSET (TREE_VALUE (path
)),
2590 BINFO_OFFSET (ffod
->overriding_base
))))
2592 /* We've found two instances of the same base that
2593 provide overriders. */
2594 cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'",
2596 BINFO_TYPE (ffod
->overriding_base
),
2597 ffod
->most_derived_type
);
2598 return error_mark_node
;
2602 ffod
->overriding_fn
= method
;
2603 ffod
->overriding_base
= TREE_VALUE (path
);
2611 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2612 FN and whose TREE_VALUE is the binfo for the base where the
2613 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2614 base object in which FN is declared. */
2617 find_final_overrider (t
, binfo
, fn
)
2622 find_final_overrider_data ffod
;
2624 /* Getting this right is a little tricky. This is legal:
2626 struct S { virtual void f (); };
2627 struct T { virtual void f (); };
2628 struct U : public S, public T { };
2630 even though calling `f' in `U' is ambiguous. But,
2632 struct R { virtual void f(); };
2633 struct S : virtual public R { virtual void f (); };
2634 struct T : virtual public R { virtual void f (); };
2635 struct U : public S, public T { };
2637 is not -- there's no way to decide whether to put `S::f' or
2638 `T::f' in the vtable for `R'.
2640 The solution is to look at all paths to BINFO. If we find
2641 different overriders along any two, then there is a problem. */
2643 ffod
.declaring_base
= binfo
;
2644 ffod
.most_derived_type
= t
;
2645 ffod
.overriding_fn
= NULL_TREE
;
2646 ffod
.overriding_base
= NULL_TREE
;
2648 if (dfs_walk (TYPE_BINFO (t
),
2649 dfs_find_final_overrider
,
2652 return error_mark_node
;
2654 return build_tree_list (ffod
.overriding_fn
, ffod
.overriding_base
);
2657 /* Update a entry in the vtable for BINFO, which is in the hierarchy
2658 dominated by T. FN has been overridden in BINFO; VIRTUALS points
2659 to the corresponding position in the BINFO_VIRTUALS list. */
2662 update_vtable_entry_for_fn (t
, binfo
, fn
, virtuals
)
2672 int generate_thunk_with_vtable_p
;
2674 /* Find the function which originally caused this vtable
2675 entry to be present. */
2682 primary_base
= get_primary_binfo (b
);
2686 for (f
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (primary_base
)));
2689 if (same_signature_p (BV_FN (f
), fn
))
2699 /* Find the final overrider. */
2700 overrider
= find_final_overrider (t
, b
, fn
);
2701 if (overrider
== error_mark_node
)
2704 /* Compute the constant adjustment to the `this' pointer. The
2705 `this' pointer, when this function is called, will point at the
2706 class whose vtable this is. */
2707 delta
= size_binop (PLUS_EXPR
,
2708 get_derived_offset (binfo
,
2709 DECL_VIRTUAL_CONTEXT (fn
)),
2710 BINFO_OFFSET (binfo
));
2712 /* Assume that we will produce a thunk that convert all the way to
2713 the final overrider, and not to an intermediate virtual base. */
2714 virtual_base
= NULL_TREE
;
2716 /* Assume that we will always generate thunks with the vtables that
2718 generate_thunk_with_vtable_p
= 1;
2720 /* Under the new ABI, we will convert to an intermediate virtual
2721 base first, and then use the vcall offset located there to finish
2725 /* If we find BINFO, then the final overrider is in a class
2726 derived from BINFO, so the thunks can be generated with
2727 the final overrider. */
2729 && same_type_p (BINFO_TYPE (b
), BINFO_TYPE (binfo
)))
2730 generate_thunk_with_vtable_p
= 0;
2732 /* If we find the final overrider, then we can stop
2734 if (same_type_p (BINFO_TYPE (b
),
2735 BINFO_TYPE (TREE_VALUE (overrider
))))
2738 /* If we find a virtual base, and we haven't yet found the
2739 overrider, then there is a virtual base between the
2740 declaring base and the final overrider. */
2741 if (!virtual_base
&& TREE_VIA_VIRTUAL (b
))
2743 generate_thunk_with_vtable_p
= 1;
2747 b
= BINFO_INHERITANCE_CHAIN (b
);
2751 /* The `this' pointer needs to be adjusted to the nearest virtual
2753 delta
= size_diffop (BINFO_OFFSET (virtual_base
), delta
);
2755 /* The `this' pointer needs to be adjusted from pointing to
2756 BINFO to pointing at the base where the final overrider
2758 delta
= size_diffop (BINFO_OFFSET (TREE_VALUE (overrider
)), delta
);
2760 modify_vtable_entry (t
,
2762 TREE_PURPOSE (overrider
),
2767 BV_USE_VCALL_INDEX_P (*virtuals
) = 1;
2768 if (generate_thunk_with_vtable_p
)
2769 BV_GENERATE_THUNK_WITH_VTABLE_P (*virtuals
) = 1;
2772 /* Called from modify_all_vtables via dfs_walk. */
2775 dfs_modify_vtables (binfo
, data
)
2779 if (/* There's no need to modify the vtable for a non-virtual
2780 primary base; we're not going to use that vtable anyhow
2781 (virtual primary bases can become non-primary in a
2782 class derivation of this one.) */
2783 (!BINFO_PRIMARY_P (binfo
) || TREE_VIA_VIRTUAL (binfo
))
2784 /* Similarly, a base without a vtable needs no modification. */
2785 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2793 /* If we're supporting RTTI then we always need a new vtable to
2794 point to the RTTI information. Under the new ABI we may need
2795 a new vtable to contain vcall and vbase offsets. */
2796 make_new_vtable (t
, binfo
);
2798 /* Now, go through each of the virtual functions in the virtual
2799 function table for BINFO. Find the final overrider, and
2800 update the BINFO_VIRTUALS list appropriately. */
2801 for (virtuals
= BINFO_VIRTUALS (binfo
),
2802 old_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
2804 virtuals
= TREE_CHAIN (virtuals
),
2805 old_virtuals
= TREE_CHAIN (old_virtuals
))
2806 update_vtable_entry_for_fn (t
,
2808 BV_FN (old_virtuals
),
2812 SET_BINFO_MARKED (binfo
);
2817 /* Update all of the primary and secondary vtables for T. Create new
2818 vtables as required, and initialize their RTTI information. Each
2819 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2820 function from a base class; find and modify the appropriate entries
2821 to point to the overriding functions. Returns a list, in
2822 declaration order, of the functions that are overridden in this
2823 class, but do not appear in the primary base class vtable, and
2824 which should therefore be appended to the end of the vtable for T. */
2827 modify_all_vtables (t
, vfuns_p
, overridden_virtuals
)
2830 tree overridden_virtuals
;
2834 binfo
= TYPE_BINFO (t
);
2836 /* Update all of the vtables. */
2839 dfs_unmarked_real_bases_queue_p
,
2841 dfs_walk (binfo
, dfs_unmark
, dfs_marked_real_bases_queue_p
, t
);
2843 /* If we should include overriding functions for secondary vtables
2844 in our primary vtable, add them now. */
2845 if (all_overridden_vfuns_in_vtables_p ())
2847 tree
*fnsp
= &overridden_virtuals
;
2851 tree fn
= TREE_VALUE (*fnsp
);
2853 if (!BINFO_VIRTUALS (binfo
)
2854 || !value_member (fn
, BINFO_VIRTUALS (binfo
)))
2856 /* Set the vtable index. */
2857 set_vindex (fn
, vfuns_p
);
2858 /* We don't need to convert to a base class when calling
2860 DECL_VIRTUAL_CONTEXT (fn
) = t
;
2862 /* We don't need to adjust the `this' pointer when
2863 calling this function. */
2864 BV_DELTA (*fnsp
) = integer_zero_node
;
2865 BV_VCALL_INDEX (*fnsp
) = NULL_TREE
;
2867 /* This is an overridden function not already in our
2869 fnsp
= &TREE_CHAIN (*fnsp
);
2872 /* We've already got an entry for this function. Skip
2874 *fnsp
= TREE_CHAIN (*fnsp
);
2878 overridden_virtuals
= NULL_TREE
;
2880 return overridden_virtuals
;
2883 /* Here, we already know that they match in every respect.
2884 All we have to check is where they had their declarations. */
2887 strictly_overrides (fndecl1
, fndecl2
)
2888 tree fndecl1
, fndecl2
;
2890 int distance
= get_base_distance (DECL_CONTEXT (fndecl2
),
2891 DECL_CONTEXT (fndecl1
),
2893 if (distance
== -2 || distance
> 0)
2898 /* Get the base virtual function declarations in T that are either
2899 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2900 the overrider/hider. */
2903 get_basefndecls (fndecl
, t
)
2906 tree methods
= TYPE_METHODS (t
);
2907 tree base_fndecls
= NULL_TREE
;
2908 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
2909 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
2913 if (TREE_CODE (methods
) == FUNCTION_DECL
2914 && DECL_VINDEX (methods
) != NULL_TREE
2915 && DECL_NAME (fndecl
) == DECL_NAME (methods
))
2916 base_fndecls
= tree_cons (fndecl
, methods
, base_fndecls
);
2918 methods
= TREE_CHAIN (methods
);
2922 return base_fndecls
;
2924 for (i
= 0; i
< n_baseclasses
; i
++)
2926 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
2927 tree basetype
= BINFO_TYPE (base_binfo
);
2929 base_fndecls
= chainon (get_basefndecls (fndecl
, basetype
),
2933 return base_fndecls
;
2936 /* Mark the functions that have been hidden with their overriders.
2937 Since we start out with all functions already marked with a hider,
2938 no need to mark functions that are just hidden.
2940 Subroutine of warn_hidden. */
2943 mark_overriders (fndecl
, base_fndecls
)
2944 tree fndecl
, base_fndecls
;
2946 for (; base_fndecls
; base_fndecls
= TREE_CHAIN (base_fndecls
))
2947 if (same_signature_p (fndecl
, TREE_VALUE (base_fndecls
)))
2948 TREE_PURPOSE (base_fndecls
) = fndecl
;
2951 /* If this declaration supersedes the declaration of
2952 a method declared virtual in the base class, then
2953 mark this field as being virtual as well. */
2956 check_for_override (decl
, ctype
)
2959 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2960 /* In [temp.mem] we have:
2962 A specialization of a member function template does not
2963 override a virtual function from a base class. */
2965 if ((DECL_DESTRUCTOR_P (decl
)
2966 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)))
2967 && look_for_overrides (ctype
, decl
)
2968 && !DECL_STATIC_FUNCTION_P (decl
))
2970 /* Set DECL_VINDEX to a value that is neither an
2971 INTEGER_CST nor the error_mark_node so that
2972 add_virtual_function will realize this is an
2973 overriding function. */
2974 DECL_VINDEX (decl
) = decl
;
2976 if (DECL_VIRTUAL_P (decl
))
2978 if (DECL_VINDEX (decl
) == NULL_TREE
)
2979 DECL_VINDEX (decl
) = error_mark_node
;
2980 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = 1;
2984 /* Warn about hidden virtual functions that are not overridden in t.
2985 We know that constructors and destructors don't apply. */
2991 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2992 int n_methods
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
2995 /* We go through each separately named virtual function. */
2996 for (i
= 2; i
< n_methods
&& TREE_VEC_ELT (method_vec
, i
); ++i
)
2998 tree fns
= TREE_VEC_ELT (method_vec
, i
);
2999 tree fndecl
= NULL_TREE
;
3001 tree base_fndecls
= NULL_TREE
;
3002 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
3003 int i
, n_baseclasses
= binfos
? TREE_VEC_LENGTH (binfos
) : 0;
3005 /* First see if we have any virtual functions in this batch. */
3006 for (; fns
; fns
= OVL_NEXT (fns
))
3008 fndecl
= OVL_CURRENT (fns
);
3009 if (DECL_VINDEX (fndecl
))
3013 if (fns
== NULL_TREE
)
3016 /* First we get a list of all possible functions that might be
3017 hidden from each base class. */
3018 for (i
= 0; i
< n_baseclasses
; i
++)
3020 tree base_binfo
= TREE_VEC_ELT (binfos
, i
);
3021 tree basetype
= BINFO_TYPE (base_binfo
);
3023 base_fndecls
= chainon (get_basefndecls (fndecl
, basetype
),
3027 fns
= OVL_NEXT (fns
);
3029 /* ...then mark up all the base functions with overriders, preferring
3030 overriders to hiders. */
3032 for (; fns
; fns
= OVL_NEXT (fns
))
3034 fndecl
= OVL_CURRENT (fns
);
3035 if (DECL_VINDEX (fndecl
))
3036 mark_overriders (fndecl
, base_fndecls
);
3039 /* Now give a warning for all base functions without overriders,
3040 as they are hidden. */
3041 for (; base_fndecls
; base_fndecls
= TREE_CHAIN (base_fndecls
))
3042 if (!same_signature_p (TREE_PURPOSE (base_fndecls
),
3043 TREE_VALUE (base_fndecls
)))
3045 /* Here we know it is a hider, and no overrider exists. */
3046 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls
));
3047 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls
));
3052 /* Check for things that are invalid. There are probably plenty of other
3053 things we should check for also. */
3056 finish_struct_anon (t
)
3061 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
3063 if (TREE_STATIC (field
))
3065 if (TREE_CODE (field
) != FIELD_DECL
)
3068 if (DECL_NAME (field
) == NULL_TREE
3069 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
3071 tree elt
= TYPE_FIELDS (TREE_TYPE (field
));
3072 for (; elt
; elt
= TREE_CHAIN (elt
))
3074 if (DECL_ARTIFICIAL (elt
))
3077 if (DECL_NAME (elt
) == constructor_name (t
))
3078 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
3081 if (TREE_CODE (elt
) != FIELD_DECL
)
3083 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3088 if (TREE_PRIVATE (elt
))
3089 cp_pedwarn_at ("private member `%#D' in anonymous union",
3091 else if (TREE_PROTECTED (elt
))
3092 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3095 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
3096 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
3102 /* Create default constructors, assignment operators, and so forth for
3103 the type indicated by T, if they are needed.
3104 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3105 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
3106 class cannot have a default constructor, copy constructor taking a
3107 const reference argument, or an assignment operator taking a const
3108 reference, respectively. If a virtual destructor is created, its
3109 DECL is returned; otherwise the return value is NULL_TREE. */
3112 add_implicitly_declared_members (t
, cant_have_default_ctor
,
3113 cant_have_const_cctor
,
3114 cant_have_const_assignment
)
3116 int cant_have_default_ctor
;
3117 int cant_have_const_cctor
;
3118 int cant_have_const_assignment
;
3121 tree implicit_fns
= NULL_TREE
;
3122 tree virtual_dtor
= NULL_TREE
;
3126 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) && !TYPE_HAS_DESTRUCTOR (t
))
3128 default_fn
= implicitly_declare_fn (sfk_destructor
, t
, /*const_p=*/0);
3129 check_for_override (default_fn
, t
);
3131 /* If we couldn't make it work, then pretend we didn't need it. */
3132 if (default_fn
== void_type_node
)
3133 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = 0;
3136 TREE_CHAIN (default_fn
) = implicit_fns
;
3137 implicit_fns
= default_fn
;
3139 if (DECL_VINDEX (default_fn
))
3140 virtual_dtor
= default_fn
;
3144 /* Any non-implicit destructor is non-trivial. */
3145 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) |= TYPE_HAS_DESTRUCTOR (t
);
3147 /* Default constructor. */
3148 if (! TYPE_HAS_CONSTRUCTOR (t
) && ! cant_have_default_ctor
)
3150 default_fn
= implicitly_declare_fn (sfk_constructor
, t
, /*const_p=*/0);
3151 TREE_CHAIN (default_fn
) = implicit_fns
;
3152 implicit_fns
= default_fn
;
3155 /* Copy constructor. */
3156 if (! TYPE_HAS_INIT_REF (t
) && ! TYPE_FOR_JAVA (t
))
3158 /* ARM 12.18: You get either X(X&) or X(const X&), but
3161 = implicitly_declare_fn (sfk_copy_constructor
, t
,
3162 /*const_p=*/!cant_have_const_cctor
);
3163 TREE_CHAIN (default_fn
) = implicit_fns
;
3164 implicit_fns
= default_fn
;
3167 /* Assignment operator. */
3168 if (! TYPE_HAS_ASSIGN_REF (t
) && ! TYPE_FOR_JAVA (t
))
3171 = implicitly_declare_fn (sfk_assignment_operator
, t
,
3172 /*const_p=*/!cant_have_const_assignment
);
3173 TREE_CHAIN (default_fn
) = implicit_fns
;
3174 implicit_fns
= default_fn
;
3177 /* Now, hook all of the new functions on to TYPE_METHODS,
3178 and add them to the CLASSTYPE_METHOD_VEC. */
3179 for (f
= &implicit_fns
; *f
; f
= &TREE_CHAIN (*f
))
3180 add_method (t
, *f
, /*error_p=*/0);
3181 *f
= TYPE_METHODS (t
);
3182 TYPE_METHODS (t
) = implicit_fns
;
3184 return virtual_dtor
;
3187 /* Subroutine of finish_struct_1. Recursively count the number of fields
3188 in TYPE, including anonymous union members. */
3191 count_fields (fields
)
3196 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3198 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3199 n_fields
+= count_fields (TYPE_FIELDS (TREE_TYPE (x
)));
3206 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3207 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3210 add_fields_to_vec (fields
, field_vec
, idx
)
3211 tree fields
, field_vec
;
3215 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3217 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3218 idx
= add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x
)), field_vec
, idx
);
3220 TREE_VEC_ELT (field_vec
, idx
++) = x
;
3225 /* FIELD is a bit-field. We are finishing the processing for its
3226 enclosing type. Issue any appropriate messages and set appropriate
3230 check_bitfield_decl (field
)
3233 tree type
= TREE_TYPE (field
);
3236 /* Detect invalid bit-field type. */
3237 if (DECL_INITIAL (field
)
3238 && ! INTEGRAL_TYPE_P (TREE_TYPE (field
)))
3240 cp_error_at ("bit-field `%#D' with non-integral type", field
);
3241 w
= error_mark_node
;
3244 /* Detect and ignore out of range field width. */
3245 if (DECL_INITIAL (field
))
3247 w
= DECL_INITIAL (field
);
3249 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3252 /* detect invalid field size. */
3253 if (TREE_CODE (w
) == CONST_DECL
)
3254 w
= DECL_INITIAL (w
);
3256 w
= decl_constant_value (w
);
3258 if (TREE_CODE (w
) != INTEGER_CST
)
3260 cp_error_at ("bit-field `%D' width not an integer constant",
3262 w
= error_mark_node
;
3264 else if (tree_int_cst_sgn (w
) < 0)
3266 cp_error_at ("negative width in bit-field `%D'", field
);
3267 w
= error_mark_node
;
3269 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3271 cp_error_at ("zero width for bit-field `%D'", field
);
3272 w
= error_mark_node
;
3274 else if (compare_tree_int (w
, TYPE_PRECISION (type
)) > 0
3275 && TREE_CODE (type
) != ENUMERAL_TYPE
3276 && TREE_CODE (type
) != BOOLEAN_TYPE
)
3277 cp_warning_at ("width of `%D' exceeds its type", field
);
3278 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3279 && (0 > compare_tree_int (w
,
3280 min_precision (TYPE_MIN_VALUE (type
),
3281 TREE_UNSIGNED (type
)))
3282 || 0 > compare_tree_int (w
,
3284 (TYPE_MAX_VALUE (type
),
3285 TREE_UNSIGNED (type
)))))
3286 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3290 /* Remove the bit-field width indicator so that the rest of the
3291 compiler does not treat that value as an initializer. */
3292 DECL_INITIAL (field
) = NULL_TREE
;
3294 if (w
!= error_mark_node
)
3296 DECL_SIZE (field
) = convert (bitsizetype
, w
);
3297 DECL_BIT_FIELD (field
) = 1;
3299 if (integer_zerop (w
))
3301 #ifdef EMPTY_FIELD_BOUNDARY
3302 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
),
3303 EMPTY_FIELD_BOUNDARY
);
3305 #ifdef PCC_BITFIELD_TYPE_MATTERS
3306 if (PCC_BITFIELD_TYPE_MATTERS
)
3308 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
),
3310 DECL_USER_ALIGN (field
) |= TYPE_USER_ALIGN (type
);
3317 /* Non-bit-fields are aligned for their type. */
3318 DECL_BIT_FIELD (field
) = 0;
3319 CLEAR_DECL_C_BIT_FIELD (field
);
3320 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
), TYPE_ALIGN (type
));
3321 DECL_USER_ALIGN (field
) |= TYPE_USER_ALIGN (type
);
3325 /* FIELD is a non bit-field. We are finishing the processing for its
3326 enclosing type T. Issue any appropriate messages and set appropriate
3330 check_field_decl (field
, t
, cant_have_const_ctor
,
3331 cant_have_default_ctor
, no_const_asn_ref
,
3332 any_default_members
)
3335 int *cant_have_const_ctor
;
3336 int *cant_have_default_ctor
;
3337 int *no_const_asn_ref
;
3338 int *any_default_members
;
3340 tree type
= strip_array_types (TREE_TYPE (field
));
3342 /* An anonymous union cannot contain any fields which would change
3343 the settings of CANT_HAVE_CONST_CTOR and friends. */
3344 if (ANON_UNION_TYPE_P (type
))
3346 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3347 structs. So, we recurse through their fields here. */
3348 else if (ANON_AGGR_TYPE_P (type
))
3352 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3353 if (TREE_CODE (fields
) == FIELD_DECL
&& !DECL_C_BIT_FIELD (field
))
3354 check_field_decl (fields
, t
, cant_have_const_ctor
,
3355 cant_have_default_ctor
, no_const_asn_ref
,
3356 any_default_members
);
3358 /* Check members with class type for constructors, destructors,
3360 else if (CLASS_TYPE_P (type
))
3362 /* Never let anything with uninheritable virtuals
3363 make it through without complaint. */
3364 abstract_virtuals_error (field
, type
);
3366 if (TREE_CODE (t
) == UNION_TYPE
)
3368 if (TYPE_NEEDS_CONSTRUCTING (type
))
3369 cp_error_at ("member `%#D' with constructor not allowed in union",
3371 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3372 cp_error_at ("member `%#D' with destructor not allowed in union",
3374 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type
))
3375 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3380 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3381 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3382 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3383 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type
);
3384 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (type
);
3387 if (!TYPE_HAS_CONST_INIT_REF (type
))
3388 *cant_have_const_ctor
= 1;
3390 if (!TYPE_HAS_CONST_ASSIGN_REF (type
))
3391 *no_const_asn_ref
= 1;
3393 if (TYPE_HAS_CONSTRUCTOR (type
)
3394 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
3395 *cant_have_default_ctor
= 1;
3397 if (DECL_INITIAL (field
) != NULL_TREE
)
3399 /* `build_class_init_list' does not recognize
3401 if (TREE_CODE (t
) == UNION_TYPE
&& any_default_members
!= 0)
3402 cp_error_at ("multiple fields in union `%T' initialized");
3403 *any_default_members
= 1;
3406 /* Non-bit-fields are aligned for their type, except packed fields
3407 which require only BITS_PER_UNIT alignment. */
3408 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
),
3409 (DECL_PACKED (field
)
3411 : TYPE_ALIGN (TREE_TYPE (field
))));
3412 if (! DECL_PACKED (field
))
3413 DECL_USER_ALIGN (field
) |= TYPE_USER_ALIGN (TREE_TYPE (field
));
3416 /* Check the data members (both static and non-static), class-scoped
3417 typedefs, etc., appearing in the declaration of T. Issue
3418 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3419 declaration order) of access declarations; each TREE_VALUE in this
3420 list is a USING_DECL.
3422 In addition, set the following flags:
3425 The class is empty, i.e., contains no non-static data members.
3427 CANT_HAVE_DEFAULT_CTOR_P
3428 This class cannot have an implicitly generated default
3431 CANT_HAVE_CONST_CTOR_P
3432 This class cannot have an implicitly generated copy constructor
3433 taking a const reference.
3435 CANT_HAVE_CONST_ASN_REF
3436 This class cannot have an implicitly generated assignment
3437 operator taking a const reference.
3439 All of these flags should be initialized before calling this
3442 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3443 fields can be added by adding to this chain. */
3446 check_field_decls (t
, access_decls
, empty_p
,
3447 cant_have_default_ctor_p
, cant_have_const_ctor_p
,
3452 int *cant_have_default_ctor_p
;
3453 int *cant_have_const_ctor_p
;
3454 int *no_const_asn_ref_p
;
3459 int any_default_members
;
3461 /* First, delete any duplicate fields. */
3462 delete_duplicate_fields (TYPE_FIELDS (t
));
3464 /* Assume there are no access declarations. */
3465 *access_decls
= NULL_TREE
;
3466 /* Assume this class has no pointer members. */
3468 /* Assume none of the members of this class have default
3470 any_default_members
= 0;
3472 for (field
= &TYPE_FIELDS (t
); *field
; field
= next
)
3475 tree type
= TREE_TYPE (x
);
3477 GNU_xref_member (current_class_name
, x
);
3479 next
= &TREE_CHAIN (x
);
3481 if (TREE_CODE (x
) == FIELD_DECL
)
3483 DECL_PACKED (x
) |= TYPE_PACKED (t
);
3485 if (DECL_C_BIT_FIELD (x
) && integer_zerop (DECL_INITIAL (x
)))
3486 /* We don't treat zero-width bitfields as making a class
3491 /* The class is non-empty. */
3493 /* The class is not even nearly empty. */
3494 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3498 if (TREE_CODE (x
) == USING_DECL
)
3500 /* Prune the access declaration from the list of fields. */
3501 *field
= TREE_CHAIN (x
);
3503 /* Save the access declarations for our caller. */
3504 *access_decls
= tree_cons (NULL_TREE
, x
, *access_decls
);
3506 /* Since we've reset *FIELD there's no reason to skip to the
3512 if (TREE_CODE (x
) == TYPE_DECL
3513 || TREE_CODE (x
) == TEMPLATE_DECL
)
3516 /* If we've gotten this far, it's a data member, possibly static,
3517 or an enumerator. */
3519 DECL_CONTEXT (x
) = t
;
3521 /* ``A local class cannot have static data members.'' ARM 9.4 */
3522 if (current_function_decl
&& TREE_STATIC (x
))
3523 cp_error_at ("field `%D' in local class cannot be static", x
);
3525 /* Perform error checking that did not get done in
3527 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3529 cp_error_at ("field `%D' invalidly declared function type",
3531 type
= build_pointer_type (type
);
3532 TREE_TYPE (x
) = type
;
3534 else if (TREE_CODE (type
) == METHOD_TYPE
)
3536 cp_error_at ("field `%D' invalidly declared method type", x
);
3537 type
= build_pointer_type (type
);
3538 TREE_TYPE (x
) = type
;
3540 else if (TREE_CODE (type
) == OFFSET_TYPE
)
3542 cp_error_at ("field `%D' invalidly declared offset type", x
);
3543 type
= build_pointer_type (type
);
3544 TREE_TYPE (x
) = type
;
3547 if (type
== error_mark_node
)
3550 /* When this goes into scope, it will be a non-local reference. */
3551 DECL_NONLOCAL (x
) = 1;
3553 if (TREE_CODE (x
) == CONST_DECL
)
3556 if (TREE_CODE (x
) == VAR_DECL
)
3558 if (TREE_CODE (t
) == UNION_TYPE
)
3559 /* Unions cannot have static members. */
3560 cp_error_at ("field `%D' declared static in union", x
);
3565 /* Now it can only be a FIELD_DECL. */
3567 if (TREE_PRIVATE (x
) || TREE_PROTECTED (x
))
3568 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3570 /* If this is of reference type, check if it needs an init.
3571 Also do a little ANSI jig if necessary. */
3572 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3574 CLASSTYPE_NON_POD_P (t
) = 1;
3575 if (DECL_INITIAL (x
) == NULL_TREE
)
3576 CLASSTYPE_REF_FIELDS_NEED_INIT (t
) = 1;
3578 /* ARM $12.6.2: [A member initializer list] (or, for an
3579 aggregate, initialization by a brace-enclosed list) is the
3580 only way to initialize nonstatic const and reference
3582 *cant_have_default_ctor_p
= 1;
3583 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
3585 if (! TYPE_HAS_CONSTRUCTOR (t
) && extra_warnings
)
3586 cp_warning_at ("non-static reference `%#D' in class without a constructor", x
);
3589 type
= strip_array_types (type
);
3591 if (TREE_CODE (type
) == POINTER_TYPE
)
3594 if (DECL_MUTABLE_P (x
) || TYPE_HAS_MUTABLE_P (type
))
3595 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3597 if (! pod_type_p (type
))
3598 /* DR 148 now allows pointers to members (which are POD themselves),
3599 to be allowed in POD structs. */
3600 CLASSTYPE_NON_POD_P (t
) = 1;
3602 /* If any field is const, the structure type is pseudo-const. */
3603 if (CP_TYPE_CONST_P (type
))
3605 C_TYPE_FIELDS_READONLY (t
) = 1;
3606 if (DECL_INITIAL (x
) == NULL_TREE
)
3607 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
) = 1;
3609 /* ARM $12.6.2: [A member initializer list] (or, for an
3610 aggregate, initialization by a brace-enclosed list) is the
3611 only way to initialize nonstatic const and reference
3613 *cant_have_default_ctor_p
= 1;
3614 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
3616 if (! TYPE_HAS_CONSTRUCTOR (t
) && extra_warnings
)
3617 cp_warning_at ("non-static const member `%#D' in class without a constructor", x
);
3619 /* A field that is pseudo-const makes the structure likewise. */
3620 else if (IS_AGGR_TYPE (type
))
3622 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
3623 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
3624 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
);
3627 /* Core issue 80: A nonstatic data member is required to have a
3628 different name from the class iff the class has a
3629 user-defined constructor. */
3630 if (DECL_NAME (x
) == constructor_name (t
)
3631 && TYPE_HAS_CONSTRUCTOR (t
))
3632 cp_pedwarn_at ("field `%#D' with same name as class", x
);
3634 /* We set DECL_C_BIT_FIELD in grokbitfield.
3635 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3636 if (DECL_C_BIT_FIELD (x
))
3637 check_bitfield_decl (x
);
3639 check_field_decl (x
, t
,
3640 cant_have_const_ctor_p
,
3641 cant_have_default_ctor_p
,
3643 &any_default_members
);
3646 /* Effective C++ rule 11. */
3647 if (has_pointers
&& warn_ecpp
&& TYPE_HAS_CONSTRUCTOR (t
)
3648 && ! (TYPE_HAS_INIT_REF (t
) && TYPE_HAS_ASSIGN_REF (t
)))
3650 cp_warning ("`%#T' has pointer data members", t
);
3652 if (! TYPE_HAS_INIT_REF (t
))
3654 cp_warning (" but does not override `%T(const %T&)'", t
, t
);
3655 if (! TYPE_HAS_ASSIGN_REF (t
))
3656 cp_warning (" or `operator=(const %T&)'", t
);
3658 else if (! TYPE_HAS_ASSIGN_REF (t
))
3659 cp_warning (" but does not override `operator=(const %T&)'", t
);
3663 /* Check anonymous struct/anonymous union fields. */
3664 finish_struct_anon (t
);
3666 /* We've built up the list of access declarations in reverse order.
3668 *access_decls
= nreverse (*access_decls
);
3671 /* Return a FIELD_DECL for a pointer-to-virtual-table or
3672 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
3673 field are as indicated. The CLASS_TYPE in which this field occurs
3674 is also indicated. FCONTEXT is the type that is needed for the debug
3675 info output routines. *EMPTY_P is set to a non-zero value by this
3676 function to indicate that a class containing this field is
3680 build_vtbl_or_vbase_field (name
, assembler_name
, type
, class_type
, fcontext
,
3683 tree assembler_name
;
3691 /* This class is non-empty. */
3694 /* Build the FIELD_DECL. */
3695 field
= build_decl (FIELD_DECL
, name
, type
);
3696 DECL_ASSEMBLER_NAME (field
) = assembler_name
;
3697 DECL_VIRTUAL_P (field
) = 1;
3698 DECL_ARTIFICIAL (field
) = 1;
3699 DECL_FIELD_CONTEXT (field
) = class_type
;
3700 DECL_FCONTEXT (field
) = fcontext
;
3701 DECL_ALIGN (field
) = TYPE_ALIGN (type
);
3702 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (type
);
3708 /* If TYPE is an empty class type, records its OFFSET in the table of
3712 record_subobject_offset (type
, offset
, offsets
)
3719 if (!is_empty_class (type
))
3722 /* Record the location of this empty object in OFFSETS. */
3723 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3725 n
= splay_tree_insert (offsets
,
3726 (splay_tree_key
) offset
,
3727 (splay_tree_value
) NULL_TREE
);
3728 n
->value
= ((splay_tree_value
)
3729 tree_cons (NULL_TREE
,
3736 /* Returns non-zero if TYPE is an empty class type and there is
3737 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3740 check_subobject_offset (type
, offset
, offsets
)
3748 if (!is_empty_class (type
))
3751 /* Record the location of this empty object in OFFSETS. */
3752 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3756 for (t
= (tree
) n
->value
; t
; t
= TREE_CHAIN (t
))
3757 if (same_type_p (TREE_VALUE (t
), type
))
3763 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3764 F for every subobject, passing it the type, offset, and table of
3765 OFFSETS. If VBASES_P is non-zero, then even virtual non-primary
3766 bases should be traversed; otherwise, they are ignored. If F
3767 returns a non-zero value, the traversal ceases, and that value is
3768 returned. Otherwise, returns zero. */
3771 walk_subobject_offsets (type
, f
, offset
, offsets
, vbases_p
)
3773 subobject_offset_fn f
;
3780 if (CLASS_TYPE_P (type
))
3785 /* Record the location of TYPE. */
3786 r
= (*f
) (type
, offset
, offsets
);
3790 /* Iterate through the direct base classes of TYPE. */
3791 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); ++i
)
3793 tree binfo
= BINFO_BASETYPE (TYPE_BINFO (type
), i
);
3796 && TREE_VIA_VIRTUAL (binfo
)
3797 && !BINFO_PRIMARY_P (binfo
))
3800 r
= walk_subobject_offsets (BINFO_TYPE (binfo
),
3802 size_binop (PLUS_EXPR
,
3804 BINFO_OFFSET (binfo
)),
3811 /* Iterate through the fields of TYPE. */
3812 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
3813 if (TREE_CODE (field
) == FIELD_DECL
)
3815 r
= walk_subobject_offsets (TREE_TYPE (field
),
3817 size_binop (PLUS_EXPR
,
3819 DECL_FIELD_OFFSET (field
)),
3826 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3828 tree domain
= TYPE_DOMAIN (type
);
3831 /* Step through each of the elements in the array. */
3832 for (index
= size_zero_node
;
3833 INT_CST_LT (index
, TYPE_MAX_VALUE (domain
));
3834 index
= size_binop (PLUS_EXPR
, index
, size_one_node
))
3836 r
= walk_subobject_offsets (TREE_TYPE (type
),
3843 offset
= size_binop (PLUS_EXPR
, offset
,
3844 TYPE_SIZE_UNIT (TREE_TYPE (type
)));
3851 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3852 OFFSETS. If VBASES_P is non-zero, virtual bases of TYPE are
3856 record_subobject_offsets (type
, offset
, offsets
, vbases_p
)
3862 walk_subobject_offsets (type
, record_subobject_offset
, offset
,
3866 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3867 OFFSET) conflict with entries in OFFSETS. If VBASES_P is non-zero,
3868 virtual bases of TYPE are examined. */
3871 layout_conflict_p (type
, offset
, offsets
, vbases_p
)
3877 return walk_subobject_offsets (type
, check_subobject_offset
, offset
,
3881 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3882 non-static data member of the type indicated by RLI. BINFO is the
3883 binfo corresponding to the base subobject, OFFSETS maps offsets to
3884 types already located at those offsets. This function determines
3885 the position of the DECL. */
3888 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
)
3889 record_layout_info rli
;
3894 tree offset
= NULL_TREE
;
3895 tree type
= TREE_TYPE (decl
);
3896 /* If we are laying out a base class, rather than a field, then
3897 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
3898 int field_p
= !DECL_ARTIFICIAL (decl
);
3900 /* Try to place the field. It may take more than one try if we have
3901 a hard time placing the field without putting two objects of the
3902 same type at the same address. */
3905 struct record_layout_info_s old_rli
= *rli
;
3907 /* Place this field. */
3908 place_field (rli
, decl
);
3909 offset
= byte_position (decl
);
3911 /* We have to check to see whether or not there is already
3912 something of the same type at the offset we're about to use.
3916 struct T : public S { int i; };
3917 struct U : public S, public T {};
3919 Here, we put S at offset zero in U. Then, we can't put T at
3920 offset zero -- its S component would be at the same address
3921 as the S we already allocated. So, we have to skip ahead.
3922 Since all data members, including those whose type is an
3923 empty class, have non-zero size, any overlap can happen only
3924 with a direct or indirect base-class -- it can't happen with
3926 if (layout_conflict_p (TREE_TYPE (decl
),
3931 /* Strip off the size allocated to this field. That puts us
3932 at the first place we could have put the field with
3933 proper alignment. */
3936 /* Bump up by the alignment required for the type. */
3938 = size_binop (PLUS_EXPR
, rli
->bitpos
,
3940 ? CLASSTYPE_ALIGN (type
)
3941 : TYPE_ALIGN (type
)));
3942 normalize_rli (rli
);
3945 /* There was no conflict. We're done laying out this field. */
3949 /* Now that we know where it wil be placed, update its
3951 if (binfo
&& CLASS_TYPE_P (BINFO_TYPE (binfo
)))
3952 propagate_binfo_offsets (binfo
,
3953 convert (ssizetype
, offset
));
3956 /* Layout the empty base BINFO. EOC indicates the byte currently just
3957 past the end of the class, and should be correctly aligned for a
3958 class of the type indicated by BINFO; OFFSETS gives the offsets of
3959 the empty bases allocated so far. */
3962 layout_empty_base (binfo
, eoc
, offsets
)
3968 tree basetype
= BINFO_TYPE (binfo
);
3970 /* This routine should only be used for empty classes. */
3971 my_friendly_assert (is_empty_class (basetype
), 20000321);
3972 alignment
= ssize_int (CLASSTYPE_ALIGN_UNIT (basetype
));
3974 /* This is an empty base class. We first try to put it at offset
3976 if (layout_conflict_p (BINFO_TYPE (binfo
),
3977 BINFO_OFFSET (binfo
),
3981 /* That didn't work. Now, we move forward from the next
3982 available spot in the class. */
3983 propagate_binfo_offsets (binfo
, convert (ssizetype
, eoc
));
3986 if (!layout_conflict_p (BINFO_TYPE (binfo
),
3987 BINFO_OFFSET (binfo
),
3990 /* We finally found a spot where there's no overlap. */
3993 /* There's overlap here, too. Bump along to the next spot. */
3994 propagate_binfo_offsets (binfo
, alignment
);
3999 /* Build a FIELD_DECL for the base given by BINFO in the class
4000 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
4001 *BASE_ALIGN is a running maximum of the alignments of any base
4002 class. OFFSETS gives the location of empty base subobjects. */
4005 build_base_field (rli
, binfo
, empty_p
, offsets
)
4006 record_layout_info rli
;
4011 tree basetype
= BINFO_TYPE (binfo
);
4014 if (!COMPLETE_TYPE_P (basetype
))
4015 /* This error is now reported in xref_tag, thus giving better
4016 location information. */
4019 decl
= build_decl (FIELD_DECL
, NULL_TREE
, basetype
);
4020 DECL_ARTIFICIAL (decl
) = 1;
4021 DECL_FIELD_CONTEXT (decl
) = rli
->t
;
4022 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
4023 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
4024 DECL_ALIGN (decl
) = CLASSTYPE_ALIGN (basetype
);
4025 DECL_USER_ALIGN (decl
) = CLASSTYPE_USER_ALIGN (basetype
);
4027 if (!integer_zerop (DECL_SIZE (decl
)))
4029 /* The containing class is non-empty because it has a non-empty
4033 /* Try to place the field. It may take more than one try if we
4034 have a hard time placing the field without putting two
4035 objects of the same type at the same address. */
4036 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
);
4040 unsigned HOST_WIDE_INT eoc
;
4042 /* On some platforms (ARM), even empty classes will not be
4044 eoc
= tree_low_cst (rli_size_unit_so_far (rli
), 0);
4045 eoc
= CEIL (eoc
, DECL_ALIGN_UNIT (decl
)) * DECL_ALIGN_UNIT (decl
);
4046 layout_empty_base (binfo
, size_int (eoc
), offsets
);
4049 /* Check for inaccessible base classes. If the same base class
4050 appears more than once in the hierarchy, but isn't virtual, then
4052 if (get_base_distance (basetype
, rli
->t
, 0, NULL
) == -2)
4053 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4056 /* Record the offsets of BINFO and its base subobjects. */
4057 record_subobject_offsets (BINFO_TYPE (binfo
),
4058 BINFO_OFFSET (binfo
),
4063 /* Layout all of the non-virtual base classes. Record empty
4064 subobjects in OFFSETS. */
4067 build_base_fields (rli
, empty_p
, offsets
)
4068 record_layout_info rli
;
4072 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4075 int n_baseclasses
= CLASSTYPE_N_BASECLASSES (rec
);
4078 /* Under the new ABI, the primary base class is always allocated
4080 if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec
))
4081 build_base_field (rli
, CLASSTYPE_PRIMARY_BINFO (rec
),
4084 /* Now allocate the rest of the bases. */
4085 for (i
= 0; i
< n_baseclasses
; ++i
)
4089 base_binfo
= BINFO_BASETYPE (TYPE_BINFO (rec
), i
);
4091 /* Under the new ABI, the primary base was already allocated
4092 above, so we don't need to allocate it again here. */
4093 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (rec
))
4096 /* A primary virtual base class is allocated just like any other
4097 base class, but a non-primary virtual base is allocated
4098 later, in layout_virtual_bases. */
4099 if (TREE_VIA_VIRTUAL (base_binfo
)
4100 && !BINFO_PRIMARY_P (base_binfo
))
4103 build_base_field (rli
, base_binfo
, empty_p
, offsets
);
4107 /* Go through the TYPE_METHODS of T issuing any appropriate
4108 diagnostics, figuring out which methods override which other
4109 methods, and so forth. */
4116 int seen_one_arg_array_delete_p
= 0;
4118 for (x
= TYPE_METHODS (t
); x
; x
= TREE_CHAIN (x
))
4120 GNU_xref_member (current_class_name
, x
);
4122 /* If this was an evil function, don't keep it in class. */
4123 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x
)))
4126 check_for_override (x
, t
);
4127 if (DECL_PURE_VIRTUAL_P (x
) && ! DECL_VINDEX (x
))
4128 cp_error_at ("initializer specified for non-virtual method `%D'", x
);
4130 /* The name of the field is the original field name
4131 Save this in auxiliary field for later overloading. */
4132 if (DECL_VINDEX (x
))
4134 TYPE_POLYMORPHIC_P (t
) = 1;
4135 if (DECL_PURE_VIRTUAL_P (x
))
4136 CLASSTYPE_PURE_VIRTUALS (t
)
4137 = tree_cons (NULL_TREE
, x
, CLASSTYPE_PURE_VIRTUALS (t
));
4140 if (DECL_ARRAY_DELETE_OPERATOR_P (x
))
4144 /* When dynamically allocating an array of this type, we
4145 need a "cookie" to record how many elements we allocated,
4146 even if the array elements have no non-trivial
4147 destructor, if the usual array deallocation function
4148 takes a second argument of type size_t. The standard (in
4149 [class.free]) requires that the second argument be set
4151 second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x
)));
4152 /* Under the new ABI, we choose only those function that are
4153 explicitly declared as `operator delete[] (void *,
4155 if (!seen_one_arg_array_delete_p
4157 && TREE_CHAIN (second_parm
) == void_list_node
4158 && same_type_p (TREE_VALUE (second_parm
), sizetype
))
4159 TYPE_VEC_DELETE_TAKES_SIZE (t
) = 1;
4160 /* If there's no second parameter, then this is the usual
4161 deallocation function. */
4162 else if (second_parm
== void_list_node
)
4163 seen_one_arg_array_delete_p
= 1;
4168 /* FN is a constructor or destructor. Clone the declaration to create
4169 a specialized in-charge or not-in-charge version, as indicated by
4173 build_clone (fn
, name
)
4180 /* Copy the function. */
4181 clone
= copy_decl (fn
);
4182 /* Remember where this function came from. */
4183 DECL_CLONED_FUNCTION (clone
) = fn
;
4184 DECL_ABSTRACT_ORIGIN (clone
) = fn
;
4185 /* Reset the function name. */
4186 DECL_NAME (clone
) = name
;
4187 DECL_ASSEMBLER_NAME (clone
) = DECL_NAME (clone
);
4188 /* There's no pending inline data for this function. */
4189 DECL_PENDING_INLINE_INFO (clone
) = NULL
;
4190 DECL_PENDING_INLINE_P (clone
) = 0;
4191 /* And it hasn't yet been deferred. */
4192 DECL_DEFERRED_FN (clone
) = 0;
4194 /* The base-class destructor is not virtual. */
4195 if (name
== base_dtor_identifier
)
4197 DECL_VIRTUAL_P (clone
) = 0;
4198 if (TREE_CODE (clone
) != TEMPLATE_DECL
)
4199 DECL_VINDEX (clone
) = NULL_TREE
;
4202 /* If there was an in-charge parameter, drop it from the function
4204 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4210 exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone
));
4211 basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4212 parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4213 /* Skip the `this' parameter. */
4214 parmtypes
= TREE_CHAIN (parmtypes
);
4215 /* Skip the in-charge parameter. */
4216 parmtypes
= TREE_CHAIN (parmtypes
);
4217 /* And the VTT parm, in a complete [cd]tor. */
4218 if (DECL_HAS_VTT_PARM_P (fn
)
4219 && ! DECL_NEEDS_VTT_PARM_P (clone
))
4220 parmtypes
= TREE_CHAIN (parmtypes
);
4221 /* If this is subobject constructor or destructor, add the vtt
4224 = build_cplus_method_type (basetype
,
4225 TREE_TYPE (TREE_TYPE (clone
)),
4228 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
),
4232 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
4233 aren't function parameters; those are the template parameters. */
4234 if (TREE_CODE (clone
) != TEMPLATE_DECL
)
4236 DECL_ARGUMENTS (clone
) = copy_list (DECL_ARGUMENTS (clone
));
4237 /* Remove the in-charge parameter. */
4238 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4240 TREE_CHAIN (DECL_ARGUMENTS (clone
))
4241 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone
)));
4242 DECL_HAS_IN_CHARGE_PARM_P (clone
) = 0;
4244 /* And the VTT parm, in a complete [cd]tor. */
4245 if (DECL_HAS_VTT_PARM_P (fn
))
4247 if (DECL_NEEDS_VTT_PARM_P (clone
))
4248 DECL_HAS_VTT_PARM_P (clone
) = 1;
4251 TREE_CHAIN (DECL_ARGUMENTS (clone
))
4252 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone
)));
4253 DECL_HAS_VTT_PARM_P (clone
) = 0;
4257 for (parms
= DECL_ARGUMENTS (clone
); parms
; parms
= TREE_CHAIN (parms
))
4259 DECL_CONTEXT (parms
) = clone
;
4260 copy_lang_decl (parms
);
4264 /* Mangle the function name. */
4265 set_mangled_name_for_decl (clone
);
4267 /* Create the RTL for this function. */
4268 SET_DECL_RTL (clone
, NULL_RTX
);
4269 rest_of_decl_compilation (clone
, NULL
, /*top_level=*/1, at_eof
);
4271 /* Make it easy to find the CLONE given the FN. */
4272 TREE_CHAIN (clone
) = TREE_CHAIN (fn
);
4273 TREE_CHAIN (fn
) = clone
;
4275 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4276 if (TREE_CODE (clone
) == TEMPLATE_DECL
)
4280 DECL_TEMPLATE_RESULT (clone
)
4281 = build_clone (DECL_TEMPLATE_RESULT (clone
), name
);
4282 result
= DECL_TEMPLATE_RESULT (clone
);
4283 DECL_TEMPLATE_INFO (result
) = copy_node (DECL_TEMPLATE_INFO (result
));
4284 DECL_TI_TEMPLATE (result
) = clone
;
4286 else if (DECL_DEFERRED_FN (fn
))
4292 /* Produce declarations for all appropriate clones of FN. If
4293 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4294 CLASTYPE_METHOD_VEC as well. */
4297 clone_function_decl (fn
, update_method_vec_p
)
4299 int update_method_vec_p
;
4303 /* Avoid inappropriate cloning. */
4305 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn
)))
4308 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
4310 /* For each constructor, we need two variants: an in-charge version
4311 and a not-in-charge version. */
4312 clone
= build_clone (fn
, complete_ctor_identifier
);
4313 if (update_method_vec_p
)
4314 add_method (DECL_CONTEXT (clone
), clone
, /*error_p=*/0);
4315 clone
= build_clone (fn
, base_ctor_identifier
);
4316 if (update_method_vec_p
)
4317 add_method (DECL_CONTEXT (clone
), clone
, /*error_p=*/0);
4321 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
), 20000411);
4323 /* For each destructor, we need three variants: an in-charge
4324 version, a not-in-charge version, and an in-charge deleting
4325 version. We clone the deleting version first because that
4326 means it will go second on the TYPE_METHODS list -- and that
4327 corresponds to the correct layout order in the virtual
4330 For a non-virtual destructor, we do not build a deleting
4332 if (DECL_VIRTUAL_P (fn
))
4334 clone
= build_clone (fn
, deleting_dtor_identifier
);
4335 if (update_method_vec_p
)
4336 add_method (DECL_CONTEXT (clone
), clone
, /*error_p=*/0);
4338 clone
= build_clone (fn
, complete_dtor_identifier
);
4339 if (update_method_vec_p
)
4340 add_method (DECL_CONTEXT (clone
), clone
, /*error_p=*/0);
4341 clone
= build_clone (fn
, base_dtor_identifier
);
4342 if (update_method_vec_p
)
4343 add_method (DECL_CONTEXT (clone
), clone
, /*error_p=*/0);
4346 /* Note that this is an abstract function that is never emitted. */
4347 DECL_ABSTRACT (fn
) = 1;
4350 /* For each of the constructors and destructors in T, create an
4351 in-charge and not-in-charge variant. */
4354 clone_constructors_and_destructors (t
)
4359 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4361 if (!CLASSTYPE_METHOD_VEC (t
))
4364 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4365 clone_function_decl (OVL_CURRENT (fns
), /*update_method_vec_p=*/1);
4366 for (fns
= CLASSTYPE_DESTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4367 clone_function_decl (OVL_CURRENT (fns
), /*update_method_vec_p=*/1);
4370 /* Remove all zero-width bit-fields from T. */
4373 remove_zero_width_bit_fields (t
)
4378 fieldsp
= &TYPE_FIELDS (t
);
4381 if (TREE_CODE (*fieldsp
) == FIELD_DECL
4382 && DECL_C_BIT_FIELD (*fieldsp
)
4383 && DECL_INITIAL (*fieldsp
))
4384 *fieldsp
= TREE_CHAIN (*fieldsp
);
4386 fieldsp
= &TREE_CHAIN (*fieldsp
);
4390 /* Check the validity of the bases and members declared in T. Add any
4391 implicitly-generated functions (like copy-constructors and
4392 assignment operators). Compute various flag bits (like
4393 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4394 level: i.e., independently of the ABI in use. */
4397 check_bases_and_members (t
, empty_p
)
4401 /* Nonzero if we are not allowed to generate a default constructor
4403 int cant_have_default_ctor
;
4404 /* Nonzero if the implicitly generated copy constructor should take
4405 a non-const reference argument. */
4406 int cant_have_const_ctor
;
4407 /* Nonzero if the the implicitly generated assignment operator
4408 should take a non-const reference argument. */
4409 int no_const_asn_ref
;
4412 /* By default, we use const reference arguments and generate default
4414 cant_have_default_ctor
= 0;
4415 cant_have_const_ctor
= 0;
4416 no_const_asn_ref
= 0;
4418 /* Assume that the class is nearly empty; we'll clear this flag if
4419 it turns out not to be nearly empty. */
4420 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
4422 /* Check all the base-classes. */
4423 check_bases (t
, &cant_have_default_ctor
, &cant_have_const_ctor
,
4426 /* Check all the data member declarations. */
4427 check_field_decls (t
, &access_decls
, empty_p
,
4428 &cant_have_default_ctor
,
4429 &cant_have_const_ctor
,
4432 /* Check all the method declarations. */
4435 /* A nearly-empty class has to be vptr-containing; a nearly empty
4436 class contains just a vptr. */
4437 if (!TYPE_CONTAINS_VPTR_P (t
))
4438 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4440 /* Do some bookkeeping that will guide the generation of implicitly
4441 declared member functions. */
4442 TYPE_HAS_COMPLEX_INIT_REF (t
)
4443 |= (TYPE_HAS_INIT_REF (t
)
4444 || TYPE_USES_VIRTUAL_BASECLASSES (t
)
4445 || TYPE_POLYMORPHIC_P (t
));
4446 TYPE_NEEDS_CONSTRUCTING (t
)
4447 |= (TYPE_HAS_CONSTRUCTOR (t
)
4448 || TYPE_USES_VIRTUAL_BASECLASSES (t
)
4449 || TYPE_POLYMORPHIC_P (t
));
4450 CLASSTYPE_NON_AGGREGATE (t
) |= (TYPE_HAS_CONSTRUCTOR (t
)
4451 || TYPE_POLYMORPHIC_P (t
));
4452 CLASSTYPE_NON_POD_P (t
)
4453 |= (CLASSTYPE_NON_AGGREGATE (t
) || TYPE_HAS_DESTRUCTOR (t
)
4454 || TYPE_HAS_ASSIGN_REF (t
));
4455 TYPE_HAS_REAL_ASSIGN_REF (t
) |= TYPE_HAS_ASSIGN_REF (t
);
4456 TYPE_HAS_COMPLEX_ASSIGN_REF (t
)
4457 |= TYPE_HAS_ASSIGN_REF (t
) || TYPE_USES_VIRTUAL_BASECLASSES (t
);
4459 /* Synthesize any needed methods. Note that methods will be synthesized
4460 for anonymous unions; grok_x_components undoes that. */
4461 add_implicitly_declared_members (t
, cant_have_default_ctor
,
4462 cant_have_const_ctor
,
4465 /* Create the in-charge and not-in-charge variants of constructors
4467 clone_constructors_and_destructors (t
);
4469 /* Process the using-declarations. */
4470 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
4471 handle_using_decl (TREE_VALUE (access_decls
), t
);
4473 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4474 finish_struct_methods (t
);
4477 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4478 accordingly. If a new vfield was created (because T doesn't have a
4479 primary base class), then the newly created field is returned. It
4480 is not added to the TYPE_FIELDS list; it is the caller's
4481 responsibility to do that. */
4484 create_vtable_ptr (t
, empty_p
, vfuns_p
,
4485 new_virtuals_p
, overridden_virtuals_p
)
4489 tree
*new_virtuals_p
;
4490 tree
*overridden_virtuals_p
;
4494 /* Loop over the virtual functions, adding them to our various
4496 for (fn
= TYPE_METHODS (t
); fn
; fn
= TREE_CHAIN (fn
))
4497 if (DECL_VINDEX (fn
) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
))
4498 add_virtual_function (new_virtuals_p
, overridden_virtuals_p
,
4501 /* If we couldn't find an appropriate base class, create a new field
4502 here. Even if there weren't any new virtual functions, we might need a
4503 new virtual function table if we're supposed to include vptrs in
4504 all classes that need them. */
4505 if (!TYPE_VFIELD (t
)
4507 || (TYPE_CONTAINS_VPTR_P (t
) && vptrs_present_everywhere_p ())))
4509 /* We build this decl with vtbl_ptr_type_node, which is a
4510 `vtable_entry_type*'. It might seem more precise to use
4511 `vtable_entry_type (*)[N]' where N is the number of firtual
4512 functions. However, that would require the vtable pointer in
4513 base classes to have a different type than the vtable pointer
4514 in derived classes. We could make that happen, but that
4515 still wouldn't solve all the problems. In particular, the
4516 type-based alias analysis code would decide that assignments
4517 to the base class vtable pointer can't alias assignments to
4518 the derived class vtable pointer, since they have different
4519 types. Thus, in an derived class destructor, where the base
4520 class constructor was inlined, we could generate bad code for
4521 setting up the vtable pointer.
4523 Therefore, we use one type for all vtable pointers. We still
4524 use a type-correct type; it's just doesn't indicate the array
4525 bounds. That's better than using `void*' or some such; it's
4526 cleaner, and it let's the alias analysis code know that these
4527 stores cannot alias stores to void*! */
4529 = build_vtbl_or_vbase_field (get_vfield_name (t
),
4530 get_identifier (VFIELD_BASE
),
4536 if (CLASSTYPE_N_BASECLASSES (t
))
4537 /* If there were any baseclasses, they can't possibly be at
4538 offset zero any more, because that's where the vtable
4539 pointer is. So, converting to a base class is going to
4541 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t
) = 1;
4543 return TYPE_VFIELD (t
);
4549 /* Fixup the inline function given by INFO now that the class is
4553 fixup_pending_inline (fn
)
4556 if (DECL_PENDING_INLINE_INFO (fn
))
4558 tree args
= DECL_ARGUMENTS (fn
);
4561 DECL_CONTEXT (args
) = fn
;
4562 args
= TREE_CHAIN (args
);
4567 /* Fixup the inline methods and friends in TYPE now that TYPE is
4571 fixup_inline_methods (type
)
4574 tree method
= TYPE_METHODS (type
);
4576 if (method
&& TREE_CODE (method
) == TREE_VEC
)
4578 if (TREE_VEC_ELT (method
, 1))
4579 method
= TREE_VEC_ELT (method
, 1);
4580 else if (TREE_VEC_ELT (method
, 0))
4581 method
= TREE_VEC_ELT (method
, 0);
4583 method
= TREE_VEC_ELT (method
, 2);
4586 /* Do inline member functions. */
4587 for (; method
; method
= TREE_CHAIN (method
))
4588 fixup_pending_inline (method
);
4591 for (method
= CLASSTYPE_INLINE_FRIENDS (type
);
4593 method
= TREE_CHAIN (method
))
4594 fixup_pending_inline (TREE_VALUE (method
));
4595 CLASSTYPE_INLINE_FRIENDS (type
) = NULL_TREE
;
4598 /* Add OFFSET to all base types of BINFO which is a base in the
4599 hierarchy dominated by T.
4601 OFFSET, which is a type offset, is number of bytes. */
4604 propagate_binfo_offsets (binfo
, offset
)
4611 /* Update BINFO's offset. */
4612 BINFO_OFFSET (binfo
)
4613 = convert (sizetype
,
4614 size_binop (PLUS_EXPR
,
4615 convert (ssizetype
, BINFO_OFFSET (binfo
)),
4618 /* Find the primary base class. */
4619 primary_binfo
= get_primary_binfo (binfo
);
4621 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4623 for (i
= -1; i
< BINFO_N_BASETYPES (binfo
); ++i
)
4627 /* On the first through the loop, do the primary base. Because
4628 the primary base need not be an immediate base, we must
4629 handle the primary base specially. */
4635 base_binfo
= primary_binfo
;
4639 base_binfo
= BINFO_BASETYPE (binfo
, i
);
4640 /* Don't do the primary base twice. */
4641 if (base_binfo
== primary_binfo
)
4645 /* Skip virtual bases that aren't our primary base. */
4646 if (TREE_VIA_VIRTUAL (base_binfo
)
4647 && BINFO_PRIMARY_BASE_OF (base_binfo
) != binfo
)
4650 propagate_binfo_offsets (base_binfo
, offset
);
4654 /* Called via dfs_walk from layout_virtual bases. */
4657 dfs_set_offset_for_unshared_vbases (binfo
, data
)
4661 /* If this is a virtual base, make sure it has the same offset as
4662 the shared copy. If it's a primary base, then we know it's
4664 if (TREE_VIA_VIRTUAL (binfo
) && !BINFO_PRIMARY_P (binfo
))
4666 tree t
= (tree
) data
;
4670 vbase
= binfo_for_vbase (BINFO_TYPE (binfo
), t
);
4671 offset
= size_diffop (BINFO_OFFSET (vbase
), BINFO_OFFSET (binfo
));
4672 propagate_binfo_offsets (binfo
, offset
);
4678 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4679 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4680 empty subobjects of T. */
4683 layout_virtual_bases (t
, offsets
)
4688 unsigned HOST_WIDE_INT dsize
;
4689 unsigned HOST_WIDE_INT eoc
;
4691 if (CLASSTYPE_N_BASECLASSES (t
) == 0)
4694 #ifdef STRUCTURE_SIZE_BOUNDARY
4695 /* Packed structures don't need to have minimum size. */
4696 if (! TYPE_PACKED (t
))
4697 TYPE_ALIGN (t
) = MAX (TYPE_ALIGN (t
), STRUCTURE_SIZE_BOUNDARY
);
4700 /* DSIZE is the size of the class without the virtual bases. */
4701 dsize
= tree_low_cst (TYPE_SIZE (t
), 1);
4703 /* Make every class have alignment of at least one. */
4704 TYPE_ALIGN (t
) = MAX (TYPE_ALIGN (t
), BITS_PER_UNIT
);
4706 /* Go through the virtual bases, allocating space for each virtual
4707 base that is not already a primary base class. Under the new
4708 ABI, these are allocated according to a depth-first left-to-right
4709 postorder traversal; in the new ABI, inheritance graph order is
4711 for (vbases
= TYPE_BINFO (t
);
4713 vbases
= TREE_CHAIN (vbases
))
4717 if (!TREE_VIA_VIRTUAL (vbases
))
4719 vbase
= binfo_for_vbase (BINFO_TYPE (vbases
), t
);
4721 if (!BINFO_PRIMARY_P (vbase
))
4723 /* This virtual base is not a primary base of any class in the
4724 hierarchy, so we have to add space for it. */
4726 unsigned int desired_align
;
4728 basetype
= BINFO_TYPE (vbase
);
4730 desired_align
= CLASSTYPE_ALIGN (basetype
);
4731 TYPE_ALIGN (t
) = MAX (TYPE_ALIGN (t
), desired_align
);
4733 /* Add padding so that we can put the virtual base class at an
4734 appropriately aligned offset. */
4735 dsize
= CEIL (dsize
, desired_align
) * desired_align
;
4737 /* Under the new ABI, we try to squish empty virtual bases in
4738 just like ordinary empty bases. */
4739 if (is_empty_class (basetype
))
4740 layout_empty_base (vbase
,
4741 size_int (CEIL (dsize
, BITS_PER_UNIT
)),
4747 offset
= ssize_int (CEIL (dsize
, BITS_PER_UNIT
));
4748 offset
= size_diffop (offset
,
4750 BINFO_OFFSET (vbase
)));
4752 /* And compute the offset of the virtual base. */
4753 propagate_binfo_offsets (vbase
, offset
);
4754 /* Every virtual baseclass takes a least a UNIT, so that
4755 we can take it's address and get something different
4757 dsize
+= MAX (BITS_PER_UNIT
,
4758 tree_low_cst (CLASSTYPE_SIZE (basetype
), 0));
4761 /* Keep track of the offsets assigned to this virtual base. */
4762 record_subobject_offsets (BINFO_TYPE (vbase
),
4763 BINFO_OFFSET (vbase
),
4769 /* Now, go through the TYPE_BINFO hierarchy, setting the
4770 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4771 bases and their direct and indirect bases. The ambiguity checks
4772 in get_base_distance depend on the BINFO_OFFSETs being set
4774 dfs_walk (TYPE_BINFO (t
), dfs_set_offset_for_unshared_vbases
, NULL
, t
);
4776 /* If we had empty base classes that protruded beyond the end of the
4777 class, we didn't update DSIZE above; we were hoping to overlay
4778 multiple such bases at the same location. */
4779 eoc
= end_of_class (t
, /*include_virtuals_p=*/1);
4780 if (eoc
* BITS_PER_UNIT
> dsize
)
4781 dsize
= (eoc
+ 1) * BITS_PER_UNIT
;
4783 /* Now, make sure that the total size of the type is a multiple of
4785 dsize
= CEIL (dsize
, TYPE_ALIGN (t
)) * TYPE_ALIGN (t
);
4786 TYPE_SIZE (t
) = bitsize_int (dsize
);
4787 TYPE_SIZE_UNIT (t
) = convert (sizetype
,
4788 size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (t
),
4789 bitsize_unit_node
));
4791 /* Check for ambiguous virtual bases. */
4793 for (vbases
= CLASSTYPE_VBASECLASSES (t
);
4795 vbases
= TREE_CHAIN (vbases
))
4797 tree basetype
= BINFO_TYPE (TREE_VALUE (vbases
));
4798 if (get_base_distance (basetype
, t
, 0, (tree
*)0) == -2)
4799 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4804 /* Returns the offset of the byte just past the end of the base class
4805 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4806 only non-virtual bases are included. */
4808 static unsigned HOST_WIDE_INT
4809 end_of_class (t
, include_virtuals_p
)
4811 int include_virtuals_p
;
4813 unsigned HOST_WIDE_INT result
= 0;
4816 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (t
); ++i
)
4820 unsigned HOST_WIDE_INT end_of_base
;
4822 base_binfo
= BINFO_BASETYPE (TYPE_BINFO (t
), i
);
4824 if (!include_virtuals_p
4825 && TREE_VIA_VIRTUAL (base_binfo
)
4826 && !BINFO_PRIMARY_P (base_binfo
))
4829 offset
= size_binop (PLUS_EXPR
,
4830 BINFO_OFFSET (base_binfo
),
4831 CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo
)));
4832 end_of_base
= tree_low_cst (offset
, /*pos=*/1);
4833 if (end_of_base
> result
)
4834 result
= end_of_base
;
4840 /* Compare two INTEGER_CSTs K1 and K2. */
4843 splay_tree_compare_integer_csts (k1
, k2
)
4847 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
4850 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4851 BINFO_OFFSETs for all of the base-classes. Position the vtable
4855 layout_class_type (t
, empty_p
, vfuns_p
,
4856 new_virtuals_p
, overridden_virtuals_p
)
4860 tree
*new_virtuals_p
;
4861 tree
*overridden_virtuals_p
;
4863 tree non_static_data_members
;
4866 record_layout_info rli
;
4867 unsigned HOST_WIDE_INT eoc
;
4868 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4869 types that appear at that offset. */
4870 splay_tree empty_base_offsets
;
4872 /* Keep track of the first non-static data member. */
4873 non_static_data_members
= TYPE_FIELDS (t
);
4875 /* Start laying out the record. */
4876 rli
= start_record_layout (t
);
4878 /* If possible, we reuse the virtual function table pointer from one
4879 of our base classes. */
4880 determine_primary_base (t
, vfuns_p
);
4882 /* Create a pointer to our virtual function table. */
4883 vptr
= create_vtable_ptr (t
, empty_p
, vfuns_p
,
4884 new_virtuals_p
, overridden_virtuals_p
);
4886 /* Under the new ABI, the vptr is always the first thing in the
4890 TYPE_FIELDS (t
) = chainon (vptr
, TYPE_FIELDS (t
));
4891 place_field (rli
, vptr
);
4894 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4895 empty_base_offsets
= splay_tree_new (splay_tree_compare_integer_csts
,
4897 build_base_fields (rli
, empty_p
, empty_base_offsets
);
4898 /* Add pointers to all of our virtual base-classes. */
4899 TYPE_FIELDS (t
) = chainon (build_vbase_pointer_fields (rli
, empty_p
),
4902 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4903 we have to save this before we start modifying
4904 TYPE_NONCOPIED_PARTS. */
4905 fixup_inline_methods (t
);
4907 /* Layout the non-static data members. */
4908 for (field
= non_static_data_members
; field
; field
= TREE_CHAIN (field
))
4913 /* We still pass things that aren't non-static data members to
4914 the back-end, in case it wants to do something with them. */
4915 if (TREE_CODE (field
) != FIELD_DECL
)
4917 place_field (rli
, field
);
4921 type
= TREE_TYPE (field
);
4923 /* If this field is a bit-field whose width is greater than its
4924 type, then there are some special rules for allocating it
4925 under the new ABI. Under the old ABI, there were no special
4926 rules, but the back-end can't handle bitfields longer than a
4927 `long long', so we use the same mechanism. */
4928 if (DECL_C_BIT_FIELD (field
)
4929 && INT_CST_LT (TYPE_SIZE (type
), DECL_SIZE (field
)))
4931 integer_type_kind itk
;
4934 /* We must allocate the bits as if suitably aligned for the
4935 longest integer type that fits in this many bits. type
4936 of the field. Then, we are supposed to use the left over
4937 bits as additional padding. */
4938 for (itk
= itk_char
; itk
!= itk_none
; ++itk
)
4939 if (INT_CST_LT (DECL_SIZE (field
),
4940 TYPE_SIZE (integer_types
[itk
])))
4943 /* ITK now indicates a type that is too large for the
4944 field. We have to back up by one to find the largest
4946 integer_type
= integer_types
[itk
- 1];
4947 padding
= size_binop (MINUS_EXPR
, DECL_SIZE (field
),
4948 TYPE_SIZE (integer_type
));
4949 DECL_SIZE (field
) = TYPE_SIZE (integer_type
);
4950 DECL_ALIGN (field
) = TYPE_ALIGN (integer_type
);
4951 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (integer_type
);
4954 padding
= NULL_TREE
;
4956 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
4957 empty_base_offsets
);
4959 /* If we needed additional padding after this field, add it
4965 padding_field
= build_decl (FIELD_DECL
,
4968 DECL_BIT_FIELD (padding_field
) = 1;
4969 DECL_SIZE (padding_field
) = padding
;
4970 DECL_ALIGN (padding_field
) = 1;
4971 DECL_USER_ALIGN (padding_field
) = 0;
4972 layout_nonempty_base_or_field (rli
, padding_field
,
4974 empty_base_offsets
);
4978 /* It might be the case that we grew the class to allocate a
4979 zero-sized base class. That won't be reflected in RLI, yet,
4980 because we are willing to overlay multiple bases at the same
4981 offset. However, now we need to make sure that RLI is big enough
4982 to reflect the entire class. */
4983 eoc
= end_of_class (t
, /*include_virtuals_p=*/0);
4984 if (TREE_CODE (rli_size_unit_so_far (rli
)) == INTEGER_CST
4985 && compare_tree_int (rli_size_unit_so_far (rli
), eoc
) < 0)
4987 rli
->offset
= size_binop (MAX_EXPR
, rli
->offset
, size_int (eoc
+ 1));
4988 rli
->bitpos
= bitsize_zero_node
;
4991 /* We make all structures have at least one element, so that they
4992 have non-zero size. In the new ABI, the class may be empty even
4993 if it has basetypes. Therefore, we add the fake field after all
4994 the other fields; if there are already FIELD_DECLs on the list,
4995 their offsets will not be disturbed. */
5000 padding
= build_decl (FIELD_DECL
, NULL_TREE
, char_type_node
);
5001 place_field (rli
, padding
);
5002 TYPE_NONCOPIED_PARTS (t
)
5003 = tree_cons (NULL_TREE
, padding
, TYPE_NONCOPIED_PARTS (t
));
5004 TREE_STATIC (TYPE_NONCOPIED_PARTS (t
)) = 1;
5007 /* Let the back-end lay out the type. Note that at this point we
5008 have only included non-virtual base-classes; we will lay out the
5009 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
5010 this call are not necessarily correct; they are just the size and
5011 alignment when no virtual base clases are used. */
5012 finish_record_layout (rli
);
5014 /* Delete all zero-width bit-fields from the list of fields. Now
5015 that the type is laid out they are no longer important. */
5016 remove_zero_width_bit_fields (t
);
5018 /* Remember the size and alignment of the class before adding
5019 the virtual bases. */
5022 CLASSTYPE_SIZE (t
) = bitsize_zero_node
;
5023 CLASSTYPE_SIZE_UNIT (t
) = size_zero_node
;
5027 CLASSTYPE_SIZE (t
) = TYPE_BINFO_SIZE (t
);
5028 CLASSTYPE_SIZE_UNIT (t
) = TYPE_BINFO_SIZE_UNIT (t
);
5031 CLASSTYPE_ALIGN (t
) = TYPE_ALIGN (t
);
5032 CLASSTYPE_USER_ALIGN (t
) = TYPE_USER_ALIGN (t
);
5034 /* Set the TYPE_DECL for this type to contain the right
5035 value for DECL_OFFSET, so that we can use it as part
5036 of a COMPONENT_REF for multiple inheritance. */
5037 layout_decl (TYPE_MAIN_DECL (t
), 0);
5039 /* Now fix up any virtual base class types that we left lying
5040 around. We must get these done before we try to lay out the
5041 virtual function table. As a side-effect, this will remove the
5042 base subobject fields. */
5043 layout_virtual_bases (t
, empty_base_offsets
);
5046 splay_tree_delete (empty_base_offsets
);
5049 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5050 (or C++ class declaration).
5052 For C++, we must handle the building of derived classes.
5053 Also, C++ allows static class members. The way that this is
5054 handled is to keep the field name where it is (as the DECL_NAME
5055 of the field), and place the overloaded decl in the bit position
5056 of the field. layout_record and layout_union will know about this.
5058 More C++ hair: inline functions have text in their
5059 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5060 meaningful tree structure. After the struct has been laid out, set
5061 things up so that this can happen.
5063 And still more: virtual functions. In the case of single inheritance,
5064 when a new virtual function is seen which redefines a virtual function
5065 from the base class, the new virtual function is placed into
5066 the virtual function table at exactly the same address that
5067 it had in the base class. When this is extended to multiple
5068 inheritance, the same thing happens, except that multiple virtual
5069 function tables must be maintained. The first virtual function
5070 table is treated in exactly the same way as in the case of single
5071 inheritance. Additional virtual function tables have different
5072 DELTAs, which tell how to adjust `this' to point to the right thing.
5074 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5082 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5083 a FUNCTION_DECL. Each of these functions is a virtual function
5084 declared in T that does not override any virtual function from a
5086 tree new_virtuals
= NULL_TREE
;
5087 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5088 except that each declaration here overrides the declaration from
5090 tree overridden_virtuals
= NULL_TREE
;
5095 if (COMPLETE_TYPE_P (t
))
5097 if (IS_AGGR_TYPE (t
))
5098 cp_error ("redefinition of `%#T'", t
);
5100 my_friendly_abort (172);
5105 GNU_xref_decl (current_function_decl
, t
);
5107 /* If this type was previously laid out as a forward reference,
5108 make sure we lay it out again. */
5109 TYPE_SIZE (t
) = NULL_TREE
;
5110 CLASSTYPE_GOT_SEMICOLON (t
) = 0;
5111 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
5113 CLASSTYPE_RTTI (t
) = NULL_TREE
;
5115 /* Do end-of-class semantic processing: checking the validity of the
5116 bases and members and add implicitly generated methods. */
5117 check_bases_and_members (t
, &empty
);
5119 /* Layout the class itself. */
5120 layout_class_type (t
, &empty
, &vfuns
,
5121 &new_virtuals
, &overridden_virtuals
);
5123 if (flag_dump_class_layout
)
5124 dump_class_hierarchy (*flag_dump_class_layout
5125 ? flag_dump_class_layout
: NULL
,
5128 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
5129 might need to know it for setting up the offsets in the vtable
5130 (or in thunks) below. */
5131 vfield
= TYPE_VFIELD (t
);
5132 if (vfield
!= NULL_TREE
5133 && DECL_FIELD_CONTEXT (vfield
) != t
)
5135 tree binfo
= get_binfo (DECL_FIELD_CONTEXT (vfield
), t
, 0);
5137 vfield
= copy_decl (vfield
);
5139 DECL_FIELD_CONTEXT (vfield
) = t
;
5140 DECL_FIELD_OFFSET (vfield
)
5141 = size_binop (PLUS_EXPR
,
5142 BINFO_OFFSET (binfo
),
5143 DECL_FIELD_OFFSET (vfield
));
5144 TYPE_VFIELD (t
) = vfield
;
5148 = modify_all_vtables (t
, &vfuns
, nreverse (overridden_virtuals
));
5150 /* If we created a new vtbl pointer for this class, add it to the
5152 if (TYPE_VFIELD (t
) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5153 CLASSTYPE_VFIELDS (t
)
5154 = chainon (CLASSTYPE_VFIELDS (t
), build_tree_list (NULL_TREE
, t
));
5156 /* If necessary, create the primary vtable for this class. */
5158 || overridden_virtuals
5159 || (TYPE_CONTAINS_VPTR_P (t
) && vptrs_present_everywhere_p ()))
5161 new_virtuals
= nreverse (new_virtuals
);
5162 /* We must enter these virtuals into the table. */
5163 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5164 build_primary_vtable (NULL_TREE
, t
);
5165 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
), t
))
5166 /* Here we know enough to change the type of our virtual
5167 function table, but we will wait until later this function. */
5168 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
5170 /* If this type has basetypes with constructors, then those
5171 constructors might clobber the virtual function table. But
5172 they don't if the derived class shares the exact vtable of the base
5175 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
5177 /* If we didn't need a new vtable, see if we should copy one from
5179 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5181 tree binfo
= CLASSTYPE_PRIMARY_BINFO (t
);
5183 /* If this class uses a different vtable than its primary base
5184 then when we will need to initialize our vptr after the base
5185 class constructor runs. */
5186 if (TYPE_BINFO_VTABLE (t
) != BINFO_VTABLE (binfo
))
5187 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
5190 if (TYPE_CONTAINS_VPTR_P (t
))
5192 if (TYPE_BINFO_VTABLE (t
))
5193 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t
)),
5195 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5196 my_friendly_assert (TYPE_BINFO_VIRTUALS (t
) == NULL_TREE
,
5199 CLASSTYPE_VSIZE (t
) = vfuns
;
5200 /* Entries for virtual functions defined in the primary base are
5201 followed by entries for new functions unique to this class. */
5202 TYPE_BINFO_VIRTUALS (t
)
5203 = chainon (TYPE_BINFO_VIRTUALS (t
), new_virtuals
);
5204 /* Finally, add entries for functions that override virtuals
5205 from non-primary bases. */
5206 TYPE_BINFO_VIRTUALS (t
)
5207 = chainon (TYPE_BINFO_VIRTUALS (t
), overridden_virtuals
);
5210 finish_struct_bits (t
);
5212 /* Complete the rtl for any static member objects of the type we're
5214 for (x
= TYPE_FIELDS (t
); x
; x
= TREE_CHAIN (x
))
5215 if (TREE_CODE (x
) == VAR_DECL
&& TREE_STATIC (x
)
5216 && TREE_TYPE (x
) == t
)
5217 DECL_MODE (x
) = TYPE_MODE (t
);
5219 /* Done with FIELDS...now decide whether to sort these for
5220 faster lookups later.
5222 The C front-end only does this when n_fields > 15. We use
5223 a smaller number because most searches fail (succeeding
5224 ultimately as the search bores through the inheritance
5225 hierarchy), and we want this failure to occur quickly. */
5227 n_fields
= count_fields (TYPE_FIELDS (t
));
5230 tree field_vec
= make_tree_vec (n_fields
);
5231 add_fields_to_vec (TYPE_FIELDS (t
), field_vec
, 0);
5232 qsort (&TREE_VEC_ELT (field_vec
, 0), n_fields
, sizeof (tree
),
5233 (int (*)(const void *, const void *))field_decl_cmp
);
5234 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t
)))
5235 retrofit_lang_decl (TYPE_MAIN_DECL (t
));
5236 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t
)) = field_vec
;
5239 if (TYPE_HAS_CONSTRUCTOR (t
))
5241 tree vfields
= CLASSTYPE_VFIELDS (t
);
5245 /* Mark the fact that constructor for T
5246 could affect anybody inheriting from T
5247 who wants to initialize vtables for VFIELDS's type. */
5248 if (VF_DERIVED_VALUE (vfields
))
5249 TREE_ADDRESSABLE (vfields
) = 1;
5250 vfields
= TREE_CHAIN (vfields
);
5254 /* Make the rtl for any new vtables we have created, and unmark
5255 the base types we marked. */
5258 /* Build the VTT for T. */
5261 if (TYPE_VFIELD (t
))
5263 /* In addition to this one, all the other vfields should be listed. */
5264 /* Before that can be done, we have to have FIELD_DECLs for them, and
5265 a place to find them. */
5266 TYPE_NONCOPIED_PARTS (t
)
5267 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t
)),
5268 TYPE_VFIELD (t
), TYPE_NONCOPIED_PARTS (t
));
5270 if (warn_nonvdtor
&& TYPE_HAS_DESTRUCTOR (t
)
5271 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 1)) == NULL_TREE
)
5272 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5276 hack_incomplete_structures (t
);
5278 if (warn_overloaded_virtual
)
5281 maybe_suppress_debug_info (t
);
5283 /* Finish debugging output for this type. */
5284 rest_of_type_compilation (t
, ! LOCAL_CLASS_P (t
));
5287 /* When T was built up, the member declarations were added in reverse
5288 order. Rearrange them to declaration order. */
5291 unreverse_member_declarations (t
)
5298 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5299 reverse order. Put them in declaration order now. */
5300 TYPE_METHODS (t
) = nreverse (TYPE_METHODS (t
));
5301 CLASSTYPE_TAGS (t
) = nreverse (CLASSTYPE_TAGS (t
));
5303 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5304 reverse order, so we can't just use nreverse. */
5306 for (x
= TYPE_FIELDS (t
);
5307 x
&& TREE_CODE (x
) != TYPE_DECL
;
5310 next
= TREE_CHAIN (x
);
5311 TREE_CHAIN (x
) = prev
;
5316 TREE_CHAIN (TYPE_FIELDS (t
)) = x
;
5318 TYPE_FIELDS (t
) = prev
;
5323 finish_struct (t
, attributes
)
5326 const char *saved_filename
= input_filename
;
5327 int saved_lineno
= lineno
;
5329 /* Now that we've got all the field declarations, reverse everything
5331 unreverse_member_declarations (t
);
5333 cplus_decl_attributes (t
, attributes
, NULL_TREE
);
5335 /* Nadger the current location so that diagnostics point to the start of
5336 the struct, not the end. */
5337 input_filename
= DECL_SOURCE_FILE (TYPE_NAME (t
));
5338 lineno
= DECL_SOURCE_LINE (TYPE_NAME (t
));
5340 if (processing_template_decl
)
5342 finish_struct_methods (t
);
5343 TYPE_SIZE (t
) = bitsize_zero_node
;
5346 finish_struct_1 (t
);
5348 input_filename
= saved_filename
;
5349 lineno
= saved_lineno
;
5351 TYPE_BEING_DEFINED (t
) = 0;
5353 if (current_class_type
)
5356 error ("trying to finish struct, but kicked out due to previous parse errors.");
5358 if (processing_template_decl
)
5360 tree scope
= current_scope ();
5361 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
5362 add_stmt (build_min (TAG_DEFN
, t
));
5368 /* Return the dynamic type of INSTANCE, if known.
5369 Used to determine whether the virtual function table is needed
5372 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5373 of our knowledge of its type. *NONNULL should be initialized
5374 before this function is called. */
5377 fixed_type_or_null (instance
, nonnull
, cdtorp
)
5382 switch (TREE_CODE (instance
))
5385 /* Check that we are not going through a cast of some sort. */
5386 if (TREE_TYPE (instance
)
5387 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance
, 0))))
5388 instance
= TREE_OPERAND (instance
, 0);
5389 /* fall through... */
5391 /* This is a call to a constructor, hence it's never zero. */
5392 if (TREE_HAS_CONSTRUCTOR (instance
))
5396 return TREE_TYPE (instance
);
5401 /* This is a call to a constructor, hence it's never zero. */
5402 if (TREE_HAS_CONSTRUCTOR (instance
))
5406 return TREE_TYPE (instance
);
5408 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
, cdtorp
);
5415 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
5416 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
, cdtorp
);
5417 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
5418 /* Propagate nonnull. */
5419 fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
, cdtorp
);
5424 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
, cdtorp
);
5429 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
, cdtorp
);
5432 return fixed_type_or_null (TREE_OPERAND (instance
, 1), nonnull
, cdtorp
);
5436 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
5437 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance
))))
5441 return TREE_TYPE (TREE_TYPE (instance
));
5443 /* fall through... */
5446 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
5450 return TREE_TYPE (instance
);
5452 else if (instance
== current_class_ptr
)
5457 /* if we're in a ctor or dtor, we know our type. */
5458 if (DECL_LANG_SPECIFIC (current_function_decl
)
5459 && (DECL_CONSTRUCTOR_P (current_function_decl
)
5460 || DECL_DESTRUCTOR_P (current_function_decl
)))
5464 return TREE_TYPE (TREE_TYPE (instance
));
5467 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
5469 /* Reference variables should be references to objects. */
5480 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5481 to the static type. We also handle the case where INSTANCE is really
5482 a pointer. Return negative if this is a ctor/dtor. There the dynamic type
5483 is known, but this might not be the most derived base of the original object,
5484 and hence virtual bases may not be layed out according to this type.
5486 Used to determine whether the virtual function table is needed
5489 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5490 of our knowledge of its type. *NONNULL should be initialized
5491 before this function is called. */
5494 resolves_to_fixed_type_p (instance
, nonnull
)
5498 tree t
= TREE_TYPE (instance
);
5501 tree fixed
= fixed_type_or_null (instance
, nonnull
, &cdtorp
);
5502 if (fixed
== NULL_TREE
)
5504 if (POINTER_TYPE_P (t
))
5506 if (!same_type_ignoring_top_level_qualifiers_p (t
, fixed
))
5508 return cdtorp
? -1 : 1;
5513 init_class_processing ()
5515 current_class_depth
= 0;
5516 current_class_stack_size
= 10;
5518 = (class_stack_node_t
) xmalloc (current_class_stack_size
5519 * sizeof (struct class_stack_node
));
5520 VARRAY_TREE_INIT (local_classes
, 8, "local_classes");
5521 ggc_add_tree_varray_root (&local_classes
, 1);
5523 access_default_node
= build_int_2 (0, 0);
5524 access_public_node
= build_int_2 (ak_public
, 0);
5525 access_protected_node
= build_int_2 (ak_protected
, 0);
5526 access_private_node
= build_int_2 (ak_private
, 0);
5527 access_default_virtual_node
= build_int_2 (4, 0);
5528 access_public_virtual_node
= build_int_2 (4 | ak_public
, 0);
5529 access_protected_virtual_node
= build_int_2 (4 | ak_protected
, 0);
5530 access_private_virtual_node
= build_int_2 (4 | ak_private
, 0);
5532 ridpointers
[(int) RID_PUBLIC
] = access_public_node
;
5533 ridpointers
[(int) RID_PRIVATE
] = access_private_node
;
5534 ridpointers
[(int) RID_PROTECTED
] = access_protected_node
;
5537 /* Set current scope to NAME. CODE tells us if this is a
5538 STRUCT, UNION, or ENUM environment.
5540 NAME may end up being NULL_TREE if this is an anonymous or
5541 late-bound struct (as in "struct { ... } foo;") */
5543 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5544 appropriate values, found by looking up the type definition of
5547 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5548 which can be seen locally to the class. They are shadowed by
5549 any subsequent local declaration (including parameter names).
5551 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5552 which have static meaning (i.e., static members, static
5553 member functions, enum declarations, etc).
5555 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5556 which can be seen locally to the class (as in 1), but
5557 know that we are doing this for declaration purposes
5558 (i.e. friend foo::bar (int)).
5560 So that we may avoid calls to lookup_name, we cache the _TYPE
5561 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5563 For multiple inheritance, we perform a two-pass depth-first search
5564 of the type lattice. The first pass performs a pre-order search,
5565 marking types after the type has had its fields installed in
5566 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5567 unmarks the marked types. If a field or member function name
5568 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5569 that name becomes `error_mark_node'. */
5572 pushclass (type
, modify
)
5576 type
= TYPE_MAIN_VARIANT (type
);
5578 /* Make sure there is enough room for the new entry on the stack. */
5579 if (current_class_depth
+ 1 >= current_class_stack_size
)
5581 current_class_stack_size
*= 2;
5583 = (class_stack_node_t
) xrealloc (current_class_stack
,
5584 current_class_stack_size
5585 * sizeof (struct class_stack_node
));
5588 /* Insert a new entry on the class stack. */
5589 current_class_stack
[current_class_depth
].name
= current_class_name
;
5590 current_class_stack
[current_class_depth
].type
= current_class_type
;
5591 current_class_stack
[current_class_depth
].access
= current_access_specifier
;
5592 current_class_stack
[current_class_depth
].names_used
= 0;
5593 current_class_depth
++;
5595 /* Now set up the new type. */
5596 current_class_name
= TYPE_NAME (type
);
5597 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
5598 current_class_name
= DECL_NAME (current_class_name
);
5599 current_class_type
= type
;
5601 /* By default, things in classes are private, while things in
5602 structures or unions are public. */
5603 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
5604 ? access_private_node
5605 : access_public_node
);
5607 if (previous_class_type
!= NULL_TREE
5608 && (type
!= previous_class_type
5609 || !COMPLETE_TYPE_P (previous_class_type
))
5610 && current_class_depth
== 1)
5612 /* Forcibly remove any old class remnants. */
5613 invalidate_class_lookup_cache ();
5616 /* If we're about to enter a nested class, clear
5617 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5618 if (modify
&& current_class_depth
> 1)
5619 clear_identifier_class_values ();
5625 if (type
!= previous_class_type
|| current_class_depth
> 1)
5626 push_class_decls (type
);
5631 /* We are re-entering the same class we just left, so we
5632 don't have to search the whole inheritance matrix to find
5633 all the decls to bind again. Instead, we install the
5634 cached class_shadowed list, and walk through it binding
5635 names and setting up IDENTIFIER_TYPE_VALUEs. */
5636 set_class_shadows (previous_class_values
);
5637 for (item
= previous_class_values
; item
; item
= TREE_CHAIN (item
))
5639 tree id
= TREE_PURPOSE (item
);
5640 tree decl
= TREE_TYPE (item
);
5642 push_class_binding (id
, decl
);
5643 if (TREE_CODE (decl
) == TYPE_DECL
)
5644 set_identifier_type_value (id
, TREE_TYPE (decl
));
5646 unuse_fields (type
);
5649 storetags (CLASSTYPE_TAGS (type
));
5653 /* When we exit a toplevel class scope, we save the
5654 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5655 reenter the class. Here, we've entered some other class, so we
5656 must invalidate our cache. */
5659 invalidate_class_lookup_cache ()
5663 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5664 for (t
= previous_class_values
; t
; t
= TREE_CHAIN (t
))
5665 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t
)) = NULL_TREE
;
5667 previous_class_values
= NULL_TREE
;
5668 previous_class_type
= NULL_TREE
;
5671 /* Get out of the current class scope. If we were in a class scope
5672 previously, that is the one popped to. */
5678 /* Since poplevel_class does the popping of class decls nowadays,
5679 this really only frees the obstack used for these decls. */
5682 current_class_depth
--;
5683 current_class_name
= current_class_stack
[current_class_depth
].name
;
5684 current_class_type
= current_class_stack
[current_class_depth
].type
;
5685 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
5686 if (current_class_stack
[current_class_depth
].names_used
)
5687 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
5690 /* Returns 1 if current_class_type is either T or a nested type of T.
5691 We start looking from 1 because entry 0 is from global scope, and has
5695 currently_open_class (t
)
5699 if (t
== current_class_type
)
5701 for (i
= 1; i
< current_class_depth
; ++i
)
5702 if (current_class_stack
[i
].type
== t
)
5707 /* If either current_class_type or one of its enclosing classes are derived
5708 from T, return the appropriate type. Used to determine how we found
5709 something via unqualified lookup. */
5712 currently_open_derived_class (t
)
5717 if (DERIVED_FROM_P (t
, current_class_type
))
5718 return current_class_type
;
5720 for (i
= current_class_depth
- 1; i
> 0; --i
)
5721 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
5722 return current_class_stack
[i
].type
;
5727 /* When entering a class scope, all enclosing class scopes' names with
5728 static meaning (static variables, static functions, types and enumerators)
5729 have to be visible. This recursive function calls pushclass for all
5730 enclosing class contexts until global or a local scope is reached.
5731 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5732 formal of the same name. */
5735 push_nested_class (type
, modify
)
5741 /* A namespace might be passed in error cases, like A::B:C. */
5742 if (type
== NULL_TREE
5743 || type
== error_mark_node
5744 || TREE_CODE (type
) == NAMESPACE_DECL
5745 || ! IS_AGGR_TYPE (type
)
5746 || TREE_CODE (type
) == TEMPLATE_TYPE_PARM
5747 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
5750 context
= DECL_CONTEXT (TYPE_MAIN_DECL (type
));
5752 if (context
&& CLASS_TYPE_P (context
))
5753 push_nested_class (context
, 2);
5754 pushclass (type
, modify
);
5757 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5762 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
5765 if (context
&& CLASS_TYPE_P (context
))
5766 pop_nested_class ();
5769 /* Set global variables CURRENT_LANG_NAME to appropriate value
5770 so that behavior of name-mangling machinery is correct. */
5773 push_lang_context (name
)
5776 *current_lang_stack
++ = current_lang_name
;
5777 if (current_lang_stack
- &VARRAY_TREE (current_lang_base
, 0)
5778 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base
))
5780 size_t old_size
= VARRAY_SIZE (current_lang_base
);
5782 VARRAY_GROW (current_lang_base
, old_size
+ 10);
5783 current_lang_stack
= &VARRAY_TREE (current_lang_base
, old_size
);
5786 if (name
== lang_name_cplusplus
)
5788 current_lang_name
= name
;
5790 else if (name
== lang_name_java
)
5792 current_lang_name
= name
;
5793 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5794 (See record_builtin_java_type in decl.c.) However, that causes
5795 incorrect debug entries if these types are actually used.
5796 So we re-enable debug output after extern "Java". */
5797 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node
)) = 0;
5798 DECL_IGNORED_P (TYPE_NAME (java_short_type_node
)) = 0;
5799 DECL_IGNORED_P (TYPE_NAME (java_int_type_node
)) = 0;
5800 DECL_IGNORED_P (TYPE_NAME (java_long_type_node
)) = 0;
5801 DECL_IGNORED_P (TYPE_NAME (java_float_type_node
)) = 0;
5802 DECL_IGNORED_P (TYPE_NAME (java_double_type_node
)) = 0;
5803 DECL_IGNORED_P (TYPE_NAME (java_char_type_node
)) = 0;
5804 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node
)) = 0;
5806 else if (name
== lang_name_c
)
5808 current_lang_name
= name
;
5811 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name
));
5814 /* Get out of the current language scope. */
5819 /* Clear the current entry so that garbage collector won't hold on
5821 *current_lang_stack
= NULL_TREE
;
5822 current_lang_name
= *--current_lang_stack
;
5825 /* Type instantiation routines. */
5827 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5828 matches the TARGET_TYPE. If there is no satisfactory match, return
5829 error_mark_node, and issue an error message if COMPLAIN is
5830 non-zero. Permit pointers to member function if PTRMEM is non-zero.
5831 If TEMPLATE_ONLY, the name of the overloaded function
5832 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5833 template arguments. */
5836 resolve_address_of_overloaded_function (target_type
,
5847 tree explicit_targs
;
5849 /* Here's what the standard says:
5853 If the name is a function template, template argument deduction
5854 is done, and if the argument deduction succeeds, the deduced
5855 arguments are used to generate a single template function, which
5856 is added to the set of overloaded functions considered.
5858 Non-member functions and static member functions match targets of
5859 type "pointer-to-function" or "reference-to-function." Nonstatic
5860 member functions match targets of type "pointer-to-member
5861 function;" the function type of the pointer to member is used to
5862 select the member function from the set of overloaded member
5863 functions. If a nonstatic member function is selected, the
5864 reference to the overloaded function name is required to have the
5865 form of a pointer to member as described in 5.3.1.
5867 If more than one function is selected, any template functions in
5868 the set are eliminated if the set also contains a non-template
5869 function, and any given template function is eliminated if the
5870 set contains a second template function that is more specialized
5871 than the first according to the partial ordering rules 14.5.5.2.
5872 After such eliminations, if any, there shall remain exactly one
5873 selected function. */
5876 int is_reference
= 0;
5877 /* We store the matches in a TREE_LIST rooted here. The functions
5878 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5879 interoperability with most_specialized_instantiation. */
5880 tree matches
= NULL_TREE
;
5883 /* By the time we get here, we should be seeing only real
5884 pointer-to-member types, not the internal POINTER_TYPE to
5885 METHOD_TYPE representation. */
5886 my_friendly_assert (!(TREE_CODE (target_type
) == POINTER_TYPE
5887 && (TREE_CODE (TREE_TYPE (target_type
))
5888 == METHOD_TYPE
)), 0);
5890 if (TREE_CODE (overload
) == COMPONENT_REF
)
5891 overload
= TREE_OPERAND (overload
, 1);
5893 /* Check that the TARGET_TYPE is reasonable. */
5894 if (TYPE_PTRFN_P (target_type
))
5896 else if (TYPE_PTRMEMFUNC_P (target_type
))
5897 /* This is OK, too. */
5899 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5901 /* This is OK, too. This comes from a conversion to reference
5903 target_type
= build_reference_type (target_type
);
5910 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5911 DECL_NAME (OVL_FUNCTION (overload
)), target_type
);
5912 return error_mark_node
;
5915 /* If we can find a non-template function that matches, we can just
5916 use it. There's no point in generating template instantiations
5917 if we're just going to throw them out anyhow. But, of course, we
5918 can only do this when we don't *need* a template function. */
5923 for (fns
= overload
; fns
; fns
= OVL_CHAIN (fns
))
5925 tree fn
= OVL_FUNCTION (fns
);
5928 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
5929 /* We're not looking for templates just yet. */
5932 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
5934 /* We're looking for a non-static member, and this isn't
5935 one, or vice versa. */
5938 /* See if there's a match. */
5939 fntype
= TREE_TYPE (fn
);
5941 fntype
= build_ptrmemfunc_type (build_pointer_type (fntype
));
5942 else if (!is_reference
)
5943 fntype
= build_pointer_type (fntype
);
5945 if (can_convert_arg (target_type
, fntype
, fn
))
5946 matches
= tree_cons (fn
, NULL_TREE
, matches
);
5950 /* Now, if we've already got a match (or matches), there's no need
5951 to proceed to the template functions. But, if we don't have a
5952 match we need to look at them, too. */
5955 tree target_fn_type
;
5956 tree target_arg_types
;
5957 tree target_ret_type
;
5962 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type
));
5964 target_fn_type
= TREE_TYPE (target_type
);
5965 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
5966 target_ret_type
= TREE_TYPE (target_fn_type
);
5968 /* Never do unification on the 'this' parameter. */
5969 if (TREE_CODE (target_fn_type
) == METHOD_TYPE
)
5970 target_arg_types
= TREE_CHAIN (target_arg_types
);
5972 for (fns
= overload
; fns
; fns
= OVL_CHAIN (fns
))
5974 tree fn
= OVL_FUNCTION (fns
);
5976 tree instantiation_type
;
5979 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
5980 /* We're only looking for templates. */
5983 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
5985 /* We're not looking for a non-static member, and this is
5986 one, or vice versa. */
5989 /* Try to do argument deduction. */
5990 targs
= make_tree_vec (DECL_NTPARMS (fn
));
5991 if (fn_type_unification (fn
, explicit_targs
, targs
,
5992 target_arg_types
, target_ret_type
,
5993 DEDUCE_EXACT
, -1) != 0)
5994 /* Argument deduction failed. */
5997 /* Instantiate the template. */
5998 instantiation
= instantiate_template (fn
, targs
);
5999 if (instantiation
== error_mark_node
)
6000 /* Instantiation failed. */
6003 /* See if there's a match. */
6004 instantiation_type
= TREE_TYPE (instantiation
);
6006 instantiation_type
=
6007 build_ptrmemfunc_type (build_pointer_type (instantiation_type
));
6008 else if (!is_reference
)
6009 instantiation_type
= build_pointer_type (instantiation_type
);
6010 if (can_convert_arg (target_type
, instantiation_type
, instantiation
))
6011 matches
= tree_cons (instantiation
, fn
, matches
);
6014 /* Now, remove all but the most specialized of the matches. */
6017 tree match
= most_specialized_instantiation (matches
);
6019 if (match
!= error_mark_node
)
6020 matches
= tree_cons (match
, NULL_TREE
, NULL_TREE
);
6024 /* Now we should have exactly one function in MATCHES. */
6025 if (matches
== NULL_TREE
)
6027 /* There were *no* matches. */
6030 cp_error ("no matches converting function `%D' to type `%#T'",
6031 DECL_NAME (OVL_FUNCTION (overload
)),
6034 /* print_candidates expects a chain with the functions in
6035 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6036 so why be clever?). */
6037 for (; overload
; overload
= OVL_NEXT (overload
))
6038 matches
= tree_cons (NULL_TREE
, OVL_CURRENT (overload
),
6041 print_candidates (matches
);
6043 return error_mark_node
;
6045 else if (TREE_CHAIN (matches
))
6047 /* There were too many matches. */
6053 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6054 DECL_NAME (OVL_FUNCTION (overload
)),
6057 /* Since print_candidates expects the functions in the
6058 TREE_VALUE slot, we flip them here. */
6059 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
6060 TREE_VALUE (match
) = TREE_PURPOSE (match
);
6062 print_candidates (matches
);
6065 return error_mark_node
;
6068 /* Good, exactly one match. Now, convert it to the correct type. */
6069 fn
= TREE_PURPOSE (matches
);
6071 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
6072 && !ptrmem
&& !flag_ms_extensions
)
6074 static int explained
;
6077 return error_mark_node
;
6079 cp_pedwarn ("assuming pointer to member `%D'", fn
);
6082 cp_pedwarn ("(a pointer to member can only be formed with `&%E')", fn
);
6088 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
6089 return build_unary_op (ADDR_EXPR
, fn
, 0);
6092 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6093 will mark the function as addressed, but here we must do it
6095 mark_addressable (fn
);
6101 /* This function will instantiate the type of the expression given in
6102 RHS to match the type of LHSTYPE. If errors exist, then return
6103 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6104 we complain on errors. If we are not complaining, never modify rhs,
6105 as overload resolution wants to try many possible instantiations, in
6106 the hope that at least one will work.
6108 For non-recursive calls, LHSTYPE should be a function, pointer to
6109 function, or a pointer to member function. */
6112 instantiate_type (lhstype
, rhs
, flags
)
6114 enum instantiate_type_flags flags
;
6116 int complain
= (flags
& itf_complain
);
6117 int strict
= (flags
& itf_no_attributes
)
6118 ? COMPARE_NO_ATTRIBUTES
: COMPARE_STRICT
;
6119 int allow_ptrmem
= flags
& itf_ptrmem_ok
;
6121 flags
&= ~itf_ptrmem_ok
;
6123 if (TREE_CODE (lhstype
) == UNKNOWN_TYPE
)
6126 error ("not enough type information");
6127 return error_mark_node
;
6130 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
6132 if (comptypes (lhstype
, TREE_TYPE (rhs
), strict
))
6135 cp_error ("argument of type `%T' does not match `%T'",
6136 TREE_TYPE (rhs
), lhstype
);
6137 return error_mark_node
;
6140 /* We don't overwrite rhs if it is an overloaded function.
6141 Copying it would destroy the tree link. */
6142 if (TREE_CODE (rhs
) != OVERLOAD
)
6143 rhs
= copy_node (rhs
);
6145 /* This should really only be used when attempting to distinguish
6146 what sort of a pointer to function we have. For now, any
6147 arithmetic operation which is not supported on pointers
6148 is rejected as an error. */
6150 switch (TREE_CODE (rhs
))
6157 my_friendly_abort (177);
6158 return error_mark_node
;
6165 new_rhs
= instantiate_type (build_pointer_type (lhstype
),
6166 TREE_OPERAND (rhs
, 0), flags
);
6167 if (new_rhs
== error_mark_node
)
6168 return error_mark_node
;
6170 TREE_TYPE (rhs
) = lhstype
;
6171 TREE_OPERAND (rhs
, 0) = new_rhs
;
6176 rhs
= copy_node (TREE_OPERAND (rhs
, 0));
6177 TREE_TYPE (rhs
) = unknown_type_node
;
6178 return instantiate_type (lhstype
, rhs
, flags
);
6181 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6184 rhs
= TREE_OPERAND (rhs
, 1);
6185 if (BASELINK_P (rhs
))
6186 return instantiate_type (lhstype
, TREE_VALUE (rhs
),
6187 flags
| allow_ptrmem
);
6189 /* This can happen if we are forming a pointer-to-member for a
6191 my_friendly_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
, 0);
6195 case TEMPLATE_ID_EXPR
:
6197 tree fns
= TREE_OPERAND (rhs
, 0);
6198 tree args
= TREE_OPERAND (rhs
, 1);
6201 resolve_address_of_overloaded_function (lhstype
,
6205 /*template_only=*/1,
6211 resolve_address_of_overloaded_function (lhstype
,
6215 /*template_only=*/0,
6216 /*explicit_targs=*/NULL_TREE
);
6219 /* Now we should have a baselink. */
6220 my_friendly_assert (BASELINK_P (rhs
), 990412);
6222 return instantiate_type (lhstype
, TREE_VALUE (rhs
), flags
);
6225 /* This is too hard for now. */
6226 my_friendly_abort (183);
6227 return error_mark_node
;
6232 TREE_OPERAND (rhs
, 0)
6233 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), flags
);
6234 if (TREE_OPERAND (rhs
, 0) == error_mark_node
)
6235 return error_mark_node
;
6236 TREE_OPERAND (rhs
, 1)
6237 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6238 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
6239 return error_mark_node
;
6241 TREE_TYPE (rhs
) = lhstype
;
6245 case TRUNC_DIV_EXPR
:
6246 case FLOOR_DIV_EXPR
:
6248 case ROUND_DIV_EXPR
:
6250 case TRUNC_MOD_EXPR
:
6251 case FLOOR_MOD_EXPR
:
6253 case ROUND_MOD_EXPR
:
6254 case FIX_ROUND_EXPR
:
6255 case FIX_FLOOR_EXPR
:
6257 case FIX_TRUNC_EXPR
:
6273 case PREINCREMENT_EXPR
:
6274 case PREDECREMENT_EXPR
:
6275 case POSTINCREMENT_EXPR
:
6276 case POSTDECREMENT_EXPR
:
6278 error ("invalid operation on uninstantiated type");
6279 return error_mark_node
;
6281 case TRUTH_AND_EXPR
:
6283 case TRUTH_XOR_EXPR
:
6290 case TRUTH_ANDIF_EXPR
:
6291 case TRUTH_ORIF_EXPR
:
6292 case TRUTH_NOT_EXPR
:
6294 error ("not enough type information");
6295 return error_mark_node
;
6298 if (type_unknown_p (TREE_OPERAND (rhs
, 0)))
6301 error ("not enough type information");
6302 return error_mark_node
;
6304 TREE_OPERAND (rhs
, 1)
6305 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6306 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
6307 return error_mark_node
;
6308 TREE_OPERAND (rhs
, 2)
6309 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 2), flags
);
6310 if (TREE_OPERAND (rhs
, 2) == error_mark_node
)
6311 return error_mark_node
;
6313 TREE_TYPE (rhs
) = lhstype
;
6317 TREE_OPERAND (rhs
, 1)
6318 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6319 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
6320 return error_mark_node
;
6322 TREE_TYPE (rhs
) = lhstype
;
6327 if (PTRMEM_OK_P (rhs
))
6328 flags
|= itf_ptrmem_ok
;
6330 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), flags
);
6332 case ENTRY_VALUE_EXPR
:
6333 my_friendly_abort (184);
6334 return error_mark_node
;
6337 return error_mark_node
;
6340 my_friendly_abort (185);
6341 return error_mark_node
;
6345 /* Return the name of the virtual function pointer field
6346 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6347 this may have to look back through base types to find the
6348 ultimate field name. (For single inheritance, these could
6349 all be the same name. Who knows for multiple inheritance). */
6352 get_vfield_name (type
)
6355 tree binfo
= TYPE_BINFO (type
);
6358 while (BINFO_BASETYPES (binfo
)
6359 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo
, 0)))
6360 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo
, 0)))
6361 binfo
= BINFO_BASETYPE (binfo
, 0);
6363 type
= BINFO_TYPE (binfo
);
6364 buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
6365 + TYPE_NAME_LENGTH (type
) + 2);
6366 sprintf (buf
, VFIELD_NAME_FORMAT
, TYPE_NAME_STRING (type
));
6367 return get_identifier (buf
);
6371 print_class_statistics ()
6373 #ifdef GATHER_STATISTICS
6374 fprintf (stderr
, "convert_harshness = %d\n", n_convert_harshness
);
6375 fprintf (stderr
, "compute_conversion_costs = %d\n", n_compute_conversion_costs
);
6376 fprintf (stderr
, "build_method_call = %d (inner = %d)\n",
6377 n_build_method_call
, n_inner_fields_searched
);
6380 fprintf (stderr
, "vtables = %d; vtable searches = %d\n",
6381 n_vtables
, n_vtable_searches
);
6382 fprintf (stderr
, "vtable entries = %d; vtable elems = %d\n",
6383 n_vtable_entries
, n_vtable_elems
);
6388 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6389 according to [class]:
6390 The class-name is also inserted
6391 into the scope of the class itself. For purposes of access checking,
6392 the inserted class name is treated as if it were a public member name. */
6395 build_self_reference ()
6397 tree name
= constructor_name (current_class_type
);
6398 tree value
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
6401 DECL_NONLOCAL (value
) = 1;
6402 DECL_CONTEXT (value
) = current_class_type
;
6403 DECL_ARTIFICIAL (value
) = 1;
6405 if (processing_template_decl
)
6406 value
= push_template_decl (value
);
6408 saved_cas
= current_access_specifier
;
6409 current_access_specifier
= access_public_node
;
6410 finish_member_declaration (value
);
6411 current_access_specifier
= saved_cas
;
6414 /* Returns 1 if TYPE contains only padding bytes. */
6417 is_empty_class (type
)
6420 if (type
== error_mark_node
)
6423 if (! IS_AGGR_TYPE (type
))
6426 return integer_zerop (CLASSTYPE_SIZE (type
));
6429 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6430 a *_TYPE node. NODE can also be a local class. */
6433 get_enclosing_class (type
)
6438 while (node
&& TREE_CODE (node
) != NAMESPACE_DECL
)
6440 switch (TREE_CODE_CLASS (TREE_CODE (node
)))
6443 node
= DECL_CONTEXT (node
);
6449 node
= TYPE_CONTEXT (node
);
6453 my_friendly_abort (0);
6459 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6462 is_base_of_enclosing_class (base
, type
)
6467 if (get_binfo (base
, type
, 0))
6470 type
= get_enclosing_class (type
);
6475 /* Note that NAME was looked up while the current class was being
6476 defined and that the result of that lookup was DECL. */
6479 maybe_note_name_used_in_class (name
, decl
)
6483 splay_tree names_used
;
6485 /* If we're not defining a class, there's nothing to do. */
6486 if (!current_class_type
|| !TYPE_BEING_DEFINED (current_class_type
))
6489 /* If there's already a binding for this NAME, then we don't have
6490 anything to worry about. */
6491 if (IDENTIFIER_CLASS_VALUE (name
))
6494 if (!current_class_stack
[current_class_depth
- 1].names_used
)
6495 current_class_stack
[current_class_depth
- 1].names_used
6496 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
6497 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
6499 splay_tree_insert (names_used
,
6500 (splay_tree_key
) name
,
6501 (splay_tree_value
) decl
);
6504 /* Note that NAME was declared (as DECL) in the current class. Check
6505 to see that the declaration is legal. */
6508 note_name_declared_in_class (name
, decl
)
6512 splay_tree names_used
;
6515 /* Look to see if we ever used this name. */
6517 = current_class_stack
[current_class_depth
- 1].names_used
;
6521 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
6524 /* [basic.scope.class]
6526 A name N used in a class S shall refer to the same declaration
6527 in its context and when re-evaluated in the completed scope of
6529 cp_error ("declaration of `%#D'", decl
);
6530 cp_error_at ("changes meaning of `%D' from `%+#D'",
6531 DECL_NAME (OVL_CURRENT (decl
)),
6536 /* Returns the VAR_DECL for the complete vtable associated with
6537 BINFO. (Under the new ABI, secondary vtables are merged with
6538 primary vtables; this function will return the VAR_DECL for the
6542 get_vtbl_decl_for_binfo (binfo
)
6547 decl
= BINFO_VTABLE (binfo
);
6548 if (decl
&& TREE_CODE (decl
) == PLUS_EXPR
)
6550 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl
, 0)) == ADDR_EXPR
,
6552 decl
= TREE_OPERAND (TREE_OPERAND (decl
, 0), 0);
6555 my_friendly_assert (TREE_CODE (decl
) == VAR_DECL
, 20000403);
6559 /* Called from get_primary_binfo via dfs_walk. */
6562 dfs_get_primary_binfo (binfo
, data
)
6566 tree primary_base
= (tree
) data
;
6568 if (TREE_VIA_VIRTUAL (binfo
)
6569 && same_type_p (BINFO_TYPE (binfo
), BINFO_TYPE (primary_base
)))
6575 /* Returns the binfo for the primary base of BINFO. Note that in a
6576 complex hierarchy the resulting BINFO may not actually *be*
6577 primary. In particular if the resulting BINFO is a virtual base,
6578 and it occurs elsewhere in the hierarchy, then this occurrence may
6579 not actually be a primary base in the complete object. Check
6580 BINFO_PRIMARY_P to be sure. */
6583 get_primary_binfo (binfo
)
6589 primary_base
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo
));
6593 /* A non-virtual primary base is always a direct base, and easy to
6595 if (!TREE_VIA_VIRTUAL (primary_base
))
6599 /* Scan the direct basetypes until we find a base with the same
6600 type as the primary base. */
6601 for (i
= 0; i
< BINFO_N_BASETYPES (binfo
); ++i
)
6603 tree base_binfo
= BINFO_BASETYPE (binfo
, i
);
6605 if (same_type_p (BINFO_TYPE (base_binfo
),
6606 BINFO_TYPE (primary_base
)))
6610 /* We should always find the primary base. */
6611 my_friendly_abort (20000729);
6614 /* For a primary virtual base, we have to scan the entire hierarchy
6615 rooted at BINFO; the virtual base could be an indirect virtual
6617 result
= dfs_walk (binfo
, dfs_get_primary_binfo
, NULL
, primary_base
);
6618 my_friendly_assert (result
!= NULL_TREE
, 20000730);
6622 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6623 dominated by T) to stderr. INDENT should be zero when called from
6624 the top level; it is incremented recursively. */
6627 dump_class_hierarchy_r (stream
, t
, binfo
, indent
)
6635 fprintf (stream
, "%*s0x%lx (%s) ", indent
, "",
6636 (unsigned long) binfo
,
6637 type_as_string (binfo
, TFF_PLAIN_IDENTIFIER
));
6638 fprintf (stream
, HOST_WIDE_INT_PRINT_DEC
,
6639 tree_low_cst (BINFO_OFFSET (binfo
), 0));
6640 if (is_empty_class (BINFO_TYPE (binfo
)))
6641 fprintf (stream
, " empty");
6642 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo
)))
6643 fprintf (stream
, " nearly-empty");
6644 if (TREE_VIA_VIRTUAL (binfo
))
6646 tree canonical
= binfo_for_vbase (BINFO_TYPE (binfo
), t
);
6648 if (canonical
== binfo
)
6649 fprintf (stream
, " virtual-canonical");
6651 fprintf (stream
, " virtual-non-canonical");
6653 if (BINFO_PRIMARY_P (binfo
))
6654 fprintf (stream
, " primary-for 0x%lx (%s)",
6655 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo
),
6656 type_as_string (BINFO_PRIMARY_BASE_OF (binfo
), TFF_PLAIN_IDENTIFIER
));
6657 if (BINFO_LOST_PRIMARY_P (binfo
))
6658 fprintf (stream
, " lost-primary");
6659 fprintf (stream
, "\n");
6661 for (i
= 0; i
< BINFO_N_BASETYPES (binfo
); ++i
)
6662 dump_class_hierarchy_r (stream
, t
, BINFO_BASETYPE (binfo
, i
), indent
+ 2);
6665 /* Dump the BINFO hierarchy for T. */
6668 dump_class_hierarchy (name
, t
)
6672 FILE *stream
= stderr
;
6676 static int append
= 0;
6678 stream
= fopen (name
, append
++ ? "a" : "w");
6680 error ("could not open dump file `%s'", name
);
6683 fprintf (stream
, "%s\n",
6684 type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
6685 dump_class_hierarchy_r (stream
, t
, TYPE_BINFO (t
), 0);
6686 fprintf (stream
, "\n");
6691 /* Virtual function table initialization. */
6693 /* Create all the necessary vtables for T and its base classes. */
6699 if (merge_primary_and_secondary_vtables_p ())
6704 /* Under the new ABI, we lay out the primary and secondary
6705 vtables in one contiguous vtable. The primary vtable is
6706 first, followed by the non-virtual secondary vtables in
6707 inheritance graph order. */
6708 list
= build_tree_list (TYPE_BINFO_VTABLE (t
), NULL_TREE
);
6709 accumulate_vtbl_inits (TYPE_BINFO (t
), TYPE_BINFO (t
),
6710 TYPE_BINFO (t
), t
, list
);
6711 /* Then come the virtual bases, also in inheritance graph
6713 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
6717 if (!TREE_VIA_VIRTUAL (vbase
))
6720 /* Although we walk in inheritance order, that might not get the
6722 real_base
= binfo_for_vbase (BINFO_TYPE (vbase
), t
);
6724 accumulate_vtbl_inits (real_base
, real_base
,
6725 TYPE_BINFO (t
), t
, list
);
6728 if (TYPE_BINFO_VTABLE (t
))
6729 initialize_vtable (TYPE_BINFO (t
), TREE_VALUE (list
));
6733 dfs_walk (TYPE_BINFO (t
), dfs_finish_vtbls
,
6734 dfs_unmarked_real_bases_queue_p
, t
);
6735 dfs_walk (TYPE_BINFO (t
), dfs_unmark
,
6736 dfs_marked_real_bases_queue_p
, t
);
6740 /* Called from finish_vtbls via dfs_walk. */
6743 dfs_finish_vtbls (binfo
, data
)
6747 tree t
= (tree
) data
;
6749 if (BINFO_NEW_VTABLE_MARKED (binfo
, t
))
6750 initialize_vtable (binfo
,
6751 build_vtbl_initializer (binfo
, binfo
, t
,
6752 TYPE_BINFO (t
), NULL
));
6754 SET_BINFO_MARKED (binfo
);
6759 /* Initialize the vtable for BINFO with the INITS. */
6762 initialize_vtable (binfo
, inits
)
6768 layout_vtable_decl (binfo
, list_length (inits
));
6769 decl
= get_vtbl_decl_for_binfo (binfo
);
6770 initialize_array (decl
, inits
);
6773 /* Initialize DECL (a declaration for a namespace-scope array) with
6777 initialize_array (decl
, inits
)
6783 context
= DECL_CONTEXT (decl
);
6784 DECL_CONTEXT (decl
) = NULL_TREE
;
6785 DECL_INITIAL (decl
) = build_nt (CONSTRUCTOR
, NULL_TREE
, inits
);
6786 cp_finish_decl (decl
, DECL_INITIAL (decl
), NULL_TREE
, 0);
6787 DECL_CONTEXT (decl
) = context
;
6790 /* Build the VTT (virtual table table) for T.
6791 A class requires a VTT if it has virtual bases.
6794 1 - primary virtual pointer for complete object T
6795 2 - secondary VTTs for each direct non-virtual base of T which requires a VTT
6796 3 - secondary virtual pointers for each direct or indirect base of T which
6797 has virtual bases or is reachable via a virtual path from T.
6798 4 - secondary VTTs for each direct or indirect virtual base of T.
6800 Secondary VTTs look like complete object VTTs without part 4. */
6811 /* Build up the initializers for the VTT. */
6813 index
= size_zero_node
;
6814 build_vtt_inits (TYPE_BINFO (t
), t
, &inits
, &index
);
6816 /* If we didn't need a VTT, we're done. */
6820 /* Figure out the type of the VTT. */
6821 type
= build_index_type (size_int (list_length (inits
)));
6822 type
= build_cplus_array_type (const_ptr_type_node
, type
);
6824 /* Now, build the VTT object itself. */
6825 vtt
= build_vtable (t
, get_vtt_name (t
), type
);
6826 pushdecl_top_level (vtt
);
6827 initialize_array (vtt
, inits
);
6830 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
6831 from within some heirarchy which is inherited from the type of BINFO.
6832 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
6836 get_original_base (base_binfo
, binfo
)
6843 if (same_type_p (BINFO_TYPE (base_binfo
), BINFO_TYPE (binfo
)))
6845 if (TREE_VIA_VIRTUAL (base_binfo
))
6846 return binfo_for_vbase (BINFO_TYPE (base_binfo
), BINFO_TYPE (binfo
));
6847 derived
= get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo
), binfo
);
6849 for (ix
= 0; ix
!= BINFO_N_BASETYPES (derived
); ix
++)
6850 if (same_type_p (BINFO_TYPE (base_binfo
),
6851 BINFO_TYPE (BINFO_BASETYPE (derived
, ix
))))
6852 return BINFO_BASETYPE (derived
, ix
);
6853 my_friendly_abort (20010223);
6857 /* Recursively build the VTT-initializer for BINFO (which is in the
6858 hierarchy dominated by T). INITS points to the end of the initializer
6859 list to date. INDEX is the VTT index where the next element will be
6860 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
6861 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
6862 for virtual bases of T. When it is not so, we build the constructor
6863 vtables for the BINFO-in-T variant. */
6866 build_vtt_inits (binfo
, t
, inits
, index
)
6875 tree secondary_vptrs
;
6876 int top_level_p
= same_type_p (TREE_TYPE (binfo
), t
);
6878 /* We only need VTTs for subobjects with virtual bases. */
6879 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo
)))
6882 /* We need to use a construction vtable if this is not the primary
6886 build_ctor_vtbl_group (binfo
, t
);
6888 /* Record the offset in the VTT where this sub-VTT can be found. */
6889 BINFO_SUBVTT_INDEX (binfo
) = *index
;
6892 /* Add the address of the primary vtable for the complete object. */
6893 init
= BINFO_VTABLE (binfo
);
6894 if (TREE_CODE (init
) == TREE_LIST
)
6895 init
= TREE_VALUE (init
);
6896 *inits
= build_tree_list (NULL_TREE
, init
);
6897 inits
= &TREE_CHAIN (*inits
);
6900 my_friendly_assert (!BINFO_VPTR_INDEX (binfo
), 20010129);
6901 BINFO_VPTR_INDEX (binfo
) = *index
;
6903 *index
= size_binop (PLUS_EXPR
, *index
, TYPE_SIZE_UNIT (ptr_type_node
));
6905 /* Recursively add the secondary VTTs for non-virtual bases. */
6906 for (i
= 0; i
< BINFO_N_BASETYPES (binfo
); ++i
)
6908 b
= BINFO_BASETYPE (binfo
, i
);
6909 if (!TREE_VIA_VIRTUAL (b
))
6910 inits
= build_vtt_inits (BINFO_BASETYPE (binfo
, i
), t
,
6914 /* Add secondary virtual pointers for all subobjects of BINFO with
6915 either virtual bases or reachable along a virtual path, except
6916 subobjects that are non-virtual primary bases. */
6917 secondary_vptrs
= tree_cons (t
, NULL_TREE
, BINFO_TYPE (binfo
));
6918 TREE_TYPE (secondary_vptrs
) = *index
;
6919 VTT_TOP_LEVEL_P (secondary_vptrs
) = top_level_p
;
6920 VTT_MARKED_BINFO_P (secondary_vptrs
) = 0;
6922 dfs_walk_real (binfo
,
6923 dfs_build_secondary_vptr_vtt_inits
,
6925 dfs_ctor_vtable_bases_queue_p
,
6927 VTT_MARKED_BINFO_P (secondary_vptrs
) = 1;
6928 dfs_walk (binfo
, dfs_unmark
, dfs_ctor_vtable_bases_queue_p
,
6931 *index
= TREE_TYPE (secondary_vptrs
);
6933 /* The secondary vptrs come back in reverse order. After we reverse
6934 them, and add the INITS, the last init will be the first element
6936 secondary_vptrs
= TREE_VALUE (secondary_vptrs
);
6937 if (secondary_vptrs
)
6939 *inits
= nreverse (secondary_vptrs
);
6940 inits
= &TREE_CHAIN (secondary_vptrs
);
6941 my_friendly_assert (*inits
== NULL_TREE
, 20000517);
6944 /* Add the secondary VTTs for virtual bases. */
6946 for (b
= TYPE_BINFO (BINFO_TYPE (binfo
)); b
; b
= TREE_CHAIN (b
))
6950 if (!TREE_VIA_VIRTUAL (b
))
6953 vbase
= binfo_for_vbase (BINFO_TYPE (b
), t
);
6954 inits
= build_vtt_inits (vbase
, t
, inits
, index
);
6959 tree data
= tree_cons (t
, binfo
, NULL_TREE
);
6960 VTT_TOP_LEVEL_P (data
) = 0;
6961 VTT_MARKED_BINFO_P (data
) = 0;
6963 dfs_walk (binfo
, dfs_fixup_binfo_vtbls
,
6964 dfs_ctor_vtable_bases_queue_p
,
6971 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
6972 for the base in most derived. DATA is a TREE_LIST who's
6973 TREE_CHAIN is the type of the base being
6974 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
6975 flag of DATA indicates that this is a constructor vtable. The
6976 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
6979 dfs_build_secondary_vptr_vtt_inits (binfo
, data
)
6991 top_level_p
= VTT_TOP_LEVEL_P (l
);
6993 SET_BINFO_MARKED (binfo
);
6995 /* We don't care about bases that don't have vtables. */
6996 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
6999 /* We're only interested in proper subobjects of T. */
7000 if (same_type_p (BINFO_TYPE (binfo
), t
))
7003 /* We're not interested in non-virtual primary bases. */
7004 if (!TREE_VIA_VIRTUAL (binfo
) && BINFO_PRIMARY_P (binfo
))
7007 /* If BINFO has virtual bases or is reachable via a virtual path
7008 from T, it'll have a secondary vptr. */
7009 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo
))
7010 && !binfo_via_virtual (binfo
, t
))
7013 /* Record the index where this secondary vptr can be found. */
7014 index
= TREE_TYPE (l
);
7017 my_friendly_assert (!BINFO_VPTR_INDEX (binfo
), 20010129);
7018 BINFO_VPTR_INDEX (binfo
) = index
;
7020 TREE_TYPE (l
) = size_binop (PLUS_EXPR
, index
,
7021 TYPE_SIZE_UNIT (ptr_type_node
));
7023 /* Add the initializer for the secondary vptr itself. */
7024 if (top_level_p
&& TREE_VIA_VIRTUAL (binfo
))
7026 /* It's a primary virtual base, and this is not the construction
7027 vtable. Find the base this is primary of in the inheritance graph,
7028 and use that base's vtable now. */
7029 while (BINFO_PRIMARY_BASE_OF (binfo
))
7030 binfo
= BINFO_PRIMARY_BASE_OF (binfo
);
7032 init
= BINFO_VTABLE (binfo
);
7033 if (TREE_CODE (init
) == TREE_LIST
)
7034 init
= TREE_VALUE (init
);
7035 TREE_VALUE (l
) = tree_cons (NULL_TREE
, init
, TREE_VALUE (l
));
7040 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7041 TREE_UNSIGNED indicates that a constructor vtable is being built.
7042 TREE_USED indicates whether marked or unmarked bases should be walked.
7043 TREE_PURPOSE is the TREE_TYPE that dominates the hierarchy. */
7046 dfs_ctor_vtable_bases_queue_p (binfo
, data
)
7050 if (TREE_VIA_VIRTUAL (binfo
) && !BINFO_PRIMARY_P (binfo
))
7052 tree type
= TREE_PURPOSE ((tree
) data
);
7054 /* This is a non-primary virtual base, get the shared version. */
7055 binfo
= binfo_for_vbase (BINFO_TYPE (binfo
), type
);
7056 if (VTT_TOP_LEVEL_P ((tree
) data
) && BINFO_PRIMARY_P (binfo
))
7059 if (!BINFO_MARKED (binfo
) == VTT_MARKED_BINFO_P ((tree
) data
))
7064 /* Called from build_vtt_inits via dfs_walk. After building constructor
7065 vtables and generating the sub-vtt from them, we need to restore the
7066 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7067 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7070 dfs_fixup_binfo_vtbls (binfo
, data
)
7074 CLEAR_BINFO_MARKED (binfo
);
7076 /* We don't care about bases that don't have vtables. */
7077 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
7080 /* If we scribbled the construction vtable vptr into BINFO, clear it
7082 if (TREE_CODE (BINFO_VTABLE (binfo
)) == TREE_LIST
7083 && (TREE_PURPOSE (BINFO_VTABLE (binfo
))
7084 == TREE_VALUE ((tree
) data
)))
7085 BINFO_VTABLE (binfo
) = TREE_CHAIN (BINFO_VTABLE (binfo
));
7090 /* Build the construction vtable group for BINFO which is in the
7091 hierarchy dominated by T. */
7094 build_ctor_vtbl_group (binfo
, t
)
7105 /* See if we've already create this construction vtable group. */
7106 id
= mangle_ctor_vtbl_for_type (t
, binfo
);
7107 if (IDENTIFIER_GLOBAL_VALUE (id
))
7110 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo
), t
), 20010124);
7111 /* Build a version of VTBL (with the wrong type) for use in
7112 constructing the addresses of secondary vtables in the
7113 construction vtable group. */
7114 vtbl
= build_vtable (t
, id
, ptr_type_node
);
7115 list
= build_tree_list (vtbl
, NULL_TREE
);
7116 accumulate_vtbl_inits (binfo
, TYPE_BINFO (TREE_TYPE (binfo
)),
7119 /* Add the vtables for each of our virtual bases using the vbase in T
7121 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
7123 vbase
= TREE_CHAIN (vbase
))
7128 if (!TREE_VIA_VIRTUAL (vbase
))
7130 b
= binfo_for_vbase (BINFO_TYPE (vbase
), t
);
7131 orig_base
= binfo_for_vbase (BINFO_TYPE (vbase
), BINFO_TYPE (binfo
));
7133 accumulate_vtbl_inits (b
, orig_base
, binfo
, t
, list
);
7135 inits
= TREE_VALUE (list
);
7137 /* Figure out the type of the construction vtable. */
7138 type
= build_index_type (size_int (list_length (inits
)));
7139 type
= build_cplus_array_type (vtable_entry_type
, type
);
7140 TREE_TYPE (vtbl
) = type
;
7142 /* Initialize the construction vtable. */
7143 pushdecl_top_level (vtbl
);
7144 initialize_array (vtbl
, inits
);
7147 /* Add the vtbl initializers for BINFO (and its bases other than
7148 non-virtual primaries) to the list of INITS. BINFO is in the
7149 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7150 the constructor the vtbl inits should be accumulated for. (If this
7151 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7152 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7153 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7154 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7155 but are not necessarily the same in terms of layout. */
7158 accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, t
, inits
)
7166 int ctor_vtbl_p
= !same_type_p (BINFO_TYPE (rtti_binfo
), t
);
7168 my_friendly_assert (same_type_p (BINFO_TYPE (binfo
),
7169 BINFO_TYPE (orig_binfo
)),
7172 /* If we're building a construction vtable, we're not interested in
7173 subobjects that don't require construction vtables. */
7175 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo
))
7176 && !binfo_via_virtual (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
7179 /* Build the initializers for the BINFO-in-T vtable. */
7181 = chainon (TREE_VALUE (inits
),
7182 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
,
7183 rtti_binfo
, t
, inits
));
7185 /* Walk the BINFO and its bases. We walk in preorder so that as we
7186 initialize each vtable we can figure out at what offset the
7187 secondary vtable lies from the primary vtable. We can't use
7188 dfs_walk here because we need to iterate through bases of BINFO
7189 and RTTI_BINFO simultaneously. */
7190 for (i
= 0; i
< BINFO_N_BASETYPES (binfo
); ++i
)
7192 tree base_binfo
= BINFO_BASETYPE (binfo
, i
);
7194 /* Skip virtual bases. */
7195 if (TREE_VIA_VIRTUAL (base_binfo
))
7197 accumulate_vtbl_inits (base_binfo
,
7198 BINFO_BASETYPE (orig_binfo
, i
),
7204 /* Called from accumulate_vtbl_inits when using the new ABI.
7205 Accumulates the vtable initializers for all of the vtables into
7206 TREE_VALUE (DATA). Returns the initializers for the BINFO vtable. */
7209 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, t
, l
)
7216 tree inits
= NULL_TREE
;
7217 tree vtbl
= NULL_TREE
;
7218 int ctor_vtbl_p
= !same_type_p (BINFO_TYPE (rtti_binfo
), t
);
7221 && TREE_VIA_VIRTUAL (orig_binfo
) && BINFO_PRIMARY_P (orig_binfo
))
7223 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a primary
7224 virtual base. If it is not the same primary in the hierarchy of T,
7225 we'll need to generate a ctor vtable for it, to place at its
7226 location in T. If it is the same primary, we still need a VTT
7227 entry for the vtable, but that must be the base it is a
7228 primary for within the sub-hierarchy of RTTI_BINFO. */
7230 tree best_primary
= NULL
;
7233 my_friendly_assert (BINFO_PRIMARY_P (binfo
), 20010131);
7235 for (primary_for
= BINFO_PRIMARY_BASE_OF (binfo
);
7237 primary_for
= BINFO_PRIMARY_BASE_OF (primary_for
))
7239 for (parent
= primary_for
;
7241 parent
= BINFO_INHERITANCE_CHAIN (parent
))
7243 if (parent
== rtti_binfo
)
7245 best_primary
= primary_for
;
7254 vtbl
= BINFO_VTABLE (best_primary
);
7255 if (TREE_CODE (vtbl
) == TREE_LIST
)
7257 my_friendly_assert (TREE_PURPOSE (vtbl
) == rtti_binfo
,
7259 vtbl
= TREE_VALUE (vtbl
);
7263 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
7271 /* Compute the initializer for this vtable. */
7272 inits
= build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
,
7275 /* Figure out the position to which the VPTR should point. */
7276 vtbl
= TREE_PURPOSE (l
);
7277 vtbl
= build1 (ADDR_EXPR
,
7280 index
= size_binop (PLUS_EXPR
,
7281 size_int (non_fn_entries
),
7282 size_int (list_length (TREE_VALUE (l
))));
7283 index
= size_binop (MULT_EXPR
,
7284 TYPE_SIZE_UNIT (vtable_entry_type
),
7286 vtbl
= build (PLUS_EXPR
, TREE_TYPE (vtbl
), vtbl
, index
);
7287 TREE_CONSTANT (vtbl
) = 1;
7292 /* For an ordinary vtable, set BINFO_VTABLE. */
7293 BINFO_VTABLE (binfo
) = vtbl
;
7294 if (BINFO_PRIMARY_P (binfo
) && TREE_VIA_VIRTUAL (binfo
))
7298 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7299 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7300 straighten this out. */
7301 BINFO_VTABLE (binfo
) = tree_cons (rtti_binfo
, vtbl
, BINFO_VTABLE (binfo
));
7306 /* Construct the initializer for BINFOs virtual function table. BINFO
7307 is part of the hierarchy dominated by T. If we're building a
7308 construction vtable, the ORIG_BINFO is the binfo we should use to
7309 find the actual function pointers to put in the vtable - but they
7310 can be overridden on the path to most-derived in the graph that
7311 ORIG_BINFO belongs. Otherwise,
7312 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7313 BINFO that should be indicated by the RTTI information in the
7314 vtable; it will be a base class of T, rather than T itself, if we
7315 are building a construction vtable.
7317 The value returned is a TREE_LIST suitable for wrapping in a
7318 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7319 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7320 number of non-function entries in the vtable.
7322 It might seem that this function should never be called with a
7323 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7324 base is always subsumed by a derived class vtable. However, when
7325 we are building construction vtables, we do build vtables for
7326 primary bases; we need these while the primary base is being
7330 build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
, non_fn_entries_p
)
7335 int *non_fn_entries_p
;
7342 /* Initialize VID. */
7343 memset (&vid
, 0, sizeof (vid
));
7346 vid
.rtti_binfo
= rtti_binfo
;
7347 vid
.last_init
= &vid
.inits
;
7348 vid
.primary_vtbl_p
= (binfo
== TYPE_BINFO (t
));
7349 vid
.ctor_vtbl_p
= !same_type_p (BINFO_TYPE (rtti_binfo
), t
);
7350 /* The first vbase or vcall offset is at index -3 in the vtable. */
7351 vid
.index
= ssize_int (-3);
7353 /* Add entries to the vtable for RTTI. */
7354 build_rtti_vtbl_entries (binfo
, &vid
);
7356 /* Create an array for keeping track of the functions we've
7357 processed. When we see multiple functions with the same
7358 signature, we share the vcall offsets. */
7359 VARRAY_TREE_INIT (vid
.fns
, 32, "fns");
7360 /* Add the vcall and vbase offset entries. */
7361 build_vcall_and_vbase_vtbl_entries (binfo
, &vid
);
7363 VARRAY_FREE (vid
.fns
);
7364 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7365 build_vbase_offset_vtbl_entries. */
7366 for (vbase
= CLASSTYPE_VBASECLASSES (t
);
7368 vbase
= TREE_CHAIN (vbase
))
7369 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase
));
7371 if (non_fn_entries_p
)
7372 *non_fn_entries_p
= list_length (vid
.inits
);
7374 /* Go through all the ordinary virtual functions, building up
7376 vfun_inits
= NULL_TREE
;
7377 for (v
= BINFO_VIRTUALS (orig_binfo
); v
; v
= TREE_CHAIN (v
))
7384 int generate_with_vtable_p
= BV_GENERATE_THUNK_WITH_VTABLE_P (v
);
7386 /* Pull the offset for `this', and the function to call, out of
7388 delta
= BV_DELTA (v
);
7390 if (BV_USE_VCALL_INDEX_P (v
))
7392 vcall_index
= BV_VCALL_INDEX (v
);
7393 my_friendly_assert (vcall_index
!= NULL_TREE
, 20000621);
7395 else if (vid
.ctor_vtbl_p
&& BV_VCALL_INDEX (v
))
7397 /* In the original, we did not need to use the vcall index, even
7398 though there was one, but in a ctor vtable things might be
7399 different (a primary virtual base might have moved). Be
7400 conservative and use a vcall adjusting thunk. */
7401 vcall_index
= BV_VCALL_INDEX (v
);
7402 generate_with_vtable_p
= 1;
7405 vcall_index
= NULL_TREE
;
7408 my_friendly_assert (TREE_CODE (delta
) == INTEGER_CST
, 19990727);
7409 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 19990727);
7411 /* You can't call an abstract virtual function; it's abstract.
7412 So, we replace these functions with __pure_virtual. */
7413 if (DECL_PURE_VIRTUAL_P (fn
))
7416 /* Take the address of the function, considering it to be of an
7417 appropriate generic type. */
7418 pfn
= build1 (ADDR_EXPR
, vfunc_ptr_type_node
, fn
);
7419 /* The address of a function can't change. */
7420 TREE_CONSTANT (pfn
) = 1;
7421 /* Enter it in the vtable. */
7422 init
= build_vtable_entry (delta
, vcall_index
, pfn
,
7423 generate_with_vtable_p
);
7424 /* And add it to the chain of initializers. */
7425 vfun_inits
= tree_cons (NULL_TREE
, init
, vfun_inits
);
7428 /* The initializers for virtual functions were built up in reverse
7429 order; straighten them out now. */
7430 vfun_inits
= nreverse (vfun_inits
);
7432 /* The negative offset initializers are also in reverse order. */
7433 vid
.inits
= nreverse (vid
.inits
);
7435 /* Chain the two together. */
7436 return chainon (vid
.inits
, vfun_inits
);
7439 /* Sets vid->inits to be the initializers for the vbase and vcall
7440 offsets in BINFO, which is in the hierarchy dominated by T. */
7443 build_vcall_and_vbase_vtbl_entries (binfo
, vid
)
7445 vtbl_init_data
*vid
;
7449 /* If this is a derived class, we must first create entries
7450 corresponding to the primary base class. */
7451 b
= get_primary_binfo (binfo
);
7453 build_vcall_and_vbase_vtbl_entries (b
, vid
);
7455 /* Add the vbase entries for this base. */
7456 build_vbase_offset_vtbl_entries (binfo
, vid
);
7457 /* Add the vcall entries for this base. */
7458 build_vcall_offset_vtbl_entries (binfo
, vid
);
7461 /* Returns the initializers for the vbase offset entries in the vtable
7462 for BINFO (which is part of the class hierarchy dominated by T), in
7463 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7464 where the next vbase offset will go. */
7467 build_vbase_offset_vtbl_entries (binfo
, vid
)
7469 vtbl_init_data
*vid
;
7474 /* Under the old ABI, pointers to virtual bases are stored in each
7476 if (!vbase_offsets_in_vtable_p ())
7479 /* If there are no virtual baseclasses, then there is nothing to
7481 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo
)))
7486 /* Go through the virtual bases, adding the offsets. */
7487 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
7489 vbase
= TREE_CHAIN (vbase
))
7494 if (!TREE_VIA_VIRTUAL (vbase
))
7497 /* Find the instance of this virtual base in the complete
7499 b
= binfo_for_vbase (BINFO_TYPE (vbase
), t
);
7501 /* If we've already got an offset for this virtual base, we
7502 don't need another one. */
7503 if (BINFO_VTABLE_PATH_MARKED (b
))
7505 SET_BINFO_VTABLE_PATH_MARKED (b
);
7507 /* Figure out where we can find this vbase offset. */
7508 delta
= size_binop (MULT_EXPR
,
7511 TYPE_SIZE_UNIT (vtable_entry_type
)));
7512 if (vid
->primary_vtbl_p
)
7513 BINFO_VPTR_FIELD (b
) = delta
;
7515 if (binfo
!= TYPE_BINFO (t
))
7519 /* Find the instance of this virtual base in the type of BINFO. */
7520 orig_vbase
= binfo_for_vbase (BINFO_TYPE (vbase
),
7521 BINFO_TYPE (binfo
));
7523 /* The vbase offset had better be the same. */
7524 if (!tree_int_cst_equal (delta
,
7525 BINFO_VPTR_FIELD (orig_vbase
)))
7526 my_friendly_abort (20000403);
7529 /* The next vbase will come at a more negative offset. */
7530 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
, ssize_int (1));
7532 /* The initializer is the delta from BINFO to this virtual base.
7533 The vbase offsets go in reverse inheritance-graph order, and
7534 we are walking in inheritance graph order so these end up in
7536 delta
= size_diffop (BINFO_OFFSET (b
), BINFO_OFFSET (binfo
));
7538 = build_tree_list (NULL_TREE
,
7539 fold (build1 (NOP_EXPR
,
7542 vid
->last_init
= &TREE_CHAIN (*vid
->last_init
);
7546 /* Adds the initializers for the vcall offset entries in the vtable
7547 for BINFO (which is part of the class hierarchy dominated by T) to
7551 build_vcall_offset_vtbl_entries (binfo
, vid
)
7553 vtbl_init_data
*vid
;
7555 /* Under the old ABI, the adjustments to the `this' pointer were made
7557 if (!vcall_offsets_in_vtable_p ())
7560 /* We only need these entries if this base is a virtual base. */
7561 if (!TREE_VIA_VIRTUAL (binfo
))
7564 /* We need a vcall offset for each of the virtual functions in this
7565 vtable. For example:
7567 class A { virtual void f (); };
7568 class B : virtual public A { };
7569 class C: virtual public A, public B {};
7576 The location of `A' is not at a fixed offset relative to `B'; the
7577 offset depends on the complete object derived from `B'. So,
7578 `B' vtable contains an entry for `f' that indicates by what
7579 amount the `this' pointer for `B' needs to be adjusted to arrive
7582 We need entries for all the functions in our primary vtable and
7583 in our non-virtual bases vtables. */
7585 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7586 add_vcall_offset_vtbl_entries_r (binfo
, vid
);
7589 /* Build vcall offsets, starting with those for BINFO. */
7592 add_vcall_offset_vtbl_entries_r (binfo
, vid
)
7594 vtbl_init_data
*vid
;
7599 /* Don't walk into virtual bases -- except, of course, for the
7600 virtual base for which we are building vcall offsets. */
7601 if (TREE_VIA_VIRTUAL (binfo
) && vid
->vbase
!= binfo
)
7604 /* If BINFO has a primary base, process it first. */
7605 primary_binfo
= get_primary_binfo (binfo
);
7607 add_vcall_offset_vtbl_entries_r (primary_binfo
, vid
);
7609 /* Add BINFO itself to the list. */
7610 add_vcall_offset_vtbl_entries_1 (binfo
, vid
);
7612 /* Scan the non-primary bases of BINFO. */
7613 for (i
= 0; i
< BINFO_N_BASETYPES (binfo
); ++i
)
7617 base_binfo
= BINFO_BASETYPE (binfo
, i
);
7618 if (base_binfo
!= primary_binfo
)
7619 add_vcall_offset_vtbl_entries_r (base_binfo
, vid
);
7623 /* Called from build_vcall_offset_vtbl_entries_r. */
7626 add_vcall_offset_vtbl_entries_1 (binfo
, vid
)
7628 vtbl_init_data
* vid
;
7630 tree derived_virtuals
;
7634 int lost_primary
= 0;
7635 /* If BINFO is a primary base, this is the least derived class of
7636 BINFO that is not a primary base. */
7637 tree non_primary_binfo
;
7639 binfo_inits
= NULL_TREE
;
7641 /* We might be a primary base class. Go up the inheritance
7642 hierarchy until we find the class of which we are a primary base:
7643 it is the BINFO_VIRTUALS there that we need to consider. */
7644 non_primary_binfo
= binfo
;
7645 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
7649 /* If we have reached a virtual base, then it must be the
7650 virtual base for which we are building vcall offsets. In
7651 turn, the virtual base must be a (possibly indirect) primary
7652 base of the class that we are initializing, or we wouldn't
7653 care about its vtable offsets. */
7654 if (TREE_VIA_VIRTUAL (non_primary_binfo
))
7656 if (vid
->ctor_vtbl_p
)
7660 for (probe
= vid
->binfo
;
7661 probe
!= non_primary_binfo
;
7662 probe
= get_primary_binfo (probe
))
7664 if (BINFO_LOST_PRIMARY_P (probe
))
7671 non_primary_binfo
= vid
->binfo
;
7675 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
7676 if (get_primary_binfo (b
) != non_primary_binfo
)
7678 non_primary_binfo
= b
;
7681 if (vid
->ctor_vtbl_p
)
7682 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7683 where rtti_binfo is the most derived type. */
7684 non_primary_binfo
= get_original_base
7685 (non_primary_binfo
, TYPE_BINFO (BINFO_TYPE (vid
->rtti_binfo
)));
7687 /* Make entries for the rest of the virtuals. */
7688 for (base_virtuals
= BINFO_VIRTUALS (binfo
),
7689 derived_virtuals
= BINFO_VIRTUALS (non_primary_binfo
),
7690 orig_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
7692 base_virtuals
= TREE_CHAIN (base_virtuals
),
7693 derived_virtuals
= TREE_CHAIN (derived_virtuals
),
7694 orig_virtuals
= TREE_CHAIN (orig_virtuals
))
7703 /* Find the declaration that originally caused this function to
7705 orig_fn
= BV_FN (orig_virtuals
);
7707 /* We do not need an entry if this function is declared in a
7708 virtual base (or one of its virtual bases), and not
7709 overridden in the section of the hierarchy dominated by the
7710 virtual base for which we are building vcall offsets. */
7711 if (!same_type_p (DECL_CONTEXT (orig_fn
), BINFO_TYPE (binfo
)))
7714 /* Find the overriding function. */
7715 fn
= BV_FN (derived_virtuals
);
7717 /* If there is already an entry for a function with the same
7718 signature as FN, then we do not need a second vcall offset.
7719 Check the list of functions already present in the derived
7721 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (vid
->fns
); ++i
)
7725 derived_entry
= VARRAY_TREE (vid
->fns
, i
);
7726 if (same_signature_p (BV_FN (derived_entry
), fn
)
7727 /* We only use one vcall offset for virtual destructors,
7728 even though there are two virtual table entries. */
7729 || (DECL_DESTRUCTOR_P (BV_FN (derived_entry
))
7730 && DECL_DESTRUCTOR_P (fn
)))
7732 if (!vid
->ctor_vtbl_p
)
7733 BV_VCALL_INDEX (derived_virtuals
)
7734 = BV_VCALL_INDEX (derived_entry
);
7738 if (i
!= VARRAY_ACTIVE_SIZE (vid
->fns
))
7741 /* The FN comes from BASE. So, we must calculate the adjustment
7742 from the virtual base that derived from BINFO to BASE. */
7743 base
= DECL_CONTEXT (fn
);
7744 base_binfo
= get_binfo (base
, vid
->derived
, /*protect=*/0);
7746 /* Compute the vcall offset. */
7747 vcall_offset
= BINFO_OFFSET (vid
->vbase
);
7749 vcall_offset
= size_binop (PLUS_EXPR
, vcall_offset
,
7750 BINFO_OFFSET (vid
->binfo
));
7751 vcall_offset
= size_diffop (BINFO_OFFSET (base_binfo
),
7753 vcall_offset
= fold (build1 (NOP_EXPR
, vtable_entry_type
,
7756 *vid
->last_init
= build_tree_list (NULL_TREE
, vcall_offset
);
7757 vid
->last_init
= &TREE_CHAIN (*vid
->last_init
);
7759 /* Keep track of the vtable index where this vcall offset can be
7760 found. For a construction vtable, we already made this
7761 annotation when we build the original vtable. */
7762 if (!vid
->ctor_vtbl_p
)
7763 BV_VCALL_INDEX (derived_virtuals
) = vid
->index
;
7765 /* The next vcall offset will be found at a more negative
7767 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
, ssize_int (1));
7769 /* Keep track of this function. */
7770 VARRAY_PUSH_TREE (vid
->fns
, derived_virtuals
);
7774 /* Return vtbl initializers for the RTTI entries coresponding to the
7775 BINFO's vtable. The RTTI entries should indicate the object given
7776 by VID->rtti_binfo. */
7779 build_rtti_vtbl_entries (binfo
, vid
)
7781 vtbl_init_data
*vid
;
7790 basetype
= BINFO_TYPE (binfo
);
7791 t
= BINFO_TYPE (vid
->rtti_binfo
);
7793 /* For a COM object there is no RTTI entry. */
7794 if (CLASSTYPE_COM_INTERFACE (basetype
))
7797 /* To find the complete object, we will first convert to our most
7798 primary base, and then add the offset in the vtbl to that value. */
7800 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b
))
7801 && !BINFO_LOST_PRIMARY_P (b
))
7805 primary_base
= get_primary_binfo (b
);
7806 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base
) == b
, 20010127);
7809 offset
= size_diffop (BINFO_OFFSET (vid
->rtti_binfo
), BINFO_OFFSET (b
));
7811 /* The second entry is the address of the typeinfo object. */
7813 decl
= build_unary_op (ADDR_EXPR
, get_tinfo_decl (t
), 0);
7815 decl
= integer_zero_node
;
7817 /* Convert the declaration to a type that can be stored in the
7819 init
= build1 (NOP_EXPR
, vfunc_ptr_type_node
, decl
);
7820 TREE_CONSTANT (init
) = 1;
7821 *vid
->last_init
= build_tree_list (NULL_TREE
, init
);
7822 vid
->last_init
= &TREE_CHAIN (*vid
->last_init
);
7824 /* Add the offset-to-top entry. It comes earlier in the vtable that
7825 the the typeinfo entry. */
7826 if (flag_vtable_thunks
)
7828 /* Convert the offset to look like a function pointer, so that
7829 we can put it in the vtable. */
7830 init
= build1 (NOP_EXPR
, vfunc_ptr_type_node
, offset
);
7831 TREE_CONSTANT (init
) = 1;
7832 *vid
->last_init
= build_tree_list (NULL_TREE
, init
);
7833 vid
->last_init
= &TREE_CHAIN (*vid
->last_init
);
7837 /* Build an entry in the virtual function table. DELTA is the offset
7838 for the `this' pointer. VCALL_INDEX is the vtable index containing
7839 the vcall offset; NULL_TREE if none. ENTRY is the virtual function
7840 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
7841 but it may not actually be a virtual function table pointer. (For
7842 example, it might be the address of the RTTI object, under the new
7846 build_vtable_entry (delta
, vcall_index
, entry
, generate_with_vtable_p
)
7850 int generate_with_vtable_p
;
7852 if (flag_vtable_thunks
)
7856 fn
= TREE_OPERAND (entry
, 0);
7857 if ((!integer_zerop (delta
) || vcall_index
!= NULL_TREE
)
7858 && fn
!= abort_fndecl
7859 && !DECL_TINFO_FN_P (fn
))
7861 entry
= make_thunk (entry
, delta
, vcall_index
,
7862 generate_with_vtable_p
);
7863 entry
= build1 (ADDR_EXPR
, vtable_entry_type
, entry
);
7864 TREE_READONLY (entry
) = 1;
7865 TREE_CONSTANT (entry
) = 1;
7867 #ifdef GATHER_STATISTICS
7868 n_vtable_entries
+= 1;
7874 tree elems
= tree_cons (NULL_TREE
, delta
,
7875 tree_cons (NULL_TREE
, integer_zero_node
,
7876 build_tree_list (NULL_TREE
, entry
)));
7877 tree entry
= build (CONSTRUCTOR
, vtable_entry_type
, NULL_TREE
, elems
);
7879 /* We don't use vcall offsets when not using vtable thunks. */
7880 my_friendly_assert (vcall_index
== NULL_TREE
, 20000125);
7882 /* DELTA used to be constructed by `size_int' and/or size_binop,
7883 which caused overflow problems when it was negative. That should
7886 if (! int_fits_type_p (delta
, delta_type_node
))
7888 if (flag_huge_objects
)
7889 sorry ("object size exceeds built-in limit for virtual function table implementation");
7891 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
7894 TREE_CONSTANT (entry
) = 1;
7895 TREE_STATIC (entry
) = 1;
7896 TREE_READONLY (entry
) = 1;
7898 #ifdef GATHER_STATISTICS
7899 n_vtable_entries
+= 1;