Update version
[official-gcc.git] / gcc / cp / class.c
blob21c08aef7683c3bfb15bbd327af2977d783a36cc
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)
11 any later version.
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. */
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "rtl.h"
32 #include "output.h"
33 #include "toplev.h"
34 #include "ggc.h"
35 #include "lex.h"
37 #include "obstack.h"
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. */
52 tree name;
54 /* The _TYPE node for the class. */
55 tree type;
57 /* The access specifier pending for new declarations in the scope of
58 this class. */
59 tree access;
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. */
68 tree binfo;
69 /* The type of the most-derived type. */
70 tree derived;
71 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
72 unless ctor_vtbl_p is true. */
73 tree rtti_binfo;
74 /* The negative-index vtable initializers built up so far. These
75 are in order from least negative index to most negative index. */
76 tree inits;
77 /* The last (i.e., most negative) entry in INITS. */
78 tree* last_init;
79 /* The binfo for the virtual base for which we're building
80 vcall offset initializers. */
81 tree vbase;
82 /* The functions in vbase for which we have already provided vcall
83 offsets. */
84 varray_type fns;
85 /* The vtable index of the next vcall or vbase offset. */
86 tree index;
87 /* Nonzero if we are building the initializer for the primary
88 vtable. */
89 int primary_vtbl_p;
90 /* Nonzero if we are building the initializer for a construction
91 vtable. */
92 int ctor_vtbl_p;
93 } vtbl_init_data;
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));
111 static tree get_vtable_name PARAMS ((tree));
112 static tree get_basefndecls PARAMS ((tree, tree));
113 static int build_primary_vtable PARAMS ((tree, tree));
114 static int build_secondary_vtable PARAMS ((tree, tree));
115 static tree dfs_finish_vtbls PARAMS ((tree, void *));
116 static void finish_vtbls PARAMS ((tree));
117 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
118 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
119 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
120 static void delete_duplicate_fields PARAMS ((tree));
121 static void finish_struct_bits PARAMS ((tree));
122 static int alter_access PARAMS ((tree, tree, tree));
123 static void handle_using_decl PARAMS ((tree, tree));
124 static int strictly_overrides PARAMS ((tree, tree));
125 static void mark_overriders PARAMS ((tree, tree));
126 static void check_for_override PARAMS ((tree, tree));
127 static tree dfs_modify_vtables PARAMS ((tree, void *));
128 static tree modify_all_vtables PARAMS ((tree, int *, tree));
129 static void determine_primary_base PARAMS ((tree, int *));
130 static void finish_struct_methods PARAMS ((tree));
131 static void maybe_warn_about_overly_private_class PARAMS ((tree));
132 static int field_decl_cmp PARAMS ((const tree *, const tree *));
133 static int method_name_cmp PARAMS ((const tree *, const tree *));
134 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
135 static tree fixed_type_or_null PARAMS ((tree, int *, int *));
136 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
137 int, int, tree));
138 static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
139 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
140 static int count_fields PARAMS ((tree));
141 static int add_fields_to_vec PARAMS ((tree, tree, int));
142 static void check_bitfield_decl PARAMS ((tree));
143 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
144 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
145 int *));
146 static bool build_base_field PARAMS ((record_layout_info, tree, int *,
147 splay_tree, tree));
148 static bool build_base_fields PARAMS ((record_layout_info, int *,
149 splay_tree, tree));
150 static tree build_vbase_pointer_fields PARAMS ((record_layout_info, int *));
151 static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, tree,
152 int *));
153 static void check_methods PARAMS ((tree));
154 static void remove_zero_width_bit_fields PARAMS ((tree));
155 static void check_bases PARAMS ((tree, int *, int *, int *));
156 static void check_bases_and_members PARAMS ((tree, int *));
157 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
158 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
159 static void fixup_pending_inline PARAMS ((tree));
160 static void fixup_inline_methods PARAMS ((tree));
161 static void set_primary_base PARAMS ((tree, tree, int *));
162 static void propagate_binfo_offsets PARAMS ((tree, tree, tree));
163 static void layout_virtual_bases PARAMS ((tree, splay_tree));
164 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
165 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
166 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
167 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
168 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
169 static void layout_vtable_decl PARAMS ((tree, int));
170 static tree dfs_find_final_overrider PARAMS ((tree, void *));
171 static tree find_final_overrider PARAMS ((tree, tree, tree));
172 static int make_new_vtable PARAMS ((tree, tree));
173 static int maybe_indent_hierarchy PARAMS ((FILE *, int, int));
174 static void dump_class_hierarchy_r PARAMS ((FILE *, int, tree, tree, int));
175 static void dump_class_hierarchy PARAMS ((tree));
176 static void dump_array PARAMS ((FILE *, tree));
177 static void dump_vtable PARAMS ((tree, tree, tree));
178 static void dump_vtt PARAMS ((tree, tree));
179 static tree build_vtable PARAMS ((tree, tree, tree));
180 static void initialize_vtable PARAMS ((tree, tree));
181 static void initialize_array PARAMS ((tree, tree));
182 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
183 tree, tree,
184 splay_tree, tree));
185 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
186 static bool layout_empty_base PARAMS ((tree, tree, splay_tree, tree));
187 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
188 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
189 tree));
190 static void set_vindex PARAMS ((tree, int *));
191 static void build_rtti_vtbl_entries PARAMS ((tree, vtbl_init_data *));
192 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
193 vtbl_init_data *));
194 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
195 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
196 static tree dfs_unshared_virtual_bases PARAMS ((tree, void *));
197 static void mark_primary_bases PARAMS ((tree));
198 static tree mark_primary_virtual_base PARAMS ((tree, tree));
199 static void clone_constructors_and_destructors PARAMS ((tree));
200 static tree build_clone PARAMS ((tree, tree));
201 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
202 static tree copy_virtuals PARAMS ((tree));
203 static void build_ctor_vtbl_group PARAMS ((tree, tree));
204 static void build_vtt PARAMS ((tree));
205 static tree binfo_ctor_vtable PARAMS ((tree));
206 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
207 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
208 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
209 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
210 static tree get_original_base PARAMS ((tree, tree));
211 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
212 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
213 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
214 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
215 tree, splay_tree, tree, int));
216 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
217 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
218 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
219 splay_tree_key k2));
220 static void warn_about_ambiguous_direct_bases PARAMS ((tree));
222 /* Macros for dfs walking during vtt construction. See
223 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
224 and dfs_fixup_binfo_vtbls. */
225 #define VTT_TOP_LEVEL_P(node) TREE_UNSIGNED(node)
226 #define VTT_MARKED_BINFO_P(node) TREE_USED(node)
228 /* Variables shared between class.c and call.c. */
230 #ifdef GATHER_STATISTICS
231 int n_vtables = 0;
232 int n_vtable_entries = 0;
233 int n_vtable_searches = 0;
234 int n_vtable_elems = 0;
235 int n_convert_harshness = 0;
236 int n_compute_conversion_costs = 0;
237 int n_build_method_call = 0;
238 int n_inner_fields_searched = 0;
239 #endif
241 /* Virtual base class layout. */
243 /* Returns a list of virtual base class pointers as a chain of
244 FIELD_DECLS. */
246 static tree
247 build_vbase_pointer_fields (rli, empty_p)
248 record_layout_info rli;
249 int *empty_p;
251 /* Chain to hold all the new FIELD_DECLs which point at virtual
252 base classes. */
253 tree rec = rli->t;
254 tree vbase_decls = NULL_TREE;
255 tree binfos = TYPE_BINFO_BASETYPES (rec);
256 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
257 tree decl;
258 int i;
260 /* Under the new ABI, there are no vbase pointers in the object.
261 Instead, the offsets are stored in the vtable. */
262 if (vbase_offsets_in_vtable_p ())
263 return NULL_TREE;
265 /* Loop over the baseclasses, adding vbase pointers as needed. */
266 for (i = 0; i < n_baseclasses; i++)
268 register tree base_binfo = TREE_VEC_ELT (binfos, i);
269 register tree basetype = BINFO_TYPE (base_binfo);
271 if (!COMPLETE_TYPE_P (basetype))
272 /* This error is now reported in xref_tag, thus giving better
273 location information. */
274 continue;
276 /* All basetypes are recorded in the association list of the
277 derived type. */
279 if (TREE_VIA_VIRTUAL (base_binfo))
281 int j;
282 const char *name;
284 /* The offset for a virtual base class is only used in computing
285 virtual function tables and for initializing virtual base
286 pointers. It is built once `get_vbase_types' is called. */
288 /* If this basetype can come from another vbase pointer
289 without an additional indirection, we will share
290 that pointer. If an indirection is involved, we
291 make our own pointer. */
292 for (j = 0; j < n_baseclasses; j++)
294 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
295 if (! TREE_VIA_VIRTUAL (other_base_binfo)
296 && binfo_for_vbase (basetype, BINFO_TYPE (other_base_binfo)))
297 goto got_it;
299 FORMAT_VBASE_NAME (name, basetype);
300 decl = build_vtbl_or_vbase_field (get_identifier (name),
301 get_identifier (VTABLE_BASE),
302 build_pointer_type (basetype),
303 rec,
304 basetype,
305 empty_p);
306 BINFO_VPTR_FIELD (base_binfo) = decl;
307 TREE_CHAIN (decl) = vbase_decls;
308 place_field (rli, decl);
309 vbase_decls = decl;
310 *empty_p = 0;
312 got_it:
313 /* The space this decl occupies has already been accounted for. */
318 return vbase_decls;
321 /* Returns a pointer to the virtual base class of EXP that has the
322 indicated TYPE. EXP is of class type, not a pointer type. */
324 static tree
325 build_vbase_pointer (exp, type)
326 tree exp, type;
328 if (vbase_offsets_in_vtable_p ())
330 tree vbase;
331 tree vbase_ptr;
333 /* Find the shared copy of TYPE; that's where the vtable offset
334 is recorded. */
335 vbase = binfo_for_vbase (type, TREE_TYPE (exp));
336 /* Find the virtual function table pointer. */
337 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
338 /* Compute the location where the offset will lie. */
339 vbase_ptr = build (PLUS_EXPR,
340 TREE_TYPE (vbase_ptr),
341 vbase_ptr,
342 BINFO_VPTR_FIELD (vbase));
343 vbase_ptr = build1 (NOP_EXPR,
344 build_pointer_type (ptrdiff_type_node),
345 vbase_ptr);
346 /* Add the contents of this location to EXP. */
347 return build (PLUS_EXPR,
348 build_pointer_type (type),
349 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
350 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
352 else
354 char *name;
355 FORMAT_VBASE_NAME (name, type);
356 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
360 /* Build multi-level access to EXPR using hierarchy path PATH.
361 CODE is PLUS_EXPR if we are going with the grain,
362 and MINUS_EXPR if we are not (in which case, we cannot traverse
363 virtual baseclass links).
365 TYPE is the type we want this path to have on exit.
367 NONNULL is non-zero if we know (for any reason) that EXPR is
368 not, in fact, zero. */
370 tree
371 build_vbase_path (code, type, expr, path, nonnull)
372 enum tree_code code;
373 tree type, expr, path;
374 int nonnull;
376 register int changed = 0;
377 tree last = NULL_TREE, last_virtual = NULL_TREE;
378 int fixed_type_p;
379 tree null_expr = 0, nonnull_expr;
380 tree basetype;
381 tree offset = integer_zero_node;
383 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
384 return build1 (NOP_EXPR, type, expr);
386 /* We could do better if we had additional logic to convert back to the
387 unconverted type (the static type of the complete object), and then
388 convert back to the type we want. Until that is done, we only optimize
389 if the complete type is the same type as expr has. */
390 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
391 if (fixed_type_p < 0)
392 /* Virtual base layout is not fixed, even in ctors and dtors. */
393 fixed_type_p = 0;
395 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
396 expr = save_expr (expr);
397 nonnull_expr = expr;
399 path = reverse_path (path);
401 basetype = BINFO_TYPE (path);
403 while (path)
405 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
407 last_virtual = BINFO_TYPE (TREE_VALUE (path));
408 if (code == PLUS_EXPR)
410 changed = ! fixed_type_p;
412 if (changed)
414 tree ind;
416 /* We already check for ambiguous things in the caller, just
417 find a path. */
418 if (last)
420 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
421 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
423 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
424 nonnull_expr = build_vbase_pointer (ind, last_virtual);
425 if (nonnull == 0
426 && TREE_CODE (type) == POINTER_TYPE
427 && null_expr == NULL_TREE)
429 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
430 expr = build (COND_EXPR, build_pointer_type (last_virtual),
431 build (EQ_EXPR, boolean_type_node, expr,
432 integer_zero_node),
433 null_expr, nonnull_expr);
436 /* else we'll figure out the offset below. */
438 /* Happens in the case of parse errors. */
439 if (nonnull_expr == error_mark_node)
440 return error_mark_node;
442 else
444 cp_error ("cannot cast up from virtual baseclass `%T'",
445 last_virtual);
446 return error_mark_node;
449 last = TREE_VALUE (path);
450 path = TREE_CHAIN (path);
452 /* LAST is now the last basetype assoc on the path. */
454 /* A pointer to a virtual base member of a non-null object
455 is non-null. Therefore, we only need to test for zeroness once.
456 Make EXPR the canonical expression to deal with here. */
457 if (null_expr)
459 TREE_OPERAND (expr, 2) = nonnull_expr;
460 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
461 = TREE_TYPE (nonnull_expr);
463 else
464 expr = nonnull_expr;
466 /* If we go through any virtual base pointers, make sure that
467 casts to BASETYPE from the last virtual base class use
468 the right value for BASETYPE. */
469 if (changed)
471 tree intype = TREE_TYPE (TREE_TYPE (expr));
473 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
474 offset
475 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
477 else
478 offset = BINFO_OFFSET (last);
480 if (! integer_zerop (offset))
482 /* Bash types to make the backend happy. */
483 offset = cp_convert (type, offset);
485 /* If expr might be 0, we need to preserve that zeroness. */
486 if (nonnull == 0)
488 if (null_expr)
489 TREE_TYPE (null_expr) = type;
490 else
491 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
492 if (TREE_SIDE_EFFECTS (expr))
493 expr = save_expr (expr);
495 return build (COND_EXPR, type,
496 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
497 null_expr,
498 build (code, type, expr, offset));
500 else return build (code, type, expr, offset);
503 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
504 be used multiple times in initialization of multiple inheritance. */
505 if (null_expr)
507 TREE_TYPE (expr) = type;
508 return expr;
510 else
511 return build1 (NOP_EXPR, type, expr);
515 /* Virtual function things. */
517 /* We want to give the assembler the vtable identifier as well as
518 the offset to the function pointer. So we generate
520 __asm__ __volatile__ (".vtable_entry %c0, %c1"
521 : : "s"(&class_vtable),
522 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
524 static void
525 build_vtable_entry_ref (basetype, vtbl, idx)
526 tree basetype, vtbl, idx;
528 static char asm_stmt[] = ".vtable_entry %c0, %c1";
529 tree s, i, i2;
531 s = build_unary_op (ADDR_EXPR,
532 get_vtbl_decl_for_binfo (TYPE_BINFO (basetype)),
534 s = build_tree_list (build_string (1, "s"), s);
536 i = build_array_ref (vtbl, idx);
537 if (!flag_vtable_thunks)
538 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
539 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
540 i2 = build_array_ref (vtbl, build_int_2(0,0));
541 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
542 i = cp_build_binary_op (MINUS_EXPR, i, i2);
543 i = build_tree_list (build_string (1, "i"), i);
545 finish_asm_stmt (ridpointers[RID_VOLATILE],
546 build_string (sizeof(asm_stmt)-1, asm_stmt),
547 NULL_TREE, chainon (s, i), NULL_TREE);
550 /* Given an object INSTANCE, return an expression which yields the
551 vtable element corresponding to INDEX. There are many special
552 cases for INSTANCE which we take care of here, mainly to avoid
553 creating extra tree nodes when we don't have to. */
555 tree
556 build_vtbl_ref (instance, idx)
557 tree instance, idx;
559 tree vtbl, aref;
560 tree basetype = TREE_TYPE (instance);
562 if (TREE_CODE (basetype) == REFERENCE_TYPE)
563 basetype = TREE_TYPE (basetype);
565 if (instance == current_class_ref)
566 vtbl = build_vfield_ref (instance, basetype);
567 else
569 if (optimize)
571 /* Try to figure out what a reference refers to, and
572 access its virtual function table directly. */
573 tree ref = NULL_TREE;
575 if (TREE_CODE (instance) == INDIRECT_REF
576 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
577 ref = TREE_OPERAND (instance, 0);
578 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
579 ref = instance;
581 if (ref && TREE_CODE (ref) == VAR_DECL
582 && DECL_INITIAL (ref))
584 tree init = DECL_INITIAL (ref);
586 while (TREE_CODE (init) == NOP_EXPR
587 || TREE_CODE (init) == NON_LVALUE_EXPR)
588 init = TREE_OPERAND (init, 0);
589 if (TREE_CODE (init) == ADDR_EXPR)
591 init = TREE_OPERAND (init, 0);
592 if (IS_AGGR_TYPE (TREE_TYPE (init))
593 && (TREE_CODE (init) == PARM_DECL
594 || TREE_CODE (init) == VAR_DECL))
595 instance = init;
600 if (IS_AGGR_TYPE (TREE_TYPE (instance))
601 && (TREE_CODE (instance) == RESULT_DECL
602 || TREE_CODE (instance) == PARM_DECL
603 || TREE_CODE (instance) == VAR_DECL))
605 vtbl = TYPE_BINFO_VTABLE (basetype);
606 /* Knowing the dynamic type of INSTANCE we can easily obtain
607 the correct vtable entry. In the new ABI, we resolve
608 this back to be in terms of the primary vtable. */
609 if (TREE_CODE (vtbl) == PLUS_EXPR)
611 idx = fold (build (PLUS_EXPR,
612 TREE_TYPE (idx),
613 idx,
614 build (EXACT_DIV_EXPR,
615 TREE_TYPE (idx),
616 TREE_OPERAND (vtbl, 1),
617 TYPE_SIZE_UNIT (vtable_entry_type))));
618 vtbl = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
621 else
622 vtbl = build_vfield_ref (instance, basetype);
625 assemble_external (vtbl);
627 if (flag_vtable_gc)
628 build_vtable_entry_ref (basetype, vtbl, idx);
630 aref = build_array_ref (vtbl, idx);
632 return aref;
635 /* Given an object INSTANCE, return an expression which yields the
636 virtual function corresponding to INDEX. There are many special
637 cases for INSTANCE which we take care of here, mainly to avoid
638 creating extra tree nodes when we don't have to. */
640 tree
641 build_vfn_ref (ptr_to_instptr, instance, idx)
642 tree *ptr_to_instptr, instance;
643 tree idx;
645 tree aref = build_vtbl_ref (instance, idx);
647 /* When using thunks, there is no extra delta, and we get the pfn
648 directly. */
649 if (flag_vtable_thunks)
651 /* When using function descriptors, the address of the
652 vtable entry is treated as a function pointer. */
653 if (TARGET_VTABLE_USES_DESCRIPTORS)
654 return build1 (NOP_EXPR, TREE_TYPE (aref),
655 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
657 return aref;
660 if (ptr_to_instptr)
662 /* Save the intermediate result in a SAVE_EXPR so we don't have to
663 compute each component of the virtual function pointer twice. */
664 if (TREE_CODE (aref) == INDIRECT_REF)
665 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
667 *ptr_to_instptr
668 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
669 *ptr_to_instptr,
670 cp_convert (ptrdiff_type_node,
671 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
674 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
677 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
678 for the given TYPE. */
680 static tree
681 get_vtable_name (type)
682 tree type;
684 return mangle_vtbl_for_type (type);
687 /* Return an IDENTIFIER_NODE for the name of the virtual table table
688 for TYPE. */
690 tree
691 get_vtt_name (type)
692 tree type;
694 return mangle_vtt_for_type (type);
697 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
698 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
699 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
701 static tree
702 build_vtable (class_type, name, vtable_type)
703 tree class_type;
704 tree name;
705 tree vtable_type;
707 tree decl;
709 decl = build_lang_decl (VAR_DECL, name, vtable_type);
710 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
711 now to avoid confusion in mangle_decl. */
712 SET_DECL_ASSEMBLER_NAME (decl, name);
713 DECL_CONTEXT (decl) = class_type;
714 DECL_ARTIFICIAL (decl) = 1;
715 TREE_STATIC (decl) = 1;
716 TREE_READONLY (decl) = 1;
717 DECL_VIRTUAL_P (decl) = 1;
718 import_export_vtable (decl, class_type, 0);
720 return decl;
723 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
724 or even complete. If this does not exist, create it. If COMPLETE is
725 non-zero, then complete the definition of it -- that will render it
726 impossible to actually build the vtable, but is useful to get at those
727 which are known to exist in the runtime. */
729 tree
730 get_vtable_decl (type, complete)
731 tree type;
732 int complete;
734 tree name = get_vtable_name (type);
735 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
737 if (decl)
739 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
740 && DECL_VIRTUAL_P (decl), 20000118);
741 return decl;
744 decl = build_vtable (type, name, void_type_node);
745 decl = pushdecl_top_level (decl);
746 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
747 20000517);
749 /* At one time the vtable info was grabbed 2 words at a time. This
750 fails on sparc unless you have 8-byte alignment. (tiemann) */
751 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
752 DECL_ALIGN (decl));
754 if (complete)
756 DECL_EXTERNAL (decl) = 1;
757 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
760 return decl;
763 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
764 BV_VCALL_INDEX for each entry is cleared. */
766 static tree
767 copy_virtuals (binfo)
768 tree binfo;
770 tree copies;
771 tree t;
773 copies = copy_list (BINFO_VIRTUALS (binfo));
774 for (t = copies; t; t = TREE_CHAIN (t))
776 BV_VCALL_INDEX (t) = NULL_TREE;
777 BV_USE_VCALL_INDEX_P (t) = 0;
780 return copies;
783 /* Build the primary virtual function table for TYPE. If BINFO is
784 non-NULL, build the vtable starting with the initial approximation
785 that it is the same as the one which is the head of the association
786 list. Returns a non-zero value if a new vtable is actually
787 created. */
789 static int
790 build_primary_vtable (binfo, type)
791 tree binfo, type;
793 tree decl;
794 tree virtuals;
796 decl = get_vtable_decl (type, /*complete=*/0);
798 if (binfo)
800 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
801 /* We have already created a vtable for this base, so there's
802 no need to do it again. */
803 return 0;
805 virtuals = copy_virtuals (binfo);
806 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
807 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
808 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
810 else
812 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
813 20000118);
814 virtuals = NULL_TREE;
817 #ifdef GATHER_STATISTICS
818 n_vtables += 1;
819 n_vtable_elems += list_length (virtuals);
820 #endif
822 /* Initialize the association list for this type, based
823 on our first approximation. */
824 TYPE_BINFO_VTABLE (type) = decl;
825 TYPE_BINFO_VIRTUALS (type) = virtuals;
826 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
827 return 1;
830 /* Give TYPE a new virtual function table which is initialized
831 with a skeleton-copy of its original initialization. The only
832 entry that changes is the `delta' entry, so we can really
833 share a lot of structure.
835 FOR_TYPE is the derived type which caused this table to
836 be needed.
838 BINFO is the type association which provided TYPE for FOR_TYPE.
840 The order in which vtables are built (by calling this function) for
841 an object must remain the same, otherwise a binary incompatibility
842 can result. */
844 static int
845 build_secondary_vtable (binfo, for_type)
846 tree binfo, for_type;
848 tree basetype;
849 tree orig_decl = BINFO_VTABLE (binfo);
850 tree name;
851 tree new_decl;
852 tree offset;
853 tree path = binfo;
854 char *buf;
855 const char *buf2;
856 char joiner = '_';
857 int i;
859 #ifdef JOINER
860 joiner = JOINER;
861 #endif
863 if (TREE_VIA_VIRTUAL (binfo))
864 my_friendly_assert (binfo == binfo_for_vbase (BINFO_TYPE (binfo),
865 current_class_type),
866 170);
868 if (BINFO_NEW_VTABLE_MARKED (binfo, current_class_type))
869 /* We already created a vtable for this base. There's no need to
870 do it again. */
871 return 0;
873 /* Remember that we've created a vtable for this BINFO, so that we
874 don't try to do so again. */
875 SET_BINFO_NEW_VTABLE_MARKED (binfo, current_class_type);
877 /* Make fresh virtual list, so we can smash it later. */
878 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
880 my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
881 offset = BINFO_OFFSET (binfo);
883 /* In the new ABI, secondary vtables are laid out as part of the
884 same structure as the primary vtable. */
885 if (merge_primary_and_secondary_vtables_p ())
887 BINFO_VTABLE (binfo) = NULL_TREE;
888 return 1;
891 /* Create the declaration for the secondary vtable. */
892 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
893 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
894 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
896 /* We know that the vtable that we are going to create doesn't exist
897 yet in the global namespace, and when we finish, it will be
898 pushed into the global namespace. In complex MI hierarchies, we
899 have to loop while the name we are thinking of adding is globally
900 defined, adding more name components to the vtable name as we
901 loop, until the name is unique. This is because in complex MI
902 cases, we might have the same base more than once. This means
903 that the order in which this function is called for vtables must
904 remain the same, otherwise binary compatibility can be
905 compromised. */
907 while (1)
909 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
910 + 1 + i);
911 char *new_buf2;
913 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
914 buf2);
915 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
916 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
917 name = get_identifier (buf);
919 /* If this name doesn't clash, then we can use it, otherwise
920 we add more to the name until it is unique. */
922 if (! IDENTIFIER_GLOBAL_VALUE (name))
923 break;
925 /* Set values for next loop through, if the name isn't unique. */
927 path = BINFO_INHERITANCE_CHAIN (path);
929 /* We better not run out of stuff to make it unique. */
930 my_friendly_assert (path != NULL_TREE, 368);
932 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
934 if (for_type == basetype)
936 /* If we run out of basetypes in the path, we have already
937 found created a vtable with that name before, we now
938 resort to tacking on _%d to distinguish them. */
939 int j = 2;
940 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
941 buf1 = (char *) alloca (i);
942 do {
943 sprintf (buf1, "%s%c%s%c%d",
944 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
945 buf2, joiner, j);
946 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
947 + strlen (buf1) + 1);
948 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
949 name = get_identifier (buf);
951 /* If this name doesn't clash, then we can use it,
952 otherwise we add something different to the name until
953 it is unique. */
954 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
956 /* Hey, they really like MI don't they? Increase the 3
957 above to 6, and the 999 to 999999. :-) */
958 my_friendly_assert (j <= 999, 369);
960 break;
963 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
964 new_buf2 = (char *) alloca (i);
965 sprintf (new_buf2, "%s%c%s",
966 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
967 buf2 = new_buf2;
970 new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
971 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
972 DECL_USER_ALIGN (new_decl) = DECL_USER_ALIGN (orig_decl);
973 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
975 #ifdef GATHER_STATISTICS
976 n_vtables += 1;
977 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
978 #endif
980 return 1;
983 /* Create a new vtable for BINFO which is the hierarchy dominated by
984 T. */
986 static int
987 make_new_vtable (t, binfo)
988 tree t;
989 tree binfo;
991 if (binfo == TYPE_BINFO (t))
992 /* In this case, it is *type*'s vtable we are modifying. We start
993 with the approximation that its vtable is that of the
994 immediate base class. */
995 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
996 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
997 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
999 else
1000 /* This is our very own copy of `basetype' to play with. Later,
1001 we will fill in all the virtual functions that override the
1002 virtual functions in these base classes which are not defined
1003 by the current type. */
1004 return build_secondary_vtable (binfo, t);
1007 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
1008 (which is in the hierarchy dominated by T) list FNDECL as its
1009 BV_FN. DELTA is the required constant adjustment from the `this'
1010 pointer where the vtable entry appears to the `this' required when
1011 the function is actually called. */
1013 static void
1014 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
1015 tree t;
1016 tree binfo;
1017 tree fndecl;
1018 tree delta;
1019 tree *virtuals;
1021 tree v;
1023 v = *virtuals;
1025 if (fndecl != BV_FN (v)
1026 || !tree_int_cst_equal (delta, BV_DELTA (v)))
1028 tree base_fndecl;
1030 /* We need a new vtable for BINFO. */
1031 if (make_new_vtable (t, binfo))
1033 /* If we really did make a new vtable, we also made a copy
1034 of the BINFO_VIRTUALS list. Now, we have to find the
1035 corresponding entry in that list. */
1036 *virtuals = BINFO_VIRTUALS (binfo);
1037 while (BV_FN (*virtuals) != BV_FN (v))
1038 *virtuals = TREE_CHAIN (*virtuals);
1039 v = *virtuals;
1042 base_fndecl = BV_FN (v);
1043 BV_DELTA (v) = delta;
1044 BV_VCALL_INDEX (v) = NULL_TREE;
1045 BV_FN (v) = fndecl;
1047 /* Now assign virtual dispatch information, if unset. We can
1048 dispatch this through any overridden base function.
1050 FIXME this can choose a secondary vtable if the primary is not
1051 also lexically first, leading to useless conversions.
1052 In the V3 ABI, there's no reason for DECL_VIRTUAL_CONTEXT to
1053 ever be different from DECL_CONTEXT. */
1054 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1056 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1057 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1062 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
1063 functions present in the vtable so far. */
1065 static void
1066 set_vindex (decl, vfuns_p)
1067 tree decl;
1068 int *vfuns_p;
1070 int vindex;
1072 vindex = *vfuns_p;
1073 *vfuns_p += (TARGET_VTABLE_USES_DESCRIPTORS
1074 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
1075 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
1078 /* Add a virtual function to all the appropriate vtables for the class
1079 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1080 allocate a new slot in our table. If it is error_mark_node, we
1081 know that no other function from another vtable is overridden by X.
1082 VFUNS_P keeps track of how many virtuals there are in our
1083 main vtable for the type, and we build upon the NEW_VIRTUALS list
1084 and return it. */
1086 static void
1087 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1088 vfuns_p, fndecl, t)
1089 tree *new_virtuals_p;
1090 tree *overridden_virtuals_p;
1091 int *vfuns_p;
1092 tree fndecl;
1093 tree t; /* Structure type. */
1095 tree new_virtual;
1097 /* If this function doesn't override anything from a base class, we
1098 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1099 override something, we keep it around and assign its DECL_VINDEX
1100 later, in modify_all_vtables. */
1101 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1102 /* We've already dealt with this function. */
1103 return;
1105 new_virtual = make_node (TREE_LIST);
1106 BV_FN (new_virtual) = fndecl;
1107 BV_DELTA (new_virtual) = integer_zero_node;
1109 if (DECL_VINDEX (fndecl) == error_mark_node)
1111 /* FNDECL is a new virtual function; it doesn't override any
1112 virtual function in a base class. */
1114 /* We remember that this was the base sub-object for rtti. */
1115 CLASSTYPE_RTTI (t) = t;
1117 /* Now assign virtual dispatch information. */
1118 set_vindex (fndecl, vfuns_p);
1119 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1121 /* Save the state we've computed on the NEW_VIRTUALS list. */
1122 TREE_CHAIN (new_virtual) = *new_virtuals_p;
1123 *new_virtuals_p = new_virtual;
1125 else
1127 /* FNDECL overrides a function from a base class. */
1128 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1129 *overridden_virtuals_p = new_virtual;
1133 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
1134 the method after the class has already been defined because a
1135 declaration for it was seen. (Even though that is erroneous, we
1136 add the method for improved error recovery.) */
1138 void
1139 add_method (type, method, error_p)
1140 tree type;
1141 tree method;
1142 int error_p;
1144 int using = (DECL_CONTEXT (method) != type);
1145 int len;
1146 int slot;
1147 tree method_vec;
1149 if (!CLASSTYPE_METHOD_VEC (type))
1150 /* Make a new method vector. We start with 8 entries. We must
1151 allocate at least two (for constructors and destructors), and
1152 we're going to end up with an assignment operator at some point
1153 as well.
1155 We could use a TREE_LIST for now, and convert it to a TREE_VEC
1156 in finish_struct, but we would probably waste more memory
1157 making the links in the list than we would by over-allocating
1158 the size of the vector here. Furthermore, we would complicate
1159 all the code that expects this to be a vector. */
1160 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1162 method_vec = CLASSTYPE_METHOD_VEC (type);
1163 len = TREE_VEC_LENGTH (method_vec);
1165 /* Constructors and destructors go in special slots. */
1166 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1167 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1168 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1169 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1170 else
1172 /* See if we already have an entry with this name. */
1173 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
1174 if (!TREE_VEC_ELT (method_vec, slot)
1175 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1176 slot)))
1177 == DECL_NAME (method)))
1178 break;
1180 if (slot == len)
1182 /* We need a bigger method vector. */
1183 int new_len;
1184 tree new_vec;
1186 /* In the non-error case, we are processing a class
1187 definition. Double the size of the vector to give room
1188 for new methods. */
1189 if (!error_p)
1190 new_len = 2 * len;
1191 /* In the error case, the vector is already complete. We
1192 don't expect many errors, and the rest of the front-end
1193 will get confused if there are empty slots in the vector. */
1194 else
1195 new_len = len + 1;
1197 new_vec = make_tree_vec (new_len);
1198 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1199 (PTR) &TREE_VEC_ELT (new_vec, 0),
1200 len * sizeof (tree));
1201 len = new_len;
1202 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1205 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1207 /* Type conversion operators have to come before ordinary
1208 methods; add_conversions depends on this to speed up
1209 looking for conversion operators. So, if necessary, we
1210 slide some of the vector elements up. In theory, this
1211 makes this algorithm O(N^2) but we don't expect many
1212 conversion operators. */
1213 for (slot = 2; slot < len; ++slot)
1215 tree fn = TREE_VEC_ELT (method_vec, slot);
1217 if (!fn)
1218 /* There are no more entries in the vector, so we
1219 can insert the new conversion operator here. */
1220 break;
1222 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1223 /* We can insert the new function right at the
1224 SLOTth position. */
1225 break;
1228 if (!TREE_VEC_ELT (method_vec, slot))
1229 /* There is nothing in the Ith slot, so we can avoid
1230 moving anything. */
1232 else
1234 /* We know the last slot in the vector is empty
1235 because we know that at this point there's room
1236 for a new function. */
1237 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1238 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1239 (len - slot - 1) * sizeof (tree));
1240 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1245 if (template_class_depth (type))
1246 /* TYPE is a template class. Don't issue any errors now; wait
1247 until instantiation time to complain. */
1249 else
1251 tree fns;
1253 /* Check to see if we've already got this method. */
1254 for (fns = TREE_VEC_ELT (method_vec, slot);
1255 fns;
1256 fns = OVL_NEXT (fns))
1258 tree fn = OVL_CURRENT (fns);
1260 if (TREE_CODE (fn) != TREE_CODE (method))
1261 continue;
1263 if (TREE_CODE (method) != TEMPLATE_DECL)
1265 /* [over.load] Member function declarations with the
1266 same name and the same parameter types cannot be
1267 overloaded if any of them is a static member
1268 function declaration. */
1269 if ((DECL_STATIC_FUNCTION_P (fn)
1270 != DECL_STATIC_FUNCTION_P (method))
1271 || using)
1273 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1274 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1276 if (! DECL_STATIC_FUNCTION_P (fn))
1277 parms1 = TREE_CHAIN (parms1);
1278 if (! DECL_STATIC_FUNCTION_P (method))
1279 parms2 = TREE_CHAIN (parms2);
1281 if (compparms (parms1, parms2))
1283 if (using)
1284 /* Defer to the local function. */
1285 return;
1286 else
1287 cp_error ("`%#D' and `%#D' cannot be overloaded",
1288 fn, method);
1293 if (!decls_match (fn, method))
1294 continue;
1296 /* There has already been a declaration of this method
1297 or member template. */
1298 cp_error_at ("`%D' has already been declared in `%T'",
1299 method, type);
1301 /* We don't call duplicate_decls here to merge the
1302 declarations because that will confuse things if the
1303 methods have inline definitions. In particular, we
1304 will crash while processing the definitions. */
1305 return;
1309 /* Actually insert the new method. */
1310 TREE_VEC_ELT (method_vec, slot)
1311 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1313 /* Add the new binding. */
1314 if (!DECL_CONSTRUCTOR_P (method)
1315 && !DECL_DESTRUCTOR_P (method))
1316 push_class_level_binding (DECL_NAME (method),
1317 TREE_VEC_ELT (method_vec, slot));
1320 /* Subroutines of finish_struct. */
1322 /* Look through the list of fields for this struct, deleting
1323 duplicates as we go. This must be recursive to handle
1324 anonymous unions.
1326 FIELD is the field which may not appear anywhere in FIELDS.
1327 FIELD_PTR, if non-null, is the starting point at which
1328 chained deletions may take place.
1329 The value returned is the first acceptable entry found
1330 in FIELDS.
1332 Note that anonymous fields which are not of UNION_TYPE are
1333 not duplicates, they are just anonymous fields. This happens
1334 when we have unnamed bitfields, for example. */
1336 static tree
1337 delete_duplicate_fields_1 (field, fields)
1338 tree field, fields;
1340 tree x;
1341 tree prev = 0;
1342 if (DECL_NAME (field) == 0)
1344 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1345 return fields;
1347 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1348 fields = delete_duplicate_fields_1 (x, fields);
1349 return fields;
1351 else
1353 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1355 if (DECL_NAME (x) == 0)
1357 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1358 continue;
1359 TYPE_FIELDS (TREE_TYPE (x))
1360 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1361 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1363 if (prev == 0)
1364 fields = TREE_CHAIN (fields);
1365 else
1366 TREE_CHAIN (prev) = TREE_CHAIN (x);
1369 else if (TREE_CODE (field) == USING_DECL)
1370 /* A using declaration may is allowed to appear more than
1371 once. We'll prune these from the field list later, and
1372 handle_using_decl will complain about invalid multiple
1373 uses. */
1375 else if (DECL_NAME (field) == DECL_NAME (x))
1377 if (TREE_CODE (field) == CONST_DECL
1378 && TREE_CODE (x) == CONST_DECL)
1379 cp_error_at ("duplicate enum value `%D'", x);
1380 else if (TREE_CODE (field) == CONST_DECL
1381 || TREE_CODE (x) == CONST_DECL)
1382 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1384 else if (DECL_DECLARES_TYPE_P (field)
1385 && DECL_DECLARES_TYPE_P (x))
1387 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1388 continue;
1389 cp_error_at ("duplicate nested type `%D'", x);
1391 else if (DECL_DECLARES_TYPE_P (field)
1392 || DECL_DECLARES_TYPE_P (x))
1394 /* Hide tag decls. */
1395 if ((TREE_CODE (field) == TYPE_DECL
1396 && DECL_ARTIFICIAL (field))
1397 || (TREE_CODE (x) == TYPE_DECL
1398 && DECL_ARTIFICIAL (x)))
1399 continue;
1400 cp_error_at ("duplicate field `%D' (as type and non-type)",
1403 else
1404 cp_error_at ("duplicate member `%D'", x);
1405 if (prev == 0)
1406 fields = TREE_CHAIN (fields);
1407 else
1408 TREE_CHAIN (prev) = TREE_CHAIN (x);
1412 return fields;
1415 static void
1416 delete_duplicate_fields (fields)
1417 tree fields;
1419 tree x;
1420 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1421 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1424 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1425 legit, otherwise return 0. */
1427 static int
1428 alter_access (t, fdecl, access)
1429 tree t;
1430 tree fdecl;
1431 tree access;
1433 tree elem;
1435 if (!DECL_LANG_SPECIFIC (fdecl))
1436 retrofit_lang_decl (fdecl);
1438 if (DECL_DISCRIMINATOR_P (fdecl))
1439 abort ();
1441 elem = purpose_member (t, DECL_ACCESS (fdecl));
1442 if (elem)
1444 if (TREE_VALUE (elem) != access)
1446 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1447 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1448 else
1449 error ("conflicting access specifications for field `%s', ignored",
1450 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1452 else
1454 /* They're changing the access to the same thing they changed
1455 it to before. That's OK. */
1459 else
1461 enforce_access (t, fdecl);
1462 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1463 return 1;
1465 return 0;
1468 /* Process the USING_DECL, which is a member of T. */
1470 static void
1471 handle_using_decl (using_decl, t)
1472 tree using_decl;
1473 tree t;
1475 tree ctype = DECL_INITIAL (using_decl);
1476 tree name = DECL_NAME (using_decl);
1477 tree access
1478 = TREE_PRIVATE (using_decl) ? access_private_node
1479 : TREE_PROTECTED (using_decl) ? access_protected_node
1480 : access_public_node;
1481 tree fdecl, binfo;
1482 tree flist = NULL_TREE;
1483 tree old_value;
1485 binfo = binfo_or_else (ctype, t);
1486 if (! binfo)
1487 return;
1489 if (name == constructor_name (ctype)
1490 || name == constructor_name_full (ctype))
1492 cp_error_at ("`%D' names constructor", using_decl);
1493 return;
1495 if (name == constructor_name (t)
1496 || name == constructor_name_full (t))
1498 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1499 return;
1502 fdecl = lookup_member (binfo, name, 0, 0);
1504 if (!fdecl)
1506 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1507 return;
1510 if (BASELINK_P (fdecl))
1511 /* Ignore base type this came from. */
1512 fdecl = TREE_VALUE (fdecl);
1514 old_value = IDENTIFIER_CLASS_VALUE (name);
1515 if (old_value)
1517 if (is_overloaded_fn (old_value))
1518 old_value = OVL_CURRENT (old_value);
1520 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1521 /* OK */;
1522 else
1523 old_value = NULL_TREE;
1526 if (is_overloaded_fn (fdecl))
1527 flist = fdecl;
1529 if (! old_value)
1531 else if (is_overloaded_fn (old_value))
1533 if (flist)
1534 /* It's OK to use functions from a base when there are functions with
1535 the same name already present in the current class. */;
1536 else
1538 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1539 cp_error_at (" because of local method `%#D' with same name",
1540 OVL_CURRENT (old_value));
1541 return;
1544 else if (!DECL_ARTIFICIAL (old_value))
1546 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1547 cp_error_at (" because of local member `%#D' with same name", old_value);
1548 return;
1551 /* Make type T see field decl FDECL with access ACCESS.*/
1552 if (flist)
1553 for (; flist; flist = OVL_NEXT (flist))
1555 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1556 alter_access (t, OVL_CURRENT (flist), access);
1558 else
1559 alter_access (t, fdecl, access);
1562 /* Run through the base clases of T, updating
1563 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1564 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1565 the bases. */
1567 static void
1568 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1569 no_const_asn_ref_p)
1570 tree t;
1571 int *cant_have_default_ctor_p;
1572 int *cant_have_const_ctor_p;
1573 int *no_const_asn_ref_p;
1575 int n_baseclasses;
1576 int i;
1577 int seen_non_virtual_nearly_empty_base_p;
1578 tree binfos;
1580 binfos = TYPE_BINFO_BASETYPES (t);
1581 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1582 seen_non_virtual_nearly_empty_base_p = 0;
1584 /* An aggregate cannot have baseclasses. */
1585 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1587 for (i = 0; i < n_baseclasses; ++i)
1589 tree base_binfo;
1590 tree basetype;
1592 /* Figure out what base we're looking at. */
1593 base_binfo = TREE_VEC_ELT (binfos, i);
1594 basetype = TREE_TYPE (base_binfo);
1596 /* If the type of basetype is incomplete, then we already
1597 complained about that fact (and we should have fixed it up as
1598 well). */
1599 if (!COMPLETE_TYPE_P (basetype))
1601 int j;
1602 /* The base type is of incomplete type. It is
1603 probably best to pretend that it does not
1604 exist. */
1605 if (i == n_baseclasses-1)
1606 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1607 TREE_VEC_LENGTH (binfos) -= 1;
1608 n_baseclasses -= 1;
1609 for (j = i; j+1 < n_baseclasses; j++)
1610 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1611 continue;
1614 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1615 here because the case of virtual functions but non-virtual
1616 dtor is handled in finish_struct_1. */
1617 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1618 && TYPE_HAS_DESTRUCTOR (basetype))
1619 cp_warning ("base class `%#T' has a non-virtual destructor",
1620 basetype);
1622 /* If the base class doesn't have copy constructors or
1623 assignment operators that take const references, then the
1624 derived class cannot have such a member automatically
1625 generated. */
1626 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1627 *cant_have_const_ctor_p = 1;
1628 if (TYPE_HAS_ASSIGN_REF (basetype)
1629 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1630 *no_const_asn_ref_p = 1;
1631 /* Similarly, if the base class doesn't have a default
1632 constructor, then the derived class won't have an
1633 automatically generated default constructor. */
1634 if (TYPE_HAS_CONSTRUCTOR (basetype)
1635 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1637 *cant_have_default_ctor_p = 1;
1638 if (! TYPE_HAS_CONSTRUCTOR (t))
1639 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1640 basetype);
1643 if (TREE_VIA_VIRTUAL (base_binfo))
1644 /* A virtual base does not effect nearly emptiness. */
1646 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1648 if (seen_non_virtual_nearly_empty_base_p)
1649 /* And if there is more than one nearly empty base, then the
1650 derived class is not nearly empty either. */
1651 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1652 else
1653 /* Remember we've seen one. */
1654 seen_non_virtual_nearly_empty_base_p = 1;
1656 else if (!is_empty_class (basetype))
1657 /* If the base class is not empty or nearly empty, then this
1658 class cannot be nearly empty. */
1659 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1661 /* A lot of properties from the bases also apply to the derived
1662 class. */
1663 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1664 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1665 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1666 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1667 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1668 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1669 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1670 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1671 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1672 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1674 /* Derived classes can implicitly become COMified if their bases
1675 are COM. */
1676 if (CLASSTYPE_COM_INTERFACE (basetype))
1677 CLASSTYPE_COM_INTERFACE (t) = 1;
1678 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1680 cp_error
1681 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1682 t, basetype);
1683 CLASSTYPE_COM_INTERFACE (t) = 0;
1688 /* Binfo FROM is within a virtual heirarchy which is being reseated to
1689 TO. Move primary information from FROM to TO, and recursively traverse
1690 into FROM's bases. The heirarchy is dominated by TYPE. MAPPINGS is an
1691 assoc list of binfos that have already been reseated. */
1693 static void
1694 force_canonical_binfo_r (to, from, type, mappings)
1695 tree to;
1696 tree from;
1697 tree type;
1698 tree mappings;
1700 int i, n_baseclasses = BINFO_N_BASETYPES (from);
1702 my_friendly_assert (to != from, 20010905);
1703 BINFO_INDIRECT_PRIMARY_P (to)
1704 = BINFO_INDIRECT_PRIMARY_P (from);
1705 BINFO_INDIRECT_PRIMARY_P (from) = 0;
1706 BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1707 BINFO_UNSHARED_MARKED (from) = 0;
1708 BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1709 BINFO_LOST_PRIMARY_P (from) = 0;
1710 if (BINFO_PRIMARY_P (from))
1712 tree primary = BINFO_PRIMARY_BASE_OF (from);
1713 tree assoc;
1715 /* We might have just moved the primary base too, see if it's on our
1716 mappings. */
1717 assoc = purpose_member (primary, mappings);
1718 if (assoc)
1719 primary = TREE_VALUE (assoc);
1720 BINFO_PRIMARY_BASE_OF (to) = primary;
1721 BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1723 my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1724 20010104);
1725 mappings = tree_cons (from, to, mappings);
1727 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
1728 && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
1730 tree from_primary = get_primary_binfo (from);
1732 if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
1733 force_canonical_binfo (get_primary_binfo (to), from_primary,
1734 type, mappings);
1737 for (i = 0; i != n_baseclasses; i++)
1739 tree from_binfo = BINFO_BASETYPE (from, i);
1740 tree to_binfo = BINFO_BASETYPE (to, i);
1742 if (TREE_VIA_VIRTUAL (from_binfo))
1744 if (BINFO_PRIMARY_P (from_binfo) &&
1745 purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1746 /* This base is a primary of some binfo we have already
1747 reseated. We must reseat this one too. */
1748 force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1750 else
1751 force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1755 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1756 make TO the canonical binfo, within the heirarchy dominated by TYPE.
1757 MAPPINGS is an assoc list of binfos that have already been reseated.
1758 Adjust any non-virtual bases within FROM, and also move any virtual bases
1759 which are canonical. This complication arises because selecting primary
1760 bases walks in inheritance graph order, but we don't share binfos for
1761 virtual bases, hence we can fill in the primaries for a virtual base,
1762 and then discover that a later base requires the virtual as its
1763 primary. */
1765 static void
1766 force_canonical_binfo (to, from, type, mappings)
1767 tree to;
1768 tree from;
1769 tree type;
1770 tree mappings;
1772 tree assoc = purpose_member (BINFO_TYPE (to),
1773 CLASSTYPE_VBASECLASSES (type));
1774 if (TREE_VALUE (assoc) != to)
1776 TREE_VALUE (assoc) = to;
1777 force_canonical_binfo_r (to, from, type, mappings);
1781 /* Make BASE_BINFO the a primary virtual base within the hierarchy
1782 dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
1783 otherwise (because something else has already made it primary). */
1785 static tree
1786 mark_primary_virtual_base (base_binfo, type)
1787 tree base_binfo;
1788 tree type;
1790 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1792 if (BINFO_PRIMARY_P (shared_binfo))
1794 /* It's already allocated in the hierarchy. BINFO won't have a
1795 primary base in this hierachy, even though the complete object
1796 BINFO is for, would do. */
1797 return NULL_TREE;
1800 /* We need to make sure that the assoc list
1801 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1802 primary BINFO for the virtual base, as this is the one
1803 that'll really exist. */
1804 if (base_binfo != shared_binfo)
1805 force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1807 return base_binfo;
1810 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1811 base, then BINFO has no primary base in this graph. Called from
1812 mark_primary_bases. DATA is the most derived type. */
1814 static tree dfs_unshared_virtual_bases (binfo, data)
1815 tree binfo;
1816 void *data;
1818 tree t = (tree) data;
1820 if (!BINFO_UNSHARED_MARKED (binfo)
1821 && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1823 /* This morally virtual base has a primary base when it
1824 is a complete object. We need to locate the shared instance
1825 of this binfo in the type dominated by T. We duplicate the
1826 primary base information from there to here. */
1827 tree vbase;
1828 tree unshared_base;
1830 for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1831 vbase = BINFO_INHERITANCE_CHAIN (vbase))
1832 continue;
1833 unshared_base = get_original_base (binfo,
1834 binfo_for_vbase (BINFO_TYPE (vbase),
1835 t));
1836 my_friendly_assert (unshared_base != binfo, 20010612);
1837 BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1838 if (!BINFO_LOST_PRIMARY_P (binfo))
1839 BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1842 if (binfo != TYPE_BINFO (t))
1843 /* The vtable fields will have been copied when duplicating the
1844 base binfos. That information is bogus, make sure we don't try
1845 and use it. */
1846 BINFO_VTABLE (binfo) = NULL_TREE;
1848 /* If this is a virtual primary base, make sure its offset matches
1849 that which it is primary for. */
1850 if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
1851 binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
1853 tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
1854 BINFO_OFFSET (binfo));
1855 if (!integer_zerop (delta))
1856 propagate_binfo_offsets (binfo, delta, t);
1859 BINFO_UNSHARED_MARKED (binfo) = 0;
1860 return NULL;
1863 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1864 dominated by TYPE that are primary bases. */
1866 static void
1867 mark_primary_bases (type)
1868 tree type;
1870 tree binfo;
1872 /* Walk the bases in inheritance graph order. */
1873 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1875 tree base_binfo;
1877 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1878 /* Not a dynamic base. */
1879 continue;
1881 base_binfo = get_primary_binfo (binfo);
1883 if (TREE_VIA_VIRTUAL (base_binfo))
1884 base_binfo = mark_primary_virtual_base (base_binfo, type);
1886 if (base_binfo)
1887 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1888 else
1889 BINFO_LOST_PRIMARY_P (binfo) = 1;
1891 BINFO_UNSHARED_MARKED (binfo) = 1;
1893 /* There could remain unshared morally virtual bases which were not
1894 visited in the inheritance graph walk. These bases will have lost
1895 their virtual primary base (should they have one). We must now
1896 find them. Also we must fix up the BINFO_OFFSETs of primary
1897 virtual bases. We could not do that as we went along, as they
1898 were originally copied from the bases we inherited from by
1899 unshare_base_binfos. That may have decided differently about
1900 where a virtual primary base went. */
1901 dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1904 /* Make the BINFO the primary base of T. */
1906 static void
1907 set_primary_base (t, binfo, vfuns_p)
1908 tree t;
1909 tree binfo;
1910 int *vfuns_p;
1912 tree basetype;
1914 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1915 basetype = BINFO_TYPE (binfo);
1916 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1917 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1918 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1919 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1920 *vfuns_p = CLASSTYPE_VSIZE (basetype);
1923 /* Determine the primary class for T. */
1925 static void
1926 determine_primary_base (t, vfuns_p)
1927 tree t;
1928 int *vfuns_p;
1930 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1931 tree vbases;
1932 tree type_binfo;
1934 /* If there are no baseclasses, there is certainly no primary base. */
1935 if (n_baseclasses == 0)
1936 return;
1938 type_binfo = TYPE_BINFO (t);
1940 for (i = 0; i < n_baseclasses; i++)
1942 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1943 tree basetype = BINFO_TYPE (base_binfo);
1945 if (TYPE_CONTAINS_VPTR_P (basetype))
1947 /* Even a virtual baseclass can contain our RTTI
1948 information. But, we prefer a non-virtual polymorphic
1949 baseclass. */
1950 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1951 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1953 /* A virtual baseclass can't be the primary base under the
1954 old ABI. And under the new ABI we still prefer a
1955 non-virtual base. */
1956 if (TREE_VIA_VIRTUAL (base_binfo))
1957 continue;
1959 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1961 set_primary_base (t, base_binfo, vfuns_p);
1962 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1964 else
1966 tree vfields;
1968 /* Only add unique vfields, and flatten them out as we go. */
1969 for (vfields = CLASSTYPE_VFIELDS (basetype);
1970 vfields;
1971 vfields = TREE_CHAIN (vfields))
1972 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1973 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1974 CLASSTYPE_VFIELDS (t)
1975 = tree_cons (base_binfo,
1976 VF_BASETYPE_VALUE (vfields),
1977 CLASSTYPE_VFIELDS (t));
1982 if (!TYPE_VFIELD (t))
1983 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1985 /* Find the indirect primary bases - those virtual bases which are primary
1986 bases of something else in this hierarchy. */
1987 for (vbases = CLASSTYPE_VBASECLASSES (t);
1988 vbases;
1989 vbases = TREE_CHAIN (vbases))
1991 tree vbase_binfo = TREE_VALUE (vbases);
1993 /* See if this virtual base is an indirect primary base. To be so,
1994 it must be a primary base within the hierarchy of one of our
1995 direct bases. */
1996 for (i = 0; i < n_baseclasses; ++i)
1998 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1999 tree v;
2001 for (v = CLASSTYPE_VBASECLASSES (basetype);
2003 v = TREE_CHAIN (v))
2005 tree base_vbase = TREE_VALUE (v);
2007 if (BINFO_PRIMARY_P (base_vbase)
2008 && same_type_p (BINFO_TYPE (base_vbase),
2009 BINFO_TYPE (vbase_binfo)))
2011 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
2012 break;
2016 /* If we've discovered that this virtual base is an indirect
2017 primary base, then we can move on to the next virtual
2018 base. */
2019 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
2020 break;
2024 /* The new ABI allows for the use of a "nearly-empty" virtual base
2025 class as the primary base class if no non-virtual polymorphic
2026 base can be found. */
2027 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2029 /* If not NULL, this is the best primary base candidate we have
2030 found so far. */
2031 tree candidate = NULL_TREE;
2032 tree base_binfo;
2034 /* Loop over the baseclasses. */
2035 for (base_binfo = TYPE_BINFO (t);
2036 base_binfo;
2037 base_binfo = TREE_CHAIN (base_binfo))
2039 tree basetype = BINFO_TYPE (base_binfo);
2041 if (TREE_VIA_VIRTUAL (base_binfo)
2042 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
2044 /* If this is not an indirect primary base, then it's
2045 definitely our primary base. */
2046 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
2048 candidate = base_binfo;
2049 break;
2052 /* If this is an indirect primary base, it still could be
2053 our primary base -- unless we later find there's another
2054 nearly-empty virtual base that isn't an indirect
2055 primary base. */
2056 if (!candidate)
2057 candidate = base_binfo;
2061 /* If we've got a primary base, use it. */
2062 if (candidate)
2064 set_primary_base (t, candidate, vfuns_p);
2065 CLASSTYPE_VFIELDS (t)
2066 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
2070 /* Mark the primary base classes at this point. */
2071 mark_primary_bases (t);
2074 /* Set memoizing fields and bits of T (and its variants) for later
2075 use. */
2077 static void
2078 finish_struct_bits (t)
2079 tree t;
2081 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2083 /* Fix up variants (if any). */
2084 tree variants = TYPE_NEXT_VARIANT (t);
2085 while (variants)
2087 /* These fields are in the _TYPE part of the node, not in
2088 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2089 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
2090 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
2091 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2092 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2093 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2095 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
2096 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
2097 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2098 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2099 /* Copy whatever these are holding today. */
2100 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2101 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
2102 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2103 TYPE_SIZE (variants) = TYPE_SIZE (t);
2104 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
2105 variants = TYPE_NEXT_VARIANT (variants);
2108 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
2109 /* For a class w/o baseclasses, `finish_struct' has set
2110 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2111 definition). Similarly for a class whose base classes do not
2112 have vtables. When neither of these is true, we might have
2113 removed abstract virtuals (by providing a definition), added
2114 some (by declaring new ones), or redeclared ones from a base
2115 class. We need to recalculate what's really an abstract virtual
2116 at this point (by looking in the vtables). */
2117 get_pure_virtuals (t);
2119 if (n_baseclasses)
2121 /* Notice whether this class has type conversion functions defined. */
2122 tree binfo = TYPE_BINFO (t);
2123 tree binfos = BINFO_BASETYPES (binfo);
2124 tree basetype;
2126 for (i = n_baseclasses-1; i >= 0; i--)
2128 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2130 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
2134 /* If this type has a copy constructor, force its mode to be BLKmode, and
2135 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2136 be passed by invisible reference and prevent it from being returned in
2137 a register.
2139 Also do this if the class has BLKmode but can still be returned in
2140 registers, since function_cannot_inline_p won't let us inline
2141 functions returning such a type. This affects the HP-PA. */
2142 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
2143 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
2144 && CLASSTYPE_NON_AGGREGATE (t)))
2146 tree variants;
2147 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2148 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2150 TYPE_MODE (variants) = BLKmode;
2151 TREE_ADDRESSABLE (variants) = 1;
2156 /* Issue warnings about T having private constructors, but no friends,
2157 and so forth.
2159 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2160 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2161 non-private static member functions. */
2163 static void
2164 maybe_warn_about_overly_private_class (t)
2165 tree t;
2167 int has_member_fn = 0;
2168 int has_nonprivate_method = 0;
2169 tree fn;
2171 if (!warn_ctor_dtor_privacy
2172 /* If the class has friends, those entities might create and
2173 access instances, so we should not warn. */
2174 || (CLASSTYPE_FRIEND_CLASSES (t)
2175 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2176 /* We will have warned when the template was declared; there's
2177 no need to warn on every instantiation. */
2178 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2179 /* There's no reason to even consider warning about this
2180 class. */
2181 return;
2183 /* We only issue one warning, if more than one applies, because
2184 otherwise, on code like:
2186 class A {
2187 // Oops - forgot `public:'
2188 A();
2189 A(const A&);
2190 ~A();
2193 we warn several times about essentially the same problem. */
2195 /* Check to see if all (non-constructor, non-destructor) member
2196 functions are private. (Since there are no friends or
2197 non-private statics, we can't ever call any of the private member
2198 functions.) */
2199 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2200 /* We're not interested in compiler-generated methods; they don't
2201 provide any way to call private members. */
2202 if (!DECL_ARTIFICIAL (fn))
2204 if (!TREE_PRIVATE (fn))
2206 if (DECL_STATIC_FUNCTION_P (fn))
2207 /* A non-private static member function is just like a
2208 friend; it can create and invoke private member
2209 functions, and be accessed without a class
2210 instance. */
2211 return;
2213 has_nonprivate_method = 1;
2214 break;
2216 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2217 has_member_fn = 1;
2220 if (!has_nonprivate_method && has_member_fn)
2222 /* There are no non-private methods, and there's at least one
2223 private member function that isn't a constructor or
2224 destructor. (If all the private members are
2225 constructors/destructors we want to use the code below that
2226 issues error messages specifically referring to
2227 constructors/destructors.) */
2228 int i;
2229 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2230 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2231 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2232 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2234 has_nonprivate_method = 1;
2235 break;
2237 if (!has_nonprivate_method)
2239 cp_warning ("all member functions in class `%T' are private", t);
2240 return;
2244 /* Even if some of the member functions are non-private, the class
2245 won't be useful for much if all the constructors or destructors
2246 are private: such an object can never be created or destroyed. */
2247 if (TYPE_HAS_DESTRUCTOR (t))
2249 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2251 if (TREE_PRIVATE (dtor))
2253 cp_warning ("`%#T' only defines a private destructor and has no friends",
2255 return;
2259 if (TYPE_HAS_CONSTRUCTOR (t))
2261 int nonprivate_ctor = 0;
2263 /* If a non-template class does not define a copy
2264 constructor, one is defined for it, enabling it to avoid
2265 this warning. For a template class, this does not
2266 happen, and so we would normally get a warning on:
2268 template <class T> class C { private: C(); };
2270 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2271 complete non-template or fully instantiated classes have this
2272 flag set. */
2273 if (!TYPE_HAS_INIT_REF (t))
2274 nonprivate_ctor = 1;
2275 else
2276 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2278 fn = OVL_NEXT (fn))
2280 tree ctor = OVL_CURRENT (fn);
2281 /* Ideally, we wouldn't count copy constructors (or, in
2282 fact, any constructor that takes an argument of the
2283 class type as a parameter) because such things cannot
2284 be used to construct an instance of the class unless
2285 you already have one. But, for now at least, we're
2286 more generous. */
2287 if (! TREE_PRIVATE (ctor))
2289 nonprivate_ctor = 1;
2290 break;
2294 if (nonprivate_ctor == 0)
2296 cp_warning ("`%#T' only defines private constructors and has no friends",
2298 return;
2303 /* Function to help qsort sort FIELD_DECLs by name order. */
2305 static int
2306 field_decl_cmp (x, y)
2307 const tree *x, *y;
2309 if (DECL_NAME (*x) == DECL_NAME (*y))
2310 /* A nontype is "greater" than a type. */
2311 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2312 if (DECL_NAME (*x) == NULL_TREE)
2313 return -1;
2314 if (DECL_NAME (*y) == NULL_TREE)
2315 return 1;
2316 if (DECL_NAME (*x) < DECL_NAME (*y))
2317 return -1;
2318 return 1;
2321 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2323 static int
2324 method_name_cmp (m1, m2)
2325 const tree *m1, *m2;
2327 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2328 return 0;
2329 if (*m1 == NULL_TREE)
2330 return -1;
2331 if (*m2 == NULL_TREE)
2332 return 1;
2333 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2334 return -1;
2335 return 1;
2338 /* Warn about duplicate methods in fn_fields. Also compact method
2339 lists so that lookup can be made faster.
2341 Data Structure: List of method lists. The outer list is a
2342 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2343 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2344 links the entire list of methods for TYPE_METHODS. Friends are
2345 chained in the same way as member functions (? TREE_CHAIN or
2346 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2347 list. That allows them to be quickly deleted, and requires no
2348 extra storage.
2350 Sort methods that are not special (i.e., constructors, destructors,
2351 and type conversion operators) so that we can find them faster in
2352 search. */
2354 static void
2355 finish_struct_methods (t)
2356 tree t;
2358 tree fn_fields;
2359 tree method_vec;
2360 int slot, len;
2362 if (!TYPE_METHODS (t))
2364 /* Clear these for safety; perhaps some parsing error could set
2365 these incorrectly. */
2366 TYPE_HAS_CONSTRUCTOR (t) = 0;
2367 TYPE_HAS_DESTRUCTOR (t) = 0;
2368 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2369 return;
2372 method_vec = CLASSTYPE_METHOD_VEC (t);
2373 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2374 len = TREE_VEC_LENGTH (method_vec);
2376 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2377 and the next few with type conversion operators (if any). */
2378 for (fn_fields = TYPE_METHODS (t); fn_fields;
2379 fn_fields = TREE_CHAIN (fn_fields))
2380 /* Clear out this flag. */
2381 DECL_IN_AGGR_P (fn_fields) = 0;
2383 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2384 /* We thought there was a destructor, but there wasn't. Some
2385 parse errors cause this anomalous situation. */
2386 TYPE_HAS_DESTRUCTOR (t) = 0;
2388 /* Issue warnings about private constructors and such. If there are
2389 no methods, then some public defaults are generated. */
2390 maybe_warn_about_overly_private_class (t);
2392 /* Now sort the methods. */
2393 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2394 len--;
2395 TREE_VEC_LENGTH (method_vec) = len;
2397 /* The type conversion ops have to live at the front of the vec, so we
2398 can't sort them. */
2399 for (slot = 2; slot < len; ++slot)
2401 tree fn = TREE_VEC_ELT (method_vec, slot);
2403 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2404 break;
2406 if (len - slot > 1)
2407 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2408 (int (*)(const void *, const void *))method_name_cmp);
2411 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2413 void
2414 duplicate_tag_error (t)
2415 tree t;
2417 cp_error ("redefinition of `%#T'", t);
2418 cp_error_at ("previous definition of `%#T'", t);
2420 /* Pretend we haven't defined this type. */
2422 /* All of the component_decl's were TREE_CHAINed together in the parser.
2423 finish_struct_methods walks these chains and assembles all methods with
2424 the same base name into DECL_CHAINs. Now we don't need the parser chains
2425 anymore, so we unravel them. */
2427 /* This used to be in finish_struct, but it turns out that the
2428 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2429 things... */
2430 if (CLASSTYPE_METHOD_VEC (t))
2432 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2433 int i, len = TREE_VEC_LENGTH (method_vec);
2434 for (i = 0; i < len; i++)
2436 tree unchain = TREE_VEC_ELT (method_vec, i);
2437 while (unchain != NULL_TREE)
2439 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2440 unchain = OVL_NEXT (unchain);
2445 if (TYPE_LANG_SPECIFIC (t))
2447 tree binfo = TYPE_BINFO (t);
2448 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2449 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2450 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2451 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2453 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2454 BINFO_BASETYPES(binfo) = NULL_TREE;
2456 TYPE_BINFO (t) = binfo;
2457 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2458 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2459 TYPE_REDEFINED (t) = 1;
2460 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2461 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2463 TYPE_SIZE (t) = NULL_TREE;
2464 TYPE_MODE (t) = VOIDmode;
2465 TYPE_FIELDS (t) = NULL_TREE;
2466 TYPE_METHODS (t) = NULL_TREE;
2467 TYPE_VFIELD (t) = NULL_TREE;
2468 TYPE_CONTEXT (t) = NULL_TREE;
2469 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2471 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2472 TYPE_LANG_FLAG_0 (t) = 0;
2473 TYPE_LANG_FLAG_1 (t) = 0;
2474 TYPE_LANG_FLAG_2 (t) = 0;
2475 TYPE_LANG_FLAG_3 (t) = 0;
2476 TYPE_LANG_FLAG_4 (t) = 0;
2477 TYPE_LANG_FLAG_5 (t) = 0;
2478 TYPE_LANG_FLAG_6 (t) = 0;
2479 /* But not this one. */
2480 SET_IS_AGGR_TYPE (t, 1);
2483 /* Make the BINFO's vtablehave N entries, including RTTI entries,
2484 vbase and vcall offsets, etc. Set its type and call the backend
2485 to lay it out. */
2487 static void
2488 layout_vtable_decl (binfo, n)
2489 tree binfo;
2490 int n;
2492 tree atype;
2493 tree vtable;
2495 atype = build_cplus_array_type (vtable_entry_type,
2496 build_index_type (size_int (n - 1)));
2497 layout_type (atype);
2499 /* We may have to grow the vtable. */
2500 vtable = get_vtbl_decl_for_binfo (binfo);
2501 if (!same_type_p (TREE_TYPE (vtable), atype))
2503 TREE_TYPE (vtable) = atype;
2504 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2505 layout_decl (vtable, 0);
2507 /* At one time the vtable info was grabbed 2 words at a time. This
2508 fails on Sparc unless you have 8-byte alignment. */
2509 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2510 DECL_ALIGN (vtable));
2514 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2515 have the same signature. */
2518 same_signature_p (fndecl, base_fndecl)
2519 tree fndecl, base_fndecl;
2521 /* One destructor overrides another if they are the same kind of
2522 destructor. */
2523 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2524 && special_function_p (base_fndecl) == special_function_p (fndecl))
2525 return 1;
2526 /* But a non-destructor never overrides a destructor, nor vice
2527 versa, nor do different kinds of destructors override
2528 one-another. For example, a complete object destructor does not
2529 override a deleting destructor. */
2530 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2531 return 0;
2533 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2535 tree types, base_types;
2536 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2537 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2538 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2539 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2540 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2541 return 1;
2543 return 0;
2546 typedef struct find_final_overrider_data_s {
2547 /* The function for which we are trying to find a final overrider. */
2548 tree fn;
2549 /* The base class in which the function was declared. */
2550 tree declaring_base;
2551 /* The most derived class in the hierarchy. */
2552 tree most_derived_type;
2553 /* The final overriding function. */
2554 tree overriding_fn;
2555 /* The functions that we thought might be final overriders, but
2556 aren't. */
2557 tree candidates;
2558 /* The BINFO for the class in which the final overriding function
2559 appears. */
2560 tree overriding_base;
2561 } find_final_overrider_data;
2563 /* Called from find_final_overrider via dfs_walk. */
2565 static tree
2566 dfs_find_final_overrider (binfo, data)
2567 tree binfo;
2568 void *data;
2570 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2572 if (same_type_p (BINFO_TYPE (binfo),
2573 BINFO_TYPE (ffod->declaring_base))
2574 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2575 BINFO_OFFSET (ffod->declaring_base)))
2577 tree path;
2578 tree method;
2580 /* We haven't found an overrider yet. */
2581 method = NULL_TREE;
2582 /* We've found a path to the declaring base. Walk down the path
2583 looking for an overrider for FN. */
2584 for (path = reverse_path (binfo);
2585 path;
2586 path = TREE_CHAIN (path))
2588 method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2589 ffod->fn);
2590 if (method)
2591 break;
2594 /* If we found an overrider, record the overriding function, and
2595 the base from which it came. */
2596 if (path)
2598 tree base;
2600 /* Assume the path is non-virtual. See if there are any
2601 virtual bases from (but not including) the overrider up
2602 to and including the base where the function is
2603 defined. */
2604 for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
2605 if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
2607 base = ffod->declaring_base;
2608 break;
2611 /* If we didn't already have an overrider, or any
2612 candidates, then this function is the best candidate so
2613 far. */
2614 if (!ffod->overriding_fn && !ffod->candidates)
2616 ffod->overriding_fn = method;
2617 ffod->overriding_base = TREE_VALUE (path);
2619 else if (ffod->overriding_fn)
2621 /* We had a best overrider; let's see how this compares. */
2623 if (ffod->overriding_fn == method
2624 && (tree_int_cst_equal
2625 (BINFO_OFFSET (TREE_VALUE (path)),
2626 BINFO_OFFSET (ffod->overriding_base))))
2627 /* We found the same overrider we already have, and in the
2628 same place; it's still the best. */;
2629 else if (strictly_overrides (ffod->overriding_fn, method))
2630 /* The old function overrides this function; it's still the
2631 best. */;
2632 else if (strictly_overrides (method, ffod->overriding_fn))
2634 /* The new function overrides the old; it's now the
2635 best. */
2636 ffod->overriding_fn = method;
2637 ffod->overriding_base = TREE_VALUE (path);
2639 else
2641 /* Ambiguous. */
2642 ffod->candidates
2643 = build_tree_list (NULL_TREE,
2644 ffod->overriding_fn);
2645 if (method != ffod->overriding_fn)
2646 ffod->candidates
2647 = tree_cons (NULL_TREE, method, ffod->candidates);
2648 ffod->overriding_fn = NULL_TREE;
2649 ffod->overriding_base = NULL_TREE;
2652 else
2654 /* We had a list of ambiguous overrides; let's see how this
2655 new one compares. */
2657 tree candidates;
2658 bool incomparable = false;
2660 /* If there were previous candidates, and this function
2661 overrides all of them, then it is the new best
2662 candidate. */
2663 for (candidates = ffod->candidates;
2664 candidates;
2665 candidates = TREE_CHAIN (candidates))
2667 /* If the candidate overrides the METHOD, then we
2668 needn't worry about it any further. */
2669 if (strictly_overrides (TREE_VALUE (candidates),
2670 method))
2672 method = NULL_TREE;
2673 break;
2676 /* If the METHOD doesn't override the candidate,
2677 then it is incomporable. */
2678 if (!strictly_overrides (method,
2679 TREE_VALUE (candidates)))
2680 incomparable = true;
2683 /* If METHOD overrode all the candidates, then it is the
2684 new best candidate. */
2685 if (!candidates && !incomparable)
2687 ffod->overriding_fn = method;
2688 ffod->overriding_base = TREE_VALUE (path);
2689 ffod->candidates = NULL_TREE;
2691 /* If METHOD didn't override all the candidates, then it
2692 is another candidate. */
2693 else if (method && incomparable)
2694 ffod->candidates
2695 = tree_cons (NULL_TREE, method, ffod->candidates);
2700 return NULL_TREE;
2703 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2704 FN and whose TREE_VALUE is the binfo for the base where the
2705 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2706 base object in which FN is declared. */
2708 static tree
2709 find_final_overrider (t, binfo, fn)
2710 tree t;
2711 tree binfo;
2712 tree fn;
2714 find_final_overrider_data ffod;
2716 /* Getting this right is a little tricky. This is legal:
2718 struct S { virtual void f (); };
2719 struct T { virtual void f (); };
2720 struct U : public S, public T { };
2722 even though calling `f' in `U' is ambiguous. But,
2724 struct R { virtual void f(); };
2725 struct S : virtual public R { virtual void f (); };
2726 struct T : virtual public R { virtual void f (); };
2727 struct U : public S, public T { };
2729 is not -- there's no way to decide whether to put `S::f' or
2730 `T::f' in the vtable for `R'.
2732 The solution is to look at all paths to BINFO. If we find
2733 different overriders along any two, then there is a problem. */
2734 ffod.fn = fn;
2735 ffod.declaring_base = binfo;
2736 ffod.most_derived_type = t;
2737 ffod.overriding_fn = NULL_TREE;
2738 ffod.overriding_base = NULL_TREE;
2739 ffod.candidates = NULL_TREE;
2741 dfs_walk (TYPE_BINFO (t),
2742 dfs_find_final_overrider,
2743 NULL,
2744 &ffod);
2746 /* If there was no winner, issue an error message. */
2747 if (!ffod.overriding_fn)
2749 cp_error ("no unique final overrider for `%D' in `%T'", fn, t);
2750 return error_mark_node;
2753 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2756 /* Returns the function from the BINFO_VIRTUALS entry in T which matches
2757 the signature of FUNCTION_DECL FN, or NULL_TREE if none. In other words,
2758 the function that the slot in T's primary vtable points to. */
2760 static tree get_matching_virtual PARAMS ((tree, tree));
2761 static tree
2762 get_matching_virtual (t, fn)
2763 tree t, fn;
2765 tree f;
2767 for (f = BINFO_VIRTUALS (TYPE_BINFO (t)); f; f = TREE_CHAIN (f))
2768 if (same_signature_p (BV_FN (f), fn))
2769 return BV_FN (f);
2770 return NULL_TREE;
2773 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2774 dominated by T. FN has been overriden in BINFO; VIRTUALS points to the
2775 corresponding position in the BINFO_VIRTUALS list. */
2777 static void
2778 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2779 tree t;
2780 tree binfo;
2781 tree fn;
2782 tree *virtuals;
2784 tree b;
2785 tree overrider;
2786 tree delta;
2787 tree virtual_base;
2788 tree first_defn;
2790 /* Find the nearest primary base (possibly binfo itself) which defines
2791 this function; this is the class the caller will convert to when
2792 calling FN through BINFO. */
2793 for (b = binfo; ; b = get_primary_binfo (b))
2795 if (look_for_overrides_here (BINFO_TYPE (b), fn))
2796 break;
2798 first_defn = b;
2800 /* Find the final overrider. */
2801 overrider = find_final_overrider (t, b, fn);
2802 if (overrider == error_mark_node)
2803 return;
2805 /* Assume that we will produce a thunk that convert all the way to
2806 the final overrider, and not to an intermediate virtual base. */
2807 virtual_base = NULL_TREE;
2809 /* Under the new ABI, we will convert to an intermediate virtual
2810 base first, and then use the vcall offset located there to finish
2811 the conversion. */
2812 while (b)
2814 /* If we find the final overrider, then we can stop
2815 walking. */
2816 if (same_type_p (BINFO_TYPE (b),
2817 BINFO_TYPE (TREE_VALUE (overrider))))
2818 break;
2820 /* If we find a virtual base, and we haven't yet found the
2821 overrider, then there is a virtual base between the
2822 declaring base (first_defn) and the final overrider. */
2823 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2824 virtual_base = b;
2826 b = BINFO_INHERITANCE_CHAIN (b);
2829 /* Compute the constant adjustment to the `this' pointer. The
2830 `this' pointer, when this function is called, will point at BINFO
2831 (or one of its primary bases, which are at the same offset). */
2833 if (virtual_base)
2834 /* The `this' pointer needs to be adjusted from the declaration to
2835 the nearest virtual base. */
2836 delta = size_diffop (BINFO_OFFSET (virtual_base),
2837 BINFO_OFFSET (first_defn));
2838 else
2840 /* The `this' pointer needs to be adjusted from pointing to
2841 BINFO to pointing at the base where the final overrider
2842 appears. */
2843 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2844 BINFO_OFFSET (binfo));
2846 if (! integer_zerop (delta))
2848 /* We'll need a thunk. But if we have a (perhaps formerly)
2849 primary virtual base, we have a vcall slot for this function,
2850 so we can use it rather than create a non-virtual thunk. */
2852 b = get_primary_binfo (first_defn);
2853 for (; b; b = get_primary_binfo (b))
2855 tree f = get_matching_virtual (BINFO_TYPE (b), fn);
2856 if (!f)
2857 /* b doesn't have this function; no suitable vbase. */
2858 break;
2859 if (TREE_VIA_VIRTUAL (b))
2861 /* Found one; we can treat ourselves as a virtual base. */
2862 virtual_base = binfo;
2863 delta = size_zero_node;
2864 break;
2870 modify_vtable_entry (t,
2871 binfo,
2872 TREE_PURPOSE (overrider),
2873 delta,
2874 virtuals);
2876 if (virtual_base)
2877 BV_USE_VCALL_INDEX_P (*virtuals) = 1;
2880 /* Called from modify_all_vtables via dfs_walk. */
2882 static tree
2883 dfs_modify_vtables (binfo, data)
2884 tree binfo;
2885 void *data;
2887 if (/* There's no need to modify the vtable for a non-virtual
2888 primary base; we're not going to use that vtable anyhow.
2889 We do still need to do this for virtual primary bases, as they
2890 could become non-primary in a construction vtable. */
2891 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2892 /* Similarly, a base without a vtable needs no modification. */
2893 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2895 tree t;
2896 tree virtuals;
2897 tree old_virtuals;
2899 t = (tree) data;
2901 /* If we're supporting RTTI then we always need a new vtable to
2902 point to the RTTI information. Under the new ABI we may need
2903 a new vtable to contain vcall and vbase offsets. */
2904 make_new_vtable (t, binfo);
2906 /* Now, go through each of the virtual functions in the virtual
2907 function table for BINFO. Find the final overrider, and
2908 update the BINFO_VIRTUALS list appropriately. */
2909 for (virtuals = BINFO_VIRTUALS (binfo),
2910 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2911 virtuals;
2912 virtuals = TREE_CHAIN (virtuals),
2913 old_virtuals = TREE_CHAIN (old_virtuals))
2914 update_vtable_entry_for_fn (t,
2915 binfo,
2916 BV_FN (old_virtuals),
2917 &virtuals);
2920 SET_BINFO_MARKED (binfo);
2922 return NULL_TREE;
2925 /* Update all of the primary and secondary vtables for T. Create new
2926 vtables as required, and initialize their RTTI information. Each
2927 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2928 function from a base class; find and modify the appropriate entries
2929 to point to the overriding functions. Returns a list, in
2930 declaration order, of the functions that are overridden in this
2931 class, but do not appear in the primary base class vtable, and
2932 which should therefore be appended to the end of the vtable for T. */
2934 static tree
2935 modify_all_vtables (t, vfuns_p, overridden_virtuals)
2936 tree t;
2937 int *vfuns_p;
2938 tree overridden_virtuals;
2940 tree binfo;
2942 binfo = TYPE_BINFO (t);
2944 /* Update all of the vtables. */
2945 dfs_walk (binfo,
2946 dfs_modify_vtables,
2947 dfs_unmarked_real_bases_queue_p,
2949 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2951 /* If we should include overriding functions for secondary vtables
2952 in our primary vtable, add them now. */
2953 if (all_overridden_vfuns_in_vtables_p ())
2955 tree *fnsp = &overridden_virtuals;
2957 while (*fnsp)
2959 tree fn = TREE_VALUE (*fnsp);
2961 if (!BINFO_VIRTUALS (binfo)
2962 || !value_member (fn, BINFO_VIRTUALS (binfo)))
2964 /* Set the vtable index. */
2965 set_vindex (fn, vfuns_p);
2966 /* We don't need to convert to a base class when calling
2967 this function. */
2968 DECL_VIRTUAL_CONTEXT (fn) = t;
2970 /* We don't need to adjust the `this' pointer when
2971 calling this function. */
2972 BV_DELTA (*fnsp) = integer_zero_node;
2973 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2975 /* This is an overridden function not already in our
2976 vtable. Keep it. */
2977 fnsp = &TREE_CHAIN (*fnsp);
2979 else
2980 /* We've already got an entry for this function. Skip
2981 it. */
2982 *fnsp = TREE_CHAIN (*fnsp);
2985 else
2986 overridden_virtuals = NULL_TREE;
2988 return overridden_virtuals;
2991 /* Here, we already know that they match in every respect.
2992 All we have to check is where they had their declarations. */
2994 static int
2995 strictly_overrides (fndecl1, fndecl2)
2996 tree fndecl1, fndecl2;
2998 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
2999 DECL_CONTEXT (fndecl1),
3000 0, (tree *)0);
3001 if (distance == -2 || distance > 0)
3002 return 1;
3003 return 0;
3006 /* Get the base virtual function declarations in T that are either
3007 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
3008 the overrider/hider. */
3010 static tree
3011 get_basefndecls (fndecl, t)
3012 tree fndecl, t;
3014 tree methods = TYPE_METHODS (t);
3015 tree base_fndecls = NULL_TREE;
3016 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3017 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3019 while (methods)
3021 if (TREE_CODE (methods) == FUNCTION_DECL
3022 && DECL_VINDEX (methods) != NULL_TREE
3023 && DECL_NAME (fndecl) == DECL_NAME (methods))
3024 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
3026 methods = TREE_CHAIN (methods);
3029 if (base_fndecls)
3030 return base_fndecls;
3032 for (i = 0; i < n_baseclasses; i++)
3034 tree base_binfo = TREE_VEC_ELT (binfos, i);
3035 tree basetype = BINFO_TYPE (base_binfo);
3037 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3038 base_fndecls);
3041 return base_fndecls;
3044 /* Mark the functions that have been hidden with their overriders.
3045 Since we start out with all functions already marked with a hider,
3046 no need to mark functions that are just hidden.
3048 Subroutine of warn_hidden. */
3050 static void
3051 mark_overriders (fndecl, base_fndecls)
3052 tree fndecl, base_fndecls;
3054 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3055 if (same_signature_p (fndecl, TREE_VALUE (base_fndecls)))
3056 TREE_PURPOSE (base_fndecls) = fndecl;
3059 /* If this declaration supersedes the declaration of
3060 a method declared virtual in the base class, then
3061 mark this field as being virtual as well. */
3063 static void
3064 check_for_override (decl, ctype)
3065 tree decl, ctype;
3067 if (TREE_CODE (decl) == TEMPLATE_DECL)
3068 /* In [temp.mem] we have:
3070 A specialization of a member function template does not
3071 override a virtual function from a base class. */
3072 return;
3073 if ((DECL_DESTRUCTOR_P (decl)
3074 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
3075 && look_for_overrides (ctype, decl)
3076 && !DECL_STATIC_FUNCTION_P (decl))
3078 /* Set DECL_VINDEX to a value that is neither an
3079 INTEGER_CST nor the error_mark_node so that
3080 add_virtual_function will realize this is an
3081 overriding function. */
3082 DECL_VINDEX (decl) = decl;
3084 if (DECL_VIRTUAL_P (decl))
3086 if (DECL_VINDEX (decl) == NULL_TREE)
3087 DECL_VINDEX (decl) = error_mark_node;
3088 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3092 /* Warn about hidden virtual functions that are not overridden in t.
3093 We know that constructors and destructors don't apply. */
3095 void
3096 warn_hidden (t)
3097 tree t;
3099 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3100 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3101 int i;
3103 /* We go through each separately named virtual function. */
3104 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3106 tree fns = TREE_VEC_ELT (method_vec, i);
3107 tree fndecl = NULL_TREE;
3109 tree base_fndecls = NULL_TREE;
3110 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3111 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3113 /* First see if we have any virtual functions in this batch. */
3114 for (; fns; fns = OVL_NEXT (fns))
3116 fndecl = OVL_CURRENT (fns);
3117 if (DECL_VINDEX (fndecl))
3118 break;
3121 if (fns == NULL_TREE)
3122 continue;
3124 /* First we get a list of all possible functions that might be
3125 hidden from each base class. */
3126 for (i = 0; i < n_baseclasses; i++)
3128 tree base_binfo = TREE_VEC_ELT (binfos, i);
3129 tree basetype = BINFO_TYPE (base_binfo);
3131 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3132 base_fndecls);
3135 fns = OVL_NEXT (fns);
3137 /* ...then mark up all the base functions with overriders, preferring
3138 overriders to hiders. */
3139 if (base_fndecls)
3140 for (; fns; fns = OVL_NEXT (fns))
3142 fndecl = OVL_CURRENT (fns);
3143 if (DECL_VINDEX (fndecl))
3144 mark_overriders (fndecl, base_fndecls);
3147 /* Now give a warning for all base functions without overriders,
3148 as they are hidden. */
3149 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3150 if (!same_signature_p (TREE_PURPOSE (base_fndecls),
3151 TREE_VALUE (base_fndecls)))
3153 /* Here we know it is a hider, and no overrider exists. */
3154 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3155 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3160 /* Check for things that are invalid. There are probably plenty of other
3161 things we should check for also. */
3163 static void
3164 finish_struct_anon (t)
3165 tree t;
3167 tree field;
3169 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3171 if (TREE_STATIC (field))
3172 continue;
3173 if (TREE_CODE (field) != FIELD_DECL)
3174 continue;
3176 if (DECL_NAME (field) == NULL_TREE
3177 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3179 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3180 for (; elt; elt = TREE_CHAIN (elt))
3182 if (DECL_ARTIFICIAL (elt))
3183 continue;
3185 if (DECL_NAME (elt) == constructor_name (t))
3186 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
3187 elt);
3189 if (TREE_CODE (elt) != FIELD_DECL)
3191 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3192 elt);
3193 continue;
3196 if (TREE_PRIVATE (elt))
3197 cp_pedwarn_at ("private member `%#D' in anonymous union",
3198 elt);
3199 else if (TREE_PROTECTED (elt))
3200 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3201 elt);
3203 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3204 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3210 /* Create default constructors, assignment operators, and so forth for
3211 the type indicated by T, if they are needed.
3212 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3213 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
3214 class cannot have a default constructor, copy constructor taking a
3215 const reference argument, or an assignment operator taking a const
3216 reference, respectively. If a virtual destructor is created, its
3217 DECL is returned; otherwise the return value is NULL_TREE. */
3219 static tree
3220 add_implicitly_declared_members (t, cant_have_default_ctor,
3221 cant_have_const_cctor,
3222 cant_have_const_assignment)
3223 tree t;
3224 int cant_have_default_ctor;
3225 int cant_have_const_cctor;
3226 int cant_have_const_assignment;
3228 tree default_fn;
3229 tree implicit_fns = NULL_TREE;
3230 tree virtual_dtor = NULL_TREE;
3231 tree *f;
3233 /* Destructor. */
3234 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3236 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
3237 check_for_override (default_fn, t);
3239 /* If we couldn't make it work, then pretend we didn't need it. */
3240 if (default_fn == void_type_node)
3241 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
3242 else
3244 TREE_CHAIN (default_fn) = implicit_fns;
3245 implicit_fns = default_fn;
3247 if (DECL_VINDEX (default_fn))
3248 virtual_dtor = default_fn;
3251 else
3252 /* Any non-implicit destructor is non-trivial. */
3253 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3255 /* Default constructor. */
3256 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3258 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
3259 TREE_CHAIN (default_fn) = implicit_fns;
3260 implicit_fns = default_fn;
3263 /* Copy constructor. */
3264 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3266 /* ARM 12.18: You get either X(X&) or X(const X&), but
3267 not both. --Chip */
3268 default_fn
3269 = implicitly_declare_fn (sfk_copy_constructor, t,
3270 /*const_p=*/!cant_have_const_cctor);
3271 TREE_CHAIN (default_fn) = implicit_fns;
3272 implicit_fns = default_fn;
3275 /* Assignment operator. */
3276 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3278 default_fn
3279 = implicitly_declare_fn (sfk_assignment_operator, t,
3280 /*const_p=*/!cant_have_const_assignment);
3281 TREE_CHAIN (default_fn) = implicit_fns;
3282 implicit_fns = default_fn;
3285 /* Now, hook all of the new functions on to TYPE_METHODS,
3286 and add them to the CLASSTYPE_METHOD_VEC. */
3287 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3288 add_method (t, *f, /*error_p=*/0);
3289 *f = TYPE_METHODS (t);
3290 TYPE_METHODS (t) = implicit_fns;
3292 return virtual_dtor;
3295 /* Subroutine of finish_struct_1. Recursively count the number of fields
3296 in TYPE, including anonymous union members. */
3298 static int
3299 count_fields (fields)
3300 tree fields;
3302 tree x;
3303 int n_fields = 0;
3304 for (x = fields; x; x = TREE_CHAIN (x))
3306 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3307 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3308 else
3309 n_fields += 1;
3311 return n_fields;
3314 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3315 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3317 static int
3318 add_fields_to_vec (fields, field_vec, idx)
3319 tree fields, field_vec;
3320 int idx;
3322 tree x;
3323 for (x = fields; x; x = TREE_CHAIN (x))
3325 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3326 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3327 else
3328 TREE_VEC_ELT (field_vec, idx++) = x;
3330 return idx;
3333 /* FIELD is a bit-field. We are finishing the processing for its
3334 enclosing type. Issue any appropriate messages and set appropriate
3335 flags. */
3337 static void
3338 check_bitfield_decl (field)
3339 tree field;
3341 tree type = TREE_TYPE (field);
3342 tree w = NULL_TREE;
3344 /* Detect invalid bit-field type. */
3345 if (DECL_INITIAL (field)
3346 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3348 cp_error_at ("bit-field `%#D' with non-integral type", field);
3349 w = error_mark_node;
3352 /* Detect and ignore out of range field width. */
3353 if (DECL_INITIAL (field))
3355 w = DECL_INITIAL (field);
3357 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3358 STRIP_NOPS (w);
3360 /* detect invalid field size. */
3361 if (TREE_CODE (w) == CONST_DECL)
3362 w = DECL_INITIAL (w);
3363 else
3364 w = decl_constant_value (w);
3366 if (TREE_CODE (w) != INTEGER_CST)
3368 cp_error_at ("bit-field `%D' width not an integer constant",
3369 field);
3370 w = error_mark_node;
3372 else if (tree_int_cst_sgn (w) < 0)
3374 cp_error_at ("negative width in bit-field `%D'", field);
3375 w = error_mark_node;
3377 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3379 cp_error_at ("zero width for bit-field `%D'", field);
3380 w = error_mark_node;
3382 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3383 && TREE_CODE (type) != ENUMERAL_TYPE
3384 && TREE_CODE (type) != BOOLEAN_TYPE)
3385 cp_warning_at ("width of `%D' exceeds its type", field);
3386 else if (TREE_CODE (type) == ENUMERAL_TYPE
3387 && (0 > compare_tree_int (w,
3388 min_precision (TYPE_MIN_VALUE (type),
3389 TREE_UNSIGNED (type)))
3390 || 0 > compare_tree_int (w,
3391 min_precision
3392 (TYPE_MAX_VALUE (type),
3393 TREE_UNSIGNED (type)))))
3394 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3395 field, type);
3398 /* Remove the bit-field width indicator so that the rest of the
3399 compiler does not treat that value as an initializer. */
3400 DECL_INITIAL (field) = NULL_TREE;
3402 if (w != error_mark_node)
3404 DECL_SIZE (field) = convert (bitsizetype, w);
3405 DECL_BIT_FIELD (field) = 1;
3407 if (integer_zerop (w))
3409 #ifdef EMPTY_FIELD_BOUNDARY
3410 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3411 EMPTY_FIELD_BOUNDARY);
3412 #endif
3413 #ifdef PCC_BITFIELD_TYPE_MATTERS
3414 if (PCC_BITFIELD_TYPE_MATTERS)
3416 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3417 TYPE_ALIGN (type));
3418 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3420 #endif
3423 else
3425 /* Non-bit-fields are aligned for their type. */
3426 DECL_BIT_FIELD (field) = 0;
3427 CLEAR_DECL_C_BIT_FIELD (field);
3428 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3429 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3433 /* FIELD is a non bit-field. We are finishing the processing for its
3434 enclosing type T. Issue any appropriate messages and set appropriate
3435 flags. */
3437 static void
3438 check_field_decl (field, t, cant_have_const_ctor,
3439 cant_have_default_ctor, no_const_asn_ref,
3440 any_default_members)
3441 tree field;
3442 tree t;
3443 int *cant_have_const_ctor;
3444 int *cant_have_default_ctor;
3445 int *no_const_asn_ref;
3446 int *any_default_members;
3448 tree type = strip_array_types (TREE_TYPE (field));
3450 /* An anonymous union cannot contain any fields which would change
3451 the settings of CANT_HAVE_CONST_CTOR and friends. */
3452 if (ANON_UNION_TYPE_P (type))
3454 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3455 structs. So, we recurse through their fields here. */
3456 else if (ANON_AGGR_TYPE_P (type))
3458 tree fields;
3460 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3461 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3462 check_field_decl (fields, t, cant_have_const_ctor,
3463 cant_have_default_ctor, no_const_asn_ref,
3464 any_default_members);
3466 /* Check members with class type for constructors, destructors,
3467 etc. */
3468 else if (CLASS_TYPE_P (type))
3470 /* Never let anything with uninheritable virtuals
3471 make it through without complaint. */
3472 abstract_virtuals_error (field, type);
3474 if (TREE_CODE (t) == UNION_TYPE)
3476 if (TYPE_NEEDS_CONSTRUCTING (type))
3477 cp_error_at ("member `%#D' with constructor not allowed in union",
3478 field);
3479 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3480 cp_error_at ("member `%#D' with destructor not allowed in union",
3481 field);
3482 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3483 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3484 field);
3486 else
3488 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3489 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3490 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3491 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3492 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3495 if (!TYPE_HAS_CONST_INIT_REF (type))
3496 *cant_have_const_ctor = 1;
3498 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3499 *no_const_asn_ref = 1;
3501 if (TYPE_HAS_CONSTRUCTOR (type)
3502 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3503 *cant_have_default_ctor = 1;
3505 if (DECL_INITIAL (field) != NULL_TREE)
3507 /* `build_class_init_list' does not recognize
3508 non-FIELD_DECLs. */
3509 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3510 cp_error_at ("multiple fields in union `%T' initialized");
3511 *any_default_members = 1;
3514 /* Non-bit-fields are aligned for their type, except packed fields
3515 which require only BITS_PER_UNIT alignment. */
3516 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3517 (DECL_PACKED (field)
3518 ? BITS_PER_UNIT
3519 : TYPE_ALIGN (TREE_TYPE (field))));
3520 if (! DECL_PACKED (field))
3521 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3524 /* Check the data members (both static and non-static), class-scoped
3525 typedefs, etc., appearing in the declaration of T. Issue
3526 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3527 declaration order) of access declarations; each TREE_VALUE in this
3528 list is a USING_DECL.
3530 In addition, set the following flags:
3532 EMPTY_P
3533 The class is empty, i.e., contains no non-static data members.
3535 CANT_HAVE_DEFAULT_CTOR_P
3536 This class cannot have an implicitly generated default
3537 constructor.
3539 CANT_HAVE_CONST_CTOR_P
3540 This class cannot have an implicitly generated copy constructor
3541 taking a const reference.
3543 CANT_HAVE_CONST_ASN_REF
3544 This class cannot have an implicitly generated assignment
3545 operator taking a const reference.
3547 All of these flags should be initialized before calling this
3548 function.
3550 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3551 fields can be added by adding to this chain. */
3553 static void
3554 check_field_decls (t, access_decls, empty_p,
3555 cant_have_default_ctor_p, cant_have_const_ctor_p,
3556 no_const_asn_ref_p)
3557 tree t;
3558 tree *access_decls;
3559 int *empty_p;
3560 int *cant_have_default_ctor_p;
3561 int *cant_have_const_ctor_p;
3562 int *no_const_asn_ref_p;
3564 tree *field;
3565 tree *next;
3566 int has_pointers;
3567 int any_default_members;
3569 /* First, delete any duplicate fields. */
3570 delete_duplicate_fields (TYPE_FIELDS (t));
3572 /* Assume there are no access declarations. */
3573 *access_decls = NULL_TREE;
3574 /* Assume this class has no pointer members. */
3575 has_pointers = 0;
3576 /* Assume none of the members of this class have default
3577 initializations. */
3578 any_default_members = 0;
3580 for (field = &TYPE_FIELDS (t); *field; field = next)
3582 tree x = *field;
3583 tree type = TREE_TYPE (x);
3585 GNU_xref_member (current_class_name, x);
3587 next = &TREE_CHAIN (x);
3589 if (TREE_CODE (x) == FIELD_DECL)
3591 DECL_PACKED (x) |= TYPE_PACKED (t);
3593 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3594 /* We don't treat zero-width bitfields as making a class
3595 non-empty. */
3597 else
3599 /* The class is non-empty. */
3600 *empty_p = 0;
3601 /* The class is not even nearly empty. */
3602 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3606 if (TREE_CODE (x) == USING_DECL)
3608 /* Prune the access declaration from the list of fields. */
3609 *field = TREE_CHAIN (x);
3611 /* Save the access declarations for our caller. */
3612 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3614 /* Since we've reset *FIELD there's no reason to skip to the
3615 next field. */
3616 next = field;
3617 continue;
3620 if (TREE_CODE (x) == TYPE_DECL
3621 || TREE_CODE (x) == TEMPLATE_DECL)
3622 continue;
3624 /* If we've gotten this far, it's a data member, possibly static,
3625 or an enumerator. */
3627 DECL_CONTEXT (x) = t;
3629 /* ``A local class cannot have static data members.'' ARM 9.4 */
3630 if (current_function_decl && TREE_STATIC (x))
3631 cp_error_at ("field `%D' in local class cannot be static", x);
3633 /* Perform error checking that did not get done in
3634 grokdeclarator. */
3635 if (TREE_CODE (type) == FUNCTION_TYPE)
3637 cp_error_at ("field `%D' invalidly declared function type",
3639 type = build_pointer_type (type);
3640 TREE_TYPE (x) = type;
3642 else if (TREE_CODE (type) == METHOD_TYPE)
3644 cp_error_at ("field `%D' invalidly declared method type", x);
3645 type = build_pointer_type (type);
3646 TREE_TYPE (x) = type;
3648 else if (TREE_CODE (type) == OFFSET_TYPE)
3650 cp_error_at ("field `%D' invalidly declared offset type", x);
3651 type = build_pointer_type (type);
3652 TREE_TYPE (x) = type;
3655 if (type == error_mark_node)
3656 continue;
3658 /* When this goes into scope, it will be a non-local reference. */
3659 DECL_NONLOCAL (x) = 1;
3661 if (TREE_CODE (x) == CONST_DECL)
3662 continue;
3664 if (TREE_CODE (x) == VAR_DECL)
3666 if (TREE_CODE (t) == UNION_TYPE)
3667 /* Unions cannot have static members. */
3668 cp_error_at ("field `%D' declared static in union", x);
3670 continue;
3673 /* Now it can only be a FIELD_DECL. */
3675 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3676 CLASSTYPE_NON_AGGREGATE (t) = 1;
3678 /* If this is of reference type, check if it needs an init.
3679 Also do a little ANSI jig if necessary. */
3680 if (TREE_CODE (type) == REFERENCE_TYPE)
3682 CLASSTYPE_NON_POD_P (t) = 1;
3683 if (DECL_INITIAL (x) == NULL_TREE)
3684 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3686 /* ARM $12.6.2: [A member initializer list] (or, for an
3687 aggregate, initialization by a brace-enclosed list) is the
3688 only way to initialize nonstatic const and reference
3689 members. */
3690 *cant_have_default_ctor_p = 1;
3691 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3693 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3694 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3697 type = strip_array_types (type);
3699 if (TREE_CODE (type) == POINTER_TYPE)
3700 has_pointers = 1;
3702 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3703 CLASSTYPE_HAS_MUTABLE (t) = 1;
3705 if (! pod_type_p (type))
3706 /* DR 148 now allows pointers to members (which are POD themselves),
3707 to be allowed in POD structs. */
3708 CLASSTYPE_NON_POD_P (t) = 1;
3710 /* If any field is const, the structure type is pseudo-const. */
3711 if (CP_TYPE_CONST_P (type))
3713 C_TYPE_FIELDS_READONLY (t) = 1;
3714 if (DECL_INITIAL (x) == NULL_TREE)
3715 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3717 /* ARM $12.6.2: [A member initializer list] (or, for an
3718 aggregate, initialization by a brace-enclosed list) is the
3719 only way to initialize nonstatic const and reference
3720 members. */
3721 *cant_have_default_ctor_p = 1;
3722 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3724 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3725 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3727 /* A field that is pseudo-const makes the structure likewise. */
3728 else if (IS_AGGR_TYPE (type))
3730 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3731 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3732 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3735 /* Core issue 80: A nonstatic data member is required to have a
3736 different name from the class iff the class has a
3737 user-defined constructor. */
3738 if (DECL_NAME (x) == constructor_name (t)
3739 && TYPE_HAS_CONSTRUCTOR (t))
3740 cp_pedwarn_at ("field `%#D' with same name as class", x);
3742 /* We set DECL_C_BIT_FIELD in grokbitfield.
3743 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3744 if (DECL_C_BIT_FIELD (x))
3745 check_bitfield_decl (x);
3746 else
3747 check_field_decl (x, t,
3748 cant_have_const_ctor_p,
3749 cant_have_default_ctor_p,
3750 no_const_asn_ref_p,
3751 &any_default_members);
3754 /* Effective C++ rule 11. */
3755 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3756 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3758 cp_warning ("`%#T' has pointer data members", t);
3760 if (! TYPE_HAS_INIT_REF (t))
3762 cp_warning (" but does not override `%T(const %T&)'", t, t);
3763 if (! TYPE_HAS_ASSIGN_REF (t))
3764 cp_warning (" or `operator=(const %T&)'", t);
3766 else if (! TYPE_HAS_ASSIGN_REF (t))
3767 cp_warning (" but does not override `operator=(const %T&)'", t);
3771 /* Check anonymous struct/anonymous union fields. */
3772 finish_struct_anon (t);
3774 /* We've built up the list of access declarations in reverse order.
3775 Fix that now. */
3776 *access_decls = nreverse (*access_decls);
3779 /* Return a FIELD_DECL for a pointer-to-virtual-table or
3780 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
3781 field are as indicated. The CLASS_TYPE in which this field occurs
3782 is also indicated. FCONTEXT is the type that is needed for the debug
3783 info output routines. *EMPTY_P is set to a non-zero value by this
3784 function to indicate that a class containing this field is
3785 non-empty. */
3787 static tree
3788 build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
3789 empty_p)
3790 tree name;
3791 tree assembler_name;
3792 tree type;
3793 tree class_type;
3794 tree fcontext;
3795 int *empty_p;
3797 tree field;
3799 /* This class is non-empty. */
3800 *empty_p = 0;
3802 /* Build the FIELD_DECL. */
3803 field = build_decl (FIELD_DECL, name, type);
3804 SET_DECL_ASSEMBLER_NAME (field, assembler_name);
3805 DECL_VIRTUAL_P (field) = 1;
3806 DECL_ARTIFICIAL (field) = 1;
3807 DECL_FIELD_CONTEXT (field) = class_type;
3808 DECL_FCONTEXT (field) = fcontext;
3809 DECL_ALIGN (field) = TYPE_ALIGN (type);
3810 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (type);
3812 /* Return it. */
3813 return field;
3816 /* If TYPE is an empty class type, records its OFFSET in the table of
3817 OFFSETS. */
3819 static int
3820 record_subobject_offset (type, offset, offsets)
3821 tree type;
3822 tree offset;
3823 splay_tree offsets;
3825 splay_tree_node n;
3827 if (!is_empty_class (type))
3828 return 0;
3830 /* Record the location of this empty object in OFFSETS. */
3831 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3832 if (!n)
3833 n = splay_tree_insert (offsets,
3834 (splay_tree_key) offset,
3835 (splay_tree_value) NULL_TREE);
3836 n->value = ((splay_tree_value)
3837 tree_cons (NULL_TREE,
3838 type,
3839 (tree) n->value));
3841 return 0;
3844 /* Returns non-zero if TYPE is an empty class type and there is
3845 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3847 static int
3848 check_subobject_offset (type, offset, offsets)
3849 tree type;
3850 tree offset;
3851 splay_tree offsets;
3853 splay_tree_node n;
3854 tree t;
3856 if (!is_empty_class (type))
3857 return 0;
3859 /* Record the location of this empty object in OFFSETS. */
3860 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3861 if (!n)
3862 return 0;
3864 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3865 if (same_type_p (TREE_VALUE (t), type))
3866 return 1;
3868 return 0;
3871 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3872 F for every subobject, passing it the type, offset, and table of
3873 OFFSETS. If VBASES_P is non-zero, then even virtual non-primary
3874 bases should be traversed; otherwise, they are ignored.
3876 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3877 than MAX_OFFSET will not be walked.
3879 If F returns a non-zero value, the traversal ceases, and that value
3880 is returned. Otherwise, returns zero. */
3882 static int
3883 walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
3884 tree type;
3885 subobject_offset_fn f;
3886 tree offset;
3887 splay_tree offsets;
3888 tree max_offset;
3889 int vbases_p;
3891 int r = 0;
3893 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3894 stop. */
3895 if (max_offset && INT_CST_LT (max_offset, offset))
3896 return 0;
3898 if (CLASS_TYPE_P (type))
3900 tree field;
3901 int i;
3903 /* Record the location of TYPE. */
3904 r = (*f) (type, offset, offsets);
3905 if (r)
3906 return r;
3908 /* Iterate through the direct base classes of TYPE. */
3909 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3911 tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3913 if (!vbases_p
3914 && TREE_VIA_VIRTUAL (binfo)
3915 && !BINFO_PRIMARY_P (binfo))
3916 continue;
3918 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3920 size_binop (PLUS_EXPR,
3921 offset,
3922 BINFO_OFFSET (binfo)),
3923 offsets,
3924 max_offset,
3925 vbases_p);
3926 if (r)
3927 return r;
3930 /* Iterate through the fields of TYPE. */
3931 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3932 if (TREE_CODE (field) == FIELD_DECL)
3934 r = walk_subobject_offsets (TREE_TYPE (field),
3936 size_binop (PLUS_EXPR,
3937 offset,
3938 DECL_FIELD_OFFSET (field)),
3939 offsets,
3940 max_offset,
3941 /*vbases_p=*/1);
3942 if (r)
3943 return r;
3946 else if (TREE_CODE (type) == ARRAY_TYPE)
3948 tree domain = TYPE_DOMAIN (type);
3949 tree index;
3951 /* Step through each of the elements in the array. */
3952 for (index = size_zero_node;
3953 INT_CST_LT (index, TYPE_MAX_VALUE (domain));
3954 index = size_binop (PLUS_EXPR, index, size_one_node))
3956 r = walk_subobject_offsets (TREE_TYPE (type),
3958 offset,
3959 offsets,
3960 max_offset,
3961 /*vbases_p=*/1);
3962 if (r)
3963 return r;
3964 offset = size_binop (PLUS_EXPR, offset,
3965 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3966 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3967 there's no point in iterating through the remaining
3968 elements of the array. */
3969 if (max_offset && INT_CST_LT (max_offset, offset))
3970 break;
3974 return 0;
3977 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3978 OFFSETS. If VBASES_P is non-zero, virtual bases of TYPE are
3979 examined. */
3981 static void
3982 record_subobject_offsets (type, offset, offsets, vbases_p)
3983 tree type;
3984 tree offset;
3985 splay_tree offsets;
3986 int vbases_p;
3988 walk_subobject_offsets (type, record_subobject_offset, offset,
3989 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3992 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3993 OFFSET) conflict with entries in OFFSETS. If VBASES_P is non-zero,
3994 virtual bases of TYPE are examined. */
3996 static int
3997 layout_conflict_p (type, offset, offsets, vbases_p)
3998 tree type;
3999 tree offset;
4000 splay_tree offsets;
4001 int vbases_p;
4003 splay_tree_node max_node;
4005 /* Get the node in OFFSETS that indicates the maximum offset where
4006 an empty subobject is located. */
4007 max_node = splay_tree_max (offsets);
4008 /* If there aren't any empty subobjects, then there's no point in
4009 performing this check. */
4010 if (!max_node)
4011 return 0;
4013 return walk_subobject_offsets (type, check_subobject_offset, offset,
4014 offsets, (tree) (max_node->key),
4015 vbases_p);
4018 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4019 non-static data member of the type indicated by RLI. BINFO is the
4020 binfo corresponding to the base subobject, OFFSETS maps offsets to
4021 types already located at those offsets. T is the most derived
4022 type. This function determines the position of the DECL. */
4024 static void
4025 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
4026 record_layout_info rli;
4027 tree decl;
4028 tree binfo;
4029 splay_tree offsets;
4030 tree t;
4032 tree offset = NULL_TREE;
4033 tree type = TREE_TYPE (decl);
4034 /* If we are laying out a base class, rather than a field, then
4035 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
4036 int field_p = !DECL_ARTIFICIAL (decl);
4038 /* Try to place the field. It may take more than one try if we have
4039 a hard time placing the field without putting two objects of the
4040 same type at the same address. */
4041 while (1)
4043 struct record_layout_info_s old_rli = *rli;
4045 /* Place this field. */
4046 place_field (rli, decl);
4047 offset = byte_position (decl);
4049 /* We have to check to see whether or not there is already
4050 something of the same type at the offset we're about to use.
4051 For example:
4053 struct S {};
4054 struct T : public S { int i; };
4055 struct U : public S, public T {};
4057 Here, we put S at offset zero in U. Then, we can't put T at
4058 offset zero -- its S component would be at the same address
4059 as the S we already allocated. So, we have to skip ahead.
4060 Since all data members, including those whose type is an
4061 empty class, have non-zero size, any overlap can happen only
4062 with a direct or indirect base-class -- it can't happen with
4063 a data member. */
4064 if (layout_conflict_p (TREE_TYPE (decl),
4065 offset,
4066 offsets,
4067 field_p))
4069 /* Strip off the size allocated to this field. That puts us
4070 at the first place we could have put the field with
4071 proper alignment. */
4072 *rli = old_rli;
4074 /* Bump up by the alignment required for the type. */
4075 rli->bitpos
4076 = size_binop (PLUS_EXPR, rli->bitpos,
4077 bitsize_int (binfo
4078 ? CLASSTYPE_ALIGN (type)
4079 : TYPE_ALIGN (type)));
4080 normalize_rli (rli);
4082 else
4083 /* There was no conflict. We're done laying out this field. */
4084 break;
4087 /* Now that we know where it will be placed, update its
4088 BINFO_OFFSET. */
4089 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4090 propagate_binfo_offsets (binfo,
4091 convert (ssizetype, offset), t);
4094 /* Layout the empty base BINFO. EOC indicates the byte currently just
4095 past the end of the class, and should be correctly aligned for a
4096 class of the type indicated by BINFO; OFFSETS gives the offsets of
4097 the empty bases allocated so far. T is the most derived
4098 type. Return non-zero iff we added it at the end. */
4100 static bool
4101 layout_empty_base (binfo, eoc, offsets, t)
4102 tree binfo;
4103 tree eoc;
4104 splay_tree offsets;
4105 tree t;
4107 tree alignment;
4108 tree basetype = BINFO_TYPE (binfo);
4109 bool atend = false;
4111 /* This routine should only be used for empty classes. */
4112 my_friendly_assert (is_empty_class (basetype), 20000321);
4113 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4115 /* This is an empty base class. We first try to put it at offset
4116 zero. */
4117 if (layout_conflict_p (BINFO_TYPE (binfo),
4118 BINFO_OFFSET (binfo),
4119 offsets,
4120 /*vbases_p=*/0))
4122 /* That didn't work. Now, we move forward from the next
4123 available spot in the class. */
4124 atend = true;
4125 propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
4126 while (1)
4128 if (!layout_conflict_p (BINFO_TYPE (binfo),
4129 BINFO_OFFSET (binfo),
4130 offsets,
4131 /*vbases_p=*/0))
4132 /* We finally found a spot where there's no overlap. */
4133 break;
4135 /* There's overlap here, too. Bump along to the next spot. */
4136 propagate_binfo_offsets (binfo, alignment, t);
4139 return atend;
4142 /* Build a FIELD_DECL for the base given by BINFO in the class
4143 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
4144 *BASE_ALIGN is a running maximum of the alignments of any base
4145 class. OFFSETS gives the location of empty base subobjects. T is
4146 the most derived type. Return non-zero if the new object cannot be
4147 nearly-empty. */
4149 static bool
4150 build_base_field (rli, binfo, empty_p, offsets, t)
4151 record_layout_info rli;
4152 tree binfo;
4153 int *empty_p;
4154 splay_tree offsets;
4155 tree t;
4157 tree basetype = BINFO_TYPE (binfo);
4158 tree decl;
4159 bool atend = false;
4161 if (!COMPLETE_TYPE_P (basetype))
4162 /* This error is now reported in xref_tag, thus giving better
4163 location information. */
4164 return atend;
4166 decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
4167 DECL_ARTIFICIAL (decl) = 1;
4168 DECL_FIELD_CONTEXT (decl) = rli->t;
4169 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4170 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4171 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4172 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4174 if (!integer_zerop (DECL_SIZE (decl)))
4176 /* The containing class is non-empty because it has a non-empty
4177 base class. */
4178 *empty_p = 0;
4180 /* Try to place the field. It may take more than one try if we
4181 have a hard time placing the field without putting two
4182 objects of the same type at the same address. */
4183 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t);
4185 else
4187 unsigned HOST_WIDE_INT eoc;
4189 /* On some platforms (ARM), even empty classes will not be
4190 byte-aligned. */
4191 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
4192 eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
4193 atend |= layout_empty_base (binfo, size_int (eoc), offsets, t);
4196 /* Record the offsets of BINFO and its base subobjects. */
4197 record_subobject_offsets (BINFO_TYPE (binfo),
4198 BINFO_OFFSET (binfo),
4199 offsets,
4200 /*vbases_p=*/0);
4201 return atend;
4204 /* Layout all of the non-virtual base classes. Record empty
4205 subobjects in OFFSETS. T is the most derived type. Return
4206 non-zero if the type cannot be nearly empty. */
4208 static bool
4209 build_base_fields (rli, empty_p, offsets, t)
4210 record_layout_info rli;
4211 int *empty_p;
4212 splay_tree offsets;
4213 tree t;
4215 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4216 subobjects. */
4217 tree rec = rli->t;
4218 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
4219 int i;
4220 bool atend = 0;
4222 /* Under the new ABI, the primary base class is always allocated
4223 first. */
4224 if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
4225 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
4226 empty_p, offsets, t);
4228 /* Now allocate the rest of the bases. */
4229 for (i = 0; i < n_baseclasses; ++i)
4231 tree base_binfo;
4233 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4235 /* Under the new ABI, the primary base was already allocated
4236 above, so we don't need to allocate it again here. */
4237 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
4238 continue;
4240 /* A primary virtual base class is allocated just like any other
4241 base class, but a non-primary virtual base is allocated
4242 later, in layout_virtual_bases. */
4243 if (TREE_VIA_VIRTUAL (base_binfo)
4244 && !BINFO_PRIMARY_P (base_binfo))
4245 continue;
4247 atend |= build_base_field (rli, base_binfo, empty_p, offsets, t);
4249 return atend;
4252 /* Go through the TYPE_METHODS of T issuing any appropriate
4253 diagnostics, figuring out which methods override which other
4254 methods, and so forth. */
4256 static void
4257 check_methods (t)
4258 tree t;
4260 tree x;
4261 int seen_one_arg_array_delete_p = 0;
4263 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4265 GNU_xref_member (current_class_name, x);
4267 /* If this was an evil function, don't keep it in class. */
4268 if (DECL_ASSEMBLER_NAME_SET_P (x)
4269 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4270 continue;
4272 check_for_override (x, t);
4273 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4274 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4276 /* The name of the field is the original field name
4277 Save this in auxiliary field for later overloading. */
4278 if (DECL_VINDEX (x))
4280 TYPE_POLYMORPHIC_P (t) = 1;
4281 if (DECL_PURE_VIRTUAL_P (x))
4282 CLASSTYPE_PURE_VIRTUALS (t)
4283 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4286 if (DECL_ARRAY_DELETE_OPERATOR_P (x))
4288 tree second_parm;
4290 /* When dynamically allocating an array of this type, we
4291 need a "cookie" to record how many elements we allocated,
4292 even if the array elements have no non-trivial
4293 destructor, if the usual array deallocation function
4294 takes a second argument of type size_t. The standard (in
4295 [class.free]) requires that the second argument be set
4296 correctly. */
4297 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
4298 /* Under the new ABI, we choose only those function that are
4299 explicitly declared as `operator delete[] (void *,
4300 size_t)'. */
4301 if (!seen_one_arg_array_delete_p
4302 && second_parm
4303 && TREE_CHAIN (second_parm) == void_list_node
4304 && same_type_p (TREE_VALUE (second_parm), sizetype))
4305 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4306 /* If there's no second parameter, then this is the usual
4307 deallocation function. */
4308 else if (second_parm == void_list_node)
4309 seen_one_arg_array_delete_p = 1;
4314 /* FN is a constructor or destructor. Clone the declaration to create
4315 a specialized in-charge or not-in-charge version, as indicated by
4316 NAME. */
4318 static tree
4319 build_clone (fn, name)
4320 tree fn;
4321 tree name;
4323 tree parms;
4324 tree clone;
4326 /* Copy the function. */
4327 clone = copy_decl (fn);
4328 /* Remember where this function came from. */
4329 DECL_CLONED_FUNCTION (clone) = fn;
4330 DECL_ABSTRACT_ORIGIN (clone) = fn;
4331 /* Reset the function name. */
4332 DECL_NAME (clone) = name;
4333 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4334 /* There's no pending inline data for this function. */
4335 DECL_PENDING_INLINE_INFO (clone) = NULL;
4336 DECL_PENDING_INLINE_P (clone) = 0;
4337 /* And it hasn't yet been deferred. */
4338 DECL_DEFERRED_FN (clone) = 0;
4340 /* The base-class destructor is not virtual. */
4341 if (name == base_dtor_identifier)
4343 DECL_VIRTUAL_P (clone) = 0;
4344 if (TREE_CODE (clone) != TEMPLATE_DECL)
4345 DECL_VINDEX (clone) = NULL_TREE;
4348 /* If there was an in-charge parameter, drop it from the function
4349 type. */
4350 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4352 tree basetype;
4353 tree parmtypes;
4354 tree exceptions;
4356 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4357 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4358 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4359 /* Skip the `this' parameter. */
4360 parmtypes = TREE_CHAIN (parmtypes);
4361 /* Skip the in-charge parameter. */
4362 parmtypes = TREE_CHAIN (parmtypes);
4363 /* And the VTT parm, in a complete [cd]tor. */
4364 if (DECL_HAS_VTT_PARM_P (fn)
4365 && ! DECL_NEEDS_VTT_PARM_P (clone))
4366 parmtypes = TREE_CHAIN (parmtypes);
4367 /* If this is subobject constructor or destructor, add the vtt
4368 parameter. */
4369 TREE_TYPE (clone)
4370 = build_cplus_method_type (basetype,
4371 TREE_TYPE (TREE_TYPE (clone)),
4372 parmtypes);
4373 if (exceptions)
4374 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4375 exceptions);
4378 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
4379 aren't function parameters; those are the template parameters. */
4380 if (TREE_CODE (clone) != TEMPLATE_DECL)
4382 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4383 /* Remove the in-charge parameter. */
4384 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4386 TREE_CHAIN (DECL_ARGUMENTS (clone))
4387 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4388 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4390 /* And the VTT parm, in a complete [cd]tor. */
4391 if (DECL_HAS_VTT_PARM_P (fn))
4393 if (DECL_NEEDS_VTT_PARM_P (clone))
4394 DECL_HAS_VTT_PARM_P (clone) = 1;
4395 else
4397 TREE_CHAIN (DECL_ARGUMENTS (clone))
4398 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4399 DECL_HAS_VTT_PARM_P (clone) = 0;
4403 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4405 DECL_CONTEXT (parms) = clone;
4406 copy_lang_decl (parms);
4410 /* Create the RTL for this function. */
4411 SET_DECL_RTL (clone, NULL_RTX);
4412 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4414 /* Make it easy to find the CLONE given the FN. */
4415 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4416 TREE_CHAIN (fn) = clone;
4418 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4419 if (TREE_CODE (clone) == TEMPLATE_DECL)
4421 tree result;
4423 DECL_TEMPLATE_RESULT (clone)
4424 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4425 result = DECL_TEMPLATE_RESULT (clone);
4426 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4427 DECL_TI_TEMPLATE (result) = clone;
4429 else if (DECL_DEFERRED_FN (fn))
4430 defer_fn (clone);
4432 return clone;
4435 /* Produce declarations for all appropriate clones of FN. If
4436 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4437 CLASTYPE_METHOD_VEC as well. */
4439 void
4440 clone_function_decl (fn, update_method_vec_p)
4441 tree fn;
4442 int update_method_vec_p;
4444 tree clone;
4446 /* Avoid inappropriate cloning. */
4447 if (TREE_CHAIN (fn)
4448 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4449 return;
4451 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4453 /* For each constructor, we need two variants: an in-charge version
4454 and a not-in-charge version. */
4455 clone = build_clone (fn, complete_ctor_identifier);
4456 if (update_method_vec_p)
4457 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4458 clone = build_clone (fn, base_ctor_identifier);
4459 if (update_method_vec_p)
4460 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4462 else
4464 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4466 /* For each destructor, we need three variants: an in-charge
4467 version, a not-in-charge version, and an in-charge deleting
4468 version. We clone the deleting version first because that
4469 means it will go second on the TYPE_METHODS list -- and that
4470 corresponds to the correct layout order in the virtual
4471 function table.
4473 For a non-virtual destructor, we do not build a deleting
4474 destructor. */
4475 if (DECL_VIRTUAL_P (fn))
4477 clone = build_clone (fn, deleting_dtor_identifier);
4478 if (update_method_vec_p)
4479 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4481 clone = build_clone (fn, complete_dtor_identifier);
4482 if (update_method_vec_p)
4483 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4484 clone = build_clone (fn, base_dtor_identifier);
4485 if (update_method_vec_p)
4486 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4489 /* Note that this is an abstract function that is never emitted. */
4490 DECL_ABSTRACT (fn) = 1;
4493 /* DECL is an in charge constructor, which is being defined. This will
4494 have had an in class declaration, from whence clones were
4495 declared. An out-of-class definition can specify additional default
4496 arguments. As it is the clones that are involved in overload
4497 resolution, we must propagate the information from the DECL to its
4498 clones. */
4500 void
4501 adjust_clone_args (decl)
4502 tree decl;
4504 tree clone;
4506 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4507 clone = TREE_CHAIN (clone))
4509 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4510 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4511 tree decl_parms, clone_parms;
4513 clone_parms = orig_clone_parms;
4515 /* Skip the 'this' parameter. */
4516 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4517 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4519 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4520 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4521 if (DECL_HAS_VTT_PARM_P (decl))
4522 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4524 clone_parms = orig_clone_parms;
4525 if (DECL_HAS_VTT_PARM_P (clone))
4526 clone_parms = TREE_CHAIN (clone_parms);
4528 for (decl_parms = orig_decl_parms; decl_parms;
4529 decl_parms = TREE_CHAIN (decl_parms),
4530 clone_parms = TREE_CHAIN (clone_parms))
4532 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4533 TREE_TYPE (clone_parms)), 20010424);
4535 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4537 /* A default parameter has been added. Adjust the
4538 clone's parameters. */
4539 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4540 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4541 tree type;
4543 clone_parms = orig_decl_parms;
4545 if (DECL_HAS_VTT_PARM_P (clone))
4547 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4548 TREE_VALUE (orig_clone_parms),
4549 clone_parms);
4550 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4552 type = build_cplus_method_type (basetype,
4553 TREE_TYPE (TREE_TYPE (clone)),
4554 clone_parms);
4555 if (exceptions)
4556 type = build_exception_variant (type, exceptions);
4557 TREE_TYPE (clone) = type;
4559 clone_parms = NULL_TREE;
4560 break;
4563 my_friendly_assert (!clone_parms, 20010424);
4567 /* For each of the constructors and destructors in T, create an
4568 in-charge and not-in-charge variant. */
4570 static void
4571 clone_constructors_and_destructors (t)
4572 tree t;
4574 tree fns;
4576 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4577 out now. */
4578 if (!CLASSTYPE_METHOD_VEC (t))
4579 return;
4581 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4582 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4583 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4584 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4587 /* Remove all zero-width bit-fields from T. */
4589 static void
4590 remove_zero_width_bit_fields (t)
4591 tree t;
4593 tree *fieldsp;
4595 fieldsp = &TYPE_FIELDS (t);
4596 while (*fieldsp)
4598 if (TREE_CODE (*fieldsp) == FIELD_DECL
4599 && DECL_C_BIT_FIELD (*fieldsp)
4600 && DECL_INITIAL (*fieldsp))
4601 *fieldsp = TREE_CHAIN (*fieldsp);
4602 else
4603 fieldsp = &TREE_CHAIN (*fieldsp);
4607 /* Check the validity of the bases and members declared in T. Add any
4608 implicitly-generated functions (like copy-constructors and
4609 assignment operators). Compute various flag bits (like
4610 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4611 level: i.e., independently of the ABI in use. */
4613 static void
4614 check_bases_and_members (t, empty_p)
4615 tree t;
4616 int *empty_p;
4618 /* Nonzero if we are not allowed to generate a default constructor
4619 for this case. */
4620 int cant_have_default_ctor;
4621 /* Nonzero if the implicitly generated copy constructor should take
4622 a non-const reference argument. */
4623 int cant_have_const_ctor;
4624 /* Nonzero if the the implicitly generated assignment operator
4625 should take a non-const reference argument. */
4626 int no_const_asn_ref;
4627 tree access_decls;
4629 /* By default, we use const reference arguments and generate default
4630 constructors. */
4631 cant_have_default_ctor = 0;
4632 cant_have_const_ctor = 0;
4633 no_const_asn_ref = 0;
4635 /* Assume that the class is nearly empty; we'll clear this flag if
4636 it turns out not to be nearly empty. */
4637 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4639 /* Check all the base-classes. */
4640 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4641 &no_const_asn_ref);
4643 /* Check all the data member declarations. */
4644 check_field_decls (t, &access_decls, empty_p,
4645 &cant_have_default_ctor,
4646 &cant_have_const_ctor,
4647 &no_const_asn_ref);
4649 /* Check all the method declarations. */
4650 check_methods (t);
4652 /* A nearly-empty class has to be vptr-containing; a nearly empty
4653 class contains just a vptr. */
4654 if (!TYPE_CONTAINS_VPTR_P (t))
4655 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4657 /* Do some bookkeeping that will guide the generation of implicitly
4658 declared member functions. */
4659 TYPE_HAS_COMPLEX_INIT_REF (t)
4660 |= (TYPE_HAS_INIT_REF (t)
4661 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4662 || TYPE_POLYMORPHIC_P (t));
4663 TYPE_NEEDS_CONSTRUCTING (t)
4664 |= (TYPE_HAS_CONSTRUCTOR (t)
4665 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4666 || TYPE_POLYMORPHIC_P (t));
4667 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4668 || TYPE_POLYMORPHIC_P (t));
4669 CLASSTYPE_NON_POD_P (t)
4670 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4671 || TYPE_HAS_ASSIGN_REF (t));
4672 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4673 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4674 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4676 /* Synthesize any needed methods. Note that methods will be synthesized
4677 for anonymous unions; grok_x_components undoes that. */
4678 add_implicitly_declared_members (t, cant_have_default_ctor,
4679 cant_have_const_ctor,
4680 no_const_asn_ref);
4682 /* Create the in-charge and not-in-charge variants of constructors
4683 and destructors. */
4684 clone_constructors_and_destructors (t);
4686 /* Process the using-declarations. */
4687 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4688 handle_using_decl (TREE_VALUE (access_decls), t);
4690 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4691 finish_struct_methods (t);
4694 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4695 accordingly. If a new vfield was created (because T doesn't have a
4696 primary base class), then the newly created field is returned. It
4697 is not added to the TYPE_FIELDS list; it is the caller's
4698 responsibility to do that. */
4700 static tree
4701 create_vtable_ptr (t, empty_p, vfuns_p,
4702 new_virtuals_p, overridden_virtuals_p)
4703 tree t;
4704 int *empty_p;
4705 int *vfuns_p;
4706 tree *new_virtuals_p;
4707 tree *overridden_virtuals_p;
4709 tree fn;
4711 /* Loop over the virtual functions, adding them to our various
4712 vtables. */
4713 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4714 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
4715 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4716 vfuns_p, fn, t);
4718 /* If we couldn't find an appropriate base class, create a new field
4719 here. Even if there weren't any new virtual functions, we might need a
4720 new virtual function table if we're supposed to include vptrs in
4721 all classes that need them. */
4722 if (!TYPE_VFIELD (t)
4723 && (*vfuns_p
4724 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())))
4726 /* We build this decl with vtbl_ptr_type_node, which is a
4727 `vtable_entry_type*'. It might seem more precise to use
4728 `vtable_entry_type (*)[N]' where N is the number of firtual
4729 functions. However, that would require the vtable pointer in
4730 base classes to have a different type than the vtable pointer
4731 in derived classes. We could make that happen, but that
4732 still wouldn't solve all the problems. In particular, the
4733 type-based alias analysis code would decide that assignments
4734 to the base class vtable pointer can't alias assignments to
4735 the derived class vtable pointer, since they have different
4736 types. Thus, in an derived class destructor, where the base
4737 class constructor was inlined, we could generate bad code for
4738 setting up the vtable pointer.
4740 Therefore, we use one type for all vtable pointers. We still
4741 use a type-correct type; it's just doesn't indicate the array
4742 bounds. That's better than using `void*' or some such; it's
4743 cleaner, and it let's the alias analysis code know that these
4744 stores cannot alias stores to void*! */
4745 TYPE_VFIELD (t)
4746 = build_vtbl_or_vbase_field (get_vfield_name (t),
4747 get_identifier (VFIELD_BASE),
4748 vtbl_ptr_type_node,
4751 empty_p);
4753 if (CLASSTYPE_N_BASECLASSES (t))
4754 /* If there were any baseclasses, they can't possibly be at
4755 offset zero any more, because that's where the vtable
4756 pointer is. So, converting to a base class is going to
4757 take work. */
4758 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4760 return TYPE_VFIELD (t);
4763 return NULL_TREE;
4766 /* Fixup the inline function given by INFO now that the class is
4767 complete. */
4769 static void
4770 fixup_pending_inline (fn)
4771 tree fn;
4773 if (DECL_PENDING_INLINE_INFO (fn))
4775 tree args = DECL_ARGUMENTS (fn);
4776 while (args)
4778 DECL_CONTEXT (args) = fn;
4779 args = TREE_CHAIN (args);
4784 /* Fixup the inline methods and friends in TYPE now that TYPE is
4785 complete. */
4787 static void
4788 fixup_inline_methods (type)
4789 tree type;
4791 tree method = TYPE_METHODS (type);
4793 if (method && TREE_CODE (method) == TREE_VEC)
4795 if (TREE_VEC_ELT (method, 1))
4796 method = TREE_VEC_ELT (method, 1);
4797 else if (TREE_VEC_ELT (method, 0))
4798 method = TREE_VEC_ELT (method, 0);
4799 else
4800 method = TREE_VEC_ELT (method, 2);
4803 /* Do inline member functions. */
4804 for (; method; method = TREE_CHAIN (method))
4805 fixup_pending_inline (method);
4807 /* Do friends. */
4808 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4809 method;
4810 method = TREE_CHAIN (method))
4811 fixup_pending_inline (TREE_VALUE (method));
4812 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4815 /* Add OFFSET to all base types of BINFO which is a base in the
4816 hierarchy dominated by T.
4818 OFFSET, which is a type offset, is number of bytes. */
4820 static void
4821 propagate_binfo_offsets (binfo, offset, t)
4822 tree binfo;
4823 tree offset;
4824 tree t;
4826 int i;
4827 tree primary_binfo;
4829 /* Update BINFO's offset. */
4830 BINFO_OFFSET (binfo)
4831 = convert (sizetype,
4832 size_binop (PLUS_EXPR,
4833 convert (ssizetype, BINFO_OFFSET (binfo)),
4834 offset));
4836 /* Find the primary base class. */
4837 primary_binfo = get_primary_binfo (binfo);
4839 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4840 downwards. */
4841 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4843 tree base_binfo;
4845 /* On the first time through the loop, do the primary base.
4846 Because the primary base need not be an immediate base, we
4847 must handle the primary base specially. */
4848 if (i == -1)
4850 if (!primary_binfo)
4851 continue;
4853 base_binfo = primary_binfo;
4855 else
4857 base_binfo = BINFO_BASETYPE (binfo, i);
4858 /* Don't do the primary base twice. */
4859 if (base_binfo == primary_binfo)
4860 continue;
4863 /* Skip virtual bases that aren't our canonical primary base. */
4864 if (TREE_VIA_VIRTUAL (base_binfo)
4865 && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4866 || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4867 continue;
4869 propagate_binfo_offsets (base_binfo, offset, t);
4873 /* Called via dfs_walk from layout_virtual bases. */
4875 static tree
4876 dfs_set_offset_for_unshared_vbases (binfo, data)
4877 tree binfo;
4878 void *data;
4880 /* If this is a virtual base, make sure it has the same offset as
4881 the shared copy. If it's a primary base, then we know it's
4882 correct. */
4883 if (TREE_VIA_VIRTUAL (binfo))
4885 tree t = (tree) data;
4886 tree vbase;
4887 tree offset;
4889 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4890 if (vbase != binfo)
4892 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4893 propagate_binfo_offsets (binfo, offset, t);
4897 return NULL_TREE;
4900 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4901 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4902 empty subobjects of T. */
4904 static void
4905 layout_virtual_bases (t, offsets)
4906 tree t;
4907 splay_tree offsets;
4909 tree vbases;
4910 unsigned HOST_WIDE_INT dsize;
4911 unsigned HOST_WIDE_INT eoc;
4913 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4914 return;
4916 #ifdef STRUCTURE_SIZE_BOUNDARY
4917 /* Packed structures don't need to have minimum size. */
4918 if (! TYPE_PACKED (t))
4919 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4920 #endif
4922 /* DSIZE is the size of the class without the virtual bases. */
4923 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4925 /* Make every class have alignment of at least one. */
4926 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4928 /* Go through the virtual bases, allocating space for each virtual
4929 base that is not already a primary base class. Under the old
4930 ABI, these are allocated according to a depth-first left-to-right
4931 postorder traversal; in the new ABI, inheritance graph order is
4932 used instead. */
4933 for (vbases = TYPE_BINFO (t);
4934 vbases;
4935 vbases = TREE_CHAIN (vbases))
4937 tree vbase;
4939 if (!TREE_VIA_VIRTUAL (vbases))
4940 continue;
4941 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4943 if (!BINFO_PRIMARY_P (vbase))
4945 /* This virtual base is not a primary base of any class in the
4946 hierarchy, so we have to add space for it. */
4947 tree basetype;
4948 unsigned int desired_align;
4950 basetype = BINFO_TYPE (vbase);
4952 desired_align = CLASSTYPE_ALIGN (basetype);
4953 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4955 /* Add padding so that we can put the virtual base class at an
4956 appropriately aligned offset. */
4957 dsize = CEIL (dsize, desired_align) * desired_align;
4959 /* Under the new ABI, we try to squish empty virtual bases in
4960 just like ordinary empty bases. */
4961 if (is_empty_class (basetype))
4962 layout_empty_base (vbase,
4963 size_int (CEIL (dsize, BITS_PER_UNIT)),
4964 offsets, t);
4965 else
4967 tree offset;
4969 offset = ssize_int (CEIL (dsize, BITS_PER_UNIT));
4970 offset = size_diffop (offset,
4971 convert (ssizetype,
4972 BINFO_OFFSET (vbase)));
4974 /* And compute the offset of the virtual base. */
4975 propagate_binfo_offsets (vbase, offset, t);
4976 /* Every virtual baseclass takes a least a UNIT, so that
4977 we can take it's address and get something different
4978 for each base. */
4979 dsize += MAX (BITS_PER_UNIT,
4980 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4983 /* Keep track of the offsets assigned to this virtual base. */
4984 record_subobject_offsets (BINFO_TYPE (vbase),
4985 BINFO_OFFSET (vbase),
4986 offsets,
4987 /*vbases_p=*/0);
4991 /* Now, go through the TYPE_BINFO hierarchy, setting the
4992 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4993 bases and their direct and indirect bases. The ambiguity checks
4994 in get_base_distance depend on the BINFO_OFFSETs being set
4995 correctly. */
4996 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4998 /* If we had empty base classes that protruded beyond the end of the
4999 class, we didn't update DSIZE above; we were hoping to overlay
5000 multiple such bases at the same location. */
5001 eoc = end_of_class (t, /*include_virtuals_p=*/1);
5002 if (eoc * BITS_PER_UNIT > dsize)
5003 dsize = eoc * BITS_PER_UNIT;
5005 /* Now, make sure that the total size of the type is a multiple of
5006 its alignment. */
5007 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
5008 TYPE_SIZE (t) = bitsize_int (dsize);
5009 TYPE_SIZE_UNIT (t) = convert (sizetype,
5010 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
5011 bitsize_unit_node));
5013 /* Check for ambiguous virtual bases. */
5014 if (extra_warnings)
5015 for (vbases = CLASSTYPE_VBASECLASSES (t);
5016 vbases;
5017 vbases = TREE_CHAIN (vbases))
5019 tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
5020 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
5021 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
5022 basetype, t);
5026 /* Returns the offset of the byte just past the end of the base class
5027 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5028 only non-virtual bases are included. */
5030 static unsigned HOST_WIDE_INT
5031 end_of_class (t, include_virtuals_p)
5032 tree t;
5033 int include_virtuals_p;
5035 unsigned HOST_WIDE_INT result = 0;
5036 int i;
5038 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
5040 tree base_binfo;
5041 tree offset;
5042 tree size;
5043 unsigned HOST_WIDE_INT end_of_base;
5045 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
5047 if (!include_virtuals_p
5048 && TREE_VIA_VIRTUAL (base_binfo)
5049 && !BINFO_PRIMARY_P (base_binfo))
5050 continue;
5052 if (is_empty_class (BINFO_TYPE (base_binfo)))
5053 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5054 allocate some space for it. It cannot have virtual bases,
5055 so TYPE_SIZE_UNIT is fine. */
5056 size = TYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
5057 else
5058 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
5059 offset = size_binop (PLUS_EXPR,
5060 BINFO_OFFSET (base_binfo),
5061 size);
5062 end_of_base = tree_low_cst (offset, /*pos=*/1);
5063 if (end_of_base > result)
5064 result = end_of_base;
5067 return result;
5070 /* Warn about direct bases of T that are inaccessible because they are
5071 ambiguous. For example:
5073 struct S {};
5074 struct T : public S {};
5075 struct U : public S, public T {};
5077 Here, `(S*) new U' is not allowed because there are two `S'
5078 subobjects of U. */
5080 static void
5081 warn_about_ambiguous_direct_bases (t)
5082 tree t;
5084 int i;
5086 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
5088 tree basetype = TYPE_BINFO_BASETYPE (t, i);
5090 if (get_base_distance (basetype, t, 0, NULL) == -2)
5091 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
5092 basetype, t);
5096 /* Compare two INTEGER_CSTs K1 and K2. */
5098 static int
5099 splay_tree_compare_integer_csts (k1, k2)
5100 splay_tree_key k1;
5101 splay_tree_key k2;
5103 return tree_int_cst_compare ((tree) k1, (tree) k2);
5106 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5107 BINFO_OFFSETs for all of the base-classes. Position the vtable
5108 pointer. */
5110 static void
5111 layout_class_type (t, empty_p, vfuns_p,
5112 new_virtuals_p, overridden_virtuals_p)
5113 tree t;
5114 int *empty_p;
5115 int *vfuns_p;
5116 tree *new_virtuals_p;
5117 tree *overridden_virtuals_p;
5119 tree non_static_data_members;
5120 tree field;
5121 tree vptr;
5122 record_layout_info rli;
5123 unsigned HOST_WIDE_INT eoc;
5124 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5125 types that appear at that offset. */
5126 splay_tree empty_base_offsets;
5128 /* Keep track of the first non-static data member. */
5129 non_static_data_members = TYPE_FIELDS (t);
5131 /* Start laying out the record. */
5132 rli = start_record_layout (t);
5134 /* If possible, we reuse the virtual function table pointer from one
5135 of our base classes. */
5136 determine_primary_base (t, vfuns_p);
5138 /* Create a pointer to our virtual function table. */
5139 vptr = create_vtable_ptr (t, empty_p, vfuns_p,
5140 new_virtuals_p, overridden_virtuals_p);
5142 /* Under the new ABI, the vptr is always the first thing in the
5143 class. */
5144 if (vptr)
5146 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
5147 place_field (rli, vptr);
5150 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5151 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
5152 NULL, NULL);
5153 if (build_base_fields (rli, empty_p, empty_base_offsets, t))
5154 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5156 /* Add pointers to all of our virtual base-classes. */
5157 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (rli, empty_p),
5158 TYPE_FIELDS (t));
5160 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
5161 we have to save this before we zap TYPE_NONCOPIED_PARTS. */
5162 fixup_inline_methods (t);
5164 /* Layout the non-static data members. */
5165 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
5167 tree type;
5168 tree padding;
5170 /* We still pass things that aren't non-static data members to
5171 the back-end, in case it wants to do something with them. */
5172 if (TREE_CODE (field) != FIELD_DECL)
5174 place_field (rli, field);
5175 continue;
5178 type = TREE_TYPE (field);
5180 /* If this field is a bit-field whose width is greater than its
5181 type, then there are some special rules for allocating it
5182 under the new ABI. Under the old ABI, there were no special
5183 rules, but the back-end can't handle bitfields longer than a
5184 `long long', so we use the same mechanism. */
5185 if (DECL_C_BIT_FIELD (field)
5186 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5188 integer_type_kind itk;
5189 tree integer_type;
5191 /* We must allocate the bits as if suitably aligned for the
5192 longest integer type that fits in this many bits. type
5193 of the field. Then, we are supposed to use the left over
5194 bits as additional padding. */
5195 for (itk = itk_char; itk != itk_none; ++itk)
5196 if (INT_CST_LT (DECL_SIZE (field),
5197 TYPE_SIZE (integer_types[itk])))
5198 break;
5200 /* ITK now indicates a type that is too large for the
5201 field. We have to back up by one to find the largest
5202 type that fits. */
5203 integer_type = integer_types[itk - 1];
5204 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5205 TYPE_SIZE (integer_type));
5206 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5207 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5208 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5210 else
5211 padding = NULL_TREE;
5213 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5214 empty_base_offsets, t);
5216 /* If we needed additional padding after this field, add it
5217 now. */
5218 if (padding)
5220 tree padding_field;
5222 padding_field = build_decl (FIELD_DECL,
5223 NULL_TREE,
5224 char_type_node);
5225 DECL_BIT_FIELD (padding_field) = 1;
5226 DECL_SIZE (padding_field) = padding;
5227 DECL_ALIGN (padding_field) = 1;
5228 DECL_USER_ALIGN (padding_field) = 0;
5229 layout_nonempty_base_or_field (rli, padding_field,
5230 NULL_TREE,
5231 empty_base_offsets, t);
5235 /* It might be the case that we grew the class to allocate a
5236 zero-sized base class. That won't be reflected in RLI, yet,
5237 because we are willing to overlay multiple bases at the same
5238 offset. However, now we need to make sure that RLI is big enough
5239 to reflect the entire class. */
5240 eoc = end_of_class (t, /*include_virtuals_p=*/0);
5241 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
5242 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
5244 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc));
5245 rli->bitpos = bitsize_zero_node;
5248 /* We make all structures have at least one element, so that they
5249 have non-zero size. In the new ABI, the class may be empty even
5250 if it has basetypes. Therefore, we add the fake field after all
5251 the other fields; if there are already FIELD_DECLs on the list,
5252 their offsets will not be disturbed. */
5253 if (!eoc && *empty_p)
5255 tree padding;
5257 padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
5258 place_field (rli, padding);
5261 /* Let the back-end lay out the type. Note that at this point we
5262 have only included non-virtual base-classes; we will lay out the
5263 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
5264 this call are not necessarily correct; they are just the size and
5265 alignment when no virtual base clases are used. */
5266 finish_record_layout (rli);
5268 /* Delete all zero-width bit-fields from the list of fields. Now
5269 that the type is laid out they are no longer important. */
5270 remove_zero_width_bit_fields (t);
5272 /* Remember the size and alignment of the class before adding
5273 the virtual bases. */
5274 if (*empty_p)
5276 CLASSTYPE_SIZE (t) = bitsize_zero_node;
5277 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
5279 else
5281 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
5282 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
5285 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5286 CLASSTYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (t);
5288 /* Set the TYPE_DECL for this type to contain the right
5289 value for DECL_OFFSET, so that we can use it as part
5290 of a COMPONENT_REF for multiple inheritance. */
5291 layout_decl (TYPE_MAIN_DECL (t), 0);
5293 /* Now fix up any virtual base class types that we left lying
5294 around. We must get these done before we try to lay out the
5295 virtual function table. As a side-effect, this will remove the
5296 base subobject fields. */
5297 layout_virtual_bases (t, empty_base_offsets);
5299 /* Warn about direct bases that can't be talked about due to
5300 ambiguity. */
5301 warn_about_ambiguous_direct_bases (t);
5303 /* Clean up. */
5304 splay_tree_delete (empty_base_offsets);
5307 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5308 (or C++ class declaration).
5310 For C++, we must handle the building of derived classes.
5311 Also, C++ allows static class members. The way that this is
5312 handled is to keep the field name where it is (as the DECL_NAME
5313 of the field), and place the overloaded decl in the bit position
5314 of the field. layout_record and layout_union will know about this.
5316 More C++ hair: inline functions have text in their
5317 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5318 meaningful tree structure. After the struct has been laid out, set
5319 things up so that this can happen.
5321 And still more: virtual functions. In the case of single inheritance,
5322 when a new virtual function is seen which redefines a virtual function
5323 from the base class, the new virtual function is placed into
5324 the virtual function table at exactly the same address that
5325 it had in the base class. When this is extended to multiple
5326 inheritance, the same thing happens, except that multiple virtual
5327 function tables must be maintained. The first virtual function
5328 table is treated in exactly the same way as in the case of single
5329 inheritance. Additional virtual function tables have different
5330 DELTAs, which tell how to adjust `this' to point to the right thing.
5332 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5334 void
5335 finish_struct_1 (t)
5336 tree t;
5338 tree x;
5339 int vfuns;
5340 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5341 a FUNCTION_DECL. Each of these functions is a virtual function
5342 declared in T that does not override any virtual function from a
5343 base class. */
5344 tree new_virtuals = NULL_TREE;
5345 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5346 except that each declaration here overrides the declaration from
5347 a base class. */
5348 tree overridden_virtuals = NULL_TREE;
5349 int n_fields = 0;
5350 tree vfield;
5351 int empty = 1;
5353 if (COMPLETE_TYPE_P (t))
5355 if (IS_AGGR_TYPE (t))
5356 cp_error ("redefinition of `%#T'", t);
5357 else
5358 my_friendly_abort (172);
5359 popclass ();
5360 return;
5363 GNU_xref_decl (current_function_decl, t);
5365 /* If this type was previously laid out as a forward reference,
5366 make sure we lay it out again. */
5367 TYPE_SIZE (t) = NULL_TREE;
5368 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5369 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5370 vfuns = 0;
5371 CLASSTYPE_RTTI (t) = NULL_TREE;
5373 /* Do end-of-class semantic processing: checking the validity of the
5374 bases and members and add implicitly generated methods. */
5375 check_bases_and_members (t, &empty);
5377 /* Layout the class itself. */
5378 layout_class_type (t, &empty, &vfuns,
5379 &new_virtuals, &overridden_virtuals);
5381 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5382 vfield = TYPE_VFIELD (t);
5383 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5385 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5387 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5388 BINFO_TYPE (primary)),
5389 20010726);
5390 /* The vtable better be at the start. */
5391 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5392 20010726);
5393 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5394 20010726);
5396 vfield = copy_decl (vfield);
5397 DECL_FIELD_CONTEXT (vfield) = t;
5398 TYPE_VFIELD (t) = vfield;
5400 else
5401 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5403 overridden_virtuals
5404 = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
5406 /* If we created a new vtbl pointer for this class, add it to the
5407 list. */
5408 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5409 CLASSTYPE_VFIELDS (t)
5410 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5412 /* If necessary, create the primary vtable for this class. */
5413 if (new_virtuals
5414 || overridden_virtuals
5415 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
5417 new_virtuals = nreverse (new_virtuals);
5418 /* We must enter these virtuals into the table. */
5419 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5420 build_primary_vtable (NULL_TREE, t);
5421 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5422 /* Here we know enough to change the type of our virtual
5423 function table, but we will wait until later this function. */
5424 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5426 /* If this type has basetypes with constructors, then those
5427 constructors might clobber the virtual function table. But
5428 they don't if the derived class shares the exact vtable of the base
5429 class. */
5431 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5433 /* If we didn't need a new vtable, see if we should copy one from
5434 the base. */
5435 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5437 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5439 /* If this class uses a different vtable than its primary base
5440 then when we will need to initialize our vptr after the base
5441 class constructor runs. */
5442 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5443 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5446 if (TYPE_CONTAINS_VPTR_P (t))
5448 if (TYPE_BINFO_VTABLE (t))
5449 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5450 20000116);
5451 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5452 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5453 20000116);
5455 CLASSTYPE_VSIZE (t) = vfuns;
5456 /* Entries for virtual functions defined in the primary base are
5457 followed by entries for new functions unique to this class. */
5458 TYPE_BINFO_VIRTUALS (t)
5459 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5460 /* Finally, add entries for functions that override virtuals
5461 from non-primary bases. */
5462 TYPE_BINFO_VIRTUALS (t)
5463 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5466 finish_struct_bits (t);
5468 /* Complete the rtl for any static member objects of the type we're
5469 working on. */
5470 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5471 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5472 && TREE_TYPE (x) == t)
5473 DECL_MODE (x) = TYPE_MODE (t);
5475 /* Done with FIELDS...now decide whether to sort these for
5476 faster lookups later.
5478 The C front-end only does this when n_fields > 15. We use
5479 a smaller number because most searches fail (succeeding
5480 ultimately as the search bores through the inheritance
5481 hierarchy), and we want this failure to occur quickly. */
5483 n_fields = count_fields (TYPE_FIELDS (t));
5484 if (n_fields > 7)
5486 tree field_vec = make_tree_vec (n_fields);
5487 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5488 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5489 (int (*)(const void *, const void *))field_decl_cmp);
5490 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5491 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5492 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5495 if (TYPE_HAS_CONSTRUCTOR (t))
5497 tree vfields = CLASSTYPE_VFIELDS (t);
5499 while (vfields)
5501 /* Mark the fact that constructor for T
5502 could affect anybody inheriting from T
5503 who wants to initialize vtables for VFIELDS's type. */
5504 if (VF_DERIVED_VALUE (vfields))
5505 TREE_ADDRESSABLE (vfields) = 1;
5506 vfields = TREE_CHAIN (vfields);
5510 /* Make the rtl for any new vtables we have created, and unmark
5511 the base types we marked. */
5512 finish_vtbls (t);
5514 /* Build the VTT for T. */
5515 build_vtt (t);
5517 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5518 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5519 cp_warning ("`%#T' has virtual functions but non-virtual destructor", t);
5521 hack_incomplete_structures (t);
5523 if (warn_overloaded_virtual)
5524 warn_hidden (t);
5526 maybe_suppress_debug_info (t);
5528 dump_class_hierarchy (t);
5530 /* Finish debugging output for this type. */
5531 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5534 /* When T was built up, the member declarations were added in reverse
5535 order. Rearrange them to declaration order. */
5537 void
5538 unreverse_member_declarations (t)
5539 tree t;
5541 tree next;
5542 tree prev;
5543 tree x;
5545 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5546 reverse order. Put them in declaration order now. */
5547 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5548 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5550 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5551 reverse order, so we can't just use nreverse. */
5552 prev = NULL_TREE;
5553 for (x = TYPE_FIELDS (t);
5554 x && TREE_CODE (x) != TYPE_DECL;
5555 x = next)
5557 next = TREE_CHAIN (x);
5558 TREE_CHAIN (x) = prev;
5559 prev = x;
5561 if (prev)
5563 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5564 if (prev)
5565 TYPE_FIELDS (t) = prev;
5569 tree
5570 finish_struct (t, attributes)
5571 tree t, attributes;
5573 const char *saved_filename = input_filename;
5574 int saved_lineno = lineno;
5576 /* Now that we've got all the field declarations, reverse everything
5577 as necessary. */
5578 unreverse_member_declarations (t);
5580 cplus_decl_attributes (t, attributes, NULL_TREE);
5582 /* Nadger the current location so that diagnostics point to the start of
5583 the struct, not the end. */
5584 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5585 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5587 if (processing_template_decl)
5589 finish_struct_methods (t);
5590 TYPE_SIZE (t) = bitsize_zero_node;
5592 else
5593 finish_struct_1 (t);
5595 input_filename = saved_filename;
5596 lineno = saved_lineno;
5598 TYPE_BEING_DEFINED (t) = 0;
5600 if (current_class_type)
5601 popclass ();
5602 else
5603 error ("trying to finish struct, but kicked out due to previous parse errors.");
5605 if (processing_template_decl)
5607 tree scope = current_scope ();
5608 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5609 add_stmt (build_min (TAG_DEFN, t));
5612 return t;
5615 /* Return the dynamic type of INSTANCE, if known.
5616 Used to determine whether the virtual function table is needed
5617 or not.
5619 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5620 of our knowledge of its type. *NONNULL should be initialized
5621 before this function is called. */
5623 static tree
5624 fixed_type_or_null (instance, nonnull, cdtorp)
5625 tree instance;
5626 int *nonnull;
5627 int *cdtorp;
5629 switch (TREE_CODE (instance))
5631 case INDIRECT_REF:
5632 /* Check that we are not going through a cast of some sort. */
5633 if (TREE_TYPE (instance)
5634 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5635 instance = TREE_OPERAND (instance, 0);
5636 /* fall through... */
5637 case CALL_EXPR:
5638 /* This is a call to a constructor, hence it's never zero. */
5639 if (TREE_HAS_CONSTRUCTOR (instance))
5641 if (nonnull)
5642 *nonnull = 1;
5643 return TREE_TYPE (instance);
5645 return NULL_TREE;
5647 case SAVE_EXPR:
5648 /* This is a call to a constructor, hence it's never zero. */
5649 if (TREE_HAS_CONSTRUCTOR (instance))
5651 if (nonnull)
5652 *nonnull = 1;
5653 return TREE_TYPE (instance);
5655 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5657 case RTL_EXPR:
5658 return NULL_TREE;
5660 case PLUS_EXPR:
5661 case MINUS_EXPR:
5662 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5663 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5664 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5665 /* Propagate nonnull. */
5666 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5667 return NULL_TREE;
5669 case NOP_EXPR:
5670 case CONVERT_EXPR:
5671 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5673 case ADDR_EXPR:
5674 if (nonnull)
5675 *nonnull = 1;
5676 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5678 case COMPONENT_REF:
5679 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5681 case VAR_DECL:
5682 case FIELD_DECL:
5683 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5684 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5686 if (nonnull)
5687 *nonnull = 1;
5688 return TREE_TYPE (TREE_TYPE (instance));
5690 /* fall through... */
5691 case TARGET_EXPR:
5692 case PARM_DECL:
5693 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5695 if (nonnull)
5696 *nonnull = 1;
5697 return TREE_TYPE (instance);
5699 else if (instance == current_class_ptr)
5701 if (nonnull)
5702 *nonnull = 1;
5704 /* if we're in a ctor or dtor, we know our type. */
5705 if (DECL_LANG_SPECIFIC (current_function_decl)
5706 && (DECL_CONSTRUCTOR_P (current_function_decl)
5707 || DECL_DESTRUCTOR_P (current_function_decl)))
5709 if (cdtorp)
5710 *cdtorp = 1;
5711 return TREE_TYPE (TREE_TYPE (instance));
5714 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5716 /* Reference variables should be references to objects. */
5717 if (nonnull)
5718 *nonnull = 1;
5720 return NULL_TREE;
5722 default:
5723 return NULL_TREE;
5727 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5728 to the static type. We also handle the case where INSTANCE is really
5729 a pointer. Return negative if this is a ctor/dtor. There the dynamic type
5730 is known, but this might not be the most derived base of the original object,
5731 and hence virtual bases may not be layed out according to this type.
5733 Used to determine whether the virtual function table is needed
5734 or not.
5736 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5737 of our knowledge of its type. *NONNULL should be initialized
5738 before this function is called. */
5741 resolves_to_fixed_type_p (instance, nonnull)
5742 tree instance;
5743 int *nonnull;
5745 tree t = TREE_TYPE (instance);
5746 int cdtorp = 0;
5748 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5749 if (fixed == NULL_TREE)
5750 return 0;
5751 if (POINTER_TYPE_P (t))
5752 t = TREE_TYPE (t);
5753 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5754 return 0;
5755 return cdtorp ? -1 : 1;
5759 void
5760 init_class_processing ()
5762 current_class_depth = 0;
5763 current_class_stack_size = 10;
5764 current_class_stack
5765 = (class_stack_node_t) xmalloc (current_class_stack_size
5766 * sizeof (struct class_stack_node));
5767 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5768 ggc_add_tree_varray_root (&local_classes, 1);
5770 access_default_node = build_int_2 (0, 0);
5771 access_public_node = build_int_2 (ak_public, 0);
5772 access_protected_node = build_int_2 (ak_protected, 0);
5773 access_private_node = build_int_2 (ak_private, 0);
5774 access_default_virtual_node = build_int_2 (4, 0);
5775 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5776 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5777 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5779 ridpointers[(int) RID_PUBLIC] = access_public_node;
5780 ridpointers[(int) RID_PRIVATE] = access_private_node;
5781 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5784 /* Set current scope to NAME. CODE tells us if this is a
5785 STRUCT, UNION, or ENUM environment.
5787 NAME may end up being NULL_TREE if this is an anonymous or
5788 late-bound struct (as in "struct { ... } foo;") */
5790 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5791 appropriate values, found by looking up the type definition of
5792 NAME (as a CODE).
5794 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5795 which can be seen locally to the class. They are shadowed by
5796 any subsequent local declaration (including parameter names).
5798 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5799 which have static meaning (i.e., static members, static
5800 member functions, enum declarations, etc).
5802 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5803 which can be seen locally to the class (as in 1), but
5804 know that we are doing this for declaration purposes
5805 (i.e. friend foo::bar (int)).
5807 So that we may avoid calls to lookup_name, we cache the _TYPE
5808 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5810 For multiple inheritance, we perform a two-pass depth-first search
5811 of the type lattice. The first pass performs a pre-order search,
5812 marking types after the type has had its fields installed in
5813 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5814 unmarks the marked types. If a field or member function name
5815 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5816 that name becomes `error_mark_node'. */
5818 void
5819 pushclass (type, modify)
5820 tree type;
5821 int modify;
5823 type = TYPE_MAIN_VARIANT (type);
5825 /* Make sure there is enough room for the new entry on the stack. */
5826 if (current_class_depth + 1 >= current_class_stack_size)
5828 current_class_stack_size *= 2;
5829 current_class_stack
5830 = (class_stack_node_t) xrealloc (current_class_stack,
5831 current_class_stack_size
5832 * sizeof (struct class_stack_node));
5835 /* Insert a new entry on the class stack. */
5836 current_class_stack[current_class_depth].name = current_class_name;
5837 current_class_stack[current_class_depth].type = current_class_type;
5838 current_class_stack[current_class_depth].access = current_access_specifier;
5839 current_class_stack[current_class_depth].names_used = 0;
5840 current_class_depth++;
5842 /* Now set up the new type. */
5843 current_class_name = TYPE_NAME (type);
5844 if (TREE_CODE (current_class_name) == TYPE_DECL)
5845 current_class_name = DECL_NAME (current_class_name);
5846 current_class_type = type;
5848 /* By default, things in classes are private, while things in
5849 structures or unions are public. */
5850 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5851 ? access_private_node
5852 : access_public_node);
5854 if (previous_class_type != NULL_TREE
5855 && (type != previous_class_type
5856 || !COMPLETE_TYPE_P (previous_class_type))
5857 && current_class_depth == 1)
5859 /* Forcibly remove any old class remnants. */
5860 invalidate_class_lookup_cache ();
5863 /* If we're about to enter a nested class, clear
5864 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5865 if (modify && current_class_depth > 1)
5866 clear_identifier_class_values ();
5868 pushlevel_class ();
5870 if (modify)
5872 if (type != previous_class_type || current_class_depth > 1)
5873 push_class_decls (type);
5874 else
5876 tree item;
5878 /* We are re-entering the same class we just left, so we
5879 don't have to search the whole inheritance matrix to find
5880 all the decls to bind again. Instead, we install the
5881 cached class_shadowed list, and walk through it binding
5882 names and setting up IDENTIFIER_TYPE_VALUEs. */
5883 set_class_shadows (previous_class_values);
5884 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5886 tree id = TREE_PURPOSE (item);
5887 tree decl = TREE_TYPE (item);
5889 push_class_binding (id, decl);
5890 if (TREE_CODE (decl) == TYPE_DECL)
5891 set_identifier_type_value (id, TREE_TYPE (decl));
5893 unuse_fields (type);
5896 storetags (CLASSTYPE_TAGS (type));
5900 /* When we exit a toplevel class scope, we save the
5901 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5902 reenter the class. Here, we've entered some other class, so we
5903 must invalidate our cache. */
5905 void
5906 invalidate_class_lookup_cache ()
5908 tree t;
5910 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5911 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5912 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5914 previous_class_values = NULL_TREE;
5915 previous_class_type = NULL_TREE;
5918 /* Get out of the current class scope. If we were in a class scope
5919 previously, that is the one popped to. */
5921 void
5922 popclass ()
5924 poplevel_class ();
5925 /* Since poplevel_class does the popping of class decls nowadays,
5926 this really only frees the obstack used for these decls. */
5927 pop_class_decls ();
5929 current_class_depth--;
5930 current_class_name = current_class_stack[current_class_depth].name;
5931 current_class_type = current_class_stack[current_class_depth].type;
5932 current_access_specifier = current_class_stack[current_class_depth].access;
5933 if (current_class_stack[current_class_depth].names_used)
5934 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5937 /* Returns 1 if current_class_type is either T or a nested type of T.
5938 We start looking from 1 because entry 0 is from global scope, and has
5939 no type. */
5942 currently_open_class (t)
5943 tree t;
5945 int i;
5946 if (t == current_class_type)
5947 return 1;
5948 for (i = 1; i < current_class_depth; ++i)
5949 if (current_class_stack [i].type == t)
5950 return 1;
5951 return 0;
5954 /* If either current_class_type or one of its enclosing classes are derived
5955 from T, return the appropriate type. Used to determine how we found
5956 something via unqualified lookup. */
5958 tree
5959 currently_open_derived_class (t)
5960 tree t;
5962 int i;
5964 if (DERIVED_FROM_P (t, current_class_type))
5965 return current_class_type;
5967 for (i = current_class_depth - 1; i > 0; --i)
5968 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5969 return current_class_stack[i].type;
5971 return NULL_TREE;
5974 /* When entering a class scope, all enclosing class scopes' names with
5975 static meaning (static variables, static functions, types and enumerators)
5976 have to be visible. This recursive function calls pushclass for all
5977 enclosing class contexts until global or a local scope is reached.
5978 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5979 formal of the same name. */
5981 void
5982 push_nested_class (type, modify)
5983 tree type;
5984 int modify;
5986 tree context;
5988 /* A namespace might be passed in error cases, like A::B:C. */
5989 if (type == NULL_TREE
5990 || type == error_mark_node
5991 || TREE_CODE (type) == NAMESPACE_DECL
5992 || ! IS_AGGR_TYPE (type)
5993 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5994 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5995 return;
5997 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5999 if (context && CLASS_TYPE_P (context))
6000 push_nested_class (context, 2);
6001 pushclass (type, modify);
6004 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
6006 void
6007 pop_nested_class ()
6009 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
6011 popclass ();
6012 if (context && CLASS_TYPE_P (context))
6013 pop_nested_class ();
6016 /* Set global variables CURRENT_LANG_NAME to appropriate value
6017 so that behavior of name-mangling machinery is correct. */
6019 void
6020 push_lang_context (name)
6021 tree name;
6023 *current_lang_stack++ = current_lang_name;
6024 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
6025 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
6027 size_t old_size = VARRAY_SIZE (current_lang_base);
6029 VARRAY_GROW (current_lang_base, old_size + 10);
6030 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
6033 if (name == lang_name_cplusplus)
6035 current_lang_name = name;
6037 else if (name == lang_name_java)
6039 current_lang_name = name;
6040 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
6041 (See record_builtin_java_type in decl.c.) However, that causes
6042 incorrect debug entries if these types are actually used.
6043 So we re-enable debug output after extern "Java". */
6044 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
6045 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
6046 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
6047 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
6048 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
6049 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
6050 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
6051 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
6053 else if (name == lang_name_c)
6055 current_lang_name = name;
6057 else
6058 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
6061 /* Get out of the current language scope. */
6063 void
6064 pop_lang_context ()
6066 /* Clear the current entry so that garbage collector won't hold on
6067 to it. */
6068 *current_lang_stack = NULL_TREE;
6069 current_lang_name = *--current_lang_stack;
6072 /* Type instantiation routines. */
6074 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
6075 matches the TARGET_TYPE. If there is no satisfactory match, return
6076 error_mark_node, and issue an error message if COMPLAIN is
6077 non-zero. Permit pointers to member function if PTRMEM is non-zero.
6078 If TEMPLATE_ONLY, the name of the overloaded function
6079 was a template-id, and EXPLICIT_TARGS are the explicitly provided
6080 template arguments. */
6082 static tree
6083 resolve_address_of_overloaded_function (target_type,
6084 overload,
6085 complain,
6086 ptrmem,
6087 template_only,
6088 explicit_targs)
6089 tree target_type;
6090 tree overload;
6091 int complain;
6092 int ptrmem;
6093 int template_only;
6094 tree explicit_targs;
6096 /* Here's what the standard says:
6098 [over.over]
6100 If the name is a function template, template argument deduction
6101 is done, and if the argument deduction succeeds, the deduced
6102 arguments are used to generate a single template function, which
6103 is added to the set of overloaded functions considered.
6105 Non-member functions and static member functions match targets of
6106 type "pointer-to-function" or "reference-to-function." Nonstatic
6107 member functions match targets of type "pointer-to-member
6108 function;" the function type of the pointer to member is used to
6109 select the member function from the set of overloaded member
6110 functions. If a nonstatic member function is selected, the
6111 reference to the overloaded function name is required to have the
6112 form of a pointer to member as described in 5.3.1.
6114 If more than one function is selected, any template functions in
6115 the set are eliminated if the set also contains a non-template
6116 function, and any given template function is eliminated if the
6117 set contains a second template function that is more specialized
6118 than the first according to the partial ordering rules 14.5.5.2.
6119 After such eliminations, if any, there shall remain exactly one
6120 selected function. */
6122 int is_ptrmem = 0;
6123 int is_reference = 0;
6124 /* We store the matches in a TREE_LIST rooted here. The functions
6125 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
6126 interoperability with most_specialized_instantiation. */
6127 tree matches = NULL_TREE;
6128 tree fn;
6130 /* By the time we get here, we should be seeing only real
6131 pointer-to-member types, not the internal POINTER_TYPE to
6132 METHOD_TYPE representation. */
6133 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
6134 && (TREE_CODE (TREE_TYPE (target_type))
6135 == METHOD_TYPE)), 0);
6137 if (TREE_CODE (overload) == COMPONENT_REF)
6138 overload = TREE_OPERAND (overload, 1);
6140 /* Check that the TARGET_TYPE is reasonable. */
6141 if (TYPE_PTRFN_P (target_type))
6142 /* This is OK. */;
6143 else if (TYPE_PTRMEMFUNC_P (target_type))
6144 /* This is OK, too. */
6145 is_ptrmem = 1;
6146 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
6148 /* This is OK, too. This comes from a conversion to reference
6149 type. */
6150 target_type = build_reference_type (target_type);
6151 is_reference = 1;
6153 else
6155 if (complain)
6156 cp_error ("\
6157 cannot resolve overloaded function `%D' based on conversion to type `%T'",
6158 DECL_NAME (OVL_FUNCTION (overload)), target_type);
6159 return error_mark_node;
6162 /* If we can find a non-template function that matches, we can just
6163 use it. There's no point in generating template instantiations
6164 if we're just going to throw them out anyhow. But, of course, we
6165 can only do this when we don't *need* a template function. */
6166 if (!template_only)
6168 tree fns;
6170 for (fns = overload; fns; fns = OVL_CHAIN (fns))
6172 tree fn = OVL_FUNCTION (fns);
6173 tree fntype;
6175 if (TREE_CODE (fn) == TEMPLATE_DECL)
6176 /* We're not looking for templates just yet. */
6177 continue;
6179 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6180 != is_ptrmem)
6181 /* We're looking for a non-static member, and this isn't
6182 one, or vice versa. */
6183 continue;
6185 /* See if there's a match. */
6186 fntype = TREE_TYPE (fn);
6187 if (is_ptrmem)
6188 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
6189 else if (!is_reference)
6190 fntype = build_pointer_type (fntype);
6192 if (can_convert_arg (target_type, fntype, fn))
6193 matches = tree_cons (fn, NULL_TREE, matches);
6197 /* Now, if we've already got a match (or matches), there's no need
6198 to proceed to the template functions. But, if we don't have a
6199 match we need to look at them, too. */
6200 if (!matches)
6202 tree target_fn_type;
6203 tree target_arg_types;
6204 tree target_ret_type;
6205 tree fns;
6207 if (is_ptrmem)
6208 target_fn_type
6209 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
6210 else
6211 target_fn_type = TREE_TYPE (target_type);
6212 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
6213 target_ret_type = TREE_TYPE (target_fn_type);
6215 /* Never do unification on the 'this' parameter. */
6216 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
6217 target_arg_types = TREE_CHAIN (target_arg_types);
6219 for (fns = overload; fns; fns = OVL_CHAIN (fns))
6221 tree fn = OVL_FUNCTION (fns);
6222 tree instantiation;
6223 tree instantiation_type;
6224 tree targs;
6226 if (TREE_CODE (fn) != TEMPLATE_DECL)
6227 /* We're only looking for templates. */
6228 continue;
6230 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6231 != is_ptrmem)
6232 /* We're not looking for a non-static member, and this is
6233 one, or vice versa. */
6234 continue;
6236 /* Try to do argument deduction. */
6237 targs = make_tree_vec (DECL_NTPARMS (fn));
6238 if (fn_type_unification (fn, explicit_targs, targs,
6239 target_arg_types, target_ret_type,
6240 DEDUCE_EXACT, -1) != 0)
6241 /* Argument deduction failed. */
6242 continue;
6244 /* Instantiate the template. */
6245 instantiation = instantiate_template (fn, targs);
6246 if (instantiation == error_mark_node)
6247 /* Instantiation failed. */
6248 continue;
6250 /* See if there's a match. */
6251 instantiation_type = TREE_TYPE (instantiation);
6252 if (is_ptrmem)
6253 instantiation_type =
6254 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
6255 else if (!is_reference)
6256 instantiation_type = build_pointer_type (instantiation_type);
6257 if (can_convert_arg (target_type, instantiation_type, instantiation))
6258 matches = tree_cons (instantiation, fn, matches);
6261 /* Now, remove all but the most specialized of the matches. */
6262 if (matches)
6264 tree match = most_specialized_instantiation (matches);
6266 if (match != error_mark_node)
6267 matches = tree_cons (match, NULL_TREE, NULL_TREE);
6271 /* Now we should have exactly one function in MATCHES. */
6272 if (matches == NULL_TREE)
6274 /* There were *no* matches. */
6275 if (complain)
6277 cp_error ("no matches converting function `%D' to type `%#T'",
6278 DECL_NAME (OVL_FUNCTION (overload)),
6279 target_type);
6281 /* print_candidates expects a chain with the functions in
6282 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6283 so why be clever?). */
6284 for (; overload; overload = OVL_NEXT (overload))
6285 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6286 matches);
6288 print_candidates (matches);
6290 return error_mark_node;
6292 else if (TREE_CHAIN (matches))
6294 /* There were too many matches. */
6296 if (complain)
6298 tree match;
6300 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6301 DECL_NAME (OVL_FUNCTION (overload)),
6302 target_type);
6304 /* Since print_candidates expects the functions in the
6305 TREE_VALUE slot, we flip them here. */
6306 for (match = matches; match; match = TREE_CHAIN (match))
6307 TREE_VALUE (match) = TREE_PURPOSE (match);
6309 print_candidates (matches);
6312 return error_mark_node;
6315 /* Good, exactly one match. Now, convert it to the correct type. */
6316 fn = TREE_PURPOSE (matches);
6318 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6319 && !ptrmem && !flag_ms_extensions)
6321 static int explained;
6323 if (!complain)
6324 return error_mark_node;
6326 cp_pedwarn ("assuming pointer to member `%D'", fn);
6327 if (!explained)
6329 cp_pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6330 explained = 1;
6333 mark_used (fn);
6335 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6336 return build_unary_op (ADDR_EXPR, fn, 0);
6337 else
6339 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6340 will mark the function as addressed, but here we must do it
6341 explicitly. */
6342 mark_addressable (fn);
6344 return fn;
6348 /* This function will instantiate the type of the expression given in
6349 RHS to match the type of LHSTYPE. If errors exist, then return
6350 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6351 we complain on errors. If we are not complaining, never modify rhs,
6352 as overload resolution wants to try many possible instantiations, in
6353 the hope that at least one will work.
6355 For non-recursive calls, LHSTYPE should be a function, pointer to
6356 function, or a pointer to member function. */
6358 tree
6359 instantiate_type (lhstype, rhs, flags)
6360 tree lhstype, rhs;
6361 enum instantiate_type_flags flags;
6363 int complain = (flags & itf_complain);
6364 int strict = (flags & itf_no_attributes)
6365 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6366 int allow_ptrmem = flags & itf_ptrmem_ok;
6368 flags &= ~itf_ptrmem_ok;
6370 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6372 if (complain)
6373 error ("not enough type information");
6374 return error_mark_node;
6377 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6379 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6380 return rhs;
6381 if (complain)
6382 cp_error ("argument of type `%T' does not match `%T'",
6383 TREE_TYPE (rhs), lhstype);
6384 return error_mark_node;
6387 /* We don't overwrite rhs if it is an overloaded function.
6388 Copying it would destroy the tree link. */
6389 if (TREE_CODE (rhs) != OVERLOAD)
6390 rhs = copy_node (rhs);
6392 /* This should really only be used when attempting to distinguish
6393 what sort of a pointer to function we have. For now, any
6394 arithmetic operation which is not supported on pointers
6395 is rejected as an error. */
6397 switch (TREE_CODE (rhs))
6399 case TYPE_EXPR:
6400 case CONVERT_EXPR:
6401 case SAVE_EXPR:
6402 case CONSTRUCTOR:
6403 case BUFFER_REF:
6404 my_friendly_abort (177);
6405 return error_mark_node;
6407 case INDIRECT_REF:
6408 case ARRAY_REF:
6410 tree new_rhs;
6412 new_rhs = instantiate_type (build_pointer_type (lhstype),
6413 TREE_OPERAND (rhs, 0), flags);
6414 if (new_rhs == error_mark_node)
6415 return error_mark_node;
6417 TREE_TYPE (rhs) = lhstype;
6418 TREE_OPERAND (rhs, 0) = new_rhs;
6419 return rhs;
6422 case NOP_EXPR:
6423 rhs = copy_node (TREE_OPERAND (rhs, 0));
6424 TREE_TYPE (rhs) = unknown_type_node;
6425 return instantiate_type (lhstype, rhs, flags);
6427 case COMPONENT_REF:
6428 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6430 case OFFSET_REF:
6431 rhs = TREE_OPERAND (rhs, 1);
6432 if (BASELINK_P (rhs))
6433 return instantiate_type (lhstype, TREE_VALUE (rhs),
6434 flags | allow_ptrmem);
6436 /* This can happen if we are forming a pointer-to-member for a
6437 member template. */
6438 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6440 /* Fall through. */
6442 case TEMPLATE_ID_EXPR:
6444 tree fns = TREE_OPERAND (rhs, 0);
6445 tree args = TREE_OPERAND (rhs, 1);
6447 return
6448 resolve_address_of_overloaded_function (lhstype,
6449 fns,
6450 complain,
6451 allow_ptrmem,
6452 /*template_only=*/1,
6453 args);
6456 case OVERLOAD:
6457 return
6458 resolve_address_of_overloaded_function (lhstype,
6459 rhs,
6460 complain,
6461 allow_ptrmem,
6462 /*template_only=*/0,
6463 /*explicit_targs=*/NULL_TREE);
6465 case TREE_LIST:
6466 /* Now we should have a baselink. */
6467 my_friendly_assert (BASELINK_P (rhs), 990412);
6469 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6471 case CALL_EXPR:
6472 /* This is too hard for now. */
6473 my_friendly_abort (183);
6474 return error_mark_node;
6476 case PLUS_EXPR:
6477 case MINUS_EXPR:
6478 case COMPOUND_EXPR:
6479 TREE_OPERAND (rhs, 0)
6480 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6481 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6482 return error_mark_node;
6483 TREE_OPERAND (rhs, 1)
6484 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6485 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6486 return error_mark_node;
6488 TREE_TYPE (rhs) = lhstype;
6489 return rhs;
6491 case MULT_EXPR:
6492 case TRUNC_DIV_EXPR:
6493 case FLOOR_DIV_EXPR:
6494 case CEIL_DIV_EXPR:
6495 case ROUND_DIV_EXPR:
6496 case RDIV_EXPR:
6497 case TRUNC_MOD_EXPR:
6498 case FLOOR_MOD_EXPR:
6499 case CEIL_MOD_EXPR:
6500 case ROUND_MOD_EXPR:
6501 case FIX_ROUND_EXPR:
6502 case FIX_FLOOR_EXPR:
6503 case FIX_CEIL_EXPR:
6504 case FIX_TRUNC_EXPR:
6505 case FLOAT_EXPR:
6506 case NEGATE_EXPR:
6507 case ABS_EXPR:
6508 case MAX_EXPR:
6509 case MIN_EXPR:
6510 case FFS_EXPR:
6512 case BIT_AND_EXPR:
6513 case BIT_IOR_EXPR:
6514 case BIT_XOR_EXPR:
6515 case LSHIFT_EXPR:
6516 case RSHIFT_EXPR:
6517 case LROTATE_EXPR:
6518 case RROTATE_EXPR:
6520 case PREINCREMENT_EXPR:
6521 case PREDECREMENT_EXPR:
6522 case POSTINCREMENT_EXPR:
6523 case POSTDECREMENT_EXPR:
6524 if (complain)
6525 error ("invalid operation on uninstantiated type");
6526 return error_mark_node;
6528 case TRUTH_AND_EXPR:
6529 case TRUTH_OR_EXPR:
6530 case TRUTH_XOR_EXPR:
6531 case LT_EXPR:
6532 case LE_EXPR:
6533 case GT_EXPR:
6534 case GE_EXPR:
6535 case EQ_EXPR:
6536 case NE_EXPR:
6537 case TRUTH_ANDIF_EXPR:
6538 case TRUTH_ORIF_EXPR:
6539 case TRUTH_NOT_EXPR:
6540 if (complain)
6541 error ("not enough type information");
6542 return error_mark_node;
6544 case COND_EXPR:
6545 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6547 if (complain)
6548 error ("not enough type information");
6549 return error_mark_node;
6551 TREE_OPERAND (rhs, 1)
6552 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6553 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6554 return error_mark_node;
6555 TREE_OPERAND (rhs, 2)
6556 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6557 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6558 return error_mark_node;
6560 TREE_TYPE (rhs) = lhstype;
6561 return rhs;
6563 case MODIFY_EXPR:
6564 TREE_OPERAND (rhs, 1)
6565 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6566 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6567 return error_mark_node;
6569 TREE_TYPE (rhs) = lhstype;
6570 return rhs;
6572 case ADDR_EXPR:
6574 if (PTRMEM_OK_P (rhs))
6575 flags |= itf_ptrmem_ok;
6577 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6579 case ENTRY_VALUE_EXPR:
6580 my_friendly_abort (184);
6581 return error_mark_node;
6583 case ERROR_MARK:
6584 return error_mark_node;
6586 default:
6587 my_friendly_abort (185);
6588 return error_mark_node;
6592 /* Return the name of the virtual function pointer field
6593 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6594 this may have to look back through base types to find the
6595 ultimate field name. (For single inheritance, these could
6596 all be the same name. Who knows for multiple inheritance). */
6598 static tree
6599 get_vfield_name (type)
6600 tree type;
6602 tree binfo = TYPE_BINFO (type);
6603 char *buf;
6605 while (BINFO_BASETYPES (binfo)
6606 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6607 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6608 binfo = BINFO_BASETYPE (binfo, 0);
6610 type = BINFO_TYPE (binfo);
6611 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6612 + TYPE_NAME_LENGTH (type) + 2);
6613 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6614 return get_identifier (buf);
6617 void
6618 print_class_statistics ()
6620 #ifdef GATHER_STATISTICS
6621 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6622 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6623 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6624 n_build_method_call, n_inner_fields_searched);
6625 if (n_vtables)
6627 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6628 n_vtables, n_vtable_searches);
6629 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6630 n_vtable_entries, n_vtable_elems);
6632 #endif
6635 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6636 according to [class]:
6637 The class-name is also inserted
6638 into the scope of the class itself. For purposes of access checking,
6639 the inserted class name is treated as if it were a public member name. */
6641 void
6642 build_self_reference ()
6644 tree name = constructor_name (current_class_type);
6645 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6646 tree saved_cas;
6648 DECL_NONLOCAL (value) = 1;
6649 DECL_CONTEXT (value) = current_class_type;
6650 DECL_ARTIFICIAL (value) = 1;
6652 if (processing_template_decl)
6653 value = push_template_decl (value);
6655 saved_cas = current_access_specifier;
6656 current_access_specifier = access_public_node;
6657 finish_member_declaration (value);
6658 current_access_specifier = saved_cas;
6661 /* Returns 1 if TYPE contains only padding bytes. */
6664 is_empty_class (type)
6665 tree type;
6667 if (type == error_mark_node)
6668 return 0;
6670 if (! IS_AGGR_TYPE (type))
6671 return 0;
6673 return integer_zerop (CLASSTYPE_SIZE (type));
6676 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6677 a *_TYPE node. NODE can also be a local class. */
6679 tree
6680 get_enclosing_class (type)
6681 tree type;
6683 tree node = type;
6685 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6687 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6689 case 'd':
6690 node = DECL_CONTEXT (node);
6691 break;
6693 case 't':
6694 if (node != type)
6695 return node;
6696 node = TYPE_CONTEXT (node);
6697 break;
6699 default:
6700 my_friendly_abort (0);
6703 return NULL_TREE;
6706 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6709 is_base_of_enclosing_class (base, type)
6710 tree base, type;
6712 while (type)
6714 if (get_binfo (base, type, 0))
6715 return 1;
6717 type = get_enclosing_class (type);
6719 return 0;
6722 /* Note that NAME was looked up while the current class was being
6723 defined and that the result of that lookup was DECL. */
6725 void
6726 maybe_note_name_used_in_class (name, decl)
6727 tree name;
6728 tree decl;
6730 splay_tree names_used;
6732 /* If we're not defining a class, there's nothing to do. */
6733 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6734 return;
6736 /* If there's already a binding for this NAME, then we don't have
6737 anything to worry about. */
6738 if (IDENTIFIER_CLASS_VALUE (name))
6739 return;
6741 if (!current_class_stack[current_class_depth - 1].names_used)
6742 current_class_stack[current_class_depth - 1].names_used
6743 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6744 names_used = current_class_stack[current_class_depth - 1].names_used;
6746 splay_tree_insert (names_used,
6747 (splay_tree_key) name,
6748 (splay_tree_value) decl);
6751 /* Note that NAME was declared (as DECL) in the current class. Check
6752 to see that the declaration is legal. */
6754 void
6755 note_name_declared_in_class (name, decl)
6756 tree name;
6757 tree decl;
6759 splay_tree names_used;
6760 splay_tree_node n;
6762 /* Look to see if we ever used this name. */
6763 names_used
6764 = current_class_stack[current_class_depth - 1].names_used;
6765 if (!names_used)
6766 return;
6768 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6769 if (n)
6771 /* [basic.scope.class]
6773 A name N used in a class S shall refer to the same declaration
6774 in its context and when re-evaluated in the completed scope of
6775 S. */
6776 cp_error ("declaration of `%#D'", decl);
6777 cp_error_at ("changes meaning of `%D' from `%+#D'",
6778 DECL_NAME (OVL_CURRENT (decl)),
6779 (tree) n->value);
6783 /* Returns the VAR_DECL for the complete vtable associated with
6784 BINFO. (Under the new ABI, secondary vtables are merged with
6785 primary vtables; this function will return the VAR_DECL for the
6786 primary vtable.) */
6788 tree
6789 get_vtbl_decl_for_binfo (binfo)
6790 tree binfo;
6792 tree decl;
6794 decl = BINFO_VTABLE (binfo);
6795 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6797 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6798 2000403);
6799 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6801 if (decl)
6802 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6803 return decl;
6806 /* Called from get_primary_binfo via dfs_walk. DATA is a TREE_LIST
6807 who's TREE_PURPOSE is the TYPE of the required primary base and
6808 who's TREE_VALUE is a list of candidate binfos that we fill in. */
6810 static tree
6811 dfs_get_primary_binfo (binfo, data)
6812 tree binfo;
6813 void *data;
6815 tree cons = (tree) data;
6816 tree primary_base = TREE_PURPOSE (cons);
6818 if (TREE_VIA_VIRTUAL (binfo)
6819 && same_type_p (BINFO_TYPE (binfo), primary_base))
6820 /* This is the right type of binfo, but it might be an unshared
6821 instance, and the shared instance is later in the dfs walk. We
6822 must keep looking. */
6823 TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6825 return NULL_TREE;
6828 /* Returns the unshared binfo for the primary base of BINFO. Note
6829 that in a complex hierarchy the resulting BINFO may not actually
6830 *be* primary. In particular if the resulting BINFO is a virtual
6831 base, and it occurs elsewhere in the hierarchy, then this
6832 occurrence may not actually be a primary base in the complete
6833 object. Check BINFO_PRIMARY_P to be sure. */
6835 tree
6836 get_primary_binfo (binfo)
6837 tree binfo;
6839 tree primary_base;
6840 tree result = NULL_TREE;
6841 tree virtuals;
6843 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6844 if (!primary_base)
6845 return NULL_TREE;
6847 /* A non-virtual primary base is always a direct base, and easy to
6848 find. */
6849 if (!TREE_VIA_VIRTUAL (primary_base))
6851 int i;
6853 /* Scan the direct basetypes until we find a base with the same
6854 type as the primary base. */
6855 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6857 tree base_binfo = BINFO_BASETYPE (binfo, i);
6859 if (same_type_p (BINFO_TYPE (base_binfo),
6860 BINFO_TYPE (primary_base)))
6861 return base_binfo;
6864 /* We should always find the primary base. */
6865 my_friendly_abort (20000729);
6868 /* For a primary virtual base, we have to scan the entire hierarchy
6869 rooted at BINFO; the virtual base could be an indirect virtual
6870 base. There could be more than one instance of the primary base
6871 in the hierarchy, and if one is the canonical binfo we want that
6872 one. If it exists, it should be the first one we find, but as a
6873 consistency check we find them all and make sure. */
6874 virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6875 dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6876 virtuals = TREE_VALUE (virtuals);
6878 /* We must have found at least one instance. */
6879 my_friendly_assert (virtuals, 20010612);
6881 if (TREE_CHAIN (virtuals))
6883 /* We found more than one instance of the base. We must make
6884 sure that, if one is the canonical one, it is the first one
6885 we found. As the chain is in reverse dfs order, that means
6886 the last on the list. */
6887 tree complete_binfo;
6888 tree canonical;
6890 for (complete_binfo = binfo;
6891 BINFO_INHERITANCE_CHAIN (complete_binfo);
6892 complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6893 continue;
6894 canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6895 BINFO_TYPE (complete_binfo));
6897 for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6899 result = TREE_VALUE (virtuals);
6901 if (canonical == result)
6903 /* This is the unshared instance. Make sure it was the
6904 first one found. */
6905 my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
6906 break;
6910 else
6911 result = TREE_VALUE (virtuals);
6912 return result;
6915 /* If INDENTED_P is zero, indent to INDENT. Return non-zero. */
6917 static int
6918 maybe_indent_hierarchy (stream, indent, indented_p)
6919 FILE *stream;
6920 int indent;
6921 int indented_p;
6923 if (!indented_p)
6924 fprintf (stream, "%*s", indent, "");
6925 return 1;
6928 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6929 dominated by T) to stderr. INDENT should be zero when called from
6930 the top level; it is incremented recursively. */
6932 static void
6933 dump_class_hierarchy_r (stream, flags, t, binfo, indent)
6934 FILE *stream;
6935 int flags;
6936 tree t;
6937 tree binfo;
6938 int indent;
6940 int i;
6941 int indented = 0;
6943 indented = maybe_indent_hierarchy (stream, indent, 0);
6944 fprintf (stream, "%s (0x%lx) ",
6945 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6946 (unsigned long) binfo);
6947 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6948 tree_low_cst (BINFO_OFFSET (binfo), 0));
6949 if (is_empty_class (BINFO_TYPE (binfo)))
6950 fprintf (stream, " empty");
6951 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6952 fprintf (stream, " nearly-empty");
6953 if (TREE_VIA_VIRTUAL (binfo))
6955 tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6957 fprintf (stream, " virtual");
6958 if (canonical == binfo)
6959 fprintf (stream, " canonical");
6960 else
6961 fprintf (stream, " non-canonical");
6963 fprintf (stream, "\n");
6965 indented = 0;
6966 if (BINFO_PRIMARY_BASE_OF (binfo))
6968 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6969 fprintf (stream, " primary-for %s (0x%lx)",
6970 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6971 TFF_PLAIN_IDENTIFIER),
6972 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6974 if (BINFO_LOST_PRIMARY_P (binfo))
6976 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6977 fprintf (stream, " lost-primary");
6979 if (indented)
6980 fprintf (stream, "\n");
6982 if (!(flags & TDF_SLIM))
6984 int indented = 0;
6986 if (BINFO_SUBVTT_INDEX (binfo))
6988 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6989 fprintf (stream, " subvttidx=%s",
6990 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6991 TFF_PLAIN_IDENTIFIER));
6993 if (BINFO_VPTR_INDEX (binfo))
6995 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6996 fprintf (stream, " vptridx=%s",
6997 expr_as_string (BINFO_VPTR_INDEX (binfo),
6998 TFF_PLAIN_IDENTIFIER));
7000 if (BINFO_VPTR_FIELD (binfo))
7002 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7003 fprintf (stream, " vbaseoffset=%s",
7004 expr_as_string (BINFO_VPTR_FIELD (binfo),
7005 TFF_PLAIN_IDENTIFIER));
7007 if (BINFO_VTABLE (binfo))
7009 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7010 fprintf (stream, " vptr=%s",
7011 expr_as_string (BINFO_VTABLE (binfo),
7012 TFF_PLAIN_IDENTIFIER));
7015 if (indented)
7016 fprintf (stream, "\n");
7020 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7021 dump_class_hierarchy_r (stream, flags,
7022 t, BINFO_BASETYPE (binfo, i),
7023 indent + 2);
7026 /* Dump the BINFO hierarchy for T. */
7028 static void
7029 dump_class_hierarchy (t)
7030 tree t;
7032 int flags;
7033 FILE *stream = dump_begin (TDI_class, &flags);
7035 if (!stream)
7036 return;
7038 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7039 fprintf (stream, " size=%lu align=%lu\n",
7040 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
7041 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
7042 dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
7043 fprintf (stream, "\n");
7044 dump_end (TDI_class, stream);
7047 static void
7048 dump_array (stream, decl)
7049 FILE *stream;
7050 tree decl;
7052 tree inits;
7053 int ix;
7054 HOST_WIDE_INT elt;
7055 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
7057 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
7058 / BITS_PER_UNIT);
7059 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
7060 fprintf (stream, " %s entries",
7061 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
7062 TFF_PLAIN_IDENTIFIER));
7063 fprintf (stream, "\n");
7065 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
7066 inits; ix++, inits = TREE_CHAIN (inits))
7067 fprintf (stream, "%-4d %s\n", ix * elt,
7068 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
7071 static void
7072 dump_vtable (t, binfo, vtable)
7073 tree t;
7074 tree binfo;
7075 tree vtable;
7077 int flags;
7078 FILE *stream = dump_begin (TDI_class, &flags);
7080 if (!stream)
7081 return;
7083 if (!(flags & TDF_SLIM))
7085 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
7087 fprintf (stream, "%s for %s",
7088 ctor_vtbl_p ? "Construction vtable" : "Vtable",
7089 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
7090 if (ctor_vtbl_p)
7092 if (!TREE_VIA_VIRTUAL (binfo))
7093 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
7094 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7096 fprintf (stream, "\n");
7097 dump_array (stream, vtable);
7098 fprintf (stream, "\n");
7101 dump_end (TDI_class, stream);
7104 static void
7105 dump_vtt (t, vtt)
7106 tree t;
7107 tree vtt;
7109 int flags;
7110 FILE *stream = dump_begin (TDI_class, &flags);
7112 if (!stream)
7113 return;
7115 if (!(flags & TDF_SLIM))
7117 fprintf (stream, "VTT for %s\n",
7118 type_as_string (t, TFF_PLAIN_IDENTIFIER));
7119 dump_array (stream, vtt);
7120 fprintf (stream, "\n");
7123 dump_end (TDI_class, stream);
7126 /* Virtual function table initialization. */
7128 /* Create all the necessary vtables for T and its base classes. */
7130 static void
7131 finish_vtbls (t)
7132 tree t;
7134 if (merge_primary_and_secondary_vtables_p ())
7136 tree list;
7137 tree vbase;
7139 /* Under the new ABI, we lay out the primary and secondary
7140 vtables in one contiguous vtable. The primary vtable is
7141 first, followed by the non-virtual secondary vtables in
7142 inheritance graph order. */
7143 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
7144 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
7145 TYPE_BINFO (t), t, list);
7146 /* Then come the virtual bases, also in inheritance graph
7147 order. */
7148 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
7150 tree real_base;
7152 if (!TREE_VIA_VIRTUAL (vbase))
7153 continue;
7155 /* Although we walk in inheritance order, that might not get the
7156 canonical base. */
7157 real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
7159 accumulate_vtbl_inits (real_base, real_base,
7160 TYPE_BINFO (t), t, list);
7163 if (TYPE_BINFO_VTABLE (t))
7164 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
7166 else
7168 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
7169 dfs_unmarked_real_bases_queue_p, t);
7170 dfs_walk (TYPE_BINFO (t), dfs_unmark,
7171 dfs_marked_real_bases_queue_p, t);
7175 /* Called from finish_vtbls via dfs_walk. */
7177 static tree
7178 dfs_finish_vtbls (binfo, data)
7179 tree binfo;
7180 void *data;
7182 tree t = (tree) data;
7184 if (BINFO_NEW_VTABLE_MARKED (binfo, t))
7185 initialize_vtable (binfo,
7186 build_vtbl_initializer (binfo, binfo, t,
7187 TYPE_BINFO (t), NULL));
7189 SET_BINFO_MARKED (binfo);
7191 return NULL_TREE;
7194 /* Initialize the vtable for BINFO with the INITS. */
7196 static void
7197 initialize_vtable (binfo, inits)
7198 tree binfo;
7199 tree inits;
7201 tree decl;
7203 layout_vtable_decl (binfo, list_length (inits));
7204 decl = get_vtbl_decl_for_binfo (binfo);
7205 initialize_array (decl, inits);
7206 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
7209 /* Initialize DECL (a declaration for a namespace-scope array) with
7210 the INITS. */
7212 static void
7213 initialize_array (decl, inits)
7214 tree decl;
7215 tree inits;
7217 tree context;
7219 context = DECL_CONTEXT (decl);
7220 DECL_CONTEXT (decl) = NULL_TREE;
7221 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
7222 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
7223 DECL_CONTEXT (decl) = context;
7226 /* Build the VTT (virtual table table) for T.
7227 A class requires a VTT if it has virtual bases.
7229 This holds
7230 1 - primary virtual pointer for complete object T
7231 2 - secondary VTTs for each direct non-virtual base of T which requires a VTT
7232 3 - secondary virtual pointers for each direct or indirect base of T which
7233 has virtual bases or is reachable via a virtual path from T.
7234 4 - secondary VTTs for each direct or indirect virtual base of T.
7236 Secondary VTTs look like complete object VTTs without part 4. */
7238 static void
7239 build_vtt (t)
7240 tree t;
7242 tree inits;
7243 tree type;
7244 tree vtt;
7245 tree index;
7247 /* Build up the initializers for the VTT. */
7248 inits = NULL_TREE;
7249 index = size_zero_node;
7250 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
7252 /* If we didn't need a VTT, we're done. */
7253 if (!inits)
7254 return;
7256 /* Figure out the type of the VTT. */
7257 type = build_index_type (size_int (list_length (inits) - 1));
7258 type = build_cplus_array_type (const_ptr_type_node, type);
7260 /* Now, build the VTT object itself. */
7261 vtt = build_vtable (t, get_vtt_name (t), type);
7262 pushdecl_top_level (vtt);
7263 initialize_array (vtt, inits);
7265 dump_vtt (t, vtt);
7268 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
7269 from within some heirarchy which is inherited from the type of BINFO.
7270 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
7271 BINFO. */
7273 static tree
7274 get_original_base (base_binfo, binfo)
7275 tree base_binfo;
7276 tree binfo;
7278 tree derived;
7279 int ix;
7281 if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
7282 return binfo;
7283 if (TREE_VIA_VIRTUAL (base_binfo))
7284 return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
7285 derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
7287 for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
7288 if (same_type_p (BINFO_TYPE (base_binfo),
7289 BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
7290 return BINFO_BASETYPE (derived, ix);
7291 my_friendly_abort (20010223);
7292 return NULL;
7295 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7296 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7297 and CHAIN the vtable pointer for this binfo after construction is
7298 complete. VALUE can also be another BINFO, in which case we recurse. */
7300 static tree
7301 binfo_ctor_vtable (binfo)
7302 tree binfo;
7304 tree vt;
7306 while (1)
7308 vt = BINFO_VTABLE (binfo);
7309 if (TREE_CODE (vt) == TREE_LIST)
7310 vt = TREE_VALUE (vt);
7311 if (TREE_CODE (vt) == TREE_VEC)
7312 binfo = vt;
7313 else
7314 break;
7317 return vt;
7320 /* Recursively build the VTT-initializer for BINFO (which is in the
7321 hierarchy dominated by T). INITS points to the end of the initializer
7322 list to date. INDEX is the VTT index where the next element will be
7323 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7324 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7325 for virtual bases of T. When it is not so, we build the constructor
7326 vtables for the BINFO-in-T variant. */
7328 static tree *
7329 build_vtt_inits (binfo, t, inits, index)
7330 tree binfo;
7331 tree t;
7332 tree *inits;
7333 tree *index;
7335 int i;
7336 tree b;
7337 tree init;
7338 tree secondary_vptrs;
7339 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7341 /* We only need VTTs for subobjects with virtual bases. */
7342 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7343 return inits;
7345 /* We need to use a construction vtable if this is not the primary
7346 VTT. */
7347 if (!top_level_p)
7349 build_ctor_vtbl_group (binfo, t);
7351 /* Record the offset in the VTT where this sub-VTT can be found. */
7352 BINFO_SUBVTT_INDEX (binfo) = *index;
7355 /* Add the address of the primary vtable for the complete object. */
7356 init = binfo_ctor_vtable (binfo);
7357 *inits = build_tree_list (NULL_TREE, init);
7358 inits = &TREE_CHAIN (*inits);
7359 if (top_level_p)
7361 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7362 BINFO_VPTR_INDEX (binfo) = *index;
7364 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7366 /* Recursively add the secondary VTTs for non-virtual bases. */
7367 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7369 b = BINFO_BASETYPE (binfo, i);
7370 if (!TREE_VIA_VIRTUAL (b))
7371 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
7372 inits, index);
7375 /* Add secondary virtual pointers for all subobjects of BINFO with
7376 either virtual bases or reachable along a virtual path, except
7377 subobjects that are non-virtual primary bases. */
7378 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7379 TREE_TYPE (secondary_vptrs) = *index;
7380 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7381 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7383 dfs_walk_real (binfo,
7384 dfs_build_secondary_vptr_vtt_inits,
7385 NULL,
7386 dfs_ctor_vtable_bases_queue_p,
7387 secondary_vptrs);
7388 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7389 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7390 secondary_vptrs);
7392 *index = TREE_TYPE (secondary_vptrs);
7394 /* The secondary vptrs come back in reverse order. After we reverse
7395 them, and add the INITS, the last init will be the first element
7396 of the chain. */
7397 secondary_vptrs = TREE_VALUE (secondary_vptrs);
7398 if (secondary_vptrs)
7400 *inits = nreverse (secondary_vptrs);
7401 inits = &TREE_CHAIN (secondary_vptrs);
7402 my_friendly_assert (*inits == NULL_TREE, 20000517);
7405 /* Add the secondary VTTs for virtual bases. */
7406 if (top_level_p)
7407 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7409 tree vbase;
7411 if (!TREE_VIA_VIRTUAL (b))
7412 continue;
7414 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7415 inits = build_vtt_inits (vbase, t, inits, index);
7418 if (!top_level_p)
7420 tree data = tree_cons (t, binfo, NULL_TREE);
7421 VTT_TOP_LEVEL_P (data) = 0;
7422 VTT_MARKED_BINFO_P (data) = 0;
7424 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7425 dfs_ctor_vtable_bases_queue_p,
7426 data);
7429 return inits;
7432 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7433 for the base in most derived. DATA is a TREE_LIST who's
7434 TREE_CHAIN is the type of the base being
7435 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7436 flag of DATA indicates that this is a constructor vtable. The
7437 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7439 static tree
7440 dfs_build_secondary_vptr_vtt_inits (binfo, data)
7441 tree binfo;
7442 void *data;
7444 tree l;
7445 tree t;
7446 tree init;
7447 tree index;
7448 int top_level_p;
7450 l = (tree) data;
7451 t = TREE_CHAIN (l);
7452 top_level_p = VTT_TOP_LEVEL_P (l);
7454 SET_BINFO_MARKED (binfo);
7456 /* We don't care about bases that don't have vtables. */
7457 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7458 return NULL_TREE;
7460 /* We're only interested in proper subobjects of T. */
7461 if (same_type_p (BINFO_TYPE (binfo), t))
7462 return NULL_TREE;
7464 /* We're not interested in non-virtual primary bases. */
7465 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7466 return NULL_TREE;
7468 /* If BINFO has virtual bases or is reachable via a virtual path
7469 from T, it'll have a secondary vptr. */
7470 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7471 && !binfo_via_virtual (binfo, t))
7472 return NULL_TREE;
7474 /* Record the index where this secondary vptr can be found. */
7475 index = TREE_TYPE (l);
7476 if (top_level_p)
7478 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7479 BINFO_VPTR_INDEX (binfo) = index;
7481 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7482 TYPE_SIZE_UNIT (ptr_type_node));
7484 /* Add the initializer for the secondary vptr itself. */
7485 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7487 /* It's a primary virtual base, and this is not the construction
7488 vtable. Find the base this is primary of in the inheritance graph,
7489 and use that base's vtable now. */
7490 while (BINFO_PRIMARY_BASE_OF (binfo))
7491 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7493 init = binfo_ctor_vtable (binfo);
7494 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7496 return NULL_TREE;
7499 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7500 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7501 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7502 hierarchy. */
7504 static tree
7505 dfs_ctor_vtable_bases_queue_p (binfo, data)
7506 tree binfo;
7507 void *data;
7509 if (TREE_VIA_VIRTUAL (binfo))
7510 /* Get the shared version. */
7511 binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
7513 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7514 return NULL_TREE;
7515 return binfo;
7518 /* Called from build_vtt_inits via dfs_walk. After building constructor
7519 vtables and generating the sub-vtt from them, we need to restore the
7520 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7521 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7523 static tree
7524 dfs_fixup_binfo_vtbls (binfo, data)
7525 tree binfo;
7526 void *data;
7528 CLEAR_BINFO_MARKED (binfo);
7530 /* We don't care about bases that don't have vtables. */
7531 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7532 return NULL_TREE;
7534 /* If we scribbled the construction vtable vptr into BINFO, clear it
7535 out now. */
7536 if (BINFO_VTABLE (binfo)
7537 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7538 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7539 == TREE_VALUE ((tree) data)))
7540 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7542 return NULL_TREE;
7545 /* Build the construction vtable group for BINFO which is in the
7546 hierarchy dominated by T. */
7548 static void
7549 build_ctor_vtbl_group (binfo, t)
7550 tree binfo;
7551 tree t;
7553 tree list;
7554 tree type;
7555 tree vtbl;
7556 tree inits;
7557 tree id;
7558 tree vbase;
7560 /* See if we've already created this construction vtable group. */
7561 id = mangle_ctor_vtbl_for_type (t, binfo);
7562 if (IDENTIFIER_GLOBAL_VALUE (id))
7563 return;
7565 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7566 /* Build a version of VTBL (with the wrong type) for use in
7567 constructing the addresses of secondary vtables in the
7568 construction vtable group. */
7569 vtbl = build_vtable (t, id, ptr_type_node);
7570 list = build_tree_list (vtbl, NULL_TREE);
7571 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7572 binfo, t, list);
7574 /* Add the vtables for each of our virtual bases using the vbase in T
7575 binfo. */
7576 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7577 vbase;
7578 vbase = TREE_CHAIN (vbase))
7580 tree b;
7581 tree orig_base;
7583 if (!TREE_VIA_VIRTUAL (vbase))
7584 continue;
7585 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7586 orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7588 accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7590 inits = TREE_VALUE (list);
7592 /* Figure out the type of the construction vtable. */
7593 type = build_index_type (size_int (list_length (inits) - 1));
7594 type = build_cplus_array_type (vtable_entry_type, type);
7595 TREE_TYPE (vtbl) = type;
7597 /* Initialize the construction vtable. */
7598 pushdecl_top_level (vtbl);
7599 initialize_array (vtbl, inits);
7600 dump_vtable (t, binfo, vtbl);
7603 /* Add the vtbl initializers for BINFO (and its bases other than
7604 non-virtual primaries) to the list of INITS. BINFO is in the
7605 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7606 the constructor the vtbl inits should be accumulated for. (If this
7607 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7608 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7609 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7610 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7611 but are not necessarily the same in terms of layout. */
7613 static void
7614 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7615 tree binfo;
7616 tree orig_binfo;
7617 tree rtti_binfo;
7618 tree t;
7619 tree inits;
7621 int i;
7622 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7624 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7625 BINFO_TYPE (orig_binfo)),
7626 20000517);
7628 /* If it doesn't have a vptr, we don't do anything. */
7629 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7630 return;
7632 /* If we're building a construction vtable, we're not interested in
7633 subobjects that don't require construction vtables. */
7634 if (ctor_vtbl_p
7635 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7636 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7637 return;
7639 /* Build the initializers for the BINFO-in-T vtable. */
7640 TREE_VALUE (inits)
7641 = chainon (TREE_VALUE (inits),
7642 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7643 rtti_binfo, t, inits));
7645 /* Walk the BINFO and its bases. We walk in preorder so that as we
7646 initialize each vtable we can figure out at what offset the
7647 secondary vtable lies from the primary vtable. We can't use
7648 dfs_walk here because we need to iterate through bases of BINFO
7649 and RTTI_BINFO simultaneously. */
7650 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7652 tree base_binfo = BINFO_BASETYPE (binfo, i);
7654 /* Skip virtual bases. */
7655 if (TREE_VIA_VIRTUAL (base_binfo))
7656 continue;
7657 accumulate_vtbl_inits (base_binfo,
7658 BINFO_BASETYPE (orig_binfo, i),
7659 rtti_binfo, t,
7660 inits);
7664 /* Called from accumulate_vtbl_inits when using the new ABI.
7665 Accumulates the vtable initializers for all of the vtables into
7666 TREE_VALUE (DATA). Returns the initializers for the BINFO vtable. */
7668 static tree
7669 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7670 tree binfo;
7671 tree orig_binfo;
7672 tree rtti_binfo;
7673 tree t;
7674 tree l;
7676 tree inits = NULL_TREE;
7677 tree vtbl = NULL_TREE;
7678 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7680 if (ctor_vtbl_p
7681 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7683 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7684 primary virtual base. If it is not the same primary in
7685 the hierarchy of T, we'll need to generate a ctor vtable
7686 for it, to place at its location in T. If it is the same
7687 primary, we still need a VTT entry for the vtable, but it
7688 should point to the ctor vtable for the base it is a
7689 primary for within the sub-hierarchy of RTTI_BINFO.
7691 There are three possible cases:
7693 1) We are in the same place.
7694 2) We are a primary base within a lost primary virtual base of
7695 RTTI_BINFO.
7696 3) We are primary to something not a base of RTTI_BINFO. */
7698 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7699 tree last = NULL_TREE;
7701 /* First, look through the bases we are primary to for RTTI_BINFO
7702 or a virtual base. */
7703 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7705 last = b;
7706 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7707 break;
7709 /* If we run out of primary links, keep looking down our
7710 inheritance chain; we might be an indirect primary. */
7711 if (b == NULL_TREE)
7712 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7713 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7714 break;
7716 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7717 base B and it is a base of RTTI_BINFO, this is case 2. In
7718 either case, we share our vtable with LAST, i.e. the
7719 derived-most base within B of which we are a primary. */
7720 if (b == rtti_binfo
7721 || (b && binfo_for_vbase (BINFO_TYPE (b),
7722 BINFO_TYPE (rtti_binfo))))
7723 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7724 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7725 binfo_ctor_vtable after everything's been set up. */
7726 vtbl = last;
7728 /* Otherwise, this is case 3 and we get our own. */
7730 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7731 return inits;
7733 if (!vtbl)
7735 tree index;
7736 int non_fn_entries;
7738 /* Compute the initializer for this vtable. */
7739 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7740 &non_fn_entries);
7742 /* Figure out the position to which the VPTR should point. */
7743 vtbl = TREE_PURPOSE (l);
7744 vtbl = build1 (ADDR_EXPR,
7745 vtbl_ptr_type_node,
7746 vtbl);
7747 index = size_binop (PLUS_EXPR,
7748 size_int (non_fn_entries),
7749 size_int (list_length (TREE_VALUE (l))));
7750 index = size_binop (MULT_EXPR,
7751 TYPE_SIZE_UNIT (vtable_entry_type),
7752 index);
7753 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7754 TREE_CONSTANT (vtbl) = 1;
7757 if (ctor_vtbl_p)
7758 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7759 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7760 straighten this out. */
7761 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7762 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7763 inits = NULL_TREE;
7764 else
7765 /* For an ordinary vtable, set BINFO_VTABLE. */
7766 BINFO_VTABLE (binfo) = vtbl;
7768 return inits;
7771 /* Construct the initializer for BINFOs virtual function table. BINFO
7772 is part of the hierarchy dominated by T. If we're building a
7773 construction vtable, the ORIG_BINFO is the binfo we should use to
7774 find the actual function pointers to put in the vtable - but they
7775 can be overridden on the path to most-derived in the graph that
7776 ORIG_BINFO belongs. Otherwise,
7777 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7778 BINFO that should be indicated by the RTTI information in the
7779 vtable; it will be a base class of T, rather than T itself, if we
7780 are building a construction vtable.
7782 The value returned is a TREE_LIST suitable for wrapping in a
7783 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7784 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7785 number of non-function entries in the vtable.
7787 It might seem that this function should never be called with a
7788 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7789 base is always subsumed by a derived class vtable. However, when
7790 we are building construction vtables, we do build vtables for
7791 primary bases; we need these while the primary base is being
7792 constructed. */
7794 static tree
7795 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7796 tree binfo;
7797 tree orig_binfo;
7798 tree t;
7799 tree rtti_binfo;
7800 int *non_fn_entries_p;
7802 tree v, b;
7803 tree vfun_inits;
7804 tree vbase;
7805 vtbl_init_data vid;
7807 /* Initialize VID. */
7808 memset (&vid, 0, sizeof (vid));
7809 vid.binfo = binfo;
7810 vid.derived = t;
7811 vid.rtti_binfo = rtti_binfo;
7812 vid.last_init = &vid.inits;
7813 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7814 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7815 /* The first vbase or vcall offset is at index -3 in the vtable. */
7816 vid.index = ssize_int (-3);
7818 /* Add entries to the vtable for RTTI. */
7819 build_rtti_vtbl_entries (binfo, &vid);
7821 /* Create an array for keeping track of the functions we've
7822 processed. When we see multiple functions with the same
7823 signature, we share the vcall offsets. */
7824 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7825 /* Add the vcall and vbase offset entries. */
7826 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7827 /* Clean up. */
7828 VARRAY_FREE (vid.fns);
7829 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7830 build_vbase_offset_vtbl_entries. */
7831 for (vbase = CLASSTYPE_VBASECLASSES (t);
7832 vbase;
7833 vbase = TREE_CHAIN (vbase))
7834 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7836 if (non_fn_entries_p)
7837 *non_fn_entries_p = list_length (vid.inits);
7839 /* Go through all the ordinary virtual functions, building up
7840 initializers. */
7841 vfun_inits = NULL_TREE;
7842 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7844 tree delta;
7845 tree vcall_index;
7846 tree fn;
7847 tree pfn;
7848 tree init;
7850 /* Pull the offset for `this', and the function to call, out of
7851 the list. */
7852 delta = BV_DELTA (v);
7854 if (BV_USE_VCALL_INDEX_P (v))
7856 vcall_index = BV_VCALL_INDEX (v);
7857 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
7859 else
7860 vcall_index = NULL_TREE;
7862 fn = BV_FN (v);
7863 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7864 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7866 /* You can't call an abstract virtual function; it's abstract.
7867 So, we replace these functions with __pure_virtual. */
7868 if (DECL_PURE_VIRTUAL_P (fn))
7869 fn = abort_fndecl;
7871 /* Take the address of the function, considering it to be of an
7872 appropriate generic type. */
7873 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7874 /* The address of a function can't change. */
7875 TREE_CONSTANT (pfn) = 1;
7877 /* Enter it in the vtable. */
7878 init = build_vtable_entry (delta, vcall_index, pfn);
7880 /* If the only definition of this function signature along our
7881 primary base chain is from a lost primary, this vtable slot will
7882 never be used, so just zero it out. This is important to avoid
7883 requiring extra thunks which cannot be generated with the function.
7885 We could also handle this in update_vtable_entry_for_fn; doing it
7886 here means we zero out unused slots in ctor vtables as well,
7887 rather than filling them with erroneous values (though harmless,
7888 apart from relocation costs). */
7889 if (fn != abort_fndecl)
7890 for (b = binfo; ; b = get_primary_binfo (b))
7892 /* We found a defn before a lost primary; go ahead as normal. */
7893 if (look_for_overrides_here (BINFO_TYPE (b), fn))
7894 break;
7896 /* The nearest definition is from a lost primary; clear the
7897 slot. */
7898 if (BINFO_LOST_PRIMARY_P (b))
7900 init = size_zero_node;
7901 break;
7905 /* And add it to the chain of initializers. */
7906 if (TARGET_VTABLE_USES_DESCRIPTORS)
7908 int i;
7909 if (init == size_zero_node)
7910 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7911 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7912 else
7913 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7915 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7916 TREE_OPERAND (init, 0),
7917 build_int_2 (i, 0));
7918 TREE_CONSTANT (fdesc) = 1;
7920 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7923 else
7924 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7927 /* The initializers for virtual functions were built up in reverse
7928 order; straighten them out now. */
7929 vfun_inits = nreverse (vfun_inits);
7931 /* The negative offset initializers are also in reverse order. */
7932 vid.inits = nreverse (vid.inits);
7934 /* Chain the two together. */
7935 return chainon (vid.inits, vfun_inits);
7938 /* Adds to vid->inits the initializers for the vbase and vcall
7939 offsets in BINFO, which is in the hierarchy dominated by T. */
7941 static void
7942 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7943 tree binfo;
7944 vtbl_init_data *vid;
7946 tree b;
7948 /* If this is a derived class, we must first create entries
7949 corresponding to the primary base class. */
7950 b = get_primary_binfo (binfo);
7951 if (b)
7952 build_vcall_and_vbase_vtbl_entries (b, vid);
7954 /* Add the vbase entries for this base. */
7955 build_vbase_offset_vtbl_entries (binfo, vid);
7956 /* Add the vcall entries for this base. */
7957 build_vcall_offset_vtbl_entries (binfo, vid);
7960 /* Returns the initializers for the vbase offset entries in the vtable
7961 for BINFO (which is part of the class hierarchy dominated by T), in
7962 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7963 where the next vbase offset will go. */
7965 static void
7966 build_vbase_offset_vtbl_entries (binfo, vid)
7967 tree binfo;
7968 vtbl_init_data *vid;
7970 tree vbase;
7971 tree t;
7972 tree non_primary_binfo;
7974 /* Under the old ABI, pointers to virtual bases are stored in each
7975 object. */
7976 if (!vbase_offsets_in_vtable_p ())
7977 return;
7979 /* If there are no virtual baseclasses, then there is nothing to
7980 do. */
7981 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7982 return;
7984 t = vid->derived;
7986 /* We might be a primary base class. Go up the inheritance hierarchy
7987 until we find the most derived class of which we are a primary base:
7988 it is the offset of that which we need to use. */
7989 non_primary_binfo = binfo;
7990 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7992 tree b;
7994 /* If we have reached a virtual base, then it must be a primary
7995 base (possibly multi-level) of vid->binfo, or we wouldn't
7996 have called build_vcall_and_vbase_vtbl_entries for it. But it
7997 might be a lost primary, so just skip down to vid->binfo. */
7998 if (TREE_VIA_VIRTUAL (non_primary_binfo))
8000 non_primary_binfo = vid->binfo;
8001 break;
8004 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8005 if (get_primary_binfo (b) != non_primary_binfo)
8006 break;
8007 non_primary_binfo = b;
8010 /* Go through the virtual bases, adding the offsets. */
8011 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8012 vbase;
8013 vbase = TREE_CHAIN (vbase))
8015 tree b;
8016 tree delta;
8018 if (!TREE_VIA_VIRTUAL (vbase))
8019 continue;
8021 /* Find the instance of this virtual base in the complete
8022 object. */
8023 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
8025 /* If we've already got an offset for this virtual base, we
8026 don't need another one. */
8027 if (BINFO_VTABLE_PATH_MARKED (b))
8028 continue;
8029 SET_BINFO_VTABLE_PATH_MARKED (b);
8031 /* Figure out where we can find this vbase offset. */
8032 delta = size_binop (MULT_EXPR,
8033 vid->index,
8034 convert (ssizetype,
8035 TYPE_SIZE_UNIT (vtable_entry_type)));
8036 if (vid->primary_vtbl_p)
8037 BINFO_VPTR_FIELD (b) = delta;
8039 if (binfo != TYPE_BINFO (t))
8041 tree orig_vbase;
8043 /* Find the instance of this virtual base in the type of BINFO. */
8044 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
8045 BINFO_TYPE (binfo));
8047 /* The vbase offset had better be the same. */
8048 if (!tree_int_cst_equal (delta,
8049 BINFO_VPTR_FIELD (orig_vbase)))
8050 my_friendly_abort (20000403);
8053 /* The next vbase will come at a more negative offset. */
8054 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
8056 /* The initializer is the delta from BINFO to this virtual base.
8057 The vbase offsets go in reverse inheritance-graph order, and
8058 we are walking in inheritance graph order so these end up in
8059 the right order. */
8060 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
8062 *vid->last_init
8063 = build_tree_list (NULL_TREE,
8064 fold (build1 (NOP_EXPR,
8065 vtable_entry_type,
8066 delta)));
8067 vid->last_init = &TREE_CHAIN (*vid->last_init);
8071 /* Adds the initializers for the vcall offset entries in the vtable
8072 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
8073 to VID->INITS. */
8075 static void
8076 build_vcall_offset_vtbl_entries (binfo, vid)
8077 tree binfo;
8078 vtbl_init_data *vid;
8080 /* Under the old ABI, the adjustments to the `this' pointer were made
8081 elsewhere. */
8082 if (!vcall_offsets_in_vtable_p ())
8083 return;
8085 /* We only need these entries if this base is a virtual base. */
8086 if (!TREE_VIA_VIRTUAL (binfo))
8087 return;
8089 /* We need a vcall offset for each of the virtual functions in this
8090 vtable. For example:
8092 class A { virtual void f (); };
8093 class B1 : virtual public A { virtual void f (); };
8094 class B2 : virtual public A { virtual void f (); };
8095 class C: public B1, public B2 { virtual void f (); };
8097 A C object has a primary base of B1, which has a primary base of A. A
8098 C also has a secondary base of B2, which no longer has a primary base
8099 of A. So the B2-in-C construction vtable needs a secondary vtable for
8100 A, which will adjust the A* to a B2* to call f. We have no way of
8101 knowing what (or even whether) this offset will be when we define B2,
8102 so we store this "vcall offset" in the A sub-vtable and look it up in
8103 a "virtual thunk" for B2::f.
8105 We need entries for all the functions in our primary vtable and
8106 in our non-virtual bases' secondary vtables. */
8107 vid->vbase = binfo;
8108 /* Now, walk through the non-virtual bases, adding vcall offsets. */
8109 add_vcall_offset_vtbl_entries_r (binfo, vid);
8112 /* Build vcall offsets, starting with those for BINFO. */
8114 static void
8115 add_vcall_offset_vtbl_entries_r (binfo, vid)
8116 tree binfo;
8117 vtbl_init_data *vid;
8119 int i;
8120 tree primary_binfo;
8122 /* Don't walk into virtual bases -- except, of course, for the
8123 virtual base for which we are building vcall offsets. Any
8124 primary virtual base will have already had its offsets generated
8125 through the recursion in build_vcall_and_vbase_vtbl_entries. */
8126 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
8127 return;
8129 /* If BINFO has a primary base, process it first. */
8130 primary_binfo = get_primary_binfo (binfo);
8131 if (primary_binfo)
8132 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
8134 /* Add BINFO itself to the list. */
8135 add_vcall_offset_vtbl_entries_1 (binfo, vid);
8137 /* Scan the non-primary bases of BINFO. */
8138 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
8140 tree base_binfo;
8142 base_binfo = BINFO_BASETYPE (binfo, i);
8143 if (base_binfo != primary_binfo)
8144 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
8148 /* Called from build_vcall_offset_vtbl_entries_r. */
8150 static void
8151 add_vcall_offset_vtbl_entries_1 (binfo, vid)
8152 tree binfo;
8153 vtbl_init_data* vid;
8155 tree derived_virtuals;
8156 tree base_virtuals;
8157 tree orig_virtuals;
8158 tree binfo_inits;
8159 /* If BINFO is a primary base, the most derived class which has BINFO as
8160 a primary base; otherwise, just BINFO. */
8161 tree non_primary_binfo;
8163 binfo_inits = NULL_TREE;
8165 /* We might be a primary base class. Go up the inheritance hierarchy
8166 until we find the most derived class of which we are a primary base:
8167 it is the BINFO_VIRTUALS there that we need to consider. */
8168 non_primary_binfo = binfo;
8169 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8171 tree b;
8173 /* If we have reached a virtual base, then it must be vid->vbase,
8174 because we ignore other virtual bases in
8175 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
8176 base (possibly multi-level) of vid->binfo, or we wouldn't
8177 have called build_vcall_and_vbase_vtbl_entries for it. But it
8178 might be a lost primary, so just skip down to vid->binfo. */
8179 if (TREE_VIA_VIRTUAL (non_primary_binfo))
8181 if (non_primary_binfo != vid->vbase)
8182 abort ();
8183 non_primary_binfo = vid->binfo;
8184 break;
8187 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8188 if (get_primary_binfo (b) != non_primary_binfo)
8189 break;
8190 non_primary_binfo = b;
8193 if (vid->ctor_vtbl_p)
8194 /* For a ctor vtable we need the equivalent binfo within the hierarchy
8195 where rtti_binfo is the most derived type. */
8196 non_primary_binfo = get_original_base
8197 (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
8199 /* Make entries for the rest of the virtuals. */
8200 for (base_virtuals = BINFO_VIRTUALS (binfo),
8201 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
8202 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
8203 base_virtuals;
8204 base_virtuals = TREE_CHAIN (base_virtuals),
8205 derived_virtuals = TREE_CHAIN (derived_virtuals),
8206 orig_virtuals = TREE_CHAIN (orig_virtuals))
8208 tree orig_fn;
8209 tree fn;
8210 tree base;
8211 tree base_binfo;
8212 size_t i;
8213 tree vcall_offset;
8215 /* Find the declaration that originally caused this function to
8216 be present in BINFO_TYPE (binfo). */
8217 orig_fn = BV_FN (orig_virtuals);
8219 /* When processing BINFO, we only want to generate vcall slots for
8220 function slots introduced in BINFO. So don't try to generate
8221 one if the function isn't even defined in BINFO. */
8222 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
8223 continue;
8225 /* Find the overriding function. */
8226 fn = BV_FN (derived_virtuals);
8228 /* If there is already an entry for a function with the same
8229 signature as FN, then we do not need a second vcall offset.
8230 Check the list of functions already present in the derived
8231 class vtable. */
8232 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
8234 tree derived_entry;
8236 derived_entry = VARRAY_TREE (vid->fns, i);
8237 if (same_signature_p (BV_FN (derived_entry), fn)
8238 /* We only use one vcall offset for virtual destructors,
8239 even though there are two virtual table entries. */
8240 || (DECL_DESTRUCTOR_P (BV_FN (derived_entry))
8241 && DECL_DESTRUCTOR_P (fn)))
8243 if (!vid->ctor_vtbl_p)
8244 BV_VCALL_INDEX (derived_virtuals)
8245 = BV_VCALL_INDEX (derived_entry);
8246 break;
8249 if (i != VARRAY_ACTIVE_SIZE (vid->fns))
8250 continue;
8252 /* The FN comes from BASE. So, we must calculate the adjustment from
8253 vid->vbase to BASE. We can just look for BASE in the complete
8254 object because we are converting from a virtual base, so if there
8255 were multiple copies, there would not be a unique final overrider
8256 and vid->derived would be ill-formed. */
8257 base = DECL_CONTEXT (fn);
8258 base_binfo = get_binfo (base, vid->derived, /*protect=*/0);
8260 /* Compute the vcall offset. */
8261 /* As mentioned above, the vbase we're working on is a primary base of
8262 vid->binfo. But it might be a lost primary, so its BINFO_OFFSET
8263 might be wrong, so we just use the BINFO_OFFSET from vid->binfo. */
8264 vcall_offset = BINFO_OFFSET (vid->binfo);
8265 vcall_offset = size_diffop (BINFO_OFFSET (base_binfo),
8266 vcall_offset);
8267 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
8268 vcall_offset));
8270 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
8271 vid->last_init = &TREE_CHAIN (*vid->last_init);
8273 /* Keep track of the vtable index where this vcall offset can be
8274 found. For a construction vtable, we already made this
8275 annotation when we built the original vtable. */
8276 if (!vid->ctor_vtbl_p)
8277 BV_VCALL_INDEX (derived_virtuals) = vid->index;
8279 /* The next vcall offset will be found at a more negative
8280 offset. */
8281 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
8283 /* Keep track of this function. */
8284 VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
8288 /* Return vtbl initializers for the RTTI entries coresponding to the
8289 BINFO's vtable. The RTTI entries should indicate the object given
8290 by VID->rtti_binfo. */
8292 static void
8293 build_rtti_vtbl_entries (binfo, vid)
8294 tree binfo;
8295 vtbl_init_data *vid;
8297 tree b;
8298 tree t;
8299 tree basetype;
8300 tree offset;
8301 tree decl;
8302 tree init;
8304 basetype = BINFO_TYPE (binfo);
8305 t = BINFO_TYPE (vid->rtti_binfo);
8307 /* For a COM object there is no RTTI entry. */
8308 if (CLASSTYPE_COM_INTERFACE (basetype))
8309 return;
8311 /* To find the complete object, we will first convert to our most
8312 primary base, and then add the offset in the vtbl to that value. */
8313 b = binfo;
8314 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8315 && !BINFO_LOST_PRIMARY_P (b))
8317 tree primary_base;
8319 primary_base = get_primary_binfo (b);
8320 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
8321 b = primary_base;
8323 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8325 /* The second entry is the address of the typeinfo object. */
8326 if (flag_rtti)
8327 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
8328 else
8329 decl = integer_zero_node;
8331 /* Convert the declaration to a type that can be stored in the
8332 vtable. */
8333 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
8334 TREE_CONSTANT (init) = 1;
8335 *vid->last_init = build_tree_list (NULL_TREE, init);
8336 vid->last_init = &TREE_CHAIN (*vid->last_init);
8338 /* Add the offset-to-top entry. It comes earlier in the vtable that
8339 the the typeinfo entry. */
8340 if (flag_vtable_thunks)
8342 /* Convert the offset to look like a function pointer, so that
8343 we can put it in the vtable. */
8344 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
8345 TREE_CONSTANT (init) = 1;
8346 *vid->last_init = build_tree_list (NULL_TREE, init);
8347 vid->last_init = &TREE_CHAIN (*vid->last_init);
8351 /* Build an entry in the virtual function table. DELTA is the offset
8352 for the `this' pointer. VCALL_INDEX is the vtable index containing
8353 the vcall offset; NULL_TREE if none. ENTRY is the virtual function
8354 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
8355 but it may not actually be a virtual function table pointer. (For
8356 example, it might be the address of the RTTI object, under the new
8357 ABI.) */
8359 static tree
8360 build_vtable_entry (delta, vcall_index, entry)
8361 tree delta;
8362 tree vcall_index;
8363 tree entry;
8365 if (flag_vtable_thunks)
8367 tree fn;
8369 fn = TREE_OPERAND (entry, 0);
8370 if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
8371 && fn != abort_fndecl
8372 && !DECL_TINFO_FN_P (fn))
8374 entry = make_thunk (entry, delta, vcall_index);
8375 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
8376 TREE_READONLY (entry) = 1;
8377 TREE_CONSTANT (entry) = 1;
8379 #ifdef GATHER_STATISTICS
8380 n_vtable_entries += 1;
8381 #endif
8382 return entry;
8384 else
8386 tree elems = tree_cons (NULL_TREE, delta,
8387 tree_cons (NULL_TREE, integer_zero_node,
8388 build_tree_list (NULL_TREE, entry)));
8389 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
8391 /* We don't use vcall offsets when not using vtable thunks. */
8392 my_friendly_assert (vcall_index == NULL_TREE, 20000125);
8394 /* DELTA used to be constructed by `size_int' and/or size_binop,
8395 which caused overflow problems when it was negative. That should
8396 be fixed now. */
8398 if (! int_fits_type_p (delta, delta_type_node))
8400 if (flag_huge_objects)
8401 sorry ("object size exceeds built-in limit for virtual function table implementation");
8402 else
8403 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
8406 TREE_CONSTANT (entry) = 1;
8407 TREE_STATIC (entry) = 1;
8408 TREE_READONLY (entry) = 1;
8410 #ifdef GATHER_STATISTICS
8411 n_vtable_entries += 1;
8412 #endif
8414 return entry;