syscall, internal/syscall/unix: Fix getrandom, clone on sparc64
[official-gcc.git] / gcc / cp / class.c
blob34d10ba5cde6bd15cd17820ad3069c3761e5ed01
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2016 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 "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "convert.h"
36 #include "dumpfile.h"
37 #include "gimplify.h"
38 #include "intl.h"
40 /* The number of nested classes being processed. If we are not in the
41 scope of any class, this is zero. */
43 int current_class_depth;
45 /* In order to deal with nested classes, we keep a stack of classes.
46 The topmost entry is the innermost class, and is the entry at index
47 CURRENT_CLASS_DEPTH */
49 typedef struct class_stack_node {
50 /* The name of the class. */
51 tree name;
53 /* The _TYPE node for the class. */
54 tree type;
56 /* The access specifier pending for new declarations in the scope of
57 this class. */
58 tree access;
60 /* If were defining TYPE, the names used in this class. */
61 splay_tree names_used;
63 /* Nonzero if this class is no longer open, because of a call to
64 push_to_top_level. */
65 size_t hidden;
66 }* class_stack_node_t;
68 struct vtbl_init_data
70 /* The base for which we're building initializers. */
71 tree binfo;
72 /* The type of the most-derived type. */
73 tree derived;
74 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
75 unless ctor_vtbl_p is true. */
76 tree rtti_binfo;
77 /* The negative-index vtable initializers built up so far. These
78 are in order from least negative index to most negative index. */
79 vec<constructor_elt, va_gc> *inits;
80 /* The binfo for the virtual base for which we're building
81 vcall offset initializers. */
82 tree vbase;
83 /* The functions in vbase for which we have already provided vcall
84 offsets. */
85 vec<tree, va_gc> *fns;
86 /* The vtable index of the next vcall or vbase offset. */
87 tree index;
88 /* Nonzero if we are building the initializer for the primary
89 vtable. */
90 int primary_vtbl_p;
91 /* Nonzero if we are building the initializer for a construction
92 vtable. */
93 int ctor_vtbl_p;
94 /* True when adding vcall offset entries to the vtable. False when
95 merely computing the indices. */
96 bool generate_vcall_entries;
99 /* The type of a function passed to walk_subobject_offsets. */
100 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
102 /* The stack itself. This is a dynamically resized array. The
103 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
104 static int current_class_stack_size;
105 static class_stack_node_t current_class_stack;
107 /* The size of the largest empty class seen in this translation unit. */
108 static GTY (()) tree sizeof_biggest_empty_class;
110 /* An array of all local classes present in this translation unit, in
111 declaration order. */
112 vec<tree, va_gc> *local_classes;
114 static tree get_vfield_name (tree);
115 static void finish_struct_anon (tree);
116 static tree get_vtable_name (tree);
117 static void get_basefndecls (tree, tree, vec<tree> *);
118 static int build_primary_vtable (tree, tree);
119 static int build_secondary_vtable (tree);
120 static void finish_vtbls (tree);
121 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
122 static void finish_struct_bits (tree);
123 static int alter_access (tree, tree, tree);
124 static void handle_using_decl (tree, tree);
125 static tree dfs_modify_vtables (tree, void *);
126 static tree modify_all_vtables (tree, tree);
127 static void determine_primary_bases (tree);
128 static void finish_struct_methods (tree);
129 static void maybe_warn_about_overly_private_class (tree);
130 static int method_name_cmp (const void *, const void *);
131 static int resort_method_name_cmp (const void *, const void *);
132 static void add_implicitly_declared_members (tree, tree*, int, int);
133 static tree fixed_type_or_null (tree, int *, int *);
134 static tree build_simple_base_path (tree expr, tree binfo);
135 static tree build_vtbl_ref_1 (tree, tree);
136 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
137 vec<constructor_elt, va_gc> **);
138 static int count_fields (tree);
139 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
140 static void insert_into_classtype_sorted_fields (tree, tree, int);
141 static bool check_bitfield_decl (tree);
142 static bool check_field_decl (tree, tree, int *, int *);
143 static void check_field_decls (tree, tree *, int *, int *);
144 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
145 static void build_base_fields (record_layout_info, splay_tree, tree *);
146 static void check_methods (tree);
147 static void remove_zero_width_bit_fields (tree);
148 static bool accessible_nvdtor_p (tree);
150 /* Used by find_flexarrays and related. */
151 struct flexmems_t;
152 static void find_flexarrays (tree, flexmems_t *);
153 static void diagnose_flexarrays (tree, const flexmems_t *);
154 static void check_flexarrays (tree, flexmems_t * = NULL);
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);
217 static bool type_maybe_constexpr_default_constructor (tree);
219 /* Variables shared between class.c and call.c. */
221 int n_vtables = 0;
222 int n_vtable_entries = 0;
223 int n_vtable_searches = 0;
224 int n_vtable_elems = 0;
225 int n_convert_harshness = 0;
226 int n_compute_conversion_costs = 0;
227 int n_inner_fields_searched = 0;
229 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
230 'structor is in charge of 'structing virtual bases, or FALSE_STMT
231 otherwise. */
233 tree
234 build_if_in_charge (tree true_stmt, tree false_stmt)
236 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
237 tree cmp = build2 (NE_EXPR, boolean_type_node,
238 current_in_charge_parm, integer_zero_node);
239 tree type = unlowered_expr_type (true_stmt);
240 if (VOID_TYPE_P (type))
241 type = unlowered_expr_type (false_stmt);
242 tree cond = build3 (COND_EXPR, type,
243 cmp, true_stmt, false_stmt);
244 return cond;
247 /* Convert to or from a base subobject. EXPR is an expression of type
248 `A' or `A*', an expression of type `B' or `B*' is returned. To
249 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
250 the B base instance within A. To convert base A to derived B, CODE
251 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
252 In this latter case, A must not be a morally virtual base of B.
253 NONNULL is true if EXPR is known to be non-NULL (this is only
254 needed when EXPR is of pointer type). CV qualifiers are preserved
255 from EXPR. */
257 tree
258 build_base_path (enum tree_code code,
259 tree expr,
260 tree binfo,
261 int nonnull,
262 tsubst_flags_t complain)
264 tree v_binfo = NULL_TREE;
265 tree d_binfo = NULL_TREE;
266 tree probe;
267 tree offset;
268 tree target_type;
269 tree null_test = NULL;
270 tree ptr_target_type;
271 int fixed_type_p;
272 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
273 bool has_empty = false;
274 bool virtual_access;
275 bool rvalue = false;
277 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
278 return error_mark_node;
280 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
282 d_binfo = probe;
283 if (is_empty_class (BINFO_TYPE (probe)))
284 has_empty = true;
285 if (!v_binfo && BINFO_VIRTUAL_P (probe))
286 v_binfo = probe;
289 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
290 if (want_pointer)
291 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
293 if (code == PLUS_EXPR
294 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
296 /* This can happen when adjust_result_of_qualified_name_lookup can't
297 find a unique base binfo in a call to a member function. We
298 couldn't give the diagnostic then since we might have been calling
299 a static member function, so we do it now. In other cases, eg.
300 during error recovery (c++/71979), we may not have a base at all. */
301 if (complain & tf_error)
303 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
304 ba_unique, NULL, complain);
305 gcc_assert (base == error_mark_node || !base);
307 return error_mark_node;
310 gcc_assert ((code == MINUS_EXPR
311 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
312 || code == PLUS_EXPR);
314 if (binfo == d_binfo)
315 /* Nothing to do. */
316 return expr;
318 if (code == MINUS_EXPR && v_binfo)
320 if (complain & tf_error)
322 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
324 if (want_pointer)
325 error ("cannot convert from pointer to base class %qT to "
326 "pointer to derived class %qT because the base is "
327 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
328 else
329 error ("cannot convert from base class %qT to derived "
330 "class %qT because the base is virtual",
331 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
333 else
335 if (want_pointer)
336 error ("cannot convert from pointer to base class %qT to "
337 "pointer to derived class %qT via virtual base %qT",
338 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
339 BINFO_TYPE (v_binfo));
340 else
341 error ("cannot convert from base class %qT to derived "
342 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
343 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
346 return error_mark_node;
349 if (!want_pointer)
351 rvalue = !lvalue_p (expr);
352 /* This must happen before the call to save_expr. */
353 expr = cp_build_addr_expr (expr, complain);
355 else
356 expr = mark_rvalue_use (expr);
358 offset = BINFO_OFFSET (binfo);
359 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
360 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
361 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
362 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
363 expression returned matches the input. */
364 target_type = cp_build_qualified_type
365 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
366 ptr_target_type = build_pointer_type (target_type);
368 /* Do we need to look in the vtable for the real offset? */
369 virtual_access = (v_binfo && fixed_type_p <= 0);
371 /* Don't bother with the calculations inside sizeof; they'll ICE if the
372 source type is incomplete and the pointer value doesn't matter. In a
373 template (even in instantiate_non_dependent_expr), we don't have vtables
374 set up properly yet, and the value doesn't matter there either; we're
375 just interested in the result of overload resolution. */
376 if (cp_unevaluated_operand != 0
377 || in_template_function ())
379 expr = build_nop (ptr_target_type, expr);
380 goto indout;
383 /* If we're in an NSDMI, we don't have the full constructor context yet
384 that we need for converting to a virtual base, so just build a stub
385 CONVERT_EXPR and expand it later in bot_replace. */
386 if (virtual_access && fixed_type_p < 0
387 && current_scope () != current_function_decl)
389 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
390 CONVERT_EXPR_VBASE_PATH (expr) = true;
391 goto indout;
394 /* Do we need to check for a null pointer? */
395 if (want_pointer && !nonnull)
397 /* If we know the conversion will not actually change the value
398 of EXPR, then we can avoid testing the expression for NULL.
399 We have to avoid generating a COMPONENT_REF for a base class
400 field, because other parts of the compiler know that such
401 expressions are always non-NULL. */
402 if (!virtual_access && integer_zerop (offset))
403 return build_nop (ptr_target_type, expr);
404 null_test = error_mark_node;
407 /* Protect against multiple evaluation if necessary. */
408 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
409 expr = save_expr (expr);
411 /* Now that we've saved expr, build the real null test. */
412 if (null_test)
414 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
415 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
416 expr, zero);
417 /* This is a compiler generated comparison, don't emit
418 e.g. -Wnonnull-compare warning for it. */
419 TREE_NO_WARNING (null_test) = 1;
422 /* If this is a simple base reference, express it as a COMPONENT_REF. */
423 if (code == PLUS_EXPR && !virtual_access
424 /* We don't build base fields for empty bases, and they aren't very
425 interesting to the optimizers anyway. */
426 && !has_empty)
428 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
429 expr = build_simple_base_path (expr, binfo);
430 if (rvalue)
431 expr = move (expr);
432 if (want_pointer)
433 expr = build_address (expr);
434 target_type = TREE_TYPE (expr);
435 goto out;
438 if (virtual_access)
440 /* Going via virtual base V_BINFO. We need the static offset
441 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
442 V_BINFO. That offset is an entry in D_BINFO's vtable. */
443 tree v_offset;
445 if (fixed_type_p < 0 && in_base_initializer)
447 /* In a base member initializer, we cannot rely on the
448 vtable being set up. We have to indirect via the
449 vtt_parm. */
450 tree t;
452 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
453 t = build_pointer_type (t);
454 v_offset = fold_convert (t, current_vtt_parm);
455 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
457 else
459 tree t = expr;
460 if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0)
462 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
463 probe, expr);
464 if (t == NULL_TREE)
465 t = expr;
467 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
468 complain),
469 TREE_TYPE (TREE_TYPE (expr)));
472 if (v_offset == error_mark_node)
473 return error_mark_node;
475 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
476 v_offset = build1 (NOP_EXPR,
477 build_pointer_type (ptrdiff_type_node),
478 v_offset);
479 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
480 TREE_CONSTANT (v_offset) = 1;
482 offset = convert_to_integer (ptrdiff_type_node,
483 size_diffop_loc (input_location, offset,
484 BINFO_OFFSET (v_binfo)));
486 if (!integer_zerop (offset))
487 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
489 if (fixed_type_p < 0)
490 /* Negative fixed_type_p means this is a constructor or destructor;
491 virtual base layout is fixed in in-charge [cd]tors, but not in
492 base [cd]tors. */
493 offset = build_if_in_charge
494 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
495 v_offset);
496 else
497 offset = v_offset;
500 if (want_pointer)
501 target_type = ptr_target_type;
503 expr = build1 (NOP_EXPR, ptr_target_type, expr);
505 if (!integer_zerop (offset))
507 offset = fold_convert (sizetype, offset);
508 if (code == MINUS_EXPR)
509 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
510 expr = fold_build_pointer_plus (expr, offset);
512 else
513 null_test = NULL;
515 indout:
516 if (!want_pointer)
518 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
519 if (rvalue)
520 expr = move (expr);
523 out:
524 if (null_test)
525 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
526 build_zero_cst (target_type));
528 return expr;
531 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
532 Perform a derived-to-base conversion by recursively building up a
533 sequence of COMPONENT_REFs to the appropriate base fields. */
535 static tree
536 build_simple_base_path (tree expr, tree binfo)
538 tree type = BINFO_TYPE (binfo);
539 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
540 tree field;
542 if (d_binfo == NULL_TREE)
544 tree temp;
546 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
548 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
549 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
550 an lvalue in the front end; only _DECLs and _REFs are lvalues
551 in the back end. */
552 temp = unary_complex_lvalue (ADDR_EXPR, expr);
553 if (temp)
554 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
556 return expr;
559 /* Recurse. */
560 expr = build_simple_base_path (expr, d_binfo);
562 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
563 field; field = DECL_CHAIN (field))
564 /* Is this the base field created by build_base_field? */
565 if (TREE_CODE (field) == FIELD_DECL
566 && DECL_FIELD_IS_BASE (field)
567 && TREE_TYPE (field) == type
568 /* If we're looking for a field in the most-derived class,
569 also check the field offset; we can have two base fields
570 of the same type if one is an indirect virtual base and one
571 is a direct non-virtual base. */
572 && (BINFO_INHERITANCE_CHAIN (d_binfo)
573 || tree_int_cst_equal (byte_position (field),
574 BINFO_OFFSET (binfo))))
576 /* We don't use build_class_member_access_expr here, as that
577 has unnecessary checks, and more importantly results in
578 recursive calls to dfs_walk_once. */
579 int type_quals = cp_type_quals (TREE_TYPE (expr));
581 expr = build3 (COMPONENT_REF,
582 cp_build_qualified_type (type, type_quals),
583 expr, field, NULL_TREE);
584 /* Mark the expression const or volatile, as appropriate.
585 Even though we've dealt with the type above, we still have
586 to mark the expression itself. */
587 if (type_quals & TYPE_QUAL_CONST)
588 TREE_READONLY (expr) = 1;
589 if (type_quals & TYPE_QUAL_VOLATILE)
590 TREE_THIS_VOLATILE (expr) = 1;
592 return expr;
595 /* Didn't find the base field?!? */
596 gcc_unreachable ();
599 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
600 type is a class type or a pointer to a class type. In the former
601 case, TYPE is also a class type; in the latter it is another
602 pointer type. If CHECK_ACCESS is true, an error message is emitted
603 if TYPE is inaccessible. If OBJECT has pointer type, the value is
604 assumed to be non-NULL. */
606 tree
607 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
608 tsubst_flags_t complain)
610 tree binfo;
611 tree object_type;
613 if (TYPE_PTR_P (TREE_TYPE (object)))
615 object_type = TREE_TYPE (TREE_TYPE (object));
616 type = TREE_TYPE (type);
618 else
619 object_type = TREE_TYPE (object);
621 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
622 NULL, complain);
623 if (!binfo || binfo == error_mark_node)
624 return error_mark_node;
626 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
629 /* EXPR is an expression with unqualified class type. BASE is a base
630 binfo of that class type. Returns EXPR, converted to the BASE
631 type. This function assumes that EXPR is the most derived class;
632 therefore virtual bases can be found at their static offsets. */
634 tree
635 convert_to_base_statically (tree expr, tree base)
637 tree expr_type;
639 expr_type = TREE_TYPE (expr);
640 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
642 /* If this is a non-empty base, use a COMPONENT_REF. */
643 if (!is_empty_class (BINFO_TYPE (base)))
644 return build_simple_base_path (expr, base);
646 /* We use fold_build2 and fold_convert below to simplify the trees
647 provided to the optimizers. It is not safe to call these functions
648 when processing a template because they do not handle C++-specific
649 trees. */
650 gcc_assert (!processing_template_decl);
651 expr = cp_build_addr_expr (expr, tf_warning_or_error);
652 if (!integer_zerop (BINFO_OFFSET (base)))
653 expr = fold_build_pointer_plus_loc (input_location,
654 expr, BINFO_OFFSET (base));
655 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
656 expr = build_fold_indirect_ref_loc (input_location, expr);
659 return expr;
663 tree
664 build_vfield_ref (tree datum, tree type)
666 tree vfield, vcontext;
668 if (datum == error_mark_node
669 /* Can happen in case of duplicate base types (c++/59082). */
670 || !TYPE_VFIELD (type))
671 return error_mark_node;
673 /* First, convert to the requested type. */
674 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
675 datum = convert_to_base (datum, type, /*check_access=*/false,
676 /*nonnull=*/true, tf_warning_or_error);
678 /* Second, the requested type may not be the owner of its own vptr.
679 If not, convert to the base class that owns it. We cannot use
680 convert_to_base here, because VCONTEXT may appear more than once
681 in the inheritance hierarchy of TYPE, and thus direct conversion
682 between the types may be ambiguous. Following the path back up
683 one step at a time via primary bases avoids the problem. */
684 vfield = TYPE_VFIELD (type);
685 vcontext = DECL_CONTEXT (vfield);
686 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
688 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
689 type = TREE_TYPE (datum);
692 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
695 /* Given an object INSTANCE, return an expression which yields the
696 vtable element corresponding to INDEX. There are many special
697 cases for INSTANCE which we take care of here, mainly to avoid
698 creating extra tree nodes when we don't have to. */
700 static tree
701 build_vtbl_ref_1 (tree instance, tree idx)
703 tree aref;
704 tree vtbl = NULL_TREE;
706 /* Try to figure out what a reference refers to, and
707 access its virtual function table directly. */
709 int cdtorp = 0;
710 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
712 tree basetype = non_reference (TREE_TYPE (instance));
714 if (fixed_type && !cdtorp)
716 tree binfo = lookup_base (fixed_type, basetype,
717 ba_unique, NULL, tf_none);
718 if (binfo && binfo != error_mark_node)
719 vtbl = unshare_expr (BINFO_VTABLE (binfo));
722 if (!vtbl)
723 vtbl = build_vfield_ref (instance, basetype);
725 aref = build_array_ref (input_location, vtbl, idx);
726 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
728 return aref;
731 tree
732 build_vtbl_ref (tree instance, tree idx)
734 tree aref = build_vtbl_ref_1 (instance, idx);
736 return aref;
739 /* Given a stable object pointer INSTANCE_PTR, return an expression which
740 yields a function pointer corresponding to vtable element INDEX. */
742 tree
743 build_vfn_ref (tree instance_ptr, tree idx)
745 tree aref;
747 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
748 tf_warning_or_error),
749 idx);
751 /* When using function descriptors, the address of the
752 vtable entry is treated as a function pointer. */
753 if (TARGET_VTABLE_USES_DESCRIPTORS)
754 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
755 cp_build_addr_expr (aref, tf_warning_or_error));
757 /* Remember this as a method reference, for later devirtualization. */
758 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
760 return aref;
763 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
764 for the given TYPE. */
766 static tree
767 get_vtable_name (tree type)
769 return mangle_vtbl_for_type (type);
772 /* DECL is an entity associated with TYPE, like a virtual table or an
773 implicitly generated constructor. Determine whether or not DECL
774 should have external or internal linkage at the object file
775 level. This routine does not deal with COMDAT linkage and other
776 similar complexities; it simply sets TREE_PUBLIC if it possible for
777 entities in other translation units to contain copies of DECL, in
778 the abstract. */
780 void
781 set_linkage_according_to_type (tree /*type*/, tree decl)
783 TREE_PUBLIC (decl) = 1;
784 determine_visibility (decl);
787 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
788 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
789 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
791 static tree
792 build_vtable (tree class_type, tree name, tree vtable_type)
794 tree decl;
796 decl = build_lang_decl (VAR_DECL, name, vtable_type);
797 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
798 now to avoid confusion in mangle_decl. */
799 SET_DECL_ASSEMBLER_NAME (decl, name);
800 DECL_CONTEXT (decl) = class_type;
801 DECL_ARTIFICIAL (decl) = 1;
802 TREE_STATIC (decl) = 1;
803 TREE_READONLY (decl) = 1;
804 DECL_VIRTUAL_P (decl) = 1;
805 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
806 DECL_USER_ALIGN (decl) = true;
807 DECL_VTABLE_OR_VTT_P (decl) = 1;
808 set_linkage_according_to_type (class_type, decl);
809 /* The vtable has not been defined -- yet. */
810 DECL_EXTERNAL (decl) = 1;
811 DECL_NOT_REALLY_EXTERN (decl) = 1;
813 /* Mark the VAR_DECL node representing the vtable itself as a
814 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
815 is rather important that such things be ignored because any
816 effort to actually generate DWARF for them will run into
817 trouble when/if we encounter code like:
819 #pragma interface
820 struct S { virtual void member (); };
822 because the artificial declaration of the vtable itself (as
823 manufactured by the g++ front end) will say that the vtable is
824 a static member of `S' but only *after* the debug output for
825 the definition of `S' has already been output. This causes
826 grief because the DWARF entry for the definition of the vtable
827 will try to refer back to an earlier *declaration* of the
828 vtable as a static member of `S' and there won't be one. We
829 might be able to arrange to have the "vtable static member"
830 attached to the member list for `S' before the debug info for
831 `S' get written (which would solve the problem) but that would
832 require more intrusive changes to the g++ front end. */
833 DECL_IGNORED_P (decl) = 1;
835 return decl;
838 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
839 or even complete. If this does not exist, create it. If COMPLETE is
840 nonzero, then complete the definition of it -- that will render it
841 impossible to actually build the vtable, but is useful to get at those
842 which are known to exist in the runtime. */
844 tree
845 get_vtable_decl (tree type, int complete)
847 tree decl;
849 if (CLASSTYPE_VTABLES (type))
850 return CLASSTYPE_VTABLES (type);
852 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
853 CLASSTYPE_VTABLES (type) = decl;
855 if (complete)
857 DECL_EXTERNAL (decl) = 1;
858 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
861 return decl;
864 /* Build the primary virtual function table for TYPE. If BINFO is
865 non-NULL, build the vtable starting with the initial approximation
866 that it is the same as the one which is the head of the association
867 list. Returns a nonzero value if a new vtable is actually
868 created. */
870 static int
871 build_primary_vtable (tree binfo, tree type)
873 tree decl;
874 tree virtuals;
876 decl = get_vtable_decl (type, /*complete=*/0);
878 if (binfo)
880 if (BINFO_NEW_VTABLE_MARKED (binfo))
881 /* We have already created a vtable for this base, so there's
882 no need to do it again. */
883 return 0;
885 virtuals = copy_list (BINFO_VIRTUALS (binfo));
886 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
887 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
888 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
890 else
892 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
893 virtuals = NULL_TREE;
896 if (GATHER_STATISTICS)
898 n_vtables += 1;
899 n_vtable_elems += list_length (virtuals);
902 /* Initialize the association list for this type, based
903 on our first approximation. */
904 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
905 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
906 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
907 return 1;
910 /* Give BINFO a new virtual function table which is initialized
911 with a skeleton-copy of its original initialization. The only
912 entry that changes is the `delta' entry, so we can really
913 share a lot of structure.
915 FOR_TYPE is the most derived type which caused this table to
916 be needed.
918 Returns nonzero if we haven't met BINFO before.
920 The order in which vtables are built (by calling this function) for
921 an object must remain the same, otherwise a binary incompatibility
922 can result. */
924 static int
925 build_secondary_vtable (tree binfo)
927 if (BINFO_NEW_VTABLE_MARKED (binfo))
928 /* We already created a vtable for this base. There's no need to
929 do it again. */
930 return 0;
932 /* Remember that we've created a vtable for this BINFO, so that we
933 don't try to do so again. */
934 SET_BINFO_NEW_VTABLE_MARKED (binfo);
936 /* Make fresh virtual list, so we can smash it later. */
937 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
939 /* Secondary vtables are laid out as part of the same structure as
940 the primary vtable. */
941 BINFO_VTABLE (binfo) = NULL_TREE;
942 return 1;
945 /* Create a new vtable for BINFO which is the hierarchy dominated by
946 T. Return nonzero if we actually created a new vtable. */
948 static int
949 make_new_vtable (tree t, tree binfo)
951 if (binfo == TYPE_BINFO (t))
952 /* In this case, it is *type*'s vtable we are modifying. We start
953 with the approximation that its vtable is that of the
954 immediate base class. */
955 return build_primary_vtable (binfo, t);
956 else
957 /* This is our very own copy of `basetype' to play with. Later,
958 we will fill in all the virtual functions that override the
959 virtual functions in these base classes which are not defined
960 by the current type. */
961 return build_secondary_vtable (binfo);
964 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
965 (which is in the hierarchy dominated by T) list FNDECL as its
966 BV_FN. DELTA is the required constant adjustment from the `this'
967 pointer where the vtable entry appears to the `this' required when
968 the function is actually called. */
970 static void
971 modify_vtable_entry (tree t,
972 tree binfo,
973 tree fndecl,
974 tree delta,
975 tree *virtuals)
977 tree v;
979 v = *virtuals;
981 if (fndecl != BV_FN (v)
982 || !tree_int_cst_equal (delta, BV_DELTA (v)))
984 /* We need a new vtable for BINFO. */
985 if (make_new_vtable (t, binfo))
987 /* If we really did make a new vtable, we also made a copy
988 of the BINFO_VIRTUALS list. Now, we have to find the
989 corresponding entry in that list. */
990 *virtuals = BINFO_VIRTUALS (binfo);
991 while (BV_FN (*virtuals) != BV_FN (v))
992 *virtuals = TREE_CHAIN (*virtuals);
993 v = *virtuals;
996 BV_DELTA (v) = delta;
997 BV_VCALL_INDEX (v) = NULL_TREE;
998 BV_FN (v) = fndecl;
1003 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is
1004 the USING_DECL naming METHOD. Returns true if the method could be
1005 added to the method vec. */
1007 bool
1008 add_method (tree type, tree method, tree using_decl)
1010 unsigned slot;
1011 tree overload;
1012 bool template_conv_p = false;
1013 bool conv_p;
1014 vec<tree, va_gc> *method_vec;
1015 bool complete_p;
1016 bool insert_p = false;
1017 tree current_fns;
1018 tree fns;
1020 if (method == error_mark_node)
1021 return false;
1023 complete_p = COMPLETE_TYPE_P (type);
1024 conv_p = DECL_CONV_FN_P (method);
1025 if (conv_p)
1026 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1027 && DECL_TEMPLATE_CONV_FN_P (method));
1029 method_vec = CLASSTYPE_METHOD_VEC (type);
1030 if (!method_vec)
1032 /* Make a new method vector. We start with 8 entries. We must
1033 allocate at least two (for constructors and destructors), and
1034 we're going to end up with an assignment operator at some
1035 point as well. */
1036 vec_alloc (method_vec, 8);
1037 /* Create slots for constructors and destructors. */
1038 method_vec->quick_push (NULL_TREE);
1039 method_vec->quick_push (NULL_TREE);
1040 CLASSTYPE_METHOD_VEC (type) = method_vec;
1043 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1044 grok_special_member_properties (method);
1046 /* Constructors and destructors go in special slots. */
1047 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1048 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1049 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1050 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1051 else
1053 tree m;
1055 insert_p = true;
1056 /* See if we already have an entry with this name. */
1057 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1058 vec_safe_iterate (method_vec, slot, &m);
1059 ++slot)
1061 m = OVL_CURRENT (m);
1062 if (template_conv_p)
1064 if (TREE_CODE (m) == TEMPLATE_DECL
1065 && DECL_TEMPLATE_CONV_FN_P (m))
1066 insert_p = false;
1067 break;
1069 if (conv_p && !DECL_CONV_FN_P (m))
1070 break;
1071 if (DECL_NAME (m) == DECL_NAME (method))
1073 insert_p = false;
1074 break;
1076 if (complete_p
1077 && !DECL_CONV_FN_P (m)
1078 && DECL_NAME (m) > DECL_NAME (method))
1079 break;
1082 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1084 /* Check to see if we've already got this method. */
1085 for (fns = current_fns; fns; fns = OVL_NEXT (fns))
1087 tree fn = OVL_CURRENT (fns);
1088 tree fn_type;
1089 tree method_type;
1090 tree parms1;
1091 tree parms2;
1093 if (TREE_CODE (fn) != TREE_CODE (method))
1094 continue;
1096 /* [over.load] Member function declarations with the
1097 same name and the same parameter types cannot be
1098 overloaded if any of them is a static member
1099 function declaration.
1101 [over.load] Member function declarations with the same name and
1102 the same parameter-type-list as well as member function template
1103 declarations with the same name, the same parameter-type-list, and
1104 the same template parameter lists cannot be overloaded if any of
1105 them, but not all, have a ref-qualifier.
1107 [namespace.udecl] When a using-declaration brings names
1108 from a base class into a derived class scope, member
1109 functions in the derived class override and/or hide member
1110 functions with the same name and parameter types in a base
1111 class (rather than conflicting). */
1112 fn_type = TREE_TYPE (fn);
1113 method_type = TREE_TYPE (method);
1114 parms1 = TYPE_ARG_TYPES (fn_type);
1115 parms2 = TYPE_ARG_TYPES (method_type);
1117 /* Compare the quals on the 'this' parm. Don't compare
1118 the whole types, as used functions are treated as
1119 coming from the using class in overload resolution. */
1120 if (! DECL_STATIC_FUNCTION_P (fn)
1121 && ! DECL_STATIC_FUNCTION_P (method)
1122 /* Either both or neither need to be ref-qualified for
1123 differing quals to allow overloading. */
1124 && (FUNCTION_REF_QUALIFIED (fn_type)
1125 == FUNCTION_REF_QUALIFIED (method_type))
1126 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1127 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1128 continue;
1130 /* For templates, the return type and template parameters
1131 must be identical. */
1132 if (TREE_CODE (fn) == TEMPLATE_DECL
1133 && (!same_type_p (TREE_TYPE (fn_type),
1134 TREE_TYPE (method_type))
1135 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1136 DECL_TEMPLATE_PARMS (method))))
1137 continue;
1139 if (! DECL_STATIC_FUNCTION_P (fn))
1140 parms1 = TREE_CHAIN (parms1);
1141 if (! DECL_STATIC_FUNCTION_P (method))
1142 parms2 = TREE_CHAIN (parms2);
1144 if (compparms (parms1, parms2)
1145 && (!DECL_CONV_FN_P (fn)
1146 || same_type_p (TREE_TYPE (fn_type),
1147 TREE_TYPE (method_type)))
1148 && equivalently_constrained (fn, method))
1150 /* For function versions, their parms and types match
1151 but they are not duplicates. Record function versions
1152 as and when they are found. extern "C" functions are
1153 not treated as versions. */
1154 if (TREE_CODE (fn) == FUNCTION_DECL
1155 && TREE_CODE (method) == FUNCTION_DECL
1156 && !DECL_EXTERN_C_P (fn)
1157 && !DECL_EXTERN_C_P (method)
1158 && targetm.target_option.function_versions (fn, method))
1160 /* Mark functions as versions if necessary. Modify the mangled
1161 decl name if necessary. */
1162 if (!DECL_FUNCTION_VERSIONED (fn))
1164 DECL_FUNCTION_VERSIONED (fn) = 1;
1165 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1166 mangle_decl (fn);
1168 if (!DECL_FUNCTION_VERSIONED (method))
1170 DECL_FUNCTION_VERSIONED (method) = 1;
1171 if (DECL_ASSEMBLER_NAME_SET_P (method))
1172 mangle_decl (method);
1174 cgraph_node::record_function_versions (fn, method);
1175 continue;
1177 if (DECL_INHERITED_CTOR_BASE (method))
1179 if (DECL_INHERITED_CTOR_BASE (fn))
1181 error_at (DECL_SOURCE_LOCATION (method),
1182 "%q#D inherited from %qT", method,
1183 DECL_INHERITED_CTOR_BASE (method));
1184 error_at (DECL_SOURCE_LOCATION (fn),
1185 "conflicts with version inherited from %qT",
1186 DECL_INHERITED_CTOR_BASE (fn));
1188 /* Otherwise defer to the other function. */
1189 return false;
1191 if (using_decl)
1193 if (DECL_CONTEXT (fn) == type)
1194 /* Defer to the local function. */
1195 return false;
1197 else
1199 error ("%q+#D cannot be overloaded", method);
1200 error ("with %q+#D", fn);
1203 /* We don't call duplicate_decls here to merge the
1204 declarations because that will confuse things if the
1205 methods have inline definitions. In particular, we
1206 will crash while processing the definitions. */
1207 return false;
1211 /* A class should never have more than one destructor. */
1212 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1213 return false;
1215 /* Add the new binding. */
1216 if (using_decl)
1218 overload = ovl_cons (method, current_fns);
1219 OVL_USED (overload) = true;
1221 else
1222 overload = build_overload (method, current_fns);
1224 if (conv_p)
1225 TYPE_HAS_CONVERSION (type) = 1;
1226 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1227 push_class_level_binding (DECL_NAME (method), overload);
1229 if (insert_p)
1231 bool reallocated;
1233 /* We only expect to add few methods in the COMPLETE_P case, so
1234 just make room for one more method in that case. */
1235 if (complete_p)
1236 reallocated = vec_safe_reserve_exact (method_vec, 1);
1237 else
1238 reallocated = vec_safe_reserve (method_vec, 1);
1239 if (reallocated)
1240 CLASSTYPE_METHOD_VEC (type) = method_vec;
1241 if (slot == method_vec->length ())
1242 method_vec->quick_push (overload);
1243 else
1244 method_vec->quick_insert (slot, overload);
1246 else
1247 /* Replace the current slot. */
1248 (*method_vec)[slot] = overload;
1249 return true;
1252 /* Subroutines of finish_struct. */
1254 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1255 legit, otherwise return 0. */
1257 static int
1258 alter_access (tree t, tree fdecl, tree access)
1260 tree elem;
1262 if (!DECL_LANG_SPECIFIC (fdecl))
1263 retrofit_lang_decl (fdecl);
1265 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1267 elem = purpose_member (t, DECL_ACCESS (fdecl));
1268 if (elem)
1270 if (TREE_VALUE (elem) != access)
1272 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1273 error ("conflicting access specifications for method"
1274 " %q+D, ignored", TREE_TYPE (fdecl));
1275 else
1276 error ("conflicting access specifications for field %qE, ignored",
1277 DECL_NAME (fdecl));
1279 else
1281 /* They're changing the access to the same thing they changed
1282 it to before. That's OK. */
1286 else
1288 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1289 tf_warning_or_error);
1290 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1291 return 1;
1293 return 0;
1296 /* Process the USING_DECL, which is a member of T. */
1298 static void
1299 handle_using_decl (tree using_decl, tree t)
1301 tree decl = USING_DECL_DECLS (using_decl);
1302 tree name = DECL_NAME (using_decl);
1303 tree access
1304 = TREE_PRIVATE (using_decl) ? access_private_node
1305 : TREE_PROTECTED (using_decl) ? access_protected_node
1306 : access_public_node;
1307 tree flist = NULL_TREE;
1308 tree old_value;
1310 gcc_assert (!processing_template_decl && decl);
1312 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1313 tf_warning_or_error);
1314 if (old_value)
1316 if (is_overloaded_fn (old_value))
1317 old_value = OVL_CURRENT (old_value);
1319 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1320 /* OK */;
1321 else
1322 old_value = NULL_TREE;
1325 cp_emit_debug_info_for_using (decl, t);
1327 if (is_overloaded_fn (decl))
1328 flist = decl;
1330 if (! old_value)
1332 else if (is_overloaded_fn (old_value))
1334 if (flist)
1335 /* It's OK to use functions from a base when there are functions with
1336 the same name already present in the current class. */;
1337 else
1339 error ("%q+D invalid in %q#T", using_decl, t);
1340 error (" because of local method %q+#D with same name",
1341 OVL_CURRENT (old_value));
1342 return;
1345 else if (!DECL_ARTIFICIAL (old_value))
1347 error ("%q+D invalid in %q#T", using_decl, t);
1348 error (" because of local member %q+#D with same name", old_value);
1349 return;
1352 /* Make type T see field decl FDECL with access ACCESS. */
1353 if (flist)
1354 for (; flist; flist = OVL_NEXT (flist))
1356 add_method (t, OVL_CURRENT (flist), using_decl);
1357 alter_access (t, OVL_CURRENT (flist), access);
1359 else
1360 alter_access (t, decl, access);
1363 /* Data structure for find_abi_tags_r, below. */
1365 struct abi_tag_data
1367 tree t; // The type that we're checking for missing tags.
1368 tree subob; // The subobject of T that we're getting tags from.
1369 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1372 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1373 in the context of P. TAG can be either an identifier (the DECL_NAME of
1374 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1376 static void
1377 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1379 if (!IDENTIFIER_MARKED (id))
1381 if (p->tags != error_mark_node)
1383 /* We're collecting tags from template arguments or from
1384 the type of a variable or function return type. */
1385 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1387 /* Don't inherit this tag multiple times. */
1388 IDENTIFIER_MARKED (id) = true;
1390 if (TYPE_P (p->t))
1392 /* Tags inherited from type template arguments are only used
1393 to avoid warnings. */
1394 ABI_TAG_IMPLICIT (p->tags) = true;
1395 return;
1397 /* For functions and variables we want to warn, too. */
1400 /* Otherwise we're diagnosing missing tags. */
1401 if (TREE_CODE (p->t) == FUNCTION_DECL)
1403 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1404 "that %qT (used in its return type) has",
1405 p->t, tag, *tp))
1406 inform (location_of (*tp), "%qT declared here", *tp);
1408 else if (VAR_P (p->t))
1410 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1411 "that %qT (used in its type) has", p->t, tag, *tp))
1412 inform (location_of (*tp), "%qT declared here", *tp);
1414 else if (TYPE_P (p->subob))
1416 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1417 "that base %qT has", p->t, tag, p->subob))
1418 inform (location_of (p->subob), "%qT declared here",
1419 p->subob);
1421 else
1423 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1424 "that %qT (used in the type of %qD) has",
1425 p->t, tag, *tp, p->subob))
1427 inform (location_of (p->subob), "%qD declared here",
1428 p->subob);
1429 inform (location_of (*tp), "%qT declared here", *tp);
1435 /* Find all the ABI tags in the attribute list ATTR and either call
1436 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1438 static void
1439 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1441 if (!attr)
1442 return;
1443 for (; (attr = lookup_attribute ("abi_tag", attr));
1444 attr = TREE_CHAIN (attr))
1445 for (tree list = TREE_VALUE (attr); list;
1446 list = TREE_CHAIN (list))
1448 tree tag = TREE_VALUE (list);
1449 tree id = get_identifier (TREE_STRING_POINTER (tag));
1450 if (tp)
1451 check_tag (tag, id, tp, p);
1452 else
1453 IDENTIFIER_MARKED (id) = val;
1457 /* Find all the ABI tags on T and its enclosing scopes and either call
1458 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1460 static void
1461 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1463 while (t != global_namespace)
1465 tree attr;
1466 if (TYPE_P (t))
1468 attr = TYPE_ATTRIBUTES (t);
1469 t = CP_TYPE_CONTEXT (t);
1471 else
1473 attr = DECL_ATTRIBUTES (t);
1474 t = CP_DECL_CONTEXT (t);
1476 mark_or_check_attr_tags (attr, tp, p, val);
1480 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1481 types with ABI tags, add the corresponding identifiers to the VEC in
1482 *DATA and set IDENTIFIER_MARKED. */
1484 static tree
1485 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1487 if (!OVERLOAD_TYPE_P (*tp))
1488 return NULL_TREE;
1490 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1491 anyway, but let's make sure of it. */
1492 *walk_subtrees = false;
1494 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1496 mark_or_check_tags (*tp, tp, p, false);
1498 return NULL_TREE;
1501 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1502 IDENTIFIER_MARKED on its ABI tags. */
1504 static tree
1505 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1507 if (!OVERLOAD_TYPE_P (*tp))
1508 return NULL_TREE;
1510 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1511 anyway, but let's make sure of it. */
1512 *walk_subtrees = false;
1514 bool *valp = static_cast<bool*>(data);
1516 mark_or_check_tags (*tp, NULL, NULL, *valp);
1518 return NULL_TREE;
1521 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1522 scopes. */
1524 static void
1525 mark_abi_tags (tree t, bool val)
1527 mark_or_check_tags (t, NULL, NULL, val);
1528 if (DECL_P (t))
1530 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1531 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1533 /* Template arguments are part of the signature. */
1534 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1535 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1537 tree arg = TREE_VEC_ELT (level, j);
1538 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1541 if (TREE_CODE (t) == FUNCTION_DECL)
1542 /* A function's parameter types are part of the signature, so
1543 we don't need to inherit any tags that are also in them. */
1544 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1545 arg = TREE_CHAIN (arg))
1546 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1547 mark_abi_tags_r, &val);
1551 /* Check that T has all the ABI tags that subobject SUBOB has, or
1552 warn if not. If T is a (variable or function) declaration, also
1553 return any missing tags, and add them to T if JUST_CHECKING is false. */
1555 static tree
1556 check_abi_tags (tree t, tree subob, bool just_checking = false)
1558 bool inherit = DECL_P (t);
1560 if (!inherit && !warn_abi_tag)
1561 return NULL_TREE;
1563 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1564 if (!TREE_PUBLIC (decl))
1565 /* No need to worry about things local to this TU. */
1566 return NULL_TREE;
1568 mark_abi_tags (t, true);
1570 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1571 struct abi_tag_data data = { t, subob, error_mark_node };
1572 if (inherit)
1573 data.tags = NULL_TREE;
1575 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1577 if (!(inherit && data.tags))
1578 /* We don't need to do anything with data.tags. */;
1579 else if (just_checking)
1580 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1582 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1583 IDENTIFIER_MARKED (id) = false;
1585 else
1587 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1588 if (attr)
1589 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1590 else
1591 DECL_ATTRIBUTES (t)
1592 = tree_cons (get_identifier ("abi_tag"), data.tags,
1593 DECL_ATTRIBUTES (t));
1596 mark_abi_tags (t, false);
1598 return data.tags;
1601 /* Check that DECL has all the ABI tags that are used in parts of its type
1602 that are not reflected in its mangled name. */
1604 void
1605 check_abi_tags (tree decl)
1607 if (VAR_P (decl))
1608 check_abi_tags (decl, TREE_TYPE (decl));
1609 else if (TREE_CODE (decl) == FUNCTION_DECL
1610 && !DECL_CONV_FN_P (decl)
1611 && !mangle_return_type_p (decl))
1612 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1615 /* Return any ABI tags that are used in parts of the type of DECL
1616 that are not reflected in its mangled name. This function is only
1617 used in backward-compatible mangling for ABI <11. */
1619 tree
1620 missing_abi_tags (tree decl)
1622 if (VAR_P (decl))
1623 return check_abi_tags (decl, TREE_TYPE (decl), true);
1624 else if (TREE_CODE (decl) == FUNCTION_DECL
1625 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1626 that we can use this function for setting need_abi_warning
1627 regardless of the current flag_abi_version. */
1628 && !mangle_return_type_p (decl))
1629 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1630 else
1631 return NULL_TREE;
1634 void
1635 inherit_targ_abi_tags (tree t)
1637 if (!CLASS_TYPE_P (t)
1638 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1639 return;
1641 mark_abi_tags (t, true);
1643 tree args = CLASSTYPE_TI_ARGS (t);
1644 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1645 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1647 tree level = TMPL_ARGS_LEVEL (args, i+1);
1648 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1650 tree arg = TREE_VEC_ELT (level, j);
1651 data.subob = arg;
1652 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1656 // If we found some tags on our template arguments, add them to our
1657 // abi_tag attribute.
1658 if (data.tags)
1660 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1661 if (attr)
1662 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1663 else
1664 TYPE_ATTRIBUTES (t)
1665 = tree_cons (get_identifier ("abi_tag"), data.tags,
1666 TYPE_ATTRIBUTES (t));
1669 mark_abi_tags (t, false);
1672 /* Return true, iff class T has a non-virtual destructor that is
1673 accessible from outside the class heirarchy (i.e. is public, or
1674 there's a suitable friend. */
1676 static bool
1677 accessible_nvdtor_p (tree t)
1679 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1681 /* An implicitly declared destructor is always public. And,
1682 if it were virtual, we would have created it by now. */
1683 if (!dtor)
1684 return true;
1686 if (DECL_VINDEX (dtor))
1687 return false; /* Virtual */
1689 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1690 return true; /* Public */
1692 if (CLASSTYPE_FRIEND_CLASSES (t)
1693 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1694 return true; /* Has friends */
1696 return false;
1699 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1700 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1701 properties of the bases. */
1703 static void
1704 check_bases (tree t,
1705 int* cant_have_const_ctor_p,
1706 int* no_const_asn_ref_p)
1708 int i;
1709 bool seen_non_virtual_nearly_empty_base_p = 0;
1710 int seen_tm_mask = 0;
1711 tree base_binfo;
1712 tree binfo;
1713 tree field = NULL_TREE;
1715 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1716 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1717 if (TREE_CODE (field) == FIELD_DECL)
1718 break;
1720 for (binfo = TYPE_BINFO (t), i = 0;
1721 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1723 tree basetype = TREE_TYPE (base_binfo);
1725 gcc_assert (COMPLETE_TYPE_P (basetype));
1727 if (CLASSTYPE_FINAL (basetype))
1728 error ("cannot derive from %<final%> base %qT in derived type %qT",
1729 basetype, t);
1731 /* If any base class is non-literal, so is the derived class. */
1732 if (!CLASSTYPE_LITERAL_P (basetype))
1733 CLASSTYPE_LITERAL_P (t) = false;
1735 /* If the base class doesn't have copy constructors or
1736 assignment operators that take const references, then the
1737 derived class cannot have such a member automatically
1738 generated. */
1739 if (TYPE_HAS_COPY_CTOR (basetype)
1740 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1741 *cant_have_const_ctor_p = 1;
1742 if (TYPE_HAS_COPY_ASSIGN (basetype)
1743 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1744 *no_const_asn_ref_p = 1;
1746 if (BINFO_VIRTUAL_P (base_binfo))
1747 /* A virtual base does not effect nearly emptiness. */
1749 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1751 if (seen_non_virtual_nearly_empty_base_p)
1752 /* And if there is more than one nearly empty base, then the
1753 derived class is not nearly empty either. */
1754 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1755 else
1756 /* Remember we've seen one. */
1757 seen_non_virtual_nearly_empty_base_p = 1;
1759 else if (!is_empty_class (basetype))
1760 /* If the base class is not empty or nearly empty, then this
1761 class cannot be nearly empty. */
1762 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1764 /* A lot of properties from the bases also apply to the derived
1765 class. */
1766 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1767 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1768 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1769 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1770 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1771 || !TYPE_HAS_COPY_ASSIGN (basetype));
1772 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1773 || !TYPE_HAS_COPY_CTOR (basetype));
1774 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1775 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1776 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1777 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1778 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1779 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1780 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1781 || TYPE_HAS_COMPLEX_DFLT (basetype));
1782 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1783 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1784 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1785 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1786 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1787 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1788 if (TYPE_HAS_MUTABLE_P (basetype))
1789 CLASSTYPE_HAS_MUTABLE (t) = 1;
1791 /* A standard-layout class is a class that:
1793 * has no non-standard-layout base classes, */
1794 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1795 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1797 tree basefield;
1798 /* ...has no base classes of the same type as the first non-static
1799 data member... */
1800 if (field && DECL_CONTEXT (field) == t
1801 && (same_type_ignoring_top_level_qualifiers_p
1802 (TREE_TYPE (field), basetype)))
1803 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1804 else
1805 /* ...either has no non-static data members in the most-derived
1806 class and at most one base class with non-static data
1807 members, or has no base classes with non-static data
1808 members */
1809 for (basefield = TYPE_FIELDS (basetype); basefield;
1810 basefield = DECL_CHAIN (basefield))
1811 if (TREE_CODE (basefield) == FIELD_DECL)
1813 if (field)
1814 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1815 else
1816 field = basefield;
1817 break;
1821 /* Don't bother collecting tm attributes if transactional memory
1822 support is not enabled. */
1823 if (flag_tm)
1825 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1826 if (tm_attr)
1827 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1830 check_abi_tags (t, basetype);
1833 /* If one of the base classes had TM attributes, and the current class
1834 doesn't define its own, then the current class inherits one. */
1835 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1837 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1838 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1842 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1843 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1844 that have had a nearly-empty virtual primary base stolen by some
1845 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1846 T. */
1848 static void
1849 determine_primary_bases (tree t)
1851 unsigned i;
1852 tree primary = NULL_TREE;
1853 tree type_binfo = TYPE_BINFO (t);
1854 tree base_binfo;
1856 /* Determine the primary bases of our bases. */
1857 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1858 base_binfo = TREE_CHAIN (base_binfo))
1860 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1862 /* See if we're the non-virtual primary of our inheritance
1863 chain. */
1864 if (!BINFO_VIRTUAL_P (base_binfo))
1866 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1867 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1869 if (parent_primary
1870 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1871 BINFO_TYPE (parent_primary)))
1872 /* We are the primary binfo. */
1873 BINFO_PRIMARY_P (base_binfo) = 1;
1875 /* Determine if we have a virtual primary base, and mark it so.
1877 if (primary && BINFO_VIRTUAL_P (primary))
1879 tree this_primary = copied_binfo (primary, base_binfo);
1881 if (BINFO_PRIMARY_P (this_primary))
1882 /* Someone already claimed this base. */
1883 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1884 else
1886 tree delta;
1888 BINFO_PRIMARY_P (this_primary) = 1;
1889 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1891 /* A virtual binfo might have been copied from within
1892 another hierarchy. As we're about to use it as a
1893 primary base, make sure the offsets match. */
1894 delta = size_diffop_loc (input_location,
1895 fold_convert (ssizetype,
1896 BINFO_OFFSET (base_binfo)),
1897 fold_convert (ssizetype,
1898 BINFO_OFFSET (this_primary)));
1900 propagate_binfo_offsets (this_primary, delta);
1905 /* First look for a dynamic direct non-virtual base. */
1906 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1908 tree basetype = BINFO_TYPE (base_binfo);
1910 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1912 primary = base_binfo;
1913 goto found;
1917 /* A "nearly-empty" virtual base class can be the primary base
1918 class, if no non-virtual polymorphic base can be found. Look for
1919 a nearly-empty virtual dynamic base that is not already a primary
1920 base of something in the hierarchy. If there is no such base,
1921 just pick the first nearly-empty virtual base. */
1923 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1924 base_binfo = TREE_CHAIN (base_binfo))
1925 if (BINFO_VIRTUAL_P (base_binfo)
1926 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1928 if (!BINFO_PRIMARY_P (base_binfo))
1930 /* Found one that is not primary. */
1931 primary = base_binfo;
1932 goto found;
1934 else if (!primary)
1935 /* Remember the first candidate. */
1936 primary = base_binfo;
1939 found:
1940 /* If we've got a primary base, use it. */
1941 if (primary)
1943 tree basetype = BINFO_TYPE (primary);
1945 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1946 if (BINFO_PRIMARY_P (primary))
1947 /* We are stealing a primary base. */
1948 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1949 BINFO_PRIMARY_P (primary) = 1;
1950 if (BINFO_VIRTUAL_P (primary))
1952 tree delta;
1954 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1955 /* A virtual binfo might have been copied from within
1956 another hierarchy. As we're about to use it as a primary
1957 base, make sure the offsets match. */
1958 delta = size_diffop_loc (input_location, ssize_int (0),
1959 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1961 propagate_binfo_offsets (primary, delta);
1964 primary = TYPE_BINFO (basetype);
1966 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1967 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1968 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1972 /* Update the variant types of T. */
1974 void
1975 fixup_type_variants (tree t)
1977 tree variants;
1979 if (!t)
1980 return;
1982 for (variants = TYPE_NEXT_VARIANT (t);
1983 variants;
1984 variants = TYPE_NEXT_VARIANT (variants))
1986 /* These fields are in the _TYPE part of the node, not in
1987 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1988 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1989 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1990 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1991 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1993 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1995 TYPE_BINFO (variants) = TYPE_BINFO (t);
1997 /* Copy whatever these are holding today. */
1998 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1999 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2003 /* KLASS is a class that we're applying may_alias to after the body is
2004 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2005 canonical type(s) will be implicitly updated. */
2007 static void
2008 fixup_may_alias (tree klass)
2010 tree t;
2012 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2013 TYPE_REF_CAN_ALIAS_ALL (t) = true;
2014 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2015 TYPE_REF_CAN_ALIAS_ALL (t) = true;
2018 /* Early variant fixups: we apply attributes at the beginning of the class
2019 definition, and we need to fix up any variants that have already been
2020 made via elaborated-type-specifier so that check_qualified_type works. */
2022 void
2023 fixup_attribute_variants (tree t)
2025 tree variants;
2027 if (!t)
2028 return;
2030 tree attrs = TYPE_ATTRIBUTES (t);
2031 unsigned align = TYPE_ALIGN (t);
2032 bool user_align = TYPE_USER_ALIGN (t);
2033 bool may_alias = lookup_attribute ("may_alias", attrs);
2035 if (may_alias)
2036 fixup_may_alias (t);
2038 for (variants = TYPE_NEXT_VARIANT (t);
2039 variants;
2040 variants = TYPE_NEXT_VARIANT (variants))
2042 /* These are the two fields that check_qualified_type looks at and
2043 are affected by attributes. */
2044 TYPE_ATTRIBUTES (variants) = attrs;
2045 unsigned valign = align;
2046 if (TYPE_USER_ALIGN (variants))
2047 valign = MAX (valign, TYPE_ALIGN (variants));
2048 else
2049 TYPE_USER_ALIGN (variants) = user_align;
2050 SET_TYPE_ALIGN (variants, valign);
2051 if (may_alias)
2052 fixup_may_alias (variants);
2056 /* Set memoizing fields and bits of T (and its variants) for later
2057 use. */
2059 static void
2060 finish_struct_bits (tree t)
2062 /* Fix up variants (if any). */
2063 fixup_type_variants (t);
2065 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2066 /* For a class w/o baseclasses, 'finish_struct' has set
2067 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2068 Similarly for a class whose base classes do not have vtables.
2069 When neither of these is true, we might have removed abstract
2070 virtuals (by providing a definition), added some (by declaring
2071 new ones), or redeclared ones from a base class. We need to
2072 recalculate what's really an abstract virtual at this point (by
2073 looking in the vtables). */
2074 get_pure_virtuals (t);
2076 /* If this type has a copy constructor or a destructor, force its
2077 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2078 nonzero. This will cause it to be passed by invisible reference
2079 and prevent it from being returned in a register. */
2080 if (type_has_nontrivial_copy_init (t)
2081 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2083 tree variants;
2084 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2085 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2087 SET_TYPE_MODE (variants, BLKmode);
2088 TREE_ADDRESSABLE (variants) = 1;
2093 /* Issue warnings about T having private constructors, but no friends,
2094 and so forth.
2096 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2097 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2098 non-private static member functions. */
2100 static void
2101 maybe_warn_about_overly_private_class (tree t)
2103 int has_member_fn = 0;
2104 int has_nonprivate_method = 0;
2105 tree fn;
2107 if (!warn_ctor_dtor_privacy
2108 /* If the class has friends, those entities might create and
2109 access instances, so we should not warn. */
2110 || (CLASSTYPE_FRIEND_CLASSES (t)
2111 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2112 /* We will have warned when the template was declared; there's
2113 no need to warn on every instantiation. */
2114 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2115 /* There's no reason to even consider warning about this
2116 class. */
2117 return;
2119 /* We only issue one warning, if more than one applies, because
2120 otherwise, on code like:
2122 class A {
2123 // Oops - forgot `public:'
2124 A();
2125 A(const A&);
2126 ~A();
2129 we warn several times about essentially the same problem. */
2131 /* Check to see if all (non-constructor, non-destructor) member
2132 functions are private. (Since there are no friends or
2133 non-private statics, we can't ever call any of the private member
2134 functions.) */
2135 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2136 /* We're not interested in compiler-generated methods; they don't
2137 provide any way to call private members. */
2138 if (!DECL_ARTIFICIAL (fn))
2140 if (!TREE_PRIVATE (fn))
2142 if (DECL_STATIC_FUNCTION_P (fn))
2143 /* A non-private static member function is just like a
2144 friend; it can create and invoke private member
2145 functions, and be accessed without a class
2146 instance. */
2147 return;
2149 has_nonprivate_method = 1;
2150 /* Keep searching for a static member function. */
2152 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2153 has_member_fn = 1;
2156 if (!has_nonprivate_method && has_member_fn)
2158 /* There are no non-private methods, and there's at least one
2159 private member function that isn't a constructor or
2160 destructor. (If all the private members are
2161 constructors/destructors we want to use the code below that
2162 issues error messages specifically referring to
2163 constructors/destructors.) */
2164 unsigned i;
2165 tree binfo = TYPE_BINFO (t);
2167 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2168 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2170 has_nonprivate_method = 1;
2171 break;
2173 if (!has_nonprivate_method)
2175 warning (OPT_Wctor_dtor_privacy,
2176 "all member functions in class %qT are private", t);
2177 return;
2181 /* Even if some of the member functions are non-private, the class
2182 won't be useful for much if all the constructors or destructors
2183 are private: such an object can never be created or destroyed. */
2184 fn = CLASSTYPE_DESTRUCTORS (t);
2185 if (fn && TREE_PRIVATE (fn))
2187 warning (OPT_Wctor_dtor_privacy,
2188 "%q#T only defines a private destructor and has no friends",
2190 return;
2193 /* Warn about classes that have private constructors and no friends. */
2194 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2195 /* Implicitly generated constructors are always public. */
2196 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2197 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2199 int nonprivate_ctor = 0;
2201 /* If a non-template class does not define a copy
2202 constructor, one is defined for it, enabling it to avoid
2203 this warning. For a template class, this does not
2204 happen, and so we would normally get a warning on:
2206 template <class T> class C { private: C(); };
2208 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2209 complete non-template or fully instantiated classes have this
2210 flag set. */
2211 if (!TYPE_HAS_COPY_CTOR (t))
2212 nonprivate_ctor = 1;
2213 else
2214 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
2216 tree ctor = OVL_CURRENT (fn);
2217 /* Ideally, we wouldn't count copy constructors (or, in
2218 fact, any constructor that takes an argument of the
2219 class type as a parameter) because such things cannot
2220 be used to construct an instance of the class unless
2221 you already have one. But, for now at least, we're
2222 more generous. */
2223 if (! TREE_PRIVATE (ctor))
2225 nonprivate_ctor = 1;
2226 break;
2230 if (nonprivate_ctor == 0)
2232 warning (OPT_Wctor_dtor_privacy,
2233 "%q#T only defines private constructors and has no friends",
2235 return;
2240 static struct {
2241 gt_pointer_operator new_value;
2242 void *cookie;
2243 } resort_data;
2245 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2247 static int
2248 method_name_cmp (const void* m1_p, const void* m2_p)
2250 const tree *const m1 = (const tree *) m1_p;
2251 const tree *const m2 = (const tree *) m2_p;
2253 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2254 return 0;
2255 if (*m1 == NULL_TREE)
2256 return -1;
2257 if (*m2 == NULL_TREE)
2258 return 1;
2259 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2260 return -1;
2261 return 1;
2264 /* This routine compares two fields like method_name_cmp but using the
2265 pointer operator in resort_field_decl_data. */
2267 static int
2268 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2270 const tree *const m1 = (const tree *) m1_p;
2271 const tree *const m2 = (const tree *) m2_p;
2272 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2273 return 0;
2274 if (*m1 == NULL_TREE)
2275 return -1;
2276 if (*m2 == NULL_TREE)
2277 return 1;
2279 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
2280 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
2281 resort_data.new_value (&d1, resort_data.cookie);
2282 resort_data.new_value (&d2, resort_data.cookie);
2283 if (d1 < d2)
2284 return -1;
2286 return 1;
2289 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2291 void
2292 resort_type_method_vec (void* obj,
2293 void* /*orig_obj*/,
2294 gt_pointer_operator new_value,
2295 void* cookie)
2297 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2298 int len = vec_safe_length (method_vec);
2299 size_t slot;
2300 tree fn;
2302 /* The type conversion ops have to live at the front of the vec, so we
2303 can't sort them. */
2304 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2305 vec_safe_iterate (method_vec, slot, &fn);
2306 ++slot)
2307 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2308 break;
2310 if (len - slot > 1)
2312 resort_data.new_value = new_value;
2313 resort_data.cookie = cookie;
2314 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2315 resort_method_name_cmp);
2319 /* Warn about duplicate methods in fn_fields.
2321 Sort methods that are not special (i.e., constructors, destructors,
2322 and type conversion operators) so that we can find them faster in
2323 search. */
2325 static void
2326 finish_struct_methods (tree t)
2328 tree fn_fields;
2329 vec<tree, va_gc> *method_vec;
2330 int slot, len;
2332 method_vec = CLASSTYPE_METHOD_VEC (t);
2333 if (!method_vec)
2334 return;
2336 len = method_vec->length ();
2338 /* Clear DECL_IN_AGGR_P for all functions. */
2339 for (fn_fields = TYPE_METHODS (t); fn_fields;
2340 fn_fields = DECL_CHAIN (fn_fields))
2341 DECL_IN_AGGR_P (fn_fields) = 0;
2343 /* Issue warnings about private constructors and such. If there are
2344 no methods, then some public defaults are generated. */
2345 maybe_warn_about_overly_private_class (t);
2347 /* The type conversion ops have to live at the front of the vec, so we
2348 can't sort them. */
2349 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2350 method_vec->iterate (slot, &fn_fields);
2351 ++slot)
2352 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
2353 break;
2354 if (len - slot > 1)
2355 qsort (method_vec->address () + slot,
2356 len-slot, sizeof (tree), method_name_cmp);
2359 /* Make BINFO's vtable have N entries, including RTTI entries,
2360 vbase and vcall offsets, etc. Set its type and call the back end
2361 to lay it out. */
2363 static void
2364 layout_vtable_decl (tree binfo, int n)
2366 tree atype;
2367 tree vtable;
2369 atype = build_array_of_n_type (vtable_entry_type, n);
2370 layout_type (atype);
2372 /* We may have to grow the vtable. */
2373 vtable = get_vtbl_decl_for_binfo (binfo);
2374 if (!same_type_p (TREE_TYPE (vtable), atype))
2376 TREE_TYPE (vtable) = atype;
2377 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2378 layout_decl (vtable, 0);
2382 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2383 have the same signature. */
2386 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2388 /* One destructor overrides another if they are the same kind of
2389 destructor. */
2390 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2391 && special_function_p (base_fndecl) == special_function_p (fndecl))
2392 return 1;
2393 /* But a non-destructor never overrides a destructor, nor vice
2394 versa, nor do different kinds of destructors override
2395 one-another. For example, a complete object destructor does not
2396 override a deleting destructor. */
2397 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2398 return 0;
2400 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2401 || (DECL_CONV_FN_P (fndecl)
2402 && DECL_CONV_FN_P (base_fndecl)
2403 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2404 DECL_CONV_FN_TYPE (base_fndecl))))
2406 tree fntype = TREE_TYPE (fndecl);
2407 tree base_fntype = TREE_TYPE (base_fndecl);
2408 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2409 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2410 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2411 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2412 return 1;
2414 return 0;
2417 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2418 subobject. */
2420 static bool
2421 base_derived_from (tree derived, tree base)
2423 tree probe;
2425 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2427 if (probe == derived)
2428 return true;
2429 else if (BINFO_VIRTUAL_P (probe))
2430 /* If we meet a virtual base, we can't follow the inheritance
2431 any more. See if the complete type of DERIVED contains
2432 such a virtual base. */
2433 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2434 != NULL_TREE);
2436 return false;
2439 struct find_final_overrider_data {
2440 /* The function for which we are trying to find a final overrider. */
2441 tree fn;
2442 /* The base class in which the function was declared. */
2443 tree declaring_base;
2444 /* The candidate overriders. */
2445 tree candidates;
2446 /* Path to most derived. */
2447 vec<tree> path;
2450 /* Add the overrider along the current path to FFOD->CANDIDATES.
2451 Returns true if an overrider was found; false otherwise. */
2453 static bool
2454 dfs_find_final_overrider_1 (tree binfo,
2455 find_final_overrider_data *ffod,
2456 unsigned depth)
2458 tree method;
2460 /* If BINFO is not the most derived type, try a more derived class.
2461 A definition there will overrider a definition here. */
2462 if (depth)
2464 depth--;
2465 if (dfs_find_final_overrider_1
2466 (ffod->path[depth], ffod, depth))
2467 return true;
2470 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2471 if (method)
2473 tree *candidate = &ffod->candidates;
2475 /* Remove any candidates overridden by this new function. */
2476 while (*candidate)
2478 /* If *CANDIDATE overrides METHOD, then METHOD
2479 cannot override anything else on the list. */
2480 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2481 return true;
2482 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2483 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2484 *candidate = TREE_CHAIN (*candidate);
2485 else
2486 candidate = &TREE_CHAIN (*candidate);
2489 /* Add the new function. */
2490 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2491 return true;
2494 return false;
2497 /* Called from find_final_overrider via dfs_walk. */
2499 static tree
2500 dfs_find_final_overrider_pre (tree binfo, void *data)
2502 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2504 if (binfo == ffod->declaring_base)
2505 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2506 ffod->path.safe_push (binfo);
2508 return NULL_TREE;
2511 static tree
2512 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2514 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2515 ffod->path.pop ();
2517 return NULL_TREE;
2520 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2521 FN and whose TREE_VALUE is the binfo for the base where the
2522 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2523 DERIVED) is the base object in which FN is declared. */
2525 static tree
2526 find_final_overrider (tree derived, tree binfo, tree fn)
2528 find_final_overrider_data ffod;
2530 /* Getting this right is a little tricky. This is valid:
2532 struct S { virtual void f (); };
2533 struct T { virtual void f (); };
2534 struct U : public S, public T { };
2536 even though calling `f' in `U' is ambiguous. But,
2538 struct R { virtual void f(); };
2539 struct S : virtual public R { virtual void f (); };
2540 struct T : virtual public R { virtual void f (); };
2541 struct U : public S, public T { };
2543 is not -- there's no way to decide whether to put `S::f' or
2544 `T::f' in the vtable for `R'.
2546 The solution is to look at all paths to BINFO. If we find
2547 different overriders along any two, then there is a problem. */
2548 if (DECL_THUNK_P (fn))
2549 fn = THUNK_TARGET (fn);
2551 /* Determine the depth of the hierarchy. */
2552 ffod.fn = fn;
2553 ffod.declaring_base = binfo;
2554 ffod.candidates = NULL_TREE;
2555 ffod.path.create (30);
2557 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2558 dfs_find_final_overrider_post, &ffod);
2560 ffod.path.release ();
2562 /* If there was no winner, issue an error message. */
2563 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2564 return error_mark_node;
2566 return ffod.candidates;
2569 /* Return the index of the vcall offset for FN when TYPE is used as a
2570 virtual base. */
2572 static tree
2573 get_vcall_index (tree fn, tree type)
2575 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2576 tree_pair_p p;
2577 unsigned ix;
2579 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2580 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2581 || same_signature_p (fn, p->purpose))
2582 return p->value;
2584 /* There should always be an appropriate index. */
2585 gcc_unreachable ();
2588 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2589 dominated by T. FN is the old function; VIRTUALS points to the
2590 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2591 of that entry in the list. */
2593 static void
2594 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2595 unsigned ix)
2597 tree b;
2598 tree overrider;
2599 tree delta;
2600 tree virtual_base;
2601 tree first_defn;
2602 tree overrider_fn, overrider_target;
2603 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2604 tree over_return, base_return;
2605 bool lost = false;
2607 /* Find the nearest primary base (possibly binfo itself) which defines
2608 this function; this is the class the caller will convert to when
2609 calling FN through BINFO. */
2610 for (b = binfo; ; b = get_primary_binfo (b))
2612 gcc_assert (b);
2613 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2614 break;
2616 /* The nearest definition is from a lost primary. */
2617 if (BINFO_LOST_PRIMARY_P (b))
2618 lost = true;
2620 first_defn = b;
2622 /* Find the final overrider. */
2623 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2624 if (overrider == error_mark_node)
2626 error ("no unique final overrider for %qD in %qT", target_fn, t);
2627 return;
2629 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2631 /* Check for adjusting covariant return types. */
2632 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2633 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2635 if (POINTER_TYPE_P (over_return)
2636 && TREE_CODE (over_return) == TREE_CODE (base_return)
2637 && CLASS_TYPE_P (TREE_TYPE (over_return))
2638 && CLASS_TYPE_P (TREE_TYPE (base_return))
2639 /* If the overrider is invalid, don't even try. */
2640 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2642 /* If FN is a covariant thunk, we must figure out the adjustment
2643 to the final base FN was converting to. As OVERRIDER_TARGET might
2644 also be converting to the return type of FN, we have to
2645 combine the two conversions here. */
2646 tree fixed_offset, virtual_offset;
2648 over_return = TREE_TYPE (over_return);
2649 base_return = TREE_TYPE (base_return);
2651 if (DECL_THUNK_P (fn))
2653 gcc_assert (DECL_RESULT_THUNK_P (fn));
2654 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2655 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2657 else
2658 fixed_offset = virtual_offset = NULL_TREE;
2660 if (virtual_offset)
2661 /* Find the equivalent binfo within the return type of the
2662 overriding function. We will want the vbase offset from
2663 there. */
2664 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2665 over_return);
2666 else if (!same_type_ignoring_top_level_qualifiers_p
2667 (over_return, base_return))
2669 /* There was no existing virtual thunk (which takes
2670 precedence). So find the binfo of the base function's
2671 return type within the overriding function's return type.
2672 Fortunately we know the covariancy is valid (it
2673 has already been checked), so we can just iterate along
2674 the binfos, which have been chained in inheritance graph
2675 order. Of course it is lame that we have to repeat the
2676 search here anyway -- we should really be caching pieces
2677 of the vtable and avoiding this repeated work. */
2678 tree thunk_binfo, base_binfo;
2680 /* Find the base binfo within the overriding function's
2681 return type. We will always find a thunk_binfo, except
2682 when the covariancy is invalid (which we will have
2683 already diagnosed). */
2684 for (base_binfo = TYPE_BINFO (base_return),
2685 thunk_binfo = TYPE_BINFO (over_return);
2686 thunk_binfo;
2687 thunk_binfo = TREE_CHAIN (thunk_binfo))
2688 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2689 BINFO_TYPE (base_binfo)))
2690 break;
2692 /* See if virtual inheritance is involved. */
2693 for (virtual_offset = thunk_binfo;
2694 virtual_offset;
2695 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2696 if (BINFO_VIRTUAL_P (virtual_offset))
2697 break;
2699 if (virtual_offset
2700 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2702 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2704 if (virtual_offset)
2706 /* We convert via virtual base. Adjust the fixed
2707 offset to be from there. */
2708 offset =
2709 size_diffop (offset,
2710 fold_convert (ssizetype,
2711 BINFO_OFFSET (virtual_offset)));
2713 if (fixed_offset)
2714 /* There was an existing fixed offset, this must be
2715 from the base just converted to, and the base the
2716 FN was thunking to. */
2717 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2718 else
2719 fixed_offset = offset;
2723 if (fixed_offset || virtual_offset)
2724 /* Replace the overriding function with a covariant thunk. We
2725 will emit the overriding function in its own slot as
2726 well. */
2727 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2728 fixed_offset, virtual_offset);
2730 else
2731 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2732 !DECL_THUNK_P (fn));
2734 /* If we need a covariant thunk, then we may need to adjust first_defn.
2735 The ABI specifies that the thunks emitted with a function are
2736 determined by which bases the function overrides, so we need to be
2737 sure that we're using a thunk for some overridden base; even if we
2738 know that the necessary this adjustment is zero, there may not be an
2739 appropriate zero-this-adjusment thunk for us to use since thunks for
2740 overriding virtual bases always use the vcall offset.
2742 Furthermore, just choosing any base that overrides this function isn't
2743 quite right, as this slot won't be used for calls through a type that
2744 puts a covariant thunk here. Calling the function through such a type
2745 will use a different slot, and that slot is the one that determines
2746 the thunk emitted for that base.
2748 So, keep looking until we find the base that we're really overriding
2749 in this slot: the nearest primary base that doesn't use a covariant
2750 thunk in this slot. */
2751 if (overrider_target != overrider_fn)
2753 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2754 /* We already know that the overrider needs a covariant thunk. */
2755 b = get_primary_binfo (b);
2756 for (; ; b = get_primary_binfo (b))
2758 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2759 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2760 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2761 break;
2762 if (BINFO_LOST_PRIMARY_P (b))
2763 lost = true;
2765 first_defn = b;
2768 /* Assume that we will produce a thunk that convert all the way to
2769 the final overrider, and not to an intermediate virtual base. */
2770 virtual_base = NULL_TREE;
2772 /* See if we can convert to an intermediate virtual base first, and then
2773 use the vcall offset located there to finish the conversion. */
2774 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2776 /* If we find the final overrider, then we can stop
2777 walking. */
2778 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2779 BINFO_TYPE (TREE_VALUE (overrider))))
2780 break;
2782 /* If we find a virtual base, and we haven't yet found the
2783 overrider, then there is a virtual base between the
2784 declaring base (first_defn) and the final overrider. */
2785 if (BINFO_VIRTUAL_P (b))
2787 virtual_base = b;
2788 break;
2792 /* Compute the constant adjustment to the `this' pointer. The
2793 `this' pointer, when this function is called, will point at BINFO
2794 (or one of its primary bases, which are at the same offset). */
2795 if (virtual_base)
2796 /* The `this' pointer needs to be adjusted from the declaration to
2797 the nearest virtual base. */
2798 delta = size_diffop_loc (input_location,
2799 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2800 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2801 else if (lost)
2802 /* If the nearest definition is in a lost primary, we don't need an
2803 entry in our vtable. Except possibly in a constructor vtable,
2804 if we happen to get our primary back. In that case, the offset
2805 will be zero, as it will be a primary base. */
2806 delta = size_zero_node;
2807 else
2808 /* The `this' pointer needs to be adjusted from pointing to
2809 BINFO to pointing at the base where the final overrider
2810 appears. */
2811 delta = size_diffop_loc (input_location,
2812 fold_convert (ssizetype,
2813 BINFO_OFFSET (TREE_VALUE (overrider))),
2814 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2816 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2818 if (virtual_base)
2819 BV_VCALL_INDEX (*virtuals)
2820 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2821 else
2822 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2824 BV_LOST_PRIMARY (*virtuals) = lost;
2827 /* Called from modify_all_vtables via dfs_walk. */
2829 static tree
2830 dfs_modify_vtables (tree binfo, void* data)
2832 tree t = (tree) data;
2833 tree virtuals;
2834 tree old_virtuals;
2835 unsigned ix;
2837 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2838 /* A base without a vtable needs no modification, and its bases
2839 are uninteresting. */
2840 return dfs_skip_bases;
2842 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2843 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2844 /* Don't do the primary vtable, if it's new. */
2845 return NULL_TREE;
2847 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2848 /* There's no need to modify the vtable for a non-virtual primary
2849 base; we're not going to use that vtable anyhow. We do still
2850 need to do this for virtual primary bases, as they could become
2851 non-primary in a construction vtable. */
2852 return NULL_TREE;
2854 make_new_vtable (t, binfo);
2856 /* Now, go through each of the virtual functions in the virtual
2857 function table for BINFO. Find the final overrider, and update
2858 the BINFO_VIRTUALS list appropriately. */
2859 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2860 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2861 virtuals;
2862 ix++, virtuals = TREE_CHAIN (virtuals),
2863 old_virtuals = TREE_CHAIN (old_virtuals))
2864 update_vtable_entry_for_fn (t,
2865 binfo,
2866 BV_FN (old_virtuals),
2867 &virtuals, ix);
2869 return NULL_TREE;
2872 /* Update all of the primary and secondary vtables for T. Create new
2873 vtables as required, and initialize their RTTI information. Each
2874 of the functions in VIRTUALS is declared in T and may override a
2875 virtual function from a base class; find and modify the appropriate
2876 entries to point to the overriding functions. Returns a list, in
2877 declaration order, of the virtual functions that are declared in T,
2878 but do not appear in the primary base class vtable, and which
2879 should therefore be appended to the end of the vtable for T. */
2881 static tree
2882 modify_all_vtables (tree t, tree virtuals)
2884 tree binfo = TYPE_BINFO (t);
2885 tree *fnsp;
2887 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2888 if (TYPE_CONTAINS_VPTR_P (t))
2889 get_vtable_decl (t, false);
2891 /* Update all of the vtables. */
2892 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2894 /* Add virtual functions not already in our primary vtable. These
2895 will be both those introduced by this class, and those overridden
2896 from secondary bases. It does not include virtuals merely
2897 inherited from secondary bases. */
2898 for (fnsp = &virtuals; *fnsp; )
2900 tree fn = TREE_VALUE (*fnsp);
2902 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2903 || DECL_VINDEX (fn) == error_mark_node)
2905 /* We don't need to adjust the `this' pointer when
2906 calling this function. */
2907 BV_DELTA (*fnsp) = integer_zero_node;
2908 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2910 /* This is a function not already in our vtable. Keep it. */
2911 fnsp = &TREE_CHAIN (*fnsp);
2913 else
2914 /* We've already got an entry for this function. Skip it. */
2915 *fnsp = TREE_CHAIN (*fnsp);
2918 return virtuals;
2921 /* Get the base virtual function declarations in T that have the
2922 indicated NAME. */
2924 static void
2925 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2927 tree methods;
2928 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2929 int i;
2931 /* Find virtual functions in T with the indicated NAME. */
2932 i = lookup_fnfields_1 (t, name);
2933 bool found_decls = false;
2934 if (i != -1)
2935 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i];
2936 methods;
2937 methods = OVL_NEXT (methods))
2939 tree method = OVL_CURRENT (methods);
2941 if (TREE_CODE (method) == FUNCTION_DECL
2942 && DECL_VINDEX (method))
2944 base_fndecls->safe_push (method);
2945 found_decls = true;
2949 if (found_decls)
2950 return;
2952 for (i = 0; i < n_baseclasses; i++)
2954 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2955 get_basefndecls (name, basetype, base_fndecls);
2959 /* If this declaration supersedes the declaration of
2960 a method declared virtual in the base class, then
2961 mark this field as being virtual as well. */
2963 void
2964 check_for_override (tree decl, tree ctype)
2966 bool overrides_found = false;
2967 if (TREE_CODE (decl) == TEMPLATE_DECL)
2968 /* In [temp.mem] we have:
2970 A specialization of a member function template does not
2971 override a virtual function from a base class. */
2972 return;
2973 if ((DECL_DESTRUCTOR_P (decl)
2974 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2975 || DECL_CONV_FN_P (decl))
2976 && look_for_overrides (ctype, decl)
2977 && !DECL_STATIC_FUNCTION_P (decl))
2978 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2979 the error_mark_node so that we know it is an overriding
2980 function. */
2982 DECL_VINDEX (decl) = decl;
2983 overrides_found = true;
2984 if (warn_override && !DECL_OVERRIDE_P (decl)
2985 && !DECL_DESTRUCTOR_P (decl))
2986 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2987 "%qD can be marked override", decl);
2990 if (DECL_VIRTUAL_P (decl))
2992 if (!DECL_VINDEX (decl))
2993 DECL_VINDEX (decl) = error_mark_node;
2994 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2995 if (DECL_DESTRUCTOR_P (decl))
2996 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2998 else if (DECL_FINAL_P (decl))
2999 error ("%q+#D marked %<final%>, but is not virtual", decl);
3000 if (DECL_OVERRIDE_P (decl) && !overrides_found)
3001 error ("%q+#D marked %<override%>, but does not override", decl);
3004 /* Warn about hidden virtual functions that are not overridden in t.
3005 We know that constructors and destructors don't apply. */
3007 static void
3008 warn_hidden (tree t)
3010 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
3011 tree fns;
3012 size_t i;
3014 /* We go through each separately named virtual function. */
3015 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
3016 vec_safe_iterate (method_vec, i, &fns);
3017 ++i)
3019 tree fn;
3020 tree name;
3021 tree fndecl;
3022 tree base_binfo;
3023 tree binfo;
3024 int j;
3026 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
3027 have the same name. Figure out what name that is. */
3028 name = DECL_NAME (OVL_CURRENT (fns));
3029 /* There are no possibly hidden functions yet. */
3030 auto_vec<tree, 20> base_fndecls;
3031 /* Iterate through all of the base classes looking for possibly
3032 hidden functions. */
3033 for (binfo = TYPE_BINFO (t), j = 0;
3034 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3036 tree basetype = BINFO_TYPE (base_binfo);
3037 get_basefndecls (name, basetype, &base_fndecls);
3040 /* If there are no functions to hide, continue. */
3041 if (base_fndecls.is_empty ())
3042 continue;
3044 /* Remove any overridden functions. */
3045 for (fn = fns; fn; fn = OVL_NEXT (fn))
3047 fndecl = OVL_CURRENT (fn);
3048 if (TREE_CODE (fndecl) == FUNCTION_DECL
3049 && DECL_VINDEX (fndecl))
3051 /* If the method from the base class has the same
3052 signature as the method from the derived class, it
3053 has been overridden. */
3054 for (size_t k = 0; k < base_fndecls.length (); k++)
3055 if (base_fndecls[k]
3056 && same_signature_p (fndecl, base_fndecls[k]))
3057 base_fndecls[k] = NULL_TREE;
3061 /* Now give a warning for all base functions without overriders,
3062 as they are hidden. */
3063 size_t k;
3064 tree base_fndecl;
3065 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3066 if (base_fndecl)
3068 /* Here we know it is a hider, and no overrider exists. */
3069 warning_at (location_of (base_fndecl),
3070 OPT_Woverloaded_virtual,
3071 "%qD was hidden", base_fndecl);
3072 warning_at (location_of (fns),
3073 OPT_Woverloaded_virtual, " by %qD", fns);
3078 /* Recursive helper for finish_struct_anon. */
3080 static void
3081 finish_struct_anon_r (tree field, bool complain)
3083 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3084 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3085 for (; elt; elt = DECL_CHAIN (elt))
3087 /* We're generally only interested in entities the user
3088 declared, but we also find nested classes by noticing
3089 the TYPE_DECL that we create implicitly. You're
3090 allowed to put one anonymous union inside another,
3091 though, so we explicitly tolerate that. We use
3092 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3093 we also allow unnamed types used for defining fields. */
3094 if (DECL_ARTIFICIAL (elt)
3095 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3096 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3097 continue;
3099 if (TREE_CODE (elt) != FIELD_DECL)
3101 /* We already complained about static data members in
3102 finish_static_data_member_decl. */
3103 if (complain && !VAR_P (elt))
3105 if (is_union)
3106 permerror (DECL_SOURCE_LOCATION (elt),
3107 "%q#D invalid; an anonymous union can "
3108 "only have non-static data members", elt);
3109 else
3110 permerror (DECL_SOURCE_LOCATION (elt),
3111 "%q#D invalid; an anonymous struct can "
3112 "only have non-static data members", elt);
3114 continue;
3117 if (complain)
3119 if (TREE_PRIVATE (elt))
3121 if (is_union)
3122 permerror (DECL_SOURCE_LOCATION (elt),
3123 "private member %q#D in anonymous union", elt);
3124 else
3125 permerror (DECL_SOURCE_LOCATION (elt),
3126 "private member %q#D in anonymous struct", elt);
3128 else if (TREE_PROTECTED (elt))
3130 if (is_union)
3131 permerror (DECL_SOURCE_LOCATION (elt),
3132 "protected member %q#D in anonymous union", elt);
3133 else
3134 permerror (DECL_SOURCE_LOCATION (elt),
3135 "protected member %q#D in anonymous struct", elt);
3139 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3140 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3142 /* Recurse into the anonymous aggregates to handle correctly
3143 access control (c++/24926):
3145 class A {
3146 union {
3147 union {
3148 int i;
3153 int j=A().i; */
3154 if (DECL_NAME (elt) == NULL_TREE
3155 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3156 finish_struct_anon_r (elt, /*complain=*/false);
3160 /* Check for things that are invalid. There are probably plenty of other
3161 things we should check for also. */
3163 static void
3164 finish_struct_anon (tree t)
3166 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3168 if (TREE_STATIC (field))
3169 continue;
3170 if (TREE_CODE (field) != FIELD_DECL)
3171 continue;
3173 if (DECL_NAME (field) == NULL_TREE
3174 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3175 finish_struct_anon_r (field, /*complain=*/true);
3179 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3180 will be used later during class template instantiation.
3181 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3182 a non-static member data (FIELD_DECL), a member function
3183 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3184 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3185 When FRIEND_P is nonzero, T is either a friend class
3186 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3187 (FUNCTION_DECL, TEMPLATE_DECL). */
3189 void
3190 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3192 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3193 if (CLASSTYPE_TEMPLATE_INFO (type))
3194 CLASSTYPE_DECL_LIST (type)
3195 = tree_cons (friend_p ? NULL_TREE : type,
3196 t, CLASSTYPE_DECL_LIST (type));
3199 /* This function is called from declare_virt_assop_and_dtor via
3200 dfs_walk_all.
3202 DATA is a type that direcly or indirectly inherits the base
3203 represented by BINFO. If BINFO contains a virtual assignment [copy
3204 assignment or move assigment] operator or a virtual constructor,
3205 declare that function in DATA if it hasn't been already declared. */
3207 static tree
3208 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3210 tree bv, fn, t = (tree)data;
3211 tree opname = ansi_assopname (NOP_EXPR);
3213 gcc_assert (t && CLASS_TYPE_P (t));
3214 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3216 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3217 /* A base without a vtable needs no modification, and its bases
3218 are uninteresting. */
3219 return dfs_skip_bases;
3221 if (BINFO_PRIMARY_P (binfo))
3222 /* If this is a primary base, then we have already looked at the
3223 virtual functions of its vtable. */
3224 return NULL_TREE;
3226 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3228 fn = BV_FN (bv);
3230 if (DECL_NAME (fn) == opname)
3232 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3233 lazily_declare_fn (sfk_copy_assignment, t);
3234 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3235 lazily_declare_fn (sfk_move_assignment, t);
3237 else if (DECL_DESTRUCTOR_P (fn)
3238 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3239 lazily_declare_fn (sfk_destructor, t);
3242 return NULL_TREE;
3245 /* If the class type T has a direct or indirect base that contains a
3246 virtual assignment operator or a virtual destructor, declare that
3247 function in T if it hasn't been already declared. */
3249 static void
3250 declare_virt_assop_and_dtor (tree t)
3252 if (!(TYPE_POLYMORPHIC_P (t)
3253 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3254 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3255 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3256 return;
3258 dfs_walk_all (TYPE_BINFO (t),
3259 dfs_declare_virt_assop_and_dtor,
3260 NULL, t);
3263 /* Declare the inheriting constructor for class T inherited from base
3264 constructor CTOR with the parameter array PARMS of size NPARMS. */
3266 static void
3267 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3269 /* We don't declare an inheriting ctor that would be a default,
3270 copy or move ctor for derived or base. */
3271 if (nparms == 0)
3272 return;
3273 if (nparms == 1
3274 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3276 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3277 if (parm == t || parm == DECL_CONTEXT (ctor))
3278 return;
3281 tree parmlist = void_list_node;
3282 for (int i = nparms - 1; i >= 0; i--)
3283 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3284 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3285 t, false, ctor, parmlist);
3286 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3287 if (add_method (t, fn, NULL_TREE))
3289 DECL_CHAIN (fn) = TYPE_METHODS (t);
3290 TYPE_METHODS (t) = fn;
3294 /* Declare all the inheriting constructors for class T inherited from base
3295 constructor CTOR. */
3297 static void
3298 one_inherited_ctor (tree ctor, tree t)
3300 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3302 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3303 int i = 0;
3304 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3306 if (TREE_PURPOSE (parms))
3307 one_inheriting_sig (t, ctor, new_parms, i);
3308 new_parms[i++] = TREE_VALUE (parms);
3310 one_inheriting_sig (t, ctor, new_parms, i);
3311 if (parms == NULL_TREE)
3313 if (warning (OPT_Winherited_variadic_ctor,
3314 "the ellipsis in %qD is not inherited", ctor))
3315 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3319 /* Create default constructors, assignment operators, and so forth for
3320 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3321 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3322 the class cannot have a default constructor, copy constructor
3323 taking a const reference argument, or an assignment operator taking
3324 a const reference, respectively. */
3326 static void
3327 add_implicitly_declared_members (tree t, tree* access_decls,
3328 int cant_have_const_cctor,
3329 int cant_have_const_assignment)
3331 bool move_ok = false;
3333 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3334 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3335 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3336 move_ok = true;
3338 /* Destructor. */
3339 if (!CLASSTYPE_DESTRUCTORS (t))
3340 /* In general, we create destructors lazily. */
3341 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3343 /* [class.ctor]
3345 If there is no user-declared constructor for a class, a default
3346 constructor is implicitly declared. */
3347 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3349 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3350 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3351 if (cxx_dialect >= cxx11)
3352 TYPE_HAS_CONSTEXPR_CTOR (t)
3353 /* Don't force the declaration to get a hard answer; if the
3354 definition would have made the class non-literal, it will still be
3355 non-literal because of the base or member in question, and that
3356 gives a better diagnostic. */
3357 = type_maybe_constexpr_default_constructor (t);
3360 /* [class.ctor]
3362 If a class definition does not explicitly declare a copy
3363 constructor, one is declared implicitly. */
3364 if (! TYPE_HAS_COPY_CTOR (t))
3366 TYPE_HAS_COPY_CTOR (t) = 1;
3367 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3368 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3369 if (move_ok)
3370 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3373 /* If there is no assignment operator, one will be created if and
3374 when it is needed. For now, just record whether or not the type
3375 of the parameter to the assignment operator will be a const or
3376 non-const reference. */
3377 if (!TYPE_HAS_COPY_ASSIGN (t))
3379 TYPE_HAS_COPY_ASSIGN (t) = 1;
3380 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3381 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3382 if (move_ok && !LAMBDA_TYPE_P (t))
3383 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3386 /* We can't be lazy about declaring functions that might override
3387 a virtual function from a base class. */
3388 declare_virt_assop_and_dtor (t);
3390 while (*access_decls)
3392 tree using_decl = TREE_VALUE (*access_decls);
3393 tree decl = USING_DECL_DECLS (using_decl);
3394 if (DECL_NAME (using_decl) == ctor_identifier)
3396 /* declare, then remove the decl */
3397 tree ctor_list = decl;
3398 location_t loc = input_location;
3399 input_location = DECL_SOURCE_LOCATION (using_decl);
3400 if (ctor_list)
3401 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list))
3402 one_inherited_ctor (OVL_CURRENT (ctor_list), t);
3403 *access_decls = TREE_CHAIN (*access_decls);
3404 input_location = loc;
3406 else
3407 access_decls = &TREE_CHAIN (*access_decls);
3411 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3412 count the number of fields in TYPE, including anonymous union
3413 members. */
3415 static int
3416 count_fields (tree fields)
3418 tree x;
3419 int n_fields = 0;
3420 for (x = fields; x; x = DECL_CHAIN (x))
3422 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3423 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3424 else
3425 n_fields += 1;
3427 return n_fields;
3430 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3431 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3432 elts, starting at offset IDX. */
3434 static int
3435 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3437 tree x;
3438 for (x = fields; x; x = DECL_CHAIN (x))
3440 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3441 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3442 else
3443 field_vec->elts[idx++] = x;
3445 return idx;
3448 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3449 starting at offset IDX. */
3451 static int
3452 add_enum_fields_to_record_type (tree enumtype,
3453 struct sorted_fields_type *field_vec,
3454 int idx)
3456 tree values;
3457 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3458 field_vec->elts[idx++] = TREE_VALUE (values);
3459 return idx;
3462 /* FIELD is a bit-field. We are finishing the processing for its
3463 enclosing type. Issue any appropriate messages and set appropriate
3464 flags. Returns false if an error has been diagnosed. */
3466 static bool
3467 check_bitfield_decl (tree field)
3469 tree type = TREE_TYPE (field);
3470 tree w;
3472 /* Extract the declared width of the bitfield, which has been
3473 temporarily stashed in DECL_INITIAL. */
3474 w = DECL_INITIAL (field);
3475 gcc_assert (w != NULL_TREE);
3476 /* Remove the bit-field width indicator so that the rest of the
3477 compiler does not treat that value as an initializer. */
3478 DECL_INITIAL (field) = NULL_TREE;
3480 /* Detect invalid bit-field type. */
3481 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3483 error ("bit-field %q+#D with non-integral type", field);
3484 w = error_mark_node;
3486 else
3488 location_t loc = input_location;
3489 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3490 STRIP_NOPS (w);
3492 /* detect invalid field size. */
3493 input_location = DECL_SOURCE_LOCATION (field);
3494 w = cxx_constant_value (w);
3495 input_location = loc;
3497 if (TREE_CODE (w) != INTEGER_CST)
3499 error ("bit-field %q+D width not an integer constant", field);
3500 w = error_mark_node;
3502 else if (tree_int_cst_sgn (w) < 0)
3504 error ("negative width in bit-field %q+D", field);
3505 w = error_mark_node;
3507 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3509 error ("zero width for bit-field %q+D", field);
3510 w = error_mark_node;
3512 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3513 && TREE_CODE (type) != BOOLEAN_TYPE
3514 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3515 || ((TREE_CODE (type) == ENUMERAL_TYPE
3516 || TREE_CODE (type) == BOOLEAN_TYPE)
3517 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3518 warning_at (DECL_SOURCE_LOCATION (field), 0,
3519 "width of %qD exceeds its type", field);
3520 else if (TREE_CODE (type) == ENUMERAL_TYPE
3521 && (0 > (compare_tree_int
3522 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3523 warning_at (DECL_SOURCE_LOCATION (field), 0,
3524 "%qD is too small to hold all values of %q#T",
3525 field, type);
3528 if (w != error_mark_node)
3530 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3531 DECL_BIT_FIELD (field) = 1;
3532 return true;
3534 else
3536 /* Non-bit-fields are aligned for their type. */
3537 DECL_BIT_FIELD (field) = 0;
3538 CLEAR_DECL_C_BIT_FIELD (field);
3539 return false;
3543 /* FIELD is a non bit-field. We are finishing the processing for its
3544 enclosing type T. Issue any appropriate messages and set appropriate
3545 flags. */
3547 static bool
3548 check_field_decl (tree field,
3549 tree t,
3550 int* cant_have_const_ctor,
3551 int* no_const_asn_ref)
3553 tree type = strip_array_types (TREE_TYPE (field));
3554 bool any_default_members = false;
3556 /* In C++98 an anonymous union cannot contain any fields which would change
3557 the settings of CANT_HAVE_CONST_CTOR and friends. */
3558 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3560 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3561 structs. So, we recurse through their fields here. */
3562 else if (ANON_AGGR_TYPE_P (type))
3564 for (tree fields = TYPE_FIELDS (type); fields;
3565 fields = DECL_CHAIN (fields))
3566 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3567 any_default_members |= check_field_decl (fields, t,
3568 cant_have_const_ctor,
3569 no_const_asn_ref);
3571 /* Check members with class type for constructors, destructors,
3572 etc. */
3573 else if (CLASS_TYPE_P (type))
3575 /* Never let anything with uninheritable virtuals
3576 make it through without complaint. */
3577 abstract_virtuals_error (field, type);
3579 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3581 static bool warned;
3582 int oldcount = errorcount;
3583 if (TYPE_NEEDS_CONSTRUCTING (type))
3584 error ("member %q+#D with constructor not allowed in union",
3585 field);
3586 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3587 error ("member %q+#D with destructor not allowed in union", field);
3588 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3589 error ("member %q+#D with copy assignment operator not allowed in union",
3590 field);
3591 if (!warned && errorcount > oldcount)
3593 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3594 "only available with -std=c++11 or -std=gnu++11");
3595 warned = true;
3598 else
3600 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3601 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3602 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3603 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3604 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3605 || !TYPE_HAS_COPY_ASSIGN (type));
3606 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3607 || !TYPE_HAS_COPY_CTOR (type));
3608 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3609 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3610 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3611 || TYPE_HAS_COMPLEX_DFLT (type));
3614 if (TYPE_HAS_COPY_CTOR (type)
3615 && !TYPE_HAS_CONST_COPY_CTOR (type))
3616 *cant_have_const_ctor = 1;
3618 if (TYPE_HAS_COPY_ASSIGN (type)
3619 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3620 *no_const_asn_ref = 1;
3623 check_abi_tags (t, field);
3625 if (DECL_INITIAL (field) != NULL_TREE)
3626 /* `build_class_init_list' does not recognize
3627 non-FIELD_DECLs. */
3628 any_default_members = true;
3630 return any_default_members;
3633 /* Check the data members (both static and non-static), class-scoped
3634 typedefs, etc., appearing in the declaration of T. Issue
3635 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3636 declaration order) of access declarations; each TREE_VALUE in this
3637 list is a USING_DECL.
3639 In addition, set the following flags:
3641 EMPTY_P
3642 The class is empty, i.e., contains no non-static data members.
3644 CANT_HAVE_CONST_CTOR_P
3645 This class cannot have an implicitly generated copy constructor
3646 taking a const reference.
3648 CANT_HAVE_CONST_ASN_REF
3649 This class cannot have an implicitly generated assignment
3650 operator taking a const reference.
3652 All of these flags should be initialized before calling this
3653 function.
3655 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3656 fields can be added by adding to this chain. */
3658 static void
3659 check_field_decls (tree t, tree *access_decls,
3660 int *cant_have_const_ctor_p,
3661 int *no_const_asn_ref_p)
3663 tree *field;
3664 tree *next;
3665 bool has_pointers;
3666 bool any_default_members;
3667 int cant_pack = 0;
3668 int field_access = -1;
3670 /* Assume there are no access declarations. */
3671 *access_decls = NULL_TREE;
3672 /* Assume this class has no pointer members. */
3673 has_pointers = false;
3674 /* Assume none of the members of this class have default
3675 initializations. */
3676 any_default_members = false;
3678 for (field = &TYPE_FIELDS (t); *field; field = next)
3680 tree x = *field;
3681 tree type = TREE_TYPE (x);
3682 int this_field_access;
3684 next = &DECL_CHAIN (x);
3686 if (TREE_CODE (x) == USING_DECL)
3688 /* Save the access declarations for our caller. */
3689 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3690 continue;
3693 if (TREE_CODE (x) == TYPE_DECL
3694 || TREE_CODE (x) == TEMPLATE_DECL)
3695 continue;
3697 /* If we've gotten this far, it's a data member, possibly static,
3698 or an enumerator. */
3699 if (TREE_CODE (x) != CONST_DECL)
3700 DECL_CONTEXT (x) = t;
3702 /* When this goes into scope, it will be a non-local reference. */
3703 DECL_NONLOCAL (x) = 1;
3705 if (TREE_CODE (t) == UNION_TYPE
3706 && cxx_dialect < cxx11)
3708 /* [class.union] (C++98)
3710 If a union contains a static data member, or a member of
3711 reference type, the program is ill-formed.
3713 In C++11 this limitation doesn't exist anymore. */
3714 if (VAR_P (x))
3716 error ("in C++98 %q+D may not be static because it is "
3717 "a member of a union", x);
3718 continue;
3720 if (TREE_CODE (type) == REFERENCE_TYPE)
3722 error ("in C++98 %q+D may not have reference type %qT "
3723 "because it is a member of a union", x, type);
3724 continue;
3728 /* Perform error checking that did not get done in
3729 grokdeclarator. */
3730 if (TREE_CODE (type) == FUNCTION_TYPE)
3732 error ("field %q+D invalidly declared function type", x);
3733 type = build_pointer_type (type);
3734 TREE_TYPE (x) = type;
3736 else if (TREE_CODE (type) == METHOD_TYPE)
3738 error ("field %q+D invalidly declared method type", x);
3739 type = build_pointer_type (type);
3740 TREE_TYPE (x) = type;
3743 if (type == error_mark_node)
3744 continue;
3746 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3747 continue;
3749 /* Now it can only be a FIELD_DECL. */
3751 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3752 CLASSTYPE_NON_AGGREGATE (t) = 1;
3754 /* If at least one non-static data member is non-literal, the whole
3755 class becomes non-literal. Per Core/1453, volatile non-static
3756 data members and base classes are also not allowed.
3757 Note: if the type is incomplete we will complain later on. */
3758 if (COMPLETE_TYPE_P (type)
3759 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3760 CLASSTYPE_LITERAL_P (t) = false;
3762 /* A standard-layout class is a class that:
3764 has the same access control (Clause 11) for all non-static data members,
3765 ... */
3766 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3767 if (field_access == -1)
3768 field_access = this_field_access;
3769 else if (this_field_access != field_access)
3770 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3772 /* If this is of reference type, check if it needs an init. */
3773 if (TREE_CODE (type) == REFERENCE_TYPE)
3775 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3776 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3777 if (DECL_INITIAL (x) == NULL_TREE)
3778 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3779 if (cxx_dialect < cxx11)
3781 /* ARM $12.6.2: [A member initializer list] (or, for an
3782 aggregate, initialization by a brace-enclosed list) is the
3783 only way to initialize nonstatic const and reference
3784 members. */
3785 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3786 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3790 type = strip_array_types (type);
3792 if (TYPE_PACKED (t))
3794 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3796 warning_at
3797 (DECL_SOURCE_LOCATION (x), 0,
3798 "ignoring packed attribute because of unpacked non-POD field %q#D",
3800 cant_pack = 1;
3802 else if (DECL_C_BIT_FIELD (x)
3803 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3804 DECL_PACKED (x) = 1;
3807 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3808 /* We don't treat zero-width bitfields as making a class
3809 non-empty. */
3811 else
3813 /* The class is non-empty. */
3814 CLASSTYPE_EMPTY_P (t) = 0;
3815 /* The class is not even nearly empty. */
3816 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3817 /* If one of the data members contains an empty class,
3818 so does T. */
3819 if (CLASS_TYPE_P (type)
3820 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3821 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3824 /* This is used by -Weffc++ (see below). Warn only for pointers
3825 to members which might hold dynamic memory. So do not warn
3826 for pointers to functions or pointers to members. */
3827 if (TYPE_PTR_P (type)
3828 && !TYPE_PTRFN_P (type))
3829 has_pointers = true;
3831 if (CLASS_TYPE_P (type))
3833 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3834 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3835 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3836 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3839 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3840 CLASSTYPE_HAS_MUTABLE (t) = 1;
3842 if (DECL_MUTABLE_P (x))
3844 if (CP_TYPE_CONST_P (type))
3846 error ("member %q+D cannot be declared both %<const%> "
3847 "and %<mutable%>", x);
3848 continue;
3850 if (TREE_CODE (type) == REFERENCE_TYPE)
3852 error ("member %q+D cannot be declared as a %<mutable%> "
3853 "reference", x);
3854 continue;
3858 if (! layout_pod_type_p (type))
3859 /* DR 148 now allows pointers to members (which are POD themselves),
3860 to be allowed in POD structs. */
3861 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3863 if (!std_layout_type_p (type))
3864 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3866 if (! zero_init_p (type))
3867 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3869 /* We set DECL_C_BIT_FIELD in grokbitfield.
3870 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3871 if ((! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3872 && check_field_decl (x, t,
3873 cant_have_const_ctor_p,
3874 no_const_asn_ref_p))
3876 if (any_default_members
3877 && TREE_CODE (t) == UNION_TYPE)
3878 error ("multiple fields in union %qT initialized", t);
3879 any_default_members = true;
3882 /* Now that we've removed bit-field widths from DECL_INITIAL,
3883 anything left in DECL_INITIAL is an NSDMI that makes the class
3884 non-aggregate in C++11. */
3885 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3886 CLASSTYPE_NON_AGGREGATE (t) = true;
3888 /* If any field is const, the structure type is pseudo-const. */
3889 if (CP_TYPE_CONST_P (type))
3891 C_TYPE_FIELDS_READONLY (t) = 1;
3892 if (DECL_INITIAL (x) == NULL_TREE)
3893 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3894 if (cxx_dialect < cxx11)
3896 /* ARM $12.6.2: [A member initializer list] (or, for an
3897 aggregate, initialization by a brace-enclosed list) is the
3898 only way to initialize nonstatic const and reference
3899 members. */
3900 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3901 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3904 /* A field that is pseudo-const makes the structure likewise. */
3905 else if (CLASS_TYPE_P (type))
3907 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3908 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3909 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3910 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3913 /* Core issue 80: A nonstatic data member is required to have a
3914 different name from the class iff the class has a
3915 user-declared constructor. */
3916 if (constructor_name_p (DECL_NAME (x), t)
3917 && TYPE_HAS_USER_CONSTRUCTOR (t))
3918 permerror (DECL_SOURCE_LOCATION (x),
3919 "field %q#D with same name as class", x);
3922 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3923 it should also define a copy constructor and an assignment operator to
3924 implement the correct copy semantic (deep vs shallow, etc.). As it is
3925 not feasible to check whether the constructors do allocate dynamic memory
3926 and store it within members, we approximate the warning like this:
3928 -- Warn only if there are members which are pointers
3929 -- Warn only if there is a non-trivial constructor (otherwise,
3930 there cannot be memory allocated).
3931 -- Warn only if there is a non-trivial destructor. We assume that the
3932 user at least implemented the cleanup correctly, and a destructor
3933 is needed to free dynamic memory.
3935 This seems enough for practical purposes. */
3936 if (warn_ecpp
3937 && has_pointers
3938 && TYPE_HAS_USER_CONSTRUCTOR (t)
3939 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3940 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3942 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3944 if (! TYPE_HAS_COPY_CTOR (t))
3946 warning (OPT_Weffc__,
3947 " but does not override %<%T(const %T&)%>", t, t);
3948 if (!TYPE_HAS_COPY_ASSIGN (t))
3949 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3951 else if (! TYPE_HAS_COPY_ASSIGN (t))
3952 warning (OPT_Weffc__,
3953 " but does not override %<operator=(const %T&)%>", t);
3956 /* Non-static data member initializers make the default constructor
3957 non-trivial. */
3958 if (any_default_members)
3960 TYPE_NEEDS_CONSTRUCTING (t) = true;
3961 TYPE_HAS_COMPLEX_DFLT (t) = true;
3964 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3965 if (cant_pack)
3966 TYPE_PACKED (t) = 0;
3968 /* Check anonymous struct/anonymous union fields. */
3969 finish_struct_anon (t);
3971 /* We've built up the list of access declarations in reverse order.
3972 Fix that now. */
3973 *access_decls = nreverse (*access_decls);
3976 /* If TYPE is an empty class type, records its OFFSET in the table of
3977 OFFSETS. */
3979 static int
3980 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3982 splay_tree_node n;
3984 if (!is_empty_class (type))
3985 return 0;
3987 /* Record the location of this empty object in OFFSETS. */
3988 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3989 if (!n)
3990 n = splay_tree_insert (offsets,
3991 (splay_tree_key) offset,
3992 (splay_tree_value) NULL_TREE);
3993 n->value = ((splay_tree_value)
3994 tree_cons (NULL_TREE,
3995 type,
3996 (tree) n->value));
3998 return 0;
4001 /* Returns nonzero if TYPE is an empty class type and there is
4002 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4004 static int
4005 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4007 splay_tree_node n;
4008 tree t;
4010 if (!is_empty_class (type))
4011 return 0;
4013 /* Record the location of this empty object in OFFSETS. */
4014 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4015 if (!n)
4016 return 0;
4018 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4019 if (same_type_p (TREE_VALUE (t), type))
4020 return 1;
4022 return 0;
4025 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4026 F for every subobject, passing it the type, offset, and table of
4027 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4028 be traversed.
4030 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4031 than MAX_OFFSET will not be walked.
4033 If F returns a nonzero value, the traversal ceases, and that value
4034 is returned. Otherwise, returns zero. */
4036 static int
4037 walk_subobject_offsets (tree type,
4038 subobject_offset_fn f,
4039 tree offset,
4040 splay_tree offsets,
4041 tree max_offset,
4042 int vbases_p)
4044 int r = 0;
4045 tree type_binfo = NULL_TREE;
4047 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4048 stop. */
4049 if (max_offset && tree_int_cst_lt (max_offset, offset))
4050 return 0;
4052 if (type == error_mark_node)
4053 return 0;
4055 if (!TYPE_P (type))
4057 type_binfo = type;
4058 type = BINFO_TYPE (type);
4061 if (CLASS_TYPE_P (type))
4063 tree field;
4064 tree binfo;
4065 int i;
4067 /* Avoid recursing into objects that are not interesting. */
4068 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4069 return 0;
4071 /* Record the location of TYPE. */
4072 r = (*f) (type, offset, offsets);
4073 if (r)
4074 return r;
4076 /* Iterate through the direct base classes of TYPE. */
4077 if (!type_binfo)
4078 type_binfo = TYPE_BINFO (type);
4079 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4081 tree binfo_offset;
4083 if (BINFO_VIRTUAL_P (binfo))
4084 continue;
4086 tree orig_binfo;
4087 /* We cannot rely on BINFO_OFFSET being set for the base
4088 class yet, but the offsets for direct non-virtual
4089 bases can be calculated by going back to the TYPE. */
4090 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4091 binfo_offset = size_binop (PLUS_EXPR,
4092 offset,
4093 BINFO_OFFSET (orig_binfo));
4095 r = walk_subobject_offsets (binfo,
4097 binfo_offset,
4098 offsets,
4099 max_offset,
4100 /*vbases_p=*/0);
4101 if (r)
4102 return r;
4105 if (CLASSTYPE_VBASECLASSES (type))
4107 unsigned ix;
4108 vec<tree, va_gc> *vbases;
4110 /* Iterate through the virtual base classes of TYPE. In G++
4111 3.2, we included virtual bases in the direct base class
4112 loop above, which results in incorrect results; the
4113 correct offsets for virtual bases are only known when
4114 working with the most derived type. */
4115 if (vbases_p)
4116 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4117 vec_safe_iterate (vbases, ix, &binfo); ix++)
4119 r = walk_subobject_offsets (binfo,
4121 size_binop (PLUS_EXPR,
4122 offset,
4123 BINFO_OFFSET (binfo)),
4124 offsets,
4125 max_offset,
4126 /*vbases_p=*/0);
4127 if (r)
4128 return r;
4130 else
4132 /* We still have to walk the primary base, if it is
4133 virtual. (If it is non-virtual, then it was walked
4134 above.) */
4135 tree vbase = get_primary_binfo (type_binfo);
4137 if (vbase && BINFO_VIRTUAL_P (vbase)
4138 && BINFO_PRIMARY_P (vbase)
4139 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4141 r = (walk_subobject_offsets
4142 (vbase, f, offset,
4143 offsets, max_offset, /*vbases_p=*/0));
4144 if (r)
4145 return r;
4150 /* Iterate through the fields of TYPE. */
4151 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4152 if (TREE_CODE (field) == FIELD_DECL
4153 && TREE_TYPE (field) != error_mark_node
4154 && !DECL_ARTIFICIAL (field))
4156 tree field_offset;
4158 field_offset = byte_position (field);
4160 r = walk_subobject_offsets (TREE_TYPE (field),
4162 size_binop (PLUS_EXPR,
4163 offset,
4164 field_offset),
4165 offsets,
4166 max_offset,
4167 /*vbases_p=*/1);
4168 if (r)
4169 return r;
4172 else if (TREE_CODE (type) == ARRAY_TYPE)
4174 tree element_type = strip_array_types (type);
4175 tree domain = TYPE_DOMAIN (type);
4176 tree index;
4178 /* Avoid recursing into objects that are not interesting. */
4179 if (!CLASS_TYPE_P (element_type)
4180 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4181 || !domain
4182 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4183 return 0;
4185 /* Step through each of the elements in the array. */
4186 for (index = size_zero_node;
4187 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4188 index = size_binop (PLUS_EXPR, index, size_one_node))
4190 r = walk_subobject_offsets (TREE_TYPE (type),
4192 offset,
4193 offsets,
4194 max_offset,
4195 /*vbases_p=*/1);
4196 if (r)
4197 return r;
4198 offset = size_binop (PLUS_EXPR, offset,
4199 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4200 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4201 there's no point in iterating through the remaining
4202 elements of the array. */
4203 if (max_offset && tree_int_cst_lt (max_offset, offset))
4204 break;
4208 return 0;
4211 /* Record all of the empty subobjects of TYPE (either a type or a
4212 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4213 is being placed at OFFSET; otherwise, it is a base class that is
4214 being placed at OFFSET. */
4216 static void
4217 record_subobject_offsets (tree type,
4218 tree offset,
4219 splay_tree offsets,
4220 bool is_data_member)
4222 tree max_offset;
4223 /* If recording subobjects for a non-static data member or a
4224 non-empty base class , we do not need to record offsets beyond
4225 the size of the biggest empty class. Additional data members
4226 will go at the end of the class. Additional base classes will go
4227 either at offset zero (if empty, in which case they cannot
4228 overlap with offsets past the size of the biggest empty class) or
4229 at the end of the class.
4231 However, if we are placing an empty base class, then we must record
4232 all offsets, as either the empty class is at offset zero (where
4233 other empty classes might later be placed) or at the end of the
4234 class (where other objects might then be placed, so other empty
4235 subobjects might later overlap). */
4236 if (is_data_member
4237 || !is_empty_class (BINFO_TYPE (type)))
4238 max_offset = sizeof_biggest_empty_class;
4239 else
4240 max_offset = NULL_TREE;
4241 walk_subobject_offsets (type, record_subobject_offset, offset,
4242 offsets, max_offset, is_data_member);
4245 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4246 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4247 virtual bases of TYPE are examined. */
4249 static int
4250 layout_conflict_p (tree type,
4251 tree offset,
4252 splay_tree offsets,
4253 int vbases_p)
4255 splay_tree_node max_node;
4257 /* Get the node in OFFSETS that indicates the maximum offset where
4258 an empty subobject is located. */
4259 max_node = splay_tree_max (offsets);
4260 /* If there aren't any empty subobjects, then there's no point in
4261 performing this check. */
4262 if (!max_node)
4263 return 0;
4265 return walk_subobject_offsets (type, check_subobject_offset, offset,
4266 offsets, (tree) (max_node->key),
4267 vbases_p);
4270 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4271 non-static data member of the type indicated by RLI. BINFO is the
4272 binfo corresponding to the base subobject, OFFSETS maps offsets to
4273 types already located at those offsets. This function determines
4274 the position of the DECL. */
4276 static void
4277 layout_nonempty_base_or_field (record_layout_info rli,
4278 tree decl,
4279 tree binfo,
4280 splay_tree offsets)
4282 tree offset = NULL_TREE;
4283 bool field_p;
4284 tree type;
4286 if (binfo)
4288 /* For the purposes of determining layout conflicts, we want to
4289 use the class type of BINFO; TREE_TYPE (DECL) will be the
4290 CLASSTYPE_AS_BASE version, which does not contain entries for
4291 zero-sized bases. */
4292 type = TREE_TYPE (binfo);
4293 field_p = false;
4295 else
4297 type = TREE_TYPE (decl);
4298 field_p = true;
4301 /* Try to place the field. It may take more than one try if we have
4302 a hard time placing the field without putting two objects of the
4303 same type at the same address. */
4304 while (1)
4306 struct record_layout_info_s old_rli = *rli;
4308 /* Place this field. */
4309 place_field (rli, decl);
4310 offset = byte_position (decl);
4312 /* We have to check to see whether or not there is already
4313 something of the same type at the offset we're about to use.
4314 For example, consider:
4316 struct S {};
4317 struct T : public S { int i; };
4318 struct U : public S, public T {};
4320 Here, we put S at offset zero in U. Then, we can't put T at
4321 offset zero -- its S component would be at the same address
4322 as the S we already allocated. So, we have to skip ahead.
4323 Since all data members, including those whose type is an
4324 empty class, have nonzero size, any overlap can happen only
4325 with a direct or indirect base-class -- it can't happen with
4326 a data member. */
4327 /* In a union, overlap is permitted; all members are placed at
4328 offset zero. */
4329 if (TREE_CODE (rli->t) == UNION_TYPE)
4330 break;
4331 if (layout_conflict_p (field_p ? type : binfo, offset,
4332 offsets, field_p))
4334 /* Strip off the size allocated to this field. That puts us
4335 at the first place we could have put the field with
4336 proper alignment. */
4337 *rli = old_rli;
4339 /* Bump up by the alignment required for the type. */
4340 rli->bitpos
4341 = size_binop (PLUS_EXPR, rli->bitpos,
4342 bitsize_int (binfo
4343 ? CLASSTYPE_ALIGN (type)
4344 : TYPE_ALIGN (type)));
4345 normalize_rli (rli);
4347 else if (TREE_CODE (type) == NULLPTR_TYPE
4348 && warn_abi && abi_version_crosses (9))
4350 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4351 the offset wasn't aligned like a pointer when we started to
4352 layout this field, that affects its position. */
4353 tree pos = rli_size_unit_so_far (&old_rli);
4354 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4356 if (abi_version_at_least (9))
4357 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4358 "alignment of %qD increased in -fabi-version=9 "
4359 "(GCC 5.2)", decl);
4360 else
4361 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4362 "of %qD will increase in -fabi-version=9", decl);
4364 break;
4366 else
4367 /* There was no conflict. We're done laying out this field. */
4368 break;
4371 /* Now that we know where it will be placed, update its
4372 BINFO_OFFSET. */
4373 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4374 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4375 this point because their BINFO_OFFSET is copied from another
4376 hierarchy. Therefore, we may not need to add the entire
4377 OFFSET. */
4378 propagate_binfo_offsets (binfo,
4379 size_diffop_loc (input_location,
4380 fold_convert (ssizetype, offset),
4381 fold_convert (ssizetype,
4382 BINFO_OFFSET (binfo))));
4385 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4387 static int
4388 empty_base_at_nonzero_offset_p (tree type,
4389 tree offset,
4390 splay_tree /*offsets*/)
4392 return is_empty_class (type) && !integer_zerop (offset);
4395 /* Layout the empty base BINFO. EOC indicates the byte currently just
4396 past the end of the class, and should be correctly aligned for a
4397 class of the type indicated by BINFO; OFFSETS gives the offsets of
4398 the empty bases allocated so far. T is the most derived
4399 type. Return nonzero iff we added it at the end. */
4401 static bool
4402 layout_empty_base (record_layout_info rli, tree binfo,
4403 tree eoc, splay_tree offsets)
4405 tree alignment;
4406 tree basetype = BINFO_TYPE (binfo);
4407 bool atend = false;
4409 /* This routine should only be used for empty classes. */
4410 gcc_assert (is_empty_class (basetype));
4411 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4413 if (!integer_zerop (BINFO_OFFSET (binfo)))
4414 propagate_binfo_offsets
4415 (binfo, size_diffop_loc (input_location,
4416 size_zero_node, BINFO_OFFSET (binfo)));
4418 /* This is an empty base class. We first try to put it at offset
4419 zero. */
4420 if (layout_conflict_p (binfo,
4421 BINFO_OFFSET (binfo),
4422 offsets,
4423 /*vbases_p=*/0))
4425 /* That didn't work. Now, we move forward from the next
4426 available spot in the class. */
4427 atend = true;
4428 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4429 while (1)
4431 if (!layout_conflict_p (binfo,
4432 BINFO_OFFSET (binfo),
4433 offsets,
4434 /*vbases_p=*/0))
4435 /* We finally found a spot where there's no overlap. */
4436 break;
4438 /* There's overlap here, too. Bump along to the next spot. */
4439 propagate_binfo_offsets (binfo, alignment);
4443 if (CLASSTYPE_USER_ALIGN (basetype))
4445 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4446 if (warn_packed)
4447 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4448 TYPE_USER_ALIGN (rli->t) = 1;
4451 return atend;
4454 /* Layout the base given by BINFO in the class indicated by RLI.
4455 *BASE_ALIGN is a running maximum of the alignments of
4456 any base class. OFFSETS gives the location of empty base
4457 subobjects. T is the most derived type. Return nonzero if the new
4458 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4459 *NEXT_FIELD, unless BINFO is for an empty base class.
4461 Returns the location at which the next field should be inserted. */
4463 static tree *
4464 build_base_field (record_layout_info rli, tree binfo,
4465 splay_tree offsets, tree *next_field)
4467 tree t = rli->t;
4468 tree basetype = BINFO_TYPE (binfo);
4470 if (!COMPLETE_TYPE_P (basetype))
4471 /* This error is now reported in xref_tag, thus giving better
4472 location information. */
4473 return next_field;
4475 /* Place the base class. */
4476 if (!is_empty_class (basetype))
4478 tree decl;
4480 /* The containing class is non-empty because it has a non-empty
4481 base class. */
4482 CLASSTYPE_EMPTY_P (t) = 0;
4484 /* Create the FIELD_DECL. */
4485 decl = build_decl (input_location,
4486 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4487 DECL_ARTIFICIAL (decl) = 1;
4488 DECL_IGNORED_P (decl) = 1;
4489 DECL_FIELD_CONTEXT (decl) = t;
4490 if (CLASSTYPE_AS_BASE (basetype))
4492 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4493 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4494 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4495 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4496 DECL_MODE (decl) = TYPE_MODE (basetype);
4497 DECL_FIELD_IS_BASE (decl) = 1;
4499 /* Try to place the field. It may take more than one try if we
4500 have a hard time placing the field without putting two
4501 objects of the same type at the same address. */
4502 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4503 /* Add the new FIELD_DECL to the list of fields for T. */
4504 DECL_CHAIN (decl) = *next_field;
4505 *next_field = decl;
4506 next_field = &DECL_CHAIN (decl);
4509 else
4511 tree eoc;
4512 bool atend;
4514 /* On some platforms (ARM), even empty classes will not be
4515 byte-aligned. */
4516 eoc = round_up_loc (input_location,
4517 rli_size_unit_so_far (rli),
4518 CLASSTYPE_ALIGN_UNIT (basetype));
4519 atend = layout_empty_base (rli, binfo, eoc, offsets);
4520 /* A nearly-empty class "has no proper base class that is empty,
4521 not morally virtual, and at an offset other than zero." */
4522 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4524 if (atend)
4525 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4526 /* The check above (used in G++ 3.2) is insufficient because
4527 an empty class placed at offset zero might itself have an
4528 empty base at a nonzero offset. */
4529 else if (walk_subobject_offsets (basetype,
4530 empty_base_at_nonzero_offset_p,
4531 size_zero_node,
4532 /*offsets=*/NULL,
4533 /*max_offset=*/NULL_TREE,
4534 /*vbases_p=*/true))
4535 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4538 /* We do not create a FIELD_DECL for empty base classes because
4539 it might overlap some other field. We want to be able to
4540 create CONSTRUCTORs for the class by iterating over the
4541 FIELD_DECLs, and the back end does not handle overlapping
4542 FIELD_DECLs. */
4544 /* An empty virtual base causes a class to be non-empty
4545 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4546 here because that was already done when the virtual table
4547 pointer was created. */
4550 /* Record the offsets of BINFO and its base subobjects. */
4551 record_subobject_offsets (binfo,
4552 BINFO_OFFSET (binfo),
4553 offsets,
4554 /*is_data_member=*/false);
4556 return next_field;
4559 /* Layout all of the non-virtual base classes. Record empty
4560 subobjects in OFFSETS. T is the most derived type. Return nonzero
4561 if the type cannot be nearly empty. The fields created
4562 corresponding to the base classes will be inserted at
4563 *NEXT_FIELD. */
4565 static void
4566 build_base_fields (record_layout_info rli,
4567 splay_tree offsets, tree *next_field)
4569 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4570 subobjects. */
4571 tree t = rli->t;
4572 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4573 int i;
4575 /* The primary base class is always allocated first. */
4576 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4577 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4578 offsets, next_field);
4580 /* Now allocate the rest of the bases. */
4581 for (i = 0; i < n_baseclasses; ++i)
4583 tree base_binfo;
4585 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4587 /* The primary base was already allocated above, so we don't
4588 need to allocate it again here. */
4589 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4590 continue;
4592 /* Virtual bases are added at the end (a primary virtual base
4593 will have already been added). */
4594 if (BINFO_VIRTUAL_P (base_binfo))
4595 continue;
4597 next_field = build_base_field (rli, base_binfo,
4598 offsets, next_field);
4602 /* Go through the TYPE_METHODS of T issuing any appropriate
4603 diagnostics, figuring out which methods override which other
4604 methods, and so forth. */
4606 static void
4607 check_methods (tree t)
4609 tree x;
4611 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4613 check_for_override (x, t);
4614 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4615 error ("initializer specified for non-virtual method %q+D", x);
4616 /* The name of the field is the original field name
4617 Save this in auxiliary field for later overloading. */
4618 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4620 TYPE_POLYMORPHIC_P (t) = 1;
4621 if (DECL_PURE_VIRTUAL_P (x))
4622 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4624 /* All user-provided destructors are non-trivial.
4625 Constructors and assignment ops are handled in
4626 grok_special_member_properties. */
4627 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4628 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4629 if (!DECL_VIRTUAL_P (x)
4630 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x)))
4631 error_at (DECL_SOURCE_LOCATION (x),
4632 "%<transaction_safe_dynamic%> may only be specified for "
4633 "a virtual function");
4637 /* FN is a constructor or destructor. Clone the declaration to create
4638 a specialized in-charge or not-in-charge version, as indicated by
4639 NAME. */
4641 static tree
4642 build_clone (tree fn, tree name)
4644 tree parms;
4645 tree clone;
4647 /* Copy the function. */
4648 clone = copy_decl (fn);
4649 /* Reset the function name. */
4650 DECL_NAME (clone) = name;
4651 /* Remember where this function came from. */
4652 DECL_ABSTRACT_ORIGIN (clone) = fn;
4653 /* Make it easy to find the CLONE given the FN. */
4654 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4655 DECL_CHAIN (fn) = clone;
4657 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4658 if (TREE_CODE (clone) == TEMPLATE_DECL)
4660 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4661 DECL_TEMPLATE_RESULT (clone) = result;
4662 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4663 DECL_TI_TEMPLATE (result) = clone;
4664 TREE_TYPE (clone) = TREE_TYPE (result);
4665 return clone;
4667 else
4669 // Clone constraints.
4670 if (flag_concepts)
4671 if (tree ci = get_constraints (fn))
4672 set_constraints (clone, copy_node (ci));
4676 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4677 DECL_CLONED_FUNCTION (clone) = fn;
4678 /* There's no pending inline data for this function. */
4679 DECL_PENDING_INLINE_INFO (clone) = NULL;
4680 DECL_PENDING_INLINE_P (clone) = 0;
4682 /* The base-class destructor is not virtual. */
4683 if (name == base_dtor_identifier)
4685 DECL_VIRTUAL_P (clone) = 0;
4686 if (TREE_CODE (clone) != TEMPLATE_DECL)
4687 DECL_VINDEX (clone) = NULL_TREE;
4690 /* If there was an in-charge parameter, drop it from the function
4691 type. */
4692 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4694 tree basetype;
4695 tree parmtypes;
4696 tree exceptions;
4698 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4699 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4700 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4701 /* Skip the `this' parameter. */
4702 parmtypes = TREE_CHAIN (parmtypes);
4703 /* Skip the in-charge parameter. */
4704 parmtypes = TREE_CHAIN (parmtypes);
4705 /* And the VTT parm, in a complete [cd]tor. */
4706 if (DECL_HAS_VTT_PARM_P (fn)
4707 && ! DECL_NEEDS_VTT_PARM_P (clone))
4708 parmtypes = TREE_CHAIN (parmtypes);
4709 /* If this is subobject constructor or destructor, add the vtt
4710 parameter. */
4711 TREE_TYPE (clone)
4712 = build_method_type_directly (basetype,
4713 TREE_TYPE (TREE_TYPE (clone)),
4714 parmtypes);
4715 if (exceptions)
4716 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4717 exceptions);
4718 TREE_TYPE (clone)
4719 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4720 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4723 /* Copy the function parameters. */
4724 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4725 /* Remove the in-charge parameter. */
4726 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4728 DECL_CHAIN (DECL_ARGUMENTS (clone))
4729 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4730 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4732 /* And the VTT parm, in a complete [cd]tor. */
4733 if (DECL_HAS_VTT_PARM_P (fn))
4735 if (DECL_NEEDS_VTT_PARM_P (clone))
4736 DECL_HAS_VTT_PARM_P (clone) = 1;
4737 else
4739 DECL_CHAIN (DECL_ARGUMENTS (clone))
4740 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4741 DECL_HAS_VTT_PARM_P (clone) = 0;
4745 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4747 DECL_CONTEXT (parms) = clone;
4748 cxx_dup_lang_specific_decl (parms);
4751 /* Create the RTL for this function. */
4752 SET_DECL_RTL (clone, NULL);
4753 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4755 return clone;
4758 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4759 not invoke this function directly.
4761 For a non-thunk function, returns the address of the slot for storing
4762 the function it is a clone of. Otherwise returns NULL_TREE.
4764 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4765 cloned_function is unset. This is to support the separate
4766 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4767 on a template makes sense, but not the former. */
4769 tree *
4770 decl_cloned_function_p (const_tree decl, bool just_testing)
4772 tree *ptr;
4773 if (just_testing)
4774 decl = STRIP_TEMPLATE (decl);
4776 if (TREE_CODE (decl) != FUNCTION_DECL
4777 || !DECL_LANG_SPECIFIC (decl)
4778 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4780 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4781 if (!just_testing)
4782 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4783 else
4784 #endif
4785 return NULL;
4788 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4789 if (just_testing && *ptr == NULL_TREE)
4790 return NULL;
4791 else
4792 return ptr;
4795 /* Produce declarations for all appropriate clones of FN. If
4796 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4797 CLASTYPE_METHOD_VEC as well. */
4799 void
4800 clone_function_decl (tree fn, int update_method_vec_p)
4802 tree clone;
4804 /* Avoid inappropriate cloning. */
4805 if (DECL_CHAIN (fn)
4806 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4807 return;
4809 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4811 /* For each constructor, we need two variants: an in-charge version
4812 and a not-in-charge version. */
4813 clone = build_clone (fn, complete_ctor_identifier);
4814 if (update_method_vec_p)
4815 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4816 clone = build_clone (fn, base_ctor_identifier);
4817 if (update_method_vec_p)
4818 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4820 else
4822 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4824 /* For each destructor, we need three variants: an in-charge
4825 version, a not-in-charge version, and an in-charge deleting
4826 version. We clone the deleting version first because that
4827 means it will go second on the TYPE_METHODS list -- and that
4828 corresponds to the correct layout order in the virtual
4829 function table.
4831 For a non-virtual destructor, we do not build a deleting
4832 destructor. */
4833 if (DECL_VIRTUAL_P (fn))
4835 clone = build_clone (fn, deleting_dtor_identifier);
4836 if (update_method_vec_p)
4837 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4839 clone = build_clone (fn, complete_dtor_identifier);
4840 if (update_method_vec_p)
4841 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4842 clone = build_clone (fn, base_dtor_identifier);
4843 if (update_method_vec_p)
4844 add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
4847 /* Note that this is an abstract function that is never emitted. */
4848 DECL_ABSTRACT_P (fn) = true;
4851 /* DECL is an in charge constructor, which is being defined. This will
4852 have had an in class declaration, from whence clones were
4853 declared. An out-of-class definition can specify additional default
4854 arguments. As it is the clones that are involved in overload
4855 resolution, we must propagate the information from the DECL to its
4856 clones. */
4858 void
4859 adjust_clone_args (tree decl)
4861 tree clone;
4863 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4864 clone = DECL_CHAIN (clone))
4866 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4867 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4868 tree decl_parms, clone_parms;
4870 clone_parms = orig_clone_parms;
4872 /* Skip the 'this' parameter. */
4873 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4874 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4876 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4877 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4878 if (DECL_HAS_VTT_PARM_P (decl))
4879 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4881 clone_parms = orig_clone_parms;
4882 if (DECL_HAS_VTT_PARM_P (clone))
4883 clone_parms = TREE_CHAIN (clone_parms);
4885 for (decl_parms = orig_decl_parms; decl_parms;
4886 decl_parms = TREE_CHAIN (decl_parms),
4887 clone_parms = TREE_CHAIN (clone_parms))
4889 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4890 TREE_TYPE (clone_parms)));
4892 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4894 /* A default parameter has been added. Adjust the
4895 clone's parameters. */
4896 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4897 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4898 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4899 tree type;
4901 clone_parms = orig_decl_parms;
4903 if (DECL_HAS_VTT_PARM_P (clone))
4905 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4906 TREE_VALUE (orig_clone_parms),
4907 clone_parms);
4908 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4910 type = build_method_type_directly (basetype,
4911 TREE_TYPE (TREE_TYPE (clone)),
4912 clone_parms);
4913 if (exceptions)
4914 type = build_exception_variant (type, exceptions);
4915 if (attrs)
4916 type = cp_build_type_attribute_variant (type, attrs);
4917 TREE_TYPE (clone) = type;
4919 clone_parms = NULL_TREE;
4920 break;
4923 gcc_assert (!clone_parms);
4927 /* For each of the constructors and destructors in T, create an
4928 in-charge and not-in-charge variant. */
4930 static void
4931 clone_constructors_and_destructors (tree t)
4933 tree fns;
4935 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4936 out now. */
4937 if (!CLASSTYPE_METHOD_VEC (t))
4938 return;
4940 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4941 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4942 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4943 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4946 /* Deduce noexcept for a destructor DTOR. */
4948 void
4949 deduce_noexcept_on_destructor (tree dtor)
4951 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4953 tree eh_spec = unevaluated_noexcept_spec ();
4954 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
4958 /* For each destructor in T, deduce noexcept:
4960 12.4/3: A declaration of a destructor that does not have an
4961 exception-specification is implicitly considered to have the
4962 same exception-specification as an implicit declaration (15.4). */
4964 static void
4965 deduce_noexcept_on_destructors (tree t)
4967 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4968 out now. */
4969 if (!CLASSTYPE_METHOD_VEC (t))
4970 return;
4972 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4973 deduce_noexcept_on_destructor (OVL_CURRENT (fns));
4976 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4977 of TYPE for virtual functions which FNDECL overrides. Return a
4978 mask of the tm attributes found therein. */
4980 static int
4981 look_for_tm_attr_overrides (tree type, tree fndecl)
4983 tree binfo = TYPE_BINFO (type);
4984 tree base_binfo;
4985 int ix, found = 0;
4987 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4989 tree o, basetype = BINFO_TYPE (base_binfo);
4991 if (!TYPE_POLYMORPHIC_P (basetype))
4992 continue;
4994 o = look_for_overrides_here (basetype, fndecl);
4995 if (o)
4997 if (lookup_attribute ("transaction_safe_dynamic",
4998 DECL_ATTRIBUTES (o)))
4999 /* transaction_safe_dynamic is not inherited. */;
5000 else
5001 found |= tm_attr_to_mask (find_tm_attribute
5002 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5004 else
5005 found |= look_for_tm_attr_overrides (basetype, fndecl);
5008 return found;
5011 /* Subroutine of set_method_tm_attributes. Handle the checks and
5012 inheritance for one virtual method FNDECL. */
5014 static void
5015 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5017 tree tm_attr;
5018 int found, have;
5020 found = look_for_tm_attr_overrides (type, fndecl);
5022 /* If FNDECL doesn't actually override anything (i.e. T is the
5023 class that first declares FNDECL virtual), then we're done. */
5024 if (found == 0)
5025 return;
5027 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5028 have = tm_attr_to_mask (tm_attr);
5030 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5031 tm_pure must match exactly, otherwise no weakening of
5032 tm_safe > tm_callable > nothing. */
5033 /* ??? The tm_pure attribute didn't make the transition to the
5034 multivendor language spec. */
5035 if (have == TM_ATTR_PURE)
5037 if (found != TM_ATTR_PURE)
5039 found &= -found;
5040 goto err_override;
5043 /* If the overridden function is tm_pure, then FNDECL must be. */
5044 else if (found == TM_ATTR_PURE && tm_attr)
5045 goto err_override;
5046 /* Look for base class combinations that cannot be satisfied. */
5047 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5049 found &= ~TM_ATTR_PURE;
5050 found &= -found;
5051 error_at (DECL_SOURCE_LOCATION (fndecl),
5052 "method overrides both %<transaction_pure%> and %qE methods",
5053 tm_mask_to_attr (found));
5055 /* If FNDECL did not declare an attribute, then inherit the most
5056 restrictive one. */
5057 else if (tm_attr == NULL)
5059 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5061 /* Otherwise validate that we're not weaker than a function
5062 that is being overridden. */
5063 else
5065 found &= -found;
5066 if (found <= TM_ATTR_CALLABLE && have > found)
5067 goto err_override;
5069 return;
5071 err_override:
5072 error_at (DECL_SOURCE_LOCATION (fndecl),
5073 "method declared %qE overriding %qE method",
5074 tm_attr, tm_mask_to_attr (found));
5077 /* For each of the methods in T, propagate a class-level tm attribute. */
5079 static void
5080 set_method_tm_attributes (tree t)
5082 tree class_tm_attr, fndecl;
5084 /* Don't bother collecting tm attributes if transactional memory
5085 support is not enabled. */
5086 if (!flag_tm)
5087 return;
5089 /* Process virtual methods first, as they inherit directly from the
5090 base virtual function and also require validation of new attributes. */
5091 if (TYPE_CONTAINS_VPTR_P (t))
5093 tree vchain;
5094 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5095 vchain = TREE_CHAIN (vchain))
5097 fndecl = BV_FN (vchain);
5098 if (DECL_THUNK_P (fndecl))
5099 fndecl = THUNK_TARGET (fndecl);
5100 set_one_vmethod_tm_attributes (t, fndecl);
5104 /* If the class doesn't have an attribute, nothing more to do. */
5105 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5106 if (class_tm_attr == NULL)
5107 return;
5109 /* Any method that does not yet have a tm attribute inherits
5110 the one from the class. */
5111 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5113 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5114 apply_tm_attr (fndecl, class_tm_attr);
5118 /* Returns true if FN is a default constructor. */
5120 bool
5121 default_ctor_p (tree fn)
5123 return (DECL_CONSTRUCTOR_P (fn)
5124 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5127 /* Returns true iff class T has a user-defined constructor that can be called
5128 with more than zero arguments. */
5130 bool
5131 type_has_user_nondefault_constructor (tree t)
5133 tree fns;
5135 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5136 return false;
5138 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5140 tree fn = OVL_CURRENT (fns);
5141 if (!DECL_ARTIFICIAL (fn)
5142 && (TREE_CODE (fn) == TEMPLATE_DECL
5143 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5144 != NULL_TREE)))
5145 return true;
5148 return false;
5151 /* Returns the defaulted constructor if T has one. Otherwise, returns
5152 NULL_TREE. */
5154 tree
5155 in_class_defaulted_default_constructor (tree t)
5157 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5158 return NULL_TREE;
5160 for (tree fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5162 tree fn = OVL_CURRENT (fns);
5164 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5165 && default_ctor_p (fn))
5166 return fn;
5169 return NULL_TREE;
5172 /* Returns true iff FN is a user-provided function, i.e. user-declared
5173 and not defaulted at its first declaration. */
5175 bool
5176 user_provided_p (tree fn)
5178 if (TREE_CODE (fn) == TEMPLATE_DECL)
5179 return true;
5180 else
5181 return (!DECL_ARTIFICIAL (fn)
5182 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5183 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5186 /* Returns true iff class T has a user-provided constructor. */
5188 bool
5189 type_has_user_provided_constructor (tree t)
5191 tree fns;
5193 if (!CLASS_TYPE_P (t))
5194 return false;
5196 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5197 return false;
5199 /* This can happen in error cases; avoid crashing. */
5200 if (!CLASSTYPE_METHOD_VEC (t))
5201 return false;
5203 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5204 if (user_provided_p (OVL_CURRENT (fns)))
5205 return true;
5207 return false;
5210 /* Returns true iff class T has a user-provided or explicit constructor. */
5212 bool
5213 type_has_user_provided_or_explicit_constructor (tree t)
5215 tree fns;
5217 if (!CLASS_TYPE_P (t))
5218 return false;
5220 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5221 return false;
5223 /* This can happen in error cases; avoid crashing. */
5224 if (!CLASSTYPE_METHOD_VEC (t))
5225 return false;
5227 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5229 tree fn = OVL_CURRENT (fns);
5230 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5231 return true;
5234 return false;
5237 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5238 declared or explicitly defaulted in the class body) default
5239 constructor. */
5241 bool
5242 type_has_non_user_provided_default_constructor (tree t)
5244 tree fns;
5246 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5247 return false;
5248 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5249 return true;
5251 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5253 tree fn = OVL_CURRENT (fns);
5254 if (TREE_CODE (fn) == FUNCTION_DECL
5255 && default_ctor_p (fn)
5256 && !user_provided_p (fn))
5257 return true;
5260 return false;
5263 /* TYPE is being used as a virtual base, and has a non-trivial move
5264 assignment. Return true if this is due to there being a user-provided
5265 move assignment in TYPE or one of its subobjects; if there isn't, then
5266 multiple move assignment can't cause any harm. */
5268 bool
5269 vbase_has_user_provided_move_assign (tree type)
5271 /* Does the type itself have a user-provided move assignment operator? */
5272 for (tree fns
5273 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR));
5274 fns; fns = OVL_NEXT (fns))
5276 tree fn = OVL_CURRENT (fns);
5277 if (move_fn_p (fn) && user_provided_p (fn))
5278 return true;
5281 /* Do any of its bases? */
5282 tree binfo = TYPE_BINFO (type);
5283 tree base_binfo;
5284 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5285 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5286 return true;
5288 /* Or non-static data members? */
5289 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5291 if (TREE_CODE (field) == FIELD_DECL
5292 && CLASS_TYPE_P (TREE_TYPE (field))
5293 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5294 return true;
5297 /* Seems not. */
5298 return false;
5301 /* If default-initialization leaves part of TYPE uninitialized, returns
5302 a DECL for the field or TYPE itself (DR 253). */
5304 tree
5305 default_init_uninitialized_part (tree type)
5307 tree t, r, binfo;
5308 int i;
5310 type = strip_array_types (type);
5311 if (!CLASS_TYPE_P (type))
5312 return type;
5313 if (!type_has_non_user_provided_default_constructor (type))
5314 return NULL_TREE;
5315 for (binfo = TYPE_BINFO (type), i = 0;
5316 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5318 r = default_init_uninitialized_part (BINFO_TYPE (t));
5319 if (r)
5320 return r;
5322 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5323 if (TREE_CODE (t) == FIELD_DECL
5324 && !DECL_ARTIFICIAL (t)
5325 && !DECL_INITIAL (t))
5327 r = default_init_uninitialized_part (TREE_TYPE (t));
5328 if (r)
5329 return DECL_P (r) ? r : t;
5332 return NULL_TREE;
5335 /* Returns true iff for class T, a trivial synthesized default constructor
5336 would be constexpr. */
5338 bool
5339 trivial_default_constructor_is_constexpr (tree t)
5341 /* A defaulted trivial default constructor is constexpr
5342 if there is nothing to initialize. */
5343 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5344 return is_really_empty_class (t);
5347 /* Returns true iff class T has a constexpr default constructor. */
5349 bool
5350 type_has_constexpr_default_constructor (tree t)
5352 tree fns;
5354 if (!CLASS_TYPE_P (t))
5356 /* The caller should have stripped an enclosing array. */
5357 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5358 return false;
5360 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5362 if (!TYPE_HAS_COMPLEX_DFLT (t))
5363 return trivial_default_constructor_is_constexpr (t);
5364 /* Non-trivial, we need to check subobject constructors. */
5365 lazily_declare_fn (sfk_constructor, t);
5367 fns = locate_ctor (t);
5368 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5371 /* Returns true iff class T has a constexpr default constructor or has an
5372 implicitly declared default constructor that we can't tell if it's constexpr
5373 without forcing a lazy declaration (which might cause undesired
5374 instantiations). */
5376 bool
5377 type_maybe_constexpr_default_constructor (tree t)
5379 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5380 && TYPE_HAS_COMPLEX_DFLT (t))
5381 /* Assume it's constexpr. */
5382 return true;
5383 return type_has_constexpr_default_constructor (t);
5386 /* Returns true iff class TYPE has a virtual destructor. */
5388 bool
5389 type_has_virtual_destructor (tree type)
5391 tree dtor;
5393 if (!CLASS_TYPE_P (type))
5394 return false;
5396 gcc_assert (COMPLETE_TYPE_P (type));
5397 dtor = CLASSTYPE_DESTRUCTORS (type);
5398 return (dtor && DECL_VIRTUAL_P (dtor));
5401 /* Returns true iff class T has a move constructor. */
5403 bool
5404 type_has_move_constructor (tree t)
5406 tree fns;
5408 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5410 gcc_assert (COMPLETE_TYPE_P (t));
5411 lazily_declare_fn (sfk_move_constructor, t);
5414 if (!CLASSTYPE_METHOD_VEC (t))
5415 return false;
5417 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5418 if (move_fn_p (OVL_CURRENT (fns)))
5419 return true;
5421 return false;
5424 /* Returns true iff class T has a move assignment operator. */
5426 bool
5427 type_has_move_assign (tree t)
5429 tree fns;
5431 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5433 gcc_assert (COMPLETE_TYPE_P (t));
5434 lazily_declare_fn (sfk_move_assignment, t);
5437 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5438 fns; fns = OVL_NEXT (fns))
5439 if (move_fn_p (OVL_CURRENT (fns)))
5440 return true;
5442 return false;
5445 /* Returns true iff class T has a move constructor that was explicitly
5446 declared in the class body. Note that this is different from
5447 "user-provided", which doesn't include functions that are defaulted in
5448 the class. */
5450 bool
5451 type_has_user_declared_move_constructor (tree t)
5453 tree fns;
5455 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5456 return false;
5458 if (!CLASSTYPE_METHOD_VEC (t))
5459 return false;
5461 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5463 tree fn = OVL_CURRENT (fns);
5464 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5465 return true;
5468 return false;
5471 /* Returns true iff class T has a move assignment operator that was
5472 explicitly declared in the class body. */
5474 bool
5475 type_has_user_declared_move_assign (tree t)
5477 tree fns;
5479 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5480 return false;
5482 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR));
5483 fns; fns = OVL_NEXT (fns))
5485 tree fn = OVL_CURRENT (fns);
5486 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5487 return true;
5490 return false;
5493 /* Nonzero if we need to build up a constructor call when initializing an
5494 object of this class, either because it has a user-declared constructor
5495 or because it doesn't have a default constructor (so we need to give an
5496 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5497 what you care about is whether or not an object can be produced by a
5498 constructor (e.g. so we don't set TREE_READONLY on const variables of
5499 such type); use this function when what you care about is whether or not
5500 to try to call a constructor to create an object. The latter case is
5501 the former plus some cases of constructors that cannot be called. */
5503 bool
5504 type_build_ctor_call (tree t)
5506 tree inner;
5507 if (TYPE_NEEDS_CONSTRUCTING (t))
5508 return true;
5509 inner = strip_array_types (t);
5510 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5511 return false;
5512 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5513 return true;
5514 if (cxx_dialect < cxx11)
5515 return false;
5516 /* A user-declared constructor might be private, and a constructor might
5517 be trivial but deleted. */
5518 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier);
5519 fns; fns = OVL_NEXT (fns))
5521 tree fn = OVL_CURRENT (fns);
5522 if (!DECL_ARTIFICIAL (fn)
5523 || DECL_DELETED_FN (fn))
5524 return true;
5526 return false;
5529 /* Like type_build_ctor_call, but for destructors. */
5531 bool
5532 type_build_dtor_call (tree t)
5534 tree inner;
5535 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5536 return true;
5537 inner = strip_array_types (t);
5538 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5539 || !COMPLETE_TYPE_P (inner))
5540 return false;
5541 if (cxx_dialect < cxx11)
5542 return false;
5543 /* A user-declared destructor might be private, and a destructor might
5544 be trivial but deleted. */
5545 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier);
5546 fns; fns = OVL_NEXT (fns))
5548 tree fn = OVL_CURRENT (fns);
5549 if (!DECL_ARTIFICIAL (fn)
5550 || DECL_DELETED_FN (fn))
5551 return true;
5553 return false;
5556 /* Remove all zero-width bit-fields from T. */
5558 static void
5559 remove_zero_width_bit_fields (tree t)
5561 tree *fieldsp;
5563 fieldsp = &TYPE_FIELDS (t);
5564 while (*fieldsp)
5566 if (TREE_CODE (*fieldsp) == FIELD_DECL
5567 && DECL_C_BIT_FIELD (*fieldsp)
5568 /* We should not be confused by the fact that grokbitfield
5569 temporarily sets the width of the bit field into
5570 DECL_INITIAL (*fieldsp).
5571 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5572 to that width. */
5573 && (DECL_SIZE (*fieldsp) == NULL_TREE
5574 || integer_zerop (DECL_SIZE (*fieldsp))))
5575 *fieldsp = DECL_CHAIN (*fieldsp);
5576 else
5577 fieldsp = &DECL_CHAIN (*fieldsp);
5581 /* Returns TRUE iff we need a cookie when dynamically allocating an
5582 array whose elements have the indicated class TYPE. */
5584 static bool
5585 type_requires_array_cookie (tree type)
5587 tree fns;
5588 bool has_two_argument_delete_p = false;
5590 gcc_assert (CLASS_TYPE_P (type));
5592 /* If there's a non-trivial destructor, we need a cookie. In order
5593 to iterate through the array calling the destructor for each
5594 element, we'll have to know how many elements there are. */
5595 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5596 return true;
5598 /* If the usual deallocation function is a two-argument whose second
5599 argument is of type `size_t', then we have to pass the size of
5600 the array to the deallocation function, so we will need to store
5601 a cookie. */
5602 fns = lookup_fnfields (TYPE_BINFO (type),
5603 ansi_opname (VEC_DELETE_EXPR),
5604 /*protect=*/0);
5605 /* If there are no `operator []' members, or the lookup is
5606 ambiguous, then we don't need a cookie. */
5607 if (!fns || fns == error_mark_node)
5608 return false;
5609 /* Loop through all of the functions. */
5610 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
5612 tree fn;
5613 tree second_parm;
5615 /* Select the current function. */
5616 fn = OVL_CURRENT (fns);
5617 /* See if this function is a one-argument delete function. If
5618 it is, then it will be the usual deallocation function. */
5619 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5620 if (second_parm == void_list_node)
5621 return false;
5622 /* Do not consider this function if its second argument is an
5623 ellipsis. */
5624 if (!second_parm)
5625 continue;
5626 /* Otherwise, if we have a two-argument function and the second
5627 argument is `size_t', it will be the usual deallocation
5628 function -- unless there is one-argument function, too. */
5629 if (TREE_CHAIN (second_parm) == void_list_node
5630 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5631 has_two_argument_delete_p = true;
5634 return has_two_argument_delete_p;
5637 /* Finish computing the `literal type' property of class type T.
5639 At this point, we have already processed base classes and
5640 non-static data members. We need to check whether the copy
5641 constructor is trivial, the destructor is trivial, and there
5642 is a trivial default constructor or at least one constexpr
5643 constructor other than the copy constructor. */
5645 static void
5646 finalize_literal_type_property (tree t)
5648 tree fn;
5650 if (cxx_dialect < cxx11
5651 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5652 CLASSTYPE_LITERAL_P (t) = false;
5653 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5654 && CLASSTYPE_NON_AGGREGATE (t)
5655 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5656 CLASSTYPE_LITERAL_P (t) = false;
5658 if (!CLASSTYPE_LITERAL_P (t))
5659 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5660 if (DECL_DECLARED_CONSTEXPR_P (fn)
5661 && TREE_CODE (fn) != TEMPLATE_DECL
5662 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5663 && !DECL_CONSTRUCTOR_P (fn))
5665 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5666 if (!DECL_GENERATED_P (fn) && !LAMBDA_TYPE_P (t))
5668 error ("enclosing class of constexpr non-static member "
5669 "function %q+#D is not a literal type", fn);
5670 explain_non_literal_class (t);
5675 /* T is a non-literal type used in a context which requires a constant
5676 expression. Explain why it isn't literal. */
5678 void
5679 explain_non_literal_class (tree t)
5681 static hash_set<tree> *diagnosed;
5683 if (!CLASS_TYPE_P (t))
5684 return;
5685 t = TYPE_MAIN_VARIANT (t);
5687 if (diagnosed == NULL)
5688 diagnosed = new hash_set<tree>;
5689 if (diagnosed->add (t))
5690 /* Already explained. */
5691 return;
5693 inform (0, "%q+T is not literal because:", t);
5694 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5695 inform (0, " %q+T has a non-trivial destructor", t);
5696 else if (CLASSTYPE_NON_AGGREGATE (t)
5697 && !TYPE_HAS_TRIVIAL_DFLT (t)
5698 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5700 inform (0, " %q+T is not an aggregate, does not have a trivial "
5701 "default constructor, and has no constexpr constructor that "
5702 "is not a copy or move constructor", t);
5703 if (type_has_non_user_provided_default_constructor (t))
5705 /* Note that we can't simply call locate_ctor because when the
5706 constructor is deleted it just returns NULL_TREE. */
5707 tree fns;
5708 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
5710 tree fn = OVL_CURRENT (fns);
5711 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5713 parms = skip_artificial_parms_for (fn, parms);
5715 if (sufficient_parms_p (parms))
5717 if (DECL_DELETED_FN (fn))
5718 maybe_explain_implicit_delete (fn);
5719 else
5720 explain_invalid_constexpr_fn (fn);
5721 break;
5726 else
5728 tree binfo, base_binfo, field; int i;
5729 for (binfo = TYPE_BINFO (t), i = 0;
5730 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5732 tree basetype = TREE_TYPE (base_binfo);
5733 if (!CLASSTYPE_LITERAL_P (basetype))
5735 inform (0, " base class %qT of %q+T is non-literal",
5736 basetype, t);
5737 explain_non_literal_class (basetype);
5738 return;
5741 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5743 tree ftype;
5744 if (TREE_CODE (field) != FIELD_DECL)
5745 continue;
5746 ftype = TREE_TYPE (field);
5747 if (!literal_type_p (ftype))
5749 inform (DECL_SOURCE_LOCATION (field),
5750 " non-static data member %qD has non-literal type",
5751 field);
5752 if (CLASS_TYPE_P (ftype))
5753 explain_non_literal_class (ftype);
5755 if (CP_TYPE_VOLATILE_P (ftype))
5756 inform (DECL_SOURCE_LOCATION (field),
5757 " non-static data member %qD has volatile type", field);
5762 /* Check the validity of the bases and members declared in T. Add any
5763 implicitly-generated functions (like copy-constructors and
5764 assignment operators). Compute various flag bits (like
5765 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5766 level: i.e., independently of the ABI in use. */
5768 static void
5769 check_bases_and_members (tree t)
5771 /* Nonzero if the implicitly generated copy constructor should take
5772 a non-const reference argument. */
5773 int cant_have_const_ctor;
5774 /* Nonzero if the implicitly generated assignment operator
5775 should take a non-const reference argument. */
5776 int no_const_asn_ref;
5777 tree access_decls;
5778 bool saved_complex_asn_ref;
5779 bool saved_nontrivial_dtor;
5780 tree fn;
5782 /* By default, we use const reference arguments and generate default
5783 constructors. */
5784 cant_have_const_ctor = 0;
5785 no_const_asn_ref = 0;
5787 /* Check all the base-classes and set FMEM members to point to arrays
5788 of potential interest. */
5789 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5791 /* Deduce noexcept on destructors. This needs to happen after we've set
5792 triviality flags appropriately for our bases. */
5793 if (cxx_dialect >= cxx11)
5794 deduce_noexcept_on_destructors (t);
5796 /* Check all the method declarations. */
5797 check_methods (t);
5799 /* Save the initial values of these flags which only indicate whether
5800 or not the class has user-provided functions. As we analyze the
5801 bases and members we can set these flags for other reasons. */
5802 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5803 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5805 /* Check all the data member declarations. We cannot call
5806 check_field_decls until we have called check_bases check_methods,
5807 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5808 being set appropriately. */
5809 check_field_decls (t, &access_decls,
5810 &cant_have_const_ctor,
5811 &no_const_asn_ref);
5813 /* A nearly-empty class has to be vptr-containing; a nearly empty
5814 class contains just a vptr. */
5815 if (!TYPE_CONTAINS_VPTR_P (t))
5816 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5818 /* Do some bookkeeping that will guide the generation of implicitly
5819 declared member functions. */
5820 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5821 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5822 /* We need to call a constructor for this class if it has a
5823 user-provided constructor, or if the default constructor is going
5824 to initialize the vptr. (This is not an if-and-only-if;
5825 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5826 themselves need constructing.) */
5827 TYPE_NEEDS_CONSTRUCTING (t)
5828 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5829 /* [dcl.init.aggr]
5831 An aggregate is an array or a class with no user-provided
5832 constructors ... and no virtual functions.
5834 Again, other conditions for being an aggregate are checked
5835 elsewhere. */
5836 CLASSTYPE_NON_AGGREGATE (t)
5837 |= (type_has_user_provided_or_explicit_constructor (t)
5838 || TYPE_POLYMORPHIC_P (t));
5839 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5840 retain the old definition internally for ABI reasons. */
5841 CLASSTYPE_NON_LAYOUT_POD_P (t)
5842 |= (CLASSTYPE_NON_AGGREGATE (t)
5843 || saved_nontrivial_dtor || saved_complex_asn_ref);
5844 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5845 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5846 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5847 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5849 /* If the only explicitly declared default constructor is user-provided,
5850 set TYPE_HAS_COMPLEX_DFLT. */
5851 if (!TYPE_HAS_COMPLEX_DFLT (t)
5852 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5853 && !type_has_non_user_provided_default_constructor (t))
5854 TYPE_HAS_COMPLEX_DFLT (t) = true;
5856 /* Warn if a public base of a polymorphic type has an accessible
5857 non-virtual destructor. It is only now that we know the class is
5858 polymorphic. Although a polymorphic base will have a already
5859 been diagnosed during its definition, we warn on use too. */
5860 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5862 tree binfo = TYPE_BINFO (t);
5863 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5864 tree base_binfo;
5865 unsigned i;
5867 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5869 tree basetype = TREE_TYPE (base_binfo);
5871 if ((*accesses)[i] == access_public_node
5872 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5873 && accessible_nvdtor_p (basetype))
5874 warning (OPT_Wnon_virtual_dtor,
5875 "base class %q#T has accessible non-virtual destructor",
5876 basetype);
5880 /* If the class has no user-declared constructor, but does have
5881 non-static const or reference data members that can never be
5882 initialized, issue a warning. */
5883 if (warn_uninitialized
5884 /* Classes with user-declared constructors are presumed to
5885 initialize these members. */
5886 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5887 /* Aggregates can be initialized with brace-enclosed
5888 initializers. */
5889 && CLASSTYPE_NON_AGGREGATE (t))
5891 tree field;
5893 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5895 tree type;
5897 if (TREE_CODE (field) != FIELD_DECL
5898 || DECL_INITIAL (field) != NULL_TREE)
5899 continue;
5901 type = TREE_TYPE (field);
5902 if (TREE_CODE (type) == REFERENCE_TYPE)
5903 warning_at (DECL_SOURCE_LOCATION (field),
5904 OPT_Wuninitialized, "non-static reference %q#D "
5905 "in class without a constructor", field);
5906 else if (CP_TYPE_CONST_P (type)
5907 && (!CLASS_TYPE_P (type)
5908 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5909 warning_at (DECL_SOURCE_LOCATION (field),
5910 OPT_Wuninitialized, "non-static const member %q#D "
5911 "in class without a constructor", field);
5915 /* Synthesize any needed methods. */
5916 add_implicitly_declared_members (t, &access_decls,
5917 cant_have_const_ctor,
5918 no_const_asn_ref);
5920 /* Check defaulted declarations here so we have cant_have_const_ctor
5921 and don't need to worry about clones. */
5922 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5923 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5925 int copy = copy_fn_p (fn);
5926 if (copy > 0)
5928 bool imp_const_p
5929 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5930 : !no_const_asn_ref);
5931 bool fn_const_p = (copy == 2);
5933 if (fn_const_p && !imp_const_p)
5934 /* If the function is defaulted outside the class, we just
5935 give the synthesis error. */
5936 error ("%q+D declared to take const reference, but implicit "
5937 "declaration would take non-const", fn);
5939 defaulted_late_check (fn);
5942 if (LAMBDA_TYPE_P (t))
5944 /* "This class type is not an aggregate." */
5945 CLASSTYPE_NON_AGGREGATE (t) = 1;
5948 /* Compute the 'literal type' property before we
5949 do anything with non-static member functions. */
5950 finalize_literal_type_property (t);
5952 /* Create the in-charge and not-in-charge variants of constructors
5953 and destructors. */
5954 clone_constructors_and_destructors (t);
5956 /* Process the using-declarations. */
5957 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5958 handle_using_decl (TREE_VALUE (access_decls), t);
5960 /* Build and sort the CLASSTYPE_METHOD_VEC. */
5961 finish_struct_methods (t);
5963 /* Figure out whether or not we will need a cookie when dynamically
5964 allocating an array of this type. */
5965 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
5966 = type_requires_array_cookie (t);
5969 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5970 accordingly. If a new vfield was created (because T doesn't have a
5971 primary base class), then the newly created field is returned. It
5972 is not added to the TYPE_FIELDS list; it is the caller's
5973 responsibility to do that. Accumulate declared virtual functions
5974 on VIRTUALS_P. */
5976 static tree
5977 create_vtable_ptr (tree t, tree* virtuals_p)
5979 tree fn;
5981 /* Collect the virtual functions declared in T. */
5982 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5983 if (TREE_CODE (fn) == FUNCTION_DECL
5984 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5985 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5987 tree new_virtual = make_node (TREE_LIST);
5989 BV_FN (new_virtual) = fn;
5990 BV_DELTA (new_virtual) = integer_zero_node;
5991 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5993 TREE_CHAIN (new_virtual) = *virtuals_p;
5994 *virtuals_p = new_virtual;
5997 /* If we couldn't find an appropriate base class, create a new field
5998 here. Even if there weren't any new virtual functions, we might need a
5999 new virtual function table if we're supposed to include vptrs in
6000 all classes that need them. */
6001 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6003 /* We build this decl with vtbl_ptr_type_node, which is a
6004 `vtable_entry_type*'. It might seem more precise to use
6005 `vtable_entry_type (*)[N]' where N is the number of virtual
6006 functions. However, that would require the vtable pointer in
6007 base classes to have a different type than the vtable pointer
6008 in derived classes. We could make that happen, but that
6009 still wouldn't solve all the problems. In particular, the
6010 type-based alias analysis code would decide that assignments
6011 to the base class vtable pointer can't alias assignments to
6012 the derived class vtable pointer, since they have different
6013 types. Thus, in a derived class destructor, where the base
6014 class constructor was inlined, we could generate bad code for
6015 setting up the vtable pointer.
6017 Therefore, we use one type for all vtable pointers. We still
6018 use a type-correct type; it's just doesn't indicate the array
6019 bounds. That's better than using `void*' or some such; it's
6020 cleaner, and it let's the alias analysis code know that these
6021 stores cannot alias stores to void*! */
6022 tree field;
6024 field = build_decl (input_location,
6025 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6026 DECL_VIRTUAL_P (field) = 1;
6027 DECL_ARTIFICIAL (field) = 1;
6028 DECL_FIELD_CONTEXT (field) = t;
6029 DECL_FCONTEXT (field) = t;
6030 if (TYPE_PACKED (t))
6031 DECL_PACKED (field) = 1;
6033 TYPE_VFIELD (t) = field;
6035 /* This class is non-empty. */
6036 CLASSTYPE_EMPTY_P (t) = 0;
6038 return field;
6041 return NULL_TREE;
6044 /* Add OFFSET to all base types of BINFO which is a base in the
6045 hierarchy dominated by T.
6047 OFFSET, which is a type offset, is number of bytes. */
6049 static void
6050 propagate_binfo_offsets (tree binfo, tree offset)
6052 int i;
6053 tree primary_binfo;
6054 tree base_binfo;
6056 /* Update BINFO's offset. */
6057 BINFO_OFFSET (binfo)
6058 = fold_convert (sizetype,
6059 size_binop (PLUS_EXPR,
6060 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6061 offset));
6063 /* Find the primary base class. */
6064 primary_binfo = get_primary_binfo (binfo);
6066 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6067 propagate_binfo_offsets (primary_binfo, offset);
6069 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6070 downwards. */
6071 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6073 /* Don't do the primary base twice. */
6074 if (base_binfo == primary_binfo)
6075 continue;
6077 if (BINFO_VIRTUAL_P (base_binfo))
6078 continue;
6080 propagate_binfo_offsets (base_binfo, offset);
6084 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6085 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6086 empty subobjects of T. */
6088 static void
6089 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6091 tree vbase;
6092 tree t = rli->t;
6093 tree *next_field;
6095 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6096 return;
6098 /* Find the last field. The artificial fields created for virtual
6099 bases will go after the last extant field to date. */
6100 next_field = &TYPE_FIELDS (t);
6101 while (*next_field)
6102 next_field = &DECL_CHAIN (*next_field);
6104 /* Go through the virtual bases, allocating space for each virtual
6105 base that is not already a primary base class. These are
6106 allocated in inheritance graph order. */
6107 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6109 if (!BINFO_VIRTUAL_P (vbase))
6110 continue;
6112 if (!BINFO_PRIMARY_P (vbase))
6114 /* This virtual base is not a primary base of any class in the
6115 hierarchy, so we have to add space for it. */
6116 next_field = build_base_field (rli, vbase,
6117 offsets, next_field);
6122 /* Returns the offset of the byte just past the end of the base class
6123 BINFO. */
6125 static tree
6126 end_of_base (tree binfo)
6128 tree size;
6130 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6131 size = TYPE_SIZE_UNIT (char_type_node);
6132 else if (is_empty_class (BINFO_TYPE (binfo)))
6133 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6134 allocate some space for it. It cannot have virtual bases, so
6135 TYPE_SIZE_UNIT is fine. */
6136 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6137 else
6138 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6140 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6143 /* Returns the offset of the byte just past the end of the base class
6144 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6145 only non-virtual bases are included. */
6147 static tree
6148 end_of_class (tree t, int include_virtuals_p)
6150 tree result = size_zero_node;
6151 vec<tree, va_gc> *vbases;
6152 tree binfo;
6153 tree base_binfo;
6154 tree offset;
6155 int i;
6157 for (binfo = TYPE_BINFO (t), i = 0;
6158 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6160 if (!include_virtuals_p
6161 && BINFO_VIRTUAL_P (base_binfo)
6162 && (!BINFO_PRIMARY_P (base_binfo)
6163 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6164 continue;
6166 offset = end_of_base (base_binfo);
6167 if (tree_int_cst_lt (result, offset))
6168 result = offset;
6171 if (include_virtuals_p)
6172 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6173 vec_safe_iterate (vbases, i, &base_binfo); i++)
6175 offset = end_of_base (base_binfo);
6176 if (tree_int_cst_lt (result, offset))
6177 result = offset;
6180 return result;
6183 /* Warn about bases of T that are inaccessible because they are
6184 ambiguous. For example:
6186 struct S {};
6187 struct T : public S {};
6188 struct U : public S, public T {};
6190 Here, `(S*) new U' is not allowed because there are two `S'
6191 subobjects of U. */
6193 static void
6194 warn_about_ambiguous_bases (tree t)
6196 int i;
6197 vec<tree, va_gc> *vbases;
6198 tree basetype;
6199 tree binfo;
6200 tree base_binfo;
6202 /* If there are no repeated bases, nothing can be ambiguous. */
6203 if (!CLASSTYPE_REPEATED_BASE_P (t))
6204 return;
6206 /* Check direct bases. */
6207 for (binfo = TYPE_BINFO (t), i = 0;
6208 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6210 basetype = BINFO_TYPE (base_binfo);
6212 if (!uniquely_derived_from_p (basetype, t))
6213 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6214 basetype, t);
6217 /* Check for ambiguous virtual bases. */
6218 if (extra_warnings)
6219 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6220 vec_safe_iterate (vbases, i, &binfo); i++)
6222 basetype = BINFO_TYPE (binfo);
6224 if (!uniquely_derived_from_p (basetype, t))
6225 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6226 "to ambiguity", basetype, t);
6230 /* Compare two INTEGER_CSTs K1 and K2. */
6232 static int
6233 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6235 return tree_int_cst_compare ((tree) k1, (tree) k2);
6238 /* Increase the size indicated in RLI to account for empty classes
6239 that are "off the end" of the class. */
6241 static void
6242 include_empty_classes (record_layout_info rli)
6244 tree eoc;
6245 tree rli_size;
6247 /* It might be the case that we grew the class to allocate a
6248 zero-sized base class. That won't be reflected in RLI, yet,
6249 because we are willing to overlay multiple bases at the same
6250 offset. However, now we need to make sure that RLI is big enough
6251 to reflect the entire class. */
6252 eoc = end_of_class (rli->t,
6253 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6254 rli_size = rli_size_unit_so_far (rli);
6255 if (TREE_CODE (rli_size) == INTEGER_CST
6256 && tree_int_cst_lt (rli_size, eoc))
6258 /* The size should have been rounded to a whole byte. */
6259 gcc_assert (tree_int_cst_equal
6260 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6261 rli->bitpos
6262 = size_binop (PLUS_EXPR,
6263 rli->bitpos,
6264 size_binop (MULT_EXPR,
6265 fold_convert (bitsizetype,
6266 size_binop (MINUS_EXPR,
6267 eoc, rli_size)),
6268 bitsize_int (BITS_PER_UNIT)));
6269 normalize_rli (rli);
6273 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6274 BINFO_OFFSETs for all of the base-classes. Position the vtable
6275 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6277 static void
6278 layout_class_type (tree t, tree *virtuals_p)
6280 tree non_static_data_members;
6281 tree field;
6282 tree vptr;
6283 record_layout_info rli;
6284 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6285 types that appear at that offset. */
6286 splay_tree empty_base_offsets;
6287 /* True if the last field laid out was a bit-field. */
6288 bool last_field_was_bitfield = false;
6289 /* The location at which the next field should be inserted. */
6290 tree *next_field;
6291 /* T, as a base class. */
6292 tree base_t;
6294 /* Keep track of the first non-static data member. */
6295 non_static_data_members = TYPE_FIELDS (t);
6297 /* Start laying out the record. */
6298 rli = start_record_layout (t);
6300 /* Mark all the primary bases in the hierarchy. */
6301 determine_primary_bases (t);
6303 /* Create a pointer to our virtual function table. */
6304 vptr = create_vtable_ptr (t, virtuals_p);
6306 /* The vptr is always the first thing in the class. */
6307 if (vptr)
6309 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6310 TYPE_FIELDS (t) = vptr;
6311 next_field = &DECL_CHAIN (vptr);
6312 place_field (rli, vptr);
6314 else
6315 next_field = &TYPE_FIELDS (t);
6317 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6318 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6319 NULL, NULL);
6320 build_base_fields (rli, empty_base_offsets, next_field);
6322 /* Layout the non-static data members. */
6323 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6325 tree type;
6326 tree padding;
6328 /* We still pass things that aren't non-static data members to
6329 the back end, in case it wants to do something with them. */
6330 if (TREE_CODE (field) != FIELD_DECL)
6332 place_field (rli, field);
6333 /* If the static data member has incomplete type, keep track
6334 of it so that it can be completed later. (The handling
6335 of pending statics in finish_record_layout is
6336 insufficient; consider:
6338 struct S1;
6339 struct S2 { static S1 s1; };
6341 At this point, finish_record_layout will be called, but
6342 S1 is still incomplete.) */
6343 if (VAR_P (field))
6345 maybe_register_incomplete_var (field);
6346 /* The visibility of static data members is determined
6347 at their point of declaration, not their point of
6348 definition. */
6349 determine_visibility (field);
6351 continue;
6354 type = TREE_TYPE (field);
6355 if (type == error_mark_node)
6356 continue;
6358 padding = NULL_TREE;
6360 /* If this field is a bit-field whose width is greater than its
6361 type, then there are some special rules for allocating
6362 it. */
6363 if (DECL_C_BIT_FIELD (field)
6364 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6366 unsigned int itk;
6367 tree integer_type;
6368 bool was_unnamed_p = false;
6369 /* We must allocate the bits as if suitably aligned for the
6370 longest integer type that fits in this many bits. type
6371 of the field. Then, we are supposed to use the left over
6372 bits as additional padding. */
6373 for (itk = itk_char; itk != itk_none; ++itk)
6374 if (integer_types[itk] != NULL_TREE
6375 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE),
6376 TYPE_SIZE (integer_types[itk]))
6377 || tree_int_cst_lt (DECL_SIZE (field),
6378 TYPE_SIZE (integer_types[itk]))))
6379 break;
6381 /* ITK now indicates a type that is too large for the
6382 field. We have to back up by one to find the largest
6383 type that fits. */
6386 --itk;
6387 integer_type = integer_types[itk];
6388 } while (itk > 0 && integer_type == NULL_TREE);
6390 /* Figure out how much additional padding is required. */
6391 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field)))
6393 if (TREE_CODE (t) == UNION_TYPE)
6394 /* In a union, the padding field must have the full width
6395 of the bit-field; all fields start at offset zero. */
6396 padding = DECL_SIZE (field);
6397 else
6398 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6399 TYPE_SIZE (integer_type));
6402 /* An unnamed bitfield does not normally affect the
6403 alignment of the containing class on a target where
6404 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6405 make any exceptions for unnamed bitfields when the
6406 bitfields are longer than their types. Therefore, we
6407 temporarily give the field a name. */
6408 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6410 was_unnamed_p = true;
6411 DECL_NAME (field) = make_anon_name ();
6414 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6415 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6416 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6417 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6418 empty_base_offsets);
6419 if (was_unnamed_p)
6420 DECL_NAME (field) = NULL_TREE;
6421 /* Now that layout has been performed, set the size of the
6422 field to the size of its declared type; the rest of the
6423 field is effectively invisible. */
6424 DECL_SIZE (field) = TYPE_SIZE (type);
6425 /* We must also reset the DECL_MODE of the field. */
6426 DECL_MODE (field) = TYPE_MODE (type);
6428 else
6429 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6430 empty_base_offsets);
6432 /* Remember the location of any empty classes in FIELD. */
6433 record_subobject_offsets (TREE_TYPE (field),
6434 byte_position(field),
6435 empty_base_offsets,
6436 /*is_data_member=*/true);
6438 /* If a bit-field does not immediately follow another bit-field,
6439 and yet it starts in the middle of a byte, we have failed to
6440 comply with the ABI. */
6441 if (warn_abi
6442 && DECL_C_BIT_FIELD (field)
6443 /* The TREE_NO_WARNING flag gets set by Objective-C when
6444 laying out an Objective-C class. The ObjC ABI differs
6445 from the C++ ABI, and so we do not want a warning
6446 here. */
6447 && !TREE_NO_WARNING (field)
6448 && !last_field_was_bitfield
6449 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6450 DECL_FIELD_BIT_OFFSET (field),
6451 bitsize_unit_node)))
6452 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6453 "offset of %qD is not ABI-compliant and may "
6454 "change in a future version of GCC", field);
6456 /* The middle end uses the type of expressions to determine the
6457 possible range of expression values. In order to optimize
6458 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6459 must be made aware of the width of "i", via its type.
6461 Because C++ does not have integer types of arbitrary width,
6462 we must (for the purposes of the front end) convert from the
6463 type assigned here to the declared type of the bitfield
6464 whenever a bitfield expression is used as an rvalue.
6465 Similarly, when assigning a value to a bitfield, the value
6466 must be converted to the type given the bitfield here. */
6467 if (DECL_C_BIT_FIELD (field))
6469 unsigned HOST_WIDE_INT width;
6470 tree ftype = TREE_TYPE (field);
6471 width = tree_to_uhwi (DECL_SIZE (field));
6472 if (width != TYPE_PRECISION (ftype))
6474 TREE_TYPE (field)
6475 = c_build_bitfield_integer_type (width,
6476 TYPE_UNSIGNED (ftype));
6477 TREE_TYPE (field)
6478 = cp_build_qualified_type (TREE_TYPE (field),
6479 cp_type_quals (ftype));
6483 /* If we needed additional padding after this field, add it
6484 now. */
6485 if (padding)
6487 tree padding_field;
6489 padding_field = build_decl (input_location,
6490 FIELD_DECL,
6491 NULL_TREE,
6492 char_type_node);
6493 DECL_BIT_FIELD (padding_field) = 1;
6494 DECL_SIZE (padding_field) = padding;
6495 DECL_CONTEXT (padding_field) = t;
6496 DECL_ARTIFICIAL (padding_field) = 1;
6497 DECL_IGNORED_P (padding_field) = 1;
6498 layout_nonempty_base_or_field (rli, padding_field,
6499 NULL_TREE,
6500 empty_base_offsets);
6503 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6506 if (!integer_zerop (rli->bitpos))
6508 /* Make sure that we are on a byte boundary so that the size of
6509 the class without virtual bases will always be a round number
6510 of bytes. */
6511 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6512 normalize_rli (rli);
6515 /* Delete all zero-width bit-fields from the list of fields. Now
6516 that the type is laid out they are no longer important. */
6517 remove_zero_width_bit_fields (t);
6519 /* Create the version of T used for virtual bases. We do not use
6520 make_class_type for this version; this is an artificial type. For
6521 a POD type, we just reuse T. */
6522 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6524 base_t = make_node (TREE_CODE (t));
6526 /* Set the size and alignment for the new type. */
6527 tree eoc;
6529 /* If the ABI version is not at least two, and the last
6530 field was a bit-field, RLI may not be on a byte
6531 boundary. In particular, rli_size_unit_so_far might
6532 indicate the last complete byte, while rli_size_so_far
6533 indicates the total number of bits used. Therefore,
6534 rli_size_so_far, rather than rli_size_unit_so_far, is
6535 used to compute TYPE_SIZE_UNIT. */
6536 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6537 TYPE_SIZE_UNIT (base_t)
6538 = size_binop (MAX_EXPR,
6539 fold_convert (sizetype,
6540 size_binop (CEIL_DIV_EXPR,
6541 rli_size_so_far (rli),
6542 bitsize_int (BITS_PER_UNIT))),
6543 eoc);
6544 TYPE_SIZE (base_t)
6545 = size_binop (MAX_EXPR,
6546 rli_size_so_far (rli),
6547 size_binop (MULT_EXPR,
6548 fold_convert (bitsizetype, eoc),
6549 bitsize_int (BITS_PER_UNIT)));
6550 SET_TYPE_ALIGN (base_t, rli->record_align);
6551 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6553 /* Copy the fields from T. */
6554 next_field = &TYPE_FIELDS (base_t);
6555 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6556 if (TREE_CODE (field) == FIELD_DECL)
6558 *next_field = copy_node (field);
6559 DECL_CONTEXT (*next_field) = base_t;
6560 next_field = &DECL_CHAIN (*next_field);
6562 *next_field = NULL_TREE;
6564 /* Record the base version of the type. */
6565 CLASSTYPE_AS_BASE (t) = base_t;
6566 TYPE_CONTEXT (base_t) = t;
6568 else
6569 CLASSTYPE_AS_BASE (t) = t;
6571 /* Every empty class contains an empty class. */
6572 if (CLASSTYPE_EMPTY_P (t))
6573 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6575 /* Set the TYPE_DECL for this type to contain the right
6576 value for DECL_OFFSET, so that we can use it as part
6577 of a COMPONENT_REF for multiple inheritance. */
6578 layout_decl (TYPE_MAIN_DECL (t), 0);
6580 /* Now fix up any virtual base class types that we left lying
6581 around. We must get these done before we try to lay out the
6582 virtual function table. As a side-effect, this will remove the
6583 base subobject fields. */
6584 layout_virtual_bases (rli, empty_base_offsets);
6586 /* Make sure that empty classes are reflected in RLI at this
6587 point. */
6588 include_empty_classes(rli);
6590 /* Make sure not to create any structures with zero size. */
6591 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6592 place_field (rli,
6593 build_decl (input_location,
6594 FIELD_DECL, NULL_TREE, char_type_node));
6596 /* If this is a non-POD, declaring it packed makes a difference to how it
6597 can be used as a field; don't let finalize_record_size undo it. */
6598 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6599 rli->packed_maybe_necessary = true;
6601 /* Let the back end lay out the type. */
6602 finish_record_layout (rli, /*free_p=*/true);
6604 if (TYPE_SIZE_UNIT (t)
6605 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6606 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6607 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6608 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6610 /* Warn about bases that can't be talked about due to ambiguity. */
6611 warn_about_ambiguous_bases (t);
6613 /* Now that we're done with layout, give the base fields the real types. */
6614 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6615 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6616 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6618 /* Clean up. */
6619 splay_tree_delete (empty_base_offsets);
6621 if (CLASSTYPE_EMPTY_P (t)
6622 && tree_int_cst_lt (sizeof_biggest_empty_class,
6623 TYPE_SIZE_UNIT (t)))
6624 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6627 /* Determine the "key method" for the class type indicated by TYPE,
6628 and set CLASSTYPE_KEY_METHOD accordingly. */
6630 void
6631 determine_key_method (tree type)
6633 tree method;
6635 if (processing_template_decl
6636 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6637 || CLASSTYPE_INTERFACE_KNOWN (type))
6638 return;
6640 /* The key method is the first non-pure virtual function that is not
6641 inline at the point of class definition. On some targets the
6642 key function may not be inline; those targets should not call
6643 this function until the end of the translation unit. */
6644 for (method = TYPE_METHODS (type); method != NULL_TREE;
6645 method = DECL_CHAIN (method))
6646 if (TREE_CODE (method) == FUNCTION_DECL
6647 && DECL_VINDEX (method) != NULL_TREE
6648 && ! DECL_DECLARED_INLINE_P (method)
6649 && ! DECL_PURE_VIRTUAL_P (method))
6651 CLASSTYPE_KEY_METHOD (type) = method;
6652 break;
6655 return;
6659 /* Allocate and return an instance of struct sorted_fields_type with
6660 N fields. */
6662 static struct sorted_fields_type *
6663 sorted_fields_type_new (int n)
6665 struct sorted_fields_type *sft;
6666 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6667 + n * sizeof (tree));
6668 sft->len = n;
6670 return sft;
6673 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6674 class data member of non-zero size, otherwise false. */
6676 static inline bool
6677 field_nonempty_p (const_tree fld)
6679 if (TREE_CODE (fld) == ERROR_MARK)
6680 return false;
6682 tree type = TREE_TYPE (fld);
6683 if (TREE_CODE (fld) == FIELD_DECL
6684 && TREE_CODE (type) != ERROR_MARK
6685 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6687 return TYPE_SIZE (type)
6688 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6689 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6692 return false;
6695 /* Used by find_flexarrays and related. */
6696 struct flexmems_t {
6697 /* The first flexible array member or non-zero array member found
6698 in order of layout. */
6699 tree array;
6700 /* First non-static non-empty data member in the class or its bases. */
6701 tree first;
6702 /* First non-static non-empty data member following either the flexible
6703 array member, if found, or the zero-length array member. */
6704 tree after;
6707 /* Find either the first flexible array member or the first zero-length
6708 array, in that order or preference, among members of class T (but not
6709 its base classes), and set members of FMEM accordingly. */
6711 static void
6712 find_flexarrays (tree t, flexmems_t *fmem)
6714 for (tree fld = TYPE_FIELDS (t), next; fld; fld = next)
6716 /* Find the next non-static data member if it exists. */
6717 for (next = fld;
6718 (next = DECL_CHAIN (next))
6719 && TREE_CODE (next) != FIELD_DECL; );
6721 tree fldtype = TREE_TYPE (fld);
6722 if (TREE_CODE (fld) != TYPE_DECL
6723 && RECORD_OR_UNION_TYPE_P (fldtype)
6724 && TYPE_UNNAMED_P (fldtype))
6726 /* Members of anonymous structs and unions are treated as if
6727 they were members of the containing class. Descend into
6728 the anonymous struct or union and find a flexible array
6729 member or zero-length array among its fields. */
6730 find_flexarrays (fldtype, fmem);
6731 continue;
6734 /* Skip anything that's not a (non-static) data member. */
6735 if (TREE_CODE (fld) != FIELD_DECL)
6736 continue;
6738 /* Skip virtual table pointers. */
6739 if (DECL_ARTIFICIAL (fld))
6740 continue;
6742 if (field_nonempty_p (fld))
6744 /* Remember the first non-static data member. */
6745 if (!fmem->first)
6746 fmem->first = fld;
6748 /* Remember the first non-static data member after the flexible
6749 array member, if one has been found, or the zero-length array
6750 if it has been found. */
6751 if (!fmem->after && fmem->array)
6752 fmem->after = fld;
6755 /* Skip non-arrays. */
6756 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6757 continue;
6759 /* Determine the upper bound of the array if it has one. */
6760 if (TYPE_DOMAIN (fldtype))
6762 if (fmem->array)
6764 /* Make a record of the zero-length array if either one
6765 such field or a flexible array member has been seen to
6766 handle the pathological and unlikely case of multiple
6767 such members. */
6768 if (!fmem->after)
6769 fmem->after = fld;
6771 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6772 /* Remember the first zero-length array unless a flexible array
6773 member has already been seen. */
6774 fmem->array = fld;
6776 else
6778 /* Flexible array members have no upper bound. */
6779 if (fmem->array)
6781 /* Replace the zero-length array if it's been stored and
6782 reset the after pointer. */
6783 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6785 fmem->array = fld;
6786 fmem->after = NULL_TREE;
6789 else
6790 fmem->array = fld;
6795 /* Issue diagnostics for invalid flexible array members or zero-length
6796 arrays that are not the last elements of the containing class or its
6797 base classes or that are its sole members. */
6799 static void
6800 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6802 /* Members of anonymous structs and unions are considered to be members
6803 of the containing struct or union. */
6804 if (TYPE_UNNAMED_P (t) || !fmem->array)
6805 return;
6807 const char *msg = 0;
6809 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6811 if (fmem->after)
6812 msg = G_("zero-size array member %qD not at end of %q#T");
6813 else if (!fmem->first)
6814 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6816 if (msg && pedwarn (DECL_SOURCE_LOCATION (fmem->array),
6817 OPT_Wpedantic, msg, fmem->array, t))
6819 inform (location_of (t), "in the definition of %q#T", t);
6821 else
6823 if (fmem->after)
6824 msg = G_("flexible array member %qD not at end of %q#T");
6825 else if (!fmem->first)
6826 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6828 if (msg)
6830 error_at (DECL_SOURCE_LOCATION (fmem->array), msg,
6831 fmem->array, t);
6833 /* In the unlikely event that the member following the flexible
6834 array member is declared in a different class, point to it.
6835 Otherwise it should be obvious. */
6836 if (fmem->after
6837 && (DECL_CONTEXT (fmem->after) != DECL_CONTEXT (fmem->array)))
6838 inform (DECL_SOURCE_LOCATION (fmem->after),
6839 "next member %q#D declared here",
6840 fmem->after);
6842 inform (location_of (t), "in the definition of %q#T", t);
6848 /* Recursively check to make sure that any flexible array or zero-length
6849 array members of class T or its bases are valid (i.e., not the sole
6850 non-static data member of T and, if one exists, that it is the last
6851 non-static data member of T and its base classes. FMEM is expected
6852 to be initially null and is used internally by recursive calls to
6853 the function. Issue the appropriate diagnostics for the array member
6854 that fails the checks. */
6856 static void
6857 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */)
6859 /* Initialize the result of a search for flexible array and zero-length
6860 array members. Avoid doing any work if the most interesting FMEM data
6861 have already been populated. */
6862 flexmems_t flexmems = flexmems_t ();
6863 if (!fmem)
6864 fmem = &flexmems;
6865 else if (fmem->array && fmem->first && fmem->after)
6866 return;
6868 /* Recursively check the primary base class first. */
6869 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6871 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6872 check_flexarrays (basetype, fmem);
6875 /* Recursively check the base classes. */
6876 int nbases = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
6877 for (int i = 0; i < nbases; ++i)
6879 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6881 /* The primary base class was already checked above. */
6882 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6883 continue;
6885 /* Virtual base classes are at the end. */
6886 if (BINFO_VIRTUAL_P (base_binfo))
6887 continue;
6889 /* Check the base class. */
6890 check_flexarrays (BINFO_TYPE (base_binfo), fmem);
6893 if (fmem == &flexmems)
6895 /* Check virtual base classes only once per derived class.
6896 I.e., this check is not performed recursively for base
6897 classes. */
6898 int i;
6899 tree base_binfo;
6900 vec<tree, va_gc> *vbases;
6901 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6902 vec_safe_iterate (vbases, i, &base_binfo); i++)
6904 /* Check the virtual base class. */
6905 tree basetype = TREE_TYPE (base_binfo);
6907 check_flexarrays (basetype, fmem);
6911 /* Search the members of the current (derived) class. */
6912 find_flexarrays (t, fmem);
6914 if (fmem == &flexmems)
6916 /* Issue diagnostics for invalid flexible and zero-length array members
6917 found in base classes or among the members of the current class. */
6918 diagnose_flexarrays (t, fmem);
6922 /* Perform processing required when the definition of T (a class type)
6923 is complete. Diagnose invalid definitions of flexible array members
6924 and zero-size arrays. */
6926 void
6927 finish_struct_1 (tree t)
6929 tree x;
6930 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6931 tree virtuals = NULL_TREE;
6933 if (COMPLETE_TYPE_P (t))
6935 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6936 error ("redefinition of %q#T", t);
6937 popclass ();
6938 return;
6941 /* If this type was previously laid out as a forward reference,
6942 make sure we lay it out again. */
6943 TYPE_SIZE (t) = NULL_TREE;
6944 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6946 /* Make assumptions about the class; we'll reset the flags if
6947 necessary. */
6948 CLASSTYPE_EMPTY_P (t) = 1;
6949 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6950 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6951 CLASSTYPE_LITERAL_P (t) = true;
6953 /* Do end-of-class semantic processing: checking the validity of the
6954 bases and members and add implicitly generated methods. */
6955 check_bases_and_members (t);
6957 /* Find the key method. */
6958 if (TYPE_CONTAINS_VPTR_P (t))
6960 /* The Itanium C++ ABI permits the key method to be chosen when
6961 the class is defined -- even though the key method so
6962 selected may later turn out to be an inline function. On
6963 some systems (such as ARM Symbian OS) the key method cannot
6964 be determined until the end of the translation unit. On such
6965 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6966 will cause the class to be added to KEYED_CLASSES. Then, in
6967 finish_file we will determine the key method. */
6968 if (targetm.cxx.key_method_may_be_inline ())
6969 determine_key_method (t);
6971 /* If a polymorphic class has no key method, we may emit the vtable
6972 in every translation unit where the class definition appears. If
6973 we're devirtualizing, we can look into the vtable even if we
6974 aren't emitting it. */
6975 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
6976 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
6979 /* Layout the class itself. */
6980 layout_class_type (t, &virtuals);
6981 if (CLASSTYPE_AS_BASE (t) != t)
6982 /* We use the base type for trivial assignments, and hence it
6983 needs a mode. */
6984 compute_record_mode (CLASSTYPE_AS_BASE (t));
6986 /* With the layout complete, check for flexible array members and
6987 zero-length arrays that might overlap other members in the final
6988 layout. */
6989 check_flexarrays (t);
6991 virtuals = modify_all_vtables (t, nreverse (virtuals));
6993 /* If necessary, create the primary vtable for this class. */
6994 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6996 /* We must enter these virtuals into the table. */
6997 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6998 build_primary_vtable (NULL_TREE, t);
6999 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7000 /* Here we know enough to change the type of our virtual
7001 function table, but we will wait until later this function. */
7002 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7004 /* If we're warning about ABI tags, check the types of the new
7005 virtual functions. */
7006 if (warn_abi_tag)
7007 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7008 check_abi_tags (t, TREE_VALUE (v));
7011 if (TYPE_CONTAINS_VPTR_P (t))
7013 int vindex;
7014 tree fn;
7016 if (BINFO_VTABLE (TYPE_BINFO (t)))
7017 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7018 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7019 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7021 /* Add entries for virtual functions introduced by this class. */
7022 BINFO_VIRTUALS (TYPE_BINFO (t))
7023 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7025 /* Set DECL_VINDEX for all functions declared in this class. */
7026 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7028 fn = TREE_CHAIN (fn),
7029 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7030 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7032 tree fndecl = BV_FN (fn);
7034 if (DECL_THUNK_P (fndecl))
7035 /* A thunk. We should never be calling this entry directly
7036 from this vtable -- we'd use the entry for the non
7037 thunk base function. */
7038 DECL_VINDEX (fndecl) = NULL_TREE;
7039 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7040 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7044 finish_struct_bits (t);
7045 set_method_tm_attributes (t);
7046 if (flag_openmp || flag_openmp_simd)
7047 finish_omp_declare_simd_methods (t);
7049 /* Complete the rtl for any static member objects of the type we're
7050 working on. */
7051 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7052 if (VAR_P (x) && TREE_STATIC (x)
7053 && TREE_TYPE (x) != error_mark_node
7054 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7055 DECL_MODE (x) = TYPE_MODE (t);
7057 /* Done with FIELDS...now decide whether to sort these for
7058 faster lookups later.
7060 We use a small number because most searches fail (succeeding
7061 ultimately as the search bores through the inheritance
7062 hierarchy), and we want this failure to occur quickly. */
7064 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
7066 /* Complain if one of the field types requires lower visibility. */
7067 constrain_class_visibility (t);
7069 /* Make the rtl for any new vtables we have created, and unmark
7070 the base types we marked. */
7071 finish_vtbls (t);
7073 /* Build the VTT for T. */
7074 build_vtt (t);
7076 if (warn_nonvdtor
7077 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7078 && !CLASSTYPE_FINAL (t))
7079 warning (OPT_Wnon_virtual_dtor,
7080 "%q#T has virtual functions and accessible"
7081 " non-virtual destructor", t);
7083 complete_vars (t);
7085 if (warn_overloaded_virtual)
7086 warn_hidden (t);
7088 /* Class layout, assignment of virtual table slots, etc., is now
7089 complete. Give the back end a chance to tweak the visibility of
7090 the class or perform any other required target modifications. */
7091 targetm.cxx.adjust_class_at_definition (t);
7093 maybe_suppress_debug_info (t);
7095 if (flag_vtable_verify)
7096 vtv_save_class_info (t);
7098 dump_class_hierarchy (t);
7100 /* Finish debugging output for this type. */
7101 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7103 if (TYPE_TRANSPARENT_AGGR (t))
7105 tree field = first_field (t);
7106 if (field == NULL_TREE || error_operand_p (field))
7108 error ("type transparent %q#T does not have any fields", t);
7109 TYPE_TRANSPARENT_AGGR (t) = 0;
7111 else if (DECL_ARTIFICIAL (field))
7113 if (DECL_FIELD_IS_BASE (field))
7114 error ("type transparent class %qT has base classes", t);
7115 else
7117 gcc_checking_assert (DECL_VIRTUAL_P (field));
7118 error ("type transparent class %qT has virtual functions", t);
7120 TYPE_TRANSPARENT_AGGR (t) = 0;
7122 else if (TYPE_MODE (t) != DECL_MODE (field))
7124 error ("type transparent %q#T cannot be made transparent because "
7125 "the type of the first field has a different ABI from the "
7126 "class overall", t);
7127 TYPE_TRANSPARENT_AGGR (t) = 0;
7132 /* Insert FIELDS into T for the sorted case if the FIELDS count is
7133 equal to THRESHOLD or greater than THRESHOLD. */
7135 static void
7136 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
7138 int n_fields = count_fields (fields);
7139 if (n_fields >= threshold)
7141 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7142 add_fields_to_record_type (fields, field_vec, 0);
7143 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7144 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7148 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
7150 void
7151 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
7153 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
7154 if (sorted_fields)
7156 int i;
7157 int n_fields
7158 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
7159 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7161 for (i = 0; i < sorted_fields->len; ++i)
7162 field_vec->elts[i] = sorted_fields->elts[i];
7164 add_enum_fields_to_record_type (enumtype, field_vec,
7165 sorted_fields->len);
7166 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7167 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7171 /* When T was built up, the member declarations were added in reverse
7172 order. Rearrange them to declaration order. */
7174 void
7175 unreverse_member_declarations (tree t)
7177 tree next;
7178 tree prev;
7179 tree x;
7181 /* The following lists are all in reverse order. Put them in
7182 declaration order now. */
7183 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
7184 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7186 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
7187 reverse order, so we can't just use nreverse. */
7188 prev = NULL_TREE;
7189 for (x = TYPE_FIELDS (t);
7190 x && TREE_CODE (x) != TYPE_DECL;
7191 x = next)
7193 next = DECL_CHAIN (x);
7194 DECL_CHAIN (x) = prev;
7195 prev = x;
7197 if (prev)
7199 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7200 if (prev)
7201 TYPE_FIELDS (t) = prev;
7205 tree
7206 finish_struct (tree t, tree attributes)
7208 location_t saved_loc = input_location;
7210 /* Now that we've got all the field declarations, reverse everything
7211 as necessary. */
7212 unreverse_member_declarations (t);
7214 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7215 fixup_attribute_variants (t);
7217 /* Nadger the current location so that diagnostics point to the start of
7218 the struct, not the end. */
7219 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7221 if (processing_template_decl)
7223 tree x;
7225 finish_struct_methods (t);
7226 TYPE_SIZE (t) = bitsize_zero_node;
7227 TYPE_SIZE_UNIT (t) = size_zero_node;
7229 /* We need to emit an error message if this type was used as a parameter
7230 and it is an abstract type, even if it is a template. We construct
7231 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7232 account and we call complete_vars with this type, which will check
7233 the PARM_DECLS. Note that while the type is being defined,
7234 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7235 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7236 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7237 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
7238 if (DECL_PURE_VIRTUAL_P (x))
7239 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7240 complete_vars (t);
7241 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
7242 an enclosing scope is a template class, so that this function be
7243 found by lookup_fnfields_1 when the using declaration is not
7244 instantiated yet. */
7245 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7246 if (TREE_CODE (x) == USING_DECL)
7248 tree fn = strip_using_decl (x);
7249 if (is_overloaded_fn (fn))
7250 for (; fn; fn = OVL_NEXT (fn))
7251 add_method (t, OVL_CURRENT (fn), x);
7254 /* Remember current #pragma pack value. */
7255 TYPE_PRECISION (t) = maximum_field_alignment;
7257 /* Fix up any variants we've already built. */
7258 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7260 TYPE_SIZE (x) = TYPE_SIZE (t);
7261 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7262 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7263 TYPE_METHODS (x) = TYPE_METHODS (t);
7266 else
7267 finish_struct_1 (t);
7269 if (is_std_init_list (t))
7271 /* People keep complaining that the compiler crashes on an invalid
7272 definition of initializer_list, so I guess we should explicitly
7273 reject it. What the compiler internals care about is that it's a
7274 template and has a pointer field followed by an integer field. */
7275 bool ok = false;
7276 if (processing_template_decl)
7278 tree f = next_initializable_field (TYPE_FIELDS (t));
7279 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7281 f = next_initializable_field (DECL_CHAIN (f));
7282 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7283 ok = true;
7286 if (!ok)
7287 fatal_error (input_location,
7288 "definition of std::initializer_list does not match "
7289 "#include <initializer_list>");
7292 input_location = saved_loc;
7294 TYPE_BEING_DEFINED (t) = 0;
7296 if (current_class_type)
7297 popclass ();
7298 else
7299 error ("trying to finish struct, but kicked out due to previous parse errors");
7301 if (processing_template_decl && at_function_scope_p ()
7302 /* Lambdas are defined by the LAMBDA_EXPR. */
7303 && !LAMBDA_TYPE_P (t))
7304 add_stmt (build_min (TAG_DEFN, t));
7306 return t;
7309 /* Hash table to avoid endless recursion when handling references. */
7310 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7312 /* Return the dynamic type of INSTANCE, if known.
7313 Used to determine whether the virtual function table is needed
7314 or not.
7316 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7317 of our knowledge of its type. *NONNULL should be initialized
7318 before this function is called. */
7320 static tree
7321 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7323 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7325 switch (TREE_CODE (instance))
7327 case INDIRECT_REF:
7328 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7329 return NULL_TREE;
7330 else
7331 return RECUR (TREE_OPERAND (instance, 0));
7333 case CALL_EXPR:
7334 /* This is a call to a constructor, hence it's never zero. */
7335 if (TREE_HAS_CONSTRUCTOR (instance))
7337 if (nonnull)
7338 *nonnull = 1;
7339 return TREE_TYPE (instance);
7341 return NULL_TREE;
7343 case SAVE_EXPR:
7344 /* This is a call to a constructor, hence it's never zero. */
7345 if (TREE_HAS_CONSTRUCTOR (instance))
7347 if (nonnull)
7348 *nonnull = 1;
7349 return TREE_TYPE (instance);
7351 return RECUR (TREE_OPERAND (instance, 0));
7353 case POINTER_PLUS_EXPR:
7354 case PLUS_EXPR:
7355 case MINUS_EXPR:
7356 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7357 return RECUR (TREE_OPERAND (instance, 0));
7358 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7359 /* Propagate nonnull. */
7360 return RECUR (TREE_OPERAND (instance, 0));
7362 return NULL_TREE;
7364 CASE_CONVERT:
7365 return RECUR (TREE_OPERAND (instance, 0));
7367 case ADDR_EXPR:
7368 instance = TREE_OPERAND (instance, 0);
7369 if (nonnull)
7371 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7372 with a real object -- given &p->f, p can still be null. */
7373 tree t = get_base_address (instance);
7374 /* ??? Probably should check DECL_WEAK here. */
7375 if (t && DECL_P (t))
7376 *nonnull = 1;
7378 return RECUR (instance);
7380 case COMPONENT_REF:
7381 /* If this component is really a base class reference, then the field
7382 itself isn't definitive. */
7383 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7384 return RECUR (TREE_OPERAND (instance, 0));
7385 return RECUR (TREE_OPERAND (instance, 1));
7387 case VAR_DECL:
7388 case FIELD_DECL:
7389 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7390 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7392 if (nonnull)
7393 *nonnull = 1;
7394 return TREE_TYPE (TREE_TYPE (instance));
7396 /* fall through. */
7397 case TARGET_EXPR:
7398 case PARM_DECL:
7399 case RESULT_DECL:
7400 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7402 if (nonnull)
7403 *nonnull = 1;
7404 return TREE_TYPE (instance);
7406 else if (instance == current_class_ptr)
7408 if (nonnull)
7409 *nonnull = 1;
7411 /* if we're in a ctor or dtor, we know our type. If
7412 current_class_ptr is set but we aren't in a function, we're in
7413 an NSDMI (and therefore a constructor). */
7414 if (current_scope () != current_function_decl
7415 || (DECL_LANG_SPECIFIC (current_function_decl)
7416 && (DECL_CONSTRUCTOR_P (current_function_decl)
7417 || DECL_DESTRUCTOR_P (current_function_decl))))
7419 if (cdtorp)
7420 *cdtorp = 1;
7421 return TREE_TYPE (TREE_TYPE (instance));
7424 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7426 /* We only need one hash table because it is always left empty. */
7427 if (!fixed_type_or_null_ref_ht)
7428 fixed_type_or_null_ref_ht
7429 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7431 /* Reference variables should be references to objects. */
7432 if (nonnull)
7433 *nonnull = 1;
7435 /* Enter the INSTANCE in a table to prevent recursion; a
7436 variable's initializer may refer to the variable
7437 itself. */
7438 if (VAR_P (instance)
7439 && DECL_INITIAL (instance)
7440 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7441 && !fixed_type_or_null_ref_ht->find (instance))
7443 tree type;
7444 tree_node **slot;
7446 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7447 *slot = instance;
7448 type = RECUR (DECL_INITIAL (instance));
7449 fixed_type_or_null_ref_ht->remove_elt (instance);
7451 return type;
7454 return NULL_TREE;
7456 default:
7457 return NULL_TREE;
7459 #undef RECUR
7462 /* Return nonzero if the dynamic type of INSTANCE is known, and
7463 equivalent to the static type. We also handle the case where
7464 INSTANCE is really a pointer. Return negative if this is a
7465 ctor/dtor. There the dynamic type is known, but this might not be
7466 the most derived base of the original object, and hence virtual
7467 bases may not be laid out according to this type.
7469 Used to determine whether the virtual function table is needed
7470 or not.
7472 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7473 of our knowledge of its type. *NONNULL should be initialized
7474 before this function is called. */
7477 resolves_to_fixed_type_p (tree instance, int* nonnull)
7479 tree t = TREE_TYPE (instance);
7480 int cdtorp = 0;
7481 tree fixed;
7483 /* processing_template_decl can be false in a template if we're in
7484 instantiate_non_dependent_expr, but we still want to suppress
7485 this check. */
7486 if (in_template_function ())
7488 /* In a template we only care about the type of the result. */
7489 if (nonnull)
7490 *nonnull = true;
7491 return true;
7494 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7495 if (fixed == NULL_TREE)
7496 return 0;
7497 if (POINTER_TYPE_P (t))
7498 t = TREE_TYPE (t);
7499 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7500 return 0;
7501 return cdtorp ? -1 : 1;
7505 void
7506 init_class_processing (void)
7508 current_class_depth = 0;
7509 current_class_stack_size = 10;
7510 current_class_stack
7511 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7512 vec_alloc (local_classes, 8);
7513 sizeof_biggest_empty_class = size_zero_node;
7515 ridpointers[(int) RID_PUBLIC] = access_public_node;
7516 ridpointers[(int) RID_PRIVATE] = access_private_node;
7517 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7520 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7522 static void
7523 restore_class_cache (void)
7525 tree type;
7527 /* We are re-entering the same class we just left, so we don't
7528 have to search the whole inheritance matrix to find all the
7529 decls to bind again. Instead, we install the cached
7530 class_shadowed list and walk through it binding names. */
7531 push_binding_level (previous_class_level);
7532 class_binding_level = previous_class_level;
7533 /* Restore IDENTIFIER_TYPE_VALUE. */
7534 for (type = class_binding_level->type_shadowed;
7535 type;
7536 type = TREE_CHAIN (type))
7537 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7540 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7541 appropriate for TYPE.
7543 So that we may avoid calls to lookup_name, we cache the _TYPE
7544 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7546 For multiple inheritance, we perform a two-pass depth-first search
7547 of the type lattice. */
7549 void
7550 pushclass (tree type)
7552 class_stack_node_t csn;
7554 type = TYPE_MAIN_VARIANT (type);
7556 /* Make sure there is enough room for the new entry on the stack. */
7557 if (current_class_depth + 1 >= current_class_stack_size)
7559 current_class_stack_size *= 2;
7560 current_class_stack
7561 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7562 current_class_stack_size);
7565 /* Insert a new entry on the class stack. */
7566 csn = current_class_stack + current_class_depth;
7567 csn->name = current_class_name;
7568 csn->type = current_class_type;
7569 csn->access = current_access_specifier;
7570 csn->names_used = 0;
7571 csn->hidden = 0;
7572 current_class_depth++;
7574 /* Now set up the new type. */
7575 current_class_name = TYPE_NAME (type);
7576 if (TREE_CODE (current_class_name) == TYPE_DECL)
7577 current_class_name = DECL_NAME (current_class_name);
7578 current_class_type = type;
7580 /* By default, things in classes are private, while things in
7581 structures or unions are public. */
7582 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7583 ? access_private_node
7584 : access_public_node);
7586 if (previous_class_level
7587 && type != previous_class_level->this_entity
7588 && current_class_depth == 1)
7590 /* Forcibly remove any old class remnants. */
7591 invalidate_class_lookup_cache ();
7594 if (!previous_class_level
7595 || type != previous_class_level->this_entity
7596 || current_class_depth > 1)
7597 pushlevel_class ();
7598 else
7599 restore_class_cache ();
7602 /* When we exit a toplevel class scope, we save its binding level so
7603 that we can restore it quickly. Here, we've entered some other
7604 class, so we must invalidate our cache. */
7606 void
7607 invalidate_class_lookup_cache (void)
7609 previous_class_level = NULL;
7612 /* Get out of the current class scope. If we were in a class scope
7613 previously, that is the one popped to. */
7615 void
7616 popclass (void)
7618 poplevel_class ();
7620 current_class_depth--;
7621 current_class_name = current_class_stack[current_class_depth].name;
7622 current_class_type = current_class_stack[current_class_depth].type;
7623 current_access_specifier = current_class_stack[current_class_depth].access;
7624 if (current_class_stack[current_class_depth].names_used)
7625 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7628 /* Mark the top of the class stack as hidden. */
7630 void
7631 push_class_stack (void)
7633 if (current_class_depth)
7634 ++current_class_stack[current_class_depth - 1].hidden;
7637 /* Mark the top of the class stack as un-hidden. */
7639 void
7640 pop_class_stack (void)
7642 if (current_class_depth)
7643 --current_class_stack[current_class_depth - 1].hidden;
7646 /* Returns 1 if the class type currently being defined is either T or
7647 a nested type of T. Returns the type from the current_class_stack,
7648 which might be equivalent to but not equal to T in case of
7649 constrained partial specializations. */
7651 tree
7652 currently_open_class (tree t)
7654 int i;
7656 if (!CLASS_TYPE_P (t))
7657 return NULL_TREE;
7659 t = TYPE_MAIN_VARIANT (t);
7661 /* We start looking from 1 because entry 0 is from global scope,
7662 and has no type. */
7663 for (i = current_class_depth; i > 0; --i)
7665 tree c;
7666 if (i == current_class_depth)
7667 c = current_class_type;
7668 else
7670 if (current_class_stack[i].hidden)
7671 break;
7672 c = current_class_stack[i].type;
7674 if (!c)
7675 continue;
7676 if (same_type_p (c, t))
7677 return c;
7679 return NULL_TREE;
7682 /* If either current_class_type or one of its enclosing classes are derived
7683 from T, return the appropriate type. Used to determine how we found
7684 something via unqualified lookup. */
7686 tree
7687 currently_open_derived_class (tree t)
7689 int i;
7691 /* The bases of a dependent type are unknown. */
7692 if (dependent_type_p (t))
7693 return NULL_TREE;
7695 if (!current_class_type)
7696 return NULL_TREE;
7698 if (DERIVED_FROM_P (t, current_class_type))
7699 return current_class_type;
7701 for (i = current_class_depth - 1; i > 0; --i)
7703 if (current_class_stack[i].hidden)
7704 break;
7705 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7706 return current_class_stack[i].type;
7709 return NULL_TREE;
7712 /* Return the outermost enclosing class type that is still open, or
7713 NULL_TREE. */
7715 tree
7716 outermost_open_class (void)
7718 if (!current_class_type)
7719 return NULL_TREE;
7720 tree r = NULL_TREE;
7721 if (TYPE_BEING_DEFINED (current_class_type))
7722 r = current_class_type;
7723 for (int i = current_class_depth - 1; i > 0; --i)
7725 if (current_class_stack[i].hidden)
7726 break;
7727 tree t = current_class_stack[i].type;
7728 if (!TYPE_BEING_DEFINED (t))
7729 break;
7730 r = t;
7732 return r;
7735 /* Returns the innermost class type which is not a lambda closure type. */
7737 tree
7738 current_nonlambda_class_type (void)
7740 int i;
7742 /* We start looking from 1 because entry 0 is from global scope,
7743 and has no type. */
7744 for (i = current_class_depth; i > 0; --i)
7746 tree c;
7747 if (i == current_class_depth)
7748 c = current_class_type;
7749 else
7751 if (current_class_stack[i].hidden)
7752 break;
7753 c = current_class_stack[i].type;
7755 if (!c)
7756 continue;
7757 if (!LAMBDA_TYPE_P (c))
7758 return c;
7760 return NULL_TREE;
7763 /* When entering a class scope, all enclosing class scopes' names with
7764 static meaning (static variables, static functions, types and
7765 enumerators) have to be visible. This recursive function calls
7766 pushclass for all enclosing class contexts until global or a local
7767 scope is reached. TYPE is the enclosed class. */
7769 void
7770 push_nested_class (tree type)
7772 /* A namespace might be passed in error cases, like A::B:C. */
7773 if (type == NULL_TREE
7774 || !CLASS_TYPE_P (type))
7775 return;
7777 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7779 pushclass (type);
7782 /* Undoes a push_nested_class call. */
7784 void
7785 pop_nested_class (void)
7787 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7789 popclass ();
7790 if (context && CLASS_TYPE_P (context))
7791 pop_nested_class ();
7794 /* Returns the number of extern "LANG" blocks we are nested within. */
7797 current_lang_depth (void)
7799 return vec_safe_length (current_lang_base);
7802 /* Set global variables CURRENT_LANG_NAME to appropriate value
7803 so that behavior of name-mangling machinery is correct. */
7805 void
7806 push_lang_context (tree name)
7808 vec_safe_push (current_lang_base, current_lang_name);
7810 if (name == lang_name_cplusplus)
7811 current_lang_name = name;
7812 else if (name == lang_name_c)
7813 current_lang_name = name;
7814 else
7815 error ("language string %<\"%E\"%> not recognized", name);
7818 /* Get out of the current language scope. */
7820 void
7821 pop_lang_context (void)
7823 current_lang_name = current_lang_base->pop ();
7826 /* Type instantiation routines. */
7828 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7829 matches the TARGET_TYPE. If there is no satisfactory match, return
7830 error_mark_node, and issue an error & warning messages under
7831 control of FLAGS. Permit pointers to member function if FLAGS
7832 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7833 a template-id, and EXPLICIT_TARGS are the explicitly provided
7834 template arguments.
7836 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7837 is the base path used to reference those member functions. If
7838 the address is resolved to a member function, access checks will be
7839 performed and errors issued if appropriate. */
7841 static tree
7842 resolve_address_of_overloaded_function (tree target_type,
7843 tree overload,
7844 tsubst_flags_t complain,
7845 bool template_only,
7846 tree explicit_targs,
7847 tree access_path)
7849 /* Here's what the standard says:
7851 [over.over]
7853 If the name is a function template, template argument deduction
7854 is done, and if the argument deduction succeeds, the deduced
7855 arguments are used to generate a single template function, which
7856 is added to the set of overloaded functions considered.
7858 Non-member functions and static member functions match targets of
7859 type "pointer-to-function" or "reference-to-function." Nonstatic
7860 member functions match targets of type "pointer-to-member
7861 function;" the function type of the pointer to member is used to
7862 select the member function from the set of overloaded member
7863 functions. If a nonstatic member function is selected, the
7864 reference to the overloaded function name is required to have the
7865 form of a pointer to member as described in 5.3.1.
7867 If more than one function is selected, any template functions in
7868 the set are eliminated if the set also contains a non-template
7869 function, and any given template function is eliminated if the
7870 set contains a second template function that is more specialized
7871 than the first according to the partial ordering rules 14.5.5.2.
7872 After such eliminations, if any, there shall remain exactly one
7873 selected function. */
7875 int is_ptrmem = 0;
7876 /* We store the matches in a TREE_LIST rooted here. The functions
7877 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7878 interoperability with most_specialized_instantiation. */
7879 tree matches = NULL_TREE;
7880 tree fn;
7881 tree target_fn_type;
7883 /* By the time we get here, we should be seeing only real
7884 pointer-to-member types, not the internal POINTER_TYPE to
7885 METHOD_TYPE representation. */
7886 gcc_assert (!TYPE_PTR_P (target_type)
7887 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7889 gcc_assert (is_overloaded_fn (overload));
7891 /* Check that the TARGET_TYPE is reasonable. */
7892 if (TYPE_PTRFN_P (target_type)
7893 || TYPE_REFFN_P (target_type))
7894 /* This is OK. */;
7895 else if (TYPE_PTRMEMFUNC_P (target_type))
7896 /* This is OK, too. */
7897 is_ptrmem = 1;
7898 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7899 /* This is OK, too. This comes from a conversion to reference
7900 type. */
7901 target_type = build_reference_type (target_type);
7902 else
7904 if (complain & tf_error)
7905 error ("cannot resolve overloaded function %qD based on"
7906 " conversion to type %qT",
7907 DECL_NAME (OVL_FUNCTION (overload)), target_type);
7908 return error_mark_node;
7911 /* Non-member functions and static member functions match targets of type
7912 "pointer-to-function" or "reference-to-function." Nonstatic member
7913 functions match targets of type "pointer-to-member-function;" the
7914 function type of the pointer to member is used to select the member
7915 function from the set of overloaded member functions.
7917 So figure out the FUNCTION_TYPE that we want to match against. */
7918 target_fn_type = static_fn_type (target_type);
7920 /* If we can find a non-template function that matches, we can just
7921 use it. There's no point in generating template instantiations
7922 if we're just going to throw them out anyhow. But, of course, we
7923 can only do this when we don't *need* a template function. */
7924 if (!template_only)
7926 tree fns;
7928 for (fns = overload; fns; fns = OVL_NEXT (fns))
7930 tree fn = OVL_CURRENT (fns);
7932 if (TREE_CODE (fn) == TEMPLATE_DECL)
7933 /* We're not looking for templates just yet. */
7934 continue;
7936 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7937 != is_ptrmem)
7938 /* We're looking for a non-static member, and this isn't
7939 one, or vice versa. */
7940 continue;
7942 /* Ignore functions which haven't been explicitly
7943 declared. */
7944 if (DECL_ANTICIPATED (fn))
7945 continue;
7947 /* See if there's a match. */
7948 tree fntype = static_fn_type (fn);
7949 if (same_type_p (target_fn_type, fntype)
7950 || can_convert_tx_safety (target_fn_type, fntype))
7951 matches = tree_cons (fn, NULL_TREE, matches);
7955 /* Now, if we've already got a match (or matches), there's no need
7956 to proceed to the template functions. But, if we don't have a
7957 match we need to look at them, too. */
7958 if (!matches)
7960 tree target_arg_types;
7961 tree target_ret_type;
7962 tree fns;
7963 tree *args;
7964 unsigned int nargs, ia;
7965 tree arg;
7967 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7968 target_ret_type = TREE_TYPE (target_fn_type);
7970 nargs = list_length (target_arg_types);
7971 args = XALLOCAVEC (tree, nargs);
7972 for (arg = target_arg_types, ia = 0;
7973 arg != NULL_TREE && arg != void_list_node;
7974 arg = TREE_CHAIN (arg), ++ia)
7975 args[ia] = TREE_VALUE (arg);
7976 nargs = ia;
7978 for (fns = overload; fns; fns = OVL_NEXT (fns))
7980 tree fn = OVL_CURRENT (fns);
7981 tree instantiation;
7982 tree targs;
7984 if (TREE_CODE (fn) != TEMPLATE_DECL)
7985 /* We're only looking for templates. */
7986 continue;
7988 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7989 != is_ptrmem)
7990 /* We're not looking for a non-static member, and this is
7991 one, or vice versa. */
7992 continue;
7994 tree ret = target_ret_type;
7996 /* If the template has a deduced return type, don't expose it to
7997 template argument deduction. */
7998 if (undeduced_auto_decl (fn))
7999 ret = NULL_TREE;
8001 /* Try to do argument deduction. */
8002 targs = make_tree_vec (DECL_NTPARMS (fn));
8003 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8004 nargs, ret,
8005 DEDUCE_EXACT, LOOKUP_NORMAL,
8006 false, false);
8007 if (instantiation == error_mark_node)
8008 /* Instantiation failed. */
8009 continue;
8011 /* Constraints must be satisfied. This is done before
8012 return type deduction since that instantiates the
8013 function. */
8014 if (flag_concepts && !constraints_satisfied_p (instantiation))
8015 continue;
8017 /* And now force instantiation to do return type deduction. */
8018 if (undeduced_auto_decl (instantiation))
8020 ++function_depth;
8021 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8022 --function_depth;
8024 require_deduced_type (instantiation);
8027 /* See if there's a match. */
8028 tree fntype = static_fn_type (instantiation);
8029 if (same_type_p (target_fn_type, fntype)
8030 || can_convert_tx_safety (target_fn_type, fntype))
8031 matches = tree_cons (instantiation, fn, matches);
8034 /* Now, remove all but the most specialized of the matches. */
8035 if (matches)
8037 tree match = most_specialized_instantiation (matches);
8039 if (match != error_mark_node)
8040 matches = tree_cons (TREE_PURPOSE (match),
8041 NULL_TREE,
8042 NULL_TREE);
8046 /* Now we should have exactly one function in MATCHES. */
8047 if (matches == NULL_TREE)
8049 /* There were *no* matches. */
8050 if (complain & tf_error)
8052 error ("no matches converting function %qD to type %q#T",
8053 DECL_NAME (OVL_CURRENT (overload)),
8054 target_type);
8056 print_candidates (overload);
8058 return error_mark_node;
8060 else if (TREE_CHAIN (matches))
8062 /* There were too many matches. First check if they're all
8063 the same function. */
8064 tree match = NULL_TREE;
8066 fn = TREE_PURPOSE (matches);
8068 /* For multi-versioned functions, more than one match is just fine and
8069 decls_match will return false as they are different. */
8070 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8071 if (!decls_match (fn, TREE_PURPOSE (match))
8072 && !targetm.target_option.function_versions
8073 (fn, TREE_PURPOSE (match)))
8074 break;
8076 if (match)
8078 if (complain & tf_error)
8080 error ("converting overloaded function %qD to type %q#T is ambiguous",
8081 DECL_NAME (OVL_FUNCTION (overload)),
8082 target_type);
8084 /* Since print_candidates expects the functions in the
8085 TREE_VALUE slot, we flip them here. */
8086 for (match = matches; match; match = TREE_CHAIN (match))
8087 TREE_VALUE (match) = TREE_PURPOSE (match);
8089 print_candidates (matches);
8092 return error_mark_node;
8096 /* Good, exactly one match. Now, convert it to the correct type. */
8097 fn = TREE_PURPOSE (matches);
8099 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8100 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8102 static int explained;
8104 if (!(complain & tf_error))
8105 return error_mark_node;
8107 permerror (input_location, "assuming pointer to member %qD", fn);
8108 if (!explained)
8110 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
8111 explained = 1;
8115 /* If a pointer to a function that is multi-versioned is requested, the
8116 pointer to the dispatcher function is returned instead. This works
8117 well because indirectly calling the function will dispatch the right
8118 function version at run-time. */
8119 if (DECL_FUNCTION_VERSIONED (fn))
8121 fn = get_function_version_dispatcher (fn);
8122 if (fn == NULL)
8123 return error_mark_node;
8124 /* Mark all the versions corresponding to the dispatcher as used. */
8125 if (!(complain & tf_conv))
8126 mark_versions_used (fn);
8129 /* If we're doing overload resolution purely for the purpose of
8130 determining conversion sequences, we should not consider the
8131 function used. If this conversion sequence is selected, the
8132 function will be marked as used at this point. */
8133 if (!(complain & tf_conv))
8135 /* Make =delete work with SFINAE. */
8136 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8137 return error_mark_node;
8138 if (!mark_used (fn, complain) && !(complain & tf_error))
8139 return error_mark_node;
8142 /* We could not check access to member functions when this
8143 expression was originally created since we did not know at that
8144 time to which function the expression referred. */
8145 if (DECL_FUNCTION_MEMBER_P (fn))
8147 gcc_assert (access_path);
8148 perform_or_defer_access_check (access_path, fn, fn, complain);
8151 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8152 return cp_build_addr_expr (fn, complain);
8153 else
8155 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8156 will mark the function as addressed, but here we must do it
8157 explicitly. */
8158 cxx_mark_addressable (fn);
8160 return fn;
8164 /* This function will instantiate the type of the expression given in
8165 RHS to match the type of LHSTYPE. If errors exist, then return
8166 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8167 we complain on errors. If we are not complaining, never modify rhs,
8168 as overload resolution wants to try many possible instantiations, in
8169 the hope that at least one will work.
8171 For non-recursive calls, LHSTYPE should be a function, pointer to
8172 function, or a pointer to member function. */
8174 tree
8175 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8177 tsubst_flags_t complain_in = complain;
8178 tree access_path = NULL_TREE;
8180 complain &= ~tf_ptrmem_ok;
8182 if (lhstype == unknown_type_node)
8184 if (complain & tf_error)
8185 error ("not enough type information");
8186 return error_mark_node;
8189 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8191 tree fntype = non_reference (lhstype);
8192 if (same_type_p (fntype, TREE_TYPE (rhs)))
8193 return rhs;
8194 if (flag_ms_extensions
8195 && TYPE_PTRMEMFUNC_P (fntype)
8196 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8197 /* Microsoft allows `A::f' to be resolved to a
8198 pointer-to-member. */
8200 else
8202 if (complain & tf_error)
8203 error ("cannot convert %qE from type %qT to type %qT",
8204 rhs, TREE_TYPE (rhs), fntype);
8205 return error_mark_node;
8209 if (BASELINK_P (rhs))
8211 access_path = BASELINK_ACCESS_BINFO (rhs);
8212 rhs = BASELINK_FUNCTIONS (rhs);
8215 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8216 deduce any type information. */
8217 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8219 if (complain & tf_error)
8220 error ("not enough type information");
8221 return error_mark_node;
8224 /* If we instantiate a template, and it is a A ?: C expression
8225 with omitted B, look through the SAVE_EXPR. */
8226 if (TREE_CODE (rhs) == SAVE_EXPR)
8227 rhs = TREE_OPERAND (rhs, 0);
8229 /* There are only a few kinds of expressions that may have a type
8230 dependent on overload resolution. */
8231 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8232 || TREE_CODE (rhs) == COMPONENT_REF
8233 || is_overloaded_fn (rhs)
8234 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8236 /* This should really only be used when attempting to distinguish
8237 what sort of a pointer to function we have. For now, any
8238 arithmetic operation which is not supported on pointers
8239 is rejected as an error. */
8241 switch (TREE_CODE (rhs))
8243 case COMPONENT_REF:
8245 tree member = TREE_OPERAND (rhs, 1);
8247 member = instantiate_type (lhstype, member, complain);
8248 if (member != error_mark_node
8249 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8250 /* Do not lose object's side effects. */
8251 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8252 TREE_OPERAND (rhs, 0), member);
8253 return member;
8256 case OFFSET_REF:
8257 rhs = TREE_OPERAND (rhs, 1);
8258 if (BASELINK_P (rhs))
8259 return instantiate_type (lhstype, rhs, complain_in);
8261 /* This can happen if we are forming a pointer-to-member for a
8262 member template. */
8263 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8265 /* Fall through. */
8267 case TEMPLATE_ID_EXPR:
8269 tree fns = TREE_OPERAND (rhs, 0);
8270 tree args = TREE_OPERAND (rhs, 1);
8272 return
8273 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8274 /*template_only=*/true,
8275 args, access_path);
8278 case OVERLOAD:
8279 case FUNCTION_DECL:
8280 return
8281 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8282 /*template_only=*/false,
8283 /*explicit_targs=*/NULL_TREE,
8284 access_path);
8286 case ADDR_EXPR:
8288 if (PTRMEM_OK_P (rhs))
8289 complain |= tf_ptrmem_ok;
8291 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8294 case ERROR_MARK:
8295 return error_mark_node;
8297 default:
8298 gcc_unreachable ();
8300 return error_mark_node;
8303 /* Return the name of the virtual function pointer field
8304 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8305 this may have to look back through base types to find the
8306 ultimate field name. (For single inheritance, these could
8307 all be the same name. Who knows for multiple inheritance). */
8309 static tree
8310 get_vfield_name (tree type)
8312 tree binfo, base_binfo;
8313 char *buf;
8315 for (binfo = TYPE_BINFO (type);
8316 BINFO_N_BASE_BINFOS (binfo);
8317 binfo = base_binfo)
8319 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8321 if (BINFO_VIRTUAL_P (base_binfo)
8322 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8323 break;
8326 type = BINFO_TYPE (binfo);
8327 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8328 + TYPE_NAME_LENGTH (type) + 2);
8329 sprintf (buf, VFIELD_NAME_FORMAT,
8330 IDENTIFIER_POINTER (constructor_name (type)));
8331 return get_identifier (buf);
8334 void
8335 print_class_statistics (void)
8337 if (! GATHER_STATISTICS)
8338 return;
8340 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8341 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8342 if (n_vtables)
8344 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8345 n_vtables, n_vtable_searches);
8346 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8347 n_vtable_entries, n_vtable_elems);
8351 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8352 according to [class]:
8353 The class-name is also inserted
8354 into the scope of the class itself. For purposes of access checking,
8355 the inserted class name is treated as if it were a public member name. */
8357 void
8358 build_self_reference (void)
8360 tree name = constructor_name (current_class_type);
8361 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8362 tree saved_cas;
8364 DECL_NONLOCAL (value) = 1;
8365 DECL_CONTEXT (value) = current_class_type;
8366 DECL_ARTIFICIAL (value) = 1;
8367 SET_DECL_SELF_REFERENCE_P (value);
8368 set_underlying_type (value);
8370 if (processing_template_decl)
8371 value = push_template_decl (value);
8373 saved_cas = current_access_specifier;
8374 current_access_specifier = access_public_node;
8375 finish_member_declaration (value);
8376 current_access_specifier = saved_cas;
8379 /* Returns 1 if TYPE contains only padding bytes. */
8382 is_empty_class (tree type)
8384 if (type == error_mark_node)
8385 return 0;
8387 if (! CLASS_TYPE_P (type))
8388 return 0;
8390 return CLASSTYPE_EMPTY_P (type);
8393 /* Returns true if TYPE contains no actual data, just various
8394 possible combinations of empty classes and possibly a vptr. */
8396 bool
8397 is_really_empty_class (tree type)
8399 if (CLASS_TYPE_P (type))
8401 tree field;
8402 tree binfo;
8403 tree base_binfo;
8404 int i;
8406 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8407 out, but we'd like to be able to check this before then. */
8408 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8409 return true;
8411 for (binfo = TYPE_BINFO (type), i = 0;
8412 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8413 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8414 return false;
8415 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8416 if (TREE_CODE (field) == FIELD_DECL
8417 && !DECL_ARTIFICIAL (field)
8418 /* An unnamed bit-field is not a data member. */
8419 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
8420 && !is_really_empty_class (TREE_TYPE (field)))
8421 return false;
8422 return true;
8424 else if (TREE_CODE (type) == ARRAY_TYPE)
8425 return (integer_zerop (array_type_nelts_top (type))
8426 || is_really_empty_class (TREE_TYPE (type)));
8427 return false;
8430 /* Note that NAME was looked up while the current class was being
8431 defined and that the result of that lookup was DECL. */
8433 void
8434 maybe_note_name_used_in_class (tree name, tree decl)
8436 splay_tree names_used;
8438 /* If we're not defining a class, there's nothing to do. */
8439 if (!(innermost_scope_kind() == sk_class
8440 && TYPE_BEING_DEFINED (current_class_type)
8441 && !LAMBDA_TYPE_P (current_class_type)))
8442 return;
8444 /* If there's already a binding for this NAME, then we don't have
8445 anything to worry about. */
8446 if (lookup_member (current_class_type, name,
8447 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8448 return;
8450 if (!current_class_stack[current_class_depth - 1].names_used)
8451 current_class_stack[current_class_depth - 1].names_used
8452 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8453 names_used = current_class_stack[current_class_depth - 1].names_used;
8455 splay_tree_insert (names_used,
8456 (splay_tree_key) name,
8457 (splay_tree_value) decl);
8460 /* Note that NAME was declared (as DECL) in the current class. Check
8461 to see that the declaration is valid. */
8463 void
8464 note_name_declared_in_class (tree name, tree decl)
8466 splay_tree names_used;
8467 splay_tree_node n;
8469 /* Look to see if we ever used this name. */
8470 names_used
8471 = current_class_stack[current_class_depth - 1].names_used;
8472 if (!names_used)
8473 return;
8474 /* The C language allows members to be declared with a type of the same
8475 name, and the C++ standard says this diagnostic is not required. So
8476 allow it in extern "C" blocks unless predantic is specified.
8477 Allow it in all cases if -ms-extensions is specified. */
8478 if ((!pedantic && current_lang_name == lang_name_c)
8479 || flag_ms_extensions)
8480 return;
8481 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8482 if (n)
8484 /* [basic.scope.class]
8486 A name N used in a class S shall refer to the same declaration
8487 in its context and when re-evaluated in the completed scope of
8488 S. */
8489 permerror (input_location, "declaration of %q#D", decl);
8490 permerror (location_of ((tree) n->value),
8491 "changes meaning of %qD from %q#D",
8492 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
8496 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8497 Secondary vtables are merged with primary vtables; this function
8498 will return the VAR_DECL for the primary vtable. */
8500 tree
8501 get_vtbl_decl_for_binfo (tree binfo)
8503 tree decl;
8505 decl = BINFO_VTABLE (binfo);
8506 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8508 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8509 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8511 if (decl)
8512 gcc_assert (VAR_P (decl));
8513 return decl;
8517 /* Returns the binfo for the primary base of BINFO. If the resulting
8518 BINFO is a virtual base, and it is inherited elsewhere in the
8519 hierarchy, then the returned binfo might not be the primary base of
8520 BINFO in the complete object. Check BINFO_PRIMARY_P or
8521 BINFO_LOST_PRIMARY_P to be sure. */
8523 static tree
8524 get_primary_binfo (tree binfo)
8526 tree primary_base;
8528 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8529 if (!primary_base)
8530 return NULL_TREE;
8532 return copied_binfo (primary_base, binfo);
8535 /* As above, but iterate until we reach the binfo that actually provides the
8536 vptr for BINFO. */
8538 static tree
8539 most_primary_binfo (tree binfo)
8541 tree b = binfo;
8542 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8543 && !BINFO_LOST_PRIMARY_P (b))
8545 tree primary_base = get_primary_binfo (b);
8546 gcc_assert (BINFO_PRIMARY_P (primary_base)
8547 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8548 b = primary_base;
8550 return b;
8553 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8554 type. Note that the virtual inheritance might be above or below BINFO in
8555 the hierarchy. */
8557 bool
8558 vptr_via_virtual_p (tree binfo)
8560 if (TYPE_P (binfo))
8561 binfo = TYPE_BINFO (binfo);
8562 tree primary = most_primary_binfo (binfo);
8563 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8564 a morally virtual base. */
8565 tree virt = binfo_via_virtual (primary, NULL_TREE);
8566 return virt != NULL_TREE;
8569 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8571 static int
8572 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8574 if (!indented_p)
8575 fprintf (stream, "%*s", indent, "");
8576 return 1;
8579 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8580 INDENT should be zero when called from the top level; it is
8581 incremented recursively. IGO indicates the next expected BINFO in
8582 inheritance graph ordering. */
8584 static tree
8585 dump_class_hierarchy_r (FILE *stream,
8586 int flags,
8587 tree binfo,
8588 tree igo,
8589 int indent)
8591 int indented = 0;
8592 tree base_binfo;
8593 int i;
8595 indented = maybe_indent_hierarchy (stream, indent, 0);
8596 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8597 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8598 (HOST_WIDE_INT) (uintptr_t) binfo);
8599 if (binfo != igo)
8601 fprintf (stream, "alternative-path\n");
8602 return igo;
8604 igo = TREE_CHAIN (binfo);
8606 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8607 tree_to_shwi (BINFO_OFFSET (binfo)));
8608 if (is_empty_class (BINFO_TYPE (binfo)))
8609 fprintf (stream, " empty");
8610 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8611 fprintf (stream, " nearly-empty");
8612 if (BINFO_VIRTUAL_P (binfo))
8613 fprintf (stream, " virtual");
8614 fprintf (stream, "\n");
8616 indented = 0;
8617 if (BINFO_PRIMARY_P (binfo))
8619 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8620 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8621 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8622 TFF_PLAIN_IDENTIFIER),
8623 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8625 if (BINFO_LOST_PRIMARY_P (binfo))
8627 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8628 fprintf (stream, " lost-primary");
8630 if (indented)
8631 fprintf (stream, "\n");
8633 if (!(flags & TDF_SLIM))
8635 int indented = 0;
8637 if (BINFO_SUBVTT_INDEX (binfo))
8639 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8640 fprintf (stream, " subvttidx=%s",
8641 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8642 TFF_PLAIN_IDENTIFIER));
8644 if (BINFO_VPTR_INDEX (binfo))
8646 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8647 fprintf (stream, " vptridx=%s",
8648 expr_as_string (BINFO_VPTR_INDEX (binfo),
8649 TFF_PLAIN_IDENTIFIER));
8651 if (BINFO_VPTR_FIELD (binfo))
8653 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8654 fprintf (stream, " vbaseoffset=%s",
8655 expr_as_string (BINFO_VPTR_FIELD (binfo),
8656 TFF_PLAIN_IDENTIFIER));
8658 if (BINFO_VTABLE (binfo))
8660 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8661 fprintf (stream, " vptr=%s",
8662 expr_as_string (BINFO_VTABLE (binfo),
8663 TFF_PLAIN_IDENTIFIER));
8666 if (indented)
8667 fprintf (stream, "\n");
8670 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8671 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8673 return igo;
8676 /* Dump the BINFO hierarchy for T. */
8678 static void
8679 dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
8681 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8682 fprintf (stream, " size=%lu align=%lu\n",
8683 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8684 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8685 fprintf (stream, " base size=%lu base align=%lu\n",
8686 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8687 / BITS_PER_UNIT),
8688 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8689 / BITS_PER_UNIT));
8690 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8691 fprintf (stream, "\n");
8694 /* Debug interface to hierarchy dumping. */
8696 void
8697 debug_class (tree t)
8699 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8702 static void
8703 dump_class_hierarchy (tree t)
8705 int flags;
8706 FILE *stream = get_dump_info (TDI_class, &flags);
8708 if (stream)
8710 dump_class_hierarchy_1 (stream, flags, t);
8714 static void
8715 dump_array (FILE * stream, tree decl)
8717 tree value;
8718 unsigned HOST_WIDE_INT ix;
8719 HOST_WIDE_INT elt;
8720 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8722 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8723 / BITS_PER_UNIT);
8724 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8725 fprintf (stream, " %s entries",
8726 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8727 TFF_PLAIN_IDENTIFIER));
8728 fprintf (stream, "\n");
8730 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8731 ix, value)
8732 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8733 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8736 static void
8737 dump_vtable (tree t, tree binfo, tree vtable)
8739 int flags;
8740 FILE *stream = get_dump_info (TDI_class, &flags);
8742 if (!stream)
8743 return;
8745 if (!(flags & TDF_SLIM))
8747 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8749 fprintf (stream, "%s for %s",
8750 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8751 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8752 if (ctor_vtbl_p)
8754 if (!BINFO_VIRTUAL_P (binfo))
8755 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8756 (HOST_WIDE_INT) (uintptr_t) binfo);
8757 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8759 fprintf (stream, "\n");
8760 dump_array (stream, vtable);
8761 fprintf (stream, "\n");
8765 static void
8766 dump_vtt (tree t, tree vtt)
8768 int flags;
8769 FILE *stream = get_dump_info (TDI_class, &flags);
8771 if (!stream)
8772 return;
8774 if (!(flags & TDF_SLIM))
8776 fprintf (stream, "VTT for %s\n",
8777 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8778 dump_array (stream, vtt);
8779 fprintf (stream, "\n");
8783 /* Dump a function or thunk and its thunkees. */
8785 static void
8786 dump_thunk (FILE *stream, int indent, tree thunk)
8788 static const char spaces[] = " ";
8789 tree name = DECL_NAME (thunk);
8790 tree thunks;
8792 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8793 (void *)thunk,
8794 !DECL_THUNK_P (thunk) ? "function"
8795 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8796 name ? IDENTIFIER_POINTER (name) : "<unset>");
8797 if (DECL_THUNK_P (thunk))
8799 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8800 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8802 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8803 if (!virtual_adjust)
8804 /*NOP*/;
8805 else if (DECL_THIS_THUNK_P (thunk))
8806 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8807 tree_to_shwi (virtual_adjust));
8808 else
8809 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8810 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8811 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8812 if (THUNK_ALIAS (thunk))
8813 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8815 fprintf (stream, "\n");
8816 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8817 dump_thunk (stream, indent + 2, thunks);
8820 /* Dump the thunks for FN. */
8822 void
8823 debug_thunks (tree fn)
8825 dump_thunk (stderr, 0, fn);
8828 /* Virtual function table initialization. */
8830 /* Create all the necessary vtables for T and its base classes. */
8832 static void
8833 finish_vtbls (tree t)
8835 tree vbase;
8836 vec<constructor_elt, va_gc> *v = NULL;
8837 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8839 /* We lay out the primary and secondary vtables in one contiguous
8840 vtable. The primary vtable is first, followed by the non-virtual
8841 secondary vtables in inheritance graph order. */
8842 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8843 vtable, t, &v);
8845 /* Then come the virtual bases, also in inheritance graph order. */
8846 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8848 if (!BINFO_VIRTUAL_P (vbase))
8849 continue;
8850 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8853 if (BINFO_VTABLE (TYPE_BINFO (t)))
8854 initialize_vtable (TYPE_BINFO (t), v);
8857 /* Initialize the vtable for BINFO with the INITS. */
8859 static void
8860 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8862 tree decl;
8864 layout_vtable_decl (binfo, vec_safe_length (inits));
8865 decl = get_vtbl_decl_for_binfo (binfo);
8866 initialize_artificial_var (decl, inits);
8867 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8870 /* Build the VTT (virtual table table) for T.
8871 A class requires a VTT if it has virtual bases.
8873 This holds
8874 1 - primary virtual pointer for complete object T
8875 2 - secondary VTTs for each direct non-virtual base of T which requires a
8877 3 - secondary virtual pointers for each direct or indirect base of T which
8878 has virtual bases or is reachable via a virtual path from T.
8879 4 - secondary VTTs for each direct or indirect virtual base of T.
8881 Secondary VTTs look like complete object VTTs without part 4. */
8883 static void
8884 build_vtt (tree t)
8886 tree type;
8887 tree vtt;
8888 tree index;
8889 vec<constructor_elt, va_gc> *inits;
8891 /* Build up the initializers for the VTT. */
8892 inits = NULL;
8893 index = size_zero_node;
8894 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8896 /* If we didn't need a VTT, we're done. */
8897 if (!inits)
8898 return;
8900 /* Figure out the type of the VTT. */
8901 type = build_array_of_n_type (const_ptr_type_node,
8902 inits->length ());
8904 /* Now, build the VTT object itself. */
8905 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8906 initialize_artificial_var (vtt, inits);
8907 /* Add the VTT to the vtables list. */
8908 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8909 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8911 dump_vtt (t, vtt);
8914 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8915 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8916 and CHAIN the vtable pointer for this binfo after construction is
8917 complete. VALUE can also be another BINFO, in which case we recurse. */
8919 static tree
8920 binfo_ctor_vtable (tree binfo)
8922 tree vt;
8924 while (1)
8926 vt = BINFO_VTABLE (binfo);
8927 if (TREE_CODE (vt) == TREE_LIST)
8928 vt = TREE_VALUE (vt);
8929 if (TREE_CODE (vt) == TREE_BINFO)
8930 binfo = vt;
8931 else
8932 break;
8935 return vt;
8938 /* Data for secondary VTT initialization. */
8939 struct secondary_vptr_vtt_init_data
8941 /* Is this the primary VTT? */
8942 bool top_level_p;
8944 /* Current index into the VTT. */
8945 tree index;
8947 /* Vector of initializers built up. */
8948 vec<constructor_elt, va_gc> *inits;
8950 /* The type being constructed by this secondary VTT. */
8951 tree type_being_constructed;
8954 /* Recursively build the VTT-initializer for BINFO (which is in the
8955 hierarchy dominated by T). INITS points to the end of the initializer
8956 list to date. INDEX is the VTT index where the next element will be
8957 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8958 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8959 for virtual bases of T. When it is not so, we build the constructor
8960 vtables for the BINFO-in-T variant. */
8962 static void
8963 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8964 tree *index)
8966 int i;
8967 tree b;
8968 tree init;
8969 secondary_vptr_vtt_init_data data;
8970 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8972 /* We only need VTTs for subobjects with virtual bases. */
8973 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8974 return;
8976 /* We need to use a construction vtable if this is not the primary
8977 VTT. */
8978 if (!top_level_p)
8980 build_ctor_vtbl_group (binfo, t);
8982 /* Record the offset in the VTT where this sub-VTT can be found. */
8983 BINFO_SUBVTT_INDEX (binfo) = *index;
8986 /* Add the address of the primary vtable for the complete object. */
8987 init = binfo_ctor_vtable (binfo);
8988 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8989 if (top_level_p)
8991 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8992 BINFO_VPTR_INDEX (binfo) = *index;
8994 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8996 /* Recursively add the secondary VTTs for non-virtual bases. */
8997 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8998 if (!BINFO_VIRTUAL_P (b))
8999 build_vtt_inits (b, t, inits, index);
9001 /* Add secondary virtual pointers for all subobjects of BINFO with
9002 either virtual bases or reachable along a virtual path, except
9003 subobjects that are non-virtual primary bases. */
9004 data.top_level_p = top_level_p;
9005 data.index = *index;
9006 data.inits = *inits;
9007 data.type_being_constructed = BINFO_TYPE (binfo);
9009 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9011 *index = data.index;
9013 /* data.inits might have grown as we added secondary virtual pointers.
9014 Make sure our caller knows about the new vector. */
9015 *inits = data.inits;
9017 if (top_level_p)
9018 /* Add the secondary VTTs for virtual bases in inheritance graph
9019 order. */
9020 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9022 if (!BINFO_VIRTUAL_P (b))
9023 continue;
9025 build_vtt_inits (b, t, inits, index);
9027 else
9028 /* Remove the ctor vtables we created. */
9029 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9032 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9033 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9035 static tree
9036 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9038 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9040 /* We don't care about bases that don't have vtables. */
9041 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9042 return dfs_skip_bases;
9044 /* We're only interested in proper subobjects of the type being
9045 constructed. */
9046 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9047 return NULL_TREE;
9049 /* We're only interested in bases with virtual bases or reachable
9050 via a virtual path from the type being constructed. */
9051 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9052 || binfo_via_virtual (binfo, data->type_being_constructed)))
9053 return dfs_skip_bases;
9055 /* We're not interested in non-virtual primary bases. */
9056 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9057 return NULL_TREE;
9059 /* Record the index where this secondary vptr can be found. */
9060 if (data->top_level_p)
9062 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9063 BINFO_VPTR_INDEX (binfo) = data->index;
9065 if (BINFO_VIRTUAL_P (binfo))
9067 /* It's a primary virtual base, and this is not a
9068 construction vtable. Find the base this is primary of in
9069 the inheritance graph, and use that base's vtable
9070 now. */
9071 while (BINFO_PRIMARY_P (binfo))
9072 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9076 /* Add the initializer for the secondary vptr itself. */
9077 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9079 /* Advance the vtt index. */
9080 data->index = size_binop (PLUS_EXPR, data->index,
9081 TYPE_SIZE_UNIT (ptr_type_node));
9083 return NULL_TREE;
9086 /* Called from build_vtt_inits via dfs_walk. After building
9087 constructor vtables and generating the sub-vtt from them, we need
9088 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9089 binfo of the base whose sub vtt was generated. */
9091 static tree
9092 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9094 tree vtable = BINFO_VTABLE (binfo);
9096 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9097 /* If this class has no vtable, none of its bases do. */
9098 return dfs_skip_bases;
9100 if (!vtable)
9101 /* This might be a primary base, so have no vtable in this
9102 hierarchy. */
9103 return NULL_TREE;
9105 /* If we scribbled the construction vtable vptr into BINFO, clear it
9106 out now. */
9107 if (TREE_CODE (vtable) == TREE_LIST
9108 && (TREE_PURPOSE (vtable) == (tree) data))
9109 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9111 return NULL_TREE;
9114 /* Build the construction vtable group for BINFO which is in the
9115 hierarchy dominated by T. */
9117 static void
9118 build_ctor_vtbl_group (tree binfo, tree t)
9120 tree type;
9121 tree vtbl;
9122 tree id;
9123 tree vbase;
9124 vec<constructor_elt, va_gc> *v;
9126 /* See if we've already created this construction vtable group. */
9127 id = mangle_ctor_vtbl_for_type (t, binfo);
9128 if (IDENTIFIER_GLOBAL_VALUE (id))
9129 return;
9131 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9132 /* Build a version of VTBL (with the wrong type) for use in
9133 constructing the addresses of secondary vtables in the
9134 construction vtable group. */
9135 vtbl = build_vtable (t, id, ptr_type_node);
9136 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
9137 /* Don't export construction vtables from shared libraries. Even on
9138 targets that don't support hidden visibility, this tells
9139 can_refer_decl_in_current_unit_p not to assume that it's safe to
9140 access from a different compilation unit (bz 54314). */
9141 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9142 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9144 v = NULL;
9145 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9146 binfo, vtbl, t, &v);
9148 /* Add the vtables for each of our virtual bases using the vbase in T
9149 binfo. */
9150 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9151 vbase;
9152 vbase = TREE_CHAIN (vbase))
9154 tree b;
9156 if (!BINFO_VIRTUAL_P (vbase))
9157 continue;
9158 b = copied_binfo (vbase, binfo);
9160 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9163 /* Figure out the type of the construction vtable. */
9164 type = build_array_of_n_type (vtable_entry_type, v->length ());
9165 layout_type (type);
9166 TREE_TYPE (vtbl) = type;
9167 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9168 layout_decl (vtbl, 0);
9170 /* Initialize the construction vtable. */
9171 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9172 initialize_artificial_var (vtbl, v);
9173 dump_vtable (t, binfo, vtbl);
9176 /* Add the vtbl initializers for BINFO (and its bases other than
9177 non-virtual primaries) to the list of INITS. BINFO is in the
9178 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9179 the constructor the vtbl inits should be accumulated for. (If this
9180 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9181 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9182 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9183 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9184 but are not necessarily the same in terms of layout. */
9186 static void
9187 accumulate_vtbl_inits (tree binfo,
9188 tree orig_binfo,
9189 tree rtti_binfo,
9190 tree vtbl,
9191 tree t,
9192 vec<constructor_elt, va_gc> **inits)
9194 int i;
9195 tree base_binfo;
9196 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9198 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9200 /* If it doesn't have a vptr, we don't do anything. */
9201 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9202 return;
9204 /* If we're building a construction vtable, we're not interested in
9205 subobjects that don't require construction vtables. */
9206 if (ctor_vtbl_p
9207 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9208 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9209 return;
9211 /* Build the initializers for the BINFO-in-T vtable. */
9212 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9214 /* Walk the BINFO and its bases. We walk in preorder so that as we
9215 initialize each vtable we can figure out at what offset the
9216 secondary vtable lies from the primary vtable. We can't use
9217 dfs_walk here because we need to iterate through bases of BINFO
9218 and RTTI_BINFO simultaneously. */
9219 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9221 /* Skip virtual bases. */
9222 if (BINFO_VIRTUAL_P (base_binfo))
9223 continue;
9224 accumulate_vtbl_inits (base_binfo,
9225 BINFO_BASE_BINFO (orig_binfo, i),
9226 rtti_binfo, vtbl, t,
9227 inits);
9231 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9232 BINFO vtable to L. */
9234 static void
9235 dfs_accumulate_vtbl_inits (tree binfo,
9236 tree orig_binfo,
9237 tree rtti_binfo,
9238 tree orig_vtbl,
9239 tree t,
9240 vec<constructor_elt, va_gc> **l)
9242 tree vtbl = NULL_TREE;
9243 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9244 int n_inits;
9246 if (ctor_vtbl_p
9247 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9249 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9250 primary virtual base. If it is not the same primary in
9251 the hierarchy of T, we'll need to generate a ctor vtable
9252 for it, to place at its location in T. If it is the same
9253 primary, we still need a VTT entry for the vtable, but it
9254 should point to the ctor vtable for the base it is a
9255 primary for within the sub-hierarchy of RTTI_BINFO.
9257 There are three possible cases:
9259 1) We are in the same place.
9260 2) We are a primary base within a lost primary virtual base of
9261 RTTI_BINFO.
9262 3) We are primary to something not a base of RTTI_BINFO. */
9264 tree b;
9265 tree last = NULL_TREE;
9267 /* First, look through the bases we are primary to for RTTI_BINFO
9268 or a virtual base. */
9269 b = binfo;
9270 while (BINFO_PRIMARY_P (b))
9272 b = BINFO_INHERITANCE_CHAIN (b);
9273 last = b;
9274 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9275 goto found;
9277 /* If we run out of primary links, keep looking down our
9278 inheritance chain; we might be an indirect primary. */
9279 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9280 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9281 break;
9282 found:
9284 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9285 base B and it is a base of RTTI_BINFO, this is case 2. In
9286 either case, we share our vtable with LAST, i.e. the
9287 derived-most base within B of which we are a primary. */
9288 if (b == rtti_binfo
9289 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9290 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9291 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9292 binfo_ctor_vtable after everything's been set up. */
9293 vtbl = last;
9295 /* Otherwise, this is case 3 and we get our own. */
9297 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9298 return;
9300 n_inits = vec_safe_length (*l);
9302 if (!vtbl)
9304 tree index;
9305 int non_fn_entries;
9307 /* Add the initializer for this vtable. */
9308 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9309 &non_fn_entries, l);
9311 /* Figure out the position to which the VPTR should point. */
9312 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9313 index = size_binop (MULT_EXPR,
9314 TYPE_SIZE_UNIT (vtable_entry_type),
9315 size_int (non_fn_entries + n_inits));
9316 vtbl = fold_build_pointer_plus (vtbl, index);
9319 if (ctor_vtbl_p)
9320 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9321 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9322 straighten this out. */
9323 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9324 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9325 /* Throw away any unneeded intializers. */
9326 (*l)->truncate (n_inits);
9327 else
9328 /* For an ordinary vtable, set BINFO_VTABLE. */
9329 BINFO_VTABLE (binfo) = vtbl;
9332 static GTY(()) tree abort_fndecl_addr;
9334 /* Construct the initializer for BINFO's virtual function table. BINFO
9335 is part of the hierarchy dominated by T. If we're building a
9336 construction vtable, the ORIG_BINFO is the binfo we should use to
9337 find the actual function pointers to put in the vtable - but they
9338 can be overridden on the path to most-derived in the graph that
9339 ORIG_BINFO belongs. Otherwise,
9340 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9341 BINFO that should be indicated by the RTTI information in the
9342 vtable; it will be a base class of T, rather than T itself, if we
9343 are building a construction vtable.
9345 The value returned is a TREE_LIST suitable for wrapping in a
9346 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9347 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9348 number of non-function entries in the vtable.
9350 It might seem that this function should never be called with a
9351 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9352 base is always subsumed by a derived class vtable. However, when
9353 we are building construction vtables, we do build vtables for
9354 primary bases; we need these while the primary base is being
9355 constructed. */
9357 static void
9358 build_vtbl_initializer (tree binfo,
9359 tree orig_binfo,
9360 tree t,
9361 tree rtti_binfo,
9362 int* non_fn_entries_p,
9363 vec<constructor_elt, va_gc> **inits)
9365 tree v;
9366 vtbl_init_data vid;
9367 unsigned ix, jx;
9368 tree vbinfo;
9369 vec<tree, va_gc> *vbases;
9370 constructor_elt *e;
9372 /* Initialize VID. */
9373 memset (&vid, 0, sizeof (vid));
9374 vid.binfo = binfo;
9375 vid.derived = t;
9376 vid.rtti_binfo = rtti_binfo;
9377 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9378 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9379 vid.generate_vcall_entries = true;
9380 /* The first vbase or vcall offset is at index -3 in the vtable. */
9381 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9383 /* Add entries to the vtable for RTTI. */
9384 build_rtti_vtbl_entries (binfo, &vid);
9386 /* Create an array for keeping track of the functions we've
9387 processed. When we see multiple functions with the same
9388 signature, we share the vcall offsets. */
9389 vec_alloc (vid.fns, 32);
9390 /* Add the vcall and vbase offset entries. */
9391 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9393 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9394 build_vbase_offset_vtbl_entries. */
9395 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9396 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9397 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9399 /* If the target requires padding between data entries, add that now. */
9400 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9402 int n_entries = vec_safe_length (vid.inits);
9404 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9406 /* Move data entries into their new positions and add padding
9407 after the new positions. Iterate backwards so we don't
9408 overwrite entries that we would need to process later. */
9409 for (ix = n_entries - 1;
9410 vid.inits->iterate (ix, &e);
9411 ix--)
9413 int j;
9414 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9415 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9417 (*vid.inits)[new_position] = *e;
9419 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9421 constructor_elt *f = &(*vid.inits)[new_position - j];
9422 f->index = NULL_TREE;
9423 f->value = build1 (NOP_EXPR, vtable_entry_type,
9424 null_pointer_node);
9429 if (non_fn_entries_p)
9430 *non_fn_entries_p = vec_safe_length (vid.inits);
9432 /* The initializers for virtual functions were built up in reverse
9433 order. Straighten them out and add them to the running list in one
9434 step. */
9435 jx = vec_safe_length (*inits);
9436 vec_safe_grow (*inits, jx + vid.inits->length ());
9438 for (ix = vid.inits->length () - 1;
9439 vid.inits->iterate (ix, &e);
9440 ix--, jx++)
9441 (**inits)[jx] = *e;
9443 /* Go through all the ordinary virtual functions, building up
9444 initializers. */
9445 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9447 tree delta;
9448 tree vcall_index;
9449 tree fn, fn_original;
9450 tree init = NULL_TREE;
9452 fn = BV_FN (v);
9453 fn_original = fn;
9454 if (DECL_THUNK_P (fn))
9456 if (!DECL_NAME (fn))
9457 finish_thunk (fn);
9458 if (THUNK_ALIAS (fn))
9460 fn = THUNK_ALIAS (fn);
9461 BV_FN (v) = fn;
9463 fn_original = THUNK_TARGET (fn);
9466 /* If the only definition of this function signature along our
9467 primary base chain is from a lost primary, this vtable slot will
9468 never be used, so just zero it out. This is important to avoid
9469 requiring extra thunks which cannot be generated with the function.
9471 We first check this in update_vtable_entry_for_fn, so we handle
9472 restored primary bases properly; we also need to do it here so we
9473 zero out unused slots in ctor vtables, rather than filling them
9474 with erroneous values (though harmless, apart from relocation
9475 costs). */
9476 if (BV_LOST_PRIMARY (v))
9477 init = size_zero_node;
9479 if (! init)
9481 /* Pull the offset for `this', and the function to call, out of
9482 the list. */
9483 delta = BV_DELTA (v);
9484 vcall_index = BV_VCALL_INDEX (v);
9486 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9487 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9489 /* You can't call an abstract virtual function; it's abstract.
9490 So, we replace these functions with __pure_virtual. */
9491 if (DECL_PURE_VIRTUAL_P (fn_original))
9493 fn = abort_fndecl;
9494 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9496 if (abort_fndecl_addr == NULL)
9497 abort_fndecl_addr
9498 = fold_convert (vfunc_ptr_type_node,
9499 build_fold_addr_expr (fn));
9500 init = abort_fndecl_addr;
9503 /* Likewise for deleted virtuals. */
9504 else if (DECL_DELETED_FN (fn_original))
9506 fn = get_identifier ("__cxa_deleted_virtual");
9507 if (!get_global_value_if_present (fn, &fn))
9508 fn = push_library_fn (fn, (build_function_type_list
9509 (void_type_node, NULL_TREE)),
9510 NULL_TREE, ECF_NORETURN);
9511 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9512 init = fold_convert (vfunc_ptr_type_node,
9513 build_fold_addr_expr (fn));
9515 else
9517 if (!integer_zerop (delta) || vcall_index)
9519 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
9520 if (!DECL_NAME (fn))
9521 finish_thunk (fn);
9523 /* Take the address of the function, considering it to be of an
9524 appropriate generic type. */
9525 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9526 init = fold_convert (vfunc_ptr_type_node,
9527 build_fold_addr_expr (fn));
9528 /* Don't refer to a virtual destructor from a constructor
9529 vtable or a vtable for an abstract class, since destroying
9530 an object under construction is undefined behavior and we
9531 don't want it to be considered a candidate for speculative
9532 devirtualization. But do create the thunk for ABI
9533 compliance. */
9534 if (DECL_DESTRUCTOR_P (fn_original)
9535 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9536 || orig_binfo != binfo))
9537 init = size_zero_node;
9541 /* And add it to the chain of initializers. */
9542 if (TARGET_VTABLE_USES_DESCRIPTORS)
9544 int i;
9545 if (init == size_zero_node)
9546 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9547 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9548 else
9549 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9551 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9552 fn, build_int_cst (NULL_TREE, i));
9553 TREE_CONSTANT (fdesc) = 1;
9555 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9558 else
9559 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9563 /* Adds to vid->inits the initializers for the vbase and vcall
9564 offsets in BINFO, which is in the hierarchy dominated by T. */
9566 static void
9567 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9569 tree b;
9571 /* If this is a derived class, we must first create entries
9572 corresponding to the primary base class. */
9573 b = get_primary_binfo (binfo);
9574 if (b)
9575 build_vcall_and_vbase_vtbl_entries (b, vid);
9577 /* Add the vbase entries for this base. */
9578 build_vbase_offset_vtbl_entries (binfo, vid);
9579 /* Add the vcall entries for this base. */
9580 build_vcall_offset_vtbl_entries (binfo, vid);
9583 /* Returns the initializers for the vbase offset entries in the vtable
9584 for BINFO (which is part of the class hierarchy dominated by T), in
9585 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9586 where the next vbase offset will go. */
9588 static void
9589 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9591 tree vbase;
9592 tree t;
9593 tree non_primary_binfo;
9595 /* If there are no virtual baseclasses, then there is nothing to
9596 do. */
9597 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9598 return;
9600 t = vid->derived;
9602 /* We might be a primary base class. Go up the inheritance hierarchy
9603 until we find the most derived class of which we are a primary base:
9604 it is the offset of that which we need to use. */
9605 non_primary_binfo = binfo;
9606 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9608 tree b;
9610 /* If we have reached a virtual base, then it must be a primary
9611 base (possibly multi-level) of vid->binfo, or we wouldn't
9612 have called build_vcall_and_vbase_vtbl_entries for it. But it
9613 might be a lost primary, so just skip down to vid->binfo. */
9614 if (BINFO_VIRTUAL_P (non_primary_binfo))
9616 non_primary_binfo = vid->binfo;
9617 break;
9620 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9621 if (get_primary_binfo (b) != non_primary_binfo)
9622 break;
9623 non_primary_binfo = b;
9626 /* Go through the virtual bases, adding the offsets. */
9627 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9628 vbase;
9629 vbase = TREE_CHAIN (vbase))
9631 tree b;
9632 tree delta;
9634 if (!BINFO_VIRTUAL_P (vbase))
9635 continue;
9637 /* Find the instance of this virtual base in the complete
9638 object. */
9639 b = copied_binfo (vbase, binfo);
9641 /* If we've already got an offset for this virtual base, we
9642 don't need another one. */
9643 if (BINFO_VTABLE_PATH_MARKED (b))
9644 continue;
9645 BINFO_VTABLE_PATH_MARKED (b) = 1;
9647 /* Figure out where we can find this vbase offset. */
9648 delta = size_binop (MULT_EXPR,
9649 vid->index,
9650 fold_convert (ssizetype,
9651 TYPE_SIZE_UNIT (vtable_entry_type)));
9652 if (vid->primary_vtbl_p)
9653 BINFO_VPTR_FIELD (b) = delta;
9655 if (binfo != TYPE_BINFO (t))
9656 /* The vbase offset had better be the same. */
9657 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9659 /* The next vbase will come at a more negative offset. */
9660 vid->index = size_binop (MINUS_EXPR, vid->index,
9661 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9663 /* The initializer is the delta from BINFO to this virtual base.
9664 The vbase offsets go in reverse inheritance-graph order, and
9665 we are walking in inheritance graph order so these end up in
9666 the right order. */
9667 delta = size_diffop_loc (input_location,
9668 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9670 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9671 fold_build1_loc (input_location, NOP_EXPR,
9672 vtable_entry_type, delta));
9676 /* Adds the initializers for the vcall offset entries in the vtable
9677 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9678 to VID->INITS. */
9680 static void
9681 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9683 /* We only need these entries if this base is a virtual base. We
9684 compute the indices -- but do not add to the vtable -- when
9685 building the main vtable for a class. */
9686 if (binfo == TYPE_BINFO (vid->derived)
9687 || (BINFO_VIRTUAL_P (binfo)
9688 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9689 correspond to VID->DERIVED), we are building a primary
9690 construction virtual table. Since this is a primary
9691 virtual table, we do not need the vcall offsets for
9692 BINFO. */
9693 && binfo != vid->rtti_binfo))
9695 /* We need a vcall offset for each of the virtual functions in this
9696 vtable. For example:
9698 class A { virtual void f (); };
9699 class B1 : virtual public A { virtual void f (); };
9700 class B2 : virtual public A { virtual void f (); };
9701 class C: public B1, public B2 { virtual void f (); };
9703 A C object has a primary base of B1, which has a primary base of A. A
9704 C also has a secondary base of B2, which no longer has a primary base
9705 of A. So the B2-in-C construction vtable needs a secondary vtable for
9706 A, which will adjust the A* to a B2* to call f. We have no way of
9707 knowing what (or even whether) this offset will be when we define B2,
9708 so we store this "vcall offset" in the A sub-vtable and look it up in
9709 a "virtual thunk" for B2::f.
9711 We need entries for all the functions in our primary vtable and
9712 in our non-virtual bases' secondary vtables. */
9713 vid->vbase = binfo;
9714 /* If we are just computing the vcall indices -- but do not need
9715 the actual entries -- not that. */
9716 if (!BINFO_VIRTUAL_P (binfo))
9717 vid->generate_vcall_entries = false;
9718 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9719 add_vcall_offset_vtbl_entries_r (binfo, vid);
9723 /* Build vcall offsets, starting with those for BINFO. */
9725 static void
9726 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9728 int i;
9729 tree primary_binfo;
9730 tree base_binfo;
9732 /* Don't walk into virtual bases -- except, of course, for the
9733 virtual base for which we are building vcall offsets. Any
9734 primary virtual base will have already had its offsets generated
9735 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9736 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9737 return;
9739 /* If BINFO has a primary base, process it first. */
9740 primary_binfo = get_primary_binfo (binfo);
9741 if (primary_binfo)
9742 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9744 /* Add BINFO itself to the list. */
9745 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9747 /* Scan the non-primary bases of BINFO. */
9748 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9749 if (base_binfo != primary_binfo)
9750 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9753 /* Called from build_vcall_offset_vtbl_entries_r. */
9755 static void
9756 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9758 /* Make entries for the rest of the virtuals. */
9759 tree orig_fn;
9761 /* The ABI requires that the methods be processed in declaration
9762 order. */
9763 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9764 orig_fn;
9765 orig_fn = DECL_CHAIN (orig_fn))
9766 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9767 add_vcall_offset (orig_fn, binfo, vid);
9770 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9772 static void
9773 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9775 size_t i;
9776 tree vcall_offset;
9777 tree derived_entry;
9779 /* If there is already an entry for a function with the same
9780 signature as FN, then we do not need a second vcall offset.
9781 Check the list of functions already present in the derived
9782 class vtable. */
9783 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9785 if (same_signature_p (derived_entry, orig_fn)
9786 /* We only use one vcall offset for virtual destructors,
9787 even though there are two virtual table entries. */
9788 || (DECL_DESTRUCTOR_P (derived_entry)
9789 && DECL_DESTRUCTOR_P (orig_fn)))
9790 return;
9793 /* If we are building these vcall offsets as part of building
9794 the vtable for the most derived class, remember the vcall
9795 offset. */
9796 if (vid->binfo == TYPE_BINFO (vid->derived))
9798 tree_pair_s elt = {orig_fn, vid->index};
9799 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9802 /* The next vcall offset will be found at a more negative
9803 offset. */
9804 vid->index = size_binop (MINUS_EXPR, vid->index,
9805 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9807 /* Keep track of this function. */
9808 vec_safe_push (vid->fns, orig_fn);
9810 if (vid->generate_vcall_entries)
9812 tree base;
9813 tree fn;
9815 /* Find the overriding function. */
9816 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9817 if (fn == error_mark_node)
9818 vcall_offset = build_zero_cst (vtable_entry_type);
9819 else
9821 base = TREE_VALUE (fn);
9823 /* The vbase we're working on is a primary base of
9824 vid->binfo. But it might be a lost primary, so its
9825 BINFO_OFFSET might be wrong, so we just use the
9826 BINFO_OFFSET from vid->binfo. */
9827 vcall_offset = size_diffop_loc (input_location,
9828 BINFO_OFFSET (base),
9829 BINFO_OFFSET (vid->binfo));
9830 vcall_offset = fold_build1_loc (input_location,
9831 NOP_EXPR, vtable_entry_type,
9832 vcall_offset);
9834 /* Add the initializer to the vtable. */
9835 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9839 /* Return vtbl initializers for the RTTI entries corresponding to the
9840 BINFO's vtable. The RTTI entries should indicate the object given
9841 by VID->rtti_binfo. */
9843 static void
9844 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9846 tree b;
9847 tree t;
9848 tree offset;
9849 tree decl;
9850 tree init;
9852 t = BINFO_TYPE (vid->rtti_binfo);
9854 /* To find the complete object, we will first convert to our most
9855 primary base, and then add the offset in the vtbl to that value. */
9856 b = most_primary_binfo (binfo);
9857 offset = size_diffop_loc (input_location,
9858 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9860 /* The second entry is the address of the typeinfo object. */
9861 if (flag_rtti)
9862 decl = build_address (get_tinfo_decl (t));
9863 else
9864 decl = integer_zero_node;
9866 /* Convert the declaration to a type that can be stored in the
9867 vtable. */
9868 init = build_nop (vfunc_ptr_type_node, decl);
9869 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9871 /* Add the offset-to-top entry. It comes earlier in the vtable than
9872 the typeinfo entry. Convert the offset to look like a
9873 function pointer, so that we can put it in the vtable. */
9874 init = build_nop (vfunc_ptr_type_node, offset);
9875 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9878 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9879 accessibility. */
9881 bool
9882 uniquely_derived_from_p (tree parent, tree type)
9884 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9885 return base && base != error_mark_node;
9888 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9890 bool
9891 publicly_uniquely_derived_p (tree parent, tree type)
9893 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9894 NULL, tf_none);
9895 return base && base != error_mark_node;
9898 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9899 class between them, if any. */
9901 tree
9902 common_enclosing_class (tree ctx1, tree ctx2)
9904 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9905 return NULL_TREE;
9906 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9907 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9908 if (ctx1 == ctx2)
9909 return ctx1;
9910 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9911 TYPE_MARKED_P (t) = true;
9912 tree found = NULL_TREE;
9913 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9914 if (TYPE_MARKED_P (t))
9916 found = t;
9917 break;
9919 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9920 TYPE_MARKED_P (t) = false;
9921 return found;
9924 #include "gt-cp-class.h"