* c-common.c (decl_attributes): Take a pointer to the node to
[official-gcc.git] / gcc / cp / class.c
blob2ec3448d6365ce717613496143f17e14218664d6
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));
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, 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);
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, idx)
526 tree basetype, idx;
528 static char asm_stmt[] = ".vtable_entry %c0, %c1";
529 tree s, i, i2;
530 tree vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
531 tree first_fn = TYPE_BINFO_VTABLE (basetype);
533 s = build_unary_op (ADDR_EXPR, vtable, 0);
534 s = build_tree_list (build_string (1, "s"), s);
536 i = build_array_ref (first_fn, idx);
537 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
538 i2 = build_array_ref (vtable, build_int_2 (0,0));
539 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
540 i = cp_build_binary_op (MINUS_EXPR, i, i2);
541 i = build_tree_list (build_string (1, "i"), i);
543 finish_asm_stmt (ridpointers[RID_VOLATILE],
544 build_string (sizeof(asm_stmt)-1, asm_stmt),
545 NULL_TREE, chainon (s, i), NULL_TREE);
548 /* Given an object INSTANCE, return an expression which yields the
549 virtual function vtable element corresponding to INDEX. There are
550 many special cases for INSTANCE which we take care of here, mainly
551 to avoid creating extra tree nodes when we don't have to. */
553 tree
554 build_vtbl_ref (instance, idx)
555 tree instance, idx;
557 tree vtbl, aref;
558 tree basetype = TREE_TYPE (instance);
560 if (TREE_CODE (basetype) == REFERENCE_TYPE)
561 basetype = TREE_TYPE (basetype);
563 if (instance == current_class_ref)
564 vtbl = build_vfield_ref (instance, basetype);
565 else
567 if (optimize)
569 /* Try to figure out what a reference refers to, and
570 access its virtual function table directly. */
571 tree ref = NULL_TREE;
573 if (TREE_CODE (instance) == INDIRECT_REF
574 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
575 ref = TREE_OPERAND (instance, 0);
576 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
577 ref = instance;
579 if (ref && TREE_CODE (ref) == VAR_DECL
580 && DECL_INITIAL (ref))
582 tree init = DECL_INITIAL (ref);
584 while (TREE_CODE (init) == NOP_EXPR
585 || TREE_CODE (init) == NON_LVALUE_EXPR)
586 init = TREE_OPERAND (init, 0);
587 if (TREE_CODE (init) == ADDR_EXPR)
589 init = TREE_OPERAND (init, 0);
590 if (IS_AGGR_TYPE (TREE_TYPE (init))
591 && (TREE_CODE (init) == PARM_DECL
592 || TREE_CODE (init) == VAR_DECL))
593 instance = init;
598 if (IS_AGGR_TYPE (TREE_TYPE (instance))
599 && (TREE_CODE (instance) == RESULT_DECL
600 || TREE_CODE (instance) == PARM_DECL
601 || TREE_CODE (instance) == VAR_DECL))
603 vtbl = TYPE_BINFO_VTABLE (basetype);
604 /* Knowing the dynamic type of INSTANCE we can easily obtain
605 the correct vtable entry. In the new ABI, we resolve
606 this back to be in terms of the primary vtable. */
607 if (TREE_CODE (vtbl) == PLUS_EXPR)
609 idx = fold (build (PLUS_EXPR,
610 TREE_TYPE (idx),
611 idx,
612 build (EXACT_DIV_EXPR,
613 TREE_TYPE (idx),
614 TREE_OPERAND (vtbl, 1),
615 TYPE_SIZE_UNIT (vtable_entry_type))));
616 vtbl = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
619 else
620 vtbl = build_vfield_ref (instance, basetype);
623 assemble_external (vtbl);
625 if (flag_vtable_gc)
626 build_vtable_entry_ref (basetype, idx);
628 aref = build_array_ref (vtbl, idx);
630 return aref;
633 /* Given an object INSTANCE, return an expression which yields the
634 virtual function corresponding to INDEX. There are many special
635 cases for INSTANCE which we take care of here, mainly to avoid
636 creating extra tree nodes when we don't have to. */
638 tree
639 build_vfn_ref (ptr_to_instptr, instance, idx)
640 tree *ptr_to_instptr, instance;
641 tree idx;
643 tree aref = build_vtbl_ref (instance, idx);
645 /* When using thunks, there is no extra delta, and we get the pfn
646 directly. */
647 if (flag_vtable_thunks)
648 return aref;
650 if (ptr_to_instptr)
652 /* Save the intermediate result in a SAVE_EXPR so we don't have to
653 compute each component of the virtual function pointer twice. */
654 if (TREE_CODE (aref) == INDIRECT_REF)
655 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
657 *ptr_to_instptr
658 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
659 *ptr_to_instptr,
660 cp_convert (ptrdiff_type_node,
661 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
664 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
667 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
668 for the given TYPE. */
670 static tree
671 get_vtable_name (type)
672 tree type;
674 return mangle_vtbl_for_type (type);
677 /* Return an IDENTIFIER_NODE for the name of the virtual table table
678 for TYPE. */
680 tree
681 get_vtt_name (type)
682 tree type;
684 return mangle_vtt_for_type (type);
687 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
688 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
689 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
691 static tree
692 build_vtable (class_type, name, vtable_type)
693 tree class_type;
694 tree name;
695 tree vtable_type;
697 tree decl;
699 decl = build_lang_decl (VAR_DECL, name, vtable_type);
700 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
701 now to avoid confusion in mangle_decl. */
702 SET_DECL_ASSEMBLER_NAME (decl, name);
703 DECL_CONTEXT (decl) = class_type;
704 DECL_ARTIFICIAL (decl) = 1;
705 TREE_STATIC (decl) = 1;
706 TREE_READONLY (decl) = 1;
707 DECL_VIRTUAL_P (decl) = 1;
708 import_export_vtable (decl, class_type, 0);
710 return decl;
713 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
714 or even complete. If this does not exist, create it. If COMPLETE is
715 non-zero, then complete the definition of it -- that will render it
716 impossible to actually build the vtable, but is useful to get at those
717 which are known to exist in the runtime. */
719 tree
720 get_vtable_decl (type, complete)
721 tree type;
722 int complete;
724 tree name = get_vtable_name (type);
725 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
727 if (decl)
729 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
730 && DECL_VIRTUAL_P (decl), 20000118);
731 return decl;
734 decl = build_vtable (type, name, void_type_node);
735 decl = pushdecl_top_level (decl);
736 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
737 20000517);
739 /* At one time the vtable info was grabbed 2 words at a time. This
740 fails on sparc unless you have 8-byte alignment. (tiemann) */
741 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
742 DECL_ALIGN (decl));
744 if (complete)
746 DECL_EXTERNAL (decl) = 1;
747 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
750 return decl;
753 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
754 BV_VCALL_INDEX for each entry is cleared. */
756 static tree
757 copy_virtuals (binfo)
758 tree binfo;
760 tree copies;
761 tree t;
763 copies = copy_list (BINFO_VIRTUALS (binfo));
764 for (t = copies; t; t = TREE_CHAIN (t))
766 BV_VCALL_INDEX (t) = NULL_TREE;
767 BV_USE_VCALL_INDEX_P (t) = 0;
770 return copies;
773 /* Build the primary virtual function table for TYPE. If BINFO is
774 non-NULL, build the vtable starting with the initial approximation
775 that it is the same as the one which is the head of the association
776 list. Returns a non-zero value if a new vtable is actually
777 created. */
779 static int
780 build_primary_vtable (binfo, type)
781 tree binfo, type;
783 tree decl;
784 tree virtuals;
786 decl = get_vtable_decl (type, /*complete=*/0);
788 if (binfo)
790 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
791 /* We have already created a vtable for this base, so there's
792 no need to do it again. */
793 return 0;
795 virtuals = copy_virtuals (binfo);
796 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
797 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
798 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
800 else
802 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
803 20000118);
804 virtuals = NULL_TREE;
807 #ifdef GATHER_STATISTICS
808 n_vtables += 1;
809 n_vtable_elems += list_length (virtuals);
810 #endif
812 /* Initialize the association list for this type, based
813 on our first approximation. */
814 TYPE_BINFO_VTABLE (type) = decl;
815 TYPE_BINFO_VIRTUALS (type) = virtuals;
816 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
817 return 1;
820 /* Give TYPE a new virtual function table which is initialized
821 with a skeleton-copy of its original initialization. The only
822 entry that changes is the `delta' entry, so we can really
823 share a lot of structure.
825 FOR_TYPE is the derived type which caused this table to
826 be needed.
828 BINFO is the type association which provided TYPE for FOR_TYPE.
830 The order in which vtables are built (by calling this function) for
831 an object must remain the same, otherwise a binary incompatibility
832 can result. */
834 static int
835 build_secondary_vtable (binfo, for_type)
836 tree binfo, for_type;
838 tree basetype;
839 tree orig_decl = BINFO_VTABLE (binfo);
840 tree name;
841 tree new_decl;
842 tree offset;
843 tree path = binfo;
844 char *buf;
845 const char *buf2;
846 char joiner = '_';
847 int i;
849 #ifdef JOINER
850 joiner = JOINER;
851 #endif
853 if (TREE_VIA_VIRTUAL (binfo))
854 my_friendly_assert (binfo == binfo_for_vbase (BINFO_TYPE (binfo),
855 current_class_type),
856 170);
858 if (BINFO_NEW_VTABLE_MARKED (binfo, current_class_type))
859 /* We already created a vtable for this base. There's no need to
860 do it again. */
861 return 0;
863 /* Remember that we've created a vtable for this BINFO, so that we
864 don't try to do so again. */
865 SET_BINFO_NEW_VTABLE_MARKED (binfo, current_class_type);
867 /* Make fresh virtual list, so we can smash it later. */
868 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
870 my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
871 offset = BINFO_OFFSET (binfo);
873 /* In the new ABI, secondary vtables are laid out as part of the
874 same structure as the primary vtable. */
875 if (merge_primary_and_secondary_vtables_p ())
877 BINFO_VTABLE (binfo) = NULL_TREE;
878 return 1;
881 /* Create the declaration for the secondary vtable. */
882 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
883 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
884 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
886 /* We know that the vtable that we are going to create doesn't exist
887 yet in the global namespace, and when we finish, it will be
888 pushed into the global namespace. In complex MI hierarchies, we
889 have to loop while the name we are thinking of adding is globally
890 defined, adding more name components to the vtable name as we
891 loop, until the name is unique. This is because in complex MI
892 cases, we might have the same base more than once. This means
893 that the order in which this function is called for vtables must
894 remain the same, otherwise binary compatibility can be
895 compromised. */
897 while (1)
899 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
900 + 1 + i);
901 char *new_buf2;
903 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
904 buf2);
905 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
906 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
907 name = get_identifier (buf);
909 /* If this name doesn't clash, then we can use it, otherwise
910 we add more to the name until it is unique. */
912 if (! IDENTIFIER_GLOBAL_VALUE (name))
913 break;
915 /* Set values for next loop through, if the name isn't unique. */
917 path = BINFO_INHERITANCE_CHAIN (path);
919 /* We better not run out of stuff to make it unique. */
920 my_friendly_assert (path != NULL_TREE, 368);
922 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
924 if (for_type == basetype)
926 /* If we run out of basetypes in the path, we have already
927 found created a vtable with that name before, we now
928 resort to tacking on _%d to distinguish them. */
929 int j = 2;
930 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
931 buf1 = (char *) alloca (i);
932 do {
933 sprintf (buf1, "%s%c%s%c%d",
934 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
935 buf2, joiner, j);
936 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
937 + strlen (buf1) + 1);
938 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
939 name = get_identifier (buf);
941 /* If this name doesn't clash, then we can use it,
942 otherwise we add something different to the name until
943 it is unique. */
944 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
946 /* Hey, they really like MI don't they? Increase the 3
947 above to 6, and the 999 to 999999. :-) */
948 my_friendly_assert (j <= 999, 369);
950 break;
953 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
954 new_buf2 = (char *) alloca (i);
955 sprintf (new_buf2, "%s%c%s",
956 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
957 buf2 = new_buf2;
960 new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
961 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
962 DECL_USER_ALIGN (new_decl) = DECL_USER_ALIGN (orig_decl);
963 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
965 #ifdef GATHER_STATISTICS
966 n_vtables += 1;
967 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
968 #endif
970 return 1;
973 /* Create a new vtable for BINFO which is the hierarchy dominated by
974 T. */
976 static int
977 make_new_vtable (t, binfo)
978 tree t;
979 tree binfo;
981 if (binfo == TYPE_BINFO (t))
982 /* In this case, it is *type*'s vtable we are modifying. We start
983 with the approximation that its vtable is that of the
984 immediate base class. */
985 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
986 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
987 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
989 else
990 /* This is our very own copy of `basetype' to play with. Later,
991 we will fill in all the virtual functions that override the
992 virtual functions in these base classes which are not defined
993 by the current type. */
994 return build_secondary_vtable (binfo, t);
997 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
998 (which is in the hierarchy dominated by T) list FNDECL as its
999 BV_FN. DELTA is the required constant adjustment from the `this'
1000 pointer where the vtable entry appears to the `this' required when
1001 the function is actually called. */
1003 static void
1004 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
1005 tree t;
1006 tree binfo;
1007 tree fndecl;
1008 tree delta;
1009 tree *virtuals;
1011 tree v;
1013 v = *virtuals;
1015 if (fndecl != BV_FN (v)
1016 || !tree_int_cst_equal (delta, BV_DELTA (v)))
1018 tree base_fndecl;
1020 /* We need a new vtable for BINFO. */
1021 if (make_new_vtable (t, binfo))
1023 /* If we really did make a new vtable, we also made a copy
1024 of the BINFO_VIRTUALS list. Now, we have to find the
1025 corresponding entry in that list. */
1026 *virtuals = BINFO_VIRTUALS (binfo);
1027 while (BV_FN (*virtuals) != BV_FN (v))
1028 *virtuals = TREE_CHAIN (*virtuals);
1029 v = *virtuals;
1032 base_fndecl = BV_FN (v);
1033 BV_DELTA (v) = delta;
1034 BV_VCALL_INDEX (v) = NULL_TREE;
1035 BV_FN (v) = fndecl;
1037 /* Now assign virtual dispatch information, if unset. We can
1038 dispatch this through any overridden base function.
1040 FIXME this can choose a secondary vtable if the primary is not
1041 also lexically first, leading to useless conversions.
1042 In the V3 ABI, there's no reason for DECL_VIRTUAL_CONTEXT to
1043 ever be different from DECL_CONTEXT. */
1044 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1046 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1047 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1052 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
1053 functions present in the vtable so far. */
1055 static void
1056 set_vindex (decl, vfuns_p)
1057 tree decl;
1058 int *vfuns_p;
1060 int vindex;
1062 vindex = (*vfuns_p)++;
1063 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
1066 /* Add a virtual function to all the appropriate vtables for the class
1067 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1068 allocate a new slot in our table. If it is error_mark_node, we
1069 know that no other function from another vtable is overridden by X.
1070 VFUNS_P keeps track of how many virtuals there are in our
1071 main vtable for the type, and we build upon the NEW_VIRTUALS list
1072 and return it. */
1074 static void
1075 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1076 vfuns_p, fndecl, t)
1077 tree *new_virtuals_p;
1078 tree *overridden_virtuals_p;
1079 int *vfuns_p;
1080 tree fndecl;
1081 tree t; /* Structure type. */
1083 tree new_virtual;
1085 /* If this function doesn't override anything from a base class, we
1086 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1087 override something, we keep it around and assign its DECL_VINDEX
1088 later, in modify_all_vtables. */
1089 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1090 /* We've already dealt with this function. */
1091 return;
1093 new_virtual = make_node (TREE_LIST);
1094 BV_FN (new_virtual) = fndecl;
1095 BV_DELTA (new_virtual) = integer_zero_node;
1097 if (DECL_VINDEX (fndecl) == error_mark_node)
1099 /* FNDECL is a new virtual function; it doesn't override any
1100 virtual function in a base class. */
1102 /* We remember that this was the base sub-object for rtti. */
1103 CLASSTYPE_RTTI (t) = t;
1105 /* Now assign virtual dispatch information. */
1106 set_vindex (fndecl, vfuns_p);
1107 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1109 /* Save the state we've computed on the NEW_VIRTUALS list. */
1110 TREE_CHAIN (new_virtual) = *new_virtuals_p;
1111 *new_virtuals_p = new_virtual;
1113 else
1115 /* FNDECL overrides a function from a base class. */
1116 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1117 *overridden_virtuals_p = new_virtual;
1121 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
1122 the method after the class has already been defined because a
1123 declaration for it was seen. (Even though that is erroneous, we
1124 add the method for improved error recovery.) */
1126 void
1127 add_method (type, method, error_p)
1128 tree type;
1129 tree method;
1130 int error_p;
1132 int using = (DECL_CONTEXT (method) != type);
1133 int len;
1134 int slot;
1135 tree method_vec;
1137 if (!CLASSTYPE_METHOD_VEC (type))
1138 /* Make a new method vector. We start with 8 entries. We must
1139 allocate at least two (for constructors and destructors), and
1140 we're going to end up with an assignment operator at some point
1141 as well.
1143 We could use a TREE_LIST for now, and convert it to a TREE_VEC
1144 in finish_struct, but we would probably waste more memory
1145 making the links in the list than we would by over-allocating
1146 the size of the vector here. Furthermore, we would complicate
1147 all the code that expects this to be a vector. */
1148 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1150 method_vec = CLASSTYPE_METHOD_VEC (type);
1151 len = TREE_VEC_LENGTH (method_vec);
1153 /* Constructors and destructors go in special slots. */
1154 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1155 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1156 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1157 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1158 else
1160 /* See if we already have an entry with this name. */
1161 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
1162 if (!TREE_VEC_ELT (method_vec, slot)
1163 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1164 slot)))
1165 == DECL_NAME (method)))
1166 break;
1168 if (slot == len)
1170 /* We need a bigger method vector. */
1171 int new_len;
1172 tree new_vec;
1174 /* In the non-error case, we are processing a class
1175 definition. Double the size of the vector to give room
1176 for new methods. */
1177 if (!error_p)
1178 new_len = 2 * len;
1179 /* In the error case, the vector is already complete. We
1180 don't expect many errors, and the rest of the front-end
1181 will get confused if there are empty slots in the vector. */
1182 else
1183 new_len = len + 1;
1185 new_vec = make_tree_vec (new_len);
1186 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
1187 len * sizeof (tree));
1188 len = new_len;
1189 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1192 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1194 /* Type conversion operators have to come before ordinary
1195 methods; add_conversions depends on this to speed up
1196 looking for conversion operators. So, if necessary, we
1197 slide some of the vector elements up. In theory, this
1198 makes this algorithm O(N^2) but we don't expect many
1199 conversion operators. */
1200 for (slot = 2; slot < len; ++slot)
1202 tree fn = TREE_VEC_ELT (method_vec, slot);
1204 if (!fn)
1205 /* There are no more entries in the vector, so we
1206 can insert the new conversion operator here. */
1207 break;
1209 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1210 /* We can insert the new function right at the
1211 SLOTth position. */
1212 break;
1215 if (!TREE_VEC_ELT (method_vec, slot))
1216 /* There is nothing in the Ith slot, so we can avoid
1217 moving anything. */
1219 else
1221 /* We know the last slot in the vector is empty
1222 because we know that at this point there's room
1223 for a new function. */
1224 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
1225 &TREE_VEC_ELT (method_vec, slot),
1226 (len - slot - 1) * sizeof (tree));
1227 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1232 if (template_class_depth (type))
1233 /* TYPE is a template class. Don't issue any errors now; wait
1234 until instantiation time to complain. */
1236 else
1238 tree fns;
1240 /* Check to see if we've already got this method. */
1241 for (fns = TREE_VEC_ELT (method_vec, slot);
1242 fns;
1243 fns = OVL_NEXT (fns))
1245 tree fn = OVL_CURRENT (fns);
1247 if (TREE_CODE (fn) != TREE_CODE (method))
1248 continue;
1250 if (TREE_CODE (method) != TEMPLATE_DECL)
1252 /* [over.load] Member function declarations with the
1253 same name and the same parameter types cannot be
1254 overloaded if any of them is a static member
1255 function declaration. */
1256 if ((DECL_STATIC_FUNCTION_P (fn)
1257 != DECL_STATIC_FUNCTION_P (method))
1258 || using)
1260 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1261 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1263 if (! DECL_STATIC_FUNCTION_P (fn))
1264 parms1 = TREE_CHAIN (parms1);
1265 if (! DECL_STATIC_FUNCTION_P (method))
1266 parms2 = TREE_CHAIN (parms2);
1268 if (compparms (parms1, parms2))
1270 if (using)
1271 /* Defer to the local function. */
1272 return;
1273 else
1274 cp_error ("`%#D' and `%#D' cannot be overloaded",
1275 fn, method);
1280 if (!decls_match (fn, method))
1281 continue;
1283 /* There has already been a declaration of this method
1284 or member template. */
1285 cp_error_at ("`%D' has already been declared in `%T'",
1286 method, type);
1288 /* We don't call duplicate_decls here to merge the
1289 declarations because that will confuse things if the
1290 methods have inline definitions. In particular, we
1291 will crash while processing the definitions. */
1292 return;
1296 /* Actually insert the new method. */
1297 TREE_VEC_ELT (method_vec, slot)
1298 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1300 /* Add the new binding. */
1301 if (!DECL_CONSTRUCTOR_P (method)
1302 && !DECL_DESTRUCTOR_P (method))
1303 push_class_level_binding (DECL_NAME (method),
1304 TREE_VEC_ELT (method_vec, slot));
1307 /* Subroutines of finish_struct. */
1309 /* Look through the list of fields for this struct, deleting
1310 duplicates as we go. This must be recursive to handle
1311 anonymous unions.
1313 FIELD is the field which may not appear anywhere in FIELDS.
1314 FIELD_PTR, if non-null, is the starting point at which
1315 chained deletions may take place.
1316 The value returned is the first acceptable entry found
1317 in FIELDS.
1319 Note that anonymous fields which are not of UNION_TYPE are
1320 not duplicates, they are just anonymous fields. This happens
1321 when we have unnamed bitfields, for example. */
1323 static tree
1324 delete_duplicate_fields_1 (field, fields)
1325 tree field, fields;
1327 tree x;
1328 tree prev = 0;
1329 if (DECL_NAME (field) == 0)
1331 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1332 return fields;
1334 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1335 fields = delete_duplicate_fields_1 (x, fields);
1336 return fields;
1338 else
1340 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1342 if (DECL_NAME (x) == 0)
1344 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1345 continue;
1346 TYPE_FIELDS (TREE_TYPE (x))
1347 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1348 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1350 if (prev == 0)
1351 fields = TREE_CHAIN (fields);
1352 else
1353 TREE_CHAIN (prev) = TREE_CHAIN (x);
1356 else if (TREE_CODE (field) == USING_DECL)
1357 /* A using declaration may is allowed to appear more than
1358 once. We'll prune these from the field list later, and
1359 handle_using_decl will complain about invalid multiple
1360 uses. */
1362 else if (DECL_NAME (field) == DECL_NAME (x))
1364 if (TREE_CODE (field) == CONST_DECL
1365 && TREE_CODE (x) == CONST_DECL)
1366 cp_error_at ("duplicate enum value `%D'", x);
1367 else if (TREE_CODE (field) == CONST_DECL
1368 || TREE_CODE (x) == CONST_DECL)
1369 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1371 else if (DECL_DECLARES_TYPE_P (field)
1372 && DECL_DECLARES_TYPE_P (x))
1374 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1375 continue;
1376 cp_error_at ("duplicate nested type `%D'", x);
1378 else if (DECL_DECLARES_TYPE_P (field)
1379 || DECL_DECLARES_TYPE_P (x))
1381 /* Hide tag decls. */
1382 if ((TREE_CODE (field) == TYPE_DECL
1383 && DECL_ARTIFICIAL (field))
1384 || (TREE_CODE (x) == TYPE_DECL
1385 && DECL_ARTIFICIAL (x)))
1386 continue;
1387 cp_error_at ("duplicate field `%D' (as type and non-type)",
1390 else
1391 cp_error_at ("duplicate member `%D'", x);
1392 if (prev == 0)
1393 fields = TREE_CHAIN (fields);
1394 else
1395 TREE_CHAIN (prev) = TREE_CHAIN (x);
1399 return fields;
1402 static void
1403 delete_duplicate_fields (fields)
1404 tree fields;
1406 tree x;
1407 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1408 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1411 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1412 legit, otherwise return 0. */
1414 static int
1415 alter_access (t, fdecl, access)
1416 tree t;
1417 tree fdecl;
1418 tree access;
1420 tree elem;
1422 if (!DECL_LANG_SPECIFIC (fdecl))
1423 retrofit_lang_decl (fdecl);
1425 if (DECL_DISCRIMINATOR_P (fdecl))
1426 abort ();
1428 elem = purpose_member (t, DECL_ACCESS (fdecl));
1429 if (elem)
1431 if (TREE_VALUE (elem) != access)
1433 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1434 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1435 else
1436 error ("conflicting access specifications for field `%s', ignored",
1437 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1439 else
1441 /* They're changing the access to the same thing they changed
1442 it to before. That's OK. */
1446 else
1448 enforce_access (t, fdecl);
1449 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1450 return 1;
1452 return 0;
1455 /* Process the USING_DECL, which is a member of T. */
1457 static void
1458 handle_using_decl (using_decl, t)
1459 tree using_decl;
1460 tree t;
1462 tree ctype = DECL_INITIAL (using_decl);
1463 tree name = DECL_NAME (using_decl);
1464 tree access
1465 = TREE_PRIVATE (using_decl) ? access_private_node
1466 : TREE_PROTECTED (using_decl) ? access_protected_node
1467 : access_public_node;
1468 tree fdecl, binfo;
1469 tree flist = NULL_TREE;
1470 tree old_value;
1472 binfo = binfo_or_else (ctype, t);
1473 if (! binfo)
1474 return;
1476 if (name == constructor_name (ctype)
1477 || name == constructor_name_full (ctype))
1479 cp_error_at ("`%D' names constructor", using_decl);
1480 return;
1482 if (name == constructor_name (t)
1483 || name == constructor_name_full (t))
1485 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1486 return;
1489 fdecl = lookup_member (binfo, name, 0, 0);
1491 if (!fdecl)
1493 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1494 return;
1497 if (BASELINK_P (fdecl))
1498 /* Ignore base type this came from. */
1499 fdecl = TREE_VALUE (fdecl);
1501 old_value = IDENTIFIER_CLASS_VALUE (name);
1502 if (old_value)
1504 if (is_overloaded_fn (old_value))
1505 old_value = OVL_CURRENT (old_value);
1507 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1508 /* OK */;
1509 else
1510 old_value = NULL_TREE;
1513 if (is_overloaded_fn (fdecl))
1514 flist = fdecl;
1516 if (! old_value)
1518 else if (is_overloaded_fn (old_value))
1520 if (flist)
1521 /* It's OK to use functions from a base when there are functions with
1522 the same name already present in the current class. */;
1523 else
1525 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1526 cp_error_at (" because of local method `%#D' with same name",
1527 OVL_CURRENT (old_value));
1528 return;
1531 else if (!DECL_ARTIFICIAL (old_value))
1533 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1534 cp_error_at (" because of local member `%#D' with same name", old_value);
1535 return;
1538 /* Make type T see field decl FDECL with access ACCESS.*/
1539 if (flist)
1540 for (; flist; flist = OVL_NEXT (flist))
1542 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1543 alter_access (t, OVL_CURRENT (flist), access);
1545 else
1546 alter_access (t, fdecl, access);
1549 /* Run through the base clases of T, updating
1550 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1551 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1552 the bases. */
1554 static void
1555 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1556 no_const_asn_ref_p)
1557 tree t;
1558 int *cant_have_default_ctor_p;
1559 int *cant_have_const_ctor_p;
1560 int *no_const_asn_ref_p;
1562 int n_baseclasses;
1563 int i;
1564 int seen_non_virtual_nearly_empty_base_p;
1565 tree binfos;
1567 binfos = TYPE_BINFO_BASETYPES (t);
1568 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1569 seen_non_virtual_nearly_empty_base_p = 0;
1571 /* An aggregate cannot have baseclasses. */
1572 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1574 for (i = 0; i < n_baseclasses; ++i)
1576 tree base_binfo;
1577 tree basetype;
1579 /* Figure out what base we're looking at. */
1580 base_binfo = TREE_VEC_ELT (binfos, i);
1581 basetype = TREE_TYPE (base_binfo);
1583 /* If the type of basetype is incomplete, then we already
1584 complained about that fact (and we should have fixed it up as
1585 well). */
1586 if (!COMPLETE_TYPE_P (basetype))
1588 int j;
1589 /* The base type is of incomplete type. It is
1590 probably best to pretend that it does not
1591 exist. */
1592 if (i == n_baseclasses-1)
1593 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1594 TREE_VEC_LENGTH (binfos) -= 1;
1595 n_baseclasses -= 1;
1596 for (j = i; j+1 < n_baseclasses; j++)
1597 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1598 continue;
1601 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1602 here because the case of virtual functions but non-virtual
1603 dtor is handled in finish_struct_1. */
1604 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1605 && TYPE_HAS_DESTRUCTOR (basetype))
1606 cp_warning ("base class `%#T' has a non-virtual destructor",
1607 basetype);
1609 /* If the base class doesn't have copy constructors or
1610 assignment operators that take const references, then the
1611 derived class cannot have such a member automatically
1612 generated. */
1613 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1614 *cant_have_const_ctor_p = 1;
1615 if (TYPE_HAS_ASSIGN_REF (basetype)
1616 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1617 *no_const_asn_ref_p = 1;
1618 /* Similarly, if the base class doesn't have a default
1619 constructor, then the derived class won't have an
1620 automatically generated default constructor. */
1621 if (TYPE_HAS_CONSTRUCTOR (basetype)
1622 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1624 *cant_have_default_ctor_p = 1;
1625 if (! TYPE_HAS_CONSTRUCTOR (t))
1626 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1627 basetype);
1630 if (TREE_VIA_VIRTUAL (base_binfo))
1631 /* A virtual base does not effect nearly emptiness. */
1633 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1635 if (seen_non_virtual_nearly_empty_base_p)
1636 /* And if there is more than one nearly empty base, then the
1637 derived class is not nearly empty either. */
1638 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1639 else
1640 /* Remember we've seen one. */
1641 seen_non_virtual_nearly_empty_base_p = 1;
1643 else if (!is_empty_class (basetype))
1644 /* If the base class is not empty or nearly empty, then this
1645 class cannot be nearly empty. */
1646 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1648 /* A lot of properties from the bases also apply to the derived
1649 class. */
1650 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1651 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1652 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1653 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1654 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1655 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1656 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1657 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1658 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1659 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1663 /* Binfo FROM is within a virtual heirarchy which is being reseated to
1664 TO. Move primary information from FROM to TO, and recursively traverse
1665 into FROM's bases. The heirarchy is dominated by TYPE. MAPPINGS is an
1666 assoc list of binfos that have already been reseated. */
1668 static void
1669 force_canonical_binfo_r (to, from, type, mappings)
1670 tree to;
1671 tree from;
1672 tree type;
1673 tree mappings;
1675 int i, n_baseclasses = BINFO_N_BASETYPES (from);
1677 BINFO_INDIRECT_PRIMARY_P (to)
1678 = BINFO_INDIRECT_PRIMARY_P (from);
1679 BINFO_INDIRECT_PRIMARY_P (from) = 0;
1680 BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1681 BINFO_UNSHARED_MARKED (from) = 0;
1682 BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1683 BINFO_LOST_PRIMARY_P (from) = 0;
1684 if (BINFO_PRIMARY_P (from))
1686 tree primary = BINFO_PRIMARY_BASE_OF (from);
1687 tree assoc;
1689 /* We might have just moved the primary base too, see if it's on our
1690 mappings. */
1691 assoc = purpose_member (primary, mappings);
1692 if (assoc)
1693 primary = TREE_VALUE (assoc);
1694 BINFO_PRIMARY_BASE_OF (to) = primary;
1695 BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1697 my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1698 20010104);
1699 mappings = tree_cons (from, to, mappings);
1700 for (i = 0; i != n_baseclasses; i++)
1702 tree from_binfo = BINFO_BASETYPE (from, i);
1703 tree to_binfo = BINFO_BASETYPE (to, i);
1705 if (TREE_VIA_VIRTUAL (from_binfo))
1707 if (BINFO_PRIMARY_P (from_binfo) &&
1708 purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1709 /* This base is a primary of some binfo we have already
1710 reseated. We must reseat this one too. */
1711 force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1713 else
1714 force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1718 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1719 make TO the canonical binfo, within the heirarchy dominated by TYPE.
1720 MAPPINGS is an assoc list of binfos that have already been reseated.
1721 Adjust any non-virtual bases within FROM, and also move any virtual bases
1722 which are canonical. This complication arises because selecting primary
1723 bases walks in inheritance graph order, but we don't share binfos for
1724 virtual bases, hence we can fill in the primaries for a virtual base,
1725 and then discover that a later base requires the virtual as its
1726 primary. */
1728 static void
1729 force_canonical_binfo (to, from, type, mappings)
1730 tree to;
1731 tree from;
1732 tree type;
1733 tree mappings;
1735 tree assoc = purpose_member (BINFO_TYPE (to),
1736 CLASSTYPE_VBASECLASSES (type));
1737 TREE_VALUE (assoc) = to;
1738 force_canonical_binfo_r (to, from, type, mappings);
1741 /* Make BASE_BINFO the primary virtual base of BINFO within the hierarchy
1742 dominated by TYPE. Returns BASE_BINFO, if it can be made so, NULL
1743 otherwise (because something else has already made it primary). */
1745 static tree
1746 mark_primary_virtual_base (binfo, base_binfo, type)
1747 tree binfo;
1748 tree base_binfo;
1749 tree type;
1751 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1752 tree delta;
1754 if (BINFO_PRIMARY_P (shared_binfo))
1756 /* It's already allocated in the hierarchy. BINFO won't have a
1757 primary base in this hierachy, even though the complete object
1758 BINFO is for, would do. */
1759 BINFO_LOST_PRIMARY_P (binfo) = 1;
1761 return NULL_TREE;
1764 /* We need to make sure that the assoc list
1765 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1766 primary BINFO for the virtual base, as this is the one
1767 that'll really exist. */
1768 if (base_binfo != shared_binfo)
1769 force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1771 delta = size_diffop (BINFO_OFFSET (binfo), BINFO_OFFSET (base_binfo));
1772 if (!integer_zerop (delta))
1774 propagate_binfo_offsets (base_binfo, delta, type);
1775 BINFO_OFFSET (base_binfo) = BINFO_OFFSET (binfo);
1777 return base_binfo;
1780 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1781 base, then BINFO has no primary base in this graph. Called from
1782 mark_primary_bases. DATA is the most derived type. */
1784 static tree dfs_unshared_virtual_bases (binfo, data)
1785 tree binfo;
1786 void *data;
1788 tree t = (tree) data;
1790 if (!BINFO_UNSHARED_MARKED (binfo)
1791 && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1793 /* This morally virtual base has a primary base when it
1794 is a complete object. We need to locate the shared instance
1795 of this binfo in the type dominated by T. We duplicate the
1796 primary base information from there to here. */
1797 tree vbase;
1798 tree unshared_base;
1800 for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1801 vbase = BINFO_INHERITANCE_CHAIN (vbase))
1802 continue;
1803 unshared_base = get_original_base (binfo,
1804 binfo_for_vbase (BINFO_TYPE (vbase),
1805 t));
1806 my_friendly_assert (unshared_base != binfo, 20010612);
1807 BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1808 if (!BINFO_LOST_PRIMARY_P (binfo))
1809 BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1812 if (binfo != TYPE_BINFO (t))
1813 /* The vtable fields will have been copied when duplicating the
1814 base binfos. That information is bogus, make sure we don't try
1815 and use it. */
1816 BINFO_VTABLE (binfo) = NULL_TREE;
1818 BINFO_UNSHARED_MARKED (binfo) = 0;
1819 return NULL;
1822 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1823 dominated by TYPE that are primary bases. */
1825 static void
1826 mark_primary_bases (type)
1827 tree type;
1829 tree binfo;
1831 /* Walk the bases in inheritance graph order. */
1832 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1834 tree base_binfo;
1836 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1837 /* Not a dynamic base. */
1838 continue;
1840 base_binfo = get_primary_binfo (binfo);
1842 if (TREE_VIA_VIRTUAL (base_binfo))
1843 base_binfo = mark_primary_virtual_base (binfo, base_binfo, type);
1845 if (base_binfo)
1846 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1848 BINFO_UNSHARED_MARKED (binfo) = 1;
1850 /* There could remain unshared morally virtual bases which were not visited
1851 in the inheritance graph walk. These bases will have lost their
1852 virtual primary base (should they have one). We must now find them. */
1853 dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1856 /* Make the BINFO the primary base of T. */
1858 static void
1859 set_primary_base (t, binfo, vfuns_p)
1860 tree t;
1861 tree binfo;
1862 int *vfuns_p;
1864 tree basetype;
1866 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1867 basetype = BINFO_TYPE (binfo);
1868 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1869 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1870 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1871 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1872 *vfuns_p = CLASSTYPE_VSIZE (basetype);
1875 /* Determine the primary class for T. */
1877 static void
1878 determine_primary_base (t, vfuns_p)
1879 tree t;
1880 int *vfuns_p;
1882 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1883 tree vbases;
1884 tree type_binfo;
1886 /* If there are no baseclasses, there is certainly no primary base. */
1887 if (n_baseclasses == 0)
1888 return;
1890 type_binfo = TYPE_BINFO (t);
1892 for (i = 0; i < n_baseclasses; i++)
1894 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1895 tree basetype = BINFO_TYPE (base_binfo);
1897 if (TYPE_CONTAINS_VPTR_P (basetype))
1899 /* Even a virtual baseclass can contain our RTTI
1900 information. But, we prefer a non-virtual polymorphic
1901 baseclass. */
1902 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1903 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1905 /* A virtual baseclass can't be the primary base under the
1906 old ABI. And under the new ABI we still prefer a
1907 non-virtual base. */
1908 if (TREE_VIA_VIRTUAL (base_binfo))
1909 continue;
1911 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1913 set_primary_base (t, base_binfo, vfuns_p);
1914 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1916 else
1918 tree vfields;
1920 /* Only add unique vfields, and flatten them out as we go. */
1921 for (vfields = CLASSTYPE_VFIELDS (basetype);
1922 vfields;
1923 vfields = TREE_CHAIN (vfields))
1924 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1925 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1926 CLASSTYPE_VFIELDS (t)
1927 = tree_cons (base_binfo,
1928 VF_BASETYPE_VALUE (vfields),
1929 CLASSTYPE_VFIELDS (t));
1934 if (!TYPE_VFIELD (t))
1935 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1937 /* Find the indirect primary bases - those virtual bases which are primary
1938 bases of something else in this hierarchy. */
1939 for (vbases = CLASSTYPE_VBASECLASSES (t);
1940 vbases;
1941 vbases = TREE_CHAIN (vbases))
1943 tree vbase_binfo = TREE_VALUE (vbases);
1945 /* See if this virtual base is an indirect primary base. To be so,
1946 it must be a primary base within the hierarchy of one of our
1947 direct bases. */
1948 for (i = 0; i < n_baseclasses; ++i)
1950 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1951 tree v;
1953 for (v = CLASSTYPE_VBASECLASSES (basetype);
1955 v = TREE_CHAIN (v))
1957 tree base_vbase = TREE_VALUE (v);
1959 if (BINFO_PRIMARY_P (base_vbase)
1960 && same_type_p (BINFO_TYPE (base_vbase),
1961 BINFO_TYPE (vbase_binfo)))
1963 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1964 break;
1968 /* If we've discovered that this virtual base is an indirect
1969 primary base, then we can move on to the next virtual
1970 base. */
1971 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1972 break;
1976 /* The new ABI allows for the use of a "nearly-empty" virtual base
1977 class as the primary base class if no non-virtual polymorphic
1978 base can be found. */
1979 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1981 /* If not NULL, this is the best primary base candidate we have
1982 found so far. */
1983 tree candidate = NULL_TREE;
1984 tree base_binfo;
1986 /* Loop over the baseclasses. */
1987 for (base_binfo = TYPE_BINFO (t);
1988 base_binfo;
1989 base_binfo = TREE_CHAIN (base_binfo))
1991 tree basetype = BINFO_TYPE (base_binfo);
1993 if (TREE_VIA_VIRTUAL (base_binfo)
1994 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1996 /* If this is not an indirect primary base, then it's
1997 definitely our primary base. */
1998 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
2000 candidate = base_binfo;
2001 break;
2004 /* If this is an indirect primary base, it still could be
2005 our primary base -- unless we later find there's another
2006 nearly-empty virtual base that isn't an indirect
2007 primary base. */
2008 if (!candidate)
2009 candidate = base_binfo;
2013 /* If we've got a primary base, use it. */
2014 if (candidate)
2016 set_primary_base (t, candidate, vfuns_p);
2017 CLASSTYPE_VFIELDS (t)
2018 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
2022 /* Mark the primary base classes at this point. */
2023 mark_primary_bases (t);
2026 /* Set memoizing fields and bits of T (and its variants) for later
2027 use. */
2029 static void
2030 finish_struct_bits (t)
2031 tree t;
2033 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2035 /* Fix up variants (if any). */
2036 tree variants = TYPE_NEXT_VARIANT (t);
2037 while (variants)
2039 /* These fields are in the _TYPE part of the node, not in
2040 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2041 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
2042 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
2043 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2044 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2045 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2047 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
2048 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
2049 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2050 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2051 /* Copy whatever these are holding today. */
2052 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2053 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
2054 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2055 TYPE_SIZE (variants) = TYPE_SIZE (t);
2056 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
2057 variants = TYPE_NEXT_VARIANT (variants);
2060 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
2061 /* For a class w/o baseclasses, `finish_struct' has set
2062 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2063 definition). Similarly for a class whose base classes do not
2064 have vtables. When neither of these is true, we might have
2065 removed abstract virtuals (by providing a definition), added
2066 some (by declaring new ones), or redeclared ones from a base
2067 class. We need to recalculate what's really an abstract virtual
2068 at this point (by looking in the vtables). */
2069 get_pure_virtuals (t);
2071 if (n_baseclasses)
2073 /* Notice whether this class has type conversion functions defined. */
2074 tree binfo = TYPE_BINFO (t);
2075 tree binfos = BINFO_BASETYPES (binfo);
2076 tree basetype;
2078 for (i = n_baseclasses-1; i >= 0; i--)
2080 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2082 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
2086 /* If this type has a copy constructor, force its mode to be BLKmode, and
2087 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2088 be passed by invisible reference and prevent it from being returned in
2089 a register.
2091 Also do this if the class has BLKmode but can still be returned in
2092 registers, since function_cannot_inline_p won't let us inline
2093 functions returning such a type. This affects the HP-PA. */
2094 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
2095 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
2096 && CLASSTYPE_NON_AGGREGATE (t)))
2098 tree variants;
2099 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2100 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2102 TYPE_MODE (variants) = BLKmode;
2103 TREE_ADDRESSABLE (variants) = 1;
2108 /* Issue warnings about T having private constructors, but no friends,
2109 and so forth.
2111 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2112 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2113 non-private static member functions. */
2115 static void
2116 maybe_warn_about_overly_private_class (t)
2117 tree t;
2119 int has_member_fn = 0;
2120 int has_nonprivate_method = 0;
2121 tree fn;
2123 if (!warn_ctor_dtor_privacy
2124 /* If the class has friends, those entities might create and
2125 access instances, so we should not warn. */
2126 || (CLASSTYPE_FRIEND_CLASSES (t)
2127 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2128 /* We will have warned when the template was declared; there's
2129 no need to warn on every instantiation. */
2130 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2131 /* There's no reason to even consider warning about this
2132 class. */
2133 return;
2135 /* We only issue one warning, if more than one applies, because
2136 otherwise, on code like:
2138 class A {
2139 // Oops - forgot `public:'
2140 A();
2141 A(const A&);
2142 ~A();
2145 we warn several times about essentially the same problem. */
2147 /* Check to see if all (non-constructor, non-destructor) member
2148 functions are private. (Since there are no friends or
2149 non-private statics, we can't ever call any of the private member
2150 functions.) */
2151 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2152 /* We're not interested in compiler-generated methods; they don't
2153 provide any way to call private members. */
2154 if (!DECL_ARTIFICIAL (fn))
2156 if (!TREE_PRIVATE (fn))
2158 if (DECL_STATIC_FUNCTION_P (fn))
2159 /* A non-private static member function is just like a
2160 friend; it can create and invoke private member
2161 functions, and be accessed without a class
2162 instance. */
2163 return;
2165 has_nonprivate_method = 1;
2166 break;
2168 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2169 has_member_fn = 1;
2172 if (!has_nonprivate_method && has_member_fn)
2174 /* There are no non-private methods, and there's at least one
2175 private member function that isn't a constructor or
2176 destructor. (If all the private members are
2177 constructors/destructors we want to use the code below that
2178 issues error messages specifically referring to
2179 constructors/destructors.) */
2180 int i;
2181 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2182 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2183 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2184 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2186 has_nonprivate_method = 1;
2187 break;
2189 if (!has_nonprivate_method)
2191 cp_warning ("all member functions in class `%T' are private", t);
2192 return;
2196 /* Even if some of the member functions are non-private, the class
2197 won't be useful for much if all the constructors or destructors
2198 are private: such an object can never be created or destroyed. */
2199 if (TYPE_HAS_DESTRUCTOR (t))
2201 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2203 if (TREE_PRIVATE (dtor))
2205 cp_warning ("`%#T' only defines a private destructor and has no friends",
2207 return;
2211 if (TYPE_HAS_CONSTRUCTOR (t))
2213 int nonprivate_ctor = 0;
2215 /* If a non-template class does not define a copy
2216 constructor, one is defined for it, enabling it to avoid
2217 this warning. For a template class, this does not
2218 happen, and so we would normally get a warning on:
2220 template <class T> class C { private: C(); };
2222 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2223 complete non-template or fully instantiated classes have this
2224 flag set. */
2225 if (!TYPE_HAS_INIT_REF (t))
2226 nonprivate_ctor = 1;
2227 else
2228 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2230 fn = OVL_NEXT (fn))
2232 tree ctor = OVL_CURRENT (fn);
2233 /* Ideally, we wouldn't count copy constructors (or, in
2234 fact, any constructor that takes an argument of the
2235 class type as a parameter) because such things cannot
2236 be used to construct an instance of the class unless
2237 you already have one. But, for now at least, we're
2238 more generous. */
2239 if (! TREE_PRIVATE (ctor))
2241 nonprivate_ctor = 1;
2242 break;
2246 if (nonprivate_ctor == 0)
2248 cp_warning ("`%#T' only defines private constructors and has no friends",
2250 return;
2255 /* Function to help qsort sort FIELD_DECLs by name order. */
2257 static int
2258 field_decl_cmp (x, y)
2259 const tree *x, *y;
2261 if (DECL_NAME (*x) == DECL_NAME (*y))
2262 /* A nontype is "greater" than a type. */
2263 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2264 if (DECL_NAME (*x) == NULL_TREE)
2265 return -1;
2266 if (DECL_NAME (*y) == NULL_TREE)
2267 return 1;
2268 if (DECL_NAME (*x) < DECL_NAME (*y))
2269 return -1;
2270 return 1;
2273 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2275 static int
2276 method_name_cmp (m1, m2)
2277 const tree *m1, *m2;
2279 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2280 return 0;
2281 if (*m1 == NULL_TREE)
2282 return -1;
2283 if (*m2 == NULL_TREE)
2284 return 1;
2285 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2286 return -1;
2287 return 1;
2290 /* Warn about duplicate methods in fn_fields. Also compact method
2291 lists so that lookup can be made faster.
2293 Data Structure: List of method lists. The outer list is a
2294 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2295 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2296 links the entire list of methods for TYPE_METHODS. Friends are
2297 chained in the same way as member functions (? TREE_CHAIN or
2298 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2299 list. That allows them to be quickly deleted, and requires no
2300 extra storage.
2302 Sort methods that are not special (i.e., constructors, destructors,
2303 and type conversion operators) so that we can find them faster in
2304 search. */
2306 static void
2307 finish_struct_methods (t)
2308 tree t;
2310 tree fn_fields;
2311 tree method_vec;
2312 int slot, len;
2314 if (!TYPE_METHODS (t))
2316 /* Clear these for safety; perhaps some parsing error could set
2317 these incorrectly. */
2318 TYPE_HAS_CONSTRUCTOR (t) = 0;
2319 TYPE_HAS_DESTRUCTOR (t) = 0;
2320 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2321 return;
2324 method_vec = CLASSTYPE_METHOD_VEC (t);
2325 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2326 len = TREE_VEC_LENGTH (method_vec);
2328 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2329 and the next few with type conversion operators (if any). */
2330 for (fn_fields = TYPE_METHODS (t); fn_fields;
2331 fn_fields = TREE_CHAIN (fn_fields))
2332 /* Clear out this flag. */
2333 DECL_IN_AGGR_P (fn_fields) = 0;
2335 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2336 /* We thought there was a destructor, but there wasn't. Some
2337 parse errors cause this anomalous situation. */
2338 TYPE_HAS_DESTRUCTOR (t) = 0;
2340 /* Issue warnings about private constructors and such. If there are
2341 no methods, then some public defaults are generated. */
2342 maybe_warn_about_overly_private_class (t);
2344 /* Now sort the methods. */
2345 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2346 len--;
2347 TREE_VEC_LENGTH (method_vec) = len;
2349 /* The type conversion ops have to live at the front of the vec, so we
2350 can't sort them. */
2351 for (slot = 2; slot < len; ++slot)
2353 tree fn = TREE_VEC_ELT (method_vec, slot);
2355 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2356 break;
2358 if (len - slot > 1)
2359 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2360 (int (*)(const void *, const void *))method_name_cmp);
2363 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2365 void
2366 duplicate_tag_error (t)
2367 tree t;
2369 cp_error ("redefinition of `%#T'", t);
2370 cp_error_at ("previous definition of `%#T'", t);
2372 /* Pretend we haven't defined this type. */
2374 /* All of the component_decl's were TREE_CHAINed together in the parser.
2375 finish_struct_methods walks these chains and assembles all methods with
2376 the same base name into DECL_CHAINs. Now we don't need the parser chains
2377 anymore, so we unravel them. */
2379 /* This used to be in finish_struct, but it turns out that the
2380 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2381 things... */
2382 if (CLASSTYPE_METHOD_VEC (t))
2384 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2385 int i, len = TREE_VEC_LENGTH (method_vec);
2386 for (i = 0; i < len; i++)
2388 tree unchain = TREE_VEC_ELT (method_vec, i);
2389 while (unchain != NULL_TREE)
2391 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2392 unchain = OVL_NEXT (unchain);
2397 if (TYPE_LANG_SPECIFIC (t))
2399 tree binfo = TYPE_BINFO (t);
2400 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2401 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2402 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2403 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2405 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2406 BINFO_BASETYPES(binfo) = NULL_TREE;
2408 TYPE_BINFO (t) = binfo;
2409 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2410 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2411 TYPE_REDEFINED (t) = 1;
2412 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2413 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2415 TYPE_SIZE (t) = NULL_TREE;
2416 TYPE_MODE (t) = VOIDmode;
2417 TYPE_FIELDS (t) = NULL_TREE;
2418 TYPE_METHODS (t) = NULL_TREE;
2419 TYPE_VFIELD (t) = NULL_TREE;
2420 TYPE_CONTEXT (t) = NULL_TREE;
2421 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2423 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2424 TYPE_LANG_FLAG_0 (t) = 0;
2425 TYPE_LANG_FLAG_1 (t) = 0;
2426 TYPE_LANG_FLAG_2 (t) = 0;
2427 TYPE_LANG_FLAG_3 (t) = 0;
2428 TYPE_LANG_FLAG_4 (t) = 0;
2429 TYPE_LANG_FLAG_5 (t) = 0;
2430 TYPE_LANG_FLAG_6 (t) = 0;
2431 /* But not this one. */
2432 SET_IS_AGGR_TYPE (t, 1);
2435 /* Make BINFO's vtable have N entries, including RTTI entries,
2436 vbase and vcall offsets, etc. Set its type and call the backend
2437 to lay it out. */
2439 static void
2440 layout_vtable_decl (binfo, n)
2441 tree binfo;
2442 int n;
2444 tree atype;
2445 tree vtable;
2447 atype = build_cplus_array_type (vtable_entry_type,
2448 build_index_type (size_int (n - 1)));
2449 layout_type (atype);
2451 /* We may have to grow the vtable. */
2452 vtable = get_vtbl_decl_for_binfo (binfo);
2453 if (!same_type_p (TREE_TYPE (vtable), atype))
2455 TREE_TYPE (vtable) = atype;
2456 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2457 layout_decl (vtable, 0);
2459 /* At one time the vtable info was grabbed 2 words at a time. This
2460 fails on Sparc unless you have 8-byte alignment. */
2461 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2462 DECL_ALIGN (vtable));
2466 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2467 have the same signature. */
2470 same_signature_p (fndecl, base_fndecl)
2471 tree fndecl, base_fndecl;
2473 /* One destructor overrides another if they are the same kind of
2474 destructor. */
2475 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2476 && special_function_p (base_fndecl) == special_function_p (fndecl))
2477 return 1;
2478 /* But a non-destructor never overrides a destructor, nor vice
2479 versa, nor do different kinds of destructors override
2480 one-another. For example, a complete object destructor does not
2481 override a deleting destructor. */
2482 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2483 return 0;
2485 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2487 tree types, base_types;
2488 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2489 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2490 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2491 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2492 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2493 return 1;
2495 return 0;
2498 typedef struct find_final_overrider_data_s {
2499 /* The function for which we are trying to find a final overrider. */
2500 tree fn;
2501 /* The base class in which the function was declared. */
2502 tree declaring_base;
2503 /* The most derived class in the hierarchy. */
2504 tree most_derived_type;
2505 /* The final overriding function. */
2506 tree overriding_fn;
2507 /* The functions that we thought might be final overriders, but
2508 aren't. */
2509 tree candidates;
2510 /* The BINFO for the class in which the final overriding function
2511 appears. */
2512 tree overriding_base;
2513 } find_final_overrider_data;
2515 /* Called from find_final_overrider via dfs_walk. */
2517 static tree
2518 dfs_find_final_overrider (binfo, data)
2519 tree binfo;
2520 void *data;
2522 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2524 if (same_type_p (BINFO_TYPE (binfo),
2525 BINFO_TYPE (ffod->declaring_base))
2526 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2527 BINFO_OFFSET (ffod->declaring_base)))
2529 tree path;
2530 tree method;
2532 /* We haven't found an overrider yet. */
2533 method = NULL_TREE;
2534 /* We've found a path to the declaring base. Walk down the path
2535 looking for an overrider for FN. */
2536 for (path = reverse_path (binfo);
2537 path;
2538 path = TREE_CHAIN (path))
2540 method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2541 ffod->fn);
2542 if (method)
2543 break;
2546 /* If we found an overrider, record the overriding function, and
2547 the base from which it came. */
2548 if (path)
2550 tree base;
2552 /* Assume the path is non-virtual. See if there are any
2553 virtual bases from (but not including) the overrider up
2554 to and including the base where the function is
2555 defined. */
2556 for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
2557 if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
2559 base = ffod->declaring_base;
2560 break;
2563 /* If we didn't already have an overrider, or any
2564 candidates, then this function is the best candidate so
2565 far. */
2566 if (!ffod->overriding_fn && !ffod->candidates)
2568 ffod->overriding_fn = method;
2569 ffod->overriding_base = TREE_VALUE (path);
2571 else if (ffod->overriding_fn)
2573 /* We had a best overrider; let's see how this compares. */
2575 if (ffod->overriding_fn == method
2576 && (tree_int_cst_equal
2577 (BINFO_OFFSET (TREE_VALUE (path)),
2578 BINFO_OFFSET (ffod->overriding_base))))
2579 /* We found the same overrider we already have, and in the
2580 same place; it's still the best. */;
2581 else if (strictly_overrides (ffod->overriding_fn, method))
2582 /* The old function overrides this function; it's still the
2583 best. */;
2584 else if (strictly_overrides (method, ffod->overriding_fn))
2586 /* The new function overrides the old; it's now the
2587 best. */
2588 ffod->overriding_fn = method;
2589 ffod->overriding_base = TREE_VALUE (path);
2591 else
2593 /* Ambiguous. */
2594 ffod->candidates
2595 = build_tree_list (NULL_TREE,
2596 ffod->overriding_fn);
2597 if (method != ffod->overriding_fn)
2598 ffod->candidates
2599 = tree_cons (NULL_TREE, method, ffod->candidates);
2600 ffod->overriding_fn = NULL_TREE;
2601 ffod->overriding_base = NULL_TREE;
2604 else
2606 /* We had a list of ambiguous overrides; let's see how this
2607 new one compares. */
2609 tree candidates;
2610 bool incomparable = false;
2612 /* If there were previous candidates, and this function
2613 overrides all of them, then it is the new best
2614 candidate. */
2615 for (candidates = ffod->candidates;
2616 candidates;
2617 candidates = TREE_CHAIN (candidates))
2619 /* If the candidate overrides the METHOD, then we
2620 needn't worry about it any further. */
2621 if (strictly_overrides (TREE_VALUE (candidates),
2622 method))
2624 method = NULL_TREE;
2625 break;
2628 /* If the METHOD doesn't override the candidate,
2629 then it is incomporable. */
2630 if (!strictly_overrides (method,
2631 TREE_VALUE (candidates)))
2632 incomparable = true;
2635 /* If METHOD overrode all the candidates, then it is the
2636 new best candidate. */
2637 if (!candidates && !incomparable)
2639 ffod->overriding_fn = method;
2640 ffod->overriding_base = TREE_VALUE (path);
2641 ffod->candidates = NULL_TREE;
2643 /* If METHOD didn't override all the candidates, then it
2644 is another candidate. */
2645 else if (method && incomparable)
2646 ffod->candidates
2647 = tree_cons (NULL_TREE, method, ffod->candidates);
2652 return NULL_TREE;
2655 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2656 FN and whose TREE_VALUE is the binfo for the base where the
2657 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2658 base object in which FN is declared. */
2660 static tree
2661 find_final_overrider (t, binfo, fn)
2662 tree t;
2663 tree binfo;
2664 tree fn;
2666 find_final_overrider_data ffod;
2668 /* Getting this right is a little tricky. This is legal:
2670 struct S { virtual void f (); };
2671 struct T { virtual void f (); };
2672 struct U : public S, public T { };
2674 even though calling `f' in `U' is ambiguous. But,
2676 struct R { virtual void f(); };
2677 struct S : virtual public R { virtual void f (); };
2678 struct T : virtual public R { virtual void f (); };
2679 struct U : public S, public T { };
2681 is not -- there's no way to decide whether to put `S::f' or
2682 `T::f' in the vtable for `R'.
2684 The solution is to look at all paths to BINFO. If we find
2685 different overriders along any two, then there is a problem. */
2686 ffod.fn = fn;
2687 ffod.declaring_base = binfo;
2688 ffod.most_derived_type = t;
2689 ffod.overriding_fn = NULL_TREE;
2690 ffod.overriding_base = NULL_TREE;
2691 ffod.candidates = NULL_TREE;
2693 dfs_walk (TYPE_BINFO (t),
2694 dfs_find_final_overrider,
2695 NULL,
2696 &ffod);
2698 /* If there was no winner, issue an error message. */
2699 if (!ffod.overriding_fn)
2701 cp_error ("no unique final overrider for `%D' in `%T'", fn, t);
2702 return error_mark_node;
2705 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2708 /* Returns the function from the BINFO_VIRTUALS entry in T which matches
2709 the signature of FUNCTION_DECL FN, or NULL_TREE if none. In other words,
2710 the function that the slot in T's primary vtable points to. */
2712 static tree get_matching_virtual PARAMS ((tree, tree));
2713 static tree
2714 get_matching_virtual (t, fn)
2715 tree t, fn;
2717 tree f;
2719 for (f = BINFO_VIRTUALS (TYPE_BINFO (t)); f; f = TREE_CHAIN (f))
2720 if (same_signature_p (BV_FN (f), fn))
2721 return BV_FN (f);
2722 return NULL_TREE;
2725 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2726 dominated by T. FN has been overriden in BINFO; VIRTUALS points to the
2727 corresponding position in the BINFO_VIRTUALS list. */
2729 static void
2730 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2731 tree t;
2732 tree binfo;
2733 tree fn;
2734 tree *virtuals;
2736 tree b;
2737 tree overrider;
2738 tree delta;
2739 tree virtual_base;
2740 tree first_defn;
2742 /* Find the nearest primary base (possibly binfo itself) which defines
2743 this function; this is the class the caller will convert to when
2744 calling FN through BINFO. */
2745 for (b = binfo; ; b = get_primary_binfo (b))
2747 if (look_for_overrides_here (BINFO_TYPE (b), fn))
2748 break;
2750 first_defn = b;
2752 /* Find the final overrider. */
2753 overrider = find_final_overrider (t, b, fn);
2754 if (overrider == error_mark_node)
2755 return;
2757 /* Assume that we will produce a thunk that convert all the way to
2758 the final overrider, and not to an intermediate virtual base. */
2759 virtual_base = NULL_TREE;
2761 /* Under the new ABI, we will convert to an intermediate virtual
2762 base first, and then use the vcall offset located there to finish
2763 the conversion. */
2764 while (b)
2766 /* If we find the final overrider, then we can stop
2767 walking. */
2768 if (same_type_p (BINFO_TYPE (b),
2769 BINFO_TYPE (TREE_VALUE (overrider))))
2770 break;
2772 /* If we find a virtual base, and we haven't yet found the
2773 overrider, then there is a virtual base between the
2774 declaring base (first_defn) and the final overrider. */
2775 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2776 virtual_base = b;
2778 b = BINFO_INHERITANCE_CHAIN (b);
2781 /* Compute the constant adjustment to the `this' pointer. The
2782 `this' pointer, when this function is called, will point at BINFO
2783 (or one of its primary bases, which are at the same offset). */
2785 if (virtual_base)
2786 /* The `this' pointer needs to be adjusted to the nearest virtual
2787 base. */
2788 delta = size_diffop (BINFO_OFFSET (virtual_base),
2789 BINFO_OFFSET (binfo));
2790 else
2792 /* The `this' pointer needs to be adjusted from pointing to
2793 BINFO to pointing at the base where the final overrider
2794 appears. */
2795 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2796 BINFO_OFFSET (binfo));
2798 if (! integer_zerop (delta))
2800 /* We'll need a thunk. But if we have a (perhaps formerly)
2801 primary virtual base, we have a vcall slot for this function,
2802 so we can use it rather than create a non-virtual thunk. */
2804 b = get_primary_binfo (first_defn);
2805 for (; b; b = get_primary_binfo (b))
2807 tree f = get_matching_virtual (BINFO_TYPE (b), fn);
2808 if (!f)
2809 /* b doesn't have this function; no suitable vbase. */
2810 break;
2811 if (TREE_VIA_VIRTUAL (b))
2813 /* Found one; we can treat ourselves as a virtual base. */
2814 virtual_base = binfo;
2815 delta = size_zero_node;
2816 break;
2822 modify_vtable_entry (t,
2823 binfo,
2824 TREE_PURPOSE (overrider),
2825 delta,
2826 virtuals);
2828 if (virtual_base)
2829 BV_USE_VCALL_INDEX_P (*virtuals) = 1;
2832 /* Called from modify_all_vtables via dfs_walk. */
2834 static tree
2835 dfs_modify_vtables (binfo, data)
2836 tree binfo;
2837 void *data;
2839 if (/* There's no need to modify the vtable for a non-virtual
2840 primary base; we're not going to use that vtable anyhow.
2841 We do still need to do this for virtual primary bases, as they
2842 could become non-primary in a construction vtable. */
2843 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2844 /* Similarly, a base without a vtable needs no modification. */
2845 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2847 tree t;
2848 tree virtuals;
2849 tree old_virtuals;
2851 t = (tree) data;
2853 /* If we're supporting RTTI then we always need a new vtable to
2854 point to the RTTI information. Under the new ABI we may need
2855 a new vtable to contain vcall and vbase offsets. */
2856 make_new_vtable (t, binfo);
2858 /* Now, go through each of the virtual functions in the virtual
2859 function table for BINFO. Find the final overrider, and
2860 update the BINFO_VIRTUALS list appropriately. */
2861 for (virtuals = BINFO_VIRTUALS (binfo),
2862 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2863 virtuals;
2864 virtuals = TREE_CHAIN (virtuals),
2865 old_virtuals = TREE_CHAIN (old_virtuals))
2866 update_vtable_entry_for_fn (t,
2867 binfo,
2868 BV_FN (old_virtuals),
2869 &virtuals);
2872 SET_BINFO_MARKED (binfo);
2874 return NULL_TREE;
2877 /* Update all of the primary and secondary vtables for T. Create new
2878 vtables as required, and initialize their RTTI information. Each
2879 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2880 function from a base class; find and modify the appropriate entries
2881 to point to the overriding functions. Returns a list, in
2882 declaration order, of the functions that are overridden in this
2883 class, but do not appear in the primary base class vtable, and
2884 which should therefore be appended to the end of the vtable for T. */
2886 static tree
2887 modify_all_vtables (t, vfuns_p, overridden_virtuals)
2888 tree t;
2889 int *vfuns_p;
2890 tree overridden_virtuals;
2892 tree binfo;
2894 binfo = TYPE_BINFO (t);
2896 /* Update all of the vtables. */
2897 dfs_walk (binfo,
2898 dfs_modify_vtables,
2899 dfs_unmarked_real_bases_queue_p,
2901 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2903 /* If we should include overriding functions for secondary vtables
2904 in our primary vtable, add them now. */
2905 if (all_overridden_vfuns_in_vtables_p ())
2907 tree *fnsp = &overridden_virtuals;
2909 while (*fnsp)
2911 tree fn = TREE_VALUE (*fnsp);
2913 if (!BINFO_VIRTUALS (binfo)
2914 || !value_member (fn, BINFO_VIRTUALS (binfo)))
2916 /* Set the vtable index. */
2917 set_vindex (fn, vfuns_p);
2918 /* We don't need to convert to a base class when calling
2919 this function. */
2920 DECL_VIRTUAL_CONTEXT (fn) = t;
2922 /* We don't need to adjust the `this' pointer when
2923 calling this function. */
2924 BV_DELTA (*fnsp) = integer_zero_node;
2925 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2927 /* This is an overridden function not already in our
2928 vtable. Keep it. */
2929 fnsp = &TREE_CHAIN (*fnsp);
2931 else
2932 /* We've already got an entry for this function. Skip
2933 it. */
2934 *fnsp = TREE_CHAIN (*fnsp);
2937 else
2938 overridden_virtuals = NULL_TREE;
2940 return overridden_virtuals;
2943 /* Here, we already know that they match in every respect.
2944 All we have to check is where they had their declarations. */
2946 static int
2947 strictly_overrides (fndecl1, fndecl2)
2948 tree fndecl1, fndecl2;
2950 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
2951 DECL_CONTEXT (fndecl1),
2952 0, (tree *)0);
2953 if (distance == -2 || distance > 0)
2954 return 1;
2955 return 0;
2958 /* Get the base virtual function declarations in T that are either
2959 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2960 the overrider/hider. */
2962 static tree
2963 get_basefndecls (fndecl, t)
2964 tree fndecl, t;
2966 tree methods = TYPE_METHODS (t);
2967 tree base_fndecls = NULL_TREE;
2968 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2969 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2971 while (methods)
2973 if (TREE_CODE (methods) == FUNCTION_DECL
2974 && DECL_VINDEX (methods) != NULL_TREE
2975 && DECL_NAME (fndecl) == DECL_NAME (methods))
2976 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
2978 methods = TREE_CHAIN (methods);
2981 if (base_fndecls)
2982 return base_fndecls;
2984 for (i = 0; i < n_baseclasses; i++)
2986 tree base_binfo = TREE_VEC_ELT (binfos, i);
2987 tree basetype = BINFO_TYPE (base_binfo);
2989 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2990 base_fndecls);
2993 return base_fndecls;
2996 /* Mark the functions that have been hidden with their overriders.
2997 Since we start out with all functions already marked with a hider,
2998 no need to mark functions that are just hidden.
3000 Subroutine of warn_hidden. */
3002 static void
3003 mark_overriders (fndecl, base_fndecls)
3004 tree fndecl, base_fndecls;
3006 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3007 if (same_signature_p (fndecl, TREE_VALUE (base_fndecls)))
3008 TREE_PURPOSE (base_fndecls) = fndecl;
3011 /* If this declaration supersedes the declaration of
3012 a method declared virtual in the base class, then
3013 mark this field as being virtual as well. */
3015 static void
3016 check_for_override (decl, ctype)
3017 tree decl, ctype;
3019 if (TREE_CODE (decl) == TEMPLATE_DECL)
3020 /* In [temp.mem] we have:
3022 A specialization of a member function template does not
3023 override a virtual function from a base class. */
3024 return;
3025 if ((DECL_DESTRUCTOR_P (decl)
3026 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
3027 && look_for_overrides (ctype, decl)
3028 && !DECL_STATIC_FUNCTION_P (decl))
3030 /* Set DECL_VINDEX to a value that is neither an
3031 INTEGER_CST nor the error_mark_node so that
3032 add_virtual_function will realize this is an
3033 overriding function. */
3034 DECL_VINDEX (decl) = decl;
3036 if (DECL_VIRTUAL_P (decl))
3038 if (DECL_VINDEX (decl) == NULL_TREE)
3039 DECL_VINDEX (decl) = error_mark_node;
3040 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3044 /* Warn about hidden virtual functions that are not overridden in t.
3045 We know that constructors and destructors don't apply. */
3047 void
3048 warn_hidden (t)
3049 tree t;
3051 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3052 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3053 int i;
3055 /* We go through each separately named virtual function. */
3056 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3058 tree fns = TREE_VEC_ELT (method_vec, i);
3059 tree fndecl = NULL_TREE;
3061 tree base_fndecls = NULL_TREE;
3062 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3063 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3065 /* First see if we have any virtual functions in this batch. */
3066 for (; fns; fns = OVL_NEXT (fns))
3068 fndecl = OVL_CURRENT (fns);
3069 if (DECL_VINDEX (fndecl))
3070 break;
3073 if (fns == NULL_TREE)
3074 continue;
3076 /* First we get a list of all possible functions that might be
3077 hidden from each base class. */
3078 for (i = 0; i < n_baseclasses; i++)
3080 tree base_binfo = TREE_VEC_ELT (binfos, i);
3081 tree basetype = BINFO_TYPE (base_binfo);
3083 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3084 base_fndecls);
3087 fns = OVL_NEXT (fns);
3089 /* ...then mark up all the base functions with overriders, preferring
3090 overriders to hiders. */
3091 if (base_fndecls)
3092 for (; fns; fns = OVL_NEXT (fns))
3094 fndecl = OVL_CURRENT (fns);
3095 if (DECL_VINDEX (fndecl))
3096 mark_overriders (fndecl, base_fndecls);
3099 /* Now give a warning for all base functions without overriders,
3100 as they are hidden. */
3101 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3102 if (!same_signature_p (TREE_PURPOSE (base_fndecls),
3103 TREE_VALUE (base_fndecls)))
3105 /* Here we know it is a hider, and no overrider exists. */
3106 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3107 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3112 /* Check for things that are invalid. There are probably plenty of other
3113 things we should check for also. */
3115 static void
3116 finish_struct_anon (t)
3117 tree t;
3119 tree field;
3121 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3123 if (TREE_STATIC (field))
3124 continue;
3125 if (TREE_CODE (field) != FIELD_DECL)
3126 continue;
3128 if (DECL_NAME (field) == NULL_TREE
3129 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3131 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3132 for (; elt; elt = TREE_CHAIN (elt))
3134 if (DECL_ARTIFICIAL (elt))
3135 continue;
3137 if (DECL_NAME (elt) == constructor_name (t))
3138 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
3139 elt);
3141 if (TREE_CODE (elt) != FIELD_DECL)
3143 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3144 elt);
3145 continue;
3148 if (TREE_PRIVATE (elt))
3149 cp_pedwarn_at ("private member `%#D' in anonymous union",
3150 elt);
3151 else if (TREE_PROTECTED (elt))
3152 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3153 elt);
3155 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3156 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3162 /* Create default constructors, assignment operators, and so forth for
3163 the type indicated by T, if they are needed.
3164 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3165 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
3166 class cannot have a default constructor, copy constructor taking a
3167 const reference argument, or an assignment operator taking a const
3168 reference, respectively. If a virtual destructor is created, its
3169 DECL is returned; otherwise the return value is NULL_TREE. */
3171 static tree
3172 add_implicitly_declared_members (t, cant_have_default_ctor,
3173 cant_have_const_cctor,
3174 cant_have_const_assignment)
3175 tree t;
3176 int cant_have_default_ctor;
3177 int cant_have_const_cctor;
3178 int cant_have_const_assignment;
3180 tree default_fn;
3181 tree implicit_fns = NULL_TREE;
3182 tree virtual_dtor = NULL_TREE;
3183 tree *f;
3185 /* Destructor. */
3186 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3188 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
3189 check_for_override (default_fn, t);
3191 /* If we couldn't make it work, then pretend we didn't need it. */
3192 if (default_fn == void_type_node)
3193 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
3194 else
3196 TREE_CHAIN (default_fn) = implicit_fns;
3197 implicit_fns = default_fn;
3199 if (DECL_VINDEX (default_fn))
3200 virtual_dtor = default_fn;
3203 else
3204 /* Any non-implicit destructor is non-trivial. */
3205 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3207 /* Default constructor. */
3208 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3210 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
3211 TREE_CHAIN (default_fn) = implicit_fns;
3212 implicit_fns = default_fn;
3215 /* Copy constructor. */
3216 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3218 /* ARM 12.18: You get either X(X&) or X(const X&), but
3219 not both. --Chip */
3220 default_fn
3221 = implicitly_declare_fn (sfk_copy_constructor, t,
3222 /*const_p=*/!cant_have_const_cctor);
3223 TREE_CHAIN (default_fn) = implicit_fns;
3224 implicit_fns = default_fn;
3227 /* Assignment operator. */
3228 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3230 default_fn
3231 = implicitly_declare_fn (sfk_assignment_operator, t,
3232 /*const_p=*/!cant_have_const_assignment);
3233 TREE_CHAIN (default_fn) = implicit_fns;
3234 implicit_fns = default_fn;
3237 /* Now, hook all of the new functions on to TYPE_METHODS,
3238 and add them to the CLASSTYPE_METHOD_VEC. */
3239 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3240 add_method (t, *f, /*error_p=*/0);
3241 *f = TYPE_METHODS (t);
3242 TYPE_METHODS (t) = implicit_fns;
3244 return virtual_dtor;
3247 /* Subroutine of finish_struct_1. Recursively count the number of fields
3248 in TYPE, including anonymous union members. */
3250 static int
3251 count_fields (fields)
3252 tree fields;
3254 tree x;
3255 int n_fields = 0;
3256 for (x = fields; x; x = TREE_CHAIN (x))
3258 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3259 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3260 else
3261 n_fields += 1;
3263 return n_fields;
3266 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3267 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3269 static int
3270 add_fields_to_vec (fields, field_vec, idx)
3271 tree fields, field_vec;
3272 int idx;
3274 tree x;
3275 for (x = fields; x; x = TREE_CHAIN (x))
3277 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3278 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3279 else
3280 TREE_VEC_ELT (field_vec, idx++) = x;
3282 return idx;
3285 /* FIELD is a bit-field. We are finishing the processing for its
3286 enclosing type. Issue any appropriate messages and set appropriate
3287 flags. */
3289 static void
3290 check_bitfield_decl (field)
3291 tree field;
3293 tree type = TREE_TYPE (field);
3294 tree w = NULL_TREE;
3296 /* Detect invalid bit-field type. */
3297 if (DECL_INITIAL (field)
3298 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3300 cp_error_at ("bit-field `%#D' with non-integral type", field);
3301 w = error_mark_node;
3304 /* Detect and ignore out of range field width. */
3305 if (DECL_INITIAL (field))
3307 w = DECL_INITIAL (field);
3309 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3310 STRIP_NOPS (w);
3312 /* detect invalid field size. */
3313 if (TREE_CODE (w) == CONST_DECL)
3314 w = DECL_INITIAL (w);
3315 else
3316 w = decl_constant_value (w);
3318 if (TREE_CODE (w) != INTEGER_CST)
3320 cp_error_at ("bit-field `%D' width not an integer constant",
3321 field);
3322 w = error_mark_node;
3324 else if (tree_int_cst_sgn (w) < 0)
3326 cp_error_at ("negative width in bit-field `%D'", field);
3327 w = error_mark_node;
3329 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3331 cp_error_at ("zero width for bit-field `%D'", field);
3332 w = error_mark_node;
3334 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3335 && TREE_CODE (type) != ENUMERAL_TYPE
3336 && TREE_CODE (type) != BOOLEAN_TYPE)
3337 cp_warning_at ("width of `%D' exceeds its type", field);
3338 else if (TREE_CODE (type) == ENUMERAL_TYPE
3339 && (0 > compare_tree_int (w,
3340 min_precision (TYPE_MIN_VALUE (type),
3341 TREE_UNSIGNED (type)))
3342 || 0 > compare_tree_int (w,
3343 min_precision
3344 (TYPE_MAX_VALUE (type),
3345 TREE_UNSIGNED (type)))))
3346 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3347 field, type);
3350 /* Remove the bit-field width indicator so that the rest of the
3351 compiler does not treat that value as an initializer. */
3352 DECL_INITIAL (field) = NULL_TREE;
3354 if (w != error_mark_node)
3356 DECL_SIZE (field) = convert (bitsizetype, w);
3357 DECL_BIT_FIELD (field) = 1;
3359 if (integer_zerop (w))
3361 #ifdef EMPTY_FIELD_BOUNDARY
3362 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3363 EMPTY_FIELD_BOUNDARY);
3364 #endif
3365 #ifdef PCC_BITFIELD_TYPE_MATTERS
3366 if (PCC_BITFIELD_TYPE_MATTERS)
3368 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3369 TYPE_ALIGN (type));
3370 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3372 #endif
3375 else
3377 /* Non-bit-fields are aligned for their type. */
3378 DECL_BIT_FIELD (field) = 0;
3379 CLEAR_DECL_C_BIT_FIELD (field);
3380 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3381 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3385 /* FIELD is a non bit-field. We are finishing the processing for its
3386 enclosing type T. Issue any appropriate messages and set appropriate
3387 flags. */
3389 static void
3390 check_field_decl (field, t, cant_have_const_ctor,
3391 cant_have_default_ctor, no_const_asn_ref,
3392 any_default_members)
3393 tree field;
3394 tree t;
3395 int *cant_have_const_ctor;
3396 int *cant_have_default_ctor;
3397 int *no_const_asn_ref;
3398 int *any_default_members;
3400 tree type = strip_array_types (TREE_TYPE (field));
3402 /* An anonymous union cannot contain any fields which would change
3403 the settings of CANT_HAVE_CONST_CTOR and friends. */
3404 if (ANON_UNION_TYPE_P (type))
3406 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3407 structs. So, we recurse through their fields here. */
3408 else if (ANON_AGGR_TYPE_P (type))
3410 tree fields;
3412 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3413 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3414 check_field_decl (fields, t, cant_have_const_ctor,
3415 cant_have_default_ctor, no_const_asn_ref,
3416 any_default_members);
3418 /* Check members with class type for constructors, destructors,
3419 etc. */
3420 else if (CLASS_TYPE_P (type))
3422 /* Never let anything with uninheritable virtuals
3423 make it through without complaint. */
3424 abstract_virtuals_error (field, type);
3426 if (TREE_CODE (t) == UNION_TYPE)
3428 if (TYPE_NEEDS_CONSTRUCTING (type))
3429 cp_error_at ("member `%#D' with constructor not allowed in union",
3430 field);
3431 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3432 cp_error_at ("member `%#D' with destructor not allowed in union",
3433 field);
3434 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3435 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3436 field);
3438 else
3440 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3441 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3442 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3443 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3444 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3447 if (!TYPE_HAS_CONST_INIT_REF (type))
3448 *cant_have_const_ctor = 1;
3450 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3451 *no_const_asn_ref = 1;
3453 if (TYPE_HAS_CONSTRUCTOR (type)
3454 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3455 *cant_have_default_ctor = 1;
3457 if (DECL_INITIAL (field) != NULL_TREE)
3459 /* `build_class_init_list' does not recognize
3460 non-FIELD_DECLs. */
3461 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3462 cp_error_at ("multiple fields in union `%T' initialized");
3463 *any_default_members = 1;
3466 /* Non-bit-fields are aligned for their type, except packed fields
3467 which require only BITS_PER_UNIT alignment. */
3468 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3469 (DECL_PACKED (field)
3470 ? BITS_PER_UNIT
3471 : TYPE_ALIGN (TREE_TYPE (field))));
3472 if (! DECL_PACKED (field))
3473 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3476 /* Check the data members (both static and non-static), class-scoped
3477 typedefs, etc., appearing in the declaration of T. Issue
3478 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3479 declaration order) of access declarations; each TREE_VALUE in this
3480 list is a USING_DECL.
3482 In addition, set the following flags:
3484 EMPTY_P
3485 The class is empty, i.e., contains no non-static data members.
3487 CANT_HAVE_DEFAULT_CTOR_P
3488 This class cannot have an implicitly generated default
3489 constructor.
3491 CANT_HAVE_CONST_CTOR_P
3492 This class cannot have an implicitly generated copy constructor
3493 taking a const reference.
3495 CANT_HAVE_CONST_ASN_REF
3496 This class cannot have an implicitly generated assignment
3497 operator taking a const reference.
3499 All of these flags should be initialized before calling this
3500 function.
3502 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3503 fields can be added by adding to this chain. */
3505 static void
3506 check_field_decls (t, access_decls, empty_p,
3507 cant_have_default_ctor_p, cant_have_const_ctor_p,
3508 no_const_asn_ref_p)
3509 tree t;
3510 tree *access_decls;
3511 int *empty_p;
3512 int *cant_have_default_ctor_p;
3513 int *cant_have_const_ctor_p;
3514 int *no_const_asn_ref_p;
3516 tree *field;
3517 tree *next;
3518 int has_pointers;
3519 int any_default_members;
3521 /* First, delete any duplicate fields. */
3522 delete_duplicate_fields (TYPE_FIELDS (t));
3524 /* Assume there are no access declarations. */
3525 *access_decls = NULL_TREE;
3526 /* Assume this class has no pointer members. */
3527 has_pointers = 0;
3528 /* Assume none of the members of this class have default
3529 initializations. */
3530 any_default_members = 0;
3532 for (field = &TYPE_FIELDS (t); *field; field = next)
3534 tree x = *field;
3535 tree type = TREE_TYPE (x);
3537 GNU_xref_member (current_class_name, x);
3539 next = &TREE_CHAIN (x);
3541 if (TREE_CODE (x) == FIELD_DECL)
3543 DECL_PACKED (x) |= TYPE_PACKED (t);
3545 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3546 /* We don't treat zero-width bitfields as making a class
3547 non-empty. */
3549 else
3551 /* The class is non-empty. */
3552 *empty_p = 0;
3553 /* The class is not even nearly empty. */
3554 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3558 if (TREE_CODE (x) == USING_DECL)
3560 /* Prune the access declaration from the list of fields. */
3561 *field = TREE_CHAIN (x);
3563 /* Save the access declarations for our caller. */
3564 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3566 /* Since we've reset *FIELD there's no reason to skip to the
3567 next field. */
3568 next = field;
3569 continue;
3572 if (TREE_CODE (x) == TYPE_DECL
3573 || TREE_CODE (x) == TEMPLATE_DECL)
3574 continue;
3576 /* If we've gotten this far, it's a data member, possibly static,
3577 or an enumerator. */
3579 DECL_CONTEXT (x) = t;
3581 /* ``A local class cannot have static data members.'' ARM 9.4 */
3582 if (current_function_decl && TREE_STATIC (x))
3583 cp_error_at ("field `%D' in local class cannot be static", x);
3585 /* Perform error checking that did not get done in
3586 grokdeclarator. */
3587 if (TREE_CODE (type) == FUNCTION_TYPE)
3589 cp_error_at ("field `%D' invalidly declared function type",
3591 type = build_pointer_type (type);
3592 TREE_TYPE (x) = type;
3594 else if (TREE_CODE (type) == METHOD_TYPE)
3596 cp_error_at ("field `%D' invalidly declared method type", x);
3597 type = build_pointer_type (type);
3598 TREE_TYPE (x) = type;
3600 else if (TREE_CODE (type) == OFFSET_TYPE)
3602 cp_error_at ("field `%D' invalidly declared offset type", x);
3603 type = build_pointer_type (type);
3604 TREE_TYPE (x) = type;
3607 if (type == error_mark_node)
3608 continue;
3610 /* When this goes into scope, it will be a non-local reference. */
3611 DECL_NONLOCAL (x) = 1;
3613 if (TREE_CODE (x) == CONST_DECL)
3614 continue;
3616 if (TREE_CODE (x) == VAR_DECL)
3618 if (TREE_CODE (t) == UNION_TYPE)
3619 /* Unions cannot have static members. */
3620 cp_error_at ("field `%D' declared static in union", x);
3622 continue;
3625 /* Now it can only be a FIELD_DECL. */
3627 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3628 CLASSTYPE_NON_AGGREGATE (t) = 1;
3630 /* If this is of reference type, check if it needs an init.
3631 Also do a little ANSI jig if necessary. */
3632 if (TREE_CODE (type) == REFERENCE_TYPE)
3634 CLASSTYPE_NON_POD_P (t) = 1;
3635 if (DECL_INITIAL (x) == NULL_TREE)
3636 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3638 /* ARM $12.6.2: [A member initializer list] (or, for an
3639 aggregate, initialization by a brace-enclosed list) is the
3640 only way to initialize nonstatic const and reference
3641 members. */
3642 *cant_have_default_ctor_p = 1;
3643 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3645 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3646 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3649 type = strip_array_types (type);
3651 if (TREE_CODE (type) == POINTER_TYPE)
3652 has_pointers = 1;
3654 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3655 CLASSTYPE_HAS_MUTABLE (t) = 1;
3657 if (! pod_type_p (type))
3658 /* DR 148 now allows pointers to members (which are POD themselves),
3659 to be allowed in POD structs. */
3660 CLASSTYPE_NON_POD_P (t) = 1;
3662 /* If any field is const, the structure type is pseudo-const. */
3663 if (CP_TYPE_CONST_P (type))
3665 C_TYPE_FIELDS_READONLY (t) = 1;
3666 if (DECL_INITIAL (x) == NULL_TREE)
3667 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3669 /* ARM $12.6.2: [A member initializer list] (or, for an
3670 aggregate, initialization by a brace-enclosed list) is the
3671 only way to initialize nonstatic const and reference
3672 members. */
3673 *cant_have_default_ctor_p = 1;
3674 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3676 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3677 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3679 /* A field that is pseudo-const makes the structure likewise. */
3680 else if (IS_AGGR_TYPE (type))
3682 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3683 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3684 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3687 /* Core issue 80: A nonstatic data member is required to have a
3688 different name from the class iff the class has a
3689 user-defined constructor. */
3690 if (DECL_NAME (x) == constructor_name (t)
3691 && TYPE_HAS_CONSTRUCTOR (t))
3692 cp_pedwarn_at ("field `%#D' with same name as class", x);
3694 /* We set DECL_C_BIT_FIELD in grokbitfield.
3695 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3696 if (DECL_C_BIT_FIELD (x))
3697 check_bitfield_decl (x);
3698 else
3699 check_field_decl (x, t,
3700 cant_have_const_ctor_p,
3701 cant_have_default_ctor_p,
3702 no_const_asn_ref_p,
3703 &any_default_members);
3706 /* Effective C++ rule 11. */
3707 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3708 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3710 cp_warning ("`%#T' has pointer data members", t);
3712 if (! TYPE_HAS_INIT_REF (t))
3714 cp_warning (" but does not override `%T(const %T&)'", t, t);
3715 if (! TYPE_HAS_ASSIGN_REF (t))
3716 cp_warning (" or `operator=(const %T&)'", t);
3718 else if (! TYPE_HAS_ASSIGN_REF (t))
3719 cp_warning (" but does not override `operator=(const %T&)'", t);
3723 /* Check anonymous struct/anonymous union fields. */
3724 finish_struct_anon (t);
3726 /* We've built up the list of access declarations in reverse order.
3727 Fix that now. */
3728 *access_decls = nreverse (*access_decls);
3731 /* Return a FIELD_DECL for a pointer-to-virtual-table or
3732 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
3733 field are as indicated. The CLASS_TYPE in which this field occurs
3734 is also indicated. FCONTEXT is the type that is needed for the debug
3735 info output routines. *EMPTY_P is set to a non-zero value by this
3736 function to indicate that a class containing this field is
3737 non-empty. */
3739 static tree
3740 build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
3741 empty_p)
3742 tree name;
3743 tree assembler_name;
3744 tree type;
3745 tree class_type;
3746 tree fcontext;
3747 int *empty_p;
3749 tree field;
3751 /* This class is non-empty. */
3752 *empty_p = 0;
3754 /* Build the FIELD_DECL. */
3755 field = build_decl (FIELD_DECL, name, type);
3756 SET_DECL_ASSEMBLER_NAME (field, assembler_name);
3757 DECL_VIRTUAL_P (field) = 1;
3758 DECL_ARTIFICIAL (field) = 1;
3759 DECL_FIELD_CONTEXT (field) = class_type;
3760 DECL_FCONTEXT (field) = fcontext;
3761 DECL_ALIGN (field) = TYPE_ALIGN (type);
3762 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (type);
3764 /* Return it. */
3765 return field;
3768 /* If TYPE is an empty class type, records its OFFSET in the table of
3769 OFFSETS. */
3771 static int
3772 record_subobject_offset (type, offset, offsets)
3773 tree type;
3774 tree offset;
3775 splay_tree offsets;
3777 splay_tree_node n;
3779 if (!is_empty_class (type))
3780 return 0;
3782 /* Record the location of this empty object in OFFSETS. */
3783 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3784 if (!n)
3785 n = splay_tree_insert (offsets,
3786 (splay_tree_key) offset,
3787 (splay_tree_value) NULL_TREE);
3788 n->value = ((splay_tree_value)
3789 tree_cons (NULL_TREE,
3790 type,
3791 (tree) n->value));
3793 return 0;
3796 /* Returns non-zero if TYPE is an empty class type and there is
3797 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3799 static int
3800 check_subobject_offset (type, offset, offsets)
3801 tree type;
3802 tree offset;
3803 splay_tree offsets;
3805 splay_tree_node n;
3806 tree t;
3808 if (!is_empty_class (type))
3809 return 0;
3811 /* Record the location of this empty object in OFFSETS. */
3812 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3813 if (!n)
3814 return 0;
3816 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3817 if (same_type_p (TREE_VALUE (t), type))
3818 return 1;
3820 return 0;
3823 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3824 F for every subobject, passing it the type, offset, and table of
3825 OFFSETS. If VBASES_P is non-zero, then even virtual non-primary
3826 bases should be traversed; otherwise, they are ignored.
3828 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3829 than MAX_OFFSET will not be walked.
3831 If F returns a non-zero value, the traversal ceases, and that value
3832 is returned. Otherwise, returns zero. */
3834 static int
3835 walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
3836 tree type;
3837 subobject_offset_fn f;
3838 tree offset;
3839 splay_tree offsets;
3840 tree max_offset;
3841 int vbases_p;
3843 int r = 0;
3845 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3846 stop. */
3847 if (max_offset && INT_CST_LT (max_offset, offset))
3848 return 0;
3850 if (CLASS_TYPE_P (type))
3852 tree field;
3853 int i;
3855 /* Record the location of TYPE. */
3856 r = (*f) (type, offset, offsets);
3857 if (r)
3858 return r;
3860 /* Iterate through the direct base classes of TYPE. */
3861 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3863 tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3865 if (!vbases_p
3866 && TREE_VIA_VIRTUAL (binfo)
3867 && !BINFO_PRIMARY_P (binfo))
3868 continue;
3870 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3872 size_binop (PLUS_EXPR,
3873 offset,
3874 BINFO_OFFSET (binfo)),
3875 offsets,
3876 max_offset,
3877 vbases_p);
3878 if (r)
3879 return r;
3882 /* Iterate through the fields of TYPE. */
3883 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3884 if (TREE_CODE (field) == FIELD_DECL)
3886 r = walk_subobject_offsets (TREE_TYPE (field),
3888 size_binop (PLUS_EXPR,
3889 offset,
3890 DECL_FIELD_OFFSET (field)),
3891 offsets,
3892 max_offset,
3893 /*vbases_p=*/1);
3894 if (r)
3895 return r;
3898 else if (TREE_CODE (type) == ARRAY_TYPE)
3900 tree domain = TYPE_DOMAIN (type);
3901 tree index;
3903 /* Step through each of the elements in the array. */
3904 for (index = size_zero_node;
3905 INT_CST_LT (index, TYPE_MAX_VALUE (domain));
3906 index = size_binop (PLUS_EXPR, index, size_one_node))
3908 r = walk_subobject_offsets (TREE_TYPE (type),
3910 offset,
3911 offsets,
3912 max_offset,
3913 /*vbases_p=*/1);
3914 if (r)
3915 return r;
3916 offset = size_binop (PLUS_EXPR, offset,
3917 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3918 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3919 there's no point in iterating through the remaining
3920 elements of the array. */
3921 if (max_offset && INT_CST_LT (max_offset, offset))
3922 break;
3926 return 0;
3929 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3930 OFFSETS. If VBASES_P is non-zero, virtual bases of TYPE are
3931 examined. */
3933 static void
3934 record_subobject_offsets (type, offset, offsets, vbases_p)
3935 tree type;
3936 tree offset;
3937 splay_tree offsets;
3938 int vbases_p;
3940 walk_subobject_offsets (type, record_subobject_offset, offset,
3941 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3944 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3945 OFFSET) conflict with entries in OFFSETS. If VBASES_P is non-zero,
3946 virtual bases of TYPE are examined. */
3948 static int
3949 layout_conflict_p (type, offset, offsets, vbases_p)
3950 tree type;
3951 tree offset;
3952 splay_tree offsets;
3953 int vbases_p;
3955 splay_tree_node max_node;
3957 /* Get the node in OFFSETS that indicates the maximum offset where
3958 an empty subobject is located. */
3959 max_node = splay_tree_max (offsets);
3960 /* If there aren't any empty subobjects, then there's no point in
3961 performing this check. */
3962 if (!max_node)
3963 return 0;
3965 return walk_subobject_offsets (type, check_subobject_offset, offset,
3966 offsets, (tree) (max_node->key),
3967 vbases_p);
3970 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3971 non-static data member of the type indicated by RLI. BINFO is the
3972 binfo corresponding to the base subobject, OFFSETS maps offsets to
3973 types already located at those offsets. T is the most derived
3974 type. This function determines the position of the DECL. */
3976 static void
3977 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
3978 record_layout_info rli;
3979 tree decl;
3980 tree binfo;
3981 splay_tree offsets;
3982 tree t;
3984 tree offset = NULL_TREE;
3985 tree type = TREE_TYPE (decl);
3986 /* If we are laying out a base class, rather than a field, then
3987 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
3988 int field_p = !DECL_ARTIFICIAL (decl);
3990 /* Try to place the field. It may take more than one try if we have
3991 a hard time placing the field without putting two objects of the
3992 same type at the same address. */
3993 while (1)
3995 struct record_layout_info_s old_rli = *rli;
3997 /* Place this field. */
3998 place_field (rli, decl);
3999 offset = byte_position (decl);
4001 /* We have to check to see whether or not there is already
4002 something of the same type at the offset we're about to use.
4003 For example:
4005 struct S {};
4006 struct T : public S { int i; };
4007 struct U : public S, public T {};
4009 Here, we put S at offset zero in U. Then, we can't put T at
4010 offset zero -- its S component would be at the same address
4011 as the S we already allocated. So, we have to skip ahead.
4012 Since all data members, including those whose type is an
4013 empty class, have non-zero size, any overlap can happen only
4014 with a direct or indirect base-class -- it can't happen with
4015 a data member. */
4016 if (layout_conflict_p (TREE_TYPE (decl),
4017 offset,
4018 offsets,
4019 field_p))
4021 /* Strip off the size allocated to this field. That puts us
4022 at the first place we could have put the field with
4023 proper alignment. */
4024 *rli = old_rli;
4026 /* Bump up by the alignment required for the type. */
4027 rli->bitpos
4028 = size_binop (PLUS_EXPR, rli->bitpos,
4029 bitsize_int (binfo
4030 ? CLASSTYPE_ALIGN (type)
4031 : TYPE_ALIGN (type)));
4032 normalize_rli (rli);
4034 else
4035 /* There was no conflict. We're done laying out this field. */
4036 break;
4039 /* Now that we know where it will be placed, update its
4040 BINFO_OFFSET. */
4041 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4042 propagate_binfo_offsets (binfo,
4043 convert (ssizetype, offset), t);
4046 /* Layout the empty base BINFO. EOC indicates the byte currently just
4047 past the end of the class, and should be correctly aligned for a
4048 class of the type indicated by BINFO; OFFSETS gives the offsets of
4049 the empty bases allocated so far. T is the most derived
4050 type. Return non-zero iff we added it at the end. */
4052 static bool
4053 layout_empty_base (binfo, eoc, offsets, t)
4054 tree binfo;
4055 tree eoc;
4056 splay_tree offsets;
4057 tree t;
4059 tree alignment;
4060 tree basetype = BINFO_TYPE (binfo);
4061 bool atend = false;
4063 /* This routine should only be used for empty classes. */
4064 my_friendly_assert (is_empty_class (basetype), 20000321);
4065 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4067 /* This is an empty base class. We first try to put it at offset
4068 zero. */
4069 if (layout_conflict_p (BINFO_TYPE (binfo),
4070 BINFO_OFFSET (binfo),
4071 offsets,
4072 /*vbases_p=*/0))
4074 /* That didn't work. Now, we move forward from the next
4075 available spot in the class. */
4076 atend = true;
4077 propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
4078 while (1)
4080 if (!layout_conflict_p (BINFO_TYPE (binfo),
4081 BINFO_OFFSET (binfo),
4082 offsets,
4083 /*vbases_p=*/0))
4084 /* We finally found a spot where there's no overlap. */
4085 break;
4087 /* There's overlap here, too. Bump along to the next spot. */
4088 propagate_binfo_offsets (binfo, alignment, t);
4091 return atend;
4094 /* Build a FIELD_DECL for the base given by BINFO in the class
4095 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
4096 *BASE_ALIGN is a running maximum of the alignments of any base
4097 class. OFFSETS gives the location of empty base subobjects. T is
4098 the most derived type. Return non-zero if the new object cannot be
4099 nearly-empty. */
4101 static bool
4102 build_base_field (rli, binfo, empty_p, offsets, t)
4103 record_layout_info rli;
4104 tree binfo;
4105 int *empty_p;
4106 splay_tree offsets;
4107 tree t;
4109 tree basetype = BINFO_TYPE (binfo);
4110 tree decl;
4111 bool atend = false;
4113 if (!COMPLETE_TYPE_P (basetype))
4114 /* This error is now reported in xref_tag, thus giving better
4115 location information. */
4116 return atend;
4118 decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
4119 DECL_ARTIFICIAL (decl) = 1;
4120 DECL_FIELD_CONTEXT (decl) = rli->t;
4121 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4122 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4123 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4124 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4126 if (!integer_zerop (DECL_SIZE (decl)))
4128 /* The containing class is non-empty because it has a non-empty
4129 base class. */
4130 *empty_p = 0;
4132 /* Try to place the field. It may take more than one try if we
4133 have a hard time placing the field without putting two
4134 objects of the same type at the same address. */
4135 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t);
4137 else
4139 unsigned HOST_WIDE_INT eoc;
4141 /* On some platforms (ARM), even empty classes will not be
4142 byte-aligned. */
4143 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
4144 eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
4145 atend |= layout_empty_base (binfo, size_int (eoc), offsets, t);
4148 /* Record the offsets of BINFO and its base subobjects. */
4149 record_subobject_offsets (BINFO_TYPE (binfo),
4150 BINFO_OFFSET (binfo),
4151 offsets,
4152 /*vbases_p=*/0);
4153 return atend;
4156 /* Layout all of the non-virtual base classes. Record empty
4157 subobjects in OFFSETS. T is the most derived type. Return
4158 non-zero if the type cannot be nearly empty. */
4160 static bool
4161 build_base_fields (rli, empty_p, offsets, t)
4162 record_layout_info rli;
4163 int *empty_p;
4164 splay_tree offsets;
4165 tree t;
4167 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4168 subobjects. */
4169 tree rec = rli->t;
4170 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
4171 int i;
4172 bool atend = 0;
4174 /* Under the new ABI, the primary base class is always allocated
4175 first. */
4176 if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
4177 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
4178 empty_p, offsets, t);
4180 /* Now allocate the rest of the bases. */
4181 for (i = 0; i < n_baseclasses; ++i)
4183 tree base_binfo;
4185 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4187 /* Under the new ABI, the primary base was already allocated
4188 above, so we don't need to allocate it again here. */
4189 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
4190 continue;
4192 /* A primary virtual base class is allocated just like any other
4193 base class, but a non-primary virtual base is allocated
4194 later, in layout_virtual_bases. */
4195 if (TREE_VIA_VIRTUAL (base_binfo)
4196 && !BINFO_PRIMARY_P (base_binfo))
4197 continue;
4199 atend |= build_base_field (rli, base_binfo, empty_p, offsets, t);
4201 return atend;
4204 /* Go through the TYPE_METHODS of T issuing any appropriate
4205 diagnostics, figuring out which methods override which other
4206 methods, and so forth. */
4208 static void
4209 check_methods (t)
4210 tree t;
4212 tree x;
4213 int seen_one_arg_array_delete_p = 0;
4215 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4217 GNU_xref_member (current_class_name, x);
4219 /* If this was an evil function, don't keep it in class. */
4220 if (DECL_ASSEMBLER_NAME_SET_P (x)
4221 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4222 continue;
4224 check_for_override (x, t);
4225 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4226 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4228 /* The name of the field is the original field name
4229 Save this in auxiliary field for later overloading. */
4230 if (DECL_VINDEX (x))
4232 TYPE_POLYMORPHIC_P (t) = 1;
4233 if (DECL_PURE_VIRTUAL_P (x))
4234 CLASSTYPE_PURE_VIRTUALS (t)
4235 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4238 if (DECL_ARRAY_DELETE_OPERATOR_P (x))
4240 tree second_parm;
4242 /* When dynamically allocating an array of this type, we
4243 need a "cookie" to record how many elements we allocated,
4244 even if the array elements have no non-trivial
4245 destructor, if the usual array deallocation function
4246 takes a second argument of type size_t. The standard (in
4247 [class.free]) requires that the second argument be set
4248 correctly. */
4249 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
4250 /* Under the new ABI, we choose only those function that are
4251 explicitly declared as `operator delete[] (void *,
4252 size_t)'. */
4253 if (!seen_one_arg_array_delete_p
4254 && second_parm
4255 && TREE_CHAIN (second_parm) == void_list_node
4256 && same_type_p (TREE_VALUE (second_parm), sizetype))
4257 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4258 /* If there's no second parameter, then this is the usual
4259 deallocation function. */
4260 else if (second_parm == void_list_node)
4261 seen_one_arg_array_delete_p = 1;
4266 /* FN is a constructor or destructor. Clone the declaration to create
4267 a specialized in-charge or not-in-charge version, as indicated by
4268 NAME. */
4270 static tree
4271 build_clone (fn, name)
4272 tree fn;
4273 tree name;
4275 tree parms;
4276 tree clone;
4278 /* Copy the function. */
4279 clone = copy_decl (fn);
4280 /* Remember where this function came from. */
4281 DECL_CLONED_FUNCTION (clone) = fn;
4282 DECL_ABSTRACT_ORIGIN (clone) = fn;
4283 /* Reset the function name. */
4284 DECL_NAME (clone) = name;
4285 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4286 /* There's no pending inline data for this function. */
4287 DECL_PENDING_INLINE_INFO (clone) = NULL;
4288 DECL_PENDING_INLINE_P (clone) = 0;
4289 /* And it hasn't yet been deferred. */
4290 DECL_DEFERRED_FN (clone) = 0;
4292 /* The base-class destructor is not virtual. */
4293 if (name == base_dtor_identifier)
4295 DECL_VIRTUAL_P (clone) = 0;
4296 if (TREE_CODE (clone) != TEMPLATE_DECL)
4297 DECL_VINDEX (clone) = NULL_TREE;
4300 /* If there was an in-charge parameter, drop it from the function
4301 type. */
4302 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4304 tree basetype;
4305 tree parmtypes;
4306 tree exceptions;
4308 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4309 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4310 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4311 /* Skip the `this' parameter. */
4312 parmtypes = TREE_CHAIN (parmtypes);
4313 /* Skip the in-charge parameter. */
4314 parmtypes = TREE_CHAIN (parmtypes);
4315 /* And the VTT parm, in a complete [cd]tor. */
4316 if (DECL_HAS_VTT_PARM_P (fn)
4317 && ! DECL_NEEDS_VTT_PARM_P (clone))
4318 parmtypes = TREE_CHAIN (parmtypes);
4319 /* If this is subobject constructor or destructor, add the vtt
4320 parameter. */
4321 TREE_TYPE (clone)
4322 = build_cplus_method_type (basetype,
4323 TREE_TYPE (TREE_TYPE (clone)),
4324 parmtypes);
4325 if (exceptions)
4326 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4327 exceptions);
4330 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
4331 aren't function parameters; those are the template parameters. */
4332 if (TREE_CODE (clone) != TEMPLATE_DECL)
4334 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4335 /* Remove the in-charge parameter. */
4336 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4338 TREE_CHAIN (DECL_ARGUMENTS (clone))
4339 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4340 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4342 /* And the VTT parm, in a complete [cd]tor. */
4343 if (DECL_HAS_VTT_PARM_P (fn))
4345 if (DECL_NEEDS_VTT_PARM_P (clone))
4346 DECL_HAS_VTT_PARM_P (clone) = 1;
4347 else
4349 TREE_CHAIN (DECL_ARGUMENTS (clone))
4350 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4351 DECL_HAS_VTT_PARM_P (clone) = 0;
4355 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4357 DECL_CONTEXT (parms) = clone;
4358 copy_lang_decl (parms);
4362 /* Create the RTL for this function. */
4363 SET_DECL_RTL (clone, NULL_RTX);
4364 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4366 /* Make it easy to find the CLONE given the FN. */
4367 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4368 TREE_CHAIN (fn) = clone;
4370 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4371 if (TREE_CODE (clone) == TEMPLATE_DECL)
4373 tree result;
4375 DECL_TEMPLATE_RESULT (clone)
4376 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4377 result = DECL_TEMPLATE_RESULT (clone);
4378 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4379 DECL_TI_TEMPLATE (result) = clone;
4381 else if (DECL_DEFERRED_FN (fn))
4382 defer_fn (clone);
4384 return clone;
4387 /* Produce declarations for all appropriate clones of FN. If
4388 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4389 CLASTYPE_METHOD_VEC as well. */
4391 void
4392 clone_function_decl (fn, update_method_vec_p)
4393 tree fn;
4394 int update_method_vec_p;
4396 tree clone;
4398 /* Avoid inappropriate cloning. */
4399 if (TREE_CHAIN (fn)
4400 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4401 return;
4403 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4405 /* For each constructor, we need two variants: an in-charge version
4406 and a not-in-charge version. */
4407 clone = build_clone (fn, complete_ctor_identifier);
4408 if (update_method_vec_p)
4409 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4410 clone = build_clone (fn, base_ctor_identifier);
4411 if (update_method_vec_p)
4412 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4414 else
4416 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4418 /* For each destructor, we need three variants: an in-charge
4419 version, a not-in-charge version, and an in-charge deleting
4420 version. We clone the deleting version first because that
4421 means it will go second on the TYPE_METHODS list -- and that
4422 corresponds to the correct layout order in the virtual
4423 function table.
4425 For a non-virtual destructor, we do not build a deleting
4426 destructor. */
4427 if (DECL_VIRTUAL_P (fn))
4429 clone = build_clone (fn, deleting_dtor_identifier);
4430 if (update_method_vec_p)
4431 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4433 clone = build_clone (fn, complete_dtor_identifier);
4434 if (update_method_vec_p)
4435 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4436 clone = build_clone (fn, base_dtor_identifier);
4437 if (update_method_vec_p)
4438 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4441 /* Note that this is an abstract function that is never emitted. */
4442 DECL_ABSTRACT (fn) = 1;
4445 /* DECL is an in charge constructor, which is being defined. This will
4446 have had an in class declaration, from whence clones were
4447 declared. An out-of-class definition can specify additional default
4448 arguments. As it is the clones that are involved in overload
4449 resolution, we must propagate the information from the DECL to its
4450 clones. */
4452 void
4453 adjust_clone_args (decl)
4454 tree decl;
4456 tree clone;
4458 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4459 clone = TREE_CHAIN (clone))
4461 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4462 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4463 tree decl_parms, clone_parms;
4465 clone_parms = orig_clone_parms;
4467 /* Skip the 'this' parameter. */
4468 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4469 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4471 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4472 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4473 if (DECL_HAS_VTT_PARM_P (decl))
4474 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4476 clone_parms = orig_clone_parms;
4477 if (DECL_HAS_VTT_PARM_P (clone))
4478 clone_parms = TREE_CHAIN (clone_parms);
4480 for (decl_parms = orig_decl_parms; decl_parms;
4481 decl_parms = TREE_CHAIN (decl_parms),
4482 clone_parms = TREE_CHAIN (clone_parms))
4484 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4485 TREE_TYPE (clone_parms)), 20010424);
4487 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4489 /* A default parameter has been added. Adjust the
4490 clone's parameters. */
4491 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4492 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4493 tree type;
4495 clone_parms = orig_decl_parms;
4497 if (DECL_HAS_VTT_PARM_P (clone))
4499 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4500 TREE_VALUE (orig_clone_parms),
4501 clone_parms);
4502 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4504 type = build_cplus_method_type (basetype,
4505 TREE_TYPE (TREE_TYPE (clone)),
4506 clone_parms);
4507 if (exceptions)
4508 type = build_exception_variant (type, exceptions);
4509 TREE_TYPE (clone) = type;
4511 clone_parms = NULL_TREE;
4512 break;
4515 my_friendly_assert (!clone_parms, 20010424);
4519 /* For each of the constructors and destructors in T, create an
4520 in-charge and not-in-charge variant. */
4522 static void
4523 clone_constructors_and_destructors (t)
4524 tree t;
4526 tree fns;
4528 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4529 out now. */
4530 if (!CLASSTYPE_METHOD_VEC (t))
4531 return;
4533 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4534 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4535 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4536 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4539 /* Remove all zero-width bit-fields from T. */
4541 static void
4542 remove_zero_width_bit_fields (t)
4543 tree t;
4545 tree *fieldsp;
4547 fieldsp = &TYPE_FIELDS (t);
4548 while (*fieldsp)
4550 if (TREE_CODE (*fieldsp) == FIELD_DECL
4551 && DECL_C_BIT_FIELD (*fieldsp)
4552 && DECL_INITIAL (*fieldsp))
4553 *fieldsp = TREE_CHAIN (*fieldsp);
4554 else
4555 fieldsp = &TREE_CHAIN (*fieldsp);
4559 /* Check the validity of the bases and members declared in T. Add any
4560 implicitly-generated functions (like copy-constructors and
4561 assignment operators). Compute various flag bits (like
4562 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4563 level: i.e., independently of the ABI in use. */
4565 static void
4566 check_bases_and_members (t, empty_p)
4567 tree t;
4568 int *empty_p;
4570 /* Nonzero if we are not allowed to generate a default constructor
4571 for this case. */
4572 int cant_have_default_ctor;
4573 /* Nonzero if the implicitly generated copy constructor should take
4574 a non-const reference argument. */
4575 int cant_have_const_ctor;
4576 /* Nonzero if the the implicitly generated assignment operator
4577 should take a non-const reference argument. */
4578 int no_const_asn_ref;
4579 tree access_decls;
4581 /* By default, we use const reference arguments and generate default
4582 constructors. */
4583 cant_have_default_ctor = 0;
4584 cant_have_const_ctor = 0;
4585 no_const_asn_ref = 0;
4587 /* Assume that the class is nearly empty; we'll clear this flag if
4588 it turns out not to be nearly empty. */
4589 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4591 /* Check all the base-classes. */
4592 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4593 &no_const_asn_ref);
4595 /* Check all the data member declarations. */
4596 check_field_decls (t, &access_decls, empty_p,
4597 &cant_have_default_ctor,
4598 &cant_have_const_ctor,
4599 &no_const_asn_ref);
4601 /* Check all the method declarations. */
4602 check_methods (t);
4604 /* A nearly-empty class has to be vptr-containing; a nearly empty
4605 class contains just a vptr. */
4606 if (!TYPE_CONTAINS_VPTR_P (t))
4607 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4609 /* Do some bookkeeping that will guide the generation of implicitly
4610 declared member functions. */
4611 TYPE_HAS_COMPLEX_INIT_REF (t)
4612 |= (TYPE_HAS_INIT_REF (t)
4613 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4614 || TYPE_POLYMORPHIC_P (t));
4615 TYPE_NEEDS_CONSTRUCTING (t)
4616 |= (TYPE_HAS_CONSTRUCTOR (t)
4617 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4618 || TYPE_POLYMORPHIC_P (t));
4619 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4620 || TYPE_POLYMORPHIC_P (t));
4621 CLASSTYPE_NON_POD_P (t)
4622 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4623 || TYPE_HAS_ASSIGN_REF (t));
4624 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4625 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4626 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4628 /* Synthesize any needed methods. Note that methods will be synthesized
4629 for anonymous unions; grok_x_components undoes that. */
4630 add_implicitly_declared_members (t, cant_have_default_ctor,
4631 cant_have_const_ctor,
4632 no_const_asn_ref);
4634 /* Create the in-charge and not-in-charge variants of constructors
4635 and destructors. */
4636 clone_constructors_and_destructors (t);
4638 /* Process the using-declarations. */
4639 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4640 handle_using_decl (TREE_VALUE (access_decls), t);
4642 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4643 finish_struct_methods (t);
4646 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4647 accordingly. If a new vfield was created (because T doesn't have a
4648 primary base class), then the newly created field is returned. It
4649 is not added to the TYPE_FIELDS list; it is the caller's
4650 responsibility to do that. */
4652 static tree
4653 create_vtable_ptr (t, empty_p, vfuns_p,
4654 new_virtuals_p, overridden_virtuals_p)
4655 tree t;
4656 int *empty_p;
4657 int *vfuns_p;
4658 tree *new_virtuals_p;
4659 tree *overridden_virtuals_p;
4661 tree fn;
4663 /* Loop over the virtual functions, adding them to our various
4664 vtables. */
4665 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4666 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
4667 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4668 vfuns_p, fn, t);
4670 /* If we couldn't find an appropriate base class, create a new field
4671 here. Even if there weren't any new virtual functions, we might need a
4672 new virtual function table if we're supposed to include vptrs in
4673 all classes that need them. */
4674 if (!TYPE_VFIELD (t)
4675 && (*vfuns_p
4676 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())))
4678 /* We build this decl with vtbl_ptr_type_node, which is a
4679 `vtable_entry_type*'. It might seem more precise to use
4680 `vtable_entry_type (*)[N]' where N is the number of firtual
4681 functions. However, that would require the vtable pointer in
4682 base classes to have a different type than the vtable pointer
4683 in derived classes. We could make that happen, but that
4684 still wouldn't solve all the problems. In particular, the
4685 type-based alias analysis code would decide that assignments
4686 to the base class vtable pointer can't alias assignments to
4687 the derived class vtable pointer, since they have different
4688 types. Thus, in an derived class destructor, where the base
4689 class constructor was inlined, we could generate bad code for
4690 setting up the vtable pointer.
4692 Therefore, we use one type for all vtable pointers. We still
4693 use a type-correct type; it's just doesn't indicate the array
4694 bounds. That's better than using `void*' or some such; it's
4695 cleaner, and it let's the alias analysis code know that these
4696 stores cannot alias stores to void*! */
4697 TYPE_VFIELD (t)
4698 = build_vtbl_or_vbase_field (get_vfield_name (t),
4699 get_identifier (VFIELD_BASE),
4700 vtbl_ptr_type_node,
4703 empty_p);
4705 if (CLASSTYPE_N_BASECLASSES (t))
4706 /* If there were any baseclasses, they can't possibly be at
4707 offset zero any more, because that's where the vtable
4708 pointer is. So, converting to a base class is going to
4709 take work. */
4710 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4712 return TYPE_VFIELD (t);
4715 return NULL_TREE;
4718 /* Fixup the inline function given by INFO now that the class is
4719 complete. */
4721 static void
4722 fixup_pending_inline (fn)
4723 tree fn;
4725 if (DECL_PENDING_INLINE_INFO (fn))
4727 tree args = DECL_ARGUMENTS (fn);
4728 while (args)
4730 DECL_CONTEXT (args) = fn;
4731 args = TREE_CHAIN (args);
4736 /* Fixup the inline methods and friends in TYPE now that TYPE is
4737 complete. */
4739 static void
4740 fixup_inline_methods (type)
4741 tree type;
4743 tree method = TYPE_METHODS (type);
4745 if (method && TREE_CODE (method) == TREE_VEC)
4747 if (TREE_VEC_ELT (method, 1))
4748 method = TREE_VEC_ELT (method, 1);
4749 else if (TREE_VEC_ELT (method, 0))
4750 method = TREE_VEC_ELT (method, 0);
4751 else
4752 method = TREE_VEC_ELT (method, 2);
4755 /* Do inline member functions. */
4756 for (; method; method = TREE_CHAIN (method))
4757 fixup_pending_inline (method);
4759 /* Do friends. */
4760 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4761 method;
4762 method = TREE_CHAIN (method))
4763 fixup_pending_inline (TREE_VALUE (method));
4764 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4767 /* Add OFFSET to all base types of BINFO which is a base in the
4768 hierarchy dominated by T.
4770 OFFSET, which is a type offset, is number of bytes. */
4772 static void
4773 propagate_binfo_offsets (binfo, offset, t)
4774 tree binfo;
4775 tree offset;
4776 tree t;
4778 int i;
4779 tree primary_binfo;
4781 /* Update BINFO's offset. */
4782 BINFO_OFFSET (binfo)
4783 = convert (sizetype,
4784 size_binop (PLUS_EXPR,
4785 convert (ssizetype, BINFO_OFFSET (binfo)),
4786 offset));
4788 /* Find the primary base class. */
4789 primary_binfo = get_primary_binfo (binfo);
4791 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4792 downwards. */
4793 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4795 tree base_binfo;
4797 /* On the first time through the loop, do the primary base.
4798 Because the primary base need not be an immediate base, we
4799 must handle the primary base specially. */
4800 if (i == -1)
4802 if (!primary_binfo)
4803 continue;
4805 base_binfo = primary_binfo;
4807 else
4809 base_binfo = BINFO_BASETYPE (binfo, i);
4810 /* Don't do the primary base twice. */
4811 if (base_binfo == primary_binfo)
4812 continue;
4815 /* Skip virtual bases that aren't our canonical primary base. */
4816 if (TREE_VIA_VIRTUAL (base_binfo)
4817 && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4818 || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4819 continue;
4821 propagate_binfo_offsets (base_binfo, offset, t);
4825 /* Called via dfs_walk from layout_virtual bases. */
4827 static tree
4828 dfs_set_offset_for_unshared_vbases (binfo, data)
4829 tree binfo;
4830 void *data;
4832 /* If this is a virtual base, make sure it has the same offset as
4833 the shared copy. If it's a primary base, then we know it's
4834 correct. */
4835 if (TREE_VIA_VIRTUAL (binfo))
4837 tree t = (tree) data;
4838 tree vbase;
4839 tree offset;
4841 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4842 if (vbase != binfo)
4844 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4845 propagate_binfo_offsets (binfo, offset, t);
4849 return NULL_TREE;
4852 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4853 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4854 empty subobjects of T. */
4856 static void
4857 layout_virtual_bases (t, offsets)
4858 tree t;
4859 splay_tree offsets;
4861 tree vbases;
4862 unsigned HOST_WIDE_INT dsize;
4863 unsigned HOST_WIDE_INT eoc;
4865 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4866 return;
4868 #ifdef STRUCTURE_SIZE_BOUNDARY
4869 /* Packed structures don't need to have minimum size. */
4870 if (! TYPE_PACKED (t))
4871 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4872 #endif
4874 /* DSIZE is the size of the class without the virtual bases. */
4875 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4877 /* Make every class have alignment of at least one. */
4878 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4880 /* Go through the virtual bases, allocating space for each virtual
4881 base that is not already a primary base class. Under the old
4882 ABI, these are allocated according to a depth-first left-to-right
4883 postorder traversal; in the new ABI, inheritance graph order is
4884 used instead. */
4885 for (vbases = TYPE_BINFO (t);
4886 vbases;
4887 vbases = TREE_CHAIN (vbases))
4889 tree vbase;
4891 if (!TREE_VIA_VIRTUAL (vbases))
4892 continue;
4893 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4895 if (!BINFO_PRIMARY_P (vbase))
4897 /* This virtual base is not a primary base of any class in the
4898 hierarchy, so we have to add space for it. */
4899 tree basetype;
4900 unsigned int desired_align;
4902 basetype = BINFO_TYPE (vbase);
4904 desired_align = CLASSTYPE_ALIGN (basetype);
4905 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4907 /* Add padding so that we can put the virtual base class at an
4908 appropriately aligned offset. */
4909 dsize = CEIL (dsize, desired_align) * desired_align;
4911 /* Under the new ABI, we try to squish empty virtual bases in
4912 just like ordinary empty bases. */
4913 if (is_empty_class (basetype))
4914 layout_empty_base (vbase,
4915 size_int (CEIL (dsize, BITS_PER_UNIT)),
4916 offsets, t);
4917 else
4919 tree offset;
4921 offset = ssize_int (CEIL (dsize, BITS_PER_UNIT));
4922 offset = size_diffop (offset,
4923 convert (ssizetype,
4924 BINFO_OFFSET (vbase)));
4926 /* And compute the offset of the virtual base. */
4927 propagate_binfo_offsets (vbase, offset, t);
4928 /* Every virtual baseclass takes a least a UNIT, so that
4929 we can take it's address and get something different
4930 for each base. */
4931 dsize += MAX (BITS_PER_UNIT,
4932 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4935 /* Keep track of the offsets assigned to this virtual base. */
4936 record_subobject_offsets (BINFO_TYPE (vbase),
4937 BINFO_OFFSET (vbase),
4938 offsets,
4939 /*vbases_p=*/0);
4943 /* Now, go through the TYPE_BINFO hierarchy, setting the
4944 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4945 bases and their direct and indirect bases. The ambiguity checks
4946 in get_base_distance depend on the BINFO_OFFSETs being set
4947 correctly. */
4948 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4950 /* If we had empty base classes that protruded beyond the end of the
4951 class, we didn't update DSIZE above; we were hoping to overlay
4952 multiple such bases at the same location. */
4953 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4954 if (eoc * BITS_PER_UNIT > dsize)
4955 dsize = eoc * BITS_PER_UNIT;
4957 /* Now, make sure that the total size of the type is a multiple of
4958 its alignment. */
4959 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4960 TYPE_SIZE (t) = bitsize_int (dsize);
4961 TYPE_SIZE_UNIT (t) = convert (sizetype,
4962 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4963 bitsize_unit_node));
4965 /* Check for ambiguous virtual bases. */
4966 if (extra_warnings)
4967 for (vbases = CLASSTYPE_VBASECLASSES (t);
4968 vbases;
4969 vbases = TREE_CHAIN (vbases))
4971 tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
4972 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4973 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4974 basetype, t);
4978 /* Returns the offset of the byte just past the end of the base class
4979 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4980 only non-virtual bases are included. */
4982 static unsigned HOST_WIDE_INT
4983 end_of_class (t, include_virtuals_p)
4984 tree t;
4985 int include_virtuals_p;
4987 unsigned HOST_WIDE_INT result = 0;
4988 int i;
4990 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4992 tree base_binfo;
4993 tree offset;
4994 tree size;
4995 unsigned HOST_WIDE_INT end_of_base;
4997 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4999 if (!include_virtuals_p
5000 && TREE_VIA_VIRTUAL (base_binfo)
5001 && !BINFO_PRIMARY_P (base_binfo))
5002 continue;
5004 if (is_empty_class (BINFO_TYPE (base_binfo)))
5005 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5006 allocate some space for it. It cannot have virtual bases,
5007 so TYPE_SIZE_UNIT is fine. */
5008 size = TYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
5009 else
5010 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
5011 offset = size_binop (PLUS_EXPR,
5012 BINFO_OFFSET (base_binfo),
5013 size);
5014 end_of_base = tree_low_cst (offset, /*pos=*/1);
5015 if (end_of_base > result)
5016 result = end_of_base;
5019 return result;
5022 /* Warn about direct bases of T that are inaccessible because they are
5023 ambiguous. For example:
5025 struct S {};
5026 struct T : public S {};
5027 struct U : public S, public T {};
5029 Here, `(S*) new U' is not allowed because there are two `S'
5030 subobjects of U. */
5032 static void
5033 warn_about_ambiguous_direct_bases (t)
5034 tree t;
5036 int i;
5038 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
5040 tree basetype = TYPE_BINFO_BASETYPE (t, i);
5042 if (get_base_distance (basetype, t, 0, NULL) == -2)
5043 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
5044 basetype, t);
5048 /* Compare two INTEGER_CSTs K1 and K2. */
5050 static int
5051 splay_tree_compare_integer_csts (k1, k2)
5052 splay_tree_key k1;
5053 splay_tree_key k2;
5055 return tree_int_cst_compare ((tree) k1, (tree) k2);
5058 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5059 BINFO_OFFSETs for all of the base-classes. Position the vtable
5060 pointer. */
5062 static void
5063 layout_class_type (t, empty_p, vfuns_p,
5064 new_virtuals_p, overridden_virtuals_p)
5065 tree t;
5066 int *empty_p;
5067 int *vfuns_p;
5068 tree *new_virtuals_p;
5069 tree *overridden_virtuals_p;
5071 tree non_static_data_members;
5072 tree field;
5073 tree vptr;
5074 record_layout_info rli;
5075 unsigned HOST_WIDE_INT eoc;
5076 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5077 types that appear at that offset. */
5078 splay_tree empty_base_offsets;
5080 /* Keep track of the first non-static data member. */
5081 non_static_data_members = TYPE_FIELDS (t);
5083 /* Start laying out the record. */
5084 rli = start_record_layout (t);
5086 /* If possible, we reuse the virtual function table pointer from one
5087 of our base classes. */
5088 determine_primary_base (t, vfuns_p);
5090 /* Create a pointer to our virtual function table. */
5091 vptr = create_vtable_ptr (t, empty_p, vfuns_p,
5092 new_virtuals_p, overridden_virtuals_p);
5094 /* Under the new ABI, the vptr is always the first thing in the
5095 class. */
5096 if (vptr)
5098 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
5099 place_field (rli, vptr);
5102 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5103 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
5104 NULL, NULL);
5105 if (build_base_fields (rli, empty_p, empty_base_offsets, t))
5106 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5108 /* Add pointers to all of our virtual base-classes. */
5109 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (rli, empty_p),
5110 TYPE_FIELDS (t));
5112 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
5113 we have to save this before we start modifying
5114 TYPE_NONCOPIED_PARTS. */
5115 fixup_inline_methods (t);
5117 /* Layout the non-static data members. */
5118 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
5120 tree type;
5121 tree padding;
5123 /* We still pass things that aren't non-static data members to
5124 the back-end, in case it wants to do something with them. */
5125 if (TREE_CODE (field) != FIELD_DECL)
5127 place_field (rli, field);
5128 continue;
5131 type = TREE_TYPE (field);
5133 /* If this field is a bit-field whose width is greater than its
5134 type, then there are some special rules for allocating it
5135 under the new ABI. Under the old ABI, there were no special
5136 rules, but the back-end can't handle bitfields longer than a
5137 `long long', so we use the same mechanism. */
5138 if (DECL_C_BIT_FIELD (field)
5139 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5141 integer_type_kind itk;
5142 tree integer_type;
5144 /* We must allocate the bits as if suitably aligned for the
5145 longest integer type that fits in this many bits. type
5146 of the field. Then, we are supposed to use the left over
5147 bits as additional padding. */
5148 for (itk = itk_char; itk != itk_none; ++itk)
5149 if (INT_CST_LT (DECL_SIZE (field),
5150 TYPE_SIZE (integer_types[itk])))
5151 break;
5153 /* ITK now indicates a type that is too large for the
5154 field. We have to back up by one to find the largest
5155 type that fits. */
5156 integer_type = integer_types[itk - 1];
5157 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5158 TYPE_SIZE (integer_type));
5159 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5160 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5161 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5163 else
5164 padding = NULL_TREE;
5166 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5167 empty_base_offsets, t);
5169 /* If we needed additional padding after this field, add it
5170 now. */
5171 if (padding)
5173 tree padding_field;
5175 padding_field = build_decl (FIELD_DECL,
5176 NULL_TREE,
5177 char_type_node);
5178 DECL_BIT_FIELD (padding_field) = 1;
5179 DECL_SIZE (padding_field) = padding;
5180 DECL_ALIGN (padding_field) = 1;
5181 DECL_USER_ALIGN (padding_field) = 0;
5182 layout_nonempty_base_or_field (rli, padding_field,
5183 NULL_TREE,
5184 empty_base_offsets, t);
5188 /* It might be the case that we grew the class to allocate a
5189 zero-sized base class. That won't be reflected in RLI, yet,
5190 because we are willing to overlay multiple bases at the same
5191 offset. However, now we need to make sure that RLI is big enough
5192 to reflect the entire class. */
5193 eoc = end_of_class (t, /*include_virtuals_p=*/0);
5194 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
5195 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
5197 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc));
5198 rli->bitpos = bitsize_zero_node;
5201 /* We make all structures have at least one element, so that they
5202 have non-zero size. In the new ABI, the class may be empty even
5203 if it has basetypes. Therefore, we add the fake field after all
5204 the other fields; if there are already FIELD_DECLs on the list,
5205 their offsets will not be disturbed. */
5206 if (!eoc && *empty_p)
5208 tree padding;
5210 padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
5211 place_field (rli, padding);
5212 TYPE_NONCOPIED_PARTS (t)
5213 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
5214 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
5217 /* Let the back-end lay out the type. Note that at this point we
5218 have only included non-virtual base-classes; we will lay out the
5219 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
5220 this call are not necessarily correct; they are just the size and
5221 alignment when no virtual base clases are used. */
5222 finish_record_layout (rli);
5224 /* Delete all zero-width bit-fields from the list of fields. Now
5225 that the type is laid out they are no longer important. */
5226 remove_zero_width_bit_fields (t);
5228 /* Remember the size and alignment of the class before adding
5229 the virtual bases. */
5230 if (*empty_p)
5232 CLASSTYPE_SIZE (t) = bitsize_zero_node;
5233 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
5235 else
5237 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
5238 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
5241 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5242 CLASSTYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (t);
5244 /* Set the TYPE_DECL for this type to contain the right
5245 value for DECL_OFFSET, so that we can use it as part
5246 of a COMPONENT_REF for multiple inheritance. */
5247 layout_decl (TYPE_MAIN_DECL (t), 0);
5249 /* Now fix up any virtual base class types that we left lying
5250 around. We must get these done before we try to lay out the
5251 virtual function table. As a side-effect, this will remove the
5252 base subobject fields. */
5253 layout_virtual_bases (t, empty_base_offsets);
5255 /* Warn about direct bases that can't be talked about due to
5256 ambiguity. */
5257 warn_about_ambiguous_direct_bases (t);
5259 /* Clean up. */
5260 splay_tree_delete (empty_base_offsets);
5263 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5264 (or C++ class declaration).
5266 For C++, we must handle the building of derived classes.
5267 Also, C++ allows static class members. The way that this is
5268 handled is to keep the field name where it is (as the DECL_NAME
5269 of the field), and place the overloaded decl in the bit position
5270 of the field. layout_record and layout_union will know about this.
5272 More C++ hair: inline functions have text in their
5273 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5274 meaningful tree structure. After the struct has been laid out, set
5275 things up so that this can happen.
5277 And still more: virtual functions. In the case of single inheritance,
5278 when a new virtual function is seen which redefines a virtual function
5279 from the base class, the new virtual function is placed into
5280 the virtual function table at exactly the same address that
5281 it had in the base class. When this is extended to multiple
5282 inheritance, the same thing happens, except that multiple virtual
5283 function tables must be maintained. The first virtual function
5284 table is treated in exactly the same way as in the case of single
5285 inheritance. Additional virtual function tables have different
5286 DELTAs, which tell how to adjust `this' to point to the right thing.
5288 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5290 void
5291 finish_struct_1 (t)
5292 tree t;
5294 tree x;
5295 int vfuns;
5296 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5297 a FUNCTION_DECL. Each of these functions is a virtual function
5298 declared in T that does not override any virtual function from a
5299 base class. */
5300 tree new_virtuals = NULL_TREE;
5301 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5302 except that each declaration here overrides the declaration from
5303 a base class. */
5304 tree overridden_virtuals = NULL_TREE;
5305 int n_fields = 0;
5306 tree vfield;
5307 int empty = 1;
5309 if (COMPLETE_TYPE_P (t))
5311 if (IS_AGGR_TYPE (t))
5312 cp_error ("redefinition of `%#T'", t);
5313 else
5314 my_friendly_abort (172);
5315 popclass ();
5316 return;
5319 GNU_xref_decl (current_function_decl, t);
5321 /* If this type was previously laid out as a forward reference,
5322 make sure we lay it out again. */
5323 TYPE_SIZE (t) = NULL_TREE;
5324 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5325 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5326 vfuns = 0;
5327 CLASSTYPE_RTTI (t) = NULL_TREE;
5329 /* Do end-of-class semantic processing: checking the validity of the
5330 bases and members and add implicitly generated methods. */
5331 check_bases_and_members (t, &empty);
5333 /* Layout the class itself. */
5334 layout_class_type (t, &empty, &vfuns,
5335 &new_virtuals, &overridden_virtuals);
5337 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5338 vfield = TYPE_VFIELD (t);
5339 if (vfield != NULL_TREE
5340 && DECL_FIELD_CONTEXT (vfield) != t)
5342 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
5344 vfield = copy_decl (vfield);
5346 DECL_FIELD_CONTEXT (vfield) = t;
5347 DECL_FIELD_OFFSET (vfield)
5348 = size_binop (PLUS_EXPR,
5349 BINFO_OFFSET (binfo),
5350 DECL_FIELD_OFFSET (vfield));
5351 TYPE_VFIELD (t) = vfield;
5354 overridden_virtuals
5355 = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
5357 /* If we created a new vtbl pointer for this class, add it to the
5358 list. */
5359 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5360 CLASSTYPE_VFIELDS (t)
5361 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5363 /* If necessary, create the primary vtable for this class. */
5364 if (new_virtuals
5365 || overridden_virtuals
5366 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
5368 new_virtuals = nreverse (new_virtuals);
5369 /* We must enter these virtuals into the table. */
5370 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5371 build_primary_vtable (NULL_TREE, t);
5372 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5373 /* Here we know enough to change the type of our virtual
5374 function table, but we will wait until later this function. */
5375 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5377 /* If this type has basetypes with constructors, then those
5378 constructors might clobber the virtual function table. But
5379 they don't if the derived class shares the exact vtable of the base
5380 class. */
5382 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5384 /* If we didn't need a new vtable, see if we should copy one from
5385 the base. */
5386 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5388 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5390 /* If this class uses a different vtable than its primary base
5391 then when we will need to initialize our vptr after the base
5392 class constructor runs. */
5393 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5394 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5397 if (TYPE_CONTAINS_VPTR_P (t))
5399 if (TYPE_BINFO_VTABLE (t))
5400 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5401 20000116);
5402 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5403 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5404 20000116);
5406 CLASSTYPE_VSIZE (t) = vfuns;
5407 /* Entries for virtual functions defined in the primary base are
5408 followed by entries for new functions unique to this class. */
5409 TYPE_BINFO_VIRTUALS (t)
5410 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5411 /* Finally, add entries for functions that override virtuals
5412 from non-primary bases. */
5413 TYPE_BINFO_VIRTUALS (t)
5414 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5417 finish_struct_bits (t);
5419 /* Complete the rtl for any static member objects of the type we're
5420 working on. */
5421 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5422 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5423 && TREE_TYPE (x) == t)
5424 DECL_MODE (x) = TYPE_MODE (t);
5426 /* Done with FIELDS...now decide whether to sort these for
5427 faster lookups later.
5429 The C front-end only does this when n_fields > 15. We use
5430 a smaller number because most searches fail (succeeding
5431 ultimately as the search bores through the inheritance
5432 hierarchy), and we want this failure to occur quickly. */
5434 n_fields = count_fields (TYPE_FIELDS (t));
5435 if (n_fields > 7)
5437 tree field_vec = make_tree_vec (n_fields);
5438 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5439 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5440 (int (*)(const void *, const void *))field_decl_cmp);
5441 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5442 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5443 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5446 if (TYPE_HAS_CONSTRUCTOR (t))
5448 tree vfields = CLASSTYPE_VFIELDS (t);
5450 while (vfields)
5452 /* Mark the fact that constructor for T
5453 could affect anybody inheriting from T
5454 who wants to initialize vtables for VFIELDS's type. */
5455 if (VF_DERIVED_VALUE (vfields))
5456 TREE_ADDRESSABLE (vfields) = 1;
5457 vfields = TREE_CHAIN (vfields);
5461 /* Make the rtl for any new vtables we have created, and unmark
5462 the base types we marked. */
5463 finish_vtbls (t);
5465 /* Build the VTT for T. */
5466 build_vtt (t);
5468 if (TYPE_VFIELD (t))
5470 /* In addition to this one, all the other vfields should be listed. */
5471 /* Before that can be done, we have to have FIELD_DECLs for them, and
5472 a place to find them. */
5473 TYPE_NONCOPIED_PARTS (t)
5474 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5475 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5477 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5478 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5479 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5483 hack_incomplete_structures (t);
5485 if (warn_overloaded_virtual)
5486 warn_hidden (t);
5488 maybe_suppress_debug_info (t);
5490 dump_class_hierarchy (t);
5492 /* Finish debugging output for this type. */
5493 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5496 /* When T was built up, the member declarations were added in reverse
5497 order. Rearrange them to declaration order. */
5499 void
5500 unreverse_member_declarations (t)
5501 tree t;
5503 tree next;
5504 tree prev;
5505 tree x;
5507 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5508 reverse order. Put them in declaration order now. */
5509 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5510 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5512 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5513 reverse order, so we can't just use nreverse. */
5514 prev = NULL_TREE;
5515 for (x = TYPE_FIELDS (t);
5516 x && TREE_CODE (x) != TYPE_DECL;
5517 x = next)
5519 next = TREE_CHAIN (x);
5520 TREE_CHAIN (x) = prev;
5521 prev = x;
5523 if (prev)
5525 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5526 if (prev)
5527 TYPE_FIELDS (t) = prev;
5531 tree
5532 finish_struct (t, attributes)
5533 tree t, attributes;
5535 const char *saved_filename = input_filename;
5536 int saved_lineno = lineno;
5538 /* Now that we've got all the field declarations, reverse everything
5539 as necessary. */
5540 unreverse_member_declarations (t);
5542 cplus_decl_attributes (&t, attributes, NULL_TREE, 0);
5544 /* Nadger the current location so that diagnostics point to the start of
5545 the struct, not the end. */
5546 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5547 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5549 if (processing_template_decl)
5551 finish_struct_methods (t);
5552 TYPE_SIZE (t) = bitsize_zero_node;
5554 else
5555 finish_struct_1 (t);
5557 input_filename = saved_filename;
5558 lineno = saved_lineno;
5560 TYPE_BEING_DEFINED (t) = 0;
5562 if (current_class_type)
5563 popclass ();
5564 else
5565 error ("trying to finish struct, but kicked out due to previous parse errors.");
5567 if (processing_template_decl)
5569 tree scope = current_scope ();
5570 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5571 add_stmt (build_min (TAG_DEFN, t));
5574 return t;
5577 /* Return the dynamic type of INSTANCE, if known.
5578 Used to determine whether the virtual function table is needed
5579 or not.
5581 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5582 of our knowledge of its type. *NONNULL should be initialized
5583 before this function is called. */
5585 static tree
5586 fixed_type_or_null (instance, nonnull, cdtorp)
5587 tree instance;
5588 int *nonnull;
5589 int *cdtorp;
5591 switch (TREE_CODE (instance))
5593 case INDIRECT_REF:
5594 /* Check that we are not going through a cast of some sort. */
5595 if (TREE_TYPE (instance)
5596 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5597 instance = TREE_OPERAND (instance, 0);
5598 /* fall through... */
5599 case CALL_EXPR:
5600 /* This is a call to a constructor, hence it's never zero. */
5601 if (TREE_HAS_CONSTRUCTOR (instance))
5603 if (nonnull)
5604 *nonnull = 1;
5605 return TREE_TYPE (instance);
5607 return NULL_TREE;
5609 case SAVE_EXPR:
5610 /* This is a call to a constructor, hence it's never zero. */
5611 if (TREE_HAS_CONSTRUCTOR (instance))
5613 if (nonnull)
5614 *nonnull = 1;
5615 return TREE_TYPE (instance);
5617 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5619 case RTL_EXPR:
5620 return NULL_TREE;
5622 case PLUS_EXPR:
5623 case MINUS_EXPR:
5624 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5625 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5626 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5627 /* Propagate nonnull. */
5628 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5629 return NULL_TREE;
5631 case NOP_EXPR:
5632 case CONVERT_EXPR:
5633 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5635 case ADDR_EXPR:
5636 if (nonnull)
5637 *nonnull = 1;
5638 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5640 case COMPONENT_REF:
5641 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5643 case VAR_DECL:
5644 case FIELD_DECL:
5645 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5646 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5648 if (nonnull)
5649 *nonnull = 1;
5650 return TREE_TYPE (TREE_TYPE (instance));
5652 /* fall through... */
5653 case TARGET_EXPR:
5654 case PARM_DECL:
5655 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5657 if (nonnull)
5658 *nonnull = 1;
5659 return TREE_TYPE (instance);
5661 else if (instance == current_class_ptr)
5663 if (nonnull)
5664 *nonnull = 1;
5666 /* if we're in a ctor or dtor, we know our type. */
5667 if (DECL_LANG_SPECIFIC (current_function_decl)
5668 && (DECL_CONSTRUCTOR_P (current_function_decl)
5669 || DECL_DESTRUCTOR_P (current_function_decl)))
5671 if (cdtorp)
5672 *cdtorp = 1;
5673 return TREE_TYPE (TREE_TYPE (instance));
5676 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5678 /* Reference variables should be references to objects. */
5679 if (nonnull)
5680 *nonnull = 1;
5682 return NULL_TREE;
5684 default:
5685 return NULL_TREE;
5689 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5690 to the static type. We also handle the case where INSTANCE is really
5691 a pointer. Return negative if this is a ctor/dtor. There the dynamic type
5692 is known, but this might not be the most derived base of the original object,
5693 and hence virtual bases may not be layed out according to this type.
5695 Used to determine whether the virtual function table is needed
5696 or not.
5698 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5699 of our knowledge of its type. *NONNULL should be initialized
5700 before this function is called. */
5703 resolves_to_fixed_type_p (instance, nonnull)
5704 tree instance;
5705 int *nonnull;
5707 tree t = TREE_TYPE (instance);
5708 int cdtorp = 0;
5710 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5711 if (fixed == NULL_TREE)
5712 return 0;
5713 if (POINTER_TYPE_P (t))
5714 t = TREE_TYPE (t);
5715 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5716 return 0;
5717 return cdtorp ? -1 : 1;
5721 void
5722 init_class_processing ()
5724 current_class_depth = 0;
5725 current_class_stack_size = 10;
5726 current_class_stack
5727 = (class_stack_node_t) xmalloc (current_class_stack_size
5728 * sizeof (struct class_stack_node));
5729 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5730 ggc_add_tree_varray_root (&local_classes, 1);
5732 access_default_node = build_int_2 (0, 0);
5733 access_public_node = build_int_2 (ak_public, 0);
5734 access_protected_node = build_int_2 (ak_protected, 0);
5735 access_private_node = build_int_2 (ak_private, 0);
5736 access_default_virtual_node = build_int_2 (4, 0);
5737 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5738 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5739 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5741 ridpointers[(int) RID_PUBLIC] = access_public_node;
5742 ridpointers[(int) RID_PRIVATE] = access_private_node;
5743 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5746 /* Set current scope to NAME. CODE tells us if this is a
5747 STRUCT, UNION, or ENUM environment.
5749 NAME may end up being NULL_TREE if this is an anonymous or
5750 late-bound struct (as in "struct { ... } foo;") */
5752 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5753 appropriate values, found by looking up the type definition of
5754 NAME (as a CODE).
5756 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5757 which can be seen locally to the class. They are shadowed by
5758 any subsequent local declaration (including parameter names).
5760 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5761 which have static meaning (i.e., static members, static
5762 member functions, enum declarations, etc).
5764 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5765 which can be seen locally to the class (as in 1), but
5766 know that we are doing this for declaration purposes
5767 (i.e. friend foo::bar (int)).
5769 So that we may avoid calls to lookup_name, we cache the _TYPE
5770 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5772 For multiple inheritance, we perform a two-pass depth-first search
5773 of the type lattice. The first pass performs a pre-order search,
5774 marking types after the type has had its fields installed in
5775 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5776 unmarks the marked types. If a field or member function name
5777 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5778 that name becomes `error_mark_node'. */
5780 void
5781 pushclass (type, modify)
5782 tree type;
5783 int modify;
5785 type = TYPE_MAIN_VARIANT (type);
5787 /* Make sure there is enough room for the new entry on the stack. */
5788 if (current_class_depth + 1 >= current_class_stack_size)
5790 current_class_stack_size *= 2;
5791 current_class_stack
5792 = (class_stack_node_t) xrealloc (current_class_stack,
5793 current_class_stack_size
5794 * sizeof (struct class_stack_node));
5797 /* Insert a new entry on the class stack. */
5798 current_class_stack[current_class_depth].name = current_class_name;
5799 current_class_stack[current_class_depth].type = current_class_type;
5800 current_class_stack[current_class_depth].access = current_access_specifier;
5801 current_class_stack[current_class_depth].names_used = 0;
5802 current_class_depth++;
5804 /* Now set up the new type. */
5805 current_class_name = TYPE_NAME (type);
5806 if (TREE_CODE (current_class_name) == TYPE_DECL)
5807 current_class_name = DECL_NAME (current_class_name);
5808 current_class_type = type;
5810 /* By default, things in classes are private, while things in
5811 structures or unions are public. */
5812 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5813 ? access_private_node
5814 : access_public_node);
5816 if (previous_class_type != NULL_TREE
5817 && (type != previous_class_type
5818 || !COMPLETE_TYPE_P (previous_class_type))
5819 && current_class_depth == 1)
5821 /* Forcibly remove any old class remnants. */
5822 invalidate_class_lookup_cache ();
5825 /* If we're about to enter a nested class, clear
5826 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5827 if (modify && current_class_depth > 1)
5828 clear_identifier_class_values ();
5830 pushlevel_class ();
5832 if (modify)
5834 if (type != previous_class_type || current_class_depth > 1)
5835 push_class_decls (type);
5836 else
5838 tree item;
5840 /* We are re-entering the same class we just left, so we
5841 don't have to search the whole inheritance matrix to find
5842 all the decls to bind again. Instead, we install the
5843 cached class_shadowed list, and walk through it binding
5844 names and setting up IDENTIFIER_TYPE_VALUEs. */
5845 set_class_shadows (previous_class_values);
5846 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5848 tree id = TREE_PURPOSE (item);
5849 tree decl = TREE_TYPE (item);
5851 push_class_binding (id, decl);
5852 if (TREE_CODE (decl) == TYPE_DECL)
5853 set_identifier_type_value (id, TREE_TYPE (decl));
5855 unuse_fields (type);
5858 storetags (CLASSTYPE_TAGS (type));
5862 /* When we exit a toplevel class scope, we save the
5863 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5864 reenter the class. Here, we've entered some other class, so we
5865 must invalidate our cache. */
5867 void
5868 invalidate_class_lookup_cache ()
5870 tree t;
5872 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5873 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5874 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5876 previous_class_values = NULL_TREE;
5877 previous_class_type = NULL_TREE;
5880 /* Get out of the current class scope. If we were in a class scope
5881 previously, that is the one popped to. */
5883 void
5884 popclass ()
5886 poplevel_class ();
5887 /* Since poplevel_class does the popping of class decls nowadays,
5888 this really only frees the obstack used for these decls. */
5889 pop_class_decls ();
5891 current_class_depth--;
5892 current_class_name = current_class_stack[current_class_depth].name;
5893 current_class_type = current_class_stack[current_class_depth].type;
5894 current_access_specifier = current_class_stack[current_class_depth].access;
5895 if (current_class_stack[current_class_depth].names_used)
5896 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5899 /* Returns 1 if current_class_type is either T or a nested type of T.
5900 We start looking from 1 because entry 0 is from global scope, and has
5901 no type. */
5904 currently_open_class (t)
5905 tree t;
5907 int i;
5908 if (t == current_class_type)
5909 return 1;
5910 for (i = 1; i < current_class_depth; ++i)
5911 if (current_class_stack [i].type == t)
5912 return 1;
5913 return 0;
5916 /* If either current_class_type or one of its enclosing classes are derived
5917 from T, return the appropriate type. Used to determine how we found
5918 something via unqualified lookup. */
5920 tree
5921 currently_open_derived_class (t)
5922 tree t;
5924 int i;
5926 if (DERIVED_FROM_P (t, current_class_type))
5927 return current_class_type;
5929 for (i = current_class_depth - 1; i > 0; --i)
5930 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5931 return current_class_stack[i].type;
5933 return NULL_TREE;
5936 /* When entering a class scope, all enclosing class scopes' names with
5937 static meaning (static variables, static functions, types and enumerators)
5938 have to be visible. This recursive function calls pushclass for all
5939 enclosing class contexts until global or a local scope is reached.
5940 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5941 formal of the same name. */
5943 void
5944 push_nested_class (type, modify)
5945 tree type;
5946 int modify;
5948 tree context;
5950 /* A namespace might be passed in error cases, like A::B:C. */
5951 if (type == NULL_TREE
5952 || type == error_mark_node
5953 || TREE_CODE (type) == NAMESPACE_DECL
5954 || ! IS_AGGR_TYPE (type)
5955 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5956 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5957 return;
5959 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5961 if (context && CLASS_TYPE_P (context))
5962 push_nested_class (context, 2);
5963 pushclass (type, modify);
5966 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5968 void
5969 pop_nested_class ()
5971 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5973 popclass ();
5974 if (context && CLASS_TYPE_P (context))
5975 pop_nested_class ();
5978 /* Returns the number of extern "LANG" blocks we are nested within. */
5981 current_lang_depth ()
5983 return VARRAY_ACTIVE_SIZE (current_lang_base);
5986 /* Set global variables CURRENT_LANG_NAME to appropriate value
5987 so that behavior of name-mangling machinery is correct. */
5989 void
5990 push_lang_context (name)
5991 tree name;
5993 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5995 if (name == lang_name_cplusplus)
5997 current_lang_name = name;
5999 else if (name == lang_name_java)
6001 current_lang_name = name;
6002 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
6003 (See record_builtin_java_type in decl.c.) However, that causes
6004 incorrect debug entries if these types are actually used.
6005 So we re-enable debug output after extern "Java". */
6006 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
6007 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
6008 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
6009 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
6010 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
6011 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
6012 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
6013 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
6015 else if (name == lang_name_c)
6017 current_lang_name = name;
6019 else
6020 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
6023 /* Get out of the current language scope. */
6025 void
6026 pop_lang_context ()
6028 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
6029 VARRAY_POP (current_lang_base);
6032 /* Type instantiation routines. */
6034 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
6035 matches the TARGET_TYPE. If there is no satisfactory match, return
6036 error_mark_node, and issue an error message if COMPLAIN is
6037 non-zero. Permit pointers to member function if PTRMEM is non-zero.
6038 If TEMPLATE_ONLY, the name of the overloaded function
6039 was a template-id, and EXPLICIT_TARGS are the explicitly provided
6040 template arguments. */
6042 static tree
6043 resolve_address_of_overloaded_function (target_type,
6044 overload,
6045 complain,
6046 ptrmem,
6047 template_only,
6048 explicit_targs)
6049 tree target_type;
6050 tree overload;
6051 int complain;
6052 int ptrmem;
6053 int template_only;
6054 tree explicit_targs;
6056 /* Here's what the standard says:
6058 [over.over]
6060 If the name is a function template, template argument deduction
6061 is done, and if the argument deduction succeeds, the deduced
6062 arguments are used to generate a single template function, which
6063 is added to the set of overloaded functions considered.
6065 Non-member functions and static member functions match targets of
6066 type "pointer-to-function" or "reference-to-function." Nonstatic
6067 member functions match targets of type "pointer-to-member
6068 function;" the function type of the pointer to member is used to
6069 select the member function from the set of overloaded member
6070 functions. If a nonstatic member function is selected, the
6071 reference to the overloaded function name is required to have the
6072 form of a pointer to member as described in 5.3.1.
6074 If more than one function is selected, any template functions in
6075 the set are eliminated if the set also contains a non-template
6076 function, and any given template function is eliminated if the
6077 set contains a second template function that is more specialized
6078 than the first according to the partial ordering rules 14.5.5.2.
6079 After such eliminations, if any, there shall remain exactly one
6080 selected function. */
6082 int is_ptrmem = 0;
6083 int is_reference = 0;
6084 /* We store the matches in a TREE_LIST rooted here. The functions
6085 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
6086 interoperability with most_specialized_instantiation. */
6087 tree matches = NULL_TREE;
6088 tree fn;
6090 /* By the time we get here, we should be seeing only real
6091 pointer-to-member types, not the internal POINTER_TYPE to
6092 METHOD_TYPE representation. */
6093 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
6094 && (TREE_CODE (TREE_TYPE (target_type))
6095 == METHOD_TYPE)), 0);
6097 if (TREE_CODE (overload) == COMPONENT_REF)
6098 overload = TREE_OPERAND (overload, 1);
6100 /* Check that the TARGET_TYPE is reasonable. */
6101 if (TYPE_PTRFN_P (target_type))
6102 /* This is OK. */;
6103 else if (TYPE_PTRMEMFUNC_P (target_type))
6104 /* This is OK, too. */
6105 is_ptrmem = 1;
6106 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
6108 /* This is OK, too. This comes from a conversion to reference
6109 type. */
6110 target_type = build_reference_type (target_type);
6111 is_reference = 1;
6113 else
6115 if (complain)
6116 cp_error ("\
6117 cannot resolve overloaded function `%D' based on conversion to type `%T'",
6118 DECL_NAME (OVL_FUNCTION (overload)), target_type);
6119 return error_mark_node;
6122 /* If we can find a non-template function that matches, we can just
6123 use it. There's no point in generating template instantiations
6124 if we're just going to throw them out anyhow. But, of course, we
6125 can only do this when we don't *need* a template function. */
6126 if (!template_only)
6128 tree fns;
6130 for (fns = overload; fns; fns = OVL_CHAIN (fns))
6132 tree fn = OVL_FUNCTION (fns);
6133 tree fntype;
6135 if (TREE_CODE (fn) == TEMPLATE_DECL)
6136 /* We're not looking for templates just yet. */
6137 continue;
6139 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6140 != is_ptrmem)
6141 /* We're looking for a non-static member, and this isn't
6142 one, or vice versa. */
6143 continue;
6145 /* See if there's a match. */
6146 fntype = TREE_TYPE (fn);
6147 if (is_ptrmem)
6148 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
6149 else if (!is_reference)
6150 fntype = build_pointer_type (fntype);
6152 if (can_convert_arg (target_type, fntype, fn))
6153 matches = tree_cons (fn, NULL_TREE, matches);
6157 /* Now, if we've already got a match (or matches), there's no need
6158 to proceed to the template functions. But, if we don't have a
6159 match we need to look at them, too. */
6160 if (!matches)
6162 tree target_fn_type;
6163 tree target_arg_types;
6164 tree target_ret_type;
6165 tree fns;
6167 if (is_ptrmem)
6168 target_fn_type
6169 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
6170 else
6171 target_fn_type = TREE_TYPE (target_type);
6172 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
6173 target_ret_type = TREE_TYPE (target_fn_type);
6175 /* Never do unification on the 'this' parameter. */
6176 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
6177 target_arg_types = TREE_CHAIN (target_arg_types);
6179 for (fns = overload; fns; fns = OVL_CHAIN (fns))
6181 tree fn = OVL_FUNCTION (fns);
6182 tree instantiation;
6183 tree instantiation_type;
6184 tree targs;
6186 if (TREE_CODE (fn) != TEMPLATE_DECL)
6187 /* We're only looking for templates. */
6188 continue;
6190 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6191 != is_ptrmem)
6192 /* We're not looking for a non-static member, and this is
6193 one, or vice versa. */
6194 continue;
6196 /* Try to do argument deduction. */
6197 targs = make_tree_vec (DECL_NTPARMS (fn));
6198 if (fn_type_unification (fn, explicit_targs, targs,
6199 target_arg_types, target_ret_type,
6200 DEDUCE_EXACT, -1) != 0)
6201 /* Argument deduction failed. */
6202 continue;
6204 /* Instantiate the template. */
6205 instantiation = instantiate_template (fn, targs);
6206 if (instantiation == error_mark_node)
6207 /* Instantiation failed. */
6208 continue;
6210 /* See if there's a match. */
6211 instantiation_type = TREE_TYPE (instantiation);
6212 if (is_ptrmem)
6213 instantiation_type =
6214 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
6215 else if (!is_reference)
6216 instantiation_type = build_pointer_type (instantiation_type);
6217 if (can_convert_arg (target_type, instantiation_type, instantiation))
6218 matches = tree_cons (instantiation, fn, matches);
6221 /* Now, remove all but the most specialized of the matches. */
6222 if (matches)
6224 tree match = most_specialized_instantiation (matches);
6226 if (match != error_mark_node)
6227 matches = tree_cons (match, NULL_TREE, NULL_TREE);
6231 /* Now we should have exactly one function in MATCHES. */
6232 if (matches == NULL_TREE)
6234 /* There were *no* matches. */
6235 if (complain)
6237 cp_error ("no matches converting function `%D' to type `%#T'",
6238 DECL_NAME (OVL_FUNCTION (overload)),
6239 target_type);
6241 /* print_candidates expects a chain with the functions in
6242 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6243 so why be clever?). */
6244 for (; overload; overload = OVL_NEXT (overload))
6245 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6246 matches);
6248 print_candidates (matches);
6250 return error_mark_node;
6252 else if (TREE_CHAIN (matches))
6254 /* There were too many matches. */
6256 if (complain)
6258 tree match;
6260 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6261 DECL_NAME (OVL_FUNCTION (overload)),
6262 target_type);
6264 /* Since print_candidates expects the functions in the
6265 TREE_VALUE slot, we flip them here. */
6266 for (match = matches; match; match = TREE_CHAIN (match))
6267 TREE_VALUE (match) = TREE_PURPOSE (match);
6269 print_candidates (matches);
6272 return error_mark_node;
6275 /* Good, exactly one match. Now, convert it to the correct type. */
6276 fn = TREE_PURPOSE (matches);
6278 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6279 && !ptrmem && !flag_ms_extensions)
6281 static int explained;
6283 if (!complain)
6284 return error_mark_node;
6286 cp_pedwarn ("assuming pointer to member `%D'", fn);
6287 if (!explained)
6289 cp_pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6290 explained = 1;
6293 mark_used (fn);
6295 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6296 return build_unary_op (ADDR_EXPR, fn, 0);
6297 else
6299 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6300 will mark the function as addressed, but here we must do it
6301 explicitly. */
6302 mark_addressable (fn);
6304 return fn;
6308 /* This function will instantiate the type of the expression given in
6309 RHS to match the type of LHSTYPE. If errors exist, then return
6310 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6311 we complain on errors. If we are not complaining, never modify rhs,
6312 as overload resolution wants to try many possible instantiations, in
6313 the hope that at least one will work.
6315 For non-recursive calls, LHSTYPE should be a function, pointer to
6316 function, or a pointer to member function. */
6318 tree
6319 instantiate_type (lhstype, rhs, flags)
6320 tree lhstype, rhs;
6321 enum instantiate_type_flags flags;
6323 int complain = (flags & itf_complain);
6324 int strict = (flags & itf_no_attributes)
6325 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6326 int allow_ptrmem = flags & itf_ptrmem_ok;
6328 flags &= ~itf_ptrmem_ok;
6330 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6332 if (complain)
6333 error ("not enough type information");
6334 return error_mark_node;
6337 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6339 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6340 return rhs;
6341 if (complain)
6342 cp_error ("argument of type `%T' does not match `%T'",
6343 TREE_TYPE (rhs), lhstype);
6344 return error_mark_node;
6347 /* We don't overwrite rhs if it is an overloaded function.
6348 Copying it would destroy the tree link. */
6349 if (TREE_CODE (rhs) != OVERLOAD)
6350 rhs = copy_node (rhs);
6352 /* This should really only be used when attempting to distinguish
6353 what sort of a pointer to function we have. For now, any
6354 arithmetic operation which is not supported on pointers
6355 is rejected as an error. */
6357 switch (TREE_CODE (rhs))
6359 case TYPE_EXPR:
6360 case CONVERT_EXPR:
6361 case SAVE_EXPR:
6362 case CONSTRUCTOR:
6363 case BUFFER_REF:
6364 my_friendly_abort (177);
6365 return error_mark_node;
6367 case INDIRECT_REF:
6368 case ARRAY_REF:
6370 tree new_rhs;
6372 new_rhs = instantiate_type (build_pointer_type (lhstype),
6373 TREE_OPERAND (rhs, 0), flags);
6374 if (new_rhs == error_mark_node)
6375 return error_mark_node;
6377 TREE_TYPE (rhs) = lhstype;
6378 TREE_OPERAND (rhs, 0) = new_rhs;
6379 return rhs;
6382 case NOP_EXPR:
6383 rhs = copy_node (TREE_OPERAND (rhs, 0));
6384 TREE_TYPE (rhs) = unknown_type_node;
6385 return instantiate_type (lhstype, rhs, flags);
6387 case COMPONENT_REF:
6388 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6390 case OFFSET_REF:
6391 rhs = TREE_OPERAND (rhs, 1);
6392 if (BASELINK_P (rhs))
6393 return instantiate_type (lhstype, TREE_VALUE (rhs),
6394 flags | allow_ptrmem);
6396 /* This can happen if we are forming a pointer-to-member for a
6397 member template. */
6398 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6400 /* Fall through. */
6402 case TEMPLATE_ID_EXPR:
6404 tree fns = TREE_OPERAND (rhs, 0);
6405 tree args = TREE_OPERAND (rhs, 1);
6407 return
6408 resolve_address_of_overloaded_function (lhstype,
6409 fns,
6410 complain,
6411 allow_ptrmem,
6412 /*template_only=*/1,
6413 args);
6416 case OVERLOAD:
6417 return
6418 resolve_address_of_overloaded_function (lhstype,
6419 rhs,
6420 complain,
6421 allow_ptrmem,
6422 /*template_only=*/0,
6423 /*explicit_targs=*/NULL_TREE);
6425 case TREE_LIST:
6426 /* Now we should have a baselink. */
6427 my_friendly_assert (BASELINK_P (rhs), 990412);
6429 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6431 case CALL_EXPR:
6432 /* This is too hard for now. */
6433 my_friendly_abort (183);
6434 return error_mark_node;
6436 case PLUS_EXPR:
6437 case MINUS_EXPR:
6438 case COMPOUND_EXPR:
6439 TREE_OPERAND (rhs, 0)
6440 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6441 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6442 return error_mark_node;
6443 TREE_OPERAND (rhs, 1)
6444 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6445 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6446 return error_mark_node;
6448 TREE_TYPE (rhs) = lhstype;
6449 return rhs;
6451 case MULT_EXPR:
6452 case TRUNC_DIV_EXPR:
6453 case FLOOR_DIV_EXPR:
6454 case CEIL_DIV_EXPR:
6455 case ROUND_DIV_EXPR:
6456 case RDIV_EXPR:
6457 case TRUNC_MOD_EXPR:
6458 case FLOOR_MOD_EXPR:
6459 case CEIL_MOD_EXPR:
6460 case ROUND_MOD_EXPR:
6461 case FIX_ROUND_EXPR:
6462 case FIX_FLOOR_EXPR:
6463 case FIX_CEIL_EXPR:
6464 case FIX_TRUNC_EXPR:
6465 case FLOAT_EXPR:
6466 case NEGATE_EXPR:
6467 case ABS_EXPR:
6468 case MAX_EXPR:
6469 case MIN_EXPR:
6470 case FFS_EXPR:
6472 case BIT_AND_EXPR:
6473 case BIT_IOR_EXPR:
6474 case BIT_XOR_EXPR:
6475 case LSHIFT_EXPR:
6476 case RSHIFT_EXPR:
6477 case LROTATE_EXPR:
6478 case RROTATE_EXPR:
6480 case PREINCREMENT_EXPR:
6481 case PREDECREMENT_EXPR:
6482 case POSTINCREMENT_EXPR:
6483 case POSTDECREMENT_EXPR:
6484 if (complain)
6485 error ("invalid operation on uninstantiated type");
6486 return error_mark_node;
6488 case TRUTH_AND_EXPR:
6489 case TRUTH_OR_EXPR:
6490 case TRUTH_XOR_EXPR:
6491 case LT_EXPR:
6492 case LE_EXPR:
6493 case GT_EXPR:
6494 case GE_EXPR:
6495 case EQ_EXPR:
6496 case NE_EXPR:
6497 case TRUTH_ANDIF_EXPR:
6498 case TRUTH_ORIF_EXPR:
6499 case TRUTH_NOT_EXPR:
6500 if (complain)
6501 error ("not enough type information");
6502 return error_mark_node;
6504 case COND_EXPR:
6505 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6507 if (complain)
6508 error ("not enough type information");
6509 return error_mark_node;
6511 TREE_OPERAND (rhs, 1)
6512 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6513 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6514 return error_mark_node;
6515 TREE_OPERAND (rhs, 2)
6516 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6517 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6518 return error_mark_node;
6520 TREE_TYPE (rhs) = lhstype;
6521 return rhs;
6523 case MODIFY_EXPR:
6524 TREE_OPERAND (rhs, 1)
6525 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6526 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6527 return error_mark_node;
6529 TREE_TYPE (rhs) = lhstype;
6530 return rhs;
6532 case ADDR_EXPR:
6534 if (PTRMEM_OK_P (rhs))
6535 flags |= itf_ptrmem_ok;
6537 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6539 case ENTRY_VALUE_EXPR:
6540 my_friendly_abort (184);
6541 return error_mark_node;
6543 case ERROR_MARK:
6544 return error_mark_node;
6546 default:
6547 my_friendly_abort (185);
6548 return error_mark_node;
6552 /* Return the name of the virtual function pointer field
6553 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6554 this may have to look back through base types to find the
6555 ultimate field name. (For single inheritance, these could
6556 all be the same name. Who knows for multiple inheritance). */
6558 static tree
6559 get_vfield_name (type)
6560 tree type;
6562 tree binfo = TYPE_BINFO (type);
6563 char *buf;
6565 while (BINFO_BASETYPES (binfo)
6566 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6567 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6568 binfo = BINFO_BASETYPE (binfo, 0);
6570 type = BINFO_TYPE (binfo);
6571 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6572 + TYPE_NAME_LENGTH (type) + 2);
6573 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6574 return get_identifier (buf);
6577 void
6578 print_class_statistics ()
6580 #ifdef GATHER_STATISTICS
6581 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6582 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6583 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6584 n_build_method_call, n_inner_fields_searched);
6585 if (n_vtables)
6587 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6588 n_vtables, n_vtable_searches);
6589 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6590 n_vtable_entries, n_vtable_elems);
6592 #endif
6595 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6596 according to [class]:
6597 The class-name is also inserted
6598 into the scope of the class itself. For purposes of access checking,
6599 the inserted class name is treated as if it were a public member name. */
6601 void
6602 build_self_reference ()
6604 tree name = constructor_name (current_class_type);
6605 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6606 tree saved_cas;
6608 DECL_NONLOCAL (value) = 1;
6609 DECL_CONTEXT (value) = current_class_type;
6610 DECL_ARTIFICIAL (value) = 1;
6612 if (processing_template_decl)
6613 value = push_template_decl (value);
6615 saved_cas = current_access_specifier;
6616 current_access_specifier = access_public_node;
6617 finish_member_declaration (value);
6618 current_access_specifier = saved_cas;
6621 /* Returns 1 if TYPE contains only padding bytes. */
6624 is_empty_class (type)
6625 tree type;
6627 if (type == error_mark_node)
6628 return 0;
6630 if (! IS_AGGR_TYPE (type))
6631 return 0;
6633 return integer_zerop (CLASSTYPE_SIZE (type));
6636 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6637 a *_TYPE node. NODE can also be a local class. */
6639 tree
6640 get_enclosing_class (type)
6641 tree type;
6643 tree node = type;
6645 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6647 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6649 case 'd':
6650 node = DECL_CONTEXT (node);
6651 break;
6653 case 't':
6654 if (node != type)
6655 return node;
6656 node = TYPE_CONTEXT (node);
6657 break;
6659 default:
6660 my_friendly_abort (0);
6663 return NULL_TREE;
6666 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6669 is_base_of_enclosing_class (base, type)
6670 tree base, type;
6672 while (type)
6674 if (get_binfo (base, type, 0))
6675 return 1;
6677 type = get_enclosing_class (type);
6679 return 0;
6682 /* Note that NAME was looked up while the current class was being
6683 defined and that the result of that lookup was DECL. */
6685 void
6686 maybe_note_name_used_in_class (name, decl)
6687 tree name;
6688 tree decl;
6690 splay_tree names_used;
6692 /* If we're not defining a class, there's nothing to do. */
6693 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6694 return;
6696 /* If there's already a binding for this NAME, then we don't have
6697 anything to worry about. */
6698 if (IDENTIFIER_CLASS_VALUE (name))
6699 return;
6701 if (!current_class_stack[current_class_depth - 1].names_used)
6702 current_class_stack[current_class_depth - 1].names_used
6703 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6704 names_used = current_class_stack[current_class_depth - 1].names_used;
6706 splay_tree_insert (names_used,
6707 (splay_tree_key) name,
6708 (splay_tree_value) decl);
6711 /* Note that NAME was declared (as DECL) in the current class. Check
6712 to see that the declaration is legal. */
6714 void
6715 note_name_declared_in_class (name, decl)
6716 tree name;
6717 tree decl;
6719 splay_tree names_used;
6720 splay_tree_node n;
6722 /* Look to see if we ever used this name. */
6723 names_used
6724 = current_class_stack[current_class_depth - 1].names_used;
6725 if (!names_used)
6726 return;
6728 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6729 if (n)
6731 /* [basic.scope.class]
6733 A name N used in a class S shall refer to the same declaration
6734 in its context and when re-evaluated in the completed scope of
6735 S. */
6736 cp_error ("declaration of `%#D'", decl);
6737 cp_error_at ("changes meaning of `%D' from `%+#D'",
6738 DECL_NAME (OVL_CURRENT (decl)),
6739 (tree) n->value);
6743 /* Returns the VAR_DECL for the complete vtable associated with
6744 BINFO. (Under the new ABI, secondary vtables are merged with
6745 primary vtables; this function will return the VAR_DECL for the
6746 primary vtable.) */
6748 tree
6749 get_vtbl_decl_for_binfo (binfo)
6750 tree binfo;
6752 tree decl;
6754 decl = BINFO_VTABLE (binfo);
6755 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6757 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6758 2000403);
6759 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6761 if (decl)
6762 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6763 return decl;
6766 /* Called from get_primary_binfo via dfs_walk. DATA is a TREE_LIST
6767 who's TREE_PURPOSE is the TYPE of the required primary base and
6768 who's TREE_VALUE is a list of candidate binfos that we fill in. */
6770 static tree
6771 dfs_get_primary_binfo (binfo, data)
6772 tree binfo;
6773 void *data;
6775 tree cons = (tree) data;
6776 tree primary_base = TREE_PURPOSE (cons);
6778 if (TREE_VIA_VIRTUAL (binfo)
6779 && same_type_p (BINFO_TYPE (binfo), primary_base))
6780 /* This is the right type of binfo, but it might be an unshared
6781 instance, and the shared instance is later in the dfs walk. We
6782 must keep looking. */
6783 TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6785 return NULL_TREE;
6788 /* Returns the unshared binfo for the primary base of BINFO. Note
6789 that in a complex hierarchy the resulting BINFO may not actually
6790 *be* primary. In particular if the resulting BINFO is a virtual
6791 base, and it occurs elsewhere in the hierarchy, then this
6792 occurrence may not actually be a primary base in the complete
6793 object. Check BINFO_PRIMARY_P to be sure. */
6795 tree
6796 get_primary_binfo (binfo)
6797 tree binfo;
6799 tree primary_base;
6800 tree result = NULL_TREE;
6801 tree virtuals;
6803 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6804 if (!primary_base)
6805 return NULL_TREE;
6807 /* A non-virtual primary base is always a direct base, and easy to
6808 find. */
6809 if (!TREE_VIA_VIRTUAL (primary_base))
6811 int i;
6813 /* Scan the direct basetypes until we find a base with the same
6814 type as the primary base. */
6815 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6817 tree base_binfo = BINFO_BASETYPE (binfo, i);
6819 if (same_type_p (BINFO_TYPE (base_binfo),
6820 BINFO_TYPE (primary_base)))
6821 return base_binfo;
6824 /* We should always find the primary base. */
6825 my_friendly_abort (20000729);
6828 /* For a primary virtual base, we have to scan the entire hierarchy
6829 rooted at BINFO; the virtual base could be an indirect virtual
6830 base. There could be more than one instance of the primary base
6831 in the hierarchy, and if one is the canonical binfo we want that
6832 one. If it exists, it should be the first one we find, but as a
6833 consistency check we find them all and make sure. */
6834 virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6835 dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6836 virtuals = TREE_VALUE (virtuals);
6838 /* We must have found at least one instance. */
6839 my_friendly_assert (virtuals, 20010612);
6841 if (TREE_CHAIN (virtuals))
6843 /* We found more than one instance of the base. We must make
6844 sure that, if one is the canonical one, it is the first one
6845 we found. As the chain is in reverse dfs order, that means
6846 the last on the list. */
6847 tree complete_binfo;
6848 tree canonical;
6850 for (complete_binfo = binfo;
6851 BINFO_INHERITANCE_CHAIN (complete_binfo);
6852 complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6853 continue;
6854 canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6855 BINFO_TYPE (complete_binfo));
6857 for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6859 result = TREE_VALUE (virtuals);
6861 if (canonical == result)
6863 /* This is the unshared instance. Make sure it was the
6864 first one found. */
6865 my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
6866 break;
6870 else
6871 result = TREE_VALUE (virtuals);
6872 return result;
6875 /* If INDENTED_P is zero, indent to INDENT. Return non-zero. */
6877 static int
6878 maybe_indent_hierarchy (stream, indent, indented_p)
6879 FILE *stream;
6880 int indent;
6881 int indented_p;
6883 if (!indented_p)
6884 fprintf (stream, "%*s", indent, "");
6885 return 1;
6888 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6889 dominated by T) to stderr. INDENT should be zero when called from
6890 the top level; it is incremented recursively. */
6892 static void
6893 dump_class_hierarchy_r (stream, flags, t, binfo, indent)
6894 FILE *stream;
6895 int flags;
6896 tree t;
6897 tree binfo;
6898 int indent;
6900 int i;
6901 int indented = 0;
6903 indented = maybe_indent_hierarchy (stream, indent, 0);
6904 fprintf (stream, "%s (0x%lx) ",
6905 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6906 (unsigned long) binfo);
6907 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6908 tree_low_cst (BINFO_OFFSET (binfo), 0));
6909 if (is_empty_class (BINFO_TYPE (binfo)))
6910 fprintf (stream, " empty");
6911 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6912 fprintf (stream, " nearly-empty");
6913 if (TREE_VIA_VIRTUAL (binfo))
6915 tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6917 fprintf (stream, " virtual");
6918 if (canonical == binfo)
6919 fprintf (stream, " canonical");
6920 else
6921 fprintf (stream, " non-canonical");
6923 fprintf (stream, "\n");
6925 indented = 0;
6926 if (BINFO_PRIMARY_BASE_OF (binfo))
6928 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6929 fprintf (stream, " primary-for %s (0x%lx)",
6930 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6931 TFF_PLAIN_IDENTIFIER),
6932 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6934 if (BINFO_LOST_PRIMARY_P (binfo))
6936 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6937 fprintf (stream, " lost-primary");
6939 if (indented)
6940 fprintf (stream, "\n");
6942 if (!(flags & TDF_SLIM))
6944 int indented = 0;
6946 if (BINFO_SUBVTT_INDEX (binfo))
6948 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6949 fprintf (stream, " subvttidx=%s",
6950 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6951 TFF_PLAIN_IDENTIFIER));
6953 if (BINFO_VPTR_INDEX (binfo))
6955 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6956 fprintf (stream, " vptridx=%s",
6957 expr_as_string (BINFO_VPTR_INDEX (binfo),
6958 TFF_PLAIN_IDENTIFIER));
6960 if (BINFO_VPTR_FIELD (binfo))
6962 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6963 fprintf (stream, " vbaseoffset=%s",
6964 expr_as_string (BINFO_VPTR_FIELD (binfo),
6965 TFF_PLAIN_IDENTIFIER));
6967 if (BINFO_VTABLE (binfo))
6969 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6970 fprintf (stream, " vptr=%s",
6971 expr_as_string (BINFO_VTABLE (binfo),
6972 TFF_PLAIN_IDENTIFIER));
6975 if (indented)
6976 fprintf (stream, "\n");
6980 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6981 dump_class_hierarchy_r (stream, flags,
6982 t, BINFO_BASETYPE (binfo, i),
6983 indent + 2);
6986 /* Dump the BINFO hierarchy for T. */
6988 static void
6989 dump_class_hierarchy (t)
6990 tree t;
6992 int flags;
6993 FILE *stream = dump_begin (TDI_class, &flags);
6995 if (!stream)
6996 return;
6998 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6999 fprintf (stream, " size=%lu align=%lu\n",
7000 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
7001 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
7002 dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
7003 fprintf (stream, "\n");
7004 dump_end (TDI_class, stream);
7007 static void
7008 dump_array (stream, decl)
7009 FILE *stream;
7010 tree decl;
7012 tree inits;
7013 int ix;
7014 HOST_WIDE_INT elt;
7015 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
7017 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
7018 / BITS_PER_UNIT);
7019 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
7020 fprintf (stream, " %s entries",
7021 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
7022 TFF_PLAIN_IDENTIFIER));
7023 fprintf (stream, "\n");
7025 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
7026 inits; ix++, inits = TREE_CHAIN (inits))
7027 fprintf (stream, "%-4d %s\n", ix * elt,
7028 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
7031 static void
7032 dump_vtable (t, binfo, vtable)
7033 tree t;
7034 tree binfo;
7035 tree vtable;
7037 int flags;
7038 FILE *stream = dump_begin (TDI_class, &flags);
7040 if (!stream)
7041 return;
7043 if (!(flags & TDF_SLIM))
7045 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
7047 fprintf (stream, "%s for %s",
7048 ctor_vtbl_p ? "Construction vtable" : "Vtable",
7049 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
7050 if (ctor_vtbl_p)
7052 if (!TREE_VIA_VIRTUAL (binfo))
7053 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
7054 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7056 fprintf (stream, "\n");
7057 dump_array (stream, vtable);
7058 fprintf (stream, "\n");
7061 dump_end (TDI_class, stream);
7064 static void
7065 dump_vtt (t, vtt)
7066 tree t;
7067 tree vtt;
7069 int flags;
7070 FILE *stream = dump_begin (TDI_class, &flags);
7072 if (!stream)
7073 return;
7075 if (!(flags & TDF_SLIM))
7077 fprintf (stream, "VTT for %s\n",
7078 type_as_string (t, TFF_PLAIN_IDENTIFIER));
7079 dump_array (stream, vtt);
7080 fprintf (stream, "\n");
7083 dump_end (TDI_class, stream);
7086 /* Virtual function table initialization. */
7088 /* Create all the necessary vtables for T and its base classes. */
7090 static void
7091 finish_vtbls (t)
7092 tree t;
7094 if (merge_primary_and_secondary_vtables_p ())
7096 tree list;
7097 tree vbase;
7099 /* Under the new ABI, we lay out the primary and secondary
7100 vtables in one contiguous vtable. The primary vtable is
7101 first, followed by the non-virtual secondary vtables in
7102 inheritance graph order. */
7103 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
7104 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
7105 TYPE_BINFO (t), t, list);
7106 /* Then come the virtual bases, also in inheritance graph
7107 order. */
7108 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
7110 tree real_base;
7112 if (!TREE_VIA_VIRTUAL (vbase))
7113 continue;
7115 /* Although we walk in inheritance order, that might not get the
7116 canonical base. */
7117 real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
7119 accumulate_vtbl_inits (real_base, real_base,
7120 TYPE_BINFO (t), t, list);
7123 if (TYPE_BINFO_VTABLE (t))
7124 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
7126 else
7128 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
7129 dfs_unmarked_real_bases_queue_p, t);
7130 dfs_walk (TYPE_BINFO (t), dfs_unmark,
7131 dfs_marked_real_bases_queue_p, t);
7135 /* Called from finish_vtbls via dfs_walk. */
7137 static tree
7138 dfs_finish_vtbls (binfo, data)
7139 tree binfo;
7140 void *data;
7142 tree t = (tree) data;
7144 if (BINFO_NEW_VTABLE_MARKED (binfo, t))
7145 initialize_vtable (binfo,
7146 build_vtbl_initializer (binfo, binfo, t,
7147 TYPE_BINFO (t), NULL));
7149 SET_BINFO_MARKED (binfo);
7151 return NULL_TREE;
7154 /* Initialize the vtable for BINFO with the INITS. */
7156 static void
7157 initialize_vtable (binfo, inits)
7158 tree binfo;
7159 tree inits;
7161 tree decl;
7163 layout_vtable_decl (binfo, list_length (inits));
7164 decl = get_vtbl_decl_for_binfo (binfo);
7165 initialize_array (decl, inits);
7166 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
7169 /* Initialize DECL (a declaration for a namespace-scope array) with
7170 the INITS. */
7172 static void
7173 initialize_array (decl, inits)
7174 tree decl;
7175 tree inits;
7177 tree context;
7179 context = DECL_CONTEXT (decl);
7180 DECL_CONTEXT (decl) = NULL_TREE;
7181 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
7182 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
7183 DECL_CONTEXT (decl) = context;
7186 /* Build the VTT (virtual table table) for T.
7187 A class requires a VTT if it has virtual bases.
7189 This holds
7190 1 - primary virtual pointer for complete object T
7191 2 - secondary VTTs for each direct non-virtual base of T which requires a
7193 3 - secondary virtual pointers for each direct or indirect base of T which
7194 has virtual bases or is reachable via a virtual path from T.
7195 4 - secondary VTTs for each direct or indirect virtual base of T.
7197 Secondary VTTs look like complete object VTTs without part 4. */
7199 static void
7200 build_vtt (t)
7201 tree t;
7203 tree inits;
7204 tree type;
7205 tree vtt;
7206 tree index;
7208 /* Build up the initializers for the VTT. */
7209 inits = NULL_TREE;
7210 index = size_zero_node;
7211 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
7213 /* If we didn't need a VTT, we're done. */
7214 if (!inits)
7215 return;
7217 /* Figure out the type of the VTT. */
7218 type = build_index_type (size_int (list_length (inits) - 1));
7219 type = build_cplus_array_type (const_ptr_type_node, type);
7221 /* Now, build the VTT object itself. */
7222 vtt = build_vtable (t, get_vtt_name (t), type);
7223 pushdecl_top_level (vtt);
7224 initialize_array (vtt, inits);
7226 dump_vtt (t, vtt);
7229 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
7230 from within some heirarchy which is inherited from the type of BINFO.
7231 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
7232 BINFO. */
7234 static tree
7235 get_original_base (base_binfo, binfo)
7236 tree base_binfo;
7237 tree binfo;
7239 tree derived;
7240 int ix;
7242 if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
7243 return binfo;
7244 if (TREE_VIA_VIRTUAL (base_binfo))
7245 return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
7246 derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
7248 for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
7249 if (same_type_p (BINFO_TYPE (base_binfo),
7250 BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
7251 return BINFO_BASETYPE (derived, ix);
7252 my_friendly_abort (20010223);
7253 return NULL;
7256 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7257 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7258 and CHAIN the vtable pointer for this binfo after construction is
7259 complete. VALUE can also be another BINFO, in which case we recurse. */
7261 static tree
7262 binfo_ctor_vtable (binfo)
7263 tree binfo;
7265 tree vt;
7267 while (1)
7269 vt = BINFO_VTABLE (binfo);
7270 if (TREE_CODE (vt) == TREE_LIST)
7271 vt = TREE_VALUE (vt);
7272 if (TREE_CODE (vt) == TREE_VEC)
7273 binfo = vt;
7274 else
7275 break;
7278 return vt;
7281 /* Recursively build the VTT-initializer for BINFO (which is in the
7282 hierarchy dominated by T). INITS points to the end of the initializer
7283 list to date. INDEX is the VTT index where the next element will be
7284 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7285 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7286 for virtual bases of T. When it is not so, we build the constructor
7287 vtables for the BINFO-in-T variant. */
7289 static tree *
7290 build_vtt_inits (binfo, t, inits, index)
7291 tree binfo;
7292 tree t;
7293 tree *inits;
7294 tree *index;
7296 int i;
7297 tree b;
7298 tree init;
7299 tree secondary_vptrs;
7300 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7302 /* We only need VTTs for subobjects with virtual bases. */
7303 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7304 return inits;
7306 /* We need to use a construction vtable if this is not the primary
7307 VTT. */
7308 if (!top_level_p)
7310 build_ctor_vtbl_group (binfo, t);
7312 /* Record the offset in the VTT where this sub-VTT can be found. */
7313 BINFO_SUBVTT_INDEX (binfo) = *index;
7316 /* Add the address of the primary vtable for the complete object. */
7317 init = binfo_ctor_vtable (binfo);
7318 *inits = build_tree_list (NULL_TREE, init);
7319 inits = &TREE_CHAIN (*inits);
7320 if (top_level_p)
7322 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7323 BINFO_VPTR_INDEX (binfo) = *index;
7325 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7327 /* Recursively add the secondary VTTs for non-virtual bases. */
7328 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7330 b = BINFO_BASETYPE (binfo, i);
7331 if (!TREE_VIA_VIRTUAL (b))
7332 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
7333 inits, index);
7336 /* Add secondary virtual pointers for all subobjects of BINFO with
7337 either virtual bases or reachable along a virtual path, except
7338 subobjects that are non-virtual primary bases. */
7339 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7340 TREE_TYPE (secondary_vptrs) = *index;
7341 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7342 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7344 dfs_walk_real (binfo,
7345 dfs_build_secondary_vptr_vtt_inits,
7346 NULL,
7347 dfs_ctor_vtable_bases_queue_p,
7348 secondary_vptrs);
7349 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7350 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7351 secondary_vptrs);
7353 *index = TREE_TYPE (secondary_vptrs);
7355 /* The secondary vptrs come back in reverse order. After we reverse
7356 them, and add the INITS, the last init will be the first element
7357 of the chain. */
7358 secondary_vptrs = TREE_VALUE (secondary_vptrs);
7359 if (secondary_vptrs)
7361 *inits = nreverse (secondary_vptrs);
7362 inits = &TREE_CHAIN (secondary_vptrs);
7363 my_friendly_assert (*inits == NULL_TREE, 20000517);
7366 /* Add the secondary VTTs for virtual bases. */
7367 if (top_level_p)
7368 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7370 tree vbase;
7372 if (!TREE_VIA_VIRTUAL (b))
7373 continue;
7375 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7376 inits = build_vtt_inits (vbase, t, inits, index);
7379 if (!top_level_p)
7381 tree data = tree_cons (t, binfo, NULL_TREE);
7382 VTT_TOP_LEVEL_P (data) = 0;
7383 VTT_MARKED_BINFO_P (data) = 0;
7385 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7386 dfs_ctor_vtable_bases_queue_p,
7387 data);
7390 return inits;
7393 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7394 for the base in most derived. DATA is a TREE_LIST who's
7395 TREE_CHAIN is the type of the base being
7396 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7397 flag of DATA indicates that this is a constructor vtable. The
7398 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7400 static tree
7401 dfs_build_secondary_vptr_vtt_inits (binfo, data)
7402 tree binfo;
7403 void *data;
7405 tree l;
7406 tree t;
7407 tree init;
7408 tree index;
7409 int top_level_p;
7411 l = (tree) data;
7412 t = TREE_CHAIN (l);
7413 top_level_p = VTT_TOP_LEVEL_P (l);
7415 SET_BINFO_MARKED (binfo);
7417 /* We don't care about bases that don't have vtables. */
7418 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7419 return NULL_TREE;
7421 /* We're only interested in proper subobjects of T. */
7422 if (same_type_p (BINFO_TYPE (binfo), t))
7423 return NULL_TREE;
7425 /* We're not interested in non-virtual primary bases. */
7426 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7427 return NULL_TREE;
7429 /* If BINFO has virtual bases or is reachable via a virtual path
7430 from T, it'll have a secondary vptr. */
7431 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7432 && !binfo_via_virtual (binfo, t))
7433 return NULL_TREE;
7435 /* Record the index where this secondary vptr can be found. */
7436 index = TREE_TYPE (l);
7437 if (top_level_p)
7439 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7440 BINFO_VPTR_INDEX (binfo) = index;
7442 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7443 TYPE_SIZE_UNIT (ptr_type_node));
7445 /* Add the initializer for the secondary vptr itself. */
7446 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7448 /* It's a primary virtual base, and this is not the construction
7449 vtable. Find the base this is primary of in the inheritance graph,
7450 and use that base's vtable now. */
7451 while (BINFO_PRIMARY_BASE_OF (binfo))
7452 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7454 init = binfo_ctor_vtable (binfo);
7455 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7457 return NULL_TREE;
7460 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7461 TREE_UNSIGNED indicates that a constructor vtable is being built.
7462 TREE_USED indicates whether marked or unmarked bases should be walked.
7463 TREE_PURPOSE is the TREE_TYPE that dominates the hierarchy. */
7465 static tree
7466 dfs_ctor_vtable_bases_queue_p (binfo, data)
7467 tree binfo;
7468 void *data;
7470 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_P (binfo))
7472 tree type = TREE_PURPOSE ((tree) data);
7474 /* This is a non-primary virtual base, get the shared version. */
7475 binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
7476 if (VTT_TOP_LEVEL_P ((tree) data) && BINFO_PRIMARY_P (binfo))
7477 return NULL_TREE;
7479 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7480 return NULL_TREE;
7481 return binfo;
7484 /* Called from build_vtt_inits via dfs_walk. After building constructor
7485 vtables and generating the sub-vtt from them, we need to restore the
7486 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7487 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7489 static tree
7490 dfs_fixup_binfo_vtbls (binfo, data)
7491 tree binfo;
7492 void *data;
7494 CLEAR_BINFO_MARKED (binfo);
7496 /* We don't care about bases that don't have vtables. */
7497 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7498 return NULL_TREE;
7500 /* If we scribbled the construction vtable vptr into BINFO, clear it
7501 out now. */
7502 if (BINFO_VTABLE (binfo)
7503 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7504 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7505 == TREE_VALUE ((tree) data)))
7506 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7508 return NULL_TREE;
7511 /* Build the construction vtable group for BINFO which is in the
7512 hierarchy dominated by T. */
7514 static void
7515 build_ctor_vtbl_group (binfo, t)
7516 tree binfo;
7517 tree t;
7519 tree list;
7520 tree type;
7521 tree vtbl;
7522 tree inits;
7523 tree id;
7524 tree vbase;
7526 /* See if we've already created this construction vtable group. */
7527 id = mangle_ctor_vtbl_for_type (t, binfo);
7528 if (IDENTIFIER_GLOBAL_VALUE (id))
7529 return;
7531 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7532 /* Build a version of VTBL (with the wrong type) for use in
7533 constructing the addresses of secondary vtables in the
7534 construction vtable group. */
7535 vtbl = build_vtable (t, id, ptr_type_node);
7536 list = build_tree_list (vtbl, NULL_TREE);
7537 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7538 binfo, t, list);
7540 /* Add the vtables for each of our virtual bases using the vbase in T
7541 binfo. */
7542 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7543 vbase;
7544 vbase = TREE_CHAIN (vbase))
7546 tree b;
7547 tree orig_base;
7549 if (!TREE_VIA_VIRTUAL (vbase))
7550 continue;
7551 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7552 orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7554 accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7556 inits = TREE_VALUE (list);
7558 /* Figure out the type of the construction vtable. */
7559 type = build_index_type (size_int (list_length (inits) - 1));
7560 type = build_cplus_array_type (vtable_entry_type, type);
7561 TREE_TYPE (vtbl) = type;
7563 /* Initialize the construction vtable. */
7564 pushdecl_top_level (vtbl);
7565 initialize_array (vtbl, inits);
7566 dump_vtable (t, binfo, vtbl);
7569 /* Add the vtbl initializers for BINFO (and its bases other than
7570 non-virtual primaries) to the list of INITS. BINFO is in the
7571 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7572 the constructor the vtbl inits should be accumulated for. (If this
7573 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7574 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7575 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7576 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7577 but are not necessarily the same in terms of layout. */
7579 static void
7580 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7581 tree binfo;
7582 tree orig_binfo;
7583 tree rtti_binfo;
7584 tree t;
7585 tree inits;
7587 int i;
7588 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7590 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7591 BINFO_TYPE (orig_binfo)),
7592 20000517);
7594 /* If it doesn't have a vptr, we don't do anything. */
7595 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7596 return;
7598 /* If we're building a construction vtable, we're not interested in
7599 subobjects that don't require construction vtables. */
7600 if (ctor_vtbl_p
7601 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7602 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7603 return;
7605 /* Build the initializers for the BINFO-in-T vtable. */
7606 TREE_VALUE (inits)
7607 = chainon (TREE_VALUE (inits),
7608 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7609 rtti_binfo, t, inits));
7611 /* Walk the BINFO and its bases. We walk in preorder so that as we
7612 initialize each vtable we can figure out at what offset the
7613 secondary vtable lies from the primary vtable. We can't use
7614 dfs_walk here because we need to iterate through bases of BINFO
7615 and RTTI_BINFO simultaneously. */
7616 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7618 tree base_binfo = BINFO_BASETYPE (binfo, i);
7620 /* Skip virtual bases. */
7621 if (TREE_VIA_VIRTUAL (base_binfo))
7622 continue;
7623 accumulate_vtbl_inits (base_binfo,
7624 BINFO_BASETYPE (orig_binfo, i),
7625 rtti_binfo, t,
7626 inits);
7630 /* Called from accumulate_vtbl_inits when using the new ABI.
7631 Accumulates the vtable initializers for all of the vtables into
7632 TREE_VALUE (DATA). Returns the initializers for the BINFO vtable. */
7634 static tree
7635 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7636 tree binfo;
7637 tree orig_binfo;
7638 tree rtti_binfo;
7639 tree t;
7640 tree l;
7642 tree inits = NULL_TREE;
7643 tree vtbl = NULL_TREE;
7644 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7646 if (ctor_vtbl_p
7647 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7649 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7650 primary virtual base. If it is not the same primary in
7651 the hierarchy of T, we'll need to generate a ctor vtable
7652 for it, to place at its location in T. If it is the same
7653 primary, we still need a VTT entry for the vtable, but it
7654 should point to the ctor vtable for the base it is a
7655 primary for within the sub-hierarchy of RTTI_BINFO.
7657 There are three possible cases:
7659 1) We are in the same place.
7660 2) We are a primary base within a lost primary virtual base of
7661 RTTI_BINFO.
7662 3) We are primary to something not a base of RTTI_BINFO. */
7664 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7665 tree last = NULL_TREE;
7667 /* First, look through the bases we are primary to for RTTI_BINFO
7668 or a virtual base. */
7669 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7671 last = b;
7672 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7673 break;
7675 /* If we run out of primary links, keep looking down our
7676 inheritance chain; we might be an indirect primary. */
7677 if (b == NULL_TREE)
7678 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7679 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7680 break;
7682 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7683 base B and it is a base of RTTI_BINFO, this is case 2. In
7684 either case, we share our vtable with LAST, i.e. the
7685 derived-most base within B of which we are a primary. */
7686 if (b == rtti_binfo
7687 || (b && binfo_for_vbase (BINFO_TYPE (b),
7688 BINFO_TYPE (rtti_binfo))))
7689 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7690 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7691 binfo_ctor_vtable after everything's been set up. */
7692 vtbl = last;
7694 /* Otherwise, this is case 3 and we get our own. */
7696 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7697 return inits;
7699 if (!vtbl)
7701 tree index;
7702 int non_fn_entries;
7704 /* Compute the initializer for this vtable. */
7705 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7706 &non_fn_entries);
7708 /* Figure out the position to which the VPTR should point. */
7709 vtbl = TREE_PURPOSE (l);
7710 vtbl = build1 (ADDR_EXPR,
7711 vtbl_ptr_type_node,
7712 vtbl);
7713 TREE_CONSTANT (vtbl) = 1;
7714 index = size_binop (PLUS_EXPR,
7715 size_int (non_fn_entries),
7716 size_int (list_length (TREE_VALUE (l))));
7717 index = size_binop (MULT_EXPR,
7718 TYPE_SIZE_UNIT (vtable_entry_type),
7719 index);
7720 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7721 TREE_CONSTANT (vtbl) = 1;
7724 if (ctor_vtbl_p)
7725 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7726 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7727 straighten this out. */
7728 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7729 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7730 inits = NULL_TREE;
7731 else
7732 /* For an ordinary vtable, set BINFO_VTABLE. */
7733 BINFO_VTABLE (binfo) = vtbl;
7735 return inits;
7738 /* Construct the initializer for BINFO's virtual function table. BINFO
7739 is part of the hierarchy dominated by T. If we're building a
7740 construction vtable, the ORIG_BINFO is the binfo we should use to
7741 find the actual function pointers to put in the vtable - but they
7742 can be overridden on the path to most-derived in the graph that
7743 ORIG_BINFO belongs. Otherwise,
7744 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7745 BINFO that should be indicated by the RTTI information in the
7746 vtable; it will be a base class of T, rather than T itself, if we
7747 are building a construction vtable.
7749 The value returned is a TREE_LIST suitable for wrapping in a
7750 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7751 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7752 number of non-function entries in the vtable.
7754 It might seem that this function should never be called with a
7755 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7756 base is always subsumed by a derived class vtable. However, when
7757 we are building construction vtables, we do build vtables for
7758 primary bases; we need these while the primary base is being
7759 constructed. */
7761 static tree
7762 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7763 tree binfo;
7764 tree orig_binfo;
7765 tree t;
7766 tree rtti_binfo;
7767 int *non_fn_entries_p;
7769 tree v, b;
7770 tree vfun_inits;
7771 tree vbase;
7772 vtbl_init_data vid;
7774 /* Initialize VID. */
7775 memset (&vid, 0, sizeof (vid));
7776 vid.binfo = binfo;
7777 vid.derived = t;
7778 vid.rtti_binfo = rtti_binfo;
7779 vid.last_init = &vid.inits;
7780 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7781 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7782 /* The first vbase or vcall offset is at index -3 in the vtable. */
7783 vid.index = ssize_int (-3);
7785 /* Add entries to the vtable for RTTI. */
7786 build_rtti_vtbl_entries (binfo, &vid);
7788 /* Create an array for keeping track of the functions we've
7789 processed. When we see multiple functions with the same
7790 signature, we share the vcall offsets. */
7791 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7792 /* Add the vcall and vbase offset entries. */
7793 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7794 /* Clean up. */
7795 VARRAY_FREE (vid.fns);
7796 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7797 build_vbase_offset_vtbl_entries. */
7798 for (vbase = CLASSTYPE_VBASECLASSES (t);
7799 vbase;
7800 vbase = TREE_CHAIN (vbase))
7801 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7803 if (non_fn_entries_p)
7804 *non_fn_entries_p = list_length (vid.inits);
7806 /* Go through all the ordinary virtual functions, building up
7807 initializers. */
7808 vfun_inits = NULL_TREE;
7809 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7811 tree delta;
7812 tree vcall_index;
7813 tree fn;
7814 tree pfn;
7815 tree init;
7817 /* Pull the offset for `this', and the function to call, out of
7818 the list. */
7819 delta = BV_DELTA (v);
7821 if (BV_USE_VCALL_INDEX_P (v))
7823 vcall_index = BV_VCALL_INDEX (v);
7824 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
7826 else
7827 vcall_index = NULL_TREE;
7829 fn = BV_FN (v);
7830 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7831 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7833 /* You can't call an abstract virtual function; it's abstract.
7834 So, we replace these functions with __pure_virtual. */
7835 if (DECL_PURE_VIRTUAL_P (fn))
7836 fn = abort_fndecl;
7838 /* Take the address of the function, considering it to be of an
7839 appropriate generic type. */
7840 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7841 /* The address of a function can't change. */
7842 TREE_CONSTANT (pfn) = 1;
7844 /* Enter it in the vtable. */
7845 init = build_vtable_entry (delta, vcall_index, pfn);
7847 /* If the only definition of this function signature along our
7848 primary base chain is from a lost primary, this vtable slot will
7849 never be used, so just zero it out. This is important to avoid
7850 requiring extra thunks which cannot be generated with the function.
7852 We could also handle this in update_vtable_entry_for_fn; doing it
7853 here means we zero out unused slots in ctor vtables as well,
7854 rather than filling them with erroneous values (though harmless,
7855 apart from relocation costs). */
7856 if (fn != abort_fndecl)
7857 for (b = binfo; ; b = get_primary_binfo (b))
7859 /* We found a defn before a lost primary; go ahead as normal. */
7860 if (look_for_overrides_here (BINFO_TYPE (b), fn))
7861 break;
7863 /* The nearest definition is from a lost primary; clear the
7864 slot. */
7865 if (BINFO_LOST_PRIMARY_P (b))
7867 init = size_zero_node;
7868 break;
7872 /* And add it to the chain of initializers. */
7873 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7876 /* The initializers for virtual functions were built up in reverse
7877 order; straighten them out now. */
7878 vfun_inits = nreverse (vfun_inits);
7880 /* The negative offset initializers are also in reverse order. */
7881 vid.inits = nreverse (vid.inits);
7883 /* Chain the two together. */
7884 return chainon (vid.inits, vfun_inits);
7887 /* Adds to vid->inits the initializers for the vbase and vcall
7888 offsets in BINFO, which is in the hierarchy dominated by T. */
7890 static void
7891 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7892 tree binfo;
7893 vtbl_init_data *vid;
7895 tree b;
7897 /* If this is a derived class, we must first create entries
7898 corresponding to the primary base class. */
7899 b = get_primary_binfo (binfo);
7900 if (b)
7901 build_vcall_and_vbase_vtbl_entries (b, vid);
7903 /* Add the vbase entries for this base. */
7904 build_vbase_offset_vtbl_entries (binfo, vid);
7905 /* Add the vcall entries for this base. */
7906 build_vcall_offset_vtbl_entries (binfo, vid);
7909 /* Returns the initializers for the vbase offset entries in the vtable
7910 for BINFO (which is part of the class hierarchy dominated by T), in
7911 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7912 where the next vbase offset will go. */
7914 static void
7915 build_vbase_offset_vtbl_entries (binfo, vid)
7916 tree binfo;
7917 vtbl_init_data *vid;
7919 tree vbase;
7920 tree t;
7922 /* Under the old ABI, pointers to virtual bases are stored in each
7923 object. */
7924 if (!vbase_offsets_in_vtable_p ())
7925 return;
7927 /* If there are no virtual baseclasses, then there is nothing to
7928 do. */
7929 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7930 return;
7932 t = vid->derived;
7934 /* Go through the virtual bases, adding the offsets. */
7935 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7936 vbase;
7937 vbase = TREE_CHAIN (vbase))
7939 tree b;
7940 tree delta;
7942 if (!TREE_VIA_VIRTUAL (vbase))
7943 continue;
7945 /* Find the instance of this virtual base in the complete
7946 object. */
7947 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7949 /* If we've already got an offset for this virtual base, we
7950 don't need another one. */
7951 if (BINFO_VTABLE_PATH_MARKED (b))
7952 continue;
7953 SET_BINFO_VTABLE_PATH_MARKED (b);
7955 /* Figure out where we can find this vbase offset. */
7956 delta = size_binop (MULT_EXPR,
7957 vid->index,
7958 convert (ssizetype,
7959 TYPE_SIZE_UNIT (vtable_entry_type)));
7960 if (vid->primary_vtbl_p)
7961 BINFO_VPTR_FIELD (b) = delta;
7963 if (binfo != TYPE_BINFO (t))
7965 tree orig_vbase;
7967 /* Find the instance of this virtual base in the type of BINFO. */
7968 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7969 BINFO_TYPE (binfo));
7971 /* The vbase offset had better be the same. */
7972 if (!tree_int_cst_equal (delta,
7973 BINFO_VPTR_FIELD (orig_vbase)))
7974 my_friendly_abort (20000403);
7977 /* The next vbase will come at a more negative offset. */
7978 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7980 /* The initializer is the delta from BINFO to this virtual base.
7981 The vbase offsets go in reverse inheritance-graph order, and
7982 we are walking in inheritance graph order so these end up in
7983 the right order. */
7984 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (binfo));
7985 *vid->last_init
7986 = build_tree_list (NULL_TREE,
7987 fold (build1 (NOP_EXPR,
7988 vtable_entry_type,
7989 delta)));
7990 vid->last_init = &TREE_CHAIN (*vid->last_init);
7994 /* Adds the initializers for the vcall offset entries in the vtable
7995 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7996 to VID->INITS. */
7998 static void
7999 build_vcall_offset_vtbl_entries (binfo, vid)
8000 tree binfo;
8001 vtbl_init_data *vid;
8003 /* Under the old ABI, the adjustments to the `this' pointer were made
8004 elsewhere. */
8005 if (!vcall_offsets_in_vtable_p ())
8006 return;
8008 /* We only need these entries if this base is a virtual base. */
8009 if (!TREE_VIA_VIRTUAL (binfo))
8010 return;
8012 /* We need a vcall offset for each of the virtual functions in this
8013 vtable. For example:
8015 class A { virtual void f (); };
8016 class B1 : virtual public A { virtual void f (); };
8017 class B2 : virtual public A { virtual void f (); };
8018 class C: public B1, public B2 { virtual void f (); };
8020 A C object has a primary base of B1, which has a primary base of A. A
8021 C also has a secondary base of B2, which no longer has a primary base
8022 of A. So the B2-in-C construction vtable needs a secondary vtable for
8023 A, which will adjust the A* to a B2* to call f. We have no way of
8024 knowing what (or even whether) this offset will be when we define B2,
8025 so we store this "vcall offset" in the A sub-vtable and look it up in
8026 a "virtual thunk" for B2::f.
8028 We need entries for all the functions in our primary vtable and
8029 in our non-virtual bases' secondary vtables. */
8030 vid->vbase = binfo;
8031 /* Now, walk through the non-virtual bases, adding vcall offsets. */
8032 add_vcall_offset_vtbl_entries_r (binfo, vid);
8035 /* Build vcall offsets, starting with those for BINFO. */
8037 static void
8038 add_vcall_offset_vtbl_entries_r (binfo, vid)
8039 tree binfo;
8040 vtbl_init_data *vid;
8042 int i;
8043 tree primary_binfo;
8045 /* Don't walk into virtual bases -- except, of course, for the
8046 virtual base for which we are building vcall offsets. Any
8047 primary virtual base will have already had its offsets generated
8048 through the recursion in build_vcall_and_vbase_vtbl_entries. */
8049 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
8050 return;
8052 /* If BINFO has a primary base, process it first. */
8053 primary_binfo = get_primary_binfo (binfo);
8054 if (primary_binfo)
8055 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
8057 /* Add BINFO itself to the list. */
8058 add_vcall_offset_vtbl_entries_1 (binfo, vid);
8060 /* Scan the non-primary bases of BINFO. */
8061 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
8063 tree base_binfo;
8065 base_binfo = BINFO_BASETYPE (binfo, i);
8066 if (base_binfo != primary_binfo)
8067 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
8071 /* Called from build_vcall_offset_vtbl_entries_r. */
8073 static void
8074 add_vcall_offset_vtbl_entries_1 (binfo, vid)
8075 tree binfo;
8076 vtbl_init_data* vid;
8078 tree derived_virtuals;
8079 tree base_virtuals;
8080 tree orig_virtuals;
8081 tree binfo_inits;
8082 /* If BINFO is a primary base, the most derived class which has BINFO as
8083 a primary base; otherwise, just BINFO. */
8084 tree non_primary_binfo;
8086 binfo_inits = NULL_TREE;
8088 /* We might be a primary base class. Go up the inheritance hierarchy
8089 until we find the most derived class of which we are a primary base:
8090 it is the BINFO_VIRTUALS there that we need to consider. */
8091 non_primary_binfo = binfo;
8092 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8094 tree b;
8096 /* If we have reached a virtual base, then it must be vid->vbase,
8097 because we ignore other virtual bases in
8098 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
8099 base (possibly multi-level) of vid->binfo, or we wouldn't
8100 have called build_vcall_and_vbase_vtbl_entries for it. But it
8101 might be a lost primary, so just skip down to vid->binfo. */
8102 if (TREE_VIA_VIRTUAL (non_primary_binfo))
8104 if (non_primary_binfo != vid->vbase)
8105 abort ();
8106 non_primary_binfo = vid->binfo;
8107 break;
8110 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8111 if (get_primary_binfo (b) != non_primary_binfo)
8112 break;
8113 non_primary_binfo = b;
8116 if (vid->ctor_vtbl_p)
8117 /* For a ctor vtable we need the equivalent binfo within the hierarchy
8118 where rtti_binfo is the most derived type. */
8119 non_primary_binfo = get_original_base
8120 (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
8122 /* Make entries for the rest of the virtuals. */
8123 for (base_virtuals = BINFO_VIRTUALS (binfo),
8124 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
8125 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
8126 base_virtuals;
8127 base_virtuals = TREE_CHAIN (base_virtuals),
8128 derived_virtuals = TREE_CHAIN (derived_virtuals),
8129 orig_virtuals = TREE_CHAIN (orig_virtuals))
8131 tree orig_fn;
8132 tree fn;
8133 tree base;
8134 tree base_binfo;
8135 size_t i;
8136 tree vcall_offset;
8138 /* Find the declaration that originally caused this function to
8139 be present in BINFO_TYPE (binfo). */
8140 orig_fn = BV_FN (orig_virtuals);
8142 /* When processing BINFO, we only want to generate vcall slots for
8143 function slots introduced in BINFO. So don't try to generate
8144 one if the function isn't even defined in BINFO. */
8145 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
8146 continue;
8148 /* Find the overriding function. */
8149 fn = BV_FN (derived_virtuals);
8151 /* If there is already an entry for a function with the same
8152 signature as FN, then we do not need a second vcall offset.
8153 Check the list of functions already present in the derived
8154 class vtable. */
8155 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
8157 tree derived_entry;
8159 derived_entry = VARRAY_TREE (vid->fns, i);
8160 if (same_signature_p (BV_FN (derived_entry), fn)
8161 /* We only use one vcall offset for virtual destructors,
8162 even though there are two virtual table entries. */
8163 || (DECL_DESTRUCTOR_P (BV_FN (derived_entry))
8164 && DECL_DESTRUCTOR_P (fn)))
8166 if (!vid->ctor_vtbl_p)
8167 BV_VCALL_INDEX (derived_virtuals)
8168 = BV_VCALL_INDEX (derived_entry);
8169 break;
8172 if (i != VARRAY_ACTIVE_SIZE (vid->fns))
8173 continue;
8175 /* The FN comes from BASE. So, we must calculate the adjustment from
8176 vid->vbase to BASE. We can just look for BASE in the complete
8177 object because we are converting from a virtual base, so if there
8178 were multiple copies, there would not be a unique final overrider
8179 and vid->derived would be ill-formed. */
8180 base = DECL_CONTEXT (fn);
8181 base_binfo = get_binfo (base, vid->derived, /*protect=*/0);
8183 /* Compute the vcall offset. */
8184 /* As mentioned above, the vbase we're working on is a primary base of
8185 vid->binfo. But it might be a lost primary, so its BINFO_OFFSET
8186 might be wrong, so we just use the BINFO_OFFSET from vid->binfo. */
8187 vcall_offset = BINFO_OFFSET (vid->binfo);
8188 vcall_offset = size_diffop (BINFO_OFFSET (base_binfo),
8189 vcall_offset);
8190 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
8191 vcall_offset));
8193 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
8194 vid->last_init = &TREE_CHAIN (*vid->last_init);
8196 /* Keep track of the vtable index where this vcall offset can be
8197 found. For a construction vtable, we already made this
8198 annotation when we built the original vtable. */
8199 if (!vid->ctor_vtbl_p)
8200 BV_VCALL_INDEX (derived_virtuals) = vid->index;
8202 /* The next vcall offset will be found at a more negative
8203 offset. */
8204 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
8206 /* Keep track of this function. */
8207 VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
8211 /* Return vtbl initializers for the RTTI entries coresponding to the
8212 BINFO's vtable. The RTTI entries should indicate the object given
8213 by VID->rtti_binfo. */
8215 static void
8216 build_rtti_vtbl_entries (binfo, vid)
8217 tree binfo;
8218 vtbl_init_data *vid;
8220 tree b;
8221 tree t;
8222 tree basetype;
8223 tree offset;
8224 tree decl;
8225 tree init;
8227 basetype = BINFO_TYPE (binfo);
8228 t = BINFO_TYPE (vid->rtti_binfo);
8230 /* To find the complete object, we will first convert to our most
8231 primary base, and then add the offset in the vtbl to that value. */
8232 b = binfo;
8233 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8234 && !BINFO_LOST_PRIMARY_P (b))
8236 tree primary_base;
8238 primary_base = get_primary_binfo (b);
8239 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
8240 b = primary_base;
8242 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8244 /* The second entry is the address of the typeinfo object. */
8245 if (flag_rtti)
8246 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
8247 else
8248 decl = integer_zero_node;
8250 /* Convert the declaration to a type that can be stored in the
8251 vtable. */
8252 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
8253 TREE_CONSTANT (init) = 1;
8254 *vid->last_init = build_tree_list (NULL_TREE, init);
8255 vid->last_init = &TREE_CHAIN (*vid->last_init);
8257 /* Add the offset-to-top entry. It comes earlier in the vtable that
8258 the the typeinfo entry. */
8259 if (flag_vtable_thunks)
8261 /* Convert the offset to look like a function pointer, so that
8262 we can put it in the vtable. */
8263 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
8264 TREE_CONSTANT (init) = 1;
8265 *vid->last_init = build_tree_list (NULL_TREE, init);
8266 vid->last_init = &TREE_CHAIN (*vid->last_init);
8270 /* Build an entry in the virtual function table. DELTA is the offset
8271 for the `this' pointer. VCALL_INDEX is the vtable index containing
8272 the vcall offset; NULL_TREE if none. ENTRY is the virtual function
8273 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
8274 but it may not actually be a virtual function table pointer. (For
8275 example, it might be the address of the RTTI object, under the new
8276 ABI.) */
8278 static tree
8279 build_vtable_entry (delta, vcall_index, entry)
8280 tree delta;
8281 tree vcall_index;
8282 tree entry;
8284 if (flag_vtable_thunks)
8286 tree fn;
8288 fn = TREE_OPERAND (entry, 0);
8289 if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
8290 && fn != abort_fndecl)
8292 entry = make_thunk (entry, delta, vcall_index);
8293 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
8294 TREE_READONLY (entry) = 1;
8295 TREE_CONSTANT (entry) = 1;
8297 #ifdef GATHER_STATISTICS
8298 n_vtable_entries += 1;
8299 #endif
8300 return entry;
8302 else
8304 tree elems = tree_cons (NULL_TREE, delta,
8305 tree_cons (NULL_TREE, integer_zero_node,
8306 build_tree_list (NULL_TREE, entry)));
8307 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
8309 /* We don't use vcall offsets when not using vtable thunks. */
8310 my_friendly_assert (vcall_index == NULL_TREE, 20000125);
8312 /* DELTA used to be constructed by `size_int' and/or size_binop,
8313 which caused overflow problems when it was negative. That should
8314 be fixed now. */
8316 if (! int_fits_type_p (delta, delta_type_node))
8318 if (flag_huge_objects)
8319 sorry ("object size exceeds built-in limit for virtual function table implementation");
8320 else
8321 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
8324 TREE_CONSTANT (entry) = 1;
8325 TREE_STATIC (entry) = 1;
8326 TREE_READONLY (entry) = 1;
8328 #ifdef GATHER_STATISTICS
8329 n_vtable_entries += 1;
8330 #endif
8332 return entry;