c-family/
[official-gcc.git] / gcc / cp / class.c
blob3e1b44a4c1ec4371b5caaf1975ed6718a91d8ab2
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, 2011,
4 2012
5 Free Software Foundation, Inc.
6 Contributed by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
25 /* High-level class interface. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "target.h"
36 #include "convert.h"
37 #include "cgraph.h"
38 #include "dumpfile.h"
39 #include "splay-tree.h"
40 #include "pointer-set.h"
41 #include "hash-table.h"
43 /* The number of nested classes being processed. If we are not in the
44 scope of any class, this is zero. */
46 int current_class_depth;
48 /* In order to deal with nested classes, we keep a stack of classes.
49 The topmost entry is the innermost class, and is the entry at index
50 CURRENT_CLASS_DEPTH */
52 typedef struct class_stack_node {
53 /* The name of the class. */
54 tree name;
56 /* The _TYPE node for the class. */
57 tree type;
59 /* The access specifier pending for new declarations in the scope of
60 this class. */
61 tree access;
63 /* If were defining TYPE, the names used in this class. */
64 splay_tree names_used;
66 /* Nonzero if this class is no longer open, because of a call to
67 push_to_top_level. */
68 size_t hidden;
69 }* class_stack_node_t;
71 typedef struct vtbl_init_data_s
73 /* The base for which we're building initializers. */
74 tree binfo;
75 /* The type of the most-derived type. */
76 tree derived;
77 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
78 unless ctor_vtbl_p is true. */
79 tree rtti_binfo;
80 /* The negative-index vtable initializers built up so far. These
81 are in order from least negative index to most negative index. */
82 VEC(constructor_elt,gc) *inits;
83 /* The binfo for the virtual base for which we're building
84 vcall offset initializers. */
85 tree vbase;
86 /* The functions in vbase for which we have already provided vcall
87 offsets. */
88 VEC(tree,gc) *fns;
89 /* The vtable index of the next vcall or vbase offset. */
90 tree index;
91 /* Nonzero if we are building the initializer for the primary
92 vtable. */
93 int primary_vtbl_p;
94 /* Nonzero if we are building the initializer for a construction
95 vtable. */
96 int ctor_vtbl_p;
97 /* True when adding vcall offset entries to the vtable. False when
98 merely computing the indices. */
99 bool generate_vcall_entries;
100 } vtbl_init_data;
102 /* The type of a function passed to walk_subobject_offsets. */
103 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
105 /* The stack itself. This is a dynamically resized array. The
106 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
107 static int current_class_stack_size;
108 static class_stack_node_t current_class_stack;
110 /* The size of the largest empty class seen in this translation unit. */
111 static GTY (()) tree sizeof_biggest_empty_class;
113 /* An array of all local classes present in this translation unit, in
114 declaration order. */
115 VEC(tree,gc) *local_classes;
117 static tree get_vfield_name (tree);
118 static void finish_struct_anon (tree);
119 static tree get_vtable_name (tree);
120 static tree get_basefndecls (tree, tree);
121 static int build_primary_vtable (tree, tree);
122 static int build_secondary_vtable (tree);
123 static void finish_vtbls (tree);
124 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
125 static void finish_struct_bits (tree);
126 static int alter_access (tree, tree, tree);
127 static void handle_using_decl (tree, tree);
128 static tree dfs_modify_vtables (tree, void *);
129 static tree modify_all_vtables (tree, tree);
130 static void determine_primary_bases (tree);
131 static void finish_struct_methods (tree);
132 static void maybe_warn_about_overly_private_class (tree);
133 static int method_name_cmp (const void *, const void *);
134 static int resort_method_name_cmp (const void *, const void *);
135 static void add_implicitly_declared_members (tree, tree*, int, int);
136 static tree fixed_type_or_null (tree, int *, int *);
137 static tree build_simple_base_path (tree expr, tree binfo);
138 static tree build_vtbl_ref_1 (tree, tree);
139 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
140 VEC(constructor_elt,gc) **);
141 static int count_fields (tree);
142 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
143 static void insert_into_classtype_sorted_fields (tree, tree, int);
144 static bool check_bitfield_decl (tree);
145 static void check_field_decl (tree, tree, int *, int *, int *);
146 static void check_field_decls (tree, tree *, int *, int *);
147 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
148 static void build_base_fields (record_layout_info, splay_tree, tree *);
149 static void check_methods (tree);
150 static void remove_zero_width_bit_fields (tree);
151 static void check_bases (tree, int *, int *);
152 static void check_bases_and_members (tree);
153 static tree create_vtable_ptr (tree, tree *);
154 static void include_empty_classes (record_layout_info);
155 static void layout_class_type (tree, tree *);
156 static void propagate_binfo_offsets (tree, tree);
157 static void layout_virtual_bases (record_layout_info, splay_tree);
158 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
159 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
160 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
161 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset (tree, tree, vtbl_init_data *);
163 static void layout_vtable_decl (tree, int);
164 static tree dfs_find_final_overrider_pre (tree, void *);
165 static tree dfs_find_final_overrider_post (tree, void *);
166 static tree find_final_overrider (tree, tree, tree);
167 static int make_new_vtable (tree, tree);
168 static tree get_primary_binfo (tree);
169 static int maybe_indent_hierarchy (FILE *, int, int);
170 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
171 static void dump_class_hierarchy (tree);
172 static void dump_class_hierarchy_1 (FILE *, int, tree);
173 static void dump_array (FILE *, tree);
174 static void dump_vtable (tree, tree, tree);
175 static void dump_vtt (tree, tree);
176 static void dump_thunk (FILE *, int, tree);
177 static tree build_vtable (tree, tree, tree);
178 static void initialize_vtable (tree, VEC(constructor_elt,gc) *);
179 static void layout_nonempty_base_or_field (record_layout_info,
180 tree, tree, splay_tree);
181 static tree end_of_class (tree, int);
182 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
183 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
184 VEC(constructor_elt,gc) **);
185 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
186 VEC(constructor_elt,gc) **);
187 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
188 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
189 static void clone_constructors_and_destructors (tree);
190 static tree build_clone (tree, tree);
191 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
192 static void build_ctor_vtbl_group (tree, tree);
193 static void build_vtt (tree);
194 static tree binfo_ctor_vtable (tree);
195 static void build_vtt_inits (tree, tree, VEC(constructor_elt,gc) **, tree *);
196 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
197 static tree dfs_fixup_binfo_vtbls (tree, void *);
198 static int record_subobject_offset (tree, tree, splay_tree);
199 static int check_subobject_offset (tree, tree, splay_tree);
200 static int walk_subobject_offsets (tree, subobject_offset_fn,
201 tree, splay_tree, tree, int);
202 static void record_subobject_offsets (tree, tree, splay_tree, bool);
203 static int layout_conflict_p (tree, tree, splay_tree, int);
204 static int splay_tree_compare_integer_csts (splay_tree_key k1,
205 splay_tree_key k2);
206 static void warn_about_ambiguous_bases (tree);
207 static bool type_requires_array_cookie (tree);
208 static bool contains_empty_class_p (tree);
209 static bool base_derived_from (tree, tree);
210 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
211 static tree end_of_base (tree);
212 static tree get_vcall_index (tree, tree);
214 /* Variables shared between class.c and call.c. */
216 int n_vtables = 0;
217 int n_vtable_entries = 0;
218 int n_vtable_searches = 0;
219 int n_vtable_elems = 0;
220 int n_convert_harshness = 0;
221 int n_compute_conversion_costs = 0;
222 int n_inner_fields_searched = 0;
224 /* Convert to or from a base subobject. EXPR is an expression of type
225 `A' or `A*', an expression of type `B' or `B*' is returned. To
226 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
227 the B base instance within A. To convert base A to derived B, CODE
228 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
229 In this latter case, A must not be a morally virtual base of B.
230 NONNULL is true if EXPR is known to be non-NULL (this is only
231 needed when EXPR is of pointer type). CV qualifiers are preserved
232 from EXPR. */
234 tree
235 build_base_path (enum tree_code code,
236 tree expr,
237 tree binfo,
238 int nonnull,
239 tsubst_flags_t complain)
241 tree v_binfo = NULL_TREE;
242 tree d_binfo = NULL_TREE;
243 tree probe;
244 tree offset;
245 tree target_type;
246 tree null_test = NULL;
247 tree ptr_target_type;
248 int fixed_type_p;
249 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
250 bool has_empty = false;
251 bool virtual_access;
253 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
254 return error_mark_node;
256 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
258 d_binfo = probe;
259 if (is_empty_class (BINFO_TYPE (probe)))
260 has_empty = true;
261 if (!v_binfo && BINFO_VIRTUAL_P (probe))
262 v_binfo = probe;
265 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
266 if (want_pointer)
267 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
269 if (code == PLUS_EXPR
270 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
272 /* This can happen when adjust_result_of_qualified_name_lookup can't
273 find a unique base binfo in a call to a member function. We
274 couldn't give the diagnostic then since we might have been calling
275 a static member function, so we do it now. */
276 if (complain & tf_error)
278 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
279 ba_unique, NULL, complain);
280 gcc_assert (base == error_mark_node);
282 return error_mark_node;
285 gcc_assert ((code == MINUS_EXPR
286 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
287 || code == PLUS_EXPR);
289 if (binfo == d_binfo)
290 /* Nothing to do. */
291 return expr;
293 if (code == MINUS_EXPR && v_binfo)
295 if (complain & tf_error)
296 error ("cannot convert from base %qT to derived type %qT via "
297 "virtual base %qT", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
298 BINFO_TYPE (v_binfo));
299 return error_mark_node;
302 if (!want_pointer)
303 /* This must happen before the call to save_expr. */
304 expr = cp_build_addr_expr (expr, complain);
305 else
306 expr = mark_rvalue_use (expr);
308 offset = BINFO_OFFSET (binfo);
309 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
310 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
311 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
312 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
313 expression returned matches the input. */
314 target_type = cp_build_qualified_type
315 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
316 ptr_target_type = build_pointer_type (target_type);
318 /* Do we need to look in the vtable for the real offset? */
319 virtual_access = (v_binfo && fixed_type_p <= 0);
321 /* Don't bother with the calculations inside sizeof; they'll ICE if the
322 source type is incomplete and the pointer value doesn't matter. In a
323 template (even in fold_non_dependent_expr), we don't have vtables set
324 up properly yet, and the value doesn't matter there either; we're just
325 interested in the result of overload resolution. */
326 if (cp_unevaluated_operand != 0
327 || in_template_function ())
329 expr = build_nop (ptr_target_type, expr);
330 if (!want_pointer)
331 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
332 return expr;
335 /* If we're in an NSDMI, we don't have the full constructor context yet
336 that we need for converting to a virtual base, so just build a stub
337 CONVERT_EXPR and expand it later in bot_replace. */
338 if (virtual_access && fixed_type_p < 0
339 && current_scope () != current_function_decl)
341 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
342 CONVERT_EXPR_VBASE_PATH (expr) = true;
343 if (!want_pointer)
344 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL);
345 return expr;
348 /* Do we need to check for a null pointer? */
349 if (want_pointer && !nonnull)
351 /* If we know the conversion will not actually change the value
352 of EXPR, then we can avoid testing the expression for NULL.
353 We have to avoid generating a COMPONENT_REF for a base class
354 field, because other parts of the compiler know that such
355 expressions are always non-NULL. */
356 if (!virtual_access && integer_zerop (offset))
357 return build_nop (ptr_target_type, expr);
358 null_test = error_mark_node;
361 /* Protect against multiple evaluation if necessary. */
362 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
363 expr = save_expr (expr);
365 /* Now that we've saved expr, build the real null test. */
366 if (null_test)
368 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
369 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
370 expr, zero);
373 /* If this is a simple base reference, express it as a COMPONENT_REF. */
374 if (code == PLUS_EXPR && !virtual_access
375 /* We don't build base fields for empty bases, and they aren't very
376 interesting to the optimizers anyway. */
377 && !has_empty)
379 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
380 expr = build_simple_base_path (expr, binfo);
381 if (want_pointer)
382 expr = build_address (expr);
383 target_type = TREE_TYPE (expr);
384 goto out;
387 if (virtual_access)
389 /* Going via virtual base V_BINFO. We need the static offset
390 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
391 V_BINFO. That offset is an entry in D_BINFO's vtable. */
392 tree v_offset;
394 if (fixed_type_p < 0 && in_base_initializer)
396 /* In a base member initializer, we cannot rely on the
397 vtable being set up. We have to indirect via the
398 vtt_parm. */
399 tree t;
401 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
402 t = build_pointer_type (t);
403 v_offset = convert (t, current_vtt_parm);
404 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
406 else
407 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL,
408 complain),
409 TREE_TYPE (TREE_TYPE (expr)));
411 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
412 v_offset = build1 (NOP_EXPR,
413 build_pointer_type (ptrdiff_type_node),
414 v_offset);
415 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
416 TREE_CONSTANT (v_offset) = 1;
418 offset = convert_to_integer (ptrdiff_type_node,
419 size_diffop_loc (input_location, offset,
420 BINFO_OFFSET (v_binfo)));
422 if (!integer_zerop (offset))
423 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
425 if (fixed_type_p < 0)
426 /* Negative fixed_type_p means this is a constructor or destructor;
427 virtual base layout is fixed in in-charge [cd]tors, but not in
428 base [cd]tors. */
429 offset = build3 (COND_EXPR, ptrdiff_type_node,
430 build2 (EQ_EXPR, boolean_type_node,
431 current_in_charge_parm, integer_zero_node),
432 v_offset,
433 convert_to_integer (ptrdiff_type_node,
434 BINFO_OFFSET (binfo)));
435 else
436 offset = v_offset;
439 if (want_pointer)
440 target_type = ptr_target_type;
442 expr = build1 (NOP_EXPR, ptr_target_type, expr);
444 if (!integer_zerop (offset))
446 offset = fold_convert (sizetype, offset);
447 if (code == MINUS_EXPR)
448 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
449 expr = fold_build_pointer_plus (expr, offset);
451 else
452 null_test = NULL;
454 if (!want_pointer)
455 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
457 out:
458 if (null_test)
459 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
460 build_zero_cst (target_type));
462 return expr;
465 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
466 Perform a derived-to-base conversion by recursively building up a
467 sequence of COMPONENT_REFs to the appropriate base fields. */
469 static tree
470 build_simple_base_path (tree expr, tree binfo)
472 tree type = BINFO_TYPE (binfo);
473 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
474 tree field;
476 if (d_binfo == NULL_TREE)
478 tree temp;
480 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
482 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
483 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
484 an lvalue in the front end; only _DECLs and _REFs are lvalues
485 in the back end. */
486 temp = unary_complex_lvalue (ADDR_EXPR, expr);
487 if (temp)
488 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
490 return expr;
493 /* Recurse. */
494 expr = build_simple_base_path (expr, d_binfo);
496 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
497 field; field = DECL_CHAIN (field))
498 /* Is this the base field created by build_base_field? */
499 if (TREE_CODE (field) == FIELD_DECL
500 && DECL_FIELD_IS_BASE (field)
501 && TREE_TYPE (field) == type
502 /* If we're looking for a field in the most-derived class,
503 also check the field offset; we can have two base fields
504 of the same type if one is an indirect virtual base and one
505 is a direct non-virtual base. */
506 && (BINFO_INHERITANCE_CHAIN (d_binfo)
507 || tree_int_cst_equal (byte_position (field),
508 BINFO_OFFSET (binfo))))
510 /* We don't use build_class_member_access_expr here, as that
511 has unnecessary checks, and more importantly results in
512 recursive calls to dfs_walk_once. */
513 int type_quals = cp_type_quals (TREE_TYPE (expr));
515 expr = build3 (COMPONENT_REF,
516 cp_build_qualified_type (type, type_quals),
517 expr, field, NULL_TREE);
518 expr = fold_if_not_in_template (expr);
520 /* Mark the expression const or volatile, as appropriate.
521 Even though we've dealt with the type above, we still have
522 to mark the expression itself. */
523 if (type_quals & TYPE_QUAL_CONST)
524 TREE_READONLY (expr) = 1;
525 if (type_quals & TYPE_QUAL_VOLATILE)
526 TREE_THIS_VOLATILE (expr) = 1;
528 return expr;
531 /* Didn't find the base field?!? */
532 gcc_unreachable ();
535 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
536 type is a class type or a pointer to a class type. In the former
537 case, TYPE is also a class type; in the latter it is another
538 pointer type. If CHECK_ACCESS is true, an error message is emitted
539 if TYPE is inaccessible. If OBJECT has pointer type, the value is
540 assumed to be non-NULL. */
542 tree
543 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
544 tsubst_flags_t complain)
546 tree binfo;
547 tree object_type;
549 if (TYPE_PTR_P (TREE_TYPE (object)))
551 object_type = TREE_TYPE (TREE_TYPE (object));
552 type = TREE_TYPE (type);
554 else
555 object_type = TREE_TYPE (object);
557 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
558 NULL, complain);
559 if (!binfo || binfo == error_mark_node)
560 return error_mark_node;
562 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
565 /* EXPR is an expression with unqualified class type. BASE is a base
566 binfo of that class type. Returns EXPR, converted to the BASE
567 type. This function assumes that EXPR is the most derived class;
568 therefore virtual bases can be found at their static offsets. */
570 tree
571 convert_to_base_statically (tree expr, tree base)
573 tree expr_type;
575 expr_type = TREE_TYPE (expr);
576 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
578 /* If this is a non-empty base, use a COMPONENT_REF. */
579 if (!is_empty_class (BINFO_TYPE (base)))
580 return build_simple_base_path (expr, base);
582 /* We use fold_build2 and fold_convert below to simplify the trees
583 provided to the optimizers. It is not safe to call these functions
584 when processing a template because they do not handle C++-specific
585 trees. */
586 gcc_assert (!processing_template_decl);
587 expr = cp_build_addr_expr (expr, tf_warning_or_error);
588 if (!integer_zerop (BINFO_OFFSET (base)))
589 expr = fold_build_pointer_plus_loc (input_location,
590 expr, BINFO_OFFSET (base));
591 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
592 expr = build_fold_indirect_ref_loc (input_location, expr);
595 return expr;
599 tree
600 build_vfield_ref (tree datum, tree type)
602 tree vfield, vcontext;
604 if (datum == error_mark_node)
605 return error_mark_node;
607 /* First, convert to the requested type. */
608 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
609 datum = convert_to_base (datum, type, /*check_access=*/false,
610 /*nonnull=*/true, tf_warning_or_error);
612 /* Second, the requested type may not be the owner of its own vptr.
613 If not, convert to the base class that owns it. We cannot use
614 convert_to_base here, because VCONTEXT may appear more than once
615 in the inheritance hierarchy of TYPE, and thus direct conversion
616 between the types may be ambiguous. Following the path back up
617 one step at a time via primary bases avoids the problem. */
618 vfield = TYPE_VFIELD (type);
619 vcontext = DECL_CONTEXT (vfield);
620 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
622 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
623 type = TREE_TYPE (datum);
626 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
629 /* Given an object INSTANCE, return an expression which yields the
630 vtable element corresponding to INDEX. There are many special
631 cases for INSTANCE which we take care of here, mainly to avoid
632 creating extra tree nodes when we don't have to. */
634 static tree
635 build_vtbl_ref_1 (tree instance, tree idx)
637 tree aref;
638 tree vtbl = NULL_TREE;
640 /* Try to figure out what a reference refers to, and
641 access its virtual function table directly. */
643 int cdtorp = 0;
644 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
646 tree basetype = non_reference (TREE_TYPE (instance));
648 if (fixed_type && !cdtorp)
650 tree binfo = lookup_base (fixed_type, basetype,
651 ba_unique, NULL, tf_none);
652 if (binfo && binfo != error_mark_node)
653 vtbl = unshare_expr (BINFO_VTABLE (binfo));
656 if (!vtbl)
657 vtbl = build_vfield_ref (instance, basetype);
659 aref = build_array_ref (input_location, vtbl, idx);
660 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
662 return aref;
665 tree
666 build_vtbl_ref (tree instance, tree idx)
668 tree aref = build_vtbl_ref_1 (instance, idx);
670 return aref;
673 /* Given a stable object pointer INSTANCE_PTR, return an expression which
674 yields a function pointer corresponding to vtable element INDEX. */
676 tree
677 build_vfn_ref (tree instance_ptr, tree idx)
679 tree aref;
681 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
682 tf_warning_or_error),
683 idx);
685 /* When using function descriptors, the address of the
686 vtable entry is treated as a function pointer. */
687 if (TARGET_VTABLE_USES_DESCRIPTORS)
688 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
689 cp_build_addr_expr (aref, tf_warning_or_error));
691 /* Remember this as a method reference, for later devirtualization. */
692 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
694 return aref;
697 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
698 for the given TYPE. */
700 static tree
701 get_vtable_name (tree type)
703 return mangle_vtbl_for_type (type);
706 /* DECL is an entity associated with TYPE, like a virtual table or an
707 implicitly generated constructor. Determine whether or not DECL
708 should have external or internal linkage at the object file
709 level. This routine does not deal with COMDAT linkage and other
710 similar complexities; it simply sets TREE_PUBLIC if it possible for
711 entities in other translation units to contain copies of DECL, in
712 the abstract. */
714 void
715 set_linkage_according_to_type (tree /*type*/, tree decl)
717 TREE_PUBLIC (decl) = 1;
718 determine_visibility (decl);
721 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
722 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
723 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
725 static tree
726 build_vtable (tree class_type, tree name, tree vtable_type)
728 tree decl;
730 decl = build_lang_decl (VAR_DECL, name, vtable_type);
731 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
732 now to avoid confusion in mangle_decl. */
733 SET_DECL_ASSEMBLER_NAME (decl, name);
734 DECL_CONTEXT (decl) = class_type;
735 DECL_ARTIFICIAL (decl) = 1;
736 TREE_STATIC (decl) = 1;
737 TREE_READONLY (decl) = 1;
738 DECL_VIRTUAL_P (decl) = 1;
739 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
740 DECL_VTABLE_OR_VTT_P (decl) = 1;
741 /* At one time the vtable info was grabbed 2 words at a time. This
742 fails on sparc unless you have 8-byte alignment. (tiemann) */
743 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
744 DECL_ALIGN (decl));
745 set_linkage_according_to_type (class_type, decl);
746 /* The vtable has not been defined -- yet. */
747 DECL_EXTERNAL (decl) = 1;
748 DECL_NOT_REALLY_EXTERN (decl) = 1;
750 /* Mark the VAR_DECL node representing the vtable itself as a
751 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
752 is rather important that such things be ignored because any
753 effort to actually generate DWARF for them will run into
754 trouble when/if we encounter code like:
756 #pragma interface
757 struct S { virtual void member (); };
759 because the artificial declaration of the vtable itself (as
760 manufactured by the g++ front end) will say that the vtable is
761 a static member of `S' but only *after* the debug output for
762 the definition of `S' has already been output. This causes
763 grief because the DWARF entry for the definition of the vtable
764 will try to refer back to an earlier *declaration* of the
765 vtable as a static member of `S' and there won't be one. We
766 might be able to arrange to have the "vtable static member"
767 attached to the member list for `S' before the debug info for
768 `S' get written (which would solve the problem) but that would
769 require more intrusive changes to the g++ front end. */
770 DECL_IGNORED_P (decl) = 1;
772 return decl;
775 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
776 or even complete. If this does not exist, create it. If COMPLETE is
777 nonzero, then complete the definition of it -- that will render it
778 impossible to actually build the vtable, but is useful to get at those
779 which are known to exist in the runtime. */
781 tree
782 get_vtable_decl (tree type, int complete)
784 tree decl;
786 if (CLASSTYPE_VTABLES (type))
787 return CLASSTYPE_VTABLES (type);
789 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
790 CLASSTYPE_VTABLES (type) = decl;
792 if (complete)
794 DECL_EXTERNAL (decl) = 1;
795 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
798 return decl;
801 /* Build the primary virtual function table for TYPE. If BINFO is
802 non-NULL, build the vtable starting with the initial approximation
803 that it is the same as the one which is the head of the association
804 list. Returns a nonzero value if a new vtable is actually
805 created. */
807 static int
808 build_primary_vtable (tree binfo, tree type)
810 tree decl;
811 tree virtuals;
813 decl = get_vtable_decl (type, /*complete=*/0);
815 if (binfo)
817 if (BINFO_NEW_VTABLE_MARKED (binfo))
818 /* We have already created a vtable for this base, so there's
819 no need to do it again. */
820 return 0;
822 virtuals = copy_list (BINFO_VIRTUALS (binfo));
823 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
824 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
825 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
827 else
829 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
830 virtuals = NULL_TREE;
833 if (GATHER_STATISTICS)
835 n_vtables += 1;
836 n_vtable_elems += list_length (virtuals);
839 /* Initialize the association list for this type, based
840 on our first approximation. */
841 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
842 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
843 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
844 return 1;
847 /* Give BINFO a new virtual function table which is initialized
848 with a skeleton-copy of its original initialization. The only
849 entry that changes is the `delta' entry, so we can really
850 share a lot of structure.
852 FOR_TYPE is the most derived type which caused this table to
853 be needed.
855 Returns nonzero if we haven't met BINFO before.
857 The order in which vtables are built (by calling this function) for
858 an object must remain the same, otherwise a binary incompatibility
859 can result. */
861 static int
862 build_secondary_vtable (tree binfo)
864 if (BINFO_NEW_VTABLE_MARKED (binfo))
865 /* We already created a vtable for this base. There's no need to
866 do it again. */
867 return 0;
869 /* Remember that we've created a vtable for this BINFO, so that we
870 don't try to do so again. */
871 SET_BINFO_NEW_VTABLE_MARKED (binfo);
873 /* Make fresh virtual list, so we can smash it later. */
874 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
876 /* Secondary vtables are laid out as part of the same structure as
877 the primary vtable. */
878 BINFO_VTABLE (binfo) = NULL_TREE;
879 return 1;
882 /* Create a new vtable for BINFO which is the hierarchy dominated by
883 T. Return nonzero if we actually created a new vtable. */
885 static int
886 make_new_vtable (tree t, tree binfo)
888 if (binfo == TYPE_BINFO (t))
889 /* In this case, it is *type*'s vtable we are modifying. We start
890 with the approximation that its vtable is that of the
891 immediate base class. */
892 return build_primary_vtable (binfo, t);
893 else
894 /* This is our very own copy of `basetype' to play with. Later,
895 we will fill in all the virtual functions that override the
896 virtual functions in these base classes which are not defined
897 by the current type. */
898 return build_secondary_vtable (binfo);
901 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
902 (which is in the hierarchy dominated by T) list FNDECL as its
903 BV_FN. DELTA is the required constant adjustment from the `this'
904 pointer where the vtable entry appears to the `this' required when
905 the function is actually called. */
907 static void
908 modify_vtable_entry (tree t,
909 tree binfo,
910 tree fndecl,
911 tree delta,
912 tree *virtuals)
914 tree v;
916 v = *virtuals;
918 if (fndecl != BV_FN (v)
919 || !tree_int_cst_equal (delta, BV_DELTA (v)))
921 /* We need a new vtable for BINFO. */
922 if (make_new_vtable (t, binfo))
924 /* If we really did make a new vtable, we also made a copy
925 of the BINFO_VIRTUALS list. Now, we have to find the
926 corresponding entry in that list. */
927 *virtuals = BINFO_VIRTUALS (binfo);
928 while (BV_FN (*virtuals) != BV_FN (v))
929 *virtuals = TREE_CHAIN (*virtuals);
930 v = *virtuals;
933 BV_DELTA (v) = delta;
934 BV_VCALL_INDEX (v) = NULL_TREE;
935 BV_FN (v) = fndecl;
940 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
941 the USING_DECL naming METHOD. Returns true if the method could be
942 added to the method vec. */
944 bool
945 add_method (tree type, tree method, tree using_decl)
947 unsigned slot;
948 tree overload;
949 bool template_conv_p = false;
950 bool conv_p;
951 VEC(tree,gc) *method_vec;
952 bool complete_p;
953 bool insert_p = false;
954 tree current_fns;
955 tree fns;
957 if (method == error_mark_node)
958 return false;
960 complete_p = COMPLETE_TYPE_P (type);
961 conv_p = DECL_CONV_FN_P (method);
962 if (conv_p)
963 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
964 && DECL_TEMPLATE_CONV_FN_P (method));
966 method_vec = CLASSTYPE_METHOD_VEC (type);
967 if (!method_vec)
969 /* Make a new method vector. We start with 8 entries. We must
970 allocate at least two (for constructors and destructors), and
971 we're going to end up with an assignment operator at some
972 point as well. */
973 method_vec = VEC_alloc (tree, gc, 8);
974 /* Create slots for constructors and destructors. */
975 VEC_quick_push (tree, method_vec, NULL_TREE);
976 VEC_quick_push (tree, method_vec, NULL_TREE);
977 CLASSTYPE_METHOD_VEC (type) = method_vec;
980 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
981 grok_special_member_properties (method);
983 /* Constructors and destructors go in special slots. */
984 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
985 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
986 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
988 slot = CLASSTYPE_DESTRUCTOR_SLOT;
990 if (TYPE_FOR_JAVA (type))
992 if (!DECL_ARTIFICIAL (method))
993 error ("Java class %qT cannot have a destructor", type);
994 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
995 error ("Java class %qT cannot have an implicit non-trivial "
996 "destructor",
997 type);
1000 else
1002 tree m;
1004 insert_p = true;
1005 /* See if we already have an entry with this name. */
1006 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1007 VEC_iterate (tree, method_vec, slot, m);
1008 ++slot)
1010 m = OVL_CURRENT (m);
1011 if (template_conv_p)
1013 if (TREE_CODE (m) == TEMPLATE_DECL
1014 && DECL_TEMPLATE_CONV_FN_P (m))
1015 insert_p = false;
1016 break;
1018 if (conv_p && !DECL_CONV_FN_P (m))
1019 break;
1020 if (DECL_NAME (m) == DECL_NAME (method))
1022 insert_p = false;
1023 break;
1025 if (complete_p
1026 && !DECL_CONV_FN_P (m)
1027 && DECL_NAME (m) > DECL_NAME (method))
1028 break;
1031 current_fns = insert_p ? NULL_TREE : VEC_index (tree, method_vec, slot);
1033 /* Check to see if we've already got this method. */
1034 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1036 tree fn = OVL_CURRENT (fns);
1037 tree fn_type;
1038 tree method_type;
1039 tree parms1;
1040 tree parms2;
1042 if (TREE_CODE (fn) != TREE_CODE (method))
1043 continue;
1045 /* [over.load] Member function declarations with the
1046 same name and the same parameter types cannot be
1047 overloaded if any of them is a static member
1048 function declaration.
1050 [namespace.udecl] When a using-declaration brings names
1051 from a base class into a derived class scope, member
1052 functions in the derived class override and/or hide member
1053 functions with the same name and parameter types in a base
1054 class (rather than conflicting). */
1055 fn_type = TREE_TYPE (fn);
1056 method_type = TREE_TYPE (method);
1057 parms1 = TYPE_ARG_TYPES (fn_type);
1058 parms2 = TYPE_ARG_TYPES (method_type);
1060 /* Compare the quals on the 'this' parm. Don't compare
1061 the whole types, as used functions are treated as
1062 coming from the using class in overload resolution. */
1063 if (! DECL_STATIC_FUNCTION_P (fn)
1064 && ! DECL_STATIC_FUNCTION_P (method)
1065 && TREE_TYPE (TREE_VALUE (parms1)) != error_mark_node
1066 && TREE_TYPE (TREE_VALUE (parms2)) != error_mark_node
1067 && (cp_type_quals (TREE_TYPE (TREE_VALUE (parms1)))
1068 != cp_type_quals (TREE_TYPE (TREE_VALUE (parms2)))))
1069 continue;
1071 /* For templates, the return type and template parameters
1072 must be identical. */
1073 if (TREE_CODE (fn) == TEMPLATE_DECL
1074 && (!same_type_p (TREE_TYPE (fn_type),
1075 TREE_TYPE (method_type))
1076 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1077 DECL_TEMPLATE_PARMS (method))))
1078 continue;
1080 if (! DECL_STATIC_FUNCTION_P (fn))
1081 parms1 = TREE_CHAIN (parms1);
1082 if (! DECL_STATIC_FUNCTION_P (method))
1083 parms2 = TREE_CHAIN (parms2);
1085 if (compparms (parms1, parms2)
1086 && (!DECL_CONV_FN_P (fn)
1087 || same_type_p (TREE_TYPE (fn_type),
1088 TREE_TYPE (method_type))))
1090 if (DECL_INHERITED_CTOR_BASE (method))
1092 if (DECL_INHERITED_CTOR_BASE (fn))
1094 error_at (DECL_SOURCE_LOCATION (method),
1095 "%q#D inherited from %qT", method,
1096 DECL_INHERITED_CTOR_BASE (method));
1097 error_at (DECL_SOURCE_LOCATION (fn),
1098 "conflicts with version inherited from %qT",
1099 DECL_INHERITED_CTOR_BASE (fn));
1101 /* Otherwise defer to the other function. */
1102 return false;
1104 if (using_decl)
1106 if (DECL_CONTEXT (fn) == type)
1107 /* Defer to the local function. */
1108 return false;
1110 else
1112 error ("%q+#D cannot be overloaded", method);
1113 error ("with %q+#D", fn);
1116 /* We don't call duplicate_decls here to merge the
1117 declarations because that will confuse things if the
1118 methods have inline definitions. In particular, we
1119 will crash while processing the definitions. */
1120 return false;
1124 /* A class should never have more than one destructor. */
1125 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1126 return false;
1128 /* Add the new binding. */
1129 if (using_decl)
1131 overload = ovl_cons (method, current_fns);
1132 OVL_USED (overload) = true;
1134 else
1135 overload = build_overload (method, current_fns);
1137 if (conv_p)
1138 TYPE_HAS_CONVERSION (type) = 1;
1139 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1140 push_class_level_binding (DECL_NAME (method), overload);
1142 if (insert_p)
1144 bool reallocated;
1146 /* We only expect to add few methods in the COMPLETE_P case, so
1147 just make room for one more method in that case. */
1148 if (complete_p)
1149 reallocated = VEC_reserve_exact (tree, gc, method_vec, 1);
1150 else
1151 reallocated = VEC_reserve (tree, gc, method_vec, 1);
1152 if (reallocated)
1153 CLASSTYPE_METHOD_VEC (type) = method_vec;
1154 if (slot == VEC_length (tree, method_vec))
1155 VEC_quick_push (tree, method_vec, overload);
1156 else
1157 VEC_quick_insert (tree, method_vec, slot, overload);
1159 else
1160 /* Replace the current slot. */
1161 VEC_replace (tree, method_vec, slot, overload);
1162 return true;
1165 /* Subroutines of finish_struct. */
1167 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1168 legit, otherwise return 0. */
1170 static int
1171 alter_access (tree t, tree fdecl, tree access)
1173 tree elem;
1175 if (!DECL_LANG_SPECIFIC (fdecl))
1176 retrofit_lang_decl (fdecl);
1178 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1180 elem = purpose_member (t, DECL_ACCESS (fdecl));
1181 if (elem)
1183 if (TREE_VALUE (elem) != access)
1185 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1186 error ("conflicting access specifications for method"
1187 " %q+D, ignored", TREE_TYPE (fdecl));
1188 else
1189 error ("conflicting access specifications for field %qE, ignored",
1190 DECL_NAME (fdecl));
1192 else
1194 /* They're changing the access to the same thing they changed
1195 it to before. That's OK. */
1199 else
1201 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1202 tf_warning_or_error);
1203 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1204 return 1;
1206 return 0;
1209 /* Process the USING_DECL, which is a member of T. */
1211 static void
1212 handle_using_decl (tree using_decl, tree t)
1214 tree decl = USING_DECL_DECLS (using_decl);
1215 tree name = DECL_NAME (using_decl);
1216 tree access
1217 = TREE_PRIVATE (using_decl) ? access_private_node
1218 : TREE_PROTECTED (using_decl) ? access_protected_node
1219 : access_public_node;
1220 tree flist = NULL_TREE;
1221 tree old_value;
1223 gcc_assert (!processing_template_decl && decl);
1225 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1226 tf_warning_or_error);
1227 if (old_value)
1229 if (is_overloaded_fn (old_value))
1230 old_value = OVL_CURRENT (old_value);
1232 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1233 /* OK */;
1234 else
1235 old_value = NULL_TREE;
1238 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
1240 if (is_overloaded_fn (decl))
1241 flist = decl;
1243 if (! old_value)
1245 else if (is_overloaded_fn (old_value))
1247 if (flist)
1248 /* It's OK to use functions from a base when there are functions with
1249 the same name already present in the current class. */;
1250 else
1252 error ("%q+D invalid in %q#T", using_decl, t);
1253 error (" because of local method %q+#D with same name",
1254 OVL_CURRENT (old_value));
1255 return;
1258 else if (!DECL_ARTIFICIAL (old_value))
1260 error ("%q+D invalid in %q#T", using_decl, t);
1261 error (" because of local member %q+#D with same name", old_value);
1262 return;
1265 /* Make type T see field decl FDECL with access ACCESS. */
1266 if (flist)
1267 for (; flist; flist = OVL_NEXT (flist))
1269 add_method (t, OVL_CURRENT (flist), using_decl);
1270 alter_access (t, OVL_CURRENT (flist), access);
1272 else
1273 alter_access (t, decl, access);
1276 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1277 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1278 properties of the bases. */
1280 static void
1281 check_bases (tree t,
1282 int* cant_have_const_ctor_p,
1283 int* no_const_asn_ref_p)
1285 int i;
1286 bool seen_non_virtual_nearly_empty_base_p = 0;
1287 int seen_tm_mask = 0;
1288 tree base_binfo;
1289 tree binfo;
1290 tree field = NULL_TREE;
1292 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1293 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1294 if (TREE_CODE (field) == FIELD_DECL)
1295 break;
1297 for (binfo = TYPE_BINFO (t), i = 0;
1298 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1300 tree basetype = TREE_TYPE (base_binfo);
1302 gcc_assert (COMPLETE_TYPE_P (basetype));
1304 if (CLASSTYPE_FINAL (basetype))
1305 error ("cannot derive from %<final%> base %qT in derived type %qT",
1306 basetype, t);
1308 /* If any base class is non-literal, so is the derived class. */
1309 if (!CLASSTYPE_LITERAL_P (basetype))
1310 CLASSTYPE_LITERAL_P (t) = false;
1312 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1313 here because the case of virtual functions but non-virtual
1314 dtor is handled in finish_struct_1. */
1315 if (!TYPE_POLYMORPHIC_P (basetype))
1316 warning (OPT_Weffc__,
1317 "base class %q#T has a non-virtual destructor", basetype);
1319 /* If the base class doesn't have copy constructors or
1320 assignment operators that take const references, then the
1321 derived class cannot have such a member automatically
1322 generated. */
1323 if (TYPE_HAS_COPY_CTOR (basetype)
1324 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1325 *cant_have_const_ctor_p = 1;
1326 if (TYPE_HAS_COPY_ASSIGN (basetype)
1327 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1328 *no_const_asn_ref_p = 1;
1330 if (BINFO_VIRTUAL_P (base_binfo))
1331 /* A virtual base does not effect nearly emptiness. */
1333 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1335 if (seen_non_virtual_nearly_empty_base_p)
1336 /* And if there is more than one nearly empty base, then the
1337 derived class is not nearly empty either. */
1338 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1339 else
1340 /* Remember we've seen one. */
1341 seen_non_virtual_nearly_empty_base_p = 1;
1343 else if (!is_empty_class (basetype))
1344 /* If the base class is not empty or nearly empty, then this
1345 class cannot be nearly empty. */
1346 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1348 /* A lot of properties from the bases also apply to the derived
1349 class. */
1350 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1351 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1352 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1353 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1354 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1355 || !TYPE_HAS_COPY_ASSIGN (basetype));
1356 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1357 || !TYPE_HAS_COPY_CTOR (basetype));
1358 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1359 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1360 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1361 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1362 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1363 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1364 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1365 || TYPE_HAS_COMPLEX_DFLT (basetype));
1367 /* A standard-layout class is a class that:
1369 * has no non-standard-layout base classes, */
1370 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1371 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1373 tree basefield;
1374 /* ...has no base classes of the same type as the first non-static
1375 data member... */
1376 if (field && DECL_CONTEXT (field) == t
1377 && (same_type_ignoring_top_level_qualifiers_p
1378 (TREE_TYPE (field), basetype)))
1379 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1380 else
1381 /* ...either has no non-static data members in the most-derived
1382 class and at most one base class with non-static data
1383 members, or has no base classes with non-static data
1384 members */
1385 for (basefield = TYPE_FIELDS (basetype); basefield;
1386 basefield = DECL_CHAIN (basefield))
1387 if (TREE_CODE (basefield) == FIELD_DECL)
1389 if (field)
1390 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1391 else
1392 field = basefield;
1393 break;
1397 /* Don't bother collecting tm attributes if transactional memory
1398 support is not enabled. */
1399 if (flag_tm)
1401 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1402 if (tm_attr)
1403 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1407 /* If one of the base classes had TM attributes, and the current class
1408 doesn't define its own, then the current class inherits one. */
1409 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1411 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1412 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1416 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1417 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1418 that have had a nearly-empty virtual primary base stolen by some
1419 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1420 T. */
1422 static void
1423 determine_primary_bases (tree t)
1425 unsigned i;
1426 tree primary = NULL_TREE;
1427 tree type_binfo = TYPE_BINFO (t);
1428 tree base_binfo;
1430 /* Determine the primary bases of our bases. */
1431 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1432 base_binfo = TREE_CHAIN (base_binfo))
1434 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1436 /* See if we're the non-virtual primary of our inheritance
1437 chain. */
1438 if (!BINFO_VIRTUAL_P (base_binfo))
1440 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1441 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1443 if (parent_primary
1444 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1445 BINFO_TYPE (parent_primary)))
1446 /* We are the primary binfo. */
1447 BINFO_PRIMARY_P (base_binfo) = 1;
1449 /* Determine if we have a virtual primary base, and mark it so.
1451 if (primary && BINFO_VIRTUAL_P (primary))
1453 tree this_primary = copied_binfo (primary, base_binfo);
1455 if (BINFO_PRIMARY_P (this_primary))
1456 /* Someone already claimed this base. */
1457 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1458 else
1460 tree delta;
1462 BINFO_PRIMARY_P (this_primary) = 1;
1463 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1465 /* A virtual binfo might have been copied from within
1466 another hierarchy. As we're about to use it as a
1467 primary base, make sure the offsets match. */
1468 delta = size_diffop_loc (input_location,
1469 convert (ssizetype,
1470 BINFO_OFFSET (base_binfo)),
1471 convert (ssizetype,
1472 BINFO_OFFSET (this_primary)));
1474 propagate_binfo_offsets (this_primary, delta);
1479 /* First look for a dynamic direct non-virtual base. */
1480 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1482 tree basetype = BINFO_TYPE (base_binfo);
1484 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1486 primary = base_binfo;
1487 goto found;
1491 /* A "nearly-empty" virtual base class can be the primary base
1492 class, if no non-virtual polymorphic base can be found. Look for
1493 a nearly-empty virtual dynamic base that is not already a primary
1494 base of something in the hierarchy. If there is no such base,
1495 just pick the first nearly-empty virtual base. */
1497 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1498 base_binfo = TREE_CHAIN (base_binfo))
1499 if (BINFO_VIRTUAL_P (base_binfo)
1500 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1502 if (!BINFO_PRIMARY_P (base_binfo))
1504 /* Found one that is not primary. */
1505 primary = base_binfo;
1506 goto found;
1508 else if (!primary)
1509 /* Remember the first candidate. */
1510 primary = base_binfo;
1513 found:
1514 /* If we've got a primary base, use it. */
1515 if (primary)
1517 tree basetype = BINFO_TYPE (primary);
1519 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1520 if (BINFO_PRIMARY_P (primary))
1521 /* We are stealing a primary base. */
1522 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1523 BINFO_PRIMARY_P (primary) = 1;
1524 if (BINFO_VIRTUAL_P (primary))
1526 tree delta;
1528 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1529 /* A virtual binfo might have been copied from within
1530 another hierarchy. As we're about to use it as a primary
1531 base, make sure the offsets match. */
1532 delta = size_diffop_loc (input_location, ssize_int (0),
1533 convert (ssizetype, BINFO_OFFSET (primary)));
1535 propagate_binfo_offsets (primary, delta);
1538 primary = TYPE_BINFO (basetype);
1540 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1541 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1542 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1546 /* Update the variant types of T. */
1548 void
1549 fixup_type_variants (tree t)
1551 tree variants;
1553 if (!t)
1554 return;
1556 for (variants = TYPE_NEXT_VARIANT (t);
1557 variants;
1558 variants = TYPE_NEXT_VARIANT (variants))
1560 /* These fields are in the _TYPE part of the node, not in
1561 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1562 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1563 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1564 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1565 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1567 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1569 TYPE_BINFO (variants) = TYPE_BINFO (t);
1571 /* Copy whatever these are holding today. */
1572 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1573 TYPE_METHODS (variants) = TYPE_METHODS (t);
1574 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1578 /* Early variant fixups: we apply attributes at the beginning of the class
1579 definition, and we need to fix up any variants that have already been
1580 made via elaborated-type-specifier so that check_qualified_type works. */
1582 void
1583 fixup_attribute_variants (tree t)
1585 tree variants;
1587 if (!t)
1588 return;
1590 for (variants = TYPE_NEXT_VARIANT (t);
1591 variants;
1592 variants = TYPE_NEXT_VARIANT (variants))
1594 /* These are the two fields that check_qualified_type looks at and
1595 are affected by attributes. */
1596 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
1597 TYPE_ALIGN (variants) = TYPE_ALIGN (t);
1601 /* Set memoizing fields and bits of T (and its variants) for later
1602 use. */
1604 static void
1605 finish_struct_bits (tree t)
1607 /* Fix up variants (if any). */
1608 fixup_type_variants (t);
1610 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1611 /* For a class w/o baseclasses, 'finish_struct' has set
1612 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
1613 Similarly for a class whose base classes do not have vtables.
1614 When neither of these is true, we might have removed abstract
1615 virtuals (by providing a definition), added some (by declaring
1616 new ones), or redeclared ones from a base class. We need to
1617 recalculate what's really an abstract virtual at this point (by
1618 looking in the vtables). */
1619 get_pure_virtuals (t);
1621 /* If this type has a copy constructor or a destructor, force its
1622 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
1623 nonzero. This will cause it to be passed by invisible reference
1624 and prevent it from being returned in a register. */
1625 if (type_has_nontrivial_copy_init (t)
1626 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1628 tree variants;
1629 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1630 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1632 SET_TYPE_MODE (variants, BLKmode);
1633 TREE_ADDRESSABLE (variants) = 1;
1638 /* Issue warnings about T having private constructors, but no friends,
1639 and so forth.
1641 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1642 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1643 non-private static member functions. */
1645 static void
1646 maybe_warn_about_overly_private_class (tree t)
1648 int has_member_fn = 0;
1649 int has_nonprivate_method = 0;
1650 tree fn;
1652 if (!warn_ctor_dtor_privacy
1653 /* If the class has friends, those entities might create and
1654 access instances, so we should not warn. */
1655 || (CLASSTYPE_FRIEND_CLASSES (t)
1656 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1657 /* We will have warned when the template was declared; there's
1658 no need to warn on every instantiation. */
1659 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1660 /* There's no reason to even consider warning about this
1661 class. */
1662 return;
1664 /* We only issue one warning, if more than one applies, because
1665 otherwise, on code like:
1667 class A {
1668 // Oops - forgot `public:'
1669 A();
1670 A(const A&);
1671 ~A();
1674 we warn several times about essentially the same problem. */
1676 /* Check to see if all (non-constructor, non-destructor) member
1677 functions are private. (Since there are no friends or
1678 non-private statics, we can't ever call any of the private member
1679 functions.) */
1680 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1681 /* We're not interested in compiler-generated methods; they don't
1682 provide any way to call private members. */
1683 if (!DECL_ARTIFICIAL (fn))
1685 if (!TREE_PRIVATE (fn))
1687 if (DECL_STATIC_FUNCTION_P (fn))
1688 /* A non-private static member function is just like a
1689 friend; it can create and invoke private member
1690 functions, and be accessed without a class
1691 instance. */
1692 return;
1694 has_nonprivate_method = 1;
1695 /* Keep searching for a static member function. */
1697 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1698 has_member_fn = 1;
1701 if (!has_nonprivate_method && has_member_fn)
1703 /* There are no non-private methods, and there's at least one
1704 private member function that isn't a constructor or
1705 destructor. (If all the private members are
1706 constructors/destructors we want to use the code below that
1707 issues error messages specifically referring to
1708 constructors/destructors.) */
1709 unsigned i;
1710 tree binfo = TYPE_BINFO (t);
1712 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
1713 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
1715 has_nonprivate_method = 1;
1716 break;
1718 if (!has_nonprivate_method)
1720 warning (OPT_Wctor_dtor_privacy,
1721 "all member functions in class %qT are private", t);
1722 return;
1726 /* Even if some of the member functions are non-private, the class
1727 won't be useful for much if all the constructors or destructors
1728 are private: such an object can never be created or destroyed. */
1729 fn = CLASSTYPE_DESTRUCTORS (t);
1730 if (fn && TREE_PRIVATE (fn))
1732 warning (OPT_Wctor_dtor_privacy,
1733 "%q#T only defines a private destructor and has no friends",
1735 return;
1738 /* Warn about classes that have private constructors and no friends. */
1739 if (TYPE_HAS_USER_CONSTRUCTOR (t)
1740 /* Implicitly generated constructors are always public. */
1741 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
1742 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
1744 int nonprivate_ctor = 0;
1746 /* If a non-template class does not define a copy
1747 constructor, one is defined for it, enabling it to avoid
1748 this warning. For a template class, this does not
1749 happen, and so we would normally get a warning on:
1751 template <class T> class C { private: C(); };
1753 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
1754 complete non-template or fully instantiated classes have this
1755 flag set. */
1756 if (!TYPE_HAS_COPY_CTOR (t))
1757 nonprivate_ctor = 1;
1758 else
1759 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
1761 tree ctor = OVL_CURRENT (fn);
1762 /* Ideally, we wouldn't count copy constructors (or, in
1763 fact, any constructor that takes an argument of the
1764 class type as a parameter) because such things cannot
1765 be used to construct an instance of the class unless
1766 you already have one. But, for now at least, we're
1767 more generous. */
1768 if (! TREE_PRIVATE (ctor))
1770 nonprivate_ctor = 1;
1771 break;
1775 if (nonprivate_ctor == 0)
1777 warning (OPT_Wctor_dtor_privacy,
1778 "%q#T only defines private constructors and has no friends",
1780 return;
1785 static struct {
1786 gt_pointer_operator new_value;
1787 void *cookie;
1788 } resort_data;
1790 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1792 static int
1793 method_name_cmp (const void* m1_p, const void* m2_p)
1795 const tree *const m1 = (const tree *) m1_p;
1796 const tree *const m2 = (const tree *) m2_p;
1798 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1799 return 0;
1800 if (*m1 == NULL_TREE)
1801 return -1;
1802 if (*m2 == NULL_TREE)
1803 return 1;
1804 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1805 return -1;
1806 return 1;
1809 /* This routine compares two fields like method_name_cmp but using the
1810 pointer operator in resort_field_decl_data. */
1812 static int
1813 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1815 const tree *const m1 = (const tree *) m1_p;
1816 const tree *const m2 = (const tree *) m2_p;
1817 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1818 return 0;
1819 if (*m1 == NULL_TREE)
1820 return -1;
1821 if (*m2 == NULL_TREE)
1822 return 1;
1824 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1825 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1826 resort_data.new_value (&d1, resort_data.cookie);
1827 resort_data.new_value (&d2, resort_data.cookie);
1828 if (d1 < d2)
1829 return -1;
1831 return 1;
1834 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1836 void
1837 resort_type_method_vec (void* obj,
1838 void* /*orig_obj*/,
1839 gt_pointer_operator new_value,
1840 void* cookie)
1842 VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj;
1843 int len = VEC_length (tree, method_vec);
1844 size_t slot;
1845 tree fn;
1847 /* The type conversion ops have to live at the front of the vec, so we
1848 can't sort them. */
1849 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1850 VEC_iterate (tree, method_vec, slot, fn);
1851 ++slot)
1852 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1853 break;
1855 if (len - slot > 1)
1857 resort_data.new_value = new_value;
1858 resort_data.cookie = cookie;
1859 qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree),
1860 resort_method_name_cmp);
1864 /* Warn about duplicate methods in fn_fields.
1866 Sort methods that are not special (i.e., constructors, destructors,
1867 and type conversion operators) so that we can find them faster in
1868 search. */
1870 static void
1871 finish_struct_methods (tree t)
1873 tree fn_fields;
1874 VEC(tree,gc) *method_vec;
1875 int slot, len;
1877 method_vec = CLASSTYPE_METHOD_VEC (t);
1878 if (!method_vec)
1879 return;
1881 len = VEC_length (tree, method_vec);
1883 /* Clear DECL_IN_AGGR_P for all functions. */
1884 for (fn_fields = TYPE_METHODS (t); fn_fields;
1885 fn_fields = DECL_CHAIN (fn_fields))
1886 DECL_IN_AGGR_P (fn_fields) = 0;
1888 /* Issue warnings about private constructors and such. If there are
1889 no methods, then some public defaults are generated. */
1890 maybe_warn_about_overly_private_class (t);
1892 /* The type conversion ops have to live at the front of the vec, so we
1893 can't sort them. */
1894 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1895 VEC_iterate (tree, method_vec, slot, fn_fields);
1896 ++slot)
1897 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
1898 break;
1899 if (len - slot > 1)
1900 qsort (VEC_address (tree, method_vec) + slot,
1901 len-slot, sizeof (tree), method_name_cmp);
1904 /* Make BINFO's vtable have N entries, including RTTI entries,
1905 vbase and vcall offsets, etc. Set its type and call the back end
1906 to lay it out. */
1908 static void
1909 layout_vtable_decl (tree binfo, int n)
1911 tree atype;
1912 tree vtable;
1914 atype = build_array_of_n_type (vtable_entry_type, n);
1915 layout_type (atype);
1917 /* We may have to grow the vtable. */
1918 vtable = get_vtbl_decl_for_binfo (binfo);
1919 if (!same_type_p (TREE_TYPE (vtable), atype))
1921 TREE_TYPE (vtable) = atype;
1922 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
1923 layout_decl (vtable, 0);
1927 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
1928 have the same signature. */
1931 same_signature_p (const_tree fndecl, const_tree base_fndecl)
1933 /* One destructor overrides another if they are the same kind of
1934 destructor. */
1935 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
1936 && special_function_p (base_fndecl) == special_function_p (fndecl))
1937 return 1;
1938 /* But a non-destructor never overrides a destructor, nor vice
1939 versa, nor do different kinds of destructors override
1940 one-another. For example, a complete object destructor does not
1941 override a deleting destructor. */
1942 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
1943 return 0;
1945 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
1946 || (DECL_CONV_FN_P (fndecl)
1947 && DECL_CONV_FN_P (base_fndecl)
1948 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
1949 DECL_CONV_FN_TYPE (base_fndecl))))
1951 tree types, base_types;
1952 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1953 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
1954 if ((cp_type_quals (TREE_TYPE (TREE_VALUE (base_types)))
1955 == cp_type_quals (TREE_TYPE (TREE_VALUE (types))))
1956 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
1957 return 1;
1959 return 0;
1962 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
1963 subobject. */
1965 static bool
1966 base_derived_from (tree derived, tree base)
1968 tree probe;
1970 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
1972 if (probe == derived)
1973 return true;
1974 else if (BINFO_VIRTUAL_P (probe))
1975 /* If we meet a virtual base, we can't follow the inheritance
1976 any more. See if the complete type of DERIVED contains
1977 such a virtual base. */
1978 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
1979 != NULL_TREE);
1981 return false;
1984 typedef struct find_final_overrider_data_s {
1985 /* The function for which we are trying to find a final overrider. */
1986 tree fn;
1987 /* The base class in which the function was declared. */
1988 tree declaring_base;
1989 /* The candidate overriders. */
1990 tree candidates;
1991 /* Path to most derived. */
1992 VEC(tree,heap) *path;
1993 } find_final_overrider_data;
1995 /* Add the overrider along the current path to FFOD->CANDIDATES.
1996 Returns true if an overrider was found; false otherwise. */
1998 static bool
1999 dfs_find_final_overrider_1 (tree binfo,
2000 find_final_overrider_data *ffod,
2001 unsigned depth)
2003 tree method;
2005 /* If BINFO is not the most derived type, try a more derived class.
2006 A definition there will overrider a definition here. */
2007 if (depth)
2009 depth--;
2010 if (dfs_find_final_overrider_1
2011 (VEC_index (tree, ffod->path, depth), ffod, depth))
2012 return true;
2015 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2016 if (method)
2018 tree *candidate = &ffod->candidates;
2020 /* Remove any candidates overridden by this new function. */
2021 while (*candidate)
2023 /* If *CANDIDATE overrides METHOD, then METHOD
2024 cannot override anything else on the list. */
2025 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2026 return true;
2027 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2028 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2029 *candidate = TREE_CHAIN (*candidate);
2030 else
2031 candidate = &TREE_CHAIN (*candidate);
2034 /* Add the new function. */
2035 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2036 return true;
2039 return false;
2042 /* Called from find_final_overrider via dfs_walk. */
2044 static tree
2045 dfs_find_final_overrider_pre (tree binfo, void *data)
2047 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2049 if (binfo == ffod->declaring_base)
2050 dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path));
2051 VEC_safe_push (tree, heap, ffod->path, binfo);
2053 return NULL_TREE;
2056 static tree
2057 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2059 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2060 VEC_pop (tree, ffod->path);
2062 return NULL_TREE;
2065 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2066 FN and whose TREE_VALUE is the binfo for the base where the
2067 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2068 DERIVED) is the base object in which FN is declared. */
2070 static tree
2071 find_final_overrider (tree derived, tree binfo, tree fn)
2073 find_final_overrider_data ffod;
2075 /* Getting this right is a little tricky. This is valid:
2077 struct S { virtual void f (); };
2078 struct T { virtual void f (); };
2079 struct U : public S, public T { };
2081 even though calling `f' in `U' is ambiguous. But,
2083 struct R { virtual void f(); };
2084 struct S : virtual public R { virtual void f (); };
2085 struct T : virtual public R { virtual void f (); };
2086 struct U : public S, public T { };
2088 is not -- there's no way to decide whether to put `S::f' or
2089 `T::f' in the vtable for `R'.
2091 The solution is to look at all paths to BINFO. If we find
2092 different overriders along any two, then there is a problem. */
2093 if (DECL_THUNK_P (fn))
2094 fn = THUNK_TARGET (fn);
2096 /* Determine the depth of the hierarchy. */
2097 ffod.fn = fn;
2098 ffod.declaring_base = binfo;
2099 ffod.candidates = NULL_TREE;
2100 ffod.path = VEC_alloc (tree, heap, 30);
2102 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2103 dfs_find_final_overrider_post, &ffod);
2105 VEC_free (tree, heap, ffod.path);
2107 /* If there was no winner, issue an error message. */
2108 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2109 return error_mark_node;
2111 return ffod.candidates;
2114 /* Return the index of the vcall offset for FN when TYPE is used as a
2115 virtual base. */
2117 static tree
2118 get_vcall_index (tree fn, tree type)
2120 VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type);
2121 tree_pair_p p;
2122 unsigned ix;
2124 FOR_EACH_VEC_ELT (tree_pair_s, indices, ix, p)
2125 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2126 || same_signature_p (fn, p->purpose))
2127 return p->value;
2129 /* There should always be an appropriate index. */
2130 gcc_unreachable ();
2133 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2134 dominated by T. FN is the old function; VIRTUALS points to the
2135 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2136 of that entry in the list. */
2138 static void
2139 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2140 unsigned ix)
2142 tree b;
2143 tree overrider;
2144 tree delta;
2145 tree virtual_base;
2146 tree first_defn;
2147 tree overrider_fn, overrider_target;
2148 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2149 tree over_return, base_return;
2150 bool lost = false;
2152 /* Find the nearest primary base (possibly binfo itself) which defines
2153 this function; this is the class the caller will convert to when
2154 calling FN through BINFO. */
2155 for (b = binfo; ; b = get_primary_binfo (b))
2157 gcc_assert (b);
2158 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2159 break;
2161 /* The nearest definition is from a lost primary. */
2162 if (BINFO_LOST_PRIMARY_P (b))
2163 lost = true;
2165 first_defn = b;
2167 /* Find the final overrider. */
2168 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2169 if (overrider == error_mark_node)
2171 error ("no unique final overrider for %qD in %qT", target_fn, t);
2172 return;
2174 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2176 /* Check for adjusting covariant return types. */
2177 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2178 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2180 if (POINTER_TYPE_P (over_return)
2181 && TREE_CODE (over_return) == TREE_CODE (base_return)
2182 && CLASS_TYPE_P (TREE_TYPE (over_return))
2183 && CLASS_TYPE_P (TREE_TYPE (base_return))
2184 /* If the overrider is invalid, don't even try. */
2185 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2187 /* If FN is a covariant thunk, we must figure out the adjustment
2188 to the final base FN was converting to. As OVERRIDER_TARGET might
2189 also be converting to the return type of FN, we have to
2190 combine the two conversions here. */
2191 tree fixed_offset, virtual_offset;
2193 over_return = TREE_TYPE (over_return);
2194 base_return = TREE_TYPE (base_return);
2196 if (DECL_THUNK_P (fn))
2198 gcc_assert (DECL_RESULT_THUNK_P (fn));
2199 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2200 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2202 else
2203 fixed_offset = virtual_offset = NULL_TREE;
2205 if (virtual_offset)
2206 /* Find the equivalent binfo within the return type of the
2207 overriding function. We will want the vbase offset from
2208 there. */
2209 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2210 over_return);
2211 else if (!same_type_ignoring_top_level_qualifiers_p
2212 (over_return, base_return))
2214 /* There was no existing virtual thunk (which takes
2215 precedence). So find the binfo of the base function's
2216 return type within the overriding function's return type.
2217 We cannot call lookup base here, because we're inside a
2218 dfs_walk, and will therefore clobber the BINFO_MARKED
2219 flags. Fortunately we know the covariancy is valid (it
2220 has already been checked), so we can just iterate along
2221 the binfos, which have been chained in inheritance graph
2222 order. Of course it is lame that we have to repeat the
2223 search here anyway -- we should really be caching pieces
2224 of the vtable and avoiding this repeated work. */
2225 tree thunk_binfo, base_binfo;
2227 /* Find the base binfo within the overriding function's
2228 return type. We will always find a thunk_binfo, except
2229 when the covariancy is invalid (which we will have
2230 already diagnosed). */
2231 for (base_binfo = TYPE_BINFO (base_return),
2232 thunk_binfo = TYPE_BINFO (over_return);
2233 thunk_binfo;
2234 thunk_binfo = TREE_CHAIN (thunk_binfo))
2235 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2236 BINFO_TYPE (base_binfo)))
2237 break;
2239 /* See if virtual inheritance is involved. */
2240 for (virtual_offset = thunk_binfo;
2241 virtual_offset;
2242 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2243 if (BINFO_VIRTUAL_P (virtual_offset))
2244 break;
2246 if (virtual_offset
2247 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2249 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2251 if (virtual_offset)
2253 /* We convert via virtual base. Adjust the fixed
2254 offset to be from there. */
2255 offset =
2256 size_diffop (offset,
2257 convert (ssizetype,
2258 BINFO_OFFSET (virtual_offset)));
2260 if (fixed_offset)
2261 /* There was an existing fixed offset, this must be
2262 from the base just converted to, and the base the
2263 FN was thunking to. */
2264 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2265 else
2266 fixed_offset = offset;
2270 if (fixed_offset || virtual_offset)
2271 /* Replace the overriding function with a covariant thunk. We
2272 will emit the overriding function in its own slot as
2273 well. */
2274 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2275 fixed_offset, virtual_offset);
2277 else
2278 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2279 !DECL_THUNK_P (fn));
2281 /* If we need a covariant thunk, then we may need to adjust first_defn.
2282 The ABI specifies that the thunks emitted with a function are
2283 determined by which bases the function overrides, so we need to be
2284 sure that we're using a thunk for some overridden base; even if we
2285 know that the necessary this adjustment is zero, there may not be an
2286 appropriate zero-this-adjusment thunk for us to use since thunks for
2287 overriding virtual bases always use the vcall offset.
2289 Furthermore, just choosing any base that overrides this function isn't
2290 quite right, as this slot won't be used for calls through a type that
2291 puts a covariant thunk here. Calling the function through such a type
2292 will use a different slot, and that slot is the one that determines
2293 the thunk emitted for that base.
2295 So, keep looking until we find the base that we're really overriding
2296 in this slot: the nearest primary base that doesn't use a covariant
2297 thunk in this slot. */
2298 if (overrider_target != overrider_fn)
2300 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2301 /* We already know that the overrider needs a covariant thunk. */
2302 b = get_primary_binfo (b);
2303 for (; ; b = get_primary_binfo (b))
2305 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2306 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2307 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2308 break;
2309 if (BINFO_LOST_PRIMARY_P (b))
2310 lost = true;
2312 first_defn = b;
2315 /* Assume that we will produce a thunk that convert all the way to
2316 the final overrider, and not to an intermediate virtual base. */
2317 virtual_base = NULL_TREE;
2319 /* See if we can convert to an intermediate virtual base first, and then
2320 use the vcall offset located there to finish the conversion. */
2321 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2323 /* If we find the final overrider, then we can stop
2324 walking. */
2325 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2326 BINFO_TYPE (TREE_VALUE (overrider))))
2327 break;
2329 /* If we find a virtual base, and we haven't yet found the
2330 overrider, then there is a virtual base between the
2331 declaring base (first_defn) and the final overrider. */
2332 if (BINFO_VIRTUAL_P (b))
2334 virtual_base = b;
2335 break;
2339 /* Compute the constant adjustment to the `this' pointer. The
2340 `this' pointer, when this function is called, will point at BINFO
2341 (or one of its primary bases, which are at the same offset). */
2342 if (virtual_base)
2343 /* The `this' pointer needs to be adjusted from the declaration to
2344 the nearest virtual base. */
2345 delta = size_diffop_loc (input_location,
2346 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2347 convert (ssizetype, BINFO_OFFSET (first_defn)));
2348 else if (lost)
2349 /* If the nearest definition is in a lost primary, we don't need an
2350 entry in our vtable. Except possibly in a constructor vtable,
2351 if we happen to get our primary back. In that case, the offset
2352 will be zero, as it will be a primary base. */
2353 delta = size_zero_node;
2354 else
2355 /* The `this' pointer needs to be adjusted from pointing to
2356 BINFO to pointing at the base where the final overrider
2357 appears. */
2358 delta = size_diffop_loc (input_location,
2359 convert (ssizetype,
2360 BINFO_OFFSET (TREE_VALUE (overrider))),
2361 convert (ssizetype, BINFO_OFFSET (binfo)));
2363 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2365 if (virtual_base)
2366 BV_VCALL_INDEX (*virtuals)
2367 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2368 else
2369 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2371 BV_LOST_PRIMARY (*virtuals) = lost;
2374 /* Called from modify_all_vtables via dfs_walk. */
2376 static tree
2377 dfs_modify_vtables (tree binfo, void* data)
2379 tree t = (tree) data;
2380 tree virtuals;
2381 tree old_virtuals;
2382 unsigned ix;
2384 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2385 /* A base without a vtable needs no modification, and its bases
2386 are uninteresting. */
2387 return dfs_skip_bases;
2389 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2390 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2391 /* Don't do the primary vtable, if it's new. */
2392 return NULL_TREE;
2394 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2395 /* There's no need to modify the vtable for a non-virtual primary
2396 base; we're not going to use that vtable anyhow. We do still
2397 need to do this for virtual primary bases, as they could become
2398 non-primary in a construction vtable. */
2399 return NULL_TREE;
2401 make_new_vtable (t, binfo);
2403 /* Now, go through each of the virtual functions in the virtual
2404 function table for BINFO. Find the final overrider, and update
2405 the BINFO_VIRTUALS list appropriately. */
2406 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2407 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2408 virtuals;
2409 ix++, virtuals = TREE_CHAIN (virtuals),
2410 old_virtuals = TREE_CHAIN (old_virtuals))
2411 update_vtable_entry_for_fn (t,
2412 binfo,
2413 BV_FN (old_virtuals),
2414 &virtuals, ix);
2416 return NULL_TREE;
2419 /* Update all of the primary and secondary vtables for T. Create new
2420 vtables as required, and initialize their RTTI information. Each
2421 of the functions in VIRTUALS is declared in T and may override a
2422 virtual function from a base class; find and modify the appropriate
2423 entries to point to the overriding functions. Returns a list, in
2424 declaration order, of the virtual functions that are declared in T,
2425 but do not appear in the primary base class vtable, and which
2426 should therefore be appended to the end of the vtable for T. */
2428 static tree
2429 modify_all_vtables (tree t, tree virtuals)
2431 tree binfo = TYPE_BINFO (t);
2432 tree *fnsp;
2434 /* Update all of the vtables. */
2435 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2437 /* Add virtual functions not already in our primary vtable. These
2438 will be both those introduced by this class, and those overridden
2439 from secondary bases. It does not include virtuals merely
2440 inherited from secondary bases. */
2441 for (fnsp = &virtuals; *fnsp; )
2443 tree fn = TREE_VALUE (*fnsp);
2445 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2446 || DECL_VINDEX (fn) == error_mark_node)
2448 /* We don't need to adjust the `this' pointer when
2449 calling this function. */
2450 BV_DELTA (*fnsp) = integer_zero_node;
2451 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2453 /* This is a function not already in our vtable. Keep it. */
2454 fnsp = &TREE_CHAIN (*fnsp);
2456 else
2457 /* We've already got an entry for this function. Skip it. */
2458 *fnsp = TREE_CHAIN (*fnsp);
2461 return virtuals;
2464 /* Get the base virtual function declarations in T that have the
2465 indicated NAME. */
2467 static tree
2468 get_basefndecls (tree name, tree t)
2470 tree methods;
2471 tree base_fndecls = NULL_TREE;
2472 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2473 int i;
2475 /* Find virtual functions in T with the indicated NAME. */
2476 i = lookup_fnfields_1 (t, name);
2477 if (i != -1)
2478 for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i);
2479 methods;
2480 methods = OVL_NEXT (methods))
2482 tree method = OVL_CURRENT (methods);
2484 if (TREE_CODE (method) == FUNCTION_DECL
2485 && DECL_VINDEX (method))
2486 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2489 if (base_fndecls)
2490 return base_fndecls;
2492 for (i = 0; i < n_baseclasses; i++)
2494 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2495 base_fndecls = chainon (get_basefndecls (name, basetype),
2496 base_fndecls);
2499 return base_fndecls;
2502 /* If this declaration supersedes the declaration of
2503 a method declared virtual in the base class, then
2504 mark this field as being virtual as well. */
2506 void
2507 check_for_override (tree decl, tree ctype)
2509 bool overrides_found = false;
2510 if (TREE_CODE (decl) == TEMPLATE_DECL)
2511 /* In [temp.mem] we have:
2513 A specialization of a member function template does not
2514 override a virtual function from a base class. */
2515 return;
2516 if ((DECL_DESTRUCTOR_P (decl)
2517 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2518 || DECL_CONV_FN_P (decl))
2519 && look_for_overrides (ctype, decl)
2520 && !DECL_STATIC_FUNCTION_P (decl))
2521 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2522 the error_mark_node so that we know it is an overriding
2523 function. */
2525 DECL_VINDEX (decl) = decl;
2526 overrides_found = true;
2529 if (DECL_VIRTUAL_P (decl))
2531 if (!DECL_VINDEX (decl))
2532 DECL_VINDEX (decl) = error_mark_node;
2533 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2534 if (DECL_DESTRUCTOR_P (decl))
2535 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2537 else if (DECL_FINAL_P (decl))
2538 error ("%q+#D marked final, but is not virtual", decl);
2539 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2540 error ("%q+#D marked override, but does not override", decl);
2543 /* Warn about hidden virtual functions that are not overridden in t.
2544 We know that constructors and destructors don't apply. */
2546 static void
2547 warn_hidden (tree t)
2549 VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t);
2550 tree fns;
2551 size_t i;
2553 /* We go through each separately named virtual function. */
2554 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2555 VEC_iterate (tree, method_vec, i, fns);
2556 ++i)
2558 tree fn;
2559 tree name;
2560 tree fndecl;
2561 tree base_fndecls;
2562 tree base_binfo;
2563 tree binfo;
2564 int j;
2566 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2567 have the same name. Figure out what name that is. */
2568 name = DECL_NAME (OVL_CURRENT (fns));
2569 /* There are no possibly hidden functions yet. */
2570 base_fndecls = NULL_TREE;
2571 /* Iterate through all of the base classes looking for possibly
2572 hidden functions. */
2573 for (binfo = TYPE_BINFO (t), j = 0;
2574 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2576 tree basetype = BINFO_TYPE (base_binfo);
2577 base_fndecls = chainon (get_basefndecls (name, basetype),
2578 base_fndecls);
2581 /* If there are no functions to hide, continue. */
2582 if (!base_fndecls)
2583 continue;
2585 /* Remove any overridden functions. */
2586 for (fn = fns; fn; fn = OVL_NEXT (fn))
2588 fndecl = OVL_CURRENT (fn);
2589 if (DECL_VINDEX (fndecl))
2591 tree *prev = &base_fndecls;
2593 while (*prev)
2594 /* If the method from the base class has the same
2595 signature as the method from the derived class, it
2596 has been overridden. */
2597 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2598 *prev = TREE_CHAIN (*prev);
2599 else
2600 prev = &TREE_CHAIN (*prev);
2604 /* Now give a warning for all base functions without overriders,
2605 as they are hidden. */
2606 while (base_fndecls)
2608 /* Here we know it is a hider, and no overrider exists. */
2609 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
2610 warning (OPT_Woverloaded_virtual, " by %q+D", fns);
2611 base_fndecls = TREE_CHAIN (base_fndecls);
2616 /* Check for things that are invalid. There are probably plenty of other
2617 things we should check for also. */
2619 static void
2620 finish_struct_anon (tree t)
2622 tree field;
2624 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2626 if (TREE_STATIC (field))
2627 continue;
2628 if (TREE_CODE (field) != FIELD_DECL)
2629 continue;
2631 if (DECL_NAME (field) == NULL_TREE
2632 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2634 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2635 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2636 for (; elt; elt = DECL_CHAIN (elt))
2638 /* We're generally only interested in entities the user
2639 declared, but we also find nested classes by noticing
2640 the TYPE_DECL that we create implicitly. You're
2641 allowed to put one anonymous union inside another,
2642 though, so we explicitly tolerate that. We use
2643 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2644 we also allow unnamed types used for defining fields. */
2645 if (DECL_ARTIFICIAL (elt)
2646 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2647 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2648 continue;
2650 if (TREE_CODE (elt) != FIELD_DECL)
2652 if (is_union)
2653 permerror (input_location, "%q+#D invalid; an anonymous union can "
2654 "only have non-static data members", elt);
2655 else
2656 permerror (input_location, "%q+#D invalid; an anonymous struct can "
2657 "only have non-static data members", elt);
2658 continue;
2661 if (TREE_PRIVATE (elt))
2663 if (is_union)
2664 permerror (input_location, "private member %q+#D in anonymous union", elt);
2665 else
2666 permerror (input_location, "private member %q+#D in anonymous struct", elt);
2668 else if (TREE_PROTECTED (elt))
2670 if (is_union)
2671 permerror (input_location, "protected member %q+#D in anonymous union", elt);
2672 else
2673 permerror (input_location, "protected member %q+#D in anonymous struct", elt);
2676 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2677 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2683 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2684 will be used later during class template instantiation.
2685 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2686 a non-static member data (FIELD_DECL), a member function
2687 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2688 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2689 When FRIEND_P is nonzero, T is either a friend class
2690 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2691 (FUNCTION_DECL, TEMPLATE_DECL). */
2693 void
2694 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2696 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2697 if (CLASSTYPE_TEMPLATE_INFO (type))
2698 CLASSTYPE_DECL_LIST (type)
2699 = tree_cons (friend_p ? NULL_TREE : type,
2700 t, CLASSTYPE_DECL_LIST (type));
2703 /* This function is called from declare_virt_assop_and_dtor via
2704 dfs_walk_all.
2706 DATA is a type that direcly or indirectly inherits the base
2707 represented by BINFO. If BINFO contains a virtual assignment [copy
2708 assignment or move assigment] operator or a virtual constructor,
2709 declare that function in DATA if it hasn't been already declared. */
2711 static tree
2712 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2714 tree bv, fn, t = (tree)data;
2715 tree opname = ansi_assopname (NOP_EXPR);
2717 gcc_assert (t && CLASS_TYPE_P (t));
2718 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
2720 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2721 /* A base without a vtable needs no modification, and its bases
2722 are uninteresting. */
2723 return dfs_skip_bases;
2725 if (BINFO_PRIMARY_P (binfo))
2726 /* If this is a primary base, then we have already looked at the
2727 virtual functions of its vtable. */
2728 return NULL_TREE;
2730 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
2732 fn = BV_FN (bv);
2734 if (DECL_NAME (fn) == opname)
2736 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
2737 lazily_declare_fn (sfk_copy_assignment, t);
2738 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
2739 lazily_declare_fn (sfk_move_assignment, t);
2741 else if (DECL_DESTRUCTOR_P (fn)
2742 && CLASSTYPE_LAZY_DESTRUCTOR (t))
2743 lazily_declare_fn (sfk_destructor, t);
2746 return NULL_TREE;
2749 /* If the class type T has a direct or indirect base that contains a
2750 virtual assignment operator or a virtual destructor, declare that
2751 function in T if it hasn't been already declared. */
2753 static void
2754 declare_virt_assop_and_dtor (tree t)
2756 if (!(TYPE_POLYMORPHIC_P (t)
2757 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
2758 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
2759 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
2760 return;
2762 dfs_walk_all (TYPE_BINFO (t),
2763 dfs_declare_virt_assop_and_dtor,
2764 NULL, t);
2767 /* Declare the inheriting constructor for class T inherited from base
2768 constructor CTOR with the parameter array PARMS of size NPARMS. */
2770 static void
2771 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
2773 /* We don't declare an inheriting ctor that would be a default,
2774 copy or move ctor. */
2775 if (nparms == 0
2776 || (nparms == 1
2777 && TREE_CODE (parms[0]) == REFERENCE_TYPE
2778 && TYPE_MAIN_VARIANT (TREE_TYPE (parms[0])) == t))
2779 return;
2780 int i;
2781 tree parmlist = void_list_node;
2782 for (i = nparms - 1; i >= 0; i--)
2783 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
2784 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
2785 t, false, ctor, parmlist);
2786 if (add_method (t, fn, NULL_TREE))
2788 DECL_CHAIN (fn) = TYPE_METHODS (t);
2789 TYPE_METHODS (t) = fn;
2793 /* Declare all the inheriting constructors for class T inherited from base
2794 constructor CTOR. */
2796 static void
2797 one_inherited_ctor (tree ctor, tree t)
2799 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
2801 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
2802 int i = 0;
2803 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
2805 if (TREE_PURPOSE (parms))
2806 one_inheriting_sig (t, ctor, new_parms, i);
2807 new_parms[i++] = TREE_VALUE (parms);
2809 one_inheriting_sig (t, ctor, new_parms, i);
2810 if (parms == NULL_TREE)
2812 warning (OPT_Winherited_variadic_ctor,
2813 "the ellipsis in %qD is not inherited", ctor);
2814 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
2818 /* Create default constructors, assignment operators, and so forth for
2819 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
2820 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
2821 the class cannot have a default constructor, copy constructor
2822 taking a const reference argument, or an assignment operator taking
2823 a const reference, respectively. */
2825 static void
2826 add_implicitly_declared_members (tree t, tree* access_decls,
2827 int cant_have_const_cctor,
2828 int cant_have_const_assignment)
2830 bool move_ok = false;
2832 if (cxx_dialect >= cxx0x && !CLASSTYPE_DESTRUCTORS (t)
2833 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
2834 && !type_has_move_constructor (t) && !type_has_move_assign (t))
2835 move_ok = true;
2837 /* Destructor. */
2838 if (!CLASSTYPE_DESTRUCTORS (t))
2840 /* In general, we create destructors lazily. */
2841 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
2843 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
2844 && TYPE_FOR_JAVA (t))
2845 /* But if this is a Java class, any non-trivial destructor is
2846 invalid, even if compiler-generated. Therefore, if the
2847 destructor is non-trivial we create it now. */
2848 lazily_declare_fn (sfk_destructor, t);
2851 /* [class.ctor]
2853 If there is no user-declared constructor for a class, a default
2854 constructor is implicitly declared. */
2855 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
2857 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
2858 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
2859 if (cxx_dialect >= cxx0x)
2860 TYPE_HAS_CONSTEXPR_CTOR (t)
2861 /* This might force the declaration. */
2862 = type_has_constexpr_default_constructor (t);
2865 /* [class.ctor]
2867 If a class definition does not explicitly declare a copy
2868 constructor, one is declared implicitly. */
2869 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
2871 TYPE_HAS_COPY_CTOR (t) = 1;
2872 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
2873 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
2874 if (move_ok)
2875 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
2878 /* If there is no assignment operator, one will be created if and
2879 when it is needed. For now, just record whether or not the type
2880 of the parameter to the assignment operator will be a const or
2881 non-const reference. */
2882 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
2884 TYPE_HAS_COPY_ASSIGN (t) = 1;
2885 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
2886 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
2887 if (move_ok)
2888 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
2891 /* We can't be lazy about declaring functions that might override
2892 a virtual function from a base class. */
2893 declare_virt_assop_and_dtor (t);
2895 while (*access_decls)
2897 tree using_decl = TREE_VALUE (*access_decls);
2898 tree decl = USING_DECL_DECLS (using_decl);
2899 if (DECL_SELF_REFERENCE_P (decl))
2901 /* declare, then remove the decl */
2902 tree ctor_list = CLASSTYPE_CONSTRUCTORS (TREE_TYPE (decl));
2903 location_t loc = input_location;
2904 input_location = DECL_SOURCE_LOCATION (using_decl);
2905 if (ctor_list)
2906 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
2907 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
2908 *access_decls = TREE_CHAIN (*access_decls);
2909 input_location = loc;
2911 else
2912 access_decls = &TREE_CHAIN (*access_decls);
2916 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
2917 count the number of fields in TYPE, including anonymous union
2918 members. */
2920 static int
2921 count_fields (tree fields)
2923 tree x;
2924 int n_fields = 0;
2925 for (x = fields; x; x = DECL_CHAIN (x))
2927 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2928 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2929 else
2930 n_fields += 1;
2932 return n_fields;
2935 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
2936 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
2937 elts, starting at offset IDX. */
2939 static int
2940 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
2942 tree x;
2943 for (x = fields; x; x = DECL_CHAIN (x))
2945 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2946 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2947 else
2948 field_vec->elts[idx++] = x;
2950 return idx;
2953 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
2954 starting at offset IDX. */
2956 static int
2957 add_enum_fields_to_record_type (tree enumtype,
2958 struct sorted_fields_type *field_vec,
2959 int idx)
2961 tree values;
2962 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
2963 field_vec->elts[idx++] = TREE_VALUE (values);
2964 return idx;
2967 /* FIELD is a bit-field. We are finishing the processing for its
2968 enclosing type. Issue any appropriate messages and set appropriate
2969 flags. Returns false if an error has been diagnosed. */
2971 static bool
2972 check_bitfield_decl (tree field)
2974 tree type = TREE_TYPE (field);
2975 tree w;
2977 /* Extract the declared width of the bitfield, which has been
2978 temporarily stashed in DECL_INITIAL. */
2979 w = DECL_INITIAL (field);
2980 gcc_assert (w != NULL_TREE);
2981 /* Remove the bit-field width indicator so that the rest of the
2982 compiler does not treat that value as an initializer. */
2983 DECL_INITIAL (field) = NULL_TREE;
2985 /* Detect invalid bit-field type. */
2986 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2988 error ("bit-field %q+#D with non-integral type", field);
2989 w = error_mark_node;
2991 else
2993 location_t loc = input_location;
2994 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2995 STRIP_NOPS (w);
2997 /* detect invalid field size. */
2998 input_location = DECL_SOURCE_LOCATION (field);
2999 w = cxx_constant_value (w);
3000 input_location = loc;
3002 if (TREE_CODE (w) != INTEGER_CST)
3004 error ("bit-field %q+D width not an integer constant", field);
3005 w = error_mark_node;
3007 else if (tree_int_cst_sgn (w) < 0)
3009 error ("negative width in bit-field %q+D", field);
3010 w = error_mark_node;
3012 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3014 error ("zero width for bit-field %q+D", field);
3015 w = error_mark_node;
3017 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3018 && TREE_CODE (type) != ENUMERAL_TYPE
3019 && TREE_CODE (type) != BOOLEAN_TYPE)
3020 warning (0, "width of %q+D exceeds its type", field);
3021 else if (TREE_CODE (type) == ENUMERAL_TYPE
3022 && (0 > (compare_tree_int
3023 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3024 warning (0, "%q+D is too small to hold all values of %q#T", field, type);
3027 if (w != error_mark_node)
3029 DECL_SIZE (field) = convert (bitsizetype, w);
3030 DECL_BIT_FIELD (field) = 1;
3031 return true;
3033 else
3035 /* Non-bit-fields are aligned for their type. */
3036 DECL_BIT_FIELD (field) = 0;
3037 CLEAR_DECL_C_BIT_FIELD (field);
3038 return false;
3042 /* FIELD is a non bit-field. We are finishing the processing for its
3043 enclosing type T. Issue any appropriate messages and set appropriate
3044 flags. */
3046 static void
3047 check_field_decl (tree field,
3048 tree t,
3049 int* cant_have_const_ctor,
3050 int* no_const_asn_ref,
3051 int* any_default_members)
3053 tree type = strip_array_types (TREE_TYPE (field));
3055 /* In C++98 an anonymous union cannot contain any fields which would change
3056 the settings of CANT_HAVE_CONST_CTOR and friends. */
3057 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx0x)
3059 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3060 structs. So, we recurse through their fields here. */
3061 else if (ANON_AGGR_TYPE_P (type))
3063 tree fields;
3065 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3066 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3067 check_field_decl (fields, t, cant_have_const_ctor,
3068 no_const_asn_ref, any_default_members);
3070 /* Check members with class type for constructors, destructors,
3071 etc. */
3072 else if (CLASS_TYPE_P (type))
3074 /* Never let anything with uninheritable virtuals
3075 make it through without complaint. */
3076 abstract_virtuals_error (field, type);
3078 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx0x)
3080 static bool warned;
3081 int oldcount = errorcount;
3082 if (TYPE_NEEDS_CONSTRUCTING (type))
3083 error ("member %q+#D with constructor not allowed in union",
3084 field);
3085 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3086 error ("member %q+#D with destructor not allowed in union", field);
3087 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3088 error ("member %q+#D with copy assignment operator not allowed in union",
3089 field);
3090 if (!warned && errorcount > oldcount)
3092 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3093 "only available with -std=c++11 or -std=gnu++11");
3094 warned = true;
3097 else
3099 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3100 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3101 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3102 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3103 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3104 || !TYPE_HAS_COPY_ASSIGN (type));
3105 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3106 || !TYPE_HAS_COPY_CTOR (type));
3107 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3108 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3109 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3110 || TYPE_HAS_COMPLEX_DFLT (type));
3113 if (TYPE_HAS_COPY_CTOR (type)
3114 && !TYPE_HAS_CONST_COPY_CTOR (type))
3115 *cant_have_const_ctor = 1;
3117 if (TYPE_HAS_COPY_ASSIGN (type)
3118 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3119 *no_const_asn_ref = 1;
3121 if (DECL_INITIAL (field) != NULL_TREE)
3123 /* `build_class_init_list' does not recognize
3124 non-FIELD_DECLs. */
3125 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3126 error ("multiple fields in union %qT initialized", t);
3127 *any_default_members = 1;
3131 /* Check the data members (both static and non-static), class-scoped
3132 typedefs, etc., appearing in the declaration of T. Issue
3133 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3134 declaration order) of access declarations; each TREE_VALUE in this
3135 list is a USING_DECL.
3137 In addition, set the following flags:
3139 EMPTY_P
3140 The class is empty, i.e., contains no non-static data members.
3142 CANT_HAVE_CONST_CTOR_P
3143 This class cannot have an implicitly generated copy constructor
3144 taking a const reference.
3146 CANT_HAVE_CONST_ASN_REF
3147 This class cannot have an implicitly generated assignment
3148 operator taking a const reference.
3150 All of these flags should be initialized before calling this
3151 function.
3153 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3154 fields can be added by adding to this chain. */
3156 static void
3157 check_field_decls (tree t, tree *access_decls,
3158 int *cant_have_const_ctor_p,
3159 int *no_const_asn_ref_p)
3161 tree *field;
3162 tree *next;
3163 bool has_pointers;
3164 int any_default_members;
3165 int cant_pack = 0;
3166 int field_access = -1;
3168 /* Assume there are no access declarations. */
3169 *access_decls = NULL_TREE;
3170 /* Assume this class has no pointer members. */
3171 has_pointers = false;
3172 /* Assume none of the members of this class have default
3173 initializations. */
3174 any_default_members = 0;
3176 for (field = &TYPE_FIELDS (t); *field; field = next)
3178 tree x = *field;
3179 tree type = TREE_TYPE (x);
3180 int this_field_access;
3182 next = &DECL_CHAIN (x);
3184 if (TREE_CODE (x) == USING_DECL)
3186 /* Save the access declarations for our caller. */
3187 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3188 continue;
3191 if (TREE_CODE (x) == TYPE_DECL
3192 || TREE_CODE (x) == TEMPLATE_DECL)
3193 continue;
3195 /* If we've gotten this far, it's a data member, possibly static,
3196 or an enumerator. */
3197 if (TREE_CODE (x) != CONST_DECL)
3198 DECL_CONTEXT (x) = t;
3200 /* When this goes into scope, it will be a non-local reference. */
3201 DECL_NONLOCAL (x) = 1;
3203 if (TREE_CODE (t) == UNION_TYPE)
3205 /* [class.union]
3207 If a union contains a static data member, or a member of
3208 reference type, the program is ill-formed. */
3209 if (TREE_CODE (x) == VAR_DECL)
3211 error ("%q+D may not be static because it is a member of a union", x);
3212 continue;
3214 if (TREE_CODE (type) == REFERENCE_TYPE)
3216 error ("%q+D may not have reference type %qT because"
3217 " it is a member of a union",
3218 x, type);
3219 continue;
3223 /* Perform error checking that did not get done in
3224 grokdeclarator. */
3225 if (TREE_CODE (type) == FUNCTION_TYPE)
3227 error ("field %q+D invalidly declared function type", x);
3228 type = build_pointer_type (type);
3229 TREE_TYPE (x) = type;
3231 else if (TREE_CODE (type) == METHOD_TYPE)
3233 error ("field %q+D invalidly declared method type", x);
3234 type = build_pointer_type (type);
3235 TREE_TYPE (x) = type;
3238 if (type == error_mark_node)
3239 continue;
3241 if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL)
3242 continue;
3244 /* Now it can only be a FIELD_DECL. */
3246 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3247 CLASSTYPE_NON_AGGREGATE (t) = 1;
3249 /* If at least one non-static data member is non-literal, the whole
3250 class becomes non-literal. Note: if the type is incomplete we
3251 will complain later on. */
3252 if (COMPLETE_TYPE_P (type) && !literal_type_p (type))
3253 CLASSTYPE_LITERAL_P (t) = false;
3255 /* A standard-layout class is a class that:
3257 has the same access control (Clause 11) for all non-static data members,
3258 ... */
3259 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3260 if (field_access == -1)
3261 field_access = this_field_access;
3262 else if (this_field_access != field_access)
3263 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3265 /* If this is of reference type, check if it needs an init. */
3266 if (TREE_CODE (type) == REFERENCE_TYPE)
3268 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3269 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3270 if (DECL_INITIAL (x) == NULL_TREE)
3271 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3273 /* ARM $12.6.2: [A member initializer list] (or, for an
3274 aggregate, initialization by a brace-enclosed list) is the
3275 only way to initialize nonstatic const and reference
3276 members. */
3277 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3278 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3281 type = strip_array_types (type);
3283 if (TYPE_PACKED (t))
3285 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3287 warning
3289 "ignoring packed attribute because of unpacked non-POD field %q+#D",
3291 cant_pack = 1;
3293 else if (DECL_C_BIT_FIELD (x)
3294 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3295 DECL_PACKED (x) = 1;
3298 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3299 /* We don't treat zero-width bitfields as making a class
3300 non-empty. */
3302 else
3304 /* The class is non-empty. */
3305 CLASSTYPE_EMPTY_P (t) = 0;
3306 /* The class is not even nearly empty. */
3307 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3308 /* If one of the data members contains an empty class,
3309 so does T. */
3310 if (CLASS_TYPE_P (type)
3311 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3312 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3315 /* This is used by -Weffc++ (see below). Warn only for pointers
3316 to members which might hold dynamic memory. So do not warn
3317 for pointers to functions or pointers to members. */
3318 if (TYPE_PTR_P (type)
3319 && !TYPE_PTRFN_P (type))
3320 has_pointers = true;
3322 if (CLASS_TYPE_P (type))
3324 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3325 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3326 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3327 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3330 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3331 CLASSTYPE_HAS_MUTABLE (t) = 1;
3333 if (! layout_pod_type_p (type))
3334 /* DR 148 now allows pointers to members (which are POD themselves),
3335 to be allowed in POD structs. */
3336 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3338 if (!std_layout_type_p (type))
3339 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3341 if (! zero_init_p (type))
3342 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3344 /* We set DECL_C_BIT_FIELD in grokbitfield.
3345 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3346 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3347 check_field_decl (x, t,
3348 cant_have_const_ctor_p,
3349 no_const_asn_ref_p,
3350 &any_default_members);
3352 /* Now that we've removed bit-field widths from DECL_INITIAL,
3353 anything left in DECL_INITIAL is an NSDMI that makes the class
3354 non-aggregate. */
3355 if (DECL_INITIAL (x))
3356 CLASSTYPE_NON_AGGREGATE (t) = true;
3358 /* If any field is const, the structure type is pseudo-const. */
3359 if (CP_TYPE_CONST_P (type))
3361 C_TYPE_FIELDS_READONLY (t) = 1;
3362 if (DECL_INITIAL (x) == NULL_TREE)
3363 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3365 /* ARM $12.6.2: [A member initializer list] (or, for an
3366 aggregate, initialization by a brace-enclosed list) is the
3367 only way to initialize nonstatic const and reference
3368 members. */
3369 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3370 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3372 /* A field that is pseudo-const makes the structure likewise. */
3373 else if (CLASS_TYPE_P (type))
3375 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3376 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3377 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3378 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3381 /* Core issue 80: A nonstatic data member is required to have a
3382 different name from the class iff the class has a
3383 user-declared constructor. */
3384 if (constructor_name_p (DECL_NAME (x), t)
3385 && TYPE_HAS_USER_CONSTRUCTOR (t))
3386 permerror (input_location, "field %q+#D with same name as class", x);
3389 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3390 it should also define a copy constructor and an assignment operator to
3391 implement the correct copy semantic (deep vs shallow, etc.). As it is
3392 not feasible to check whether the constructors do allocate dynamic memory
3393 and store it within members, we approximate the warning like this:
3395 -- Warn only if there are members which are pointers
3396 -- Warn only if there is a non-trivial constructor (otherwise,
3397 there cannot be memory allocated).
3398 -- Warn only if there is a non-trivial destructor. We assume that the
3399 user at least implemented the cleanup correctly, and a destructor
3400 is needed to free dynamic memory.
3402 This seems enough for practical purposes. */
3403 if (warn_ecpp
3404 && has_pointers
3405 && TYPE_HAS_USER_CONSTRUCTOR (t)
3406 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3407 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3409 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3411 if (! TYPE_HAS_COPY_CTOR (t))
3413 warning (OPT_Weffc__,
3414 " but does not override %<%T(const %T&)%>", t, t);
3415 if (!TYPE_HAS_COPY_ASSIGN (t))
3416 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3418 else if (! TYPE_HAS_COPY_ASSIGN (t))
3419 warning (OPT_Weffc__,
3420 " but does not override %<operator=(const %T&)%>", t);
3423 /* Non-static data member initializers make the default constructor
3424 non-trivial. */
3425 if (any_default_members)
3427 TYPE_NEEDS_CONSTRUCTING (t) = true;
3428 TYPE_HAS_COMPLEX_DFLT (t) = true;
3431 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3432 if (cant_pack)
3433 TYPE_PACKED (t) = 0;
3435 /* Check anonymous struct/anonymous union fields. */
3436 finish_struct_anon (t);
3438 /* We've built up the list of access declarations in reverse order.
3439 Fix that now. */
3440 *access_decls = nreverse (*access_decls);
3443 /* If TYPE is an empty class type, records its OFFSET in the table of
3444 OFFSETS. */
3446 static int
3447 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3449 splay_tree_node n;
3451 if (!is_empty_class (type))
3452 return 0;
3454 /* Record the location of this empty object in OFFSETS. */
3455 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3456 if (!n)
3457 n = splay_tree_insert (offsets,
3458 (splay_tree_key) offset,
3459 (splay_tree_value) NULL_TREE);
3460 n->value = ((splay_tree_value)
3461 tree_cons (NULL_TREE,
3462 type,
3463 (tree) n->value));
3465 return 0;
3468 /* Returns nonzero if TYPE is an empty class type and there is
3469 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3471 static int
3472 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3474 splay_tree_node n;
3475 tree t;
3477 if (!is_empty_class (type))
3478 return 0;
3480 /* Record the location of this empty object in OFFSETS. */
3481 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3482 if (!n)
3483 return 0;
3485 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3486 if (same_type_p (TREE_VALUE (t), type))
3487 return 1;
3489 return 0;
3492 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3493 F for every subobject, passing it the type, offset, and table of
3494 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3495 be traversed.
3497 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3498 than MAX_OFFSET will not be walked.
3500 If F returns a nonzero value, the traversal ceases, and that value
3501 is returned. Otherwise, returns zero. */
3503 static int
3504 walk_subobject_offsets (tree type,
3505 subobject_offset_fn f,
3506 tree offset,
3507 splay_tree offsets,
3508 tree max_offset,
3509 int vbases_p)
3511 int r = 0;
3512 tree type_binfo = NULL_TREE;
3514 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3515 stop. */
3516 if (max_offset && INT_CST_LT (max_offset, offset))
3517 return 0;
3519 if (type == error_mark_node)
3520 return 0;
3522 if (!TYPE_P (type))
3524 if (abi_version_at_least (2))
3525 type_binfo = type;
3526 type = BINFO_TYPE (type);
3529 if (CLASS_TYPE_P (type))
3531 tree field;
3532 tree binfo;
3533 int i;
3535 /* Avoid recursing into objects that are not interesting. */
3536 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3537 return 0;
3539 /* Record the location of TYPE. */
3540 r = (*f) (type, offset, offsets);
3541 if (r)
3542 return r;
3544 /* Iterate through the direct base classes of TYPE. */
3545 if (!type_binfo)
3546 type_binfo = TYPE_BINFO (type);
3547 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3549 tree binfo_offset;
3551 if (abi_version_at_least (2)
3552 && BINFO_VIRTUAL_P (binfo))
3553 continue;
3555 if (!vbases_p
3556 && BINFO_VIRTUAL_P (binfo)
3557 && !BINFO_PRIMARY_P (binfo))
3558 continue;
3560 if (!abi_version_at_least (2))
3561 binfo_offset = size_binop (PLUS_EXPR,
3562 offset,
3563 BINFO_OFFSET (binfo));
3564 else
3566 tree orig_binfo;
3567 /* We cannot rely on BINFO_OFFSET being set for the base
3568 class yet, but the offsets for direct non-virtual
3569 bases can be calculated by going back to the TYPE. */
3570 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3571 binfo_offset = size_binop (PLUS_EXPR,
3572 offset,
3573 BINFO_OFFSET (orig_binfo));
3576 r = walk_subobject_offsets (binfo,
3578 binfo_offset,
3579 offsets,
3580 max_offset,
3581 (abi_version_at_least (2)
3582 ? /*vbases_p=*/0 : vbases_p));
3583 if (r)
3584 return r;
3587 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
3589 unsigned ix;
3590 VEC(tree,gc) *vbases;
3592 /* Iterate through the virtual base classes of TYPE. In G++
3593 3.2, we included virtual bases in the direct base class
3594 loop above, which results in incorrect results; the
3595 correct offsets for virtual bases are only known when
3596 working with the most derived type. */
3597 if (vbases_p)
3598 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3599 VEC_iterate (tree, vbases, ix, binfo); ix++)
3601 r = walk_subobject_offsets (binfo,
3603 size_binop (PLUS_EXPR,
3604 offset,
3605 BINFO_OFFSET (binfo)),
3606 offsets,
3607 max_offset,
3608 /*vbases_p=*/0);
3609 if (r)
3610 return r;
3612 else
3614 /* We still have to walk the primary base, if it is
3615 virtual. (If it is non-virtual, then it was walked
3616 above.) */
3617 tree vbase = get_primary_binfo (type_binfo);
3619 if (vbase && BINFO_VIRTUAL_P (vbase)
3620 && BINFO_PRIMARY_P (vbase)
3621 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3623 r = (walk_subobject_offsets
3624 (vbase, f, offset,
3625 offsets, max_offset, /*vbases_p=*/0));
3626 if (r)
3627 return r;
3632 /* Iterate through the fields of TYPE. */
3633 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3634 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3636 tree field_offset;
3638 if (abi_version_at_least (2))
3639 field_offset = byte_position (field);
3640 else
3641 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3642 field_offset = DECL_FIELD_OFFSET (field);
3644 r = walk_subobject_offsets (TREE_TYPE (field),
3646 size_binop (PLUS_EXPR,
3647 offset,
3648 field_offset),
3649 offsets,
3650 max_offset,
3651 /*vbases_p=*/1);
3652 if (r)
3653 return r;
3656 else if (TREE_CODE (type) == ARRAY_TYPE)
3658 tree element_type = strip_array_types (type);
3659 tree domain = TYPE_DOMAIN (type);
3660 tree index;
3662 /* Avoid recursing into objects that are not interesting. */
3663 if (!CLASS_TYPE_P (element_type)
3664 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3665 return 0;
3667 /* Step through each of the elements in the array. */
3668 for (index = size_zero_node;
3669 /* G++ 3.2 had an off-by-one error here. */
3670 (abi_version_at_least (2)
3671 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3672 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3673 index = size_binop (PLUS_EXPR, index, size_one_node))
3675 r = walk_subobject_offsets (TREE_TYPE (type),
3677 offset,
3678 offsets,
3679 max_offset,
3680 /*vbases_p=*/1);
3681 if (r)
3682 return r;
3683 offset = size_binop (PLUS_EXPR, offset,
3684 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3685 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3686 there's no point in iterating through the remaining
3687 elements of the array. */
3688 if (max_offset && INT_CST_LT (max_offset, offset))
3689 break;
3693 return 0;
3696 /* Record all of the empty subobjects of TYPE (either a type or a
3697 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3698 is being placed at OFFSET; otherwise, it is a base class that is
3699 being placed at OFFSET. */
3701 static void
3702 record_subobject_offsets (tree type,
3703 tree offset,
3704 splay_tree offsets,
3705 bool is_data_member)
3707 tree max_offset;
3708 /* If recording subobjects for a non-static data member or a
3709 non-empty base class , we do not need to record offsets beyond
3710 the size of the biggest empty class. Additional data members
3711 will go at the end of the class. Additional base classes will go
3712 either at offset zero (if empty, in which case they cannot
3713 overlap with offsets past the size of the biggest empty class) or
3714 at the end of the class.
3716 However, if we are placing an empty base class, then we must record
3717 all offsets, as either the empty class is at offset zero (where
3718 other empty classes might later be placed) or at the end of the
3719 class (where other objects might then be placed, so other empty
3720 subobjects might later overlap). */
3721 if (is_data_member
3722 || !is_empty_class (BINFO_TYPE (type)))
3723 max_offset = sizeof_biggest_empty_class;
3724 else
3725 max_offset = NULL_TREE;
3726 walk_subobject_offsets (type, record_subobject_offset, offset,
3727 offsets, max_offset, is_data_member);
3730 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3731 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3732 virtual bases of TYPE are examined. */
3734 static int
3735 layout_conflict_p (tree type,
3736 tree offset,
3737 splay_tree offsets,
3738 int vbases_p)
3740 splay_tree_node max_node;
3742 /* Get the node in OFFSETS that indicates the maximum offset where
3743 an empty subobject is located. */
3744 max_node = splay_tree_max (offsets);
3745 /* If there aren't any empty subobjects, then there's no point in
3746 performing this check. */
3747 if (!max_node)
3748 return 0;
3750 return walk_subobject_offsets (type, check_subobject_offset, offset,
3751 offsets, (tree) (max_node->key),
3752 vbases_p);
3755 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3756 non-static data member of the type indicated by RLI. BINFO is the
3757 binfo corresponding to the base subobject, OFFSETS maps offsets to
3758 types already located at those offsets. This function determines
3759 the position of the DECL. */
3761 static void
3762 layout_nonempty_base_or_field (record_layout_info rli,
3763 tree decl,
3764 tree binfo,
3765 splay_tree offsets)
3767 tree offset = NULL_TREE;
3768 bool field_p;
3769 tree type;
3771 if (binfo)
3773 /* For the purposes of determining layout conflicts, we want to
3774 use the class type of BINFO; TREE_TYPE (DECL) will be the
3775 CLASSTYPE_AS_BASE version, which does not contain entries for
3776 zero-sized bases. */
3777 type = TREE_TYPE (binfo);
3778 field_p = false;
3780 else
3782 type = TREE_TYPE (decl);
3783 field_p = true;
3786 /* Try to place the field. It may take more than one try if we have
3787 a hard time placing the field without putting two objects of the
3788 same type at the same address. */
3789 while (1)
3791 struct record_layout_info_s old_rli = *rli;
3793 /* Place this field. */
3794 place_field (rli, decl);
3795 offset = byte_position (decl);
3797 /* We have to check to see whether or not there is already
3798 something of the same type at the offset we're about to use.
3799 For example, consider:
3801 struct S {};
3802 struct T : public S { int i; };
3803 struct U : public S, public T {};
3805 Here, we put S at offset zero in U. Then, we can't put T at
3806 offset zero -- its S component would be at the same address
3807 as the S we already allocated. So, we have to skip ahead.
3808 Since all data members, including those whose type is an
3809 empty class, have nonzero size, any overlap can happen only
3810 with a direct or indirect base-class -- it can't happen with
3811 a data member. */
3812 /* In a union, overlap is permitted; all members are placed at
3813 offset zero. */
3814 if (TREE_CODE (rli->t) == UNION_TYPE)
3815 break;
3816 /* G++ 3.2 did not check for overlaps when placing a non-empty
3817 virtual base. */
3818 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
3819 break;
3820 if (layout_conflict_p (field_p ? type : binfo, offset,
3821 offsets, field_p))
3823 /* Strip off the size allocated to this field. That puts us
3824 at the first place we could have put the field with
3825 proper alignment. */
3826 *rli = old_rli;
3828 /* Bump up by the alignment required for the type. */
3829 rli->bitpos
3830 = size_binop (PLUS_EXPR, rli->bitpos,
3831 bitsize_int (binfo
3832 ? CLASSTYPE_ALIGN (type)
3833 : TYPE_ALIGN (type)));
3834 normalize_rli (rli);
3836 else
3837 /* There was no conflict. We're done laying out this field. */
3838 break;
3841 /* Now that we know where it will be placed, update its
3842 BINFO_OFFSET. */
3843 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3844 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3845 this point because their BINFO_OFFSET is copied from another
3846 hierarchy. Therefore, we may not need to add the entire
3847 OFFSET. */
3848 propagate_binfo_offsets (binfo,
3849 size_diffop_loc (input_location,
3850 convert (ssizetype, offset),
3851 convert (ssizetype,
3852 BINFO_OFFSET (binfo))));
3855 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3857 static int
3858 empty_base_at_nonzero_offset_p (tree type,
3859 tree offset,
3860 splay_tree /*offsets*/)
3862 return is_empty_class (type) && !integer_zerop (offset);
3865 /* Layout the empty base BINFO. EOC indicates the byte currently just
3866 past the end of the class, and should be correctly aligned for a
3867 class of the type indicated by BINFO; OFFSETS gives the offsets of
3868 the empty bases allocated so far. T is the most derived
3869 type. Return nonzero iff we added it at the end. */
3871 static bool
3872 layout_empty_base (record_layout_info rli, tree binfo,
3873 tree eoc, splay_tree offsets)
3875 tree alignment;
3876 tree basetype = BINFO_TYPE (binfo);
3877 bool atend = false;
3879 /* This routine should only be used for empty classes. */
3880 gcc_assert (is_empty_class (basetype));
3881 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3883 if (!integer_zerop (BINFO_OFFSET (binfo)))
3885 if (abi_version_at_least (2))
3886 propagate_binfo_offsets
3887 (binfo, size_diffop_loc (input_location,
3888 size_zero_node, BINFO_OFFSET (binfo)));
3889 else
3890 warning (OPT_Wabi,
3891 "offset of empty base %qT may not be ABI-compliant and may"
3892 "change in a future version of GCC",
3893 BINFO_TYPE (binfo));
3896 /* This is an empty base class. We first try to put it at offset
3897 zero. */
3898 if (layout_conflict_p (binfo,
3899 BINFO_OFFSET (binfo),
3900 offsets,
3901 /*vbases_p=*/0))
3903 /* That didn't work. Now, we move forward from the next
3904 available spot in the class. */
3905 atend = true;
3906 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3907 while (1)
3909 if (!layout_conflict_p (binfo,
3910 BINFO_OFFSET (binfo),
3911 offsets,
3912 /*vbases_p=*/0))
3913 /* We finally found a spot where there's no overlap. */
3914 break;
3916 /* There's overlap here, too. Bump along to the next spot. */
3917 propagate_binfo_offsets (binfo, alignment);
3921 if (CLASSTYPE_USER_ALIGN (basetype))
3923 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
3924 if (warn_packed)
3925 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
3926 TYPE_USER_ALIGN (rli->t) = 1;
3929 return atend;
3932 /* Layout the base given by BINFO in the class indicated by RLI.
3933 *BASE_ALIGN is a running maximum of the alignments of
3934 any base class. OFFSETS gives the location of empty base
3935 subobjects. T is the most derived type. Return nonzero if the new
3936 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3937 *NEXT_FIELD, unless BINFO is for an empty base class.
3939 Returns the location at which the next field should be inserted. */
3941 static tree *
3942 build_base_field (record_layout_info rli, tree binfo,
3943 splay_tree offsets, tree *next_field)
3945 tree t = rli->t;
3946 tree basetype = BINFO_TYPE (binfo);
3948 if (!COMPLETE_TYPE_P (basetype))
3949 /* This error is now reported in xref_tag, thus giving better
3950 location information. */
3951 return next_field;
3953 /* Place the base class. */
3954 if (!is_empty_class (basetype))
3956 tree decl;
3958 /* The containing class is non-empty because it has a non-empty
3959 base class. */
3960 CLASSTYPE_EMPTY_P (t) = 0;
3962 /* Create the FIELD_DECL. */
3963 decl = build_decl (input_location,
3964 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3965 DECL_ARTIFICIAL (decl) = 1;
3966 DECL_IGNORED_P (decl) = 1;
3967 DECL_FIELD_CONTEXT (decl) = t;
3968 if (CLASSTYPE_AS_BASE (basetype))
3970 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3971 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3972 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3973 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3974 DECL_MODE (decl) = TYPE_MODE (basetype);
3975 DECL_FIELD_IS_BASE (decl) = 1;
3977 /* Try to place the field. It may take more than one try if we
3978 have a hard time placing the field without putting two
3979 objects of the same type at the same address. */
3980 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3981 /* Add the new FIELD_DECL to the list of fields for T. */
3982 DECL_CHAIN (decl) = *next_field;
3983 *next_field = decl;
3984 next_field = &DECL_CHAIN (decl);
3987 else
3989 tree eoc;
3990 bool atend;
3992 /* On some platforms (ARM), even empty classes will not be
3993 byte-aligned. */
3994 eoc = round_up_loc (input_location,
3995 rli_size_unit_so_far (rli),
3996 CLASSTYPE_ALIGN_UNIT (basetype));
3997 atend = layout_empty_base (rli, binfo, eoc, offsets);
3998 /* A nearly-empty class "has no proper base class that is empty,
3999 not morally virtual, and at an offset other than zero." */
4000 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4002 if (atend)
4003 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4004 /* The check above (used in G++ 3.2) is insufficient because
4005 an empty class placed at offset zero might itself have an
4006 empty base at a nonzero offset. */
4007 else if (walk_subobject_offsets (basetype,
4008 empty_base_at_nonzero_offset_p,
4009 size_zero_node,
4010 /*offsets=*/NULL,
4011 /*max_offset=*/NULL_TREE,
4012 /*vbases_p=*/true))
4014 if (abi_version_at_least (2))
4015 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4016 else
4017 warning (OPT_Wabi,
4018 "class %qT will be considered nearly empty in a "
4019 "future version of GCC", t);
4023 /* We do not create a FIELD_DECL for empty base classes because
4024 it might overlap some other field. We want to be able to
4025 create CONSTRUCTORs for the class by iterating over the
4026 FIELD_DECLs, and the back end does not handle overlapping
4027 FIELD_DECLs. */
4029 /* An empty virtual base causes a class to be non-empty
4030 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4031 here because that was already done when the virtual table
4032 pointer was created. */
4035 /* Record the offsets of BINFO and its base subobjects. */
4036 record_subobject_offsets (binfo,
4037 BINFO_OFFSET (binfo),
4038 offsets,
4039 /*is_data_member=*/false);
4041 return next_field;
4044 /* Layout all of the non-virtual base classes. Record empty
4045 subobjects in OFFSETS. T is the most derived type. Return nonzero
4046 if the type cannot be nearly empty. The fields created
4047 corresponding to the base classes will be inserted at
4048 *NEXT_FIELD. */
4050 static void
4051 build_base_fields (record_layout_info rli,
4052 splay_tree offsets, tree *next_field)
4054 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4055 subobjects. */
4056 tree t = rli->t;
4057 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4058 int i;
4060 /* The primary base class is always allocated first. */
4061 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4062 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4063 offsets, next_field);
4065 /* Now allocate the rest of the bases. */
4066 for (i = 0; i < n_baseclasses; ++i)
4068 tree base_binfo;
4070 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4072 /* The primary base was already allocated above, so we don't
4073 need to allocate it again here. */
4074 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4075 continue;
4077 /* Virtual bases are added at the end (a primary virtual base
4078 will have already been added). */
4079 if (BINFO_VIRTUAL_P (base_binfo))
4080 continue;
4082 next_field = build_base_field (rli, base_binfo,
4083 offsets, next_field);
4087 /* Go through the TYPE_METHODS of T issuing any appropriate
4088 diagnostics, figuring out which methods override which other
4089 methods, and so forth. */
4091 static void
4092 check_methods (tree t)
4094 tree x;
4096 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4098 check_for_override (x, t);
4099 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4100 error ("initializer specified for non-virtual method %q+D", x);
4101 /* The name of the field is the original field name
4102 Save this in auxiliary field for later overloading. */
4103 if (DECL_VINDEX (x))
4105 TYPE_POLYMORPHIC_P (t) = 1;
4106 if (DECL_PURE_VIRTUAL_P (x))
4107 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
4109 /* All user-provided destructors are non-trivial.
4110 Constructors and assignment ops are handled in
4111 grok_special_member_properties. */
4112 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4113 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4117 /* FN is a constructor or destructor. Clone the declaration to create
4118 a specialized in-charge or not-in-charge version, as indicated by
4119 NAME. */
4121 static tree
4122 build_clone (tree fn, tree name)
4124 tree parms;
4125 tree clone;
4127 /* Copy the function. */
4128 clone = copy_decl (fn);
4129 /* Reset the function name. */
4130 DECL_NAME (clone) = name;
4131 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4132 /* Remember where this function came from. */
4133 DECL_ABSTRACT_ORIGIN (clone) = fn;
4134 /* Make it easy to find the CLONE given the FN. */
4135 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4136 DECL_CHAIN (fn) = clone;
4138 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4139 if (TREE_CODE (clone) == TEMPLATE_DECL)
4141 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4142 DECL_TEMPLATE_RESULT (clone) = result;
4143 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4144 DECL_TI_TEMPLATE (result) = clone;
4145 TREE_TYPE (clone) = TREE_TYPE (result);
4146 return clone;
4149 DECL_CLONED_FUNCTION (clone) = fn;
4150 /* There's no pending inline data for this function. */
4151 DECL_PENDING_INLINE_INFO (clone) = NULL;
4152 DECL_PENDING_INLINE_P (clone) = 0;
4154 /* The base-class destructor is not virtual. */
4155 if (name == base_dtor_identifier)
4157 DECL_VIRTUAL_P (clone) = 0;
4158 if (TREE_CODE (clone) != TEMPLATE_DECL)
4159 DECL_VINDEX (clone) = NULL_TREE;
4162 /* If there was an in-charge parameter, drop it from the function
4163 type. */
4164 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4166 tree basetype;
4167 tree parmtypes;
4168 tree exceptions;
4170 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4171 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4172 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4173 /* Skip the `this' parameter. */
4174 parmtypes = TREE_CHAIN (parmtypes);
4175 /* Skip the in-charge parameter. */
4176 parmtypes = TREE_CHAIN (parmtypes);
4177 /* And the VTT parm, in a complete [cd]tor. */
4178 if (DECL_HAS_VTT_PARM_P (fn)
4179 && ! DECL_NEEDS_VTT_PARM_P (clone))
4180 parmtypes = TREE_CHAIN (parmtypes);
4181 /* If this is subobject constructor or destructor, add the vtt
4182 parameter. */
4183 TREE_TYPE (clone)
4184 = build_method_type_directly (basetype,
4185 TREE_TYPE (TREE_TYPE (clone)),
4186 parmtypes);
4187 if (exceptions)
4188 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4189 exceptions);
4190 TREE_TYPE (clone)
4191 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4192 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4195 /* Copy the function parameters. */
4196 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4197 /* Remove the in-charge parameter. */
4198 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4200 DECL_CHAIN (DECL_ARGUMENTS (clone))
4201 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4202 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4204 /* And the VTT parm, in a complete [cd]tor. */
4205 if (DECL_HAS_VTT_PARM_P (fn))
4207 if (DECL_NEEDS_VTT_PARM_P (clone))
4208 DECL_HAS_VTT_PARM_P (clone) = 1;
4209 else
4211 DECL_CHAIN (DECL_ARGUMENTS (clone))
4212 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4213 DECL_HAS_VTT_PARM_P (clone) = 0;
4217 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4219 DECL_CONTEXT (parms) = clone;
4220 cxx_dup_lang_specific_decl (parms);
4223 /* Create the RTL for this function. */
4224 SET_DECL_RTL (clone, NULL);
4225 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4227 if (pch_file)
4228 note_decl_for_pch (clone);
4230 return clone;
4233 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4234 not invoke this function directly.
4236 For a non-thunk function, returns the address of the slot for storing
4237 the function it is a clone of. Otherwise returns NULL_TREE.
4239 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4240 cloned_function is unset. This is to support the separate
4241 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4242 on a template makes sense, but not the former. */
4244 tree *
4245 decl_cloned_function_p (const_tree decl, bool just_testing)
4247 tree *ptr;
4248 if (just_testing)
4249 decl = STRIP_TEMPLATE (decl);
4251 if (TREE_CODE (decl) != FUNCTION_DECL
4252 || !DECL_LANG_SPECIFIC (decl)
4253 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4255 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4256 if (!just_testing)
4257 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4258 else
4259 #endif
4260 return NULL;
4263 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4264 if (just_testing && *ptr == NULL_TREE)
4265 return NULL;
4266 else
4267 return ptr;
4270 /* Produce declarations for all appropriate clones of FN. If
4271 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4272 CLASTYPE_METHOD_VEC as well. */
4274 void
4275 clone_function_decl (tree fn, int update_method_vec_p)
4277 tree clone;
4279 /* Avoid inappropriate cloning. */
4280 if (DECL_CHAIN (fn)
4281 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4282 return;
4284 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4286 /* For each constructor, we need two variants: an in-charge version
4287 and a not-in-charge version. */
4288 clone = build_clone (fn, complete_ctor_identifier);
4289 if (update_method_vec_p)
4290 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4291 clone = build_clone (fn, base_ctor_identifier);
4292 if (update_method_vec_p)
4293 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4295 else
4297 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4299 /* For each destructor, we need three variants: an in-charge
4300 version, a not-in-charge version, and an in-charge deleting
4301 version. We clone the deleting version first because that
4302 means it will go second on the TYPE_METHODS list -- and that
4303 corresponds to the correct layout order in the virtual
4304 function table.
4306 For a non-virtual destructor, we do not build a deleting
4307 destructor. */
4308 if (DECL_VIRTUAL_P (fn))
4310 clone = build_clone (fn, deleting_dtor_identifier);
4311 if (update_method_vec_p)
4312 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4314 clone = build_clone (fn, complete_dtor_identifier);
4315 if (update_method_vec_p)
4316 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4317 clone = build_clone (fn, base_dtor_identifier);
4318 if (update_method_vec_p)
4319 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4322 /* Note that this is an abstract function that is never emitted. */
4323 DECL_ABSTRACT (fn) = 1;
4326 /* DECL is an in charge constructor, which is being defined. This will
4327 have had an in class declaration, from whence clones were
4328 declared. An out-of-class definition can specify additional default
4329 arguments. As it is the clones that are involved in overload
4330 resolution, we must propagate the information from the DECL to its
4331 clones. */
4333 void
4334 adjust_clone_args (tree decl)
4336 tree clone;
4338 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4339 clone = DECL_CHAIN (clone))
4341 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4342 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4343 tree decl_parms, clone_parms;
4345 clone_parms = orig_clone_parms;
4347 /* Skip the 'this' parameter. */
4348 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4349 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4351 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4352 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4353 if (DECL_HAS_VTT_PARM_P (decl))
4354 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4356 clone_parms = orig_clone_parms;
4357 if (DECL_HAS_VTT_PARM_P (clone))
4358 clone_parms = TREE_CHAIN (clone_parms);
4360 for (decl_parms = orig_decl_parms; decl_parms;
4361 decl_parms = TREE_CHAIN (decl_parms),
4362 clone_parms = TREE_CHAIN (clone_parms))
4364 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4365 TREE_TYPE (clone_parms)));
4367 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4369 /* A default parameter has been added. Adjust the
4370 clone's parameters. */
4371 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4372 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4373 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4374 tree type;
4376 clone_parms = orig_decl_parms;
4378 if (DECL_HAS_VTT_PARM_P (clone))
4380 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4381 TREE_VALUE (orig_clone_parms),
4382 clone_parms);
4383 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4385 type = build_method_type_directly (basetype,
4386 TREE_TYPE (TREE_TYPE (clone)),
4387 clone_parms);
4388 if (exceptions)
4389 type = build_exception_variant (type, exceptions);
4390 if (attrs)
4391 type = cp_build_type_attribute_variant (type, attrs);
4392 TREE_TYPE (clone) = type;
4394 clone_parms = NULL_TREE;
4395 break;
4398 gcc_assert (!clone_parms);
4402 /* For each of the constructors and destructors in T, create an
4403 in-charge and not-in-charge variant. */
4405 static void
4406 clone_constructors_and_destructors (tree t)
4408 tree fns;
4410 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4411 out now. */
4412 if (!CLASSTYPE_METHOD_VEC (t))
4413 return;
4415 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4416 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4417 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4418 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4421 /* Deduce noexcept for a destructor DTOR. */
4423 void
4424 deduce_noexcept_on_destructor (tree dtor)
4426 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4428 tree ctx = DECL_CONTEXT (dtor);
4429 tree implicit_fn = implicitly_declare_fn (sfk_destructor, ctx,
4430 /*const_p=*/false,
4431 NULL, NULL);
4432 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
4433 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4437 /* For each destructor in T, deduce noexcept:
4439 12.4/3: A declaration of a destructor that does not have an
4440 exception-specification is implicitly considered to have the
4441 same exception-specification as an implicit declaration (15.4). */
4443 static void
4444 deduce_noexcept_on_destructors (tree t)
4446 tree fns;
4448 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4449 out now. */
4450 if (!CLASSTYPE_METHOD_VEC (t))
4451 return;
4453 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4454 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4457 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4458 of TYPE for virtual functions which FNDECL overrides. Return a
4459 mask of the tm attributes found therein. */
4461 static int
4462 look_for_tm_attr_overrides (tree type, tree fndecl)
4464 tree binfo = TYPE_BINFO (type);
4465 tree base_binfo;
4466 int ix, found = 0;
4468 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4470 tree o, basetype = BINFO_TYPE (base_binfo);
4472 if (!TYPE_POLYMORPHIC_P (basetype))
4473 continue;
4475 o = look_for_overrides_here (basetype, fndecl);
4476 if (o)
4477 found |= tm_attr_to_mask (find_tm_attribute
4478 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4479 else
4480 found |= look_for_tm_attr_overrides (basetype, fndecl);
4483 return found;
4486 /* Subroutine of set_method_tm_attributes. Handle the checks and
4487 inheritance for one virtual method FNDECL. */
4489 static void
4490 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4492 tree tm_attr;
4493 int found, have;
4495 found = look_for_tm_attr_overrides (type, fndecl);
4497 /* If FNDECL doesn't actually override anything (i.e. T is the
4498 class that first declares FNDECL virtual), then we're done. */
4499 if (found == 0)
4500 return;
4502 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4503 have = tm_attr_to_mask (tm_attr);
4505 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4506 tm_pure must match exactly, otherwise no weakening of
4507 tm_safe > tm_callable > nothing. */
4508 /* ??? The tm_pure attribute didn't make the transition to the
4509 multivendor language spec. */
4510 if (have == TM_ATTR_PURE)
4512 if (found != TM_ATTR_PURE)
4514 found &= -found;
4515 goto err_override;
4518 /* If the overridden function is tm_pure, then FNDECL must be. */
4519 else if (found == TM_ATTR_PURE && tm_attr)
4520 goto err_override;
4521 /* Look for base class combinations that cannot be satisfied. */
4522 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4524 found &= ~TM_ATTR_PURE;
4525 found &= -found;
4526 error_at (DECL_SOURCE_LOCATION (fndecl),
4527 "method overrides both %<transaction_pure%> and %qE methods",
4528 tm_mask_to_attr (found));
4530 /* If FNDECL did not declare an attribute, then inherit the most
4531 restrictive one. */
4532 else if (tm_attr == NULL)
4534 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
4536 /* Otherwise validate that we're not weaker than a function
4537 that is being overridden. */
4538 else
4540 found &= -found;
4541 if (found <= TM_ATTR_CALLABLE && have > found)
4542 goto err_override;
4544 return;
4546 err_override:
4547 error_at (DECL_SOURCE_LOCATION (fndecl),
4548 "method declared %qE overriding %qE method",
4549 tm_attr, tm_mask_to_attr (found));
4552 /* For each of the methods in T, propagate a class-level tm attribute. */
4554 static void
4555 set_method_tm_attributes (tree t)
4557 tree class_tm_attr, fndecl;
4559 /* Don't bother collecting tm attributes if transactional memory
4560 support is not enabled. */
4561 if (!flag_tm)
4562 return;
4564 /* Process virtual methods first, as they inherit directly from the
4565 base virtual function and also require validation of new attributes. */
4566 if (TYPE_CONTAINS_VPTR_P (t))
4568 tree vchain;
4569 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4570 vchain = TREE_CHAIN (vchain))
4572 fndecl = BV_FN (vchain);
4573 if (DECL_THUNK_P (fndecl))
4574 fndecl = THUNK_TARGET (fndecl);
4575 set_one_vmethod_tm_attributes (t, fndecl);
4579 /* If the class doesn't have an attribute, nothing more to do. */
4580 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4581 if (class_tm_attr == NULL)
4582 return;
4584 /* Any method that does not yet have a tm attribute inherits
4585 the one from the class. */
4586 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
4588 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4589 apply_tm_attr (fndecl, class_tm_attr);
4593 /* Returns true iff class T has a user-defined constructor other than
4594 the default constructor. */
4596 bool
4597 type_has_user_nondefault_constructor (tree t)
4599 tree fns;
4601 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4602 return false;
4604 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4606 tree fn = OVL_CURRENT (fns);
4607 if (!DECL_ARTIFICIAL (fn)
4608 && (TREE_CODE (fn) == TEMPLATE_DECL
4609 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4610 != NULL_TREE)))
4611 return true;
4614 return false;
4617 /* Returns the defaulted constructor if T has one. Otherwise, returns
4618 NULL_TREE. */
4620 tree
4621 in_class_defaulted_default_constructor (tree t)
4623 tree fns, args;
4625 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4626 return NULL_TREE;
4628 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4630 tree fn = OVL_CURRENT (fns);
4632 if (DECL_DEFAULTED_IN_CLASS_P (fn))
4634 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
4635 while (args && TREE_PURPOSE (args))
4636 args = TREE_CHAIN (args);
4637 if (!args || args == void_list_node)
4638 return fn;
4642 return NULL_TREE;
4645 /* Returns true iff FN is a user-provided function, i.e. user-declared
4646 and not defaulted at its first declaration; or explicit, private,
4647 protected, or non-const. */
4649 bool
4650 user_provided_p (tree fn)
4652 if (TREE_CODE (fn) == TEMPLATE_DECL)
4653 return true;
4654 else
4655 return (!DECL_ARTIFICIAL (fn)
4656 && !DECL_DEFAULTED_IN_CLASS_P (fn));
4659 /* Returns true iff class T has a user-provided constructor. */
4661 bool
4662 type_has_user_provided_constructor (tree t)
4664 tree fns;
4666 if (!CLASS_TYPE_P (t))
4667 return false;
4669 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4670 return false;
4672 /* This can happen in error cases; avoid crashing. */
4673 if (!CLASSTYPE_METHOD_VEC (t))
4674 return false;
4676 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4677 if (user_provided_p (OVL_CURRENT (fns)))
4678 return true;
4680 return false;
4683 /* Returns true iff class T has a user-provided default constructor. */
4685 bool
4686 type_has_user_provided_default_constructor (tree t)
4688 tree fns;
4690 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4691 return false;
4693 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4695 tree fn = OVL_CURRENT (fns);
4696 if (TREE_CODE (fn) == FUNCTION_DECL
4697 && user_provided_p (fn)
4698 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
4699 return true;
4702 return false;
4705 /* If default-initialization leaves part of TYPE uninitialized, returns
4706 a DECL for the field or TYPE itself (DR 253). */
4708 tree
4709 default_init_uninitialized_part (tree type)
4711 tree t, r, binfo;
4712 int i;
4714 type = strip_array_types (type);
4715 if (!CLASS_TYPE_P (type))
4716 return type;
4717 if (type_has_user_provided_default_constructor (type))
4718 return NULL_TREE;
4719 for (binfo = TYPE_BINFO (type), i = 0;
4720 BINFO_BASE_ITERATE (binfo, i, t); ++i)
4722 r = default_init_uninitialized_part (BINFO_TYPE (t));
4723 if (r)
4724 return r;
4726 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
4727 if (TREE_CODE (t) == FIELD_DECL
4728 && !DECL_ARTIFICIAL (t)
4729 && !DECL_INITIAL (t))
4731 r = default_init_uninitialized_part (TREE_TYPE (t));
4732 if (r)
4733 return DECL_P (r) ? r : t;
4736 return NULL_TREE;
4739 /* Returns true iff for class T, a trivial synthesized default constructor
4740 would be constexpr. */
4742 bool
4743 trivial_default_constructor_is_constexpr (tree t)
4745 /* A defaulted trivial default constructor is constexpr
4746 if there is nothing to initialize. */
4747 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
4748 return is_really_empty_class (t);
4751 /* Returns true iff class T has a constexpr default constructor. */
4753 bool
4754 type_has_constexpr_default_constructor (tree t)
4756 tree fns;
4758 if (!CLASS_TYPE_P (t))
4760 /* The caller should have stripped an enclosing array. */
4761 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
4762 return false;
4764 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
4766 if (!TYPE_HAS_COMPLEX_DFLT (t))
4767 return trivial_default_constructor_is_constexpr (t);
4768 /* Non-trivial, we need to check subobject constructors. */
4769 lazily_declare_fn (sfk_constructor, t);
4771 fns = locate_ctor (t);
4772 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
4775 /* Returns true iff class TYPE has a virtual destructor. */
4777 bool
4778 type_has_virtual_destructor (tree type)
4780 tree dtor;
4782 if (!CLASS_TYPE_P (type))
4783 return false;
4785 gcc_assert (COMPLETE_TYPE_P (type));
4786 dtor = CLASSTYPE_DESTRUCTORS (type);
4787 return (dtor && DECL_VIRTUAL_P (dtor));
4790 /* Returns true iff class T has a move constructor. */
4792 bool
4793 type_has_move_constructor (tree t)
4795 tree fns;
4797 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4799 gcc_assert (COMPLETE_TYPE_P (t));
4800 lazily_declare_fn (sfk_move_constructor, t);
4803 if (!CLASSTYPE_METHOD_VEC (t))
4804 return false;
4806 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4807 if (move_fn_p (OVL_CURRENT (fns)))
4808 return true;
4810 return false;
4813 /* Returns true iff class T has a move assignment operator. */
4815 bool
4816 type_has_move_assign (tree t)
4818 tree fns;
4820 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4822 gcc_assert (COMPLETE_TYPE_P (t));
4823 lazily_declare_fn (sfk_move_assignment, t);
4826 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4827 fns; fns = OVL_NEXT (fns))
4828 if (move_fn_p (OVL_CURRENT (fns)))
4829 return true;
4831 return false;
4834 /* Returns true iff class T has a move constructor that was explicitly
4835 declared in the class body. Note that this is different from
4836 "user-provided", which doesn't include functions that are defaulted in
4837 the class. */
4839 bool
4840 type_has_user_declared_move_constructor (tree t)
4842 tree fns;
4844 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4845 return false;
4847 if (!CLASSTYPE_METHOD_VEC (t))
4848 return false;
4850 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4852 tree fn = OVL_CURRENT (fns);
4853 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4854 return true;
4857 return false;
4860 /* Returns true iff class T has a move assignment operator that was
4861 explicitly declared in the class body. */
4863 bool
4864 type_has_user_declared_move_assign (tree t)
4866 tree fns;
4868 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
4869 return false;
4871 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
4872 fns; fns = OVL_NEXT (fns))
4874 tree fn = OVL_CURRENT (fns);
4875 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
4876 return true;
4879 return false;
4882 /* Nonzero if we need to build up a constructor call when initializing an
4883 object of this class, either because it has a user-provided constructor
4884 or because it doesn't have a default constructor (so we need to give an
4885 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
4886 what you care about is whether or not an object can be produced by a
4887 constructor (e.g. so we don't set TREE_READONLY on const variables of
4888 such type); use this function when what you care about is whether or not
4889 to try to call a constructor to create an object. The latter case is
4890 the former plus some cases of constructors that cannot be called. */
4892 bool
4893 type_build_ctor_call (tree t)
4895 tree inner;
4896 if (TYPE_NEEDS_CONSTRUCTING (t))
4897 return true;
4898 inner = strip_array_types (t);
4899 return (CLASS_TYPE_P (inner) && !TYPE_HAS_DEFAULT_CONSTRUCTOR (inner)
4900 && !ANON_AGGR_TYPE_P (inner));
4903 /* Remove all zero-width bit-fields from T. */
4905 static void
4906 remove_zero_width_bit_fields (tree t)
4908 tree *fieldsp;
4910 fieldsp = &TYPE_FIELDS (t);
4911 while (*fieldsp)
4913 if (TREE_CODE (*fieldsp) == FIELD_DECL
4914 && DECL_C_BIT_FIELD (*fieldsp)
4915 /* We should not be confused by the fact that grokbitfield
4916 temporarily sets the width of the bit field into
4917 DECL_INITIAL (*fieldsp).
4918 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
4919 to that width. */
4920 && integer_zerop (DECL_SIZE (*fieldsp)))
4921 *fieldsp = DECL_CHAIN (*fieldsp);
4922 else
4923 fieldsp = &DECL_CHAIN (*fieldsp);
4927 /* Returns TRUE iff we need a cookie when dynamically allocating an
4928 array whose elements have the indicated class TYPE. */
4930 static bool
4931 type_requires_array_cookie (tree type)
4933 tree fns;
4934 bool has_two_argument_delete_p = false;
4936 gcc_assert (CLASS_TYPE_P (type));
4938 /* If there's a non-trivial destructor, we need a cookie. In order
4939 to iterate through the array calling the destructor for each
4940 element, we'll have to know how many elements there are. */
4941 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4942 return true;
4944 /* If the usual deallocation function is a two-argument whose second
4945 argument is of type `size_t', then we have to pass the size of
4946 the array to the deallocation function, so we will need to store
4947 a cookie. */
4948 fns = lookup_fnfields (TYPE_BINFO (type),
4949 ansi_opname (VEC_DELETE_EXPR),
4950 /*protect=*/0);
4951 /* If there are no `operator []' members, or the lookup is
4952 ambiguous, then we don't need a cookie. */
4953 if (!fns || fns == error_mark_node)
4954 return false;
4955 /* Loop through all of the functions. */
4956 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4958 tree fn;
4959 tree second_parm;
4961 /* Select the current function. */
4962 fn = OVL_CURRENT (fns);
4963 /* See if this function is a one-argument delete function. If
4964 it is, then it will be the usual deallocation function. */
4965 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4966 if (second_parm == void_list_node)
4967 return false;
4968 /* Do not consider this function if its second argument is an
4969 ellipsis. */
4970 if (!second_parm)
4971 continue;
4972 /* Otherwise, if we have a two-argument function and the second
4973 argument is `size_t', it will be the usual deallocation
4974 function -- unless there is one-argument function, too. */
4975 if (TREE_CHAIN (second_parm) == void_list_node
4976 && same_type_p (TREE_VALUE (second_parm), size_type_node))
4977 has_two_argument_delete_p = true;
4980 return has_two_argument_delete_p;
4983 /* Finish computing the `literal type' property of class type T.
4985 At this point, we have already processed base classes and
4986 non-static data members. We need to check whether the copy
4987 constructor is trivial, the destructor is trivial, and there
4988 is a trivial default constructor or at least one constexpr
4989 constructor other than the copy constructor. */
4991 static void
4992 finalize_literal_type_property (tree t)
4994 tree fn;
4996 if (cxx_dialect < cxx0x
4997 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
4998 CLASSTYPE_LITERAL_P (t) = false;
4999 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5000 && CLASSTYPE_NON_AGGREGATE (t)
5001 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5002 CLASSTYPE_LITERAL_P (t) = false;
5004 if (!CLASSTYPE_LITERAL_P (t))
5005 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5006 if (DECL_DECLARED_CONSTEXPR_P (fn)
5007 && TREE_CODE (fn) != TEMPLATE_DECL
5008 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5009 && !DECL_CONSTRUCTOR_P (fn))
5011 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5012 if (!DECL_GENERATED_P (fn))
5014 error ("enclosing class of constexpr non-static member "
5015 "function %q+#D is not a literal type", fn);
5016 explain_non_literal_class (t);
5021 /* T is a non-literal type used in a context which requires a constant
5022 expression. Explain why it isn't literal. */
5024 void
5025 explain_non_literal_class (tree t)
5027 static struct pointer_set_t *diagnosed;
5029 if (!CLASS_TYPE_P (t))
5030 return;
5031 t = TYPE_MAIN_VARIANT (t);
5033 if (diagnosed == NULL)
5034 diagnosed = pointer_set_create ();
5035 if (pointer_set_insert (diagnosed, t) != 0)
5036 /* Already explained. */
5037 return;
5039 inform (0, "%q+T is not literal because:", t);
5040 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5041 inform (0, " %q+T has a non-trivial destructor", t);
5042 else if (CLASSTYPE_NON_AGGREGATE (t)
5043 && !TYPE_HAS_TRIVIAL_DFLT (t)
5044 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5046 inform (0, " %q+T is not an aggregate, does not have a trivial "
5047 "default constructor, and has no constexpr constructor that "
5048 "is not a copy or move constructor", t);
5049 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5050 && !type_has_user_provided_default_constructor (t))
5052 /* Note that we can't simply call locate_ctor because when the
5053 constructor is deleted it just returns NULL_TREE. */
5054 tree fns;
5055 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5057 tree fn = OVL_CURRENT (fns);
5058 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5060 parms = skip_artificial_parms_for (fn, parms);
5062 if (sufficient_parms_p (parms))
5064 if (DECL_DELETED_FN (fn))
5065 maybe_explain_implicit_delete (fn);
5066 else
5067 explain_invalid_constexpr_fn (fn);
5068 break;
5073 else
5075 tree binfo, base_binfo, field; int i;
5076 for (binfo = TYPE_BINFO (t), i = 0;
5077 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5079 tree basetype = TREE_TYPE (base_binfo);
5080 if (!CLASSTYPE_LITERAL_P (basetype))
5082 inform (0, " base class %qT of %q+T is non-literal",
5083 basetype, t);
5084 explain_non_literal_class (basetype);
5085 return;
5088 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5090 tree ftype;
5091 if (TREE_CODE (field) != FIELD_DECL)
5092 continue;
5093 ftype = TREE_TYPE (field);
5094 if (!literal_type_p (ftype))
5096 inform (0, " non-static data member %q+D has "
5097 "non-literal type", field);
5098 if (CLASS_TYPE_P (ftype))
5099 explain_non_literal_class (ftype);
5105 /* Check the validity of the bases and members declared in T. Add any
5106 implicitly-generated functions (like copy-constructors and
5107 assignment operators). Compute various flag bits (like
5108 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5109 level: i.e., independently of the ABI in use. */
5111 static void
5112 check_bases_and_members (tree t)
5114 /* Nonzero if the implicitly generated copy constructor should take
5115 a non-const reference argument. */
5116 int cant_have_const_ctor;
5117 /* Nonzero if the implicitly generated assignment operator
5118 should take a non-const reference argument. */
5119 int no_const_asn_ref;
5120 tree access_decls;
5121 bool saved_complex_asn_ref;
5122 bool saved_nontrivial_dtor;
5123 tree fn;
5125 /* By default, we use const reference arguments and generate default
5126 constructors. */
5127 cant_have_const_ctor = 0;
5128 no_const_asn_ref = 0;
5130 /* Deduce noexcept on destructors. */
5131 if (cxx_dialect >= cxx0x)
5132 deduce_noexcept_on_destructors (t);
5134 /* Check all the base-classes. */
5135 check_bases (t, &cant_have_const_ctor,
5136 &no_const_asn_ref);
5138 /* Check all the method declarations. */
5139 check_methods (t);
5141 /* Save the initial values of these flags which only indicate whether
5142 or not the class has user-provided functions. As we analyze the
5143 bases and members we can set these flags for other reasons. */
5144 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5145 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5147 /* Check all the data member declarations. We cannot call
5148 check_field_decls until we have called check_bases check_methods,
5149 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5150 being set appropriately. */
5151 check_field_decls (t, &access_decls,
5152 &cant_have_const_ctor,
5153 &no_const_asn_ref);
5155 /* A nearly-empty class has to be vptr-containing; a nearly empty
5156 class contains just a vptr. */
5157 if (!TYPE_CONTAINS_VPTR_P (t))
5158 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5160 /* Do some bookkeeping that will guide the generation of implicitly
5161 declared member functions. */
5162 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5163 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5164 /* We need to call a constructor for this class if it has a
5165 user-provided constructor, or if the default constructor is going
5166 to initialize the vptr. (This is not an if-and-only-if;
5167 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5168 themselves need constructing.) */
5169 TYPE_NEEDS_CONSTRUCTING (t)
5170 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5171 /* [dcl.init.aggr]
5173 An aggregate is an array or a class with no user-provided
5174 constructors ... and no virtual functions.
5176 Again, other conditions for being an aggregate are checked
5177 elsewhere. */
5178 CLASSTYPE_NON_AGGREGATE (t)
5179 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5180 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5181 retain the old definition internally for ABI reasons. */
5182 CLASSTYPE_NON_LAYOUT_POD_P (t)
5183 |= (CLASSTYPE_NON_AGGREGATE (t)
5184 || saved_nontrivial_dtor || saved_complex_asn_ref);
5185 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5186 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5187 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5188 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5190 /* If the class has no user-declared constructor, but does have
5191 non-static const or reference data members that can never be
5192 initialized, issue a warning. */
5193 if (warn_uninitialized
5194 /* Classes with user-declared constructors are presumed to
5195 initialize these members. */
5196 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5197 /* Aggregates can be initialized with brace-enclosed
5198 initializers. */
5199 && CLASSTYPE_NON_AGGREGATE (t))
5201 tree field;
5203 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5205 tree type;
5207 if (TREE_CODE (field) != FIELD_DECL
5208 || DECL_INITIAL (field) != NULL_TREE)
5209 continue;
5211 type = TREE_TYPE (field);
5212 if (TREE_CODE (type) == REFERENCE_TYPE)
5213 warning (OPT_Wuninitialized, "non-static reference %q+#D "
5214 "in class without a constructor", field);
5215 else if (CP_TYPE_CONST_P (type)
5216 && (!CLASS_TYPE_P (type)
5217 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5218 warning (OPT_Wuninitialized, "non-static const member %q+#D "
5219 "in class without a constructor", field);
5223 /* Synthesize any needed methods. */
5224 add_implicitly_declared_members (t, &access_decls,
5225 cant_have_const_ctor,
5226 no_const_asn_ref);
5228 /* Check defaulted declarations here so we have cant_have_const_ctor
5229 and don't need to worry about clones. */
5230 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5231 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5233 int copy = copy_fn_p (fn);
5234 if (copy > 0)
5236 bool imp_const_p
5237 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5238 : !no_const_asn_ref);
5239 bool fn_const_p = (copy == 2);
5241 if (fn_const_p && !imp_const_p)
5242 /* If the function is defaulted outside the class, we just
5243 give the synthesis error. */
5244 error ("%q+D declared to take const reference, but implicit "
5245 "declaration would take non-const", fn);
5247 defaulted_late_check (fn);
5250 if (LAMBDA_TYPE_P (t))
5252 /* "The closure type associated with a lambda-expression has a deleted
5253 default constructor and a deleted copy assignment operator." */
5254 TYPE_NEEDS_CONSTRUCTING (t) = 1;
5255 TYPE_HAS_COMPLEX_DFLT (t) = 1;
5256 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
5257 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 0;
5259 /* "This class type is not an aggregate." */
5260 CLASSTYPE_NON_AGGREGATE (t) = 1;
5263 /* Compute the 'literal type' property before we
5264 do anything with non-static member functions. */
5265 finalize_literal_type_property (t);
5267 /* Create the in-charge and not-in-charge variants of constructors
5268 and destructors. */
5269 clone_constructors_and_destructors (t);
5271 /* Process the using-declarations. */
5272 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5273 handle_using_decl (TREE_VALUE (access_decls), t);
5275 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5276 finish_struct_methods (t);
5278 /* Figure out whether or not we will need a cookie when dynamically
5279 allocating an array of this type. */
5280 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5281 = type_requires_array_cookie (t);
5284 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5285 accordingly. If a new vfield was created (because T doesn't have a
5286 primary base class), then the newly created field is returned. It
5287 is not added to the TYPE_FIELDS list; it is the caller's
5288 responsibility to do that. Accumulate declared virtual functions
5289 on VIRTUALS_P. */
5291 static tree
5292 create_vtable_ptr (tree t, tree* virtuals_p)
5294 tree fn;
5296 /* Collect the virtual functions declared in T. */
5297 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5298 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5299 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5301 tree new_virtual = make_node (TREE_LIST);
5303 BV_FN (new_virtual) = fn;
5304 BV_DELTA (new_virtual) = integer_zero_node;
5305 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5307 TREE_CHAIN (new_virtual) = *virtuals_p;
5308 *virtuals_p = new_virtual;
5311 /* If we couldn't find an appropriate base class, create a new field
5312 here. Even if there weren't any new virtual functions, we might need a
5313 new virtual function table if we're supposed to include vptrs in
5314 all classes that need them. */
5315 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5317 /* We build this decl with vtbl_ptr_type_node, which is a
5318 `vtable_entry_type*'. It might seem more precise to use
5319 `vtable_entry_type (*)[N]' where N is the number of virtual
5320 functions. However, that would require the vtable pointer in
5321 base classes to have a different type than the vtable pointer
5322 in derived classes. We could make that happen, but that
5323 still wouldn't solve all the problems. In particular, the
5324 type-based alias analysis code would decide that assignments
5325 to the base class vtable pointer can't alias assignments to
5326 the derived class vtable pointer, since they have different
5327 types. Thus, in a derived class destructor, where the base
5328 class constructor was inlined, we could generate bad code for
5329 setting up the vtable pointer.
5331 Therefore, we use one type for all vtable pointers. We still
5332 use a type-correct type; it's just doesn't indicate the array
5333 bounds. That's better than using `void*' or some such; it's
5334 cleaner, and it let's the alias analysis code know that these
5335 stores cannot alias stores to void*! */
5336 tree field;
5338 field = build_decl (input_location,
5339 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5340 DECL_VIRTUAL_P (field) = 1;
5341 DECL_ARTIFICIAL (field) = 1;
5342 DECL_FIELD_CONTEXT (field) = t;
5343 DECL_FCONTEXT (field) = t;
5344 if (TYPE_PACKED (t))
5345 DECL_PACKED (field) = 1;
5347 TYPE_VFIELD (t) = field;
5349 /* This class is non-empty. */
5350 CLASSTYPE_EMPTY_P (t) = 0;
5352 return field;
5355 return NULL_TREE;
5358 /* Add OFFSET to all base types of BINFO which is a base in the
5359 hierarchy dominated by T.
5361 OFFSET, which is a type offset, is number of bytes. */
5363 static void
5364 propagate_binfo_offsets (tree binfo, tree offset)
5366 int i;
5367 tree primary_binfo;
5368 tree base_binfo;
5370 /* Update BINFO's offset. */
5371 BINFO_OFFSET (binfo)
5372 = convert (sizetype,
5373 size_binop (PLUS_EXPR,
5374 convert (ssizetype, BINFO_OFFSET (binfo)),
5375 offset));
5377 /* Find the primary base class. */
5378 primary_binfo = get_primary_binfo (binfo);
5380 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5381 propagate_binfo_offsets (primary_binfo, offset);
5383 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5384 downwards. */
5385 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5387 /* Don't do the primary base twice. */
5388 if (base_binfo == primary_binfo)
5389 continue;
5391 if (BINFO_VIRTUAL_P (base_binfo))
5392 continue;
5394 propagate_binfo_offsets (base_binfo, offset);
5398 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5399 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5400 empty subobjects of T. */
5402 static void
5403 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5405 tree vbase;
5406 tree t = rli->t;
5407 bool first_vbase = true;
5408 tree *next_field;
5410 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5411 return;
5413 if (!abi_version_at_least(2))
5415 /* In G++ 3.2, we incorrectly rounded the size before laying out
5416 the virtual bases. */
5417 finish_record_layout (rli, /*free_p=*/false);
5418 #ifdef STRUCTURE_SIZE_BOUNDARY
5419 /* Packed structures don't need to have minimum size. */
5420 if (! TYPE_PACKED (t))
5421 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
5422 #endif
5423 rli->offset = TYPE_SIZE_UNIT (t);
5424 rli->bitpos = bitsize_zero_node;
5425 rli->record_align = TYPE_ALIGN (t);
5428 /* Find the last field. The artificial fields created for virtual
5429 bases will go after the last extant field to date. */
5430 next_field = &TYPE_FIELDS (t);
5431 while (*next_field)
5432 next_field = &DECL_CHAIN (*next_field);
5434 /* Go through the virtual bases, allocating space for each virtual
5435 base that is not already a primary base class. These are
5436 allocated in inheritance graph order. */
5437 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5439 if (!BINFO_VIRTUAL_P (vbase))
5440 continue;
5442 if (!BINFO_PRIMARY_P (vbase))
5444 tree basetype = TREE_TYPE (vbase);
5446 /* This virtual base is not a primary base of any class in the
5447 hierarchy, so we have to add space for it. */
5448 next_field = build_base_field (rli, vbase,
5449 offsets, next_field);
5451 /* If the first virtual base might have been placed at a
5452 lower address, had we started from CLASSTYPE_SIZE, rather
5453 than TYPE_SIZE, issue a warning. There can be both false
5454 positives and false negatives from this warning in rare
5455 cases; to deal with all the possibilities would probably
5456 require performing both layout algorithms and comparing
5457 the results which is not particularly tractable. */
5458 if (warn_abi
5459 && first_vbase
5460 && (tree_int_cst_lt
5461 (size_binop (CEIL_DIV_EXPR,
5462 round_up_loc (input_location,
5463 CLASSTYPE_SIZE (t),
5464 CLASSTYPE_ALIGN (basetype)),
5465 bitsize_unit_node),
5466 BINFO_OFFSET (vbase))))
5467 warning (OPT_Wabi,
5468 "offset of virtual base %qT is not ABI-compliant and "
5469 "may change in a future version of GCC",
5470 basetype);
5472 first_vbase = false;
5477 /* Returns the offset of the byte just past the end of the base class
5478 BINFO. */
5480 static tree
5481 end_of_base (tree binfo)
5483 tree size;
5485 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5486 size = TYPE_SIZE_UNIT (char_type_node);
5487 else if (is_empty_class (BINFO_TYPE (binfo)))
5488 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5489 allocate some space for it. It cannot have virtual bases, so
5490 TYPE_SIZE_UNIT is fine. */
5491 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5492 else
5493 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5495 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5498 /* Returns the offset of the byte just past the end of the base class
5499 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5500 only non-virtual bases are included. */
5502 static tree
5503 end_of_class (tree t, int include_virtuals_p)
5505 tree result = size_zero_node;
5506 VEC(tree,gc) *vbases;
5507 tree binfo;
5508 tree base_binfo;
5509 tree offset;
5510 int i;
5512 for (binfo = TYPE_BINFO (t), i = 0;
5513 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5515 if (!include_virtuals_p
5516 && BINFO_VIRTUAL_P (base_binfo)
5517 && (!BINFO_PRIMARY_P (base_binfo)
5518 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5519 continue;
5521 offset = end_of_base (base_binfo);
5522 if (INT_CST_LT_UNSIGNED (result, offset))
5523 result = offset;
5526 /* G++ 3.2 did not check indirect virtual bases. */
5527 if (abi_version_at_least (2) && include_virtuals_p)
5528 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5529 VEC_iterate (tree, vbases, i, base_binfo); i++)
5531 offset = end_of_base (base_binfo);
5532 if (INT_CST_LT_UNSIGNED (result, offset))
5533 result = offset;
5536 return result;
5539 /* Warn about bases of T that are inaccessible because they are
5540 ambiguous. For example:
5542 struct S {};
5543 struct T : public S {};
5544 struct U : public S, public T {};
5546 Here, `(S*) new U' is not allowed because there are two `S'
5547 subobjects of U. */
5549 static void
5550 warn_about_ambiguous_bases (tree t)
5552 int i;
5553 VEC(tree,gc) *vbases;
5554 tree basetype;
5555 tree binfo;
5556 tree base_binfo;
5558 /* If there are no repeated bases, nothing can be ambiguous. */
5559 if (!CLASSTYPE_REPEATED_BASE_P (t))
5560 return;
5562 /* Check direct bases. */
5563 for (binfo = TYPE_BINFO (t), i = 0;
5564 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5566 basetype = BINFO_TYPE (base_binfo);
5568 if (!uniquely_derived_from_p (basetype, t))
5569 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5570 basetype, t);
5573 /* Check for ambiguous virtual bases. */
5574 if (extra_warnings)
5575 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5576 VEC_iterate (tree, vbases, i, binfo); i++)
5578 basetype = BINFO_TYPE (binfo);
5580 if (!uniquely_derived_from_p (basetype, t))
5581 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5582 "to ambiguity", basetype, t);
5586 /* Compare two INTEGER_CSTs K1 and K2. */
5588 static int
5589 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5591 return tree_int_cst_compare ((tree) k1, (tree) k2);
5594 /* Increase the size indicated in RLI to account for empty classes
5595 that are "off the end" of the class. */
5597 static void
5598 include_empty_classes (record_layout_info rli)
5600 tree eoc;
5601 tree rli_size;
5603 /* It might be the case that we grew the class to allocate a
5604 zero-sized base class. That won't be reflected in RLI, yet,
5605 because we are willing to overlay multiple bases at the same
5606 offset. However, now we need to make sure that RLI is big enough
5607 to reflect the entire class. */
5608 eoc = end_of_class (rli->t,
5609 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5610 rli_size = rli_size_unit_so_far (rli);
5611 if (TREE_CODE (rli_size) == INTEGER_CST
5612 && INT_CST_LT_UNSIGNED (rli_size, eoc))
5614 if (!abi_version_at_least (2))
5615 /* In version 1 of the ABI, the size of a class that ends with
5616 a bitfield was not rounded up to a whole multiple of a
5617 byte. Because rli_size_unit_so_far returns only the number
5618 of fully allocated bytes, any extra bits were not included
5619 in the size. */
5620 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
5621 else
5622 /* The size should have been rounded to a whole byte. */
5623 gcc_assert (tree_int_cst_equal
5624 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5625 rli->bitpos
5626 = size_binop (PLUS_EXPR,
5627 rli->bitpos,
5628 size_binop (MULT_EXPR,
5629 convert (bitsizetype,
5630 size_binop (MINUS_EXPR,
5631 eoc, rli_size)),
5632 bitsize_int (BITS_PER_UNIT)));
5633 normalize_rli (rli);
5637 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5638 BINFO_OFFSETs for all of the base-classes. Position the vtable
5639 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5641 static void
5642 layout_class_type (tree t, tree *virtuals_p)
5644 tree non_static_data_members;
5645 tree field;
5646 tree vptr;
5647 record_layout_info rli;
5648 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5649 types that appear at that offset. */
5650 splay_tree empty_base_offsets;
5651 /* True if the last field layed out was a bit-field. */
5652 bool last_field_was_bitfield = false;
5653 /* The location at which the next field should be inserted. */
5654 tree *next_field;
5655 /* T, as a base class. */
5656 tree base_t;
5658 /* Keep track of the first non-static data member. */
5659 non_static_data_members = TYPE_FIELDS (t);
5661 /* Start laying out the record. */
5662 rli = start_record_layout (t);
5664 /* Mark all the primary bases in the hierarchy. */
5665 determine_primary_bases (t);
5667 /* Create a pointer to our virtual function table. */
5668 vptr = create_vtable_ptr (t, virtuals_p);
5670 /* The vptr is always the first thing in the class. */
5671 if (vptr)
5673 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
5674 TYPE_FIELDS (t) = vptr;
5675 next_field = &DECL_CHAIN (vptr);
5676 place_field (rli, vptr);
5678 else
5679 next_field = &TYPE_FIELDS (t);
5681 /* Build FIELD_DECLs for all of the non-virtual base-types. */
5682 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
5683 NULL, NULL);
5684 build_base_fields (rli, empty_base_offsets, next_field);
5686 /* Layout the non-static data members. */
5687 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
5689 tree type;
5690 tree padding;
5692 /* We still pass things that aren't non-static data members to
5693 the back end, in case it wants to do something with them. */
5694 if (TREE_CODE (field) != FIELD_DECL)
5696 place_field (rli, field);
5697 /* If the static data member has incomplete type, keep track
5698 of it so that it can be completed later. (The handling
5699 of pending statics in finish_record_layout is
5700 insufficient; consider:
5702 struct S1;
5703 struct S2 { static S1 s1; };
5705 At this point, finish_record_layout will be called, but
5706 S1 is still incomplete.) */
5707 if (TREE_CODE (field) == VAR_DECL)
5709 maybe_register_incomplete_var (field);
5710 /* The visibility of static data members is determined
5711 at their point of declaration, not their point of
5712 definition. */
5713 determine_visibility (field);
5715 continue;
5718 type = TREE_TYPE (field);
5719 if (type == error_mark_node)
5720 continue;
5722 padding = NULL_TREE;
5724 /* If this field is a bit-field whose width is greater than its
5725 type, then there are some special rules for allocating
5726 it. */
5727 if (DECL_C_BIT_FIELD (field)
5728 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5730 unsigned int itk;
5731 tree integer_type;
5732 bool was_unnamed_p = false;
5733 /* We must allocate the bits as if suitably aligned for the
5734 longest integer type that fits in this many bits. type
5735 of the field. Then, we are supposed to use the left over
5736 bits as additional padding. */
5737 for (itk = itk_char; itk != itk_none; ++itk)
5738 if (integer_types[itk] != NULL_TREE
5739 && (INT_CST_LT (size_int (MAX_FIXED_MODE_SIZE),
5740 TYPE_SIZE (integer_types[itk]))
5741 || INT_CST_LT (DECL_SIZE (field),
5742 TYPE_SIZE (integer_types[itk]))))
5743 break;
5745 /* ITK now indicates a type that is too large for the
5746 field. We have to back up by one to find the largest
5747 type that fits. */
5750 --itk;
5751 integer_type = integer_types[itk];
5752 } while (itk > 0 && integer_type == NULL_TREE);
5754 /* Figure out how much additional padding is required. GCC
5755 3.2 always created a padding field, even if it had zero
5756 width. */
5757 if (!abi_version_at_least (2)
5758 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
5760 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
5761 /* In a union, the padding field must have the full width
5762 of the bit-field; all fields start at offset zero. */
5763 padding = DECL_SIZE (field);
5764 else
5766 if (TREE_CODE (t) == UNION_TYPE)
5767 warning (OPT_Wabi, "size assigned to %qT may not be "
5768 "ABI-compliant and may change in a future "
5769 "version of GCC",
5771 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5772 TYPE_SIZE (integer_type));
5775 #ifdef PCC_BITFIELD_TYPE_MATTERS
5776 /* An unnamed bitfield does not normally affect the
5777 alignment of the containing class on a target where
5778 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
5779 make any exceptions for unnamed bitfields when the
5780 bitfields are longer than their types. Therefore, we
5781 temporarily give the field a name. */
5782 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
5784 was_unnamed_p = true;
5785 DECL_NAME (field) = make_anon_name ();
5787 #endif
5788 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5789 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5790 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5791 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5792 empty_base_offsets);
5793 if (was_unnamed_p)
5794 DECL_NAME (field) = NULL_TREE;
5795 /* Now that layout has been performed, set the size of the
5796 field to the size of its declared type; the rest of the
5797 field is effectively invisible. */
5798 DECL_SIZE (field) = TYPE_SIZE (type);
5799 /* We must also reset the DECL_MODE of the field. */
5800 if (abi_version_at_least (2))
5801 DECL_MODE (field) = TYPE_MODE (type);
5802 else if (warn_abi
5803 && DECL_MODE (field) != TYPE_MODE (type))
5804 /* Versions of G++ before G++ 3.4 did not reset the
5805 DECL_MODE. */
5806 warning (OPT_Wabi,
5807 "the offset of %qD may not be ABI-compliant and may "
5808 "change in a future version of GCC", field);
5810 else
5811 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5812 empty_base_offsets);
5814 /* Remember the location of any empty classes in FIELD. */
5815 if (abi_version_at_least (2))
5816 record_subobject_offsets (TREE_TYPE (field),
5817 byte_position(field),
5818 empty_base_offsets,
5819 /*is_data_member=*/true);
5821 /* If a bit-field does not immediately follow another bit-field,
5822 and yet it starts in the middle of a byte, we have failed to
5823 comply with the ABI. */
5824 if (warn_abi
5825 && DECL_C_BIT_FIELD (field)
5826 /* The TREE_NO_WARNING flag gets set by Objective-C when
5827 laying out an Objective-C class. The ObjC ABI differs
5828 from the C++ ABI, and so we do not want a warning
5829 here. */
5830 && !TREE_NO_WARNING (field)
5831 && !last_field_was_bitfield
5832 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
5833 DECL_FIELD_BIT_OFFSET (field),
5834 bitsize_unit_node)))
5835 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
5836 "change in a future version of GCC", field);
5838 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
5839 offset of the field. */
5840 if (warn_abi
5841 && !abi_version_at_least (2)
5842 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
5843 byte_position (field))
5844 && contains_empty_class_p (TREE_TYPE (field)))
5845 warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
5846 "classes to be placed at different locations in a "
5847 "future version of GCC", field);
5849 /* The middle end uses the type of expressions to determine the
5850 possible range of expression values. In order to optimize
5851 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
5852 must be made aware of the width of "i", via its type.
5854 Because C++ does not have integer types of arbitrary width,
5855 we must (for the purposes of the front end) convert from the
5856 type assigned here to the declared type of the bitfield
5857 whenever a bitfield expression is used as an rvalue.
5858 Similarly, when assigning a value to a bitfield, the value
5859 must be converted to the type given the bitfield here. */
5860 if (DECL_C_BIT_FIELD (field))
5862 unsigned HOST_WIDE_INT width;
5863 tree ftype = TREE_TYPE (field);
5864 width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
5865 if (width != TYPE_PRECISION (ftype))
5867 TREE_TYPE (field)
5868 = c_build_bitfield_integer_type (width,
5869 TYPE_UNSIGNED (ftype));
5870 TREE_TYPE (field)
5871 = cp_build_qualified_type (TREE_TYPE (field),
5872 cp_type_quals (ftype));
5876 /* If we needed additional padding after this field, add it
5877 now. */
5878 if (padding)
5880 tree padding_field;
5882 padding_field = build_decl (input_location,
5883 FIELD_DECL,
5884 NULL_TREE,
5885 char_type_node);
5886 DECL_BIT_FIELD (padding_field) = 1;
5887 DECL_SIZE (padding_field) = padding;
5888 DECL_CONTEXT (padding_field) = t;
5889 DECL_ARTIFICIAL (padding_field) = 1;
5890 DECL_IGNORED_P (padding_field) = 1;
5891 layout_nonempty_base_or_field (rli, padding_field,
5892 NULL_TREE,
5893 empty_base_offsets);
5896 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
5899 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
5901 /* Make sure that we are on a byte boundary so that the size of
5902 the class without virtual bases will always be a round number
5903 of bytes. */
5904 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
5905 normalize_rli (rli);
5908 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
5909 padding. */
5910 if (!abi_version_at_least (2))
5911 include_empty_classes(rli);
5913 /* Delete all zero-width bit-fields from the list of fields. Now
5914 that the type is laid out they are no longer important. */
5915 remove_zero_width_bit_fields (t);
5917 /* Create the version of T used for virtual bases. We do not use
5918 make_class_type for this version; this is an artificial type. For
5919 a POD type, we just reuse T. */
5920 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
5922 base_t = make_node (TREE_CODE (t));
5924 /* Set the size and alignment for the new type. In G++ 3.2, all
5925 empty classes were considered to have size zero when used as
5926 base classes. */
5927 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
5929 TYPE_SIZE (base_t) = bitsize_zero_node;
5930 TYPE_SIZE_UNIT (base_t) = size_zero_node;
5931 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
5932 warning (OPT_Wabi,
5933 "layout of classes derived from empty class %qT "
5934 "may change in a future version of GCC",
5937 else
5939 tree eoc;
5941 /* If the ABI version is not at least two, and the last
5942 field was a bit-field, RLI may not be on a byte
5943 boundary. In particular, rli_size_unit_so_far might
5944 indicate the last complete byte, while rli_size_so_far
5945 indicates the total number of bits used. Therefore,
5946 rli_size_so_far, rather than rli_size_unit_so_far, is
5947 used to compute TYPE_SIZE_UNIT. */
5948 eoc = end_of_class (t, /*include_virtuals_p=*/0);
5949 TYPE_SIZE_UNIT (base_t)
5950 = size_binop (MAX_EXPR,
5951 convert (sizetype,
5952 size_binop (CEIL_DIV_EXPR,
5953 rli_size_so_far (rli),
5954 bitsize_int (BITS_PER_UNIT))),
5955 eoc);
5956 TYPE_SIZE (base_t)
5957 = size_binop (MAX_EXPR,
5958 rli_size_so_far (rli),
5959 size_binop (MULT_EXPR,
5960 convert (bitsizetype, eoc),
5961 bitsize_int (BITS_PER_UNIT)));
5963 TYPE_ALIGN (base_t) = rli->record_align;
5964 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
5966 /* Copy the fields from T. */
5967 next_field = &TYPE_FIELDS (base_t);
5968 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5969 if (TREE_CODE (field) == FIELD_DECL)
5971 *next_field = build_decl (input_location,
5972 FIELD_DECL,
5973 DECL_NAME (field),
5974 TREE_TYPE (field));
5975 DECL_CONTEXT (*next_field) = base_t;
5976 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
5977 DECL_FIELD_BIT_OFFSET (*next_field)
5978 = DECL_FIELD_BIT_OFFSET (field);
5979 DECL_SIZE (*next_field) = DECL_SIZE (field);
5980 DECL_MODE (*next_field) = DECL_MODE (field);
5981 next_field = &DECL_CHAIN (*next_field);
5984 /* Record the base version of the type. */
5985 CLASSTYPE_AS_BASE (t) = base_t;
5986 TYPE_CONTEXT (base_t) = t;
5988 else
5989 CLASSTYPE_AS_BASE (t) = t;
5991 /* Every empty class contains an empty class. */
5992 if (CLASSTYPE_EMPTY_P (t))
5993 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
5995 /* Set the TYPE_DECL for this type to contain the right
5996 value for DECL_OFFSET, so that we can use it as part
5997 of a COMPONENT_REF for multiple inheritance. */
5998 layout_decl (TYPE_MAIN_DECL (t), 0);
6000 /* Now fix up any virtual base class types that we left lying
6001 around. We must get these done before we try to lay out the
6002 virtual function table. As a side-effect, this will remove the
6003 base subobject fields. */
6004 layout_virtual_bases (rli, empty_base_offsets);
6006 /* Make sure that empty classes are reflected in RLI at this
6007 point. */
6008 include_empty_classes(rli);
6010 /* Make sure not to create any structures with zero size. */
6011 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6012 place_field (rli,
6013 build_decl (input_location,
6014 FIELD_DECL, NULL_TREE, char_type_node));
6016 /* If this is a non-POD, declaring it packed makes a difference to how it
6017 can be used as a field; don't let finalize_record_size undo it. */
6018 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6019 rli->packed_maybe_necessary = true;
6021 /* Let the back end lay out the type. */
6022 finish_record_layout (rli, /*free_p=*/true);
6024 /* Warn about bases that can't be talked about due to ambiguity. */
6025 warn_about_ambiguous_bases (t);
6027 /* Now that we're done with layout, give the base fields the real types. */
6028 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6029 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6030 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6032 /* Clean up. */
6033 splay_tree_delete (empty_base_offsets);
6035 if (CLASSTYPE_EMPTY_P (t)
6036 && tree_int_cst_lt (sizeof_biggest_empty_class,
6037 TYPE_SIZE_UNIT (t)))
6038 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6041 /* Determine the "key method" for the class type indicated by TYPE,
6042 and set CLASSTYPE_KEY_METHOD accordingly. */
6044 void
6045 determine_key_method (tree type)
6047 tree method;
6049 if (TYPE_FOR_JAVA (type)
6050 || processing_template_decl
6051 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6052 || CLASSTYPE_INTERFACE_KNOWN (type))
6053 return;
6055 /* The key method is the first non-pure virtual function that is not
6056 inline at the point of class definition. On some targets the
6057 key function may not be inline; those targets should not call
6058 this function until the end of the translation unit. */
6059 for (method = TYPE_METHODS (type); method != NULL_TREE;
6060 method = DECL_CHAIN (method))
6061 if (DECL_VINDEX (method) != NULL_TREE
6062 && ! DECL_DECLARED_INLINE_P (method)
6063 && ! DECL_PURE_VIRTUAL_P (method))
6065 CLASSTYPE_KEY_METHOD (type) = method;
6066 break;
6069 return;
6073 /* Allocate and return an instance of struct sorted_fields_type with
6074 N fields. */
6076 static struct sorted_fields_type *
6077 sorted_fields_type_new (int n)
6079 struct sorted_fields_type *sft;
6080 sft = ggc_alloc_sorted_fields_type (sizeof (struct sorted_fields_type)
6081 + n * sizeof (tree));
6082 sft->len = n;
6084 return sft;
6088 /* Perform processing required when the definition of T (a class type)
6089 is complete. */
6091 void
6092 finish_struct_1 (tree t)
6094 tree x;
6095 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6096 tree virtuals = NULL_TREE;
6098 if (COMPLETE_TYPE_P (t))
6100 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6101 error ("redefinition of %q#T", t);
6102 popclass ();
6103 return;
6106 /* If this type was previously laid out as a forward reference,
6107 make sure we lay it out again. */
6108 TYPE_SIZE (t) = NULL_TREE;
6109 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6111 /* Make assumptions about the class; we'll reset the flags if
6112 necessary. */
6113 CLASSTYPE_EMPTY_P (t) = 1;
6114 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6115 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6116 CLASSTYPE_LITERAL_P (t) = true;
6118 /* Do end-of-class semantic processing: checking the validity of the
6119 bases and members and add implicitly generated methods. */
6120 check_bases_and_members (t);
6122 /* Find the key method. */
6123 if (TYPE_CONTAINS_VPTR_P (t))
6125 /* The Itanium C++ ABI permits the key method to be chosen when
6126 the class is defined -- even though the key method so
6127 selected may later turn out to be an inline function. On
6128 some systems (such as ARM Symbian OS) the key method cannot
6129 be determined until the end of the translation unit. On such
6130 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6131 will cause the class to be added to KEYED_CLASSES. Then, in
6132 finish_file we will determine the key method. */
6133 if (targetm.cxx.key_method_may_be_inline ())
6134 determine_key_method (t);
6136 /* If a polymorphic class has no key method, we may emit the vtable
6137 in every translation unit where the class definition appears. */
6138 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6139 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6142 /* Layout the class itself. */
6143 layout_class_type (t, &virtuals);
6144 if (CLASSTYPE_AS_BASE (t) != t)
6145 /* We use the base type for trivial assignments, and hence it
6146 needs a mode. */
6147 compute_record_mode (CLASSTYPE_AS_BASE (t));
6149 virtuals = modify_all_vtables (t, nreverse (virtuals));
6151 /* If necessary, create the primary vtable for this class. */
6152 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6154 /* We must enter these virtuals into the table. */
6155 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6156 build_primary_vtable (NULL_TREE, t);
6157 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6158 /* Here we know enough to change the type of our virtual
6159 function table, but we will wait until later this function. */
6160 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6163 if (TYPE_CONTAINS_VPTR_P (t))
6165 int vindex;
6166 tree fn;
6168 if (BINFO_VTABLE (TYPE_BINFO (t)))
6169 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6170 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6171 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6173 /* Add entries for virtual functions introduced by this class. */
6174 BINFO_VIRTUALS (TYPE_BINFO (t))
6175 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6177 /* Set DECL_VINDEX for all functions declared in this class. */
6178 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6180 fn = TREE_CHAIN (fn),
6181 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6182 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6184 tree fndecl = BV_FN (fn);
6186 if (DECL_THUNK_P (fndecl))
6187 /* A thunk. We should never be calling this entry directly
6188 from this vtable -- we'd use the entry for the non
6189 thunk base function. */
6190 DECL_VINDEX (fndecl) = NULL_TREE;
6191 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6192 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6196 finish_struct_bits (t);
6197 set_method_tm_attributes (t);
6199 /* Complete the rtl for any static member objects of the type we're
6200 working on. */
6201 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6202 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
6203 && TREE_TYPE (x) != error_mark_node
6204 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6205 DECL_MODE (x) = TYPE_MODE (t);
6207 /* Done with FIELDS...now decide whether to sort these for
6208 faster lookups later.
6210 We use a small number because most searches fail (succeeding
6211 ultimately as the search bores through the inheritance
6212 hierarchy), and we want this failure to occur quickly. */
6214 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6216 /* Complain if one of the field types requires lower visibility. */
6217 constrain_class_visibility (t);
6219 /* Make the rtl for any new vtables we have created, and unmark
6220 the base types we marked. */
6221 finish_vtbls (t);
6223 /* Build the VTT for T. */
6224 build_vtt (t);
6226 /* This warning does not make sense for Java classes, since they
6227 cannot have destructors. */
6228 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
6230 tree dtor;
6232 dtor = CLASSTYPE_DESTRUCTORS (t);
6233 if (/* An implicitly declared destructor is always public. And,
6234 if it were virtual, we would have created it by now. */
6235 !dtor
6236 || (!DECL_VINDEX (dtor)
6237 && (/* public non-virtual */
6238 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
6239 || (/* non-public non-virtual with friends */
6240 (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
6241 && (CLASSTYPE_FRIEND_CLASSES (t)
6242 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
6243 warning (OPT_Wnon_virtual_dtor,
6244 "%q#T has virtual functions and accessible"
6245 " non-virtual destructor", t);
6248 complete_vars (t);
6250 if (warn_overloaded_virtual)
6251 warn_hidden (t);
6253 /* Class layout, assignment of virtual table slots, etc., is now
6254 complete. Give the back end a chance to tweak the visibility of
6255 the class or perform any other required target modifications. */
6256 targetm.cxx.adjust_class_at_definition (t);
6258 maybe_suppress_debug_info (t);
6260 dump_class_hierarchy (t);
6262 /* Finish debugging output for this type. */
6263 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6265 if (TYPE_TRANSPARENT_AGGR (t))
6267 tree field = first_field (t);
6268 if (field == NULL_TREE || error_operand_p (field))
6270 error ("type transparent class %qT does not have any fields", t);
6271 TYPE_TRANSPARENT_AGGR (t) = 0;
6273 else if (DECL_ARTIFICIAL (field))
6275 if (DECL_FIELD_IS_BASE (field))
6276 error ("type transparent class %qT has base classes", t);
6277 else
6279 gcc_checking_assert (DECL_VIRTUAL_P (field));
6280 error ("type transparent class %qT has virtual functions", t);
6282 TYPE_TRANSPARENT_AGGR (t) = 0;
6287 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6288 equal to THRESHOLD or greater than THRESHOLD. */
6290 static void
6291 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6293 int n_fields = count_fields (fields);
6294 if (n_fields >= threshold)
6296 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6297 add_fields_to_record_type (fields, field_vec, 0);
6298 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6299 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6303 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6305 void
6306 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6308 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6309 if (sorted_fields)
6311 int i;
6312 int n_fields
6313 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6314 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6316 for (i = 0; i < sorted_fields->len; ++i)
6317 field_vec->elts[i] = sorted_fields->elts[i];
6319 add_enum_fields_to_record_type (enumtype, field_vec,
6320 sorted_fields->len);
6321 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6322 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6326 /* When T was built up, the member declarations were added in reverse
6327 order. Rearrange them to declaration order. */
6329 void
6330 unreverse_member_declarations (tree t)
6332 tree next;
6333 tree prev;
6334 tree x;
6336 /* The following lists are all in reverse order. Put them in
6337 declaration order now. */
6338 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6339 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6341 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6342 reverse order, so we can't just use nreverse. */
6343 prev = NULL_TREE;
6344 for (x = TYPE_FIELDS (t);
6345 x && TREE_CODE (x) != TYPE_DECL;
6346 x = next)
6348 next = DECL_CHAIN (x);
6349 DECL_CHAIN (x) = prev;
6350 prev = x;
6352 if (prev)
6354 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6355 if (prev)
6356 TYPE_FIELDS (t) = prev;
6360 tree
6361 finish_struct (tree t, tree attributes)
6363 location_t saved_loc = input_location;
6365 /* Now that we've got all the field declarations, reverse everything
6366 as necessary. */
6367 unreverse_member_declarations (t);
6369 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6371 /* Nadger the current location so that diagnostics point to the start of
6372 the struct, not the end. */
6373 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6375 if (processing_template_decl)
6377 tree x;
6379 finish_struct_methods (t);
6380 TYPE_SIZE (t) = bitsize_zero_node;
6381 TYPE_SIZE_UNIT (t) = size_zero_node;
6383 /* We need to emit an error message if this type was used as a parameter
6384 and it is an abstract type, even if it is a template. We construct
6385 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6386 account and we call complete_vars with this type, which will check
6387 the PARM_DECLS. Note that while the type is being defined,
6388 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6389 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6390 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6391 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6392 if (DECL_PURE_VIRTUAL_P (x))
6393 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
6394 complete_vars (t);
6395 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6396 an enclosing scope is a template class, so that this function be
6397 found by lookup_fnfields_1 when the using declaration is not
6398 instantiated yet. */
6399 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6400 if (TREE_CODE (x) == USING_DECL)
6402 tree fn = strip_using_decl (x);
6403 if (is_overloaded_fn (fn))
6404 for (; fn; fn = OVL_NEXT (fn))
6405 add_method (t, OVL_CURRENT (fn), x);
6408 /* Remember current #pragma pack value. */
6409 TYPE_PRECISION (t) = maximum_field_alignment;
6411 /* Fix up any variants we've already built. */
6412 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6414 TYPE_SIZE (x) = TYPE_SIZE (t);
6415 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6416 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6417 TYPE_METHODS (x) = TYPE_METHODS (t);
6420 else
6421 finish_struct_1 (t);
6423 input_location = saved_loc;
6425 TYPE_BEING_DEFINED (t) = 0;
6427 if (current_class_type)
6428 popclass ();
6429 else
6430 error ("trying to finish struct, but kicked out due to previous parse errors");
6432 if (processing_template_decl && at_function_scope_p ()
6433 /* Lambdas are defined by the LAMBDA_EXPR. */
6434 && !LAMBDA_TYPE_P (t))
6435 add_stmt (build_min (TAG_DEFN, t));
6437 return t;
6440 /* Return the dynamic type of INSTANCE, if known.
6441 Used to determine whether the virtual function table is needed
6442 or not.
6444 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6445 of our knowledge of its type. *NONNULL should be initialized
6446 before this function is called. */
6448 static tree
6449 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6451 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6453 switch (TREE_CODE (instance))
6455 case INDIRECT_REF:
6456 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6457 return NULL_TREE;
6458 else
6459 return RECUR (TREE_OPERAND (instance, 0));
6461 case CALL_EXPR:
6462 /* This is a call to a constructor, hence it's never zero. */
6463 if (TREE_HAS_CONSTRUCTOR (instance))
6465 if (nonnull)
6466 *nonnull = 1;
6467 return TREE_TYPE (instance);
6469 return NULL_TREE;
6471 case SAVE_EXPR:
6472 /* This is a call to a constructor, hence it's never zero. */
6473 if (TREE_HAS_CONSTRUCTOR (instance))
6475 if (nonnull)
6476 *nonnull = 1;
6477 return TREE_TYPE (instance);
6479 return RECUR (TREE_OPERAND (instance, 0));
6481 case POINTER_PLUS_EXPR:
6482 case PLUS_EXPR:
6483 case MINUS_EXPR:
6484 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
6485 return RECUR (TREE_OPERAND (instance, 0));
6486 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
6487 /* Propagate nonnull. */
6488 return RECUR (TREE_OPERAND (instance, 0));
6490 return NULL_TREE;
6492 CASE_CONVERT:
6493 return RECUR (TREE_OPERAND (instance, 0));
6495 case ADDR_EXPR:
6496 instance = TREE_OPERAND (instance, 0);
6497 if (nonnull)
6499 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
6500 with a real object -- given &p->f, p can still be null. */
6501 tree t = get_base_address (instance);
6502 /* ??? Probably should check DECL_WEAK here. */
6503 if (t && DECL_P (t))
6504 *nonnull = 1;
6506 return RECUR (instance);
6508 case COMPONENT_REF:
6509 /* If this component is really a base class reference, then the field
6510 itself isn't definitive. */
6511 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
6512 return RECUR (TREE_OPERAND (instance, 0));
6513 return RECUR (TREE_OPERAND (instance, 1));
6515 case VAR_DECL:
6516 case FIELD_DECL:
6517 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
6518 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
6520 if (nonnull)
6521 *nonnull = 1;
6522 return TREE_TYPE (TREE_TYPE (instance));
6524 /* fall through... */
6525 case TARGET_EXPR:
6526 case PARM_DECL:
6527 case RESULT_DECL:
6528 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
6530 if (nonnull)
6531 *nonnull = 1;
6532 return TREE_TYPE (instance);
6534 else if (instance == current_class_ptr)
6536 if (nonnull)
6537 *nonnull = 1;
6539 /* if we're in a ctor or dtor, we know our type. If
6540 current_class_ptr is set but we aren't in a function, we're in
6541 an NSDMI (and therefore a constructor). */
6542 if (current_scope () != current_function_decl
6543 || (DECL_LANG_SPECIFIC (current_function_decl)
6544 && (DECL_CONSTRUCTOR_P (current_function_decl)
6545 || DECL_DESTRUCTOR_P (current_function_decl))))
6547 if (cdtorp)
6548 *cdtorp = 1;
6549 return TREE_TYPE (TREE_TYPE (instance));
6552 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
6554 /* We only need one hash table because it is always left empty. */
6555 static hash_table <pointer_hash <tree_node> > ht;
6556 if (!ht.is_created ())
6557 ht.create (37);
6559 /* Reference variables should be references to objects. */
6560 if (nonnull)
6561 *nonnull = 1;
6563 /* Enter the INSTANCE in a table to prevent recursion; a
6564 variable's initializer may refer to the variable
6565 itself. */
6566 if (TREE_CODE (instance) == VAR_DECL
6567 && DECL_INITIAL (instance)
6568 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
6569 && !ht.find (instance))
6571 tree type;
6572 tree_node **slot;
6574 slot = ht.find_slot (instance, INSERT);
6575 *slot = instance;
6576 type = RECUR (DECL_INITIAL (instance));
6577 ht.remove_elt (instance);
6579 return type;
6582 return NULL_TREE;
6584 default:
6585 return NULL_TREE;
6587 #undef RECUR
6590 /* Return nonzero if the dynamic type of INSTANCE is known, and
6591 equivalent to the static type. We also handle the case where
6592 INSTANCE is really a pointer. Return negative if this is a
6593 ctor/dtor. There the dynamic type is known, but this might not be
6594 the most derived base of the original object, and hence virtual
6595 bases may not be layed out according to this type.
6597 Used to determine whether the virtual function table is needed
6598 or not.
6600 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6601 of our knowledge of its type. *NONNULL should be initialized
6602 before this function is called. */
6605 resolves_to_fixed_type_p (tree instance, int* nonnull)
6607 tree t = TREE_TYPE (instance);
6608 int cdtorp = 0;
6609 tree fixed;
6611 /* processing_template_decl can be false in a template if we're in
6612 fold_non_dependent_expr, but we still want to suppress this check. */
6613 if (in_template_function ())
6615 /* In a template we only care about the type of the result. */
6616 if (nonnull)
6617 *nonnull = true;
6618 return true;
6621 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
6622 if (fixed == NULL_TREE)
6623 return 0;
6624 if (POINTER_TYPE_P (t))
6625 t = TREE_TYPE (t);
6626 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
6627 return 0;
6628 return cdtorp ? -1 : 1;
6632 void
6633 init_class_processing (void)
6635 current_class_depth = 0;
6636 current_class_stack_size = 10;
6637 current_class_stack
6638 = XNEWVEC (struct class_stack_node, current_class_stack_size);
6639 local_classes = VEC_alloc (tree, gc, 8);
6640 sizeof_biggest_empty_class = size_zero_node;
6642 ridpointers[(int) RID_PUBLIC] = access_public_node;
6643 ridpointers[(int) RID_PRIVATE] = access_private_node;
6644 ridpointers[(int) RID_PROTECTED] = access_protected_node;
6647 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
6649 static void
6650 restore_class_cache (void)
6652 tree type;
6654 /* We are re-entering the same class we just left, so we don't
6655 have to search the whole inheritance matrix to find all the
6656 decls to bind again. Instead, we install the cached
6657 class_shadowed list and walk through it binding names. */
6658 push_binding_level (previous_class_level);
6659 class_binding_level = previous_class_level;
6660 /* Restore IDENTIFIER_TYPE_VALUE. */
6661 for (type = class_binding_level->type_shadowed;
6662 type;
6663 type = TREE_CHAIN (type))
6664 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
6667 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
6668 appropriate for TYPE.
6670 So that we may avoid calls to lookup_name, we cache the _TYPE
6671 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
6673 For multiple inheritance, we perform a two-pass depth-first search
6674 of the type lattice. */
6676 void
6677 pushclass (tree type)
6679 class_stack_node_t csn;
6681 type = TYPE_MAIN_VARIANT (type);
6683 /* Make sure there is enough room for the new entry on the stack. */
6684 if (current_class_depth + 1 >= current_class_stack_size)
6686 current_class_stack_size *= 2;
6687 current_class_stack
6688 = XRESIZEVEC (struct class_stack_node, current_class_stack,
6689 current_class_stack_size);
6692 /* Insert a new entry on the class stack. */
6693 csn = current_class_stack + current_class_depth;
6694 csn->name = current_class_name;
6695 csn->type = current_class_type;
6696 csn->access = current_access_specifier;
6697 csn->names_used = 0;
6698 csn->hidden = 0;
6699 current_class_depth++;
6701 /* Now set up the new type. */
6702 current_class_name = TYPE_NAME (type);
6703 if (TREE_CODE (current_class_name) == TYPE_DECL)
6704 current_class_name = DECL_NAME (current_class_name);
6705 current_class_type = type;
6707 /* By default, things in classes are private, while things in
6708 structures or unions are public. */
6709 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
6710 ? access_private_node
6711 : access_public_node);
6713 if (previous_class_level
6714 && type != previous_class_level->this_entity
6715 && current_class_depth == 1)
6717 /* Forcibly remove any old class remnants. */
6718 invalidate_class_lookup_cache ();
6721 if (!previous_class_level
6722 || type != previous_class_level->this_entity
6723 || current_class_depth > 1)
6724 pushlevel_class ();
6725 else
6726 restore_class_cache ();
6729 /* When we exit a toplevel class scope, we save its binding level so
6730 that we can restore it quickly. Here, we've entered some other
6731 class, so we must invalidate our cache. */
6733 void
6734 invalidate_class_lookup_cache (void)
6736 previous_class_level = NULL;
6739 /* Get out of the current class scope. If we were in a class scope
6740 previously, that is the one popped to. */
6742 void
6743 popclass (void)
6745 poplevel_class ();
6747 current_class_depth--;
6748 current_class_name = current_class_stack[current_class_depth].name;
6749 current_class_type = current_class_stack[current_class_depth].type;
6750 current_access_specifier = current_class_stack[current_class_depth].access;
6751 if (current_class_stack[current_class_depth].names_used)
6752 splay_tree_delete (current_class_stack[current_class_depth].names_used);
6755 /* Mark the top of the class stack as hidden. */
6757 void
6758 push_class_stack (void)
6760 if (current_class_depth)
6761 ++current_class_stack[current_class_depth - 1].hidden;
6764 /* Mark the top of the class stack as un-hidden. */
6766 void
6767 pop_class_stack (void)
6769 if (current_class_depth)
6770 --current_class_stack[current_class_depth - 1].hidden;
6773 /* Returns 1 if the class type currently being defined is either T or
6774 a nested type of T. */
6776 bool
6777 currently_open_class (tree t)
6779 int i;
6781 if (!CLASS_TYPE_P (t))
6782 return false;
6784 t = TYPE_MAIN_VARIANT (t);
6786 /* We start looking from 1 because entry 0 is from global scope,
6787 and has no type. */
6788 for (i = current_class_depth; i > 0; --i)
6790 tree c;
6791 if (i == current_class_depth)
6792 c = current_class_type;
6793 else
6795 if (current_class_stack[i].hidden)
6796 break;
6797 c = current_class_stack[i].type;
6799 if (!c)
6800 continue;
6801 if (same_type_p (c, t))
6802 return true;
6804 return false;
6807 /* If either current_class_type or one of its enclosing classes are derived
6808 from T, return the appropriate type. Used to determine how we found
6809 something via unqualified lookup. */
6811 tree
6812 currently_open_derived_class (tree t)
6814 int i;
6816 /* The bases of a dependent type are unknown. */
6817 if (dependent_type_p (t))
6818 return NULL_TREE;
6820 if (!current_class_type)
6821 return NULL_TREE;
6823 if (DERIVED_FROM_P (t, current_class_type))
6824 return current_class_type;
6826 for (i = current_class_depth - 1; i > 0; --i)
6828 if (current_class_stack[i].hidden)
6829 break;
6830 if (DERIVED_FROM_P (t, current_class_stack[i].type))
6831 return current_class_stack[i].type;
6834 return NULL_TREE;
6837 /* Returns the innermost class type which is not a lambda closure type. */
6839 tree
6840 current_nonlambda_class_type (void)
6842 int i;
6844 /* We start looking from 1 because entry 0 is from global scope,
6845 and has no type. */
6846 for (i = current_class_depth; i > 0; --i)
6848 tree c;
6849 if (i == current_class_depth)
6850 c = current_class_type;
6851 else
6853 if (current_class_stack[i].hidden)
6854 break;
6855 c = current_class_stack[i].type;
6857 if (!c)
6858 continue;
6859 if (!LAMBDA_TYPE_P (c))
6860 return c;
6862 return NULL_TREE;
6865 /* When entering a class scope, all enclosing class scopes' names with
6866 static meaning (static variables, static functions, types and
6867 enumerators) have to be visible. This recursive function calls
6868 pushclass for all enclosing class contexts until global or a local
6869 scope is reached. TYPE is the enclosed class. */
6871 void
6872 push_nested_class (tree type)
6874 /* A namespace might be passed in error cases, like A::B:C. */
6875 if (type == NULL_TREE
6876 || !CLASS_TYPE_P (type))
6877 return;
6879 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
6881 pushclass (type);
6884 /* Undoes a push_nested_class call. */
6886 void
6887 pop_nested_class (void)
6889 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
6891 popclass ();
6892 if (context && CLASS_TYPE_P (context))
6893 pop_nested_class ();
6896 /* Returns the number of extern "LANG" blocks we are nested within. */
6899 current_lang_depth (void)
6901 return VEC_length (tree, current_lang_base);
6904 /* Set global variables CURRENT_LANG_NAME to appropriate value
6905 so that behavior of name-mangling machinery is correct. */
6907 void
6908 push_lang_context (tree name)
6910 VEC_safe_push (tree, gc, current_lang_base, current_lang_name);
6912 if (name == lang_name_cplusplus)
6914 current_lang_name = name;
6916 else if (name == lang_name_java)
6918 current_lang_name = name;
6919 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
6920 (See record_builtin_java_type in decl.c.) However, that causes
6921 incorrect debug entries if these types are actually used.
6922 So we re-enable debug output after extern "Java". */
6923 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
6924 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
6925 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
6926 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
6927 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
6928 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
6929 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
6930 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
6932 else if (name == lang_name_c)
6934 current_lang_name = name;
6936 else
6937 error ("language string %<\"%E\"%> not recognized", name);
6940 /* Get out of the current language scope. */
6942 void
6943 pop_lang_context (void)
6945 current_lang_name = VEC_pop (tree, current_lang_base);
6948 /* Type instantiation routines. */
6950 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
6951 matches the TARGET_TYPE. If there is no satisfactory match, return
6952 error_mark_node, and issue an error & warning messages under
6953 control of FLAGS. Permit pointers to member function if FLAGS
6954 permits. If TEMPLATE_ONLY, the name of the overloaded function was
6955 a template-id, and EXPLICIT_TARGS are the explicitly provided
6956 template arguments.
6958 If OVERLOAD is for one or more member functions, then ACCESS_PATH
6959 is the base path used to reference those member functions. If
6960 the address is resolved to a member function, access checks will be
6961 performed and errors issued if appropriate. */
6963 static tree
6964 resolve_address_of_overloaded_function (tree target_type,
6965 tree overload,
6966 tsubst_flags_t flags,
6967 bool template_only,
6968 tree explicit_targs,
6969 tree access_path)
6971 /* Here's what the standard says:
6973 [over.over]
6975 If the name is a function template, template argument deduction
6976 is done, and if the argument deduction succeeds, the deduced
6977 arguments are used to generate a single template function, which
6978 is added to the set of overloaded functions considered.
6980 Non-member functions and static member functions match targets of
6981 type "pointer-to-function" or "reference-to-function." Nonstatic
6982 member functions match targets of type "pointer-to-member
6983 function;" the function type of the pointer to member is used to
6984 select the member function from the set of overloaded member
6985 functions. If a nonstatic member function is selected, the
6986 reference to the overloaded function name is required to have the
6987 form of a pointer to member as described in 5.3.1.
6989 If more than one function is selected, any template functions in
6990 the set are eliminated if the set also contains a non-template
6991 function, and any given template function is eliminated if the
6992 set contains a second template function that is more specialized
6993 than the first according to the partial ordering rules 14.5.5.2.
6994 After such eliminations, if any, there shall remain exactly one
6995 selected function. */
6997 int is_ptrmem = 0;
6998 /* We store the matches in a TREE_LIST rooted here. The functions
6999 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7000 interoperability with most_specialized_instantiation. */
7001 tree matches = NULL_TREE;
7002 tree fn;
7003 tree target_fn_type;
7005 /* By the time we get here, we should be seeing only real
7006 pointer-to-member types, not the internal POINTER_TYPE to
7007 METHOD_TYPE representation. */
7008 gcc_assert (TREE_CODE (target_type) != POINTER_TYPE
7009 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7011 gcc_assert (is_overloaded_fn (overload));
7013 /* Check that the TARGET_TYPE is reasonable. */
7014 if (TYPE_PTRFN_P (target_type))
7015 /* This is OK. */;
7016 else if (TYPE_PTRMEMFUNC_P (target_type))
7017 /* This is OK, too. */
7018 is_ptrmem = 1;
7019 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7020 /* This is OK, too. This comes from a conversion to reference
7021 type. */
7022 target_type = build_reference_type (target_type);
7023 else
7025 if (flags & tf_error)
7026 error ("cannot resolve overloaded function %qD based on"
7027 " conversion to type %qT",
7028 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7029 return error_mark_node;
7032 /* Non-member functions and static member functions match targets of type
7033 "pointer-to-function" or "reference-to-function." Nonstatic member
7034 functions match targets of type "pointer-to-member-function;" the
7035 function type of the pointer to member is used to select the member
7036 function from the set of overloaded member functions.
7038 So figure out the FUNCTION_TYPE that we want to match against. */
7039 target_fn_type = static_fn_type (target_type);
7041 /* If we can find a non-template function that matches, we can just
7042 use it. There's no point in generating template instantiations
7043 if we're just going to throw them out anyhow. But, of course, we
7044 can only do this when we don't *need* a template function. */
7045 if (!template_only)
7047 tree fns;
7049 for (fns = overload; fns; fns = OVL_NEXT (fns))
7051 tree fn = OVL_CURRENT (fns);
7053 if (TREE_CODE (fn) == TEMPLATE_DECL)
7054 /* We're not looking for templates just yet. */
7055 continue;
7057 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7058 != is_ptrmem)
7059 /* We're looking for a non-static member, and this isn't
7060 one, or vice versa. */
7061 continue;
7063 /* Ignore functions which haven't been explicitly
7064 declared. */
7065 if (DECL_ANTICIPATED (fn))
7066 continue;
7068 /* See if there's a match. */
7069 if (same_type_p (target_fn_type, static_fn_type (fn)))
7070 matches = tree_cons (fn, NULL_TREE, matches);
7074 /* Now, if we've already got a match (or matches), there's no need
7075 to proceed to the template functions. But, if we don't have a
7076 match we need to look at them, too. */
7077 if (!matches)
7079 tree target_arg_types;
7080 tree target_ret_type;
7081 tree fns;
7082 tree *args;
7083 unsigned int nargs, ia;
7084 tree arg;
7086 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7087 target_ret_type = TREE_TYPE (target_fn_type);
7089 nargs = list_length (target_arg_types);
7090 args = XALLOCAVEC (tree, nargs);
7091 for (arg = target_arg_types, ia = 0;
7092 arg != NULL_TREE && arg != void_list_node;
7093 arg = TREE_CHAIN (arg), ++ia)
7094 args[ia] = TREE_VALUE (arg);
7095 nargs = ia;
7097 for (fns = overload; fns; fns = OVL_NEXT (fns))
7099 tree fn = OVL_CURRENT (fns);
7100 tree instantiation;
7101 tree targs;
7103 if (TREE_CODE (fn) != TEMPLATE_DECL)
7104 /* We're only looking for templates. */
7105 continue;
7107 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7108 != is_ptrmem)
7109 /* We're not looking for a non-static member, and this is
7110 one, or vice versa. */
7111 continue;
7113 /* Try to do argument deduction. */
7114 targs = make_tree_vec (DECL_NTPARMS (fn));
7115 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7116 nargs, target_ret_type,
7117 DEDUCE_EXACT, LOOKUP_NORMAL,
7118 false);
7119 if (instantiation == error_mark_node)
7120 /* Instantiation failed. */
7121 continue;
7123 /* See if there's a match. */
7124 if (same_type_p (target_fn_type, static_fn_type (instantiation)))
7125 matches = tree_cons (instantiation, fn, matches);
7128 /* Now, remove all but the most specialized of the matches. */
7129 if (matches)
7131 tree match = most_specialized_instantiation (matches);
7133 if (match != error_mark_node)
7134 matches = tree_cons (TREE_PURPOSE (match),
7135 NULL_TREE,
7136 NULL_TREE);
7140 /* Now we should have exactly one function in MATCHES. */
7141 if (matches == NULL_TREE)
7143 /* There were *no* matches. */
7144 if (flags & tf_error)
7146 error ("no matches converting function %qD to type %q#T",
7147 DECL_NAME (OVL_CURRENT (overload)),
7148 target_type);
7150 print_candidates (overload);
7152 return error_mark_node;
7154 else if (TREE_CHAIN (matches))
7156 /* There were too many matches. First check if they're all
7157 the same function. */
7158 tree match;
7160 fn = TREE_PURPOSE (matches);
7161 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7162 if (!decls_match (fn, TREE_PURPOSE (match)))
7163 break;
7165 if (match)
7167 if (flags & tf_error)
7169 error ("converting overloaded function %qD to type %q#T is ambiguous",
7170 DECL_NAME (OVL_FUNCTION (overload)),
7171 target_type);
7173 /* Since print_candidates expects the functions in the
7174 TREE_VALUE slot, we flip them here. */
7175 for (match = matches; match; match = TREE_CHAIN (match))
7176 TREE_VALUE (match) = TREE_PURPOSE (match);
7178 print_candidates (matches);
7181 return error_mark_node;
7185 /* Good, exactly one match. Now, convert it to the correct type. */
7186 fn = TREE_PURPOSE (matches);
7188 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7189 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
7191 static int explained;
7193 if (!(flags & tf_error))
7194 return error_mark_node;
7196 permerror (input_location, "assuming pointer to member %qD", fn);
7197 if (!explained)
7199 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7200 explained = 1;
7204 /* If we're doing overload resolution purely for the purpose of
7205 determining conversion sequences, we should not consider the
7206 function used. If this conversion sequence is selected, the
7207 function will be marked as used at this point. */
7208 if (!(flags & tf_conv))
7210 /* Make =delete work with SFINAE. */
7211 if (DECL_DELETED_FN (fn) && !(flags & tf_error))
7212 return error_mark_node;
7214 mark_used (fn);
7217 /* We could not check access to member functions when this
7218 expression was originally created since we did not know at that
7219 time to which function the expression referred. */
7220 if (DECL_FUNCTION_MEMBER_P (fn))
7222 gcc_assert (access_path);
7223 perform_or_defer_access_check (access_path, fn, fn, flags);
7226 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7227 return cp_build_addr_expr (fn, flags);
7228 else
7230 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7231 will mark the function as addressed, but here we must do it
7232 explicitly. */
7233 cxx_mark_addressable (fn);
7235 return fn;
7239 /* This function will instantiate the type of the expression given in
7240 RHS to match the type of LHSTYPE. If errors exist, then return
7241 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then
7242 we complain on errors. If we are not complaining, never modify rhs,
7243 as overload resolution wants to try many possible instantiations, in
7244 the hope that at least one will work.
7246 For non-recursive calls, LHSTYPE should be a function, pointer to
7247 function, or a pointer to member function. */
7249 tree
7250 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
7252 tsubst_flags_t flags_in = flags;
7253 tree access_path = NULL_TREE;
7255 flags &= ~tf_ptrmem_ok;
7257 if (lhstype == unknown_type_node)
7259 if (flags & tf_error)
7260 error ("not enough type information");
7261 return error_mark_node;
7264 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7266 if (same_type_p (lhstype, TREE_TYPE (rhs)))
7267 return rhs;
7268 if (flag_ms_extensions
7269 && TYPE_PTRMEMFUNC_P (lhstype)
7270 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7271 /* Microsoft allows `A::f' to be resolved to a
7272 pointer-to-member. */
7274 else
7276 if (flags & tf_error)
7277 error ("cannot convert %qE from type %qT to type %qT",
7278 rhs, TREE_TYPE (rhs), lhstype);
7279 return error_mark_node;
7283 if (BASELINK_P (rhs))
7285 access_path = BASELINK_ACCESS_BINFO (rhs);
7286 rhs = BASELINK_FUNCTIONS (rhs);
7289 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7290 deduce any type information. */
7291 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7293 if (flags & tf_error)
7294 error ("not enough type information");
7295 return error_mark_node;
7298 /* There only a few kinds of expressions that may have a type
7299 dependent on overload resolution. */
7300 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7301 || TREE_CODE (rhs) == COMPONENT_REF
7302 || really_overloaded_fn (rhs)
7303 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7305 /* This should really only be used when attempting to distinguish
7306 what sort of a pointer to function we have. For now, any
7307 arithmetic operation which is not supported on pointers
7308 is rejected as an error. */
7310 switch (TREE_CODE (rhs))
7312 case COMPONENT_REF:
7314 tree member = TREE_OPERAND (rhs, 1);
7316 member = instantiate_type (lhstype, member, flags);
7317 if (member != error_mark_node
7318 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7319 /* Do not lose object's side effects. */
7320 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7321 TREE_OPERAND (rhs, 0), member);
7322 return member;
7325 case OFFSET_REF:
7326 rhs = TREE_OPERAND (rhs, 1);
7327 if (BASELINK_P (rhs))
7328 return instantiate_type (lhstype, rhs, flags_in);
7330 /* This can happen if we are forming a pointer-to-member for a
7331 member template. */
7332 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7334 /* Fall through. */
7336 case TEMPLATE_ID_EXPR:
7338 tree fns = TREE_OPERAND (rhs, 0);
7339 tree args = TREE_OPERAND (rhs, 1);
7341 return
7342 resolve_address_of_overloaded_function (lhstype, fns, flags_in,
7343 /*template_only=*/true,
7344 args, access_path);
7347 case OVERLOAD:
7348 case FUNCTION_DECL:
7349 return
7350 resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
7351 /*template_only=*/false,
7352 /*explicit_targs=*/NULL_TREE,
7353 access_path);
7355 case ADDR_EXPR:
7357 if (PTRMEM_OK_P (rhs))
7358 flags |= tf_ptrmem_ok;
7360 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
7363 case ERROR_MARK:
7364 return error_mark_node;
7366 default:
7367 gcc_unreachable ();
7369 return error_mark_node;
7372 /* Return the name of the virtual function pointer field
7373 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7374 this may have to look back through base types to find the
7375 ultimate field name. (For single inheritance, these could
7376 all be the same name. Who knows for multiple inheritance). */
7378 static tree
7379 get_vfield_name (tree type)
7381 tree binfo, base_binfo;
7382 char *buf;
7384 for (binfo = TYPE_BINFO (type);
7385 BINFO_N_BASE_BINFOS (binfo);
7386 binfo = base_binfo)
7388 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7390 if (BINFO_VIRTUAL_P (base_binfo)
7391 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7392 break;
7395 type = BINFO_TYPE (binfo);
7396 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7397 + TYPE_NAME_LENGTH (type) + 2);
7398 sprintf (buf, VFIELD_NAME_FORMAT,
7399 IDENTIFIER_POINTER (constructor_name (type)));
7400 return get_identifier (buf);
7403 void
7404 print_class_statistics (void)
7406 if (! GATHER_STATISTICS)
7407 return;
7409 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
7410 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
7411 if (n_vtables)
7413 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
7414 n_vtables, n_vtable_searches);
7415 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
7416 n_vtable_entries, n_vtable_elems);
7420 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
7421 according to [class]:
7422 The class-name is also inserted
7423 into the scope of the class itself. For purposes of access checking,
7424 the inserted class name is treated as if it were a public member name. */
7426 void
7427 build_self_reference (void)
7429 tree name = constructor_name (current_class_type);
7430 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
7431 tree saved_cas;
7433 DECL_NONLOCAL (value) = 1;
7434 DECL_CONTEXT (value) = current_class_type;
7435 DECL_ARTIFICIAL (value) = 1;
7436 SET_DECL_SELF_REFERENCE_P (value);
7437 set_underlying_type (value);
7439 if (processing_template_decl)
7440 value = push_template_decl (value);
7442 saved_cas = current_access_specifier;
7443 current_access_specifier = access_public_node;
7444 finish_member_declaration (value);
7445 current_access_specifier = saved_cas;
7448 /* Returns 1 if TYPE contains only padding bytes. */
7451 is_empty_class (tree type)
7453 if (type == error_mark_node)
7454 return 0;
7456 if (! CLASS_TYPE_P (type))
7457 return 0;
7459 /* In G++ 3.2, whether or not a class was empty was determined by
7460 looking at its size. */
7461 if (abi_version_at_least (2))
7462 return CLASSTYPE_EMPTY_P (type);
7463 else
7464 return integer_zerop (CLASSTYPE_SIZE (type));
7467 /* Returns true if TYPE contains an empty class. */
7469 static bool
7470 contains_empty_class_p (tree type)
7472 if (is_empty_class (type))
7473 return true;
7474 if (CLASS_TYPE_P (type))
7476 tree field;
7477 tree binfo;
7478 tree base_binfo;
7479 int i;
7481 for (binfo = TYPE_BINFO (type), i = 0;
7482 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7483 if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
7484 return true;
7485 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
7486 if (TREE_CODE (field) == FIELD_DECL
7487 && !DECL_ARTIFICIAL (field)
7488 && is_empty_class (TREE_TYPE (field)))
7489 return true;
7491 else if (TREE_CODE (type) == ARRAY_TYPE)
7492 return contains_empty_class_p (TREE_TYPE (type));
7493 return false;
7496 /* Returns true if TYPE contains no actual data, just various
7497 possible combinations of empty classes and possibly a vptr. */
7499 bool
7500 is_really_empty_class (tree type)
7502 if (CLASS_TYPE_P (type))
7504 tree field;
7505 tree binfo;
7506 tree base_binfo;
7507 int i;
7509 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
7510 out, but we'd like to be able to check this before then. */
7511 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
7512 return true;
7514 for (binfo = TYPE_BINFO (type), i = 0;
7515 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
7516 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
7517 return false;
7518 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7519 if (TREE_CODE (field) == FIELD_DECL
7520 && !DECL_ARTIFICIAL (field)
7521 && !is_really_empty_class (TREE_TYPE (field)))
7522 return false;
7523 return true;
7525 else if (TREE_CODE (type) == ARRAY_TYPE)
7526 return is_really_empty_class (TREE_TYPE (type));
7527 return false;
7530 /* Note that NAME was looked up while the current class was being
7531 defined and that the result of that lookup was DECL. */
7533 void
7534 maybe_note_name_used_in_class (tree name, tree decl)
7536 splay_tree names_used;
7538 /* If we're not defining a class, there's nothing to do. */
7539 if (!(innermost_scope_kind() == sk_class
7540 && TYPE_BEING_DEFINED (current_class_type)
7541 && !LAMBDA_TYPE_P (current_class_type)))
7542 return;
7544 /* If there's already a binding for this NAME, then we don't have
7545 anything to worry about. */
7546 if (lookup_member (current_class_type, name,
7547 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
7548 return;
7550 if (!current_class_stack[current_class_depth - 1].names_used)
7551 current_class_stack[current_class_depth - 1].names_used
7552 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
7553 names_used = current_class_stack[current_class_depth - 1].names_used;
7555 splay_tree_insert (names_used,
7556 (splay_tree_key) name,
7557 (splay_tree_value) decl);
7560 /* Note that NAME was declared (as DECL) in the current class. Check
7561 to see that the declaration is valid. */
7563 void
7564 note_name_declared_in_class (tree name, tree decl)
7566 splay_tree names_used;
7567 splay_tree_node n;
7569 /* Look to see if we ever used this name. */
7570 names_used
7571 = current_class_stack[current_class_depth - 1].names_used;
7572 if (!names_used)
7573 return;
7574 /* The C language allows members to be declared with a type of the same
7575 name, and the C++ standard says this diagnostic is not required. So
7576 allow it in extern "C" blocks unless predantic is specified.
7577 Allow it in all cases if -ms-extensions is specified. */
7578 if ((!pedantic && current_lang_name == lang_name_c)
7579 || flag_ms_extensions)
7580 return;
7581 n = splay_tree_lookup (names_used, (splay_tree_key) name);
7582 if (n)
7584 /* [basic.scope.class]
7586 A name N used in a class S shall refer to the same declaration
7587 in its context and when re-evaluated in the completed scope of
7588 S. */
7589 permerror (input_location, "declaration of %q#D", decl);
7590 permerror (input_location, "changes meaning of %qD from %q+#D",
7591 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
7595 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
7596 Secondary vtables are merged with primary vtables; this function
7597 will return the VAR_DECL for the primary vtable. */
7599 tree
7600 get_vtbl_decl_for_binfo (tree binfo)
7602 tree decl;
7604 decl = BINFO_VTABLE (binfo);
7605 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
7607 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
7608 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
7610 if (decl)
7611 gcc_assert (TREE_CODE (decl) == VAR_DECL);
7612 return decl;
7616 /* Returns the binfo for the primary base of BINFO. If the resulting
7617 BINFO is a virtual base, and it is inherited elsewhere in the
7618 hierarchy, then the returned binfo might not be the primary base of
7619 BINFO in the complete object. Check BINFO_PRIMARY_P or
7620 BINFO_LOST_PRIMARY_P to be sure. */
7622 static tree
7623 get_primary_binfo (tree binfo)
7625 tree primary_base;
7627 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
7628 if (!primary_base)
7629 return NULL_TREE;
7631 return copied_binfo (primary_base, binfo);
7634 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
7636 static int
7637 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
7639 if (!indented_p)
7640 fprintf (stream, "%*s", indent, "");
7641 return 1;
7644 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
7645 INDENT should be zero when called from the top level; it is
7646 incremented recursively. IGO indicates the next expected BINFO in
7647 inheritance graph ordering. */
7649 static tree
7650 dump_class_hierarchy_r (FILE *stream,
7651 int flags,
7652 tree binfo,
7653 tree igo,
7654 int indent)
7656 int indented = 0;
7657 tree base_binfo;
7658 int i;
7660 indented = maybe_indent_hierarchy (stream, indent, 0);
7661 fprintf (stream, "%s (0x%lx) ",
7662 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
7663 (unsigned long) binfo);
7664 if (binfo != igo)
7666 fprintf (stream, "alternative-path\n");
7667 return igo;
7669 igo = TREE_CHAIN (binfo);
7671 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
7672 tree_low_cst (BINFO_OFFSET (binfo), 0));
7673 if (is_empty_class (BINFO_TYPE (binfo)))
7674 fprintf (stream, " empty");
7675 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
7676 fprintf (stream, " nearly-empty");
7677 if (BINFO_VIRTUAL_P (binfo))
7678 fprintf (stream, " virtual");
7679 fprintf (stream, "\n");
7681 indented = 0;
7682 if (BINFO_PRIMARY_P (binfo))
7684 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7685 fprintf (stream, " primary-for %s (0x%lx)",
7686 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
7687 TFF_PLAIN_IDENTIFIER),
7688 (unsigned long)BINFO_INHERITANCE_CHAIN (binfo));
7690 if (BINFO_LOST_PRIMARY_P (binfo))
7692 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7693 fprintf (stream, " lost-primary");
7695 if (indented)
7696 fprintf (stream, "\n");
7698 if (!(flags & TDF_SLIM))
7700 int indented = 0;
7702 if (BINFO_SUBVTT_INDEX (binfo))
7704 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7705 fprintf (stream, " subvttidx=%s",
7706 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
7707 TFF_PLAIN_IDENTIFIER));
7709 if (BINFO_VPTR_INDEX (binfo))
7711 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7712 fprintf (stream, " vptridx=%s",
7713 expr_as_string (BINFO_VPTR_INDEX (binfo),
7714 TFF_PLAIN_IDENTIFIER));
7716 if (BINFO_VPTR_FIELD (binfo))
7718 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7719 fprintf (stream, " vbaseoffset=%s",
7720 expr_as_string (BINFO_VPTR_FIELD (binfo),
7721 TFF_PLAIN_IDENTIFIER));
7723 if (BINFO_VTABLE (binfo))
7725 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
7726 fprintf (stream, " vptr=%s",
7727 expr_as_string (BINFO_VTABLE (binfo),
7728 TFF_PLAIN_IDENTIFIER));
7731 if (indented)
7732 fprintf (stream, "\n");
7735 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7736 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
7738 return igo;
7741 /* Dump the BINFO hierarchy for T. */
7743 static void
7744 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
7746 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7747 fprintf (stream, " size=%lu align=%lu\n",
7748 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
7749 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
7750 fprintf (stream, " base size=%lu base align=%lu\n",
7751 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
7752 / BITS_PER_UNIT),
7753 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
7754 / BITS_PER_UNIT));
7755 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
7756 fprintf (stream, "\n");
7759 /* Debug interface to hierarchy dumping. */
7761 void
7762 debug_class (tree t)
7764 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
7767 static void
7768 dump_class_hierarchy (tree t)
7770 int flags;
7771 FILE *stream = dump_begin (TDI_class, &flags);
7773 if (stream)
7775 dump_class_hierarchy_1 (stream, flags, t);
7776 dump_end (TDI_class, stream);
7780 static void
7781 dump_array (FILE * stream, tree decl)
7783 tree value;
7784 unsigned HOST_WIDE_INT ix;
7785 HOST_WIDE_INT elt;
7786 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
7788 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
7789 / BITS_PER_UNIT);
7790 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
7791 fprintf (stream, " %s entries",
7792 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
7793 TFF_PLAIN_IDENTIFIER));
7794 fprintf (stream, "\n");
7796 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
7797 ix, value)
7798 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
7799 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
7802 static void
7803 dump_vtable (tree t, tree binfo, tree vtable)
7805 int flags;
7806 FILE *stream = dump_begin (TDI_class, &flags);
7808 if (!stream)
7809 return;
7811 if (!(flags & TDF_SLIM))
7813 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
7815 fprintf (stream, "%s for %s",
7816 ctor_vtbl_p ? "Construction vtable" : "Vtable",
7817 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
7818 if (ctor_vtbl_p)
7820 if (!BINFO_VIRTUAL_P (binfo))
7821 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
7822 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7824 fprintf (stream, "\n");
7825 dump_array (stream, vtable);
7826 fprintf (stream, "\n");
7829 dump_end (TDI_class, stream);
7832 static void
7833 dump_vtt (tree t, tree vtt)
7835 int flags;
7836 FILE *stream = dump_begin (TDI_class, &flags);
7838 if (!stream)
7839 return;
7841 if (!(flags & TDF_SLIM))
7843 fprintf (stream, "VTT for %s\n",
7844 type_as_string (t, TFF_PLAIN_IDENTIFIER));
7845 dump_array (stream, vtt);
7846 fprintf (stream, "\n");
7849 dump_end (TDI_class, stream);
7852 /* Dump a function or thunk and its thunkees. */
7854 static void
7855 dump_thunk (FILE *stream, int indent, tree thunk)
7857 static const char spaces[] = " ";
7858 tree name = DECL_NAME (thunk);
7859 tree thunks;
7861 fprintf (stream, "%.*s%p %s %s", indent, spaces,
7862 (void *)thunk,
7863 !DECL_THUNK_P (thunk) ? "function"
7864 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
7865 name ? IDENTIFIER_POINTER (name) : "<unset>");
7866 if (DECL_THUNK_P (thunk))
7868 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
7869 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
7871 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
7872 if (!virtual_adjust)
7873 /*NOP*/;
7874 else if (DECL_THIS_THUNK_P (thunk))
7875 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
7876 tree_low_cst (virtual_adjust, 0));
7877 else
7878 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
7879 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
7880 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
7881 if (THUNK_ALIAS (thunk))
7882 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
7884 fprintf (stream, "\n");
7885 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
7886 dump_thunk (stream, indent + 2, thunks);
7889 /* Dump the thunks for FN. */
7891 void
7892 debug_thunks (tree fn)
7894 dump_thunk (stderr, 0, fn);
7897 /* Virtual function table initialization. */
7899 /* Create all the necessary vtables for T and its base classes. */
7901 static void
7902 finish_vtbls (tree t)
7904 tree vbase;
7905 VEC(constructor_elt,gc) *v = NULL;
7906 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
7908 /* We lay out the primary and secondary vtables in one contiguous
7909 vtable. The primary vtable is first, followed by the non-virtual
7910 secondary vtables in inheritance graph order. */
7911 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
7912 vtable, t, &v);
7914 /* Then come the virtual bases, also in inheritance graph order. */
7915 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
7917 if (!BINFO_VIRTUAL_P (vbase))
7918 continue;
7919 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
7922 if (BINFO_VTABLE (TYPE_BINFO (t)))
7923 initialize_vtable (TYPE_BINFO (t), v);
7926 /* Initialize the vtable for BINFO with the INITS. */
7928 static void
7929 initialize_vtable (tree binfo, VEC(constructor_elt,gc) *inits)
7931 tree decl;
7933 layout_vtable_decl (binfo, VEC_length (constructor_elt, inits));
7934 decl = get_vtbl_decl_for_binfo (binfo);
7935 initialize_artificial_var (decl, inits);
7936 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
7939 /* Build the VTT (virtual table table) for T.
7940 A class requires a VTT if it has virtual bases.
7942 This holds
7943 1 - primary virtual pointer for complete object T
7944 2 - secondary VTTs for each direct non-virtual base of T which requires a
7946 3 - secondary virtual pointers for each direct or indirect base of T which
7947 has virtual bases or is reachable via a virtual path from T.
7948 4 - secondary VTTs for each direct or indirect virtual base of T.
7950 Secondary VTTs look like complete object VTTs without part 4. */
7952 static void
7953 build_vtt (tree t)
7955 tree type;
7956 tree vtt;
7957 tree index;
7958 VEC(constructor_elt,gc) *inits;
7960 /* Build up the initializers for the VTT. */
7961 inits = NULL;
7962 index = size_zero_node;
7963 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
7965 /* If we didn't need a VTT, we're done. */
7966 if (!inits)
7967 return;
7969 /* Figure out the type of the VTT. */
7970 type = build_array_of_n_type (const_ptr_type_node,
7971 VEC_length (constructor_elt, inits));
7973 /* Now, build the VTT object itself. */
7974 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
7975 initialize_artificial_var (vtt, inits);
7976 /* Add the VTT to the vtables list. */
7977 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
7978 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
7980 dump_vtt (t, vtt);
7983 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7984 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7985 and CHAIN the vtable pointer for this binfo after construction is
7986 complete. VALUE can also be another BINFO, in which case we recurse. */
7988 static tree
7989 binfo_ctor_vtable (tree binfo)
7991 tree vt;
7993 while (1)
7995 vt = BINFO_VTABLE (binfo);
7996 if (TREE_CODE (vt) == TREE_LIST)
7997 vt = TREE_VALUE (vt);
7998 if (TREE_CODE (vt) == TREE_BINFO)
7999 binfo = vt;
8000 else
8001 break;
8004 return vt;
8007 /* Data for secondary VTT initialization. */
8008 typedef struct secondary_vptr_vtt_init_data_s
8010 /* Is this the primary VTT? */
8011 bool top_level_p;
8013 /* Current index into the VTT. */
8014 tree index;
8016 /* Vector of initializers built up. */
8017 VEC(constructor_elt,gc) *inits;
8019 /* The type being constructed by this secondary VTT. */
8020 tree type_being_constructed;
8021 } secondary_vptr_vtt_init_data;
8023 /* Recursively build the VTT-initializer for BINFO (which is in the
8024 hierarchy dominated by T). INITS points to the end of the initializer
8025 list to date. INDEX is the VTT index where the next element will be
8026 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8027 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8028 for virtual bases of T. When it is not so, we build the constructor
8029 vtables for the BINFO-in-T variant. */
8031 static void
8032 build_vtt_inits (tree binfo, tree t, VEC(constructor_elt,gc) **inits, tree *index)
8034 int i;
8035 tree b;
8036 tree init;
8037 secondary_vptr_vtt_init_data data;
8038 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8040 /* We only need VTTs for subobjects with virtual bases. */
8041 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8042 return;
8044 /* We need to use a construction vtable if this is not the primary
8045 VTT. */
8046 if (!top_level_p)
8048 build_ctor_vtbl_group (binfo, t);
8050 /* Record the offset in the VTT where this sub-VTT can be found. */
8051 BINFO_SUBVTT_INDEX (binfo) = *index;
8054 /* Add the address of the primary vtable for the complete object. */
8055 init = binfo_ctor_vtable (binfo);
8056 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8057 if (top_level_p)
8059 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8060 BINFO_VPTR_INDEX (binfo) = *index;
8062 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8064 /* Recursively add the secondary VTTs for non-virtual bases. */
8065 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8066 if (!BINFO_VIRTUAL_P (b))
8067 build_vtt_inits (b, t, inits, index);
8069 /* Add secondary virtual pointers for all subobjects of BINFO with
8070 either virtual bases or reachable along a virtual path, except
8071 subobjects that are non-virtual primary bases. */
8072 data.top_level_p = top_level_p;
8073 data.index = *index;
8074 data.inits = *inits;
8075 data.type_being_constructed = BINFO_TYPE (binfo);
8077 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8079 *index = data.index;
8081 /* data.inits might have grown as we added secondary virtual pointers.
8082 Make sure our caller knows about the new vector. */
8083 *inits = data.inits;
8085 if (top_level_p)
8086 /* Add the secondary VTTs for virtual bases in inheritance graph
8087 order. */
8088 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8090 if (!BINFO_VIRTUAL_P (b))
8091 continue;
8093 build_vtt_inits (b, t, inits, index);
8095 else
8096 /* Remove the ctor vtables we created. */
8097 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8100 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8101 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8103 static tree
8104 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8106 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8108 /* We don't care about bases that don't have vtables. */
8109 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8110 return dfs_skip_bases;
8112 /* We're only interested in proper subobjects of the type being
8113 constructed. */
8114 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8115 return NULL_TREE;
8117 /* We're only interested in bases with virtual bases or reachable
8118 via a virtual path from the type being constructed. */
8119 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8120 || binfo_via_virtual (binfo, data->type_being_constructed)))
8121 return dfs_skip_bases;
8123 /* We're not interested in non-virtual primary bases. */
8124 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8125 return NULL_TREE;
8127 /* Record the index where this secondary vptr can be found. */
8128 if (data->top_level_p)
8130 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8131 BINFO_VPTR_INDEX (binfo) = data->index;
8133 if (BINFO_VIRTUAL_P (binfo))
8135 /* It's a primary virtual base, and this is not a
8136 construction vtable. Find the base this is primary of in
8137 the inheritance graph, and use that base's vtable
8138 now. */
8139 while (BINFO_PRIMARY_P (binfo))
8140 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8144 /* Add the initializer for the secondary vptr itself. */
8145 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8147 /* Advance the vtt index. */
8148 data->index = size_binop (PLUS_EXPR, data->index,
8149 TYPE_SIZE_UNIT (ptr_type_node));
8151 return NULL_TREE;
8154 /* Called from build_vtt_inits via dfs_walk. After building
8155 constructor vtables and generating the sub-vtt from them, we need
8156 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8157 binfo of the base whose sub vtt was generated. */
8159 static tree
8160 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8162 tree vtable = BINFO_VTABLE (binfo);
8164 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8165 /* If this class has no vtable, none of its bases do. */
8166 return dfs_skip_bases;
8168 if (!vtable)
8169 /* This might be a primary base, so have no vtable in this
8170 hierarchy. */
8171 return NULL_TREE;
8173 /* If we scribbled the construction vtable vptr into BINFO, clear it
8174 out now. */
8175 if (TREE_CODE (vtable) == TREE_LIST
8176 && (TREE_PURPOSE (vtable) == (tree) data))
8177 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8179 return NULL_TREE;
8182 /* Build the construction vtable group for BINFO which is in the
8183 hierarchy dominated by T. */
8185 static void
8186 build_ctor_vtbl_group (tree binfo, tree t)
8188 tree type;
8189 tree vtbl;
8190 tree id;
8191 tree vbase;
8192 VEC(constructor_elt,gc) *v;
8194 /* See if we've already created this construction vtable group. */
8195 id = mangle_ctor_vtbl_for_type (t, binfo);
8196 if (IDENTIFIER_GLOBAL_VALUE (id))
8197 return;
8199 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8200 /* Build a version of VTBL (with the wrong type) for use in
8201 constructing the addresses of secondary vtables in the
8202 construction vtable group. */
8203 vtbl = build_vtable (t, id, ptr_type_node);
8204 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8206 v = NULL;
8207 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8208 binfo, vtbl, t, &v);
8210 /* Add the vtables for each of our virtual bases using the vbase in T
8211 binfo. */
8212 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8213 vbase;
8214 vbase = TREE_CHAIN (vbase))
8216 tree b;
8218 if (!BINFO_VIRTUAL_P (vbase))
8219 continue;
8220 b = copied_binfo (vbase, binfo);
8222 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8225 /* Figure out the type of the construction vtable. */
8226 type = build_array_of_n_type (vtable_entry_type,
8227 VEC_length (constructor_elt, v));
8228 layout_type (type);
8229 TREE_TYPE (vtbl) = type;
8230 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8231 layout_decl (vtbl, 0);
8233 /* Initialize the construction vtable. */
8234 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8235 initialize_artificial_var (vtbl, v);
8236 dump_vtable (t, binfo, vtbl);
8239 /* Add the vtbl initializers for BINFO (and its bases other than
8240 non-virtual primaries) to the list of INITS. BINFO is in the
8241 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8242 the constructor the vtbl inits should be accumulated for. (If this
8243 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8244 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8245 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8246 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8247 but are not necessarily the same in terms of layout. */
8249 static void
8250 accumulate_vtbl_inits (tree binfo,
8251 tree orig_binfo,
8252 tree rtti_binfo,
8253 tree vtbl,
8254 tree t,
8255 VEC(constructor_elt,gc) **inits)
8257 int i;
8258 tree base_binfo;
8259 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8261 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8263 /* If it doesn't have a vptr, we don't do anything. */
8264 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8265 return;
8267 /* If we're building a construction vtable, we're not interested in
8268 subobjects that don't require construction vtables. */
8269 if (ctor_vtbl_p
8270 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8271 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8272 return;
8274 /* Build the initializers for the BINFO-in-T vtable. */
8275 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8277 /* Walk the BINFO and its bases. We walk in preorder so that as we
8278 initialize each vtable we can figure out at what offset the
8279 secondary vtable lies from the primary vtable. We can't use
8280 dfs_walk here because we need to iterate through bases of BINFO
8281 and RTTI_BINFO simultaneously. */
8282 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8284 /* Skip virtual bases. */
8285 if (BINFO_VIRTUAL_P (base_binfo))
8286 continue;
8287 accumulate_vtbl_inits (base_binfo,
8288 BINFO_BASE_BINFO (orig_binfo, i),
8289 rtti_binfo, vtbl, t,
8290 inits);
8294 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8295 BINFO vtable to L. */
8297 static void
8298 dfs_accumulate_vtbl_inits (tree binfo,
8299 tree orig_binfo,
8300 tree rtti_binfo,
8301 tree orig_vtbl,
8302 tree t,
8303 VEC(constructor_elt,gc) **l)
8305 tree vtbl = NULL_TREE;
8306 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8307 int n_inits;
8309 if (ctor_vtbl_p
8310 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8312 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8313 primary virtual base. If it is not the same primary in
8314 the hierarchy of T, we'll need to generate a ctor vtable
8315 for it, to place at its location in T. If it is the same
8316 primary, we still need a VTT entry for the vtable, but it
8317 should point to the ctor vtable for the base it is a
8318 primary for within the sub-hierarchy of RTTI_BINFO.
8320 There are three possible cases:
8322 1) We are in the same place.
8323 2) We are a primary base within a lost primary virtual base of
8324 RTTI_BINFO.
8325 3) We are primary to something not a base of RTTI_BINFO. */
8327 tree b;
8328 tree last = NULL_TREE;
8330 /* First, look through the bases we are primary to for RTTI_BINFO
8331 or a virtual base. */
8332 b = binfo;
8333 while (BINFO_PRIMARY_P (b))
8335 b = BINFO_INHERITANCE_CHAIN (b);
8336 last = b;
8337 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8338 goto found;
8340 /* If we run out of primary links, keep looking down our
8341 inheritance chain; we might be an indirect primary. */
8342 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8343 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8344 break;
8345 found:
8347 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8348 base B and it is a base of RTTI_BINFO, this is case 2. In
8349 either case, we share our vtable with LAST, i.e. the
8350 derived-most base within B of which we are a primary. */
8351 if (b == rtti_binfo
8352 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8353 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8354 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8355 binfo_ctor_vtable after everything's been set up. */
8356 vtbl = last;
8358 /* Otherwise, this is case 3 and we get our own. */
8360 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8361 return;
8363 n_inits = VEC_length (constructor_elt, *l);
8365 if (!vtbl)
8367 tree index;
8368 int non_fn_entries;
8370 /* Add the initializer for this vtable. */
8371 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8372 &non_fn_entries, l);
8374 /* Figure out the position to which the VPTR should point. */
8375 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8376 index = size_binop (MULT_EXPR,
8377 TYPE_SIZE_UNIT (vtable_entry_type),
8378 size_int (non_fn_entries + n_inits));
8379 vtbl = fold_build_pointer_plus (vtbl, index);
8382 if (ctor_vtbl_p)
8383 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8384 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8385 straighten this out. */
8386 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8387 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8388 /* Throw away any unneeded intializers. */
8389 VEC_truncate (constructor_elt, *l, n_inits);
8390 else
8391 /* For an ordinary vtable, set BINFO_VTABLE. */
8392 BINFO_VTABLE (binfo) = vtbl;
8395 static GTY(()) tree abort_fndecl_addr;
8397 /* Construct the initializer for BINFO's virtual function table. BINFO
8398 is part of the hierarchy dominated by T. If we're building a
8399 construction vtable, the ORIG_BINFO is the binfo we should use to
8400 find the actual function pointers to put in the vtable - but they
8401 can be overridden on the path to most-derived in the graph that
8402 ORIG_BINFO belongs. Otherwise,
8403 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8404 BINFO that should be indicated by the RTTI information in the
8405 vtable; it will be a base class of T, rather than T itself, if we
8406 are building a construction vtable.
8408 The value returned is a TREE_LIST suitable for wrapping in a
8409 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8410 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8411 number of non-function entries in the vtable.
8413 It might seem that this function should never be called with a
8414 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8415 base is always subsumed by a derived class vtable. However, when
8416 we are building construction vtables, we do build vtables for
8417 primary bases; we need these while the primary base is being
8418 constructed. */
8420 static void
8421 build_vtbl_initializer (tree binfo,
8422 tree orig_binfo,
8423 tree t,
8424 tree rtti_binfo,
8425 int* non_fn_entries_p,
8426 VEC(constructor_elt,gc) **inits)
8428 tree v;
8429 vtbl_init_data vid;
8430 unsigned ix, jx;
8431 tree vbinfo;
8432 VEC(tree,gc) *vbases;
8433 constructor_elt *e;
8435 /* Initialize VID. */
8436 memset (&vid, 0, sizeof (vid));
8437 vid.binfo = binfo;
8438 vid.derived = t;
8439 vid.rtti_binfo = rtti_binfo;
8440 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8441 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8442 vid.generate_vcall_entries = true;
8443 /* The first vbase or vcall offset is at index -3 in the vtable. */
8444 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
8446 /* Add entries to the vtable for RTTI. */
8447 build_rtti_vtbl_entries (binfo, &vid);
8449 /* Create an array for keeping track of the functions we've
8450 processed. When we see multiple functions with the same
8451 signature, we share the vcall offsets. */
8452 vid.fns = VEC_alloc (tree, gc, 32);
8453 /* Add the vcall and vbase offset entries. */
8454 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
8456 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
8457 build_vbase_offset_vtbl_entries. */
8458 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
8459 VEC_iterate (tree, vbases, ix, vbinfo); ix++)
8460 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
8462 /* If the target requires padding between data entries, add that now. */
8463 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
8465 int n_entries = VEC_length (constructor_elt, vid.inits);
8467 VEC_safe_grow (constructor_elt, gc, vid.inits,
8468 TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
8470 /* Move data entries into their new positions and add padding
8471 after the new positions. Iterate backwards so we don't
8472 overwrite entries that we would need to process later. */
8473 for (ix = n_entries - 1;
8474 VEC_iterate (constructor_elt, vid.inits, ix, e);
8475 ix--)
8477 int j;
8478 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
8479 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
8481 VEC_replace (constructor_elt, vid.inits, new_position, *e);
8483 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
8485 constructor_elt *f = &VEC_index (constructor_elt, vid.inits,
8486 new_position - j);
8487 f->index = NULL_TREE;
8488 f->value = build1 (NOP_EXPR, vtable_entry_type,
8489 null_pointer_node);
8494 if (non_fn_entries_p)
8495 *non_fn_entries_p = VEC_length (constructor_elt, vid.inits);
8497 /* The initializers for virtual functions were built up in reverse
8498 order. Straighten them out and add them to the running list in one
8499 step. */
8500 jx = VEC_length (constructor_elt, *inits);
8501 VEC_safe_grow (constructor_elt, gc, *inits,
8502 (jx + VEC_length (constructor_elt, vid.inits)));
8504 for (ix = VEC_length (constructor_elt, vid.inits) - 1;
8505 VEC_iterate (constructor_elt, vid.inits, ix, e);
8506 ix--, jx++)
8507 VEC_replace (constructor_elt, *inits, jx, *e);
8509 /* Go through all the ordinary virtual functions, building up
8510 initializers. */
8511 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
8513 tree delta;
8514 tree vcall_index;
8515 tree fn, fn_original;
8516 tree init = NULL_TREE;
8518 fn = BV_FN (v);
8519 fn_original = fn;
8520 if (DECL_THUNK_P (fn))
8522 if (!DECL_NAME (fn))
8523 finish_thunk (fn);
8524 if (THUNK_ALIAS (fn))
8526 fn = THUNK_ALIAS (fn);
8527 BV_FN (v) = fn;
8529 fn_original = THUNK_TARGET (fn);
8532 /* If the only definition of this function signature along our
8533 primary base chain is from a lost primary, this vtable slot will
8534 never be used, so just zero it out. This is important to avoid
8535 requiring extra thunks which cannot be generated with the function.
8537 We first check this in update_vtable_entry_for_fn, so we handle
8538 restored primary bases properly; we also need to do it here so we
8539 zero out unused slots in ctor vtables, rather than filling them
8540 with erroneous values (though harmless, apart from relocation
8541 costs). */
8542 if (BV_LOST_PRIMARY (v))
8543 init = size_zero_node;
8545 if (! init)
8547 /* Pull the offset for `this', and the function to call, out of
8548 the list. */
8549 delta = BV_DELTA (v);
8550 vcall_index = BV_VCALL_INDEX (v);
8552 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
8553 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
8555 /* You can't call an abstract virtual function; it's abstract.
8556 So, we replace these functions with __pure_virtual. */
8557 if (DECL_PURE_VIRTUAL_P (fn_original))
8559 fn = abort_fndecl;
8560 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8562 if (abort_fndecl_addr == NULL)
8563 abort_fndecl_addr
8564 = fold_convert (vfunc_ptr_type_node,
8565 build_fold_addr_expr (fn));
8566 init = abort_fndecl_addr;
8569 /* Likewise for deleted virtuals. */
8570 else if (DECL_DELETED_FN (fn_original))
8572 fn = get_identifier ("__cxa_deleted_virtual");
8573 if (!get_global_value_if_present (fn, &fn))
8574 fn = push_library_fn (fn, (build_function_type_list
8575 (void_type_node, NULL_TREE)),
8576 NULL_TREE);
8577 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8578 init = fold_convert (vfunc_ptr_type_node,
8579 build_fold_addr_expr (fn));
8581 else
8583 if (!integer_zerop (delta) || vcall_index)
8585 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
8586 if (!DECL_NAME (fn))
8587 finish_thunk (fn);
8589 /* Take the address of the function, considering it to be of an
8590 appropriate generic type. */
8591 if (!TARGET_VTABLE_USES_DESCRIPTORS)
8592 init = fold_convert (vfunc_ptr_type_node,
8593 build_fold_addr_expr (fn));
8597 /* And add it to the chain of initializers. */
8598 if (TARGET_VTABLE_USES_DESCRIPTORS)
8600 int i;
8601 if (init == size_zero_node)
8602 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8603 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8604 else
8605 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
8607 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
8608 fn, build_int_cst (NULL_TREE, i));
8609 TREE_CONSTANT (fdesc) = 1;
8611 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
8614 else
8615 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8619 /* Adds to vid->inits the initializers for the vbase and vcall
8620 offsets in BINFO, which is in the hierarchy dominated by T. */
8622 static void
8623 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
8625 tree b;
8627 /* If this is a derived class, we must first create entries
8628 corresponding to the primary base class. */
8629 b = get_primary_binfo (binfo);
8630 if (b)
8631 build_vcall_and_vbase_vtbl_entries (b, vid);
8633 /* Add the vbase entries for this base. */
8634 build_vbase_offset_vtbl_entries (binfo, vid);
8635 /* Add the vcall entries for this base. */
8636 build_vcall_offset_vtbl_entries (binfo, vid);
8639 /* Returns the initializers for the vbase offset entries in the vtable
8640 for BINFO (which is part of the class hierarchy dominated by T), in
8641 reverse order. VBASE_OFFSET_INDEX gives the vtable index
8642 where the next vbase offset will go. */
8644 static void
8645 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8647 tree vbase;
8648 tree t;
8649 tree non_primary_binfo;
8651 /* If there are no virtual baseclasses, then there is nothing to
8652 do. */
8653 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8654 return;
8656 t = vid->derived;
8658 /* We might be a primary base class. Go up the inheritance hierarchy
8659 until we find the most derived class of which we are a primary base:
8660 it is the offset of that which we need to use. */
8661 non_primary_binfo = binfo;
8662 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8664 tree b;
8666 /* If we have reached a virtual base, then it must be a primary
8667 base (possibly multi-level) of vid->binfo, or we wouldn't
8668 have called build_vcall_and_vbase_vtbl_entries for it. But it
8669 might be a lost primary, so just skip down to vid->binfo. */
8670 if (BINFO_VIRTUAL_P (non_primary_binfo))
8672 non_primary_binfo = vid->binfo;
8673 break;
8676 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8677 if (get_primary_binfo (b) != non_primary_binfo)
8678 break;
8679 non_primary_binfo = b;
8682 /* Go through the virtual bases, adding the offsets. */
8683 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8684 vbase;
8685 vbase = TREE_CHAIN (vbase))
8687 tree b;
8688 tree delta;
8690 if (!BINFO_VIRTUAL_P (vbase))
8691 continue;
8693 /* Find the instance of this virtual base in the complete
8694 object. */
8695 b = copied_binfo (vbase, binfo);
8697 /* If we've already got an offset for this virtual base, we
8698 don't need another one. */
8699 if (BINFO_VTABLE_PATH_MARKED (b))
8700 continue;
8701 BINFO_VTABLE_PATH_MARKED (b) = 1;
8703 /* Figure out where we can find this vbase offset. */
8704 delta = size_binop (MULT_EXPR,
8705 vid->index,
8706 convert (ssizetype,
8707 TYPE_SIZE_UNIT (vtable_entry_type)));
8708 if (vid->primary_vtbl_p)
8709 BINFO_VPTR_FIELD (b) = delta;
8711 if (binfo != TYPE_BINFO (t))
8712 /* The vbase offset had better be the same. */
8713 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
8715 /* The next vbase will come at a more negative offset. */
8716 vid->index = size_binop (MINUS_EXPR, vid->index,
8717 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8719 /* The initializer is the delta from BINFO to this virtual base.
8720 The vbase offsets go in reverse inheritance-graph order, and
8721 we are walking in inheritance graph order so these end up in
8722 the right order. */
8723 delta = size_diffop_loc (input_location,
8724 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
8726 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
8727 fold_build1_loc (input_location, NOP_EXPR,
8728 vtable_entry_type, delta));
8732 /* Adds the initializers for the vcall offset entries in the vtable
8733 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
8734 to VID->INITS. */
8736 static void
8737 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
8739 /* We only need these entries if this base is a virtual base. We
8740 compute the indices -- but do not add to the vtable -- when
8741 building the main vtable for a class. */
8742 if (binfo == TYPE_BINFO (vid->derived)
8743 || (BINFO_VIRTUAL_P (binfo)
8744 /* If BINFO is RTTI_BINFO, then (since BINFO does not
8745 correspond to VID->DERIVED), we are building a primary
8746 construction virtual table. Since this is a primary
8747 virtual table, we do not need the vcall offsets for
8748 BINFO. */
8749 && binfo != vid->rtti_binfo))
8751 /* We need a vcall offset for each of the virtual functions in this
8752 vtable. For example:
8754 class A { virtual void f (); };
8755 class B1 : virtual public A { virtual void f (); };
8756 class B2 : virtual public A { virtual void f (); };
8757 class C: public B1, public B2 { virtual void f (); };
8759 A C object has a primary base of B1, which has a primary base of A. A
8760 C also has a secondary base of B2, which no longer has a primary base
8761 of A. So the B2-in-C construction vtable needs a secondary vtable for
8762 A, which will adjust the A* to a B2* to call f. We have no way of
8763 knowing what (or even whether) this offset will be when we define B2,
8764 so we store this "vcall offset" in the A sub-vtable and look it up in
8765 a "virtual thunk" for B2::f.
8767 We need entries for all the functions in our primary vtable and
8768 in our non-virtual bases' secondary vtables. */
8769 vid->vbase = binfo;
8770 /* If we are just computing the vcall indices -- but do not need
8771 the actual entries -- not that. */
8772 if (!BINFO_VIRTUAL_P (binfo))
8773 vid->generate_vcall_entries = false;
8774 /* Now, walk through the non-virtual bases, adding vcall offsets. */
8775 add_vcall_offset_vtbl_entries_r (binfo, vid);
8779 /* Build vcall offsets, starting with those for BINFO. */
8781 static void
8782 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
8784 int i;
8785 tree primary_binfo;
8786 tree base_binfo;
8788 /* Don't walk into virtual bases -- except, of course, for the
8789 virtual base for which we are building vcall offsets. Any
8790 primary virtual base will have already had its offsets generated
8791 through the recursion in build_vcall_and_vbase_vtbl_entries. */
8792 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
8793 return;
8795 /* If BINFO has a primary base, process it first. */
8796 primary_binfo = get_primary_binfo (binfo);
8797 if (primary_binfo)
8798 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
8800 /* Add BINFO itself to the list. */
8801 add_vcall_offset_vtbl_entries_1 (binfo, vid);
8803 /* Scan the non-primary bases of BINFO. */
8804 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8805 if (base_binfo != primary_binfo)
8806 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
8809 /* Called from build_vcall_offset_vtbl_entries_r. */
8811 static void
8812 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
8814 /* Make entries for the rest of the virtuals. */
8815 if (abi_version_at_least (2))
8817 tree orig_fn;
8819 /* The ABI requires that the methods be processed in declaration
8820 order. G++ 3.2 used the order in the vtable. */
8821 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
8822 orig_fn;
8823 orig_fn = DECL_CHAIN (orig_fn))
8824 if (DECL_VINDEX (orig_fn))
8825 add_vcall_offset (orig_fn, binfo, vid);
8827 else
8829 tree derived_virtuals;
8830 tree base_virtuals;
8831 tree orig_virtuals;
8832 /* If BINFO is a primary base, the most derived class which has
8833 BINFO as a primary base; otherwise, just BINFO. */
8834 tree non_primary_binfo;
8836 /* We might be a primary base class. Go up the inheritance hierarchy
8837 until we find the most derived class of which we are a primary base:
8838 it is the BINFO_VIRTUALS there that we need to consider. */
8839 non_primary_binfo = binfo;
8840 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8842 tree b;
8844 /* If we have reached a virtual base, then it must be vid->vbase,
8845 because we ignore other virtual bases in
8846 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
8847 base (possibly multi-level) of vid->binfo, or we wouldn't
8848 have called build_vcall_and_vbase_vtbl_entries for it. But it
8849 might be a lost primary, so just skip down to vid->binfo. */
8850 if (BINFO_VIRTUAL_P (non_primary_binfo))
8852 gcc_assert (non_primary_binfo == vid->vbase);
8853 non_primary_binfo = vid->binfo;
8854 break;
8857 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8858 if (get_primary_binfo (b) != non_primary_binfo)
8859 break;
8860 non_primary_binfo = b;
8863 if (vid->ctor_vtbl_p)
8864 /* For a ctor vtable we need the equivalent binfo within the hierarchy
8865 where rtti_binfo is the most derived type. */
8866 non_primary_binfo
8867 = original_binfo (non_primary_binfo, vid->rtti_binfo);
8869 for (base_virtuals = BINFO_VIRTUALS (binfo),
8870 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
8871 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
8872 base_virtuals;
8873 base_virtuals = TREE_CHAIN (base_virtuals),
8874 derived_virtuals = TREE_CHAIN (derived_virtuals),
8875 orig_virtuals = TREE_CHAIN (orig_virtuals))
8877 tree orig_fn;
8879 /* Find the declaration that originally caused this function to
8880 be present in BINFO_TYPE (binfo). */
8881 orig_fn = BV_FN (orig_virtuals);
8883 /* When processing BINFO, we only want to generate vcall slots for
8884 function slots introduced in BINFO. So don't try to generate
8885 one if the function isn't even defined in BINFO. */
8886 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
8887 continue;
8889 add_vcall_offset (orig_fn, binfo, vid);
8894 /* Add a vcall offset entry for ORIG_FN to the vtable. */
8896 static void
8897 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
8899 size_t i;
8900 tree vcall_offset;
8901 tree derived_entry;
8903 /* If there is already an entry for a function with the same
8904 signature as FN, then we do not need a second vcall offset.
8905 Check the list of functions already present in the derived
8906 class vtable. */
8907 FOR_EACH_VEC_ELT (tree, vid->fns, i, derived_entry)
8909 if (same_signature_p (derived_entry, orig_fn)
8910 /* We only use one vcall offset for virtual destructors,
8911 even though there are two virtual table entries. */
8912 || (DECL_DESTRUCTOR_P (derived_entry)
8913 && DECL_DESTRUCTOR_P (orig_fn)))
8914 return;
8917 /* If we are building these vcall offsets as part of building
8918 the vtable for the most derived class, remember the vcall
8919 offset. */
8920 if (vid->binfo == TYPE_BINFO (vid->derived))
8922 tree_pair_s elt = {orig_fn, vid->index};
8923 VEC_safe_push (tree_pair_s, gc, CLASSTYPE_VCALL_INDICES (vid->derived),
8924 elt);
8927 /* The next vcall offset will be found at a more negative
8928 offset. */
8929 vid->index = size_binop (MINUS_EXPR, vid->index,
8930 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8932 /* Keep track of this function. */
8933 VEC_safe_push (tree, gc, vid->fns, orig_fn);
8935 if (vid->generate_vcall_entries)
8937 tree base;
8938 tree fn;
8940 /* Find the overriding function. */
8941 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
8942 if (fn == error_mark_node)
8943 vcall_offset = build_zero_cst (vtable_entry_type);
8944 else
8946 base = TREE_VALUE (fn);
8948 /* The vbase we're working on is a primary base of
8949 vid->binfo. But it might be a lost primary, so its
8950 BINFO_OFFSET might be wrong, so we just use the
8951 BINFO_OFFSET from vid->binfo. */
8952 vcall_offset = size_diffop_loc (input_location,
8953 BINFO_OFFSET (base),
8954 BINFO_OFFSET (vid->binfo));
8955 vcall_offset = fold_build1_loc (input_location,
8956 NOP_EXPR, vtable_entry_type,
8957 vcall_offset);
8959 /* Add the initializer to the vtable. */
8960 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
8964 /* Return vtbl initializers for the RTTI entries corresponding to the
8965 BINFO's vtable. The RTTI entries should indicate the object given
8966 by VID->rtti_binfo. */
8968 static void
8969 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
8971 tree b;
8972 tree t;
8973 tree offset;
8974 tree decl;
8975 tree init;
8977 t = BINFO_TYPE (vid->rtti_binfo);
8979 /* To find the complete object, we will first convert to our most
8980 primary base, and then add the offset in the vtbl to that value. */
8981 b = binfo;
8982 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8983 && !BINFO_LOST_PRIMARY_P (b))
8985 tree primary_base;
8987 primary_base = get_primary_binfo (b);
8988 gcc_assert (BINFO_PRIMARY_P (primary_base)
8989 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8990 b = primary_base;
8992 offset = size_diffop_loc (input_location,
8993 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8995 /* The second entry is the address of the typeinfo object. */
8996 if (flag_rtti)
8997 decl = build_address (get_tinfo_decl (t));
8998 else
8999 decl = integer_zero_node;
9001 /* Convert the declaration to a type that can be stored in the
9002 vtable. */
9003 init = build_nop (vfunc_ptr_type_node, decl);
9004 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9006 /* Add the offset-to-top entry. It comes earlier in the vtable than
9007 the typeinfo entry. Convert the offset to look like a
9008 function pointer, so that we can put it in the vtable. */
9009 init = build_nop (vfunc_ptr_type_node, offset);
9010 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9013 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9014 accessibility. */
9016 bool
9017 uniquely_derived_from_p (tree parent, tree type)
9019 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9020 return base && base != error_mark_node;
9023 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9025 bool
9026 publicly_uniquely_derived_p (tree parent, tree type)
9028 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9029 NULL, tf_none);
9030 return base && base != error_mark_node;
9033 #include "gt-cp-class.h"