Implement C++0x unrestricted unions (N2544)
[official-gcc.git] / gcc / cp / class.c
bloba572af83f637c3d4dfec909b03e7187f1b07af18
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, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* High-level class interface. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "flags.h"
33 #include "output.h"
34 #include "toplev.h"
35 #include "target.h"
36 #include "convert.h"
37 #include "cgraph.h"
38 #include "tree-dump.h"
39 #include "splay-tree.h"
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;
64 /* Nonzero if this class is no longer open, because of a call to
65 push_to_top_level. */
66 size_t hidden;
67 }* class_stack_node_t;
69 typedef struct vtbl_init_data_s
71 /* The base for which we're building initializers. */
72 tree binfo;
73 /* The type of the most-derived type. */
74 tree derived;
75 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
76 unless ctor_vtbl_p is true. */
77 tree rtti_binfo;
78 /* The negative-index vtable initializers built up so far. These
79 are in order from least negative index to most negative index. */
80 VEC(constructor_elt,gc) *inits;
81 /* The binfo for the virtual base for which we're building
82 vcall offset initializers. */
83 tree vbase;
84 /* The functions in vbase for which we have already provided vcall
85 offsets. */
86 VEC(tree,gc) *fns;
87 /* The vtable index of the next vcall or vbase offset. */
88 tree index;
89 /* Nonzero if we are building the initializer for the primary
90 vtable. */
91 int primary_vtbl_p;
92 /* Nonzero if we are building the initializer for a construction
93 vtable. */
94 int ctor_vtbl_p;
95 /* True when adding vcall offset entries to the vtable. False when
96 merely computing the indices. */
97 bool generate_vcall_entries;
98 } vtbl_init_data;
100 /* The type of a function passed to walk_subobject_offsets. */
101 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
103 /* The stack itself. This is a dynamically resized array. The
104 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
105 static int current_class_stack_size;
106 static class_stack_node_t current_class_stack;
108 /* The size of the largest empty class seen in this translation unit. */
109 static GTY (()) tree sizeof_biggest_empty_class;
111 /* An array of all local classes present in this translation unit, in
112 declaration order. */
113 VEC(tree,gc) *local_classes;
115 static tree get_vfield_name (tree);
116 static void finish_struct_anon (tree);
117 static tree get_vtable_name (tree);
118 static tree get_basefndecls (tree, tree);
119 static int build_primary_vtable (tree, tree);
120 static int build_secondary_vtable (tree);
121 static void finish_vtbls (tree);
122 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
123 static void finish_struct_bits (tree);
124 static int alter_access (tree, tree, tree);
125 static void handle_using_decl (tree, tree);
126 static tree dfs_modify_vtables (tree, void *);
127 static tree modify_all_vtables (tree, tree);
128 static void determine_primary_bases (tree);
129 static void finish_struct_methods (tree);
130 static void maybe_warn_about_overly_private_class (tree);
131 static int method_name_cmp (const void *, const void *);
132 static int resort_method_name_cmp (const void *, const void *);
133 static void add_implicitly_declared_members (tree, int, int, int, int);
134 static tree fixed_type_or_null (tree, int *, int *);
135 static tree build_simple_base_path (tree expr, tree binfo);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
138 VEC(constructor_elt,gc) **);
139 static int count_fields (tree);
140 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
141 static bool check_bitfield_decl (tree);
142 static void check_field_decl (tree, tree, int *, int *, int *, int *, int *);
143 static void check_field_decls (tree, tree *, int *, int *, int *, int *);
144 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
145 static void build_base_fields (record_layout_info, splay_tree, tree *);
146 static void check_methods (tree);
147 static void remove_zero_width_bit_fields (tree);
148 static void check_bases (tree, int *, int *, int *, int *);
149 static void check_bases_and_members (tree);
150 static tree create_vtable_ptr (tree, tree *);
151 static void include_empty_classes (record_layout_info);
152 static void layout_class_type (tree, tree *);
153 static void propagate_binfo_offsets (tree, tree);
154 static void layout_virtual_bases (record_layout_info, splay_tree);
155 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
156 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
157 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
158 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
159 static void add_vcall_offset (tree, tree, vtbl_init_data *);
160 static void layout_vtable_decl (tree, int);
161 static tree dfs_find_final_overrider_pre (tree, void *);
162 static tree dfs_find_final_overrider_post (tree, void *);
163 static tree find_final_overrider (tree, tree, tree);
164 static int make_new_vtable (tree, tree);
165 static tree get_primary_binfo (tree);
166 static int maybe_indent_hierarchy (FILE *, int, int);
167 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
168 static void dump_class_hierarchy (tree);
169 static void dump_class_hierarchy_1 (FILE *, int, tree);
170 static void dump_array (FILE *, tree);
171 static void dump_vtable (tree, tree, tree);
172 static void dump_vtt (tree, tree);
173 static void dump_thunk (FILE *, int, tree);
174 static tree build_vtable (tree, tree, tree);
175 static void initialize_vtable (tree, VEC(constructor_elt,gc) *);
176 static void layout_nonempty_base_or_field (record_layout_info,
177 tree, tree, splay_tree);
178 static tree end_of_class (tree, int);
179 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
180 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
181 VEC(constructor_elt,gc) **);
182 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
183 VEC(constructor_elt,gc) **);
184 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
185 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
186 static void clone_constructors_and_destructors (tree);
187 static tree build_clone (tree, tree);
188 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
189 static void build_ctor_vtbl_group (tree, tree);
190 static void build_vtt (tree);
191 static tree binfo_ctor_vtable (tree);
192 static void build_vtt_inits (tree, tree, VEC(constructor_elt,gc) **, tree *);
193 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
194 static tree dfs_fixup_binfo_vtbls (tree, void *);
195 static int record_subobject_offset (tree, tree, splay_tree);
196 static int check_subobject_offset (tree, tree, splay_tree);
197 static int walk_subobject_offsets (tree, subobject_offset_fn,
198 tree, splay_tree, tree, int);
199 static void record_subobject_offsets (tree, tree, splay_tree, bool);
200 static int layout_conflict_p (tree, tree, splay_tree, int);
201 static int splay_tree_compare_integer_csts (splay_tree_key k1,
202 splay_tree_key k2);
203 static void warn_about_ambiguous_bases (tree);
204 static bool type_requires_array_cookie (tree);
205 static bool contains_empty_class_p (tree);
206 static bool base_derived_from (tree, tree);
207 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
208 static tree end_of_base (tree);
209 static tree get_vcall_index (tree, tree);
211 /* Variables shared between class.c and call.c. */
213 #ifdef GATHER_STATISTICS
214 int n_vtables = 0;
215 int n_vtable_entries = 0;
216 int n_vtable_searches = 0;
217 int n_vtable_elems = 0;
218 int n_convert_harshness = 0;
219 int n_compute_conversion_costs = 0;
220 int n_inner_fields_searched = 0;
221 #endif
223 /* Convert to or from a base subobject. EXPR is an expression of type
224 `A' or `A*', an expression of type `B' or `B*' is returned. To
225 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
226 the B base instance within A. To convert base A to derived B, CODE
227 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
228 In this latter case, A must not be a morally virtual base of B.
229 NONNULL is true if EXPR is known to be non-NULL (this is only
230 needed when EXPR is of pointer type). CV qualifiers are preserved
231 from EXPR. */
233 tree
234 build_base_path (enum tree_code code,
235 tree expr,
236 tree binfo,
237 int nonnull)
239 tree v_binfo = NULL_TREE;
240 tree d_binfo = NULL_TREE;
241 tree probe;
242 tree offset;
243 tree target_type;
244 tree null_test = NULL;
245 tree ptr_target_type;
246 int fixed_type_p;
247 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
248 bool has_empty = false;
249 bool virtual_access;
251 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
252 return error_mark_node;
254 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
256 d_binfo = probe;
257 if (is_empty_class (BINFO_TYPE (probe)))
258 has_empty = true;
259 if (!v_binfo && BINFO_VIRTUAL_P (probe))
260 v_binfo = probe;
263 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
264 if (want_pointer)
265 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
267 gcc_assert ((code == MINUS_EXPR
268 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
269 || (code == PLUS_EXPR
270 && SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe)));
272 if (binfo == d_binfo)
273 /* Nothing to do. */
274 return expr;
276 if (code == MINUS_EXPR && v_binfo)
278 error ("cannot convert from base %qT to derived type %qT via virtual base %qT",
279 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
280 return error_mark_node;
283 if (!want_pointer)
284 /* This must happen before the call to save_expr. */
285 expr = cp_build_unary_op (ADDR_EXPR, expr, 0, tf_warning_or_error);
286 else
287 expr = mark_rvalue_use (expr);
289 offset = BINFO_OFFSET (binfo);
290 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
291 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
293 /* Do we need to look in the vtable for the real offset? */
294 virtual_access = (v_binfo && fixed_type_p <= 0);
296 /* Don't bother with the calculations inside sizeof; they'll ICE if the
297 source type is incomplete and the pointer value doesn't matter. */
298 if (cp_unevaluated_operand != 0)
300 expr = build_nop (build_pointer_type (target_type), expr);
301 if (!want_pointer)
302 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
303 return expr;
306 /* Do we need to check for a null pointer? */
307 if (want_pointer && !nonnull)
309 /* If we know the conversion will not actually change the value
310 of EXPR, then we can avoid testing the expression for NULL.
311 We have to avoid generating a COMPONENT_REF for a base class
312 field, because other parts of the compiler know that such
313 expressions are always non-NULL. */
314 if (!virtual_access && integer_zerop (offset))
316 tree class_type;
317 /* TARGET_TYPE has been extracted from BINFO, and, is
318 therefore always cv-unqualified. Extract the
319 cv-qualifiers from EXPR so that the expression returned
320 matches the input. */
321 class_type = TREE_TYPE (TREE_TYPE (expr));
322 target_type
323 = cp_build_qualified_type (target_type,
324 cp_type_quals (class_type));
325 return build_nop (build_pointer_type (target_type), expr);
327 null_test = error_mark_node;
330 /* Protect against multiple evaluation if necessary. */
331 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
332 expr = save_expr (expr);
334 /* Now that we've saved expr, build the real null test. */
335 if (null_test)
337 tree zero = cp_convert (TREE_TYPE (expr), integer_zero_node);
338 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
339 expr, zero);
342 /* If this is a simple base reference, express it as a COMPONENT_REF. */
343 if (code == PLUS_EXPR && !virtual_access
344 /* We don't build base fields for empty bases, and they aren't very
345 interesting to the optimizers anyway. */
346 && !has_empty)
348 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error);
349 expr = build_simple_base_path (expr, binfo);
350 if (want_pointer)
351 expr = build_address (expr);
352 target_type = TREE_TYPE (expr);
353 goto out;
356 if (virtual_access)
358 /* Going via virtual base V_BINFO. We need the static offset
359 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
360 V_BINFO. That offset is an entry in D_BINFO's vtable. */
361 tree v_offset;
363 if (fixed_type_p < 0 && in_base_initializer)
365 /* In a base member initializer, we cannot rely on the
366 vtable being set up. We have to indirect via the
367 vtt_parm. */
368 tree t;
370 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
371 t = build_pointer_type (t);
372 v_offset = convert (t, current_vtt_parm);
373 v_offset = cp_build_indirect_ref (v_offset, RO_NULL,
374 tf_warning_or_error);
376 else
377 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
378 tf_warning_or_error),
379 TREE_TYPE (TREE_TYPE (expr)));
381 v_offset = build2 (POINTER_PLUS_EXPR, TREE_TYPE (v_offset),
382 v_offset, fold_convert (sizetype, BINFO_VPTR_FIELD (v_binfo)));
383 v_offset = build1 (NOP_EXPR,
384 build_pointer_type (ptrdiff_type_node),
385 v_offset);
386 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, tf_warning_or_error);
387 TREE_CONSTANT (v_offset) = 1;
389 offset = convert_to_integer (ptrdiff_type_node,
390 size_diffop_loc (input_location, offset,
391 BINFO_OFFSET (v_binfo)));
393 if (!integer_zerop (offset))
394 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
396 if (fixed_type_p < 0)
397 /* Negative fixed_type_p means this is a constructor or destructor;
398 virtual base layout is fixed in in-charge [cd]tors, but not in
399 base [cd]tors. */
400 offset = build3 (COND_EXPR, ptrdiff_type_node,
401 build2 (EQ_EXPR, boolean_type_node,
402 current_in_charge_parm, integer_zero_node),
403 v_offset,
404 convert_to_integer (ptrdiff_type_node,
405 BINFO_OFFSET (binfo)));
406 else
407 offset = v_offset;
410 target_type = cp_build_qualified_type
411 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
412 ptr_target_type = build_pointer_type (target_type);
413 if (want_pointer)
414 target_type = ptr_target_type;
416 expr = build1 (NOP_EXPR, ptr_target_type, expr);
418 if (!integer_zerop (offset))
420 offset = fold_convert (sizetype, offset);
421 if (code == MINUS_EXPR)
422 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
423 expr = build2 (POINTER_PLUS_EXPR, ptr_target_type, expr, offset);
425 else
426 null_test = NULL;
428 if (!want_pointer)
429 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error);
431 out:
432 if (null_test)
433 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
434 fold_build1_loc (input_location, NOP_EXPR, target_type,
435 integer_zero_node));
437 return expr;
440 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
441 Perform a derived-to-base conversion by recursively building up a
442 sequence of COMPONENT_REFs to the appropriate base fields. */
444 static tree
445 build_simple_base_path (tree expr, tree binfo)
447 tree type = BINFO_TYPE (binfo);
448 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
449 tree field;
451 if (d_binfo == NULL_TREE)
453 tree temp;
455 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
457 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
458 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
459 an lvalue in the front end; only _DECLs and _REFs are lvalues
460 in the back end. */
461 temp = unary_complex_lvalue (ADDR_EXPR, expr);
462 if (temp)
463 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
465 return expr;
468 /* Recurse. */
469 expr = build_simple_base_path (expr, d_binfo);
471 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
472 field; field = TREE_CHAIN (field))
473 /* Is this the base field created by build_base_field? */
474 if (TREE_CODE (field) == FIELD_DECL
475 && DECL_FIELD_IS_BASE (field)
476 && TREE_TYPE (field) == type)
478 /* We don't use build_class_member_access_expr here, as that
479 has unnecessary checks, and more importantly results in
480 recursive calls to dfs_walk_once. */
481 int type_quals = cp_type_quals (TREE_TYPE (expr));
483 expr = build3 (COMPONENT_REF,
484 cp_build_qualified_type (type, type_quals),
485 expr, field, NULL_TREE);
486 expr = fold_if_not_in_template (expr);
488 /* Mark the expression const or volatile, as appropriate.
489 Even though we've dealt with the type above, we still have
490 to mark the expression itself. */
491 if (type_quals & TYPE_QUAL_CONST)
492 TREE_READONLY (expr) = 1;
493 if (type_quals & TYPE_QUAL_VOLATILE)
494 TREE_THIS_VOLATILE (expr) = 1;
496 return expr;
499 /* Didn't find the base field?!? */
500 gcc_unreachable ();
503 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
504 type is a class type or a pointer to a class type. In the former
505 case, TYPE is also a class type; in the latter it is another
506 pointer type. If CHECK_ACCESS is true, an error message is emitted
507 if TYPE is inaccessible. If OBJECT has pointer type, the value is
508 assumed to be non-NULL. */
510 tree
511 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
512 tsubst_flags_t complain)
514 tree binfo;
515 tree object_type;
516 base_access access;
518 if (TYPE_PTR_P (TREE_TYPE (object)))
520 object_type = TREE_TYPE (TREE_TYPE (object));
521 type = TREE_TYPE (type);
523 else
524 object_type = TREE_TYPE (object);
526 access = check_access ? ba_check : ba_unique;
527 if (!(complain & tf_error))
528 access |= ba_quiet;
529 binfo = lookup_base (object_type, type,
530 access,
531 NULL);
532 if (!binfo || binfo == error_mark_node)
533 return error_mark_node;
535 return build_base_path (PLUS_EXPR, object, binfo, nonnull);
538 /* EXPR is an expression with unqualified class type. BASE is a base
539 binfo of that class type. Returns EXPR, converted to the BASE
540 type. This function assumes that EXPR is the most derived class;
541 therefore virtual bases can be found at their static offsets. */
543 tree
544 convert_to_base_statically (tree expr, tree base)
546 tree expr_type;
548 expr_type = TREE_TYPE (expr);
549 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
551 tree pointer_type;
553 pointer_type = build_pointer_type (expr_type);
555 /* We use fold_build2 and fold_convert below to simplify the trees
556 provided to the optimizers. It is not safe to call these functions
557 when processing a template because they do not handle C++-specific
558 trees. */
559 gcc_assert (!processing_template_decl);
560 expr = cp_build_unary_op (ADDR_EXPR, expr, /*noconvert=*/1,
561 tf_warning_or_error);
562 if (!integer_zerop (BINFO_OFFSET (base)))
563 expr = fold_build2_loc (input_location,
564 POINTER_PLUS_EXPR, pointer_type, expr,
565 fold_convert (sizetype, BINFO_OFFSET (base)));
566 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
567 expr = build_fold_indirect_ref_loc (input_location, expr);
570 return expr;
574 tree
575 build_vfield_ref (tree datum, tree type)
577 tree vfield, vcontext;
579 if (datum == error_mark_node)
580 return error_mark_node;
582 /* First, convert to the requested type. */
583 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
584 datum = convert_to_base (datum, type, /*check_access=*/false,
585 /*nonnull=*/true, tf_warning_or_error);
587 /* Second, the requested type may not be the owner of its own vptr.
588 If not, convert to the base class that owns it. We cannot use
589 convert_to_base here, because VCONTEXT may appear more than once
590 in the inheritance hierarchy of TYPE, and thus direct conversion
591 between the types may be ambiguous. Following the path back up
592 one step at a time via primary bases avoids the problem. */
593 vfield = TYPE_VFIELD (type);
594 vcontext = DECL_CONTEXT (vfield);
595 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
597 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
598 type = TREE_TYPE (datum);
601 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
604 /* Given an object INSTANCE, return an expression which yields the
605 vtable element corresponding to INDEX. There are many special
606 cases for INSTANCE which we take care of here, mainly to avoid
607 creating extra tree nodes when we don't have to. */
609 static tree
610 build_vtbl_ref_1 (tree instance, tree idx)
612 tree aref;
613 tree vtbl = NULL_TREE;
615 /* Try to figure out what a reference refers to, and
616 access its virtual function table directly. */
618 int cdtorp = 0;
619 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
621 tree basetype = non_reference (TREE_TYPE (instance));
623 if (fixed_type && !cdtorp)
625 tree binfo = lookup_base (fixed_type, basetype,
626 ba_unique | ba_quiet, NULL);
627 if (binfo)
628 vtbl = unshare_expr (BINFO_VTABLE (binfo));
631 if (!vtbl)
632 vtbl = build_vfield_ref (instance, basetype);
634 aref = build_array_ref (input_location, vtbl, idx);
635 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
637 return aref;
640 tree
641 build_vtbl_ref (tree instance, tree idx)
643 tree aref = build_vtbl_ref_1 (instance, idx);
645 return aref;
648 /* Given a stable object pointer INSTANCE_PTR, return an expression which
649 yields a function pointer corresponding to vtable element INDEX. */
651 tree
652 build_vfn_ref (tree instance_ptr, tree idx)
654 tree aref;
656 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
657 tf_warning_or_error),
658 idx);
660 /* When using function descriptors, the address of the
661 vtable entry is treated as a function pointer. */
662 if (TARGET_VTABLE_USES_DESCRIPTORS)
663 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
664 cp_build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1,
665 tf_warning_or_error));
667 /* Remember this as a method reference, for later devirtualization. */
668 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
670 return aref;
673 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
674 for the given TYPE. */
676 static tree
677 get_vtable_name (tree type)
679 return mangle_vtbl_for_type (type);
682 /* DECL is an entity associated with TYPE, like a virtual table or an
683 implicitly generated constructor. Determine whether or not DECL
684 should have external or internal linkage at the object file
685 level. This routine does not deal with COMDAT linkage and other
686 similar complexities; it simply sets TREE_PUBLIC if it possible for
687 entities in other translation units to contain copies of DECL, in
688 the abstract. */
690 void
691 set_linkage_according_to_type (tree type, tree decl)
693 /* If TYPE involves a local class in a function with internal
694 linkage, then DECL should have internal linkage too. Other local
695 classes have no linkage -- but if their containing functions
696 have external linkage, it makes sense for DECL to have external
697 linkage too. That will allow template definitions to be merged,
698 for example. */
699 if (no_linkage_check (type, /*relaxed_p=*/true))
701 TREE_PUBLIC (decl) = 0;
702 DECL_INTERFACE_KNOWN (decl) = 1;
704 else
705 TREE_PUBLIC (decl) = 1;
708 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
709 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
710 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
712 static tree
713 build_vtable (tree class_type, tree name, tree vtable_type)
715 tree decl;
717 decl = build_lang_decl (VAR_DECL, name, vtable_type);
718 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
719 now to avoid confusion in mangle_decl. */
720 SET_DECL_ASSEMBLER_NAME (decl, name);
721 DECL_CONTEXT (decl) = class_type;
722 DECL_ARTIFICIAL (decl) = 1;
723 TREE_STATIC (decl) = 1;
724 TREE_READONLY (decl) = 1;
725 DECL_VIRTUAL_P (decl) = 1;
726 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
727 DECL_VTABLE_OR_VTT_P (decl) = 1;
728 /* At one time the vtable info was grabbed 2 words at a time. This
729 fails on sparc unless you have 8-byte alignment. (tiemann) */
730 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
731 DECL_ALIGN (decl));
732 set_linkage_according_to_type (class_type, decl);
733 /* The vtable has not been defined -- yet. */
734 DECL_EXTERNAL (decl) = 1;
735 DECL_NOT_REALLY_EXTERN (decl) = 1;
737 /* Mark the VAR_DECL node representing the vtable itself as a
738 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
739 is rather important that such things be ignored because any
740 effort to actually generate DWARF for them will run into
741 trouble when/if we encounter code like:
743 #pragma interface
744 struct S { virtual void member (); };
746 because the artificial declaration of the vtable itself (as
747 manufactured by the g++ front end) will say that the vtable is
748 a static member of `S' but only *after* the debug output for
749 the definition of `S' has already been output. This causes
750 grief because the DWARF entry for the definition of the vtable
751 will try to refer back to an earlier *declaration* of the
752 vtable as a static member of `S' and there won't be one. We
753 might be able to arrange to have the "vtable static member"
754 attached to the member list for `S' before the debug info for
755 `S' get written (which would solve the problem) but that would
756 require more intrusive changes to the g++ front end. */
757 DECL_IGNORED_P (decl) = 1;
759 return decl;
762 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
763 or even complete. If this does not exist, create it. If COMPLETE is
764 nonzero, then complete the definition of it -- that will render it
765 impossible to actually build the vtable, but is useful to get at those
766 which are known to exist in the runtime. */
768 tree
769 get_vtable_decl (tree type, int complete)
771 tree decl;
773 if (CLASSTYPE_VTABLES (type))
774 return CLASSTYPE_VTABLES (type);
776 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
777 CLASSTYPE_VTABLES (type) = decl;
779 if (complete)
781 DECL_EXTERNAL (decl) = 1;
782 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
785 return decl;
788 /* Build the primary virtual function table for TYPE. If BINFO is
789 non-NULL, build the vtable starting with the initial approximation
790 that it is the same as the one which is the head of the association
791 list. Returns a nonzero value if a new vtable is actually
792 created. */
794 static int
795 build_primary_vtable (tree binfo, tree type)
797 tree decl;
798 tree virtuals;
800 decl = get_vtable_decl (type, /*complete=*/0);
802 if (binfo)
804 if (BINFO_NEW_VTABLE_MARKED (binfo))
805 /* We have already created a vtable for this base, so there's
806 no need to do it again. */
807 return 0;
809 virtuals = copy_list (BINFO_VIRTUALS (binfo));
810 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
811 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
812 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
814 else
816 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
817 virtuals = NULL_TREE;
820 #ifdef GATHER_STATISTICS
821 n_vtables += 1;
822 n_vtable_elems += list_length (virtuals);
823 #endif
825 /* Initialize the association list for this type, based
826 on our first approximation. */
827 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
828 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
829 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
830 return 1;
833 /* Give BINFO a new virtual function table which is initialized
834 with a skeleton-copy of its original initialization. The only
835 entry that changes is the `delta' entry, so we can really
836 share a lot of structure.
838 FOR_TYPE is the most derived type which caused this table to
839 be needed.
841 Returns nonzero if we haven't met BINFO before.
843 The order in which vtables are built (by calling this function) for
844 an object must remain the same, otherwise a binary incompatibility
845 can result. */
847 static int
848 build_secondary_vtable (tree binfo)
850 if (BINFO_NEW_VTABLE_MARKED (binfo))
851 /* We already created a vtable for this base. There's no need to
852 do it again. */
853 return 0;
855 /* Remember that we've created a vtable for this BINFO, so that we
856 don't try to do so again. */
857 SET_BINFO_NEW_VTABLE_MARKED (binfo);
859 /* Make fresh virtual list, so we can smash it later. */
860 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
862 /* Secondary vtables are laid out as part of the same structure as
863 the primary vtable. */
864 BINFO_VTABLE (binfo) = NULL_TREE;
865 return 1;
868 /* Create a new vtable for BINFO which is the hierarchy dominated by
869 T. Return nonzero if we actually created a new vtable. */
871 static int
872 make_new_vtable (tree t, tree binfo)
874 if (binfo == TYPE_BINFO (t))
875 /* In this case, it is *type*'s vtable we are modifying. We start
876 with the approximation that its vtable is that of the
877 immediate base class. */
878 return build_primary_vtable (binfo, t);
879 else
880 /* This is our very own copy of `basetype' to play with. Later,
881 we will fill in all the virtual functions that override the
882 virtual functions in these base classes which are not defined
883 by the current type. */
884 return build_secondary_vtable (binfo);
887 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
888 (which is in the hierarchy dominated by T) list FNDECL as its
889 BV_FN. DELTA is the required constant adjustment from the `this'
890 pointer where the vtable entry appears to the `this' required when
891 the function is actually called. */
893 static void
894 modify_vtable_entry (tree t,
895 tree binfo,
896 tree fndecl,
897 tree delta,
898 tree *virtuals)
900 tree v;
902 v = *virtuals;
904 if (fndecl != BV_FN (v)
905 || !tree_int_cst_equal (delta, BV_DELTA (v)))
907 /* We need a new vtable for BINFO. */
908 if (make_new_vtable (t, binfo))
910 /* If we really did make a new vtable, we also made a copy
911 of the BINFO_VIRTUALS list. Now, we have to find the
912 corresponding entry in that list. */
913 *virtuals = BINFO_VIRTUALS (binfo);
914 while (BV_FN (*virtuals) != BV_FN (v))
915 *virtuals = TREE_CHAIN (*virtuals);
916 v = *virtuals;
919 BV_DELTA (v) = delta;
920 BV_VCALL_INDEX (v) = NULL_TREE;
921 BV_FN (v) = fndecl;
926 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
927 the USING_DECL naming METHOD. Returns true if the method could be
928 added to the method vec. */
930 bool
931 add_method (tree type, tree method, tree using_decl)
933 unsigned slot;
934 tree overload;
935 bool template_conv_p = false;
936 bool conv_p;
937 VEC(tree,gc) *method_vec;
938 bool complete_p;
939 bool insert_p = false;
940 tree current_fns;
941 tree fns;
943 if (method == error_mark_node)
944 return false;
946 complete_p = COMPLETE_TYPE_P (type);
947 conv_p = DECL_CONV_FN_P (method);
948 if (conv_p)
949 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
950 && DECL_TEMPLATE_CONV_FN_P (method));
952 method_vec = CLASSTYPE_METHOD_VEC (type);
953 if (!method_vec)
955 /* Make a new method vector. We start with 8 entries. We must
956 allocate at least two (for constructors and destructors), and
957 we're going to end up with an assignment operator at some
958 point as well. */
959 method_vec = VEC_alloc (tree, gc, 8);
960 /* Create slots for constructors and destructors. */
961 VEC_quick_push (tree, method_vec, NULL_TREE);
962 VEC_quick_push (tree, method_vec, NULL_TREE);
963 CLASSTYPE_METHOD_VEC (type) = method_vec;
966 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
967 grok_special_member_properties (method);
969 /* Constructors and destructors go in special slots. */
970 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
971 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
972 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
974 slot = CLASSTYPE_DESTRUCTOR_SLOT;
976 if (TYPE_FOR_JAVA (type))
978 if (!DECL_ARTIFICIAL (method))
979 error ("Java class %qT cannot have a destructor", type);
980 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
981 error ("Java class %qT cannot have an implicit non-trivial "
982 "destructor",
983 type);
986 else
988 tree m;
990 insert_p = true;
991 /* See if we already have an entry with this name. */
992 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
993 VEC_iterate (tree, method_vec, slot, m);
994 ++slot)
996 m = OVL_CURRENT (m);
997 if (template_conv_p)
999 if (TREE_CODE (m) == TEMPLATE_DECL
1000 && DECL_TEMPLATE_CONV_FN_P (m))
1001 insert_p = false;
1002 break;
1004 if (conv_p && !DECL_CONV_FN_P (m))
1005 break;
1006 if (DECL_NAME (m) == DECL_NAME (method))
1008 insert_p = false;
1009 break;
1011 if (complete_p
1012 && !DECL_CONV_FN_P (m)
1013 && DECL_NAME (m) > DECL_NAME (method))
1014 break;
1017 current_fns = insert_p ? NULL_TREE : VEC_index (tree, method_vec, slot);
1019 /* Check to see if we've already got this method. */
1020 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1022 tree fn = OVL_CURRENT (fns);
1023 tree fn_type;
1024 tree method_type;
1025 tree parms1;
1026 tree parms2;
1028 if (TREE_CODE (fn) != TREE_CODE (method))
1029 continue;
1031 /* [over.load] Member function declarations with the
1032 same name and the same parameter types cannot be
1033 overloaded if any of them is a static member
1034 function declaration.
1036 [namespace.udecl] When a using-declaration brings names
1037 from a base class into a derived class scope, member
1038 functions in the derived class override and/or hide member
1039 functions with the same name and parameter types in a base
1040 class (rather than conflicting). */
1041 fn_type = TREE_TYPE (fn);
1042 method_type = TREE_TYPE (method);
1043 parms1 = TYPE_ARG_TYPES (fn_type);
1044 parms2 = TYPE_ARG_TYPES (method_type);
1046 /* Compare the quals on the 'this' parm. Don't compare
1047 the whole types, as used functions are treated as
1048 coming from the using class in overload resolution. */
1049 if (! DECL_STATIC_FUNCTION_P (fn)
1050 && ! DECL_STATIC_FUNCTION_P (method)
1051 && TREE_TYPE (TREE_VALUE (parms1)) != error_mark_node
1052 && TREE_TYPE (TREE_VALUE (parms2)) != error_mark_node
1053 && (cp_type_quals (TREE_TYPE (TREE_VALUE (parms1)))
1054 != cp_type_quals (TREE_TYPE (TREE_VALUE (parms2)))))
1055 continue;
1057 /* For templates, the return type and template parameters
1058 must be identical. */
1059 if (TREE_CODE (fn) == TEMPLATE_DECL
1060 && (!same_type_p (TREE_TYPE (fn_type),
1061 TREE_TYPE (method_type))
1062 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1063 DECL_TEMPLATE_PARMS (method))))
1064 continue;
1066 if (! DECL_STATIC_FUNCTION_P (fn))
1067 parms1 = TREE_CHAIN (parms1);
1068 if (! DECL_STATIC_FUNCTION_P (method))
1069 parms2 = TREE_CHAIN (parms2);
1071 if (compparms (parms1, parms2)
1072 && (!DECL_CONV_FN_P (fn)
1073 || same_type_p (TREE_TYPE (fn_type),
1074 TREE_TYPE (method_type))))
1076 if (using_decl)
1078 if (DECL_CONTEXT (fn) == type)
1079 /* Defer to the local function. */
1080 return false;
1081 if (DECL_CONTEXT (fn) == DECL_CONTEXT (method))
1082 error ("repeated using declaration %q+D", using_decl);
1083 else
1084 error ("using declaration %q+D conflicts with a previous using declaration",
1085 using_decl);
1087 else
1089 error ("%q+#D cannot be overloaded", method);
1090 error ("with %q+#D", fn);
1093 /* We don't call duplicate_decls here to merge the
1094 declarations because that will confuse things if the
1095 methods have inline definitions. In particular, we
1096 will crash while processing the definitions. */
1097 return false;
1101 /* A class should never have more than one destructor. */
1102 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1103 return false;
1105 /* Add the new binding. */
1106 overload = build_overload (method, current_fns);
1108 if (conv_p)
1109 TYPE_HAS_CONVERSION (type) = 1;
1110 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1111 push_class_level_binding (DECL_NAME (method), overload);
1113 if (insert_p)
1115 bool reallocated;
1117 /* We only expect to add few methods in the COMPLETE_P case, so
1118 just make room for one more method in that case. */
1119 if (complete_p)
1120 reallocated = VEC_reserve_exact (tree, gc, method_vec, 1);
1121 else
1122 reallocated = VEC_reserve (tree, gc, method_vec, 1);
1123 if (reallocated)
1124 CLASSTYPE_METHOD_VEC (type) = method_vec;
1125 if (slot == VEC_length (tree, method_vec))
1126 VEC_quick_push (tree, method_vec, overload);
1127 else
1128 VEC_quick_insert (tree, method_vec, slot, overload);
1130 else
1131 /* Replace the current slot. */
1132 VEC_replace (tree, method_vec, slot, overload);
1133 return true;
1136 /* Subroutines of finish_struct. */
1138 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1139 legit, otherwise return 0. */
1141 static int
1142 alter_access (tree t, tree fdecl, tree access)
1144 tree elem;
1146 if (!DECL_LANG_SPECIFIC (fdecl))
1147 retrofit_lang_decl (fdecl);
1149 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1151 elem = purpose_member (t, DECL_ACCESS (fdecl));
1152 if (elem)
1154 if (TREE_VALUE (elem) != access)
1156 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1157 error ("conflicting access specifications for method"
1158 " %q+D, ignored", TREE_TYPE (fdecl));
1159 else
1160 error ("conflicting access specifications for field %qE, ignored",
1161 DECL_NAME (fdecl));
1163 else
1165 /* They're changing the access to the same thing they changed
1166 it to before. That's OK. */
1170 else
1172 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl);
1173 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1174 return 1;
1176 return 0;
1179 /* Process the USING_DECL, which is a member of T. */
1181 static void
1182 handle_using_decl (tree using_decl, tree t)
1184 tree decl = USING_DECL_DECLS (using_decl);
1185 tree name = DECL_NAME (using_decl);
1186 tree access
1187 = TREE_PRIVATE (using_decl) ? access_private_node
1188 : TREE_PROTECTED (using_decl) ? access_protected_node
1189 : access_public_node;
1190 tree flist = NULL_TREE;
1191 tree old_value;
1193 gcc_assert (!processing_template_decl && decl);
1195 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false);
1196 if (old_value)
1198 if (is_overloaded_fn (old_value))
1199 old_value = OVL_CURRENT (old_value);
1201 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1202 /* OK */;
1203 else
1204 old_value = NULL_TREE;
1207 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1209 if (is_overloaded_fn (decl))
1210 flist = decl;
1212 if (! old_value)
1214 else if (is_overloaded_fn (old_value))
1216 if (flist)
1217 /* It's OK to use functions from a base when there are functions with
1218 the same name already present in the current class. */;
1219 else
1221 error ("%q+D invalid in %q#T", using_decl, t);
1222 error (" because of local method %q+#D with same name",
1223 OVL_CURRENT (old_value));
1224 return;
1227 else if (!DECL_ARTIFICIAL (old_value))
1229 error ("%q+D invalid in %q#T", using_decl, t);
1230 error (" because of local member %q+#D with same name", old_value);
1231 return;
1234 /* Make type T see field decl FDECL with access ACCESS. */
1235 if (flist)
1236 for (; flist; flist = OVL_NEXT (flist))
1238 add_method (t, OVL_CURRENT (flist), using_decl);
1239 alter_access (t, OVL_CURRENT (flist), access);
1241 else
1242 alter_access (t, decl, access);
1245 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1246 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1247 properties of the bases. */
1249 static void
1250 check_bases (tree t,
1251 int* cant_have_const_ctor_p,
1252 int* no_const_asn_ref_p,
1253 int* cant_have_lazy_ctor,
1254 int* cant_have_lazy_opeq)
1256 int i;
1257 int seen_non_virtual_nearly_empty_base_p;
1258 tree base_binfo;
1259 tree binfo;
1260 tree field = NULL_TREE;
1262 seen_non_virtual_nearly_empty_base_p = 0;
1264 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1265 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
1266 if (TREE_CODE (field) == FIELD_DECL)
1267 break;
1269 for (binfo = TYPE_BINFO (t), i = 0;
1270 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1272 tree basetype = TREE_TYPE (base_binfo);
1274 gcc_assert (COMPLETE_TYPE_P (basetype));
1276 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1277 here because the case of virtual functions but non-virtual
1278 dtor is handled in finish_struct_1. */
1279 if (!TYPE_POLYMORPHIC_P (basetype))
1280 warning (OPT_Weffc__,
1281 "base class %q#T has a non-virtual destructor", basetype);
1283 /* If the base class doesn't have copy constructors or
1284 assignment operators that take const references, then the
1285 derived class cannot have such a member automatically
1286 generated. */
1287 if (TYPE_HAS_COPY_CTOR (basetype)
1288 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1289 *cant_have_const_ctor_p = 1;
1290 if (TYPE_HAS_COPY_ASSIGN (basetype)
1291 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1292 *no_const_asn_ref_p = 1;
1293 if (TYPE_HAS_USER_CONSTRUCTOR (basetype))
1294 *cant_have_lazy_ctor = 1;
1295 if (TYPE_HAS_USER_OPEQ (basetype))
1296 *cant_have_lazy_opeq = 1;
1298 if (BINFO_VIRTUAL_P (base_binfo))
1299 /* A virtual base does not effect nearly emptiness. */
1301 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1303 if (seen_non_virtual_nearly_empty_base_p)
1304 /* And if there is more than one nearly empty base, then the
1305 derived class is not nearly empty either. */
1306 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1307 else
1308 /* Remember we've seen one. */
1309 seen_non_virtual_nearly_empty_base_p = 1;
1311 else if (!is_empty_class (basetype))
1312 /* If the base class is not empty or nearly empty, then this
1313 class cannot be nearly empty. */
1314 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1316 /* A lot of properties from the bases also apply to the derived
1317 class. */
1318 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1319 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1320 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1321 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1322 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1323 || !TYPE_HAS_COPY_ASSIGN (basetype));
1324 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1325 || !TYPE_HAS_COPY_CTOR (basetype));
1326 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1327 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1328 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1329 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1330 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1331 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1332 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1333 || TYPE_HAS_COMPLEX_DFLT (basetype));
1335 /* A standard-layout class is a class that:
1337 * has no non-standard-layout base classes, */
1338 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1339 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1341 tree basefield;
1342 /* ...has no base classes of the same type as the first non-static
1343 data member... */
1344 if (field && DECL_CONTEXT (field) == t
1345 && (same_type_ignoring_top_level_qualifiers_p
1346 (TREE_TYPE (field), basetype)))
1347 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1348 else
1349 /* ...either has no non-static data members in the most-derived
1350 class and at most one base class with non-static data
1351 members, or has no base classes with non-static data
1352 members */
1353 for (basefield = TYPE_FIELDS (basetype); basefield;
1354 basefield = TREE_CHAIN (basefield))
1355 if (TREE_CODE (basefield) == FIELD_DECL)
1357 if (field)
1358 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1359 else
1360 field = basefield;
1361 break;
1367 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1368 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1369 that have had a nearly-empty virtual primary base stolen by some
1370 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1371 T. */
1373 static void
1374 determine_primary_bases (tree t)
1376 unsigned i;
1377 tree primary = NULL_TREE;
1378 tree type_binfo = TYPE_BINFO (t);
1379 tree base_binfo;
1381 /* Determine the primary bases of our bases. */
1382 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1383 base_binfo = TREE_CHAIN (base_binfo))
1385 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1387 /* See if we're the non-virtual primary of our inheritance
1388 chain. */
1389 if (!BINFO_VIRTUAL_P (base_binfo))
1391 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1392 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1394 if (parent_primary
1395 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1396 BINFO_TYPE (parent_primary)))
1397 /* We are the primary binfo. */
1398 BINFO_PRIMARY_P (base_binfo) = 1;
1400 /* Determine if we have a virtual primary base, and mark it so.
1402 if (primary && BINFO_VIRTUAL_P (primary))
1404 tree this_primary = copied_binfo (primary, base_binfo);
1406 if (BINFO_PRIMARY_P (this_primary))
1407 /* Someone already claimed this base. */
1408 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1409 else
1411 tree delta;
1413 BINFO_PRIMARY_P (this_primary) = 1;
1414 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1416 /* A virtual binfo might have been copied from within
1417 another hierarchy. As we're about to use it as a
1418 primary base, make sure the offsets match. */
1419 delta = size_diffop_loc (input_location,
1420 convert (ssizetype,
1421 BINFO_OFFSET (base_binfo)),
1422 convert (ssizetype,
1423 BINFO_OFFSET (this_primary)));
1425 propagate_binfo_offsets (this_primary, delta);
1430 /* First look for a dynamic direct non-virtual base. */
1431 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1433 tree basetype = BINFO_TYPE (base_binfo);
1435 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1437 primary = base_binfo;
1438 goto found;
1442 /* A "nearly-empty" virtual base class can be the primary base
1443 class, if no non-virtual polymorphic base can be found. Look for
1444 a nearly-empty virtual dynamic base that is not already a primary
1445 base of something in the hierarchy. If there is no such base,
1446 just pick the first nearly-empty virtual base. */
1448 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1449 base_binfo = TREE_CHAIN (base_binfo))
1450 if (BINFO_VIRTUAL_P (base_binfo)
1451 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1453 if (!BINFO_PRIMARY_P (base_binfo))
1455 /* Found one that is not primary. */
1456 primary = base_binfo;
1457 goto found;
1459 else if (!primary)
1460 /* Remember the first candidate. */
1461 primary = base_binfo;
1464 found:
1465 /* If we've got a primary base, use it. */
1466 if (primary)
1468 tree basetype = BINFO_TYPE (primary);
1470 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1471 if (BINFO_PRIMARY_P (primary))
1472 /* We are stealing a primary base. */
1473 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1474 BINFO_PRIMARY_P (primary) = 1;
1475 if (BINFO_VIRTUAL_P (primary))
1477 tree delta;
1479 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1480 /* A virtual binfo might have been copied from within
1481 another hierarchy. As we're about to use it as a primary
1482 base, make sure the offsets match. */
1483 delta = size_diffop_loc (input_location, ssize_int (0),
1484 convert (ssizetype, BINFO_OFFSET (primary)));
1486 propagate_binfo_offsets (primary, delta);
1489 primary = TYPE_BINFO (basetype);
1491 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1492 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1493 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1497 /* Update the variant types of T. */
1499 void
1500 fixup_type_variants (tree t)
1502 tree variants;
1504 if (!t)
1505 return;
1507 for (variants = TYPE_NEXT_VARIANT (t);
1508 variants;
1509 variants = TYPE_NEXT_VARIANT (variants))
1511 /* These fields are in the _TYPE part of the node, not in
1512 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1513 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1514 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1515 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1516 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1518 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1520 TYPE_BINFO (variants) = TYPE_BINFO (t);
1522 /* Copy whatever these are holding today. */
1523 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1524 TYPE_METHODS (variants) = TYPE_METHODS (t);
1525 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1527 /* All variants of a class have the same attributes. */
1528 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1533 /* Set memoizing fields and bits of T (and its variants) for later
1534 use. */
1536 static void
1537 finish_struct_bits (tree t)
1539 /* Fix up variants (if any). */
1540 fixup_type_variants (t);
1542 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1543 /* For a class w/o baseclasses, 'finish_struct' has set
1544 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1545 Similarly for a class whose base classes do not have vtables.
1546 When neither of these is true, we might have removed abstract
1547 virtuals (by providing a definition), added some (by declaring
1548 new ones), or redeclared ones from a base class. We need to
1549 recalculate what's really an abstract virtual at this point (by
1550 looking in the vtables). */
1551 get_pure_virtuals (t);
1553 /* If this type has a copy constructor or a destructor, force its
1554 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1555 nonzero. This will cause it to be passed by invisible reference
1556 and prevent it from being returned in a register. */
1557 if (type_has_nontrivial_copy_init (t)
1558 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1560 tree variants;
1561 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1562 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1564 SET_TYPE_MODE (variants, BLKmode);
1565 TREE_ADDRESSABLE (variants) = 1;
1570 /* Issue warnings about T having private constructors, but no friends,
1571 and so forth.
1573 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1574 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1575 non-private static member functions. */
1577 static void
1578 maybe_warn_about_overly_private_class (tree t)
1580 int has_member_fn = 0;
1581 int has_nonprivate_method = 0;
1582 tree fn;
1584 if (!warn_ctor_dtor_privacy
1585 /* If the class has friends, those entities might create and
1586 access instances, so we should not warn. */
1587 || (CLASSTYPE_FRIEND_CLASSES (t)
1588 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1589 /* We will have warned when the template was declared; there's
1590 no need to warn on every instantiation. */
1591 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1592 /* There's no reason to even consider warning about this
1593 class. */
1594 return;
1596 /* We only issue one warning, if more than one applies, because
1597 otherwise, on code like:
1599 class A {
1600 // Oops - forgot `public:'
1601 A();
1602 A(const A&);
1603 ~A();
1606 we warn several times about essentially the same problem. */
1608 /* Check to see if all (non-constructor, non-destructor) member
1609 functions are private. (Since there are no friends or
1610 non-private statics, we can't ever call any of the private member
1611 functions.) */
1612 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1613 /* We're not interested in compiler-generated methods; they don't
1614 provide any way to call private members. */
1615 if (!DECL_ARTIFICIAL (fn))
1617 if (!TREE_PRIVATE (fn))
1619 if (DECL_STATIC_FUNCTION_P (fn))
1620 /* A non-private static member function is just like a
1621 friend; it can create and invoke private member
1622 functions, and be accessed without a class
1623 instance. */
1624 return;
1626 has_nonprivate_method = 1;
1627 /* Keep searching for a static member function. */
1629 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1630 has_member_fn = 1;
1633 if (!has_nonprivate_method && has_member_fn)
1635 /* There are no non-private methods, and there's at least one
1636 private member function that isn't a constructor or
1637 destructor. (If all the private members are
1638 constructors/destructors we want to use the code below that
1639 issues error messages specifically referring to
1640 constructors/destructors.) */
1641 unsigned i;
1642 tree binfo = TYPE_BINFO (t);
1644 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1645 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1647 has_nonprivate_method = 1;
1648 break;
1650 if (!has_nonprivate_method)
1652 warning (OPT_Wctor_dtor_privacy,
1653 "all member functions in class %qT are private", t);
1654 return;
1658 /* Even if some of the member functions are non-private, the class
1659 won't be useful for much if all the constructors or destructors
1660 are private: such an object can never be created or destroyed. */
1661 fn = CLASSTYPE_DESTRUCTORS (t);
1662 if (fn && TREE_PRIVATE (fn))
1664 warning (OPT_Wctor_dtor_privacy,
1665 "%q#T only defines a private destructor and has no friends",
1667 return;
1670 /* Warn about classes that have private constructors and no friends. */
1671 if (TYPE_HAS_USER_CONSTRUCTOR (t)
1672 /* Implicitly generated constructors are always public. */
1673 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1674 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1676 int nonprivate_ctor = 0;
1678 /* If a non-template class does not define a copy
1679 constructor, one is defined for it, enabling it to avoid
1680 this warning. For a template class, this does not
1681 happen, and so we would normally get a warning on:
1683 template <class T> class C { private: C(); };
1685 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
1686 complete non-template or fully instantiated classes have this
1687 flag set. */
1688 if (!TYPE_HAS_COPY_CTOR (t))
1689 nonprivate_ctor = 1;
1690 else
1691 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1693 tree ctor = OVL_CURRENT (fn);
1694 /* Ideally, we wouldn't count copy constructors (or, in
1695 fact, any constructor that takes an argument of the
1696 class type as a parameter) because such things cannot
1697 be used to construct an instance of the class unless
1698 you already have one. But, for now at least, we're
1699 more generous. */
1700 if (! TREE_PRIVATE (ctor))
1702 nonprivate_ctor = 1;
1703 break;
1707 if (nonprivate_ctor == 0)
1709 warning (OPT_Wctor_dtor_privacy,
1710 "%q#T only defines private constructors and has no friends",
1712 return;
1717 static struct {
1718 gt_pointer_operator new_value;
1719 void *cookie;
1720 } resort_data;
1722 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1724 static int
1725 method_name_cmp (const void* m1_p, const void* m2_p)
1727 const tree *const m1 = (const tree *) m1_p;
1728 const tree *const m2 = (const tree *) m2_p;
1730 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1731 return 0;
1732 if (*m1 == NULL_TREE)
1733 return -1;
1734 if (*m2 == NULL_TREE)
1735 return 1;
1736 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1737 return -1;
1738 return 1;
1741 /* This routine compares two fields like method_name_cmp but using the
1742 pointer operator in resort_field_decl_data. */
1744 static int
1745 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1747 const tree *const m1 = (const tree *) m1_p;
1748 const tree *const m2 = (const tree *) m2_p;
1749 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1750 return 0;
1751 if (*m1 == NULL_TREE)
1752 return -1;
1753 if (*m2 == NULL_TREE)
1754 return 1;
1756 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1757 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1758 resort_data.new_value (&d1, resort_data.cookie);
1759 resort_data.new_value (&d2, resort_data.cookie);
1760 if (d1 < d2)
1761 return -1;
1763 return 1;
1766 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1768 void
1769 resort_type_method_vec (void* obj,
1770 void* orig_obj ATTRIBUTE_UNUSED ,
1771 gt_pointer_operator new_value,
1772 void* cookie)
1774 VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj;
1775 int len = VEC_length (tree, method_vec);
1776 size_t slot;
1777 tree fn;
1779 /* The type conversion ops have to live at the front of the vec, so we
1780 can't sort them. */
1781 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1782 VEC_iterate (tree, method_vec, slot, fn);
1783 ++slot)
1784 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1785 break;
1787 if (len - slot > 1)
1789 resort_data.new_value = new_value;
1790 resort_data.cookie = cookie;
1791 qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree),
1792 resort_method_name_cmp);
1796 /* Warn about duplicate methods in fn_fields.
1798 Sort methods that are not special (i.e., constructors, destructors,
1799 and type conversion operators) so that we can find them faster in
1800 search. */
1802 static void
1803 finish_struct_methods (tree t)
1805 tree fn_fields;
1806 VEC(tree,gc) *method_vec;
1807 int slot, len;
1809 method_vec = CLASSTYPE_METHOD_VEC (t);
1810 if (!method_vec)
1811 return;
1813 len = VEC_length (tree, method_vec);
1815 /* Clear DECL_IN_AGGR_P for all functions. */
1816 for (fn_fields = TYPE_METHODS (t); fn_fields;
1817 fn_fields = TREE_CHAIN (fn_fields))
1818 DECL_IN_AGGR_P (fn_fields) = 0;
1820 /* Issue warnings about private constructors and such. If there are
1821 no methods, then some public defaults are generated. */
1822 maybe_warn_about_overly_private_class (t);
1824 /* The type conversion ops have to live at the front of the vec, so we
1825 can't sort them. */
1826 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1827 VEC_iterate (tree, method_vec, slot, fn_fields);
1828 ++slot)
1829 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
1830 break;
1831 if (len - slot > 1)
1832 qsort (VEC_address (tree, method_vec) + slot,
1833 len-slot, sizeof (tree), method_name_cmp);
1836 /* Make BINFO's vtable have N entries, including RTTI entries,
1837 vbase and vcall offsets, etc. Set its type and call the back end
1838 to lay it out. */
1840 static void
1841 layout_vtable_decl (tree binfo, int n)
1843 tree atype;
1844 tree vtable;
1846 atype = build_cplus_array_type (vtable_entry_type,
1847 build_index_type (size_int (n - 1)));
1848 layout_type (atype);
1850 /* We may have to grow the vtable. */
1851 vtable = get_vtbl_decl_for_binfo (binfo);
1852 if (!same_type_p (TREE_TYPE (vtable), atype))
1854 TREE_TYPE (vtable) = atype;
1855 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
1856 layout_decl (vtable, 0);
1860 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
1861 have the same signature. */
1864 same_signature_p (const_tree fndecl, const_tree base_fndecl)
1866 /* One destructor overrides another if they are the same kind of
1867 destructor. */
1868 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
1869 && special_function_p (base_fndecl) == special_function_p (fndecl))
1870 return 1;
1871 /* But a non-destructor never overrides a destructor, nor vice
1872 versa, nor do different kinds of destructors override
1873 one-another. For example, a complete object destructor does not
1874 override a deleting destructor. */
1875 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
1876 return 0;
1878 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
1879 || (DECL_CONV_FN_P (fndecl)
1880 && DECL_CONV_FN_P (base_fndecl)
1881 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
1882 DECL_CONV_FN_TYPE (base_fndecl))))
1884 tree types, base_types;
1885 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1886 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
1887 if ((cp_type_quals (TREE_TYPE (TREE_VALUE (base_types)))
1888 == cp_type_quals (TREE_TYPE (TREE_VALUE (types))))
1889 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
1890 return 1;
1892 return 0;
1895 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
1896 subobject. */
1898 static bool
1899 base_derived_from (tree derived, tree base)
1901 tree probe;
1903 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1905 if (probe == derived)
1906 return true;
1907 else if (BINFO_VIRTUAL_P (probe))
1908 /* If we meet a virtual base, we can't follow the inheritance
1909 any more. See if the complete type of DERIVED contains
1910 such a virtual base. */
1911 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
1912 != NULL_TREE);
1914 return false;
1917 typedef struct find_final_overrider_data_s {
1918 /* The function for which we are trying to find a final overrider. */
1919 tree fn;
1920 /* The base class in which the function was declared. */
1921 tree declaring_base;
1922 /* The candidate overriders. */
1923 tree candidates;
1924 /* Path to most derived. */
1925 VEC(tree,heap) *path;
1926 } find_final_overrider_data;
1928 /* Add the overrider along the current path to FFOD->CANDIDATES.
1929 Returns true if an overrider was found; false otherwise. */
1931 static bool
1932 dfs_find_final_overrider_1 (tree binfo,
1933 find_final_overrider_data *ffod,
1934 unsigned depth)
1936 tree method;
1938 /* If BINFO is not the most derived type, try a more derived class.
1939 A definition there will overrider a definition here. */
1940 if (depth)
1942 depth--;
1943 if (dfs_find_final_overrider_1
1944 (VEC_index (tree, ffod->path, depth), ffod, depth))
1945 return true;
1948 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
1949 if (method)
1951 tree *candidate = &ffod->candidates;
1953 /* Remove any candidates overridden by this new function. */
1954 while (*candidate)
1956 /* If *CANDIDATE overrides METHOD, then METHOD
1957 cannot override anything else on the list. */
1958 if (base_derived_from (TREE_VALUE (*candidate), binfo))
1959 return true;
1960 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
1961 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
1962 *candidate = TREE_CHAIN (*candidate);
1963 else
1964 candidate = &TREE_CHAIN (*candidate);
1967 /* Add the new function. */
1968 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
1969 return true;
1972 return false;
1975 /* Called from find_final_overrider via dfs_walk. */
1977 static tree
1978 dfs_find_final_overrider_pre (tree binfo, void *data)
1980 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
1982 if (binfo == ffod->declaring_base)
1983 dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path));
1984 VEC_safe_push (tree, heap, ffod->path, binfo);
1986 return NULL_TREE;
1989 static tree
1990 dfs_find_final_overrider_post (tree binfo ATTRIBUTE_UNUSED, void *data)
1992 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
1993 VEC_pop (tree, ffod->path);
1995 return NULL_TREE;
1998 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
1999 FN and whose TREE_VALUE is the binfo for the base where the
2000 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2001 DERIVED) is the base object in which FN is declared. */
2003 static tree
2004 find_final_overrider (tree derived, tree binfo, tree fn)
2006 find_final_overrider_data ffod;
2008 /* Getting this right is a little tricky. This is valid:
2010 struct S { virtual void f (); };
2011 struct T { virtual void f (); };
2012 struct U : public S, public T { };
2014 even though calling `f' in `U' is ambiguous. But,
2016 struct R { virtual void f(); };
2017 struct S : virtual public R { virtual void f (); };
2018 struct T : virtual public R { virtual void f (); };
2019 struct U : public S, public T { };
2021 is not -- there's no way to decide whether to put `S::f' or
2022 `T::f' in the vtable for `R'.
2024 The solution is to look at all paths to BINFO. If we find
2025 different overriders along any two, then there is a problem. */
2026 if (DECL_THUNK_P (fn))
2027 fn = THUNK_TARGET (fn);
2029 /* Determine the depth of the hierarchy. */
2030 ffod.fn = fn;
2031 ffod.declaring_base = binfo;
2032 ffod.candidates = NULL_TREE;
2033 ffod.path = VEC_alloc (tree, heap, 30);
2035 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2036 dfs_find_final_overrider_post, &ffod);
2038 VEC_free (tree, heap, ffod.path);
2040 /* If there was no winner, issue an error message. */
2041 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2042 return error_mark_node;
2044 return ffod.candidates;
2047 /* Return the index of the vcall offset for FN when TYPE is used as a
2048 virtual base. */
2050 static tree
2051 get_vcall_index (tree fn, tree type)
2053 VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type);
2054 tree_pair_p p;
2055 unsigned ix;
2057 for (ix = 0; VEC_iterate (tree_pair_s, indices, ix, p); ix++)
2058 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2059 || same_signature_p (fn, p->purpose))
2060 return p->value;
2062 /* There should always be an appropriate index. */
2063 gcc_unreachable ();
2066 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2067 dominated by T. FN is the old function; VIRTUALS points to the
2068 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2069 of that entry in the list. */
2071 static void
2072 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2073 unsigned ix)
2075 tree b;
2076 tree overrider;
2077 tree delta;
2078 tree virtual_base;
2079 tree first_defn;
2080 tree overrider_fn, overrider_target;
2081 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2082 tree over_return, base_return;
2083 bool lost = false;
2085 /* Find the nearest primary base (possibly binfo itself) which defines
2086 this function; this is the class the caller will convert to when
2087 calling FN through BINFO. */
2088 for (b = binfo; ; b = get_primary_binfo (b))
2090 gcc_assert (b);
2091 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2092 break;
2094 /* The nearest definition is from a lost primary. */
2095 if (BINFO_LOST_PRIMARY_P (b))
2096 lost = true;
2098 first_defn = b;
2100 /* Find the final overrider. */
2101 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2102 if (overrider == error_mark_node)
2104 error ("no unique final overrider for %qD in %qT", target_fn, t);
2105 return;
2107 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2109 /* Check for adjusting covariant return types. */
2110 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2111 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2113 if (POINTER_TYPE_P (over_return)
2114 && TREE_CODE (over_return) == TREE_CODE (base_return)
2115 && CLASS_TYPE_P (TREE_TYPE (over_return))
2116 && CLASS_TYPE_P (TREE_TYPE (base_return))
2117 /* If the overrider is invalid, don't even try. */
2118 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2120 /* If FN is a covariant thunk, we must figure out the adjustment
2121 to the final base FN was converting to. As OVERRIDER_TARGET might
2122 also be converting to the return type of FN, we have to
2123 combine the two conversions here. */
2124 tree fixed_offset, virtual_offset;
2126 over_return = TREE_TYPE (over_return);
2127 base_return = TREE_TYPE (base_return);
2129 if (DECL_THUNK_P (fn))
2131 gcc_assert (DECL_RESULT_THUNK_P (fn));
2132 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2133 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2135 else
2136 fixed_offset = virtual_offset = NULL_TREE;
2138 if (virtual_offset)
2139 /* Find the equivalent binfo within the return type of the
2140 overriding function. We will want the vbase offset from
2141 there. */
2142 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2143 over_return);
2144 else if (!same_type_ignoring_top_level_qualifiers_p
2145 (over_return, base_return))
2147 /* There was no existing virtual thunk (which takes
2148 precedence). So find the binfo of the base function's
2149 return type within the overriding function's return type.
2150 We cannot call lookup base here, because we're inside a
2151 dfs_walk, and will therefore clobber the BINFO_MARKED
2152 flags. Fortunately we know the covariancy is valid (it
2153 has already been checked), so we can just iterate along
2154 the binfos, which have been chained in inheritance graph
2155 order. Of course it is lame that we have to repeat the
2156 search here anyway -- we should really be caching pieces
2157 of the vtable and avoiding this repeated work. */
2158 tree thunk_binfo, base_binfo;
2160 /* Find the base binfo within the overriding function's
2161 return type. We will always find a thunk_binfo, except
2162 when the covariancy is invalid (which we will have
2163 already diagnosed). */
2164 for (base_binfo = TYPE_BINFO (base_return),
2165 thunk_binfo = TYPE_BINFO (over_return);
2166 thunk_binfo;
2167 thunk_binfo = TREE_CHAIN (thunk_binfo))
2168 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2169 BINFO_TYPE (base_binfo)))
2170 break;
2172 /* See if virtual inheritance is involved. */
2173 for (virtual_offset = thunk_binfo;
2174 virtual_offset;
2175 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2176 if (BINFO_VIRTUAL_P (virtual_offset))
2177 break;
2179 if (virtual_offset
2180 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2182 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2184 if (virtual_offset)
2186 /* We convert via virtual base. Adjust the fixed
2187 offset to be from there. */
2188 offset =
2189 size_diffop (offset,
2190 convert (ssizetype,
2191 BINFO_OFFSET (virtual_offset)));
2193 if (fixed_offset)
2194 /* There was an existing fixed offset, this must be
2195 from the base just converted to, and the base the
2196 FN was thunking to. */
2197 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2198 else
2199 fixed_offset = offset;
2203 if (fixed_offset || virtual_offset)
2204 /* Replace the overriding function with a covariant thunk. We
2205 will emit the overriding function in its own slot as
2206 well. */
2207 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2208 fixed_offset, virtual_offset);
2210 else
2211 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2212 !DECL_THUNK_P (fn));
2214 /* If we need a covariant thunk, then we may need to adjust first_defn.
2215 The ABI specifies that the thunks emitted with a function are
2216 determined by which bases the function overrides, so we need to be
2217 sure that we're using a thunk for some overridden base; even if we
2218 know that the necessary this adjustment is zero, there may not be an
2219 appropriate zero-this-adjusment thunk for us to use since thunks for
2220 overriding virtual bases always use the vcall offset.
2222 Furthermore, just choosing any base that overrides this function isn't
2223 quite right, as this slot won't be used for calls through a type that
2224 puts a covariant thunk here. Calling the function through such a type
2225 will use a different slot, and that slot is the one that determines
2226 the thunk emitted for that base.
2228 So, keep looking until we find the base that we're really overriding
2229 in this slot: the nearest primary base that doesn't use a covariant
2230 thunk in this slot. */
2231 if (overrider_target != overrider_fn)
2233 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2234 /* We already know that the overrider needs a covariant thunk. */
2235 b = get_primary_binfo (b);
2236 for (; ; b = get_primary_binfo (b))
2238 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2239 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2240 if (BINFO_LOST_PRIMARY_P (b))
2241 lost = true;
2242 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2243 break;
2245 first_defn = b;
2248 /* Assume that we will produce a thunk that convert all the way to
2249 the final overrider, and not to an intermediate virtual base. */
2250 virtual_base = NULL_TREE;
2252 /* See if we can convert to an intermediate virtual base first, and then
2253 use the vcall offset located there to finish the conversion. */
2254 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2256 /* If we find the final overrider, then we can stop
2257 walking. */
2258 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2259 BINFO_TYPE (TREE_VALUE (overrider))))
2260 break;
2262 /* If we find a virtual base, and we haven't yet found the
2263 overrider, then there is a virtual base between the
2264 declaring base (first_defn) and the final overrider. */
2265 if (BINFO_VIRTUAL_P (b))
2267 virtual_base = b;
2268 break;
2272 /* Compute the constant adjustment to the `this' pointer. The
2273 `this' pointer, when this function is called, will point at BINFO
2274 (or one of its primary bases, which are at the same offset). */
2275 if (virtual_base)
2276 /* The `this' pointer needs to be adjusted from the declaration to
2277 the nearest virtual base. */
2278 delta = size_diffop_loc (input_location,
2279 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2280 convert (ssizetype, BINFO_OFFSET (first_defn)));
2281 else if (lost)
2282 /* If the nearest definition is in a lost primary, we don't need an
2283 entry in our vtable. Except possibly in a constructor vtable,
2284 if we happen to get our primary back. In that case, the offset
2285 will be zero, as it will be a primary base. */
2286 delta = size_zero_node;
2287 else
2288 /* The `this' pointer needs to be adjusted from pointing to
2289 BINFO to pointing at the base where the final overrider
2290 appears. */
2291 delta = size_diffop_loc (input_location,
2292 convert (ssizetype,
2293 BINFO_OFFSET (TREE_VALUE (overrider))),
2294 convert (ssizetype, BINFO_OFFSET (binfo)));
2296 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2298 if (virtual_base)
2299 BV_VCALL_INDEX (*virtuals)
2300 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2301 else
2302 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2304 if (lost)
2305 BV_LOST_PRIMARY (*virtuals) = true;
2308 /* Called from modify_all_vtables via dfs_walk. */
2310 static tree
2311 dfs_modify_vtables (tree binfo, void* data)
2313 tree t = (tree) data;
2314 tree virtuals;
2315 tree old_virtuals;
2316 unsigned ix;
2318 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2319 /* A base without a vtable needs no modification, and its bases
2320 are uninteresting. */
2321 return dfs_skip_bases;
2323 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2324 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2325 /* Don't do the primary vtable, if it's new. */
2326 return NULL_TREE;
2328 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2329 /* There's no need to modify the vtable for a non-virtual primary
2330 base; we're not going to use that vtable anyhow. We do still
2331 need to do this for virtual primary bases, as they could become
2332 non-primary in a construction vtable. */
2333 return NULL_TREE;
2335 make_new_vtable (t, binfo);
2337 /* Now, go through each of the virtual functions in the virtual
2338 function table for BINFO. Find the final overrider, and update
2339 the BINFO_VIRTUALS list appropriately. */
2340 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2341 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2342 virtuals;
2343 ix++, virtuals = TREE_CHAIN (virtuals),
2344 old_virtuals = TREE_CHAIN (old_virtuals))
2345 update_vtable_entry_for_fn (t,
2346 binfo,
2347 BV_FN (old_virtuals),
2348 &virtuals, ix);
2350 return NULL_TREE;
2353 /* Update all of the primary and secondary vtables for T. Create new
2354 vtables as required, and initialize their RTTI information. Each
2355 of the functions in VIRTUALS is declared in T and may override a
2356 virtual function from a base class; find and modify the appropriate
2357 entries to point to the overriding functions. Returns a list, in
2358 declaration order, of the virtual functions that are declared in T,
2359 but do not appear in the primary base class vtable, and which
2360 should therefore be appended to the end of the vtable for T. */
2362 static tree
2363 modify_all_vtables (tree t, tree virtuals)
2365 tree binfo = TYPE_BINFO (t);
2366 tree *fnsp;
2368 /* Update all of the vtables. */
2369 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2371 /* Add virtual functions not already in our primary vtable. These
2372 will be both those introduced by this class, and those overridden
2373 from secondary bases. It does not include virtuals merely
2374 inherited from secondary bases. */
2375 for (fnsp = &virtuals; *fnsp; )
2377 tree fn = TREE_VALUE (*fnsp);
2379 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2380 || DECL_VINDEX (fn) == error_mark_node)
2382 /* We don't need to adjust the `this' pointer when
2383 calling this function. */
2384 BV_DELTA (*fnsp) = integer_zero_node;
2385 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2387 /* This is a function not already in our vtable. Keep it. */
2388 fnsp = &TREE_CHAIN (*fnsp);
2390 else
2391 /* We've already got an entry for this function. Skip it. */
2392 *fnsp = TREE_CHAIN (*fnsp);
2395 return virtuals;
2398 /* Get the base virtual function declarations in T that have the
2399 indicated NAME. */
2401 static tree
2402 get_basefndecls (tree name, tree t)
2404 tree methods;
2405 tree base_fndecls = NULL_TREE;
2406 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2407 int i;
2409 /* Find virtual functions in T with the indicated NAME. */
2410 i = lookup_fnfields_1 (t, name);
2411 if (i != -1)
2412 for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i);
2413 methods;
2414 methods = OVL_NEXT (methods))
2416 tree method = OVL_CURRENT (methods);
2418 if (TREE_CODE (method) == FUNCTION_DECL
2419 && DECL_VINDEX (method))
2420 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2423 if (base_fndecls)
2424 return base_fndecls;
2426 for (i = 0; i < n_baseclasses; i++)
2428 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2429 base_fndecls = chainon (get_basefndecls (name, basetype),
2430 base_fndecls);
2433 return base_fndecls;
2436 /* If this declaration supersedes the declaration of
2437 a method declared virtual in the base class, then
2438 mark this field as being virtual as well. */
2440 void
2441 check_for_override (tree decl, tree ctype)
2443 if (TREE_CODE (decl) == TEMPLATE_DECL)
2444 /* In [temp.mem] we have:
2446 A specialization of a member function template does not
2447 override a virtual function from a base class. */
2448 return;
2449 if ((DECL_DESTRUCTOR_P (decl)
2450 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2451 || DECL_CONV_FN_P (decl))
2452 && look_for_overrides (ctype, decl)
2453 && !DECL_STATIC_FUNCTION_P (decl))
2454 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2455 the error_mark_node so that we know it is an overriding
2456 function. */
2457 DECL_VINDEX (decl) = decl;
2459 if (DECL_VIRTUAL_P (decl))
2461 if (!DECL_VINDEX (decl))
2462 DECL_VINDEX (decl) = error_mark_node;
2463 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2467 /* Warn about hidden virtual functions that are not overridden in t.
2468 We know that constructors and destructors don't apply. */
2470 static void
2471 warn_hidden (tree t)
2473 VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t);
2474 tree fns;
2475 size_t i;
2477 /* We go through each separately named virtual function. */
2478 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2479 VEC_iterate (tree, method_vec, i, fns);
2480 ++i)
2482 tree fn;
2483 tree name;
2484 tree fndecl;
2485 tree base_fndecls;
2486 tree base_binfo;
2487 tree binfo;
2488 int j;
2490 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2491 have the same name. Figure out what name that is. */
2492 name = DECL_NAME (OVL_CURRENT (fns));
2493 /* There are no possibly hidden functions yet. */
2494 base_fndecls = NULL_TREE;
2495 /* Iterate through all of the base classes looking for possibly
2496 hidden functions. */
2497 for (binfo = TYPE_BINFO (t), j = 0;
2498 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2500 tree basetype = BINFO_TYPE (base_binfo);
2501 base_fndecls = chainon (get_basefndecls (name, basetype),
2502 base_fndecls);
2505 /* If there are no functions to hide, continue. */
2506 if (!base_fndecls)
2507 continue;
2509 /* Remove any overridden functions. */
2510 for (fn = fns; fn; fn = OVL_NEXT (fn))
2512 fndecl = OVL_CURRENT (fn);
2513 if (DECL_VINDEX (fndecl))
2515 tree *prev = &base_fndecls;
2517 while (*prev)
2518 /* If the method from the base class has the same
2519 signature as the method from the derived class, it
2520 has been overridden. */
2521 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2522 *prev = TREE_CHAIN (*prev);
2523 else
2524 prev = &TREE_CHAIN (*prev);
2528 /* Now give a warning for all base functions without overriders,
2529 as they are hidden. */
2530 while (base_fndecls)
2532 /* Here we know it is a hider, and no overrider exists. */
2533 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2534 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
2535 base_fndecls = TREE_CHAIN (base_fndecls);
2540 /* Check for things that are invalid. There are probably plenty of other
2541 things we should check for also. */
2543 static void
2544 finish_struct_anon (tree t)
2546 tree field;
2548 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2550 if (TREE_STATIC (field))
2551 continue;
2552 if (TREE_CODE (field) != FIELD_DECL)
2553 continue;
2555 if (DECL_NAME (field) == NULL_TREE
2556 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2558 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2559 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2560 for (; elt; elt = TREE_CHAIN (elt))
2562 /* We're generally only interested in entities the user
2563 declared, but we also find nested classes by noticing
2564 the TYPE_DECL that we create implicitly. You're
2565 allowed to put one anonymous union inside another,
2566 though, so we explicitly tolerate that. We use
2567 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2568 we also allow unnamed types used for defining fields. */
2569 if (DECL_ARTIFICIAL (elt)
2570 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2571 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2572 continue;
2574 if (TREE_CODE (elt) != FIELD_DECL)
2576 if (is_union)
2577 permerror (input_location, "%q+#D invalid; an anonymous union can "
2578 "only have non-static data members", elt);
2579 else
2580 permerror (input_location, "%q+#D invalid; an anonymous struct can "
2581 "only have non-static data members", elt);
2582 continue;
2585 if (TREE_PRIVATE (elt))
2587 if (is_union)
2588 permerror (input_location, "private member %q+#D in anonymous union", elt);
2589 else
2590 permerror (input_location, "private member %q+#D in anonymous struct", elt);
2592 else if (TREE_PROTECTED (elt))
2594 if (is_union)
2595 permerror (input_location, "protected member %q+#D in anonymous union", elt);
2596 else
2597 permerror (input_location, "protected member %q+#D in anonymous struct", elt);
2600 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2601 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2607 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2608 will be used later during class template instantiation.
2609 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2610 a non-static member data (FIELD_DECL), a member function
2611 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2612 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2613 When FRIEND_P is nonzero, T is either a friend class
2614 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2615 (FUNCTION_DECL, TEMPLATE_DECL). */
2617 void
2618 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2620 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2621 if (CLASSTYPE_TEMPLATE_INFO (type))
2622 CLASSTYPE_DECL_LIST (type)
2623 = tree_cons (friend_p ? NULL_TREE : type,
2624 t, CLASSTYPE_DECL_LIST (type));
2627 /* Create default constructors, assignment operators, and so forth for
2628 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
2629 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
2630 the class cannot have a default constructor, copy constructor
2631 taking a const reference argument, or an assignment operator taking
2632 a const reference, respectively. */
2634 static void
2635 add_implicitly_declared_members (tree t,
2636 int cant_have_const_cctor,
2637 int cant_have_const_assignment,
2638 int cant_have_lazy_ctor,
2639 int cant_have_lazy_opeq)
2641 /* Destructor. */
2642 if (!CLASSTYPE_DESTRUCTORS (t))
2644 /* In general, we create destructors lazily. */
2645 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
2647 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2648 && TYPE_FOR_JAVA (t))
2649 /* But if this is a Java class, any non-trivial destructor is
2650 invalid, even if compiler-generated. Therefore, if the
2651 destructor is non-trivial we create it now. */
2652 lazily_declare_fn (sfk_destructor, t);
2655 /* [class.ctor]
2657 If there is no user-declared constructor for a class, a default
2658 constructor is implicitly declared. */
2659 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
2661 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
2662 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
2665 /* [class.ctor]
2667 If a class definition does not explicitly declare a copy
2668 constructor, one is declared implicitly. */
2669 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t)
2670 && !type_has_move_constructor (t))
2672 TYPE_HAS_COPY_CTOR (t) = 1;
2673 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
2674 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
2675 if (cxx_dialect >= cxx0x)
2676 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
2679 /* If there is no assignment operator, one will be created if and
2680 when it is needed. For now, just record whether or not the type
2681 of the parameter to the assignment operator will be a const or
2682 non-const reference. */
2683 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t)
2684 && !type_has_move_assign (t))
2686 TYPE_HAS_COPY_ASSIGN (t) = 1;
2687 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
2688 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
2689 if (cxx_dialect >= cxx0x)
2690 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
2693 /* If a base or member type has a user-declared constructor or operator=,
2694 we need to declare ours now to avoid issues with circular lazy
2695 declarations (cpp0x/implicit6.C). */
2696 if (cant_have_lazy_ctor)
2698 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2699 lazily_declare_fn (sfk_constructor, t);
2700 if (CLASSTYPE_LAZY_COPY_CTOR (t))
2701 lazily_declare_fn (sfk_copy_constructor, t);
2702 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
2703 lazily_declare_fn (sfk_move_constructor, t);
2705 if (cant_have_lazy_opeq)
2707 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
2708 lazily_declare_fn (sfk_copy_assignment, t);
2709 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
2710 lazily_declare_fn (sfk_move_assignment, t);
2713 /* We can't be lazy about declaring functions that might override
2714 a virtual function from a base class. */
2715 if (TYPE_POLYMORPHIC_P (t)
2716 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
2717 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
2718 || CLASSTYPE_LAZY_DESTRUCTOR (t)))
2720 tree binfo = TYPE_BINFO (t);
2721 tree base_binfo;
2722 int ix;
2723 tree opname = ansi_assopname (NOP_EXPR);
2724 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
2726 tree bv;
2727 for (bv = BINFO_VIRTUALS (base_binfo); bv; bv = TREE_CHAIN (bv))
2729 tree fn = BV_FN (bv);
2730 if (DECL_NAME (fn) == opname)
2732 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
2733 lazily_declare_fn (sfk_copy_assignment, t);
2734 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
2735 lazily_declare_fn (sfk_move_assignment, t);
2737 else if (DECL_DESTRUCTOR_P (fn)
2738 && CLASSTYPE_LAZY_DESTRUCTOR (t))
2739 lazily_declare_fn (sfk_destructor, t);
2745 /* Subroutine of finish_struct_1. Recursively count the number of fields
2746 in TYPE, including anonymous union members. */
2748 static int
2749 count_fields (tree fields)
2751 tree x;
2752 int n_fields = 0;
2753 for (x = fields; x; x = TREE_CHAIN (x))
2755 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2756 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2757 else
2758 n_fields += 1;
2760 return n_fields;
2763 /* Subroutine of finish_struct_1. Recursively add all the fields in the
2764 TREE_LIST FIELDS to the SORTED_FIELDS_TYPE elts, starting at offset IDX. */
2766 static int
2767 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
2769 tree x;
2770 for (x = fields; x; x = TREE_CHAIN (x))
2772 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2773 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2774 else
2775 field_vec->elts[idx++] = x;
2777 return idx;
2780 /* FIELD is a bit-field. We are finishing the processing for its
2781 enclosing type. Issue any appropriate messages and set appropriate
2782 flags. Returns false if an error has been diagnosed. */
2784 static bool
2785 check_bitfield_decl (tree field)
2787 tree type = TREE_TYPE (field);
2788 tree w;
2790 /* Extract the declared width of the bitfield, which has been
2791 temporarily stashed in DECL_INITIAL. */
2792 w = DECL_INITIAL (field);
2793 gcc_assert (w != NULL_TREE);
2794 /* Remove the bit-field width indicator so that the rest of the
2795 compiler does not treat that value as an initializer. */
2796 DECL_INITIAL (field) = NULL_TREE;
2798 /* Detect invalid bit-field type. */
2799 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2801 error ("bit-field %q+#D with non-integral type", field);
2802 w = error_mark_node;
2804 else
2806 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2807 STRIP_NOPS (w);
2809 /* detect invalid field size. */
2810 w = integral_constant_value (w);
2812 if (TREE_CODE (w) != INTEGER_CST)
2814 error ("bit-field %q+D width not an integer constant", field);
2815 w = error_mark_node;
2817 else if (tree_int_cst_sgn (w) < 0)
2819 error ("negative width in bit-field %q+D", field);
2820 w = error_mark_node;
2822 else if (integer_zerop (w) && DECL_NAME (field) != 0)
2824 error ("zero width for bit-field %q+D", field);
2825 w = error_mark_node;
2827 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
2828 && TREE_CODE (type) != ENUMERAL_TYPE
2829 && TREE_CODE (type) != BOOLEAN_TYPE)
2830 warning (0, "width of %q+D exceeds its type", field);
2831 else if (TREE_CODE (type) == ENUMERAL_TYPE
2832 && (0 > (compare_tree_int
2833 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
2834 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
2837 if (w != error_mark_node)
2839 DECL_SIZE (field) = convert (bitsizetype, w);
2840 DECL_BIT_FIELD (field) = 1;
2841 return true;
2843 else
2845 /* Non-bit-fields are aligned for their type. */
2846 DECL_BIT_FIELD (field) = 0;
2847 CLEAR_DECL_C_BIT_FIELD (field);
2848 return false;
2852 /* FIELD is a non bit-field. We are finishing the processing for its
2853 enclosing type T. Issue any appropriate messages and set appropriate
2854 flags. */
2856 static void
2857 check_field_decl (tree field,
2858 tree t,
2859 int* cant_have_const_ctor,
2860 int* no_const_asn_ref,
2861 int* any_default_members,
2862 int* cant_have_lazy_ctor,
2863 int* cant_have_lazy_opeq)
2865 tree type = strip_array_types (TREE_TYPE (field));
2867 /* In C++98 an anonymous union cannot contain any fields which would change
2868 the settings of CANT_HAVE_CONST_CTOR and friends. */
2869 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx0x)
2871 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
2872 structs. So, we recurse through their fields here. */
2873 else if (ANON_AGGR_TYPE_P (type))
2875 tree fields;
2877 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2878 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
2879 check_field_decl (fields, t, cant_have_const_ctor,
2880 no_const_asn_ref, any_default_members,
2881 cant_have_lazy_ctor, cant_have_lazy_opeq);
2883 /* Check members with class type for constructors, destructors,
2884 etc. */
2885 else if (CLASS_TYPE_P (type))
2887 /* Never let anything with uninheritable virtuals
2888 make it through without complaint. */
2889 abstract_virtuals_error (field, type);
2891 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx0x)
2893 static bool warned;
2894 int oldcount = errorcount;
2895 if (TYPE_NEEDS_CONSTRUCTING (type))
2896 error ("member %q+#D with constructor not allowed in union",
2897 field);
2898 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2899 error ("member %q+#D with destructor not allowed in union", field);
2900 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
2901 error ("member %q+#D with copy assignment operator not allowed in union",
2902 field);
2903 if (!warned && errorcount > oldcount)
2905 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
2906 "only available with -std=c++0x or -std=gnu++0x");
2907 warned = true;
2910 else
2912 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
2913 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2914 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
2915 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
2916 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
2917 || !TYPE_HAS_COPY_ASSIGN (type));
2918 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
2919 || !TYPE_HAS_COPY_CTOR (type));
2920 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
2921 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
2922 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
2923 || TYPE_HAS_COMPLEX_DFLT (type));
2926 if (TYPE_HAS_COPY_CTOR (type)
2927 && !TYPE_HAS_CONST_COPY_CTOR (type))
2928 *cant_have_const_ctor = 1;
2930 if (TYPE_HAS_COPY_ASSIGN (type)
2931 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
2932 *no_const_asn_ref = 1;
2934 if (TYPE_HAS_USER_CONSTRUCTOR (type))
2935 *cant_have_lazy_ctor = 1;
2936 if (TYPE_HAS_USER_OPEQ (type))
2937 *cant_have_lazy_opeq = 1;
2939 if (DECL_INITIAL (field) != NULL_TREE)
2941 /* `build_class_init_list' does not recognize
2942 non-FIELD_DECLs. */
2943 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
2944 error ("multiple fields in union %qT initialized", t);
2945 *any_default_members = 1;
2949 /* Check the data members (both static and non-static), class-scoped
2950 typedefs, etc., appearing in the declaration of T. Issue
2951 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
2952 declaration order) of access declarations; each TREE_VALUE in this
2953 list is a USING_DECL.
2955 In addition, set the following flags:
2957 EMPTY_P
2958 The class is empty, i.e., contains no non-static data members.
2960 CANT_HAVE_CONST_CTOR_P
2961 This class cannot have an implicitly generated copy constructor
2962 taking a const reference.
2964 CANT_HAVE_CONST_ASN_REF
2965 This class cannot have an implicitly generated assignment
2966 operator taking a const reference.
2968 All of these flags should be initialized before calling this
2969 function.
2971 Returns a pointer to the end of the TYPE_FIELDs chain; additional
2972 fields can be added by adding to this chain. */
2974 static void
2975 check_field_decls (tree t, tree *access_decls,
2976 int *cant_have_const_ctor_p,
2977 int *no_const_asn_ref_p,
2978 int *cant_have_lazy_ctor_p,
2979 int *cant_have_lazy_opeq_p)
2981 tree *field;
2982 tree *next;
2983 bool has_pointers;
2984 int any_default_members;
2985 int cant_pack = 0;
2986 int field_access = -1;
2988 /* Assume there are no access declarations. */
2989 *access_decls = NULL_TREE;
2990 /* Assume this class has no pointer members. */
2991 has_pointers = false;
2992 /* Assume none of the members of this class have default
2993 initializations. */
2994 any_default_members = 0;
2996 for (field = &TYPE_FIELDS (t); *field; field = next)
2998 tree x = *field;
2999 tree type = TREE_TYPE (x);
3000 int this_field_access;
3002 next = &TREE_CHAIN (x);
3004 if (TREE_CODE (x) == USING_DECL)
3006 /* Prune the access declaration from the list of fields. */
3007 *field = TREE_CHAIN (x);
3009 /* Save the access declarations for our caller. */
3010 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3012 /* Since we've reset *FIELD there's no reason to skip to the
3013 next field. */
3014 next = field;
3015 continue;
3018 if (TREE_CODE (x) == TYPE_DECL
3019 || TREE_CODE (x) == TEMPLATE_DECL)
3020 continue;
3022 /* If we've gotten this far, it's a data member, possibly static,
3023 or an enumerator. */
3024 DECL_CONTEXT (x) = t;
3026 /* When this goes into scope, it will be a non-local reference. */
3027 DECL_NONLOCAL (x) = 1;
3029 if (TREE_CODE (t) == UNION_TYPE)
3031 /* [class.union]
3033 If a union contains a static data member, or a member of
3034 reference type, the program is ill-formed. */
3035 if (TREE_CODE (x) == VAR_DECL)
3037 error ("%q+D may not be static because it is a member of a union", x);
3038 continue;
3040 if (TREE_CODE (type) == REFERENCE_TYPE)
3042 error ("%q+D may not have reference type %qT because"
3043 " it is a member of a union",
3044 x, type);
3045 continue;
3049 /* Perform error checking that did not get done in
3050 grokdeclarator. */
3051 if (TREE_CODE (type) == FUNCTION_TYPE)
3053 error ("field %q+D invalidly declared function type", x);
3054 type = build_pointer_type (type);
3055 TREE_TYPE (x) = type;
3057 else if (TREE_CODE (type) == METHOD_TYPE)
3059 error ("field %q+D invalidly declared method type", x);
3060 type = build_pointer_type (type);
3061 TREE_TYPE (x) = type;
3064 if (type == error_mark_node)
3065 continue;
3067 if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL)
3068 continue;
3070 /* Now it can only be a FIELD_DECL. */
3072 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3073 CLASSTYPE_NON_AGGREGATE (t) = 1;
3075 /* A standard-layout class is a class that:
3077 has the same access control (Clause 11) for all non-static data members,
3078 ... */
3079 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3080 if (field_access == -1)
3081 field_access = this_field_access;
3082 else if (this_field_access != field_access)
3083 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3085 /* If this is of reference type, check if it needs an init. */
3086 if (TREE_CODE (type) == REFERENCE_TYPE)
3088 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3089 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3090 if (DECL_INITIAL (x) == NULL_TREE)
3091 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3093 /* ARM $12.6.2: [A member initializer list] (or, for an
3094 aggregate, initialization by a brace-enclosed list) is the
3095 only way to initialize nonstatic const and reference
3096 members. */
3097 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3098 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3101 type = strip_array_types (type);
3103 if (TYPE_PACKED (t))
3105 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3107 warning
3109 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3111 cant_pack = 1;
3113 else if (DECL_C_BIT_FIELD (x)
3114 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3115 DECL_PACKED (x) = 1;
3118 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3119 /* We don't treat zero-width bitfields as making a class
3120 non-empty. */
3122 else
3124 /* The class is non-empty. */
3125 CLASSTYPE_EMPTY_P (t) = 0;
3126 /* The class is not even nearly empty. */
3127 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3128 /* If one of the data members contains an empty class,
3129 so does T. */
3130 if (CLASS_TYPE_P (type)
3131 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3132 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3135 /* This is used by -Weffc++ (see below). Warn only for pointers
3136 to members which might hold dynamic memory. So do not warn
3137 for pointers to functions or pointers to members. */
3138 if (TYPE_PTR_P (type)
3139 && !TYPE_PTRFN_P (type)
3140 && !TYPE_PTR_TO_MEMBER_P (type))
3141 has_pointers = true;
3143 if (CLASS_TYPE_P (type))
3145 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3146 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3147 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3148 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3151 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3152 CLASSTYPE_HAS_MUTABLE (t) = 1;
3154 if (! layout_pod_type_p (type))
3155 /* DR 148 now allows pointers to members (which are POD themselves),
3156 to be allowed in POD structs. */
3157 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3159 if (!std_layout_type_p (type))
3160 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3162 if (! zero_init_p (type))
3163 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3165 /* We set DECL_C_BIT_FIELD in grokbitfield.
3166 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3167 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3168 check_field_decl (x, t,
3169 cant_have_const_ctor_p,
3170 no_const_asn_ref_p,
3171 &any_default_members,
3172 cant_have_lazy_ctor_p,
3173 cant_have_lazy_opeq_p);
3175 /* If any field is const, the structure type is pseudo-const. */
3176 if (CP_TYPE_CONST_P (type))
3178 C_TYPE_FIELDS_READONLY (t) = 1;
3179 if (DECL_INITIAL (x) == NULL_TREE)
3180 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3182 /* ARM $12.6.2: [A member initializer list] (or, for an
3183 aggregate, initialization by a brace-enclosed list) is the
3184 only way to initialize nonstatic const and reference
3185 members. */
3186 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3187 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3189 /* A field that is pseudo-const makes the structure likewise. */
3190 else if (CLASS_TYPE_P (type))
3192 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3193 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3194 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3195 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3198 /* Core issue 80: A nonstatic data member is required to have a
3199 different name from the class iff the class has a
3200 user-declared constructor. */
3201 if (constructor_name_p (DECL_NAME (x), t)
3202 && TYPE_HAS_USER_CONSTRUCTOR (t))
3203 permerror (input_location, "field %q+#D with same name as class", x);
3206 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3207 it should also define a copy constructor and an assignment operator to
3208 implement the correct copy semantic (deep vs shallow, etc.). As it is
3209 not feasible to check whether the constructors do allocate dynamic memory
3210 and store it within members, we approximate the warning like this:
3212 -- Warn only if there are members which are pointers
3213 -- Warn only if there is a non-trivial constructor (otherwise,
3214 there cannot be memory allocated).
3215 -- Warn only if there is a non-trivial destructor. We assume that the
3216 user at least implemented the cleanup correctly, and a destructor
3217 is needed to free dynamic memory.
3219 This seems enough for practical purposes. */
3220 if (warn_ecpp
3221 && has_pointers
3222 && TYPE_HAS_USER_CONSTRUCTOR (t)
3223 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3224 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3226 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3228 if (! TYPE_HAS_COPY_CTOR (t))
3230 warning (OPT_Weffc__,
3231 " but does not override %<%T(const %T&)%>", t, t);
3232 if (!TYPE_HAS_COPY_ASSIGN (t))
3233 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3235 else if (! TYPE_HAS_COPY_ASSIGN (t))
3236 warning (OPT_Weffc__,
3237 " but does not override %<operator=(const %T&)%>", t);
3240 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3241 if (cant_pack)
3242 TYPE_PACKED (t) = 0;
3244 /* Check anonymous struct/anonymous union fields. */
3245 finish_struct_anon (t);
3247 /* We've built up the list of access declarations in reverse order.
3248 Fix that now. */
3249 *access_decls = nreverse (*access_decls);
3252 /* If TYPE is an empty class type, records its OFFSET in the table of
3253 OFFSETS. */
3255 static int
3256 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3258 splay_tree_node n;
3260 if (!is_empty_class (type))
3261 return 0;
3263 /* Record the location of this empty object in OFFSETS. */
3264 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3265 if (!n)
3266 n = splay_tree_insert (offsets,
3267 (splay_tree_key) offset,
3268 (splay_tree_value) NULL_TREE);
3269 n->value = ((splay_tree_value)
3270 tree_cons (NULL_TREE,
3271 type,
3272 (tree) n->value));
3274 return 0;
3277 /* Returns nonzero if TYPE is an empty class type and there is
3278 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3280 static int
3281 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3283 splay_tree_node n;
3284 tree t;
3286 if (!is_empty_class (type))
3287 return 0;
3289 /* Record the location of this empty object in OFFSETS. */
3290 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3291 if (!n)
3292 return 0;
3294 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3295 if (same_type_p (TREE_VALUE (t), type))
3296 return 1;
3298 return 0;
3301 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3302 F for every subobject, passing it the type, offset, and table of
3303 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3304 be traversed.
3306 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3307 than MAX_OFFSET will not be walked.
3309 If F returns a nonzero value, the traversal ceases, and that value
3310 is returned. Otherwise, returns zero. */
3312 static int
3313 walk_subobject_offsets (tree type,
3314 subobject_offset_fn f,
3315 tree offset,
3316 splay_tree offsets,
3317 tree max_offset,
3318 int vbases_p)
3320 int r = 0;
3321 tree type_binfo = NULL_TREE;
3323 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3324 stop. */
3325 if (max_offset && INT_CST_LT (max_offset, offset))
3326 return 0;
3328 if (type == error_mark_node)
3329 return 0;
3331 if (!TYPE_P (type))
3333 if (abi_version_at_least (2))
3334 type_binfo = type;
3335 type = BINFO_TYPE (type);
3338 if (CLASS_TYPE_P (type))
3340 tree field;
3341 tree binfo;
3342 int i;
3344 /* Avoid recursing into objects that are not interesting. */
3345 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3346 return 0;
3348 /* Record the location of TYPE. */
3349 r = (*f) (type, offset, offsets);
3350 if (r)
3351 return r;
3353 /* Iterate through the direct base classes of TYPE. */
3354 if (!type_binfo)
3355 type_binfo = TYPE_BINFO (type);
3356 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3358 tree binfo_offset;
3360 if (abi_version_at_least (2)
3361 && BINFO_VIRTUAL_P (binfo))
3362 continue;
3364 if (!vbases_p
3365 && BINFO_VIRTUAL_P (binfo)
3366 && !BINFO_PRIMARY_P (binfo))
3367 continue;
3369 if (!abi_version_at_least (2))
3370 binfo_offset = size_binop (PLUS_EXPR,
3371 offset,
3372 BINFO_OFFSET (binfo));
3373 else
3375 tree orig_binfo;
3376 /* We cannot rely on BINFO_OFFSET being set for the base
3377 class yet, but the offsets for direct non-virtual
3378 bases can be calculated by going back to the TYPE. */
3379 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3380 binfo_offset = size_binop (PLUS_EXPR,
3381 offset,
3382 BINFO_OFFSET (orig_binfo));
3385 r = walk_subobject_offsets (binfo,
3387 binfo_offset,
3388 offsets,
3389 max_offset,
3390 (abi_version_at_least (2)
3391 ? /*vbases_p=*/0 : vbases_p));
3392 if (r)
3393 return r;
3396 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3398 unsigned ix;
3399 VEC(tree,gc) *vbases;
3401 /* Iterate through the virtual base classes of TYPE. In G++
3402 3.2, we included virtual bases in the direct base class
3403 loop above, which results in incorrect results; the
3404 correct offsets for virtual bases are only known when
3405 working with the most derived type. */
3406 if (vbases_p)
3407 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3408 VEC_iterate (tree, vbases, ix, binfo); ix++)
3410 r = walk_subobject_offsets (binfo,
3412 size_binop (PLUS_EXPR,
3413 offset,
3414 BINFO_OFFSET (binfo)),
3415 offsets,
3416 max_offset,
3417 /*vbases_p=*/0);
3418 if (r)
3419 return r;
3421 else
3423 /* We still have to walk the primary base, if it is
3424 virtual. (If it is non-virtual, then it was walked
3425 above.) */
3426 tree vbase = get_primary_binfo (type_binfo);
3428 if (vbase && BINFO_VIRTUAL_P (vbase)
3429 && BINFO_PRIMARY_P (vbase)
3430 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3432 r = (walk_subobject_offsets
3433 (vbase, f, offset,
3434 offsets, max_offset, /*vbases_p=*/0));
3435 if (r)
3436 return r;
3441 /* Iterate through the fields of TYPE. */
3442 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3443 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3445 tree field_offset;
3447 if (abi_version_at_least (2))
3448 field_offset = byte_position (field);
3449 else
3450 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3451 field_offset = DECL_FIELD_OFFSET (field);
3453 r = walk_subobject_offsets (TREE_TYPE (field),
3455 size_binop (PLUS_EXPR,
3456 offset,
3457 field_offset),
3458 offsets,
3459 max_offset,
3460 /*vbases_p=*/1);
3461 if (r)
3462 return r;
3465 else if (TREE_CODE (type) == ARRAY_TYPE)
3467 tree element_type = strip_array_types (type);
3468 tree domain = TYPE_DOMAIN (type);
3469 tree index;
3471 /* Avoid recursing into objects that are not interesting. */
3472 if (!CLASS_TYPE_P (element_type)
3473 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3474 return 0;
3476 /* Step through each of the elements in the array. */
3477 for (index = size_zero_node;
3478 /* G++ 3.2 had an off-by-one error here. */
3479 (abi_version_at_least (2)
3480 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3481 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3482 index = size_binop (PLUS_EXPR, index, size_one_node))
3484 r = walk_subobject_offsets (TREE_TYPE (type),
3486 offset,
3487 offsets,
3488 max_offset,
3489 /*vbases_p=*/1);
3490 if (r)
3491 return r;
3492 offset = size_binop (PLUS_EXPR, offset,
3493 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3494 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3495 there's no point in iterating through the remaining
3496 elements of the array. */
3497 if (max_offset && INT_CST_LT (max_offset, offset))
3498 break;
3502 return 0;
3505 /* Record all of the empty subobjects of TYPE (either a type or a
3506 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3507 is being placed at OFFSET; otherwise, it is a base class that is
3508 being placed at OFFSET. */
3510 static void
3511 record_subobject_offsets (tree type,
3512 tree offset,
3513 splay_tree offsets,
3514 bool is_data_member)
3516 tree max_offset;
3517 /* If recording subobjects for a non-static data member or a
3518 non-empty base class , we do not need to record offsets beyond
3519 the size of the biggest empty class. Additional data members
3520 will go at the end of the class. Additional base classes will go
3521 either at offset zero (if empty, in which case they cannot
3522 overlap with offsets past the size of the biggest empty class) or
3523 at the end of the class.
3525 However, if we are placing an empty base class, then we must record
3526 all offsets, as either the empty class is at offset zero (where
3527 other empty classes might later be placed) or at the end of the
3528 class (where other objects might then be placed, so other empty
3529 subobjects might later overlap). */
3530 if (is_data_member
3531 || !is_empty_class (BINFO_TYPE (type)))
3532 max_offset = sizeof_biggest_empty_class;
3533 else
3534 max_offset = NULL_TREE;
3535 walk_subobject_offsets (type, record_subobject_offset, offset,
3536 offsets, max_offset, is_data_member);
3539 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3540 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3541 virtual bases of TYPE are examined. */
3543 static int
3544 layout_conflict_p (tree type,
3545 tree offset,
3546 splay_tree offsets,
3547 int vbases_p)
3549 splay_tree_node max_node;
3551 /* Get the node in OFFSETS that indicates the maximum offset where
3552 an empty subobject is located. */
3553 max_node = splay_tree_max (offsets);
3554 /* If there aren't any empty subobjects, then there's no point in
3555 performing this check. */
3556 if (!max_node)
3557 return 0;
3559 return walk_subobject_offsets (type, check_subobject_offset, offset,
3560 offsets, (tree) (max_node->key),
3561 vbases_p);
3564 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3565 non-static data member of the type indicated by RLI. BINFO is the
3566 binfo corresponding to the base subobject, OFFSETS maps offsets to
3567 types already located at those offsets. This function determines
3568 the position of the DECL. */
3570 static void
3571 layout_nonempty_base_or_field (record_layout_info rli,
3572 tree decl,
3573 tree binfo,
3574 splay_tree offsets)
3576 tree offset = NULL_TREE;
3577 bool field_p;
3578 tree type;
3580 if (binfo)
3582 /* For the purposes of determining layout conflicts, we want to
3583 use the class type of BINFO; TREE_TYPE (DECL) will be the
3584 CLASSTYPE_AS_BASE version, which does not contain entries for
3585 zero-sized bases. */
3586 type = TREE_TYPE (binfo);
3587 field_p = false;
3589 else
3591 type = TREE_TYPE (decl);
3592 field_p = true;
3595 /* Try to place the field. It may take more than one try if we have
3596 a hard time placing the field without putting two objects of the
3597 same type at the same address. */
3598 while (1)
3600 struct record_layout_info_s old_rli = *rli;
3602 /* Place this field. */
3603 place_field (rli, decl);
3604 offset = byte_position (decl);
3606 /* We have to check to see whether or not there is already
3607 something of the same type at the offset we're about to use.
3608 For example, consider:
3610 struct S {};
3611 struct T : public S { int i; };
3612 struct U : public S, public T {};
3614 Here, we put S at offset zero in U. Then, we can't put T at
3615 offset zero -- its S component would be at the same address
3616 as the S we already allocated. So, we have to skip ahead.
3617 Since all data members, including those whose type is an
3618 empty class, have nonzero size, any overlap can happen only
3619 with a direct or indirect base-class -- it can't happen with
3620 a data member. */
3621 /* In a union, overlap is permitted; all members are placed at
3622 offset zero. */
3623 if (TREE_CODE (rli->t) == UNION_TYPE)
3624 break;
3625 /* G++ 3.2 did not check for overlaps when placing a non-empty
3626 virtual base. */
3627 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
3628 break;
3629 if (layout_conflict_p (field_p ? type : binfo, offset,
3630 offsets, field_p))
3632 /* Strip off the size allocated to this field. That puts us
3633 at the first place we could have put the field with
3634 proper alignment. */
3635 *rli = old_rli;
3637 /* Bump up by the alignment required for the type. */
3638 rli->bitpos
3639 = size_binop (PLUS_EXPR, rli->bitpos,
3640 bitsize_int (binfo
3641 ? CLASSTYPE_ALIGN (type)
3642 : TYPE_ALIGN (type)));
3643 normalize_rli (rli);
3645 else
3646 /* There was no conflict. We're done laying out this field. */
3647 break;
3650 /* Now that we know where it will be placed, update its
3651 BINFO_OFFSET. */
3652 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3653 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3654 this point because their BINFO_OFFSET is copied from another
3655 hierarchy. Therefore, we may not need to add the entire
3656 OFFSET. */
3657 propagate_binfo_offsets (binfo,
3658 size_diffop_loc (input_location,
3659 convert (ssizetype, offset),
3660 convert (ssizetype,
3661 BINFO_OFFSET (binfo))));
3664 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3666 static int
3667 empty_base_at_nonzero_offset_p (tree type,
3668 tree offset,
3669 splay_tree offsets ATTRIBUTE_UNUSED)
3671 return is_empty_class (type) && !integer_zerop (offset);
3674 /* Layout the empty base BINFO. EOC indicates the byte currently just
3675 past the end of the class, and should be correctly aligned for a
3676 class of the type indicated by BINFO; OFFSETS gives the offsets of
3677 the empty bases allocated so far. T is the most derived
3678 type. Return nonzero iff we added it at the end. */
3680 static bool
3681 layout_empty_base (record_layout_info rli, tree binfo,
3682 tree eoc, splay_tree offsets)
3684 tree alignment;
3685 tree basetype = BINFO_TYPE (binfo);
3686 bool atend = false;
3688 /* This routine should only be used for empty classes. */
3689 gcc_assert (is_empty_class (basetype));
3690 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3692 if (!integer_zerop (BINFO_OFFSET (binfo)))
3694 if (abi_version_at_least (2))
3695 propagate_binfo_offsets
3696 (binfo, size_diffop_loc (input_location,
3697 size_zero_node, BINFO_OFFSET (binfo)));
3698 else
3699 warning (OPT_Wabi,
3700 "offset of empty base %qT may not be ABI-compliant and may"
3701 "change in a future version of GCC",
3702 BINFO_TYPE (binfo));
3705 /* This is an empty base class. We first try to put it at offset
3706 zero. */
3707 if (layout_conflict_p (binfo,
3708 BINFO_OFFSET (binfo),
3709 offsets,
3710 /*vbases_p=*/0))
3712 /* That didn't work. Now, we move forward from the next
3713 available spot in the class. */
3714 atend = true;
3715 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3716 while (1)
3718 if (!layout_conflict_p (binfo,
3719 BINFO_OFFSET (binfo),
3720 offsets,
3721 /*vbases_p=*/0))
3722 /* We finally found a spot where there's no overlap. */
3723 break;
3725 /* There's overlap here, too. Bump along to the next spot. */
3726 propagate_binfo_offsets (binfo, alignment);
3730 if (CLASSTYPE_USER_ALIGN (basetype))
3732 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
3733 if (warn_packed)
3734 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
3735 TYPE_USER_ALIGN (rli->t) = 1;
3738 return atend;
3741 /* Layout the base given by BINFO in the class indicated by RLI.
3742 *BASE_ALIGN is a running maximum of the alignments of
3743 any base class. OFFSETS gives the location of empty base
3744 subobjects. T is the most derived type. Return nonzero if the new
3745 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3746 *NEXT_FIELD, unless BINFO is for an empty base class.
3748 Returns the location at which the next field should be inserted. */
3750 static tree *
3751 build_base_field (record_layout_info rli, tree binfo,
3752 splay_tree offsets, tree *next_field)
3754 tree t = rli->t;
3755 tree basetype = BINFO_TYPE (binfo);
3757 if (!COMPLETE_TYPE_P (basetype))
3758 /* This error is now reported in xref_tag, thus giving better
3759 location information. */
3760 return next_field;
3762 /* Place the base class. */
3763 if (!is_empty_class (basetype))
3765 tree decl;
3767 /* The containing class is non-empty because it has a non-empty
3768 base class. */
3769 CLASSTYPE_EMPTY_P (t) = 0;
3771 /* Create the FIELD_DECL. */
3772 decl = build_decl (input_location,
3773 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3774 DECL_ARTIFICIAL (decl) = 1;
3775 DECL_IGNORED_P (decl) = 1;
3776 DECL_FIELD_CONTEXT (decl) = t;
3777 if (CLASSTYPE_AS_BASE (basetype))
3779 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3780 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3781 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3782 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3783 DECL_MODE (decl) = TYPE_MODE (basetype);
3784 DECL_FIELD_IS_BASE (decl) = 1;
3786 /* Try to place the field. It may take more than one try if we
3787 have a hard time placing the field without putting two
3788 objects of the same type at the same address. */
3789 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3790 /* Add the new FIELD_DECL to the list of fields for T. */
3791 TREE_CHAIN (decl) = *next_field;
3792 *next_field = decl;
3793 next_field = &TREE_CHAIN (decl);
3796 else
3798 tree eoc;
3799 bool atend;
3801 /* On some platforms (ARM), even empty classes will not be
3802 byte-aligned. */
3803 eoc = round_up_loc (input_location,
3804 rli_size_unit_so_far (rli),
3805 CLASSTYPE_ALIGN_UNIT (basetype));
3806 atend = layout_empty_base (rli, binfo, eoc, offsets);
3807 /* A nearly-empty class "has no proper base class that is empty,
3808 not morally virtual, and at an offset other than zero." */
3809 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3811 if (atend)
3812 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3813 /* The check above (used in G++ 3.2) is insufficient because
3814 an empty class placed at offset zero might itself have an
3815 empty base at a nonzero offset. */
3816 else if (walk_subobject_offsets (basetype,
3817 empty_base_at_nonzero_offset_p,
3818 size_zero_node,
3819 /*offsets=*/NULL,
3820 /*max_offset=*/NULL_TREE,
3821 /*vbases_p=*/true))
3823 if (abi_version_at_least (2))
3824 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3825 else
3826 warning (OPT_Wabi,
3827 "class %qT will be considered nearly empty in a "
3828 "future version of GCC", t);
3832 /* We do not create a FIELD_DECL for empty base classes because
3833 it might overlap some other field. We want to be able to
3834 create CONSTRUCTORs for the class by iterating over the
3835 FIELD_DECLs, and the back end does not handle overlapping
3836 FIELD_DECLs. */
3838 /* An empty virtual base causes a class to be non-empty
3839 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3840 here because that was already done when the virtual table
3841 pointer was created. */
3844 /* Record the offsets of BINFO and its base subobjects. */
3845 record_subobject_offsets (binfo,
3846 BINFO_OFFSET (binfo),
3847 offsets,
3848 /*is_data_member=*/false);
3850 return next_field;
3853 /* Layout all of the non-virtual base classes. Record empty
3854 subobjects in OFFSETS. T is the most derived type. Return nonzero
3855 if the type cannot be nearly empty. The fields created
3856 corresponding to the base classes will be inserted at
3857 *NEXT_FIELD. */
3859 static void
3860 build_base_fields (record_layout_info rli,
3861 splay_tree offsets, tree *next_field)
3863 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3864 subobjects. */
3865 tree t = rli->t;
3866 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
3867 int i;
3869 /* The primary base class is always allocated first. */
3870 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3871 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
3872 offsets, next_field);
3874 /* Now allocate the rest of the bases. */
3875 for (i = 0; i < n_baseclasses; ++i)
3877 tree base_binfo;
3879 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
3881 /* The primary base was already allocated above, so we don't
3882 need to allocate it again here. */
3883 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
3884 continue;
3886 /* Virtual bases are added at the end (a primary virtual base
3887 will have already been added). */
3888 if (BINFO_VIRTUAL_P (base_binfo))
3889 continue;
3891 next_field = build_base_field (rli, base_binfo,
3892 offsets, next_field);
3896 /* Go through the TYPE_METHODS of T issuing any appropriate
3897 diagnostics, figuring out which methods override which other
3898 methods, and so forth. */
3900 static void
3901 check_methods (tree t)
3903 tree x;
3905 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3907 check_for_override (x, t);
3908 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3909 error ("initializer specified for non-virtual method %q+D", x);
3910 /* The name of the field is the original field name
3911 Save this in auxiliary field for later overloading. */
3912 if (DECL_VINDEX (x))
3914 TYPE_POLYMORPHIC_P (t) = 1;
3915 if (DECL_PURE_VIRTUAL_P (x))
3916 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
3918 /* All user-provided destructors are non-trivial.
3919 Constructors and assignment ops are handled in
3920 grok_special_member_properties. */
3921 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
3922 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
3926 /* FN is a constructor or destructor. Clone the declaration to create
3927 a specialized in-charge or not-in-charge version, as indicated by
3928 NAME. */
3930 static tree
3931 build_clone (tree fn, tree name)
3933 tree parms;
3934 tree clone;
3936 /* Copy the function. */
3937 clone = copy_decl (fn);
3938 /* Reset the function name. */
3939 DECL_NAME (clone) = name;
3940 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3941 /* Remember where this function came from. */
3942 DECL_ABSTRACT_ORIGIN (clone) = fn;
3943 /* Make it easy to find the CLONE given the FN. */
3944 TREE_CHAIN (clone) = TREE_CHAIN (fn);
3945 TREE_CHAIN (fn) = clone;
3947 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
3948 if (TREE_CODE (clone) == TEMPLATE_DECL)
3950 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
3951 DECL_TEMPLATE_RESULT (clone) = result;
3952 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
3953 DECL_TI_TEMPLATE (result) = clone;
3954 TREE_TYPE (clone) = TREE_TYPE (result);
3955 return clone;
3958 DECL_CLONED_FUNCTION (clone) = fn;
3959 /* There's no pending inline data for this function. */
3960 DECL_PENDING_INLINE_INFO (clone) = NULL;
3961 DECL_PENDING_INLINE_P (clone) = 0;
3963 /* The base-class destructor is not virtual. */
3964 if (name == base_dtor_identifier)
3966 DECL_VIRTUAL_P (clone) = 0;
3967 if (TREE_CODE (clone) != TEMPLATE_DECL)
3968 DECL_VINDEX (clone) = NULL_TREE;
3971 /* If there was an in-charge parameter, drop it from the function
3972 type. */
3973 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3975 tree basetype;
3976 tree parmtypes;
3977 tree exceptions;
3979 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3980 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3981 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3982 /* Skip the `this' parameter. */
3983 parmtypes = TREE_CHAIN (parmtypes);
3984 /* Skip the in-charge parameter. */
3985 parmtypes = TREE_CHAIN (parmtypes);
3986 /* And the VTT parm, in a complete [cd]tor. */
3987 if (DECL_HAS_VTT_PARM_P (fn)
3988 && ! DECL_NEEDS_VTT_PARM_P (clone))
3989 parmtypes = TREE_CHAIN (parmtypes);
3990 /* If this is subobject constructor or destructor, add the vtt
3991 parameter. */
3992 TREE_TYPE (clone)
3993 = build_method_type_directly (basetype,
3994 TREE_TYPE (TREE_TYPE (clone)),
3995 parmtypes);
3996 if (exceptions)
3997 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3998 exceptions);
3999 TREE_TYPE (clone)
4000 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4001 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4004 /* Copy the function parameters. */
4005 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4006 /* Remove the in-charge parameter. */
4007 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4009 TREE_CHAIN (DECL_ARGUMENTS (clone))
4010 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4011 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4013 /* And the VTT parm, in a complete [cd]tor. */
4014 if (DECL_HAS_VTT_PARM_P (fn))
4016 if (DECL_NEEDS_VTT_PARM_P (clone))
4017 DECL_HAS_VTT_PARM_P (clone) = 1;
4018 else
4020 TREE_CHAIN (DECL_ARGUMENTS (clone))
4021 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4022 DECL_HAS_VTT_PARM_P (clone) = 0;
4026 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4028 DECL_CONTEXT (parms) = clone;
4029 cxx_dup_lang_specific_decl (parms);
4032 /* Create the RTL for this function. */
4033 SET_DECL_RTL (clone, NULL);
4034 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4036 if (pch_file)
4037 note_decl_for_pch (clone);
4039 return clone;
4042 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4043 not invoke this function directly.
4045 For a non-thunk function, returns the address of the slot for storing
4046 the function it is a clone of. Otherwise returns NULL_TREE.
4048 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4049 cloned_function is unset. This is to support the separate
4050 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4051 on a template makes sense, but not the former. */
4053 tree *
4054 decl_cloned_function_p (const_tree decl, bool just_testing)
4056 tree *ptr;
4057 if (just_testing)
4058 decl = STRIP_TEMPLATE (decl);
4060 if (TREE_CODE (decl) != FUNCTION_DECL
4061 || !DECL_LANG_SPECIFIC (decl)
4062 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4064 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4065 if (!just_testing)
4066 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4067 else
4068 #endif
4069 return NULL;
4072 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4073 if (just_testing && *ptr == NULL_TREE)
4074 return NULL;
4075 else
4076 return ptr;
4079 /* Produce declarations for all appropriate clones of FN. If
4080 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4081 CLASTYPE_METHOD_VEC as well. */
4083 void
4084 clone_function_decl (tree fn, int update_method_vec_p)
4086 tree clone;
4088 /* Avoid inappropriate cloning. */
4089 if (TREE_CHAIN (fn)
4090 && DECL_CLONED_FUNCTION_P (TREE_CHAIN (fn)))
4091 return;
4093 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4095 /* For each constructor, we need two variants: an in-charge version
4096 and a not-in-charge version. */
4097 clone = build_clone (fn, complete_ctor_identifier);
4098 if (update_method_vec_p)
4099 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4100 clone = build_clone (fn, base_ctor_identifier);
4101 if (update_method_vec_p)
4102 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4104 else
4106 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4108 /* For each destructor, we need three variants: an in-charge
4109 version, a not-in-charge version, and an in-charge deleting
4110 version. We clone the deleting version first because that
4111 means it will go second on the TYPE_METHODS list -- and that
4112 corresponds to the correct layout order in the virtual
4113 function table.
4115 For a non-virtual destructor, we do not build a deleting
4116 destructor. */
4117 if (DECL_VIRTUAL_P (fn))
4119 clone = build_clone (fn, deleting_dtor_identifier);
4120 if (update_method_vec_p)
4121 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4123 clone = build_clone (fn, complete_dtor_identifier);
4124 if (update_method_vec_p)
4125 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4126 clone = build_clone (fn, base_dtor_identifier);
4127 if (update_method_vec_p)
4128 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4131 /* Note that this is an abstract function that is never emitted. */
4132 DECL_ABSTRACT (fn) = 1;
4135 /* DECL is an in charge constructor, which is being defined. This will
4136 have had an in class declaration, from whence clones were
4137 declared. An out-of-class definition can specify additional default
4138 arguments. As it is the clones that are involved in overload
4139 resolution, we must propagate the information from the DECL to its
4140 clones. */
4142 void
4143 adjust_clone_args (tree decl)
4145 tree clone;
4147 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4148 clone = TREE_CHAIN (clone))
4150 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4151 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4152 tree decl_parms, clone_parms;
4154 clone_parms = orig_clone_parms;
4156 /* Skip the 'this' parameter. */
4157 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4158 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4160 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4161 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4162 if (DECL_HAS_VTT_PARM_P (decl))
4163 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4165 clone_parms = orig_clone_parms;
4166 if (DECL_HAS_VTT_PARM_P (clone))
4167 clone_parms = TREE_CHAIN (clone_parms);
4169 for (decl_parms = orig_decl_parms; decl_parms;
4170 decl_parms = TREE_CHAIN (decl_parms),
4171 clone_parms = TREE_CHAIN (clone_parms))
4173 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4174 TREE_TYPE (clone_parms)));
4176 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4178 /* A default parameter has been added. Adjust the
4179 clone's parameters. */
4180 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4181 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4182 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4183 tree type;
4185 clone_parms = orig_decl_parms;
4187 if (DECL_HAS_VTT_PARM_P (clone))
4189 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4190 TREE_VALUE (orig_clone_parms),
4191 clone_parms);
4192 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4194 type = build_method_type_directly (basetype,
4195 TREE_TYPE (TREE_TYPE (clone)),
4196 clone_parms);
4197 if (exceptions)
4198 type = build_exception_variant (type, exceptions);
4199 if (attrs)
4200 type = cp_build_type_attribute_variant (type, attrs);
4201 TREE_TYPE (clone) = type;
4203 clone_parms = NULL_TREE;
4204 break;
4207 gcc_assert (!clone_parms);
4211 /* For each of the constructors and destructors in T, create an
4212 in-charge and not-in-charge variant. */
4214 static void
4215 clone_constructors_and_destructors (tree t)
4217 tree fns;
4219 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4220 out now. */
4221 if (!CLASSTYPE_METHOD_VEC (t))
4222 return;
4224 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4225 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4226 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4227 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4230 /* Returns true iff class T has a user-defined constructor other than
4231 the default constructor. */
4233 bool
4234 type_has_user_nondefault_constructor (tree t)
4236 tree fns;
4238 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4239 return false;
4241 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4243 tree fn = OVL_CURRENT (fns);
4244 if (!DECL_ARTIFICIAL (fn)
4245 && (TREE_CODE (fn) == TEMPLATE_DECL
4246 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4247 != NULL_TREE)))
4248 return true;
4251 return false;
4254 /* Returns the defaulted constructor if T has one. Otherwise, returns
4255 NULL_TREE. */
4257 tree
4258 in_class_defaulted_default_constructor (tree t)
4260 tree fns, args;
4262 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4263 return NULL_TREE;
4265 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4267 tree fn = OVL_CURRENT (fns);
4269 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4271 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4272 while (args && TREE_PURPOSE (args))
4273 args = TREE_CHAIN (args);
4274 if (!args || args == void_list_node)
4275 return fn;
4279 return NULL_TREE;
4282 /* Returns true iff FN is a user-provided function, i.e. user-declared
4283 and not defaulted at its first declaration; or explicit, private,
4284 protected, or non-const. */
4286 bool
4287 user_provided_p (tree fn)
4289 if (TREE_CODE (fn) == TEMPLATE_DECL)
4290 return true;
4291 else
4292 return (!DECL_ARTIFICIAL (fn)
4293 && !DECL_DEFAULTED_IN_CLASS_P (fn));
4296 /* Returns true iff class T has a user-provided constructor. */
4298 bool
4299 type_has_user_provided_constructor (tree t)
4301 tree fns;
4303 if (!CLASS_TYPE_P (t))
4304 return false;
4306 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4307 return false;
4309 /* This can happen in error cases; avoid crashing. */
4310 if (!CLASSTYPE_METHOD_VEC (t))
4311 return false;
4313 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4314 if (user_provided_p (OVL_CURRENT (fns)))
4315 return true;
4317 return false;
4320 /* Returns true iff class T has a user-provided default constructor. */
4322 bool
4323 type_has_user_provided_default_constructor (tree t)
4325 tree fns;
4327 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4328 return false;
4330 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4332 tree fn = OVL_CURRENT (fns);
4333 if (TREE_CODE (fn) == FUNCTION_DECL
4334 && user_provided_p (fn)
4335 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
4336 return true;
4339 return false;
4342 /* Returns true iff class TYPE has a virtual destructor. */
4344 bool
4345 type_has_virtual_destructor (tree type)
4347 tree dtor;
4349 if (!CLASS_TYPE_P (type))
4350 return false;
4352 gcc_assert (COMPLETE_TYPE_P (type));
4353 dtor = CLASSTYPE_DESTRUCTORS (type);
4354 return (dtor && DECL_VIRTUAL_P (dtor));
4357 /* Returns true iff class T has a move constructor. */
4359 bool
4360 type_has_move_constructor (tree t)
4362 tree fns;
4364 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4366 gcc_assert (COMPLETE_TYPE_P (t));
4367 lazily_declare_fn (sfk_move_constructor, t);
4370 if (!CLASSTYPE_METHOD_VEC (t))
4371 return false;
4373 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4374 if (move_fn_p (OVL_CURRENT (fns)))
4375 return true;
4377 return false;
4380 /* Returns true iff class T has a move assignment operator. */
4382 bool
4383 type_has_move_assign (tree t)
4385 tree fns;
4387 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4389 gcc_assert (COMPLETE_TYPE_P (t));
4390 lazily_declare_fn (sfk_move_assignment, t);
4393 for (fns = lookup_fnfields_slot (t, ansi_assopname (NOP_EXPR));
4394 fns; fns = OVL_NEXT (fns))
4395 if (move_fn_p (OVL_CURRENT (fns)))
4396 return true;
4398 return false;
4401 /* Remove all zero-width bit-fields from T. */
4403 static void
4404 remove_zero_width_bit_fields (tree t)
4406 tree *fieldsp;
4408 fieldsp = &TYPE_FIELDS (t);
4409 while (*fieldsp)
4411 if (TREE_CODE (*fieldsp) == FIELD_DECL
4412 && DECL_C_BIT_FIELD (*fieldsp)
4413 /* We should not be confused by the fact that grokbitfield
4414 temporarily sets the width of the bit field into
4415 DECL_INITIAL (*fieldsp).
4416 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
4417 to that width. */
4418 && integer_zerop (DECL_SIZE (*fieldsp)))
4419 *fieldsp = TREE_CHAIN (*fieldsp);
4420 else
4421 fieldsp = &TREE_CHAIN (*fieldsp);
4425 /* Returns TRUE iff we need a cookie when dynamically allocating an
4426 array whose elements have the indicated class TYPE. */
4428 static bool
4429 type_requires_array_cookie (tree type)
4431 tree fns;
4432 bool has_two_argument_delete_p = false;
4434 gcc_assert (CLASS_TYPE_P (type));
4436 /* If there's a non-trivial destructor, we need a cookie. In order
4437 to iterate through the array calling the destructor for each
4438 element, we'll have to know how many elements there are. */
4439 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4440 return true;
4442 /* If the usual deallocation function is a two-argument whose second
4443 argument is of type `size_t', then we have to pass the size of
4444 the array to the deallocation function, so we will need to store
4445 a cookie. */
4446 fns = lookup_fnfields (TYPE_BINFO (type),
4447 ansi_opname (VEC_DELETE_EXPR),
4448 /*protect=*/0);
4449 /* If there are no `operator []' members, or the lookup is
4450 ambiguous, then we don't need a cookie. */
4451 if (!fns || fns == error_mark_node)
4452 return false;
4453 /* Loop through all of the functions. */
4454 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4456 tree fn;
4457 tree second_parm;
4459 /* Select the current function. */
4460 fn = OVL_CURRENT (fns);
4461 /* See if this function is a one-argument delete function. If
4462 it is, then it will be the usual deallocation function. */
4463 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4464 if (second_parm == void_list_node)
4465 return false;
4466 /* Do not consider this function if its second argument is an
4467 ellipsis. */
4468 if (!second_parm)
4469 continue;
4470 /* Otherwise, if we have a two-argument function and the second
4471 argument is `size_t', it will be the usual deallocation
4472 function -- unless there is one-argument function, too. */
4473 if (TREE_CHAIN (second_parm) == void_list_node
4474 && same_type_p (TREE_VALUE (second_parm), size_type_node))
4475 has_two_argument_delete_p = true;
4478 return has_two_argument_delete_p;
4481 /* Check the validity of the bases and members declared in T. Add any
4482 implicitly-generated functions (like copy-constructors and
4483 assignment operators). Compute various flag bits (like
4484 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
4485 level: i.e., independently of the ABI in use. */
4487 static void
4488 check_bases_and_members (tree t)
4490 /* Nonzero if the implicitly generated copy constructor should take
4491 a non-const reference argument. */
4492 int cant_have_const_ctor;
4493 /* Nonzero if the implicitly generated assignment operator
4494 should take a non-const reference argument. */
4495 int no_const_asn_ref;
4496 int cant_have_lazy_ctor = 0;
4497 int cant_have_lazy_opeq = 0;
4498 tree access_decls;
4499 bool saved_complex_asn_ref;
4500 bool saved_nontrivial_dtor;
4501 tree fn;
4503 /* By default, we use const reference arguments and generate default
4504 constructors. */
4505 cant_have_const_ctor = 0;
4506 no_const_asn_ref = 0;
4508 /* Check all the base-classes. */
4509 check_bases (t, &cant_have_const_ctor,
4510 &no_const_asn_ref, &cant_have_lazy_ctor,
4511 &cant_have_lazy_opeq);
4513 /* Check all the method declarations. */
4514 check_methods (t);
4516 /* Save the initial values of these flags which only indicate whether
4517 or not the class has user-provided functions. As we analyze the
4518 bases and members we can set these flags for other reasons. */
4519 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
4520 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
4522 /* Check all the data member declarations. We cannot call
4523 check_field_decls until we have called check_bases check_methods,
4524 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4525 being set appropriately. */
4526 check_field_decls (t, &access_decls,
4527 &cant_have_const_ctor,
4528 &no_const_asn_ref,
4529 &cant_have_lazy_ctor,
4530 &cant_have_lazy_opeq);
4532 /* A nearly-empty class has to be vptr-containing; a nearly empty
4533 class contains just a vptr. */
4534 if (!TYPE_CONTAINS_VPTR_P (t))
4535 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4537 /* Do some bookkeeping that will guide the generation of implicitly
4538 declared member functions. */
4539 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
4540 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
4541 /* We need to call a constructor for this class if it has a
4542 user-provided constructor, or if the default constructor is going
4543 to initialize the vptr. (This is not an if-and-only-if;
4544 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
4545 themselves need constructing.) */
4546 TYPE_NEEDS_CONSTRUCTING (t)
4547 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
4548 /* [dcl.init.aggr]
4550 An aggregate is an array or a class with no user-provided
4551 constructors ... and no virtual functions.
4553 Again, other conditions for being an aggregate are checked
4554 elsewhere. */
4555 CLASSTYPE_NON_AGGREGATE (t)
4556 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
4557 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
4558 retain the old definition internally for ABI reasons. */
4559 CLASSTYPE_NON_LAYOUT_POD_P (t)
4560 |= (CLASSTYPE_NON_AGGREGATE (t)
4561 || saved_nontrivial_dtor || saved_complex_asn_ref);
4562 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
4563 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
4564 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
4565 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
4567 /* If the class has no user-declared constructor, but does have
4568 non-static const or reference data members that can never be
4569 initialized, issue a warning. */
4570 if (warn_uninitialized
4571 /* Classes with user-declared constructors are presumed to
4572 initialize these members. */
4573 && !TYPE_HAS_USER_CONSTRUCTOR (t)
4574 /* Aggregates can be initialized with brace-enclosed
4575 initializers. */
4576 && CLASSTYPE_NON_AGGREGATE (t))
4578 tree field;
4580 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
4582 tree type;
4584 if (TREE_CODE (field) != FIELD_DECL)
4585 continue;
4587 type = TREE_TYPE (field);
4588 if (TREE_CODE (type) == REFERENCE_TYPE)
4589 warning (OPT_Wuninitialized, "non-static reference %q+#D "
4590 "in class without a constructor", field);
4591 else if (CP_TYPE_CONST_P (type)
4592 && (!CLASS_TYPE_P (type)
4593 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
4594 warning (OPT_Wuninitialized, "non-static const member %q+#D "
4595 "in class without a constructor", field);
4599 /* Synthesize any needed methods. */
4600 add_implicitly_declared_members (t,
4601 cant_have_const_ctor,
4602 no_const_asn_ref,
4603 cant_have_lazy_ctor,
4604 cant_have_lazy_opeq);
4606 /* Check defaulted declarations here so we have cant_have_const_ctor
4607 and don't need to worry about clones. */
4608 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4609 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4611 int copy = copy_fn_p (fn);
4612 if (copy > 0)
4614 bool imp_const_p
4615 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
4616 : !no_const_asn_ref);
4617 bool fn_const_p = (copy == 2);
4619 if (fn_const_p && !imp_const_p)
4620 /* If the function is defaulted outside the class, we just
4621 give the synthesis error. */
4622 error ("%q+D declared to take const reference, but implicit "
4623 "declaration would take non-const", fn);
4624 else if (imp_const_p && !fn_const_p)
4625 error ("%q+D declared to take non-const reference cannot be "
4626 "defaulted in the class body", fn);
4628 defaulted_late_check (fn);
4631 if (LAMBDA_TYPE_P (t))
4633 /* "The closure type associated with a lambda-expression has a deleted
4634 default constructor and a deleted copy assignment operator." */
4635 TYPE_NEEDS_CONSTRUCTING (t) = 1;
4636 TYPE_HAS_COMPLEX_DFLT (t) = 1;
4637 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
4638 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
4640 /* "This class type is not an aggregate." */
4641 CLASSTYPE_NON_AGGREGATE (t) = 1;
4644 /* Create the in-charge and not-in-charge variants of constructors
4645 and destructors. */
4646 clone_constructors_and_destructors (t);
4648 /* Process the using-declarations. */
4649 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4650 handle_using_decl (TREE_VALUE (access_decls), t);
4652 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4653 finish_struct_methods (t);
4655 /* Figure out whether or not we will need a cookie when dynamically
4656 allocating an array of this type. */
4657 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4658 = type_requires_array_cookie (t);
4661 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4662 accordingly. If a new vfield was created (because T doesn't have a
4663 primary base class), then the newly created field is returned. It
4664 is not added to the TYPE_FIELDS list; it is the caller's
4665 responsibility to do that. Accumulate declared virtual functions
4666 on VIRTUALS_P. */
4668 static tree
4669 create_vtable_ptr (tree t, tree* virtuals_p)
4671 tree fn;
4673 /* Collect the virtual functions declared in T. */
4674 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4675 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4676 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4678 tree new_virtual = make_node (TREE_LIST);
4680 BV_FN (new_virtual) = fn;
4681 BV_DELTA (new_virtual) = integer_zero_node;
4682 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
4684 TREE_CHAIN (new_virtual) = *virtuals_p;
4685 *virtuals_p = new_virtual;
4688 /* If we couldn't find an appropriate base class, create a new field
4689 here. Even if there weren't any new virtual functions, we might need a
4690 new virtual function table if we're supposed to include vptrs in
4691 all classes that need them. */
4692 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
4694 /* We build this decl with vtbl_ptr_type_node, which is a
4695 `vtable_entry_type*'. It might seem more precise to use
4696 `vtable_entry_type (*)[N]' where N is the number of virtual
4697 functions. However, that would require the vtable pointer in
4698 base classes to have a different type than the vtable pointer
4699 in derived classes. We could make that happen, but that
4700 still wouldn't solve all the problems. In particular, the
4701 type-based alias analysis code would decide that assignments
4702 to the base class vtable pointer can't alias assignments to
4703 the derived class vtable pointer, since they have different
4704 types. Thus, in a derived class destructor, where the base
4705 class constructor was inlined, we could generate bad code for
4706 setting up the vtable pointer.
4708 Therefore, we use one type for all vtable pointers. We still
4709 use a type-correct type; it's just doesn't indicate the array
4710 bounds. That's better than using `void*' or some such; it's
4711 cleaner, and it let's the alias analysis code know that these
4712 stores cannot alias stores to void*! */
4713 tree field;
4715 field = build_decl (input_location,
4716 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4717 DECL_VIRTUAL_P (field) = 1;
4718 DECL_ARTIFICIAL (field) = 1;
4719 DECL_FIELD_CONTEXT (field) = t;
4720 DECL_FCONTEXT (field) = t;
4721 if (TYPE_PACKED (t))
4722 DECL_PACKED (field) = 1;
4724 TYPE_VFIELD (t) = field;
4726 /* This class is non-empty. */
4727 CLASSTYPE_EMPTY_P (t) = 0;
4729 return field;
4732 return NULL_TREE;
4735 /* Add OFFSET to all base types of BINFO which is a base in the
4736 hierarchy dominated by T.
4738 OFFSET, which is a type offset, is number of bytes. */
4740 static void
4741 propagate_binfo_offsets (tree binfo, tree offset)
4743 int i;
4744 tree primary_binfo;
4745 tree base_binfo;
4747 /* Update BINFO's offset. */
4748 BINFO_OFFSET (binfo)
4749 = convert (sizetype,
4750 size_binop (PLUS_EXPR,
4751 convert (ssizetype, BINFO_OFFSET (binfo)),
4752 offset));
4754 /* Find the primary base class. */
4755 primary_binfo = get_primary_binfo (binfo);
4757 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
4758 propagate_binfo_offsets (primary_binfo, offset);
4760 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4761 downwards. */
4762 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4764 /* Don't do the primary base twice. */
4765 if (base_binfo == primary_binfo)
4766 continue;
4768 if (BINFO_VIRTUAL_P (base_binfo))
4769 continue;
4771 propagate_binfo_offsets (base_binfo, offset);
4775 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
4776 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4777 empty subobjects of T. */
4779 static void
4780 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
4782 tree vbase;
4783 tree t = rli->t;
4784 bool first_vbase = true;
4785 tree *next_field;
4787 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
4788 return;
4790 if (!abi_version_at_least(2))
4792 /* In G++ 3.2, we incorrectly rounded the size before laying out
4793 the virtual bases. */
4794 finish_record_layout (rli, /*free_p=*/false);
4795 #ifdef STRUCTURE_SIZE_BOUNDARY
4796 /* Packed structures don't need to have minimum size. */
4797 if (! TYPE_PACKED (t))
4798 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
4799 #endif
4800 rli->offset = TYPE_SIZE_UNIT (t);
4801 rli->bitpos = bitsize_zero_node;
4802 rli->record_align = TYPE_ALIGN (t);
4805 /* Find the last field. The artificial fields created for virtual
4806 bases will go after the last extant field to date. */
4807 next_field = &TYPE_FIELDS (t);
4808 while (*next_field)
4809 next_field = &TREE_CHAIN (*next_field);
4811 /* Go through the virtual bases, allocating space for each virtual
4812 base that is not already a primary base class. These are
4813 allocated in inheritance graph order. */
4814 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
4816 if (!BINFO_VIRTUAL_P (vbase))
4817 continue;
4819 if (!BINFO_PRIMARY_P (vbase))
4821 tree basetype = TREE_TYPE (vbase);
4823 /* This virtual base is not a primary base of any class in the
4824 hierarchy, so we have to add space for it. */
4825 next_field = build_base_field (rli, vbase,
4826 offsets, next_field);
4828 /* If the first virtual base might have been placed at a
4829 lower address, had we started from CLASSTYPE_SIZE, rather
4830 than TYPE_SIZE, issue a warning. There can be both false
4831 positives and false negatives from this warning in rare
4832 cases; to deal with all the possibilities would probably
4833 require performing both layout algorithms and comparing
4834 the results which is not particularly tractable. */
4835 if (warn_abi
4836 && first_vbase
4837 && (tree_int_cst_lt
4838 (size_binop (CEIL_DIV_EXPR,
4839 round_up_loc (input_location,
4840 CLASSTYPE_SIZE (t),
4841 CLASSTYPE_ALIGN (basetype)),
4842 bitsize_unit_node),
4843 BINFO_OFFSET (vbase))))
4844 warning (OPT_Wabi,
4845 "offset of virtual base %qT is not ABI-compliant and "
4846 "may change in a future version of GCC",
4847 basetype);
4849 first_vbase = false;
4854 /* Returns the offset of the byte just past the end of the base class
4855 BINFO. */
4857 static tree
4858 end_of_base (tree binfo)
4860 tree size;
4862 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
4863 size = TYPE_SIZE_UNIT (char_type_node);
4864 else if (is_empty_class (BINFO_TYPE (binfo)))
4865 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4866 allocate some space for it. It cannot have virtual bases, so
4867 TYPE_SIZE_UNIT is fine. */
4868 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4869 else
4870 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4872 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
4875 /* Returns the offset of the byte just past the end of the base class
4876 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4877 only non-virtual bases are included. */
4879 static tree
4880 end_of_class (tree t, int include_virtuals_p)
4882 tree result = size_zero_node;
4883 VEC(tree,gc) *vbases;
4884 tree binfo;
4885 tree base_binfo;
4886 tree offset;
4887 int i;
4889 for (binfo = TYPE_BINFO (t), i = 0;
4890 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4892 if (!include_virtuals_p
4893 && BINFO_VIRTUAL_P (base_binfo)
4894 && (!BINFO_PRIMARY_P (base_binfo)
4895 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
4896 continue;
4898 offset = end_of_base (base_binfo);
4899 if (INT_CST_LT_UNSIGNED (result, offset))
4900 result = offset;
4903 /* G++ 3.2 did not check indirect virtual bases. */
4904 if (abi_version_at_least (2) && include_virtuals_p)
4905 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
4906 VEC_iterate (tree, vbases, i, base_binfo); i++)
4908 offset = end_of_base (base_binfo);
4909 if (INT_CST_LT_UNSIGNED (result, offset))
4910 result = offset;
4913 return result;
4916 /* Warn about bases of T that are inaccessible because they are
4917 ambiguous. For example:
4919 struct S {};
4920 struct T : public S {};
4921 struct U : public S, public T {};
4923 Here, `(S*) new U' is not allowed because there are two `S'
4924 subobjects of U. */
4926 static void
4927 warn_about_ambiguous_bases (tree t)
4929 int i;
4930 VEC(tree,gc) *vbases;
4931 tree basetype;
4932 tree binfo;
4933 tree base_binfo;
4935 /* If there are no repeated bases, nothing can be ambiguous. */
4936 if (!CLASSTYPE_REPEATED_BASE_P (t))
4937 return;
4939 /* Check direct bases. */
4940 for (binfo = TYPE_BINFO (t), i = 0;
4941 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
4943 basetype = BINFO_TYPE (base_binfo);
4945 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
4946 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
4947 basetype, t);
4950 /* Check for ambiguous virtual bases. */
4951 if (extra_warnings)
4952 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
4953 VEC_iterate (tree, vbases, i, binfo); i++)
4955 basetype = BINFO_TYPE (binfo);
4957 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
4958 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due to ambiguity",
4959 basetype, t);
4963 /* Compare two INTEGER_CSTs K1 and K2. */
4965 static int
4966 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
4968 return tree_int_cst_compare ((tree) k1, (tree) k2);
4971 /* Increase the size indicated in RLI to account for empty classes
4972 that are "off the end" of the class. */
4974 static void
4975 include_empty_classes (record_layout_info rli)
4977 tree eoc;
4978 tree rli_size;
4980 /* It might be the case that we grew the class to allocate a
4981 zero-sized base class. That won't be reflected in RLI, yet,
4982 because we are willing to overlay multiple bases at the same
4983 offset. However, now we need to make sure that RLI is big enough
4984 to reflect the entire class. */
4985 eoc = end_of_class (rli->t,
4986 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
4987 rli_size = rli_size_unit_so_far (rli);
4988 if (TREE_CODE (rli_size) == INTEGER_CST
4989 && INT_CST_LT_UNSIGNED (rli_size, eoc))
4991 if (!abi_version_at_least (2))
4992 /* In version 1 of the ABI, the size of a class that ends with
4993 a bitfield was not rounded up to a whole multiple of a
4994 byte. Because rli_size_unit_so_far returns only the number
4995 of fully allocated bytes, any extra bits were not included
4996 in the size. */
4997 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
4998 else
4999 /* The size should have been rounded to a whole byte. */
5000 gcc_assert (tree_int_cst_equal
5001 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5002 rli->bitpos
5003 = size_binop (PLUS_EXPR,
5004 rli->bitpos,
5005 size_binop (MULT_EXPR,
5006 convert (bitsizetype,
5007 size_binop (MINUS_EXPR,
5008 eoc, rli_size)),
5009 bitsize_int (BITS_PER_UNIT)));
5010 normalize_rli (rli);
5014 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5015 BINFO_OFFSETs for all of the base-classes. Position the vtable
5016 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5018 static void
5019 layout_class_type (tree t, tree *virtuals_p)
5021 tree non_static_data_members;
5022 tree field;
5023 tree vptr;
5024 record_layout_info rli;
5025 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5026 types that appear at that offset. */
5027 splay_tree empty_base_offsets;
5028 /* True if the last field layed out was a bit-field. */
5029 bool last_field_was_bitfield = false;
5030 /* The location at which the next field should be inserted. */
5031 tree *next_field;
5032 /* T, as a base class. */
5033 tree base_t;
5035 /* Keep track of the first non-static data member. */
5036 non_static_data_members = TYPE_FIELDS (t);
5038 /* Start laying out the record. */
5039 rli = start_record_layout (t);
5041 /* Mark all the primary bases in the hierarchy. */
5042 determine_primary_bases (t);
5044 /* Create a pointer to our virtual function table. */
5045 vptr = create_vtable_ptr (t, virtuals_p);
5047 /* The vptr is always the first thing in the class. */
5048 if (vptr)
5050 TREE_CHAIN (vptr) = TYPE_FIELDS (t);
5051 TYPE_FIELDS (t) = vptr;
5052 next_field = &TREE_CHAIN (vptr);
5053 place_field (rli, vptr);
5055 else
5056 next_field = &TYPE_FIELDS (t);
5058 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5059 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
5060 NULL, NULL);
5061 build_base_fields (rli, empty_base_offsets, next_field);
5063 /* Layout the non-static data members. */
5064 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
5066 tree type;
5067 tree padding;
5069 /* We still pass things that aren't non-static data members to
5070 the back end, in case it wants to do something with them. */
5071 if (TREE_CODE (field) != FIELD_DECL)
5073 place_field (rli, field);
5074 /* If the static data member has incomplete type, keep track
5075 of it so that it can be completed later. (The handling
5076 of pending statics in finish_record_layout is
5077 insufficient; consider:
5079 struct S1;
5080 struct S2 { static S1 s1; };
5082 At this point, finish_record_layout will be called, but
5083 S1 is still incomplete.) */
5084 if (TREE_CODE (field) == VAR_DECL)
5086 maybe_register_incomplete_var (field);
5087 /* The visibility of static data members is determined
5088 at their point of declaration, not their point of
5089 definition. */
5090 determine_visibility (field);
5092 continue;
5095 type = TREE_TYPE (field);
5096 if (type == error_mark_node)
5097 continue;
5099 padding = NULL_TREE;
5101 /* If this field is a bit-field whose width is greater than its
5102 type, then there are some special rules for allocating
5103 it. */
5104 if (DECL_C_BIT_FIELD (field)
5105 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5107 unsigned int itk;
5108 tree integer_type;
5109 bool was_unnamed_p = false;
5110 /* We must allocate the bits as if suitably aligned for the
5111 longest integer type that fits in this many bits. type
5112 of the field. Then, we are supposed to use the left over
5113 bits as additional padding. */
5114 for (itk = itk_char; itk != itk_none; ++itk)
5115 if (integer_types[itk] != NULL_TREE
5116 && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE),
5117 TYPE_SIZE (integer_types[itk]))
5118 || INT_CST_LT (DECL_SIZE (field),
5119 TYPE_SIZE (integer_types[itk]))))
5120 break;
5122 /* ITK now indicates a type that is too large for the
5123 field. We have to back up by one to find the largest
5124 type that fits. */
5127 --itk;
5128 integer_type = integer_types[itk];
5129 } while (itk > 0 && integer_type == NULL_TREE);
5131 /* Figure out how much additional padding is required. GCC
5132 3.2 always created a padding field, even if it had zero
5133 width. */
5134 if (!abi_version_at_least (2)
5135 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
5137 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
5138 /* In a union, the padding field must have the full width
5139 of the bit-field; all fields start at offset zero. */
5140 padding = DECL_SIZE (field);
5141 else
5143 if (TREE_CODE (t) == UNION_TYPE)
5144 warning (OPT_Wabi, "size assigned to %qT may not be "
5145 "ABI-compliant and may change in a future "
5146 "version of GCC",
5148 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5149 TYPE_SIZE (integer_type));
5152 #ifdef PCC_BITFIELD_TYPE_MATTERS
5153 /* An unnamed bitfield does not normally affect the
5154 alignment of the containing class on a target where
5155 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
5156 make any exceptions for unnamed bitfields when the
5157 bitfields are longer than their types. Therefore, we
5158 temporarily give the field a name. */
5159 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
5161 was_unnamed_p = true;
5162 DECL_NAME (field) = make_anon_name ();
5164 #endif
5165 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5166 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5167 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5168 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5169 empty_base_offsets);
5170 if (was_unnamed_p)
5171 DECL_NAME (field) = NULL_TREE;
5172 /* Now that layout has been performed, set the size of the
5173 field to the size of its declared type; the rest of the
5174 field is effectively invisible. */
5175 DECL_SIZE (field) = TYPE_SIZE (type);
5176 /* We must also reset the DECL_MODE of the field. */
5177 if (abi_version_at_least (2))
5178 DECL_MODE (field) = TYPE_MODE (type);
5179 else if (warn_abi
5180 && DECL_MODE (field) != TYPE_MODE (type))
5181 /* Versions of G++ before G++ 3.4 did not reset the
5182 DECL_MODE. */
5183 warning (OPT_Wabi,
5184 "the offset of %qD may not be ABI-compliant and may "
5185 "change in a future version of GCC", field);
5187 else
5188 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5189 empty_base_offsets);
5191 /* Remember the location of any empty classes in FIELD. */
5192 if (abi_version_at_least (2))
5193 record_subobject_offsets (TREE_TYPE (field),
5194 byte_position(field),
5195 empty_base_offsets,
5196 /*is_data_member=*/true);
5198 /* If a bit-field does not immediately follow another bit-field,
5199 and yet it starts in the middle of a byte, we have failed to
5200 comply with the ABI. */
5201 if (warn_abi
5202 && DECL_C_BIT_FIELD (field)
5203 /* The TREE_NO_WARNING flag gets set by Objective-C when
5204 laying out an Objective-C class. The ObjC ABI differs
5205 from the C++ ABI, and so we do not want a warning
5206 here. */
5207 && !TREE_NO_WARNING (field)
5208 && !last_field_was_bitfield
5209 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
5210 DECL_FIELD_BIT_OFFSET (field),
5211 bitsize_unit_node)))
5212 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
5213 "change in a future version of GCC", field);
5215 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
5216 offset of the field. */
5217 if (warn_abi
5218 && !abi_version_at_least (2)
5219 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
5220 byte_position (field))
5221 && contains_empty_class_p (TREE_TYPE (field)))
5222 warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
5223 "classes to be placed at different locations in a "
5224 "future version of GCC", field);
5226 /* The middle end uses the type of expressions to determine the
5227 possible range of expression values. In order to optimize
5228 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
5229 must be made aware of the width of "i", via its type.
5231 Because C++ does not have integer types of arbitrary width,
5232 we must (for the purposes of the front end) convert from the
5233 type assigned here to the declared type of the bitfield
5234 whenever a bitfield expression is used as an rvalue.
5235 Similarly, when assigning a value to a bitfield, the value
5236 must be converted to the type given the bitfield here. */
5237 if (DECL_C_BIT_FIELD (field))
5239 unsigned HOST_WIDE_INT width;
5240 tree ftype = TREE_TYPE (field);
5241 width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
5242 if (width != TYPE_PRECISION (ftype))
5244 TREE_TYPE (field)
5245 = c_build_bitfield_integer_type (width,
5246 TYPE_UNSIGNED (ftype));
5247 TREE_TYPE (field)
5248 = cp_build_qualified_type (TREE_TYPE (field),
5249 cp_type_quals (ftype));
5253 /* If we needed additional padding after this field, add it
5254 now. */
5255 if (padding)
5257 tree padding_field;
5259 padding_field = build_decl (input_location,
5260 FIELD_DECL,
5261 NULL_TREE,
5262 char_type_node);
5263 DECL_BIT_FIELD (padding_field) = 1;
5264 DECL_SIZE (padding_field) = padding;
5265 DECL_CONTEXT (padding_field) = t;
5266 DECL_ARTIFICIAL (padding_field) = 1;
5267 DECL_IGNORED_P (padding_field) = 1;
5268 layout_nonempty_base_or_field (rli, padding_field,
5269 NULL_TREE,
5270 empty_base_offsets);
5273 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
5276 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
5278 /* Make sure that we are on a byte boundary so that the size of
5279 the class without virtual bases will always be a round number
5280 of bytes. */
5281 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
5282 normalize_rli (rli);
5285 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
5286 padding. */
5287 if (!abi_version_at_least (2))
5288 include_empty_classes(rli);
5290 /* Delete all zero-width bit-fields from the list of fields. Now
5291 that the type is laid out they are no longer important. */
5292 remove_zero_width_bit_fields (t);
5294 /* Create the version of T used for virtual bases. We do not use
5295 make_class_type for this version; this is an artificial type. For
5296 a POD type, we just reuse T. */
5297 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
5299 base_t = make_node (TREE_CODE (t));
5301 /* Set the size and alignment for the new type. In G++ 3.2, all
5302 empty classes were considered to have size zero when used as
5303 base classes. */
5304 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
5306 TYPE_SIZE (base_t) = bitsize_zero_node;
5307 TYPE_SIZE_UNIT (base_t) = size_zero_node;
5308 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
5309 warning (OPT_Wabi,
5310 "layout of classes derived from empty class %qT "
5311 "may change in a future version of GCC",
5314 else
5316 tree eoc;
5318 /* If the ABI version is not at least two, and the last
5319 field was a bit-field, RLI may not be on a byte
5320 boundary. In particular, rli_size_unit_so_far might
5321 indicate the last complete byte, while rli_size_so_far
5322 indicates the total number of bits used. Therefore,
5323 rli_size_so_far, rather than rli_size_unit_so_far, is
5324 used to compute TYPE_SIZE_UNIT. */
5325 eoc = end_of_class (t, /*include_virtuals_p=*/0);
5326 TYPE_SIZE_UNIT (base_t)
5327 = size_binop (MAX_EXPR,
5328 convert (sizetype,
5329 size_binop (CEIL_DIV_EXPR,
5330 rli_size_so_far (rli),
5331 bitsize_int (BITS_PER_UNIT))),
5332 eoc);
5333 TYPE_SIZE (base_t)
5334 = size_binop (MAX_EXPR,
5335 rli_size_so_far (rli),
5336 size_binop (MULT_EXPR,
5337 convert (bitsizetype, eoc),
5338 bitsize_int (BITS_PER_UNIT)));
5340 TYPE_ALIGN (base_t) = rli->record_align;
5341 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
5343 /* Copy the fields from T. */
5344 next_field = &TYPE_FIELDS (base_t);
5345 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5346 if (TREE_CODE (field) == FIELD_DECL)
5348 *next_field = build_decl (input_location,
5349 FIELD_DECL,
5350 DECL_NAME (field),
5351 TREE_TYPE (field));
5352 DECL_CONTEXT (*next_field) = base_t;
5353 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
5354 DECL_FIELD_BIT_OFFSET (*next_field)
5355 = DECL_FIELD_BIT_OFFSET (field);
5356 DECL_SIZE (*next_field) = DECL_SIZE (field);
5357 DECL_MODE (*next_field) = DECL_MODE (field);
5358 next_field = &TREE_CHAIN (*next_field);
5361 /* Record the base version of the type. */
5362 CLASSTYPE_AS_BASE (t) = base_t;
5363 TYPE_CONTEXT (base_t) = t;
5365 else
5366 CLASSTYPE_AS_BASE (t) = t;
5368 /* Every empty class contains an empty class. */
5369 if (CLASSTYPE_EMPTY_P (t))
5370 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
5372 /* Set the TYPE_DECL for this type to contain the right
5373 value for DECL_OFFSET, so that we can use it as part
5374 of a COMPONENT_REF for multiple inheritance. */
5375 layout_decl (TYPE_MAIN_DECL (t), 0);
5377 /* Now fix up any virtual base class types that we left lying
5378 around. We must get these done before we try to lay out the
5379 virtual function table. As a side-effect, this will remove the
5380 base subobject fields. */
5381 layout_virtual_bases (rli, empty_base_offsets);
5383 /* Make sure that empty classes are reflected in RLI at this
5384 point. */
5385 include_empty_classes(rli);
5387 /* Make sure not to create any structures with zero size. */
5388 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
5389 place_field (rli,
5390 build_decl (input_location,
5391 FIELD_DECL, NULL_TREE, char_type_node));
5393 /* If this is a non-POD, declaring it packed makes a difference to how it
5394 can be used as a field; don't let finalize_record_size undo it. */
5395 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
5396 rli->packed_maybe_necessary = true;
5398 /* Let the back end lay out the type. */
5399 finish_record_layout (rli, /*free_p=*/true);
5401 /* Warn about bases that can't be talked about due to ambiguity. */
5402 warn_about_ambiguous_bases (t);
5404 /* Now that we're done with layout, give the base fields the real types. */
5405 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5406 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
5407 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
5409 /* Clean up. */
5410 splay_tree_delete (empty_base_offsets);
5412 if (CLASSTYPE_EMPTY_P (t)
5413 && tree_int_cst_lt (sizeof_biggest_empty_class,
5414 TYPE_SIZE_UNIT (t)))
5415 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
5418 /* Determine the "key method" for the class type indicated by TYPE,
5419 and set CLASSTYPE_KEY_METHOD accordingly. */
5421 void
5422 determine_key_method (tree type)
5424 tree method;
5426 if (TYPE_FOR_JAVA (type)
5427 || processing_template_decl
5428 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
5429 || CLASSTYPE_INTERFACE_KNOWN (type))
5430 return;
5432 /* The key method is the first non-pure virtual function that is not
5433 inline at the point of class definition. On some targets the
5434 key function may not be inline; those targets should not call
5435 this function until the end of the translation unit. */
5436 for (method = TYPE_METHODS (type); method != NULL_TREE;
5437 method = TREE_CHAIN (method))
5438 if (DECL_VINDEX (method) != NULL_TREE
5439 && ! DECL_DECLARED_INLINE_P (method)
5440 && ! DECL_PURE_VIRTUAL_P (method))
5442 CLASSTYPE_KEY_METHOD (type) = method;
5443 break;
5446 return;
5449 /* Perform processing required when the definition of T (a class type)
5450 is complete. */
5452 void
5453 finish_struct_1 (tree t)
5455 tree x;
5456 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
5457 tree virtuals = NULL_TREE;
5458 int n_fields = 0;
5460 if (COMPLETE_TYPE_P (t))
5462 gcc_assert (MAYBE_CLASS_TYPE_P (t));
5463 error ("redefinition of %q#T", t);
5464 popclass ();
5465 return;
5468 /* If this type was previously laid out as a forward reference,
5469 make sure we lay it out again. */
5470 TYPE_SIZE (t) = NULL_TREE;
5471 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5473 /* Make assumptions about the class; we'll reset the flags if
5474 necessary. */
5475 CLASSTYPE_EMPTY_P (t) = 1;
5476 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
5477 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
5479 /* Do end-of-class semantic processing: checking the validity of the
5480 bases and members and add implicitly generated methods. */
5481 check_bases_and_members (t);
5483 /* Find the key method. */
5484 if (TYPE_CONTAINS_VPTR_P (t))
5486 /* The Itanium C++ ABI permits the key method to be chosen when
5487 the class is defined -- even though the key method so
5488 selected may later turn out to be an inline function. On
5489 some systems (such as ARM Symbian OS) the key method cannot
5490 be determined until the end of the translation unit. On such
5491 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
5492 will cause the class to be added to KEYED_CLASSES. Then, in
5493 finish_file we will determine the key method. */
5494 if (targetm.cxx.key_method_may_be_inline ())
5495 determine_key_method (t);
5497 /* If a polymorphic class has no key method, we may emit the vtable
5498 in every translation unit where the class definition appears. */
5499 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
5500 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
5503 /* Layout the class itself. */
5504 layout_class_type (t, &virtuals);
5505 if (CLASSTYPE_AS_BASE (t) != t)
5506 /* We use the base type for trivial assignments, and hence it
5507 needs a mode. */
5508 compute_record_mode (CLASSTYPE_AS_BASE (t));
5510 virtuals = modify_all_vtables (t, nreverse (virtuals));
5512 /* If necessary, create the primary vtable for this class. */
5513 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
5515 /* We must enter these virtuals into the table. */
5516 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5517 build_primary_vtable (NULL_TREE, t);
5518 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
5519 /* Here we know enough to change the type of our virtual
5520 function table, but we will wait until later this function. */
5521 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5524 if (TYPE_CONTAINS_VPTR_P (t))
5526 int vindex;
5527 tree fn;
5529 if (BINFO_VTABLE (TYPE_BINFO (t)))
5530 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
5531 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5532 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
5534 /* Add entries for virtual functions introduced by this class. */
5535 BINFO_VIRTUALS (TYPE_BINFO (t))
5536 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
5538 /* Set DECL_VINDEX for all functions declared in this class. */
5539 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
5541 fn = TREE_CHAIN (fn),
5542 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
5543 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
5545 tree fndecl = BV_FN (fn);
5547 if (DECL_THUNK_P (fndecl))
5548 /* A thunk. We should never be calling this entry directly
5549 from this vtable -- we'd use the entry for the non
5550 thunk base function. */
5551 DECL_VINDEX (fndecl) = NULL_TREE;
5552 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
5553 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
5557 finish_struct_bits (t);
5559 /* Complete the rtl for any static member objects of the type we're
5560 working on. */
5561 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5562 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5563 && TREE_TYPE (x) != error_mark_node
5564 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
5565 DECL_MODE (x) = TYPE_MODE (t);
5567 /* Done with FIELDS...now decide whether to sort these for
5568 faster lookups later.
5570 We use a small number because most searches fail (succeeding
5571 ultimately as the search bores through the inheritance
5572 hierarchy), and we want this failure to occur quickly. */
5574 n_fields = count_fields (TYPE_FIELDS (t));
5575 if (n_fields > 7)
5577 struct sorted_fields_type *field_vec = ggc_alloc_sorted_fields_type
5578 (sizeof (struct sorted_fields_type) + n_fields * sizeof (tree));
5579 field_vec->len = n_fields;
5580 add_fields_to_record_type (TYPE_FIELDS (t), field_vec, 0);
5581 qsort (field_vec->elts, n_fields, sizeof (tree),
5582 field_decl_cmp);
5583 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
5586 /* Complain if one of the field types requires lower visibility. */
5587 constrain_class_visibility (t);
5589 /* Make the rtl for any new vtables we have created, and unmark
5590 the base types we marked. */
5591 finish_vtbls (t);
5593 /* Build the VTT for T. */
5594 build_vtt (t);
5596 /* This warning does not make sense for Java classes, since they
5597 cannot have destructors. */
5598 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
5600 tree dtor;
5602 dtor = CLASSTYPE_DESTRUCTORS (t);
5603 if (/* An implicitly declared destructor is always public. And,
5604 if it were virtual, we would have created it by now. */
5605 !dtor
5606 || (!DECL_VINDEX (dtor)
5607 && (/* public non-virtual */
5608 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
5609 || (/* non-public non-virtual with friends */
5610 (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
5611 && (CLASSTYPE_FRIEND_CLASSES (t)
5612 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
5613 warning (OPT_Wnon_virtual_dtor,
5614 "%q#T has virtual functions and accessible"
5615 " non-virtual destructor", t);
5618 complete_vars (t);
5620 if (warn_overloaded_virtual)
5621 warn_hidden (t);
5623 /* Class layout, assignment of virtual table slots, etc., is now
5624 complete. Give the back end a chance to tweak the visibility of
5625 the class or perform any other required target modifications. */
5626 targetm.cxx.adjust_class_at_definition (t);
5628 maybe_suppress_debug_info (t);
5630 dump_class_hierarchy (t);
5632 /* Finish debugging output for this type. */
5633 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5636 /* When T was built up, the member declarations were added in reverse
5637 order. Rearrange them to declaration order. */
5639 void
5640 unreverse_member_declarations (tree t)
5642 tree next;
5643 tree prev;
5644 tree x;
5646 /* The following lists are all in reverse order. Put them in
5647 declaration order now. */
5648 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5649 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
5651 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5652 reverse order, so we can't just use nreverse. */
5653 prev = NULL_TREE;
5654 for (x = TYPE_FIELDS (t);
5655 x && TREE_CODE (x) != TYPE_DECL;
5656 x = next)
5658 next = TREE_CHAIN (x);
5659 TREE_CHAIN (x) = prev;
5660 prev = x;
5662 if (prev)
5664 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5665 if (prev)
5666 TYPE_FIELDS (t) = prev;
5670 tree
5671 finish_struct (tree t, tree attributes)
5673 location_t saved_loc = input_location;
5675 /* Now that we've got all the field declarations, reverse everything
5676 as necessary. */
5677 unreverse_member_declarations (t);
5679 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5681 /* Nadger the current location so that diagnostics point to the start of
5682 the struct, not the end. */
5683 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
5685 if (processing_template_decl)
5687 tree x;
5689 finish_struct_methods (t);
5690 TYPE_SIZE (t) = bitsize_zero_node;
5691 TYPE_SIZE_UNIT (t) = size_zero_node;
5693 /* We need to emit an error message if this type was used as a parameter
5694 and it is an abstract type, even if it is a template. We construct
5695 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
5696 account and we call complete_vars with this type, which will check
5697 the PARM_DECLS. Note that while the type is being defined,
5698 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
5699 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
5700 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
5701 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
5702 if (DECL_PURE_VIRTUAL_P (x))
5703 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
5704 complete_vars (t);
5706 /* Remember current #pragma pack value. */
5707 TYPE_PRECISION (t) = maximum_field_alignment;
5709 else
5710 finish_struct_1 (t);
5712 input_location = saved_loc;
5714 TYPE_BEING_DEFINED (t) = 0;
5716 if (current_class_type)
5717 popclass ();
5718 else
5719 error ("trying to finish struct, but kicked out due to previous parse errors");
5721 if (processing_template_decl && at_function_scope_p ())
5722 add_stmt (build_min (TAG_DEFN, t));
5724 return t;
5727 /* Return the dynamic type of INSTANCE, if known.
5728 Used to determine whether the virtual function table is needed
5729 or not.
5731 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5732 of our knowledge of its type. *NONNULL should be initialized
5733 before this function is called. */
5735 static tree
5736 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
5738 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
5740 switch (TREE_CODE (instance))
5742 case INDIRECT_REF:
5743 if (POINTER_TYPE_P (TREE_TYPE (instance)))
5744 return NULL_TREE;
5745 else
5746 return RECUR (TREE_OPERAND (instance, 0));
5748 case CALL_EXPR:
5749 /* This is a call to a constructor, hence it's never zero. */
5750 if (TREE_HAS_CONSTRUCTOR (instance))
5752 if (nonnull)
5753 *nonnull = 1;
5754 return TREE_TYPE (instance);
5756 return NULL_TREE;
5758 case SAVE_EXPR:
5759 /* This is a call to a constructor, hence it's never zero. */
5760 if (TREE_HAS_CONSTRUCTOR (instance))
5762 if (nonnull)
5763 *nonnull = 1;
5764 return TREE_TYPE (instance);
5766 return RECUR (TREE_OPERAND (instance, 0));
5768 case POINTER_PLUS_EXPR:
5769 case PLUS_EXPR:
5770 case MINUS_EXPR:
5771 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5772 return RECUR (TREE_OPERAND (instance, 0));
5773 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5774 /* Propagate nonnull. */
5775 return RECUR (TREE_OPERAND (instance, 0));
5777 return NULL_TREE;
5779 CASE_CONVERT:
5780 return RECUR (TREE_OPERAND (instance, 0));
5782 case ADDR_EXPR:
5783 instance = TREE_OPERAND (instance, 0);
5784 if (nonnull)
5786 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
5787 with a real object -- given &p->f, p can still be null. */
5788 tree t = get_base_address (instance);
5789 /* ??? Probably should check DECL_WEAK here. */
5790 if (t && DECL_P (t))
5791 *nonnull = 1;
5793 return RECUR (instance);
5795 case COMPONENT_REF:
5796 /* If this component is really a base class reference, then the field
5797 itself isn't definitive. */
5798 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
5799 return RECUR (TREE_OPERAND (instance, 0));
5800 return RECUR (TREE_OPERAND (instance, 1));
5802 case VAR_DECL:
5803 case FIELD_DECL:
5804 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5805 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
5807 if (nonnull)
5808 *nonnull = 1;
5809 return TREE_TYPE (TREE_TYPE (instance));
5811 /* fall through... */
5812 case TARGET_EXPR:
5813 case PARM_DECL:
5814 case RESULT_DECL:
5815 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
5817 if (nonnull)
5818 *nonnull = 1;
5819 return TREE_TYPE (instance);
5821 else if (instance == current_class_ptr)
5823 if (nonnull)
5824 *nonnull = 1;
5826 /* if we're in a ctor or dtor, we know our type. */
5827 if (DECL_LANG_SPECIFIC (current_function_decl)
5828 && (DECL_CONSTRUCTOR_P (current_function_decl)
5829 || DECL_DESTRUCTOR_P (current_function_decl)))
5831 if (cdtorp)
5832 *cdtorp = 1;
5833 return TREE_TYPE (TREE_TYPE (instance));
5836 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5838 /* We only need one hash table because it is always left empty. */
5839 static htab_t ht;
5840 if (!ht)
5841 ht = htab_create (37,
5842 htab_hash_pointer,
5843 htab_eq_pointer,
5844 /*htab_del=*/NULL);
5846 /* Reference variables should be references to objects. */
5847 if (nonnull)
5848 *nonnull = 1;
5850 /* Enter the INSTANCE in a table to prevent recursion; a
5851 variable's initializer may refer to the variable
5852 itself. */
5853 if (TREE_CODE (instance) == VAR_DECL
5854 && DECL_INITIAL (instance)
5855 && !htab_find (ht, instance))
5857 tree type;
5858 void **slot;
5860 slot = htab_find_slot (ht, instance, INSERT);
5861 *slot = instance;
5862 type = RECUR (DECL_INITIAL (instance));
5863 htab_remove_elt (ht, instance);
5865 return type;
5868 return NULL_TREE;
5870 default:
5871 return NULL_TREE;
5873 #undef RECUR
5876 /* Return nonzero if the dynamic type of INSTANCE is known, and
5877 equivalent to the static type. We also handle the case where
5878 INSTANCE is really a pointer. Return negative if this is a
5879 ctor/dtor. There the dynamic type is known, but this might not be
5880 the most derived base of the original object, and hence virtual
5881 bases may not be layed out according to this type.
5883 Used to determine whether the virtual function table is needed
5884 or not.
5886 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5887 of our knowledge of its type. *NONNULL should be initialized
5888 before this function is called. */
5891 resolves_to_fixed_type_p (tree instance, int* nonnull)
5893 tree t = TREE_TYPE (instance);
5894 int cdtorp = 0;
5895 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5896 if (fixed == NULL_TREE)
5897 return 0;
5898 if (POINTER_TYPE_P (t))
5899 t = TREE_TYPE (t);
5900 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5901 return 0;
5902 return cdtorp ? -1 : 1;
5906 void
5907 init_class_processing (void)
5909 current_class_depth = 0;
5910 current_class_stack_size = 10;
5911 current_class_stack
5912 = XNEWVEC (struct class_stack_node, current_class_stack_size);
5913 local_classes = VEC_alloc (tree, gc, 8);
5914 sizeof_biggest_empty_class = size_zero_node;
5916 ridpointers[(int) RID_PUBLIC] = access_public_node;
5917 ridpointers[(int) RID_PRIVATE] = access_private_node;
5918 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5921 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
5923 static void
5924 restore_class_cache (void)
5926 tree type;
5928 /* We are re-entering the same class we just left, so we don't
5929 have to search the whole inheritance matrix to find all the
5930 decls to bind again. Instead, we install the cached
5931 class_shadowed list and walk through it binding names. */
5932 push_binding_level (previous_class_level);
5933 class_binding_level = previous_class_level;
5934 /* Restore IDENTIFIER_TYPE_VALUE. */
5935 for (type = class_binding_level->type_shadowed;
5936 type;
5937 type = TREE_CHAIN (type))
5938 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
5941 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
5942 appropriate for TYPE.
5944 So that we may avoid calls to lookup_name, we cache the _TYPE
5945 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5947 For multiple inheritance, we perform a two-pass depth-first search
5948 of the type lattice. */
5950 void
5951 pushclass (tree type)
5953 class_stack_node_t csn;
5955 type = TYPE_MAIN_VARIANT (type);
5957 /* Make sure there is enough room for the new entry on the stack. */
5958 if (current_class_depth + 1 >= current_class_stack_size)
5960 current_class_stack_size *= 2;
5961 current_class_stack
5962 = XRESIZEVEC (struct class_stack_node, current_class_stack,
5963 current_class_stack_size);
5966 /* Insert a new entry on the class stack. */
5967 csn = current_class_stack + current_class_depth;
5968 csn->name = current_class_name;
5969 csn->type = current_class_type;
5970 csn->access = current_access_specifier;
5971 csn->names_used = 0;
5972 csn->hidden = 0;
5973 current_class_depth++;
5975 /* Now set up the new type. */
5976 current_class_name = TYPE_NAME (type);
5977 if (TREE_CODE (current_class_name) == TYPE_DECL)
5978 current_class_name = DECL_NAME (current_class_name);
5979 current_class_type = type;
5981 /* By default, things in classes are private, while things in
5982 structures or unions are public. */
5983 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5984 ? access_private_node
5985 : access_public_node);
5987 if (previous_class_level
5988 && type != previous_class_level->this_entity
5989 && current_class_depth == 1)
5991 /* Forcibly remove any old class remnants. */
5992 invalidate_class_lookup_cache ();
5995 if (!previous_class_level
5996 || type != previous_class_level->this_entity
5997 || current_class_depth > 1)
5998 pushlevel_class ();
5999 else
6000 restore_class_cache ();
6003 /* When we exit a toplevel class scope, we save its binding level so
6004 that we can restore it quickly. Here, we've entered some other
6005 class, so we must invalidate our cache. */
6007 void
6008 invalidate_class_lookup_cache (void)
6010 previous_class_level = NULL;
6013 /* Get out of the current class scope. If we were in a class scope
6014 previously, that is the one popped to. */
6016 void
6017 popclass (void)
6019 poplevel_class ();
6021 current_class_depth--;
6022 current_class_name = current_class_stack[current_class_depth].name;
6023 current_class_type = current_class_stack[current_class_depth].type;
6024 current_access_specifier = current_class_stack[current_class_depth].access;
6025 if (current_class_stack[current_class_depth].names_used)
6026 splay_tree_delete (current_class_stack[current_class_depth].names_used);
6029 /* Mark the top of the class stack as hidden. */
6031 void
6032 push_class_stack (void)
6034 if (current_class_depth)
6035 ++current_class_stack[current_class_depth - 1].hidden;
6038 /* Mark the top of the class stack as un-hidden. */
6040 void
6041 pop_class_stack (void)
6043 if (current_class_depth)
6044 --current_class_stack[current_class_depth - 1].hidden;
6047 /* Returns 1 if the class type currently being defined is either T or
6048 a nested type of T. */
6050 bool
6051 currently_open_class (tree t)
6053 int i;
6055 if (!CLASS_TYPE_P (t))
6056 return false;
6058 t = TYPE_MAIN_VARIANT (t);
6060 /* We start looking from 1 because entry 0 is from global scope,
6061 and has no type. */
6062 for (i = current_class_depth; i > 0; --i)
6064 tree c;
6065 if (i == current_class_depth)
6066 c = current_class_type;
6067 else
6069 if (current_class_stack[i].hidden)
6070 break;
6071 c = current_class_stack[i].type;
6073 if (!c)
6074 continue;
6075 if (same_type_p (c, t))
6076 return true;
6078 return false;
6081 /* If either current_class_type or one of its enclosing classes are derived
6082 from T, return the appropriate type. Used to determine how we found
6083 something via unqualified lookup. */
6085 tree
6086 currently_open_derived_class (tree t)
6088 int i;
6090 /* The bases of a dependent type are unknown. */
6091 if (dependent_type_p (t))
6092 return NULL_TREE;
6094 if (!current_class_type)
6095 return NULL_TREE;
6097 if (DERIVED_FROM_P (t, current_class_type))
6098 return current_class_type;
6100 for (i = current_class_depth - 1; i > 0; --i)
6102 if (current_class_stack[i].hidden)
6103 break;
6104 if (DERIVED_FROM_P (t, current_class_stack[i].type))
6105 return current_class_stack[i].type;
6108 return NULL_TREE;
6111 /* Returns the innermost class type which is not a lambda closure type. */
6113 tree
6114 current_nonlambda_class_type (void)
6116 int i;
6118 /* We start looking from 1 because entry 0 is from global scope,
6119 and has no type. */
6120 for (i = current_class_depth; i > 0; --i)
6122 tree c;
6123 if (i == current_class_depth)
6124 c = current_class_type;
6125 else
6127 if (current_class_stack[i].hidden)
6128 break;
6129 c = current_class_stack[i].type;
6131 if (!c)
6132 continue;
6133 if (!LAMBDA_TYPE_P (c))
6134 return c;
6136 return NULL_TREE;
6139 /* When entering a class scope, all enclosing class scopes' names with
6140 static meaning (static variables, static functions, types and
6141 enumerators) have to be visible. This recursive function calls
6142 pushclass for all enclosing class contexts until global or a local
6143 scope is reached. TYPE is the enclosed class. */
6145 void
6146 push_nested_class (tree type)
6148 /* A namespace might be passed in error cases, like A::B:C. */
6149 if (type == NULL_TREE
6150 || !CLASS_TYPE_P (type))
6151 return;
6153 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
6155 pushclass (type);
6158 /* Undoes a push_nested_class call. */
6160 void
6161 pop_nested_class (void)
6163 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
6165 popclass ();
6166 if (context && CLASS_TYPE_P (context))
6167 pop_nested_class ();
6170 /* Returns the number of extern "LANG" blocks we are nested within. */
6173 current_lang_depth (void)
6175 return VEC_length (tree, current_lang_base);
6178 /* Set global variables CURRENT_LANG_NAME to appropriate value
6179 so that behavior of name-mangling machinery is correct. */
6181 void
6182 push_lang_context (tree name)
6184 VEC_safe_push (tree, gc, current_lang_base, current_lang_name);
6186 if (name == lang_name_cplusplus)
6188 current_lang_name = name;
6190 else if (name == lang_name_java)
6192 current_lang_name = name;
6193 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
6194 (See record_builtin_java_type in decl.c.) However, that causes
6195 incorrect debug entries if these types are actually used.
6196 So we re-enable debug output after extern "Java". */
6197 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
6198 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
6199 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
6200 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
6201 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
6202 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
6203 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
6204 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
6206 else if (name == lang_name_c)
6208 current_lang_name = name;
6210 else
6211 error ("language string %<\"%E\"%> not recognized", name);
6214 /* Get out of the current language scope. */
6216 void
6217 pop_lang_context (void)
6219 current_lang_name = VEC_pop (tree, current_lang_base);
6222 /* Type instantiation routines. */
6224 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
6225 matches the TARGET_TYPE. If there is no satisfactory match, return
6226 error_mark_node, and issue an error & warning messages under
6227 control of FLAGS. Permit pointers to member function if FLAGS
6228 permits. If TEMPLATE_ONLY, the name of the overloaded function was
6229 a template-id, and EXPLICIT_TARGS are the explicitly provided
6230 template arguments.
6232 If OVERLOAD is for one or more member functions, then ACCESS_PATH
6233 is the base path used to reference those member functions. If
6234 TF_NO_ACCESS_CONTROL is not set in FLAGS, and the address is
6235 resolved to a member function, access checks will be performed and
6236 errors issued if appropriate. */
6238 static tree
6239 resolve_address_of_overloaded_function (tree target_type,
6240 tree overload,
6241 tsubst_flags_t flags,
6242 bool template_only,
6243 tree explicit_targs,
6244 tree access_path)
6246 /* Here's what the standard says:
6248 [over.over]
6250 If the name is a function template, template argument deduction
6251 is done, and if the argument deduction succeeds, the deduced
6252 arguments are used to generate a single template function, which
6253 is added to the set of overloaded functions considered.
6255 Non-member functions and static member functions match targets of
6256 type "pointer-to-function" or "reference-to-function." Nonstatic
6257 member functions match targets of type "pointer-to-member
6258 function;" the function type of the pointer to member is used to
6259 select the member function from the set of overloaded member
6260 functions. If a nonstatic member function is selected, the
6261 reference to the overloaded function name is required to have the
6262 form of a pointer to member as described in 5.3.1.
6264 If more than one function is selected, any template functions in
6265 the set are eliminated if the set also contains a non-template
6266 function, and any given template function is eliminated if the
6267 set contains a second template function that is more specialized
6268 than the first according to the partial ordering rules 14.5.5.2.
6269 After such eliminations, if any, there shall remain exactly one
6270 selected function. */
6272 int is_ptrmem = 0;
6273 /* We store the matches in a TREE_LIST rooted here. The functions
6274 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
6275 interoperability with most_specialized_instantiation. */
6276 tree matches = NULL_TREE;
6277 tree fn;
6278 tree target_fn_type;
6280 /* By the time we get here, we should be seeing only real
6281 pointer-to-member types, not the internal POINTER_TYPE to
6282 METHOD_TYPE representation. */
6283 gcc_assert (TREE_CODE (target_type) != POINTER_TYPE
6284 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
6286 gcc_assert (is_overloaded_fn (overload));
6288 /* Check that the TARGET_TYPE is reasonable. */
6289 if (TYPE_PTRFN_P (target_type))
6290 /* This is OK. */;
6291 else if (TYPE_PTRMEMFUNC_P (target_type))
6292 /* This is OK, too. */
6293 is_ptrmem = 1;
6294 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
6295 /* This is OK, too. This comes from a conversion to reference
6296 type. */
6297 target_type = build_reference_type (target_type);
6298 else
6300 if (flags & tf_error)
6301 error ("cannot resolve overloaded function %qD based on"
6302 " conversion to type %qT",
6303 DECL_NAME (OVL_FUNCTION (overload)), target_type);
6304 return error_mark_node;
6307 /* Non-member functions and static member functions match targets of type
6308 "pointer-to-function" or "reference-to-function." Nonstatic member
6309 functions match targets of type "pointer-to-member-function;" the
6310 function type of the pointer to member is used to select the member
6311 function from the set of overloaded member functions.
6313 So figure out the FUNCTION_TYPE that we want to match against. */
6314 target_fn_type = static_fn_type (target_type);
6316 /* If we can find a non-template function that matches, we can just
6317 use it. There's no point in generating template instantiations
6318 if we're just going to throw them out anyhow. But, of course, we
6319 can only do this when we don't *need* a template function. */
6320 if (!template_only)
6322 tree fns;
6324 for (fns = overload; fns; fns = OVL_NEXT (fns))
6326 tree fn = OVL_CURRENT (fns);
6328 if (TREE_CODE (fn) == TEMPLATE_DECL)
6329 /* We're not looking for templates just yet. */
6330 continue;
6332 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6333 != is_ptrmem)
6334 /* We're looking for a non-static member, and this isn't
6335 one, or vice versa. */
6336 continue;
6338 /* Ignore functions which haven't been explicitly
6339 declared. */
6340 if (DECL_ANTICIPATED (fn))
6341 continue;
6343 /* See if there's a match. */
6344 if (same_type_p (target_fn_type, static_fn_type (fn)))
6345 matches = tree_cons (fn, NULL_TREE, matches);
6349 /* Now, if we've already got a match (or matches), there's no need
6350 to proceed to the template functions. But, if we don't have a
6351 match we need to look at them, too. */
6352 if (!matches)
6354 tree target_arg_types;
6355 tree target_ret_type;
6356 tree fns;
6357 tree *args;
6358 unsigned int nargs, ia;
6359 tree arg;
6361 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
6362 target_ret_type = TREE_TYPE (target_fn_type);
6364 nargs = list_length (target_arg_types);
6365 args = XALLOCAVEC (tree, nargs);
6366 for (arg = target_arg_types, ia = 0;
6367 arg != NULL_TREE && arg != void_list_node;
6368 arg = TREE_CHAIN (arg), ++ia)
6369 args[ia] = TREE_VALUE (arg);
6370 nargs = ia;
6372 for (fns = overload; fns; fns = OVL_NEXT (fns))
6374 tree fn = OVL_CURRENT (fns);
6375 tree instantiation;
6376 tree targs;
6378 if (TREE_CODE (fn) != TEMPLATE_DECL)
6379 /* We're only looking for templates. */
6380 continue;
6382 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6383 != is_ptrmem)
6384 /* We're not looking for a non-static member, and this is
6385 one, or vice versa. */
6386 continue;
6388 /* Try to do argument deduction. */
6389 targs = make_tree_vec (DECL_NTPARMS (fn));
6390 if (fn_type_unification (fn, explicit_targs, targs, args, nargs,
6391 target_ret_type, DEDUCE_EXACT,
6392 LOOKUP_NORMAL))
6393 /* Argument deduction failed. */
6394 continue;
6396 /* Instantiate the template. */
6397 instantiation = instantiate_template (fn, targs, flags);
6398 if (instantiation == error_mark_node)
6399 /* Instantiation failed. */
6400 continue;
6402 /* See if there's a match. */
6403 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
6404 matches = tree_cons (instantiation, fn, matches);
6407 /* Now, remove all but the most specialized of the matches. */
6408 if (matches)
6410 tree match = most_specialized_instantiation (matches);
6412 if (match != error_mark_node)
6413 matches = tree_cons (TREE_PURPOSE (match),
6414 NULL_TREE,
6415 NULL_TREE);
6419 /* Now we should have exactly one function in MATCHES. */
6420 if (matches == NULL_TREE)
6422 /* There were *no* matches. */
6423 if (flags & tf_error)
6425 error ("no matches converting function %qD to type %q#T",
6426 DECL_NAME (OVL_CURRENT (overload)),
6427 target_type);
6429 /* print_candidates expects a chain with the functions in
6430 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6431 so why be clever?). */
6432 for (; overload; overload = OVL_NEXT (overload))
6433 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6434 matches);
6436 print_candidates (matches);
6438 return error_mark_node;
6440 else if (TREE_CHAIN (matches))
6442 /* There were too many matches. First check if they're all
6443 the same function. */
6444 tree match;
6446 fn = TREE_PURPOSE (matches);
6447 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
6448 if (!decls_match (fn, TREE_PURPOSE (match)))
6449 break;
6451 if (match)
6453 if (flags & tf_error)
6455 error ("converting overloaded function %qD to type %q#T is ambiguous",
6456 DECL_NAME (OVL_FUNCTION (overload)),
6457 target_type);
6459 /* Since print_candidates expects the functions in the
6460 TREE_VALUE slot, we flip them here. */
6461 for (match = matches; match; match = TREE_CHAIN (match))
6462 TREE_VALUE (match) = TREE_PURPOSE (match);
6464 print_candidates (matches);
6467 return error_mark_node;
6471 /* Good, exactly one match. Now, convert it to the correct type. */
6472 fn = TREE_PURPOSE (matches);
6474 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6475 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
6477 static int explained;
6479 if (!(flags & tf_error))
6480 return error_mark_node;
6482 permerror (input_location, "assuming pointer to member %qD", fn);
6483 if (!explained)
6485 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
6486 explained = 1;
6490 /* If we're doing overload resolution purely for the purpose of
6491 determining conversion sequences, we should not consider the
6492 function used. If this conversion sequence is selected, the
6493 function will be marked as used at this point. */
6494 if (!(flags & tf_conv))
6496 /* Make =delete work with SFINAE. */
6497 if (DECL_DELETED_FN (fn) && !(flags & tf_error))
6498 return error_mark_node;
6500 mark_used (fn);
6503 /* We could not check access to member functions when this
6504 expression was originally created since we did not know at that
6505 time to which function the expression referred. */
6506 if (!(flags & tf_no_access_control)
6507 && DECL_FUNCTION_MEMBER_P (fn))
6509 gcc_assert (access_path);
6510 perform_or_defer_access_check (access_path, fn, fn);
6513 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6514 return cp_build_unary_op (ADDR_EXPR, fn, 0, flags);
6515 else
6517 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
6518 will mark the function as addressed, but here we must do it
6519 explicitly. */
6520 cxx_mark_addressable (fn);
6522 return fn;
6526 /* This function will instantiate the type of the expression given in
6527 RHS to match the type of LHSTYPE. If errors exist, then return
6528 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
6529 we complain on errors. If we are not complaining, never modify rhs,
6530 as overload resolution wants to try many possible instantiations, in
6531 the hope that at least one will work.
6533 For non-recursive calls, LHSTYPE should be a function, pointer to
6534 function, or a pointer to member function. */
6536 tree
6537 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
6539 tsubst_flags_t flags_in = flags;
6540 tree access_path = NULL_TREE;
6542 flags &= ~tf_ptrmem_ok;
6544 if (lhstype == unknown_type_node)
6546 if (flags & tf_error)
6547 error ("not enough type information");
6548 return error_mark_node;
6551 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6553 if (same_type_p (lhstype, TREE_TYPE (rhs)))
6554 return rhs;
6555 if (flag_ms_extensions
6556 && TYPE_PTRMEMFUNC_P (lhstype)
6557 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6558 /* Microsoft allows `A::f' to be resolved to a
6559 pointer-to-member. */
6561 else
6563 if (flags & tf_error)
6564 error ("argument of type %qT does not match %qT",
6565 TREE_TYPE (rhs), lhstype);
6566 return error_mark_node;
6570 if (TREE_CODE (rhs) == BASELINK)
6572 access_path = BASELINK_ACCESS_BINFO (rhs);
6573 rhs = BASELINK_FUNCTIONS (rhs);
6576 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
6577 deduce any type information. */
6578 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
6580 if (flags & tf_error)
6581 error ("not enough type information");
6582 return error_mark_node;
6585 /* There only a few kinds of expressions that may have a type
6586 dependent on overload resolution. */
6587 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
6588 || TREE_CODE (rhs) == COMPONENT_REF
6589 || really_overloaded_fn (rhs)
6590 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
6592 /* This should really only be used when attempting to distinguish
6593 what sort of a pointer to function we have. For now, any
6594 arithmetic operation which is not supported on pointers
6595 is rejected as an error. */
6597 switch (TREE_CODE (rhs))
6599 case COMPONENT_REF:
6601 tree member = TREE_OPERAND (rhs, 1);
6603 member = instantiate_type (lhstype, member, flags);
6604 if (member != error_mark_node
6605 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
6606 /* Do not lose object's side effects. */
6607 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
6608 TREE_OPERAND (rhs, 0), member);
6609 return member;
6612 case OFFSET_REF:
6613 rhs = TREE_OPERAND (rhs, 1);
6614 if (BASELINK_P (rhs))
6615 return instantiate_type (lhstype, rhs, flags_in);
6617 /* This can happen if we are forming a pointer-to-member for a
6618 member template. */
6619 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
6621 /* Fall through. */
6623 case TEMPLATE_ID_EXPR:
6625 tree fns = TREE_OPERAND (rhs, 0);
6626 tree args = TREE_OPERAND (rhs, 1);
6628 return
6629 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
6630 /*template_only=*/true,
6631 args, access_path);
6634 case OVERLOAD:
6635 case FUNCTION_DECL:
6636 return
6637 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
6638 /*template_only=*/false,
6639 /*explicit_targs=*/NULL_TREE,
6640 access_path);
6642 case ADDR_EXPR:
6644 if (PTRMEM_OK_P (rhs))
6645 flags |= tf_ptrmem_ok;
6647 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6650 case ERROR_MARK:
6651 return error_mark_node;
6653 default:
6654 gcc_unreachable ();
6656 return error_mark_node;
6659 /* Return the name of the virtual function pointer field
6660 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6661 this may have to look back through base types to find the
6662 ultimate field name. (For single inheritance, these could
6663 all be the same name. Who knows for multiple inheritance). */
6665 static tree
6666 get_vfield_name (tree type)
6668 tree binfo, base_binfo;
6669 char *buf;
6671 for (binfo = TYPE_BINFO (type);
6672 BINFO_N_BASE_BINFOS (binfo);
6673 binfo = base_binfo)
6675 base_binfo = BINFO_BASE_BINFO (binfo, 0);
6677 if (BINFO_VIRTUAL_P (base_binfo)
6678 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
6679 break;
6682 type = BINFO_TYPE (binfo);
6683 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6684 + TYPE_NAME_LENGTH (type) + 2);
6685 sprintf (buf, VFIELD_NAME_FORMAT,
6686 IDENTIFIER_POINTER (constructor_name (type)));
6687 return get_identifier (buf);
6690 void
6691 print_class_statistics (void)
6693 #ifdef GATHER_STATISTICS
6694 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6695 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6696 if (n_vtables)
6698 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6699 n_vtables, n_vtable_searches);
6700 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6701 n_vtable_entries, n_vtable_elems);
6703 #endif
6706 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6707 according to [class]:
6708 The class-name is also inserted
6709 into the scope of the class itself. For purposes of access checking,
6710 the inserted class name is treated as if it were a public member name. */
6712 void
6713 build_self_reference (void)
6715 tree name = constructor_name (current_class_type);
6716 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6717 tree saved_cas;
6719 DECL_NONLOCAL (value) = 1;
6720 DECL_CONTEXT (value) = current_class_type;
6721 DECL_ARTIFICIAL (value) = 1;
6722 SET_DECL_SELF_REFERENCE_P (value);
6723 cp_set_underlying_type (value);
6725 if (processing_template_decl)
6726 value = push_template_decl (value);
6728 saved_cas = current_access_specifier;
6729 current_access_specifier = access_public_node;
6730 finish_member_declaration (value);
6731 current_access_specifier = saved_cas;
6734 /* Returns 1 if TYPE contains only padding bytes. */
6737 is_empty_class (tree type)
6739 if (type == error_mark_node)
6740 return 0;
6742 if (! CLASS_TYPE_P (type))
6743 return 0;
6745 /* In G++ 3.2, whether or not a class was empty was determined by
6746 looking at its size. */
6747 if (abi_version_at_least (2))
6748 return CLASSTYPE_EMPTY_P (type);
6749 else
6750 return integer_zerop (CLASSTYPE_SIZE (type));
6753 /* Returns true if TYPE contains an empty class. */
6755 static bool
6756 contains_empty_class_p (tree type)
6758 if (is_empty_class (type))
6759 return true;
6760 if (CLASS_TYPE_P (type))
6762 tree field;
6763 tree binfo;
6764 tree base_binfo;
6765 int i;
6767 for (binfo = TYPE_BINFO (type), i = 0;
6768 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6769 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
6770 return true;
6771 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6772 if (TREE_CODE (field) == FIELD_DECL
6773 && !DECL_ARTIFICIAL (field)
6774 && is_empty_class (TREE_TYPE (field)))
6775 return true;
6777 else if (TREE_CODE (type) == ARRAY_TYPE)
6778 return contains_empty_class_p (TREE_TYPE (type));
6779 return false;
6782 /* Returns true if TYPE contains no actual data, just various
6783 possible combinations of empty classes. */
6785 bool
6786 is_really_empty_class (tree type)
6788 if (is_empty_class (type))
6789 return true;
6790 if (CLASS_TYPE_P (type))
6792 tree field;
6793 tree binfo;
6794 tree base_binfo;
6795 int i;
6797 for (binfo = TYPE_BINFO (type), i = 0;
6798 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6799 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
6800 return false;
6801 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6802 if (TREE_CODE (field) == FIELD_DECL
6803 && !DECL_ARTIFICIAL (field)
6804 && !is_really_empty_class (TREE_TYPE (field)))
6805 return false;
6806 return true;
6808 else if (TREE_CODE (type) == ARRAY_TYPE)
6809 return is_really_empty_class (TREE_TYPE (type));
6810 return false;
6813 /* Note that NAME was looked up while the current class was being
6814 defined and that the result of that lookup was DECL. */
6816 void
6817 maybe_note_name_used_in_class (tree name, tree decl)
6819 splay_tree names_used;
6821 /* If we're not defining a class, there's nothing to do. */
6822 if (!(innermost_scope_kind() == sk_class
6823 && TYPE_BEING_DEFINED (current_class_type)
6824 && !LAMBDA_TYPE_P (current_class_type)))
6825 return;
6827 /* If there's already a binding for this NAME, then we don't have
6828 anything to worry about. */
6829 if (lookup_member (current_class_type, name,
6830 /*protect=*/0, /*want_type=*/false))
6831 return;
6833 if (!current_class_stack[current_class_depth - 1].names_used)
6834 current_class_stack[current_class_depth - 1].names_used
6835 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6836 names_used = current_class_stack[current_class_depth - 1].names_used;
6838 splay_tree_insert (names_used,
6839 (splay_tree_key) name,
6840 (splay_tree_value) decl);
6843 /* Note that NAME was declared (as DECL) in the current class. Check
6844 to see that the declaration is valid. */
6846 void
6847 note_name_declared_in_class (tree name, tree decl)
6849 splay_tree names_used;
6850 splay_tree_node n;
6852 /* Look to see if we ever used this name. */
6853 names_used
6854 = current_class_stack[current_class_depth - 1].names_used;
6855 if (!names_used)
6856 return;
6858 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6859 if (n)
6861 /* [basic.scope.class]
6863 A name N used in a class S shall refer to the same declaration
6864 in its context and when re-evaluated in the completed scope of
6865 S. */
6866 permerror (input_location, "declaration of %q#D", decl);
6867 permerror (input_location, "changes meaning of %qD from %q+#D",
6868 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
6872 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6873 Secondary vtables are merged with primary vtables; this function
6874 will return the VAR_DECL for the primary vtable. */
6876 tree
6877 get_vtbl_decl_for_binfo (tree binfo)
6879 tree decl;
6881 decl = BINFO_VTABLE (binfo);
6882 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
6884 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
6885 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6887 if (decl)
6888 gcc_assert (TREE_CODE (decl) == VAR_DECL);
6889 return decl;
6893 /* Returns the binfo for the primary base of BINFO. If the resulting
6894 BINFO is a virtual base, and it is inherited elsewhere in the
6895 hierarchy, then the returned binfo might not be the primary base of
6896 BINFO in the complete object. Check BINFO_PRIMARY_P or
6897 BINFO_LOST_PRIMARY_P to be sure. */
6899 static tree
6900 get_primary_binfo (tree binfo)
6902 tree primary_base;
6904 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6905 if (!primary_base)
6906 return NULL_TREE;
6908 return copied_binfo (primary_base, binfo);
6911 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
6913 static int
6914 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
6916 if (!indented_p)
6917 fprintf (stream, "%*s", indent, "");
6918 return 1;
6921 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
6922 INDENT should be zero when called from the top level; it is
6923 incremented recursively. IGO indicates the next expected BINFO in
6924 inheritance graph ordering. */
6926 static tree
6927 dump_class_hierarchy_r (FILE *stream,
6928 int flags,
6929 tree binfo,
6930 tree igo,
6931 int indent)
6933 int indented = 0;
6934 tree base_binfo;
6935 int i;
6937 indented = maybe_indent_hierarchy (stream, indent, 0);
6938 fprintf (stream, "%s (0x%lx) ",
6939 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
6940 (unsigned long) binfo);
6941 if (binfo != igo)
6943 fprintf (stream, "alternative-path\n");
6944 return igo;
6946 igo = TREE_CHAIN (binfo);
6948 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6949 tree_low_cst (BINFO_OFFSET (binfo), 0));
6950 if (is_empty_class (BINFO_TYPE (binfo)))
6951 fprintf (stream, " empty");
6952 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6953 fprintf (stream, " nearly-empty");
6954 if (BINFO_VIRTUAL_P (binfo))
6955 fprintf (stream, " virtual");
6956 fprintf (stream, "\n");
6958 indented = 0;
6959 if (BINFO_PRIMARY_P (binfo))
6961 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6962 fprintf (stream, " primary-for %s (0x%lx)",
6963 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
6964 TFF_PLAIN_IDENTIFIER),
6965 (unsigned long)BINFO_INHERITANCE_CHAIN (binfo));
6967 if (BINFO_LOST_PRIMARY_P (binfo))
6969 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6970 fprintf (stream, " lost-primary");
6972 if (indented)
6973 fprintf (stream, "\n");
6975 if (!(flags & TDF_SLIM))
6977 int indented = 0;
6979 if (BINFO_SUBVTT_INDEX (binfo))
6981 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6982 fprintf (stream, " subvttidx=%s",
6983 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6984 TFF_PLAIN_IDENTIFIER));
6986 if (BINFO_VPTR_INDEX (binfo))
6988 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6989 fprintf (stream, " vptridx=%s",
6990 expr_as_string (BINFO_VPTR_INDEX (binfo),
6991 TFF_PLAIN_IDENTIFIER));
6993 if (BINFO_VPTR_FIELD (binfo))
6995 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6996 fprintf (stream, " vbaseoffset=%s",
6997 expr_as_string (BINFO_VPTR_FIELD (binfo),
6998 TFF_PLAIN_IDENTIFIER));
7000 if (BINFO_VTABLE (binfo))
7002 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7003 fprintf (stream, " vptr=%s",
7004 expr_as_string (BINFO_VTABLE (binfo),
7005 TFF_PLAIN_IDENTIFIER));
7008 if (indented)
7009 fprintf (stream, "\n");
7012 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7013 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
7015 return igo;
7018 /* Dump the BINFO hierarchy for T. */
7020 static void
7021 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
7023 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7024 fprintf (stream, " size=%lu align=%lu\n",
7025 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
7026 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
7027 fprintf (stream, " base size=%lu base align=%lu\n",
7028 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
7029 / BITS_PER_UNIT),
7030 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
7031 / BITS_PER_UNIT));
7032 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
7033 fprintf (stream, "\n");
7036 /* Debug interface to hierarchy dumping. */
7038 void
7039 debug_class (tree t)
7041 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
7044 static void
7045 dump_class_hierarchy (tree t)
7047 int flags;
7048 FILE *stream = dump_begin (TDI_class, &flags);
7050 if (stream)
7052 dump_class_hierarchy_1 (stream, flags, t);
7053 dump_end (TDI_class, stream);
7057 static void
7058 dump_array (FILE * stream, tree decl)
7060 tree value;
7061 unsigned HOST_WIDE_INT ix;
7062 HOST_WIDE_INT elt;
7063 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
7065 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
7066 / BITS_PER_UNIT);
7067 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
7068 fprintf (stream, " %s entries",
7069 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
7070 TFF_PLAIN_IDENTIFIER));
7071 fprintf (stream, "\n");
7073 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
7074 ix, value)
7075 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
7076 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
7079 static void
7080 dump_vtable (tree t, tree binfo, tree vtable)
7082 int flags;
7083 FILE *stream = dump_begin (TDI_class, &flags);
7085 if (!stream)
7086 return;
7088 if (!(flags & TDF_SLIM))
7090 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
7092 fprintf (stream, "%s for %s",
7093 ctor_vtbl_p ? "Construction vtable" : "Vtable",
7094 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
7095 if (ctor_vtbl_p)
7097 if (!BINFO_VIRTUAL_P (binfo))
7098 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
7099 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7101 fprintf (stream, "\n");
7102 dump_array (stream, vtable);
7103 fprintf (stream, "\n");
7106 dump_end (TDI_class, stream);
7109 static void
7110 dump_vtt (tree t, tree vtt)
7112 int flags;
7113 FILE *stream = dump_begin (TDI_class, &flags);
7115 if (!stream)
7116 return;
7118 if (!(flags & TDF_SLIM))
7120 fprintf (stream, "VTT for %s\n",
7121 type_as_string (t, TFF_PLAIN_IDENTIFIER));
7122 dump_array (stream, vtt);
7123 fprintf (stream, "\n");
7126 dump_end (TDI_class, stream);
7129 /* Dump a function or thunk and its thunkees. */
7131 static void
7132 dump_thunk (FILE *stream, int indent, tree thunk)
7134 static const char spaces[] = " ";
7135 tree name = DECL_NAME (thunk);
7136 tree thunks;
7138 fprintf (stream, "%.*s%p %s %s", indent, spaces,
7139 (void *)thunk,
7140 !DECL_THUNK_P (thunk) ? "function"
7141 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
7142 name ? IDENTIFIER_POINTER (name) : "<unset>");
7143 if (DECL_THUNK_P (thunk))
7145 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
7146 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
7148 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
7149 if (!virtual_adjust)
7150 /*NOP*/;
7151 else if (DECL_THIS_THUNK_P (thunk))
7152 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
7153 tree_low_cst (virtual_adjust, 0));
7154 else
7155 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
7156 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
7157 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
7158 if (THUNK_ALIAS (thunk))
7159 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
7161 fprintf (stream, "\n");
7162 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
7163 dump_thunk (stream, indent + 2, thunks);
7166 /* Dump the thunks for FN. */
7168 void
7169 debug_thunks (tree fn)
7171 dump_thunk (stderr, 0, fn);
7174 /* Virtual function table initialization. */
7176 /* Create all the necessary vtables for T and its base classes. */
7178 static void
7179 finish_vtbls (tree t)
7181 tree vbase;
7182 VEC(constructor_elt,gc) *v = NULL;
7183 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
7185 /* We lay out the primary and secondary vtables in one contiguous
7186 vtable. The primary vtable is first, followed by the non-virtual
7187 secondary vtables in inheritance graph order. */
7188 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
7189 vtable, t, &v);
7191 /* Then come the virtual bases, also in inheritance graph order. */
7192 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
7194 if (!BINFO_VIRTUAL_P (vbase))
7195 continue;
7196 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
7199 if (BINFO_VTABLE (TYPE_BINFO (t)))
7200 initialize_vtable (TYPE_BINFO (t), v);
7203 /* Initialize the vtable for BINFO with the INITS. */
7205 static void
7206 initialize_vtable (tree binfo, VEC(constructor_elt,gc) *inits)
7208 tree decl;
7210 layout_vtable_decl (binfo, VEC_length (constructor_elt, inits));
7211 decl = get_vtbl_decl_for_binfo (binfo);
7212 initialize_artificial_var (decl, inits);
7213 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
7216 /* Build the VTT (virtual table table) for T.
7217 A class requires a VTT if it has virtual bases.
7219 This holds
7220 1 - primary virtual pointer for complete object T
7221 2 - secondary VTTs for each direct non-virtual base of T which requires a
7223 3 - secondary virtual pointers for each direct or indirect base of T which
7224 has virtual bases or is reachable via a virtual path from T.
7225 4 - secondary VTTs for each direct or indirect virtual base of T.
7227 Secondary VTTs look like complete object VTTs without part 4. */
7229 static void
7230 build_vtt (tree t)
7232 tree type;
7233 tree vtt;
7234 tree index;
7235 VEC(constructor_elt,gc) *inits;
7237 /* Build up the initializers for the VTT. */
7238 inits = NULL;
7239 index = size_zero_node;
7240 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
7242 /* If we didn't need a VTT, we're done. */
7243 if (!inits)
7244 return;
7246 /* Figure out the type of the VTT. */
7247 type = build_index_type (size_int (VEC_length (constructor_elt, inits) - 1));
7248 type = build_cplus_array_type (const_ptr_type_node, type);
7250 /* Now, build the VTT object itself. */
7251 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
7252 initialize_artificial_var (vtt, inits);
7253 /* Add the VTT to the vtables list. */
7254 TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
7255 TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
7257 dump_vtt (t, vtt);
7260 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7261 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7262 and CHAIN the vtable pointer for this binfo after construction is
7263 complete. VALUE can also be another BINFO, in which case we recurse. */
7265 static tree
7266 binfo_ctor_vtable (tree binfo)
7268 tree vt;
7270 while (1)
7272 vt = BINFO_VTABLE (binfo);
7273 if (TREE_CODE (vt) == TREE_LIST)
7274 vt = TREE_VALUE (vt);
7275 if (TREE_CODE (vt) == TREE_BINFO)
7276 binfo = vt;
7277 else
7278 break;
7281 return vt;
7284 /* Data for secondary VTT initialization. */
7285 typedef struct secondary_vptr_vtt_init_data_s
7287 /* Is this the primary VTT? */
7288 bool top_level_p;
7290 /* Current index into the VTT. */
7291 tree index;
7293 /* Vector of initializers built up. */
7294 VEC(constructor_elt,gc) *inits;
7296 /* The type being constructed by this secondary VTT. */
7297 tree type_being_constructed;
7298 } secondary_vptr_vtt_init_data;
7300 /* Recursively build the VTT-initializer for BINFO (which is in the
7301 hierarchy dominated by T). INITS points to the end of the initializer
7302 list to date. INDEX is the VTT index where the next element will be
7303 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7304 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7305 for virtual bases of T. When it is not so, we build the constructor
7306 vtables for the BINFO-in-T variant. */
7308 static void
7309 build_vtt_inits (tree binfo, tree t, VEC(constructor_elt,gc) **inits, tree *index)
7311 int i;
7312 tree b;
7313 tree init;
7314 secondary_vptr_vtt_init_data data;
7315 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
7317 /* We only need VTTs for subobjects with virtual bases. */
7318 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
7319 return;
7321 /* We need to use a construction vtable if this is not the primary
7322 VTT. */
7323 if (!top_level_p)
7325 build_ctor_vtbl_group (binfo, t);
7327 /* Record the offset in the VTT where this sub-VTT can be found. */
7328 BINFO_SUBVTT_INDEX (binfo) = *index;
7331 /* Add the address of the primary vtable for the complete object. */
7332 init = binfo_ctor_vtable (binfo);
7333 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
7334 if (top_level_p)
7336 gcc_assert (!BINFO_VPTR_INDEX (binfo));
7337 BINFO_VPTR_INDEX (binfo) = *index;
7339 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7341 /* Recursively add the secondary VTTs for non-virtual bases. */
7342 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
7343 if (!BINFO_VIRTUAL_P (b))
7344 build_vtt_inits (b, t, inits, index);
7346 /* Add secondary virtual pointers for all subobjects of BINFO with
7347 either virtual bases or reachable along a virtual path, except
7348 subobjects that are non-virtual primary bases. */
7349 data.top_level_p = top_level_p;
7350 data.index = *index;
7351 data.inits = *inits;
7352 data.type_being_constructed = BINFO_TYPE (binfo);
7354 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
7356 *index = data.index;
7358 /* data.inits might have grown as we added secondary virtual pointers.
7359 Make sure our caller knows about the new vector. */
7360 *inits = data.inits;
7362 if (top_level_p)
7363 /* Add the secondary VTTs for virtual bases in inheritance graph
7364 order. */
7365 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7367 if (!BINFO_VIRTUAL_P (b))
7368 continue;
7370 build_vtt_inits (b, t, inits, index);
7372 else
7373 /* Remove the ctor vtables we created. */
7374 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
7377 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
7378 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
7380 static tree
7381 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
7383 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
7385 /* We don't care about bases that don't have vtables. */
7386 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7387 return dfs_skip_bases;
7389 /* We're only interested in proper subobjects of the type being
7390 constructed. */
7391 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
7392 return NULL_TREE;
7394 /* We're only interested in bases with virtual bases or reachable
7395 via a virtual path from the type being constructed. */
7396 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
7397 || binfo_via_virtual (binfo, data->type_being_constructed)))
7398 return dfs_skip_bases;
7400 /* We're not interested in non-virtual primary bases. */
7401 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
7402 return NULL_TREE;
7404 /* Record the index where this secondary vptr can be found. */
7405 if (data->top_level_p)
7407 gcc_assert (!BINFO_VPTR_INDEX (binfo));
7408 BINFO_VPTR_INDEX (binfo) = data->index;
7410 if (BINFO_VIRTUAL_P (binfo))
7412 /* It's a primary virtual base, and this is not a
7413 construction vtable. Find the base this is primary of in
7414 the inheritance graph, and use that base's vtable
7415 now. */
7416 while (BINFO_PRIMARY_P (binfo))
7417 binfo = BINFO_INHERITANCE_CHAIN (binfo);
7421 /* Add the initializer for the secondary vptr itself. */
7422 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
7424 /* Advance the vtt index. */
7425 data->index = size_binop (PLUS_EXPR, data->index,
7426 TYPE_SIZE_UNIT (ptr_type_node));
7428 return NULL_TREE;
7431 /* Called from build_vtt_inits via dfs_walk. After building
7432 constructor vtables and generating the sub-vtt from them, we need
7433 to restore the BINFO_VTABLES that were scribbled on. DATA is the
7434 binfo of the base whose sub vtt was generated. */
7436 static tree
7437 dfs_fixup_binfo_vtbls (tree binfo, void* data)
7439 tree vtable = BINFO_VTABLE (binfo);
7441 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7442 /* If this class has no vtable, none of its bases do. */
7443 return dfs_skip_bases;
7445 if (!vtable)
7446 /* This might be a primary base, so have no vtable in this
7447 hierarchy. */
7448 return NULL_TREE;
7450 /* If we scribbled the construction vtable vptr into BINFO, clear it
7451 out now. */
7452 if (TREE_CODE (vtable) == TREE_LIST
7453 && (TREE_PURPOSE (vtable) == (tree) data))
7454 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
7456 return NULL_TREE;
7459 /* Build the construction vtable group for BINFO which is in the
7460 hierarchy dominated by T. */
7462 static void
7463 build_ctor_vtbl_group (tree binfo, tree t)
7465 tree type;
7466 tree vtbl;
7467 tree id;
7468 tree vbase;
7469 VEC(constructor_elt,gc) *v;
7471 /* See if we've already created this construction vtable group. */
7472 id = mangle_ctor_vtbl_for_type (t, binfo);
7473 if (IDENTIFIER_GLOBAL_VALUE (id))
7474 return;
7476 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
7477 /* Build a version of VTBL (with the wrong type) for use in
7478 constructing the addresses of secondary vtables in the
7479 construction vtable group. */
7480 vtbl = build_vtable (t, id, ptr_type_node);
7481 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
7483 v = NULL;
7484 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7485 binfo, vtbl, t, &v);
7487 /* Add the vtables for each of our virtual bases using the vbase in T
7488 binfo. */
7489 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7490 vbase;
7491 vbase = TREE_CHAIN (vbase))
7493 tree b;
7495 if (!BINFO_VIRTUAL_P (vbase))
7496 continue;
7497 b = copied_binfo (vbase, binfo);
7499 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
7502 /* Figure out the type of the construction vtable. */
7503 type = build_index_type (size_int (VEC_length (constructor_elt, v) - 1));
7504 type = build_cplus_array_type (vtable_entry_type, type);
7505 layout_type (type);
7506 TREE_TYPE (vtbl) = type;
7507 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
7508 layout_decl (vtbl, 0);
7510 /* Initialize the construction vtable. */
7511 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
7512 initialize_artificial_var (vtbl, v);
7513 dump_vtable (t, binfo, vtbl);
7516 /* Add the vtbl initializers for BINFO (and its bases other than
7517 non-virtual primaries) to the list of INITS. BINFO is in the
7518 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7519 the constructor the vtbl inits should be accumulated for. (If this
7520 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7521 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7522 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7523 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7524 but are not necessarily the same in terms of layout. */
7526 static void
7527 accumulate_vtbl_inits (tree binfo,
7528 tree orig_binfo,
7529 tree rtti_binfo,
7530 tree vtbl,
7531 tree t,
7532 VEC(constructor_elt,gc) **inits)
7534 int i;
7535 tree base_binfo;
7536 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
7538 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
7540 /* If it doesn't have a vptr, we don't do anything. */
7541 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7542 return;
7544 /* If we're building a construction vtable, we're not interested in
7545 subobjects that don't require construction vtables. */
7546 if (ctor_vtbl_p
7547 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
7548 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7549 return;
7551 /* Build the initializers for the BINFO-in-T vtable. */
7552 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
7554 /* Walk the BINFO and its bases. We walk in preorder so that as we
7555 initialize each vtable we can figure out at what offset the
7556 secondary vtable lies from the primary vtable. We can't use
7557 dfs_walk here because we need to iterate through bases of BINFO
7558 and RTTI_BINFO simultaneously. */
7559 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7561 /* Skip virtual bases. */
7562 if (BINFO_VIRTUAL_P (base_binfo))
7563 continue;
7564 accumulate_vtbl_inits (base_binfo,
7565 BINFO_BASE_BINFO (orig_binfo, i),
7566 rtti_binfo, vtbl, t,
7567 inits);
7571 /* Called from accumulate_vtbl_inits. Adds the initializers for the
7572 BINFO vtable to L. */
7574 static void
7575 dfs_accumulate_vtbl_inits (tree binfo,
7576 tree orig_binfo,
7577 tree rtti_binfo,
7578 tree orig_vtbl,
7579 tree t,
7580 VEC(constructor_elt,gc) **l)
7582 tree vtbl = NULL_TREE;
7583 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
7584 int n_inits;
7586 if (ctor_vtbl_p
7587 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7589 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7590 primary virtual base. If it is not the same primary in
7591 the hierarchy of T, we'll need to generate a ctor vtable
7592 for it, to place at its location in T. If it is the same
7593 primary, we still need a VTT entry for the vtable, but it
7594 should point to the ctor vtable for the base it is a
7595 primary for within the sub-hierarchy of RTTI_BINFO.
7597 There are three possible cases:
7599 1) We are in the same place.
7600 2) We are a primary base within a lost primary virtual base of
7601 RTTI_BINFO.
7602 3) We are primary to something not a base of RTTI_BINFO. */
7604 tree b;
7605 tree last = NULL_TREE;
7607 /* First, look through the bases we are primary to for RTTI_BINFO
7608 or a virtual base. */
7609 b = binfo;
7610 while (BINFO_PRIMARY_P (b))
7612 b = BINFO_INHERITANCE_CHAIN (b);
7613 last = b;
7614 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
7615 goto found;
7617 /* If we run out of primary links, keep looking down our
7618 inheritance chain; we might be an indirect primary. */
7619 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7620 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
7621 break;
7622 found:
7624 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7625 base B and it is a base of RTTI_BINFO, this is case 2. In
7626 either case, we share our vtable with LAST, i.e. the
7627 derived-most base within B of which we are a primary. */
7628 if (b == rtti_binfo
7629 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
7630 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7631 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7632 binfo_ctor_vtable after everything's been set up. */
7633 vtbl = last;
7635 /* Otherwise, this is case 3 and we get our own. */
7637 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
7638 return;
7640 n_inits = VEC_length (constructor_elt, *l);
7642 if (!vtbl)
7644 tree index;
7645 int non_fn_entries;
7647 /* Add the initializer for this vtable. */
7648 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7649 &non_fn_entries, l);
7651 /* Figure out the position to which the VPTR should point. */
7652 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
7653 index = size_binop (PLUS_EXPR,
7654 size_int (non_fn_entries),
7655 size_int (n_inits));
7656 index = size_binop (MULT_EXPR,
7657 TYPE_SIZE_UNIT (vtable_entry_type),
7658 index);
7659 vtbl = build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7662 if (ctor_vtbl_p)
7663 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7664 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7665 straighten this out. */
7666 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7667 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
7668 /* Throw away any unneeded intializers. */
7669 VEC_truncate (constructor_elt, *l, n_inits);
7670 else
7671 /* For an ordinary vtable, set BINFO_VTABLE. */
7672 BINFO_VTABLE (binfo) = vtbl;
7675 static GTY(()) tree abort_fndecl_addr;
7677 /* Construct the initializer for BINFO's virtual function table. BINFO
7678 is part of the hierarchy dominated by T. If we're building a
7679 construction vtable, the ORIG_BINFO is the binfo we should use to
7680 find the actual function pointers to put in the vtable - but they
7681 can be overridden on the path to most-derived in the graph that
7682 ORIG_BINFO belongs. Otherwise,
7683 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7684 BINFO that should be indicated by the RTTI information in the
7685 vtable; it will be a base class of T, rather than T itself, if we
7686 are building a construction vtable.
7688 The value returned is a TREE_LIST suitable for wrapping in a
7689 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7690 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7691 number of non-function entries in the vtable.
7693 It might seem that this function should never be called with a
7694 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7695 base is always subsumed by a derived class vtable. However, when
7696 we are building construction vtables, we do build vtables for
7697 primary bases; we need these while the primary base is being
7698 constructed. */
7700 static void
7701 build_vtbl_initializer (tree binfo,
7702 tree orig_binfo,
7703 tree t,
7704 tree rtti_binfo,
7705 int* non_fn_entries_p,
7706 VEC(constructor_elt,gc) **inits)
7708 tree v;
7709 vtbl_init_data vid;
7710 unsigned ix, jx;
7711 tree vbinfo;
7712 VEC(tree,gc) *vbases;
7713 constructor_elt *e;
7715 /* Initialize VID. */
7716 memset (&vid, 0, sizeof (vid));
7717 vid.binfo = binfo;
7718 vid.derived = t;
7719 vid.rtti_binfo = rtti_binfo;
7720 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
7721 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
7722 vid.generate_vcall_entries = true;
7723 /* The first vbase or vcall offset is at index -3 in the vtable. */
7724 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7726 /* Add entries to the vtable for RTTI. */
7727 build_rtti_vtbl_entries (binfo, &vid);
7729 /* Create an array for keeping track of the functions we've
7730 processed. When we see multiple functions with the same
7731 signature, we share the vcall offsets. */
7732 vid.fns = VEC_alloc (tree, gc, 32);
7733 /* Add the vcall and vbase offset entries. */
7734 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7736 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7737 build_vbase_offset_vtbl_entries. */
7738 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
7739 VEC_iterate (tree, vbases, ix, vbinfo); ix++)
7740 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
7742 /* If the target requires padding between data entries, add that now. */
7743 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7745 int n_entries = VEC_length (constructor_elt, vid.inits);
7747 VEC_safe_grow (constructor_elt, gc, vid.inits,
7748 TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
7750 /* Move data entries into their new positions and add padding
7751 after the new positions. Iterate backwards so we don't
7752 overwrite entries that we would need to process later. */
7753 for (ix = n_entries - 1;
7754 VEC_iterate (constructor_elt, vid.inits, ix, e);
7755 ix--)
7757 int j;
7758 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
7759 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
7761 VEC_replace (constructor_elt, vid.inits, new_position, e);
7763 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
7765 constructor_elt *f = VEC_index (constructor_elt, vid.inits,
7766 new_position - j);
7767 f->index = NULL_TREE;
7768 f->value = build1 (NOP_EXPR, vtable_entry_type,
7769 null_pointer_node);
7774 if (non_fn_entries_p)
7775 *non_fn_entries_p = VEC_length (constructor_elt, vid.inits);
7777 /* The initializers for virtual functions were built up in reverse
7778 order. Straighten them out and add them to the running list in one
7779 step. */
7780 jx = VEC_length (constructor_elt, *inits);
7781 VEC_safe_grow (constructor_elt, gc, *inits,
7782 (jx + VEC_length (constructor_elt, vid.inits)));
7784 for (ix = VEC_length (constructor_elt, vid.inits) - 1;
7785 VEC_iterate (constructor_elt, vid.inits, ix, e);
7786 ix--, jx++)
7787 VEC_replace (constructor_elt, *inits, jx, e);
7789 /* Go through all the ordinary virtual functions, building up
7790 initializers. */
7791 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7793 tree delta;
7794 tree vcall_index;
7795 tree fn, fn_original;
7796 tree init = NULL_TREE;
7798 fn = BV_FN (v);
7799 fn_original = fn;
7800 if (DECL_THUNK_P (fn))
7802 if (!DECL_NAME (fn))
7803 finish_thunk (fn);
7804 if (THUNK_ALIAS (fn))
7806 fn = THUNK_ALIAS (fn);
7807 BV_FN (v) = fn;
7809 fn_original = THUNK_TARGET (fn);
7812 /* If the only definition of this function signature along our
7813 primary base chain is from a lost primary, this vtable slot will
7814 never be used, so just zero it out. This is important to avoid
7815 requiring extra thunks which cannot be generated with the function.
7817 We first check this in update_vtable_entry_for_fn, so we handle
7818 restored primary bases properly; we also need to do it here so we
7819 zero out unused slots in ctor vtables, rather than filling them
7820 with erroneous values (though harmless, apart from relocation
7821 costs). */
7822 if (BV_LOST_PRIMARY (v))
7823 init = size_zero_node;
7825 if (! init)
7827 /* Pull the offset for `this', and the function to call, out of
7828 the list. */
7829 delta = BV_DELTA (v);
7830 vcall_index = BV_VCALL_INDEX (v);
7832 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
7833 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7835 /* You can't call an abstract virtual function; it's abstract.
7836 So, we replace these functions with __pure_virtual. */
7837 if (DECL_PURE_VIRTUAL_P (fn_original))
7839 fn = abort_fndecl;
7840 if (abort_fndecl_addr == NULL)
7841 abort_fndecl_addr = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7842 init = abort_fndecl_addr;
7844 else
7846 if (!integer_zerop (delta) || vcall_index)
7848 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
7849 if (!DECL_NAME (fn))
7850 finish_thunk (fn);
7852 /* Take the address of the function, considering it to be of an
7853 appropriate generic type. */
7854 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7858 /* And add it to the chain of initializers. */
7859 if (TARGET_VTABLE_USES_DESCRIPTORS)
7861 int i;
7862 if (init == size_zero_node)
7863 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7864 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
7865 else
7866 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7868 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
7869 TREE_OPERAND (init, 0),
7870 build_int_cst (NULL_TREE, i));
7871 TREE_CONSTANT (fdesc) = 1;
7873 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
7876 else
7877 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
7881 /* Adds to vid->inits the initializers for the vbase and vcall
7882 offsets in BINFO, which is in the hierarchy dominated by T. */
7884 static void
7885 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
7887 tree b;
7889 /* If this is a derived class, we must first create entries
7890 corresponding to the primary base class. */
7891 b = get_primary_binfo (binfo);
7892 if (b)
7893 build_vcall_and_vbase_vtbl_entries (b, vid);
7895 /* Add the vbase entries for this base. */
7896 build_vbase_offset_vtbl_entries (binfo, vid);
7897 /* Add the vcall entries for this base. */
7898 build_vcall_offset_vtbl_entries (binfo, vid);
7901 /* Returns the initializers for the vbase offset entries in the vtable
7902 for BINFO (which is part of the class hierarchy dominated by T), in
7903 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7904 where the next vbase offset will go. */
7906 static void
7907 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7909 tree vbase;
7910 tree t;
7911 tree non_primary_binfo;
7913 /* If there are no virtual baseclasses, then there is nothing to
7914 do. */
7915 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
7916 return;
7918 t = vid->derived;
7920 /* We might be a primary base class. Go up the inheritance hierarchy
7921 until we find the most derived class of which we are a primary base:
7922 it is the offset of that which we need to use. */
7923 non_primary_binfo = binfo;
7924 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7926 tree b;
7928 /* If we have reached a virtual base, then it must be a primary
7929 base (possibly multi-level) of vid->binfo, or we wouldn't
7930 have called build_vcall_and_vbase_vtbl_entries for it. But it
7931 might be a lost primary, so just skip down to vid->binfo. */
7932 if (BINFO_VIRTUAL_P (non_primary_binfo))
7934 non_primary_binfo = vid->binfo;
7935 break;
7938 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7939 if (get_primary_binfo (b) != non_primary_binfo)
7940 break;
7941 non_primary_binfo = b;
7944 /* Go through the virtual bases, adding the offsets. */
7945 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7946 vbase;
7947 vbase = TREE_CHAIN (vbase))
7949 tree b;
7950 tree delta;
7952 if (!BINFO_VIRTUAL_P (vbase))
7953 continue;
7955 /* Find the instance of this virtual base in the complete
7956 object. */
7957 b = copied_binfo (vbase, binfo);
7959 /* If we've already got an offset for this virtual base, we
7960 don't need another one. */
7961 if (BINFO_VTABLE_PATH_MARKED (b))
7962 continue;
7963 BINFO_VTABLE_PATH_MARKED (b) = 1;
7965 /* Figure out where we can find this vbase offset. */
7966 delta = size_binop (MULT_EXPR,
7967 vid->index,
7968 convert (ssizetype,
7969 TYPE_SIZE_UNIT (vtable_entry_type)));
7970 if (vid->primary_vtbl_p)
7971 BINFO_VPTR_FIELD (b) = delta;
7973 if (binfo != TYPE_BINFO (t))
7974 /* The vbase offset had better be the same. */
7975 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
7977 /* The next vbase will come at a more negative offset. */
7978 vid->index = size_binop (MINUS_EXPR, vid->index,
7979 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7981 /* The initializer is the delta from BINFO to this virtual base.
7982 The vbase offsets go in reverse inheritance-graph order, and
7983 we are walking in inheritance graph order so these end up in
7984 the right order. */
7985 delta = size_diffop_loc (input_location,
7986 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7988 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
7989 fold_build1_loc (input_location, NOP_EXPR,
7990 vtable_entry_type, delta));
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 (tree binfo, vtbl_init_data* vid)
8001 /* We only need these entries if this base is a virtual base. We
8002 compute the indices -- but do not add to the vtable -- when
8003 building the main vtable for a class. */
8004 if (binfo == TYPE_BINFO (vid->derived)
8005 || (BINFO_VIRTUAL_P (binfo)
8006 /* If BINFO is RTTI_BINFO, then (since BINFO does not
8007 correspond to VID->DERIVED), we are building a primary
8008 construction virtual table. Since this is a primary
8009 virtual table, we do not need the vcall offsets for
8010 BINFO. */
8011 && binfo != vid->rtti_binfo))
8013 /* We need a vcall offset for each of the virtual functions in this
8014 vtable. For example:
8016 class A { virtual void f (); };
8017 class B1 : virtual public A { virtual void f (); };
8018 class B2 : virtual public A { virtual void f (); };
8019 class C: public B1, public B2 { virtual void f (); };
8021 A C object has a primary base of B1, which has a primary base of A. A
8022 C also has a secondary base of B2, which no longer has a primary base
8023 of A. So the B2-in-C construction vtable needs a secondary vtable for
8024 A, which will adjust the A* to a B2* to call f. We have no way of
8025 knowing what (or even whether) this offset will be when we define B2,
8026 so we store this "vcall offset" in the A sub-vtable and look it up in
8027 a "virtual thunk" for B2::f.
8029 We need entries for all the functions in our primary vtable and
8030 in our non-virtual bases' secondary vtables. */
8031 vid->vbase = binfo;
8032 /* If we are just computing the vcall indices -- but do not need
8033 the actual entries -- not that. */
8034 if (!BINFO_VIRTUAL_P (binfo))
8035 vid->generate_vcall_entries = false;
8036 /* Now, walk through the non-virtual bases, adding vcall offsets. */
8037 add_vcall_offset_vtbl_entries_r (binfo, vid);
8041 /* Build vcall offsets, starting with those for BINFO. */
8043 static void
8044 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
8046 int i;
8047 tree primary_binfo;
8048 tree base_binfo;
8050 /* Don't walk into virtual bases -- except, of course, for the
8051 virtual base for which we are building vcall offsets. Any
8052 primary virtual base will have already had its offsets generated
8053 through the recursion in build_vcall_and_vbase_vtbl_entries. */
8054 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
8055 return;
8057 /* If BINFO has a primary base, process it first. */
8058 primary_binfo = get_primary_binfo (binfo);
8059 if (primary_binfo)
8060 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
8062 /* Add BINFO itself to the list. */
8063 add_vcall_offset_vtbl_entries_1 (binfo, vid);
8065 /* Scan the non-primary bases of BINFO. */
8066 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8067 if (base_binfo != primary_binfo)
8068 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 (tree binfo, vtbl_init_data* vid)
8076 /* Make entries for the rest of the virtuals. */
8077 if (abi_version_at_least (2))
8079 tree orig_fn;
8081 /* The ABI requires that the methods be processed in declaration
8082 order. G++ 3.2 used the order in the vtable. */
8083 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
8084 orig_fn;
8085 orig_fn = TREE_CHAIN (orig_fn))
8086 if (DECL_VINDEX (orig_fn))
8087 add_vcall_offset (orig_fn, binfo, vid);
8089 else
8091 tree derived_virtuals;
8092 tree base_virtuals;
8093 tree orig_virtuals;
8094 /* If BINFO is a primary base, the most derived class which has
8095 BINFO as a primary base; otherwise, just BINFO. */
8096 tree non_primary_binfo;
8098 /* We might be a primary base class. Go up the inheritance hierarchy
8099 until we find the most derived class of which we are a primary base:
8100 it is the BINFO_VIRTUALS there that we need to consider. */
8101 non_primary_binfo = binfo;
8102 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8104 tree b;
8106 /* If we have reached a virtual base, then it must be vid->vbase,
8107 because we ignore other virtual bases in
8108 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
8109 base (possibly multi-level) of vid->binfo, or we wouldn't
8110 have called build_vcall_and_vbase_vtbl_entries for it. But it
8111 might be a lost primary, so just skip down to vid->binfo. */
8112 if (BINFO_VIRTUAL_P (non_primary_binfo))
8114 gcc_assert (non_primary_binfo == vid->vbase);
8115 non_primary_binfo = vid->binfo;
8116 break;
8119 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8120 if (get_primary_binfo (b) != non_primary_binfo)
8121 break;
8122 non_primary_binfo = b;
8125 if (vid->ctor_vtbl_p)
8126 /* For a ctor vtable we need the equivalent binfo within the hierarchy
8127 where rtti_binfo is the most derived type. */
8128 non_primary_binfo
8129 = original_binfo (non_primary_binfo, vid->rtti_binfo);
8131 for (base_virtuals = BINFO_VIRTUALS (binfo),
8132 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
8133 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
8134 base_virtuals;
8135 base_virtuals = TREE_CHAIN (base_virtuals),
8136 derived_virtuals = TREE_CHAIN (derived_virtuals),
8137 orig_virtuals = TREE_CHAIN (orig_virtuals))
8139 tree orig_fn;
8141 /* Find the declaration that originally caused this function to
8142 be present in BINFO_TYPE (binfo). */
8143 orig_fn = BV_FN (orig_virtuals);
8145 /* When processing BINFO, we only want to generate vcall slots for
8146 function slots introduced in BINFO. So don't try to generate
8147 one if the function isn't even defined in BINFO. */
8148 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
8149 continue;
8151 add_vcall_offset (orig_fn, binfo, vid);
8156 /* Add a vcall offset entry for ORIG_FN to the vtable. */
8158 static void
8159 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
8161 size_t i;
8162 tree vcall_offset;
8163 tree derived_entry;
8165 /* If there is already an entry for a function with the same
8166 signature as FN, then we do not need a second vcall offset.
8167 Check the list of functions already present in the derived
8168 class vtable. */
8169 for (i = 0; VEC_iterate (tree, vid->fns, i, derived_entry); ++i)
8171 if (same_signature_p (derived_entry, orig_fn)
8172 /* We only use one vcall offset for virtual destructors,
8173 even though there are two virtual table entries. */
8174 || (DECL_DESTRUCTOR_P (derived_entry)
8175 && DECL_DESTRUCTOR_P (orig_fn)))
8176 return;
8179 /* If we are building these vcall offsets as part of building
8180 the vtable for the most derived class, remember the vcall
8181 offset. */
8182 if (vid->binfo == TYPE_BINFO (vid->derived))
8184 tree_pair_p elt = VEC_safe_push (tree_pair_s, gc,
8185 CLASSTYPE_VCALL_INDICES (vid->derived),
8186 NULL);
8187 elt->purpose = orig_fn;
8188 elt->value = vid->index;
8191 /* The next vcall offset will be found at a more negative
8192 offset. */
8193 vid->index = size_binop (MINUS_EXPR, vid->index,
8194 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8196 /* Keep track of this function. */
8197 VEC_safe_push (tree, gc, vid->fns, orig_fn);
8199 if (vid->generate_vcall_entries)
8201 tree base;
8202 tree fn;
8204 /* Find the overriding function. */
8205 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
8206 if (fn == error_mark_node)
8207 vcall_offset = build1 (NOP_EXPR, vtable_entry_type,
8208 integer_zero_node);
8209 else
8211 base = TREE_VALUE (fn);
8213 /* The vbase we're working on is a primary base of
8214 vid->binfo. But it might be a lost primary, so its
8215 BINFO_OFFSET might be wrong, so we just use the
8216 BINFO_OFFSET from vid->binfo. */
8217 vcall_offset = size_diffop_loc (input_location,
8218 BINFO_OFFSET (base),
8219 BINFO_OFFSET (vid->binfo));
8220 vcall_offset = fold_build1_loc (input_location,
8221 NOP_EXPR, vtable_entry_type,
8222 vcall_offset);
8224 /* Add the initializer to the vtable. */
8225 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
8229 /* Return vtbl initializers for the RTTI entries corresponding to the
8230 BINFO's vtable. The RTTI entries should indicate the object given
8231 by VID->rtti_binfo. */
8233 static void
8234 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
8236 tree b;
8237 tree t;
8238 tree offset;
8239 tree decl;
8240 tree init;
8242 t = BINFO_TYPE (vid->rtti_binfo);
8244 /* To find the complete object, we will first convert to our most
8245 primary base, and then add the offset in the vtbl to that value. */
8246 b = binfo;
8247 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8248 && !BINFO_LOST_PRIMARY_P (b))
8250 tree primary_base;
8252 primary_base = get_primary_binfo (b);
8253 gcc_assert (BINFO_PRIMARY_P (primary_base)
8254 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8255 b = primary_base;
8257 offset = size_diffop_loc (input_location,
8258 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8260 /* The second entry is the address of the typeinfo object. */
8261 if (flag_rtti)
8262 decl = build_address (get_tinfo_decl (t));
8263 else
8264 decl = integer_zero_node;
8266 /* Convert the declaration to a type that can be stored in the
8267 vtable. */
8268 init = build_nop (vfunc_ptr_type_node, decl);
8269 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
8271 /* Add the offset-to-top entry. It comes earlier in the vtable than
8272 the typeinfo entry. Convert the offset to look like a
8273 function pointer, so that we can put it in the vtable. */
8274 init = build_nop (vfunc_ptr_type_node, offset);
8275 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
8278 /* Fold a OBJ_TYPE_REF expression to the address of a function.
8279 KNOWN_TYPE carries the true type of OBJ_TYPE_REF_OBJECT(REF). */
8281 tree
8282 cp_fold_obj_type_ref (tree ref, tree known_type)
8284 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
8285 HOST_WIDE_INT i = 0;
8286 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
8287 tree fndecl;
8289 while (i != index)
8291 i += (TARGET_VTABLE_USES_DESCRIPTORS
8292 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
8293 v = TREE_CHAIN (v);
8296 fndecl = BV_FN (v);
8298 #ifdef ENABLE_CHECKING
8299 gcc_assert (tree_int_cst_equal (OBJ_TYPE_REF_TOKEN (ref),
8300 DECL_VINDEX (fndecl)));
8301 #endif
8303 cgraph_node (fndecl)->local.vtable_method = true;
8305 return build_address (fndecl);
8308 #include "gt-cp-class.h"