2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / cp / class.c
blob685b7b3fc18dda8b2bd2da4d34f8d1fdd72ad349
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "alias.h"
28 #include "tree.h"
29 #include "options.h"
30 #include "tm.h"
31 #include "stringpool.h"
32 #include "stor-layout.h"
33 #include "attribs.h"
34 #include "cp-tree.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "target.h"
38 #include "convert.h"
39 #include "hard-reg-set.h"
40 #include "function.h"
41 #include "cgraph.h"
42 #include "dumpfile.h"
43 #include "splay-tree.h"
44 #include "gimplify.h"
46 /* The number of nested classes being processed. If we are not in the
47 scope of any class, this is zero. */
49 int current_class_depth;
51 /* In order to deal with nested classes, we keep a stack of classes.
52 The topmost entry is the innermost class, and is the entry at index
53 CURRENT_CLASS_DEPTH */
55 typedef struct class_stack_node {
56 /* The name of the class. */
57 tree name;
59 /* The _TYPE node for the class. */
60 tree type;
62 /* The access specifier pending for new declarations in the scope of
63 this class. */
64 tree access;
66 /* If were defining TYPE, the names used in this class. */
67 splay_tree names_used;
69 /* Nonzero if this class is no longer open, because of a call to
70 push_to_top_level. */
71 size_t hidden;
72 }* class_stack_node_t;
74 struct vtbl_init_data
76 /* The base for which we're building initializers. */
77 tree binfo;
78 /* The type of the most-derived type. */
79 tree derived;
80 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
81 unless ctor_vtbl_p is true. */
82 tree rtti_binfo;
83 /* The negative-index vtable initializers built up so far. These
84 are in order from least negative index to most negative index. */
85 vec<constructor_elt, va_gc> *inits;
86 /* The binfo for the virtual base for which we're building
87 vcall offset initializers. */
88 tree vbase;
89 /* The functions in vbase for which we have already provided vcall
90 offsets. */
91 vec<tree, va_gc> *fns;
92 /* The vtable index of the next vcall or vbase offset. */
93 tree index;
94 /* Nonzero if we are building the initializer for the primary
95 vtable. */
96 int primary_vtbl_p;
97 /* Nonzero if we are building the initializer for a construction
98 vtable. */
99 int ctor_vtbl_p;
100 /* True when adding vcall offset entries to the vtable. False when
101 merely computing the indices. */
102 bool generate_vcall_entries;
105 /* The type of a function passed to walk_subobject_offsets. */
106 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
108 /* The stack itself. This is a dynamically resized array. The
109 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
110 static int current_class_stack_size;
111 static class_stack_node_t current_class_stack;
113 /* The size of the largest empty class seen in this translation unit. */
114 static GTY (()) tree sizeof_biggest_empty_class;
116 /* An array of all local classes present in this translation unit, in
117 declaration order. */
118 vec<tree, va_gc> *local_classes;
120 static tree get_vfield_name (tree);
121 static void finish_struct_anon (tree);
122 static tree get_vtable_name (tree);
123 static void get_basefndecls (tree, tree, vec<tree> *);
124 static int build_primary_vtable (tree, tree);
125 static int build_secondary_vtable (tree);
126 static void finish_vtbls (tree);
127 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
128 static void finish_struct_bits (tree);
129 static int alter_access (tree, tree, tree);
130 static void handle_using_decl (tree, tree);
131 static tree dfs_modify_vtables (tree, void *);
132 static tree modify_all_vtables (tree, tree);
133 static void determine_primary_bases (tree);
134 static void finish_struct_methods (tree);
135 static void maybe_warn_about_overly_private_class (tree);
136 static int method_name_cmp (const void *, const void *);
137 static int resort_method_name_cmp (const void *, const void *);
138 static void add_implicitly_declared_members (tree, tree*, int, int);
139 static tree fixed_type_or_null (tree, int *, int *);
140 static tree build_simple_base_path (tree expr, tree binfo);
141 static tree build_vtbl_ref_1 (tree, tree);
142 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
143 vec<constructor_elt, va_gc> **);
144 static int count_fields (tree);
145 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
146 static void insert_into_classtype_sorted_fields (tree, tree, int);
147 static bool check_bitfield_decl (tree);
148 static void check_field_decl (tree, tree, int *, int *, int *);
149 static void check_field_decls (tree, tree *, int *, int *);
150 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
151 static void build_base_fields (record_layout_info, splay_tree, tree *);
152 static void check_methods (tree);
153 static void remove_zero_width_bit_fields (tree);
154 static bool accessible_nvdtor_p (tree);
155 static void check_bases (tree, int *, int *);
156 static void check_bases_and_members (tree);
157 static tree create_vtable_ptr (tree, tree *);
158 static void include_empty_classes (record_layout_info);
159 static void layout_class_type (tree, tree *);
160 static void propagate_binfo_offsets (tree, tree);
161 static void layout_virtual_bases (record_layout_info, splay_tree);
162 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
164 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
165 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
166 static void add_vcall_offset (tree, tree, vtbl_init_data *);
167 static void layout_vtable_decl (tree, int);
168 static tree dfs_find_final_overrider_pre (tree, void *);
169 static tree dfs_find_final_overrider_post (tree, void *);
170 static tree find_final_overrider (tree, tree, tree);
171 static int make_new_vtable (tree, tree);
172 static tree get_primary_binfo (tree);
173 static int maybe_indent_hierarchy (FILE *, int, int);
174 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
175 static void dump_class_hierarchy (tree);
176 static void dump_class_hierarchy_1 (FILE *, int, tree);
177 static void dump_array (FILE *, tree);
178 static void dump_vtable (tree, tree, tree);
179 static void dump_vtt (tree, tree);
180 static void dump_thunk (FILE *, int, tree);
181 static tree build_vtable (tree, tree, tree);
182 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
183 static void layout_nonempty_base_or_field (record_layout_info,
184 tree, tree, splay_tree);
185 static tree end_of_class (tree, int);
186 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
187 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
188 vec<constructor_elt, va_gc> **);
189 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
190 vec<constructor_elt, va_gc> **);
191 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
192 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
193 static void clone_constructors_and_destructors (tree);
194 static tree build_clone (tree, tree);
195 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
196 static void build_ctor_vtbl_group (tree, tree);
197 static void build_vtt (tree);
198 static tree binfo_ctor_vtable (tree);
199 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
200 tree *);
201 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
202 static tree dfs_fixup_binfo_vtbls (tree, void *);
203 static int record_subobject_offset (tree, tree, splay_tree);
204 static int check_subobject_offset (tree, tree, splay_tree);
205 static int walk_subobject_offsets (tree, subobject_offset_fn,
206 tree, splay_tree, tree, int);
207 static void record_subobject_offsets (tree, tree, splay_tree, bool);
208 static int layout_conflict_p (tree, tree, splay_tree, int);
209 static int splay_tree_compare_integer_csts (splay_tree_key k1,
210 splay_tree_key k2);
211 static void warn_about_ambiguous_bases (tree);
212 static bool type_requires_array_cookie (tree);
213 static bool base_derived_from (tree, tree);
214 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
215 static tree end_of_base (tree);
216 static tree get_vcall_index (tree, tree);
218 /* Variables shared between class.c and call.c. */
220 int n_vtables = 0;
221 int n_vtable_entries = 0;
222 int n_vtable_searches = 0;
223 int n_vtable_elems = 0;
224 int n_convert_harshness = 0;
225 int n_compute_conversion_costs = 0;
226 int n_inner_fields_searched = 0;
228 /* Convert to or from a base subobject. EXPR is an expression of type
229 `A' or `A*', an expression of type `B' or `B*' is returned. To
230 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
231 the B base instance within A. To convert base A to derived B, CODE
232 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
233 In this latter case, A must not be a morally virtual base of B.
234 NONNULL is true if EXPR is known to be non-NULL (this is only
235 needed when EXPR is of pointer type). CV qualifiers are preserved
236 from EXPR. */
238 tree
239 build_base_path (enum tree_code code,
240 tree expr,
241 tree binfo,
242 int nonnull,
243 tsubst_flags_t complain)
245 tree v_binfo = NULL_TREE;
246 tree d_binfo = NULL_TREE;
247 tree probe;
248 tree offset;
249 tree target_type;
250 tree null_test = NULL;
251 tree ptr_target_type;
252 int fixed_type_p;
253 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
254 bool has_empty = false;
255 bool virtual_access;
256 bool rvalue = false;
258 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
259 return error_mark_node;
261 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
263 d_binfo = probe;
264 if (is_empty_class (BINFO_TYPE (probe)))
265 has_empty = true;
266 if (!v_binfo && BINFO_VIRTUAL_P (probe))
267 v_binfo = probe;
270 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
271 if (want_pointer)
272 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
274 if (code == PLUS_EXPR
275 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
277 /* This can happen when adjust_result_of_qualified_name_lookup can't
278 find a unique base binfo in a call to a member function. We
279 couldn't give the diagnostic then since we might have been calling
280 a static member function, so we do it now. */
281 if (complain & tf_error)
283 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
284 ba_unique, NULL, complain);
285 gcc_assert (base == error_mark_node);
287 return error_mark_node;
290 gcc_assert ((code == MINUS_EXPR
291 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
292 || code == PLUS_EXPR);
294 if (binfo == d_binfo)
295 /* Nothing to do. */
296 return expr;
298 if (code == MINUS_EXPR && v_binfo)
300 if (complain & tf_error)
302 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
304 if (want_pointer)
305 error ("cannot convert from pointer to base class %qT to "
306 "pointer to derived class %qT because the base is "
307 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
308 else
309 error ("cannot convert from base class %qT to derived "
310 "class %qT because the base is virtual",
311 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
313 else
315 if (want_pointer)
316 error ("cannot convert from pointer to base class %qT to "
317 "pointer to derived class %qT via virtual base %qT",
318 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
319 BINFO_TYPE (v_binfo));
320 else
321 error ("cannot convert from base class %qT to derived "
322 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
323 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
326 return error_mark_node;
329 if (!want_pointer)
331 rvalue = !real_lvalue_p (expr);
332 /* This must happen before the call to save_expr. */
333 expr = cp_build_addr_expr (expr, complain);
335 else
336 expr = mark_rvalue_use (expr);
338 offset = BINFO_OFFSET (binfo);
339 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
340 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
341 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
342 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
343 expression returned matches the input. */
344 target_type = cp_build_qualified_type
345 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
346 ptr_target_type = build_pointer_type (target_type);
348 /* Do we need to look in the vtable for the real offset? */
349 virtual_access = (v_binfo && fixed_type_p <= 0);
351 /* Don't bother with the calculations inside sizeof; they'll ICE if the
352 source type is incomplete and the pointer value doesn't matter. In a
353 template (even in instantiate_non_dependent_expr), we don't have vtables
354 set up properly yet, and the value doesn't matter there either; we're
355 just interested in the result of overload resolution. */
356 if (cp_unevaluated_operand != 0
357 || in_template_function ())
359 expr = build_nop (ptr_target_type, expr);
360 goto indout;
363 /* If we're in an NSDMI, we don't have the full constructor context yet
364 that we need for converting to a virtual base, so just build a stub
365 CONVERT_EXPR and expand it later in bot_replace. */
366 if (virtual_access && fixed_type_p < 0
367 && current_scope () != current_function_decl)
369 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
370 CONVERT_EXPR_VBASE_PATH (expr) = true;
371 goto indout;
374 /* Do we need to check for a null pointer? */
375 if (want_pointer && !nonnull)
377 /* If we know the conversion will not actually change the value
378 of EXPR, then we can avoid testing the expression for NULL.
379 We have to avoid generating a COMPONENT_REF for a base class
380 field, because other parts of the compiler know that such
381 expressions are always non-NULL. */
382 if (!virtual_access && integer_zerop (offset))
383 return build_nop (ptr_target_type, expr);
384 null_test = error_mark_node;
387 /* Protect against multiple evaluation if necessary. */
388 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
389 expr = save_expr (expr);
391 /* Now that we've saved expr, build the real null test. */
392 if (null_test)
394 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
395 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
396 expr, zero);
399 /* If this is a simple base reference, express it as a COMPONENT_REF. */
400 if (code == PLUS_EXPR && !virtual_access
401 /* We don't build base fields for empty bases, and they aren't very
402 interesting to the optimizers anyway. */
403 && !has_empty)
405 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
406 expr = build_simple_base_path (expr, binfo);
407 if (rvalue)
408 expr = move (expr);
409 if (want_pointer)
410 expr = build_address (expr);
411 target_type = TREE_TYPE (expr);
412 goto out;
415 if (virtual_access)
417 /* Going via virtual base V_BINFO. We need the static offset
418 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
419 V_BINFO. That offset is an entry in D_BINFO's vtable. */
420 tree v_offset;
422 if (fixed_type_p < 0 && in_base_initializer)
424 /* In a base member initializer, we cannot rely on the
425 vtable being set up. We have to indirect via the
426 vtt_parm. */
427 tree t;
429 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
430 t = build_pointer_type (t);
431 v_offset = convert (t, current_vtt_parm);
432 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
434 else
436 tree t = expr;
437 if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0)
439 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
440 probe, expr);
441 if (t == NULL_TREE)
442 t = expr;
444 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
445 complain),
446 TREE_TYPE (TREE_TYPE (expr)));
449 if (v_offset == error_mark_node)
450 return error_mark_node;
452 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
453 v_offset = build1 (NOP_EXPR,
454 build_pointer_type (ptrdiff_type_node),
455 v_offset);
456 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
457 TREE_CONSTANT (v_offset) = 1;
459 offset = convert_to_integer (ptrdiff_type_node,
460 size_diffop_loc (input_location, offset,
461 BINFO_OFFSET (v_binfo)));
463 if (!integer_zerop (offset))
464 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
466 if (fixed_type_p < 0)
467 /* Negative fixed_type_p means this is a constructor or destructor;
468 virtual base layout is fixed in in-charge [cd]tors, but not in
469 base [cd]tors. */
470 offset = build3 (COND_EXPR, ptrdiff_type_node,
471 build2 (EQ_EXPR, boolean_type_node,
472 current_in_charge_parm, integer_zero_node),
473 v_offset,
474 convert_to_integer (ptrdiff_type_node,
475 BINFO_OFFSET (binfo)));
476 else
477 offset = v_offset;
480 if (want_pointer)
481 target_type = ptr_target_type;
483 expr = build1 (NOP_EXPR, ptr_target_type, expr);
485 if (!integer_zerop (offset))
487 offset = fold_convert (sizetype, offset);
488 if (code == MINUS_EXPR)
489 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
490 expr = fold_build_pointer_plus (expr, offset);
492 else
493 null_test = NULL;
495 indout:
496 if (!want_pointer)
498 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
499 if (rvalue)
500 expr = move (expr);
503 out:
504 if (null_test)
505 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
506 build_zero_cst (target_type));
508 return expr;
511 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
512 Perform a derived-to-base conversion by recursively building up a
513 sequence of COMPONENT_REFs to the appropriate base fields. */
515 static tree
516 build_simple_base_path (tree expr, tree binfo)
518 tree type = BINFO_TYPE (binfo);
519 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
520 tree field;
522 if (d_binfo == NULL_TREE)
524 tree temp;
526 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
528 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
529 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
530 an lvalue in the front end; only _DECLs and _REFs are lvalues
531 in the back end. */
532 temp = unary_complex_lvalue (ADDR_EXPR, expr);
533 if (temp)
534 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
536 return expr;
539 /* Recurse. */
540 expr = build_simple_base_path (expr, d_binfo);
542 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
543 field; field = DECL_CHAIN (field))
544 /* Is this the base field created by build_base_field? */
545 if (TREE_CODE (field) == FIELD_DECL
546 && DECL_FIELD_IS_BASE (field)
547 && TREE_TYPE (field) == type
548 /* If we're looking for a field in the most-derived class,
549 also check the field offset; we can have two base fields
550 of the same type if one is an indirect virtual base and one
551 is a direct non-virtual base. */
552 && (BINFO_INHERITANCE_CHAIN (d_binfo)
553 || tree_int_cst_equal (byte_position (field),
554 BINFO_OFFSET (binfo))))
556 /* We don't use build_class_member_access_expr here, as that
557 has unnecessary checks, and more importantly results in
558 recursive calls to dfs_walk_once. */
559 int type_quals = cp_type_quals (TREE_TYPE (expr));
561 expr = build3 (COMPONENT_REF,
562 cp_build_qualified_type (type, type_quals),
563 expr, field, NULL_TREE);
564 expr = fold_if_not_in_template (expr);
566 /* Mark the expression const or volatile, as appropriate.
567 Even though we've dealt with the type above, we still have
568 to mark the expression itself. */
569 if (type_quals & TYPE_QUAL_CONST)
570 TREE_READONLY (expr) = 1;
571 if (type_quals & TYPE_QUAL_VOLATILE)
572 TREE_THIS_VOLATILE (expr) = 1;
574 return expr;
577 /* Didn't find the base field?!? */
578 gcc_unreachable ();
581 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
582 type is a class type or a pointer to a class type. In the former
583 case, TYPE is also a class type; in the latter it is another
584 pointer type. If CHECK_ACCESS is true, an error message is emitted
585 if TYPE is inaccessible. If OBJECT has pointer type, the value is
586 assumed to be non-NULL. */
588 tree
589 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
590 tsubst_flags_t complain)
592 tree binfo;
593 tree object_type;
595 if (TYPE_PTR_P (TREE_TYPE (object)))
597 object_type = TREE_TYPE (TREE_TYPE (object));
598 type = TREE_TYPE (type);
600 else
601 object_type = TREE_TYPE (object);
603 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
604 NULL, complain);
605 if (!binfo || binfo == error_mark_node)
606 return error_mark_node;
608 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
611 /* EXPR is an expression with unqualified class type. BASE is a base
612 binfo of that class type. Returns EXPR, converted to the BASE
613 type. This function assumes that EXPR is the most derived class;
614 therefore virtual bases can be found at their static offsets. */
616 tree
617 convert_to_base_statically (tree expr, tree base)
619 tree expr_type;
621 expr_type = TREE_TYPE (expr);
622 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
624 /* If this is a non-empty base, use a COMPONENT_REF. */
625 if (!is_empty_class (BINFO_TYPE (base)))
626 return build_simple_base_path (expr, base);
628 /* We use fold_build2 and fold_convert below to simplify the trees
629 provided to the optimizers. It is not safe to call these functions
630 when processing a template because they do not handle C++-specific
631 trees. */
632 gcc_assert (!processing_template_decl);
633 expr = cp_build_addr_expr (expr, tf_warning_or_error);
634 if (!integer_zerop (BINFO_OFFSET (base)))
635 expr = fold_build_pointer_plus_loc (input_location,
636 expr, BINFO_OFFSET (base));
637 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
638 expr = build_fold_indirect_ref_loc (input_location, expr);
641 return expr;
645 tree
646 build_vfield_ref (tree datum, tree type)
648 tree vfield, vcontext;
650 if (datum == error_mark_node
651 /* Can happen in case of duplicate base types (c++/59082). */
652 || !TYPE_VFIELD (type))
653 return error_mark_node;
655 /* First, convert to the requested type. */
656 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
657 datum = convert_to_base (datum, type, /*check_access=*/false,
658 /*nonnull=*/true, tf_warning_or_error);
660 /* Second, the requested type may not be the owner of its own vptr.
661 If not, convert to the base class that owns it. We cannot use
662 convert_to_base here, because VCONTEXT may appear more than once
663 in the inheritance hierarchy of TYPE, and thus direct conversion
664 between the types may be ambiguous. Following the path back up
665 one step at a time via primary bases avoids the problem. */
666 vfield = TYPE_VFIELD (type);
667 vcontext = DECL_CONTEXT (vfield);
668 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
670 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
671 type = TREE_TYPE (datum);
674 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
677 /* Given an object INSTANCE, return an expression which yields the
678 vtable element corresponding to INDEX. There are many special
679 cases for INSTANCE which we take care of here, mainly to avoid
680 creating extra tree nodes when we don't have to. */
682 static tree
683 build_vtbl_ref_1 (tree instance, tree idx)
685 tree aref;
686 tree vtbl = NULL_TREE;
688 /* Try to figure out what a reference refers to, and
689 access its virtual function table directly. */
691 int cdtorp = 0;
692 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
694 tree basetype = non_reference (TREE_TYPE (instance));
696 if (fixed_type && !cdtorp)
698 tree binfo = lookup_base (fixed_type, basetype,
699 ba_unique, NULL, tf_none);
700 if (binfo && binfo != error_mark_node)
701 vtbl = unshare_expr (BINFO_VTABLE (binfo));
704 if (!vtbl)
705 vtbl = build_vfield_ref (instance, basetype);
707 aref = build_array_ref (input_location, vtbl, idx);
708 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
710 return aref;
713 tree
714 build_vtbl_ref (tree instance, tree idx)
716 tree aref = build_vtbl_ref_1 (instance, idx);
718 return aref;
721 /* Given a stable object pointer INSTANCE_PTR, return an expression which
722 yields a function pointer corresponding to vtable element INDEX. */
724 tree
725 build_vfn_ref (tree instance_ptr, tree idx)
727 tree aref;
729 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
730 tf_warning_or_error),
731 idx);
733 /* When using function descriptors, the address of the
734 vtable entry is treated as a function pointer. */
735 if (TARGET_VTABLE_USES_DESCRIPTORS)
736 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
737 cp_build_addr_expr (aref, tf_warning_or_error));
739 /* Remember this as a method reference, for later devirtualization. */
740 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
742 return aref;
745 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
746 for the given TYPE. */
748 static tree
749 get_vtable_name (tree type)
751 return mangle_vtbl_for_type (type);
754 /* DECL is an entity associated with TYPE, like a virtual table or an
755 implicitly generated constructor. Determine whether or not DECL
756 should have external or internal linkage at the object file
757 level. This routine does not deal with COMDAT linkage and other
758 similar complexities; it simply sets TREE_PUBLIC if it possible for
759 entities in other translation units to contain copies of DECL, in
760 the abstract. */
762 void
763 set_linkage_according_to_type (tree /*type*/, tree decl)
765 TREE_PUBLIC (decl) = 1;
766 determine_visibility (decl);
769 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
770 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
771 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
773 static tree
774 build_vtable (tree class_type, tree name, tree vtable_type)
776 tree decl;
778 decl = build_lang_decl (VAR_DECL, name, vtable_type);
779 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
780 now to avoid confusion in mangle_decl. */
781 SET_DECL_ASSEMBLER_NAME (decl, name);
782 DECL_CONTEXT (decl) = class_type;
783 DECL_ARTIFICIAL (decl) = 1;
784 TREE_STATIC (decl) = 1;
785 TREE_READONLY (decl) = 1;
786 DECL_VIRTUAL_P (decl) = 1;
787 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
788 DECL_USER_ALIGN (decl) = true;
789 DECL_VTABLE_OR_VTT_P (decl) = 1;
790 set_linkage_according_to_type (class_type, decl);
791 /* The vtable has not been defined -- yet. */
792 DECL_EXTERNAL (decl) = 1;
793 DECL_NOT_REALLY_EXTERN (decl) = 1;
795 /* Mark the VAR_DECL node representing the vtable itself as a
796 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
797 is rather important that such things be ignored because any
798 effort to actually generate DWARF for them will run into
799 trouble when/if we encounter code like:
801 #pragma interface
802 struct S { virtual void member (); };
804 because the artificial declaration of the vtable itself (as
805 manufactured by the g++ front end) will say that the vtable is
806 a static member of `S' but only *after* the debug output for
807 the definition of `S' has already been output. This causes
808 grief because the DWARF entry for the definition of the vtable
809 will try to refer back to an earlier *declaration* of the
810 vtable as a static member of `S' and there won't be one. We
811 might be able to arrange to have the "vtable static member"
812 attached to the member list for `S' before the debug info for
813 `S' get written (which would solve the problem) but that would
814 require more intrusive changes to the g++ front end. */
815 DECL_IGNORED_P (decl) = 1;
817 return decl;
820 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
821 or even complete. If this does not exist, create it. If COMPLETE is
822 nonzero, then complete the definition of it -- that will render it
823 impossible to actually build the vtable, but is useful to get at those
824 which are known to exist in the runtime. */
826 tree
827 get_vtable_decl (tree type, int complete)
829 tree decl;
831 if (CLASSTYPE_VTABLES (type))
832 return CLASSTYPE_VTABLES (type);
834 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
835 CLASSTYPE_VTABLES (type) = decl;
837 if (complete)
839 DECL_EXTERNAL (decl) = 1;
840 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
843 return decl;
846 /* Build the primary virtual function table for TYPE. If BINFO is
847 non-NULL, build the vtable starting with the initial approximation
848 that it is the same as the one which is the head of the association
849 list. Returns a nonzero value if a new vtable is actually
850 created. */
852 static int
853 build_primary_vtable (tree binfo, tree type)
855 tree decl;
856 tree virtuals;
858 decl = get_vtable_decl (type, /*complete=*/0);
860 if (binfo)
862 if (BINFO_NEW_VTABLE_MARKED (binfo))
863 /* We have already created a vtable for this base, so there's
864 no need to do it again. */
865 return 0;
867 virtuals = copy_list (BINFO_VIRTUALS (binfo));
868 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
869 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
870 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
872 else
874 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
875 virtuals = NULL_TREE;
878 if (GATHER_STATISTICS)
880 n_vtables += 1;
881 n_vtable_elems += list_length (virtuals);
884 /* Initialize the association list for this type, based
885 on our first approximation. */
886 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
887 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
888 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
889 return 1;
892 /* Give BINFO a new virtual function table which is initialized
893 with a skeleton-copy of its original initialization. The only
894 entry that changes is the `delta' entry, so we can really
895 share a lot of structure.
897 FOR_TYPE is the most derived type which caused this table to
898 be needed.
900 Returns nonzero if we haven't met BINFO before.
902 The order in which vtables are built (by calling this function) for
903 an object must remain the same, otherwise a binary incompatibility
904 can result. */
906 static int
907 build_secondary_vtable (tree binfo)
909 if (BINFO_NEW_VTABLE_MARKED (binfo))
910 /* We already created a vtable for this base. There's no need to
911 do it again. */
912 return 0;
914 /* Remember that we've created a vtable for this BINFO, so that we
915 don't try to do so again. */
916 SET_BINFO_NEW_VTABLE_MARKED (binfo);
918 /* Make fresh virtual list, so we can smash it later. */
919 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
921 /* Secondary vtables are laid out as part of the same structure as
922 the primary vtable. */
923 BINFO_VTABLE (binfo) = NULL_TREE;
924 return 1;
927 /* Create a new vtable for BINFO which is the hierarchy dominated by
928 T. Return nonzero if we actually created a new vtable. */
930 static int
931 make_new_vtable (tree t, tree binfo)
933 if (binfo == TYPE_BINFO (t))
934 /* In this case, it is *type*'s vtable we are modifying. We start
935 with the approximation that its vtable is that of the
936 immediate base class. */
937 return build_primary_vtable (binfo, t);
938 else
939 /* This is our very own copy of `basetype' to play with. Later,
940 we will fill in all the virtual functions that override the
941 virtual functions in these base classes which are not defined
942 by the current type. */
943 return build_secondary_vtable (binfo);
946 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
947 (which is in the hierarchy dominated by T) list FNDECL as its
948 BV_FN. DELTA is the required constant adjustment from the `this'
949 pointer where the vtable entry appears to the `this' required when
950 the function is actually called. */
952 static void
953 modify_vtable_entry (tree t,
954 tree binfo,
955 tree fndecl,
956 tree delta,
957 tree *virtuals)
959 tree v;
961 v = *virtuals;
963 if (fndecl != BV_FN (v)
964 || !tree_int_cst_equal (delta, BV_DELTA (v)))
966 /* We need a new vtable for BINFO. */
967 if (make_new_vtable (t, binfo))
969 /* If we really did make a new vtable, we also made a copy
970 of the BINFO_VIRTUALS list. Now, we have to find the
971 corresponding entry in that list. */
972 *virtuals = BINFO_VIRTUALS (binfo);
973 while (BV_FN (*virtuals) != BV_FN (v))
974 *virtuals = TREE_CHAIN (*virtuals);
975 v = *virtuals;
978 BV_DELTA (v) = delta;
979 BV_VCALL_INDEX (v) = NULL_TREE;
980 BV_FN (v) = fndecl;
985 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
986 the USING_DECL naming METHOD. Returns true if the method could be
987 added to the method vec. */
989 bool
990 add_method (tree type, tree method, tree using_decl)
992 unsigned slot;
993 tree overload;
994 bool template_conv_p = false;
995 bool conv_p;
996 vec<tree, va_gc> *method_vec;
997 bool complete_p;
998 bool insert_p = false;
999 tree current_fns;
1000 tree fns;
1002 if (method == error_mark_node)
1003 return false;
1005 complete_p = COMPLETE_TYPE_P (type);
1006 conv_p = DECL_CONV_FN_P (method);
1007 if (conv_p)
1008 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1009 && DECL_TEMPLATE_CONV_FN_P (method));
1011 method_vec = CLASSTYPE_METHOD_VEC (type);
1012 if (!method_vec)
1014 /* Make a new method vector. We start with 8 entries. We must
1015 allocate at least two (for constructors and destructors), and
1016 we're going to end up with an assignment operator at some
1017 point as well. */
1018 vec_alloc (method_vec, 8);
1019 /* Create slots for constructors and destructors. */
1020 method_vec->quick_push (NULL_TREE);
1021 method_vec->quick_push (NULL_TREE);
1022 CLASSTYPE_METHOD_VEC (type) = method_vec;
1025 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1026 grok_special_member_properties (method);
1028 /* Constructors and destructors go in special slots. */
1029 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1030 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1031 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1033 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1035 if (TYPE_FOR_JAVA (type))
1037 if (!DECL_ARTIFICIAL (method))
1038 error ("Java class %qT cannot have a destructor", type);
1039 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1040 error ("Java class %qT cannot have an implicit non-trivial "
1041 "destructor",
1042 type);
1045 else
1047 tree m;
1049 insert_p = true;
1050 /* See if we already have an entry with this name. */
1051 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1052 vec_safe_iterate (method_vec, slot, &m);
1053 ++slot)
1055 m = OVL_CURRENT (m);
1056 if (template_conv_p)
1058 if (TREE_CODE (m) == TEMPLATE_DECL
1059 && DECL_TEMPLATE_CONV_FN_P (m))
1060 insert_p = false;
1061 break;
1063 if (conv_p && !DECL_CONV_FN_P (m))
1064 break;
1065 if (DECL_NAME (m) == DECL_NAME (method))
1067 insert_p = false;
1068 break;
1070 if (complete_p
1071 && !DECL_CONV_FN_P (m)
1072 && DECL_NAME (m) > DECL_NAME (method))
1073 break;
1076 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1078 /* Check to see if we've already got this method. */
1079 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1081 tree fn = OVL_CURRENT (fns);
1082 tree fn_type;
1083 tree method_type;
1084 tree parms1;
1085 tree parms2;
1087 if (TREE_CODE (fn) != TREE_CODE (method))
1088 continue;
1090 /* [over.load] Member function declarations with the
1091 same name and the same parameter types cannot be
1092 overloaded if any of them is a static member
1093 function declaration.
1095 [over.load] Member function declarations with the same name and
1096 the same parameter-type-list as well as member function template
1097 declarations with the same name, the same parameter-type-list, and
1098 the same template parameter lists cannot be overloaded if any of
1099 them, but not all, have a ref-qualifier.
1101 [namespace.udecl] When a using-declaration brings names
1102 from a base class into a derived class scope, member
1103 functions in the derived class override and/or hide member
1104 functions with the same name and parameter types in a base
1105 class (rather than conflicting). */
1106 fn_type = TREE_TYPE (fn);
1107 method_type = TREE_TYPE (method);
1108 parms1 = TYPE_ARG_TYPES (fn_type);
1109 parms2 = TYPE_ARG_TYPES (method_type);
1111 /* Compare the quals on the 'this' parm. Don't compare
1112 the whole types, as used functions are treated as
1113 coming from the using class in overload resolution. */
1114 if (! DECL_STATIC_FUNCTION_P (fn)
1115 && ! DECL_STATIC_FUNCTION_P (method)
1116 /* Either both or neither need to be ref-qualified for
1117 differing quals to allow overloading. */
1118 && (FUNCTION_REF_QUALIFIED (fn_type)
1119 == FUNCTION_REF_QUALIFIED (method_type))
1120 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1121 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1122 continue;
1124 /* For templates, the return type and template parameters
1125 must be identical. */
1126 if (TREE_CODE (fn) == TEMPLATE_DECL
1127 && (!same_type_p (TREE_TYPE (fn_type),
1128 TREE_TYPE (method_type))
1129 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1130 DECL_TEMPLATE_PARMS (method))))
1131 continue;
1133 if (! DECL_STATIC_FUNCTION_P (fn))
1134 parms1 = TREE_CHAIN (parms1);
1135 if (! DECL_STATIC_FUNCTION_P (method))
1136 parms2 = TREE_CHAIN (parms2);
1138 if (compparms (parms1, parms2)
1139 && (!DECL_CONV_FN_P (fn)
1140 || same_type_p (TREE_TYPE (fn_type),
1141 TREE_TYPE (method_type)))
1142 && equivalently_constrained (fn, method))
1144 /* For function versions, their parms and types match
1145 but they are not duplicates. Record function versions
1146 as and when they are found. extern "C" functions are
1147 not treated as versions. */
1148 if (TREE_CODE (fn) == FUNCTION_DECL
1149 && TREE_CODE (method) == FUNCTION_DECL
1150 && !DECL_EXTERN_C_P (fn)
1151 && !DECL_EXTERN_C_P (method)
1152 && targetm.target_option.function_versions (fn, method))
1154 /* Mark functions as versions if necessary. Modify the mangled
1155 decl name if necessary. */
1156 if (!DECL_FUNCTION_VERSIONED (fn))
1158 DECL_FUNCTION_VERSIONED (fn) = 1;
1159 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1160 mangle_decl (fn);
1162 if (!DECL_FUNCTION_VERSIONED (method))
1164 DECL_FUNCTION_VERSIONED (method) = 1;
1165 if (DECL_ASSEMBLER_NAME_SET_P (method))
1166 mangle_decl (method);
1168 cgraph_node::record_function_versions (fn, method);
1169 continue;
1171 if (DECL_INHERITED_CTOR_BASE (method))
1173 if (DECL_INHERITED_CTOR_BASE (fn))
1175 error_at (DECL_SOURCE_LOCATION (method),
1176 "%q#D inherited from %qT", method,
1177 DECL_INHERITED_CTOR_BASE (method));
1178 error_at (DECL_SOURCE_LOCATION (fn),
1179 "conflicts with version inherited from %qT",
1180 DECL_INHERITED_CTOR_BASE (fn));
1182 /* Otherwise defer to the other function. */
1183 return false;
1185 if (using_decl)
1187 if (DECL_CONTEXT (fn) == type)
1188 /* Defer to the local function. */
1189 return false;
1191 else
1193 error ("%q+#D cannot be overloaded", method);
1194 error ("with %q+#D", fn);
1197 /* We don't call duplicate_decls here to merge the
1198 declarations because that will confuse things if the
1199 methods have inline definitions. In particular, we
1200 will crash while processing the definitions. */
1201 return false;
1205 /* A class should never have more than one destructor. */
1206 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1207 return false;
1209 /* Add the new binding. */
1210 if (using_decl)
1212 overload = ovl_cons (method, current_fns);
1213 OVL_USED (overload) = true;
1215 else
1216 overload = build_overload (method, current_fns);
1218 if (conv_p)
1219 TYPE_HAS_CONVERSION (type) = 1;
1220 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1221 push_class_level_binding (DECL_NAME (method), overload);
1223 if (insert_p)
1225 bool reallocated;
1227 /* We only expect to add few methods in the COMPLETE_P case, so
1228 just make room for one more method in that case. */
1229 if (complete_p)
1230 reallocated = vec_safe_reserve_exact (method_vec, 1);
1231 else
1232 reallocated = vec_safe_reserve (method_vec, 1);
1233 if (reallocated)
1234 CLASSTYPE_METHOD_VEC (type) = method_vec;
1235 if (slot == method_vec->length ())
1236 method_vec->quick_push (overload);
1237 else
1238 method_vec->quick_insert (slot, overload);
1240 else
1241 /* Replace the current slot. */
1242 (*method_vec)[slot] = overload;
1243 return true;
1246 /* Subroutines of finish_struct. */
1248 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1249 legit, otherwise return 0. */
1251 static int
1252 alter_access (tree t, tree fdecl, tree access)
1254 tree elem;
1256 if (!DECL_LANG_SPECIFIC (fdecl))
1257 retrofit_lang_decl (fdecl);
1259 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1261 elem = purpose_member (t, DECL_ACCESS (fdecl));
1262 if (elem)
1264 if (TREE_VALUE (elem) != access)
1266 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1267 error ("conflicting access specifications for method"
1268 " %q+D, ignored", TREE_TYPE (fdecl));
1269 else
1270 error ("conflicting access specifications for field %qE, ignored",
1271 DECL_NAME (fdecl));
1273 else
1275 /* They're changing the access to the same thing they changed
1276 it to before. That's OK. */
1280 else
1282 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1283 tf_warning_or_error);
1284 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1285 return 1;
1287 return 0;
1290 /* Process the USING_DECL, which is a member of T. */
1292 static void
1293 handle_using_decl (tree using_decl, tree t)
1295 tree decl = USING_DECL_DECLS (using_decl);
1296 tree name = DECL_NAME (using_decl);
1297 tree access
1298 = TREE_PRIVATE (using_decl) ? access_private_node
1299 : TREE_PROTECTED (using_decl) ? access_protected_node
1300 : access_public_node;
1301 tree flist = NULL_TREE;
1302 tree old_value;
1304 gcc_assert (!processing_template_decl && decl);
1306 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1307 tf_warning_or_error);
1308 if (old_value)
1310 if (is_overloaded_fn (old_value))
1311 old_value = OVL_CURRENT (old_value);
1313 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1314 /* OK */;
1315 else
1316 old_value = NULL_TREE;
1319 cp_emit_debug_info_for_using (decl, t);
1321 if (is_overloaded_fn (decl))
1322 flist = decl;
1324 if (! old_value)
1326 else if (is_overloaded_fn (old_value))
1328 if (flist)
1329 /* It's OK to use functions from a base when there are functions with
1330 the same name already present in the current class. */;
1331 else
1333 error ("%q+D invalid in %q#T", using_decl, t);
1334 error (" because of local method %q+#D with same name",
1335 OVL_CURRENT (old_value));
1336 return;
1339 else if (!DECL_ARTIFICIAL (old_value))
1341 error ("%q+D invalid in %q#T", using_decl, t);
1342 error (" because of local member %q+#D with same name", old_value);
1343 return;
1346 /* Make type T see field decl FDECL with access ACCESS. */
1347 if (flist)
1348 for (; flist; flist = OVL_NEXT (flist))
1350 add_method (t, OVL_CURRENT (flist), using_decl);
1351 alter_access (t, OVL_CURRENT (flist), access);
1353 else
1354 alter_access (t, decl, access);
1357 /* Data structure for find_abi_tags_r, below. */
1359 struct abi_tag_data
1361 tree t; // The type that we're checking for missing tags.
1362 tree subob; // The subobject of T that we're getting tags from.
1363 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1366 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1367 in the context of P. TAG can be either an identifier (the DECL_NAME of
1368 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1370 static void
1371 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1373 if (!IDENTIFIER_MARKED (id))
1375 if (p->tags != error_mark_node)
1377 /* We're collecting tags from template arguments or from
1378 the type of a variable or function return type. */
1379 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1381 /* Don't inherit this tag multiple times. */
1382 IDENTIFIER_MARKED (id) = true;
1384 if (TYPE_P (p->t))
1386 /* Tags inherited from type template arguments are only used
1387 to avoid warnings. */
1388 ABI_TAG_IMPLICIT (p->tags) = true;
1389 return;
1391 /* For functions and variables we want to warn, too. */
1394 /* Otherwise we're diagnosing missing tags. */
1395 if (TREE_CODE (p->t) == FUNCTION_DECL)
1397 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1398 "that %qT (used in its return type) has",
1399 p->t, tag, *tp))
1400 inform (location_of (*tp), "%qT declared here", *tp);
1402 else if (VAR_P (p->t))
1404 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1405 "that %qT (used in its type) has", p->t, tag, *tp))
1406 inform (location_of (*tp), "%qT declared here", *tp);
1408 else if (TYPE_P (p->subob))
1410 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1411 "that base %qT has", p->t, tag, p->subob))
1412 inform (location_of (p->subob), "%qT declared here",
1413 p->subob);
1415 else
1417 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1418 "that %qT (used in the type of %qD) has",
1419 p->t, tag, *tp, p->subob))
1421 inform (location_of (p->subob), "%qD declared here",
1422 p->subob);
1423 inform (location_of (*tp), "%qT declared here", *tp);
1429 /* Find all the ABI tags in the attribute list ATTR and either call
1430 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1432 static void
1433 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1435 if (!attr)
1436 return;
1437 for (; (attr = lookup_attribute ("abi_tag", attr));
1438 attr = TREE_CHAIN (attr))
1439 for (tree list = TREE_VALUE (attr); list;
1440 list = TREE_CHAIN (list))
1442 tree tag = TREE_VALUE (list);
1443 tree id = get_identifier (TREE_STRING_POINTER (tag));
1444 if (tp)
1445 check_tag (tag, id, tp, p);
1446 else
1447 IDENTIFIER_MARKED (id) = val;
1451 /* Find all the ABI tags on T and its enclosing scopes and either call
1452 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1454 static void
1455 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1457 while (t != global_namespace)
1459 tree attr;
1460 if (TYPE_P (t))
1462 attr = TYPE_ATTRIBUTES (t);
1463 t = CP_TYPE_CONTEXT (t);
1465 else
1467 attr = DECL_ATTRIBUTES (t);
1468 t = CP_DECL_CONTEXT (t);
1470 mark_or_check_attr_tags (attr, tp, p, val);
1474 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1475 types with ABI tags, add the corresponding identifiers to the VEC in
1476 *DATA and set IDENTIFIER_MARKED. */
1478 static tree
1479 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1481 if (!OVERLOAD_TYPE_P (*tp))
1482 return NULL_TREE;
1484 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1485 anyway, but let's make sure of it. */
1486 *walk_subtrees = false;
1488 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1490 mark_or_check_tags (*tp, tp, p, false);
1492 return NULL_TREE;
1495 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1496 IDENTIFIER_MARKED on its ABI tags. */
1498 static tree
1499 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1501 if (!OVERLOAD_TYPE_P (*tp))
1502 return NULL_TREE;
1504 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1505 anyway, but let's make sure of it. */
1506 *walk_subtrees = false;
1508 bool *valp = static_cast<bool*>(data);
1510 mark_or_check_tags (*tp, NULL, NULL, *valp);
1512 return NULL_TREE;
1515 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1516 scopes. */
1518 static void
1519 mark_abi_tags (tree t, bool val)
1521 mark_or_check_tags (t, NULL, NULL, val);
1522 if (DECL_P (t))
1524 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1525 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1527 /* Template arguments are part of the signature. */
1528 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1529 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1531 tree arg = TREE_VEC_ELT (level, j);
1532 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1535 if (TREE_CODE (t) == FUNCTION_DECL)
1536 /* A function's parameter types are part of the signature, so
1537 we don't need to inherit any tags that are also in them. */
1538 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1539 arg = TREE_CHAIN (arg))
1540 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1541 mark_abi_tags_r, &val);
1545 /* Check that T has all the ABI tags that subobject SUBOB has, or
1546 warn if not. If T is a (variable or function) declaration, also
1547 add any missing tags. */
1549 static void
1550 check_abi_tags (tree t, tree subob)
1552 bool inherit = DECL_P (t);
1554 if (!inherit && !warn_abi_tag)
1555 return;
1557 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1558 if (!TREE_PUBLIC (decl))
1559 /* No need to worry about things local to this TU. */
1560 return;
1562 mark_abi_tags (t, true);
1564 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1565 struct abi_tag_data data = { t, subob, error_mark_node };
1566 if (inherit)
1567 data.tags = NULL_TREE;
1569 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1571 if (inherit && data.tags)
1573 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1574 if (attr)
1575 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1576 else
1577 DECL_ATTRIBUTES (t)
1578 = tree_cons (get_identifier ("abi_tag"), data.tags,
1579 DECL_ATTRIBUTES (t));
1582 mark_abi_tags (t, false);
1585 /* Check that DECL has all the ABI tags that are used in parts of its type
1586 that are not reflected in its mangled name. */
1588 void
1589 check_abi_tags (tree decl)
1591 if (VAR_P (decl))
1592 check_abi_tags (decl, TREE_TYPE (decl));
1593 else if (TREE_CODE (decl) == FUNCTION_DECL
1594 && !mangle_return_type_p (decl))
1595 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1598 void
1599 inherit_targ_abi_tags (tree t)
1601 if (!CLASS_TYPE_P (t)
1602 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1603 return;
1605 mark_abi_tags (t, true);
1607 tree args = CLASSTYPE_TI_ARGS (t);
1608 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1609 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1611 tree level = TMPL_ARGS_LEVEL (args, i+1);
1612 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1614 tree arg = TREE_VEC_ELT (level, j);
1615 data.subob = arg;
1616 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1620 // If we found some tags on our template arguments, add them to our
1621 // abi_tag attribute.
1622 if (data.tags)
1624 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1625 if (attr)
1626 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1627 else
1628 TYPE_ATTRIBUTES (t)
1629 = tree_cons (get_identifier ("abi_tag"), data.tags,
1630 TYPE_ATTRIBUTES (t));
1633 mark_abi_tags (t, false);
1636 /* Return true, iff class T has a non-virtual destructor that is
1637 accessible from outside the class heirarchy (i.e. is public, or
1638 there's a suitable friend. */
1640 static bool
1641 accessible_nvdtor_p (tree t)
1643 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1645 /* An implicitly declared destructor is always public. And,
1646 if it were virtual, we would have created it by now. */
1647 if (!dtor)
1648 return true;
1650 if (DECL_VINDEX (dtor))
1651 return false; /* Virtual */
1653 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1654 return true; /* Public */
1656 if (CLASSTYPE_FRIEND_CLASSES (t)
1657 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1658 return true; /* Has friends */
1660 return false;
1663 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1664 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1665 properties of the bases. */
1667 static void
1668 check_bases (tree t,
1669 int* cant_have_const_ctor_p,
1670 int* no_const_asn_ref_p)
1672 int i;
1673 bool seen_non_virtual_nearly_empty_base_p = 0;
1674 int seen_tm_mask = 0;
1675 tree base_binfo;
1676 tree binfo;
1677 tree field = NULL_TREE;
1679 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1680 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1681 if (TREE_CODE (field) == FIELD_DECL)
1682 break;
1684 for (binfo = TYPE_BINFO (t), i = 0;
1685 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1687 tree basetype = TREE_TYPE (base_binfo);
1689 gcc_assert (COMPLETE_TYPE_P (basetype));
1691 if (CLASSTYPE_FINAL (basetype))
1692 error ("cannot derive from %<final%> base %qT in derived type %qT",
1693 basetype, t);
1695 /* If any base class is non-literal, so is the derived class. */
1696 if (!CLASSTYPE_LITERAL_P (basetype))
1697 CLASSTYPE_LITERAL_P (t) = false;
1699 /* If the base class doesn't have copy constructors or
1700 assignment operators that take const references, then the
1701 derived class cannot have such a member automatically
1702 generated. */
1703 if (TYPE_HAS_COPY_CTOR (basetype)
1704 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1705 *cant_have_const_ctor_p = 1;
1706 if (TYPE_HAS_COPY_ASSIGN (basetype)
1707 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1708 *no_const_asn_ref_p = 1;
1710 if (BINFO_VIRTUAL_P (base_binfo))
1711 /* A virtual base does not effect nearly emptiness. */
1713 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1715 if (seen_non_virtual_nearly_empty_base_p)
1716 /* And if there is more than one nearly empty base, then the
1717 derived class is not nearly empty either. */
1718 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1719 else
1720 /* Remember we've seen one. */
1721 seen_non_virtual_nearly_empty_base_p = 1;
1723 else if (!is_empty_class (basetype))
1724 /* If the base class is not empty or nearly empty, then this
1725 class cannot be nearly empty. */
1726 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1728 /* A lot of properties from the bases also apply to the derived
1729 class. */
1730 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1731 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1732 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1733 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1734 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1735 || !TYPE_HAS_COPY_ASSIGN (basetype));
1736 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1737 || !TYPE_HAS_COPY_CTOR (basetype));
1738 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1739 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1740 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1741 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1742 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1743 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1744 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1745 || TYPE_HAS_COMPLEX_DFLT (basetype));
1746 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1747 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1748 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1749 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1750 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1751 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1753 /* A standard-layout class is a class that:
1755 * has no non-standard-layout base classes, */
1756 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1757 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1759 tree basefield;
1760 /* ...has no base classes of the same type as the first non-static
1761 data member... */
1762 if (field && DECL_CONTEXT (field) == t
1763 && (same_type_ignoring_top_level_qualifiers_p
1764 (TREE_TYPE (field), basetype)))
1765 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1766 else
1767 /* ...either has no non-static data members in the most-derived
1768 class and at most one base class with non-static data
1769 members, or has no base classes with non-static data
1770 members */
1771 for (basefield = TYPE_FIELDS (basetype); basefield;
1772 basefield = DECL_CHAIN (basefield))
1773 if (TREE_CODE (basefield) == FIELD_DECL)
1775 if (field)
1776 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1777 else
1778 field = basefield;
1779 break;
1783 /* Don't bother collecting tm attributes if transactional memory
1784 support is not enabled. */
1785 if (flag_tm)
1787 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1788 if (tm_attr)
1789 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1792 check_abi_tags (t, basetype);
1795 /* If one of the base classes had TM attributes, and the current class
1796 doesn't define its own, then the current class inherits one. */
1797 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1799 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask);
1800 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1804 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1805 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1806 that have had a nearly-empty virtual primary base stolen by some
1807 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1808 T. */
1810 static void
1811 determine_primary_bases (tree t)
1813 unsigned i;
1814 tree primary = NULL_TREE;
1815 tree type_binfo = TYPE_BINFO (t);
1816 tree base_binfo;
1818 /* Determine the primary bases of our bases. */
1819 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1820 base_binfo = TREE_CHAIN (base_binfo))
1822 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1824 /* See if we're the non-virtual primary of our inheritance
1825 chain. */
1826 if (!BINFO_VIRTUAL_P (base_binfo))
1828 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1829 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1831 if (parent_primary
1832 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1833 BINFO_TYPE (parent_primary)))
1834 /* We are the primary binfo. */
1835 BINFO_PRIMARY_P (base_binfo) = 1;
1837 /* Determine if we have a virtual primary base, and mark it so.
1839 if (primary && BINFO_VIRTUAL_P (primary))
1841 tree this_primary = copied_binfo (primary, base_binfo);
1843 if (BINFO_PRIMARY_P (this_primary))
1844 /* Someone already claimed this base. */
1845 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1846 else
1848 tree delta;
1850 BINFO_PRIMARY_P (this_primary) = 1;
1851 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1853 /* A virtual binfo might have been copied from within
1854 another hierarchy. As we're about to use it as a
1855 primary base, make sure the offsets match. */
1856 delta = size_diffop_loc (input_location,
1857 convert (ssizetype,
1858 BINFO_OFFSET (base_binfo)),
1859 convert (ssizetype,
1860 BINFO_OFFSET (this_primary)));
1862 propagate_binfo_offsets (this_primary, delta);
1867 /* First look for a dynamic direct non-virtual base. */
1868 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1870 tree basetype = BINFO_TYPE (base_binfo);
1872 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1874 primary = base_binfo;
1875 goto found;
1879 /* A "nearly-empty" virtual base class can be the primary base
1880 class, if no non-virtual polymorphic base can be found. Look for
1881 a nearly-empty virtual dynamic base that is not already a primary
1882 base of something in the hierarchy. If there is no such base,
1883 just pick the first nearly-empty virtual base. */
1885 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1886 base_binfo = TREE_CHAIN (base_binfo))
1887 if (BINFO_VIRTUAL_P (base_binfo)
1888 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1890 if (!BINFO_PRIMARY_P (base_binfo))
1892 /* Found one that is not primary. */
1893 primary = base_binfo;
1894 goto found;
1896 else if (!primary)
1897 /* Remember the first candidate. */
1898 primary = base_binfo;
1901 found:
1902 /* If we've got a primary base, use it. */
1903 if (primary)
1905 tree basetype = BINFO_TYPE (primary);
1907 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1908 if (BINFO_PRIMARY_P (primary))
1909 /* We are stealing a primary base. */
1910 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1911 BINFO_PRIMARY_P (primary) = 1;
1912 if (BINFO_VIRTUAL_P (primary))
1914 tree delta;
1916 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1917 /* A virtual binfo might have been copied from within
1918 another hierarchy. As we're about to use it as a primary
1919 base, make sure the offsets match. */
1920 delta = size_diffop_loc (input_location, ssize_int (0),
1921 convert (ssizetype, BINFO_OFFSET (primary)));
1923 propagate_binfo_offsets (primary, delta);
1926 primary = TYPE_BINFO (basetype);
1928 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1929 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1930 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1934 /* Update the variant types of T. */
1936 void
1937 fixup_type_variants (tree t)
1939 tree variants;
1941 if (!t)
1942 return;
1944 for (variants = TYPE_NEXT_VARIANT (t);
1945 variants;
1946 variants = TYPE_NEXT_VARIANT (variants))
1948 /* These fields are in the _TYPE part of the node, not in
1949 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1950 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1951 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1952 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1953 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1955 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1957 TYPE_BINFO (variants) = TYPE_BINFO (t);
1959 /* Copy whatever these are holding today. */
1960 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1961 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1965 /* Early variant fixups: we apply attributes at the beginning of the class
1966 definition, and we need to fix up any variants that have already been
1967 made via elaborated-type-specifier so that check_qualified_type works. */
1969 void
1970 fixup_attribute_variants (tree t)
1972 tree variants;
1974 if (!t)
1975 return;
1977 tree attrs = TYPE_ATTRIBUTES (t);
1978 unsigned align = TYPE_ALIGN (t);
1979 bool user_align = TYPE_USER_ALIGN (t);
1981 for (variants = TYPE_NEXT_VARIANT (t);
1982 variants;
1983 variants = TYPE_NEXT_VARIANT (variants))
1985 /* These are the two fields that check_qualified_type looks at and
1986 are affected by attributes. */
1987 TYPE_ATTRIBUTES (variants) = attrs;
1988 unsigned valign = align;
1989 if (TYPE_USER_ALIGN (variants))
1990 valign = MAX (valign, TYPE_ALIGN (variants));
1991 else
1992 TYPE_USER_ALIGN (variants) = user_align;
1993 TYPE_ALIGN (variants) = valign;
1997 /* Set memoizing fields and bits of T (and its variants) for later
1998 use. */
2000 static void
2001 finish_struct_bits (tree t)
2003 /* Fix up variants (if any). */
2004 fixup_type_variants (t);
2006 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2007 /* For a class w/o baseclasses, 'finish_struct' has set
2008 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2009 Similarly for a class whose base classes do not have vtables.
2010 When neither of these is true, we might have removed abstract
2011 virtuals (by providing a definition), added some (by declaring
2012 new ones), or redeclared ones from a base class. We need to
2013 recalculate what's really an abstract virtual at this point (by
2014 looking in the vtables). */
2015 get_pure_virtuals (t);
2017 /* If this type has a copy constructor or a destructor, force its
2018 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2019 nonzero. This will cause it to be passed by invisible reference
2020 and prevent it from being returned in a register. */
2021 if (type_has_nontrivial_copy_init (t)
2022 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2024 tree variants;
2025 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2026 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2028 SET_TYPE_MODE (variants, BLKmode);
2029 TREE_ADDRESSABLE (variants) = 1;
2034 /* Issue warnings about T having private constructors, but no friends,
2035 and so forth.
2037 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2038 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2039 non-private static member functions. */
2041 static void
2042 maybe_warn_about_overly_private_class (tree t)
2044 int has_member_fn = 0;
2045 int has_nonprivate_method = 0;
2046 tree fn;
2048 if (!warn_ctor_dtor_privacy
2049 /* If the class has friends, those entities might create and
2050 access instances, so we should not warn. */
2051 || (CLASSTYPE_FRIEND_CLASSES (t)
2052 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2053 /* We will have warned when the template was declared; there's
2054 no need to warn on every instantiation. */
2055 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2056 /* There's no reason to even consider warning about this
2057 class. */
2058 return;
2060 /* We only issue one warning, if more than one applies, because
2061 otherwise, on code like:
2063 class A {
2064 // Oops - forgot `public:'
2065 A();
2066 A(const A&);
2067 ~A();
2070 we warn several times about essentially the same problem. */
2072 /* Check to see if all (non-constructor, non-destructor) member
2073 functions are private. (Since there are no friends or
2074 non-private statics, we can't ever call any of the private member
2075 functions.) */
2076 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2077 /* We're not interested in compiler-generated methods; they don't
2078 provide any way to call private members. */
2079 if (!DECL_ARTIFICIAL (fn))
2081 if (!TREE_PRIVATE (fn))
2083 if (DECL_STATIC_FUNCTION_P (fn))
2084 /* A non-private static member function is just like a
2085 friend; it can create and invoke private member
2086 functions, and be accessed without a class
2087 instance. */
2088 return;
2090 has_nonprivate_method = 1;
2091 /* Keep searching for a static member function. */
2093 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2094 has_member_fn = 1;
2097 if (!has_nonprivate_method && has_member_fn)
2099 /* There are no non-private methods, and there's at least one
2100 private member function that isn't a constructor or
2101 destructor. (If all the private members are
2102 constructors/destructors we want to use the code below that
2103 issues error messages specifically referring to
2104 constructors/destructors.) */
2105 unsigned i;
2106 tree binfo = TYPE_BINFO (t);
2108 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2109 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2111 has_nonprivate_method = 1;
2112 break;
2114 if (!has_nonprivate_method)
2116 warning (OPT_Wctor_dtor_privacy,
2117 "all member functions in class %qT are private", t);
2118 return;
2122 /* Even if some of the member functions are non-private, the class
2123 won't be useful for much if all the constructors or destructors
2124 are private: such an object can never be created or destroyed. */
2125 fn = CLASSTYPE_DESTRUCTORS (t);
2126 if (fn && TREE_PRIVATE (fn))
2128 warning (OPT_Wctor_dtor_privacy,
2129 "%q#T only defines a private destructor and has no friends",
2131 return;
2134 /* Warn about classes that have private constructors and no friends. */
2135 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2136 /* Implicitly generated constructors are always public. */
2137 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2138 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2140 int nonprivate_ctor = 0;
2142 /* If a non-template class does not define a copy
2143 constructor, one is defined for it, enabling it to avoid
2144 this warning. For a template class, this does not
2145 happen, and so we would normally get a warning on:
2147 template <class T> class C { private: C(); };
2149 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2150 complete non-template or fully instantiated classes have this
2151 flag set. */
2152 if (!TYPE_HAS_COPY_CTOR (t))
2153 nonprivate_ctor = 1;
2154 else
2155 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2157 tree ctor = OVL_CURRENT (fn);
2158 /* Ideally, we wouldn't count copy constructors (or, in
2159 fact, any constructor that takes an argument of the
2160 class type as a parameter) because such things cannot
2161 be used to construct an instance of the class unless
2162 you already have one. But, for now at least, we're
2163 more generous. */
2164 if (! TREE_PRIVATE (ctor))
2166 nonprivate_ctor = 1;
2167 break;
2171 if (nonprivate_ctor == 0)
2173 warning (OPT_Wctor_dtor_privacy,
2174 "%q#T only defines private constructors and has no friends",
2176 return;
2181 static struct {
2182 gt_pointer_operator new_value;
2183 void *cookie;
2184 } resort_data;
2186 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2188 static int
2189 method_name_cmp (const void* m1_p, const void* m2_p)
2191 const tree *const m1 = (const tree *) m1_p;
2192 const tree *const m2 = (const tree *) m2_p;
2194 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2195 return 0;
2196 if (*m1 == NULL_TREE)
2197 return -1;
2198 if (*m2 == NULL_TREE)
2199 return 1;
2200 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2201 return -1;
2202 return 1;
2205 /* This routine compares two fields like method_name_cmp but using the
2206 pointer operator in resort_field_decl_data. */
2208 static int
2209 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2211 const tree *const m1 = (const tree *) m1_p;
2212 const tree *const m2 = (const tree *) m2_p;
2213 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2214 return 0;
2215 if (*m1 == NULL_TREE)
2216 return -1;
2217 if (*m2 == NULL_TREE)
2218 return 1;
2220 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2221 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2222 resort_data.new_value (&d1, resort_data.cookie);
2223 resort_data.new_value (&d2, resort_data.cookie);
2224 if (d1 < d2)
2225 return -1;
2227 return 1;
2230 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2232 void
2233 resort_type_method_vec (void* obj,
2234 void* /*orig_obj*/,
2235 gt_pointer_operator new_value,
2236 void* cookie)
2238 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2239 int len = vec_safe_length (method_vec);
2240 size_t slot;
2241 tree fn;
2243 /* The type conversion ops have to live at the front of the vec, so we
2244 can't sort them. */
2245 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2246 vec_safe_iterate (method_vec, slot, &fn);
2247 ++slot)
2248 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2249 break;
2251 if (len - slot > 1)
2253 resort_data.new_value = new_value;
2254 resort_data.cookie = cookie;
2255 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2256 resort_method_name_cmp);
2260 /* Warn about duplicate methods in fn_fields.
2262 Sort methods that are not special (i.e., constructors, destructors,
2263 and type conversion operators) so that we can find them faster in
2264 search. */
2266 static void
2267 finish_struct_methods (tree t)
2269 tree fn_fields;
2270 vec<tree, va_gc> *method_vec;
2271 int slot, len;
2273 method_vec = CLASSTYPE_METHOD_VEC (t);
2274 if (!method_vec)
2275 return;
2277 len = method_vec->length ();
2279 /* Clear DECL_IN_AGGR_P for all functions. */
2280 for (fn_fields = TYPE_METHODS (t); fn_fields;
2281 fn_fields = DECL_CHAIN (fn_fields))
2282 DECL_IN_AGGR_P (fn_fields) = 0;
2284 /* Issue warnings about private constructors and such. If there are
2285 no methods, then some public defaults are generated. */
2286 maybe_warn_about_overly_private_class (t);
2288 /* The type conversion ops have to live at the front of the vec, so we
2289 can't sort them. */
2290 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2291 method_vec->iterate (slot, &fn_fields);
2292 ++slot)
2293 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2294 break;
2295 if (len - slot > 1)
2296 qsort (method_vec->address () + slot,
2297 len-slot, sizeof (tree), method_name_cmp);
2300 /* Make BINFO's vtable have N entries, including RTTI entries,
2301 vbase and vcall offsets, etc. Set its type and call the back end
2302 to lay it out. */
2304 static void
2305 layout_vtable_decl (tree binfo, int n)
2307 tree atype;
2308 tree vtable;
2310 atype = build_array_of_n_type (vtable_entry_type, n);
2311 layout_type (atype);
2313 /* We may have to grow the vtable. */
2314 vtable = get_vtbl_decl_for_binfo (binfo);
2315 if (!same_type_p (TREE_TYPE (vtable), atype))
2317 TREE_TYPE (vtable) = atype;
2318 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2319 layout_decl (vtable, 0);
2323 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2324 have the same signature. */
2327 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2329 /* One destructor overrides another if they are the same kind of
2330 destructor. */
2331 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2332 && special_function_p (base_fndecl) == special_function_p (fndecl))
2333 return 1;
2334 /* But a non-destructor never overrides a destructor, nor vice
2335 versa, nor do different kinds of destructors override
2336 one-another. For example, a complete object destructor does not
2337 override a deleting destructor. */
2338 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2339 return 0;
2341 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2342 || (DECL_CONV_FN_P (fndecl)
2343 && DECL_CONV_FN_P (base_fndecl)
2344 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2345 DECL_CONV_FN_TYPE (base_fndecl))))
2347 tree fntype = TREE_TYPE (fndecl);
2348 tree base_fntype = TREE_TYPE (base_fndecl);
2349 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2350 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2351 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2352 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2353 return 1;
2355 return 0;
2358 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2359 subobject. */
2361 static bool
2362 base_derived_from (tree derived, tree base)
2364 tree probe;
2366 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2368 if (probe == derived)
2369 return true;
2370 else if (BINFO_VIRTUAL_P (probe))
2371 /* If we meet a virtual base, we can't follow the inheritance
2372 any more. See if the complete type of DERIVED contains
2373 such a virtual base. */
2374 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2375 != NULL_TREE);
2377 return false;
2380 struct find_final_overrider_data {
2381 /* The function for which we are trying to find a final overrider. */
2382 tree fn;
2383 /* The base class in which the function was declared. */
2384 tree declaring_base;
2385 /* The candidate overriders. */
2386 tree candidates;
2387 /* Path to most derived. */
2388 vec<tree> path;
2391 /* Add the overrider along the current path to FFOD->CANDIDATES.
2392 Returns true if an overrider was found; false otherwise. */
2394 static bool
2395 dfs_find_final_overrider_1 (tree binfo,
2396 find_final_overrider_data *ffod,
2397 unsigned depth)
2399 tree method;
2401 /* If BINFO is not the most derived type, try a more derived class.
2402 A definition there will overrider a definition here. */
2403 if (depth)
2405 depth--;
2406 if (dfs_find_final_overrider_1
2407 (ffod->path[depth], ffod, depth))
2408 return true;
2411 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2412 if (method)
2414 tree *candidate = &ffod->candidates;
2416 /* Remove any candidates overridden by this new function. */
2417 while (*candidate)
2419 /* If *CANDIDATE overrides METHOD, then METHOD
2420 cannot override anything else on the list. */
2421 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2422 return true;
2423 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2424 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2425 *candidate = TREE_CHAIN (*candidate);
2426 else
2427 candidate = &TREE_CHAIN (*candidate);
2430 /* Add the new function. */
2431 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2432 return true;
2435 return false;
2438 /* Called from find_final_overrider via dfs_walk. */
2440 static tree
2441 dfs_find_final_overrider_pre (tree binfo, void *data)
2443 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2445 if (binfo == ffod->declaring_base)
2446 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2447 ffod->path.safe_push (binfo);
2449 return NULL_TREE;
2452 static tree
2453 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2455 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2456 ffod->path.pop ();
2458 return NULL_TREE;
2461 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2462 FN and whose TREE_VALUE is the binfo for the base where the
2463 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2464 DERIVED) is the base object in which FN is declared. */
2466 static tree
2467 find_final_overrider (tree derived, tree binfo, tree fn)
2469 find_final_overrider_data ffod;
2471 /* Getting this right is a little tricky. This is valid:
2473 struct S { virtual void f (); };
2474 struct T { virtual void f (); };
2475 struct U : public S, public T { };
2477 even though calling `f' in `U' is ambiguous. But,
2479 struct R { virtual void f(); };
2480 struct S : virtual public R { virtual void f (); };
2481 struct T : virtual public R { virtual void f (); };
2482 struct U : public S, public T { };
2484 is not -- there's no way to decide whether to put `S::f' or
2485 `T::f' in the vtable for `R'.
2487 The solution is to look at all paths to BINFO. If we find
2488 different overriders along any two, then there is a problem. */
2489 if (DECL_THUNK_P (fn))
2490 fn = THUNK_TARGET (fn);
2492 /* Determine the depth of the hierarchy. */
2493 ffod.fn = fn;
2494 ffod.declaring_base = binfo;
2495 ffod.candidates = NULL_TREE;
2496 ffod.path.create (30);
2498 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2499 dfs_find_final_overrider_post, &ffod);
2501 ffod.path.release ();
2503 /* If there was no winner, issue an error message. */
2504 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2505 return error_mark_node;
2507 return ffod.candidates;
2510 /* Return the index of the vcall offset for FN when TYPE is used as a
2511 virtual base. */
2513 static tree
2514 get_vcall_index (tree fn, tree type)
2516 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2517 tree_pair_p p;
2518 unsigned ix;
2520 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2521 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2522 || same_signature_p (fn, p->purpose))
2523 return p->value;
2525 /* There should always be an appropriate index. */
2526 gcc_unreachable ();
2529 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2530 dominated by T. FN is the old function; VIRTUALS points to the
2531 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2532 of that entry in the list. */
2534 static void
2535 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2536 unsigned ix)
2538 tree b;
2539 tree overrider;
2540 tree delta;
2541 tree virtual_base;
2542 tree first_defn;
2543 tree overrider_fn, overrider_target;
2544 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2545 tree over_return, base_return;
2546 bool lost = false;
2548 /* Find the nearest primary base (possibly binfo itself) which defines
2549 this function; this is the class the caller will convert to when
2550 calling FN through BINFO. */
2551 for (b = binfo; ; b = get_primary_binfo (b))
2553 gcc_assert (b);
2554 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2555 break;
2557 /* The nearest definition is from a lost primary. */
2558 if (BINFO_LOST_PRIMARY_P (b))
2559 lost = true;
2561 first_defn = b;
2563 /* Find the final overrider. */
2564 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2565 if (overrider == error_mark_node)
2567 error ("no unique final overrider for %qD in %qT", target_fn, t);
2568 return;
2570 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2572 /* Check for adjusting covariant return types. */
2573 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2574 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2576 if (POINTER_TYPE_P (over_return)
2577 && TREE_CODE (over_return) == TREE_CODE (base_return)
2578 && CLASS_TYPE_P (TREE_TYPE (over_return))
2579 && CLASS_TYPE_P (TREE_TYPE (base_return))
2580 /* If the overrider is invalid, don't even try. */
2581 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2583 /* If FN is a covariant thunk, we must figure out the adjustment
2584 to the final base FN was converting to. As OVERRIDER_TARGET might
2585 also be converting to the return type of FN, we have to
2586 combine the two conversions here. */
2587 tree fixed_offset, virtual_offset;
2589 over_return = TREE_TYPE (over_return);
2590 base_return = TREE_TYPE (base_return);
2592 if (DECL_THUNK_P (fn))
2594 gcc_assert (DECL_RESULT_THUNK_P (fn));
2595 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2596 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2598 else
2599 fixed_offset = virtual_offset = NULL_TREE;
2601 if (virtual_offset)
2602 /* Find the equivalent binfo within the return type of the
2603 overriding function. We will want the vbase offset from
2604 there. */
2605 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2606 over_return);
2607 else if (!same_type_ignoring_top_level_qualifiers_p
2608 (over_return, base_return))
2610 /* There was no existing virtual thunk (which takes
2611 precedence). So find the binfo of the base function's
2612 return type within the overriding function's return type.
2613 We cannot call lookup base here, because we're inside a
2614 dfs_walk, and will therefore clobber the BINFO_MARKED
2615 flags. Fortunately we know the covariancy is valid (it
2616 has already been checked), so we can just iterate along
2617 the binfos, which have been chained in inheritance graph
2618 order. Of course it is lame that we have to repeat the
2619 search here anyway -- we should really be caching pieces
2620 of the vtable and avoiding this repeated work. */
2621 tree thunk_binfo, base_binfo;
2623 /* Find the base binfo within the overriding function's
2624 return type. We will always find a thunk_binfo, except
2625 when the covariancy is invalid (which we will have
2626 already diagnosed). */
2627 for (base_binfo = TYPE_BINFO (base_return),
2628 thunk_binfo = TYPE_BINFO (over_return);
2629 thunk_binfo;
2630 thunk_binfo = TREE_CHAIN (thunk_binfo))
2631 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2632 BINFO_TYPE (base_binfo)))
2633 break;
2635 /* See if virtual inheritance is involved. */
2636 for (virtual_offset = thunk_binfo;
2637 virtual_offset;
2638 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2639 if (BINFO_VIRTUAL_P (virtual_offset))
2640 break;
2642 if (virtual_offset
2643 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2645 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2647 if (virtual_offset)
2649 /* We convert via virtual base. Adjust the fixed
2650 offset to be from there. */
2651 offset =
2652 size_diffop (offset,
2653 convert (ssizetype,
2654 BINFO_OFFSET (virtual_offset)));
2656 if (fixed_offset)
2657 /* There was an existing fixed offset, this must be
2658 from the base just converted to, and the base the
2659 FN was thunking to. */
2660 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2661 else
2662 fixed_offset = offset;
2666 if (fixed_offset || virtual_offset)
2667 /* Replace the overriding function with a covariant thunk. We
2668 will emit the overriding function in its own slot as
2669 well. */
2670 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2671 fixed_offset, virtual_offset);
2673 else
2674 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2675 !DECL_THUNK_P (fn));
2677 /* If we need a covariant thunk, then we may need to adjust first_defn.
2678 The ABI specifies that the thunks emitted with a function are
2679 determined by which bases the function overrides, so we need to be
2680 sure that we're using a thunk for some overridden base; even if we
2681 know that the necessary this adjustment is zero, there may not be an
2682 appropriate zero-this-adjusment thunk for us to use since thunks for
2683 overriding virtual bases always use the vcall offset.
2685 Furthermore, just choosing any base that overrides this function isn't
2686 quite right, as this slot won't be used for calls through a type that
2687 puts a covariant thunk here. Calling the function through such a type
2688 will use a different slot, and that slot is the one that determines
2689 the thunk emitted for that base.
2691 So, keep looking until we find the base that we're really overriding
2692 in this slot: the nearest primary base that doesn't use a covariant
2693 thunk in this slot. */
2694 if (overrider_target != overrider_fn)
2696 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2697 /* We already know that the overrider needs a covariant thunk. */
2698 b = get_primary_binfo (b);
2699 for (; ; b = get_primary_binfo (b))
2701 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2702 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2703 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2704 break;
2705 if (BINFO_LOST_PRIMARY_P (b))
2706 lost = true;
2708 first_defn = b;
2711 /* Assume that we will produce a thunk that convert all the way to
2712 the final overrider, and not to an intermediate virtual base. */
2713 virtual_base = NULL_TREE;
2715 /* See if we can convert to an intermediate virtual base first, and then
2716 use the vcall offset located there to finish the conversion. */
2717 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2719 /* If we find the final overrider, then we can stop
2720 walking. */
2721 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2722 BINFO_TYPE (TREE_VALUE (overrider))))
2723 break;
2725 /* If we find a virtual base, and we haven't yet found the
2726 overrider, then there is a virtual base between the
2727 declaring base (first_defn) and the final overrider. */
2728 if (BINFO_VIRTUAL_P (b))
2730 virtual_base = b;
2731 break;
2735 /* Compute the constant adjustment to the `this' pointer. The
2736 `this' pointer, when this function is called, will point at BINFO
2737 (or one of its primary bases, which are at the same offset). */
2738 if (virtual_base)
2739 /* The `this' pointer needs to be adjusted from the declaration to
2740 the nearest virtual base. */
2741 delta = size_diffop_loc (input_location,
2742 convert (ssizetype, BINFO_OFFSET (virtual_base)),
2743 convert (ssizetype, BINFO_OFFSET (first_defn)));
2744 else if (lost)
2745 /* If the nearest definition is in a lost primary, we don't need an
2746 entry in our vtable. Except possibly in a constructor vtable,
2747 if we happen to get our primary back. In that case, the offset
2748 will be zero, as it will be a primary base. */
2749 delta = size_zero_node;
2750 else
2751 /* The `this' pointer needs to be adjusted from pointing to
2752 BINFO to pointing at the base where the final overrider
2753 appears. */
2754 delta = size_diffop_loc (input_location,
2755 convert (ssizetype,
2756 BINFO_OFFSET (TREE_VALUE (overrider))),
2757 convert (ssizetype, BINFO_OFFSET (binfo)));
2759 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2761 if (virtual_base)
2762 BV_VCALL_INDEX (*virtuals)
2763 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2764 else
2765 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2767 BV_LOST_PRIMARY (*virtuals) = lost;
2770 /* Called from modify_all_vtables via dfs_walk. */
2772 static tree
2773 dfs_modify_vtables (tree binfo, void* data)
2775 tree t = (tree) data;
2776 tree virtuals;
2777 tree old_virtuals;
2778 unsigned ix;
2780 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2781 /* A base without a vtable needs no modification, and its bases
2782 are uninteresting. */
2783 return dfs_skip_bases;
2785 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2786 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2787 /* Don't do the primary vtable, if it's new. */
2788 return NULL_TREE;
2790 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2791 /* There's no need to modify the vtable for a non-virtual primary
2792 base; we're not going to use that vtable anyhow. We do still
2793 need to do this for virtual primary bases, as they could become
2794 non-primary in a construction vtable. */
2795 return NULL_TREE;
2797 make_new_vtable (t, binfo);
2799 /* Now, go through each of the virtual functions in the virtual
2800 function table for BINFO. Find the final overrider, and update
2801 the BINFO_VIRTUALS list appropriately. */
2802 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2803 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2804 virtuals;
2805 ix++, virtuals = TREE_CHAIN (virtuals),
2806 old_virtuals = TREE_CHAIN (old_virtuals))
2807 update_vtable_entry_for_fn (t,
2808 binfo,
2809 BV_FN (old_virtuals),
2810 &virtuals, ix);
2812 return NULL_TREE;
2815 /* Update all of the primary and secondary vtables for T. Create new
2816 vtables as required, and initialize their RTTI information. Each
2817 of the functions in VIRTUALS is declared in T and may override a
2818 virtual function from a base class; find and modify the appropriate
2819 entries to point to the overriding functions. Returns a list, in
2820 declaration order, of the virtual functions that are declared in T,
2821 but do not appear in the primary base class vtable, and which
2822 should therefore be appended to the end of the vtable for T. */
2824 static tree
2825 modify_all_vtables (tree t, tree virtuals)
2827 tree binfo = TYPE_BINFO (t);
2828 tree *fnsp;
2830 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2831 if (TYPE_CONTAINS_VPTR_P (t))
2832 get_vtable_decl (t, false);
2834 /* Update all of the vtables. */
2835 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2837 /* Add virtual functions not already in our primary vtable. These
2838 will be both those introduced by this class, and those overridden
2839 from secondary bases. It does not include virtuals merely
2840 inherited from secondary bases. */
2841 for (fnsp = &virtuals; *fnsp; )
2843 tree fn = TREE_VALUE (*fnsp);
2845 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2846 || DECL_VINDEX (fn) == error_mark_node)
2848 /* We don't need to adjust the `this' pointer when
2849 calling this function. */
2850 BV_DELTA (*fnsp) = integer_zero_node;
2851 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2853 /* This is a function not already in our vtable. Keep it. */
2854 fnsp = &TREE_CHAIN (*fnsp);
2856 else
2857 /* We've already got an entry for this function. Skip it. */
2858 *fnsp = TREE_CHAIN (*fnsp);
2861 return virtuals;
2864 /* Get the base virtual function declarations in T that have the
2865 indicated NAME. */
2867 static void
2868 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2870 tree methods;
2871 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2872 int i;
2874 /* Find virtual functions in T with the indicated NAME. */
2875 i = lookup_fnfields_1 (t, name);
2876 bool found_decls = false;
2877 if (i != -1)
2878 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2879 methods;
2880 methods = OVL_NEXT (methods))
2882 tree method = OVL_CURRENT (methods);
2884 if (TREE_CODE (method) == FUNCTION_DECL
2885 && DECL_VINDEX (method))
2887 base_fndecls->safe_push (method);
2888 found_decls = true;
2892 if (found_decls)
2893 return;
2895 for (i = 0; i < n_baseclasses; i++)
2897 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2898 get_basefndecls (name, basetype, base_fndecls);
2902 /* If this declaration supersedes the declaration of
2903 a method declared virtual in the base class, then
2904 mark this field as being virtual as well. */
2906 void
2907 check_for_override (tree decl, tree ctype)
2909 bool overrides_found = false;
2910 if (TREE_CODE (decl) == TEMPLATE_DECL)
2911 /* In [temp.mem] we have:
2913 A specialization of a member function template does not
2914 override a virtual function from a base class. */
2915 return;
2916 if ((DECL_DESTRUCTOR_P (decl)
2917 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2918 || DECL_CONV_FN_P (decl))
2919 && look_for_overrides (ctype, decl)
2920 && !DECL_STATIC_FUNCTION_P (decl))
2921 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2922 the error_mark_node so that we know it is an overriding
2923 function. */
2925 DECL_VINDEX (decl) = decl;
2926 overrides_found = true;
2927 if (warn_override && !DECL_OVERRIDE_P (decl)
2928 && !DECL_DESTRUCTOR_P (decl))
2929 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2930 "%qD can be marked override", decl);
2933 if (DECL_VIRTUAL_P (decl))
2935 if (!DECL_VINDEX (decl))
2936 DECL_VINDEX (decl) = error_mark_node;
2937 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2938 if (DECL_DESTRUCTOR_P (decl))
2939 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2941 else if (DECL_FINAL_P (decl))
2942 error ("%q+#D marked %<final%>, but is not virtual", decl);
2943 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2944 error ("%q+#D marked %<override%>, but does not override", decl);
2947 /* Warn about hidden virtual functions that are not overridden in t.
2948 We know that constructors and destructors don't apply. */
2950 static void
2951 warn_hidden (tree t)
2953 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
2954 tree fns;
2955 size_t i;
2957 /* We go through each separately named virtual function. */
2958 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
2959 vec_safe_iterate (method_vec, i, &fns);
2960 ++i)
2962 tree fn;
2963 tree name;
2964 tree fndecl;
2965 tree base_binfo;
2966 tree binfo;
2967 int j;
2969 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2970 have the same name. Figure out what name that is. */
2971 name = DECL_NAME (OVL_CURRENT (fns));
2972 /* There are no possibly hidden functions yet. */
2973 auto_vec<tree, 20> base_fndecls;
2974 /* Iterate through all of the base classes looking for possibly
2975 hidden functions. */
2976 for (binfo = TYPE_BINFO (t), j = 0;
2977 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2979 tree basetype = BINFO_TYPE (base_binfo);
2980 get_basefndecls (name, basetype, &base_fndecls);
2983 /* If there are no functions to hide, continue. */
2984 if (base_fndecls.is_empty ())
2985 continue;
2987 /* Remove any overridden functions. */
2988 for (fn = fns; fn; fn = OVL_NEXT (fn))
2990 fndecl = OVL_CURRENT (fn);
2991 if (TREE_CODE (fndecl) == FUNCTION_DECL
2992 && DECL_VINDEX (fndecl))
2994 /* If the method from the base class has the same
2995 signature as the method from the derived class, it
2996 has been overridden. */
2997 for (size_t k = 0; k < base_fndecls.length (); k++)
2998 if (base_fndecls[k]
2999 && same_signature_p (fndecl, base_fndecls[k]))
3000 base_fndecls[k] = NULL_TREE;
3004 /* Now give a warning for all base functions without overriders,
3005 as they are hidden. */
3006 size_t k;
3007 tree base_fndecl;
3008 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3009 if (base_fndecl)
3011 /* Here we know it is a hider, and no overrider exists. */
3012 warning_at (location_of (base_fndecl),
3013 OPT_Woverloaded_virtual,
3014 "%qD was hidden", base_fndecl);
3015 warning_at (location_of (fns),
3016 OPT_Woverloaded_virtual, " by %qD", fns);
3021 /* Recursive helper for finish_struct_anon. */
3023 static void
3024 finish_struct_anon_r (tree field, bool complain)
3026 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3027 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3028 for (; elt; elt = DECL_CHAIN (elt))
3030 /* We're generally only interested in entities the user
3031 declared, but we also find nested classes by noticing
3032 the TYPE_DECL that we create implicitly. You're
3033 allowed to put one anonymous union inside another,
3034 though, so we explicitly tolerate that. We use
3035 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
3036 we also allow unnamed types used for defining fields. */
3037 if (DECL_ARTIFICIAL (elt)
3038 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3039 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
3040 continue;
3042 if (TREE_CODE (elt) != FIELD_DECL)
3044 /* We already complained about static data members in
3045 finish_static_data_member_decl. */
3046 if (complain && !VAR_P (elt))
3048 if (is_union)
3049 permerror (DECL_SOURCE_LOCATION (elt),
3050 "%q#D invalid; an anonymous union can "
3051 "only have non-static data members", elt);
3052 else
3053 permerror (DECL_SOURCE_LOCATION (elt),
3054 "%q#D invalid; an anonymous struct can "
3055 "only have non-static data members", elt);
3057 continue;
3060 if (complain)
3062 if (TREE_PRIVATE (elt))
3064 if (is_union)
3065 permerror (DECL_SOURCE_LOCATION (elt),
3066 "private member %q#D in anonymous union", elt);
3067 else
3068 permerror (DECL_SOURCE_LOCATION (elt),
3069 "private member %q#D in anonymous struct", elt);
3071 else if (TREE_PROTECTED (elt))
3073 if (is_union)
3074 permerror (DECL_SOURCE_LOCATION (elt),
3075 "protected member %q#D in anonymous union", elt);
3076 else
3077 permerror (DECL_SOURCE_LOCATION (elt),
3078 "protected member %q#D in anonymous struct", elt);
3082 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3083 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3085 /* Recurse into the anonymous aggregates to handle correctly
3086 access control (c++/24926):
3088 class A {
3089 union {
3090 union {
3091 int i;
3096 int j=A().i; */
3097 if (DECL_NAME (elt) == NULL_TREE
3098 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3099 finish_struct_anon_r (elt, /*complain=*/false);
3103 /* Check for things that are invalid. There are probably plenty of other
3104 things we should check for also. */
3106 static void
3107 finish_struct_anon (tree t)
3109 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3111 if (TREE_STATIC (field))
3112 continue;
3113 if (TREE_CODE (field) != FIELD_DECL)
3114 continue;
3116 if (DECL_NAME (field) == NULL_TREE
3117 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3118 finish_struct_anon_r (field, /*complain=*/true);
3122 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3123 will be used later during class template instantiation.
3124 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3125 a non-static member data (FIELD_DECL), a member function
3126 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3127 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3128 When FRIEND_P is nonzero, T is either a friend class
3129 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3130 (FUNCTION_DECL, TEMPLATE_DECL). */
3132 void
3133 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3135 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3136 if (CLASSTYPE_TEMPLATE_INFO (type))
3137 CLASSTYPE_DECL_LIST (type)
3138 = tree_cons (friend_p ? NULL_TREE : type,
3139 t, CLASSTYPE_DECL_LIST (type));
3142 /* This function is called from declare_virt_assop_and_dtor via
3143 dfs_walk_all.
3145 DATA is a type that direcly or indirectly inherits the base
3146 represented by BINFO. If BINFO contains a virtual assignment [copy
3147 assignment or move assigment] operator or a virtual constructor,
3148 declare that function in DATA if it hasn't been already declared. */
3150 static tree
3151 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3153 tree bv, fn, t = (tree)data;
3154 tree opname = ansi_assopname (NOP_EXPR);
3156 gcc_assert (t && CLASS_TYPE_P (t));
3157 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3159 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3160 /* A base without a vtable needs no modification, and its bases
3161 are uninteresting. */
3162 return dfs_skip_bases;
3164 if (BINFO_PRIMARY_P (binfo))
3165 /* If this is a primary base, then we have already looked at the
3166 virtual functions of its vtable. */
3167 return NULL_TREE;
3169 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3171 fn = BV_FN (bv);
3173 if (DECL_NAME (fn) == opname)
3175 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3176 lazily_declare_fn (sfk_copy_assignment, t);
3177 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3178 lazily_declare_fn (sfk_move_assignment, t);
3180 else if (DECL_DESTRUCTOR_P (fn)
3181 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3182 lazily_declare_fn (sfk_destructor, t);
3185 return NULL_TREE;
3188 /* If the class type T has a direct or indirect base that contains a
3189 virtual assignment operator or a virtual destructor, declare that
3190 function in T if it hasn't been already declared. */
3192 static void
3193 declare_virt_assop_and_dtor (tree t)
3195 if (!(TYPE_POLYMORPHIC_P (t)
3196 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3197 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3198 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3199 return;
3201 dfs_walk_all (TYPE_BINFO (t),
3202 dfs_declare_virt_assop_and_dtor,
3203 NULL, t);
3206 /* Declare the inheriting constructor for class T inherited from base
3207 constructor CTOR with the parameter array PARMS of size NPARMS. */
3209 static void
3210 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3212 /* We don't declare an inheriting ctor that would be a default,
3213 copy or move ctor for derived or base. */
3214 if (nparms == 0)
3215 return;
3216 if (nparms == 1
3217 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3219 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3220 if (parm == t || parm == DECL_CONTEXT (ctor))
3221 return;
3224 tree parmlist = void_list_node;
3225 for (int i = nparms - 1; i >= 0; i--)
3226 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3227 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3228 t, false, ctor, parmlist);
3229 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3230 if (add_method (t, fn, NULL_TREE))
3232 DECL_CHAIN (fn) = TYPE_METHODS (t);
3233 TYPE_METHODS (t) = fn;
3237 /* Declare all the inheriting constructors for class T inherited from base
3238 constructor CTOR. */
3240 static void
3241 one_inherited_ctor (tree ctor, tree t)
3243 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3245 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3246 int i = 0;
3247 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3249 if (TREE_PURPOSE (parms))
3250 one_inheriting_sig (t, ctor, new_parms, i);
3251 new_parms[i++] = TREE_VALUE (parms);
3253 one_inheriting_sig (t, ctor, new_parms, i);
3254 if (parms == NULL_TREE)
3256 if (warning (OPT_Winherited_variadic_ctor,
3257 "the ellipsis in %qD is not inherited", ctor))
3258 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3262 /* Create default constructors, assignment operators, and so forth for
3263 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3264 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3265 the class cannot have a default constructor, copy constructor
3266 taking a const reference argument, or an assignment operator taking
3267 a const reference, respectively. */
3269 static void
3270 add_implicitly_declared_members (tree t, tree* access_decls,
3271 int cant_have_const_cctor,
3272 int cant_have_const_assignment)
3274 bool move_ok = false;
3276 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3277 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3278 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3279 move_ok = true;
3281 /* Destructor. */
3282 if (!CLASSTYPE_DESTRUCTORS (t))
3284 /* In general, we create destructors lazily. */
3285 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3287 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3288 && TYPE_FOR_JAVA (t))
3289 /* But if this is a Java class, any non-trivial destructor is
3290 invalid, even if compiler-generated. Therefore, if the
3291 destructor is non-trivial we create it now. */
3292 lazily_declare_fn (sfk_destructor, t);
3295 /* [class.ctor]
3297 If there is no user-declared constructor for a class, a default
3298 constructor is implicitly declared. */
3299 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3301 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3302 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3303 if (cxx_dialect >= cxx11)
3304 TYPE_HAS_CONSTEXPR_CTOR (t)
3305 /* This might force the declaration. */
3306 = type_has_constexpr_default_constructor (t);
3309 /* [class.ctor]
3311 If a class definition does not explicitly declare a copy
3312 constructor, one is declared implicitly. */
3313 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t))
3315 TYPE_HAS_COPY_CTOR (t) = 1;
3316 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3317 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3318 if (move_ok)
3319 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3322 /* If there is no assignment operator, one will be created if and
3323 when it is needed. For now, just record whether or not the type
3324 of the parameter to the assignment operator will be a const or
3325 non-const reference. */
3326 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t))
3328 TYPE_HAS_COPY_ASSIGN (t) = 1;
3329 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3330 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3331 if (move_ok && !LAMBDA_TYPE_P (t))
3332 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3335 /* We can't be lazy about declaring functions that might override
3336 a virtual function from a base class. */
3337 declare_virt_assop_and_dtor (t);
3339 while (*access_decls)
3341 tree using_decl = TREE_VALUE (*access_decls);
3342 tree decl = USING_DECL_DECLS (using_decl);
3343 if (DECL_NAME (using_decl) == ctor_identifier)
3345 /* declare, then remove the decl */
3346 tree ctor_list = decl;
3347 location_t loc = input_location;
3348 input_location = DECL_SOURCE_LOCATION (using_decl);
3349 if (ctor_list)
3350 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3351 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3352 *access_decls = TREE_CHAIN (*access_decls);
3353 input_location = loc;
3355 else
3356 access_decls = &TREE_CHAIN (*access_decls);
3360 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3361 count the number of fields in TYPE, including anonymous union
3362 members. */
3364 static int
3365 count_fields (tree fields)
3367 tree x;
3368 int n_fields = 0;
3369 for (x = fields; x; x = DECL_CHAIN (x))
3371 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3372 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3373 else
3374 n_fields += 1;
3376 return n_fields;
3379 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3380 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3381 elts, starting at offset IDX. */
3383 static int
3384 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3386 tree x;
3387 for (x = fields; x; x = DECL_CHAIN (x))
3389 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3390 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3391 else
3392 field_vec->elts[idx++] = x;
3394 return idx;
3397 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3398 starting at offset IDX. */
3400 static int
3401 add_enum_fields_to_record_type (tree enumtype,
3402 struct sorted_fields_type *field_vec,
3403 int idx)
3405 tree values;
3406 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3407 field_vec->elts[idx++] = TREE_VALUE (values);
3408 return idx;
3411 /* FIELD is a bit-field. We are finishing the processing for its
3412 enclosing type. Issue any appropriate messages and set appropriate
3413 flags. Returns false if an error has been diagnosed. */
3415 static bool
3416 check_bitfield_decl (tree field)
3418 tree type = TREE_TYPE (field);
3419 tree w;
3421 /* Extract the declared width of the bitfield, which has been
3422 temporarily stashed in DECL_INITIAL. */
3423 w = DECL_INITIAL (field);
3424 gcc_assert (w != NULL_TREE);
3425 /* Remove the bit-field width indicator so that the rest of the
3426 compiler does not treat that value as an initializer. */
3427 DECL_INITIAL (field) = NULL_TREE;
3429 /* Detect invalid bit-field type. */
3430 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3432 error ("bit-field %q+#D with non-integral type", field);
3433 w = error_mark_node;
3435 else
3437 location_t loc = input_location;
3438 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3439 STRIP_NOPS (w);
3441 /* detect invalid field size. */
3442 input_location = DECL_SOURCE_LOCATION (field);
3443 w = cxx_constant_value (w);
3444 input_location = loc;
3446 if (TREE_CODE (w) != INTEGER_CST)
3448 error ("bit-field %q+D width not an integer constant", field);
3449 w = error_mark_node;
3451 else if (tree_int_cst_sgn (w) < 0)
3453 error ("negative width in bit-field %q+D", field);
3454 w = error_mark_node;
3456 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3458 error ("zero width for bit-field %q+D", field);
3459 w = error_mark_node;
3461 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3462 && TREE_CODE (type) != BOOLEAN_TYPE
3463 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3464 || ((TREE_CODE (type) == ENUMERAL_TYPE
3465 || TREE_CODE (type) == BOOLEAN_TYPE)
3466 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3467 warning_at (DECL_SOURCE_LOCATION (field), 0,
3468 "width of %qD exceeds its type", field);
3469 else if (TREE_CODE (type) == ENUMERAL_TYPE
3470 && (0 > (compare_tree_int
3471 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3472 warning_at (DECL_SOURCE_LOCATION (field), 0,
3473 "%qD is too small to hold all values of %q#T",
3474 field, type);
3477 if (w != error_mark_node)
3479 DECL_SIZE (field) = convert (bitsizetype, w);
3480 DECL_BIT_FIELD (field) = 1;
3481 return true;
3483 else
3485 /* Non-bit-fields are aligned for their type. */
3486 DECL_BIT_FIELD (field) = 0;
3487 CLEAR_DECL_C_BIT_FIELD (field);
3488 return false;
3492 /* FIELD is a non bit-field. We are finishing the processing for its
3493 enclosing type T. Issue any appropriate messages and set appropriate
3494 flags. */
3496 static void
3497 check_field_decl (tree field,
3498 tree t,
3499 int* cant_have_const_ctor,
3500 int* no_const_asn_ref,
3501 int* any_default_members)
3503 tree type = strip_array_types (TREE_TYPE (field));
3505 /* In C++98 an anonymous union cannot contain any fields which would change
3506 the settings of CANT_HAVE_CONST_CTOR and friends. */
3507 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3509 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3510 structs. So, we recurse through their fields here. */
3511 else if (ANON_AGGR_TYPE_P (type))
3513 tree fields;
3515 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
3516 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3517 check_field_decl (fields, t, cant_have_const_ctor,
3518 no_const_asn_ref, any_default_members);
3520 /* Check members with class type for constructors, destructors,
3521 etc. */
3522 else if (CLASS_TYPE_P (type))
3524 /* Never let anything with uninheritable virtuals
3525 make it through without complaint. */
3526 abstract_virtuals_error (field, type);
3528 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3530 static bool warned;
3531 int oldcount = errorcount;
3532 if (TYPE_NEEDS_CONSTRUCTING (type))
3533 error ("member %q+#D with constructor not allowed in union",
3534 field);
3535 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3536 error ("member %q+#D with destructor not allowed in union", field);
3537 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3538 error ("member %q+#D with copy assignment operator not allowed in union",
3539 field);
3540 if (!warned && errorcount > oldcount)
3542 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3543 "only available with -std=c++11 or -std=gnu++11");
3544 warned = true;
3547 else
3549 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3550 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3551 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3552 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3553 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3554 || !TYPE_HAS_COPY_ASSIGN (type));
3555 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3556 || !TYPE_HAS_COPY_CTOR (type));
3557 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3558 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3559 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3560 || TYPE_HAS_COMPLEX_DFLT (type));
3563 if (TYPE_HAS_COPY_CTOR (type)
3564 && !TYPE_HAS_CONST_COPY_CTOR (type))
3565 *cant_have_const_ctor = 1;
3567 if (TYPE_HAS_COPY_ASSIGN (type)
3568 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3569 *no_const_asn_ref = 1;
3572 check_abi_tags (t, field);
3574 if (DECL_INITIAL (field) != NULL_TREE)
3576 /* `build_class_init_list' does not recognize
3577 non-FIELD_DECLs. */
3578 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0)
3579 error ("multiple fields in union %qT initialized", t);
3580 *any_default_members = 1;
3584 /* Check the data members (both static and non-static), class-scoped
3585 typedefs, etc., appearing in the declaration of T. Issue
3586 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3587 declaration order) of access declarations; each TREE_VALUE in this
3588 list is a USING_DECL.
3590 In addition, set the following flags:
3592 EMPTY_P
3593 The class is empty, i.e., contains no non-static data members.
3595 CANT_HAVE_CONST_CTOR_P
3596 This class cannot have an implicitly generated copy constructor
3597 taking a const reference.
3599 CANT_HAVE_CONST_ASN_REF
3600 This class cannot have an implicitly generated assignment
3601 operator taking a const reference.
3603 All of these flags should be initialized before calling this
3604 function.
3606 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3607 fields can be added by adding to this chain. */
3609 static void
3610 check_field_decls (tree t, tree *access_decls,
3611 int *cant_have_const_ctor_p,
3612 int *no_const_asn_ref_p)
3614 tree *field;
3615 tree *next;
3616 bool has_pointers;
3617 int any_default_members;
3618 int cant_pack = 0;
3619 int field_access = -1;
3621 /* Assume there are no access declarations. */
3622 *access_decls = NULL_TREE;
3623 /* Assume this class has no pointer members. */
3624 has_pointers = false;
3625 /* Assume none of the members of this class have default
3626 initializations. */
3627 any_default_members = 0;
3629 for (field = &TYPE_FIELDS (t); *field; field = next)
3631 tree x = *field;
3632 tree type = TREE_TYPE (x);
3633 int this_field_access;
3635 next = &DECL_CHAIN (x);
3637 if (TREE_CODE (x) == USING_DECL)
3639 /* Save the access declarations for our caller. */
3640 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3641 continue;
3644 if (TREE_CODE (x) == TYPE_DECL
3645 || TREE_CODE (x) == TEMPLATE_DECL)
3646 continue;
3648 /* If we've gotten this far, it's a data member, possibly static,
3649 or an enumerator. */
3650 if (TREE_CODE (x) != CONST_DECL)
3651 DECL_CONTEXT (x) = t;
3653 /* When this goes into scope, it will be a non-local reference. */
3654 DECL_NONLOCAL (x) = 1;
3656 if (TREE_CODE (t) == UNION_TYPE
3657 && cxx_dialect < cxx11)
3659 /* [class.union] (C++98)
3661 If a union contains a static data member, or a member of
3662 reference type, the program is ill-formed.
3664 In C++11 this limitation doesn't exist anymore. */
3665 if (VAR_P (x))
3667 error ("in C++98 %q+D may not be static because it is "
3668 "a member of a union", x);
3669 continue;
3671 if (TREE_CODE (type) == REFERENCE_TYPE)
3673 error ("in C++98 %q+D may not have reference type %qT "
3674 "because it is a member of a union", x, type);
3675 continue;
3679 /* Perform error checking that did not get done in
3680 grokdeclarator. */
3681 if (TREE_CODE (type) == FUNCTION_TYPE)
3683 error ("field %q+D invalidly declared function type", x);
3684 type = build_pointer_type (type);
3685 TREE_TYPE (x) = type;
3687 else if (TREE_CODE (type) == METHOD_TYPE)
3689 error ("field %q+D invalidly declared method type", x);
3690 type = build_pointer_type (type);
3691 TREE_TYPE (x) = type;
3694 if (type == error_mark_node)
3695 continue;
3697 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3698 continue;
3700 /* Now it can only be a FIELD_DECL. */
3702 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3703 CLASSTYPE_NON_AGGREGATE (t) = 1;
3705 /* If at least one non-static data member is non-literal, the whole
3706 class becomes non-literal. Per Core/1453, volatile non-static
3707 data members and base classes are also not allowed.
3708 Note: if the type is incomplete we will complain later on. */
3709 if (COMPLETE_TYPE_P (type)
3710 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3711 CLASSTYPE_LITERAL_P (t) = false;
3713 /* A standard-layout class is a class that:
3715 has the same access control (Clause 11) for all non-static data members,
3716 ... */
3717 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3718 if (field_access == -1)
3719 field_access = this_field_access;
3720 else if (this_field_access != field_access)
3721 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3723 /* If this is of reference type, check if it needs an init. */
3724 if (TREE_CODE (type) == REFERENCE_TYPE)
3726 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3727 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3728 if (DECL_INITIAL (x) == NULL_TREE)
3729 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3730 if (cxx_dialect < cxx11)
3732 /* ARM $12.6.2: [A member initializer list] (or, for an
3733 aggregate, initialization by a brace-enclosed list) is the
3734 only way to initialize nonstatic const and reference
3735 members. */
3736 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3737 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3741 type = strip_array_types (type);
3743 if (TYPE_PACKED (t))
3745 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3747 warning_at
3748 (DECL_SOURCE_LOCATION (x), 0,
3749 "ignoring packed attribute because of unpacked non-POD field %q#D",
3751 cant_pack = 1;
3753 else if (DECL_C_BIT_FIELD (x)
3754 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3755 DECL_PACKED (x) = 1;
3758 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3759 /* We don't treat zero-width bitfields as making a class
3760 non-empty. */
3762 else
3764 /* The class is non-empty. */
3765 CLASSTYPE_EMPTY_P (t) = 0;
3766 /* The class is not even nearly empty. */
3767 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3768 /* If one of the data members contains an empty class,
3769 so does T. */
3770 if (CLASS_TYPE_P (type)
3771 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3772 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3775 /* This is used by -Weffc++ (see below). Warn only for pointers
3776 to members which might hold dynamic memory. So do not warn
3777 for pointers to functions or pointers to members. */
3778 if (TYPE_PTR_P (type)
3779 && !TYPE_PTRFN_P (type))
3780 has_pointers = true;
3782 if (CLASS_TYPE_P (type))
3784 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3785 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3786 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3787 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3790 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3791 CLASSTYPE_HAS_MUTABLE (t) = 1;
3793 if (DECL_MUTABLE_P (x))
3795 if (CP_TYPE_CONST_P (type))
3797 error ("member %q+D cannot be declared both %<const%> "
3798 "and %<mutable%>", x);
3799 continue;
3801 if (TREE_CODE (type) == REFERENCE_TYPE)
3803 error ("member %q+D cannot be declared as a %<mutable%> "
3804 "reference", x);
3805 continue;
3809 if (! layout_pod_type_p (type))
3810 /* DR 148 now allows pointers to members (which are POD themselves),
3811 to be allowed in POD structs. */
3812 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3814 if (!std_layout_type_p (type))
3815 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3817 if (! zero_init_p (type))
3818 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3820 /* We set DECL_C_BIT_FIELD in grokbitfield.
3821 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3822 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3823 check_field_decl (x, t,
3824 cant_have_const_ctor_p,
3825 no_const_asn_ref_p,
3826 &any_default_members);
3828 /* Now that we've removed bit-field widths from DECL_INITIAL,
3829 anything left in DECL_INITIAL is an NSDMI that makes the class
3830 non-aggregate in C++11. */
3831 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3832 CLASSTYPE_NON_AGGREGATE (t) = true;
3834 /* If any field is const, the structure type is pseudo-const. */
3835 if (CP_TYPE_CONST_P (type))
3837 C_TYPE_FIELDS_READONLY (t) = 1;
3838 if (DECL_INITIAL (x) == NULL_TREE)
3839 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3840 if (cxx_dialect < cxx11)
3842 /* ARM $12.6.2: [A member initializer list] (or, for an
3843 aggregate, initialization by a brace-enclosed list) is the
3844 only way to initialize nonstatic const and reference
3845 members. */
3846 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3847 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3850 /* A field that is pseudo-const makes the structure likewise. */
3851 else if (CLASS_TYPE_P (type))
3853 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3854 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3855 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3856 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3859 /* Core issue 80: A nonstatic data member is required to have a
3860 different name from the class iff the class has a
3861 user-declared constructor. */
3862 if (constructor_name_p (DECL_NAME (x), t)
3863 && TYPE_HAS_USER_CONSTRUCTOR (t))
3864 permerror (DECL_SOURCE_LOCATION (x),
3865 "field %q#D with same name as class", x);
3868 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3869 it should also define a copy constructor and an assignment operator to
3870 implement the correct copy semantic (deep vs shallow, etc.). As it is
3871 not feasible to check whether the constructors do allocate dynamic memory
3872 and store it within members, we approximate the warning like this:
3874 -- Warn only if there are members which are pointers
3875 -- Warn only if there is a non-trivial constructor (otherwise,
3876 there cannot be memory allocated).
3877 -- Warn only if there is a non-trivial destructor. We assume that the
3878 user at least implemented the cleanup correctly, and a destructor
3879 is needed to free dynamic memory.
3881 This seems enough for practical purposes. */
3882 if (warn_ecpp
3883 && has_pointers
3884 && TYPE_HAS_USER_CONSTRUCTOR (t)
3885 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3886 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3888 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3890 if (! TYPE_HAS_COPY_CTOR (t))
3892 warning (OPT_Weffc__,
3893 " but does not override %<%T(const %T&)%>", t, t);
3894 if (!TYPE_HAS_COPY_ASSIGN (t))
3895 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3897 else if (! TYPE_HAS_COPY_ASSIGN (t))
3898 warning (OPT_Weffc__,
3899 " but does not override %<operator=(const %T&)%>", t);
3902 /* Non-static data member initializers make the default constructor
3903 non-trivial. */
3904 if (any_default_members)
3906 TYPE_NEEDS_CONSTRUCTING (t) = true;
3907 TYPE_HAS_COMPLEX_DFLT (t) = true;
3910 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3911 if (cant_pack)
3912 TYPE_PACKED (t) = 0;
3914 /* Check anonymous struct/anonymous union fields. */
3915 finish_struct_anon (t);
3917 /* We've built up the list of access declarations in reverse order.
3918 Fix that now. */
3919 *access_decls = nreverse (*access_decls);
3922 /* If TYPE is an empty class type, records its OFFSET in the table of
3923 OFFSETS. */
3925 static int
3926 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3928 splay_tree_node n;
3930 if (!is_empty_class (type))
3931 return 0;
3933 /* Record the location of this empty object in OFFSETS. */
3934 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3935 if (!n)
3936 n = splay_tree_insert (offsets,
3937 (splay_tree_key) offset,
3938 (splay_tree_value) NULL_TREE);
3939 n->value = ((splay_tree_value)
3940 tree_cons (NULL_TREE,
3941 type,
3942 (tree) n->value));
3944 return 0;
3947 /* Returns nonzero if TYPE is an empty class type and there is
3948 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3950 static int
3951 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3953 splay_tree_node n;
3954 tree t;
3956 if (!is_empty_class (type))
3957 return 0;
3959 /* Record the location of this empty object in OFFSETS. */
3960 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3961 if (!n)
3962 return 0;
3964 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3965 if (same_type_p (TREE_VALUE (t), type))
3966 return 1;
3968 return 0;
3971 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3972 F for every subobject, passing it the type, offset, and table of
3973 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3974 be traversed.
3976 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3977 than MAX_OFFSET will not be walked.
3979 If F returns a nonzero value, the traversal ceases, and that value
3980 is returned. Otherwise, returns zero. */
3982 static int
3983 walk_subobject_offsets (tree type,
3984 subobject_offset_fn f,
3985 tree offset,
3986 splay_tree offsets,
3987 tree max_offset,
3988 int vbases_p)
3990 int r = 0;
3991 tree type_binfo = NULL_TREE;
3993 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3994 stop. */
3995 if (max_offset && tree_int_cst_lt (max_offset, offset))
3996 return 0;
3998 if (type == error_mark_node)
3999 return 0;
4001 if (!TYPE_P (type))
4003 type_binfo = type;
4004 type = BINFO_TYPE (type);
4007 if (CLASS_TYPE_P (type))
4009 tree field;
4010 tree binfo;
4011 int i;
4013 /* Avoid recursing into objects that are not interesting. */
4014 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4015 return 0;
4017 /* Record the location of TYPE. */
4018 r = (*f) (type, offset, offsets);
4019 if (r)
4020 return r;
4022 /* Iterate through the direct base classes of TYPE. */
4023 if (!type_binfo)
4024 type_binfo = TYPE_BINFO (type);
4025 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4027 tree binfo_offset;
4029 if (BINFO_VIRTUAL_P (binfo))
4030 continue;
4032 tree orig_binfo;
4033 /* We cannot rely on BINFO_OFFSET being set for the base
4034 class yet, but the offsets for direct non-virtual
4035 bases can be calculated by going back to the TYPE. */
4036 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4037 binfo_offset = size_binop (PLUS_EXPR,
4038 offset,
4039 BINFO_OFFSET (orig_binfo));
4041 r = walk_subobject_offsets (binfo,
4043 binfo_offset,
4044 offsets,
4045 max_offset,
4046 /*vbases_p=*/0);
4047 if (r)
4048 return r;
4051 if (CLASSTYPE_VBASECLASSES (type))
4053 unsigned ix;
4054 vec<tree, va_gc> *vbases;
4056 /* Iterate through the virtual base classes of TYPE. In G++
4057 3.2, we included virtual bases in the direct base class
4058 loop above, which results in incorrect results; the
4059 correct offsets for virtual bases are only known when
4060 working with the most derived type. */
4061 if (vbases_p)
4062 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4063 vec_safe_iterate (vbases, ix, &binfo); ix++)
4065 r = walk_subobject_offsets (binfo,
4067 size_binop (PLUS_EXPR,
4068 offset,
4069 BINFO_OFFSET (binfo)),
4070 offsets,
4071 max_offset,
4072 /*vbases_p=*/0);
4073 if (r)
4074 return r;
4076 else
4078 /* We still have to walk the primary base, if it is
4079 virtual. (If it is non-virtual, then it was walked
4080 above.) */
4081 tree vbase = get_primary_binfo (type_binfo);
4083 if (vbase && BINFO_VIRTUAL_P (vbase)
4084 && BINFO_PRIMARY_P (vbase)
4085 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4087 r = (walk_subobject_offsets
4088 (vbase, f, offset,
4089 offsets, max_offset, /*vbases_p=*/0));
4090 if (r)
4091 return r;
4096 /* Iterate through the fields of TYPE. */
4097 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4098 if (TREE_CODE (field) == FIELD_DECL
4099 && TREE_TYPE (field) != error_mark_node
4100 && !DECL_ARTIFICIAL (field))
4102 tree field_offset;
4104 field_offset = byte_position (field);
4106 r = walk_subobject_offsets (TREE_TYPE (field),
4108 size_binop (PLUS_EXPR,
4109 offset,
4110 field_offset),
4111 offsets,
4112 max_offset,
4113 /*vbases_p=*/1);
4114 if (r)
4115 return r;
4118 else if (TREE_CODE (type) == ARRAY_TYPE)
4120 tree element_type = strip_array_types (type);
4121 tree domain = TYPE_DOMAIN (type);
4122 tree index;
4124 /* Avoid recursing into objects that are not interesting. */
4125 if (!CLASS_TYPE_P (element_type)
4126 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
4127 return 0;
4129 /* Step through each of the elements in the array. */
4130 for (index = size_zero_node;
4131 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4132 index = size_binop (PLUS_EXPR, index, size_one_node))
4134 r = walk_subobject_offsets (TREE_TYPE (type),
4136 offset,
4137 offsets,
4138 max_offset,
4139 /*vbases_p=*/1);
4140 if (r)
4141 return r;
4142 offset = size_binop (PLUS_EXPR, offset,
4143 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4144 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4145 there's no point in iterating through the remaining
4146 elements of the array. */
4147 if (max_offset && tree_int_cst_lt (max_offset, offset))
4148 break;
4152 return 0;
4155 /* Record all of the empty subobjects of TYPE (either a type or a
4156 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4157 is being placed at OFFSET; otherwise, it is a base class that is
4158 being placed at OFFSET. */
4160 static void
4161 record_subobject_offsets (tree type,
4162 tree offset,
4163 splay_tree offsets,
4164 bool is_data_member)
4166 tree max_offset;
4167 /* If recording subobjects for a non-static data member or a
4168 non-empty base class , we do not need to record offsets beyond
4169 the size of the biggest empty class. Additional data members
4170 will go at the end of the class. Additional base classes will go
4171 either at offset zero (if empty, in which case they cannot
4172 overlap with offsets past the size of the biggest empty class) or
4173 at the end of the class.
4175 However, if we are placing an empty base class, then we must record
4176 all offsets, as either the empty class is at offset zero (where
4177 other empty classes might later be placed) or at the end of the
4178 class (where other objects might then be placed, so other empty
4179 subobjects might later overlap). */
4180 if (is_data_member
4181 || !is_empty_class (BINFO_TYPE (type)))
4182 max_offset = sizeof_biggest_empty_class;
4183 else
4184 max_offset = NULL_TREE;
4185 walk_subobject_offsets (type, record_subobject_offset, offset,
4186 offsets, max_offset, is_data_member);
4189 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4190 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4191 virtual bases of TYPE are examined. */
4193 static int
4194 layout_conflict_p (tree type,
4195 tree offset,
4196 splay_tree offsets,
4197 int vbases_p)
4199 splay_tree_node max_node;
4201 /* Get the node in OFFSETS that indicates the maximum offset where
4202 an empty subobject is located. */
4203 max_node = splay_tree_max (offsets);
4204 /* If there aren't any empty subobjects, then there's no point in
4205 performing this check. */
4206 if (!max_node)
4207 return 0;
4209 return walk_subobject_offsets (type, check_subobject_offset, offset,
4210 offsets, (tree) (max_node->key),
4211 vbases_p);
4214 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4215 non-static data member of the type indicated by RLI. BINFO is the
4216 binfo corresponding to the base subobject, OFFSETS maps offsets to
4217 types already located at those offsets. This function determines
4218 the position of the DECL. */
4220 static void
4221 layout_nonempty_base_or_field (record_layout_info rli,
4222 tree decl,
4223 tree binfo,
4224 splay_tree offsets)
4226 tree offset = NULL_TREE;
4227 bool field_p;
4228 tree type;
4230 if (binfo)
4232 /* For the purposes of determining layout conflicts, we want to
4233 use the class type of BINFO; TREE_TYPE (DECL) will be the
4234 CLASSTYPE_AS_BASE version, which does not contain entries for
4235 zero-sized bases. */
4236 type = TREE_TYPE (binfo);
4237 field_p = false;
4239 else
4241 type = TREE_TYPE (decl);
4242 field_p = true;
4245 /* Try to place the field. It may take more than one try if we have
4246 a hard time placing the field without putting two objects of the
4247 same type at the same address. */
4248 while (1)
4250 struct record_layout_info_s old_rli = *rli;
4252 /* Place this field. */
4253 place_field (rli, decl);
4254 offset = byte_position (decl);
4256 /* We have to check to see whether or not there is already
4257 something of the same type at the offset we're about to use.
4258 For example, consider:
4260 struct S {};
4261 struct T : public S { int i; };
4262 struct U : public S, public T {};
4264 Here, we put S at offset zero in U. Then, we can't put T at
4265 offset zero -- its S component would be at the same address
4266 as the S we already allocated. So, we have to skip ahead.
4267 Since all data members, including those whose type is an
4268 empty class, have nonzero size, any overlap can happen only
4269 with a direct or indirect base-class -- it can't happen with
4270 a data member. */
4271 /* In a union, overlap is permitted; all members are placed at
4272 offset zero. */
4273 if (TREE_CODE (rli->t) == UNION_TYPE)
4274 break;
4275 if (layout_conflict_p (field_p ? type : binfo, offset,
4276 offsets, field_p))
4278 /* Strip off the size allocated to this field. That puts us
4279 at the first place we could have put the field with
4280 proper alignment. */
4281 *rli = old_rli;
4283 /* Bump up by the alignment required for the type. */
4284 rli->bitpos
4285 = size_binop (PLUS_EXPR, rli->bitpos,
4286 bitsize_int (binfo
4287 ? CLASSTYPE_ALIGN (type)
4288 : TYPE_ALIGN (type)));
4289 normalize_rli (rli);
4291 else if (TREE_CODE (type) == NULLPTR_TYPE
4292 && warn_abi && abi_version_crosses (9))
4294 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4295 the offset wasn't aligned like a pointer when we started to
4296 layout this field, that affects its position. */
4297 tree pos = rli_size_unit_so_far (&old_rli);
4298 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4300 if (abi_version_at_least (9))
4301 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4302 "alignment of %qD increased in -fabi-version=9 "
4303 "(GCC 5.2)", decl);
4304 else
4305 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4306 "of %qD will increase in -fabi-version=9", decl);
4308 break;
4310 else
4311 /* There was no conflict. We're done laying out this field. */
4312 break;
4315 /* Now that we know where it will be placed, update its
4316 BINFO_OFFSET. */
4317 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4318 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4319 this point because their BINFO_OFFSET is copied from another
4320 hierarchy. Therefore, we may not need to add the entire
4321 OFFSET. */
4322 propagate_binfo_offsets (binfo,
4323 size_diffop_loc (input_location,
4324 convert (ssizetype, offset),
4325 convert (ssizetype,
4326 BINFO_OFFSET (binfo))));
4329 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4331 static int
4332 empty_base_at_nonzero_offset_p (tree type,
4333 tree offset,
4334 splay_tree /*offsets*/)
4336 return is_empty_class (type) && !integer_zerop (offset);
4339 /* Layout the empty base BINFO. EOC indicates the byte currently just
4340 past the end of the class, and should be correctly aligned for a
4341 class of the type indicated by BINFO; OFFSETS gives the offsets of
4342 the empty bases allocated so far. T is the most derived
4343 type. Return nonzero iff we added it at the end. */
4345 static bool
4346 layout_empty_base (record_layout_info rli, tree binfo,
4347 tree eoc, splay_tree offsets)
4349 tree alignment;
4350 tree basetype = BINFO_TYPE (binfo);
4351 bool atend = false;
4353 /* This routine should only be used for empty classes. */
4354 gcc_assert (is_empty_class (basetype));
4355 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4357 if (!integer_zerop (BINFO_OFFSET (binfo)))
4358 propagate_binfo_offsets
4359 (binfo, size_diffop_loc (input_location,
4360 size_zero_node, BINFO_OFFSET (binfo)));
4362 /* This is an empty base class. We first try to put it at offset
4363 zero. */
4364 if (layout_conflict_p (binfo,
4365 BINFO_OFFSET (binfo),
4366 offsets,
4367 /*vbases_p=*/0))
4369 /* That didn't work. Now, we move forward from the next
4370 available spot in the class. */
4371 atend = true;
4372 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
4373 while (1)
4375 if (!layout_conflict_p (binfo,
4376 BINFO_OFFSET (binfo),
4377 offsets,
4378 /*vbases_p=*/0))
4379 /* We finally found a spot where there's no overlap. */
4380 break;
4382 /* There's overlap here, too. Bump along to the next spot. */
4383 propagate_binfo_offsets (binfo, alignment);
4387 if (CLASSTYPE_USER_ALIGN (basetype))
4389 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4390 if (warn_packed)
4391 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4392 TYPE_USER_ALIGN (rli->t) = 1;
4395 return atend;
4398 /* Layout the base given by BINFO in the class indicated by RLI.
4399 *BASE_ALIGN is a running maximum of the alignments of
4400 any base class. OFFSETS gives the location of empty base
4401 subobjects. T is the most derived type. Return nonzero if the new
4402 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4403 *NEXT_FIELD, unless BINFO is for an empty base class.
4405 Returns the location at which the next field should be inserted. */
4407 static tree *
4408 build_base_field (record_layout_info rli, tree binfo,
4409 splay_tree offsets, tree *next_field)
4411 tree t = rli->t;
4412 tree basetype = BINFO_TYPE (binfo);
4414 if (!COMPLETE_TYPE_P (basetype))
4415 /* This error is now reported in xref_tag, thus giving better
4416 location information. */
4417 return next_field;
4419 /* Place the base class. */
4420 if (!is_empty_class (basetype))
4422 tree decl;
4424 /* The containing class is non-empty because it has a non-empty
4425 base class. */
4426 CLASSTYPE_EMPTY_P (t) = 0;
4428 /* Create the FIELD_DECL. */
4429 decl = build_decl (input_location,
4430 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4431 DECL_ARTIFICIAL (decl) = 1;
4432 DECL_IGNORED_P (decl) = 1;
4433 DECL_FIELD_CONTEXT (decl) = t;
4434 if (CLASSTYPE_AS_BASE (basetype))
4436 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4437 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4438 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4439 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4440 DECL_MODE (decl) = TYPE_MODE (basetype);
4441 DECL_FIELD_IS_BASE (decl) = 1;
4443 /* Try to place the field. It may take more than one try if we
4444 have a hard time placing the field without putting two
4445 objects of the same type at the same address. */
4446 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4447 /* Add the new FIELD_DECL to the list of fields for T. */
4448 DECL_CHAIN (decl) = *next_field;
4449 *next_field = decl;
4450 next_field = &DECL_CHAIN (decl);
4453 else
4455 tree eoc;
4456 bool atend;
4458 /* On some platforms (ARM), even empty classes will not be
4459 byte-aligned. */
4460 eoc = round_up_loc (input_location,
4461 rli_size_unit_so_far (rli),
4462 CLASSTYPE_ALIGN_UNIT (basetype));
4463 atend = layout_empty_base (rli, binfo, eoc, offsets);
4464 /* A nearly-empty class "has no proper base class that is empty,
4465 not morally virtual, and at an offset other than zero." */
4466 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4468 if (atend)
4469 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4470 /* The check above (used in G++ 3.2) is insufficient because
4471 an empty class placed at offset zero might itself have an
4472 empty base at a nonzero offset. */
4473 else if (walk_subobject_offsets (basetype,
4474 empty_base_at_nonzero_offset_p,
4475 size_zero_node,
4476 /*offsets=*/NULL,
4477 /*max_offset=*/NULL_TREE,
4478 /*vbases_p=*/true))
4479 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4482 /* We do not create a FIELD_DECL for empty base classes because
4483 it might overlap some other field. We want to be able to
4484 create CONSTRUCTORs for the class by iterating over the
4485 FIELD_DECLs, and the back end does not handle overlapping
4486 FIELD_DECLs. */
4488 /* An empty virtual base causes a class to be non-empty
4489 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4490 here because that was already done when the virtual table
4491 pointer was created. */
4494 /* Record the offsets of BINFO and its base subobjects. */
4495 record_subobject_offsets (binfo,
4496 BINFO_OFFSET (binfo),
4497 offsets,
4498 /*is_data_member=*/false);
4500 return next_field;
4503 /* Layout all of the non-virtual base classes. Record empty
4504 subobjects in OFFSETS. T is the most derived type. Return nonzero
4505 if the type cannot be nearly empty. The fields created
4506 corresponding to the base classes will be inserted at
4507 *NEXT_FIELD. */
4509 static void
4510 build_base_fields (record_layout_info rli,
4511 splay_tree offsets, tree *next_field)
4513 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4514 subobjects. */
4515 tree t = rli->t;
4516 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4517 int i;
4519 /* The primary base class is always allocated first. */
4520 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4521 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4522 offsets, next_field);
4524 /* Now allocate the rest of the bases. */
4525 for (i = 0; i < n_baseclasses; ++i)
4527 tree base_binfo;
4529 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4531 /* The primary base was already allocated above, so we don't
4532 need to allocate it again here. */
4533 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4534 continue;
4536 /* Virtual bases are added at the end (a primary virtual base
4537 will have already been added). */
4538 if (BINFO_VIRTUAL_P (base_binfo))
4539 continue;
4541 next_field = build_base_field (rli, base_binfo,
4542 offsets, next_field);
4546 /* Go through the TYPE_METHODS of T issuing any appropriate
4547 diagnostics, figuring out which methods override which other
4548 methods, and so forth. */
4550 static void
4551 check_methods (tree t)
4553 tree x;
4555 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4557 check_for_override (x, t);
4558 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4559 error ("initializer specified for non-virtual method %q+D", x);
4560 /* The name of the field is the original field name
4561 Save this in auxiliary field for later overloading. */
4562 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4564 TYPE_POLYMORPHIC_P (t) = 1;
4565 if (DECL_PURE_VIRTUAL_P (x))
4566 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4568 /* All user-provided destructors are non-trivial.
4569 Constructors and assignment ops are handled in
4570 grok_special_member_properties. */
4571 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4572 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4573 if (!DECL_VIRTUAL_P (x)
4574 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x)))
4575 error_at (DECL_SOURCE_LOCATION (x),
4576 "%<transaction_safe_dynamic%> may only be specified for "
4577 "a virtual function");
4581 /* FN is a constructor or destructor. Clone the declaration to create
4582 a specialized in-charge or not-in-charge version, as indicated by
4583 NAME. */
4585 static tree
4586 build_clone (tree fn, tree name)
4588 tree parms;
4589 tree clone;
4591 /* Copy the function. */
4592 clone = copy_decl (fn);
4593 /* Reset the function name. */
4594 DECL_NAME (clone) = name;
4595 /* Remember where this function came from. */
4596 DECL_ABSTRACT_ORIGIN (clone) = fn;
4597 /* Make it easy to find the CLONE given the FN. */
4598 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4599 DECL_CHAIN (fn) = clone;
4601 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4602 if (TREE_CODE (clone) == TEMPLATE_DECL)
4604 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4605 DECL_TEMPLATE_RESULT (clone) = result;
4606 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4607 DECL_TI_TEMPLATE (result) = clone;
4608 TREE_TYPE (clone) = TREE_TYPE (result);
4609 return clone;
4611 else
4613 // Clone constraints.
4614 if (flag_concepts)
4615 if (tree ci = get_constraints (fn))
4616 set_constraints (clone, copy_node (ci));
4620 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4621 DECL_CLONED_FUNCTION (clone) = fn;
4622 /* There's no pending inline data for this function. */
4623 DECL_PENDING_INLINE_INFO (clone) = NULL;
4624 DECL_PENDING_INLINE_P (clone) = 0;
4626 /* The base-class destructor is not virtual. */
4627 if (name == base_dtor_identifier)
4629 DECL_VIRTUAL_P (clone) = 0;
4630 if (TREE_CODE (clone) != TEMPLATE_DECL)
4631 DECL_VINDEX (clone) = NULL_TREE;
4634 /* If there was an in-charge parameter, drop it from the function
4635 type. */
4636 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4638 tree basetype;
4639 tree parmtypes;
4640 tree exceptions;
4642 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4643 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4644 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4645 /* Skip the `this' parameter. */
4646 parmtypes = TREE_CHAIN (parmtypes);
4647 /* Skip the in-charge parameter. */
4648 parmtypes = TREE_CHAIN (parmtypes);
4649 /* And the VTT parm, in a complete [cd]tor. */
4650 if (DECL_HAS_VTT_PARM_P (fn)
4651 && ! DECL_NEEDS_VTT_PARM_P (clone))
4652 parmtypes = TREE_CHAIN (parmtypes);
4653 /* If this is subobject constructor or destructor, add the vtt
4654 parameter. */
4655 TREE_TYPE (clone)
4656 = build_method_type_directly (basetype,
4657 TREE_TYPE (TREE_TYPE (clone)),
4658 parmtypes);
4659 if (exceptions)
4660 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4661 exceptions);
4662 TREE_TYPE (clone)
4663 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4664 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4667 /* Copy the function parameters. */
4668 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4669 /* Remove the in-charge parameter. */
4670 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4672 DECL_CHAIN (DECL_ARGUMENTS (clone))
4673 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4674 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4676 /* And the VTT parm, in a complete [cd]tor. */
4677 if (DECL_HAS_VTT_PARM_P (fn))
4679 if (DECL_NEEDS_VTT_PARM_P (clone))
4680 DECL_HAS_VTT_PARM_P (clone) = 1;
4681 else
4683 DECL_CHAIN (DECL_ARGUMENTS (clone))
4684 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4685 DECL_HAS_VTT_PARM_P (clone) = 0;
4689 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4691 DECL_CONTEXT (parms) = clone;
4692 cxx_dup_lang_specific_decl (parms);
4695 /* Create the RTL for this function. */
4696 SET_DECL_RTL (clone, NULL);
4697 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4699 return clone;
4702 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4703 not invoke this function directly.
4705 For a non-thunk function, returns the address of the slot for storing
4706 the function it is a clone of. Otherwise returns NULL_TREE.
4708 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4709 cloned_function is unset. This is to support the separate
4710 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4711 on a template makes sense, but not the former. */
4713 tree *
4714 decl_cloned_function_p (const_tree decl, bool just_testing)
4716 tree *ptr;
4717 if (just_testing)
4718 decl = STRIP_TEMPLATE (decl);
4720 if (TREE_CODE (decl) != FUNCTION_DECL
4721 || !DECL_LANG_SPECIFIC (decl)
4722 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4724 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4725 if (!just_testing)
4726 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4727 else
4728 #endif
4729 return NULL;
4732 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4733 if (just_testing && *ptr == NULL_TREE)
4734 return NULL;
4735 else
4736 return ptr;
4739 /* Produce declarations for all appropriate clones of FN. If
4740 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4741 CLASTYPE_METHOD_VEC as well. */
4743 void
4744 clone_function_decl (tree fn, int update_method_vec_p)
4746 tree clone;
4748 /* Avoid inappropriate cloning. */
4749 if (DECL_CHAIN (fn)
4750 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4751 return;
4753 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4755 /* For each constructor, we need two variants: an in-charge version
4756 and a not-in-charge version. */
4757 clone = build_clone (fn, complete_ctor_identifier);
4758 if (update_method_vec_p)
4759 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4760 clone = build_clone (fn, base_ctor_identifier);
4761 if (update_method_vec_p)
4762 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4764 else
4766 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4768 /* For each destructor, we need three variants: an in-charge
4769 version, a not-in-charge version, and an in-charge deleting
4770 version. We clone the deleting version first because that
4771 means it will go second on the TYPE_METHODS list -- and that
4772 corresponds to the correct layout order in the virtual
4773 function table.
4775 For a non-virtual destructor, we do not build a deleting
4776 destructor. */
4777 if (DECL_VIRTUAL_P (fn))
4779 clone = build_clone (fn, deleting_dtor_identifier);
4780 if (update_method_vec_p)
4781 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4783 clone = build_clone (fn, complete_dtor_identifier);
4784 if (update_method_vec_p)
4785 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4786 clone = build_clone (fn, base_dtor_identifier);
4787 if (update_method_vec_p)
4788 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4791 /* Note that this is an abstract function that is never emitted. */
4792 DECL_ABSTRACT_P (fn) = true;
4795 /* DECL is an in charge constructor, which is being defined. This will
4796 have had an in class declaration, from whence clones were
4797 declared. An out-of-class definition can specify additional default
4798 arguments. As it is the clones that are involved in overload
4799 resolution, we must propagate the information from the DECL to its
4800 clones. */
4802 void
4803 adjust_clone_args (tree decl)
4805 tree clone;
4807 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4808 clone = DECL_CHAIN (clone))
4810 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4811 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4812 tree decl_parms, clone_parms;
4814 clone_parms = orig_clone_parms;
4816 /* Skip the 'this' parameter. */
4817 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4818 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4820 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4821 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4822 if (DECL_HAS_VTT_PARM_P (decl))
4823 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4825 clone_parms = orig_clone_parms;
4826 if (DECL_HAS_VTT_PARM_P (clone))
4827 clone_parms = TREE_CHAIN (clone_parms);
4829 for (decl_parms = orig_decl_parms; decl_parms;
4830 decl_parms = TREE_CHAIN (decl_parms),
4831 clone_parms = TREE_CHAIN (clone_parms))
4833 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4834 TREE_TYPE (clone_parms)));
4836 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4838 /* A default parameter has been added. Adjust the
4839 clone's parameters. */
4840 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4841 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4842 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4843 tree type;
4845 clone_parms = orig_decl_parms;
4847 if (DECL_HAS_VTT_PARM_P (clone))
4849 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4850 TREE_VALUE (orig_clone_parms),
4851 clone_parms);
4852 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4854 type = build_method_type_directly (basetype,
4855 TREE_TYPE (TREE_TYPE (clone)),
4856 clone_parms);
4857 if (exceptions)
4858 type = build_exception_variant (type, exceptions);
4859 if (attrs)
4860 type = cp_build_type_attribute_variant (type, attrs);
4861 TREE_TYPE (clone) = type;
4863 clone_parms = NULL_TREE;
4864 break;
4867 gcc_assert (!clone_parms);
4871 /* For each of the constructors and destructors in T, create an
4872 in-charge and not-in-charge variant. */
4874 static void
4875 clone_constructors_and_destructors (tree t)
4877 tree fns;
4879 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4880 out now. */
4881 if (!CLASSTYPE_METHOD_VEC (t))
4882 return;
4884 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4885 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4886 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4887 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4890 /* Deduce noexcept for a destructor DTOR. */
4892 void
4893 deduce_noexcept_on_destructor (tree dtor)
4895 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4897 tree eh_spec = unevaluated_noexcept_spec ();
4898 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4902 /* For each destructor in T, deduce noexcept:
4904 12.4/3: A declaration of a destructor that does not have an
4905 exception-specification is implicitly considered to have the
4906 same exception-specification as an implicit declaration (15.4). */
4908 static void
4909 deduce_noexcept_on_destructors (tree t)
4911 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4912 out now. */
4913 if (!CLASSTYPE_METHOD_VEC (t))
4914 return;
4916 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4917 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4920 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4921 of TYPE for virtual functions which FNDECL overrides. Return a
4922 mask of the tm attributes found therein. */
4924 static int
4925 look_for_tm_attr_overrides (tree type, tree fndecl)
4927 tree binfo = TYPE_BINFO (type);
4928 tree base_binfo;
4929 int ix, found = 0;
4931 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4933 tree o, basetype = BINFO_TYPE (base_binfo);
4935 if (!TYPE_POLYMORPHIC_P (basetype))
4936 continue;
4938 o = look_for_overrides_here (basetype, fndecl);
4939 if (o)
4941 if (lookup_attribute ("transaction_safe_dynamic",
4942 DECL_ATTRIBUTES (o)))
4943 /* transaction_safe_dynamic is not inherited. */;
4944 else
4945 found |= tm_attr_to_mask (find_tm_attribute
4946 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4948 else
4949 found |= look_for_tm_attr_overrides (basetype, fndecl);
4952 return found;
4955 /* Subroutine of set_method_tm_attributes. Handle the checks and
4956 inheritance for one virtual method FNDECL. */
4958 static void
4959 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4961 tree tm_attr;
4962 int found, have;
4964 found = look_for_tm_attr_overrides (type, fndecl);
4966 /* If FNDECL doesn't actually override anything (i.e. T is the
4967 class that first declares FNDECL virtual), then we're done. */
4968 if (found == 0)
4969 return;
4971 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4972 have = tm_attr_to_mask (tm_attr);
4974 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4975 tm_pure must match exactly, otherwise no weakening of
4976 tm_safe > tm_callable > nothing. */
4977 /* ??? The tm_pure attribute didn't make the transition to the
4978 multivendor language spec. */
4979 if (have == TM_ATTR_PURE)
4981 if (found != TM_ATTR_PURE)
4983 found &= -found;
4984 goto err_override;
4987 /* If the overridden function is tm_pure, then FNDECL must be. */
4988 else if (found == TM_ATTR_PURE && tm_attr)
4989 goto err_override;
4990 /* Look for base class combinations that cannot be satisfied. */
4991 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4993 found &= ~TM_ATTR_PURE;
4994 found &= -found;
4995 error_at (DECL_SOURCE_LOCATION (fndecl),
4996 "method overrides both %<transaction_pure%> and %qE methods",
4997 tm_mask_to_attr (found));
4999 /* If FNDECL did not declare an attribute, then inherit the most
5000 restrictive one. */
5001 else if (tm_attr == NULL)
5003 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found));
5005 /* Otherwise validate that we're not weaker than a function
5006 that is being overridden. */
5007 else
5009 found &= -found;
5010 if (found <= TM_ATTR_CALLABLE && have > found)
5011 goto err_override;
5013 return;
5015 err_override:
5016 error_at (DECL_SOURCE_LOCATION (fndecl),
5017 "method declared %qE overriding %qE method",
5018 tm_attr, tm_mask_to_attr (found));
5021 /* For each of the methods in T, propagate a class-level tm attribute. */
5023 static void
5024 set_method_tm_attributes (tree t)
5026 tree class_tm_attr, fndecl;
5028 /* Don't bother collecting tm attributes if transactional memory
5029 support is not enabled. */
5030 if (!flag_tm)
5031 return;
5033 /* Process virtual methods first, as they inherit directly from the
5034 base virtual function and also require validation of new attributes. */
5035 if (TYPE_CONTAINS_VPTR_P (t))
5037 tree vchain;
5038 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5039 vchain = TREE_CHAIN (vchain))
5041 fndecl = BV_FN (vchain);
5042 if (DECL_THUNK_P (fndecl))
5043 fndecl = THUNK_TARGET (fndecl);
5044 set_one_vmethod_tm_attributes (t, fndecl);
5048 /* If the class doesn't have an attribute, nothing more to do. */
5049 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5050 if (class_tm_attr == NULL)
5051 return;
5053 /* Any method that does not yet have a tm attribute inherits
5054 the one from the class. */
5055 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5057 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5058 apply_tm_attr (fndecl, class_tm_attr);
5062 /* Returns true iff class T has a user-defined constructor other than
5063 the default constructor. */
5065 bool
5066 type_has_user_nondefault_constructor (tree t)
5068 tree fns;
5070 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5071 return false;
5073 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5075 tree fn = OVL_CURRENT (fns);
5076 if (!DECL_ARTIFICIAL (fn)
5077 && (TREE_CODE (fn) == TEMPLATE_DECL
5078 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5079 != NULL_TREE)))
5080 return true;
5083 return false;
5086 /* Returns the defaulted constructor if T has one. Otherwise, returns
5087 NULL_TREE. */
5089 tree
5090 in_class_defaulted_default_constructor (tree t)
5092 tree fns, args;
5094 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5095 return NULL_TREE;
5097 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5099 tree fn = OVL_CURRENT (fns);
5101 if (DECL_DEFAULTED_IN_CLASS_P (fn))
5103 args = FUNCTION_FIRST_USER_PARMTYPE (fn);
5104 while (args && TREE_PURPOSE (args))
5105 args = TREE_CHAIN (args);
5106 if (!args || args == void_list_node)
5107 return fn;
5111 return NULL_TREE;
5114 /* Returns true iff FN is a user-provided function, i.e. user-declared
5115 and not defaulted at its first declaration; or explicit, private,
5116 protected, or non-const. */
5118 bool
5119 user_provided_p (tree fn)
5121 if (TREE_CODE (fn) == TEMPLATE_DECL)
5122 return true;
5123 else
5124 return (!DECL_ARTIFICIAL (fn)
5125 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5126 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5129 /* Returns true iff class T has a user-provided constructor. */
5131 bool
5132 type_has_user_provided_constructor (tree t)
5134 tree fns;
5136 if (!CLASS_TYPE_P (t))
5137 return false;
5139 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5140 return false;
5142 /* This can happen in error cases; avoid crashing. */
5143 if (!CLASSTYPE_METHOD_VEC (t))
5144 return false;
5146 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5147 if (user_provided_p (OVL_CURRENT (fns)))
5148 return true;
5150 return false;
5153 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5154 declared or explicitly defaulted in the class body) default
5155 constructor. */
5157 bool
5158 type_has_non_user_provided_default_constructor (tree t)
5160 tree fns;
5162 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5163 return false;
5164 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5165 return true;
5167 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5169 tree fn = OVL_CURRENT (fns);
5170 if (TREE_CODE (fn) == FUNCTION_DECL
5171 && !user_provided_p (fn)
5172 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)))
5173 return true;
5176 return false;
5179 /* TYPE is being used as a virtual base, and has a non-trivial move
5180 assignment. Return true if this is due to there being a user-provided
5181 move assignment in TYPE or one of its subobjects; if there isn't, then
5182 multiple move assignment can't cause any harm. */
5184 bool
5185 vbase_has_user_provided_move_assign (tree type)
5187 /* Does the type itself have a user-provided move assignment operator? */
5188 for (tree fns
5189 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5190 fns; fns = OVL_NEXT (fns))
5192 tree fn = OVL_CURRENT (fns);
5193 if (move_fn_p (fn) && user_provided_p (fn))
5194 return true;
5197 /* Do any of its bases? */
5198 tree binfo = TYPE_BINFO (type);
5199 tree base_binfo;
5200 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5201 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5202 return true;
5204 /* Or non-static data members? */
5205 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5207 if (TREE_CODE (field) == FIELD_DECL
5208 && CLASS_TYPE_P (TREE_TYPE (field))
5209 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5210 return true;
5213 /* Seems not. */
5214 return false;
5217 /* If default-initialization leaves part of TYPE uninitialized, returns
5218 a DECL for the field or TYPE itself (DR 253). */
5220 tree
5221 default_init_uninitialized_part (tree type)
5223 tree t, r, binfo;
5224 int i;
5226 type = strip_array_types (type);
5227 if (!CLASS_TYPE_P (type))
5228 return type;
5229 if (!type_has_non_user_provided_default_constructor (type))
5230 return NULL_TREE;
5231 for (binfo = TYPE_BINFO (type), i = 0;
5232 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5234 r = default_init_uninitialized_part (BINFO_TYPE (t));
5235 if (r)
5236 return r;
5238 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5239 if (TREE_CODE (t) == FIELD_DECL
5240 && !DECL_ARTIFICIAL (t)
5241 && !DECL_INITIAL (t))
5243 r = default_init_uninitialized_part (TREE_TYPE (t));
5244 if (r)
5245 return DECL_P (r) ? r : t;
5248 return NULL_TREE;
5251 /* Returns true iff for class T, a trivial synthesized default constructor
5252 would be constexpr. */
5254 bool
5255 trivial_default_constructor_is_constexpr (tree t)
5257 /* A defaulted trivial default constructor is constexpr
5258 if there is nothing to initialize. */
5259 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5260 return is_really_empty_class (t);
5263 /* Returns true iff class T has a constexpr default constructor. */
5265 bool
5266 type_has_constexpr_default_constructor (tree t)
5268 tree fns;
5270 if (!CLASS_TYPE_P (t))
5272 /* The caller should have stripped an enclosing array. */
5273 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5274 return false;
5276 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5278 if (!TYPE_HAS_COMPLEX_DFLT (t))
5279 return trivial_default_constructor_is_constexpr (t);
5280 /* Non-trivial, we need to check subobject constructors. */
5281 lazily_declare_fn (sfk_constructor, t);
5283 fns = locate_ctor (t);
5284 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5287 /* Returns true iff class TYPE has a virtual destructor. */
5289 bool
5290 type_has_virtual_destructor (tree type)
5292 tree dtor;
5294 if (!CLASS_TYPE_P (type))
5295 return false;
5297 gcc_assert (COMPLETE_TYPE_P (type));
5298 dtor = CLASSTYPE_DESTRUCTORS (type);
5299 return (dtor && DECL_VIRTUAL_P (dtor));
5302 /* Returns true iff class T has a move constructor. */
5304 bool
5305 type_has_move_constructor (tree t)
5307 tree fns;
5309 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5311 gcc_assert (COMPLETE_TYPE_P (t));
5312 lazily_declare_fn (sfk_move_constructor, t);
5315 if (!CLASSTYPE_METHOD_VEC (t))
5316 return false;
5318 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5319 if (move_fn_p (OVL_CURRENT (fns)))
5320 return true;
5322 return false;
5325 /* Returns true iff class T has a move assignment operator. */
5327 bool
5328 type_has_move_assign (tree t)
5330 tree fns;
5332 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5334 gcc_assert (COMPLETE_TYPE_P (t));
5335 lazily_declare_fn (sfk_move_assignment, t);
5338 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5339 fns; fns = OVL_NEXT (fns))
5340 if (move_fn_p (OVL_CURRENT (fns)))
5341 return true;
5343 return false;
5346 /* Returns true iff class T has a move constructor that was explicitly
5347 declared in the class body. Note that this is different from
5348 "user-provided", which doesn't include functions that are defaulted in
5349 the class. */
5351 bool
5352 type_has_user_declared_move_constructor (tree t)
5354 tree fns;
5356 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5357 return false;
5359 if (!CLASSTYPE_METHOD_VEC (t))
5360 return false;
5362 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5364 tree fn = OVL_CURRENT (fns);
5365 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5366 return true;
5369 return false;
5372 /* Returns true iff class T has a move assignment operator that was
5373 explicitly declared in the class body. */
5375 bool
5376 type_has_user_declared_move_assign (tree t)
5378 tree fns;
5380 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5381 return false;
5383 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5384 fns; fns = OVL_NEXT (fns))
5386 tree fn = OVL_CURRENT (fns);
5387 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5388 return true;
5391 return false;
5394 /* Nonzero if we need to build up a constructor call when initializing an
5395 object of this class, either because it has a user-declared constructor
5396 or because it doesn't have a default constructor (so we need to give an
5397 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5398 what you care about is whether or not an object can be produced by a
5399 constructor (e.g. so we don't set TREE_READONLY on const variables of
5400 such type); use this function when what you care about is whether or not
5401 to try to call a constructor to create an object. The latter case is
5402 the former plus some cases of constructors that cannot be called. */
5404 bool
5405 type_build_ctor_call (tree t)
5407 tree inner;
5408 if (TYPE_NEEDS_CONSTRUCTING (t))
5409 return true;
5410 inner = strip_array_types (t);
5411 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5412 return false;
5413 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5414 return true;
5415 if (cxx_dialect < cxx11)
5416 return false;
5417 /* A user-declared constructor might be private, and a constructor might
5418 be trivial but deleted. */
5419 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5420 fns; fns = OVL_NEXT (fns))
5422 tree fn = OVL_CURRENT (fns);
5423 if (!DECL_ARTIFICIAL (fn)
5424 || DECL_DELETED_FN (fn))
5425 return true;
5427 return false;
5430 /* Like type_build_ctor_call, but for destructors. */
5432 bool
5433 type_build_dtor_call (tree t)
5435 tree inner;
5436 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5437 return true;
5438 inner = strip_array_types (t);
5439 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5440 || !COMPLETE_TYPE_P (inner))
5441 return false;
5442 if (cxx_dialect < cxx11)
5443 return false;
5444 /* A user-declared destructor might be private, and a destructor might
5445 be trivial but deleted. */
5446 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5447 fns; fns = OVL_NEXT (fns))
5449 tree fn = OVL_CURRENT (fns);
5450 if (!DECL_ARTIFICIAL (fn)
5451 || DECL_DELETED_FN (fn))
5452 return true;
5454 return false;
5457 /* Remove all zero-width bit-fields from T. */
5459 static void
5460 remove_zero_width_bit_fields (tree t)
5462 tree *fieldsp;
5464 fieldsp = &TYPE_FIELDS (t);
5465 while (*fieldsp)
5467 if (TREE_CODE (*fieldsp) == FIELD_DECL
5468 && DECL_C_BIT_FIELD (*fieldsp)
5469 /* We should not be confused by the fact that grokbitfield
5470 temporarily sets the width of the bit field into
5471 DECL_INITIAL (*fieldsp).
5472 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5473 to that width. */
5474 && (DECL_SIZE (*fieldsp) == NULL_TREE
5475 || integer_zerop (DECL_SIZE (*fieldsp))))
5476 *fieldsp = DECL_CHAIN (*fieldsp);
5477 else
5478 fieldsp = &DECL_CHAIN (*fieldsp);
5482 /* Returns TRUE iff we need a cookie when dynamically allocating an
5483 array whose elements have the indicated class TYPE. */
5485 static bool
5486 type_requires_array_cookie (tree type)
5488 tree fns;
5489 bool has_two_argument_delete_p = false;
5491 gcc_assert (CLASS_TYPE_P (type));
5493 /* If there's a non-trivial destructor, we need a cookie. In order
5494 to iterate through the array calling the destructor for each
5495 element, we'll have to know how many elements there are. */
5496 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5497 return true;
5499 /* If the usual deallocation function is a two-argument whose second
5500 argument is of type `size_t', then we have to pass the size of
5501 the array to the deallocation function, so we will need to store
5502 a cookie. */
5503 fns = lookup_fnfields (TYPE_BINFO (type),
5504 ansi_opname (VEC_DELETE_EXPR),
5505 /*protect=*/0);
5506 /* If there are no `operator []' members, or the lookup is
5507 ambiguous, then we don't need a cookie. */
5508 if (!fns || fns == error_mark_node)
5509 return false;
5510 /* Loop through all of the functions. */
5511 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5513 tree fn;
5514 tree second_parm;
5516 /* Select the current function. */
5517 fn = OVL_CURRENT (fns);
5518 /* See if this function is a one-argument delete function. If
5519 it is, then it will be the usual deallocation function. */
5520 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5521 if (second_parm == void_list_node)
5522 return false;
5523 /* Do not consider this function if its second argument is an
5524 ellipsis. */
5525 if (!second_parm)
5526 continue;
5527 /* Otherwise, if we have a two-argument function and the second
5528 argument is `size_t', it will be the usual deallocation
5529 function -- unless there is one-argument function, too. */
5530 if (TREE_CHAIN (second_parm) == void_list_node
5531 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5532 has_two_argument_delete_p = true;
5535 return has_two_argument_delete_p;
5538 /* Finish computing the `literal type' property of class type T.
5540 At this point, we have already processed base classes and
5541 non-static data members. We need to check whether the copy
5542 constructor is trivial, the destructor is trivial, and there
5543 is a trivial default constructor or at least one constexpr
5544 constructor other than the copy constructor. */
5546 static void
5547 finalize_literal_type_property (tree t)
5549 tree fn;
5551 if (cxx_dialect < cxx11
5552 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5553 CLASSTYPE_LITERAL_P (t) = false;
5554 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5555 && CLASSTYPE_NON_AGGREGATE (t)
5556 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5557 CLASSTYPE_LITERAL_P (t) = false;
5559 if (!CLASSTYPE_LITERAL_P (t))
5560 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5561 if (DECL_DECLARED_CONSTEXPR_P (fn)
5562 && TREE_CODE (fn) != TEMPLATE_DECL
5563 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5564 && !DECL_CONSTRUCTOR_P (fn))
5566 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5567 if (!DECL_GENERATED_P (fn))
5569 error ("enclosing class of constexpr non-static member "
5570 "function %q+#D is not a literal type", fn);
5571 explain_non_literal_class (t);
5576 /* T is a non-literal type used in a context which requires a constant
5577 expression. Explain why it isn't literal. */
5579 void
5580 explain_non_literal_class (tree t)
5582 static hash_set<tree> *diagnosed;
5584 if (!CLASS_TYPE_P (t))
5585 return;
5586 t = TYPE_MAIN_VARIANT (t);
5588 if (diagnosed == NULL)
5589 diagnosed = new hash_set<tree>;
5590 if (diagnosed->add (t))
5591 /* Already explained. */
5592 return;
5594 inform (0, "%q+T is not literal because:", t);
5595 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5596 inform (0, " %q+T has a non-trivial destructor", t);
5597 else if (CLASSTYPE_NON_AGGREGATE (t)
5598 && !TYPE_HAS_TRIVIAL_DFLT (t)
5599 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5601 inform (0, " %q+T is not an aggregate, does not have a trivial "
5602 "default constructor, and has no constexpr constructor that "
5603 "is not a copy or move constructor", t);
5604 if (type_has_non_user_provided_default_constructor (t))
5606 /* Note that we can't simply call locate_ctor because when the
5607 constructor is deleted it just returns NULL_TREE. */
5608 tree fns;
5609 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5611 tree fn = OVL_CURRENT (fns);
5612 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5614 parms = skip_artificial_parms_for (fn, parms);
5616 if (sufficient_parms_p (parms))
5618 if (DECL_DELETED_FN (fn))
5619 maybe_explain_implicit_delete (fn);
5620 else
5621 explain_invalid_constexpr_fn (fn);
5622 break;
5627 else
5629 tree binfo, base_binfo, field; int i;
5630 for (binfo = TYPE_BINFO (t), i = 0;
5631 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5633 tree basetype = TREE_TYPE (base_binfo);
5634 if (!CLASSTYPE_LITERAL_P (basetype))
5636 inform (0, " base class %qT of %q+T is non-literal",
5637 basetype, t);
5638 explain_non_literal_class (basetype);
5639 return;
5642 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5644 tree ftype;
5645 if (TREE_CODE (field) != FIELD_DECL)
5646 continue;
5647 ftype = TREE_TYPE (field);
5648 if (!literal_type_p (ftype))
5650 inform (DECL_SOURCE_LOCATION (field),
5651 " non-static data member %qD has non-literal type",
5652 field);
5653 if (CLASS_TYPE_P (ftype))
5654 explain_non_literal_class (ftype);
5656 if (CP_TYPE_VOLATILE_P (ftype))
5657 inform (DECL_SOURCE_LOCATION (field),
5658 " non-static data member %qD has volatile type", field);
5663 /* Check the validity of the bases and members declared in T. Add any
5664 implicitly-generated functions (like copy-constructors and
5665 assignment operators). Compute various flag bits (like
5666 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5667 level: i.e., independently of the ABI in use. */
5669 static void
5670 check_bases_and_members (tree t)
5672 /* Nonzero if the implicitly generated copy constructor should take
5673 a non-const reference argument. */
5674 int cant_have_const_ctor;
5675 /* Nonzero if the implicitly generated assignment operator
5676 should take a non-const reference argument. */
5677 int no_const_asn_ref;
5678 tree access_decls;
5679 bool saved_complex_asn_ref;
5680 bool saved_nontrivial_dtor;
5681 tree fn;
5683 /* By default, we use const reference arguments and generate default
5684 constructors. */
5685 cant_have_const_ctor = 0;
5686 no_const_asn_ref = 0;
5688 /* Check all the base-classes. */
5689 check_bases (t, &cant_have_const_ctor,
5690 &no_const_asn_ref);
5692 /* Deduce noexcept on destructors. This needs to happen after we've set
5693 triviality flags appropriately for our bases. */
5694 if (cxx_dialect >= cxx11)
5695 deduce_noexcept_on_destructors (t);
5697 /* Check all the method declarations. */
5698 check_methods (t);
5700 /* Save the initial values of these flags which only indicate whether
5701 or not the class has user-provided functions. As we analyze the
5702 bases and members we can set these flags for other reasons. */
5703 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5704 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5706 /* Check all the data member declarations. We cannot call
5707 check_field_decls until we have called check_bases check_methods,
5708 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5709 being set appropriately. */
5710 check_field_decls (t, &access_decls,
5711 &cant_have_const_ctor,
5712 &no_const_asn_ref);
5714 /* A nearly-empty class has to be vptr-containing; a nearly empty
5715 class contains just a vptr. */
5716 if (!TYPE_CONTAINS_VPTR_P (t))
5717 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5719 /* Do some bookkeeping that will guide the generation of implicitly
5720 declared member functions. */
5721 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5722 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5723 /* We need to call a constructor for this class if it has a
5724 user-provided constructor, or if the default constructor is going
5725 to initialize the vptr. (This is not an if-and-only-if;
5726 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5727 themselves need constructing.) */
5728 TYPE_NEEDS_CONSTRUCTING (t)
5729 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5730 /* [dcl.init.aggr]
5732 An aggregate is an array or a class with no user-provided
5733 constructors ... and no virtual functions.
5735 Again, other conditions for being an aggregate are checked
5736 elsewhere. */
5737 CLASSTYPE_NON_AGGREGATE (t)
5738 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
5739 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5740 retain the old definition internally for ABI reasons. */
5741 CLASSTYPE_NON_LAYOUT_POD_P (t)
5742 |= (CLASSTYPE_NON_AGGREGATE (t)
5743 || saved_nontrivial_dtor || saved_complex_asn_ref);
5744 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5745 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5746 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5747 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5749 /* If the only explicitly declared default constructor is user-provided,
5750 set TYPE_HAS_COMPLEX_DFLT. */
5751 if (!TYPE_HAS_COMPLEX_DFLT (t)
5752 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5753 && !type_has_non_user_provided_default_constructor (t))
5754 TYPE_HAS_COMPLEX_DFLT (t) = true;
5756 /* Warn if a public base of a polymorphic type has an accessible
5757 non-virtual destructor. It is only now that we know the class is
5758 polymorphic. Although a polymorphic base will have a already
5759 been diagnosed during its definition, we warn on use too. */
5760 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5762 tree binfo = TYPE_BINFO (t);
5763 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5764 tree base_binfo;
5765 unsigned i;
5767 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5769 tree basetype = TREE_TYPE (base_binfo);
5771 if ((*accesses)[i] == access_public_node
5772 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5773 && accessible_nvdtor_p (basetype))
5774 warning (OPT_Wnon_virtual_dtor,
5775 "base class %q#T has accessible non-virtual destructor",
5776 basetype);
5780 /* If the class has no user-declared constructor, but does have
5781 non-static const or reference data members that can never be
5782 initialized, issue a warning. */
5783 if (warn_uninitialized
5784 /* Classes with user-declared constructors are presumed to
5785 initialize these members. */
5786 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5787 /* Aggregates can be initialized with brace-enclosed
5788 initializers. */
5789 && CLASSTYPE_NON_AGGREGATE (t))
5791 tree field;
5793 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5795 tree type;
5797 if (TREE_CODE (field) != FIELD_DECL
5798 || DECL_INITIAL (field) != NULL_TREE)
5799 continue;
5801 type = TREE_TYPE (field);
5802 if (TREE_CODE (type) == REFERENCE_TYPE)
5803 warning_at (DECL_SOURCE_LOCATION (field),
5804 OPT_Wuninitialized, "non-static reference %q#D "
5805 "in class without a constructor", field);
5806 else if (CP_TYPE_CONST_P (type)
5807 && (!CLASS_TYPE_P (type)
5808 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5809 warning_at (DECL_SOURCE_LOCATION (field),
5810 OPT_Wuninitialized, "non-static const member %q#D "
5811 "in class without a constructor", field);
5815 /* Synthesize any needed methods. */
5816 add_implicitly_declared_members (t, &access_decls,
5817 cant_have_const_ctor,
5818 no_const_asn_ref);
5820 /* Check defaulted declarations here so we have cant_have_const_ctor
5821 and don't need to worry about clones. */
5822 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5823 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5825 int copy = copy_fn_p (fn);
5826 if (copy > 0)
5828 bool imp_const_p
5829 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5830 : !no_const_asn_ref);
5831 bool fn_const_p = (copy == 2);
5833 if (fn_const_p && !imp_const_p)
5834 /* If the function is defaulted outside the class, we just
5835 give the synthesis error. */
5836 error ("%q+D declared to take const reference, but implicit "
5837 "declaration would take non-const", fn);
5839 defaulted_late_check (fn);
5842 if (LAMBDA_TYPE_P (t))
5844 /* "This class type is not an aggregate." */
5845 CLASSTYPE_NON_AGGREGATE (t) = 1;
5848 /* Compute the 'literal type' property before we
5849 do anything with non-static member functions. */
5850 finalize_literal_type_property (t);
5852 /* Create the in-charge and not-in-charge variants of constructors
5853 and destructors. */
5854 clone_constructors_and_destructors (t);
5856 /* Process the using-declarations. */
5857 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5858 handle_using_decl (TREE_VALUE (access_decls), t);
5860 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5861 finish_struct_methods (t);
5863 /* Figure out whether or not we will need a cookie when dynamically
5864 allocating an array of this type. */
5865 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5866 = type_requires_array_cookie (t);
5869 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5870 accordingly. If a new vfield was created (because T doesn't have a
5871 primary base class), then the newly created field is returned. It
5872 is not added to the TYPE_FIELDS list; it is the caller's
5873 responsibility to do that. Accumulate declared virtual functions
5874 on VIRTUALS_P. */
5876 static tree
5877 create_vtable_ptr (tree t, tree* virtuals_p)
5879 tree fn;
5881 /* Collect the virtual functions declared in T. */
5882 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5883 if (TREE_CODE (fn) == FUNCTION_DECL
5884 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5885 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5887 tree new_virtual = make_node (TREE_LIST);
5889 BV_FN (new_virtual) = fn;
5890 BV_DELTA (new_virtual) = integer_zero_node;
5891 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5893 TREE_CHAIN (new_virtual) = *virtuals_p;
5894 *virtuals_p = new_virtual;
5897 /* If we couldn't find an appropriate base class, create a new field
5898 here. Even if there weren't any new virtual functions, we might need a
5899 new virtual function table if we're supposed to include vptrs in
5900 all classes that need them. */
5901 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5903 /* We build this decl with vtbl_ptr_type_node, which is a
5904 `vtable_entry_type*'. It might seem more precise to use
5905 `vtable_entry_type (*)[N]' where N is the number of virtual
5906 functions. However, that would require the vtable pointer in
5907 base classes to have a different type than the vtable pointer
5908 in derived classes. We could make that happen, but that
5909 still wouldn't solve all the problems. In particular, the
5910 type-based alias analysis code would decide that assignments
5911 to the base class vtable pointer can't alias assignments to
5912 the derived class vtable pointer, since they have different
5913 types. Thus, in a derived class destructor, where the base
5914 class constructor was inlined, we could generate bad code for
5915 setting up the vtable pointer.
5917 Therefore, we use one type for all vtable pointers. We still
5918 use a type-correct type; it's just doesn't indicate the array
5919 bounds. That's better than using `void*' or some such; it's
5920 cleaner, and it let's the alias analysis code know that these
5921 stores cannot alias stores to void*! */
5922 tree field;
5924 field = build_decl (input_location,
5925 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5926 DECL_VIRTUAL_P (field) = 1;
5927 DECL_ARTIFICIAL (field) = 1;
5928 DECL_FIELD_CONTEXT (field) = t;
5929 DECL_FCONTEXT (field) = t;
5930 if (TYPE_PACKED (t))
5931 DECL_PACKED (field) = 1;
5933 TYPE_VFIELD (t) = field;
5935 /* This class is non-empty. */
5936 CLASSTYPE_EMPTY_P (t) = 0;
5938 return field;
5941 return NULL_TREE;
5944 /* Add OFFSET to all base types of BINFO which is a base in the
5945 hierarchy dominated by T.
5947 OFFSET, which is a type offset, is number of bytes. */
5949 static void
5950 propagate_binfo_offsets (tree binfo, tree offset)
5952 int i;
5953 tree primary_binfo;
5954 tree base_binfo;
5956 /* Update BINFO's offset. */
5957 BINFO_OFFSET (binfo)
5958 = convert (sizetype,
5959 size_binop (PLUS_EXPR,
5960 convert (ssizetype, BINFO_OFFSET (binfo)),
5961 offset));
5963 /* Find the primary base class. */
5964 primary_binfo = get_primary_binfo (binfo);
5966 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5967 propagate_binfo_offsets (primary_binfo, offset);
5969 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5970 downwards. */
5971 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5973 /* Don't do the primary base twice. */
5974 if (base_binfo == primary_binfo)
5975 continue;
5977 if (BINFO_VIRTUAL_P (base_binfo))
5978 continue;
5980 propagate_binfo_offsets (base_binfo, offset);
5984 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5985 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5986 empty subobjects of T. */
5988 static void
5989 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5991 tree vbase;
5992 tree t = rli->t;
5993 tree *next_field;
5995 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5996 return;
5998 /* Find the last field. The artificial fields created for virtual
5999 bases will go after the last extant field to date. */
6000 next_field = &TYPE_FIELDS (t);
6001 while (*next_field)
6002 next_field = &DECL_CHAIN (*next_field);
6004 /* Go through the virtual bases, allocating space for each virtual
6005 base that is not already a primary base class. These are
6006 allocated in inheritance graph order. */
6007 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6009 if (!BINFO_VIRTUAL_P (vbase))
6010 continue;
6012 if (!BINFO_PRIMARY_P (vbase))
6014 /* This virtual base is not a primary base of any class in the
6015 hierarchy, so we have to add space for it. */
6016 next_field = build_base_field (rli, vbase,
6017 offsets, next_field);
6022 /* Returns the offset of the byte just past the end of the base class
6023 BINFO. */
6025 static tree
6026 end_of_base (tree binfo)
6028 tree size;
6030 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6031 size = TYPE_SIZE_UNIT (char_type_node);
6032 else if (is_empty_class (BINFO_TYPE (binfo)))
6033 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6034 allocate some space for it. It cannot have virtual bases, so
6035 TYPE_SIZE_UNIT is fine. */
6036 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6037 else
6038 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6040 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6043 /* Returns the offset of the byte just past the end of the base class
6044 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6045 only non-virtual bases are included. */
6047 static tree
6048 end_of_class (tree t, int include_virtuals_p)
6050 tree result = size_zero_node;
6051 vec<tree, va_gc> *vbases;
6052 tree binfo;
6053 tree base_binfo;
6054 tree offset;
6055 int i;
6057 for (binfo = TYPE_BINFO (t), i = 0;
6058 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6060 if (!include_virtuals_p
6061 && BINFO_VIRTUAL_P (base_binfo)
6062 && (!BINFO_PRIMARY_P (base_binfo)
6063 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6064 continue;
6066 offset = end_of_base (base_binfo);
6067 if (tree_int_cst_lt (result, offset))
6068 result = offset;
6071 if (include_virtuals_p)
6072 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6073 vec_safe_iterate (vbases, i, &base_binfo); i++)
6075 offset = end_of_base (base_binfo);
6076 if (tree_int_cst_lt (result, offset))
6077 result = offset;
6080 return result;
6083 /* Warn about bases of T that are inaccessible because they are
6084 ambiguous. For example:
6086 struct S {};
6087 struct T : public S {};
6088 struct U : public S, public T {};
6090 Here, `(S*) new U' is not allowed because there are two `S'
6091 subobjects of U. */
6093 static void
6094 warn_about_ambiguous_bases (tree t)
6096 int i;
6097 vec<tree, va_gc> *vbases;
6098 tree basetype;
6099 tree binfo;
6100 tree base_binfo;
6102 /* If there are no repeated bases, nothing can be ambiguous. */
6103 if (!CLASSTYPE_REPEATED_BASE_P (t))
6104 return;
6106 /* Check direct bases. */
6107 for (binfo = TYPE_BINFO (t), i = 0;
6108 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6110 basetype = BINFO_TYPE (base_binfo);
6112 if (!uniquely_derived_from_p (basetype, t))
6113 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6114 basetype, t);
6117 /* Check for ambiguous virtual bases. */
6118 if (extra_warnings)
6119 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6120 vec_safe_iterate (vbases, i, &binfo); i++)
6122 basetype = BINFO_TYPE (binfo);
6124 if (!uniquely_derived_from_p (basetype, t))
6125 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6126 "to ambiguity", basetype, t);
6130 /* Compare two INTEGER_CSTs K1 and K2. */
6132 static int
6133 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6135 return tree_int_cst_compare ((tree) k1, (tree) k2);
6138 /* Increase the size indicated in RLI to account for empty classes
6139 that are "off the end" of the class. */
6141 static void
6142 include_empty_classes (record_layout_info rli)
6144 tree eoc;
6145 tree rli_size;
6147 /* It might be the case that we grew the class to allocate a
6148 zero-sized base class. That won't be reflected in RLI, yet,
6149 because we are willing to overlay multiple bases at the same
6150 offset. However, now we need to make sure that RLI is big enough
6151 to reflect the entire class. */
6152 eoc = end_of_class (rli->t,
6153 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6154 rli_size = rli_size_unit_so_far (rli);
6155 if (TREE_CODE (rli_size) == INTEGER_CST
6156 && tree_int_cst_lt (rli_size, eoc))
6158 /* The size should have been rounded to a whole byte. */
6159 gcc_assert (tree_int_cst_equal
6160 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6161 rli->bitpos
6162 = size_binop (PLUS_EXPR,
6163 rli->bitpos,
6164 size_binop (MULT_EXPR,
6165 convert (bitsizetype,
6166 size_binop (MINUS_EXPR,
6167 eoc, rli_size)),
6168 bitsize_int (BITS_PER_UNIT)));
6169 normalize_rli (rli);
6173 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6174 BINFO_OFFSETs for all of the base-classes. Position the vtable
6175 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6177 static void
6178 layout_class_type (tree t, tree *virtuals_p)
6180 tree non_static_data_members;
6181 tree field;
6182 tree vptr;
6183 record_layout_info rli;
6184 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6185 types that appear at that offset. */
6186 splay_tree empty_base_offsets;
6187 /* True if the last field laid out was a bit-field. */
6188 bool last_field_was_bitfield = false;
6189 /* The location at which the next field should be inserted. */
6190 tree *next_field;
6191 /* T, as a base class. */
6192 tree base_t;
6194 /* Keep track of the first non-static data member. */
6195 non_static_data_members = TYPE_FIELDS (t);
6197 /* Start laying out the record. */
6198 rli = start_record_layout (t);
6200 /* Mark all the primary bases in the hierarchy. */
6201 determine_primary_bases (t);
6203 /* Create a pointer to our virtual function table. */
6204 vptr = create_vtable_ptr (t, virtuals_p);
6206 /* The vptr is always the first thing in the class. */
6207 if (vptr)
6209 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6210 TYPE_FIELDS (t) = vptr;
6211 next_field = &DECL_CHAIN (vptr);
6212 place_field (rli, vptr);
6214 else
6215 next_field = &TYPE_FIELDS (t);
6217 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6218 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6219 NULL, NULL);
6220 build_base_fields (rli, empty_base_offsets, next_field);
6222 /* Layout the non-static data members. */
6223 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6225 tree type;
6226 tree padding;
6228 /* We still pass things that aren't non-static data members to
6229 the back end, in case it wants to do something with them. */
6230 if (TREE_CODE (field) != FIELD_DECL)
6232 place_field (rli, field);
6233 /* If the static data member has incomplete type, keep track
6234 of it so that it can be completed later. (The handling
6235 of pending statics in finish_record_layout is
6236 insufficient; consider:
6238 struct S1;
6239 struct S2 { static S1 s1; };
6241 At this point, finish_record_layout will be called, but
6242 S1 is still incomplete.) */
6243 if (VAR_P (field))
6245 maybe_register_incomplete_var (field);
6246 /* The visibility of static data members is determined
6247 at their point of declaration, not their point of
6248 definition. */
6249 determine_visibility (field);
6251 continue;
6254 type = TREE_TYPE (field);
6255 if (type == error_mark_node)
6256 continue;
6258 padding = NULL_TREE;
6260 /* If this field is a bit-field whose width is greater than its
6261 type, then there are some special rules for allocating
6262 it. */
6263 if (DECL_C_BIT_FIELD (field)
6264 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6266 unsigned int itk;
6267 tree integer_type;
6268 bool was_unnamed_p = false;
6269 /* We must allocate the bits as if suitably aligned for the
6270 longest integer type that fits in this many bits. type
6271 of the field. Then, we are supposed to use the left over
6272 bits as additional padding. */
6273 for (itk = itk_char; itk != itk_none; ++itk)
6274 if (integer_types[itk] != NULL_TREE
6275 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6276 TYPE_SIZE (integer_types[itk]))
6277 || tree_int_cst_lt (DECL_SIZE (field),
6278 TYPE_SIZE (integer_types[itk]))))
6279 break;
6281 /* ITK now indicates a type that is too large for the
6282 field. We have to back up by one to find the largest
6283 type that fits. */
6286 --itk;
6287 integer_type = integer_types[itk];
6288 } while (itk > 0 && integer_type == NULL_TREE);
6290 /* Figure out how much additional padding is required. */
6291 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6293 if (TREE_CODE (t) == UNION_TYPE)
6294 /* In a union, the padding field must have the full width
6295 of the bit-field; all fields start at offset zero. */
6296 padding = DECL_SIZE (field);
6297 else
6298 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6299 TYPE_SIZE (integer_type));
6302 /* An unnamed bitfield does not normally affect the
6303 alignment of the containing class on a target where
6304 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6305 make any exceptions for unnamed bitfields when the
6306 bitfields are longer than their types. Therefore, we
6307 temporarily give the field a name. */
6308 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6310 was_unnamed_p = true;
6311 DECL_NAME (field) = make_anon_name ();
6314 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6315 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
6316 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6317 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6318 empty_base_offsets);
6319 if (was_unnamed_p)
6320 DECL_NAME (field) = NULL_TREE;
6321 /* Now that layout has been performed, set the size of the
6322 field to the size of its declared type; the rest of the
6323 field is effectively invisible. */
6324 DECL_SIZE (field) = TYPE_SIZE (type);
6325 /* We must also reset the DECL_MODE of the field. */
6326 DECL_MODE (field) = TYPE_MODE (type);
6328 else
6329 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6330 empty_base_offsets);
6332 /* Remember the location of any empty classes in FIELD. */
6333 record_subobject_offsets (TREE_TYPE (field),
6334 byte_position(field),
6335 empty_base_offsets,
6336 /*is_data_member=*/true);
6338 /* If a bit-field does not immediately follow another bit-field,
6339 and yet it starts in the middle of a byte, we have failed to
6340 comply with the ABI. */
6341 if (warn_abi
6342 && DECL_C_BIT_FIELD (field)
6343 /* The TREE_NO_WARNING flag gets set by Objective-C when
6344 laying out an Objective-C class. The ObjC ABI differs
6345 from the C++ ABI, and so we do not want a warning
6346 here. */
6347 && !TREE_NO_WARNING (field)
6348 && !last_field_was_bitfield
6349 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6350 DECL_FIELD_BIT_OFFSET (field),
6351 bitsize_unit_node)))
6352 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6353 "offset of %qD is not ABI-compliant and may "
6354 "change in a future version of GCC", field);
6356 /* The middle end uses the type of expressions to determine the
6357 possible range of expression values. In order to optimize
6358 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6359 must be made aware of the width of "i", via its type.
6361 Because C++ does not have integer types of arbitrary width,
6362 we must (for the purposes of the front end) convert from the
6363 type assigned here to the declared type of the bitfield
6364 whenever a bitfield expression is used as an rvalue.
6365 Similarly, when assigning a value to a bitfield, the value
6366 must be converted to the type given the bitfield here. */
6367 if (DECL_C_BIT_FIELD (field))
6369 unsigned HOST_WIDE_INT width;
6370 tree ftype = TREE_TYPE (field);
6371 width = tree_to_uhwi (DECL_SIZE (field));
6372 if (width != TYPE_PRECISION (ftype))
6374 TREE_TYPE (field)
6375 = c_build_bitfield_integer_type (width,
6376 TYPE_UNSIGNED (ftype));
6377 TREE_TYPE (field)
6378 = cp_build_qualified_type (TREE_TYPE (field),
6379 cp_type_quals (ftype));
6383 /* If we needed additional padding after this field, add it
6384 now. */
6385 if (padding)
6387 tree padding_field;
6389 padding_field = build_decl (input_location,
6390 FIELD_DECL,
6391 NULL_TREE,
6392 char_type_node);
6393 DECL_BIT_FIELD (padding_field) = 1;
6394 DECL_SIZE (padding_field) = padding;
6395 DECL_CONTEXT (padding_field) = t;
6396 DECL_ARTIFICIAL (padding_field) = 1;
6397 DECL_IGNORED_P (padding_field) = 1;
6398 layout_nonempty_base_or_field (rli, padding_field,
6399 NULL_TREE,
6400 empty_base_offsets);
6403 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6406 if (!integer_zerop (rli->bitpos))
6408 /* Make sure that we are on a byte boundary so that the size of
6409 the class without virtual bases will always be a round number
6410 of bytes. */
6411 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6412 normalize_rli (rli);
6415 /* Delete all zero-width bit-fields from the list of fields. Now
6416 that the type is laid out they are no longer important. */
6417 remove_zero_width_bit_fields (t);
6419 /* Create the version of T used for virtual bases. We do not use
6420 make_class_type for this version; this is an artificial type. For
6421 a POD type, we just reuse T. */
6422 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6424 base_t = make_node (TREE_CODE (t));
6426 /* Set the size and alignment for the new type. */
6427 tree eoc;
6429 /* If the ABI version is not at least two, and the last
6430 field was a bit-field, RLI may not be on a byte
6431 boundary. In particular, rli_size_unit_so_far might
6432 indicate the last complete byte, while rli_size_so_far
6433 indicates the total number of bits used. Therefore,
6434 rli_size_so_far, rather than rli_size_unit_so_far, is
6435 used to compute TYPE_SIZE_UNIT. */
6436 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6437 TYPE_SIZE_UNIT (base_t)
6438 = size_binop (MAX_EXPR,
6439 convert (sizetype,
6440 size_binop (CEIL_DIV_EXPR,
6441 rli_size_so_far (rli),
6442 bitsize_int (BITS_PER_UNIT))),
6443 eoc);
6444 TYPE_SIZE (base_t)
6445 = size_binop (MAX_EXPR,
6446 rli_size_so_far (rli),
6447 size_binop (MULT_EXPR,
6448 convert (bitsizetype, eoc),
6449 bitsize_int (BITS_PER_UNIT)));
6450 TYPE_ALIGN (base_t) = rli->record_align;
6451 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6453 /* Copy the fields from T. */
6454 next_field = &TYPE_FIELDS (base_t);
6455 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6456 if (TREE_CODE (field) == FIELD_DECL)
6458 *next_field = build_decl (input_location,
6459 FIELD_DECL,
6460 DECL_NAME (field),
6461 TREE_TYPE (field));
6462 DECL_CONTEXT (*next_field) = base_t;
6463 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
6464 DECL_FIELD_BIT_OFFSET (*next_field)
6465 = DECL_FIELD_BIT_OFFSET (field);
6466 DECL_SIZE (*next_field) = DECL_SIZE (field);
6467 DECL_MODE (*next_field) = DECL_MODE (field);
6468 next_field = &DECL_CHAIN (*next_field);
6471 /* Record the base version of the type. */
6472 CLASSTYPE_AS_BASE (t) = base_t;
6473 TYPE_CONTEXT (base_t) = t;
6475 else
6476 CLASSTYPE_AS_BASE (t) = t;
6478 /* Every empty class contains an empty class. */
6479 if (CLASSTYPE_EMPTY_P (t))
6480 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6482 /* Set the TYPE_DECL for this type to contain the right
6483 value for DECL_OFFSET, so that we can use it as part
6484 of a COMPONENT_REF for multiple inheritance. */
6485 layout_decl (TYPE_MAIN_DECL (t), 0);
6487 /* Now fix up any virtual base class types that we left lying
6488 around. We must get these done before we try to lay out the
6489 virtual function table. As a side-effect, this will remove the
6490 base subobject fields. */
6491 layout_virtual_bases (rli, empty_base_offsets);
6493 /* Make sure that empty classes are reflected in RLI at this
6494 point. */
6495 include_empty_classes(rli);
6497 /* Make sure not to create any structures with zero size. */
6498 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6499 place_field (rli,
6500 build_decl (input_location,
6501 FIELD_DECL, NULL_TREE, char_type_node));
6503 /* If this is a non-POD, declaring it packed makes a difference to how it
6504 can be used as a field; don't let finalize_record_size undo it. */
6505 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6506 rli->packed_maybe_necessary = true;
6508 /* Let the back end lay out the type. */
6509 finish_record_layout (rli, /*free_p=*/true);
6511 if (TYPE_SIZE_UNIT (t)
6512 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6513 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6514 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6515 error ("type %qT is too large", t);
6517 /* Warn about bases that can't be talked about due to ambiguity. */
6518 warn_about_ambiguous_bases (t);
6520 /* Now that we're done with layout, give the base fields the real types. */
6521 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6522 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6523 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6525 /* Clean up. */
6526 splay_tree_delete (empty_base_offsets);
6528 if (CLASSTYPE_EMPTY_P (t)
6529 && tree_int_cst_lt (sizeof_biggest_empty_class,
6530 TYPE_SIZE_UNIT (t)))
6531 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6534 /* Determine the "key method" for the class type indicated by TYPE,
6535 and set CLASSTYPE_KEY_METHOD accordingly. */
6537 void
6538 determine_key_method (tree type)
6540 tree method;
6542 if (TYPE_FOR_JAVA (type)
6543 || processing_template_decl
6544 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6545 || CLASSTYPE_INTERFACE_KNOWN (type))
6546 return;
6548 /* The key method is the first non-pure virtual function that is not
6549 inline at the point of class definition. On some targets the
6550 key function may not be inline; those targets should not call
6551 this function until the end of the translation unit. */
6552 for (method = TYPE_METHODS (type); method != NULL_TREE;
6553 method = DECL_CHAIN (method))
6554 if (TREE_CODE (method) == FUNCTION_DECL
6555 && DECL_VINDEX (method) != NULL_TREE
6556 && ! DECL_DECLARED_INLINE_P (method)
6557 && ! DECL_PURE_VIRTUAL_P (method))
6559 CLASSTYPE_KEY_METHOD (type) = method;
6560 break;
6563 return;
6567 /* Allocate and return an instance of struct sorted_fields_type with
6568 N fields. */
6570 static struct sorted_fields_type *
6571 sorted_fields_type_new (int n)
6573 struct sorted_fields_type *sft;
6574 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6575 + n * sizeof (tree));
6576 sft->len = n;
6578 return sft;
6582 /* Perform processing required when the definition of T (a class type)
6583 is complete. */
6585 void
6586 finish_struct_1 (tree t)
6588 tree x;
6589 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6590 tree virtuals = NULL_TREE;
6592 if (COMPLETE_TYPE_P (t))
6594 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6595 error ("redefinition of %q#T", t);
6596 popclass ();
6597 return;
6600 /* If this type was previously laid out as a forward reference,
6601 make sure we lay it out again. */
6602 TYPE_SIZE (t) = NULL_TREE;
6603 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6605 /* Make assumptions about the class; we'll reset the flags if
6606 necessary. */
6607 CLASSTYPE_EMPTY_P (t) = 1;
6608 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6609 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6610 CLASSTYPE_LITERAL_P (t) = true;
6612 /* Do end-of-class semantic processing: checking the validity of the
6613 bases and members and add implicitly generated methods. */
6614 check_bases_and_members (t);
6616 /* Find the key method. */
6617 if (TYPE_CONTAINS_VPTR_P (t))
6619 /* The Itanium C++ ABI permits the key method to be chosen when
6620 the class is defined -- even though the key method so
6621 selected may later turn out to be an inline function. On
6622 some systems (such as ARM Symbian OS) the key method cannot
6623 be determined until the end of the translation unit. On such
6624 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6625 will cause the class to be added to KEYED_CLASSES. Then, in
6626 finish_file we will determine the key method. */
6627 if (targetm.cxx.key_method_may_be_inline ())
6628 determine_key_method (t);
6630 /* If a polymorphic class has no key method, we may emit the vtable
6631 in every translation unit where the class definition appears. If
6632 we're devirtualizing, we can look into the vtable even if we
6633 aren't emitting it. */
6634 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6635 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6638 /* Layout the class itself. */
6639 layout_class_type (t, &virtuals);
6640 if (CLASSTYPE_AS_BASE (t) != t)
6641 /* We use the base type for trivial assignments, and hence it
6642 needs a mode. */
6643 compute_record_mode (CLASSTYPE_AS_BASE (t));
6645 virtuals = modify_all_vtables (t, nreverse (virtuals));
6647 /* If necessary, create the primary vtable for this class. */
6648 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6650 /* We must enter these virtuals into the table. */
6651 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6652 build_primary_vtable (NULL_TREE, t);
6653 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6654 /* Here we know enough to change the type of our virtual
6655 function table, but we will wait until later this function. */
6656 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6658 /* If we're warning about ABI tags, check the types of the new
6659 virtual functions. */
6660 if (warn_abi_tag)
6661 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6662 check_abi_tags (t, TREE_VALUE (v));
6665 if (TYPE_CONTAINS_VPTR_P (t))
6667 int vindex;
6668 tree fn;
6670 if (BINFO_VTABLE (TYPE_BINFO (t)))
6671 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6672 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6673 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6675 /* Add entries for virtual functions introduced by this class. */
6676 BINFO_VIRTUALS (TYPE_BINFO (t))
6677 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6679 /* Set DECL_VINDEX for all functions declared in this class. */
6680 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6682 fn = TREE_CHAIN (fn),
6683 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6684 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6686 tree fndecl = BV_FN (fn);
6688 if (DECL_THUNK_P (fndecl))
6689 /* A thunk. We should never be calling this entry directly
6690 from this vtable -- we'd use the entry for the non
6691 thunk base function. */
6692 DECL_VINDEX (fndecl) = NULL_TREE;
6693 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6694 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6698 finish_struct_bits (t);
6699 set_method_tm_attributes (t);
6700 if (flag_openmp || flag_openmp_simd)
6701 finish_omp_declare_simd_methods (t);
6703 /* Complete the rtl for any static member objects of the type we're
6704 working on. */
6705 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6706 if (VAR_P (x) && TREE_STATIC (x)
6707 && TREE_TYPE (x) != error_mark_node
6708 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6709 DECL_MODE (x) = TYPE_MODE (t);
6711 /* Done with FIELDS...now decide whether to sort these for
6712 faster lookups later.
6714 We use a small number because most searches fail (succeeding
6715 ultimately as the search bores through the inheritance
6716 hierarchy), and we want this failure to occur quickly. */
6718 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
6720 /* Complain if one of the field types requires lower visibility. */
6721 constrain_class_visibility (t);
6723 /* Make the rtl for any new vtables we have created, and unmark
6724 the base types we marked. */
6725 finish_vtbls (t);
6727 /* Build the VTT for T. */
6728 build_vtt (t);
6730 /* This warning does not make sense for Java classes, since they
6731 cannot have destructors. */
6732 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor
6733 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6734 && !CLASSTYPE_FINAL (t))
6735 warning (OPT_Wnon_virtual_dtor,
6736 "%q#T has virtual functions and accessible"
6737 " non-virtual destructor", t);
6739 complete_vars (t);
6741 if (warn_overloaded_virtual)
6742 warn_hidden (t);
6744 /* Class layout, assignment of virtual table slots, etc., is now
6745 complete. Give the back end a chance to tweak the visibility of
6746 the class or perform any other required target modifications. */
6747 targetm.cxx.adjust_class_at_definition (t);
6749 maybe_suppress_debug_info (t);
6751 if (flag_vtable_verify)
6752 vtv_save_class_info (t);
6754 dump_class_hierarchy (t);
6756 /* Finish debugging output for this type. */
6757 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6759 if (TYPE_TRANSPARENT_AGGR (t))
6761 tree field = first_field (t);
6762 if (field == NULL_TREE || error_operand_p (field))
6764 error ("type transparent %q#T does not have any fields", t);
6765 TYPE_TRANSPARENT_AGGR (t) = 0;
6767 else if (DECL_ARTIFICIAL (field))
6769 if (DECL_FIELD_IS_BASE (field))
6770 error ("type transparent class %qT has base classes", t);
6771 else
6773 gcc_checking_assert (DECL_VIRTUAL_P (field));
6774 error ("type transparent class %qT has virtual functions", t);
6776 TYPE_TRANSPARENT_AGGR (t) = 0;
6778 else if (TYPE_MODE (t) != DECL_MODE (field))
6780 error ("type transparent %q#T cannot be made transparent because "
6781 "the type of the first field has a different ABI from the "
6782 "class overall", t);
6783 TYPE_TRANSPARENT_AGGR (t) = 0;
6788 /* Insert FIELDS into T for the sorted case if the FIELDS count is
6789 equal to THRESHOLD or greater than THRESHOLD. */
6791 static void
6792 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
6794 int n_fields = count_fields (fields);
6795 if (n_fields >= threshold)
6797 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6798 add_fields_to_record_type (fields, field_vec, 0);
6799 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6800 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6804 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
6806 void
6807 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
6809 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
6810 if (sorted_fields)
6812 int i;
6813 int n_fields
6814 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
6815 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
6817 for (i = 0; i < sorted_fields->len; ++i)
6818 field_vec->elts[i] = sorted_fields->elts[i];
6820 add_enum_fields_to_record_type (enumtype, field_vec,
6821 sorted_fields->len);
6822 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
6823 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
6827 /* When T was built up, the member declarations were added in reverse
6828 order. Rearrange them to declaration order. */
6830 void
6831 unreverse_member_declarations (tree t)
6833 tree next;
6834 tree prev;
6835 tree x;
6837 /* The following lists are all in reverse order. Put them in
6838 declaration order now. */
6839 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
6840 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6842 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
6843 reverse order, so we can't just use nreverse. */
6844 prev = NULL_TREE;
6845 for (x = TYPE_FIELDS (t);
6846 x && TREE_CODE (x) != TYPE_DECL;
6847 x = next)
6849 next = DECL_CHAIN (x);
6850 DECL_CHAIN (x) = prev;
6851 prev = x;
6853 if (prev)
6855 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6856 if (prev)
6857 TYPE_FIELDS (t) = prev;
6861 tree
6862 finish_struct (tree t, tree attributes)
6864 location_t saved_loc = input_location;
6866 /* Now that we've got all the field declarations, reverse everything
6867 as necessary. */
6868 unreverse_member_declarations (t);
6870 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
6871 fixup_attribute_variants (t);
6873 /* Nadger the current location so that diagnostics point to the start of
6874 the struct, not the end. */
6875 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
6877 if (processing_template_decl)
6879 tree x;
6881 finish_struct_methods (t);
6882 TYPE_SIZE (t) = bitsize_zero_node;
6883 TYPE_SIZE_UNIT (t) = size_zero_node;
6885 /* We need to emit an error message if this type was used as a parameter
6886 and it is an abstract type, even if it is a template. We construct
6887 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
6888 account and we call complete_vars with this type, which will check
6889 the PARM_DECLS. Note that while the type is being defined,
6890 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
6891 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
6892 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
6893 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
6894 if (DECL_PURE_VIRTUAL_P (x))
6895 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
6896 complete_vars (t);
6897 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
6898 an enclosing scope is a template class, so that this function be
6899 found by lookup_fnfields_1 when the using declaration is not
6900 instantiated yet. */
6901 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6902 if (TREE_CODE (x) == USING_DECL)
6904 tree fn = strip_using_decl (x);
6905 if (is_overloaded_fn (fn))
6906 for (; fn; fn = OVL_NEXT (fn))
6907 add_method (t, OVL_CURRENT (fn), x);
6910 /* Remember current #pragma pack value. */
6911 TYPE_PRECISION (t) = maximum_field_alignment;
6913 /* Fix up any variants we've already built. */
6914 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
6916 TYPE_SIZE (x) = TYPE_SIZE (t);
6917 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
6918 TYPE_FIELDS (x) = TYPE_FIELDS (t);
6919 TYPE_METHODS (x) = TYPE_METHODS (t);
6922 else
6923 finish_struct_1 (t);
6925 if (is_std_init_list (t))
6927 /* People keep complaining that the compiler crashes on an invalid
6928 definition of initializer_list, so I guess we should explicitly
6929 reject it. What the compiler internals care about is that it's a
6930 template and has a pointer field followed by an integer field. */
6931 bool ok = false;
6932 if (processing_template_decl)
6934 tree f = next_initializable_field (TYPE_FIELDS (t));
6935 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
6937 f = next_initializable_field (DECL_CHAIN (f));
6938 if (f && same_type_p (TREE_TYPE (f), size_type_node))
6939 ok = true;
6942 if (!ok)
6943 fatal_error (input_location,
6944 "definition of std::initializer_list does not match "
6945 "#include <initializer_list>");
6948 input_location = saved_loc;
6950 TYPE_BEING_DEFINED (t) = 0;
6952 if (current_class_type)
6953 popclass ();
6954 else
6955 error ("trying to finish struct, but kicked out due to previous parse errors");
6957 if (processing_template_decl && at_function_scope_p ()
6958 /* Lambdas are defined by the LAMBDA_EXPR. */
6959 && !LAMBDA_TYPE_P (t))
6960 add_stmt (build_min (TAG_DEFN, t));
6962 return t;
6965 /* Hash table to avoid endless recursion when handling references. */
6966 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
6968 /* Return the dynamic type of INSTANCE, if known.
6969 Used to determine whether the virtual function table is needed
6970 or not.
6972 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
6973 of our knowledge of its type. *NONNULL should be initialized
6974 before this function is called. */
6976 static tree
6977 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
6979 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
6981 switch (TREE_CODE (instance))
6983 case INDIRECT_REF:
6984 if (POINTER_TYPE_P (TREE_TYPE (instance)))
6985 return NULL_TREE;
6986 else
6987 return RECUR (TREE_OPERAND (instance, 0));
6989 case CALL_EXPR:
6990 /* This is a call to a constructor, hence it's never zero. */
6991 if (TREE_HAS_CONSTRUCTOR (instance))
6993 if (nonnull)
6994 *nonnull = 1;
6995 return TREE_TYPE (instance);
6997 return NULL_TREE;
6999 case SAVE_EXPR:
7000 /* This is a call to a constructor, hence it's never zero. */
7001 if (TREE_HAS_CONSTRUCTOR (instance))
7003 if (nonnull)
7004 *nonnull = 1;
7005 return TREE_TYPE (instance);
7007 return RECUR (TREE_OPERAND (instance, 0));
7009 case POINTER_PLUS_EXPR:
7010 case PLUS_EXPR:
7011 case MINUS_EXPR:
7012 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7013 return RECUR (TREE_OPERAND (instance, 0));
7014 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7015 /* Propagate nonnull. */
7016 return RECUR (TREE_OPERAND (instance, 0));
7018 return NULL_TREE;
7020 CASE_CONVERT:
7021 return RECUR (TREE_OPERAND (instance, 0));
7023 case ADDR_EXPR:
7024 instance = TREE_OPERAND (instance, 0);
7025 if (nonnull)
7027 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7028 with a real object -- given &p->f, p can still be null. */
7029 tree t = get_base_address (instance);
7030 /* ??? Probably should check DECL_WEAK here. */
7031 if (t && DECL_P (t))
7032 *nonnull = 1;
7034 return RECUR (instance);
7036 case COMPONENT_REF:
7037 /* If this component is really a base class reference, then the field
7038 itself isn't definitive. */
7039 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7040 return RECUR (TREE_OPERAND (instance, 0));
7041 return RECUR (TREE_OPERAND (instance, 1));
7043 case VAR_DECL:
7044 case FIELD_DECL:
7045 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7046 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7048 if (nonnull)
7049 *nonnull = 1;
7050 return TREE_TYPE (TREE_TYPE (instance));
7052 /* fall through... */
7053 case TARGET_EXPR:
7054 case PARM_DECL:
7055 case RESULT_DECL:
7056 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7058 if (nonnull)
7059 *nonnull = 1;
7060 return TREE_TYPE (instance);
7062 else if (instance == current_class_ptr)
7064 if (nonnull)
7065 *nonnull = 1;
7067 /* if we're in a ctor or dtor, we know our type. If
7068 current_class_ptr is set but we aren't in a function, we're in
7069 an NSDMI (and therefore a constructor). */
7070 if (current_scope () != current_function_decl
7071 || (DECL_LANG_SPECIFIC (current_function_decl)
7072 && (DECL_CONSTRUCTOR_P (current_function_decl)
7073 || DECL_DESTRUCTOR_P (current_function_decl))))
7075 if (cdtorp)
7076 *cdtorp = 1;
7077 return TREE_TYPE (TREE_TYPE (instance));
7080 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7082 /* We only need one hash table because it is always left empty. */
7083 if (!fixed_type_or_null_ref_ht)
7084 fixed_type_or_null_ref_ht
7085 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7087 /* Reference variables should be references to objects. */
7088 if (nonnull)
7089 *nonnull = 1;
7091 /* Enter the INSTANCE in a table to prevent recursion; a
7092 variable's initializer may refer to the variable
7093 itself. */
7094 if (VAR_P (instance)
7095 && DECL_INITIAL (instance)
7096 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7097 && !fixed_type_or_null_ref_ht->find (instance))
7099 tree type;
7100 tree_node **slot;
7102 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7103 *slot = instance;
7104 type = RECUR (DECL_INITIAL (instance));
7105 fixed_type_or_null_ref_ht->remove_elt (instance);
7107 return type;
7110 return NULL_TREE;
7112 default:
7113 return NULL_TREE;
7115 #undef RECUR
7118 /* Return nonzero if the dynamic type of INSTANCE is known, and
7119 equivalent to the static type. We also handle the case where
7120 INSTANCE is really a pointer. Return negative if this is a
7121 ctor/dtor. There the dynamic type is known, but this might not be
7122 the most derived base of the original object, and hence virtual
7123 bases may not be laid out according to this type.
7125 Used to determine whether the virtual function table is needed
7126 or not.
7128 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7129 of our knowledge of its type. *NONNULL should be initialized
7130 before this function is called. */
7133 resolves_to_fixed_type_p (tree instance, int* nonnull)
7135 tree t = TREE_TYPE (instance);
7136 int cdtorp = 0;
7137 tree fixed;
7139 /* processing_template_decl can be false in a template if we're in
7140 instantiate_non_dependent_expr, but we still want to suppress
7141 this check. */
7142 if (in_template_function ())
7144 /* In a template we only care about the type of the result. */
7145 if (nonnull)
7146 *nonnull = true;
7147 return true;
7150 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7151 if (fixed == NULL_TREE)
7152 return 0;
7153 if (POINTER_TYPE_P (t))
7154 t = TREE_TYPE (t);
7155 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7156 return 0;
7157 return cdtorp ? -1 : 1;
7161 void
7162 init_class_processing (void)
7164 current_class_depth = 0;
7165 current_class_stack_size = 10;
7166 current_class_stack
7167 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7168 vec_alloc (local_classes, 8);
7169 sizeof_biggest_empty_class = size_zero_node;
7171 ridpointers[(int) RID_PUBLIC] = access_public_node;
7172 ridpointers[(int) RID_PRIVATE] = access_private_node;
7173 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7176 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7178 static void
7179 restore_class_cache (void)
7181 tree type;
7183 /* We are re-entering the same class we just left, so we don't
7184 have to search the whole inheritance matrix to find all the
7185 decls to bind again. Instead, we install the cached
7186 class_shadowed list and walk through it binding names. */
7187 push_binding_level (previous_class_level);
7188 class_binding_level = previous_class_level;
7189 /* Restore IDENTIFIER_TYPE_VALUE. */
7190 for (type = class_binding_level->type_shadowed;
7191 type;
7192 type = TREE_CHAIN (type))
7193 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7196 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7197 appropriate for TYPE.
7199 So that we may avoid calls to lookup_name, we cache the _TYPE
7200 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7202 For multiple inheritance, we perform a two-pass depth-first search
7203 of the type lattice. */
7205 void
7206 pushclass (tree type)
7208 class_stack_node_t csn;
7210 type = TYPE_MAIN_VARIANT (type);
7212 /* Make sure there is enough room for the new entry on the stack. */
7213 if (current_class_depth + 1 >= current_class_stack_size)
7215 current_class_stack_size *= 2;
7216 current_class_stack
7217 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7218 current_class_stack_size);
7221 /* Insert a new entry on the class stack. */
7222 csn = current_class_stack + current_class_depth;
7223 csn->name = current_class_name;
7224 csn->type = current_class_type;
7225 csn->access = current_access_specifier;
7226 csn->names_used = 0;
7227 csn->hidden = 0;
7228 current_class_depth++;
7230 /* Now set up the new type. */
7231 current_class_name = TYPE_NAME (type);
7232 if (TREE_CODE (current_class_name) == TYPE_DECL)
7233 current_class_name = DECL_NAME (current_class_name);
7234 current_class_type = type;
7236 /* By default, things in classes are private, while things in
7237 structures or unions are public. */
7238 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7239 ? access_private_node
7240 : access_public_node);
7242 if (previous_class_level
7243 && type != previous_class_level->this_entity
7244 && current_class_depth == 1)
7246 /* Forcibly remove any old class remnants. */
7247 invalidate_class_lookup_cache ();
7250 if (!previous_class_level
7251 || type != previous_class_level->this_entity
7252 || current_class_depth > 1)
7253 pushlevel_class ();
7254 else
7255 restore_class_cache ();
7258 /* When we exit a toplevel class scope, we save its binding level so
7259 that we can restore it quickly. Here, we've entered some other
7260 class, so we must invalidate our cache. */
7262 void
7263 invalidate_class_lookup_cache (void)
7265 previous_class_level = NULL;
7268 /* Get out of the current class scope. If we were in a class scope
7269 previously, that is the one popped to. */
7271 void
7272 popclass (void)
7274 poplevel_class ();
7276 current_class_depth--;
7277 current_class_name = current_class_stack[current_class_depth].name;
7278 current_class_type = current_class_stack[current_class_depth].type;
7279 current_access_specifier = current_class_stack[current_class_depth].access;
7280 if (current_class_stack[current_class_depth].names_used)
7281 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7284 /* Mark the top of the class stack as hidden. */
7286 void
7287 push_class_stack (void)
7289 if (current_class_depth)
7290 ++current_class_stack[current_class_depth - 1].hidden;
7293 /* Mark the top of the class stack as un-hidden. */
7295 void
7296 pop_class_stack (void)
7298 if (current_class_depth)
7299 --current_class_stack[current_class_depth - 1].hidden;
7302 /* Returns 1 if the class type currently being defined is either T or
7303 a nested type of T. Returns the type from the current_class_stack,
7304 which might be equivalent to but not equal to T in case of
7305 constrained partial specializations. */
7307 tree
7308 currently_open_class (tree t)
7310 int i;
7312 if (!CLASS_TYPE_P (t))
7313 return NULL_TREE;
7315 t = TYPE_MAIN_VARIANT (t);
7317 /* We start looking from 1 because entry 0 is from global scope,
7318 and has no type. */
7319 for (i = current_class_depth; i > 0; --i)
7321 tree c;
7322 if (i == current_class_depth)
7323 c = current_class_type;
7324 else
7326 if (current_class_stack[i].hidden)
7327 break;
7328 c = current_class_stack[i].type;
7330 if (!c)
7331 continue;
7332 if (same_type_p (c, t))
7333 return c;
7335 return NULL_TREE;
7338 /* If either current_class_type or one of its enclosing classes are derived
7339 from T, return the appropriate type. Used to determine how we found
7340 something via unqualified lookup. */
7342 tree
7343 currently_open_derived_class (tree t)
7345 int i;
7347 /* The bases of a dependent type are unknown. */
7348 if (dependent_type_p (t))
7349 return NULL_TREE;
7351 if (!current_class_type)
7352 return NULL_TREE;
7354 if (DERIVED_FROM_P (t, current_class_type))
7355 return current_class_type;
7357 for (i = current_class_depth - 1; i > 0; --i)
7359 if (current_class_stack[i].hidden)
7360 break;
7361 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7362 return current_class_stack[i].type;
7365 return NULL_TREE;
7368 /* Return the outermost enclosing class type that is still open, or
7369 NULL_TREE. */
7371 tree
7372 outermost_open_class (void)
7374 if (!current_class_type)
7375 return NULL_TREE;
7376 tree r = NULL_TREE;
7377 if (TYPE_BEING_DEFINED (current_class_type))
7378 r = current_class_type;
7379 for (int i = current_class_depth - 1; i > 0; --i)
7381 if (current_class_stack[i].hidden)
7382 break;
7383 tree t = current_class_stack[i].type;
7384 if (!TYPE_BEING_DEFINED (t))
7385 break;
7386 r = t;
7388 return r;
7391 /* Returns the innermost class type which is not a lambda closure type. */
7393 tree
7394 current_nonlambda_class_type (void)
7396 int i;
7398 /* We start looking from 1 because entry 0 is from global scope,
7399 and has no type. */
7400 for (i = current_class_depth; i > 0; --i)
7402 tree c;
7403 if (i == current_class_depth)
7404 c = current_class_type;
7405 else
7407 if (current_class_stack[i].hidden)
7408 break;
7409 c = current_class_stack[i].type;
7411 if (!c)
7412 continue;
7413 if (!LAMBDA_TYPE_P (c))
7414 return c;
7416 return NULL_TREE;
7419 /* When entering a class scope, all enclosing class scopes' names with
7420 static meaning (static variables, static functions, types and
7421 enumerators) have to be visible. This recursive function calls
7422 pushclass for all enclosing class contexts until global or a local
7423 scope is reached. TYPE is the enclosed class. */
7425 void
7426 push_nested_class (tree type)
7428 /* A namespace might be passed in error cases, like A::B:C. */
7429 if (type == NULL_TREE
7430 || !CLASS_TYPE_P (type))
7431 return;
7433 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7435 pushclass (type);
7438 /* Undoes a push_nested_class call. */
7440 void
7441 pop_nested_class (void)
7443 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7445 popclass ();
7446 if (context && CLASS_TYPE_P (context))
7447 pop_nested_class ();
7450 /* Returns the number of extern "LANG" blocks we are nested within. */
7453 current_lang_depth (void)
7455 return vec_safe_length (current_lang_base);
7458 /* Set global variables CURRENT_LANG_NAME to appropriate value
7459 so that behavior of name-mangling machinery is correct. */
7461 void
7462 push_lang_context (tree name)
7464 vec_safe_push (current_lang_base, current_lang_name);
7466 if (name == lang_name_cplusplus)
7468 current_lang_name = name;
7470 else if (name == lang_name_java)
7472 current_lang_name = name;
7473 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
7474 (See record_builtin_java_type in decl.c.) However, that causes
7475 incorrect debug entries if these types are actually used.
7476 So we re-enable debug output after extern "Java". */
7477 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
7478 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
7479 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
7480 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
7481 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
7482 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
7483 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
7484 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
7486 else if (name == lang_name_c)
7488 current_lang_name = name;
7490 else
7491 error ("language string %<\"%E\"%> not recognized", name);
7494 /* Get out of the current language scope. */
7496 void
7497 pop_lang_context (void)
7499 current_lang_name = current_lang_base->pop ();
7502 /* Type instantiation routines. */
7504 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7505 matches the TARGET_TYPE. If there is no satisfactory match, return
7506 error_mark_node, and issue an error & warning messages under
7507 control of FLAGS. Permit pointers to member function if FLAGS
7508 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7509 a template-id, and EXPLICIT_TARGS are the explicitly provided
7510 template arguments.
7512 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7513 is the base path used to reference those member functions. If
7514 the address is resolved to a member function, access checks will be
7515 performed and errors issued if appropriate. */
7517 static tree
7518 resolve_address_of_overloaded_function (tree target_type,
7519 tree overload,
7520 tsubst_flags_t complain,
7521 bool template_only,
7522 tree explicit_targs,
7523 tree access_path)
7525 /* Here's what the standard says:
7527 [over.over]
7529 If the name is a function template, template argument deduction
7530 is done, and if the argument deduction succeeds, the deduced
7531 arguments are used to generate a single template function, which
7532 is added to the set of overloaded functions considered.
7534 Non-member functions and static member functions match targets of
7535 type "pointer-to-function" or "reference-to-function." Nonstatic
7536 member functions match targets of type "pointer-to-member
7537 function;" the function type of the pointer to member is used to
7538 select the member function from the set of overloaded member
7539 functions. If a nonstatic member function is selected, the
7540 reference to the overloaded function name is required to have the
7541 form of a pointer to member as described in 5.3.1.
7543 If more than one function is selected, any template functions in
7544 the set are eliminated if the set also contains a non-template
7545 function, and any given template function is eliminated if the
7546 set contains a second template function that is more specialized
7547 than the first according to the partial ordering rules 14.5.5.2.
7548 After such eliminations, if any, there shall remain exactly one
7549 selected function. */
7551 int is_ptrmem = 0;
7552 /* We store the matches in a TREE_LIST rooted here. The functions
7553 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7554 interoperability with most_specialized_instantiation. */
7555 tree matches = NULL_TREE;
7556 tree fn;
7557 tree target_fn_type;
7559 /* By the time we get here, we should be seeing only real
7560 pointer-to-member types, not the internal POINTER_TYPE to
7561 METHOD_TYPE representation. */
7562 gcc_assert (!TYPE_PTR_P (target_type)
7563 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7565 gcc_assert (is_overloaded_fn (overload));
7567 /* Check that the TARGET_TYPE is reasonable. */
7568 if (TYPE_PTRFN_P (target_type)
7569 || TYPE_REFFN_P (target_type))
7570 /* This is OK. */;
7571 else if (TYPE_PTRMEMFUNC_P (target_type))
7572 /* This is OK, too. */
7573 is_ptrmem = 1;
7574 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7575 /* This is OK, too. This comes from a conversion to reference
7576 type. */
7577 target_type = build_reference_type (target_type);
7578 else
7580 if (complain & tf_error)
7581 error ("cannot resolve overloaded function %qD based on"
7582 " conversion to type %qT",
7583 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7584 return error_mark_node;
7587 /* Non-member functions and static member functions match targets of type
7588 "pointer-to-function" or "reference-to-function." Nonstatic member
7589 functions match targets of type "pointer-to-member-function;" the
7590 function type of the pointer to member is used to select the member
7591 function from the set of overloaded member functions.
7593 So figure out the FUNCTION_TYPE that we want to match against. */
7594 target_fn_type = static_fn_type (target_type);
7596 /* If we can find a non-template function that matches, we can just
7597 use it. There's no point in generating template instantiations
7598 if we're just going to throw them out anyhow. But, of course, we
7599 can only do this when we don't *need* a template function. */
7600 if (!template_only)
7602 tree fns;
7604 for (fns = overload; fns; fns = OVL_NEXT (fns))
7606 tree fn = OVL_CURRENT (fns);
7608 if (TREE_CODE (fn) == TEMPLATE_DECL)
7609 /* We're not looking for templates just yet. */
7610 continue;
7612 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7613 != is_ptrmem)
7614 /* We're looking for a non-static member, and this isn't
7615 one, or vice versa. */
7616 continue;
7618 /* Ignore functions which haven't been explicitly
7619 declared. */
7620 if (DECL_ANTICIPATED (fn))
7621 continue;
7623 /* See if there's a match. */
7624 tree fntype = static_fn_type (fn);
7625 if (same_type_p (target_fn_type, fntype)
7626 || can_convert_tx_safety (target_fn_type, fntype))
7627 matches = tree_cons (fn, NULL_TREE, matches);
7631 /* Now, if we've already got a match (or matches), there's no need
7632 to proceed to the template functions. But, if we don't have a
7633 match we need to look at them, too. */
7634 if (!matches)
7636 tree target_arg_types;
7637 tree target_ret_type;
7638 tree fns;
7639 tree *args;
7640 unsigned int nargs, ia;
7641 tree arg;
7643 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7644 target_ret_type = TREE_TYPE (target_fn_type);
7646 nargs = list_length (target_arg_types);
7647 args = XALLOCAVEC (tree, nargs);
7648 for (arg = target_arg_types, ia = 0;
7649 arg != NULL_TREE && arg != void_list_node;
7650 arg = TREE_CHAIN (arg), ++ia)
7651 args[ia] = TREE_VALUE (arg);
7652 nargs = ia;
7654 for (fns = overload; fns; fns = OVL_NEXT (fns))
7656 tree fn = OVL_CURRENT (fns);
7657 tree instantiation;
7658 tree targs;
7660 if (TREE_CODE (fn) != TEMPLATE_DECL)
7661 /* We're only looking for templates. */
7662 continue;
7664 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7665 != is_ptrmem)
7666 /* We're not looking for a non-static member, and this is
7667 one, or vice versa. */
7668 continue;
7670 tree ret = target_ret_type;
7672 /* If the template has a deduced return type, don't expose it to
7673 template argument deduction. */
7674 if (undeduced_auto_decl (fn))
7675 ret = NULL_TREE;
7677 /* Try to do argument deduction. */
7678 targs = make_tree_vec (DECL_NTPARMS (fn));
7679 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7680 nargs, ret,
7681 DEDUCE_EXACT, LOOKUP_NORMAL,
7682 false, false);
7683 if (instantiation == error_mark_node)
7684 /* Instantiation failed. */
7685 continue;
7687 /* Constraints must be satisfied. This is done before
7688 return type deduction since that instantiates the
7689 function. */
7690 if (flag_concepts && !constraints_satisfied_p (instantiation))
7691 continue;
7693 /* And now force instantiation to do return type deduction. */
7694 if (undeduced_auto_decl (instantiation))
7696 ++function_depth;
7697 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7698 --function_depth;
7700 require_deduced_type (instantiation);
7703 /* See if there's a match. */
7704 tree fntype = static_fn_type (instantiation);
7705 if (same_type_p (target_fn_type, fntype)
7706 || can_convert_tx_safety (target_fn_type, fntype))
7707 matches = tree_cons (instantiation, fn, matches);
7710 /* Now, remove all but the most specialized of the matches. */
7711 if (matches)
7713 tree match = most_specialized_instantiation (matches);
7715 if (match != error_mark_node)
7716 matches = tree_cons (TREE_PURPOSE (match),
7717 NULL_TREE,
7718 NULL_TREE);
7722 /* Now we should have exactly one function in MATCHES. */
7723 if (matches == NULL_TREE)
7725 /* There were *no* matches. */
7726 if (complain & tf_error)
7728 error ("no matches converting function %qD to type %q#T",
7729 DECL_NAME (OVL_CURRENT (overload)),
7730 target_type);
7732 print_candidates (overload);
7734 return error_mark_node;
7736 else if (TREE_CHAIN (matches))
7738 /* There were too many matches. First check if they're all
7739 the same function. */
7740 tree match = NULL_TREE;
7742 fn = TREE_PURPOSE (matches);
7744 /* For multi-versioned functions, more than one match is just fine and
7745 decls_match will return false as they are different. */
7746 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7747 if (!decls_match (fn, TREE_PURPOSE (match))
7748 && !targetm.target_option.function_versions
7749 (fn, TREE_PURPOSE (match)))
7750 break;
7752 if (match)
7754 if (complain & tf_error)
7756 error ("converting overloaded function %qD to type %q#T is ambiguous",
7757 DECL_NAME (OVL_FUNCTION (overload)),
7758 target_type);
7760 /* Since print_candidates expects the functions in the
7761 TREE_VALUE slot, we flip them here. */
7762 for (match = matches; match; match = TREE_CHAIN (match))
7763 TREE_VALUE (match) = TREE_PURPOSE (match);
7765 print_candidates (matches);
7768 return error_mark_node;
7772 /* Good, exactly one match. Now, convert it to the correct type. */
7773 fn = TREE_PURPOSE (matches);
7775 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7776 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7778 static int explained;
7780 if (!(complain & tf_error))
7781 return error_mark_node;
7783 permerror (input_location, "assuming pointer to member %qD", fn);
7784 if (!explained)
7786 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7787 explained = 1;
7791 /* If a pointer to a function that is multi-versioned is requested, the
7792 pointer to the dispatcher function is returned instead. This works
7793 well because indirectly calling the function will dispatch the right
7794 function version at run-time. */
7795 if (DECL_FUNCTION_VERSIONED (fn))
7797 fn = get_function_version_dispatcher (fn);
7798 if (fn == NULL)
7799 return error_mark_node;
7800 /* Mark all the versions corresponding to the dispatcher as used. */
7801 if (!(complain & tf_conv))
7802 mark_versions_used (fn);
7805 /* If we're doing overload resolution purely for the purpose of
7806 determining conversion sequences, we should not consider the
7807 function used. If this conversion sequence is selected, the
7808 function will be marked as used at this point. */
7809 if (!(complain & tf_conv))
7811 /* Make =delete work with SFINAE. */
7812 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7813 return error_mark_node;
7814 if (!mark_used (fn, complain) && !(complain & tf_error))
7815 return error_mark_node;
7818 /* We could not check access to member functions when this
7819 expression was originally created since we did not know at that
7820 time to which function the expression referred. */
7821 if (DECL_FUNCTION_MEMBER_P (fn))
7823 gcc_assert (access_path);
7824 perform_or_defer_access_check (access_path, fn, fn, complain);
7827 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7828 return cp_build_addr_expr (fn, complain);
7829 else
7831 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7832 will mark the function as addressed, but here we must do it
7833 explicitly. */
7834 cxx_mark_addressable (fn);
7836 return fn;
7840 /* This function will instantiate the type of the expression given in
7841 RHS to match the type of LHSTYPE. If errors exist, then return
7842 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7843 we complain on errors. If we are not complaining, never modify rhs,
7844 as overload resolution wants to try many possible instantiations, in
7845 the hope that at least one will work.
7847 For non-recursive calls, LHSTYPE should be a function, pointer to
7848 function, or a pointer to member function. */
7850 tree
7851 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7853 tsubst_flags_t complain_in = complain;
7854 tree access_path = NULL_TREE;
7856 complain &= ~tf_ptrmem_ok;
7858 if (lhstype == unknown_type_node)
7860 if (complain & tf_error)
7861 error ("not enough type information");
7862 return error_mark_node;
7865 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7867 tree fntype = non_reference (lhstype);
7868 if (same_type_p (fntype, TREE_TYPE (rhs)))
7869 return rhs;
7870 if (flag_ms_extensions
7871 && TYPE_PTRMEMFUNC_P (fntype)
7872 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7873 /* Microsoft allows `A::f' to be resolved to a
7874 pointer-to-member. */
7876 else
7878 if (complain & tf_error)
7879 error ("cannot convert %qE from type %qT to type %qT",
7880 rhs, TREE_TYPE (rhs), fntype);
7881 return error_mark_node;
7885 if (BASELINK_P (rhs))
7887 access_path = BASELINK_ACCESS_BINFO (rhs);
7888 rhs = BASELINK_FUNCTIONS (rhs);
7891 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7892 deduce any type information. */
7893 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
7895 if (complain & tf_error)
7896 error ("not enough type information");
7897 return error_mark_node;
7900 /* There only a few kinds of expressions that may have a type
7901 dependent on overload resolution. */
7902 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
7903 || TREE_CODE (rhs) == COMPONENT_REF
7904 || is_overloaded_fn (rhs)
7905 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
7907 /* This should really only be used when attempting to distinguish
7908 what sort of a pointer to function we have. For now, any
7909 arithmetic operation which is not supported on pointers
7910 is rejected as an error. */
7912 switch (TREE_CODE (rhs))
7914 case COMPONENT_REF:
7916 tree member = TREE_OPERAND (rhs, 1);
7918 member = instantiate_type (lhstype, member, complain);
7919 if (member != error_mark_node
7920 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
7921 /* Do not lose object's side effects. */
7922 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
7923 TREE_OPERAND (rhs, 0), member);
7924 return member;
7927 case OFFSET_REF:
7928 rhs = TREE_OPERAND (rhs, 1);
7929 if (BASELINK_P (rhs))
7930 return instantiate_type (lhstype, rhs, complain_in);
7932 /* This can happen if we are forming a pointer-to-member for a
7933 member template. */
7934 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
7936 /* Fall through. */
7938 case TEMPLATE_ID_EXPR:
7940 tree fns = TREE_OPERAND (rhs, 0);
7941 tree args = TREE_OPERAND (rhs, 1);
7943 return
7944 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
7945 /*template_only=*/true,
7946 args, access_path);
7949 case OVERLOAD:
7950 case FUNCTION_DECL:
7951 return
7952 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
7953 /*template_only=*/false,
7954 /*explicit_targs=*/NULL_TREE,
7955 access_path);
7957 case ADDR_EXPR:
7959 if (PTRMEM_OK_P (rhs))
7960 complain |= tf_ptrmem_ok;
7962 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
7965 case ERROR_MARK:
7966 return error_mark_node;
7968 default:
7969 gcc_unreachable ();
7971 return error_mark_node;
7974 /* Return the name of the virtual function pointer field
7975 (as an IDENTIFIER_NODE) for the given TYPE. Note that
7976 this may have to look back through base types to find the
7977 ultimate field name. (For single inheritance, these could
7978 all be the same name. Who knows for multiple inheritance). */
7980 static tree
7981 get_vfield_name (tree type)
7983 tree binfo, base_binfo;
7984 char *buf;
7986 for (binfo = TYPE_BINFO (type);
7987 BINFO_N_BASE_BINFOS (binfo);
7988 binfo = base_binfo)
7990 base_binfo = BINFO_BASE_BINFO (binfo, 0);
7992 if (BINFO_VIRTUAL_P (base_binfo)
7993 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
7994 break;
7997 type = BINFO_TYPE (binfo);
7998 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
7999 + TYPE_NAME_LENGTH (type) + 2);
8000 sprintf (buf, VFIELD_NAME_FORMAT,
8001 IDENTIFIER_POINTER (constructor_name (type)));
8002 return get_identifier (buf);
8005 void
8006 print_class_statistics (void)
8008 if (! GATHER_STATISTICS)
8009 return;
8011 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8012 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8013 if (n_vtables)
8015 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8016 n_vtables, n_vtable_searches);
8017 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8018 n_vtable_entries, n_vtable_elems);
8022 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8023 according to [class]:
8024 The class-name is also inserted
8025 into the scope of the class itself. For purposes of access checking,
8026 the inserted class name is treated as if it were a public member name. */
8028 void
8029 build_self_reference (void)
8031 tree name = constructor_name (current_class_type);
8032 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8033 tree saved_cas;
8035 DECL_NONLOCAL (value) = 1;
8036 DECL_CONTEXT (value) = current_class_type;
8037 DECL_ARTIFICIAL (value) = 1;
8038 SET_DECL_SELF_REFERENCE_P (value);
8039 set_underlying_type (value);
8041 if (processing_template_decl)
8042 value = push_template_decl (value);
8044 saved_cas = current_access_specifier;
8045 current_access_specifier = access_public_node;
8046 finish_member_declaration (value);
8047 current_access_specifier = saved_cas;
8050 /* Returns 1 if TYPE contains only padding bytes. */
8053 is_empty_class (tree type)
8055 if (type == error_mark_node)
8056 return 0;
8058 if (! CLASS_TYPE_P (type))
8059 return 0;
8061 return CLASSTYPE_EMPTY_P (type);
8064 /* Returns true if TYPE contains no actual data, just various
8065 possible combinations of empty classes and possibly a vptr. */
8067 bool
8068 is_really_empty_class (tree type)
8070 if (CLASS_TYPE_P (type))
8072 tree field;
8073 tree binfo;
8074 tree base_binfo;
8075 int i;
8077 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8078 out, but we'd like to be able to check this before then. */
8079 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8080 return true;
8082 for (binfo = TYPE_BINFO (type), i = 0;
8083 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8084 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8085 return false;
8086 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8087 if (TREE_CODE (field) == FIELD_DECL
8088 && !DECL_ARTIFICIAL (field)
8089 && !is_really_empty_class (TREE_TYPE (field)))
8090 return false;
8091 return true;
8093 else if (TREE_CODE (type) == ARRAY_TYPE)
8094 return is_really_empty_class (TREE_TYPE (type));
8095 return false;
8098 /* Note that NAME was looked up while the current class was being
8099 defined and that the result of that lookup was DECL. */
8101 void
8102 maybe_note_name_used_in_class (tree name, tree decl)
8104 splay_tree names_used;
8106 /* If we're not defining a class, there's nothing to do. */
8107 if (!(innermost_scope_kind() == sk_class
8108 && TYPE_BEING_DEFINED (current_class_type)
8109 && !LAMBDA_TYPE_P (current_class_type)))
8110 return;
8112 /* If there's already a binding for this NAME, then we don't have
8113 anything to worry about. */
8114 if (lookup_member (current_class_type, name,
8115 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8116 return;
8118 if (!current_class_stack[current_class_depth - 1].names_used)
8119 current_class_stack[current_class_depth - 1].names_used
8120 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8121 names_used = current_class_stack[current_class_depth - 1].names_used;
8123 splay_tree_insert (names_used,
8124 (splay_tree_key) name,
8125 (splay_tree_value) decl);
8128 /* Note that NAME was declared (as DECL) in the current class. Check
8129 to see that the declaration is valid. */
8131 void
8132 note_name_declared_in_class (tree name, tree decl)
8134 splay_tree names_used;
8135 splay_tree_node n;
8137 /* Look to see if we ever used this name. */
8138 names_used
8139 = current_class_stack[current_class_depth - 1].names_used;
8140 if (!names_used)
8141 return;
8142 /* The C language allows members to be declared with a type of the same
8143 name, and the C++ standard says this diagnostic is not required. So
8144 allow it in extern "C" blocks unless predantic is specified.
8145 Allow it in all cases if -ms-extensions is specified. */
8146 if ((!pedantic && current_lang_name == lang_name_c)
8147 || flag_ms_extensions)
8148 return;
8149 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8150 if (n)
8152 /* [basic.scope.class]
8154 A name N used in a class S shall refer to the same declaration
8155 in its context and when re-evaluated in the completed scope of
8156 S. */
8157 permerror (input_location, "declaration of %q#D", decl);
8158 permerror (location_of ((tree) n->value),
8159 "changes meaning of %qD from %q#D",
8160 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8164 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8165 Secondary vtables are merged with primary vtables; this function
8166 will return the VAR_DECL for the primary vtable. */
8168 tree
8169 get_vtbl_decl_for_binfo (tree binfo)
8171 tree decl;
8173 decl = BINFO_VTABLE (binfo);
8174 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8176 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8177 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8179 if (decl)
8180 gcc_assert (VAR_P (decl));
8181 return decl;
8185 /* Returns the binfo for the primary base of BINFO. If the resulting
8186 BINFO is a virtual base, and it is inherited elsewhere in the
8187 hierarchy, then the returned binfo might not be the primary base of
8188 BINFO in the complete object. Check BINFO_PRIMARY_P or
8189 BINFO_LOST_PRIMARY_P to be sure. */
8191 static tree
8192 get_primary_binfo (tree binfo)
8194 tree primary_base;
8196 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8197 if (!primary_base)
8198 return NULL_TREE;
8200 return copied_binfo (primary_base, binfo);
8203 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8205 static int
8206 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8208 if (!indented_p)
8209 fprintf (stream, "%*s", indent, "");
8210 return 1;
8213 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8214 INDENT should be zero when called from the top level; it is
8215 incremented recursively. IGO indicates the next expected BINFO in
8216 inheritance graph ordering. */
8218 static tree
8219 dump_class_hierarchy_r (FILE *stream,
8220 int flags,
8221 tree binfo,
8222 tree igo,
8223 int indent)
8225 int indented = 0;
8226 tree base_binfo;
8227 int i;
8229 indented = maybe_indent_hierarchy (stream, indent, 0);
8230 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8231 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8232 (HOST_WIDE_INT) (uintptr_t) binfo);
8233 if (binfo != igo)
8235 fprintf (stream, "alternative-path\n");
8236 return igo;
8238 igo = TREE_CHAIN (binfo);
8240 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8241 tree_to_shwi (BINFO_OFFSET (binfo)));
8242 if (is_empty_class (BINFO_TYPE (binfo)))
8243 fprintf (stream, " empty");
8244 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8245 fprintf (stream, " nearly-empty");
8246 if (BINFO_VIRTUAL_P (binfo))
8247 fprintf (stream, " virtual");
8248 fprintf (stream, "\n");
8250 indented = 0;
8251 if (BINFO_PRIMARY_P (binfo))
8253 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8254 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8255 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8256 TFF_PLAIN_IDENTIFIER),
8257 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8259 if (BINFO_LOST_PRIMARY_P (binfo))
8261 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8262 fprintf (stream, " lost-primary");
8264 if (indented)
8265 fprintf (stream, "\n");
8267 if (!(flags & TDF_SLIM))
8269 int indented = 0;
8271 if (BINFO_SUBVTT_INDEX (binfo))
8273 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8274 fprintf (stream, " subvttidx=%s",
8275 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8276 TFF_PLAIN_IDENTIFIER));
8278 if (BINFO_VPTR_INDEX (binfo))
8280 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8281 fprintf (stream, " vptridx=%s",
8282 expr_as_string (BINFO_VPTR_INDEX (binfo),
8283 TFF_PLAIN_IDENTIFIER));
8285 if (BINFO_VPTR_FIELD (binfo))
8287 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8288 fprintf (stream, " vbaseoffset=%s",
8289 expr_as_string (BINFO_VPTR_FIELD (binfo),
8290 TFF_PLAIN_IDENTIFIER));
8292 if (BINFO_VTABLE (binfo))
8294 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8295 fprintf (stream, " vptr=%s",
8296 expr_as_string (BINFO_VTABLE (binfo),
8297 TFF_PLAIN_IDENTIFIER));
8300 if (indented)
8301 fprintf (stream, "\n");
8304 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8305 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8307 return igo;
8310 /* Dump the BINFO hierarchy for T. */
8312 static void
8313 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8315 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8316 fprintf (stream, " size=%lu align=%lu\n",
8317 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8318 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8319 fprintf (stream, " base size=%lu base align=%lu\n",
8320 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8321 / BITS_PER_UNIT),
8322 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8323 / BITS_PER_UNIT));
8324 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8325 fprintf (stream, "\n");
8328 /* Debug interface to hierarchy dumping. */
8330 void
8331 debug_class (tree t)
8333 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8336 static void
8337 dump_class_hierarchy (tree t)
8339 int flags;
8340 FILE *stream = get_dump_info (TDI_class, &flags);
8342 if (stream)
8344 dump_class_hierarchy_1 (stream, flags, t);
8348 static void
8349 dump_array (FILE * stream, tree decl)
8351 tree value;
8352 unsigned HOST_WIDE_INT ix;
8353 HOST_WIDE_INT elt;
8354 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8356 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8357 / BITS_PER_UNIT);
8358 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8359 fprintf (stream, " %s entries",
8360 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8361 TFF_PLAIN_IDENTIFIER));
8362 fprintf (stream, "\n");
8364 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8365 ix, value)
8366 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8367 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8370 static void
8371 dump_vtable (tree t, tree binfo, tree vtable)
8373 int flags;
8374 FILE *stream = get_dump_info (TDI_class, &flags);
8376 if (!stream)
8377 return;
8379 if (!(flags & TDF_SLIM))
8381 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8383 fprintf (stream, "%s for %s",
8384 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8385 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8386 if (ctor_vtbl_p)
8388 if (!BINFO_VIRTUAL_P (binfo))
8389 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8390 (HOST_WIDE_INT) (uintptr_t) binfo);
8391 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8393 fprintf (stream, "\n");
8394 dump_array (stream, vtable);
8395 fprintf (stream, "\n");
8399 static void
8400 dump_vtt (tree t, tree vtt)
8402 int flags;
8403 FILE *stream = get_dump_info (TDI_class, &flags);
8405 if (!stream)
8406 return;
8408 if (!(flags & TDF_SLIM))
8410 fprintf (stream, "VTT for %s\n",
8411 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8412 dump_array (stream, vtt);
8413 fprintf (stream, "\n");
8417 /* Dump a function or thunk and its thunkees. */
8419 static void
8420 dump_thunk (FILE *stream, int indent, tree thunk)
8422 static const char spaces[] = " ";
8423 tree name = DECL_NAME (thunk);
8424 tree thunks;
8426 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8427 (void *)thunk,
8428 !DECL_THUNK_P (thunk) ? "function"
8429 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8430 name ? IDENTIFIER_POINTER (name) : "<unset>");
8431 if (DECL_THUNK_P (thunk))
8433 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8434 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8436 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8437 if (!virtual_adjust)
8438 /*NOP*/;
8439 else if (DECL_THIS_THUNK_P (thunk))
8440 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8441 tree_to_shwi (virtual_adjust));
8442 else
8443 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8444 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8445 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8446 if (THUNK_ALIAS (thunk))
8447 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8449 fprintf (stream, "\n");
8450 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8451 dump_thunk (stream, indent + 2, thunks);
8454 /* Dump the thunks for FN. */
8456 void
8457 debug_thunks (tree fn)
8459 dump_thunk (stderr, 0, fn);
8462 /* Virtual function table initialization. */
8464 /* Create all the necessary vtables for T and its base classes. */
8466 static void
8467 finish_vtbls (tree t)
8469 tree vbase;
8470 vec<constructor_elt, va_gc> *v = NULL;
8471 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8473 /* We lay out the primary and secondary vtables in one contiguous
8474 vtable. The primary vtable is first, followed by the non-virtual
8475 secondary vtables in inheritance graph order. */
8476 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8477 vtable, t, &v);
8479 /* Then come the virtual bases, also in inheritance graph order. */
8480 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8482 if (!BINFO_VIRTUAL_P (vbase))
8483 continue;
8484 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8487 if (BINFO_VTABLE (TYPE_BINFO (t)))
8488 initialize_vtable (TYPE_BINFO (t), v);
8491 /* Initialize the vtable for BINFO with the INITS. */
8493 static void
8494 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8496 tree decl;
8498 layout_vtable_decl (binfo, vec_safe_length (inits));
8499 decl = get_vtbl_decl_for_binfo (binfo);
8500 initialize_artificial_var (decl, inits);
8501 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8504 /* Build the VTT (virtual table table) for T.
8505 A class requires a VTT if it has virtual bases.
8507 This holds
8508 1 - primary virtual pointer for complete object T
8509 2 - secondary VTTs for each direct non-virtual base of T which requires a
8511 3 - secondary virtual pointers for each direct or indirect base of T which
8512 has virtual bases or is reachable via a virtual path from T.
8513 4 - secondary VTTs for each direct or indirect virtual base of T.
8515 Secondary VTTs look like complete object VTTs without part 4. */
8517 static void
8518 build_vtt (tree t)
8520 tree type;
8521 tree vtt;
8522 tree index;
8523 vec<constructor_elt, va_gc> *inits;
8525 /* Build up the initializers for the VTT. */
8526 inits = NULL;
8527 index = size_zero_node;
8528 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8530 /* If we didn't need a VTT, we're done. */
8531 if (!inits)
8532 return;
8534 /* Figure out the type of the VTT. */
8535 type = build_array_of_n_type (const_ptr_type_node,
8536 inits->length ());
8538 /* Now, build the VTT object itself. */
8539 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8540 initialize_artificial_var (vtt, inits);
8541 /* Add the VTT to the vtables list. */
8542 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8543 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8545 dump_vtt (t, vtt);
8548 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8549 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8550 and CHAIN the vtable pointer for this binfo after construction is
8551 complete. VALUE can also be another BINFO, in which case we recurse. */
8553 static tree
8554 binfo_ctor_vtable (tree binfo)
8556 tree vt;
8558 while (1)
8560 vt = BINFO_VTABLE (binfo);
8561 if (TREE_CODE (vt) == TREE_LIST)
8562 vt = TREE_VALUE (vt);
8563 if (TREE_CODE (vt) == TREE_BINFO)
8564 binfo = vt;
8565 else
8566 break;
8569 return vt;
8572 /* Data for secondary VTT initialization. */
8573 struct secondary_vptr_vtt_init_data
8575 /* Is this the primary VTT? */
8576 bool top_level_p;
8578 /* Current index into the VTT. */
8579 tree index;
8581 /* Vector of initializers built up. */
8582 vec<constructor_elt, va_gc> *inits;
8584 /* The type being constructed by this secondary VTT. */
8585 tree type_being_constructed;
8588 /* Recursively build the VTT-initializer for BINFO (which is in the
8589 hierarchy dominated by T). INITS points to the end of the initializer
8590 list to date. INDEX is the VTT index where the next element will be
8591 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8592 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8593 for virtual bases of T. When it is not so, we build the constructor
8594 vtables for the BINFO-in-T variant. */
8596 static void
8597 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8598 tree *index)
8600 int i;
8601 tree b;
8602 tree init;
8603 secondary_vptr_vtt_init_data data;
8604 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8606 /* We only need VTTs for subobjects with virtual bases. */
8607 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8608 return;
8610 /* We need to use a construction vtable if this is not the primary
8611 VTT. */
8612 if (!top_level_p)
8614 build_ctor_vtbl_group (binfo, t);
8616 /* Record the offset in the VTT where this sub-VTT can be found. */
8617 BINFO_SUBVTT_INDEX (binfo) = *index;
8620 /* Add the address of the primary vtable for the complete object. */
8621 init = binfo_ctor_vtable (binfo);
8622 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8623 if (top_level_p)
8625 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8626 BINFO_VPTR_INDEX (binfo) = *index;
8628 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8630 /* Recursively add the secondary VTTs for non-virtual bases. */
8631 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8632 if (!BINFO_VIRTUAL_P (b))
8633 build_vtt_inits (b, t, inits, index);
8635 /* Add secondary virtual pointers for all subobjects of BINFO with
8636 either virtual bases or reachable along a virtual path, except
8637 subobjects that are non-virtual primary bases. */
8638 data.top_level_p = top_level_p;
8639 data.index = *index;
8640 data.inits = *inits;
8641 data.type_being_constructed = BINFO_TYPE (binfo);
8643 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8645 *index = data.index;
8647 /* data.inits might have grown as we added secondary virtual pointers.
8648 Make sure our caller knows about the new vector. */
8649 *inits = data.inits;
8651 if (top_level_p)
8652 /* Add the secondary VTTs for virtual bases in inheritance graph
8653 order. */
8654 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8656 if (!BINFO_VIRTUAL_P (b))
8657 continue;
8659 build_vtt_inits (b, t, inits, index);
8661 else
8662 /* Remove the ctor vtables we created. */
8663 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8666 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8667 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8669 static tree
8670 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8672 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8674 /* We don't care about bases that don't have vtables. */
8675 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8676 return dfs_skip_bases;
8678 /* We're only interested in proper subobjects of the type being
8679 constructed. */
8680 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8681 return NULL_TREE;
8683 /* We're only interested in bases with virtual bases or reachable
8684 via a virtual path from the type being constructed. */
8685 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8686 || binfo_via_virtual (binfo, data->type_being_constructed)))
8687 return dfs_skip_bases;
8689 /* We're not interested in non-virtual primary bases. */
8690 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8691 return NULL_TREE;
8693 /* Record the index where this secondary vptr can be found. */
8694 if (data->top_level_p)
8696 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8697 BINFO_VPTR_INDEX (binfo) = data->index;
8699 if (BINFO_VIRTUAL_P (binfo))
8701 /* It's a primary virtual base, and this is not a
8702 construction vtable. Find the base this is primary of in
8703 the inheritance graph, and use that base's vtable
8704 now. */
8705 while (BINFO_PRIMARY_P (binfo))
8706 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8710 /* Add the initializer for the secondary vptr itself. */
8711 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8713 /* Advance the vtt index. */
8714 data->index = size_binop (PLUS_EXPR, data->index,
8715 TYPE_SIZE_UNIT (ptr_type_node));
8717 return NULL_TREE;
8720 /* Called from build_vtt_inits via dfs_walk. After building
8721 constructor vtables and generating the sub-vtt from them, we need
8722 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8723 binfo of the base whose sub vtt was generated. */
8725 static tree
8726 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8728 tree vtable = BINFO_VTABLE (binfo);
8730 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8731 /* If this class has no vtable, none of its bases do. */
8732 return dfs_skip_bases;
8734 if (!vtable)
8735 /* This might be a primary base, so have no vtable in this
8736 hierarchy. */
8737 return NULL_TREE;
8739 /* If we scribbled the construction vtable vptr into BINFO, clear it
8740 out now. */
8741 if (TREE_CODE (vtable) == TREE_LIST
8742 && (TREE_PURPOSE (vtable) == (tree) data))
8743 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8745 return NULL_TREE;
8748 /* Build the construction vtable group for BINFO which is in the
8749 hierarchy dominated by T. */
8751 static void
8752 build_ctor_vtbl_group (tree binfo, tree t)
8754 tree type;
8755 tree vtbl;
8756 tree id;
8757 tree vbase;
8758 vec<constructor_elt, va_gc> *v;
8760 /* See if we've already created this construction vtable group. */
8761 id = mangle_ctor_vtbl_for_type (t, binfo);
8762 if (IDENTIFIER_GLOBAL_VALUE (id))
8763 return;
8765 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8766 /* Build a version of VTBL (with the wrong type) for use in
8767 constructing the addresses of secondary vtables in the
8768 construction vtable group. */
8769 vtbl = build_vtable (t, id, ptr_type_node);
8770 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8771 /* Don't export construction vtables from shared libraries. Even on
8772 targets that don't support hidden visibility, this tells
8773 can_refer_decl_in_current_unit_p not to assume that it's safe to
8774 access from a different compilation unit (bz 54314). */
8775 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8776 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8778 v = NULL;
8779 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8780 binfo, vtbl, t, &v);
8782 /* Add the vtables for each of our virtual bases using the vbase in T
8783 binfo. */
8784 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8785 vbase;
8786 vbase = TREE_CHAIN (vbase))
8788 tree b;
8790 if (!BINFO_VIRTUAL_P (vbase))
8791 continue;
8792 b = copied_binfo (vbase, binfo);
8794 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8797 /* Figure out the type of the construction vtable. */
8798 type = build_array_of_n_type (vtable_entry_type, v->length ());
8799 layout_type (type);
8800 TREE_TYPE (vtbl) = type;
8801 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8802 layout_decl (vtbl, 0);
8804 /* Initialize the construction vtable. */
8805 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8806 initialize_artificial_var (vtbl, v);
8807 dump_vtable (t, binfo, vtbl);
8810 /* Add the vtbl initializers for BINFO (and its bases other than
8811 non-virtual primaries) to the list of INITS. BINFO is in the
8812 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8813 the constructor the vtbl inits should be accumulated for. (If this
8814 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8815 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8816 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8817 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8818 but are not necessarily the same in terms of layout. */
8820 static void
8821 accumulate_vtbl_inits (tree binfo,
8822 tree orig_binfo,
8823 tree rtti_binfo,
8824 tree vtbl,
8825 tree t,
8826 vec<constructor_elt, va_gc> **inits)
8828 int i;
8829 tree base_binfo;
8830 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8832 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8834 /* If it doesn't have a vptr, we don't do anything. */
8835 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8836 return;
8838 /* If we're building a construction vtable, we're not interested in
8839 subobjects that don't require construction vtables. */
8840 if (ctor_vtbl_p
8841 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8842 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8843 return;
8845 /* Build the initializers for the BINFO-in-T vtable. */
8846 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8848 /* Walk the BINFO and its bases. We walk in preorder so that as we
8849 initialize each vtable we can figure out at what offset the
8850 secondary vtable lies from the primary vtable. We can't use
8851 dfs_walk here because we need to iterate through bases of BINFO
8852 and RTTI_BINFO simultaneously. */
8853 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8855 /* Skip virtual bases. */
8856 if (BINFO_VIRTUAL_P (base_binfo))
8857 continue;
8858 accumulate_vtbl_inits (base_binfo,
8859 BINFO_BASE_BINFO (orig_binfo, i),
8860 rtti_binfo, vtbl, t,
8861 inits);
8865 /* Called from accumulate_vtbl_inits. Adds the initializers for the
8866 BINFO vtable to L. */
8868 static void
8869 dfs_accumulate_vtbl_inits (tree binfo,
8870 tree orig_binfo,
8871 tree rtti_binfo,
8872 tree orig_vtbl,
8873 tree t,
8874 vec<constructor_elt, va_gc> **l)
8876 tree vtbl = NULL_TREE;
8877 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8878 int n_inits;
8880 if (ctor_vtbl_p
8881 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
8883 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
8884 primary virtual base. If it is not the same primary in
8885 the hierarchy of T, we'll need to generate a ctor vtable
8886 for it, to place at its location in T. If it is the same
8887 primary, we still need a VTT entry for the vtable, but it
8888 should point to the ctor vtable for the base it is a
8889 primary for within the sub-hierarchy of RTTI_BINFO.
8891 There are three possible cases:
8893 1) We are in the same place.
8894 2) We are a primary base within a lost primary virtual base of
8895 RTTI_BINFO.
8896 3) We are primary to something not a base of RTTI_BINFO. */
8898 tree b;
8899 tree last = NULL_TREE;
8901 /* First, look through the bases we are primary to for RTTI_BINFO
8902 or a virtual base. */
8903 b = binfo;
8904 while (BINFO_PRIMARY_P (b))
8906 b = BINFO_INHERITANCE_CHAIN (b);
8907 last = b;
8908 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8909 goto found;
8911 /* If we run out of primary links, keep looking down our
8912 inheritance chain; we might be an indirect primary. */
8913 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
8914 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
8915 break;
8916 found:
8918 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
8919 base B and it is a base of RTTI_BINFO, this is case 2. In
8920 either case, we share our vtable with LAST, i.e. the
8921 derived-most base within B of which we are a primary. */
8922 if (b == rtti_binfo
8923 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
8924 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
8925 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
8926 binfo_ctor_vtable after everything's been set up. */
8927 vtbl = last;
8929 /* Otherwise, this is case 3 and we get our own. */
8931 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
8932 return;
8934 n_inits = vec_safe_length (*l);
8936 if (!vtbl)
8938 tree index;
8939 int non_fn_entries;
8941 /* Add the initializer for this vtable. */
8942 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
8943 &non_fn_entries, l);
8945 /* Figure out the position to which the VPTR should point. */
8946 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
8947 index = size_binop (MULT_EXPR,
8948 TYPE_SIZE_UNIT (vtable_entry_type),
8949 size_int (non_fn_entries + n_inits));
8950 vtbl = fold_build_pointer_plus (vtbl, index);
8953 if (ctor_vtbl_p)
8954 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
8955 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
8956 straighten this out. */
8957 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
8958 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
8959 /* Throw away any unneeded intializers. */
8960 (*l)->truncate (n_inits);
8961 else
8962 /* For an ordinary vtable, set BINFO_VTABLE. */
8963 BINFO_VTABLE (binfo) = vtbl;
8966 static GTY(()) tree abort_fndecl_addr;
8968 /* Construct the initializer for BINFO's virtual function table. BINFO
8969 is part of the hierarchy dominated by T. If we're building a
8970 construction vtable, the ORIG_BINFO is the binfo we should use to
8971 find the actual function pointers to put in the vtable - but they
8972 can be overridden on the path to most-derived in the graph that
8973 ORIG_BINFO belongs. Otherwise,
8974 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
8975 BINFO that should be indicated by the RTTI information in the
8976 vtable; it will be a base class of T, rather than T itself, if we
8977 are building a construction vtable.
8979 The value returned is a TREE_LIST suitable for wrapping in a
8980 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
8981 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
8982 number of non-function entries in the vtable.
8984 It might seem that this function should never be called with a
8985 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
8986 base is always subsumed by a derived class vtable. However, when
8987 we are building construction vtables, we do build vtables for
8988 primary bases; we need these while the primary base is being
8989 constructed. */
8991 static void
8992 build_vtbl_initializer (tree binfo,
8993 tree orig_binfo,
8994 tree t,
8995 tree rtti_binfo,
8996 int* non_fn_entries_p,
8997 vec<constructor_elt, va_gc> **inits)
8999 tree v;
9000 vtbl_init_data vid;
9001 unsigned ix, jx;
9002 tree vbinfo;
9003 vec<tree, va_gc> *vbases;
9004 constructor_elt *e;
9006 /* Initialize VID. */
9007 memset (&vid, 0, sizeof (vid));
9008 vid.binfo = binfo;
9009 vid.derived = t;
9010 vid.rtti_binfo = rtti_binfo;
9011 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9012 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9013 vid.generate_vcall_entries = true;
9014 /* The first vbase or vcall offset is at index -3 in the vtable. */
9015 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9017 /* Add entries to the vtable for RTTI. */
9018 build_rtti_vtbl_entries (binfo, &vid);
9020 /* Create an array for keeping track of the functions we've
9021 processed. When we see multiple functions with the same
9022 signature, we share the vcall offsets. */
9023 vec_alloc (vid.fns, 32);
9024 /* Add the vcall and vbase offset entries. */
9025 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9027 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9028 build_vbase_offset_vtbl_entries. */
9029 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9030 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9031 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9033 /* If the target requires padding between data entries, add that now. */
9034 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9036 int n_entries = vec_safe_length (vid.inits);
9038 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9040 /* Move data entries into their new positions and add padding
9041 after the new positions. Iterate backwards so we don't
9042 overwrite entries that we would need to process later. */
9043 for (ix = n_entries - 1;
9044 vid.inits->iterate (ix, &e);
9045 ix--)
9047 int j;
9048 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9049 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9051 (*vid.inits)[new_position] = *e;
9053 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9055 constructor_elt *f = &(*vid.inits)[new_position - j];
9056 f->index = NULL_TREE;
9057 f->value = build1 (NOP_EXPR, vtable_entry_type,
9058 null_pointer_node);
9063 if (non_fn_entries_p)
9064 *non_fn_entries_p = vec_safe_length (vid.inits);
9066 /* The initializers for virtual functions were built up in reverse
9067 order. Straighten them out and add them to the running list in one
9068 step. */
9069 jx = vec_safe_length (*inits);
9070 vec_safe_grow (*inits, jx + vid.inits->length ());
9072 for (ix = vid.inits->length () - 1;
9073 vid.inits->iterate (ix, &e);
9074 ix--, jx++)
9075 (**inits)[jx] = *e;
9077 /* Go through all the ordinary virtual functions, building up
9078 initializers. */
9079 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9081 tree delta;
9082 tree vcall_index;
9083 tree fn, fn_original;
9084 tree init = NULL_TREE;
9086 fn = BV_FN (v);
9087 fn_original = fn;
9088 if (DECL_THUNK_P (fn))
9090 if (!DECL_NAME (fn))
9091 finish_thunk (fn);
9092 if (THUNK_ALIAS (fn))
9094 fn = THUNK_ALIAS (fn);
9095 BV_FN (v) = fn;
9097 fn_original = THUNK_TARGET (fn);
9100 /* If the only definition of this function signature along our
9101 primary base chain is from a lost primary, this vtable slot will
9102 never be used, so just zero it out. This is important to avoid
9103 requiring extra thunks which cannot be generated with the function.
9105 We first check this in update_vtable_entry_for_fn, so we handle
9106 restored primary bases properly; we also need to do it here so we
9107 zero out unused slots in ctor vtables, rather than filling them
9108 with erroneous values (though harmless, apart from relocation
9109 costs). */
9110 if (BV_LOST_PRIMARY (v))
9111 init = size_zero_node;
9113 if (! init)
9115 /* Pull the offset for `this', and the function to call, out of
9116 the list. */
9117 delta = BV_DELTA (v);
9118 vcall_index = BV_VCALL_INDEX (v);
9120 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9121 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9123 /* You can't call an abstract virtual function; it's abstract.
9124 So, we replace these functions with __pure_virtual. */
9125 if (DECL_PURE_VIRTUAL_P (fn_original))
9127 fn = abort_fndecl;
9128 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9130 if (abort_fndecl_addr == NULL)
9131 abort_fndecl_addr
9132 = fold_convert (vfunc_ptr_type_node,
9133 build_fold_addr_expr (fn));
9134 init = abort_fndecl_addr;
9137 /* Likewise for deleted virtuals. */
9138 else if (DECL_DELETED_FN (fn_original))
9140 fn = get_identifier ("__cxa_deleted_virtual");
9141 if (!get_global_value_if_present (fn, &fn))
9142 fn = push_library_fn (fn, (build_function_type_list
9143 (void_type_node, NULL_TREE)),
9144 NULL_TREE, ECF_NORETURN);
9145 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9146 init = fold_convert (vfunc_ptr_type_node,
9147 build_fold_addr_expr (fn));
9149 else
9151 if (!integer_zerop (delta) || vcall_index)
9153 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9154 if (!DECL_NAME (fn))
9155 finish_thunk (fn);
9157 /* Take the address of the function, considering it to be of an
9158 appropriate generic type. */
9159 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9160 init = fold_convert (vfunc_ptr_type_node,
9161 build_fold_addr_expr (fn));
9162 /* Don't refer to a virtual destructor from a constructor
9163 vtable or a vtable for an abstract class, since destroying
9164 an object under construction is undefined behavior and we
9165 don't want it to be considered a candidate for speculative
9166 devirtualization. But do create the thunk for ABI
9167 compliance. */
9168 if (DECL_DESTRUCTOR_P (fn_original)
9169 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9170 || orig_binfo != binfo))
9171 init = size_zero_node;
9175 /* And add it to the chain of initializers. */
9176 if (TARGET_VTABLE_USES_DESCRIPTORS)
9178 int i;
9179 if (init == size_zero_node)
9180 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9181 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9182 else
9183 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9185 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9186 fn, build_int_cst (NULL_TREE, i));
9187 TREE_CONSTANT (fdesc) = 1;
9189 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9192 else
9193 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9197 /* Adds to vid->inits the initializers for the vbase and vcall
9198 offsets in BINFO, which is in the hierarchy dominated by T. */
9200 static void
9201 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9203 tree b;
9205 /* If this is a derived class, we must first create entries
9206 corresponding to the primary base class. */
9207 b = get_primary_binfo (binfo);
9208 if (b)
9209 build_vcall_and_vbase_vtbl_entries (b, vid);
9211 /* Add the vbase entries for this base. */
9212 build_vbase_offset_vtbl_entries (binfo, vid);
9213 /* Add the vcall entries for this base. */
9214 build_vcall_offset_vtbl_entries (binfo, vid);
9217 /* Returns the initializers for the vbase offset entries in the vtable
9218 for BINFO (which is part of the class hierarchy dominated by T), in
9219 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9220 where the next vbase offset will go. */
9222 static void
9223 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9225 tree vbase;
9226 tree t;
9227 tree non_primary_binfo;
9229 /* If there are no virtual baseclasses, then there is nothing to
9230 do. */
9231 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9232 return;
9234 t = vid->derived;
9236 /* We might be a primary base class. Go up the inheritance hierarchy
9237 until we find the most derived class of which we are a primary base:
9238 it is the offset of that which we need to use. */
9239 non_primary_binfo = binfo;
9240 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9242 tree b;
9244 /* If we have reached a virtual base, then it must be a primary
9245 base (possibly multi-level) of vid->binfo, or we wouldn't
9246 have called build_vcall_and_vbase_vtbl_entries for it. But it
9247 might be a lost primary, so just skip down to vid->binfo. */
9248 if (BINFO_VIRTUAL_P (non_primary_binfo))
9250 non_primary_binfo = vid->binfo;
9251 break;
9254 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9255 if (get_primary_binfo (b) != non_primary_binfo)
9256 break;
9257 non_primary_binfo = b;
9260 /* Go through the virtual bases, adding the offsets. */
9261 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9262 vbase;
9263 vbase = TREE_CHAIN (vbase))
9265 tree b;
9266 tree delta;
9268 if (!BINFO_VIRTUAL_P (vbase))
9269 continue;
9271 /* Find the instance of this virtual base in the complete
9272 object. */
9273 b = copied_binfo (vbase, binfo);
9275 /* If we've already got an offset for this virtual base, we
9276 don't need another one. */
9277 if (BINFO_VTABLE_PATH_MARKED (b))
9278 continue;
9279 BINFO_VTABLE_PATH_MARKED (b) = 1;
9281 /* Figure out where we can find this vbase offset. */
9282 delta = size_binop (MULT_EXPR,
9283 vid->index,
9284 convert (ssizetype,
9285 TYPE_SIZE_UNIT (vtable_entry_type)));
9286 if (vid->primary_vtbl_p)
9287 BINFO_VPTR_FIELD (b) = delta;
9289 if (binfo != TYPE_BINFO (t))
9290 /* The vbase offset had better be the same. */
9291 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9293 /* The next vbase will come at a more negative offset. */
9294 vid->index = size_binop (MINUS_EXPR, vid->index,
9295 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9297 /* The initializer is the delta from BINFO to this virtual base.
9298 The vbase offsets go in reverse inheritance-graph order, and
9299 we are walking in inheritance graph order so these end up in
9300 the right order. */
9301 delta = size_diffop_loc (input_location,
9302 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9304 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9305 fold_build1_loc (input_location, NOP_EXPR,
9306 vtable_entry_type, delta));
9310 /* Adds the initializers for the vcall offset entries in the vtable
9311 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9312 to VID->INITS. */
9314 static void
9315 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9317 /* We only need these entries if this base is a virtual base. We
9318 compute the indices -- but do not add to the vtable -- when
9319 building the main vtable for a class. */
9320 if (binfo == TYPE_BINFO (vid->derived)
9321 || (BINFO_VIRTUAL_P (binfo)
9322 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9323 correspond to VID->DERIVED), we are building a primary
9324 construction virtual table. Since this is a primary
9325 virtual table, we do not need the vcall offsets for
9326 BINFO. */
9327 && binfo != vid->rtti_binfo))
9329 /* We need a vcall offset for each of the virtual functions in this
9330 vtable. For example:
9332 class A { virtual void f (); };
9333 class B1 : virtual public A { virtual void f (); };
9334 class B2 : virtual public A { virtual void f (); };
9335 class C: public B1, public B2 { virtual void f (); };
9337 A C object has a primary base of B1, which has a primary base of A. A
9338 C also has a secondary base of B2, which no longer has a primary base
9339 of A. So the B2-in-C construction vtable needs a secondary vtable for
9340 A, which will adjust the A* to a B2* to call f. We have no way of
9341 knowing what (or even whether) this offset will be when we define B2,
9342 so we store this "vcall offset" in the A sub-vtable and look it up in
9343 a "virtual thunk" for B2::f.
9345 We need entries for all the functions in our primary vtable and
9346 in our non-virtual bases' secondary vtables. */
9347 vid->vbase = binfo;
9348 /* If we are just computing the vcall indices -- but do not need
9349 the actual entries -- not that. */
9350 if (!BINFO_VIRTUAL_P (binfo))
9351 vid->generate_vcall_entries = false;
9352 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9353 add_vcall_offset_vtbl_entries_r (binfo, vid);
9357 /* Build vcall offsets, starting with those for BINFO. */
9359 static void
9360 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9362 int i;
9363 tree primary_binfo;
9364 tree base_binfo;
9366 /* Don't walk into virtual bases -- except, of course, for the
9367 virtual base for which we are building vcall offsets. Any
9368 primary virtual base will have already had its offsets generated
9369 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9370 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9371 return;
9373 /* If BINFO has a primary base, process it first. */
9374 primary_binfo = get_primary_binfo (binfo);
9375 if (primary_binfo)
9376 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9378 /* Add BINFO itself to the list. */
9379 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9381 /* Scan the non-primary bases of BINFO. */
9382 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9383 if (base_binfo != primary_binfo)
9384 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9387 /* Called from build_vcall_offset_vtbl_entries_r. */
9389 static void
9390 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9392 /* Make entries for the rest of the virtuals. */
9393 tree orig_fn;
9395 /* The ABI requires that the methods be processed in declaration
9396 order. */
9397 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9398 orig_fn;
9399 orig_fn = DECL_CHAIN (orig_fn))
9400 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9401 add_vcall_offset (orig_fn, binfo, vid);
9404 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9406 static void
9407 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9409 size_t i;
9410 tree vcall_offset;
9411 tree derived_entry;
9413 /* If there is already an entry for a function with the same
9414 signature as FN, then we do not need a second vcall offset.
9415 Check the list of functions already present in the derived
9416 class vtable. */
9417 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9419 if (same_signature_p (derived_entry, orig_fn)
9420 /* We only use one vcall offset for virtual destructors,
9421 even though there are two virtual table entries. */
9422 || (DECL_DESTRUCTOR_P (derived_entry)
9423 && DECL_DESTRUCTOR_P (orig_fn)))
9424 return;
9427 /* If we are building these vcall offsets as part of building
9428 the vtable for the most derived class, remember the vcall
9429 offset. */
9430 if (vid->binfo == TYPE_BINFO (vid->derived))
9432 tree_pair_s elt = {orig_fn, vid->index};
9433 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9436 /* The next vcall offset will be found at a more negative
9437 offset. */
9438 vid->index = size_binop (MINUS_EXPR, vid->index,
9439 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9441 /* Keep track of this function. */
9442 vec_safe_push (vid->fns, orig_fn);
9444 if (vid->generate_vcall_entries)
9446 tree base;
9447 tree fn;
9449 /* Find the overriding function. */
9450 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9451 if (fn == error_mark_node)
9452 vcall_offset = build_zero_cst (vtable_entry_type);
9453 else
9455 base = TREE_VALUE (fn);
9457 /* The vbase we're working on is a primary base of
9458 vid->binfo. But it might be a lost primary, so its
9459 BINFO_OFFSET might be wrong, so we just use the
9460 BINFO_OFFSET from vid->binfo. */
9461 vcall_offset = size_diffop_loc (input_location,
9462 BINFO_OFFSET (base),
9463 BINFO_OFFSET (vid->binfo));
9464 vcall_offset = fold_build1_loc (input_location,
9465 NOP_EXPR, vtable_entry_type,
9466 vcall_offset);
9468 /* Add the initializer to the vtable. */
9469 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9473 /* Return vtbl initializers for the RTTI entries corresponding to the
9474 BINFO's vtable. The RTTI entries should indicate the object given
9475 by VID->rtti_binfo. */
9477 static void
9478 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9480 tree b;
9481 tree t;
9482 tree offset;
9483 tree decl;
9484 tree init;
9486 t = BINFO_TYPE (vid->rtti_binfo);
9488 /* To find the complete object, we will first convert to our most
9489 primary base, and then add the offset in the vtbl to that value. */
9490 b = binfo;
9491 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
9492 && !BINFO_LOST_PRIMARY_P (b))
9494 tree primary_base;
9496 primary_base = get_primary_binfo (b);
9497 gcc_assert (BINFO_PRIMARY_P (primary_base)
9498 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
9499 b = primary_base;
9501 offset = size_diffop_loc (input_location,
9502 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9504 /* The second entry is the address of the typeinfo object. */
9505 if (flag_rtti)
9506 decl = build_address (get_tinfo_decl (t));
9507 else
9508 decl = integer_zero_node;
9510 /* Convert the declaration to a type that can be stored in the
9511 vtable. */
9512 init = build_nop (vfunc_ptr_type_node, decl);
9513 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9515 /* Add the offset-to-top entry. It comes earlier in the vtable than
9516 the typeinfo entry. Convert the offset to look like a
9517 function pointer, so that we can put it in the vtable. */
9518 init = build_nop (vfunc_ptr_type_node, offset);
9519 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9522 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9523 accessibility. */
9525 bool
9526 uniquely_derived_from_p (tree parent, tree type)
9528 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9529 return base && base != error_mark_node;
9532 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9534 bool
9535 publicly_uniquely_derived_p (tree parent, tree type)
9537 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9538 NULL, tf_none);
9539 return base && base != error_mark_node;
9542 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9543 class between them, if any. */
9545 tree
9546 common_enclosing_class (tree ctx1, tree ctx2)
9548 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9549 return NULL_TREE;
9550 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9551 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9552 if (ctx1 == ctx2)
9553 return ctx1;
9554 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9555 TYPE_MARKED_P (t) = true;
9556 tree found = NULL_TREE;
9557 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9558 if (TYPE_MARKED_P (t))
9560 found = t;
9561 break;
9563 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9564 TYPE_MARKED_P (t) = false;
9565 return found;
9568 #include "gt-cp-class.h"