PR c++/80384 - ICE with dependent noexcept-specifier
[official-gcc.git] / gcc / cp / class.c
blob66f42627715f891cc6b5d4ff2a5930e292a5dbbc
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2017 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 /* Id for dumping the class hierarchy. */
41 int class_dump_id;
43 /* The number of nested classes being processed. If we are not in the
44 scope of any class, this is zero. */
46 int current_class_depth;
48 /* In order to deal with nested classes, we keep a stack of classes.
49 The topmost entry is the innermost class, and is the entry at index
50 CURRENT_CLASS_DEPTH */
52 typedef struct class_stack_node {
53 /* The name of the class. */
54 tree name;
56 /* The _TYPE node for the class. */
57 tree type;
59 /* The access specifier pending for new declarations in the scope of
60 this class. */
61 tree access;
63 /* If were defining TYPE, the names used in this class. */
64 splay_tree names_used;
66 /* Nonzero if this class is no longer open, because of a call to
67 push_to_top_level. */
68 size_t hidden;
69 }* class_stack_node_t;
71 struct vtbl_init_data
73 /* The base for which we're building initializers. */
74 tree binfo;
75 /* The type of the most-derived type. */
76 tree derived;
77 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
78 unless ctor_vtbl_p is true. */
79 tree rtti_binfo;
80 /* The negative-index vtable initializers built up so far. These
81 are in order from least negative index to most negative index. */
82 vec<constructor_elt, va_gc> *inits;
83 /* The binfo for the virtual base for which we're building
84 vcall offset initializers. */
85 tree vbase;
86 /* The functions in vbase for which we have already provided vcall
87 offsets. */
88 vec<tree, va_gc> *fns;
89 /* The vtable index of the next vcall or vbase offset. */
90 tree index;
91 /* Nonzero if we are building the initializer for the primary
92 vtable. */
93 int primary_vtbl_p;
94 /* Nonzero if we are building the initializer for a construction
95 vtable. */
96 int ctor_vtbl_p;
97 /* True when adding vcall offset entries to the vtable. False when
98 merely computing the indices. */
99 bool generate_vcall_entries;
102 /* The type of a function passed to walk_subobject_offsets. */
103 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
105 /* The stack itself. This is a dynamically resized array. The
106 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
107 static int current_class_stack_size;
108 static class_stack_node_t current_class_stack;
110 /* The size of the largest empty class seen in this translation unit. */
111 static GTY (()) tree sizeof_biggest_empty_class;
113 /* An array of all local classes present in this translation unit, in
114 declaration order. */
115 vec<tree, va_gc> *local_classes;
117 static tree get_vfield_name (tree);
118 static void finish_struct_anon (tree);
119 static tree get_vtable_name (tree);
120 static void get_basefndecls (tree, tree, vec<tree> *);
121 static int build_primary_vtable (tree, tree);
122 static int build_secondary_vtable (tree);
123 static void finish_vtbls (tree);
124 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
125 static void finish_struct_bits (tree);
126 static int alter_access (tree, tree, tree);
127 static void handle_using_decl (tree, tree);
128 static tree dfs_modify_vtables (tree, void *);
129 static tree modify_all_vtables (tree, tree);
130 static void determine_primary_bases (tree);
131 static void finish_struct_methods (tree);
132 static void maybe_warn_about_overly_private_class (tree);
133 static int method_name_cmp (const void *, const void *);
134 static int resort_method_name_cmp (const void *, const void *);
135 static void add_implicitly_declared_members (tree, tree*, int, int);
136 static tree fixed_type_or_null (tree, int *, int *);
137 static tree build_simple_base_path (tree expr, tree binfo);
138 static tree build_vtbl_ref_1 (tree, tree);
139 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
140 vec<constructor_elt, va_gc> **);
141 static int count_fields (tree);
142 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
143 static void insert_into_classtype_sorted_fields (tree, tree, int);
144 static bool check_bitfield_decl (tree);
145 static bool check_field_decl (tree, tree, int *, int *);
146 static void check_field_decls (tree, tree *, int *, int *);
147 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
148 static void build_base_fields (record_layout_info, splay_tree, tree *);
149 static void check_methods (tree);
150 static void remove_zero_width_bit_fields (tree);
151 static bool accessible_nvdtor_p (tree);
153 /* Used by find_flexarrays and related functions. */
154 struct flexmems_t;
155 static void diagnose_flexarrays (tree, const flexmems_t *);
156 static void find_flexarrays (tree, flexmems_t *, bool = false,
157 tree = NULL_TREE, tree = NULL_TREE);
158 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
159 static void check_bases (tree, int *, int *);
160 static void check_bases_and_members (tree);
161 static tree create_vtable_ptr (tree, tree *);
162 static void include_empty_classes (record_layout_info);
163 static void layout_class_type (tree, tree *);
164 static void propagate_binfo_offsets (tree, tree);
165 static void layout_virtual_bases (record_layout_info, splay_tree);
166 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
167 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
168 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
169 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
170 static void add_vcall_offset (tree, tree, vtbl_init_data *);
171 static void layout_vtable_decl (tree, int);
172 static tree dfs_find_final_overrider_pre (tree, void *);
173 static tree dfs_find_final_overrider_post (tree, void *);
174 static tree find_final_overrider (tree, tree, tree);
175 static int make_new_vtable (tree, tree);
176 static tree get_primary_binfo (tree);
177 static int maybe_indent_hierarchy (FILE *, int, int);
178 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
179 static void dump_class_hierarchy (tree);
180 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
181 static void dump_array (FILE *, tree);
182 static void dump_vtable (tree, tree, tree);
183 static void dump_vtt (tree, tree);
184 static void dump_thunk (FILE *, int, tree);
185 static tree build_vtable (tree, tree, tree);
186 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
187 static void layout_nonempty_base_or_field (record_layout_info,
188 tree, tree, splay_tree);
189 static tree end_of_class (tree, int);
190 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
191 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
192 vec<constructor_elt, va_gc> **);
193 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
194 vec<constructor_elt, va_gc> **);
195 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
196 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
197 static void clone_constructors_and_destructors (tree);
198 static tree build_clone (tree, tree);
199 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
200 static void build_ctor_vtbl_group (tree, tree);
201 static void build_vtt (tree);
202 static tree binfo_ctor_vtable (tree);
203 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
204 tree *);
205 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
206 static tree dfs_fixup_binfo_vtbls (tree, void *);
207 static int record_subobject_offset (tree, tree, splay_tree);
208 static int check_subobject_offset (tree, tree, splay_tree);
209 static int walk_subobject_offsets (tree, subobject_offset_fn,
210 tree, splay_tree, tree, int);
211 static void record_subobject_offsets (tree, tree, splay_tree, bool);
212 static int layout_conflict_p (tree, tree, splay_tree, int);
213 static int splay_tree_compare_integer_csts (splay_tree_key k1,
214 splay_tree_key k2);
215 static void warn_about_ambiguous_bases (tree);
216 static bool type_requires_array_cookie (tree);
217 static bool base_derived_from (tree, tree);
218 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
219 static tree end_of_base (tree);
220 static tree get_vcall_index (tree, tree);
221 static bool type_maybe_constexpr_default_constructor (tree);
223 /* Variables shared between class.c and call.c. */
225 int n_vtables = 0;
226 int n_vtable_entries = 0;
227 int n_vtable_searches = 0;
228 int n_vtable_elems = 0;
229 int n_convert_harshness = 0;
230 int n_compute_conversion_costs = 0;
231 int n_inner_fields_searched = 0;
233 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
234 'structor is in charge of 'structing virtual bases, or FALSE_STMT
235 otherwise. */
237 tree
238 build_if_in_charge (tree true_stmt, tree false_stmt)
240 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
241 tree cmp = build2 (NE_EXPR, boolean_type_node,
242 current_in_charge_parm, integer_zero_node);
243 tree type = unlowered_expr_type (true_stmt);
244 if (VOID_TYPE_P (type))
245 type = unlowered_expr_type (false_stmt);
246 tree cond = build3 (COND_EXPR, type,
247 cmp, true_stmt, false_stmt);
248 return cond;
251 /* Convert to or from a base subobject. EXPR is an expression of type
252 `A' or `A*', an expression of type `B' or `B*' is returned. To
253 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
254 the B base instance within A. To convert base A to derived B, CODE
255 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
256 In this latter case, A must not be a morally virtual base of B.
257 NONNULL is true if EXPR is known to be non-NULL (this is only
258 needed when EXPR is of pointer type). CV qualifiers are preserved
259 from EXPR. */
261 tree
262 build_base_path (enum tree_code code,
263 tree expr,
264 tree binfo,
265 int nonnull,
266 tsubst_flags_t complain)
268 tree v_binfo = NULL_TREE;
269 tree d_binfo = NULL_TREE;
270 tree probe;
271 tree offset;
272 tree target_type;
273 tree null_test = NULL;
274 tree ptr_target_type;
275 int fixed_type_p;
276 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
277 bool has_empty = false;
278 bool virtual_access;
279 bool rvalue = false;
281 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
282 return error_mark_node;
284 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
286 d_binfo = probe;
287 if (is_empty_class (BINFO_TYPE (probe)))
288 has_empty = true;
289 if (!v_binfo && BINFO_VIRTUAL_P (probe))
290 v_binfo = probe;
293 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
294 if (want_pointer)
295 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
297 if (code == PLUS_EXPR
298 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
300 /* This can happen when adjust_result_of_qualified_name_lookup can't
301 find a unique base binfo in a call to a member function. We
302 couldn't give the diagnostic then since we might have been calling
303 a static member function, so we do it now. In other cases, eg.
304 during error recovery (c++/71979), we may not have a base at all. */
305 if (complain & tf_error)
307 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
308 ba_unique, NULL, complain);
309 gcc_assert (base == error_mark_node || !base);
311 return error_mark_node;
314 gcc_assert ((code == MINUS_EXPR
315 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
316 || code == PLUS_EXPR);
318 if (binfo == d_binfo)
319 /* Nothing to do. */
320 return expr;
322 if (code == MINUS_EXPR && v_binfo)
324 if (complain & tf_error)
326 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
328 if (want_pointer)
329 error ("cannot convert from pointer to base class %qT to "
330 "pointer to derived class %qT because the base is "
331 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
332 else
333 error ("cannot convert from base class %qT to derived "
334 "class %qT because the base is virtual",
335 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
337 else
339 if (want_pointer)
340 error ("cannot convert from pointer to base class %qT to "
341 "pointer to derived class %qT via virtual base %qT",
342 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
343 BINFO_TYPE (v_binfo));
344 else
345 error ("cannot convert from base class %qT to derived "
346 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
347 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
350 return error_mark_node;
353 if (!want_pointer)
355 rvalue = !lvalue_p (expr);
356 /* This must happen before the call to save_expr. */
357 expr = cp_build_addr_expr (expr, complain);
359 else
360 expr = mark_rvalue_use (expr);
362 offset = BINFO_OFFSET (binfo);
363 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
364 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
365 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
366 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
367 expression returned matches the input. */
368 target_type = cp_build_qualified_type
369 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
370 ptr_target_type = build_pointer_type (target_type);
372 /* Do we need to look in the vtable for the real offset? */
373 virtual_access = (v_binfo && fixed_type_p <= 0);
375 /* Don't bother with the calculations inside sizeof; they'll ICE if the
376 source type is incomplete and the pointer value doesn't matter. In a
377 template (even in instantiate_non_dependent_expr), we don't have vtables
378 set up properly yet, and the value doesn't matter there either; we're
379 just interested in the result of overload resolution. */
380 if (cp_unevaluated_operand != 0
381 || processing_template_decl
382 || in_template_function ())
384 expr = build_nop (ptr_target_type, expr);
385 goto indout;
388 /* If we're in an NSDMI, we don't have the full constructor context yet
389 that we need for converting to a virtual base, so just build a stub
390 CONVERT_EXPR and expand it later in bot_replace. */
391 if (virtual_access && fixed_type_p < 0
392 && current_scope () != current_function_decl)
394 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
395 CONVERT_EXPR_VBASE_PATH (expr) = true;
396 goto indout;
399 /* Do we need to check for a null pointer? */
400 if (want_pointer && !nonnull)
402 /* If we know the conversion will not actually change the value
403 of EXPR, then we can avoid testing the expression for NULL.
404 We have to avoid generating a COMPONENT_REF for a base class
405 field, because other parts of the compiler know that such
406 expressions are always non-NULL. */
407 if (!virtual_access && integer_zerop (offset))
408 return build_nop (ptr_target_type, expr);
409 null_test = error_mark_node;
412 /* Protect against multiple evaluation if necessary. */
413 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
414 expr = save_expr (expr);
416 /* Now that we've saved expr, build the real null test. */
417 if (null_test)
419 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
420 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
421 expr, zero);
422 /* This is a compiler generated comparison, don't emit
423 e.g. -Wnonnull-compare warning for it. */
424 TREE_NO_WARNING (null_test) = 1;
427 /* If this is a simple base reference, express it as a COMPONENT_REF. */
428 if (code == PLUS_EXPR && !virtual_access
429 /* We don't build base fields for empty bases, and they aren't very
430 interesting to the optimizers anyway. */
431 && !has_empty)
433 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
434 expr = build_simple_base_path (expr, binfo);
435 if (rvalue)
436 expr = move (expr);
437 if (want_pointer)
438 expr = build_address (expr);
439 target_type = TREE_TYPE (expr);
440 goto out;
443 if (virtual_access)
445 /* Going via virtual base V_BINFO. We need the static offset
446 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
447 V_BINFO. That offset is an entry in D_BINFO's vtable. */
448 tree v_offset;
450 if (fixed_type_p < 0 && in_base_initializer)
452 /* In a base member initializer, we cannot rely on the
453 vtable being set up. We have to indirect via the
454 vtt_parm. */
455 tree t;
457 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
458 t = build_pointer_type (t);
459 v_offset = fold_convert (t, current_vtt_parm);
460 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
462 else
464 tree t = expr;
465 if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0)
467 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
468 probe, expr);
469 if (t == NULL_TREE)
470 t = expr;
472 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
473 complain),
474 TREE_TYPE (TREE_TYPE (expr)));
477 if (v_offset == error_mark_node)
478 return error_mark_node;
480 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
481 v_offset = build1 (NOP_EXPR,
482 build_pointer_type (ptrdiff_type_node),
483 v_offset);
484 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
485 TREE_CONSTANT (v_offset) = 1;
487 offset = convert_to_integer (ptrdiff_type_node,
488 size_diffop_loc (input_location, offset,
489 BINFO_OFFSET (v_binfo)));
491 if (!integer_zerop (offset))
492 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
494 if (fixed_type_p < 0)
495 /* Negative fixed_type_p means this is a constructor or destructor;
496 virtual base layout is fixed in in-charge [cd]tors, but not in
497 base [cd]tors. */
498 offset = build_if_in_charge
499 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
500 v_offset);
501 else
502 offset = v_offset;
505 if (want_pointer)
506 target_type = ptr_target_type;
508 expr = build1 (NOP_EXPR, ptr_target_type, expr);
510 if (!integer_zerop (offset))
512 offset = fold_convert (sizetype, offset);
513 if (code == MINUS_EXPR)
514 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
515 expr = fold_build_pointer_plus (expr, offset);
517 else
518 null_test = NULL;
520 indout:
521 if (!want_pointer)
523 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
524 if (rvalue)
525 expr = move (expr);
528 out:
529 if (null_test)
530 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
531 build_zero_cst (target_type));
533 return expr;
536 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
537 Perform a derived-to-base conversion by recursively building up a
538 sequence of COMPONENT_REFs to the appropriate base fields. */
540 static tree
541 build_simple_base_path (tree expr, tree binfo)
543 tree type = BINFO_TYPE (binfo);
544 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
545 tree field;
547 if (d_binfo == NULL_TREE)
549 tree temp;
551 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
553 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
554 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
555 an lvalue in the front end; only _DECLs and _REFs are lvalues
556 in the back end. */
557 temp = unary_complex_lvalue (ADDR_EXPR, expr);
558 if (temp)
559 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
561 return expr;
564 /* Recurse. */
565 expr = build_simple_base_path (expr, d_binfo);
567 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
568 field; field = DECL_CHAIN (field))
569 /* Is this the base field created by build_base_field? */
570 if (TREE_CODE (field) == FIELD_DECL
571 && DECL_FIELD_IS_BASE (field)
572 && TREE_TYPE (field) == type
573 /* If we're looking for a field in the most-derived class,
574 also check the field offset; we can have two base fields
575 of the same type if one is an indirect virtual base and one
576 is a direct non-virtual base. */
577 && (BINFO_INHERITANCE_CHAIN (d_binfo)
578 || tree_int_cst_equal (byte_position (field),
579 BINFO_OFFSET (binfo))))
581 /* We don't use build_class_member_access_expr here, as that
582 has unnecessary checks, and more importantly results in
583 recursive calls to dfs_walk_once. */
584 int type_quals = cp_type_quals (TREE_TYPE (expr));
586 expr = build3 (COMPONENT_REF,
587 cp_build_qualified_type (type, type_quals),
588 expr, field, NULL_TREE);
589 /* Mark the expression const or volatile, as appropriate.
590 Even though we've dealt with the type above, we still have
591 to mark the expression itself. */
592 if (type_quals & TYPE_QUAL_CONST)
593 TREE_READONLY (expr) = 1;
594 if (type_quals & TYPE_QUAL_VOLATILE)
595 TREE_THIS_VOLATILE (expr) = 1;
597 return expr;
600 /* Didn't find the base field?!? */
601 gcc_unreachable ();
604 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
605 type is a class type or a pointer to a class type. In the former
606 case, TYPE is also a class type; in the latter it is another
607 pointer type. If CHECK_ACCESS is true, an error message is emitted
608 if TYPE is inaccessible. If OBJECT has pointer type, the value is
609 assumed to be non-NULL. */
611 tree
612 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
613 tsubst_flags_t complain)
615 tree binfo;
616 tree object_type;
618 if (TYPE_PTR_P (TREE_TYPE (object)))
620 object_type = TREE_TYPE (TREE_TYPE (object));
621 type = TREE_TYPE (type);
623 else
624 object_type = TREE_TYPE (object);
626 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
627 NULL, complain);
628 if (!binfo || binfo == error_mark_node)
629 return error_mark_node;
631 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
634 /* EXPR is an expression with unqualified class type. BASE is a base
635 binfo of that class type. Returns EXPR, converted to the BASE
636 type. This function assumes that EXPR is the most derived class;
637 therefore virtual bases can be found at their static offsets. */
639 tree
640 convert_to_base_statically (tree expr, tree base)
642 tree expr_type;
644 expr_type = TREE_TYPE (expr);
645 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
647 /* If this is a non-empty base, use a COMPONENT_REF. */
648 if (!is_empty_class (BINFO_TYPE (base)))
649 return build_simple_base_path (expr, base);
651 /* We use fold_build2 and fold_convert below to simplify the trees
652 provided to the optimizers. It is not safe to call these functions
653 when processing a template because they do not handle C++-specific
654 trees. */
655 gcc_assert (!processing_template_decl);
656 expr = cp_build_addr_expr (expr, tf_warning_or_error);
657 if (!integer_zerop (BINFO_OFFSET (base)))
658 expr = fold_build_pointer_plus_loc (input_location,
659 expr, BINFO_OFFSET (base));
660 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
661 expr = build_fold_indirect_ref_loc (input_location, expr);
664 return expr;
668 tree
669 build_vfield_ref (tree datum, tree type)
671 tree vfield, vcontext;
673 if (datum == error_mark_node
674 /* Can happen in case of duplicate base types (c++/59082). */
675 || !TYPE_VFIELD (type))
676 return error_mark_node;
678 /* First, convert to the requested type. */
679 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
680 datum = convert_to_base (datum, type, /*check_access=*/false,
681 /*nonnull=*/true, tf_warning_or_error);
683 /* Second, the requested type may not be the owner of its own vptr.
684 If not, convert to the base class that owns it. We cannot use
685 convert_to_base here, because VCONTEXT may appear more than once
686 in the inheritance hierarchy of TYPE, and thus direct conversion
687 between the types may be ambiguous. Following the path back up
688 one step at a time via primary bases avoids the problem. */
689 vfield = TYPE_VFIELD (type);
690 vcontext = DECL_CONTEXT (vfield);
691 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
693 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
694 type = TREE_TYPE (datum);
697 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
700 /* Given an object INSTANCE, return an expression which yields the
701 vtable element corresponding to INDEX. There are many special
702 cases for INSTANCE which we take care of here, mainly to avoid
703 creating extra tree nodes when we don't have to. */
705 static tree
706 build_vtbl_ref_1 (tree instance, tree idx)
708 tree aref;
709 tree vtbl = NULL_TREE;
711 /* Try to figure out what a reference refers to, and
712 access its virtual function table directly. */
714 int cdtorp = 0;
715 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
717 tree basetype = non_reference (TREE_TYPE (instance));
719 if (fixed_type && !cdtorp)
721 tree binfo = lookup_base (fixed_type, basetype,
722 ba_unique, NULL, tf_none);
723 if (binfo && binfo != error_mark_node)
724 vtbl = unshare_expr (BINFO_VTABLE (binfo));
727 if (!vtbl)
728 vtbl = build_vfield_ref (instance, basetype);
730 aref = build_array_ref (input_location, vtbl, idx);
731 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
733 return aref;
736 tree
737 build_vtbl_ref (tree instance, tree idx)
739 tree aref = build_vtbl_ref_1 (instance, idx);
741 return aref;
744 /* Given a stable object pointer INSTANCE_PTR, return an expression which
745 yields a function pointer corresponding to vtable element INDEX. */
747 tree
748 build_vfn_ref (tree instance_ptr, tree idx)
750 tree aref;
752 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
753 tf_warning_or_error),
754 idx);
756 /* When using function descriptors, the address of the
757 vtable entry is treated as a function pointer. */
758 if (TARGET_VTABLE_USES_DESCRIPTORS)
759 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
760 cp_build_addr_expr (aref, tf_warning_or_error));
762 /* Remember this as a method reference, for later devirtualization. */
763 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
765 return aref;
768 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
769 for the given TYPE. */
771 static tree
772 get_vtable_name (tree type)
774 return mangle_vtbl_for_type (type);
777 /* DECL is an entity associated with TYPE, like a virtual table or an
778 implicitly generated constructor. Determine whether or not DECL
779 should have external or internal linkage at the object file
780 level. This routine does not deal with COMDAT linkage and other
781 similar complexities; it simply sets TREE_PUBLIC if it possible for
782 entities in other translation units to contain copies of DECL, in
783 the abstract. */
785 void
786 set_linkage_according_to_type (tree /*type*/, tree decl)
788 TREE_PUBLIC (decl) = 1;
789 determine_visibility (decl);
792 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
793 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
794 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
796 static tree
797 build_vtable (tree class_type, tree name, tree vtable_type)
799 tree decl;
801 decl = build_lang_decl (VAR_DECL, name, vtable_type);
802 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
803 now to avoid confusion in mangle_decl. */
804 SET_DECL_ASSEMBLER_NAME (decl, name);
805 DECL_CONTEXT (decl) = class_type;
806 DECL_ARTIFICIAL (decl) = 1;
807 TREE_STATIC (decl) = 1;
808 TREE_READONLY (decl) = 1;
809 DECL_VIRTUAL_P (decl) = 1;
810 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
811 DECL_USER_ALIGN (decl) = true;
812 DECL_VTABLE_OR_VTT_P (decl) = 1;
813 set_linkage_according_to_type (class_type, decl);
814 /* The vtable has not been defined -- yet. */
815 DECL_EXTERNAL (decl) = 1;
816 DECL_NOT_REALLY_EXTERN (decl) = 1;
818 /* Mark the VAR_DECL node representing the vtable itself as a
819 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
820 is rather important that such things be ignored because any
821 effort to actually generate DWARF for them will run into
822 trouble when/if we encounter code like:
824 #pragma interface
825 struct S { virtual void member (); };
827 because the artificial declaration of the vtable itself (as
828 manufactured by the g++ front end) will say that the vtable is
829 a static member of `S' but only *after* the debug output for
830 the definition of `S' has already been output. This causes
831 grief because the DWARF entry for the definition of the vtable
832 will try to refer back to an earlier *declaration* of the
833 vtable as a static member of `S' and there won't be one. We
834 might be able to arrange to have the "vtable static member"
835 attached to the member list for `S' before the debug info for
836 `S' get written (which would solve the problem) but that would
837 require more intrusive changes to the g++ front end. */
838 DECL_IGNORED_P (decl) = 1;
840 return decl;
843 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
844 or even complete. If this does not exist, create it. If COMPLETE is
845 nonzero, then complete the definition of it -- that will render it
846 impossible to actually build the vtable, but is useful to get at those
847 which are known to exist in the runtime. */
849 tree
850 get_vtable_decl (tree type, int complete)
852 tree decl;
854 if (CLASSTYPE_VTABLES (type))
855 return CLASSTYPE_VTABLES (type);
857 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
858 CLASSTYPE_VTABLES (type) = decl;
860 if (complete)
862 DECL_EXTERNAL (decl) = 1;
863 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
866 return decl;
869 /* Build the primary virtual function table for TYPE. If BINFO is
870 non-NULL, build the vtable starting with the initial approximation
871 that it is the same as the one which is the head of the association
872 list. Returns a nonzero value if a new vtable is actually
873 created. */
875 static int
876 build_primary_vtable (tree binfo, tree type)
878 tree decl;
879 tree virtuals;
881 decl = get_vtable_decl (type, /*complete=*/0);
883 if (binfo)
885 if (BINFO_NEW_VTABLE_MARKED (binfo))
886 /* We have already created a vtable for this base, so there's
887 no need to do it again. */
888 return 0;
890 virtuals = copy_list (BINFO_VIRTUALS (binfo));
891 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
892 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
893 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
895 else
897 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
898 virtuals = NULL_TREE;
901 if (GATHER_STATISTICS)
903 n_vtables += 1;
904 n_vtable_elems += list_length (virtuals);
907 /* Initialize the association list for this type, based
908 on our first approximation. */
909 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
910 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
911 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
912 return 1;
915 /* Give BINFO a new virtual function table which is initialized
916 with a skeleton-copy of its original initialization. The only
917 entry that changes is the `delta' entry, so we can really
918 share a lot of structure.
920 FOR_TYPE is the most derived type which caused this table to
921 be needed.
923 Returns nonzero if we haven't met BINFO before.
925 The order in which vtables are built (by calling this function) for
926 an object must remain the same, otherwise a binary incompatibility
927 can result. */
929 static int
930 build_secondary_vtable (tree binfo)
932 if (BINFO_NEW_VTABLE_MARKED (binfo))
933 /* We already created a vtable for this base. There's no need to
934 do it again. */
935 return 0;
937 /* Remember that we've created a vtable for this BINFO, so that we
938 don't try to do so again. */
939 SET_BINFO_NEW_VTABLE_MARKED (binfo);
941 /* Make fresh virtual list, so we can smash it later. */
942 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
944 /* Secondary vtables are laid out as part of the same structure as
945 the primary vtable. */
946 BINFO_VTABLE (binfo) = NULL_TREE;
947 return 1;
950 /* Create a new vtable for BINFO which is the hierarchy dominated by
951 T. Return nonzero if we actually created a new vtable. */
953 static int
954 make_new_vtable (tree t, tree binfo)
956 if (binfo == TYPE_BINFO (t))
957 /* In this case, it is *type*'s vtable we are modifying. We start
958 with the approximation that its vtable is that of the
959 immediate base class. */
960 return build_primary_vtable (binfo, t);
961 else
962 /* This is our very own copy of `basetype' to play with. Later,
963 we will fill in all the virtual functions that override the
964 virtual functions in these base classes which are not defined
965 by the current type. */
966 return build_secondary_vtable (binfo);
969 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
970 (which is in the hierarchy dominated by T) list FNDECL as its
971 BV_FN. DELTA is the required constant adjustment from the `this'
972 pointer where the vtable entry appears to the `this' required when
973 the function is actually called. */
975 static void
976 modify_vtable_entry (tree t,
977 tree binfo,
978 tree fndecl,
979 tree delta,
980 tree *virtuals)
982 tree v;
984 v = *virtuals;
986 if (fndecl != BV_FN (v)
987 || !tree_int_cst_equal (delta, BV_DELTA (v)))
989 /* We need a new vtable for BINFO. */
990 if (make_new_vtable (t, binfo))
992 /* If we really did make a new vtable, we also made a copy
993 of the BINFO_VIRTUALS list. Now, we have to find the
994 corresponding entry in that list. */
995 *virtuals = BINFO_VIRTUALS (binfo);
996 while (BV_FN (*virtuals) != BV_FN (v))
997 *virtuals = TREE_CHAIN (*virtuals);
998 v = *virtuals;
1001 BV_DELTA (v) = delta;
1002 BV_VCALL_INDEX (v) = NULL_TREE;
1003 BV_FN (v) = fndecl;
1008 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1009 METHOD is being injected via a using_decl. Returns true if the
1010 method could be added to the method vec. */
1012 bool
1013 add_method (tree type, tree method, bool via_using)
1015 unsigned slot;
1016 bool template_conv_p = false;
1017 bool conv_p;
1018 vec<tree, va_gc> *method_vec;
1019 bool complete_p;
1020 bool insert_p = false;
1021 tree current_fns;
1023 if (method == error_mark_node)
1024 return false;
1026 complete_p = COMPLETE_TYPE_P (type);
1027 conv_p = DECL_CONV_FN_P (method);
1028 if (conv_p)
1029 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1030 && DECL_TEMPLATE_CONV_FN_P (method));
1032 method_vec = CLASSTYPE_METHOD_VEC (type);
1033 if (!method_vec)
1035 /* Make a new method vector. We start with 8 entries. We must
1036 allocate at least two (for constructors and destructors), and
1037 we're going to end up with an assignment operator at some
1038 point as well. */
1039 vec_alloc (method_vec, 8);
1040 /* Create slots for constructors and destructors. */
1041 method_vec->quick_push (NULL_TREE);
1042 method_vec->quick_push (NULL_TREE);
1043 CLASSTYPE_METHOD_VEC (type) = method_vec;
1046 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1047 grok_special_member_properties (method);
1049 /* Constructors and destructors go in special slots. */
1050 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1051 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1052 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1053 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1054 else
1056 tree m;
1058 insert_p = true;
1059 /* See if we already have an entry with this name. */
1060 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1061 vec_safe_iterate (method_vec, slot, &m);
1062 ++slot)
1064 m = OVL_FIRST (m);
1065 if (template_conv_p)
1067 if (TREE_CODE (m) == TEMPLATE_DECL
1068 && DECL_TEMPLATE_CONV_FN_P (m))
1069 insert_p = false;
1070 break;
1072 if (conv_p && !DECL_CONV_FN_P (m))
1073 break;
1074 if (DECL_NAME (m) == DECL_NAME (method))
1076 insert_p = false;
1077 break;
1079 if (complete_p
1080 && !DECL_CONV_FN_P (m)
1081 && DECL_NAME (m) > DECL_NAME (method))
1082 break;
1085 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1087 /* Check to see if we've already got this method. */
1088 for (ovl_iterator iter (current_fns); iter; ++iter)
1090 tree fn = *iter;
1091 tree fn_type;
1092 tree method_type;
1093 tree parms1;
1094 tree parms2;
1096 if (TREE_CODE (fn) != TREE_CODE (method))
1097 continue;
1099 /* Two using-declarations can coexist, we'll complain about ambiguity in
1100 overload resolution. */
1101 if (via_using && iter.using_p ()
1102 /* Except handle inherited constructors specially. */
1103 && ! DECL_CONSTRUCTOR_P (fn))
1104 continue;
1106 /* [over.load] Member function declarations with the
1107 same name and the same parameter types cannot be
1108 overloaded if any of them is a static member
1109 function declaration.
1111 [over.load] Member function declarations with the same name and
1112 the same parameter-type-list as well as member function template
1113 declarations with the same name, the same parameter-type-list, and
1114 the same template parameter lists cannot be overloaded if any of
1115 them, but not all, have a ref-qualifier.
1117 [namespace.udecl] When a using-declaration brings names
1118 from a base class into a derived class scope, member
1119 functions in the derived class override and/or hide member
1120 functions with the same name and parameter types in a base
1121 class (rather than conflicting). */
1122 fn_type = TREE_TYPE (fn);
1123 method_type = TREE_TYPE (method);
1124 parms1 = TYPE_ARG_TYPES (fn_type);
1125 parms2 = TYPE_ARG_TYPES (method_type);
1127 /* Compare the quals on the 'this' parm. Don't compare
1128 the whole types, as used functions are treated as
1129 coming from the using class in overload resolution. */
1130 if (! DECL_STATIC_FUNCTION_P (fn)
1131 && ! DECL_STATIC_FUNCTION_P (method)
1132 /* Either both or neither need to be ref-qualified for
1133 differing quals to allow overloading. */
1134 && (FUNCTION_REF_QUALIFIED (fn_type)
1135 == FUNCTION_REF_QUALIFIED (method_type))
1136 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1137 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1138 continue;
1140 /* For templates, the return type and template parameters
1141 must be identical. */
1142 if (TREE_CODE (fn) == TEMPLATE_DECL
1143 && (!same_type_p (TREE_TYPE (fn_type),
1144 TREE_TYPE (method_type))
1145 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1146 DECL_TEMPLATE_PARMS (method))))
1147 continue;
1149 if (! DECL_STATIC_FUNCTION_P (fn))
1150 parms1 = TREE_CHAIN (parms1);
1151 if (! DECL_STATIC_FUNCTION_P (method))
1152 parms2 = TREE_CHAIN (parms2);
1154 /* Bring back parameters omitted from an inherited ctor. */
1155 if (ctor_omit_inherited_parms (fn))
1156 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1157 if (ctor_omit_inherited_parms (method))
1158 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1160 if (compparms (parms1, parms2)
1161 && (!DECL_CONV_FN_P (fn)
1162 || same_type_p (TREE_TYPE (fn_type),
1163 TREE_TYPE (method_type)))
1164 && equivalently_constrained (fn, method))
1166 /* For function versions, their parms and types match
1167 but they are not duplicates. Record function versions
1168 as and when they are found. extern "C" functions are
1169 not treated as versions. */
1170 if (TREE_CODE (fn) == FUNCTION_DECL
1171 && TREE_CODE (method) == FUNCTION_DECL
1172 && !DECL_EXTERN_C_P (fn)
1173 && !DECL_EXTERN_C_P (method)
1174 && targetm.target_option.function_versions (fn, method))
1176 /* Mark functions as versions if necessary. Modify the mangled
1177 decl name if necessary. */
1178 if (!DECL_FUNCTION_VERSIONED (fn))
1180 DECL_FUNCTION_VERSIONED (fn) = 1;
1181 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1182 mangle_decl (fn);
1184 if (!DECL_FUNCTION_VERSIONED (method))
1186 DECL_FUNCTION_VERSIONED (method) = 1;
1187 if (DECL_ASSEMBLER_NAME_SET_P (method))
1188 mangle_decl (method);
1190 cgraph_node::record_function_versions (fn, method);
1191 continue;
1194 if (DECL_INHERITED_CTOR (method))
1196 if (DECL_INHERITED_CTOR (fn))
1198 tree basem = DECL_INHERITED_CTOR_BASE (method);
1199 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1200 if (flag_new_inheriting_ctors)
1202 if (basem == basef)
1204 /* Inheriting the same constructor along different
1205 paths, combine them. */
1206 SET_DECL_INHERITED_CTOR
1207 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1208 DECL_INHERITED_CTOR (fn)));
1209 /* And discard the new one. */
1210 return false;
1212 else
1213 /* Inherited ctors can coexist until overload
1214 resolution. */
1215 continue;
1217 error_at (DECL_SOURCE_LOCATION (method),
1218 "%q#D", method);
1219 error_at (DECL_SOURCE_LOCATION (fn),
1220 "conflicts with version inherited from %qT",
1221 basef);
1223 /* Otherwise defer to the other function. */
1224 return false;
1227 if (via_using)
1228 /* Defer to the local function. */
1229 return false;
1230 else if (flag_new_inheriting_ctors
1231 && DECL_INHERITED_CTOR (fn))
1233 /* Remove the inherited constructor. */
1234 current_fns = iter.remove_node (current_fns);
1235 continue;
1237 else
1239 error ("%q+#D cannot be overloaded", method);
1240 error ("with %q+#D", fn);
1241 return false;
1246 /* A class should never have more than one destructor. */
1247 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1248 return false;
1250 current_fns = ovl_insert (method, current_fns, via_using);
1252 if (conv_p)
1253 TYPE_HAS_CONVERSION (type) = 1;
1254 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1255 push_class_level_binding (DECL_NAME (method), current_fns);
1257 if (insert_p)
1259 bool reallocated;
1261 /* We only expect to add few methods in the COMPLETE_P case, so
1262 just make room for one more method in that case. */
1263 if (complete_p)
1264 reallocated = vec_safe_reserve_exact (method_vec, 1);
1265 else
1266 reallocated = vec_safe_reserve (method_vec, 1);
1267 if (reallocated)
1268 CLASSTYPE_METHOD_VEC (type) = method_vec;
1269 if (slot == method_vec->length ())
1270 method_vec->quick_push (current_fns);
1271 else
1272 method_vec->quick_insert (slot, current_fns);
1274 else
1275 /* Replace the current slot. */
1276 (*method_vec)[slot] = current_fns;
1277 return true;
1280 /* Subroutines of finish_struct. */
1282 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1283 legit, otherwise return 0. */
1285 static int
1286 alter_access (tree t, tree fdecl, tree access)
1288 tree elem;
1290 retrofit_lang_decl (fdecl);
1292 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1294 elem = purpose_member (t, DECL_ACCESS (fdecl));
1295 if (elem)
1297 if (TREE_VALUE (elem) != access)
1299 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1300 error ("conflicting access specifications for method"
1301 " %q+D, ignored", TREE_TYPE (fdecl));
1302 else
1303 error ("conflicting access specifications for field %qE, ignored",
1304 DECL_NAME (fdecl));
1306 else
1308 /* They're changing the access to the same thing they changed
1309 it to before. That's OK. */
1313 else
1315 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1316 tf_warning_or_error);
1317 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1318 return 1;
1320 return 0;
1323 /* Return the access node for DECL's access in its enclosing class. */
1325 tree
1326 declared_access (tree decl)
1328 return (TREE_PRIVATE (decl) ? access_private_node
1329 : TREE_PROTECTED (decl) ? access_protected_node
1330 : access_public_node);
1333 /* Process the USING_DECL, which is a member of T. */
1335 static void
1336 handle_using_decl (tree using_decl, tree t)
1338 tree decl = USING_DECL_DECLS (using_decl);
1339 tree name = DECL_NAME (using_decl);
1340 tree access = declared_access (using_decl);
1341 tree flist = NULL_TREE;
1342 tree old_value;
1344 gcc_assert (!processing_template_decl && decl);
1346 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1347 tf_warning_or_error);
1348 if (old_value)
1350 old_value = OVL_FIRST (old_value);
1352 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1353 /* OK */;
1354 else
1355 old_value = NULL_TREE;
1358 cp_emit_debug_info_for_using (decl, t);
1360 if (is_overloaded_fn (decl))
1361 flist = decl;
1363 if (! old_value)
1365 else if (is_overloaded_fn (old_value))
1367 if (flist)
1368 /* It's OK to use functions from a base when there are functions with
1369 the same name already present in the current class. */;
1370 else
1372 error ("%q+D invalid in %q#T", using_decl, t);
1373 error (" because of local method %q+#D with same name",
1374 old_value);
1375 return;
1378 else if (!DECL_ARTIFICIAL (old_value))
1380 error ("%q+D invalid in %q#T", using_decl, t);
1381 error (" because of local member %q+#D with same name", old_value);
1382 return;
1385 /* Make type T see field decl FDECL with access ACCESS. */
1386 if (flist)
1387 for (ovl_iterator iter (flist); iter; ++iter)
1389 add_method (t, *iter, true);
1390 alter_access (t, *iter, access);
1392 else
1393 alter_access (t, decl, access);
1396 /* Data structure for find_abi_tags_r, below. */
1398 struct abi_tag_data
1400 tree t; // The type that we're checking for missing tags.
1401 tree subob; // The subobject of T that we're getting tags from.
1402 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1405 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1406 in the context of P. TAG can be either an identifier (the DECL_NAME of
1407 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1409 static void
1410 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1412 if (!IDENTIFIER_MARKED (id))
1414 if (p->tags != error_mark_node)
1416 /* We're collecting tags from template arguments or from
1417 the type of a variable or function return type. */
1418 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1420 /* Don't inherit this tag multiple times. */
1421 IDENTIFIER_MARKED (id) = true;
1423 if (TYPE_P (p->t))
1425 /* Tags inherited from type template arguments are only used
1426 to avoid warnings. */
1427 ABI_TAG_IMPLICIT (p->tags) = true;
1428 return;
1430 /* For functions and variables we want to warn, too. */
1433 /* Otherwise we're diagnosing missing tags. */
1434 if (TREE_CODE (p->t) == FUNCTION_DECL)
1436 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1437 "that %qT (used in its return type) has",
1438 p->t, tag, *tp))
1439 inform (location_of (*tp), "%qT declared here", *tp);
1441 else if (VAR_P (p->t))
1443 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1444 "that %qT (used in its type) has", p->t, tag, *tp))
1445 inform (location_of (*tp), "%qT declared here", *tp);
1447 else if (TYPE_P (p->subob))
1449 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1450 "that base %qT has", p->t, tag, p->subob))
1451 inform (location_of (p->subob), "%qT declared here",
1452 p->subob);
1454 else
1456 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1457 "that %qT (used in the type of %qD) has",
1458 p->t, tag, *tp, p->subob))
1460 inform (location_of (p->subob), "%qD declared here",
1461 p->subob);
1462 inform (location_of (*tp), "%qT declared here", *tp);
1468 /* Find all the ABI tags in the attribute list ATTR and either call
1469 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1471 static void
1472 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1474 if (!attr)
1475 return;
1476 for (; (attr = lookup_attribute ("abi_tag", attr));
1477 attr = TREE_CHAIN (attr))
1478 for (tree list = TREE_VALUE (attr); list;
1479 list = TREE_CHAIN (list))
1481 tree tag = TREE_VALUE (list);
1482 tree id = get_identifier (TREE_STRING_POINTER (tag));
1483 if (tp)
1484 check_tag (tag, id, tp, p);
1485 else
1486 IDENTIFIER_MARKED (id) = val;
1490 /* Find all the ABI tags on T and its enclosing scopes and either call
1491 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1493 static void
1494 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1496 while (t != global_namespace)
1498 tree attr;
1499 if (TYPE_P (t))
1501 attr = TYPE_ATTRIBUTES (t);
1502 t = CP_TYPE_CONTEXT (t);
1504 else
1506 attr = DECL_ATTRIBUTES (t);
1507 t = CP_DECL_CONTEXT (t);
1509 mark_or_check_attr_tags (attr, tp, p, val);
1513 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1514 types with ABI tags, add the corresponding identifiers to the VEC in
1515 *DATA and set IDENTIFIER_MARKED. */
1517 static tree
1518 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1520 if (!OVERLOAD_TYPE_P (*tp))
1521 return NULL_TREE;
1523 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1524 anyway, but let's make sure of it. */
1525 *walk_subtrees = false;
1527 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1529 mark_or_check_tags (*tp, tp, p, false);
1531 return NULL_TREE;
1534 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1535 IDENTIFIER_MARKED on its ABI tags. */
1537 static tree
1538 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1540 if (!OVERLOAD_TYPE_P (*tp))
1541 return NULL_TREE;
1543 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1544 anyway, but let's make sure of it. */
1545 *walk_subtrees = false;
1547 bool *valp = static_cast<bool*>(data);
1549 mark_or_check_tags (*tp, NULL, NULL, *valp);
1551 return NULL_TREE;
1554 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1555 scopes. */
1557 static void
1558 mark_abi_tags (tree t, bool val)
1560 mark_or_check_tags (t, NULL, NULL, val);
1561 if (DECL_P (t))
1563 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1564 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1566 /* Template arguments are part of the signature. */
1567 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1568 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1570 tree arg = TREE_VEC_ELT (level, j);
1571 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1574 if (TREE_CODE (t) == FUNCTION_DECL)
1575 /* A function's parameter types are part of the signature, so
1576 we don't need to inherit any tags that are also in them. */
1577 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1578 arg = TREE_CHAIN (arg))
1579 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1580 mark_abi_tags_r, &val);
1584 /* Check that T has all the ABI tags that subobject SUBOB has, or
1585 warn if not. If T is a (variable or function) declaration, also
1586 return any missing tags, and add them to T if JUST_CHECKING is false. */
1588 static tree
1589 check_abi_tags (tree t, tree subob, bool just_checking = false)
1591 bool inherit = DECL_P (t);
1593 if (!inherit && !warn_abi_tag)
1594 return NULL_TREE;
1596 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1597 if (!TREE_PUBLIC (decl))
1598 /* No need to worry about things local to this TU. */
1599 return NULL_TREE;
1601 mark_abi_tags (t, true);
1603 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1604 struct abi_tag_data data = { t, subob, error_mark_node };
1605 if (inherit)
1606 data.tags = NULL_TREE;
1608 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1610 if (!(inherit && data.tags))
1611 /* We don't need to do anything with data.tags. */;
1612 else if (just_checking)
1613 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1615 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1616 IDENTIFIER_MARKED (id) = false;
1618 else
1620 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1621 if (attr)
1622 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1623 else
1624 DECL_ATTRIBUTES (t)
1625 = tree_cons (get_identifier ("abi_tag"), data.tags,
1626 DECL_ATTRIBUTES (t));
1629 mark_abi_tags (t, false);
1631 return data.tags;
1634 /* Check that DECL has all the ABI tags that are used in parts of its type
1635 that are not reflected in its mangled name. */
1637 void
1638 check_abi_tags (tree decl)
1640 if (VAR_P (decl))
1641 check_abi_tags (decl, TREE_TYPE (decl));
1642 else if (TREE_CODE (decl) == FUNCTION_DECL
1643 && !DECL_CONV_FN_P (decl)
1644 && !mangle_return_type_p (decl))
1645 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1648 /* Return any ABI tags that are used in parts of the type of DECL
1649 that are not reflected in its mangled name. This function is only
1650 used in backward-compatible mangling for ABI <11. */
1652 tree
1653 missing_abi_tags (tree decl)
1655 if (VAR_P (decl))
1656 return check_abi_tags (decl, TREE_TYPE (decl), true);
1657 else if (TREE_CODE (decl) == FUNCTION_DECL
1658 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1659 that we can use this function for setting need_abi_warning
1660 regardless of the current flag_abi_version. */
1661 && !mangle_return_type_p (decl))
1662 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1663 else
1664 return NULL_TREE;
1667 void
1668 inherit_targ_abi_tags (tree t)
1670 if (!CLASS_TYPE_P (t)
1671 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1672 return;
1674 mark_abi_tags (t, true);
1676 tree args = CLASSTYPE_TI_ARGS (t);
1677 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1678 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1680 tree level = TMPL_ARGS_LEVEL (args, i+1);
1681 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1683 tree arg = TREE_VEC_ELT (level, j);
1684 data.subob = arg;
1685 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1689 // If we found some tags on our template arguments, add them to our
1690 // abi_tag attribute.
1691 if (data.tags)
1693 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1694 if (attr)
1695 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1696 else
1697 TYPE_ATTRIBUTES (t)
1698 = tree_cons (get_identifier ("abi_tag"), data.tags,
1699 TYPE_ATTRIBUTES (t));
1702 mark_abi_tags (t, false);
1705 /* Return true, iff class T has a non-virtual destructor that is
1706 accessible from outside the class heirarchy (i.e. is public, or
1707 there's a suitable friend. */
1709 static bool
1710 accessible_nvdtor_p (tree t)
1712 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1714 /* An implicitly declared destructor is always public. And,
1715 if it were virtual, we would have created it by now. */
1716 if (!dtor)
1717 return true;
1719 if (DECL_VINDEX (dtor))
1720 return false; /* Virtual */
1722 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1723 return true; /* Public */
1725 if (CLASSTYPE_FRIEND_CLASSES (t)
1726 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1727 return true; /* Has friends */
1729 return false;
1732 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1733 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1734 properties of the bases. */
1736 static void
1737 check_bases (tree t,
1738 int* cant_have_const_ctor_p,
1739 int* no_const_asn_ref_p)
1741 int i;
1742 bool seen_non_virtual_nearly_empty_base_p = 0;
1743 int seen_tm_mask = 0;
1744 tree base_binfo;
1745 tree binfo;
1746 tree field = NULL_TREE;
1748 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1749 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1750 if (TREE_CODE (field) == FIELD_DECL)
1751 break;
1753 for (binfo = TYPE_BINFO (t), i = 0;
1754 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1756 tree basetype = TREE_TYPE (base_binfo);
1758 gcc_assert (COMPLETE_TYPE_P (basetype));
1760 if (CLASSTYPE_FINAL (basetype))
1761 error ("cannot derive from %<final%> base %qT in derived type %qT",
1762 basetype, t);
1764 /* If any base class is non-literal, so is the derived class. */
1765 if (!CLASSTYPE_LITERAL_P (basetype))
1766 CLASSTYPE_LITERAL_P (t) = false;
1768 /* If the base class doesn't have copy constructors or
1769 assignment operators that take const references, then the
1770 derived class cannot have such a member automatically
1771 generated. */
1772 if (TYPE_HAS_COPY_CTOR (basetype)
1773 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1774 *cant_have_const_ctor_p = 1;
1775 if (TYPE_HAS_COPY_ASSIGN (basetype)
1776 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1777 *no_const_asn_ref_p = 1;
1779 if (BINFO_VIRTUAL_P (base_binfo))
1780 /* A virtual base does not effect nearly emptiness. */
1782 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1784 if (seen_non_virtual_nearly_empty_base_p)
1785 /* And if there is more than one nearly empty base, then the
1786 derived class is not nearly empty either. */
1787 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1788 else
1789 /* Remember we've seen one. */
1790 seen_non_virtual_nearly_empty_base_p = 1;
1792 else if (!is_empty_class (basetype))
1793 /* If the base class is not empty or nearly empty, then this
1794 class cannot be nearly empty. */
1795 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1797 /* A lot of properties from the bases also apply to the derived
1798 class. */
1799 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1800 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1801 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1802 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1803 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1804 || !TYPE_HAS_COPY_ASSIGN (basetype));
1805 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1806 || !TYPE_HAS_COPY_CTOR (basetype));
1807 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1808 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1809 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1810 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1811 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1812 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1813 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1814 || TYPE_HAS_COMPLEX_DFLT (basetype));
1815 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1816 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1817 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1818 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1819 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1820 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1821 if (TYPE_HAS_MUTABLE_P (basetype))
1822 CLASSTYPE_HAS_MUTABLE (t) = 1;
1824 /* A standard-layout class is a class that:
1826 * has no non-standard-layout base classes, */
1827 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1828 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1830 tree basefield;
1831 /* ...has no base classes of the same type as the first non-static
1832 data member... */
1833 if (field && DECL_CONTEXT (field) == t
1834 && (same_type_ignoring_top_level_qualifiers_p
1835 (TREE_TYPE (field), basetype)))
1836 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1837 else
1838 /* ...either has no non-static data members in the most-derived
1839 class and at most one base class with non-static data
1840 members, or has no base classes with non-static data
1841 members */
1842 for (basefield = TYPE_FIELDS (basetype); basefield;
1843 basefield = DECL_CHAIN (basefield))
1844 if (TREE_CODE (basefield) == FIELD_DECL
1845 && !(DECL_FIELD_IS_BASE (basefield)
1846 && integer_zerop (DECL_SIZE (basefield))))
1848 if (field)
1849 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1850 else
1851 field = basefield;
1852 break;
1856 /* Don't bother collecting tm attributes if transactional memory
1857 support is not enabled. */
1858 if (flag_tm)
1860 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1861 if (tm_attr)
1862 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1865 check_abi_tags (t, basetype);
1868 /* If one of the base classes had TM attributes, and the current class
1869 doesn't define its own, then the current class inherits one. */
1870 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1872 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1873 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1877 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1878 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1879 that have had a nearly-empty virtual primary base stolen by some
1880 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1881 T. */
1883 static void
1884 determine_primary_bases (tree t)
1886 unsigned i;
1887 tree primary = NULL_TREE;
1888 tree type_binfo = TYPE_BINFO (t);
1889 tree base_binfo;
1891 /* Determine the primary bases of our bases. */
1892 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1893 base_binfo = TREE_CHAIN (base_binfo))
1895 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1897 /* See if we're the non-virtual primary of our inheritance
1898 chain. */
1899 if (!BINFO_VIRTUAL_P (base_binfo))
1901 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1902 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1904 if (parent_primary
1905 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1906 BINFO_TYPE (parent_primary)))
1907 /* We are the primary binfo. */
1908 BINFO_PRIMARY_P (base_binfo) = 1;
1910 /* Determine if we have a virtual primary base, and mark it so.
1912 if (primary && BINFO_VIRTUAL_P (primary))
1914 tree this_primary = copied_binfo (primary, base_binfo);
1916 if (BINFO_PRIMARY_P (this_primary))
1917 /* Someone already claimed this base. */
1918 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1919 else
1921 tree delta;
1923 BINFO_PRIMARY_P (this_primary) = 1;
1924 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1926 /* A virtual binfo might have been copied from within
1927 another hierarchy. As we're about to use it as a
1928 primary base, make sure the offsets match. */
1929 delta = size_diffop_loc (input_location,
1930 fold_convert (ssizetype,
1931 BINFO_OFFSET (base_binfo)),
1932 fold_convert (ssizetype,
1933 BINFO_OFFSET (this_primary)));
1935 propagate_binfo_offsets (this_primary, delta);
1940 /* First look for a dynamic direct non-virtual base. */
1941 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1943 tree basetype = BINFO_TYPE (base_binfo);
1945 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1947 primary = base_binfo;
1948 goto found;
1952 /* A "nearly-empty" virtual base class can be the primary base
1953 class, if no non-virtual polymorphic base can be found. Look for
1954 a nearly-empty virtual dynamic base that is not already a primary
1955 base of something in the hierarchy. If there is no such base,
1956 just pick the first nearly-empty virtual base. */
1958 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1959 base_binfo = TREE_CHAIN (base_binfo))
1960 if (BINFO_VIRTUAL_P (base_binfo)
1961 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1963 if (!BINFO_PRIMARY_P (base_binfo))
1965 /* Found one that is not primary. */
1966 primary = base_binfo;
1967 goto found;
1969 else if (!primary)
1970 /* Remember the first candidate. */
1971 primary = base_binfo;
1974 found:
1975 /* If we've got a primary base, use it. */
1976 if (primary)
1978 tree basetype = BINFO_TYPE (primary);
1980 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1981 if (BINFO_PRIMARY_P (primary))
1982 /* We are stealing a primary base. */
1983 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1984 BINFO_PRIMARY_P (primary) = 1;
1985 if (BINFO_VIRTUAL_P (primary))
1987 tree delta;
1989 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1990 /* A virtual binfo might have been copied from within
1991 another hierarchy. As we're about to use it as a primary
1992 base, make sure the offsets match. */
1993 delta = size_diffop_loc (input_location, ssize_int (0),
1994 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1996 propagate_binfo_offsets (primary, delta);
1999 primary = TYPE_BINFO (basetype);
2001 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2002 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2003 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2007 /* Update the variant types of T. */
2009 void
2010 fixup_type_variants (tree t)
2012 tree variants;
2014 if (!t)
2015 return;
2017 for (variants = TYPE_NEXT_VARIANT (t);
2018 variants;
2019 variants = TYPE_NEXT_VARIANT (variants))
2021 /* These fields are in the _TYPE part of the node, not in
2022 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2023 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
2024 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2025 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2026 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2028 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2030 TYPE_BINFO (variants) = TYPE_BINFO (t);
2032 /* Copy whatever these are holding today. */
2033 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
2034 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2038 /* KLASS is a class that we're applying may_alias to after the body is
2039 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2040 canonical type(s) will be implicitly updated. */
2042 static void
2043 fixup_may_alias (tree klass)
2045 tree t, v;
2047 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2048 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2049 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2050 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2051 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2052 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2055 /* Early variant fixups: we apply attributes at the beginning of the class
2056 definition, and we need to fix up any variants that have already been
2057 made via elaborated-type-specifier so that check_qualified_type works. */
2059 void
2060 fixup_attribute_variants (tree t)
2062 tree variants;
2064 if (!t)
2065 return;
2067 tree attrs = TYPE_ATTRIBUTES (t);
2068 unsigned align = TYPE_ALIGN (t);
2069 bool user_align = TYPE_USER_ALIGN (t);
2070 bool may_alias = lookup_attribute ("may_alias", attrs);
2072 if (may_alias)
2073 fixup_may_alias (t);
2075 for (variants = TYPE_NEXT_VARIANT (t);
2076 variants;
2077 variants = TYPE_NEXT_VARIANT (variants))
2079 /* These are the two fields that check_qualified_type looks at and
2080 are affected by attributes. */
2081 TYPE_ATTRIBUTES (variants) = attrs;
2082 unsigned valign = align;
2083 if (TYPE_USER_ALIGN (variants))
2084 valign = MAX (valign, TYPE_ALIGN (variants));
2085 else
2086 TYPE_USER_ALIGN (variants) = user_align;
2087 SET_TYPE_ALIGN (variants, valign);
2088 if (may_alias)
2089 fixup_may_alias (variants);
2093 /* Set memoizing fields and bits of T (and its variants) for later
2094 use. */
2096 static void
2097 finish_struct_bits (tree t)
2099 /* Fix up variants (if any). */
2100 fixup_type_variants (t);
2102 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2103 /* For a class w/o baseclasses, 'finish_struct' has set
2104 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2105 Similarly for a class whose base classes do not have vtables.
2106 When neither of these is true, we might have removed abstract
2107 virtuals (by providing a definition), added some (by declaring
2108 new ones), or redeclared ones from a base class. We need to
2109 recalculate what's really an abstract virtual at this point (by
2110 looking in the vtables). */
2111 get_pure_virtuals (t);
2113 /* If this type has a copy constructor or a destructor, force its
2114 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2115 nonzero. This will cause it to be passed by invisible reference
2116 and prevent it from being returned in a register. */
2117 if (type_has_nontrivial_copy_init (t)
2118 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2120 tree variants;
2121 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2122 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2124 SET_TYPE_MODE (variants, BLKmode);
2125 TREE_ADDRESSABLE (variants) = 1;
2130 /* Issue warnings about T having private constructors, but no friends,
2131 and so forth.
2133 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2134 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2135 non-private static member functions. */
2137 static void
2138 maybe_warn_about_overly_private_class (tree t)
2140 int has_member_fn = 0;
2141 int has_nonprivate_method = 0;
2142 tree fn;
2144 if (!warn_ctor_dtor_privacy
2145 /* If the class has friends, those entities might create and
2146 access instances, so we should not warn. */
2147 || (CLASSTYPE_FRIEND_CLASSES (t)
2148 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2149 /* We will have warned when the template was declared; there's
2150 no need to warn on every instantiation. */
2151 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2152 /* There's no reason to even consider warning about this
2153 class. */
2154 return;
2156 /* We only issue one warning, if more than one applies, because
2157 otherwise, on code like:
2159 class A {
2160 // Oops - forgot `public:'
2161 A();
2162 A(const A&);
2163 ~A();
2166 we warn several times about essentially the same problem. */
2168 /* Check to see if all (non-constructor, non-destructor) member
2169 functions are private. (Since there are no friends or
2170 non-private statics, we can't ever call any of the private member
2171 functions.) */
2172 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2173 /* We're not interested in compiler-generated methods; they don't
2174 provide any way to call private members. */
2175 if (!DECL_ARTIFICIAL (fn))
2177 if (!TREE_PRIVATE (fn))
2179 if (DECL_STATIC_FUNCTION_P (fn))
2180 /* A non-private static member function is just like a
2181 friend; it can create and invoke private member
2182 functions, and be accessed without a class
2183 instance. */
2184 return;
2186 has_nonprivate_method = 1;
2187 /* Keep searching for a static member function. */
2189 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2190 has_member_fn = 1;
2193 if (!has_nonprivate_method && has_member_fn)
2195 /* There are no non-private methods, and there's at least one
2196 private member function that isn't a constructor or
2197 destructor. (If all the private members are
2198 constructors/destructors we want to use the code below that
2199 issues error messages specifically referring to
2200 constructors/destructors.) */
2201 unsigned i;
2202 tree binfo = TYPE_BINFO (t);
2204 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2205 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2207 has_nonprivate_method = 1;
2208 break;
2210 if (!has_nonprivate_method)
2212 warning (OPT_Wctor_dtor_privacy,
2213 "all member functions in class %qT are private", t);
2214 return;
2218 /* Even if some of the member functions are non-private, the class
2219 won't be useful for much if all the constructors or destructors
2220 are private: such an object can never be created or destroyed. */
2221 fn = CLASSTYPE_DESTRUCTORS (t);
2222 if (fn && TREE_PRIVATE (fn))
2224 warning (OPT_Wctor_dtor_privacy,
2225 "%q#T only defines a private destructor and has no friends",
2227 return;
2230 /* Warn about classes that have private constructors and no friends. */
2231 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2232 /* Implicitly generated constructors are always public. */
2233 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2234 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2236 bool nonprivate_ctor = false;
2238 /* If a non-template class does not define a copy
2239 constructor, one is defined for it, enabling it to avoid
2240 this warning. For a template class, this does not
2241 happen, and so we would normally get a warning on:
2243 template <class T> class C { private: C(); };
2245 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2246 complete non-template or fully instantiated classes have this
2247 flag set. */
2248 if (!TYPE_HAS_COPY_CTOR (t))
2249 nonprivate_ctor = true;
2250 else
2251 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2252 !nonprivate_ctor && iter; ++iter)
2253 /* Ideally, we wouldn't count copy constructors (or, in
2254 fact, any constructor that takes an argument of the class
2255 type as a parameter) because such things cannot be used
2256 to construct an instance of the class unless you already
2257 have one. But, for now at least, we're more
2258 generous. */
2259 if (! TREE_PRIVATE (*iter))
2260 nonprivate_ctor = true;
2262 if (!nonprivate_ctor)
2264 warning (OPT_Wctor_dtor_privacy,
2265 "%q#T only defines private constructors and has no friends",
2267 return;
2272 static struct {
2273 gt_pointer_operator new_value;
2274 void *cookie;
2275 } resort_data;
2277 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2279 static int
2280 method_name_cmp (const void* m1_p, const void* m2_p)
2282 const tree *const m1 = (const tree *) m1_p;
2283 const tree *const m2 = (const tree *) m2_p;
2285 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2286 return 0;
2287 if (*m1 == NULL_TREE)
2288 return -1;
2289 if (*m2 == NULL_TREE)
2290 return 1;
2291 if (OVL_NAME (*m1) < OVL_NAME (*m2))
2292 return -1;
2293 return 1;
2296 /* This routine compares two fields like method_name_cmp but using the
2297 pointer operator in resort_field_decl_data. */
2299 static int
2300 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2302 const tree *const m1 = (const tree *) m1_p;
2303 const tree *const m2 = (const tree *) m2_p;
2304 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2305 return 0;
2306 if (*m1 == NULL_TREE)
2307 return -1;
2308 if (*m2 == NULL_TREE)
2309 return 1;
2311 tree d1 = OVL_NAME (*m1);
2312 tree d2 = OVL_NAME (*m2);
2313 resort_data.new_value (&d1, resort_data.cookie);
2314 resort_data.new_value (&d2, resort_data.cookie);
2315 if (d1 < d2)
2316 return -1;
2318 return 1;
2321 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2323 void
2324 resort_type_method_vec (void* obj,
2325 void* /*orig_obj*/,
2326 gt_pointer_operator new_value,
2327 void* cookie)
2329 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
2330 int len = vec_safe_length (method_vec);
2331 size_t slot;
2332 tree fn;
2334 /* The type conversion ops have to live at the front of the vec, so we
2335 can't sort them. */
2336 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2337 vec_safe_iterate (method_vec, slot, &fn);
2338 ++slot)
2339 if (!DECL_CONV_FN_P (OVL_FIRST (fn)))
2340 break;
2342 if (len - slot > 1)
2344 resort_data.new_value = new_value;
2345 resort_data.cookie = cookie;
2346 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2347 resort_method_name_cmp);
2351 /* Warn about duplicate methods in fn_fields.
2353 Sort methods that are not special (i.e., constructors, destructors,
2354 and type conversion operators) so that we can find them faster in
2355 search. */
2357 static void
2358 finish_struct_methods (tree t)
2360 tree fn_fields;
2361 vec<tree, va_gc> *method_vec;
2362 int slot, len;
2364 method_vec = CLASSTYPE_METHOD_VEC (t);
2365 if (!method_vec)
2366 return;
2368 len = method_vec->length ();
2370 /* Clear DECL_IN_AGGR_P for all functions. */
2371 for (fn_fields = TYPE_METHODS (t); fn_fields;
2372 fn_fields = DECL_CHAIN (fn_fields))
2373 DECL_IN_AGGR_P (fn_fields) = 0;
2375 /* Issue warnings about private constructors and such. If there are
2376 no methods, then some public defaults are generated. */
2377 maybe_warn_about_overly_private_class (t);
2379 /* The type conversion ops have to live at the front of the vec, so we
2380 can't sort them. */
2381 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2382 method_vec->iterate (slot, &fn_fields);
2383 ++slot)
2384 if (!DECL_CONV_FN_P (OVL_FIRST (fn_fields)))
2385 break;
2386 if (len - slot > 1)
2387 qsort (method_vec->address () + slot,
2388 len-slot, sizeof (tree), method_name_cmp);
2391 /* Make BINFO's vtable have N entries, including RTTI entries,
2392 vbase and vcall offsets, etc. Set its type and call the back end
2393 to lay it out. */
2395 static void
2396 layout_vtable_decl (tree binfo, int n)
2398 tree atype;
2399 tree vtable;
2401 atype = build_array_of_n_type (vtable_entry_type, n);
2402 layout_type (atype);
2404 /* We may have to grow the vtable. */
2405 vtable = get_vtbl_decl_for_binfo (binfo);
2406 if (!same_type_p (TREE_TYPE (vtable), atype))
2408 TREE_TYPE (vtable) = atype;
2409 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2410 layout_decl (vtable, 0);
2414 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2415 have the same signature. */
2418 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2420 /* One destructor overrides another if they are the same kind of
2421 destructor. */
2422 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2423 && special_function_p (base_fndecl) == special_function_p (fndecl))
2424 return 1;
2425 /* But a non-destructor never overrides a destructor, nor vice
2426 versa, nor do different kinds of destructors override
2427 one-another. For example, a complete object destructor does not
2428 override a deleting destructor. */
2429 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2430 return 0;
2432 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2433 || (DECL_CONV_FN_P (fndecl)
2434 && DECL_CONV_FN_P (base_fndecl)
2435 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2436 DECL_CONV_FN_TYPE (base_fndecl))))
2438 tree fntype = TREE_TYPE (fndecl);
2439 tree base_fntype = TREE_TYPE (base_fndecl);
2440 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2441 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2442 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2443 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2444 return 1;
2446 return 0;
2449 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2450 subobject. */
2452 static bool
2453 base_derived_from (tree derived, tree base)
2455 tree probe;
2457 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2459 if (probe == derived)
2460 return true;
2461 else if (BINFO_VIRTUAL_P (probe))
2462 /* If we meet a virtual base, we can't follow the inheritance
2463 any more. See if the complete type of DERIVED contains
2464 such a virtual base. */
2465 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2466 != NULL_TREE);
2468 return false;
2471 struct find_final_overrider_data {
2472 /* The function for which we are trying to find a final overrider. */
2473 tree fn;
2474 /* The base class in which the function was declared. */
2475 tree declaring_base;
2476 /* The candidate overriders. */
2477 tree candidates;
2478 /* Path to most derived. */
2479 vec<tree> path;
2482 /* Add the overrider along the current path to FFOD->CANDIDATES.
2483 Returns true if an overrider was found; false otherwise. */
2485 static bool
2486 dfs_find_final_overrider_1 (tree binfo,
2487 find_final_overrider_data *ffod,
2488 unsigned depth)
2490 tree method;
2492 /* If BINFO is not the most derived type, try a more derived class.
2493 A definition there will overrider a definition here. */
2494 if (depth)
2496 depth--;
2497 if (dfs_find_final_overrider_1
2498 (ffod->path[depth], ffod, depth))
2499 return true;
2502 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2503 if (method)
2505 tree *candidate = &ffod->candidates;
2507 /* Remove any candidates overridden by this new function. */
2508 while (*candidate)
2510 /* If *CANDIDATE overrides METHOD, then METHOD
2511 cannot override anything else on the list. */
2512 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2513 return true;
2514 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2515 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2516 *candidate = TREE_CHAIN (*candidate);
2517 else
2518 candidate = &TREE_CHAIN (*candidate);
2521 /* Add the new function. */
2522 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2523 return true;
2526 return false;
2529 /* Called from find_final_overrider via dfs_walk. */
2531 static tree
2532 dfs_find_final_overrider_pre (tree binfo, void *data)
2534 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2536 if (binfo == ffod->declaring_base)
2537 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2538 ffod->path.safe_push (binfo);
2540 return NULL_TREE;
2543 static tree
2544 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2546 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2547 ffod->path.pop ();
2549 return NULL_TREE;
2552 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2553 FN and whose TREE_VALUE is the binfo for the base where the
2554 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2555 DERIVED) is the base object in which FN is declared. */
2557 static tree
2558 find_final_overrider (tree derived, tree binfo, tree fn)
2560 find_final_overrider_data ffod;
2562 /* Getting this right is a little tricky. This is valid:
2564 struct S { virtual void f (); };
2565 struct T { virtual void f (); };
2566 struct U : public S, public T { };
2568 even though calling `f' in `U' is ambiguous. But,
2570 struct R { virtual void f(); };
2571 struct S : virtual public R { virtual void f (); };
2572 struct T : virtual public R { virtual void f (); };
2573 struct U : public S, public T { };
2575 is not -- there's no way to decide whether to put `S::f' or
2576 `T::f' in the vtable for `R'.
2578 The solution is to look at all paths to BINFO. If we find
2579 different overriders along any two, then there is a problem. */
2580 if (DECL_THUNK_P (fn))
2581 fn = THUNK_TARGET (fn);
2583 /* Determine the depth of the hierarchy. */
2584 ffod.fn = fn;
2585 ffod.declaring_base = binfo;
2586 ffod.candidates = NULL_TREE;
2587 ffod.path.create (30);
2589 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2590 dfs_find_final_overrider_post, &ffod);
2592 ffod.path.release ();
2594 /* If there was no winner, issue an error message. */
2595 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2596 return error_mark_node;
2598 return ffod.candidates;
2601 /* Return the index of the vcall offset for FN when TYPE is used as a
2602 virtual base. */
2604 static tree
2605 get_vcall_index (tree fn, tree type)
2607 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2608 tree_pair_p p;
2609 unsigned ix;
2611 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2612 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2613 || same_signature_p (fn, p->purpose))
2614 return p->value;
2616 /* There should always be an appropriate index. */
2617 gcc_unreachable ();
2620 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2621 dominated by T. FN is the old function; VIRTUALS points to the
2622 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2623 of that entry in the list. */
2625 static void
2626 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2627 unsigned ix)
2629 tree b;
2630 tree overrider;
2631 tree delta;
2632 tree virtual_base;
2633 tree first_defn;
2634 tree overrider_fn, overrider_target;
2635 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2636 tree over_return, base_return;
2637 bool lost = false;
2639 /* Find the nearest primary base (possibly binfo itself) which defines
2640 this function; this is the class the caller will convert to when
2641 calling FN through BINFO. */
2642 for (b = binfo; ; b = get_primary_binfo (b))
2644 gcc_assert (b);
2645 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2646 break;
2648 /* The nearest definition is from a lost primary. */
2649 if (BINFO_LOST_PRIMARY_P (b))
2650 lost = true;
2652 first_defn = b;
2654 /* Find the final overrider. */
2655 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2656 if (overrider == error_mark_node)
2658 error ("no unique final overrider for %qD in %qT", target_fn, t);
2659 return;
2661 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2663 /* Check for adjusting covariant return types. */
2664 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2665 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2667 if (POINTER_TYPE_P (over_return)
2668 && TREE_CODE (over_return) == TREE_CODE (base_return)
2669 && CLASS_TYPE_P (TREE_TYPE (over_return))
2670 && CLASS_TYPE_P (TREE_TYPE (base_return))
2671 /* If the overrider is invalid, don't even try. */
2672 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2674 /* If FN is a covariant thunk, we must figure out the adjustment
2675 to the final base FN was converting to. As OVERRIDER_TARGET might
2676 also be converting to the return type of FN, we have to
2677 combine the two conversions here. */
2678 tree fixed_offset, virtual_offset;
2680 over_return = TREE_TYPE (over_return);
2681 base_return = TREE_TYPE (base_return);
2683 if (DECL_THUNK_P (fn))
2685 gcc_assert (DECL_RESULT_THUNK_P (fn));
2686 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2687 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2689 else
2690 fixed_offset = virtual_offset = NULL_TREE;
2692 if (virtual_offset)
2693 /* Find the equivalent binfo within the return type of the
2694 overriding function. We will want the vbase offset from
2695 there. */
2696 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2697 over_return);
2698 else if (!same_type_ignoring_top_level_qualifiers_p
2699 (over_return, base_return))
2701 /* There was no existing virtual thunk (which takes
2702 precedence). So find the binfo of the base function's
2703 return type within the overriding function's return type.
2704 Fortunately we know the covariancy is valid (it
2705 has already been checked), so we can just iterate along
2706 the binfos, which have been chained in inheritance graph
2707 order. Of course it is lame that we have to repeat the
2708 search here anyway -- we should really be caching pieces
2709 of the vtable and avoiding this repeated work. */
2710 tree thunk_binfo, base_binfo;
2712 /* Find the base binfo within the overriding function's
2713 return type. We will always find a thunk_binfo, except
2714 when the covariancy is invalid (which we will have
2715 already diagnosed). */
2716 for (base_binfo = TYPE_BINFO (base_return),
2717 thunk_binfo = TYPE_BINFO (over_return);
2718 thunk_binfo;
2719 thunk_binfo = TREE_CHAIN (thunk_binfo))
2720 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2721 BINFO_TYPE (base_binfo)))
2722 break;
2724 /* See if virtual inheritance is involved. */
2725 for (virtual_offset = thunk_binfo;
2726 virtual_offset;
2727 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2728 if (BINFO_VIRTUAL_P (virtual_offset))
2729 break;
2731 if (virtual_offset
2732 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2734 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2736 if (virtual_offset)
2738 /* We convert via virtual base. Adjust the fixed
2739 offset to be from there. */
2740 offset =
2741 size_diffop (offset,
2742 fold_convert (ssizetype,
2743 BINFO_OFFSET (virtual_offset)));
2745 if (fixed_offset)
2746 /* There was an existing fixed offset, this must be
2747 from the base just converted to, and the base the
2748 FN was thunking to. */
2749 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2750 else
2751 fixed_offset = offset;
2755 if (fixed_offset || virtual_offset)
2756 /* Replace the overriding function with a covariant thunk. We
2757 will emit the overriding function in its own slot as
2758 well. */
2759 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2760 fixed_offset, virtual_offset);
2762 else
2763 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2764 !DECL_THUNK_P (fn));
2766 /* If we need a covariant thunk, then we may need to adjust first_defn.
2767 The ABI specifies that the thunks emitted with a function are
2768 determined by which bases the function overrides, so we need to be
2769 sure that we're using a thunk for some overridden base; even if we
2770 know that the necessary this adjustment is zero, there may not be an
2771 appropriate zero-this-adjustment thunk for us to use since thunks for
2772 overriding virtual bases always use the vcall offset.
2774 Furthermore, just choosing any base that overrides this function isn't
2775 quite right, as this slot won't be used for calls through a type that
2776 puts a covariant thunk here. Calling the function through such a type
2777 will use a different slot, and that slot is the one that determines
2778 the thunk emitted for that base.
2780 So, keep looking until we find the base that we're really overriding
2781 in this slot: the nearest primary base that doesn't use a covariant
2782 thunk in this slot. */
2783 if (overrider_target != overrider_fn)
2785 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2786 /* We already know that the overrider needs a covariant thunk. */
2787 b = get_primary_binfo (b);
2788 for (; ; b = get_primary_binfo (b))
2790 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2791 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2792 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2793 break;
2794 if (BINFO_LOST_PRIMARY_P (b))
2795 lost = true;
2797 first_defn = b;
2800 /* Assume that we will produce a thunk that convert all the way to
2801 the final overrider, and not to an intermediate virtual base. */
2802 virtual_base = NULL_TREE;
2804 /* See if we can convert to an intermediate virtual base first, and then
2805 use the vcall offset located there to finish the conversion. */
2806 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2808 /* If we find the final overrider, then we can stop
2809 walking. */
2810 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2811 BINFO_TYPE (TREE_VALUE (overrider))))
2812 break;
2814 /* If we find a virtual base, and we haven't yet found the
2815 overrider, then there is a virtual base between the
2816 declaring base (first_defn) and the final overrider. */
2817 if (BINFO_VIRTUAL_P (b))
2819 virtual_base = b;
2820 break;
2824 /* Compute the constant adjustment to the `this' pointer. The
2825 `this' pointer, when this function is called, will point at BINFO
2826 (or one of its primary bases, which are at the same offset). */
2827 if (virtual_base)
2828 /* The `this' pointer needs to be adjusted from the declaration to
2829 the nearest virtual base. */
2830 delta = size_diffop_loc (input_location,
2831 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2832 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2833 else if (lost)
2834 /* If the nearest definition is in a lost primary, we don't need an
2835 entry in our vtable. Except possibly in a constructor vtable,
2836 if we happen to get our primary back. In that case, the offset
2837 will be zero, as it will be a primary base. */
2838 delta = size_zero_node;
2839 else
2840 /* The `this' pointer needs to be adjusted from pointing to
2841 BINFO to pointing at the base where the final overrider
2842 appears. */
2843 delta = size_diffop_loc (input_location,
2844 fold_convert (ssizetype,
2845 BINFO_OFFSET (TREE_VALUE (overrider))),
2846 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2848 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2850 if (virtual_base)
2851 BV_VCALL_INDEX (*virtuals)
2852 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2853 else
2854 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2856 BV_LOST_PRIMARY (*virtuals) = lost;
2859 /* Called from modify_all_vtables via dfs_walk. */
2861 static tree
2862 dfs_modify_vtables (tree binfo, void* data)
2864 tree t = (tree) data;
2865 tree virtuals;
2866 tree old_virtuals;
2867 unsigned ix;
2869 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2870 /* A base without a vtable needs no modification, and its bases
2871 are uninteresting. */
2872 return dfs_skip_bases;
2874 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2875 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2876 /* Don't do the primary vtable, if it's new. */
2877 return NULL_TREE;
2879 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2880 /* There's no need to modify the vtable for a non-virtual primary
2881 base; we're not going to use that vtable anyhow. We do still
2882 need to do this for virtual primary bases, as they could become
2883 non-primary in a construction vtable. */
2884 return NULL_TREE;
2886 make_new_vtable (t, binfo);
2888 /* Now, go through each of the virtual functions in the virtual
2889 function table for BINFO. Find the final overrider, and update
2890 the BINFO_VIRTUALS list appropriately. */
2891 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2892 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2893 virtuals;
2894 ix++, virtuals = TREE_CHAIN (virtuals),
2895 old_virtuals = TREE_CHAIN (old_virtuals))
2896 update_vtable_entry_for_fn (t,
2897 binfo,
2898 BV_FN (old_virtuals),
2899 &virtuals, ix);
2901 return NULL_TREE;
2904 /* Update all of the primary and secondary vtables for T. Create new
2905 vtables as required, and initialize their RTTI information. Each
2906 of the functions in VIRTUALS is declared in T and may override a
2907 virtual function from a base class; find and modify the appropriate
2908 entries to point to the overriding functions. Returns a list, in
2909 declaration order, of the virtual functions that are declared in T,
2910 but do not appear in the primary base class vtable, and which
2911 should therefore be appended to the end of the vtable for T. */
2913 static tree
2914 modify_all_vtables (tree t, tree virtuals)
2916 tree binfo = TYPE_BINFO (t);
2917 tree *fnsp;
2919 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2920 if (TYPE_CONTAINS_VPTR_P (t))
2921 get_vtable_decl (t, false);
2923 /* Update all of the vtables. */
2924 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2926 /* Add virtual functions not already in our primary vtable. These
2927 will be both those introduced by this class, and those overridden
2928 from secondary bases. It does not include virtuals merely
2929 inherited from secondary bases. */
2930 for (fnsp = &virtuals; *fnsp; )
2932 tree fn = TREE_VALUE (*fnsp);
2934 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2935 || DECL_VINDEX (fn) == error_mark_node)
2937 /* We don't need to adjust the `this' pointer when
2938 calling this function. */
2939 BV_DELTA (*fnsp) = integer_zero_node;
2940 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2942 /* This is a function not already in our vtable. Keep it. */
2943 fnsp = &TREE_CHAIN (*fnsp);
2945 else
2946 /* We've already got an entry for this function. Skip it. */
2947 *fnsp = TREE_CHAIN (*fnsp);
2950 return virtuals;
2953 /* Get the base virtual function declarations in T that have the
2954 indicated NAME. */
2956 static void
2957 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2959 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2960 int i;
2962 /* Find virtual functions in T with the indicated NAME. */
2963 i = lookup_fnfields_1 (t, name);
2964 bool found_decls = false;
2965 if (i != -1)
2966 for (ovl_iterator iter ((*CLASSTYPE_METHOD_VEC (t))[i]); iter; ++iter)
2968 tree method = *iter;
2970 if (TREE_CODE (method) == FUNCTION_DECL
2971 && DECL_VINDEX (method))
2973 base_fndecls->safe_push (method);
2974 found_decls = true;
2978 if (found_decls)
2979 return;
2981 for (i = 0; i < n_baseclasses; i++)
2983 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2984 get_basefndecls (name, basetype, base_fndecls);
2988 /* If this declaration supersedes the declaration of
2989 a method declared virtual in the base class, then
2990 mark this field as being virtual as well. */
2992 void
2993 check_for_override (tree decl, tree ctype)
2995 bool overrides_found = false;
2996 if (TREE_CODE (decl) == TEMPLATE_DECL)
2997 /* In [temp.mem] we have:
2999 A specialization of a member function template does not
3000 override a virtual function from a base class. */
3001 return;
3002 if ((DECL_DESTRUCTOR_P (decl)
3003 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
3004 || DECL_CONV_FN_P (decl))
3005 && look_for_overrides (ctype, decl)
3006 && !DECL_STATIC_FUNCTION_P (decl))
3007 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3008 the error_mark_node so that we know it is an overriding
3009 function. */
3011 DECL_VINDEX (decl) = decl;
3012 overrides_found = true;
3013 if (warn_override && !DECL_OVERRIDE_P (decl)
3014 && !DECL_DESTRUCTOR_P (decl))
3015 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
3016 "%qD can be marked override", decl);
3019 if (DECL_VIRTUAL_P (decl))
3021 if (!DECL_VINDEX (decl))
3022 DECL_VINDEX (decl) = error_mark_node;
3023 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3024 if (DECL_DESTRUCTOR_P (decl))
3025 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3027 else if (DECL_FINAL_P (decl))
3028 error ("%q+#D marked %<final%>, but is not virtual", decl);
3029 if (DECL_OVERRIDE_P (decl) && !overrides_found)
3030 error ("%q+#D marked %<override%>, but does not override", decl);
3033 /* Warn about hidden virtual functions that are not overridden in t.
3034 We know that constructors and destructors don't apply. */
3036 static void
3037 warn_hidden (tree t)
3039 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
3040 tree fns;
3041 size_t i;
3043 /* We go through each separately named virtual function. */
3044 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
3045 vec_safe_iterate (method_vec, i, &fns);
3046 ++i)
3048 tree fndecl;
3049 tree base_binfo;
3050 tree binfo;
3051 int j;
3053 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
3054 have the same name. Figure out what name that is. */
3055 tree name = OVL_NAME (fns);
3056 /* There are no possibly hidden functions yet. */
3057 auto_vec<tree, 20> base_fndecls;
3058 /* Iterate through all of the base classes looking for possibly
3059 hidden functions. */
3060 for (binfo = TYPE_BINFO (t), j = 0;
3061 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3063 tree basetype = BINFO_TYPE (base_binfo);
3064 get_basefndecls (name, basetype, &base_fndecls);
3067 /* If there are no functions to hide, continue. */
3068 if (base_fndecls.is_empty ())
3069 continue;
3071 /* Remove any overridden functions. */
3072 for (ovl_iterator iter (fns); iter; ++iter)
3074 fndecl = *iter;
3075 if (TREE_CODE (fndecl) == FUNCTION_DECL
3076 && DECL_VINDEX (fndecl))
3078 /* If the method from the base class has the same
3079 signature as the method from the derived class, it
3080 has been overridden. */
3081 for (size_t k = 0; k < base_fndecls.length (); k++)
3082 if (base_fndecls[k]
3083 && same_signature_p (fndecl, base_fndecls[k]))
3084 base_fndecls[k] = NULL_TREE;
3088 /* Now give a warning for all base functions without overriders,
3089 as they are hidden. */
3090 size_t k;
3091 tree base_fndecl;
3092 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3093 if (base_fndecl)
3095 /* Here we know it is a hider, and no overrider exists. */
3096 warning_at (location_of (base_fndecl),
3097 OPT_Woverloaded_virtual,
3098 "%qD was hidden", base_fndecl);
3099 warning_at (location_of (fns),
3100 OPT_Woverloaded_virtual, " by %qD", fns);
3105 /* Recursive helper for finish_struct_anon. */
3107 static void
3108 finish_struct_anon_r (tree field, bool complain)
3110 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3111 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3112 for (; elt; elt = DECL_CHAIN (elt))
3114 /* We're generally only interested in entities the user
3115 declared, but we also find nested classes by noticing
3116 the TYPE_DECL that we create implicitly. You're
3117 allowed to put one anonymous union inside another,
3118 though, so we explicitly tolerate that. We use
3119 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3120 we also allow unnamed types used for defining fields. */
3121 if (DECL_ARTIFICIAL (elt)
3122 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3123 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3124 continue;
3126 if (TREE_CODE (elt) != FIELD_DECL)
3128 /* We already complained about static data members in
3129 finish_static_data_member_decl. */
3130 if (complain && !VAR_P (elt))
3132 if (is_union)
3133 permerror (DECL_SOURCE_LOCATION (elt),
3134 "%q#D invalid; an anonymous union can "
3135 "only have non-static data members", elt);
3136 else
3137 permerror (DECL_SOURCE_LOCATION (elt),
3138 "%q#D invalid; an anonymous struct can "
3139 "only have non-static data members", elt);
3141 continue;
3144 if (complain)
3146 if (TREE_PRIVATE (elt))
3148 if (is_union)
3149 permerror (DECL_SOURCE_LOCATION (elt),
3150 "private member %q#D in anonymous union", elt);
3151 else
3152 permerror (DECL_SOURCE_LOCATION (elt),
3153 "private member %q#D in anonymous struct", elt);
3155 else if (TREE_PROTECTED (elt))
3157 if (is_union)
3158 permerror (DECL_SOURCE_LOCATION (elt),
3159 "protected member %q#D in anonymous union", elt);
3160 else
3161 permerror (DECL_SOURCE_LOCATION (elt),
3162 "protected member %q#D in anonymous struct", elt);
3166 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3167 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3169 /* Recurse into the anonymous aggregates to handle correctly
3170 access control (c++/24926):
3172 class A {
3173 union {
3174 union {
3175 int i;
3180 int j=A().i; */
3181 if (DECL_NAME (elt) == NULL_TREE
3182 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3183 finish_struct_anon_r (elt, /*complain=*/false);
3187 /* Check for things that are invalid. There are probably plenty of other
3188 things we should check for also. */
3190 static void
3191 finish_struct_anon (tree t)
3193 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3195 if (TREE_STATIC (field))
3196 continue;
3197 if (TREE_CODE (field) != FIELD_DECL)
3198 continue;
3200 if (DECL_NAME (field) == NULL_TREE
3201 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3202 finish_struct_anon_r (field, /*complain=*/true);
3206 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3207 will be used later during class template instantiation.
3208 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3209 a non-static member data (FIELD_DECL), a member function
3210 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3211 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3212 When FRIEND_P is nonzero, T is either a friend class
3213 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3214 (FUNCTION_DECL, TEMPLATE_DECL). */
3216 void
3217 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3219 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3220 if (CLASSTYPE_TEMPLATE_INFO (type))
3221 CLASSTYPE_DECL_LIST (type)
3222 = tree_cons (friend_p ? NULL_TREE : type,
3223 t, CLASSTYPE_DECL_LIST (type));
3226 /* This function is called from declare_virt_assop_and_dtor via
3227 dfs_walk_all.
3229 DATA is a type that direcly or indirectly inherits the base
3230 represented by BINFO. If BINFO contains a virtual assignment [copy
3231 assignment or move assigment] operator or a virtual constructor,
3232 declare that function in DATA if it hasn't been already declared. */
3234 static tree
3235 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3237 tree bv, fn, t = (tree)data;
3238 tree opname = cp_assignment_operator_id (NOP_EXPR);
3240 gcc_assert (t && CLASS_TYPE_P (t));
3241 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3243 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3244 /* A base without a vtable needs no modification, and its bases
3245 are uninteresting. */
3246 return dfs_skip_bases;
3248 if (BINFO_PRIMARY_P (binfo))
3249 /* If this is a primary base, then we have already looked at the
3250 virtual functions of its vtable. */
3251 return NULL_TREE;
3253 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3255 fn = BV_FN (bv);
3257 if (DECL_NAME (fn) == opname)
3259 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3260 lazily_declare_fn (sfk_copy_assignment, t);
3261 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3262 lazily_declare_fn (sfk_move_assignment, t);
3264 else if (DECL_DESTRUCTOR_P (fn)
3265 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3266 lazily_declare_fn (sfk_destructor, t);
3269 return NULL_TREE;
3272 /* If the class type T has a direct or indirect base that contains a
3273 virtual assignment operator or a virtual destructor, declare that
3274 function in T if it hasn't been already declared. */
3276 static void
3277 declare_virt_assop_and_dtor (tree t)
3279 if (!(TYPE_POLYMORPHIC_P (t)
3280 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3281 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3282 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3283 return;
3285 dfs_walk_all (TYPE_BINFO (t),
3286 dfs_declare_virt_assop_and_dtor,
3287 NULL, t);
3290 /* Declare the inheriting constructor for class T inherited from base
3291 constructor CTOR with the parameter array PARMS of size NPARMS. */
3293 static void
3294 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3296 /* We don't declare an inheriting ctor that would be a default,
3297 copy or move ctor for derived or base. */
3298 if (nparms == 0)
3299 return;
3300 if (nparms == 1
3301 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3303 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3304 if (parm == t || parm == DECL_CONTEXT (ctor))
3305 return;
3308 tree parmlist = void_list_node;
3309 for (int i = nparms - 1; i >= 0; i--)
3310 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3311 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3312 t, false, ctor, parmlist);
3313 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3314 if (add_method (t, fn, false))
3316 DECL_CHAIN (fn) = TYPE_METHODS (t);
3317 TYPE_METHODS (t) = fn;
3321 /* Declare all the inheriting constructors for class T inherited from base
3322 constructor CTOR. */
3324 static void
3325 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3327 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3329 if (flag_new_inheriting_ctors)
3331 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3332 t, /*const*/false, ctor, parms);
3333 add_method (t, ctor, using_decl != NULL_TREE);
3334 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3335 return;
3338 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3339 int i = 0;
3340 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3342 if (TREE_PURPOSE (parms))
3343 one_inheriting_sig (t, ctor, new_parms, i);
3344 new_parms[i++] = TREE_VALUE (parms);
3346 one_inheriting_sig (t, ctor, new_parms, i);
3347 if (parms == NULL_TREE)
3349 if (warning (OPT_Winherited_variadic_ctor,
3350 "the ellipsis in %qD is not inherited", ctor))
3351 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3355 /* Create default constructors, assignment operators, and so forth for
3356 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3357 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3358 the class cannot have a default constructor, copy constructor
3359 taking a const reference argument, or an assignment operator taking
3360 a const reference, respectively. */
3362 static void
3363 add_implicitly_declared_members (tree t, tree* access_decls,
3364 int cant_have_const_cctor,
3365 int cant_have_const_assignment)
3367 bool move_ok = false;
3369 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3370 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3371 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3372 move_ok = true;
3374 /* Destructor. */
3375 if (!CLASSTYPE_DESTRUCTORS (t))
3376 /* In general, we create destructors lazily. */
3377 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3379 /* [class.ctor]
3381 If there is no user-declared constructor for a class, a default
3382 constructor is implicitly declared. */
3383 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3385 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3386 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3387 if (cxx_dialect >= cxx11)
3388 TYPE_HAS_CONSTEXPR_CTOR (t)
3389 /* Don't force the declaration to get a hard answer; if the
3390 definition would have made the class non-literal, it will still be
3391 non-literal because of the base or member in question, and that
3392 gives a better diagnostic. */
3393 = type_maybe_constexpr_default_constructor (t);
3396 /* [class.ctor]
3398 If a class definition does not explicitly declare a copy
3399 constructor, one is declared implicitly. */
3400 if (! TYPE_HAS_COPY_CTOR (t))
3402 TYPE_HAS_COPY_CTOR (t) = 1;
3403 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3404 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3405 if (move_ok)
3406 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3409 /* If there is no assignment operator, one will be created if and
3410 when it is needed. For now, just record whether or not the type
3411 of the parameter to the assignment operator will be a const or
3412 non-const reference. */
3413 if (!TYPE_HAS_COPY_ASSIGN (t))
3415 TYPE_HAS_COPY_ASSIGN (t) = 1;
3416 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3417 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3418 if (move_ok && !LAMBDA_TYPE_P (t))
3419 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3422 /* We can't be lazy about declaring functions that might override
3423 a virtual function from a base class. */
3424 declare_virt_assop_and_dtor (t);
3426 while (*access_decls)
3428 tree using_decl = TREE_VALUE (*access_decls);
3429 tree decl = USING_DECL_DECLS (using_decl);
3430 if (DECL_NAME (using_decl) == ctor_identifier)
3432 /* declare, then remove the decl */
3433 tree ctor_list = decl;
3434 location_t loc = input_location;
3435 input_location = DECL_SOURCE_LOCATION (using_decl);
3436 for (ovl_iterator iter (ctor_list); iter; ++iter)
3437 one_inherited_ctor (*iter, t, using_decl);
3438 *access_decls = TREE_CHAIN (*access_decls);
3439 input_location = loc;
3441 else
3442 access_decls = &TREE_CHAIN (*access_decls);
3446 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3447 count the number of fields in TYPE, including anonymous union
3448 members. */
3450 static int
3451 count_fields (tree fields)
3453 tree x;
3454 int n_fields = 0;
3455 for (x = fields; x; x = DECL_CHAIN (x))
3457 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3458 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3459 else
3460 n_fields += 1;
3462 return n_fields;
3465 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3466 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3467 elts, starting at offset IDX. */
3469 static int
3470 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3472 tree x;
3473 for (x = fields; x; x = DECL_CHAIN (x))
3475 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3476 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3477 else
3478 field_vec->elts[idx++] = x;
3480 return idx;
3483 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3484 starting at offset IDX. */
3486 static int
3487 add_enum_fields_to_record_type (tree enumtype,
3488 struct sorted_fields_type *field_vec,
3489 int idx)
3491 tree values;
3492 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3493 field_vec->elts[idx++] = TREE_VALUE (values);
3494 return idx;
3497 /* FIELD is a bit-field. We are finishing the processing for its
3498 enclosing type. Issue any appropriate messages and set appropriate
3499 flags. Returns false if an error has been diagnosed. */
3501 static bool
3502 check_bitfield_decl (tree field)
3504 tree type = TREE_TYPE (field);
3505 tree w;
3507 /* Extract the declared width of the bitfield, which has been
3508 temporarily stashed in DECL_INITIAL. */
3509 w = DECL_INITIAL (field);
3510 gcc_assert (w != NULL_TREE);
3511 /* Remove the bit-field width indicator so that the rest of the
3512 compiler does not treat that value as an initializer. */
3513 DECL_INITIAL (field) = NULL_TREE;
3515 /* Detect invalid bit-field type. */
3516 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3518 error ("bit-field %q+#D with non-integral type", field);
3519 w = error_mark_node;
3521 else
3523 location_t loc = input_location;
3524 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3525 STRIP_NOPS (w);
3527 /* detect invalid field size. */
3528 input_location = DECL_SOURCE_LOCATION (field);
3529 w = cxx_constant_value (w);
3530 input_location = loc;
3532 if (TREE_CODE (w) != INTEGER_CST)
3534 error ("bit-field %q+D width not an integer constant", field);
3535 w = error_mark_node;
3537 else if (tree_int_cst_sgn (w) < 0)
3539 error ("negative width in bit-field %q+D", field);
3540 w = error_mark_node;
3542 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3544 error ("zero width for bit-field %q+D", field);
3545 w = error_mark_node;
3547 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3548 && TREE_CODE (type) != BOOLEAN_TYPE
3549 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3550 || ((TREE_CODE (type) == ENUMERAL_TYPE
3551 || TREE_CODE (type) == BOOLEAN_TYPE)
3552 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3553 warning_at (DECL_SOURCE_LOCATION (field), 0,
3554 "width of %qD exceeds its type", field);
3555 else if (TREE_CODE (type) == ENUMERAL_TYPE
3556 && (0 > (compare_tree_int
3557 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3558 warning_at (DECL_SOURCE_LOCATION (field), 0,
3559 "%qD is too small to hold all values of %q#T",
3560 field, type);
3563 if (w != error_mark_node)
3565 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3566 DECL_BIT_FIELD (field) = 1;
3567 return true;
3569 else
3571 /* Non-bit-fields are aligned for their type. */
3572 DECL_BIT_FIELD (field) = 0;
3573 CLEAR_DECL_C_BIT_FIELD (field);
3574 return false;
3578 /* FIELD is a non bit-field. We are finishing the processing for its
3579 enclosing type T. Issue any appropriate messages and set appropriate
3580 flags. */
3582 static bool
3583 check_field_decl (tree field,
3584 tree t,
3585 int* cant_have_const_ctor,
3586 int* no_const_asn_ref)
3588 tree type = strip_array_types (TREE_TYPE (field));
3589 bool any_default_members = false;
3591 /* In C++98 an anonymous union cannot contain any fields which would change
3592 the settings of CANT_HAVE_CONST_CTOR and friends. */
3593 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3595 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3596 structs. So, we recurse through their fields here. */
3597 else if (ANON_AGGR_TYPE_P (type))
3599 for (tree fields = TYPE_FIELDS (type); fields;
3600 fields = DECL_CHAIN (fields))
3601 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3602 any_default_members |= check_field_decl (fields, t,
3603 cant_have_const_ctor,
3604 no_const_asn_ref);
3606 /* Check members with class type for constructors, destructors,
3607 etc. */
3608 else if (CLASS_TYPE_P (type))
3610 /* Never let anything with uninheritable virtuals
3611 make it through without complaint. */
3612 abstract_virtuals_error (field, type);
3614 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3616 static bool warned;
3617 int oldcount = errorcount;
3618 if (TYPE_NEEDS_CONSTRUCTING (type))
3619 error ("member %q+#D with constructor not allowed in union",
3620 field);
3621 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3622 error ("member %q+#D with destructor not allowed in union", field);
3623 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3624 error ("member %q+#D with copy assignment operator not allowed in union",
3625 field);
3626 if (!warned && errorcount > oldcount)
3628 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3629 "only available with -std=c++11 or -std=gnu++11");
3630 warned = true;
3633 else
3635 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3636 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3637 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3638 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3639 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3640 || !TYPE_HAS_COPY_ASSIGN (type));
3641 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3642 || !TYPE_HAS_COPY_CTOR (type));
3643 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3644 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3645 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3646 || TYPE_HAS_COMPLEX_DFLT (type));
3649 if (TYPE_HAS_COPY_CTOR (type)
3650 && !TYPE_HAS_CONST_COPY_CTOR (type))
3651 *cant_have_const_ctor = 1;
3653 if (TYPE_HAS_COPY_ASSIGN (type)
3654 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3655 *no_const_asn_ref = 1;
3658 check_abi_tags (t, field);
3660 if (DECL_INITIAL (field) != NULL_TREE)
3661 /* `build_class_init_list' does not recognize
3662 non-FIELD_DECLs. */
3663 any_default_members = true;
3665 return any_default_members;
3668 /* Check the data members (both static and non-static), class-scoped
3669 typedefs, etc., appearing in the declaration of T. Issue
3670 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3671 declaration order) of access declarations; each TREE_VALUE in this
3672 list is a USING_DECL.
3674 In addition, set the following flags:
3676 EMPTY_P
3677 The class is empty, i.e., contains no non-static data members.
3679 CANT_HAVE_CONST_CTOR_P
3680 This class cannot have an implicitly generated copy constructor
3681 taking a const reference.
3683 CANT_HAVE_CONST_ASN_REF
3684 This class cannot have an implicitly generated assignment
3685 operator taking a const reference.
3687 All of these flags should be initialized before calling this
3688 function.
3690 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3691 fields can be added by adding to this chain. */
3693 static void
3694 check_field_decls (tree t, tree *access_decls,
3695 int *cant_have_const_ctor_p,
3696 int *no_const_asn_ref_p)
3698 tree *field;
3699 tree *next;
3700 bool has_pointers;
3701 bool any_default_members;
3702 int cant_pack = 0;
3703 int field_access = -1;
3705 /* Assume there are no access declarations. */
3706 *access_decls = NULL_TREE;
3707 /* Assume this class has no pointer members. */
3708 has_pointers = false;
3709 /* Assume none of the members of this class have default
3710 initializations. */
3711 any_default_members = false;
3713 for (field = &TYPE_FIELDS (t); *field; field = next)
3715 tree x = *field;
3716 tree type = TREE_TYPE (x);
3717 int this_field_access;
3719 next = &DECL_CHAIN (x);
3721 if (TREE_CODE (x) == USING_DECL)
3723 /* Save the access declarations for our caller. */
3724 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3725 continue;
3728 if (TREE_CODE (x) == TYPE_DECL
3729 || TREE_CODE (x) == TEMPLATE_DECL)
3730 continue;
3732 /* If we've gotten this far, it's a data member, possibly static,
3733 or an enumerator. */
3734 if (TREE_CODE (x) != CONST_DECL)
3735 DECL_CONTEXT (x) = t;
3737 /* When this goes into scope, it will be a non-local reference. */
3738 DECL_NONLOCAL (x) = 1;
3740 if (TREE_CODE (t) == UNION_TYPE)
3742 /* [class.union] (C++98)
3744 If a union contains a static data member, or a member of
3745 reference type, the program is ill-formed.
3747 In C++11 [class.union] says:
3748 If a union contains a non-static data member of reference type
3749 the program is ill-formed. */
3750 if (VAR_P (x) && cxx_dialect < cxx11)
3752 error ("in C++98 %q+D may not be static because it is "
3753 "a member of a union", x);
3754 continue;
3756 if (TREE_CODE (type) == REFERENCE_TYPE
3757 && TREE_CODE (x) == FIELD_DECL)
3759 error ("non-static data member %q+D in a union may not "
3760 "have reference type %qT", x, type);
3761 continue;
3765 /* Perform error checking that did not get done in
3766 grokdeclarator. */
3767 if (TREE_CODE (type) == FUNCTION_TYPE)
3769 error ("field %q+D invalidly declared function type", x);
3770 type = build_pointer_type (type);
3771 TREE_TYPE (x) = type;
3773 else if (TREE_CODE (type) == METHOD_TYPE)
3775 error ("field %q+D invalidly declared method type", x);
3776 type = build_pointer_type (type);
3777 TREE_TYPE (x) = type;
3780 if (type == error_mark_node)
3781 continue;
3783 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3784 continue;
3786 /* Now it can only be a FIELD_DECL. */
3788 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3789 CLASSTYPE_NON_AGGREGATE (t) = 1;
3791 /* If at least one non-static data member is non-literal, the whole
3792 class becomes non-literal. Per Core/1453, volatile non-static
3793 data members and base classes are also not allowed.
3794 Note: if the type is incomplete we will complain later on. */
3795 if (COMPLETE_TYPE_P (type)
3796 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3797 CLASSTYPE_LITERAL_P (t) = false;
3799 /* A standard-layout class is a class that:
3801 has the same access control (Clause 11) for all non-static data members,
3802 ... */
3803 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3804 if (field_access == -1)
3805 field_access = this_field_access;
3806 else if (this_field_access != field_access)
3807 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3809 /* If this is of reference type, check if it needs an init. */
3810 if (TREE_CODE (type) == REFERENCE_TYPE)
3812 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3813 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3814 if (DECL_INITIAL (x) == NULL_TREE)
3815 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3816 if (cxx_dialect < cxx11)
3818 /* ARM $12.6.2: [A member initializer list] (or, for an
3819 aggregate, initialization by a brace-enclosed list) is the
3820 only way to initialize nonstatic const and reference
3821 members. */
3822 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3823 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3827 type = strip_array_types (type);
3829 if (TYPE_PACKED (t))
3831 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3833 warning_at
3834 (DECL_SOURCE_LOCATION (x), 0,
3835 "ignoring packed attribute because of unpacked non-POD field %q#D",
3837 cant_pack = 1;
3839 else if (DECL_C_BIT_FIELD (x)
3840 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3841 DECL_PACKED (x) = 1;
3844 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3845 /* We don't treat zero-width bitfields as making a class
3846 non-empty. */
3848 else
3850 /* The class is non-empty. */
3851 CLASSTYPE_EMPTY_P (t) = 0;
3852 /* The class is not even nearly empty. */
3853 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3854 /* If one of the data members contains an empty class,
3855 so does T. */
3856 if (CLASS_TYPE_P (type)
3857 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3858 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3861 /* This is used by -Weffc++ (see below). Warn only for pointers
3862 to members which might hold dynamic memory. So do not warn
3863 for pointers to functions or pointers to members. */
3864 if (TYPE_PTR_P (type)
3865 && !TYPE_PTRFN_P (type))
3866 has_pointers = true;
3868 if (CLASS_TYPE_P (type))
3870 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3871 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3872 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3873 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3876 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3877 CLASSTYPE_HAS_MUTABLE (t) = 1;
3879 if (DECL_MUTABLE_P (x))
3881 if (CP_TYPE_CONST_P (type))
3883 error ("member %q+D cannot be declared both %<const%> "
3884 "and %<mutable%>", x);
3885 continue;
3887 if (TREE_CODE (type) == REFERENCE_TYPE)
3889 error ("member %q+D cannot be declared as a %<mutable%> "
3890 "reference", x);
3891 continue;
3895 if (! layout_pod_type_p (type))
3896 /* DR 148 now allows pointers to members (which are POD themselves),
3897 to be allowed in POD structs. */
3898 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3900 if (!std_layout_type_p (type))
3901 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3903 if (! zero_init_p (type))
3904 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3906 /* We set DECL_C_BIT_FIELD in grokbitfield.
3907 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3908 if ((! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3909 && check_field_decl (x, t,
3910 cant_have_const_ctor_p,
3911 no_const_asn_ref_p))
3913 if (any_default_members
3914 && TREE_CODE (t) == UNION_TYPE)
3915 error ("multiple fields in union %qT initialized", t);
3916 any_default_members = true;
3919 /* Now that we've removed bit-field widths from DECL_INITIAL,
3920 anything left in DECL_INITIAL is an NSDMI that makes the class
3921 non-aggregate in C++11. */
3922 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3923 CLASSTYPE_NON_AGGREGATE (t) = true;
3925 /* If any field is const, the structure type is pseudo-const. */
3926 if (CP_TYPE_CONST_P (type))
3928 C_TYPE_FIELDS_READONLY (t) = 1;
3929 if (DECL_INITIAL (x) == NULL_TREE)
3930 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3931 if (cxx_dialect < cxx11)
3933 /* ARM $12.6.2: [A member initializer list] (or, for an
3934 aggregate, initialization by a brace-enclosed list) is the
3935 only way to initialize nonstatic const and reference
3936 members. */
3937 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3938 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3941 /* A field that is pseudo-const makes the structure likewise. */
3942 else if (CLASS_TYPE_P (type))
3944 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3945 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3946 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3947 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3950 /* Core issue 80: A nonstatic data member is required to have a
3951 different name from the class iff the class has a
3952 user-declared constructor. */
3953 if (constructor_name_p (DECL_NAME (x), t)
3954 && TYPE_HAS_USER_CONSTRUCTOR (t))
3955 permerror (DECL_SOURCE_LOCATION (x),
3956 "field %q#D with same name as class", x);
3959 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3960 it should also define a copy constructor and an assignment operator to
3961 implement the correct copy semantic (deep vs shallow, etc.). As it is
3962 not feasible to check whether the constructors do allocate dynamic memory
3963 and store it within members, we approximate the warning like this:
3965 -- Warn only if there are members which are pointers
3966 -- Warn only if there is a non-trivial constructor (otherwise,
3967 there cannot be memory allocated).
3968 -- Warn only if there is a non-trivial destructor. We assume that the
3969 user at least implemented the cleanup correctly, and a destructor
3970 is needed to free dynamic memory.
3972 This seems enough for practical purposes. */
3973 if (warn_ecpp
3974 && has_pointers
3975 && TYPE_HAS_USER_CONSTRUCTOR (t)
3976 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3977 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3979 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3981 if (! TYPE_HAS_COPY_CTOR (t))
3983 warning (OPT_Weffc__,
3984 " but does not override %<%T(const %T&)%>", t, t);
3985 if (!TYPE_HAS_COPY_ASSIGN (t))
3986 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3988 else if (! TYPE_HAS_COPY_ASSIGN (t))
3989 warning (OPT_Weffc__,
3990 " but does not override %<operator=(const %T&)%>", t);
3993 /* Non-static data member initializers make the default constructor
3994 non-trivial. */
3995 if (any_default_members)
3997 TYPE_NEEDS_CONSTRUCTING (t) = true;
3998 TYPE_HAS_COMPLEX_DFLT (t) = true;
4001 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4002 if (cant_pack)
4003 TYPE_PACKED (t) = 0;
4005 /* Check anonymous struct/anonymous union fields. */
4006 finish_struct_anon (t);
4008 /* We've built up the list of access declarations in reverse order.
4009 Fix that now. */
4010 *access_decls = nreverse (*access_decls);
4013 /* If TYPE is an empty class type, records its OFFSET in the table of
4014 OFFSETS. */
4016 static int
4017 record_subobject_offset (tree type, tree offset, splay_tree offsets)
4019 splay_tree_node n;
4021 if (!is_empty_class (type))
4022 return 0;
4024 /* Record the location of this empty object in OFFSETS. */
4025 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4026 if (!n)
4027 n = splay_tree_insert (offsets,
4028 (splay_tree_key) offset,
4029 (splay_tree_value) NULL_TREE);
4030 n->value = ((splay_tree_value)
4031 tree_cons (NULL_TREE,
4032 type,
4033 (tree) n->value));
4035 return 0;
4038 /* Returns nonzero if TYPE is an empty class type and there is
4039 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4041 static int
4042 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4044 splay_tree_node n;
4045 tree t;
4047 if (!is_empty_class (type))
4048 return 0;
4050 /* Record the location of this empty object in OFFSETS. */
4051 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4052 if (!n)
4053 return 0;
4055 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4056 if (same_type_p (TREE_VALUE (t), type))
4057 return 1;
4059 return 0;
4062 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4063 F for every subobject, passing it the type, offset, and table of
4064 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4065 be traversed.
4067 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4068 than MAX_OFFSET will not be walked.
4070 If F returns a nonzero value, the traversal ceases, and that value
4071 is returned. Otherwise, returns zero. */
4073 static int
4074 walk_subobject_offsets (tree type,
4075 subobject_offset_fn f,
4076 tree offset,
4077 splay_tree offsets,
4078 tree max_offset,
4079 int vbases_p)
4081 int r = 0;
4082 tree type_binfo = NULL_TREE;
4084 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4085 stop. */
4086 if (max_offset && tree_int_cst_lt (max_offset, offset))
4087 return 0;
4089 if (type == error_mark_node)
4090 return 0;
4092 if (!TYPE_P (type))
4094 type_binfo = type;
4095 type = BINFO_TYPE (type);
4098 if (CLASS_TYPE_P (type))
4100 tree field;
4101 tree binfo;
4102 int i;
4104 /* Avoid recursing into objects that are not interesting. */
4105 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4106 return 0;
4108 /* Record the location of TYPE. */
4109 r = (*f) (type, offset, offsets);
4110 if (r)
4111 return r;
4113 /* Iterate through the direct base classes of TYPE. */
4114 if (!type_binfo)
4115 type_binfo = TYPE_BINFO (type);
4116 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4118 tree binfo_offset;
4120 if (BINFO_VIRTUAL_P (binfo))
4121 continue;
4123 tree orig_binfo;
4124 /* We cannot rely on BINFO_OFFSET being set for the base
4125 class yet, but the offsets for direct non-virtual
4126 bases can be calculated by going back to the TYPE. */
4127 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4128 binfo_offset = size_binop (PLUS_EXPR,
4129 offset,
4130 BINFO_OFFSET (orig_binfo));
4132 r = walk_subobject_offsets (binfo,
4134 binfo_offset,
4135 offsets,
4136 max_offset,
4137 /*vbases_p=*/0);
4138 if (r)
4139 return r;
4142 if (CLASSTYPE_VBASECLASSES (type))
4144 unsigned ix;
4145 vec<tree, va_gc> *vbases;
4147 /* Iterate through the virtual base classes of TYPE. In G++
4148 3.2, we included virtual bases in the direct base class
4149 loop above, which results in incorrect results; the
4150 correct offsets for virtual bases are only known when
4151 working with the most derived type. */
4152 if (vbases_p)
4153 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4154 vec_safe_iterate (vbases, ix, &binfo); ix++)
4156 r = walk_subobject_offsets (binfo,
4158 size_binop (PLUS_EXPR,
4159 offset,
4160 BINFO_OFFSET (binfo)),
4161 offsets,
4162 max_offset,
4163 /*vbases_p=*/0);
4164 if (r)
4165 return r;
4167 else
4169 /* We still have to walk the primary base, if it is
4170 virtual. (If it is non-virtual, then it was walked
4171 above.) */
4172 tree vbase = get_primary_binfo (type_binfo);
4174 if (vbase && BINFO_VIRTUAL_P (vbase)
4175 && BINFO_PRIMARY_P (vbase)
4176 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4178 r = (walk_subobject_offsets
4179 (vbase, f, offset,
4180 offsets, max_offset, /*vbases_p=*/0));
4181 if (r)
4182 return r;
4187 /* Iterate through the fields of TYPE. */
4188 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4189 if (TREE_CODE (field) == FIELD_DECL
4190 && TREE_TYPE (field) != error_mark_node
4191 && !DECL_ARTIFICIAL (field))
4193 tree field_offset;
4195 field_offset = byte_position (field);
4197 r = walk_subobject_offsets (TREE_TYPE (field),
4199 size_binop (PLUS_EXPR,
4200 offset,
4201 field_offset),
4202 offsets,
4203 max_offset,
4204 /*vbases_p=*/1);
4205 if (r)
4206 return r;
4209 else if (TREE_CODE (type) == ARRAY_TYPE)
4211 tree element_type = strip_array_types (type);
4212 tree domain = TYPE_DOMAIN (type);
4213 tree index;
4215 /* Avoid recursing into objects that are not interesting. */
4216 if (!CLASS_TYPE_P (element_type)
4217 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4218 || !domain
4219 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4220 return 0;
4222 /* Step through each of the elements in the array. */
4223 for (index = size_zero_node;
4224 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4225 index = size_binop (PLUS_EXPR, index, size_one_node))
4227 r = walk_subobject_offsets (TREE_TYPE (type),
4229 offset,
4230 offsets,
4231 max_offset,
4232 /*vbases_p=*/1);
4233 if (r)
4234 return r;
4235 offset = size_binop (PLUS_EXPR, offset,
4236 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4237 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4238 there's no point in iterating through the remaining
4239 elements of the array. */
4240 if (max_offset && tree_int_cst_lt (max_offset, offset))
4241 break;
4245 return 0;
4248 /* Record all of the empty subobjects of TYPE (either a type or a
4249 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4250 is being placed at OFFSET; otherwise, it is a base class that is
4251 being placed at OFFSET. */
4253 static void
4254 record_subobject_offsets (tree type,
4255 tree offset,
4256 splay_tree offsets,
4257 bool is_data_member)
4259 tree max_offset;
4260 /* If recording subobjects for a non-static data member or a
4261 non-empty base class , we do not need to record offsets beyond
4262 the size of the biggest empty class. Additional data members
4263 will go at the end of the class. Additional base classes will go
4264 either at offset zero (if empty, in which case they cannot
4265 overlap with offsets past the size of the biggest empty class) or
4266 at the end of the class.
4268 However, if we are placing an empty base class, then we must record
4269 all offsets, as either the empty class is at offset zero (where
4270 other empty classes might later be placed) or at the end of the
4271 class (where other objects might then be placed, so other empty
4272 subobjects might later overlap). */
4273 if (is_data_member
4274 || !is_empty_class (BINFO_TYPE (type)))
4275 max_offset = sizeof_biggest_empty_class;
4276 else
4277 max_offset = NULL_TREE;
4278 walk_subobject_offsets (type, record_subobject_offset, offset,
4279 offsets, max_offset, is_data_member);
4282 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4283 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4284 virtual bases of TYPE are examined. */
4286 static int
4287 layout_conflict_p (tree type,
4288 tree offset,
4289 splay_tree offsets,
4290 int vbases_p)
4292 splay_tree_node max_node;
4294 /* Get the node in OFFSETS that indicates the maximum offset where
4295 an empty subobject is located. */
4296 max_node = splay_tree_max (offsets);
4297 /* If there aren't any empty subobjects, then there's no point in
4298 performing this check. */
4299 if (!max_node)
4300 return 0;
4302 return walk_subobject_offsets (type, check_subobject_offset, offset,
4303 offsets, (tree) (max_node->key),
4304 vbases_p);
4307 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4308 non-static data member of the type indicated by RLI. BINFO is the
4309 binfo corresponding to the base subobject, OFFSETS maps offsets to
4310 types already located at those offsets. This function determines
4311 the position of the DECL. */
4313 static void
4314 layout_nonempty_base_or_field (record_layout_info rli,
4315 tree decl,
4316 tree binfo,
4317 splay_tree offsets)
4319 tree offset = NULL_TREE;
4320 bool field_p;
4321 tree type;
4323 if (binfo)
4325 /* For the purposes of determining layout conflicts, we want to
4326 use the class type of BINFO; TREE_TYPE (DECL) will be the
4327 CLASSTYPE_AS_BASE version, which does not contain entries for
4328 zero-sized bases. */
4329 type = TREE_TYPE (binfo);
4330 field_p = false;
4332 else
4334 type = TREE_TYPE (decl);
4335 field_p = true;
4338 /* Try to place the field. It may take more than one try if we have
4339 a hard time placing the field without putting two objects of the
4340 same type at the same address. */
4341 while (1)
4343 struct record_layout_info_s old_rli = *rli;
4345 /* Place this field. */
4346 place_field (rli, decl);
4347 offset = byte_position (decl);
4349 /* We have to check to see whether or not there is already
4350 something of the same type at the offset we're about to use.
4351 For example, consider:
4353 struct S {};
4354 struct T : public S { int i; };
4355 struct U : public S, public T {};
4357 Here, we put S at offset zero in U. Then, we can't put T at
4358 offset zero -- its S component would be at the same address
4359 as the S we already allocated. So, we have to skip ahead.
4360 Since all data members, including those whose type is an
4361 empty class, have nonzero size, any overlap can happen only
4362 with a direct or indirect base-class -- it can't happen with
4363 a data member. */
4364 /* In a union, overlap is permitted; all members are placed at
4365 offset zero. */
4366 if (TREE_CODE (rli->t) == UNION_TYPE)
4367 break;
4368 if (layout_conflict_p (field_p ? type : binfo, offset,
4369 offsets, field_p))
4371 /* Strip off the size allocated to this field. That puts us
4372 at the first place we could have put the field with
4373 proper alignment. */
4374 *rli = old_rli;
4376 /* Bump up by the alignment required for the type. */
4377 rli->bitpos
4378 = size_binop (PLUS_EXPR, rli->bitpos,
4379 bitsize_int (binfo
4380 ? CLASSTYPE_ALIGN (type)
4381 : TYPE_ALIGN (type)));
4382 normalize_rli (rli);
4384 else if (TREE_CODE (type) == NULLPTR_TYPE
4385 && warn_abi && abi_version_crosses (9))
4387 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4388 the offset wasn't aligned like a pointer when we started to
4389 layout this field, that affects its position. */
4390 tree pos = rli_size_unit_so_far (&old_rli);
4391 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4393 if (abi_version_at_least (9))
4394 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4395 "alignment of %qD increased in -fabi-version=9 "
4396 "(GCC 5.2)", decl);
4397 else
4398 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4399 "of %qD will increase in -fabi-version=9", decl);
4401 break;
4403 else
4404 /* There was no conflict. We're done laying out this field. */
4405 break;
4408 /* Now that we know where it will be placed, update its
4409 BINFO_OFFSET. */
4410 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4411 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4412 this point because their BINFO_OFFSET is copied from another
4413 hierarchy. Therefore, we may not need to add the entire
4414 OFFSET. */
4415 propagate_binfo_offsets (binfo,
4416 size_diffop_loc (input_location,
4417 fold_convert (ssizetype, offset),
4418 fold_convert (ssizetype,
4419 BINFO_OFFSET (binfo))));
4422 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4424 static int
4425 empty_base_at_nonzero_offset_p (tree type,
4426 tree offset,
4427 splay_tree /*offsets*/)
4429 return is_empty_class (type) && !integer_zerop (offset);
4432 /* Layout the empty base BINFO. EOC indicates the byte currently just
4433 past the end of the class, and should be correctly aligned for a
4434 class of the type indicated by BINFO; OFFSETS gives the offsets of
4435 the empty bases allocated so far. T is the most derived
4436 type. Return nonzero iff we added it at the end. */
4438 static bool
4439 layout_empty_base (record_layout_info rli, tree binfo,
4440 tree eoc, splay_tree offsets)
4442 tree alignment;
4443 tree basetype = BINFO_TYPE (binfo);
4444 bool atend = false;
4446 /* This routine should only be used for empty classes. */
4447 gcc_assert (is_empty_class (basetype));
4448 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4450 if (!integer_zerop (BINFO_OFFSET (binfo)))
4451 propagate_binfo_offsets
4452 (binfo, size_diffop_loc (input_location,
4453 size_zero_node, BINFO_OFFSET (binfo)));
4455 /* This is an empty base class. We first try to put it at offset
4456 zero. */
4457 if (layout_conflict_p (binfo,
4458 BINFO_OFFSET (binfo),
4459 offsets,
4460 /*vbases_p=*/0))
4462 /* That didn't work. Now, we move forward from the next
4463 available spot in the class. */
4464 atend = true;
4465 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4466 while (1)
4468 if (!layout_conflict_p (binfo,
4469 BINFO_OFFSET (binfo),
4470 offsets,
4471 /*vbases_p=*/0))
4472 /* We finally found a spot where there's no overlap. */
4473 break;
4475 /* There's overlap here, too. Bump along to the next spot. */
4476 propagate_binfo_offsets (binfo, alignment);
4480 if (CLASSTYPE_USER_ALIGN (basetype))
4482 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4483 if (warn_packed)
4484 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4485 TYPE_USER_ALIGN (rli->t) = 1;
4488 return atend;
4491 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4492 fields at NEXT_FIELD, and return it. */
4494 static tree
4495 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4497 /* Create the FIELD_DECL. */
4498 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4499 tree decl = build_decl (input_location,
4500 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4501 DECL_ARTIFICIAL (decl) = 1;
4502 DECL_IGNORED_P (decl) = 1;
4503 DECL_FIELD_CONTEXT (decl) = t;
4504 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4505 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4506 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4507 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4508 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4509 DECL_FIELD_IS_BASE (decl) = 1;
4511 /* Add the new FIELD_DECL to the list of fields for T. */
4512 DECL_CHAIN (decl) = *next_field;
4513 *next_field = decl;
4514 next_field = &DECL_CHAIN (decl);
4516 return decl;
4519 /* Layout the base given by BINFO in the class indicated by RLI.
4520 *BASE_ALIGN is a running maximum of the alignments of
4521 any base class. OFFSETS gives the location of empty base
4522 subobjects. T is the most derived type. Return nonzero if the new
4523 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4524 *NEXT_FIELD, unless BINFO is for an empty base class.
4526 Returns the location at which the next field should be inserted. */
4528 static tree *
4529 build_base_field (record_layout_info rli, tree binfo,
4530 splay_tree offsets, tree *next_field)
4532 tree t = rli->t;
4533 tree basetype = BINFO_TYPE (binfo);
4535 if (!COMPLETE_TYPE_P (basetype))
4536 /* This error is now reported in xref_tag, thus giving better
4537 location information. */
4538 return next_field;
4540 /* Place the base class. */
4541 if (!is_empty_class (basetype))
4543 tree decl;
4545 /* The containing class is non-empty because it has a non-empty
4546 base class. */
4547 CLASSTYPE_EMPTY_P (t) = 0;
4549 /* Create the FIELD_DECL. */
4550 decl = build_base_field_1 (t, basetype, next_field);
4552 /* Try to place the field. It may take more than one try if we
4553 have a hard time placing the field without putting two
4554 objects of the same type at the same address. */
4555 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4557 else
4559 tree eoc;
4560 bool atend;
4562 /* On some platforms (ARM), even empty classes will not be
4563 byte-aligned. */
4564 eoc = round_up_loc (input_location,
4565 rli_size_unit_so_far (rli),
4566 CLASSTYPE_ALIGN_UNIT (basetype));
4567 atend = layout_empty_base (rli, binfo, eoc, offsets);
4568 /* A nearly-empty class "has no proper base class that is empty,
4569 not morally virtual, and at an offset other than zero." */
4570 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4572 if (atend)
4573 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4574 /* The check above (used in G++ 3.2) is insufficient because
4575 an empty class placed at offset zero might itself have an
4576 empty base at a nonzero offset. */
4577 else if (walk_subobject_offsets (basetype,
4578 empty_base_at_nonzero_offset_p,
4579 size_zero_node,
4580 /*offsets=*/NULL,
4581 /*max_offset=*/NULL_TREE,
4582 /*vbases_p=*/true))
4583 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4586 /* We used to not create a FIELD_DECL for empty base classes because of
4587 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4588 be a problem anymore. We need them to handle initialization of C++17
4589 aggregate bases. */
4590 if (cxx_dialect >= cxx1z && !BINFO_VIRTUAL_P (binfo))
4592 tree decl = build_base_field_1 (t, basetype, next_field);
4593 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4594 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4595 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4598 /* An empty virtual base causes a class to be non-empty
4599 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4600 here because that was already done when the virtual table
4601 pointer was created. */
4604 /* Record the offsets of BINFO and its base subobjects. */
4605 record_subobject_offsets (binfo,
4606 BINFO_OFFSET (binfo),
4607 offsets,
4608 /*is_data_member=*/false);
4610 return next_field;
4613 /* Layout all of the non-virtual base classes. Record empty
4614 subobjects in OFFSETS. T is the most derived type. Return nonzero
4615 if the type cannot be nearly empty. The fields created
4616 corresponding to the base classes will be inserted at
4617 *NEXT_FIELD. */
4619 static void
4620 build_base_fields (record_layout_info rli,
4621 splay_tree offsets, tree *next_field)
4623 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4624 subobjects. */
4625 tree t = rli->t;
4626 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4627 int i;
4629 /* The primary base class is always allocated first. */
4630 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4631 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4632 offsets, next_field);
4634 /* Now allocate the rest of the bases. */
4635 for (i = 0; i < n_baseclasses; ++i)
4637 tree base_binfo;
4639 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4641 /* The primary base was already allocated above, so we don't
4642 need to allocate it again here. */
4643 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4644 continue;
4646 /* Virtual bases are added at the end (a primary virtual base
4647 will have already been added). */
4648 if (BINFO_VIRTUAL_P (base_binfo))
4649 continue;
4651 next_field = build_base_field (rli, base_binfo,
4652 offsets, next_field);
4656 /* Go through the TYPE_METHODS of T issuing any appropriate
4657 diagnostics, figuring out which methods override which other
4658 methods, and so forth. */
4660 static void
4661 check_methods (tree t)
4663 tree x;
4665 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4667 check_for_override (x, t);
4668 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4669 error ("initializer specified for non-virtual method %q+D", x);
4670 /* The name of the field is the original field name
4671 Save this in auxiliary field for later overloading. */
4672 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4674 TYPE_POLYMORPHIC_P (t) = 1;
4675 if (DECL_PURE_VIRTUAL_P (x))
4676 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4678 /* All user-provided destructors are non-trivial.
4679 Constructors and assignment ops are handled in
4680 grok_special_member_properties. */
4681 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4682 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4683 if (!DECL_VIRTUAL_P (x)
4684 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x)))
4685 error_at (DECL_SOURCE_LOCATION (x),
4686 "%<transaction_safe_dynamic%> may only be specified for "
4687 "a virtual function");
4691 /* FN is a constructor or destructor. Clone the declaration to create
4692 a specialized in-charge or not-in-charge version, as indicated by
4693 NAME. */
4695 static tree
4696 build_clone (tree fn, tree name)
4698 tree parms;
4699 tree clone;
4701 /* Copy the function. */
4702 clone = copy_decl (fn);
4703 /* Reset the function name. */
4704 DECL_NAME (clone) = name;
4705 /* Remember where this function came from. */
4706 DECL_ABSTRACT_ORIGIN (clone) = fn;
4707 /* Make it easy to find the CLONE given the FN. */
4708 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4709 DECL_CHAIN (fn) = clone;
4711 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4712 if (TREE_CODE (clone) == TEMPLATE_DECL)
4714 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4715 DECL_TEMPLATE_RESULT (clone) = result;
4716 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4717 DECL_TI_TEMPLATE (result) = clone;
4718 TREE_TYPE (clone) = TREE_TYPE (result);
4719 return clone;
4721 else
4723 // Clone constraints.
4724 if (flag_concepts)
4725 if (tree ci = get_constraints (fn))
4726 set_constraints (clone, copy_node (ci));
4730 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4731 DECL_CLONED_FUNCTION (clone) = fn;
4732 /* There's no pending inline data for this function. */
4733 DECL_PENDING_INLINE_INFO (clone) = NULL;
4734 DECL_PENDING_INLINE_P (clone) = 0;
4736 /* The base-class destructor is not virtual. */
4737 if (name == base_dtor_identifier)
4739 DECL_VIRTUAL_P (clone) = 0;
4740 if (TREE_CODE (clone) != TEMPLATE_DECL)
4741 DECL_VINDEX (clone) = NULL_TREE;
4744 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4745 if (ctor_omit_inherited_parms_p)
4746 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4748 /* If there was an in-charge parameter, drop it from the function
4749 type. */
4750 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4752 tree basetype;
4753 tree parmtypes;
4754 tree exceptions;
4756 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4757 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4758 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4759 /* Skip the `this' parameter. */
4760 parmtypes = TREE_CHAIN (parmtypes);
4761 /* Skip the in-charge parameter. */
4762 parmtypes = TREE_CHAIN (parmtypes);
4763 /* And the VTT parm, in a complete [cd]tor. */
4764 if (DECL_HAS_VTT_PARM_P (fn)
4765 && ! DECL_NEEDS_VTT_PARM_P (clone))
4766 parmtypes = TREE_CHAIN (parmtypes);
4767 if (ctor_omit_inherited_parms_p)
4769 /* If we're omitting inherited parms, that just leaves the VTT. */
4770 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4771 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4773 TREE_TYPE (clone)
4774 = build_method_type_directly (basetype,
4775 TREE_TYPE (TREE_TYPE (clone)),
4776 parmtypes);
4777 if (exceptions)
4778 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4779 exceptions);
4780 TREE_TYPE (clone)
4781 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4782 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4785 /* Copy the function parameters. */
4786 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4787 /* Remove the in-charge parameter. */
4788 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4790 DECL_CHAIN (DECL_ARGUMENTS (clone))
4791 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4792 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4794 /* And the VTT parm, in a complete [cd]tor. */
4795 if (DECL_HAS_VTT_PARM_P (fn))
4797 if (DECL_NEEDS_VTT_PARM_P (clone))
4798 DECL_HAS_VTT_PARM_P (clone) = 1;
4799 else
4801 DECL_CHAIN (DECL_ARGUMENTS (clone))
4802 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4803 DECL_HAS_VTT_PARM_P (clone) = 0;
4807 /* A base constructor inheriting from a virtual base doesn't get the
4808 arguments. */
4809 if (ctor_omit_inherited_parms_p)
4810 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4812 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4814 DECL_CONTEXT (parms) = clone;
4815 cxx_dup_lang_specific_decl (parms);
4818 /* Create the RTL for this function. */
4819 SET_DECL_RTL (clone, NULL);
4820 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4822 return clone;
4825 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4826 not invoke this function directly.
4828 For a non-thunk function, returns the address of the slot for storing
4829 the function it is a clone of. Otherwise returns NULL_TREE.
4831 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4832 cloned_function is unset. This is to support the separate
4833 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4834 on a template makes sense, but not the former. */
4836 tree *
4837 decl_cloned_function_p (const_tree decl, bool just_testing)
4839 tree *ptr;
4840 if (just_testing)
4841 decl = STRIP_TEMPLATE (decl);
4843 if (TREE_CODE (decl) != FUNCTION_DECL
4844 || !DECL_LANG_SPECIFIC (decl)
4845 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4847 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4848 if (!just_testing)
4849 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4850 else
4851 #endif
4852 return NULL;
4855 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4856 if (just_testing && *ptr == NULL_TREE)
4857 return NULL;
4858 else
4859 return ptr;
4862 /* Produce declarations for all appropriate clones of FN. If
4863 UPDATE_METHODS is true, the clones are added to the
4864 CLASSTYPE_METHOD_VEC. */
4866 void
4867 clone_function_decl (tree fn, bool update_methods)
4869 tree clone;
4871 /* Avoid inappropriate cloning. */
4872 if (DECL_CHAIN (fn)
4873 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4874 return;
4876 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4878 /* For each constructor, we need two variants: an in-charge version
4879 and a not-in-charge version. */
4880 clone = build_clone (fn, complete_ctor_identifier);
4881 if (update_methods)
4882 add_method (DECL_CONTEXT (clone), clone, false);
4883 clone = build_clone (fn, base_ctor_identifier);
4884 if (update_methods)
4885 add_method (DECL_CONTEXT (clone), clone, false);
4887 else
4889 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4891 /* For each destructor, we need three variants: an in-charge
4892 version, a not-in-charge version, and an in-charge deleting
4893 version. We clone the deleting version first because that
4894 means it will go second on the TYPE_METHODS list -- and that
4895 corresponds to the correct layout order in the virtual
4896 function table.
4898 For a non-virtual destructor, we do not build a deleting
4899 destructor. */
4900 if (DECL_VIRTUAL_P (fn))
4902 clone = build_clone (fn, deleting_dtor_identifier);
4903 if (update_methods)
4904 add_method (DECL_CONTEXT (clone), clone, false);
4906 clone = build_clone (fn, complete_dtor_identifier);
4907 if (update_methods)
4908 add_method (DECL_CONTEXT (clone), clone, false);
4909 clone = build_clone (fn, base_dtor_identifier);
4910 if (update_methods)
4911 add_method (DECL_CONTEXT (clone), clone, false);
4914 /* Note that this is an abstract function that is never emitted. */
4915 DECL_ABSTRACT_P (fn) = true;
4918 /* DECL is an in charge constructor, which is being defined. This will
4919 have had an in class declaration, from whence clones were
4920 declared. An out-of-class definition can specify additional default
4921 arguments. As it is the clones that are involved in overload
4922 resolution, we must propagate the information from the DECL to its
4923 clones. */
4925 void
4926 adjust_clone_args (tree decl)
4928 tree clone;
4930 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4931 clone = DECL_CHAIN (clone))
4933 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4934 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4935 tree decl_parms, clone_parms;
4937 clone_parms = orig_clone_parms;
4939 /* Skip the 'this' parameter. */
4940 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4941 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4943 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4944 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4945 if (DECL_HAS_VTT_PARM_P (decl))
4946 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4948 clone_parms = orig_clone_parms;
4949 if (DECL_HAS_VTT_PARM_P (clone))
4950 clone_parms = TREE_CHAIN (clone_parms);
4952 for (decl_parms = orig_decl_parms; decl_parms;
4953 decl_parms = TREE_CHAIN (decl_parms),
4954 clone_parms = TREE_CHAIN (clone_parms))
4956 if (clone_parms == void_list_node)
4958 gcc_assert (decl_parms == clone_parms
4959 || ctor_omit_inherited_parms (clone));
4960 break;
4963 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4964 TREE_TYPE (clone_parms)));
4966 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4968 /* A default parameter has been added. Adjust the
4969 clone's parameters. */
4970 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4971 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4972 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4973 tree type;
4975 clone_parms = orig_decl_parms;
4977 if (DECL_HAS_VTT_PARM_P (clone))
4979 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4980 TREE_VALUE (orig_clone_parms),
4981 clone_parms);
4982 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4984 type = build_method_type_directly (basetype,
4985 TREE_TYPE (TREE_TYPE (clone)),
4986 clone_parms);
4987 if (exceptions)
4988 type = build_exception_variant (type, exceptions);
4989 if (attrs)
4990 type = cp_build_type_attribute_variant (type, attrs);
4991 TREE_TYPE (clone) = type;
4993 clone_parms = NULL_TREE;
4994 break;
4997 gcc_assert (!clone_parms || clone_parms == void_list_node);
5001 /* For each of the constructors and destructors in T, create an
5002 in-charge and not-in-charge variant. */
5004 static void
5005 clone_constructors_and_destructors (tree t)
5007 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
5008 out now. */
5009 if (!CLASSTYPE_METHOD_VEC (t))
5010 return;
5012 /* While constructors can be via a using declaration, at this point
5013 we no longer need to know that. */
5014 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5015 clone_function_decl (*iter, /*update_methods=*/true);
5016 for (ovl_iterator iter (CLASSTYPE_DESTRUCTORS (t)); iter; ++iter)
5017 clone_function_decl (*iter, /*update_methods=*/true);
5020 /* Deduce noexcept for a destructor DTOR. */
5022 void
5023 deduce_noexcept_on_destructor (tree dtor)
5025 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5027 tree eh_spec = unevaluated_noexcept_spec ();
5028 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec);
5032 /* For each destructor in T, deduce noexcept:
5034 12.4/3: A declaration of a destructor that does not have an
5035 exception-specification is implicitly considered to have the
5036 same exception-specification as an implicit declaration (15.4). */
5038 static void
5039 deduce_noexcept_on_destructors (tree t)
5041 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
5042 out now. */
5043 if (!CLASSTYPE_METHOD_VEC (t))
5044 return;
5046 for (ovl_iterator iter (CLASSTYPE_DESTRUCTORS (t)); iter; ++iter)
5047 deduce_noexcept_on_destructor (*iter);
5050 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
5051 of TYPE for virtual functions which FNDECL overrides. Return a
5052 mask of the tm attributes found therein. */
5054 static int
5055 look_for_tm_attr_overrides (tree type, tree fndecl)
5057 tree binfo = TYPE_BINFO (type);
5058 tree base_binfo;
5059 int ix, found = 0;
5061 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
5063 tree o, basetype = BINFO_TYPE (base_binfo);
5065 if (!TYPE_POLYMORPHIC_P (basetype))
5066 continue;
5068 o = look_for_overrides_here (basetype, fndecl);
5069 if (o)
5071 if (lookup_attribute ("transaction_safe_dynamic",
5072 DECL_ATTRIBUTES (o)))
5073 /* transaction_safe_dynamic is not inherited. */;
5074 else
5075 found |= tm_attr_to_mask (find_tm_attribute
5076 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
5078 else
5079 found |= look_for_tm_attr_overrides (basetype, fndecl);
5082 return found;
5085 /* Subroutine of set_method_tm_attributes. Handle the checks and
5086 inheritance for one virtual method FNDECL. */
5088 static void
5089 set_one_vmethod_tm_attributes (tree type, tree fndecl)
5091 tree tm_attr;
5092 int found, have;
5094 found = look_for_tm_attr_overrides (type, fndecl);
5096 /* If FNDECL doesn't actually override anything (i.e. T is the
5097 class that first declares FNDECL virtual), then we're done. */
5098 if (found == 0)
5099 return;
5101 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
5102 have = tm_attr_to_mask (tm_attr);
5104 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
5105 tm_pure must match exactly, otherwise no weakening of
5106 tm_safe > tm_callable > nothing. */
5107 /* ??? The tm_pure attribute didn't make the transition to the
5108 multivendor language spec. */
5109 if (have == TM_ATTR_PURE)
5111 if (found != TM_ATTR_PURE)
5113 found &= -found;
5114 goto err_override;
5117 /* If the overridden function is tm_pure, then FNDECL must be. */
5118 else if (found == TM_ATTR_PURE && tm_attr)
5119 goto err_override;
5120 /* Look for base class combinations that cannot be satisfied. */
5121 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
5123 found &= ~TM_ATTR_PURE;
5124 found &= -found;
5125 error_at (DECL_SOURCE_LOCATION (fndecl),
5126 "method overrides both %<transaction_pure%> and %qE methods",
5127 tm_mask_to_attr (found));
5129 /* If FNDECL did not declare an attribute, then inherit the most
5130 restrictive one. */
5131 else if (tm_attr == NULL)
5133 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
5135 /* Otherwise validate that we're not weaker than a function
5136 that is being overridden. */
5137 else
5139 found &= -found;
5140 if (found <= TM_ATTR_CALLABLE && have > found)
5141 goto err_override;
5143 return;
5145 err_override:
5146 error_at (DECL_SOURCE_LOCATION (fndecl),
5147 "method declared %qE overriding %qE method",
5148 tm_attr, tm_mask_to_attr (found));
5151 /* For each of the methods in T, propagate a class-level tm attribute. */
5153 static void
5154 set_method_tm_attributes (tree t)
5156 tree class_tm_attr, fndecl;
5158 /* Don't bother collecting tm attributes if transactional memory
5159 support is not enabled. */
5160 if (!flag_tm)
5161 return;
5163 /* Process virtual methods first, as they inherit directly from the
5164 base virtual function and also require validation of new attributes. */
5165 if (TYPE_CONTAINS_VPTR_P (t))
5167 tree vchain;
5168 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
5169 vchain = TREE_CHAIN (vchain))
5171 fndecl = BV_FN (vchain);
5172 if (DECL_THUNK_P (fndecl))
5173 fndecl = THUNK_TARGET (fndecl);
5174 set_one_vmethod_tm_attributes (t, fndecl);
5178 /* If the class doesn't have an attribute, nothing more to do. */
5179 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
5180 if (class_tm_attr == NULL)
5181 return;
5183 /* Any method that does not yet have a tm attribute inherits
5184 the one from the class. */
5185 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl))
5187 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
5188 apply_tm_attr (fndecl, class_tm_attr);
5192 /* Returns true if FN is a default constructor. */
5194 bool
5195 default_ctor_p (tree fn)
5197 return (DECL_CONSTRUCTOR_P (fn)
5198 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
5201 /* Returns true iff class T has a user-defined constructor that can be called
5202 with more than zero arguments. */
5204 bool
5205 type_has_user_nondefault_constructor (tree t)
5207 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5208 return false;
5210 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5212 tree fn = *iter;
5213 if (!DECL_ARTIFICIAL (fn)
5214 && (TREE_CODE (fn) == TEMPLATE_DECL
5215 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
5216 != NULL_TREE)))
5217 return true;
5220 return false;
5223 /* Returns the defaulted constructor if T has one. Otherwise, returns
5224 NULL_TREE. */
5226 tree
5227 in_class_defaulted_default_constructor (tree t)
5229 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5230 return NULL_TREE;
5232 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5234 tree fn = *iter;
5236 if (DECL_DEFAULTED_IN_CLASS_P (fn)
5237 && default_ctor_p (fn))
5238 return fn;
5241 return NULL_TREE;
5244 /* Returns true iff FN is a user-provided function, i.e. user-declared
5245 and not defaulted at its first declaration. */
5247 bool
5248 user_provided_p (tree fn)
5250 if (TREE_CODE (fn) == TEMPLATE_DECL)
5251 return true;
5252 else
5253 return (!DECL_ARTIFICIAL (fn)
5254 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
5255 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
5258 /* Returns true iff class T has a user-provided constructor. */
5260 bool
5261 type_has_user_provided_constructor (tree t)
5263 if (!CLASS_TYPE_P (t))
5264 return false;
5266 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5267 return false;
5269 /* This can happen in error cases; avoid crashing. */
5270 if (!CLASSTYPE_METHOD_VEC (t))
5271 return false;
5273 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5274 if (user_provided_p (*iter))
5275 return true;
5277 return false;
5280 /* Returns true iff class T has a user-provided or explicit constructor. */
5282 bool
5283 type_has_user_provided_or_explicit_constructor (tree t)
5285 if (!CLASS_TYPE_P (t))
5286 return false;
5288 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
5289 return false;
5291 /* This can happen in error cases; avoid crashing. */
5292 if (!CLASSTYPE_METHOD_VEC (t))
5293 return false;
5295 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5297 tree fn = *iter;
5298 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
5299 return true;
5302 return false;
5305 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5306 declared or explicitly defaulted in the class body) default
5307 constructor. */
5309 bool
5310 type_has_non_user_provided_default_constructor (tree t)
5312 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5313 return false;
5314 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5315 return true;
5317 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5319 tree fn = *iter;
5320 if (TREE_CODE (fn) == FUNCTION_DECL
5321 && default_ctor_p (fn)
5322 && !user_provided_p (fn))
5323 return true;
5326 return false;
5329 /* TYPE is being used as a virtual base, and has a non-trivial move
5330 assignment. Return true if this is due to there being a user-provided
5331 move assignment in TYPE or one of its subobjects; if there isn't, then
5332 multiple move assignment can't cause any harm. */
5334 bool
5335 vbase_has_user_provided_move_assign (tree type)
5337 /* Does the type itself have a user-provided move assignment operator? */
5338 for (ovl_iterator iter (lookup_fnfields_slot_nolazy
5339 (type, cp_assignment_operator_id (NOP_EXPR)));
5340 iter; ++iter)
5342 tree fn = *iter;
5343 if (move_fn_p (fn) && user_provided_p (fn))
5344 return true;
5347 /* Do any of its bases? */
5348 tree binfo = TYPE_BINFO (type);
5349 tree base_binfo;
5350 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5351 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5352 return true;
5354 /* Or non-static data members? */
5355 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5357 if (TREE_CODE (field) == FIELD_DECL
5358 && CLASS_TYPE_P (TREE_TYPE (field))
5359 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5360 return true;
5363 /* Seems not. */
5364 return false;
5367 /* If default-initialization leaves part of TYPE uninitialized, returns
5368 a DECL for the field or TYPE itself (DR 253). */
5370 tree
5371 default_init_uninitialized_part (tree type)
5373 tree t, r, binfo;
5374 int i;
5376 type = strip_array_types (type);
5377 if (!CLASS_TYPE_P (type))
5378 return type;
5379 if (!type_has_non_user_provided_default_constructor (type))
5380 return NULL_TREE;
5381 for (binfo = TYPE_BINFO (type), i = 0;
5382 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5384 r = default_init_uninitialized_part (BINFO_TYPE (t));
5385 if (r)
5386 return r;
5388 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5389 if (TREE_CODE (t) == FIELD_DECL
5390 && !DECL_ARTIFICIAL (t)
5391 && !DECL_INITIAL (t))
5393 r = default_init_uninitialized_part (TREE_TYPE (t));
5394 if (r)
5395 return DECL_P (r) ? r : t;
5398 return NULL_TREE;
5401 /* Returns true iff for class T, a trivial synthesized default constructor
5402 would be constexpr. */
5404 bool
5405 trivial_default_constructor_is_constexpr (tree t)
5407 /* A defaulted trivial default constructor is constexpr
5408 if there is nothing to initialize. */
5409 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5410 return is_really_empty_class (t);
5413 /* Returns true iff class T has a constexpr default constructor. */
5415 bool
5416 type_has_constexpr_default_constructor (tree t)
5418 tree fns;
5420 if (!CLASS_TYPE_P (t))
5422 /* The caller should have stripped an enclosing array. */
5423 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5424 return false;
5426 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5428 if (!TYPE_HAS_COMPLEX_DFLT (t))
5429 return trivial_default_constructor_is_constexpr (t);
5430 /* Non-trivial, we need to check subobject constructors. */
5431 lazily_declare_fn (sfk_constructor, t);
5433 fns = locate_ctor (t);
5434 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5437 /* Returns true iff class T has a constexpr default constructor or has an
5438 implicitly declared default constructor that we can't tell if it's constexpr
5439 without forcing a lazy declaration (which might cause undesired
5440 instantiations). */
5442 bool
5443 type_maybe_constexpr_default_constructor (tree t)
5445 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5446 && TYPE_HAS_COMPLEX_DFLT (t))
5447 /* Assume it's constexpr. */
5448 return true;
5449 return type_has_constexpr_default_constructor (t);
5452 /* Returns true iff class TYPE has a virtual destructor. */
5454 bool
5455 type_has_virtual_destructor (tree type)
5457 tree dtor;
5459 if (!CLASS_TYPE_P (type))
5460 return false;
5462 gcc_assert (COMPLETE_TYPE_P (type));
5463 dtor = CLASSTYPE_DESTRUCTORS (type);
5464 return (dtor && DECL_VIRTUAL_P (dtor));
5467 /* Returns true iff class T has a move constructor. */
5469 bool
5470 type_has_move_constructor (tree t)
5472 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5474 gcc_assert (COMPLETE_TYPE_P (t));
5475 lazily_declare_fn (sfk_move_constructor, t);
5478 if (!CLASSTYPE_METHOD_VEC (t))
5479 return false;
5481 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5482 if (move_fn_p (*iter))
5483 return true;
5485 return false;
5488 /* Returns true iff class T has a move assignment operator. */
5490 bool
5491 type_has_move_assign (tree t)
5493 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5495 gcc_assert (COMPLETE_TYPE_P (t));
5496 lazily_declare_fn (sfk_move_assignment, t);
5499 for (ovl_iterator iter (lookup_fnfields_slot_nolazy
5500 (t, cp_assignment_operator_id (NOP_EXPR)));
5501 iter; ++iter)
5502 if (move_fn_p (*iter))
5503 return true;
5505 return false;
5508 /* Returns true iff class T has a move constructor that was explicitly
5509 declared in the class body. Note that this is different from
5510 "user-provided", which doesn't include functions that are defaulted in
5511 the class. */
5513 bool
5514 type_has_user_declared_move_constructor (tree t)
5516 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5517 return false;
5519 if (!CLASSTYPE_METHOD_VEC (t))
5520 return false;
5522 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5524 tree fn = *iter;
5525 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5526 return true;
5529 return false;
5532 /* Returns true iff class T has a move assignment operator that was
5533 explicitly declared in the class body. */
5535 bool
5536 type_has_user_declared_move_assign (tree t)
5538 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5539 return false;
5541 for (ovl_iterator iter (lookup_fnfields_slot_nolazy
5542 (t, cp_assignment_operator_id (NOP_EXPR)));
5543 iter; ++iter)
5545 tree fn = *iter;
5546 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn))
5547 return true;
5550 return false;
5553 /* Nonzero if we need to build up a constructor call when initializing an
5554 object of this class, either because it has a user-declared constructor
5555 or because it doesn't have a default constructor (so we need to give an
5556 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5557 what you care about is whether or not an object can be produced by a
5558 constructor (e.g. so we don't set TREE_READONLY on const variables of
5559 such type); use this function when what you care about is whether or not
5560 to try to call a constructor to create an object. The latter case is
5561 the former plus some cases of constructors that cannot be called. */
5563 bool
5564 type_build_ctor_call (tree t)
5566 tree inner;
5567 if (TYPE_NEEDS_CONSTRUCTING (t))
5568 return true;
5569 inner = strip_array_types (t);
5570 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5571 return false;
5572 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5573 return true;
5574 if (cxx_dialect < cxx11)
5575 return false;
5576 /* A user-declared constructor might be private, and a constructor might
5577 be trivial but deleted. */
5578 for (ovl_iterator iter
5579 (lookup_fnfields_slot (inner, complete_ctor_identifier));
5580 iter; ++iter)
5582 tree fn = *iter;
5583 if (!DECL_ARTIFICIAL (fn)
5584 || DECL_DELETED_FN (fn))
5585 return true;
5587 return false;
5590 /* Like type_build_ctor_call, but for destructors. */
5592 bool
5593 type_build_dtor_call (tree t)
5595 tree inner;
5596 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5597 return true;
5598 inner = strip_array_types (t);
5599 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5600 || !COMPLETE_TYPE_P (inner))
5601 return false;
5602 if (cxx_dialect < cxx11)
5603 return false;
5604 /* A user-declared destructor might be private, and a destructor might
5605 be trivial but deleted. */
5606 for (ovl_iterator iter
5607 (lookup_fnfields_slot (inner, complete_dtor_identifier));
5608 iter; ++iter)
5610 tree fn = *iter;
5611 if (!DECL_ARTIFICIAL (fn)
5612 || DECL_DELETED_FN (fn))
5613 return true;
5615 return false;
5618 /* Remove all zero-width bit-fields from T. */
5620 static void
5621 remove_zero_width_bit_fields (tree t)
5623 tree *fieldsp;
5625 fieldsp = &TYPE_FIELDS (t);
5626 while (*fieldsp)
5628 if (TREE_CODE (*fieldsp) == FIELD_DECL
5629 && DECL_C_BIT_FIELD (*fieldsp)
5630 /* We should not be confused by the fact that grokbitfield
5631 temporarily sets the width of the bit field into
5632 DECL_INITIAL (*fieldsp).
5633 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5634 to that width. */
5635 && (DECL_SIZE (*fieldsp) == NULL_TREE
5636 || integer_zerop (DECL_SIZE (*fieldsp))))
5637 *fieldsp = DECL_CHAIN (*fieldsp);
5638 else
5639 fieldsp = &DECL_CHAIN (*fieldsp);
5643 /* Returns TRUE iff we need a cookie when dynamically allocating an
5644 array whose elements have the indicated class TYPE. */
5646 static bool
5647 type_requires_array_cookie (tree type)
5649 tree fns;
5650 bool has_two_argument_delete_p = false;
5652 gcc_assert (CLASS_TYPE_P (type));
5654 /* If there's a non-trivial destructor, we need a cookie. In order
5655 to iterate through the array calling the destructor for each
5656 element, we'll have to know how many elements there are. */
5657 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5658 return true;
5660 /* If the usual deallocation function is a two-argument whose second
5661 argument is of type `size_t', then we have to pass the size of
5662 the array to the deallocation function, so we will need to store
5663 a cookie. */
5664 fns = lookup_fnfields (TYPE_BINFO (type),
5665 cp_operator_id (VEC_DELETE_EXPR),
5666 /*protect=*/0);
5667 /* If there are no `operator []' members, or the lookup is
5668 ambiguous, then we don't need a cookie. */
5669 if (!fns || fns == error_mark_node)
5670 return false;
5671 /* Loop through all of the functions. */
5672 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5674 tree fn = *iter;
5676 /* See if this function is a one-argument delete function. If
5677 it is, then it will be the usual deallocation function. */
5678 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5679 if (second_parm == void_list_node)
5680 return false;
5681 /* Do not consider this function if its second argument is an
5682 ellipsis. */
5683 if (!second_parm)
5684 continue;
5685 /* Otherwise, if we have a two-argument function and the second
5686 argument is `size_t', it will be the usual deallocation
5687 function -- unless there is one-argument function, too. */
5688 if (TREE_CHAIN (second_parm) == void_list_node
5689 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5690 has_two_argument_delete_p = true;
5693 return has_two_argument_delete_p;
5696 /* Finish computing the `literal type' property of class type T.
5698 At this point, we have already processed base classes and
5699 non-static data members. We need to check whether the copy
5700 constructor is trivial, the destructor is trivial, and there
5701 is a trivial default constructor or at least one constexpr
5702 constructor other than the copy constructor. */
5704 static void
5705 finalize_literal_type_property (tree t)
5707 tree fn;
5709 if (cxx_dialect < cxx11
5710 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5711 CLASSTYPE_LITERAL_P (t) = false;
5712 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5713 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx1z);
5714 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5715 && CLASSTYPE_NON_AGGREGATE (t)
5716 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5717 CLASSTYPE_LITERAL_P (t) = false;
5719 /* C++14 DR 1684 removed this restriction. */
5720 if (cxx_dialect < cxx14
5721 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5722 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5723 if (DECL_DECLARED_CONSTEXPR_P (fn)
5724 && TREE_CODE (fn) != TEMPLATE_DECL
5725 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5726 && !DECL_CONSTRUCTOR_P (fn))
5728 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5729 if (!DECL_GENERATED_P (fn)
5730 && pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5731 "enclosing class of constexpr non-static member "
5732 "function %q+#D is not a literal type", fn))
5733 explain_non_literal_class (t);
5737 /* T is a non-literal type used in a context which requires a constant
5738 expression. Explain why it isn't literal. */
5740 void
5741 explain_non_literal_class (tree t)
5743 static hash_set<tree> *diagnosed;
5745 if (!CLASS_TYPE_P (t))
5746 return;
5747 t = TYPE_MAIN_VARIANT (t);
5749 if (diagnosed == NULL)
5750 diagnosed = new hash_set<tree>;
5751 if (diagnosed->add (t))
5752 /* Already explained. */
5753 return;
5755 inform (0, "%q+T is not literal because:", t);
5756 if (cxx_dialect < cxx1z && LAMBDA_TYPE_P (t))
5757 inform (0, " %qT is a closure type, which is only literal in "
5758 "C++1z and later", t);
5759 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5760 inform (0, " %q+T has a non-trivial destructor", t);
5761 else if (CLASSTYPE_NON_AGGREGATE (t)
5762 && !TYPE_HAS_TRIVIAL_DFLT (t)
5763 && !LAMBDA_TYPE_P (t)
5764 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5766 inform (0, " %q+T is not an aggregate, does not have a trivial "
5767 "default constructor, and has no constexpr constructor that "
5768 "is not a copy or move constructor", t);
5769 if (type_has_non_user_provided_default_constructor (t))
5770 /* Note that we can't simply call locate_ctor because when the
5771 constructor is deleted it just returns NULL_TREE. */
5772 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5774 tree fn = *iter;
5775 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5777 parms = skip_artificial_parms_for (fn, parms);
5779 if (sufficient_parms_p (parms))
5781 if (DECL_DELETED_FN (fn))
5782 maybe_explain_implicit_delete (fn);
5783 else
5784 explain_invalid_constexpr_fn (fn);
5785 break;
5789 else
5791 tree binfo, base_binfo, field; int i;
5792 for (binfo = TYPE_BINFO (t), i = 0;
5793 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5795 tree basetype = TREE_TYPE (base_binfo);
5796 if (!CLASSTYPE_LITERAL_P (basetype))
5798 inform (0, " base class %qT of %q+T is non-literal",
5799 basetype, t);
5800 explain_non_literal_class (basetype);
5801 return;
5804 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5806 tree ftype;
5807 if (TREE_CODE (field) != FIELD_DECL)
5808 continue;
5809 ftype = TREE_TYPE (field);
5810 if (!literal_type_p (ftype))
5812 inform (DECL_SOURCE_LOCATION (field),
5813 " non-static data member %qD has non-literal type",
5814 field);
5815 if (CLASS_TYPE_P (ftype))
5816 explain_non_literal_class (ftype);
5818 if (CP_TYPE_VOLATILE_P (ftype))
5819 inform (DECL_SOURCE_LOCATION (field),
5820 " non-static data member %qD has volatile type", field);
5825 /* Check the validity of the bases and members declared in T. Add any
5826 implicitly-generated functions (like copy-constructors and
5827 assignment operators). Compute various flag bits (like
5828 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5829 level: i.e., independently of the ABI in use. */
5831 static void
5832 check_bases_and_members (tree t)
5834 /* Nonzero if the implicitly generated copy constructor should take
5835 a non-const reference argument. */
5836 int cant_have_const_ctor;
5837 /* Nonzero if the implicitly generated assignment operator
5838 should take a non-const reference argument. */
5839 int no_const_asn_ref;
5840 tree access_decls;
5841 bool saved_complex_asn_ref;
5842 bool saved_nontrivial_dtor;
5843 tree fn;
5845 /* By default, we use const reference arguments and generate default
5846 constructors. */
5847 cant_have_const_ctor = 0;
5848 no_const_asn_ref = 0;
5850 /* Check all the base-classes and set FMEM members to point to arrays
5851 of potential interest. */
5852 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5854 /* Deduce noexcept on destructors. This needs to happen after we've set
5855 triviality flags appropriately for our bases. */
5856 if (cxx_dialect >= cxx11)
5857 deduce_noexcept_on_destructors (t);
5859 /* Check all the method declarations. */
5860 check_methods (t);
5862 /* Save the initial values of these flags which only indicate whether
5863 or not the class has user-provided functions. As we analyze the
5864 bases and members we can set these flags for other reasons. */
5865 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5866 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5868 /* Check all the data member declarations. We cannot call
5869 check_field_decls until we have called check_bases check_methods,
5870 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5871 being set appropriately. */
5872 check_field_decls (t, &access_decls,
5873 &cant_have_const_ctor,
5874 &no_const_asn_ref);
5876 /* A nearly-empty class has to be vptr-containing; a nearly empty
5877 class contains just a vptr. */
5878 if (!TYPE_CONTAINS_VPTR_P (t))
5879 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5881 /* Do some bookkeeping that will guide the generation of implicitly
5882 declared member functions. */
5883 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5884 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5885 /* We need to call a constructor for this class if it has a
5886 user-provided constructor, or if the default constructor is going
5887 to initialize the vptr. (This is not an if-and-only-if;
5888 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5889 themselves need constructing.) */
5890 TYPE_NEEDS_CONSTRUCTING (t)
5891 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5892 /* [dcl.init.aggr]
5894 An aggregate is an array or a class with no user-provided
5895 constructors ... and no virtual functions.
5897 Again, other conditions for being an aggregate are checked
5898 elsewhere. */
5899 CLASSTYPE_NON_AGGREGATE (t)
5900 |= (type_has_user_provided_or_explicit_constructor (t)
5901 || TYPE_POLYMORPHIC_P (t));
5902 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5903 retain the old definition internally for ABI reasons. */
5904 CLASSTYPE_NON_LAYOUT_POD_P (t)
5905 |= (CLASSTYPE_NON_AGGREGATE (t)
5906 || saved_nontrivial_dtor || saved_complex_asn_ref);
5907 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5908 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5909 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5910 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5912 /* If the only explicitly declared default constructor is user-provided,
5913 set TYPE_HAS_COMPLEX_DFLT. */
5914 if (!TYPE_HAS_COMPLEX_DFLT (t)
5915 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5916 && !type_has_non_user_provided_default_constructor (t))
5917 TYPE_HAS_COMPLEX_DFLT (t) = true;
5919 /* Warn if a public base of a polymorphic type has an accessible
5920 non-virtual destructor. It is only now that we know the class is
5921 polymorphic. Although a polymorphic base will have a already
5922 been diagnosed during its definition, we warn on use too. */
5923 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5925 tree binfo = TYPE_BINFO (t);
5926 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5927 tree base_binfo;
5928 unsigned i;
5930 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5932 tree basetype = TREE_TYPE (base_binfo);
5934 if ((*accesses)[i] == access_public_node
5935 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5936 && accessible_nvdtor_p (basetype))
5937 warning (OPT_Wnon_virtual_dtor,
5938 "base class %q#T has accessible non-virtual destructor",
5939 basetype);
5943 /* If the class has no user-declared constructor, but does have
5944 non-static const or reference data members that can never be
5945 initialized, issue a warning. */
5946 if (warn_uninitialized
5947 /* Classes with user-declared constructors are presumed to
5948 initialize these members. */
5949 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5950 /* Aggregates can be initialized with brace-enclosed
5951 initializers. */
5952 && CLASSTYPE_NON_AGGREGATE (t))
5954 tree field;
5956 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5958 tree type;
5960 if (TREE_CODE (field) != FIELD_DECL
5961 || DECL_INITIAL (field) != NULL_TREE)
5962 continue;
5964 type = TREE_TYPE (field);
5965 if (TREE_CODE (type) == REFERENCE_TYPE)
5966 warning_at (DECL_SOURCE_LOCATION (field),
5967 OPT_Wuninitialized, "non-static reference %q#D "
5968 "in class without a constructor", field);
5969 else if (CP_TYPE_CONST_P (type)
5970 && (!CLASS_TYPE_P (type)
5971 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5972 warning_at (DECL_SOURCE_LOCATION (field),
5973 OPT_Wuninitialized, "non-static const member %q#D "
5974 "in class without a constructor", field);
5978 /* Synthesize any needed methods. */
5979 add_implicitly_declared_members (t, &access_decls,
5980 cant_have_const_ctor,
5981 no_const_asn_ref);
5983 /* Check defaulted declarations here so we have cant_have_const_ctor
5984 and don't need to worry about clones. */
5985 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
5986 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
5988 int copy = copy_fn_p (fn);
5989 if (copy > 0)
5991 bool imp_const_p
5992 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5993 : !no_const_asn_ref);
5994 bool fn_const_p = (copy == 2);
5996 if (fn_const_p && !imp_const_p)
5997 /* If the function is defaulted outside the class, we just
5998 give the synthesis error. */
5999 error ("%q+D declared to take const reference, but implicit "
6000 "declaration would take non-const", fn);
6002 defaulted_late_check (fn);
6005 if (LAMBDA_TYPE_P (t))
6007 /* "This class type is not an aggregate." */
6008 CLASSTYPE_NON_AGGREGATE (t) = 1;
6011 /* Compute the 'literal type' property before we
6012 do anything with non-static member functions. */
6013 finalize_literal_type_property (t);
6015 /* Create the in-charge and not-in-charge variants of constructors
6016 and destructors. */
6017 clone_constructors_and_destructors (t);
6019 /* Process the using-declarations. */
6020 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
6021 handle_using_decl (TREE_VALUE (access_decls), t);
6023 /* Build and sort the CLASSTYPE_METHOD_VEC. */
6024 finish_struct_methods (t);
6026 /* Figure out whether or not we will need a cookie when dynamically
6027 allocating an array of this type. */
6028 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
6029 = type_requires_array_cookie (t);
6032 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
6033 accordingly. If a new vfield was created (because T doesn't have a
6034 primary base class), then the newly created field is returned. It
6035 is not added to the TYPE_FIELDS list; it is the caller's
6036 responsibility to do that. Accumulate declared virtual functions
6037 on VIRTUALS_P. */
6039 static tree
6040 create_vtable_ptr (tree t, tree* virtuals_p)
6042 tree fn;
6044 /* Collect the virtual functions declared in T. */
6045 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
6046 if (TREE_CODE (fn) == FUNCTION_DECL
6047 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
6048 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
6050 tree new_virtual = make_node (TREE_LIST);
6052 BV_FN (new_virtual) = fn;
6053 BV_DELTA (new_virtual) = integer_zero_node;
6054 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
6056 TREE_CHAIN (new_virtual) = *virtuals_p;
6057 *virtuals_p = new_virtual;
6060 /* If we couldn't find an appropriate base class, create a new field
6061 here. Even if there weren't any new virtual functions, we might need a
6062 new virtual function table if we're supposed to include vptrs in
6063 all classes that need them. */
6064 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
6066 /* We build this decl with vtbl_ptr_type_node, which is a
6067 `vtable_entry_type*'. It might seem more precise to use
6068 `vtable_entry_type (*)[N]' where N is the number of virtual
6069 functions. However, that would require the vtable pointer in
6070 base classes to have a different type than the vtable pointer
6071 in derived classes. We could make that happen, but that
6072 still wouldn't solve all the problems. In particular, the
6073 type-based alias analysis code would decide that assignments
6074 to the base class vtable pointer can't alias assignments to
6075 the derived class vtable pointer, since they have different
6076 types. Thus, in a derived class destructor, where the base
6077 class constructor was inlined, we could generate bad code for
6078 setting up the vtable pointer.
6080 Therefore, we use one type for all vtable pointers. We still
6081 use a type-correct type; it's just doesn't indicate the array
6082 bounds. That's better than using `void*' or some such; it's
6083 cleaner, and it let's the alias analysis code know that these
6084 stores cannot alias stores to void*! */
6085 tree field;
6087 field = build_decl (input_location,
6088 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
6089 DECL_VIRTUAL_P (field) = 1;
6090 DECL_ARTIFICIAL (field) = 1;
6091 DECL_FIELD_CONTEXT (field) = t;
6092 DECL_FCONTEXT (field) = t;
6093 if (TYPE_PACKED (t))
6094 DECL_PACKED (field) = 1;
6096 TYPE_VFIELD (t) = field;
6098 /* This class is non-empty. */
6099 CLASSTYPE_EMPTY_P (t) = 0;
6101 return field;
6104 return NULL_TREE;
6107 /* Add OFFSET to all base types of BINFO which is a base in the
6108 hierarchy dominated by T.
6110 OFFSET, which is a type offset, is number of bytes. */
6112 static void
6113 propagate_binfo_offsets (tree binfo, tree offset)
6115 int i;
6116 tree primary_binfo;
6117 tree base_binfo;
6119 /* Update BINFO's offset. */
6120 BINFO_OFFSET (binfo)
6121 = fold_convert (sizetype,
6122 size_binop (PLUS_EXPR,
6123 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
6124 offset));
6126 /* Find the primary base class. */
6127 primary_binfo = get_primary_binfo (binfo);
6129 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
6130 propagate_binfo_offsets (primary_binfo, offset);
6132 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
6133 downwards. */
6134 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6136 /* Don't do the primary base twice. */
6137 if (base_binfo == primary_binfo)
6138 continue;
6140 if (BINFO_VIRTUAL_P (base_binfo))
6141 continue;
6143 propagate_binfo_offsets (base_binfo, offset);
6147 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
6148 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
6149 empty subobjects of T. */
6151 static void
6152 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
6154 tree vbase;
6155 tree t = rli->t;
6156 tree *next_field;
6158 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
6159 return;
6161 /* Find the last field. The artificial fields created for virtual
6162 bases will go after the last extant field to date. */
6163 next_field = &TYPE_FIELDS (t);
6164 while (*next_field)
6165 next_field = &DECL_CHAIN (*next_field);
6167 /* Go through the virtual bases, allocating space for each virtual
6168 base that is not already a primary base class. These are
6169 allocated in inheritance graph order. */
6170 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6172 if (!BINFO_VIRTUAL_P (vbase))
6173 continue;
6175 if (!BINFO_PRIMARY_P (vbase))
6177 /* This virtual base is not a primary base of any class in the
6178 hierarchy, so we have to add space for it. */
6179 next_field = build_base_field (rli, vbase,
6180 offsets, next_field);
6185 /* Returns the offset of the byte just past the end of the base class
6186 BINFO. */
6188 static tree
6189 end_of_base (tree binfo)
6191 tree size;
6193 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
6194 size = TYPE_SIZE_UNIT (char_type_node);
6195 else if (is_empty_class (BINFO_TYPE (binfo)))
6196 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
6197 allocate some space for it. It cannot have virtual bases, so
6198 TYPE_SIZE_UNIT is fine. */
6199 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6200 else
6201 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
6203 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
6206 /* Returns the offset of the byte just past the end of the base class
6207 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
6208 only non-virtual bases are included. */
6210 static tree
6211 end_of_class (tree t, int include_virtuals_p)
6213 tree result = size_zero_node;
6214 vec<tree, va_gc> *vbases;
6215 tree binfo;
6216 tree base_binfo;
6217 tree offset;
6218 int i;
6220 for (binfo = TYPE_BINFO (t), i = 0;
6221 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6223 if (!include_virtuals_p
6224 && BINFO_VIRTUAL_P (base_binfo)
6225 && (!BINFO_PRIMARY_P (base_binfo)
6226 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
6227 continue;
6229 offset = end_of_base (base_binfo);
6230 if (tree_int_cst_lt (result, offset))
6231 result = offset;
6234 if (include_virtuals_p)
6235 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6236 vec_safe_iterate (vbases, i, &base_binfo); i++)
6238 offset = end_of_base (base_binfo);
6239 if (tree_int_cst_lt (result, offset))
6240 result = offset;
6243 return result;
6246 /* Warn about bases of T that are inaccessible because they are
6247 ambiguous. For example:
6249 struct S {};
6250 struct T : public S {};
6251 struct U : public S, public T {};
6253 Here, `(S*) new U' is not allowed because there are two `S'
6254 subobjects of U. */
6256 static void
6257 warn_about_ambiguous_bases (tree t)
6259 int i;
6260 vec<tree, va_gc> *vbases;
6261 tree basetype;
6262 tree binfo;
6263 tree base_binfo;
6265 /* If there are no repeated bases, nothing can be ambiguous. */
6266 if (!CLASSTYPE_REPEATED_BASE_P (t))
6267 return;
6269 /* Check direct bases. */
6270 for (binfo = TYPE_BINFO (t), i = 0;
6271 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
6273 basetype = BINFO_TYPE (base_binfo);
6275 if (!uniquely_derived_from_p (basetype, t))
6276 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
6277 basetype, t);
6280 /* Check for ambiguous virtual bases. */
6281 if (extra_warnings)
6282 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6283 vec_safe_iterate (vbases, i, &binfo); i++)
6285 basetype = BINFO_TYPE (binfo);
6287 if (!uniquely_derived_from_p (basetype, t))
6288 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
6289 "to ambiguity", basetype, t);
6293 /* Compare two INTEGER_CSTs K1 and K2. */
6295 static int
6296 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
6298 return tree_int_cst_compare ((tree) k1, (tree) k2);
6301 /* Increase the size indicated in RLI to account for empty classes
6302 that are "off the end" of the class. */
6304 static void
6305 include_empty_classes (record_layout_info rli)
6307 tree eoc;
6308 tree rli_size;
6310 /* It might be the case that we grew the class to allocate a
6311 zero-sized base class. That won't be reflected in RLI, yet,
6312 because we are willing to overlay multiple bases at the same
6313 offset. However, now we need to make sure that RLI is big enough
6314 to reflect the entire class. */
6315 eoc = end_of_class (rli->t,
6316 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
6317 rli_size = rli_size_unit_so_far (rli);
6318 if (TREE_CODE (rli_size) == INTEGER_CST
6319 && tree_int_cst_lt (rli_size, eoc))
6321 /* The size should have been rounded to a whole byte. */
6322 gcc_assert (tree_int_cst_equal
6323 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6324 rli->bitpos
6325 = size_binop (PLUS_EXPR,
6326 rli->bitpos,
6327 size_binop (MULT_EXPR,
6328 fold_convert (bitsizetype,
6329 size_binop (MINUS_EXPR,
6330 eoc, rli_size)),
6331 bitsize_int (BITS_PER_UNIT)));
6332 normalize_rli (rli);
6336 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6337 BINFO_OFFSETs for all of the base-classes. Position the vtable
6338 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6340 static void
6341 layout_class_type (tree t, tree *virtuals_p)
6343 tree non_static_data_members;
6344 tree field;
6345 tree vptr;
6346 record_layout_info rli;
6347 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6348 types that appear at that offset. */
6349 splay_tree empty_base_offsets;
6350 /* True if the last field laid out was a bit-field. */
6351 bool last_field_was_bitfield = false;
6352 /* The location at which the next field should be inserted. */
6353 tree *next_field;
6354 /* T, as a base class. */
6355 tree base_t;
6357 /* Keep track of the first non-static data member. */
6358 non_static_data_members = TYPE_FIELDS (t);
6360 /* Start laying out the record. */
6361 rli = start_record_layout (t);
6363 /* Mark all the primary bases in the hierarchy. */
6364 determine_primary_bases (t);
6366 /* Create a pointer to our virtual function table. */
6367 vptr = create_vtable_ptr (t, virtuals_p);
6369 /* The vptr is always the first thing in the class. */
6370 if (vptr)
6372 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6373 TYPE_FIELDS (t) = vptr;
6374 next_field = &DECL_CHAIN (vptr);
6375 place_field (rli, vptr);
6377 else
6378 next_field = &TYPE_FIELDS (t);
6380 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6381 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6382 NULL, NULL);
6383 build_base_fields (rli, empty_base_offsets, next_field);
6385 /* Layout the non-static data members. */
6386 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6388 tree type;
6389 tree padding;
6391 /* We still pass things that aren't non-static data members to
6392 the back end, in case it wants to do something with them. */
6393 if (TREE_CODE (field) != FIELD_DECL)
6395 place_field (rli, field);
6396 /* If the static data member has incomplete type, keep track
6397 of it so that it can be completed later. (The handling
6398 of pending statics in finish_record_layout is
6399 insufficient; consider:
6401 struct S1;
6402 struct S2 { static S1 s1; };
6404 At this point, finish_record_layout will be called, but
6405 S1 is still incomplete.) */
6406 if (VAR_P (field))
6408 maybe_register_incomplete_var (field);
6409 /* The visibility of static data members is determined
6410 at their point of declaration, not their point of
6411 definition. */
6412 determine_visibility (field);
6414 continue;
6417 type = TREE_TYPE (field);
6418 if (type == error_mark_node)
6419 continue;
6421 padding = NULL_TREE;
6423 /* If this field is a bit-field whose width is greater than its
6424 type, then there are some special rules for allocating
6425 it. */
6426 if (DECL_C_BIT_FIELD (field)
6427 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6429 bool was_unnamed_p = false;
6430 /* We must allocate the bits as if suitably aligned for the
6431 longest integer type that fits in this many bits. Then,
6432 we are supposed to use the left over bits as additional
6433 padding. */
6435 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6436 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6437 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6438 limit = DECL_SIZE (field);
6440 tree integer_type = integer_types[itk_char];
6441 for (unsigned itk = itk_char; itk != itk_none; itk++)
6442 if (tree next = integer_types[itk])
6444 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6445 /* Too big, so our current guess is what we want. */
6446 break;
6447 /* Not bigger than limit, ok */
6448 integer_type = next;
6451 /* Figure out how much additional padding is required. */
6452 if (TREE_CODE (t) == UNION_TYPE)
6453 /* In a union, the padding field must have the full width
6454 of the bit-field; all fields start at offset zero. */
6455 padding = DECL_SIZE (field);
6456 else
6457 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6458 TYPE_SIZE (integer_type));
6460 if (integer_zerop (padding))
6461 padding = NULL_TREE;
6463 /* An unnamed bitfield does not normally affect the
6464 alignment of the containing class on a target where
6465 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6466 make any exceptions for unnamed bitfields when the
6467 bitfields are longer than their types. Therefore, we
6468 temporarily give the field a name. */
6469 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6471 was_unnamed_p = true;
6472 DECL_NAME (field) = make_anon_name ();
6475 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6476 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6477 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6478 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6479 empty_base_offsets);
6480 if (was_unnamed_p)
6481 DECL_NAME (field) = NULL_TREE;
6482 /* Now that layout has been performed, set the size of the
6483 field to the size of its declared type; the rest of the
6484 field is effectively invisible. */
6485 DECL_SIZE (field) = TYPE_SIZE (type);
6486 /* We must also reset the DECL_MODE of the field. */
6487 SET_DECL_MODE (field, TYPE_MODE (type));
6489 else
6490 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6491 empty_base_offsets);
6493 /* Remember the location of any empty classes in FIELD. */
6494 record_subobject_offsets (TREE_TYPE (field),
6495 byte_position(field),
6496 empty_base_offsets,
6497 /*is_data_member=*/true);
6499 /* If a bit-field does not immediately follow another bit-field,
6500 and yet it starts in the middle of a byte, we have failed to
6501 comply with the ABI. */
6502 if (warn_abi
6503 && DECL_C_BIT_FIELD (field)
6504 /* The TREE_NO_WARNING flag gets set by Objective-C when
6505 laying out an Objective-C class. The ObjC ABI differs
6506 from the C++ ABI, and so we do not want a warning
6507 here. */
6508 && !TREE_NO_WARNING (field)
6509 && !last_field_was_bitfield
6510 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6511 DECL_FIELD_BIT_OFFSET (field),
6512 bitsize_unit_node)))
6513 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6514 "offset of %qD is not ABI-compliant and may "
6515 "change in a future version of GCC", field);
6517 /* The middle end uses the type of expressions to determine the
6518 possible range of expression values. In order to optimize
6519 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6520 must be made aware of the width of "i", via its type.
6522 Because C++ does not have integer types of arbitrary width,
6523 we must (for the purposes of the front end) convert from the
6524 type assigned here to the declared type of the bitfield
6525 whenever a bitfield expression is used as an rvalue.
6526 Similarly, when assigning a value to a bitfield, the value
6527 must be converted to the type given the bitfield here. */
6528 if (DECL_C_BIT_FIELD (field))
6530 unsigned HOST_WIDE_INT width;
6531 tree ftype = TREE_TYPE (field);
6532 width = tree_to_uhwi (DECL_SIZE (field));
6533 if (width != TYPE_PRECISION (ftype))
6535 TREE_TYPE (field)
6536 = c_build_bitfield_integer_type (width,
6537 TYPE_UNSIGNED (ftype));
6538 TREE_TYPE (field)
6539 = cp_build_qualified_type (TREE_TYPE (field),
6540 cp_type_quals (ftype));
6544 /* If we needed additional padding after this field, add it
6545 now. */
6546 if (padding)
6548 tree padding_field;
6550 padding_field = build_decl (input_location,
6551 FIELD_DECL,
6552 NULL_TREE,
6553 char_type_node);
6554 DECL_BIT_FIELD (padding_field) = 1;
6555 DECL_SIZE (padding_field) = padding;
6556 DECL_CONTEXT (padding_field) = t;
6557 DECL_ARTIFICIAL (padding_field) = 1;
6558 DECL_IGNORED_P (padding_field) = 1;
6559 layout_nonempty_base_or_field (rli, padding_field,
6560 NULL_TREE,
6561 empty_base_offsets);
6564 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6567 if (!integer_zerop (rli->bitpos))
6569 /* Make sure that we are on a byte boundary so that the size of
6570 the class without virtual bases will always be a round number
6571 of bytes. */
6572 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6573 normalize_rli (rli);
6576 /* Delete all zero-width bit-fields from the list of fields. Now
6577 that the type is laid out they are no longer important. */
6578 remove_zero_width_bit_fields (t);
6580 /* Create the version of T used for virtual bases. We do not use
6581 make_class_type for this version; this is an artificial type. For
6582 a POD type, we just reuse T. */
6583 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6585 base_t = make_node (TREE_CODE (t));
6587 /* Set the size and alignment for the new type. */
6588 tree eoc;
6590 /* If the ABI version is not at least two, and the last
6591 field was a bit-field, RLI may not be on a byte
6592 boundary. In particular, rli_size_unit_so_far might
6593 indicate the last complete byte, while rli_size_so_far
6594 indicates the total number of bits used. Therefore,
6595 rli_size_so_far, rather than rli_size_unit_so_far, is
6596 used to compute TYPE_SIZE_UNIT. */
6597 eoc = end_of_class (t, /*include_virtuals_p=*/0);
6598 TYPE_SIZE_UNIT (base_t)
6599 = size_binop (MAX_EXPR,
6600 fold_convert (sizetype,
6601 size_binop (CEIL_DIV_EXPR,
6602 rli_size_so_far (rli),
6603 bitsize_int (BITS_PER_UNIT))),
6604 eoc);
6605 TYPE_SIZE (base_t)
6606 = size_binop (MAX_EXPR,
6607 rli_size_so_far (rli),
6608 size_binop (MULT_EXPR,
6609 fold_convert (bitsizetype, eoc),
6610 bitsize_int (BITS_PER_UNIT)));
6611 SET_TYPE_ALIGN (base_t, rli->record_align);
6612 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6614 /* Copy the fields from T. */
6615 next_field = &TYPE_FIELDS (base_t);
6616 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6617 if (TREE_CODE (field) == FIELD_DECL)
6619 *next_field = copy_node (field);
6620 DECL_CONTEXT (*next_field) = base_t;
6621 next_field = &DECL_CHAIN (*next_field);
6623 *next_field = NULL_TREE;
6625 /* Record the base version of the type. */
6626 CLASSTYPE_AS_BASE (t) = base_t;
6627 TYPE_CONTEXT (base_t) = t;
6629 else
6630 CLASSTYPE_AS_BASE (t) = t;
6632 /* Every empty class contains an empty class. */
6633 if (CLASSTYPE_EMPTY_P (t))
6634 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6636 /* Set the TYPE_DECL for this type to contain the right
6637 value for DECL_OFFSET, so that we can use it as part
6638 of a COMPONENT_REF for multiple inheritance. */
6639 layout_decl (TYPE_MAIN_DECL (t), 0);
6641 /* Now fix up any virtual base class types that we left lying
6642 around. We must get these done before we try to lay out the
6643 virtual function table. As a side-effect, this will remove the
6644 base subobject fields. */
6645 layout_virtual_bases (rli, empty_base_offsets);
6647 /* Make sure that empty classes are reflected in RLI at this
6648 point. */
6649 include_empty_classes (rli);
6651 /* Make sure not to create any structures with zero size. */
6652 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6653 place_field (rli,
6654 build_decl (input_location,
6655 FIELD_DECL, NULL_TREE, char_type_node));
6657 /* If this is a non-POD, declaring it packed makes a difference to how it
6658 can be used as a field; don't let finalize_record_size undo it. */
6659 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6660 rli->packed_maybe_necessary = true;
6662 /* Let the back end lay out the type. */
6663 finish_record_layout (rli, /*free_p=*/true);
6665 if (TYPE_SIZE_UNIT (t)
6666 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6667 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6668 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6669 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6671 /* Warn about bases that can't be talked about due to ambiguity. */
6672 warn_about_ambiguous_bases (t);
6674 /* Now that we're done with layout, give the base fields the real types. */
6675 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6676 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6677 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6679 /* Clean up. */
6680 splay_tree_delete (empty_base_offsets);
6682 if (CLASSTYPE_EMPTY_P (t)
6683 && tree_int_cst_lt (sizeof_biggest_empty_class,
6684 TYPE_SIZE_UNIT (t)))
6685 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6688 /* Determine the "key method" for the class type indicated by TYPE,
6689 and set CLASSTYPE_KEY_METHOD accordingly. */
6691 void
6692 determine_key_method (tree type)
6694 tree method;
6696 if (processing_template_decl
6697 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6698 || CLASSTYPE_INTERFACE_KNOWN (type))
6699 return;
6701 /* The key method is the first non-pure virtual function that is not
6702 inline at the point of class definition. On some targets the
6703 key function may not be inline; those targets should not call
6704 this function until the end of the translation unit. */
6705 for (method = TYPE_METHODS (type); method != NULL_TREE;
6706 method = DECL_CHAIN (method))
6707 if (TREE_CODE (method) == FUNCTION_DECL
6708 && DECL_VINDEX (method) != NULL_TREE
6709 && ! DECL_DECLARED_INLINE_P (method)
6710 && ! DECL_PURE_VIRTUAL_P (method))
6712 CLASSTYPE_KEY_METHOD (type) = method;
6713 break;
6716 return;
6720 /* Allocate and return an instance of struct sorted_fields_type with
6721 N fields. */
6723 static struct sorted_fields_type *
6724 sorted_fields_type_new (int n)
6726 struct sorted_fields_type *sft;
6727 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type)
6728 + n * sizeof (tree));
6729 sft->len = n;
6731 return sft;
6734 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6735 class data member of non-zero size, otherwise false. */
6737 static inline bool
6738 field_nonempty_p (const_tree fld)
6740 if (TREE_CODE (fld) == ERROR_MARK)
6741 return false;
6743 tree type = TREE_TYPE (fld);
6744 if (TREE_CODE (fld) == FIELD_DECL
6745 && TREE_CODE (type) != ERROR_MARK
6746 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6748 return TYPE_SIZE (type)
6749 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6750 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6753 return false;
6756 /* Used by find_flexarrays and related functions. */
6758 struct flexmems_t
6760 /* The first flexible array member or non-zero array member found
6761 in the order of layout. */
6762 tree array;
6763 /* First non-static non-empty data member in the class or its bases. */
6764 tree first;
6765 /* The first non-static non-empty data member following either
6766 the flexible array member, if found, or the zero-length array member
6767 otherwise. AFTER[1] refers to the first such data member of a union
6768 of which the struct containing the flexible array member or zero-length
6769 array is a member, or NULL when no such union exists. This element is
6770 only used during searching, not for diagnosing problems. AFTER[0]
6771 refers to the first such data member that is not a member of such
6772 a union. */
6773 tree after[2];
6775 /* Refers to a struct (not union) in which the struct of which the flexible
6776 array is member is defined. Used to diagnose strictly (according to C)
6777 invalid uses of the latter structs. */
6778 tree enclosing;
6781 /* Find either the first flexible array member or the first zero-length
6782 array, in that order of preference, among members of class T (but not
6783 its base classes), and set members of FMEM accordingly.
6784 BASE_P is true if T is a base class of another class.
6785 PUN is set to the outermost union in which the flexible array member
6786 (or zero-length array) is defined if one such union exists, otherwise
6787 to NULL.
6788 Similarly, PSTR is set to a data member of the outermost struct of
6789 which the flexible array is a member if one such struct exists,
6790 otherwise to NULL. */
6792 static void
6793 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6794 tree pun /* = NULL_TREE */,
6795 tree pstr /* = NULL_TREE */)
6797 /* Set the "pointer" to the outermost enclosing union if not set
6798 yet and maintain it for the remainder of the recursion. */
6799 if (!pun && TREE_CODE (t) == UNION_TYPE)
6800 pun = t;
6802 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6804 if (fld == error_mark_node)
6805 return;
6807 /* Is FLD a typedef for an anonymous struct? */
6809 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6810 handled elsewhere so that errors like the following are detected
6811 as well:
6812 typedef struct { int i, a[], j; } S; // bug c++/72753
6813 S s [2]; // bug c++/68489
6815 if (TREE_CODE (fld) == TYPE_DECL
6816 && DECL_IMPLICIT_TYPEDEF_P (fld)
6817 && CLASS_TYPE_P (TREE_TYPE (fld))
6818 && anon_aggrname_p (DECL_NAME (fld)))
6820 /* Check the nested unnamed type referenced via a typedef
6821 independently of FMEM (since it's not a data member of
6822 the enclosing class). */
6823 check_flexarrays (TREE_TYPE (fld));
6824 continue;
6827 /* Skip anything that's GCC-generated or not a (non-static) data
6828 member. */
6829 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6830 continue;
6832 /* Type of the member. */
6833 tree fldtype = TREE_TYPE (fld);
6834 if (fldtype == error_mark_node)
6835 return;
6837 /* Determine the type of the array element or object referenced
6838 by the member so that it can be checked for flexible array
6839 members if it hasn't been yet. */
6840 tree eltype = fldtype;
6841 while (TREE_CODE (eltype) == ARRAY_TYPE
6842 || TREE_CODE (eltype) == POINTER_TYPE
6843 || TREE_CODE (eltype) == REFERENCE_TYPE)
6844 eltype = TREE_TYPE (eltype);
6846 if (RECORD_OR_UNION_TYPE_P (eltype))
6848 if (fmem->array && !fmem->after[bool (pun)])
6850 /* Once the member after the flexible array has been found
6851 we're done. */
6852 fmem->after[bool (pun)] = fld;
6853 break;
6856 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6858 /* Descend into the non-static member struct or union and try
6859 to find a flexible array member or zero-length array among
6860 its members. This is only necessary for anonymous types
6861 and types in whose context the current type T has not been
6862 defined (the latter must not be checked again because they
6863 are already in the process of being checked by one of the
6864 recursive calls). */
6866 tree first = fmem->first;
6867 tree array = fmem->array;
6869 /* If this member isn't anonymous and a prior non-flexible array
6870 member has been seen in one of the enclosing structs, clear
6871 the FIRST member since it doesn't contribute to the flexible
6872 array struct's members. */
6873 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6874 fmem->first = NULL_TREE;
6876 find_flexarrays (eltype, fmem, false, pun,
6877 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6879 if (fmem->array != array)
6880 continue;
6882 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6884 /* Restore the FIRST member reset above if no flexible
6885 array member has been found in this member's struct. */
6886 fmem->first = first;
6889 /* If the member struct contains the first flexible array
6890 member, or if this member is a base class, continue to
6891 the next member and avoid setting the FMEM->NEXT pointer
6892 to point to it. */
6893 if (base_p)
6894 continue;
6898 if (field_nonempty_p (fld))
6900 /* Remember the first non-static data member. */
6901 if (!fmem->first)
6902 fmem->first = fld;
6904 /* Remember the first non-static data member after the flexible
6905 array member, if one has been found, or the zero-length array
6906 if it has been found. */
6907 if (fmem->array && !fmem->after[bool (pun)])
6908 fmem->after[bool (pun)] = fld;
6911 /* Skip non-arrays. */
6912 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6913 continue;
6915 /* Determine the upper bound of the array if it has one. */
6916 if (TYPE_DOMAIN (fldtype))
6918 if (fmem->array)
6920 /* Make a record of the zero-length array if either one
6921 such field or a flexible array member has been seen to
6922 handle the pathological and unlikely case of multiple
6923 such members. */
6924 if (!fmem->after[bool (pun)])
6925 fmem->after[bool (pun)] = fld;
6927 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6929 /* Remember the first zero-length array unless a flexible array
6930 member has already been seen. */
6931 fmem->array = fld;
6932 fmem->enclosing = pstr;
6935 else
6937 /* Flexible array members have no upper bound. */
6938 if (fmem->array)
6940 /* Replace the zero-length array if it's been stored and
6941 reset the after pointer. */
6942 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6944 fmem->after[bool (pun)] = NULL_TREE;
6945 fmem->array = fld;
6946 fmem->enclosing = pstr;
6949 else
6951 fmem->array = fld;
6952 fmem->enclosing = pstr;
6958 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6959 a flexible array member (or the zero-length array extension). */
6961 static void
6962 diagnose_invalid_flexarray (const flexmems_t *fmem)
6964 if (fmem->array && fmem->enclosing
6965 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6966 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6967 ? G_("invalid use of %q#T with a zero-size array "
6968 "in %q#D")
6969 : G_("invalid use of %q#T with a flexible array member "
6970 "in %q#T"),
6971 DECL_CONTEXT (fmem->array),
6972 DECL_CONTEXT (fmem->enclosing)))
6973 inform (DECL_SOURCE_LOCATION (fmem->array),
6974 "array member %q#D declared here", fmem->array);
6977 /* Issue diagnostics for invalid flexible array members or zero-length
6978 arrays that are not the last elements of the containing class or its
6979 base classes or that are its sole members. */
6981 static void
6982 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6984 if (!fmem->array)
6985 return;
6987 if (fmem->first && !fmem->after[0])
6989 diagnose_invalid_flexarray (fmem);
6990 return;
6993 /* Has a diagnostic been issued? */
6994 bool diagd = false;
6996 const char *msg = 0;
6998 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
7000 if (fmem->after[0])
7001 msg = G_("zero-size array member %qD not at end of %q#T");
7002 else if (!fmem->first)
7003 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
7005 if (msg)
7007 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7009 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
7011 inform (location_of (t), "in the definition of %q#T", t);
7012 diagd = true;
7016 else
7018 if (fmem->after[0])
7019 msg = G_("flexible array member %qD not at end of %q#T");
7020 else if (!fmem->first)
7021 msg = G_("flexible array member %qD in an otherwise empty %q#T");
7023 if (msg)
7025 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
7026 diagd = true;
7028 error_at (loc, msg, fmem->array, t);
7030 /* In the unlikely event that the member following the flexible
7031 array member is declared in a different class, or the member
7032 overlaps another member of a common union, point to it.
7033 Otherwise it should be obvious. */
7034 if (fmem->after[0]
7035 && ((DECL_CONTEXT (fmem->after[0])
7036 != DECL_CONTEXT (fmem->array))))
7038 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
7039 "next member %q#D declared here",
7040 fmem->after[0]);
7041 inform (location_of (t), "in the definition of %q#T", t);
7046 if (!diagd && fmem->array && fmem->enclosing)
7047 diagnose_invalid_flexarray (fmem);
7051 /* Recursively check to make sure that any flexible array or zero-length
7052 array members of class T or its bases are valid (i.e., not the sole
7053 non-static data member of T and, if one exists, that it is the last
7054 non-static data member of T and its base classes. FMEM is expected
7055 to be initially null and is used internally by recursive calls to
7056 the function. Issue the appropriate diagnostics for the array member
7057 that fails the checks. */
7059 static void
7060 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
7061 bool base_p /* = false */)
7063 /* Initialize the result of a search for flexible array and zero-length
7064 array members. Avoid doing any work if the most interesting FMEM data
7065 have already been populated. */
7066 flexmems_t flexmems = flexmems_t ();
7067 if (!fmem)
7068 fmem = &flexmems;
7069 else if (fmem->array && fmem->first && fmem->after[0])
7070 return;
7072 tree fam = fmem->array;
7074 /* Recursively check the primary base class first. */
7075 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7077 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
7078 check_flexarrays (basetype, fmem, true);
7081 /* Recursively check the base classes. */
7082 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
7083 for (int i = 0; i < nbases; ++i)
7085 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
7087 /* The primary base class was already checked above. */
7088 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
7089 continue;
7091 /* Virtual base classes are at the end. */
7092 if (BINFO_VIRTUAL_P (base_binfo))
7093 continue;
7095 /* Check the base class. */
7096 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
7099 if (fmem == &flexmems)
7101 /* Check virtual base classes only once per derived class.
7102 I.e., this check is not performed recursively for base
7103 classes. */
7104 int i;
7105 tree base_binfo;
7106 vec<tree, va_gc> *vbases;
7107 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
7108 vec_safe_iterate (vbases, i, &base_binfo); i++)
7110 /* Check the virtual base class. */
7111 tree basetype = TREE_TYPE (base_binfo);
7113 check_flexarrays (basetype, fmem, /*base_p=*/true);
7117 /* Is the type unnamed (and therefore a member of it potentially
7118 an anonymous struct or union)? */
7119 bool maybe_anon_p = TYPE_UNNAMED_P (t);
7121 /* Search the members of the current (possibly derived) class, skipping
7122 unnamed structs and unions since those could be anonymous. */
7123 if (fmem != &flexmems || !maybe_anon_p)
7124 find_flexarrays (t, fmem, base_p || fam != fmem->array);
7126 if (fmem == &flexmems && !maybe_anon_p)
7128 /* Issue diagnostics for invalid flexible and zero-length array
7129 members found in base classes or among the members of the current
7130 class. Ignore anonymous structs and unions whose members are
7131 considered to be members of the enclosing class and thus will
7132 be diagnosed when checking it. */
7133 diagnose_flexarrays (t, fmem);
7137 /* Perform processing required when the definition of T (a class type)
7138 is complete. Diagnose invalid definitions of flexible array members
7139 and zero-size arrays. */
7141 void
7142 finish_struct_1 (tree t)
7144 tree x;
7145 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
7146 tree virtuals = NULL_TREE;
7148 if (COMPLETE_TYPE_P (t))
7150 gcc_assert (MAYBE_CLASS_TYPE_P (t));
7151 error ("redefinition of %q#T", t);
7152 popclass ();
7153 return;
7156 /* If this type was previously laid out as a forward reference,
7157 make sure we lay it out again. */
7158 TYPE_SIZE (t) = NULL_TREE;
7159 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
7161 /* Make assumptions about the class; we'll reset the flags if
7162 necessary. */
7163 CLASSTYPE_EMPTY_P (t) = 1;
7164 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
7165 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
7166 CLASSTYPE_LITERAL_P (t) = true;
7168 /* Do end-of-class semantic processing: checking the validity of the
7169 bases and members and add implicitly generated methods. */
7170 check_bases_and_members (t);
7172 /* Find the key method. */
7173 if (TYPE_CONTAINS_VPTR_P (t))
7175 /* The Itanium C++ ABI permits the key method to be chosen when
7176 the class is defined -- even though the key method so
7177 selected may later turn out to be an inline function. On
7178 some systems (such as ARM Symbian OS) the key method cannot
7179 be determined until the end of the translation unit. On such
7180 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
7181 will cause the class to be added to KEYED_CLASSES. Then, in
7182 finish_file we will determine the key method. */
7183 if (targetm.cxx.key_method_may_be_inline ())
7184 determine_key_method (t);
7186 /* If a polymorphic class has no key method, we may emit the vtable
7187 in every translation unit where the class definition appears. If
7188 we're devirtualizing, we can look into the vtable even if we
7189 aren't emitting it. */
7190 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
7191 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
7194 /* Layout the class itself. */
7195 layout_class_type (t, &virtuals);
7196 if (CLASSTYPE_AS_BASE (t) != t)
7197 /* We use the base type for trivial assignments, and hence it
7198 needs a mode. */
7199 compute_record_mode (CLASSTYPE_AS_BASE (t));
7201 /* With the layout complete, check for flexible array members and
7202 zero-length arrays that might overlap other members in the final
7203 layout. */
7204 check_flexarrays (t);
7206 virtuals = modify_all_vtables (t, nreverse (virtuals));
7208 /* If necessary, create the primary vtable for this class. */
7209 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
7211 /* We must enter these virtuals into the table. */
7212 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7213 build_primary_vtable (NULL_TREE, t);
7214 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
7215 /* Here we know enough to change the type of our virtual
7216 function table, but we will wait until later this function. */
7217 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
7219 /* If we're warning about ABI tags, check the types of the new
7220 virtual functions. */
7221 if (warn_abi_tag)
7222 for (tree v = virtuals; v; v = TREE_CHAIN (v))
7223 check_abi_tags (t, TREE_VALUE (v));
7226 if (TYPE_CONTAINS_VPTR_P (t))
7228 int vindex;
7229 tree fn;
7231 if (BINFO_VTABLE (TYPE_BINFO (t)))
7232 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
7233 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
7234 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
7236 /* Add entries for virtual functions introduced by this class. */
7237 BINFO_VIRTUALS (TYPE_BINFO (t))
7238 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
7240 /* Set DECL_VINDEX for all functions declared in this class. */
7241 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
7243 fn = TREE_CHAIN (fn),
7244 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
7245 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
7247 tree fndecl = BV_FN (fn);
7249 if (DECL_THUNK_P (fndecl))
7250 /* A thunk. We should never be calling this entry directly
7251 from this vtable -- we'd use the entry for the non
7252 thunk base function. */
7253 DECL_VINDEX (fndecl) = NULL_TREE;
7254 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
7255 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
7259 finish_struct_bits (t);
7260 set_method_tm_attributes (t);
7261 if (flag_openmp || flag_openmp_simd)
7262 finish_omp_declare_simd_methods (t);
7264 /* Complete the rtl for any static member objects of the type we're
7265 working on. */
7266 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7267 if (VAR_P (x) && TREE_STATIC (x)
7268 && TREE_TYPE (x) != error_mark_node
7269 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
7270 SET_DECL_MODE (x, TYPE_MODE (t));
7272 /* Done with FIELDS...now decide whether to sort these for
7273 faster lookups later.
7275 We use a small number because most searches fail (succeeding
7276 ultimately as the search bores through the inheritance
7277 hierarchy), and we want this failure to occur quickly. */
7279 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
7281 /* Complain if one of the field types requires lower visibility. */
7282 constrain_class_visibility (t);
7284 /* Make the rtl for any new vtables we have created, and unmark
7285 the base types we marked. */
7286 finish_vtbls (t);
7288 /* Build the VTT for T. */
7289 build_vtt (t);
7291 if (warn_nonvdtor
7292 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
7293 && !CLASSTYPE_FINAL (t))
7294 warning (OPT_Wnon_virtual_dtor,
7295 "%q#T has virtual functions and accessible"
7296 " non-virtual destructor", t);
7298 complete_vars (t);
7300 if (warn_overloaded_virtual)
7301 warn_hidden (t);
7303 /* Class layout, assignment of virtual table slots, etc., is now
7304 complete. Give the back end a chance to tweak the visibility of
7305 the class or perform any other required target modifications. */
7306 targetm.cxx.adjust_class_at_definition (t);
7308 maybe_suppress_debug_info (t);
7310 if (flag_vtable_verify)
7311 vtv_save_class_info (t);
7313 dump_class_hierarchy (t);
7315 /* Finish debugging output for this type. */
7316 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
7318 if (TYPE_TRANSPARENT_AGGR (t))
7320 tree field = first_field (t);
7321 if (field == NULL_TREE || error_operand_p (field))
7323 error ("type transparent %q#T does not have any fields", t);
7324 TYPE_TRANSPARENT_AGGR (t) = 0;
7326 else if (DECL_ARTIFICIAL (field))
7328 if (DECL_FIELD_IS_BASE (field))
7329 error ("type transparent class %qT has base classes", t);
7330 else
7332 gcc_checking_assert (DECL_VIRTUAL_P (field));
7333 error ("type transparent class %qT has virtual functions", t);
7335 TYPE_TRANSPARENT_AGGR (t) = 0;
7337 else if (TYPE_MODE (t) != DECL_MODE (field))
7339 error ("type transparent %q#T cannot be made transparent because "
7340 "the type of the first field has a different ABI from the "
7341 "class overall", t);
7342 TYPE_TRANSPARENT_AGGR (t) = 0;
7347 /* Insert FIELDS into T for the sorted case if the FIELDS count is
7348 equal to THRESHOLD or greater than THRESHOLD. */
7350 static void
7351 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
7353 int n_fields = count_fields (fields);
7354 if (n_fields >= threshold)
7356 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7357 add_fields_to_record_type (fields, field_vec, 0);
7358 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7359 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7363 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */
7365 void
7366 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
7368 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
7369 if (sorted_fields)
7371 int i;
7372 int n_fields
7373 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
7374 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
7376 for (i = 0; i < sorted_fields->len; ++i)
7377 field_vec->elts[i] = sorted_fields->elts[i];
7379 add_enum_fields_to_record_type (enumtype, field_vec,
7380 sorted_fields->len);
7381 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
7382 CLASSTYPE_SORTED_FIELDS (t) = field_vec;
7386 /* When T was built up, the member declarations were added in reverse
7387 order. Rearrange them to declaration order. */
7389 void
7390 unreverse_member_declarations (tree t)
7392 tree next;
7393 tree prev;
7394 tree x;
7396 /* The following lists are all in reverse order. Put them in
7397 declaration order now. */
7398 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
7399 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7401 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
7402 reverse order, so we can't just use nreverse. */
7403 prev = NULL_TREE;
7404 for (x = TYPE_FIELDS (t);
7405 x && TREE_CODE (x) != TYPE_DECL;
7406 x = next)
7408 next = DECL_CHAIN (x);
7409 DECL_CHAIN (x) = prev;
7410 prev = x;
7412 if (prev)
7414 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7415 if (prev)
7416 TYPE_FIELDS (t) = prev;
7420 tree
7421 finish_struct (tree t, tree attributes)
7423 location_t saved_loc = input_location;
7425 /* Now that we've got all the field declarations, reverse everything
7426 as necessary. */
7427 unreverse_member_declarations (t);
7429 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7430 fixup_attribute_variants (t);
7432 /* Nadger the current location so that diagnostics point to the start of
7433 the struct, not the end. */
7434 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7436 if (processing_template_decl)
7438 tree x;
7440 finish_struct_methods (t);
7441 TYPE_SIZE (t) = bitsize_zero_node;
7442 TYPE_SIZE_UNIT (t) = size_zero_node;
7444 /* We need to emit an error message if this type was used as a parameter
7445 and it is an abstract type, even if it is a template. We construct
7446 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7447 account and we call complete_vars with this type, which will check
7448 the PARM_DECLS. Note that while the type is being defined,
7449 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7450 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7451 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7452 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
7453 if (DECL_PURE_VIRTUAL_P (x))
7454 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7455 complete_vars (t);
7456 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if
7457 an enclosing scope is a template class, so that this function be
7458 found by lookup_fnfields_1 when the using declaration is not
7459 instantiated yet. */
7460 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7461 if (TREE_CODE (x) == USING_DECL)
7463 tree fn = strip_using_decl (x);
7464 if (is_overloaded_fn (fn))
7465 for (lkp_iterator iter (fn); iter; ++iter)
7466 add_method (t, *iter, true);
7469 /* Remember current #pragma pack value. */
7470 TYPE_PRECISION (t) = maximum_field_alignment;
7472 /* Fix up any variants we've already built. */
7473 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7475 TYPE_SIZE (x) = TYPE_SIZE (t);
7476 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7477 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7478 TYPE_METHODS (x) = TYPE_METHODS (t);
7481 else
7482 finish_struct_1 (t);
7484 if (is_std_init_list (t))
7486 /* People keep complaining that the compiler crashes on an invalid
7487 definition of initializer_list, so I guess we should explicitly
7488 reject it. What the compiler internals care about is that it's a
7489 template and has a pointer field followed by an integer field. */
7490 bool ok = false;
7491 if (processing_template_decl)
7493 tree f = next_initializable_field (TYPE_FIELDS (t));
7494 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7496 f = next_initializable_field (DECL_CHAIN (f));
7497 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7498 ok = true;
7501 if (!ok)
7502 fatal_error (input_location,
7503 "definition of std::initializer_list does not match "
7504 "#include <initializer_list>");
7507 input_location = saved_loc;
7509 TYPE_BEING_DEFINED (t) = 0;
7511 if (current_class_type)
7512 popclass ();
7513 else
7514 error ("trying to finish struct, but kicked out due to previous parse errors");
7516 if (processing_template_decl && at_function_scope_p ()
7517 /* Lambdas are defined by the LAMBDA_EXPR. */
7518 && !LAMBDA_TYPE_P (t))
7519 add_stmt (build_min (TAG_DEFN, t));
7521 return t;
7524 /* Hash table to avoid endless recursion when handling references. */
7525 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7527 /* Return the dynamic type of INSTANCE, if known.
7528 Used to determine whether the virtual function table is needed
7529 or not.
7531 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7532 of our knowledge of its type. *NONNULL should be initialized
7533 before this function is called. */
7535 static tree
7536 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7538 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7540 switch (TREE_CODE (instance))
7542 case INDIRECT_REF:
7543 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7544 return NULL_TREE;
7545 else
7546 return RECUR (TREE_OPERAND (instance, 0));
7548 case CALL_EXPR:
7549 /* This is a call to a constructor, hence it's never zero. */
7550 if (TREE_HAS_CONSTRUCTOR (instance))
7552 if (nonnull)
7553 *nonnull = 1;
7554 return TREE_TYPE (instance);
7556 return NULL_TREE;
7558 case SAVE_EXPR:
7559 /* This is a call to a constructor, hence it's never zero. */
7560 if (TREE_HAS_CONSTRUCTOR (instance))
7562 if (nonnull)
7563 *nonnull = 1;
7564 return TREE_TYPE (instance);
7566 return RECUR (TREE_OPERAND (instance, 0));
7568 case POINTER_PLUS_EXPR:
7569 case PLUS_EXPR:
7570 case MINUS_EXPR:
7571 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7572 return RECUR (TREE_OPERAND (instance, 0));
7573 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7574 /* Propagate nonnull. */
7575 return RECUR (TREE_OPERAND (instance, 0));
7577 return NULL_TREE;
7579 CASE_CONVERT:
7580 return RECUR (TREE_OPERAND (instance, 0));
7582 case ADDR_EXPR:
7583 instance = TREE_OPERAND (instance, 0);
7584 if (nonnull)
7586 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7587 with a real object -- given &p->f, p can still be null. */
7588 tree t = get_base_address (instance);
7589 /* ??? Probably should check DECL_WEAK here. */
7590 if (t && DECL_P (t))
7591 *nonnull = 1;
7593 return RECUR (instance);
7595 case COMPONENT_REF:
7596 /* If this component is really a base class reference, then the field
7597 itself isn't definitive. */
7598 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7599 return RECUR (TREE_OPERAND (instance, 0));
7600 return RECUR (TREE_OPERAND (instance, 1));
7602 case VAR_DECL:
7603 case FIELD_DECL:
7604 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7605 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7607 if (nonnull)
7608 *nonnull = 1;
7609 return TREE_TYPE (TREE_TYPE (instance));
7611 /* fall through. */
7612 case TARGET_EXPR:
7613 case PARM_DECL:
7614 case RESULT_DECL:
7615 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7617 if (nonnull)
7618 *nonnull = 1;
7619 return TREE_TYPE (instance);
7621 else if (instance == current_class_ptr)
7623 if (nonnull)
7624 *nonnull = 1;
7626 /* if we're in a ctor or dtor, we know our type. If
7627 current_class_ptr is set but we aren't in a function, we're in
7628 an NSDMI (and therefore a constructor). */
7629 if (current_scope () != current_function_decl
7630 || (DECL_LANG_SPECIFIC (current_function_decl)
7631 && (DECL_CONSTRUCTOR_P (current_function_decl)
7632 || DECL_DESTRUCTOR_P (current_function_decl))))
7634 if (cdtorp)
7635 *cdtorp = 1;
7636 return TREE_TYPE (TREE_TYPE (instance));
7639 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7641 /* We only need one hash table because it is always left empty. */
7642 if (!fixed_type_or_null_ref_ht)
7643 fixed_type_or_null_ref_ht
7644 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7646 /* Reference variables should be references to objects. */
7647 if (nonnull)
7648 *nonnull = 1;
7650 /* Enter the INSTANCE in a table to prevent recursion; a
7651 variable's initializer may refer to the variable
7652 itself. */
7653 if (VAR_P (instance)
7654 && DECL_INITIAL (instance)
7655 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7656 && !fixed_type_or_null_ref_ht->find (instance))
7658 tree type;
7659 tree_node **slot;
7661 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7662 *slot = instance;
7663 type = RECUR (DECL_INITIAL (instance));
7664 fixed_type_or_null_ref_ht->remove_elt (instance);
7666 return type;
7669 return NULL_TREE;
7671 default:
7672 return NULL_TREE;
7674 #undef RECUR
7677 /* Return nonzero if the dynamic type of INSTANCE is known, and
7678 equivalent to the static type. We also handle the case where
7679 INSTANCE is really a pointer. Return negative if this is a
7680 ctor/dtor. There the dynamic type is known, but this might not be
7681 the most derived base of the original object, and hence virtual
7682 bases may not be laid out according to this type.
7684 Used to determine whether the virtual function table is needed
7685 or not.
7687 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7688 of our knowledge of its type. *NONNULL should be initialized
7689 before this function is called. */
7692 resolves_to_fixed_type_p (tree instance, int* nonnull)
7694 tree t = TREE_TYPE (instance);
7695 int cdtorp = 0;
7696 tree fixed;
7698 /* processing_template_decl can be false in a template if we're in
7699 instantiate_non_dependent_expr, but we still want to suppress
7700 this check. */
7701 if (in_template_function ())
7703 /* In a template we only care about the type of the result. */
7704 if (nonnull)
7705 *nonnull = true;
7706 return true;
7709 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7710 if (fixed == NULL_TREE)
7711 return 0;
7712 if (POINTER_TYPE_P (t))
7713 t = TREE_TYPE (t);
7714 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7715 return 0;
7716 return cdtorp ? -1 : 1;
7720 void
7721 init_class_processing (void)
7723 current_class_depth = 0;
7724 current_class_stack_size = 10;
7725 current_class_stack
7726 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7727 vec_alloc (local_classes, 8);
7728 sizeof_biggest_empty_class = size_zero_node;
7730 ridpointers[(int) RID_PUBLIC] = access_public_node;
7731 ridpointers[(int) RID_PRIVATE] = access_private_node;
7732 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7735 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7737 static void
7738 restore_class_cache (void)
7740 tree type;
7742 /* We are re-entering the same class we just left, so we don't
7743 have to search the whole inheritance matrix to find all the
7744 decls to bind again. Instead, we install the cached
7745 class_shadowed list and walk through it binding names. */
7746 push_binding_level (previous_class_level);
7747 class_binding_level = previous_class_level;
7748 /* Restore IDENTIFIER_TYPE_VALUE. */
7749 for (type = class_binding_level->type_shadowed;
7750 type;
7751 type = TREE_CHAIN (type))
7752 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7755 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7756 appropriate for TYPE.
7758 So that we may avoid calls to lookup_name, we cache the _TYPE
7759 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7761 For multiple inheritance, we perform a two-pass depth-first search
7762 of the type lattice. */
7764 void
7765 pushclass (tree type)
7767 class_stack_node_t csn;
7769 type = TYPE_MAIN_VARIANT (type);
7771 /* Make sure there is enough room for the new entry on the stack. */
7772 if (current_class_depth + 1 >= current_class_stack_size)
7774 current_class_stack_size *= 2;
7775 current_class_stack
7776 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7777 current_class_stack_size);
7780 /* Insert a new entry on the class stack. */
7781 csn = current_class_stack + current_class_depth;
7782 csn->name = current_class_name;
7783 csn->type = current_class_type;
7784 csn->access = current_access_specifier;
7785 csn->names_used = 0;
7786 csn->hidden = 0;
7787 current_class_depth++;
7789 /* Now set up the new type. */
7790 current_class_name = TYPE_NAME (type);
7791 if (TREE_CODE (current_class_name) == TYPE_DECL)
7792 current_class_name = DECL_NAME (current_class_name);
7793 current_class_type = type;
7795 /* By default, things in classes are private, while things in
7796 structures or unions are public. */
7797 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7798 ? access_private_node
7799 : access_public_node);
7801 if (previous_class_level
7802 && type != previous_class_level->this_entity
7803 && current_class_depth == 1)
7805 /* Forcibly remove any old class remnants. */
7806 invalidate_class_lookup_cache ();
7809 if (!previous_class_level
7810 || type != previous_class_level->this_entity
7811 || current_class_depth > 1)
7812 pushlevel_class ();
7813 else
7814 restore_class_cache ();
7817 /* When we exit a toplevel class scope, we save its binding level so
7818 that we can restore it quickly. Here, we've entered some other
7819 class, so we must invalidate our cache. */
7821 void
7822 invalidate_class_lookup_cache (void)
7824 previous_class_level = NULL;
7827 /* Get out of the current class scope. If we were in a class scope
7828 previously, that is the one popped to. */
7830 void
7831 popclass (void)
7833 poplevel_class ();
7835 current_class_depth--;
7836 current_class_name = current_class_stack[current_class_depth].name;
7837 current_class_type = current_class_stack[current_class_depth].type;
7838 current_access_specifier = current_class_stack[current_class_depth].access;
7839 if (current_class_stack[current_class_depth].names_used)
7840 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7843 /* Mark the top of the class stack as hidden. */
7845 void
7846 push_class_stack (void)
7848 if (current_class_depth)
7849 ++current_class_stack[current_class_depth - 1].hidden;
7852 /* Mark the top of the class stack as un-hidden. */
7854 void
7855 pop_class_stack (void)
7857 if (current_class_depth)
7858 --current_class_stack[current_class_depth - 1].hidden;
7861 /* Returns 1 if the class type currently being defined is either T or
7862 a nested type of T. Returns the type from the current_class_stack,
7863 which might be equivalent to but not equal to T in case of
7864 constrained partial specializations. */
7866 tree
7867 currently_open_class (tree t)
7869 int i;
7871 if (!CLASS_TYPE_P (t))
7872 return NULL_TREE;
7874 t = TYPE_MAIN_VARIANT (t);
7876 /* We start looking from 1 because entry 0 is from global scope,
7877 and has no type. */
7878 for (i = current_class_depth; i > 0; --i)
7880 tree c;
7881 if (i == current_class_depth)
7882 c = current_class_type;
7883 else
7885 if (current_class_stack[i].hidden)
7886 break;
7887 c = current_class_stack[i].type;
7889 if (!c)
7890 continue;
7891 if (same_type_p (c, t))
7892 return c;
7894 return NULL_TREE;
7897 /* If either current_class_type or one of its enclosing classes are derived
7898 from T, return the appropriate type. Used to determine how we found
7899 something via unqualified lookup. */
7901 tree
7902 currently_open_derived_class (tree t)
7904 int i;
7906 /* The bases of a dependent type are unknown. */
7907 if (dependent_type_p (t))
7908 return NULL_TREE;
7910 if (!current_class_type)
7911 return NULL_TREE;
7913 if (DERIVED_FROM_P (t, current_class_type))
7914 return current_class_type;
7916 for (i = current_class_depth - 1; i > 0; --i)
7918 if (current_class_stack[i].hidden)
7919 break;
7920 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7921 return current_class_stack[i].type;
7924 return NULL_TREE;
7927 /* Return the outermost enclosing class type that is still open, or
7928 NULL_TREE. */
7930 tree
7931 outermost_open_class (void)
7933 if (!current_class_type)
7934 return NULL_TREE;
7935 tree r = NULL_TREE;
7936 if (TYPE_BEING_DEFINED (current_class_type))
7937 r = current_class_type;
7938 for (int i = current_class_depth - 1; i > 0; --i)
7940 if (current_class_stack[i].hidden)
7941 break;
7942 tree t = current_class_stack[i].type;
7943 if (!TYPE_BEING_DEFINED (t))
7944 break;
7945 r = t;
7947 return r;
7950 /* Returns the innermost class type which is not a lambda closure type. */
7952 tree
7953 current_nonlambda_class_type (void)
7955 int i;
7957 /* We start looking from 1 because entry 0 is from global scope,
7958 and has no type. */
7959 for (i = current_class_depth; i > 0; --i)
7961 tree c;
7962 if (i == current_class_depth)
7963 c = current_class_type;
7964 else
7966 if (current_class_stack[i].hidden)
7967 break;
7968 c = current_class_stack[i].type;
7970 if (!c)
7971 continue;
7972 if (!LAMBDA_TYPE_P (c))
7973 return c;
7975 return NULL_TREE;
7978 /* When entering a class scope, all enclosing class scopes' names with
7979 static meaning (static variables, static functions, types and
7980 enumerators) have to be visible. This recursive function calls
7981 pushclass for all enclosing class contexts until global or a local
7982 scope is reached. TYPE is the enclosed class. */
7984 void
7985 push_nested_class (tree type)
7987 /* A namespace might be passed in error cases, like A::B:C. */
7988 if (type == NULL_TREE
7989 || !CLASS_TYPE_P (type))
7990 return;
7992 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7994 pushclass (type);
7997 /* Undoes a push_nested_class call. */
7999 void
8000 pop_nested_class (void)
8002 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
8004 popclass ();
8005 if (context && CLASS_TYPE_P (context))
8006 pop_nested_class ();
8009 /* Returns the number of extern "LANG" blocks we are nested within. */
8012 current_lang_depth (void)
8014 return vec_safe_length (current_lang_base);
8017 /* Set global variables CURRENT_LANG_NAME to appropriate value
8018 so that behavior of name-mangling machinery is correct. */
8020 void
8021 push_lang_context (tree name)
8023 vec_safe_push (current_lang_base, current_lang_name);
8025 if (name == lang_name_cplusplus)
8026 current_lang_name = name;
8027 else if (name == lang_name_c)
8028 current_lang_name = name;
8029 else
8030 error ("language string %<\"%E\"%> not recognized", name);
8033 /* Get out of the current language scope. */
8035 void
8036 pop_lang_context (void)
8038 current_lang_name = current_lang_base->pop ();
8041 /* Type instantiation routines. */
8043 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
8044 matches the TARGET_TYPE. If there is no satisfactory match, return
8045 error_mark_node, and issue an error & warning messages under
8046 control of FLAGS. Permit pointers to member function if FLAGS
8047 permits. If TEMPLATE_ONLY, the name of the overloaded function was
8048 a template-id, and EXPLICIT_TARGS are the explicitly provided
8049 template arguments.
8051 If OVERLOAD is for one or more member functions, then ACCESS_PATH
8052 is the base path used to reference those member functions. If
8053 the address is resolved to a member function, access checks will be
8054 performed and errors issued if appropriate. */
8056 static tree
8057 resolve_address_of_overloaded_function (tree target_type,
8058 tree overload,
8059 tsubst_flags_t complain,
8060 bool template_only,
8061 tree explicit_targs,
8062 tree access_path)
8064 /* Here's what the standard says:
8066 [over.over]
8068 If the name is a function template, template argument deduction
8069 is done, and if the argument deduction succeeds, the deduced
8070 arguments are used to generate a single template function, which
8071 is added to the set of overloaded functions considered.
8073 Non-member functions and static member functions match targets of
8074 type "pointer-to-function" or "reference-to-function." Nonstatic
8075 member functions match targets of type "pointer-to-member
8076 function;" the function type of the pointer to member is used to
8077 select the member function from the set of overloaded member
8078 functions. If a nonstatic member function is selected, the
8079 reference to the overloaded function name is required to have the
8080 form of a pointer to member as described in 5.3.1.
8082 If more than one function is selected, any template functions in
8083 the set are eliminated if the set also contains a non-template
8084 function, and any given template function is eliminated if the
8085 set contains a second template function that is more specialized
8086 than the first according to the partial ordering rules 14.5.5.2.
8087 After such eliminations, if any, there shall remain exactly one
8088 selected function. */
8090 int is_ptrmem = 0;
8091 /* We store the matches in a TREE_LIST rooted here. The functions
8092 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
8093 interoperability with most_specialized_instantiation. */
8094 tree matches = NULL_TREE;
8095 tree fn;
8096 tree target_fn_type;
8098 /* By the time we get here, we should be seeing only real
8099 pointer-to-member types, not the internal POINTER_TYPE to
8100 METHOD_TYPE representation. */
8101 gcc_assert (!TYPE_PTR_P (target_type)
8102 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
8104 gcc_assert (is_overloaded_fn (overload));
8106 /* Check that the TARGET_TYPE is reasonable. */
8107 if (TYPE_PTRFN_P (target_type)
8108 || TYPE_REFFN_P (target_type))
8109 /* This is OK. */;
8110 else if (TYPE_PTRMEMFUNC_P (target_type))
8111 /* This is OK, too. */
8112 is_ptrmem = 1;
8113 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
8114 /* This is OK, too. This comes from a conversion to reference
8115 type. */
8116 target_type = build_reference_type (target_type);
8117 else
8119 if (complain & tf_error)
8120 error ("cannot resolve overloaded function %qD based on"
8121 " conversion to type %qT",
8122 OVL_NAME (overload), target_type);
8123 return error_mark_node;
8126 /* Non-member functions and static member functions match targets of type
8127 "pointer-to-function" or "reference-to-function." Nonstatic member
8128 functions match targets of type "pointer-to-member-function;" the
8129 function type of the pointer to member is used to select the member
8130 function from the set of overloaded member functions.
8132 So figure out the FUNCTION_TYPE that we want to match against. */
8133 target_fn_type = static_fn_type (target_type);
8135 /* If we can find a non-template function that matches, we can just
8136 use it. There's no point in generating template instantiations
8137 if we're just going to throw them out anyhow. But, of course, we
8138 can only do this when we don't *need* a template function. */
8139 if (!template_only)
8140 for (lkp_iterator iter (overload); iter; ++iter)
8142 tree fn = *iter;
8144 if (TREE_CODE (fn) == TEMPLATE_DECL)
8145 /* We're not looking for templates just yet. */
8146 continue;
8148 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
8149 /* We're looking for a non-static member, and this isn't
8150 one, or vice versa. */
8151 continue;
8153 /* In C++17 we need the noexcept-qualifier to compare types. */
8154 if (flag_noexcept_type)
8155 maybe_instantiate_noexcept (fn);
8157 /* See if there's a match. */
8158 tree fntype = static_fn_type (fn);
8159 if (same_type_p (target_fn_type, fntype)
8160 || fnptr_conv_p (target_fn_type, fntype))
8161 matches = tree_cons (fn, NULL_TREE, matches);
8164 /* Now, if we've already got a match (or matches), there's no need
8165 to proceed to the template functions. But, if we don't have a
8166 match we need to look at them, too. */
8167 if (!matches)
8169 tree target_arg_types;
8170 tree target_ret_type;
8171 tree *args;
8172 unsigned int nargs, ia;
8173 tree arg;
8175 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
8176 target_ret_type = TREE_TYPE (target_fn_type);
8178 nargs = list_length (target_arg_types);
8179 args = XALLOCAVEC (tree, nargs);
8180 for (arg = target_arg_types, ia = 0;
8181 arg != NULL_TREE && arg != void_list_node;
8182 arg = TREE_CHAIN (arg), ++ia)
8183 args[ia] = TREE_VALUE (arg);
8184 nargs = ia;
8186 for (lkp_iterator iter (overload); iter; ++iter)
8188 tree fn = *iter;
8189 tree instantiation;
8190 tree targs;
8192 if (TREE_CODE (fn) != TEMPLATE_DECL)
8193 /* We're only looking for templates. */
8194 continue;
8196 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
8197 != is_ptrmem)
8198 /* We're not looking for a non-static member, and this is
8199 one, or vice versa. */
8200 continue;
8202 tree ret = target_ret_type;
8204 /* If the template has a deduced return type, don't expose it to
8205 template argument deduction. */
8206 if (undeduced_auto_decl (fn))
8207 ret = NULL_TREE;
8209 /* Try to do argument deduction. */
8210 targs = make_tree_vec (DECL_NTPARMS (fn));
8211 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
8212 nargs, ret,
8213 DEDUCE_EXACT, LOOKUP_NORMAL,
8214 false, false);
8215 if (instantiation == error_mark_node)
8216 /* Instantiation failed. */
8217 continue;
8219 /* Constraints must be satisfied. This is done before
8220 return type deduction since that instantiates the
8221 function. */
8222 if (flag_concepts && !constraints_satisfied_p (instantiation))
8223 continue;
8225 /* And now force instantiation to do return type deduction. */
8226 if (undeduced_auto_decl (instantiation))
8228 ++function_depth;
8229 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
8230 --function_depth;
8232 require_deduced_type (instantiation);
8235 /* In C++17 we need the noexcept-qualifier to compare types. */
8236 if (flag_noexcept_type)
8237 maybe_instantiate_noexcept (instantiation);
8239 /* See if there's a match. */
8240 tree fntype = static_fn_type (instantiation);
8241 if (same_type_p (target_fn_type, fntype)
8242 || fnptr_conv_p (target_fn_type, fntype))
8243 matches = tree_cons (instantiation, fn, matches);
8246 /* Now, remove all but the most specialized of the matches. */
8247 if (matches)
8249 tree match = most_specialized_instantiation (matches);
8251 if (match != error_mark_node)
8252 matches = tree_cons (TREE_PURPOSE (match),
8253 NULL_TREE,
8254 NULL_TREE);
8258 /* Now we should have exactly one function in MATCHES. */
8259 if (matches == NULL_TREE)
8261 /* There were *no* matches. */
8262 if (complain & tf_error)
8264 error ("no matches converting function %qD to type %q#T",
8265 OVL_NAME (overload), target_type);
8267 print_candidates (overload);
8269 return error_mark_node;
8271 else if (TREE_CHAIN (matches))
8273 /* There were too many matches. First check if they're all
8274 the same function. */
8275 tree match = NULL_TREE;
8277 fn = TREE_PURPOSE (matches);
8279 /* For multi-versioned functions, more than one match is just fine and
8280 decls_match will return false as they are different. */
8281 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
8282 if (!decls_match (fn, TREE_PURPOSE (match))
8283 && !targetm.target_option.function_versions
8284 (fn, TREE_PURPOSE (match)))
8285 break;
8287 if (match)
8289 if (complain & tf_error)
8291 error ("converting overloaded function %qD to type %q#T is ambiguous",
8292 OVL_NAME (overload), target_type);
8294 /* Since print_candidates expects the functions in the
8295 TREE_VALUE slot, we flip them here. */
8296 for (match = matches; match; match = TREE_CHAIN (match))
8297 TREE_VALUE (match) = TREE_PURPOSE (match);
8299 print_candidates (matches);
8302 return error_mark_node;
8306 /* Good, exactly one match. Now, convert it to the correct type. */
8307 fn = TREE_PURPOSE (matches);
8309 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
8310 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
8312 static int explained;
8314 if (!(complain & tf_error))
8315 return error_mark_node;
8317 permerror (input_location, "assuming pointer to member %qD", fn);
8318 if (!explained)
8320 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
8321 explained = 1;
8325 /* If a pointer to a function that is multi-versioned is requested, the
8326 pointer to the dispatcher function is returned instead. This works
8327 well because indirectly calling the function will dispatch the right
8328 function version at run-time. */
8329 if (DECL_FUNCTION_VERSIONED (fn))
8331 fn = get_function_version_dispatcher (fn);
8332 if (fn == NULL)
8333 return error_mark_node;
8334 /* Mark all the versions corresponding to the dispatcher as used. */
8335 if (!(complain & tf_conv))
8336 mark_versions_used (fn);
8339 /* If we're doing overload resolution purely for the purpose of
8340 determining conversion sequences, we should not consider the
8341 function used. If this conversion sequence is selected, the
8342 function will be marked as used at this point. */
8343 if (!(complain & tf_conv))
8345 /* Make =delete work with SFINAE. */
8346 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
8347 return error_mark_node;
8348 if (!mark_used (fn, complain) && !(complain & tf_error))
8349 return error_mark_node;
8352 /* We could not check access to member functions when this
8353 expression was originally created since we did not know at that
8354 time to which function the expression referred. */
8355 if (DECL_FUNCTION_MEMBER_P (fn))
8357 gcc_assert (access_path);
8358 perform_or_defer_access_check (access_path, fn, fn, complain);
8361 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
8362 return cp_build_addr_expr (fn, complain);
8363 else
8365 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
8366 will mark the function as addressed, but here we must do it
8367 explicitly. */
8368 cxx_mark_addressable (fn);
8370 return fn;
8374 /* This function will instantiate the type of the expression given in
8375 RHS to match the type of LHSTYPE. If errors exist, then return
8376 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
8377 we complain on errors. If we are not complaining, never modify rhs,
8378 as overload resolution wants to try many possible instantiations, in
8379 the hope that at least one will work.
8381 For non-recursive calls, LHSTYPE should be a function, pointer to
8382 function, or a pointer to member function. */
8384 tree
8385 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
8387 tsubst_flags_t complain_in = complain;
8388 tree access_path = NULL_TREE;
8390 complain &= ~tf_ptrmem_ok;
8392 if (lhstype == unknown_type_node)
8394 if (complain & tf_error)
8395 error ("not enough type information");
8396 return error_mark_node;
8399 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8401 tree fntype = non_reference (lhstype);
8402 if (same_type_p (fntype, TREE_TYPE (rhs)))
8403 return rhs;
8404 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8405 return rhs;
8406 if (flag_ms_extensions
8407 && TYPE_PTRMEMFUNC_P (fntype)
8408 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8409 /* Microsoft allows `A::f' to be resolved to a
8410 pointer-to-member. */
8412 else
8414 if (complain & tf_error)
8415 error ("cannot convert %qE from type %qT to type %qT",
8416 rhs, TREE_TYPE (rhs), fntype);
8417 return error_mark_node;
8421 if (BASELINK_P (rhs))
8423 access_path = BASELINK_ACCESS_BINFO (rhs);
8424 rhs = BASELINK_FUNCTIONS (rhs);
8427 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8428 deduce any type information. */
8429 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8431 if (complain & tf_error)
8432 error ("not enough type information");
8433 return error_mark_node;
8436 /* If we instantiate a template, and it is a A ?: C expression
8437 with omitted B, look through the SAVE_EXPR. */
8438 if (TREE_CODE (rhs) == SAVE_EXPR)
8439 rhs = TREE_OPERAND (rhs, 0);
8441 /* There are only a few kinds of expressions that may have a type
8442 dependent on overload resolution. */
8443 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8444 || TREE_CODE (rhs) == COMPONENT_REF
8445 || is_overloaded_fn (rhs)
8446 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8448 /* This should really only be used when attempting to distinguish
8449 what sort of a pointer to function we have. For now, any
8450 arithmetic operation which is not supported on pointers
8451 is rejected as an error. */
8453 switch (TREE_CODE (rhs))
8455 case COMPONENT_REF:
8457 tree member = TREE_OPERAND (rhs, 1);
8459 member = instantiate_type (lhstype, member, complain);
8460 if (member != error_mark_node
8461 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8462 /* Do not lose object's side effects. */
8463 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8464 TREE_OPERAND (rhs, 0), member);
8465 return member;
8468 case OFFSET_REF:
8469 rhs = TREE_OPERAND (rhs, 1);
8470 if (BASELINK_P (rhs))
8471 return instantiate_type (lhstype, rhs, complain_in);
8473 /* This can happen if we are forming a pointer-to-member for a
8474 member template. */
8475 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8477 /* Fall through. */
8479 case TEMPLATE_ID_EXPR:
8481 tree fns = TREE_OPERAND (rhs, 0);
8482 tree args = TREE_OPERAND (rhs, 1);
8484 return
8485 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8486 /*template_only=*/true,
8487 args, access_path);
8490 case OVERLOAD:
8491 case FUNCTION_DECL:
8492 return
8493 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8494 /*template_only=*/false,
8495 /*explicit_targs=*/NULL_TREE,
8496 access_path);
8498 case ADDR_EXPR:
8500 if (PTRMEM_OK_P (rhs))
8501 complain |= tf_ptrmem_ok;
8503 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8506 case ERROR_MARK:
8507 return error_mark_node;
8509 default:
8510 gcc_unreachable ();
8512 return error_mark_node;
8515 /* Return the name of the virtual function pointer field
8516 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8517 this may have to look back through base types to find the
8518 ultimate field name. (For single inheritance, these could
8519 all be the same name. Who knows for multiple inheritance). */
8521 static tree
8522 get_vfield_name (tree type)
8524 tree binfo, base_binfo;
8525 char *buf;
8527 for (binfo = TYPE_BINFO (type);
8528 BINFO_N_BASE_BINFOS (binfo);
8529 binfo = base_binfo)
8531 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8533 if (BINFO_VIRTUAL_P (base_binfo)
8534 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8535 break;
8538 type = BINFO_TYPE (binfo);
8539 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8540 + TYPE_NAME_LENGTH (type) + 2);
8541 sprintf (buf, VFIELD_NAME_FORMAT,
8542 IDENTIFIER_POINTER (constructor_name (type)));
8543 return get_identifier (buf);
8546 void
8547 print_class_statistics (void)
8549 if (! GATHER_STATISTICS)
8550 return;
8552 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8553 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8554 if (n_vtables)
8556 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8557 n_vtables, n_vtable_searches);
8558 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8559 n_vtable_entries, n_vtable_elems);
8563 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8564 according to [class]:
8565 The class-name is also inserted
8566 into the scope of the class itself. For purposes of access checking,
8567 the inserted class name is treated as if it were a public member name. */
8569 void
8570 build_self_reference (void)
8572 tree name = constructor_name (current_class_type);
8573 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8574 tree saved_cas;
8576 DECL_NONLOCAL (value) = 1;
8577 DECL_CONTEXT (value) = current_class_type;
8578 DECL_ARTIFICIAL (value) = 1;
8579 SET_DECL_SELF_REFERENCE_P (value);
8580 set_underlying_type (value);
8582 if (processing_template_decl)
8583 value = push_template_decl (value);
8585 saved_cas = current_access_specifier;
8586 current_access_specifier = access_public_node;
8587 finish_member_declaration (value);
8588 current_access_specifier = saved_cas;
8591 /* Returns 1 if TYPE contains only padding bytes. */
8594 is_empty_class (tree type)
8596 if (type == error_mark_node)
8597 return 0;
8599 if (! CLASS_TYPE_P (type))
8600 return 0;
8602 return CLASSTYPE_EMPTY_P (type);
8605 /* Returns true if TYPE contains no actual data, just various
8606 possible combinations of empty classes and possibly a vptr. */
8608 bool
8609 is_really_empty_class (tree type)
8611 if (CLASS_TYPE_P (type))
8613 tree field;
8614 tree binfo;
8615 tree base_binfo;
8616 int i;
8618 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8619 out, but we'd like to be able to check this before then. */
8620 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8621 return true;
8623 for (binfo = TYPE_BINFO (type), i = 0;
8624 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8625 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8626 return false;
8627 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8628 if (TREE_CODE (field) == FIELD_DECL
8629 && !DECL_ARTIFICIAL (field)
8630 /* An unnamed bit-field is not a data member. */
8631 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
8632 && !is_really_empty_class (TREE_TYPE (field)))
8633 return false;
8634 return true;
8636 else if (TREE_CODE (type) == ARRAY_TYPE)
8637 return (integer_zerop (array_type_nelts_top (type))
8638 || is_really_empty_class (TREE_TYPE (type)));
8639 return false;
8642 /* Note that NAME was looked up while the current class was being
8643 defined and that the result of that lookup was DECL. */
8645 void
8646 maybe_note_name_used_in_class (tree name, tree decl)
8648 splay_tree names_used;
8650 /* If we're not defining a class, there's nothing to do. */
8651 if (!(innermost_scope_kind() == sk_class
8652 && TYPE_BEING_DEFINED (current_class_type)
8653 && !LAMBDA_TYPE_P (current_class_type)))
8654 return;
8656 /* If there's already a binding for this NAME, then we don't have
8657 anything to worry about. */
8658 if (lookup_member (current_class_type, name,
8659 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8660 return;
8662 if (!current_class_stack[current_class_depth - 1].names_used)
8663 current_class_stack[current_class_depth - 1].names_used
8664 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8665 names_used = current_class_stack[current_class_depth - 1].names_used;
8667 splay_tree_insert (names_used,
8668 (splay_tree_key) name,
8669 (splay_tree_value) decl);
8672 /* Note that NAME was declared (as DECL) in the current class. Check
8673 to see that the declaration is valid. */
8675 void
8676 note_name_declared_in_class (tree name, tree decl)
8678 splay_tree names_used;
8679 splay_tree_node n;
8681 /* Look to see if we ever used this name. */
8682 names_used
8683 = current_class_stack[current_class_depth - 1].names_used;
8684 if (!names_used)
8685 return;
8686 /* The C language allows members to be declared with a type of the same
8687 name, and the C++ standard says this diagnostic is not required. So
8688 allow it in extern "C" blocks unless predantic is specified.
8689 Allow it in all cases if -ms-extensions is specified. */
8690 if ((!pedantic && current_lang_name == lang_name_c)
8691 || flag_ms_extensions)
8692 return;
8693 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8694 if (n)
8696 /* [basic.scope.class]
8698 A name N used in a class S shall refer to the same declaration
8699 in its context and when re-evaluated in the completed scope of
8700 S. */
8701 permerror (input_location, "declaration of %q#D", decl);
8702 permerror (location_of ((tree) n->value),
8703 "changes meaning of %qD from %q#D",
8704 OVL_NAME (decl), (tree) n->value);
8708 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8709 Secondary vtables are merged with primary vtables; this function
8710 will return the VAR_DECL for the primary vtable. */
8712 tree
8713 get_vtbl_decl_for_binfo (tree binfo)
8715 tree decl;
8717 decl = BINFO_VTABLE (binfo);
8718 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8720 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8721 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8723 if (decl)
8724 gcc_assert (VAR_P (decl));
8725 return decl;
8729 /* Returns the binfo for the primary base of BINFO. If the resulting
8730 BINFO is a virtual base, and it is inherited elsewhere in the
8731 hierarchy, then the returned binfo might not be the primary base of
8732 BINFO in the complete object. Check BINFO_PRIMARY_P or
8733 BINFO_LOST_PRIMARY_P to be sure. */
8735 static tree
8736 get_primary_binfo (tree binfo)
8738 tree primary_base;
8740 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8741 if (!primary_base)
8742 return NULL_TREE;
8744 return copied_binfo (primary_base, binfo);
8747 /* As above, but iterate until we reach the binfo that actually provides the
8748 vptr for BINFO. */
8750 static tree
8751 most_primary_binfo (tree binfo)
8753 tree b = binfo;
8754 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8755 && !BINFO_LOST_PRIMARY_P (b))
8757 tree primary_base = get_primary_binfo (b);
8758 gcc_assert (BINFO_PRIMARY_P (primary_base)
8759 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8760 b = primary_base;
8762 return b;
8765 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8766 type. Note that the virtual inheritance might be above or below BINFO in
8767 the hierarchy. */
8769 bool
8770 vptr_via_virtual_p (tree binfo)
8772 if (TYPE_P (binfo))
8773 binfo = TYPE_BINFO (binfo);
8774 tree primary = most_primary_binfo (binfo);
8775 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8776 a morally virtual base. */
8777 tree virt = binfo_via_virtual (primary, NULL_TREE);
8778 return virt != NULL_TREE;
8781 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8783 static int
8784 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8786 if (!indented_p)
8787 fprintf (stream, "%*s", indent, "");
8788 return 1;
8791 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8792 INDENT should be zero when called from the top level; it is
8793 incremented recursively. IGO indicates the next expected BINFO in
8794 inheritance graph ordering. */
8796 static tree
8797 dump_class_hierarchy_r (FILE *stream,
8798 dump_flags_t flags,
8799 tree binfo,
8800 tree igo,
8801 int indent)
8803 int indented = 0;
8804 tree base_binfo;
8805 int i;
8807 indented = maybe_indent_hierarchy (stream, indent, 0);
8808 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8809 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8810 (HOST_WIDE_INT) (uintptr_t) binfo);
8811 if (binfo != igo)
8813 fprintf (stream, "alternative-path\n");
8814 return igo;
8816 igo = TREE_CHAIN (binfo);
8818 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8819 tree_to_shwi (BINFO_OFFSET (binfo)));
8820 if (is_empty_class (BINFO_TYPE (binfo)))
8821 fprintf (stream, " empty");
8822 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8823 fprintf (stream, " nearly-empty");
8824 if (BINFO_VIRTUAL_P (binfo))
8825 fprintf (stream, " virtual");
8826 fprintf (stream, "\n");
8828 indented = 0;
8829 if (BINFO_PRIMARY_P (binfo))
8831 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8832 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8833 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8834 TFF_PLAIN_IDENTIFIER),
8835 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8837 if (BINFO_LOST_PRIMARY_P (binfo))
8839 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8840 fprintf (stream, " lost-primary");
8842 if (indented)
8843 fprintf (stream, "\n");
8845 if (!(flags & TDF_SLIM))
8847 int indented = 0;
8849 if (BINFO_SUBVTT_INDEX (binfo))
8851 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8852 fprintf (stream, " subvttidx=%s",
8853 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8854 TFF_PLAIN_IDENTIFIER));
8856 if (BINFO_VPTR_INDEX (binfo))
8858 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8859 fprintf (stream, " vptridx=%s",
8860 expr_as_string (BINFO_VPTR_INDEX (binfo),
8861 TFF_PLAIN_IDENTIFIER));
8863 if (BINFO_VPTR_FIELD (binfo))
8865 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8866 fprintf (stream, " vbaseoffset=%s",
8867 expr_as_string (BINFO_VPTR_FIELD (binfo),
8868 TFF_PLAIN_IDENTIFIER));
8870 if (BINFO_VTABLE (binfo))
8872 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8873 fprintf (stream, " vptr=%s",
8874 expr_as_string (BINFO_VTABLE (binfo),
8875 TFF_PLAIN_IDENTIFIER));
8878 if (indented)
8879 fprintf (stream, "\n");
8882 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8883 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8885 return igo;
8888 /* Dump the BINFO hierarchy for T. */
8890 static void
8891 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8893 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8894 fprintf (stream, " size=%lu align=%lu\n",
8895 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8896 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8897 fprintf (stream, " base size=%lu base align=%lu\n",
8898 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8899 / BITS_PER_UNIT),
8900 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8901 / BITS_PER_UNIT));
8902 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8903 fprintf (stream, "\n");
8906 /* Debug interface to hierarchy dumping. */
8908 void
8909 debug_class (tree t)
8911 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8914 static void
8915 dump_class_hierarchy (tree t)
8917 dump_flags_t flags;
8918 if (FILE *stream = dump_begin (class_dump_id, &flags))
8920 dump_class_hierarchy_1 (stream, flags, t);
8921 dump_end (class_dump_id, stream);
8925 static void
8926 dump_array (FILE * stream, tree decl)
8928 tree value;
8929 unsigned HOST_WIDE_INT ix;
8930 HOST_WIDE_INT elt;
8931 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8933 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8934 / BITS_PER_UNIT);
8935 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8936 fprintf (stream, " %s entries",
8937 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8938 TFF_PLAIN_IDENTIFIER));
8939 fprintf (stream, "\n");
8941 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8942 ix, value)
8943 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8944 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8947 static void
8948 dump_vtable (tree t, tree binfo, tree vtable)
8950 dump_flags_t flags;
8951 FILE *stream = dump_begin (class_dump_id, &flags);
8953 if (!stream)
8954 return;
8956 if (!(flags & TDF_SLIM))
8958 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8960 fprintf (stream, "%s for %s",
8961 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8962 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8963 if (ctor_vtbl_p)
8965 if (!BINFO_VIRTUAL_P (binfo))
8966 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8967 (HOST_WIDE_INT) (uintptr_t) binfo);
8968 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8970 fprintf (stream, "\n");
8971 dump_array (stream, vtable);
8972 fprintf (stream, "\n");
8975 dump_end (class_dump_id, stream);
8978 static void
8979 dump_vtt (tree t, tree vtt)
8981 dump_flags_t flags;
8982 FILE *stream = dump_begin (class_dump_id, &flags);
8984 if (!stream)
8985 return;
8987 if (!(flags & TDF_SLIM))
8989 fprintf (stream, "VTT for %s\n",
8990 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8991 dump_array (stream, vtt);
8992 fprintf (stream, "\n");
8995 dump_end (class_dump_id, stream);
8998 /* Dump a function or thunk and its thunkees. */
9000 static void
9001 dump_thunk (FILE *stream, int indent, tree thunk)
9003 static const char spaces[] = " ";
9004 tree name = DECL_NAME (thunk);
9005 tree thunks;
9007 fprintf (stream, "%.*s%p %s %s", indent, spaces,
9008 (void *)thunk,
9009 !DECL_THUNK_P (thunk) ? "function"
9010 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
9011 name ? IDENTIFIER_POINTER (name) : "<unset>");
9012 if (DECL_THUNK_P (thunk))
9014 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
9015 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
9017 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
9018 if (!virtual_adjust)
9019 /*NOP*/;
9020 else if (DECL_THIS_THUNK_P (thunk))
9021 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
9022 tree_to_shwi (virtual_adjust));
9023 else
9024 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
9025 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
9026 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
9027 if (THUNK_ALIAS (thunk))
9028 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
9030 fprintf (stream, "\n");
9031 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
9032 dump_thunk (stream, indent + 2, thunks);
9035 /* Dump the thunks for FN. */
9037 void
9038 debug_thunks (tree fn)
9040 dump_thunk (stderr, 0, fn);
9043 /* Virtual function table initialization. */
9045 /* Create all the necessary vtables for T and its base classes. */
9047 static void
9048 finish_vtbls (tree t)
9050 tree vbase;
9051 vec<constructor_elt, va_gc> *v = NULL;
9052 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
9054 /* We lay out the primary and secondary vtables in one contiguous
9055 vtable. The primary vtable is first, followed by the non-virtual
9056 secondary vtables in inheritance graph order. */
9057 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
9058 vtable, t, &v);
9060 /* Then come the virtual bases, also in inheritance graph order. */
9061 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
9063 if (!BINFO_VIRTUAL_P (vbase))
9064 continue;
9065 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
9068 if (BINFO_VTABLE (TYPE_BINFO (t)))
9069 initialize_vtable (TYPE_BINFO (t), v);
9072 /* Initialize the vtable for BINFO with the INITS. */
9074 static void
9075 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
9077 tree decl;
9079 layout_vtable_decl (binfo, vec_safe_length (inits));
9080 decl = get_vtbl_decl_for_binfo (binfo);
9081 initialize_artificial_var (decl, inits);
9082 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
9085 /* Build the VTT (virtual table table) for T.
9086 A class requires a VTT if it has virtual bases.
9088 This holds
9089 1 - primary virtual pointer for complete object T
9090 2 - secondary VTTs for each direct non-virtual base of T which requires a
9092 3 - secondary virtual pointers for each direct or indirect base of T which
9093 has virtual bases or is reachable via a virtual path from T.
9094 4 - secondary VTTs for each direct or indirect virtual base of T.
9096 Secondary VTTs look like complete object VTTs without part 4. */
9098 static void
9099 build_vtt (tree t)
9101 tree type;
9102 tree vtt;
9103 tree index;
9104 vec<constructor_elt, va_gc> *inits;
9106 /* Build up the initializers for the VTT. */
9107 inits = NULL;
9108 index = size_zero_node;
9109 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
9111 /* If we didn't need a VTT, we're done. */
9112 if (!inits)
9113 return;
9115 /* Figure out the type of the VTT. */
9116 type = build_array_of_n_type (const_ptr_type_node,
9117 inits->length ());
9119 /* Now, build the VTT object itself. */
9120 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
9121 initialize_artificial_var (vtt, inits);
9122 /* Add the VTT to the vtables list. */
9123 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
9124 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
9126 dump_vtt (t, vtt);
9129 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
9130 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
9131 and CHAIN the vtable pointer for this binfo after construction is
9132 complete. VALUE can also be another BINFO, in which case we recurse. */
9134 static tree
9135 binfo_ctor_vtable (tree binfo)
9137 tree vt;
9139 while (1)
9141 vt = BINFO_VTABLE (binfo);
9142 if (TREE_CODE (vt) == TREE_LIST)
9143 vt = TREE_VALUE (vt);
9144 if (TREE_CODE (vt) == TREE_BINFO)
9145 binfo = vt;
9146 else
9147 break;
9150 return vt;
9153 /* Data for secondary VTT initialization. */
9154 struct secondary_vptr_vtt_init_data
9156 /* Is this the primary VTT? */
9157 bool top_level_p;
9159 /* Current index into the VTT. */
9160 tree index;
9162 /* Vector of initializers built up. */
9163 vec<constructor_elt, va_gc> *inits;
9165 /* The type being constructed by this secondary VTT. */
9166 tree type_being_constructed;
9169 /* Recursively build the VTT-initializer for BINFO (which is in the
9170 hierarchy dominated by T). INITS points to the end of the initializer
9171 list to date. INDEX is the VTT index where the next element will be
9172 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
9173 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
9174 for virtual bases of T. When it is not so, we build the constructor
9175 vtables for the BINFO-in-T variant. */
9177 static void
9178 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
9179 tree *index)
9181 int i;
9182 tree b;
9183 tree init;
9184 secondary_vptr_vtt_init_data data;
9185 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9187 /* We only need VTTs for subobjects with virtual bases. */
9188 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9189 return;
9191 /* We need to use a construction vtable if this is not the primary
9192 VTT. */
9193 if (!top_level_p)
9195 build_ctor_vtbl_group (binfo, t);
9197 /* Record the offset in the VTT where this sub-VTT can be found. */
9198 BINFO_SUBVTT_INDEX (binfo) = *index;
9201 /* Add the address of the primary vtable for the complete object. */
9202 init = binfo_ctor_vtable (binfo);
9203 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9204 if (top_level_p)
9206 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9207 BINFO_VPTR_INDEX (binfo) = *index;
9209 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
9211 /* Recursively add the secondary VTTs for non-virtual bases. */
9212 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
9213 if (!BINFO_VIRTUAL_P (b))
9214 build_vtt_inits (b, t, inits, index);
9216 /* Add secondary virtual pointers for all subobjects of BINFO with
9217 either virtual bases or reachable along a virtual path, except
9218 subobjects that are non-virtual primary bases. */
9219 data.top_level_p = top_level_p;
9220 data.index = *index;
9221 data.inits = *inits;
9222 data.type_being_constructed = BINFO_TYPE (binfo);
9224 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
9226 *index = data.index;
9228 /* data.inits might have grown as we added secondary virtual pointers.
9229 Make sure our caller knows about the new vector. */
9230 *inits = data.inits;
9232 if (top_level_p)
9233 /* Add the secondary VTTs for virtual bases in inheritance graph
9234 order. */
9235 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
9237 if (!BINFO_VIRTUAL_P (b))
9238 continue;
9240 build_vtt_inits (b, t, inits, index);
9242 else
9243 /* Remove the ctor vtables we created. */
9244 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
9247 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
9248 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
9250 static tree
9251 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
9253 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
9255 /* We don't care about bases that don't have vtables. */
9256 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
9257 return dfs_skip_bases;
9259 /* We're only interested in proper subobjects of the type being
9260 constructed. */
9261 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
9262 return NULL_TREE;
9264 /* We're only interested in bases with virtual bases or reachable
9265 via a virtual path from the type being constructed. */
9266 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9267 || binfo_via_virtual (binfo, data->type_being_constructed)))
9268 return dfs_skip_bases;
9270 /* We're not interested in non-virtual primary bases. */
9271 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
9272 return NULL_TREE;
9274 /* Record the index where this secondary vptr can be found. */
9275 if (data->top_level_p)
9277 gcc_assert (!BINFO_VPTR_INDEX (binfo));
9278 BINFO_VPTR_INDEX (binfo) = data->index;
9280 if (BINFO_VIRTUAL_P (binfo))
9282 /* It's a primary virtual base, and this is not a
9283 construction vtable. Find the base this is primary of in
9284 the inheritance graph, and use that base's vtable
9285 now. */
9286 while (BINFO_PRIMARY_P (binfo))
9287 binfo = BINFO_INHERITANCE_CHAIN (binfo);
9291 /* Add the initializer for the secondary vptr itself. */
9292 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
9294 /* Advance the vtt index. */
9295 data->index = size_binop (PLUS_EXPR, data->index,
9296 TYPE_SIZE_UNIT (ptr_type_node));
9298 return NULL_TREE;
9301 /* Called from build_vtt_inits via dfs_walk. After building
9302 constructor vtables and generating the sub-vtt from them, we need
9303 to restore the BINFO_VTABLES that were scribbled on. DATA is the
9304 binfo of the base whose sub vtt was generated. */
9306 static tree
9307 dfs_fixup_binfo_vtbls (tree binfo, void* data)
9309 tree vtable = BINFO_VTABLE (binfo);
9311 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9312 /* If this class has no vtable, none of its bases do. */
9313 return dfs_skip_bases;
9315 if (!vtable)
9316 /* This might be a primary base, so have no vtable in this
9317 hierarchy. */
9318 return NULL_TREE;
9320 /* If we scribbled the construction vtable vptr into BINFO, clear it
9321 out now. */
9322 if (TREE_CODE (vtable) == TREE_LIST
9323 && (TREE_PURPOSE (vtable) == (tree) data))
9324 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
9326 return NULL_TREE;
9329 /* Build the construction vtable group for BINFO which is in the
9330 hierarchy dominated by T. */
9332 static void
9333 build_ctor_vtbl_group (tree binfo, tree t)
9335 tree type;
9336 tree vtbl;
9337 tree id;
9338 tree vbase;
9339 vec<constructor_elt, va_gc> *v;
9341 /* See if we've already created this construction vtable group. */
9342 id = mangle_ctor_vtbl_for_type (t, binfo);
9343 if (IDENTIFIER_GLOBAL_VALUE (id))
9344 return;
9346 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
9347 /* Build a version of VTBL (with the wrong type) for use in
9348 constructing the addresses of secondary vtables in the
9349 construction vtable group. */
9350 vtbl = build_vtable (t, id, ptr_type_node);
9351 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
9352 /* Don't export construction vtables from shared libraries. Even on
9353 targets that don't support hidden visibility, this tells
9354 can_refer_decl_in_current_unit_p not to assume that it's safe to
9355 access from a different compilation unit (bz 54314). */
9356 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
9357 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
9359 v = NULL;
9360 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
9361 binfo, vtbl, t, &v);
9363 /* Add the vtables for each of our virtual bases using the vbase in T
9364 binfo. */
9365 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9366 vbase;
9367 vbase = TREE_CHAIN (vbase))
9369 tree b;
9371 if (!BINFO_VIRTUAL_P (vbase))
9372 continue;
9373 b = copied_binfo (vbase, binfo);
9375 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
9378 /* Figure out the type of the construction vtable. */
9379 type = build_array_of_n_type (vtable_entry_type, v->length ());
9380 layout_type (type);
9381 TREE_TYPE (vtbl) = type;
9382 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
9383 layout_decl (vtbl, 0);
9385 /* Initialize the construction vtable. */
9386 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
9387 initialize_artificial_var (vtbl, v);
9388 dump_vtable (t, binfo, vtbl);
9391 /* Add the vtbl initializers for BINFO (and its bases other than
9392 non-virtual primaries) to the list of INITS. BINFO is in the
9393 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
9394 the constructor the vtbl inits should be accumulated for. (If this
9395 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
9396 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
9397 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
9398 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
9399 but are not necessarily the same in terms of layout. */
9401 static void
9402 accumulate_vtbl_inits (tree binfo,
9403 tree orig_binfo,
9404 tree rtti_binfo,
9405 tree vtbl,
9406 tree t,
9407 vec<constructor_elt, va_gc> **inits)
9409 int i;
9410 tree base_binfo;
9411 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9413 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9415 /* If it doesn't have a vptr, we don't do anything. */
9416 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9417 return;
9419 /* If we're building a construction vtable, we're not interested in
9420 subobjects that don't require construction vtables. */
9421 if (ctor_vtbl_p
9422 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9423 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9424 return;
9426 /* Build the initializers for the BINFO-in-T vtable. */
9427 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9429 /* Walk the BINFO and its bases. We walk in preorder so that as we
9430 initialize each vtable we can figure out at what offset the
9431 secondary vtable lies from the primary vtable. We can't use
9432 dfs_walk here because we need to iterate through bases of BINFO
9433 and RTTI_BINFO simultaneously. */
9434 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9436 /* Skip virtual bases. */
9437 if (BINFO_VIRTUAL_P (base_binfo))
9438 continue;
9439 accumulate_vtbl_inits (base_binfo,
9440 BINFO_BASE_BINFO (orig_binfo, i),
9441 rtti_binfo, vtbl, t,
9442 inits);
9446 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9447 BINFO vtable to L. */
9449 static void
9450 dfs_accumulate_vtbl_inits (tree binfo,
9451 tree orig_binfo,
9452 tree rtti_binfo,
9453 tree orig_vtbl,
9454 tree t,
9455 vec<constructor_elt, va_gc> **l)
9457 tree vtbl = NULL_TREE;
9458 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9459 int n_inits;
9461 if (ctor_vtbl_p
9462 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9464 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9465 primary virtual base. If it is not the same primary in
9466 the hierarchy of T, we'll need to generate a ctor vtable
9467 for it, to place at its location in T. If it is the same
9468 primary, we still need a VTT entry for the vtable, but it
9469 should point to the ctor vtable for the base it is a
9470 primary for within the sub-hierarchy of RTTI_BINFO.
9472 There are three possible cases:
9474 1) We are in the same place.
9475 2) We are a primary base within a lost primary virtual base of
9476 RTTI_BINFO.
9477 3) We are primary to something not a base of RTTI_BINFO. */
9479 tree b;
9480 tree last = NULL_TREE;
9482 /* First, look through the bases we are primary to for RTTI_BINFO
9483 or a virtual base. */
9484 b = binfo;
9485 while (BINFO_PRIMARY_P (b))
9487 b = BINFO_INHERITANCE_CHAIN (b);
9488 last = b;
9489 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9490 goto found;
9492 /* If we run out of primary links, keep looking down our
9493 inheritance chain; we might be an indirect primary. */
9494 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9495 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9496 break;
9497 found:
9499 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9500 base B and it is a base of RTTI_BINFO, this is case 2. In
9501 either case, we share our vtable with LAST, i.e. the
9502 derived-most base within B of which we are a primary. */
9503 if (b == rtti_binfo
9504 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9505 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9506 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9507 binfo_ctor_vtable after everything's been set up. */
9508 vtbl = last;
9510 /* Otherwise, this is case 3 and we get our own. */
9512 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9513 return;
9515 n_inits = vec_safe_length (*l);
9517 if (!vtbl)
9519 tree index;
9520 int non_fn_entries;
9522 /* Add the initializer for this vtable. */
9523 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9524 &non_fn_entries, l);
9526 /* Figure out the position to which the VPTR should point. */
9527 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9528 index = size_binop (MULT_EXPR,
9529 TYPE_SIZE_UNIT (vtable_entry_type),
9530 size_int (non_fn_entries + n_inits));
9531 vtbl = fold_build_pointer_plus (vtbl, index);
9534 if (ctor_vtbl_p)
9535 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9536 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9537 straighten this out. */
9538 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9539 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9540 /* Throw away any unneeded intializers. */
9541 (*l)->truncate (n_inits);
9542 else
9543 /* For an ordinary vtable, set BINFO_VTABLE. */
9544 BINFO_VTABLE (binfo) = vtbl;
9547 static GTY(()) tree abort_fndecl_addr;
9548 static GTY(()) tree dvirt_fn;
9550 /* Construct the initializer for BINFO's virtual function table. BINFO
9551 is part of the hierarchy dominated by T. If we're building a
9552 construction vtable, the ORIG_BINFO is the binfo we should use to
9553 find the actual function pointers to put in the vtable - but they
9554 can be overridden on the path to most-derived in the graph that
9555 ORIG_BINFO belongs. Otherwise,
9556 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9557 BINFO that should be indicated by the RTTI information in the
9558 vtable; it will be a base class of T, rather than T itself, if we
9559 are building a construction vtable.
9561 The value returned is a TREE_LIST suitable for wrapping in a
9562 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9563 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9564 number of non-function entries in the vtable.
9566 It might seem that this function should never be called with a
9567 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9568 base is always subsumed by a derived class vtable. However, when
9569 we are building construction vtables, we do build vtables for
9570 primary bases; we need these while the primary base is being
9571 constructed. */
9573 static void
9574 build_vtbl_initializer (tree binfo,
9575 tree orig_binfo,
9576 tree t,
9577 tree rtti_binfo,
9578 int* non_fn_entries_p,
9579 vec<constructor_elt, va_gc> **inits)
9581 tree v;
9582 vtbl_init_data vid;
9583 unsigned ix, jx;
9584 tree vbinfo;
9585 vec<tree, va_gc> *vbases;
9586 constructor_elt *e;
9588 /* Initialize VID. */
9589 memset (&vid, 0, sizeof (vid));
9590 vid.binfo = binfo;
9591 vid.derived = t;
9592 vid.rtti_binfo = rtti_binfo;
9593 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9594 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9595 vid.generate_vcall_entries = true;
9596 /* The first vbase or vcall offset is at index -3 in the vtable. */
9597 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9599 /* Add entries to the vtable for RTTI. */
9600 build_rtti_vtbl_entries (binfo, &vid);
9602 /* Create an array for keeping track of the functions we've
9603 processed. When we see multiple functions with the same
9604 signature, we share the vcall offsets. */
9605 vec_alloc (vid.fns, 32);
9606 /* Add the vcall and vbase offset entries. */
9607 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9609 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9610 build_vbase_offset_vtbl_entries. */
9611 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9612 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9613 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9615 /* If the target requires padding between data entries, add that now. */
9616 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9618 int n_entries = vec_safe_length (vid.inits);
9620 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9622 /* Move data entries into their new positions and add padding
9623 after the new positions. Iterate backwards so we don't
9624 overwrite entries that we would need to process later. */
9625 for (ix = n_entries - 1;
9626 vid.inits->iterate (ix, &e);
9627 ix--)
9629 int j;
9630 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9631 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9633 (*vid.inits)[new_position] = *e;
9635 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9637 constructor_elt *f = &(*vid.inits)[new_position - j];
9638 f->index = NULL_TREE;
9639 f->value = build1 (NOP_EXPR, vtable_entry_type,
9640 null_pointer_node);
9645 if (non_fn_entries_p)
9646 *non_fn_entries_p = vec_safe_length (vid.inits);
9648 /* The initializers for virtual functions were built up in reverse
9649 order. Straighten them out and add them to the running list in one
9650 step. */
9651 jx = vec_safe_length (*inits);
9652 vec_safe_grow (*inits, jx + vid.inits->length ());
9654 for (ix = vid.inits->length () - 1;
9655 vid.inits->iterate (ix, &e);
9656 ix--, jx++)
9657 (**inits)[jx] = *e;
9659 /* Go through all the ordinary virtual functions, building up
9660 initializers. */
9661 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9663 tree delta;
9664 tree vcall_index;
9665 tree fn, fn_original;
9666 tree init = NULL_TREE;
9668 fn = BV_FN (v);
9669 fn_original = fn;
9670 if (DECL_THUNK_P (fn))
9672 if (!DECL_NAME (fn))
9673 finish_thunk (fn);
9674 if (THUNK_ALIAS (fn))
9676 fn = THUNK_ALIAS (fn);
9677 BV_FN (v) = fn;
9679 fn_original = THUNK_TARGET (fn);
9682 /* If the only definition of this function signature along our
9683 primary base chain is from a lost primary, this vtable slot will
9684 never be used, so just zero it out. This is important to avoid
9685 requiring extra thunks which cannot be generated with the function.
9687 We first check this in update_vtable_entry_for_fn, so we handle
9688 restored primary bases properly; we also need to do it here so we
9689 zero out unused slots in ctor vtables, rather than filling them
9690 with erroneous values (though harmless, apart from relocation
9691 costs). */
9692 if (BV_LOST_PRIMARY (v))
9693 init = size_zero_node;
9695 if (! init)
9697 /* Pull the offset for `this', and the function to call, out of
9698 the list. */
9699 delta = BV_DELTA (v);
9700 vcall_index = BV_VCALL_INDEX (v);
9702 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9703 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9705 /* You can't call an abstract virtual function; it's abstract.
9706 So, we replace these functions with __pure_virtual. */
9707 if (DECL_PURE_VIRTUAL_P (fn_original))
9709 fn = abort_fndecl;
9710 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9712 if (abort_fndecl_addr == NULL)
9713 abort_fndecl_addr
9714 = fold_convert (vfunc_ptr_type_node,
9715 build_fold_addr_expr (fn));
9716 init = abort_fndecl_addr;
9719 /* Likewise for deleted virtuals. */
9720 else if (DECL_DELETED_FN (fn_original))
9722 if (!dvirt_fn)
9724 tree name = get_identifier ("__cxa_deleted_virtual");
9725 dvirt_fn = IDENTIFIER_GLOBAL_VALUE (name);
9726 if (!dvirt_fn)
9727 dvirt_fn = push_library_fn
9728 (name,
9729 build_function_type_list (void_type_node, NULL_TREE),
9730 NULL_TREE, ECF_NORETURN | ECF_COLD);
9732 fn = dvirt_fn;
9733 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9734 init = fold_convert (vfunc_ptr_type_node,
9735 build_fold_addr_expr (fn));
9737 else
9739 if (!integer_zerop (delta) || vcall_index)
9741 fn = make_thunk (fn, /*this_adjusting=*/1,
9742 delta, vcall_index);
9743 if (!DECL_NAME (fn))
9744 finish_thunk (fn);
9746 /* Take the address of the function, considering it to be of an
9747 appropriate generic type. */
9748 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9749 init = fold_convert (vfunc_ptr_type_node,
9750 build_fold_addr_expr (fn));
9751 /* Don't refer to a virtual destructor from a constructor
9752 vtable or a vtable for an abstract class, since destroying
9753 an object under construction is undefined behavior and we
9754 don't want it to be considered a candidate for speculative
9755 devirtualization. But do create the thunk for ABI
9756 compliance. */
9757 if (DECL_DESTRUCTOR_P (fn_original)
9758 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9759 || orig_binfo != binfo))
9760 init = size_zero_node;
9764 /* And add it to the chain of initializers. */
9765 if (TARGET_VTABLE_USES_DESCRIPTORS)
9767 int i;
9768 if (init == size_zero_node)
9769 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9770 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9771 else
9772 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9774 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9775 fn, build_int_cst (NULL_TREE, i));
9776 TREE_CONSTANT (fdesc) = 1;
9778 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9781 else
9782 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9786 /* Adds to vid->inits the initializers for the vbase and vcall
9787 offsets in BINFO, which is in the hierarchy dominated by T. */
9789 static void
9790 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9792 tree b;
9794 /* If this is a derived class, we must first create entries
9795 corresponding to the primary base class. */
9796 b = get_primary_binfo (binfo);
9797 if (b)
9798 build_vcall_and_vbase_vtbl_entries (b, vid);
9800 /* Add the vbase entries for this base. */
9801 build_vbase_offset_vtbl_entries (binfo, vid);
9802 /* Add the vcall entries for this base. */
9803 build_vcall_offset_vtbl_entries (binfo, vid);
9806 /* Returns the initializers for the vbase offset entries in the vtable
9807 for BINFO (which is part of the class hierarchy dominated by T), in
9808 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9809 where the next vbase offset will go. */
9811 static void
9812 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9814 tree vbase;
9815 tree t;
9816 tree non_primary_binfo;
9818 /* If there are no virtual baseclasses, then there is nothing to
9819 do. */
9820 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9821 return;
9823 t = vid->derived;
9825 /* We might be a primary base class. Go up the inheritance hierarchy
9826 until we find the most derived class of which we are a primary base:
9827 it is the offset of that which we need to use. */
9828 non_primary_binfo = binfo;
9829 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9831 tree b;
9833 /* If we have reached a virtual base, then it must be a primary
9834 base (possibly multi-level) of vid->binfo, or we wouldn't
9835 have called build_vcall_and_vbase_vtbl_entries for it. But it
9836 might be a lost primary, so just skip down to vid->binfo. */
9837 if (BINFO_VIRTUAL_P (non_primary_binfo))
9839 non_primary_binfo = vid->binfo;
9840 break;
9843 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9844 if (get_primary_binfo (b) != non_primary_binfo)
9845 break;
9846 non_primary_binfo = b;
9849 /* Go through the virtual bases, adding the offsets. */
9850 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9851 vbase;
9852 vbase = TREE_CHAIN (vbase))
9854 tree b;
9855 tree delta;
9857 if (!BINFO_VIRTUAL_P (vbase))
9858 continue;
9860 /* Find the instance of this virtual base in the complete
9861 object. */
9862 b = copied_binfo (vbase, binfo);
9864 /* If we've already got an offset for this virtual base, we
9865 don't need another one. */
9866 if (BINFO_VTABLE_PATH_MARKED (b))
9867 continue;
9868 BINFO_VTABLE_PATH_MARKED (b) = 1;
9870 /* Figure out where we can find this vbase offset. */
9871 delta = size_binop (MULT_EXPR,
9872 vid->index,
9873 fold_convert (ssizetype,
9874 TYPE_SIZE_UNIT (vtable_entry_type)));
9875 if (vid->primary_vtbl_p)
9876 BINFO_VPTR_FIELD (b) = delta;
9878 if (binfo != TYPE_BINFO (t))
9879 /* The vbase offset had better be the same. */
9880 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9882 /* The next vbase will come at a more negative offset. */
9883 vid->index = size_binop (MINUS_EXPR, vid->index,
9884 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9886 /* The initializer is the delta from BINFO to this virtual base.
9887 The vbase offsets go in reverse inheritance-graph order, and
9888 we are walking in inheritance graph order so these end up in
9889 the right order. */
9890 delta = size_diffop_loc (input_location,
9891 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9893 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9894 fold_build1_loc (input_location, NOP_EXPR,
9895 vtable_entry_type, delta));
9899 /* Adds the initializers for the vcall offset entries in the vtable
9900 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9901 to VID->INITS. */
9903 static void
9904 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9906 /* We only need these entries if this base is a virtual base. We
9907 compute the indices -- but do not add to the vtable -- when
9908 building the main vtable for a class. */
9909 if (binfo == TYPE_BINFO (vid->derived)
9910 || (BINFO_VIRTUAL_P (binfo)
9911 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9912 correspond to VID->DERIVED), we are building a primary
9913 construction virtual table. Since this is a primary
9914 virtual table, we do not need the vcall offsets for
9915 BINFO. */
9916 && binfo != vid->rtti_binfo))
9918 /* We need a vcall offset for each of the virtual functions in this
9919 vtable. For example:
9921 class A { virtual void f (); };
9922 class B1 : virtual public A { virtual void f (); };
9923 class B2 : virtual public A { virtual void f (); };
9924 class C: public B1, public B2 { virtual void f (); };
9926 A C object has a primary base of B1, which has a primary base of A. A
9927 C also has a secondary base of B2, which no longer has a primary base
9928 of A. So the B2-in-C construction vtable needs a secondary vtable for
9929 A, which will adjust the A* to a B2* to call f. We have no way of
9930 knowing what (or even whether) this offset will be when we define B2,
9931 so we store this "vcall offset" in the A sub-vtable and look it up in
9932 a "virtual thunk" for B2::f.
9934 We need entries for all the functions in our primary vtable and
9935 in our non-virtual bases' secondary vtables. */
9936 vid->vbase = binfo;
9937 /* If we are just computing the vcall indices -- but do not need
9938 the actual entries -- not that. */
9939 if (!BINFO_VIRTUAL_P (binfo))
9940 vid->generate_vcall_entries = false;
9941 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9942 add_vcall_offset_vtbl_entries_r (binfo, vid);
9946 /* Build vcall offsets, starting with those for BINFO. */
9948 static void
9949 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9951 int i;
9952 tree primary_binfo;
9953 tree base_binfo;
9955 /* Don't walk into virtual bases -- except, of course, for the
9956 virtual base for which we are building vcall offsets. Any
9957 primary virtual base will have already had its offsets generated
9958 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9959 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9960 return;
9962 /* If BINFO has a primary base, process it first. */
9963 primary_binfo = get_primary_binfo (binfo);
9964 if (primary_binfo)
9965 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9967 /* Add BINFO itself to the list. */
9968 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9970 /* Scan the non-primary bases of BINFO. */
9971 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9972 if (base_binfo != primary_binfo)
9973 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9976 /* Called from build_vcall_offset_vtbl_entries_r. */
9978 static void
9979 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9981 /* Make entries for the rest of the virtuals. */
9982 tree orig_fn;
9984 /* The ABI requires that the methods be processed in declaration
9985 order. */
9986 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
9987 orig_fn;
9988 orig_fn = DECL_CHAIN (orig_fn))
9989 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9990 add_vcall_offset (orig_fn, binfo, vid);
9993 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9995 static void
9996 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9998 size_t i;
9999 tree vcall_offset;
10000 tree derived_entry;
10002 /* If there is already an entry for a function with the same
10003 signature as FN, then we do not need a second vcall offset.
10004 Check the list of functions already present in the derived
10005 class vtable. */
10006 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
10008 if (same_signature_p (derived_entry, orig_fn)
10009 /* We only use one vcall offset for virtual destructors,
10010 even though there are two virtual table entries. */
10011 || (DECL_DESTRUCTOR_P (derived_entry)
10012 && DECL_DESTRUCTOR_P (orig_fn)))
10013 return;
10016 /* If we are building these vcall offsets as part of building
10017 the vtable for the most derived class, remember the vcall
10018 offset. */
10019 if (vid->binfo == TYPE_BINFO (vid->derived))
10021 tree_pair_s elt = {orig_fn, vid->index};
10022 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
10025 /* The next vcall offset will be found at a more negative
10026 offset. */
10027 vid->index = size_binop (MINUS_EXPR, vid->index,
10028 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
10030 /* Keep track of this function. */
10031 vec_safe_push (vid->fns, orig_fn);
10033 if (vid->generate_vcall_entries)
10035 tree base;
10036 tree fn;
10038 /* Find the overriding function. */
10039 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
10040 if (fn == error_mark_node)
10041 vcall_offset = build_zero_cst (vtable_entry_type);
10042 else
10044 base = TREE_VALUE (fn);
10046 /* The vbase we're working on is a primary base of
10047 vid->binfo. But it might be a lost primary, so its
10048 BINFO_OFFSET might be wrong, so we just use the
10049 BINFO_OFFSET from vid->binfo. */
10050 vcall_offset = size_diffop_loc (input_location,
10051 BINFO_OFFSET (base),
10052 BINFO_OFFSET (vid->binfo));
10053 vcall_offset = fold_build1_loc (input_location,
10054 NOP_EXPR, vtable_entry_type,
10055 vcall_offset);
10057 /* Add the initializer to the vtable. */
10058 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
10062 /* Return vtbl initializers for the RTTI entries corresponding to the
10063 BINFO's vtable. The RTTI entries should indicate the object given
10064 by VID->rtti_binfo. */
10066 static void
10067 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
10069 tree b;
10070 tree t;
10071 tree offset;
10072 tree decl;
10073 tree init;
10075 t = BINFO_TYPE (vid->rtti_binfo);
10077 /* To find the complete object, we will first convert to our most
10078 primary base, and then add the offset in the vtbl to that value. */
10079 b = most_primary_binfo (binfo);
10080 offset = size_diffop_loc (input_location,
10081 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
10083 /* The second entry is the address of the typeinfo object. */
10084 if (flag_rtti)
10085 decl = build_address (get_tinfo_decl (t));
10086 else
10087 decl = integer_zero_node;
10089 /* Convert the declaration to a type that can be stored in the
10090 vtable. */
10091 init = build_nop (vfunc_ptr_type_node, decl);
10092 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10094 /* Add the offset-to-top entry. It comes earlier in the vtable than
10095 the typeinfo entry. Convert the offset to look like a
10096 function pointer, so that we can put it in the vtable. */
10097 init = build_nop (vfunc_ptr_type_node, offset);
10098 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
10101 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
10102 accessibility. */
10104 bool
10105 uniquely_derived_from_p (tree parent, tree type)
10107 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
10108 return base && base != error_mark_node;
10111 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
10113 bool
10114 publicly_uniquely_derived_p (tree parent, tree type)
10116 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
10117 NULL, tf_none);
10118 return base && base != error_mark_node;
10121 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
10122 class between them, if any. */
10124 tree
10125 common_enclosing_class (tree ctx1, tree ctx2)
10127 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
10128 return NULL_TREE;
10129 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
10130 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
10131 if (ctx1 == ctx2)
10132 return ctx1;
10133 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10134 TYPE_MARKED_P (t) = true;
10135 tree found = NULL_TREE;
10136 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
10137 if (TYPE_MARKED_P (t))
10139 found = t;
10140 break;
10142 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
10143 TYPE_MARKED_P (t) = false;
10144 return found;
10147 #include "gt-cp-class.h"