libgo: add misc/cgo files
[official-gcc.git] / gcc / cp / class.c
blob1243f33605f367c5b42369a07242c34289923676
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"
39 #include "asan.h"
41 /* Id for dumping the class hierarchy. */
42 int class_dump_id;
44 /* The number of nested classes being processed. If we are not in the
45 scope of any class, this is zero. */
47 int current_class_depth;
49 /* In order to deal with nested classes, we keep a stack of classes.
50 The topmost entry is the innermost class, and is the entry at index
51 CURRENT_CLASS_DEPTH */
53 typedef struct class_stack_node {
54 /* The name of the class. */
55 tree name;
57 /* The _TYPE node for the class. */
58 tree type;
60 /* The access specifier pending for new declarations in the scope of
61 this class. */
62 tree access;
64 /* If were defining TYPE, the names used in this class. */
65 splay_tree names_used;
67 /* Nonzero if this class is no longer open, because of a call to
68 push_to_top_level. */
69 size_t hidden;
70 }* class_stack_node_t;
72 struct vtbl_init_data
74 /* The base for which we're building initializers. */
75 tree binfo;
76 /* The type of the most-derived type. */
77 tree derived;
78 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
79 unless ctor_vtbl_p is true. */
80 tree rtti_binfo;
81 /* The negative-index vtable initializers built up so far. These
82 are in order from least negative index to most negative index. */
83 vec<constructor_elt, va_gc> *inits;
84 /* The binfo for the virtual base for which we're building
85 vcall offset initializers. */
86 tree vbase;
87 /* The functions in vbase for which we have already provided vcall
88 offsets. */
89 vec<tree, va_gc> *fns;
90 /* The vtable index of the next vcall or vbase offset. */
91 tree index;
92 /* Nonzero if we are building the initializer for the primary
93 vtable. */
94 int primary_vtbl_p;
95 /* Nonzero if we are building the initializer for a construction
96 vtable. */
97 int ctor_vtbl_p;
98 /* True when adding vcall offset entries to the vtable. False when
99 merely computing the indices. */
100 bool generate_vcall_entries;
103 /* The type of a function passed to walk_subobject_offsets. */
104 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
106 /* The stack itself. This is a dynamically resized array. The
107 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
108 static int current_class_stack_size;
109 static class_stack_node_t current_class_stack;
111 /* The size of the largest empty class seen in this translation unit. */
112 static GTY (()) tree sizeof_biggest_empty_class;
114 /* An array of all local classes present in this translation unit, in
115 declaration order. */
116 vec<tree, va_gc> *local_classes;
118 static tree get_vfield_name (tree);
119 static void finish_struct_anon (tree);
120 static tree get_vtable_name (tree);
121 static void get_basefndecls (tree, tree, vec<tree> *);
122 static int build_primary_vtable (tree, tree);
123 static int build_secondary_vtable (tree);
124 static void finish_vtbls (tree);
125 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
126 static void finish_struct_bits (tree);
127 static int alter_access (tree, tree, tree);
128 static void handle_using_decl (tree, tree);
129 static tree dfs_modify_vtables (tree, void *);
130 static tree modify_all_vtables (tree, tree);
131 static void determine_primary_bases (tree);
132 static void finish_struct_methods (tree);
133 static void maybe_warn_about_overly_private_class (tree);
134 static int method_name_cmp (const void *, const void *);
135 static int resort_method_name_cmp (const void *, const void *);
136 static void add_implicitly_declared_members (tree, tree*, int, int);
137 static tree fixed_type_or_null (tree, int *, int *);
138 static tree build_simple_base_path (tree expr, tree binfo);
139 static tree build_vtbl_ref_1 (tree, tree);
140 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
141 vec<constructor_elt, va_gc> **);
142 static int count_fields (tree);
143 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
144 static void insert_into_classtype_sorted_fields (tree, tree, int);
145 static bool check_bitfield_decl (tree);
146 static bool check_field_decl (tree, tree, int *, int *);
147 static void check_field_decls (tree, tree *, int *, int *);
148 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
149 static void build_base_fields (record_layout_info, splay_tree, tree *);
150 static void check_methods (tree);
151 static void remove_zero_width_bit_fields (tree);
152 static bool accessible_nvdtor_p (tree);
154 /* Used by find_flexarrays and related functions. */
155 struct flexmems_t;
156 static void diagnose_flexarrays (tree, const flexmems_t *);
157 static void find_flexarrays (tree, flexmems_t *, bool = false,
158 tree = NULL_TREE, tree = NULL_TREE);
159 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
160 static void check_bases (tree, int *, int *);
161 static void check_bases_and_members (tree);
162 static tree create_vtable_ptr (tree, tree *);
163 static void include_empty_classes (record_layout_info);
164 static void layout_class_type (tree, tree *);
165 static void propagate_binfo_offsets (tree, tree);
166 static void layout_virtual_bases (record_layout_info, splay_tree);
167 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
168 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
169 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
170 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
171 static void add_vcall_offset (tree, tree, vtbl_init_data *);
172 static void layout_vtable_decl (tree, int);
173 static tree dfs_find_final_overrider_pre (tree, void *);
174 static tree dfs_find_final_overrider_post (tree, void *);
175 static tree find_final_overrider (tree, tree, tree);
176 static int make_new_vtable (tree, tree);
177 static tree get_primary_binfo (tree);
178 static int maybe_indent_hierarchy (FILE *, int, int);
179 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
180 static void dump_class_hierarchy (tree);
181 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
182 static void dump_array (FILE *, tree);
183 static void dump_vtable (tree, tree, tree);
184 static void dump_vtt (tree, tree);
185 static void dump_thunk (FILE *, int, tree);
186 static tree build_vtable (tree, tree, tree);
187 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
188 static void layout_nonempty_base_or_field (record_layout_info,
189 tree, tree, splay_tree);
190 static tree end_of_class (tree, int);
191 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
192 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
193 vec<constructor_elt, va_gc> **);
194 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
195 vec<constructor_elt, va_gc> **);
196 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
197 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
198 static void clone_constructors_and_destructors (tree);
199 static tree build_clone (tree, tree);
200 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
201 static void build_ctor_vtbl_group (tree, tree);
202 static void build_vtt (tree);
203 static tree binfo_ctor_vtable (tree);
204 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
205 tree *);
206 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
207 static tree dfs_fixup_binfo_vtbls (tree, void *);
208 static int record_subobject_offset (tree, tree, splay_tree);
209 static int check_subobject_offset (tree, tree, splay_tree);
210 static int walk_subobject_offsets (tree, subobject_offset_fn,
211 tree, splay_tree, tree, int);
212 static void record_subobject_offsets (tree, tree, splay_tree, bool);
213 static int layout_conflict_p (tree, tree, splay_tree, int);
214 static int splay_tree_compare_integer_csts (splay_tree_key k1,
215 splay_tree_key k2);
216 static void warn_about_ambiguous_bases (tree);
217 static bool type_requires_array_cookie (tree);
218 static bool base_derived_from (tree, tree);
219 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
220 static tree end_of_base (tree);
221 static tree get_vcall_index (tree, tree);
222 static bool type_maybe_constexpr_default_constructor (tree);
224 /* Variables shared between class.c and call.c. */
226 int n_vtables = 0;
227 int n_vtable_entries = 0;
228 int n_vtable_searches = 0;
229 int n_vtable_elems = 0;
230 int n_convert_harshness = 0;
231 int n_compute_conversion_costs = 0;
232 int n_inner_fields_searched = 0;
234 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
235 'structor is in charge of 'structing virtual bases, or FALSE_STMT
236 otherwise. */
238 tree
239 build_if_in_charge (tree true_stmt, tree false_stmt)
241 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
242 tree cmp = build2 (NE_EXPR, boolean_type_node,
243 current_in_charge_parm, integer_zero_node);
244 tree type = unlowered_expr_type (true_stmt);
245 if (VOID_TYPE_P (type))
246 type = unlowered_expr_type (false_stmt);
247 tree cond = build3 (COND_EXPR, type,
248 cmp, true_stmt, false_stmt);
249 return cond;
252 /* Convert to or from a base subobject. EXPR is an expression of type
253 `A' or `A*', an expression of type `B' or `B*' is returned. To
254 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
255 the B base instance within A. To convert base A to derived B, CODE
256 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
257 In this latter case, A must not be a morally virtual base of B.
258 NONNULL is true if EXPR is known to be non-NULL (this is only
259 needed when EXPR is of pointer type). CV qualifiers are preserved
260 from EXPR. */
262 tree
263 build_base_path (enum tree_code code,
264 tree expr,
265 tree binfo,
266 int nonnull,
267 tsubst_flags_t complain)
269 tree v_binfo = NULL_TREE;
270 tree d_binfo = NULL_TREE;
271 tree probe;
272 tree offset;
273 tree target_type;
274 tree null_test = NULL;
275 tree ptr_target_type;
276 int fixed_type_p;
277 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
278 bool has_empty = false;
279 bool virtual_access;
280 bool rvalue = false;
282 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
283 return error_mark_node;
285 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
287 d_binfo = probe;
288 if (is_empty_class (BINFO_TYPE (probe)))
289 has_empty = true;
290 if (!v_binfo && BINFO_VIRTUAL_P (probe))
291 v_binfo = probe;
294 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
295 if (want_pointer)
296 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
298 if (code == PLUS_EXPR
299 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
301 /* This can happen when adjust_result_of_qualified_name_lookup can't
302 find a unique base binfo in a call to a member function. We
303 couldn't give the diagnostic then since we might have been calling
304 a static member function, so we do it now. In other cases, eg.
305 during error recovery (c++/71979), we may not have a base at all. */
306 if (complain & tf_error)
308 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
309 ba_unique, NULL, complain);
310 gcc_assert (base == error_mark_node || !base);
312 return error_mark_node;
315 gcc_assert ((code == MINUS_EXPR
316 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
317 || code == PLUS_EXPR);
319 if (binfo == d_binfo)
320 /* Nothing to do. */
321 return expr;
323 if (code == MINUS_EXPR && v_binfo)
325 if (complain & tf_error)
327 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
329 if (want_pointer)
330 error ("cannot convert from pointer to base class %qT to "
331 "pointer to derived class %qT because the base is "
332 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
333 else
334 error ("cannot convert from base class %qT to derived "
335 "class %qT because the base is virtual",
336 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
338 else
340 if (want_pointer)
341 error ("cannot convert from pointer to base class %qT to "
342 "pointer to derived class %qT via virtual base %qT",
343 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
344 BINFO_TYPE (v_binfo));
345 else
346 error ("cannot convert from base class %qT to derived "
347 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
348 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
351 return error_mark_node;
354 if (!want_pointer)
356 rvalue = !lvalue_p (expr);
357 /* This must happen before the call to save_expr. */
358 expr = cp_build_addr_expr (expr, complain);
360 else
361 expr = mark_rvalue_use (expr);
363 offset = BINFO_OFFSET (binfo);
364 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
365 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
366 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
367 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
368 expression returned matches the input. */
369 target_type = cp_build_qualified_type
370 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
371 ptr_target_type = build_pointer_type (target_type);
373 /* Do we need to look in the vtable for the real offset? */
374 virtual_access = (v_binfo && fixed_type_p <= 0);
376 /* Don't bother with the calculations inside sizeof; they'll ICE if the
377 source type is incomplete and the pointer value doesn't matter. In a
378 template (even in instantiate_non_dependent_expr), we don't have vtables
379 set up properly yet, and the value doesn't matter there either; we're
380 just interested in the result of overload resolution. */
381 if (cp_unevaluated_operand != 0
382 || processing_template_decl
383 || in_template_function ())
385 expr = build_nop (ptr_target_type, expr);
386 goto indout;
389 /* If we're in an NSDMI, we don't have the full constructor context yet
390 that we need for converting to a virtual base, so just build a stub
391 CONVERT_EXPR and expand it later in bot_replace. */
392 if (virtual_access && fixed_type_p < 0
393 && current_scope () != current_function_decl)
395 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
396 CONVERT_EXPR_VBASE_PATH (expr) = true;
397 goto indout;
400 /* Do we need to check for a null pointer? */
401 if (want_pointer && !nonnull)
403 /* If we know the conversion will not actually change the value
404 of EXPR, then we can avoid testing the expression for NULL.
405 We have to avoid generating a COMPONENT_REF for a base class
406 field, because other parts of the compiler know that such
407 expressions are always non-NULL. */
408 if (!virtual_access && integer_zerop (offset))
409 return build_nop (ptr_target_type, expr);
410 null_test = error_mark_node;
413 /* Protect against multiple evaluation if necessary. */
414 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
415 expr = save_expr (expr);
417 /* Now that we've saved expr, build the real null test. */
418 if (null_test)
420 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
421 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
422 expr, zero);
423 /* This is a compiler generated comparison, don't emit
424 e.g. -Wnonnull-compare warning for it. */
425 TREE_NO_WARNING (null_test) = 1;
428 /* If this is a simple base reference, express it as a COMPONENT_REF. */
429 if (code == PLUS_EXPR && !virtual_access
430 /* We don't build base fields for empty bases, and they aren't very
431 interesting to the optimizers anyway. */
432 && !has_empty)
434 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
435 expr = build_simple_base_path (expr, binfo);
436 if (rvalue)
437 expr = move (expr);
438 if (want_pointer)
439 expr = build_address (expr);
440 target_type = TREE_TYPE (expr);
441 goto out;
444 if (virtual_access)
446 /* Going via virtual base V_BINFO. We need the static offset
447 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
448 V_BINFO. That offset is an entry in D_BINFO's vtable. */
449 tree v_offset;
451 if (fixed_type_p < 0 && in_base_initializer)
453 /* In a base member initializer, we cannot rely on the
454 vtable being set up. We have to indirect via the
455 vtt_parm. */
456 tree t;
458 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
459 t = build_pointer_type (t);
460 v_offset = fold_convert (t, current_vtt_parm);
461 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
463 else
465 tree t = expr;
466 if (sanitize_flags_p (SANITIZE_VPTR)
467 && fixed_type_p == 0)
469 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
470 probe, expr);
471 if (t == NULL_TREE)
472 t = expr;
474 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL,
475 complain),
476 TREE_TYPE (TREE_TYPE (expr)));
479 if (v_offset == error_mark_node)
480 return error_mark_node;
482 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
483 v_offset = build1 (NOP_EXPR,
484 build_pointer_type (ptrdiff_type_node),
485 v_offset);
486 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain);
487 TREE_CONSTANT (v_offset) = 1;
489 offset = convert_to_integer (ptrdiff_type_node,
490 size_diffop_loc (input_location, offset,
491 BINFO_OFFSET (v_binfo)));
493 if (!integer_zerop (offset))
494 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
496 if (fixed_type_p < 0)
497 /* Negative fixed_type_p means this is a constructor or destructor;
498 virtual base layout is fixed in in-charge [cd]tors, but not in
499 base [cd]tors. */
500 offset = build_if_in_charge
501 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
502 v_offset);
503 else
504 offset = v_offset;
507 if (want_pointer)
508 target_type = ptr_target_type;
510 expr = build1 (NOP_EXPR, ptr_target_type, expr);
512 if (!integer_zerop (offset))
514 offset = fold_convert (sizetype, offset);
515 if (code == MINUS_EXPR)
516 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
517 expr = fold_build_pointer_plus (expr, offset);
519 else
520 null_test = NULL;
522 indout:
523 if (!want_pointer)
525 expr = cp_build_indirect_ref (expr, RO_NULL, complain);
526 if (rvalue)
527 expr = move (expr);
530 out:
531 if (null_test)
532 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
533 build_zero_cst (target_type));
535 return expr;
538 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
539 Perform a derived-to-base conversion by recursively building up a
540 sequence of COMPONENT_REFs to the appropriate base fields. */
542 static tree
543 build_simple_base_path (tree expr, tree binfo)
545 tree type = BINFO_TYPE (binfo);
546 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
547 tree field;
549 if (d_binfo == NULL_TREE)
551 tree temp;
553 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
555 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
556 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
557 an lvalue in the front end; only _DECLs and _REFs are lvalues
558 in the back end. */
559 temp = unary_complex_lvalue (ADDR_EXPR, expr);
560 if (temp)
561 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error);
563 return expr;
566 /* Recurse. */
567 expr = build_simple_base_path (expr, d_binfo);
569 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
570 field; field = DECL_CHAIN (field))
571 /* Is this the base field created by build_base_field? */
572 if (TREE_CODE (field) == FIELD_DECL
573 && DECL_FIELD_IS_BASE (field)
574 && TREE_TYPE (field) == type
575 /* If we're looking for a field in the most-derived class,
576 also check the field offset; we can have two base fields
577 of the same type if one is an indirect virtual base and one
578 is a direct non-virtual base. */
579 && (BINFO_INHERITANCE_CHAIN (d_binfo)
580 || tree_int_cst_equal (byte_position (field),
581 BINFO_OFFSET (binfo))))
583 /* We don't use build_class_member_access_expr here, as that
584 has unnecessary checks, and more importantly results in
585 recursive calls to dfs_walk_once. */
586 int type_quals = cp_type_quals (TREE_TYPE (expr));
588 expr = build3 (COMPONENT_REF,
589 cp_build_qualified_type (type, type_quals),
590 expr, field, NULL_TREE);
591 /* Mark the expression const or volatile, as appropriate.
592 Even though we've dealt with the type above, we still have
593 to mark the expression itself. */
594 if (type_quals & TYPE_QUAL_CONST)
595 TREE_READONLY (expr) = 1;
596 if (type_quals & TYPE_QUAL_VOLATILE)
597 TREE_THIS_VOLATILE (expr) = 1;
599 return expr;
602 /* Didn't find the base field?!? */
603 gcc_unreachable ();
606 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
607 type is a class type or a pointer to a class type. In the former
608 case, TYPE is also a class type; in the latter it is another
609 pointer type. If CHECK_ACCESS is true, an error message is emitted
610 if TYPE is inaccessible. If OBJECT has pointer type, the value is
611 assumed to be non-NULL. */
613 tree
614 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
615 tsubst_flags_t complain)
617 tree binfo;
618 tree object_type;
620 if (TYPE_PTR_P (TREE_TYPE (object)))
622 object_type = TREE_TYPE (TREE_TYPE (object));
623 type = TREE_TYPE (type);
625 else
626 object_type = TREE_TYPE (object);
628 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
629 NULL, complain);
630 if (!binfo || binfo == error_mark_node)
631 return error_mark_node;
633 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
636 /* EXPR is an expression with unqualified class type. BASE is a base
637 binfo of that class type. Returns EXPR, converted to the BASE
638 type. This function assumes that EXPR is the most derived class;
639 therefore virtual bases can be found at their static offsets. */
641 tree
642 convert_to_base_statically (tree expr, tree base)
644 tree expr_type;
646 expr_type = TREE_TYPE (expr);
647 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
649 /* If this is a non-empty base, use a COMPONENT_REF. */
650 if (!is_empty_class (BINFO_TYPE (base)))
651 return build_simple_base_path (expr, base);
653 /* We use fold_build2 and fold_convert below to simplify the trees
654 provided to the optimizers. It is not safe to call these functions
655 when processing a template because they do not handle C++-specific
656 trees. */
657 gcc_assert (!processing_template_decl);
658 expr = cp_build_addr_expr (expr, tf_warning_or_error);
659 if (!integer_zerop (BINFO_OFFSET (base)))
660 expr = fold_build_pointer_plus_loc (input_location,
661 expr, BINFO_OFFSET (base));
662 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
663 expr = build_fold_indirect_ref_loc (input_location, expr);
666 return expr;
670 tree
671 build_vfield_ref (tree datum, tree type)
673 tree vfield, vcontext;
675 if (datum == error_mark_node
676 /* Can happen in case of duplicate base types (c++/59082). */
677 || !TYPE_VFIELD (type))
678 return error_mark_node;
680 /* First, convert to the requested type. */
681 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
682 datum = convert_to_base (datum, type, /*check_access=*/false,
683 /*nonnull=*/true, tf_warning_or_error);
685 /* Second, the requested type may not be the owner of its own vptr.
686 If not, convert to the base class that owns it. We cannot use
687 convert_to_base here, because VCONTEXT may appear more than once
688 in the inheritance hierarchy of TYPE, and thus direct conversion
689 between the types may be ambiguous. Following the path back up
690 one step at a time via primary bases avoids the problem. */
691 vfield = TYPE_VFIELD (type);
692 vcontext = DECL_CONTEXT (vfield);
693 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
695 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
696 type = TREE_TYPE (datum);
699 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
702 /* Given an object INSTANCE, return an expression which yields the
703 vtable element corresponding to INDEX. There are many special
704 cases for INSTANCE which we take care of here, mainly to avoid
705 creating extra tree nodes when we don't have to. */
707 static tree
708 build_vtbl_ref_1 (tree instance, tree idx)
710 tree aref;
711 tree vtbl = NULL_TREE;
713 /* Try to figure out what a reference refers to, and
714 access its virtual function table directly. */
716 int cdtorp = 0;
717 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
719 tree basetype = non_reference (TREE_TYPE (instance));
721 if (fixed_type && !cdtorp)
723 tree binfo = lookup_base (fixed_type, basetype,
724 ba_unique, NULL, tf_none);
725 if (binfo && binfo != error_mark_node)
726 vtbl = unshare_expr (BINFO_VTABLE (binfo));
729 if (!vtbl)
730 vtbl = build_vfield_ref (instance, basetype);
732 aref = build_array_ref (input_location, vtbl, idx);
733 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
735 return aref;
738 tree
739 build_vtbl_ref (tree instance, tree idx)
741 tree aref = build_vtbl_ref_1 (instance, idx);
743 return aref;
746 /* Given a stable object pointer INSTANCE_PTR, return an expression which
747 yields a function pointer corresponding to vtable element INDEX. */
749 tree
750 build_vfn_ref (tree instance_ptr, tree idx)
752 tree aref;
754 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL,
755 tf_warning_or_error),
756 idx);
758 /* When using function descriptors, the address of the
759 vtable entry is treated as a function pointer. */
760 if (TARGET_VTABLE_USES_DESCRIPTORS)
761 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
762 cp_build_addr_expr (aref, tf_warning_or_error));
764 /* Remember this as a method reference, for later devirtualization. */
765 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
767 return aref;
770 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
771 for the given TYPE. */
773 static tree
774 get_vtable_name (tree type)
776 return mangle_vtbl_for_type (type);
779 /* DECL is an entity associated with TYPE, like a virtual table or an
780 implicitly generated constructor. Determine whether or not DECL
781 should have external or internal linkage at the object file
782 level. This routine does not deal with COMDAT linkage and other
783 similar complexities; it simply sets TREE_PUBLIC if it possible for
784 entities in other translation units to contain copies of DECL, in
785 the abstract. */
787 void
788 set_linkage_according_to_type (tree /*type*/, tree decl)
790 TREE_PUBLIC (decl) = 1;
791 determine_visibility (decl);
794 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
795 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
796 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
798 static tree
799 build_vtable (tree class_type, tree name, tree vtable_type)
801 tree decl;
803 decl = build_lang_decl (VAR_DECL, name, vtable_type);
804 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
805 now to avoid confusion in mangle_decl. */
806 SET_DECL_ASSEMBLER_NAME (decl, name);
807 DECL_CONTEXT (decl) = class_type;
808 DECL_ARTIFICIAL (decl) = 1;
809 TREE_STATIC (decl) = 1;
810 TREE_READONLY (decl) = 1;
811 DECL_VIRTUAL_P (decl) = 1;
812 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
813 DECL_USER_ALIGN (decl) = true;
814 DECL_VTABLE_OR_VTT_P (decl) = 1;
815 set_linkage_according_to_type (class_type, decl);
816 /* The vtable has not been defined -- yet. */
817 DECL_EXTERNAL (decl) = 1;
818 DECL_NOT_REALLY_EXTERN (decl) = 1;
820 /* Mark the VAR_DECL node representing the vtable itself as a
821 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
822 is rather important that such things be ignored because any
823 effort to actually generate DWARF for them will run into
824 trouble when/if we encounter code like:
826 #pragma interface
827 struct S { virtual void member (); };
829 because the artificial declaration of the vtable itself (as
830 manufactured by the g++ front end) will say that the vtable is
831 a static member of `S' but only *after* the debug output for
832 the definition of `S' has already been output. This causes
833 grief because the DWARF entry for the definition of the vtable
834 will try to refer back to an earlier *declaration* of the
835 vtable as a static member of `S' and there won't be one. We
836 might be able to arrange to have the "vtable static member"
837 attached to the member list for `S' before the debug info for
838 `S' get written (which would solve the problem) but that would
839 require more intrusive changes to the g++ front end. */
840 DECL_IGNORED_P (decl) = 1;
842 return decl;
845 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
846 or even complete. If this does not exist, create it. If COMPLETE is
847 nonzero, then complete the definition of it -- that will render it
848 impossible to actually build the vtable, but is useful to get at those
849 which are known to exist in the runtime. */
851 tree
852 get_vtable_decl (tree type, int complete)
854 tree decl;
856 if (CLASSTYPE_VTABLES (type))
857 return CLASSTYPE_VTABLES (type);
859 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
860 CLASSTYPE_VTABLES (type) = decl;
862 if (complete)
864 DECL_EXTERNAL (decl) = 1;
865 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
868 return decl;
871 /* Build the primary virtual function table for TYPE. If BINFO is
872 non-NULL, build the vtable starting with the initial approximation
873 that it is the same as the one which is the head of the association
874 list. Returns a nonzero value if a new vtable is actually
875 created. */
877 static int
878 build_primary_vtable (tree binfo, tree type)
880 tree decl;
881 tree virtuals;
883 decl = get_vtable_decl (type, /*complete=*/0);
885 if (binfo)
887 if (BINFO_NEW_VTABLE_MARKED (binfo))
888 /* We have already created a vtable for this base, so there's
889 no need to do it again. */
890 return 0;
892 virtuals = copy_list (BINFO_VIRTUALS (binfo));
893 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
894 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
895 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
897 else
899 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
900 virtuals = NULL_TREE;
903 if (GATHER_STATISTICS)
905 n_vtables += 1;
906 n_vtable_elems += list_length (virtuals);
909 /* Initialize the association list for this type, based
910 on our first approximation. */
911 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
912 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
913 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
914 return 1;
917 /* Give BINFO a new virtual function table which is initialized
918 with a skeleton-copy of its original initialization. The only
919 entry that changes is the `delta' entry, so we can really
920 share a lot of structure.
922 FOR_TYPE is the most derived type which caused this table to
923 be needed.
925 Returns nonzero if we haven't met BINFO before.
927 The order in which vtables are built (by calling this function) for
928 an object must remain the same, otherwise a binary incompatibility
929 can result. */
931 static int
932 build_secondary_vtable (tree binfo)
934 if (BINFO_NEW_VTABLE_MARKED (binfo))
935 /* We already created a vtable for this base. There's no need to
936 do it again. */
937 return 0;
939 /* Remember that we've created a vtable for this BINFO, so that we
940 don't try to do so again. */
941 SET_BINFO_NEW_VTABLE_MARKED (binfo);
943 /* Make fresh virtual list, so we can smash it later. */
944 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
946 /* Secondary vtables are laid out as part of the same structure as
947 the primary vtable. */
948 BINFO_VTABLE (binfo) = NULL_TREE;
949 return 1;
952 /* Create a new vtable for BINFO which is the hierarchy dominated by
953 T. Return nonzero if we actually created a new vtable. */
955 static int
956 make_new_vtable (tree t, tree binfo)
958 if (binfo == TYPE_BINFO (t))
959 /* In this case, it is *type*'s vtable we are modifying. We start
960 with the approximation that its vtable is that of the
961 immediate base class. */
962 return build_primary_vtable (binfo, t);
963 else
964 /* This is our very own copy of `basetype' to play with. Later,
965 we will fill in all the virtual functions that override the
966 virtual functions in these base classes which are not defined
967 by the current type. */
968 return build_secondary_vtable (binfo);
971 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
972 (which is in the hierarchy dominated by T) list FNDECL as its
973 BV_FN. DELTA is the required constant adjustment from the `this'
974 pointer where the vtable entry appears to the `this' required when
975 the function is actually called. */
977 static void
978 modify_vtable_entry (tree t,
979 tree binfo,
980 tree fndecl,
981 tree delta,
982 tree *virtuals)
984 tree v;
986 v = *virtuals;
988 if (fndecl != BV_FN (v)
989 || !tree_int_cst_equal (delta, BV_DELTA (v)))
991 /* We need a new vtable for BINFO. */
992 if (make_new_vtable (t, binfo))
994 /* If we really did make a new vtable, we also made a copy
995 of the BINFO_VIRTUALS list. Now, we have to find the
996 corresponding entry in that list. */
997 *virtuals = BINFO_VIRTUALS (binfo);
998 while (BV_FN (*virtuals) != BV_FN (v))
999 *virtuals = TREE_CHAIN (*virtuals);
1000 v = *virtuals;
1003 BV_DELTA (v) = delta;
1004 BV_VCALL_INDEX (v) = NULL_TREE;
1005 BV_FN (v) = fndecl;
1010 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1011 METHOD is being injected via a using_decl. Returns true if the
1012 method could be added to the method vec. */
1014 bool
1015 add_method (tree type, tree method, bool via_using)
1017 unsigned slot;
1018 bool template_conv_p = false;
1019 bool conv_p;
1020 vec<tree, va_gc> *method_vec;
1021 bool complete_p;
1022 bool insert_p = false;
1023 tree current_fns;
1025 if (method == error_mark_node)
1026 return false;
1028 complete_p = COMPLETE_TYPE_P (type);
1029 conv_p = DECL_CONV_FN_P (method);
1030 if (conv_p)
1031 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
1032 && DECL_TEMPLATE_CONV_FN_P (method));
1034 method_vec = CLASSTYPE_METHOD_VEC (type);
1035 if (!method_vec)
1037 /* Make a new method vector. We start with 8 entries. We must
1038 allocate at least two (for constructors and destructors), and
1039 we're going to end up with an assignment operator at some
1040 point as well. */
1041 vec_alloc (method_vec, 8);
1042 /* Create slots for constructors and destructors. */
1043 method_vec->quick_push (NULL_TREE);
1044 method_vec->quick_push (NULL_TREE);
1045 CLASSTYPE_METHOD_VEC (type) = method_vec;
1048 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1049 grok_special_member_properties (method);
1051 /* Constructors and destructors go in special slots. */
1052 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1053 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1054 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1055 slot = CLASSTYPE_DESTRUCTOR_SLOT;
1056 else
1058 tree m;
1060 insert_p = true;
1061 /* See if we already have an entry with this name. */
1062 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
1063 vec_safe_iterate (method_vec, slot, &m);
1064 ++slot)
1066 m = OVL_FIRST (m);
1067 if (template_conv_p)
1069 if (TREE_CODE (m) == TEMPLATE_DECL
1070 && DECL_TEMPLATE_CONV_FN_P (m))
1071 insert_p = false;
1072 break;
1074 if (conv_p && !DECL_CONV_FN_P (m))
1075 break;
1076 if (DECL_NAME (m) == DECL_NAME (method))
1078 insert_p = false;
1079 break;
1081 if (complete_p
1082 && !DECL_CONV_FN_P (m)
1083 && DECL_NAME (m) > DECL_NAME (method))
1084 break;
1087 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot];
1089 /* Check to see if we've already got this method. */
1090 for (ovl_iterator iter (current_fns); iter; ++iter)
1092 tree fn = *iter;
1093 tree fn_type;
1094 tree method_type;
1095 tree parms1;
1096 tree parms2;
1098 if (TREE_CODE (fn) != TREE_CODE (method))
1099 continue;
1101 /* Two using-declarations can coexist, we'll complain about ambiguity in
1102 overload resolution. */
1103 if (via_using && iter.using_p ()
1104 /* Except handle inherited constructors specially. */
1105 && ! DECL_CONSTRUCTOR_P (fn))
1106 continue;
1108 /* [over.load] Member function declarations with the
1109 same name and the same parameter types cannot be
1110 overloaded if any of them is a static member
1111 function declaration.
1113 [over.load] Member function declarations with the same name and
1114 the same parameter-type-list as well as member function template
1115 declarations with the same name, the same parameter-type-list, and
1116 the same template parameter lists cannot be overloaded if any of
1117 them, but not all, have a ref-qualifier.
1119 [namespace.udecl] When a using-declaration brings names
1120 from a base class into a derived class scope, member
1121 functions in the derived class override and/or hide member
1122 functions with the same name and parameter types in a base
1123 class (rather than conflicting). */
1124 fn_type = TREE_TYPE (fn);
1125 method_type = TREE_TYPE (method);
1126 parms1 = TYPE_ARG_TYPES (fn_type);
1127 parms2 = TYPE_ARG_TYPES (method_type);
1129 /* Compare the quals on the 'this' parm. Don't compare
1130 the whole types, as used functions are treated as
1131 coming from the using class in overload resolution. */
1132 if (! DECL_STATIC_FUNCTION_P (fn)
1133 && ! DECL_STATIC_FUNCTION_P (method)
1134 /* Either both or neither need to be ref-qualified for
1135 differing quals to allow overloading. */
1136 && (FUNCTION_REF_QUALIFIED (fn_type)
1137 == FUNCTION_REF_QUALIFIED (method_type))
1138 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1139 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1140 continue;
1142 /* For templates, the return type and template parameters
1143 must be identical. */
1144 if (TREE_CODE (fn) == TEMPLATE_DECL
1145 && (!same_type_p (TREE_TYPE (fn_type),
1146 TREE_TYPE (method_type))
1147 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1148 DECL_TEMPLATE_PARMS (method))))
1149 continue;
1151 if (! DECL_STATIC_FUNCTION_P (fn))
1152 parms1 = TREE_CHAIN (parms1);
1153 if (! DECL_STATIC_FUNCTION_P (method))
1154 parms2 = TREE_CHAIN (parms2);
1156 /* Bring back parameters omitted from an inherited ctor. */
1157 if (ctor_omit_inherited_parms (fn))
1158 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1159 if (ctor_omit_inherited_parms (method))
1160 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1162 if (compparms (parms1, parms2)
1163 && (!DECL_CONV_FN_P (fn)
1164 || same_type_p (TREE_TYPE (fn_type),
1165 TREE_TYPE (method_type)))
1166 && equivalently_constrained (fn, method))
1168 /* For function versions, their parms and types match
1169 but they are not duplicates. Record function versions
1170 as and when they are found. extern "C" functions are
1171 not treated as versions. */
1172 if (TREE_CODE (fn) == FUNCTION_DECL
1173 && TREE_CODE (method) == FUNCTION_DECL
1174 && !DECL_EXTERN_C_P (fn)
1175 && !DECL_EXTERN_C_P (method)
1176 && targetm.target_option.function_versions (fn, method))
1178 /* Mark functions as versions if necessary. Modify the mangled
1179 decl name if necessary. */
1180 if (!DECL_FUNCTION_VERSIONED (fn))
1182 DECL_FUNCTION_VERSIONED (fn) = 1;
1183 if (DECL_ASSEMBLER_NAME_SET_P (fn))
1184 mangle_decl (fn);
1186 if (!DECL_FUNCTION_VERSIONED (method))
1188 DECL_FUNCTION_VERSIONED (method) = 1;
1189 if (DECL_ASSEMBLER_NAME_SET_P (method))
1190 mangle_decl (method);
1192 cgraph_node::record_function_versions (fn, method);
1193 continue;
1196 if (DECL_INHERITED_CTOR (method))
1198 if (DECL_INHERITED_CTOR (fn))
1200 tree basem = DECL_INHERITED_CTOR_BASE (method);
1201 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1202 if (flag_new_inheriting_ctors)
1204 if (basem == basef)
1206 /* Inheriting the same constructor along different
1207 paths, combine them. */
1208 SET_DECL_INHERITED_CTOR
1209 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1210 DECL_INHERITED_CTOR (fn)));
1211 /* And discard the new one. */
1212 return false;
1214 else
1215 /* Inherited ctors can coexist until overload
1216 resolution. */
1217 continue;
1219 error_at (DECL_SOURCE_LOCATION (method),
1220 "%q#D", method);
1221 error_at (DECL_SOURCE_LOCATION (fn),
1222 "conflicts with version inherited from %qT",
1223 basef);
1225 /* Otherwise defer to the other function. */
1226 return false;
1229 if (via_using)
1230 /* Defer to the local function. */
1231 return false;
1232 else if (flag_new_inheriting_ctors
1233 && DECL_INHERITED_CTOR (fn))
1235 /* Remove the inherited constructor. */
1236 current_fns = iter.remove_node (current_fns);
1237 continue;
1239 else
1241 error ("%q+#D cannot be overloaded", method);
1242 error ("with %q+#D", fn);
1243 return false;
1248 /* A class should never have more than one destructor. */
1249 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1250 return false;
1252 current_fns = ovl_insert (method, current_fns, via_using);
1254 if (conv_p)
1255 TYPE_HAS_CONVERSION (type) = 1;
1256 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
1257 push_class_level_binding (DECL_NAME (method), current_fns);
1259 if (insert_p)
1261 bool reallocated;
1263 /* We only expect to add few methods in the COMPLETE_P case, so
1264 just make room for one more method in that case. */
1265 if (complete_p)
1266 reallocated = vec_safe_reserve_exact (method_vec, 1);
1267 else
1268 reallocated = vec_safe_reserve (method_vec, 1);
1269 if (reallocated)
1270 CLASSTYPE_METHOD_VEC (type) = method_vec;
1271 if (slot == method_vec->length ())
1272 method_vec->quick_push (current_fns);
1273 else
1274 method_vec->quick_insert (slot, current_fns);
1276 else
1277 /* Replace the current slot. */
1278 (*method_vec)[slot] = current_fns;
1279 return true;
1282 /* Subroutines of finish_struct. */
1284 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1285 legit, otherwise return 0. */
1287 static int
1288 alter_access (tree t, tree fdecl, tree access)
1290 tree elem;
1292 retrofit_lang_decl (fdecl);
1294 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1296 elem = purpose_member (t, DECL_ACCESS (fdecl));
1297 if (elem)
1299 if (TREE_VALUE (elem) != access)
1301 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1302 error ("conflicting access specifications for method"
1303 " %q+D, ignored", TREE_TYPE (fdecl));
1304 else
1305 error ("conflicting access specifications for field %qE, ignored",
1306 DECL_NAME (fdecl));
1308 else
1310 /* They're changing the access to the same thing they changed
1311 it to before. That's OK. */
1315 else
1317 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1318 tf_warning_or_error);
1319 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1320 return 1;
1322 return 0;
1325 /* Return the access node for DECL's access in its enclosing class. */
1327 tree
1328 declared_access (tree decl)
1330 return (TREE_PRIVATE (decl) ? access_private_node
1331 : TREE_PROTECTED (decl) ? access_protected_node
1332 : access_public_node);
1335 /* Process the USING_DECL, which is a member of T. */
1337 static void
1338 handle_using_decl (tree using_decl, tree t)
1340 tree decl = USING_DECL_DECLS (using_decl);
1341 tree name = DECL_NAME (using_decl);
1342 tree access = declared_access (using_decl);
1343 tree flist = NULL_TREE;
1344 tree old_value;
1346 gcc_assert (!processing_template_decl && decl);
1348 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1349 tf_warning_or_error);
1350 if (old_value)
1352 old_value = OVL_FIRST (old_value);
1354 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1355 /* OK */;
1356 else
1357 old_value = NULL_TREE;
1360 cp_emit_debug_info_for_using (decl, t);
1362 if (is_overloaded_fn (decl))
1363 flist = decl;
1365 if (! old_value)
1367 else if (is_overloaded_fn (old_value))
1369 if (flist)
1370 /* It's OK to use functions from a base when there are functions with
1371 the same name already present in the current class. */;
1372 else
1374 error ("%q+D invalid in %q#T", using_decl, t);
1375 error (" because of local method %q+#D with same name",
1376 old_value);
1377 return;
1380 else if (!DECL_ARTIFICIAL (old_value))
1382 error ("%q+D invalid in %q#T", using_decl, t);
1383 error (" because of local member %q+#D with same name", old_value);
1384 return;
1387 /* Make type T see field decl FDECL with access ACCESS. */
1388 if (flist)
1389 for (ovl_iterator iter (flist); iter; ++iter)
1391 add_method (t, *iter, true);
1392 alter_access (t, *iter, access);
1394 else
1395 alter_access (t, decl, access);
1398 /* Data structure for find_abi_tags_r, below. */
1400 struct abi_tag_data
1402 tree t; // The type that we're checking for missing tags.
1403 tree subob; // The subobject of T that we're getting tags from.
1404 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1407 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1408 in the context of P. TAG can be either an identifier (the DECL_NAME of
1409 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1411 static void
1412 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1414 if (!IDENTIFIER_MARKED (id))
1416 if (p->tags != error_mark_node)
1418 /* We're collecting tags from template arguments or from
1419 the type of a variable or function return type. */
1420 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1422 /* Don't inherit this tag multiple times. */
1423 IDENTIFIER_MARKED (id) = true;
1425 if (TYPE_P (p->t))
1427 /* Tags inherited from type template arguments are only used
1428 to avoid warnings. */
1429 ABI_TAG_IMPLICIT (p->tags) = true;
1430 return;
1432 /* For functions and variables we want to warn, too. */
1435 /* Otherwise we're diagnosing missing tags. */
1436 if (TREE_CODE (p->t) == FUNCTION_DECL)
1438 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1439 "that %qT (used in its return type) has",
1440 p->t, tag, *tp))
1441 inform (location_of (*tp), "%qT declared here", *tp);
1443 else if (VAR_P (p->t))
1445 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1446 "that %qT (used in its type) has", p->t, tag, *tp))
1447 inform (location_of (*tp), "%qT declared here", *tp);
1449 else if (TYPE_P (p->subob))
1451 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1452 "that base %qT has", p->t, tag, p->subob))
1453 inform (location_of (p->subob), "%qT declared here",
1454 p->subob);
1456 else
1458 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1459 "that %qT (used in the type of %qD) has",
1460 p->t, tag, *tp, p->subob))
1462 inform (location_of (p->subob), "%qD declared here",
1463 p->subob);
1464 inform (location_of (*tp), "%qT declared here", *tp);
1470 /* Find all the ABI tags in the attribute list ATTR and either call
1471 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1473 static void
1474 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1476 if (!attr)
1477 return;
1478 for (; (attr = lookup_attribute ("abi_tag", attr));
1479 attr = TREE_CHAIN (attr))
1480 for (tree list = TREE_VALUE (attr); list;
1481 list = TREE_CHAIN (list))
1483 tree tag = TREE_VALUE (list);
1484 tree id = get_identifier (TREE_STRING_POINTER (tag));
1485 if (tp)
1486 check_tag (tag, id, tp, p);
1487 else
1488 IDENTIFIER_MARKED (id) = val;
1492 /* Find all the ABI tags on T and its enclosing scopes and either call
1493 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1495 static void
1496 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1498 while (t != global_namespace)
1500 tree attr;
1501 if (TYPE_P (t))
1503 attr = TYPE_ATTRIBUTES (t);
1504 t = CP_TYPE_CONTEXT (t);
1506 else
1508 attr = DECL_ATTRIBUTES (t);
1509 t = CP_DECL_CONTEXT (t);
1511 mark_or_check_attr_tags (attr, tp, p, val);
1515 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1516 types with ABI tags, add the corresponding identifiers to the VEC in
1517 *DATA and set IDENTIFIER_MARKED. */
1519 static tree
1520 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1522 if (!OVERLOAD_TYPE_P (*tp))
1523 return NULL_TREE;
1525 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1526 anyway, but let's make sure of it. */
1527 *walk_subtrees = false;
1529 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1531 mark_or_check_tags (*tp, tp, p, false);
1533 return NULL_TREE;
1536 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1537 IDENTIFIER_MARKED on its ABI tags. */
1539 static tree
1540 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1542 if (!OVERLOAD_TYPE_P (*tp))
1543 return NULL_TREE;
1545 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1546 anyway, but let's make sure of it. */
1547 *walk_subtrees = false;
1549 bool *valp = static_cast<bool*>(data);
1551 mark_or_check_tags (*tp, NULL, NULL, *valp);
1553 return NULL_TREE;
1556 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1557 scopes. */
1559 static void
1560 mark_abi_tags (tree t, bool val)
1562 mark_or_check_tags (t, NULL, NULL, val);
1563 if (DECL_P (t))
1565 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1566 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1568 /* Template arguments are part of the signature. */
1569 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1570 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1572 tree arg = TREE_VEC_ELT (level, j);
1573 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1576 if (TREE_CODE (t) == FUNCTION_DECL)
1577 /* A function's parameter types are part of the signature, so
1578 we don't need to inherit any tags that are also in them. */
1579 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1580 arg = TREE_CHAIN (arg))
1581 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1582 mark_abi_tags_r, &val);
1586 /* Check that T has all the ABI tags that subobject SUBOB has, or
1587 warn if not. If T is a (variable or function) declaration, also
1588 return any missing tags, and add them to T if JUST_CHECKING is false. */
1590 static tree
1591 check_abi_tags (tree t, tree subob, bool just_checking = false)
1593 bool inherit = DECL_P (t);
1595 if (!inherit && !warn_abi_tag)
1596 return NULL_TREE;
1598 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1599 if (!TREE_PUBLIC (decl))
1600 /* No need to worry about things local to this TU. */
1601 return NULL_TREE;
1603 mark_abi_tags (t, true);
1605 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1606 struct abi_tag_data data = { t, subob, error_mark_node };
1607 if (inherit)
1608 data.tags = NULL_TREE;
1610 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1612 if (!(inherit && data.tags))
1613 /* We don't need to do anything with data.tags. */;
1614 else if (just_checking)
1615 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1617 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1618 IDENTIFIER_MARKED (id) = false;
1620 else
1622 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1623 if (attr)
1624 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1625 else
1626 DECL_ATTRIBUTES (t)
1627 = tree_cons (get_identifier ("abi_tag"), data.tags,
1628 DECL_ATTRIBUTES (t));
1631 mark_abi_tags (t, false);
1633 return data.tags;
1636 /* Check that DECL has all the ABI tags that are used in parts of its type
1637 that are not reflected in its mangled name. */
1639 void
1640 check_abi_tags (tree decl)
1642 if (VAR_P (decl))
1643 check_abi_tags (decl, TREE_TYPE (decl));
1644 else if (TREE_CODE (decl) == FUNCTION_DECL
1645 && !DECL_CONV_FN_P (decl)
1646 && !mangle_return_type_p (decl))
1647 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1650 /* Return any ABI tags that are used in parts of the type of DECL
1651 that are not reflected in its mangled name. This function is only
1652 used in backward-compatible mangling for ABI <11. */
1654 tree
1655 missing_abi_tags (tree decl)
1657 if (VAR_P (decl))
1658 return check_abi_tags (decl, TREE_TYPE (decl), true);
1659 else if (TREE_CODE (decl) == FUNCTION_DECL
1660 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1661 that we can use this function for setting need_abi_warning
1662 regardless of the current flag_abi_version. */
1663 && !mangle_return_type_p (decl))
1664 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1665 else
1666 return NULL_TREE;
1669 void
1670 inherit_targ_abi_tags (tree t)
1672 if (!CLASS_TYPE_P (t)
1673 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1674 return;
1676 mark_abi_tags (t, true);
1678 tree args = CLASSTYPE_TI_ARGS (t);
1679 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1680 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1682 tree level = TMPL_ARGS_LEVEL (args, i+1);
1683 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1685 tree arg = TREE_VEC_ELT (level, j);
1686 data.subob = arg;
1687 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1691 // If we found some tags on our template arguments, add them to our
1692 // abi_tag attribute.
1693 if (data.tags)
1695 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1696 if (attr)
1697 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1698 else
1699 TYPE_ATTRIBUTES (t)
1700 = tree_cons (get_identifier ("abi_tag"), data.tags,
1701 TYPE_ATTRIBUTES (t));
1704 mark_abi_tags (t, false);
1707 /* Return true, iff class T has a non-virtual destructor that is
1708 accessible from outside the class heirarchy (i.e. is public, or
1709 there's a suitable friend. */
1711 static bool
1712 accessible_nvdtor_p (tree t)
1714 tree dtor = CLASSTYPE_DESTRUCTORS (t);
1716 /* An implicitly declared destructor is always public. And,
1717 if it were virtual, we would have created it by now. */
1718 if (!dtor)
1719 return true;
1721 if (DECL_VINDEX (dtor))
1722 return false; /* Virtual */
1724 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1725 return true; /* Public */
1727 if (CLASSTYPE_FRIEND_CLASSES (t)
1728 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1729 return true; /* Has friends */
1731 return false;
1734 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1735 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1736 properties of the bases. */
1738 static void
1739 check_bases (tree t,
1740 int* cant_have_const_ctor_p,
1741 int* no_const_asn_ref_p)
1743 int i;
1744 bool seen_non_virtual_nearly_empty_base_p = 0;
1745 int seen_tm_mask = 0;
1746 tree base_binfo;
1747 tree binfo;
1748 tree field = NULL_TREE;
1750 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1751 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1752 if (TREE_CODE (field) == FIELD_DECL)
1753 break;
1755 for (binfo = TYPE_BINFO (t), i = 0;
1756 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1758 tree basetype = TREE_TYPE (base_binfo);
1760 gcc_assert (COMPLETE_TYPE_P (basetype));
1762 if (CLASSTYPE_FINAL (basetype))
1763 error ("cannot derive from %<final%> base %qT in derived type %qT",
1764 basetype, t);
1766 /* If any base class is non-literal, so is the derived class. */
1767 if (!CLASSTYPE_LITERAL_P (basetype))
1768 CLASSTYPE_LITERAL_P (t) = false;
1770 /* If the base class doesn't have copy constructors or
1771 assignment operators that take const references, then the
1772 derived class cannot have such a member automatically
1773 generated. */
1774 if (TYPE_HAS_COPY_CTOR (basetype)
1775 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1776 *cant_have_const_ctor_p = 1;
1777 if (TYPE_HAS_COPY_ASSIGN (basetype)
1778 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1779 *no_const_asn_ref_p = 1;
1781 if (BINFO_VIRTUAL_P (base_binfo))
1782 /* A virtual base does not effect nearly emptiness. */
1784 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1786 if (seen_non_virtual_nearly_empty_base_p)
1787 /* And if there is more than one nearly empty base, then the
1788 derived class is not nearly empty either. */
1789 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1790 else
1791 /* Remember we've seen one. */
1792 seen_non_virtual_nearly_empty_base_p = 1;
1794 else if (!is_empty_class (basetype))
1795 /* If the base class is not empty or nearly empty, then this
1796 class cannot be nearly empty. */
1797 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1799 /* A lot of properties from the bases also apply to the derived
1800 class. */
1801 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1802 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1803 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1804 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1805 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1806 || !TYPE_HAS_COPY_ASSIGN (basetype));
1807 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1808 || !TYPE_HAS_COPY_CTOR (basetype));
1809 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1810 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1811 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1812 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1813 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1814 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1815 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1816 || TYPE_HAS_COMPLEX_DFLT (basetype));
1817 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1818 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1819 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1820 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1821 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1822 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1823 if (TYPE_HAS_MUTABLE_P (basetype))
1824 CLASSTYPE_HAS_MUTABLE (t) = 1;
1826 /* A standard-layout class is a class that:
1828 * has no non-standard-layout base classes, */
1829 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1830 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1832 tree basefield;
1833 /* ...has no base classes of the same type as the first non-static
1834 data member... */
1835 if (field && DECL_CONTEXT (field) == t
1836 && (same_type_ignoring_top_level_qualifiers_p
1837 (TREE_TYPE (field), basetype)))
1838 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1839 else
1840 /* ...either has no non-static data members in the most-derived
1841 class and at most one base class with non-static data
1842 members, or has no base classes with non-static data
1843 members */
1844 for (basefield = TYPE_FIELDS (basetype); basefield;
1845 basefield = DECL_CHAIN (basefield))
1846 if (TREE_CODE (basefield) == FIELD_DECL
1847 && !(DECL_FIELD_IS_BASE (basefield)
1848 && integer_zerop (DECL_SIZE (basefield))))
1850 if (field)
1851 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1852 else
1853 field = basefield;
1854 break;
1858 /* Don't bother collecting tm attributes if transactional memory
1859 support is not enabled. */
1860 if (flag_tm)
1862 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1863 if (tm_attr)
1864 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1867 check_abi_tags (t, basetype);
1870 /* If one of the base classes had TM attributes, and the current class
1871 doesn't define its own, then the current class inherits one. */
1872 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1874 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1875 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1879 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1880 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1881 that have had a nearly-empty virtual primary base stolen by some
1882 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1883 T. */
1885 static void
1886 determine_primary_bases (tree t)
1888 unsigned i;
1889 tree primary = NULL_TREE;
1890 tree type_binfo = TYPE_BINFO (t);
1891 tree base_binfo;
1893 /* Determine the primary bases of our bases. */
1894 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1895 base_binfo = TREE_CHAIN (base_binfo))
1897 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1899 /* See if we're the non-virtual primary of our inheritance
1900 chain. */
1901 if (!BINFO_VIRTUAL_P (base_binfo))
1903 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1904 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1906 if (parent_primary
1907 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1908 BINFO_TYPE (parent_primary)))
1909 /* We are the primary binfo. */
1910 BINFO_PRIMARY_P (base_binfo) = 1;
1912 /* Determine if we have a virtual primary base, and mark it so.
1914 if (primary && BINFO_VIRTUAL_P (primary))
1916 tree this_primary = copied_binfo (primary, base_binfo);
1918 if (BINFO_PRIMARY_P (this_primary))
1919 /* Someone already claimed this base. */
1920 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1921 else
1923 tree delta;
1925 BINFO_PRIMARY_P (this_primary) = 1;
1926 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1928 /* A virtual binfo might have been copied from within
1929 another hierarchy. As we're about to use it as a
1930 primary base, make sure the offsets match. */
1931 delta = size_diffop_loc (input_location,
1932 fold_convert (ssizetype,
1933 BINFO_OFFSET (base_binfo)),
1934 fold_convert (ssizetype,
1935 BINFO_OFFSET (this_primary)));
1937 propagate_binfo_offsets (this_primary, delta);
1942 /* First look for a dynamic direct non-virtual base. */
1943 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1945 tree basetype = BINFO_TYPE (base_binfo);
1947 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1949 primary = base_binfo;
1950 goto found;
1954 /* A "nearly-empty" virtual base class can be the primary base
1955 class, if no non-virtual polymorphic base can be found. Look for
1956 a nearly-empty virtual dynamic base that is not already a primary
1957 base of something in the hierarchy. If there is no such base,
1958 just pick the first nearly-empty virtual base. */
1960 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1961 base_binfo = TREE_CHAIN (base_binfo))
1962 if (BINFO_VIRTUAL_P (base_binfo)
1963 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1965 if (!BINFO_PRIMARY_P (base_binfo))
1967 /* Found one that is not primary. */
1968 primary = base_binfo;
1969 goto found;
1971 else if (!primary)
1972 /* Remember the first candidate. */
1973 primary = base_binfo;
1976 found:
1977 /* If we've got a primary base, use it. */
1978 if (primary)
1980 tree basetype = BINFO_TYPE (primary);
1982 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1983 if (BINFO_PRIMARY_P (primary))
1984 /* We are stealing a primary base. */
1985 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1986 BINFO_PRIMARY_P (primary) = 1;
1987 if (BINFO_VIRTUAL_P (primary))
1989 tree delta;
1991 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1992 /* A virtual binfo might have been copied from within
1993 another hierarchy. As we're about to use it as a primary
1994 base, make sure the offsets match. */
1995 delta = size_diffop_loc (input_location, ssize_int (0),
1996 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1998 propagate_binfo_offsets (primary, delta);
2001 primary = TYPE_BINFO (basetype);
2003 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
2004 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
2005 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
2009 /* Update the variant types of T. */
2011 void
2012 fixup_type_variants (tree t)
2014 tree variants;
2016 if (!t)
2017 return;
2019 for (variants = TYPE_NEXT_VARIANT (t);
2020 variants;
2021 variants = TYPE_NEXT_VARIANT (variants))
2023 /* These fields are in the _TYPE part of the node, not in
2024 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2025 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
2026 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2027 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2028 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2030 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2032 TYPE_BINFO (variants) = TYPE_BINFO (t);
2034 /* Copy whatever these are holding today. */
2035 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
2036 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2040 /* KLASS is a class that we're applying may_alias to after the body is
2041 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
2042 canonical type(s) will be implicitly updated. */
2044 static void
2045 fixup_may_alias (tree klass)
2047 tree t, v;
2049 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
2050 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2051 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2052 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
2053 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
2054 TYPE_REF_CAN_ALIAS_ALL (v) = true;
2057 /* Early variant fixups: we apply attributes at the beginning of the class
2058 definition, and we need to fix up any variants that have already been
2059 made via elaborated-type-specifier so that check_qualified_type works. */
2061 void
2062 fixup_attribute_variants (tree t)
2064 tree variants;
2066 if (!t)
2067 return;
2069 tree attrs = TYPE_ATTRIBUTES (t);
2070 unsigned align = TYPE_ALIGN (t);
2071 bool user_align = TYPE_USER_ALIGN (t);
2072 bool may_alias = lookup_attribute ("may_alias", attrs);
2074 if (may_alias)
2075 fixup_may_alias (t);
2077 for (variants = TYPE_NEXT_VARIANT (t);
2078 variants;
2079 variants = TYPE_NEXT_VARIANT (variants))
2081 /* These are the two fields that check_qualified_type looks at and
2082 are affected by attributes. */
2083 TYPE_ATTRIBUTES (variants) = attrs;
2084 unsigned valign = align;
2085 if (TYPE_USER_ALIGN (variants))
2086 valign = MAX (valign, TYPE_ALIGN (variants));
2087 else
2088 TYPE_USER_ALIGN (variants) = user_align;
2089 SET_TYPE_ALIGN (variants, valign);
2090 if (may_alias)
2091 fixup_may_alias (variants);
2095 /* Set memoizing fields and bits of T (and its variants) for later
2096 use. */
2098 static void
2099 finish_struct_bits (tree t)
2101 /* Fix up variants (if any). */
2102 fixup_type_variants (t);
2104 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2105 /* For a class w/o baseclasses, 'finish_struct' has set
2106 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2107 Similarly for a class whose base classes do not have vtables.
2108 When neither of these is true, we might have removed abstract
2109 virtuals (by providing a definition), added some (by declaring
2110 new ones), or redeclared ones from a base class. We need to
2111 recalculate what's really an abstract virtual at this point (by
2112 looking in the vtables). */
2113 get_pure_virtuals (t);
2115 /* If this type has a copy constructor or a destructor, force its
2116 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2117 nonzero. This will cause it to be passed by invisible reference
2118 and prevent it from being returned in a register. */
2119 if (type_has_nontrivial_copy_init (t)
2120 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2122 tree variants;
2123 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2124 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2126 SET_TYPE_MODE (variants, BLKmode);
2127 TREE_ADDRESSABLE (variants) = 1;
2132 /* Issue warnings about T having private constructors, but no friends,
2133 and so forth.
2135 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2136 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2137 non-private static member functions. */
2139 static void
2140 maybe_warn_about_overly_private_class (tree t)
2142 int has_member_fn = 0;
2143 int has_nonprivate_method = 0;
2144 tree fn;
2146 if (!warn_ctor_dtor_privacy
2147 /* If the class has friends, those entities might create and
2148 access instances, so we should not warn. */
2149 || (CLASSTYPE_FRIEND_CLASSES (t)
2150 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2151 /* We will have warned when the template was declared; there's
2152 no need to warn on every instantiation. */
2153 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2154 /* There's no reason to even consider warning about this
2155 class. */
2156 return;
2158 /* We only issue one warning, if more than one applies, because
2159 otherwise, on code like:
2161 class A {
2162 // Oops - forgot `public:'
2163 A();
2164 A(const A&);
2165 ~A();
2168 we warn several times about essentially the same problem. */
2170 /* Check to see if all (non-constructor, non-destructor) member
2171 functions are private. (Since there are no friends or
2172 non-private statics, we can't ever call any of the private member
2173 functions.) */
2174 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2175 /* We're not interested in compiler-generated methods; they don't
2176 provide any way to call private members. */
2177 if (!DECL_ARTIFICIAL (fn))
2179 if (!TREE_PRIVATE (fn))
2181 if (DECL_STATIC_FUNCTION_P (fn))
2182 /* A non-private static member function is just like a
2183 friend; it can create and invoke private member
2184 functions, and be accessed without a class
2185 instance. */
2186 return;
2188 has_nonprivate_method = 1;
2189 /* Keep searching for a static member function. */
2191 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2192 has_member_fn = 1;
2195 if (!has_nonprivate_method && has_member_fn)
2197 /* There are no non-private methods, and there's at least one
2198 private member function that isn't a constructor or
2199 destructor. (If all the private members are
2200 constructors/destructors we want to use the code below that
2201 issues error messages specifically referring to
2202 constructors/destructors.) */
2203 unsigned i;
2204 tree binfo = TYPE_BINFO (t);
2206 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2207 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2209 has_nonprivate_method = 1;
2210 break;
2212 if (!has_nonprivate_method)
2214 warning (OPT_Wctor_dtor_privacy,
2215 "all member functions in class %qT are private", t);
2216 return;
2220 /* Even if some of the member functions are non-private, the class
2221 won't be useful for much if all the constructors or destructors
2222 are private: such an object can never be created or destroyed. */
2223 fn = CLASSTYPE_DESTRUCTORS (t);
2224 if (fn && TREE_PRIVATE (fn))
2226 warning (OPT_Wctor_dtor_privacy,
2227 "%q#T only defines a private destructor and has no friends",
2229 return;
2232 /* Warn about classes that have private constructors and no friends. */
2233 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2234 /* Implicitly generated constructors are always public. */
2235 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
2236 || !CLASSTYPE_LAZY_COPY_CTOR (t)))
2238 bool nonprivate_ctor = false;
2240 /* If a non-template class does not define a copy
2241 constructor, one is defined for it, enabling it to avoid
2242 this warning. For a template class, this does not
2243 happen, and so we would normally get a warning on:
2245 template <class T> class C { private: C(); };
2247 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2248 complete non-template or fully instantiated classes have this
2249 flag set. */
2250 if (!TYPE_HAS_COPY_CTOR (t))
2251 nonprivate_ctor = true;
2252 else
2253 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2254 !nonprivate_ctor && iter; ++iter)
2255 /* Ideally, we wouldn't count copy constructors (or, in
2256 fact, any constructor that takes an argument of the class
2257 type as a parameter) because such things cannot be used
2258 to construct an instance of the class unless you already
2259 have one. But, for now at least, we're more
2260 generous. */
2261 if (! TREE_PRIVATE (*iter))
2262 nonprivate_ctor = true;
2264 if (!nonprivate_ctor)
2266 warning (OPT_Wctor_dtor_privacy,
2267 "%q#T only defines private constructors and has no friends",
2269 return;
2274 static struct {
2275 gt_pointer_operator new_value;
2276 void *cookie;
2277 } resort_data;
2279 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2281 static int
2282 method_name_cmp (const void* m1_p, const void* m2_p)
2284 const tree *const m1 = (const tree *) m1_p;
2285 const tree *const m2 = (const tree *) m2_p;
2287 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2288 return 0;
2289 if (*m1 == NULL_TREE)
2290 return -1;
2291 if (*m2 == NULL_TREE)
2292 return 1;
2293 if (OVL_NAME (*m1) < OVL_NAME (*m2))
2294 return -1;
2295 return 1;
2298 /* This routine compares two fields like method_name_cmp but using the
2299 pointer operator in resort_field_decl_data. */
2301 static int
2302 resort_method_name_cmp (const void* m1_p, const void* m2_p)
2304 const tree *const m1 = (const tree *) m1_p;
2305 const tree *const m2 = (const tree *) m2_p;
2306 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2307 return 0;
2308 if (*m1 == NULL_TREE)
2309 return -1;
2310 if (*m2 == NULL_TREE)
2311 return 1;
2313 tree d1 = OVL_NAME (*m1);
2314 tree d2 = OVL_NAME (*m2);
2315 resort_data.new_value (&d1, resort_data.cookie);
2316 resort_data.new_value (&d2, resort_data.cookie);
2317 if (d1 < d2)
2318 return -1;
2320 return 1;
2323 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
2325 void
2326 resort_type_method_vec (void* obj,
2327 void* /*orig_obj*/,
2328 gt_pointer_operator new_value,
2329 void* cookie)
2331 if (vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj)
2333 int len = method_vec->length ();
2334 int slot;
2336 /* The type conversion ops have to live at the front of the vec, so we
2337 can't sort them. */
2338 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2339 slot < len; slot++)
2340 if (!DECL_CONV_FN_P (OVL_FIRST ((*method_vec)[slot])))
2341 break;
2343 if (len > slot + 1)
2345 resort_data.new_value = new_value;
2346 resort_data.cookie = cookie;
2347 qsort (method_vec->address () + slot, len - slot, sizeof (tree),
2348 resort_method_name_cmp);
2353 /* Warn about duplicate methods in fn_fields.
2355 Sort methods that are not special (i.e., constructors, destructors,
2356 and type conversion operators) so that we can find them faster in
2357 search. */
2359 static void
2360 finish_struct_methods (tree t)
2362 tree fn_fields;
2363 vec<tree, va_gc> *method_vec;
2364 int slot, len;
2366 method_vec = CLASSTYPE_METHOD_VEC (t);
2367 if (!method_vec)
2368 return;
2370 len = method_vec->length ();
2372 /* Clear DECL_IN_AGGR_P for all functions. */
2373 for (fn_fields = TYPE_METHODS (t); fn_fields;
2374 fn_fields = DECL_CHAIN (fn_fields))
2375 DECL_IN_AGGR_P (fn_fields) = 0;
2377 /* Issue warnings about private constructors and such. If there are
2378 no methods, then some public defaults are generated. */
2379 maybe_warn_about_overly_private_class (t);
2381 /* The type conversion ops have to live at the front of the vec, so we
2382 can't sort them. */
2383 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
2384 method_vec->iterate (slot, &fn_fields);
2385 ++slot)
2386 if (!DECL_CONV_FN_P (OVL_FIRST (fn_fields)))
2387 break;
2388 if (len - slot > 1)
2389 qsort (method_vec->address () + slot,
2390 len-slot, sizeof (tree), method_name_cmp);
2393 /* Make BINFO's vtable have N entries, including RTTI entries,
2394 vbase and vcall offsets, etc. Set its type and call the back end
2395 to lay it out. */
2397 static void
2398 layout_vtable_decl (tree binfo, int n)
2400 tree atype;
2401 tree vtable;
2403 atype = build_array_of_n_type (vtable_entry_type, n);
2404 layout_type (atype);
2406 /* We may have to grow the vtable. */
2407 vtable = get_vtbl_decl_for_binfo (binfo);
2408 if (!same_type_p (TREE_TYPE (vtable), atype))
2410 TREE_TYPE (vtable) = atype;
2411 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2412 layout_decl (vtable, 0);
2416 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2417 have the same signature. */
2420 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2422 /* One destructor overrides another if they are the same kind of
2423 destructor. */
2424 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2425 && special_function_p (base_fndecl) == special_function_p (fndecl))
2426 return 1;
2427 /* But a non-destructor never overrides a destructor, nor vice
2428 versa, nor do different kinds of destructors override
2429 one-another. For example, a complete object destructor does not
2430 override a deleting destructor. */
2431 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2432 return 0;
2434 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2435 || (DECL_CONV_FN_P (fndecl)
2436 && DECL_CONV_FN_P (base_fndecl)
2437 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2438 DECL_CONV_FN_TYPE (base_fndecl))))
2440 tree fntype = TREE_TYPE (fndecl);
2441 tree base_fntype = TREE_TYPE (base_fndecl);
2442 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2443 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2444 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2445 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2446 return 1;
2448 return 0;
2451 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2452 subobject. */
2454 static bool
2455 base_derived_from (tree derived, tree base)
2457 tree probe;
2459 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2461 if (probe == derived)
2462 return true;
2463 else if (BINFO_VIRTUAL_P (probe))
2464 /* If we meet a virtual base, we can't follow the inheritance
2465 any more. See if the complete type of DERIVED contains
2466 such a virtual base. */
2467 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2468 != NULL_TREE);
2470 return false;
2473 struct find_final_overrider_data {
2474 /* The function for which we are trying to find a final overrider. */
2475 tree fn;
2476 /* The base class in which the function was declared. */
2477 tree declaring_base;
2478 /* The candidate overriders. */
2479 tree candidates;
2480 /* Path to most derived. */
2481 vec<tree> path;
2484 /* Add the overrider along the current path to FFOD->CANDIDATES.
2485 Returns true if an overrider was found; false otherwise. */
2487 static bool
2488 dfs_find_final_overrider_1 (tree binfo,
2489 find_final_overrider_data *ffod,
2490 unsigned depth)
2492 tree method;
2494 /* If BINFO is not the most derived type, try a more derived class.
2495 A definition there will overrider a definition here. */
2496 if (depth)
2498 depth--;
2499 if (dfs_find_final_overrider_1
2500 (ffod->path[depth], ffod, depth))
2501 return true;
2504 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2505 if (method)
2507 tree *candidate = &ffod->candidates;
2509 /* Remove any candidates overridden by this new function. */
2510 while (*candidate)
2512 /* If *CANDIDATE overrides METHOD, then METHOD
2513 cannot override anything else on the list. */
2514 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2515 return true;
2516 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2517 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2518 *candidate = TREE_CHAIN (*candidate);
2519 else
2520 candidate = &TREE_CHAIN (*candidate);
2523 /* Add the new function. */
2524 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2525 return true;
2528 return false;
2531 /* Called from find_final_overrider via dfs_walk. */
2533 static tree
2534 dfs_find_final_overrider_pre (tree binfo, void *data)
2536 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2538 if (binfo == ffod->declaring_base)
2539 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2540 ffod->path.safe_push (binfo);
2542 return NULL_TREE;
2545 static tree
2546 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2548 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2549 ffod->path.pop ();
2551 return NULL_TREE;
2554 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2555 FN and whose TREE_VALUE is the binfo for the base where the
2556 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2557 DERIVED) is the base object in which FN is declared. */
2559 static tree
2560 find_final_overrider (tree derived, tree binfo, tree fn)
2562 find_final_overrider_data ffod;
2564 /* Getting this right is a little tricky. This is valid:
2566 struct S { virtual void f (); };
2567 struct T { virtual void f (); };
2568 struct U : public S, public T { };
2570 even though calling `f' in `U' is ambiguous. But,
2572 struct R { virtual void f(); };
2573 struct S : virtual public R { virtual void f (); };
2574 struct T : virtual public R { virtual void f (); };
2575 struct U : public S, public T { };
2577 is not -- there's no way to decide whether to put `S::f' or
2578 `T::f' in the vtable for `R'.
2580 The solution is to look at all paths to BINFO. If we find
2581 different overriders along any two, then there is a problem. */
2582 if (DECL_THUNK_P (fn))
2583 fn = THUNK_TARGET (fn);
2585 /* Determine the depth of the hierarchy. */
2586 ffod.fn = fn;
2587 ffod.declaring_base = binfo;
2588 ffod.candidates = NULL_TREE;
2589 ffod.path.create (30);
2591 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2592 dfs_find_final_overrider_post, &ffod);
2594 ffod.path.release ();
2596 /* If there was no winner, issue an error message. */
2597 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2598 return error_mark_node;
2600 return ffod.candidates;
2603 /* Return the index of the vcall offset for FN when TYPE is used as a
2604 virtual base. */
2606 static tree
2607 get_vcall_index (tree fn, tree type)
2609 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2610 tree_pair_p p;
2611 unsigned ix;
2613 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2614 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2615 || same_signature_p (fn, p->purpose))
2616 return p->value;
2618 /* There should always be an appropriate index. */
2619 gcc_unreachable ();
2622 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2623 dominated by T. FN is the old function; VIRTUALS points to the
2624 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2625 of that entry in the list. */
2627 static void
2628 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2629 unsigned ix)
2631 tree b;
2632 tree overrider;
2633 tree delta;
2634 tree virtual_base;
2635 tree first_defn;
2636 tree overrider_fn, overrider_target;
2637 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2638 tree over_return, base_return;
2639 bool lost = false;
2641 /* Find the nearest primary base (possibly binfo itself) which defines
2642 this function; this is the class the caller will convert to when
2643 calling FN through BINFO. */
2644 for (b = binfo; ; b = get_primary_binfo (b))
2646 gcc_assert (b);
2647 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2648 break;
2650 /* The nearest definition is from a lost primary. */
2651 if (BINFO_LOST_PRIMARY_P (b))
2652 lost = true;
2654 first_defn = b;
2656 /* Find the final overrider. */
2657 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2658 if (overrider == error_mark_node)
2660 error ("no unique final overrider for %qD in %qT", target_fn, t);
2661 return;
2663 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2665 /* Check for adjusting covariant return types. */
2666 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2667 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2669 if (POINTER_TYPE_P (over_return)
2670 && TREE_CODE (over_return) == TREE_CODE (base_return)
2671 && CLASS_TYPE_P (TREE_TYPE (over_return))
2672 && CLASS_TYPE_P (TREE_TYPE (base_return))
2673 /* If the overrider is invalid, don't even try. */
2674 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2676 /* If FN is a covariant thunk, we must figure out the adjustment
2677 to the final base FN was converting to. As OVERRIDER_TARGET might
2678 also be converting to the return type of FN, we have to
2679 combine the two conversions here. */
2680 tree fixed_offset, virtual_offset;
2682 over_return = TREE_TYPE (over_return);
2683 base_return = TREE_TYPE (base_return);
2685 if (DECL_THUNK_P (fn))
2687 gcc_assert (DECL_RESULT_THUNK_P (fn));
2688 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2689 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2691 else
2692 fixed_offset = virtual_offset = NULL_TREE;
2694 if (virtual_offset)
2695 /* Find the equivalent binfo within the return type of the
2696 overriding function. We will want the vbase offset from
2697 there. */
2698 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2699 over_return);
2700 else if (!same_type_ignoring_top_level_qualifiers_p
2701 (over_return, base_return))
2703 /* There was no existing virtual thunk (which takes
2704 precedence). So find the binfo of the base function's
2705 return type within the overriding function's return type.
2706 Fortunately we know the covariancy is valid (it
2707 has already been checked), so we can just iterate along
2708 the binfos, which have been chained in inheritance graph
2709 order. Of course it is lame that we have to repeat the
2710 search here anyway -- we should really be caching pieces
2711 of the vtable and avoiding this repeated work. */
2712 tree thunk_binfo, base_binfo;
2714 /* Find the base binfo within the overriding function's
2715 return type. We will always find a thunk_binfo, except
2716 when the covariancy is invalid (which we will have
2717 already diagnosed). */
2718 for (base_binfo = TYPE_BINFO (base_return),
2719 thunk_binfo = TYPE_BINFO (over_return);
2720 thunk_binfo;
2721 thunk_binfo = TREE_CHAIN (thunk_binfo))
2722 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2723 BINFO_TYPE (base_binfo)))
2724 break;
2726 /* See if virtual inheritance is involved. */
2727 for (virtual_offset = thunk_binfo;
2728 virtual_offset;
2729 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2730 if (BINFO_VIRTUAL_P (virtual_offset))
2731 break;
2733 if (virtual_offset
2734 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2736 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2738 if (virtual_offset)
2740 /* We convert via virtual base. Adjust the fixed
2741 offset to be from there. */
2742 offset =
2743 size_diffop (offset,
2744 fold_convert (ssizetype,
2745 BINFO_OFFSET (virtual_offset)));
2747 if (fixed_offset)
2748 /* There was an existing fixed offset, this must be
2749 from the base just converted to, and the base the
2750 FN was thunking to. */
2751 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2752 else
2753 fixed_offset = offset;
2757 if (fixed_offset || virtual_offset)
2758 /* Replace the overriding function with a covariant thunk. We
2759 will emit the overriding function in its own slot as
2760 well. */
2761 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2762 fixed_offset, virtual_offset);
2764 else
2765 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2766 !DECL_THUNK_P (fn));
2768 /* If we need a covariant thunk, then we may need to adjust first_defn.
2769 The ABI specifies that the thunks emitted with a function are
2770 determined by which bases the function overrides, so we need to be
2771 sure that we're using a thunk for some overridden base; even if we
2772 know that the necessary this adjustment is zero, there may not be an
2773 appropriate zero-this-adjustment thunk for us to use since thunks for
2774 overriding virtual bases always use the vcall offset.
2776 Furthermore, just choosing any base that overrides this function isn't
2777 quite right, as this slot won't be used for calls through a type that
2778 puts a covariant thunk here. Calling the function through such a type
2779 will use a different slot, and that slot is the one that determines
2780 the thunk emitted for that base.
2782 So, keep looking until we find the base that we're really overriding
2783 in this slot: the nearest primary base that doesn't use a covariant
2784 thunk in this slot. */
2785 if (overrider_target != overrider_fn)
2787 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2788 /* We already know that the overrider needs a covariant thunk. */
2789 b = get_primary_binfo (b);
2790 for (; ; b = get_primary_binfo (b))
2792 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2793 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2794 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2795 break;
2796 if (BINFO_LOST_PRIMARY_P (b))
2797 lost = true;
2799 first_defn = b;
2802 /* Assume that we will produce a thunk that convert all the way to
2803 the final overrider, and not to an intermediate virtual base. */
2804 virtual_base = NULL_TREE;
2806 /* See if we can convert to an intermediate virtual base first, and then
2807 use the vcall offset located there to finish the conversion. */
2808 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2810 /* If we find the final overrider, then we can stop
2811 walking. */
2812 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2813 BINFO_TYPE (TREE_VALUE (overrider))))
2814 break;
2816 /* If we find a virtual base, and we haven't yet found the
2817 overrider, then there is a virtual base between the
2818 declaring base (first_defn) and the final overrider. */
2819 if (BINFO_VIRTUAL_P (b))
2821 virtual_base = b;
2822 break;
2826 /* Compute the constant adjustment to the `this' pointer. The
2827 `this' pointer, when this function is called, will point at BINFO
2828 (or one of its primary bases, which are at the same offset). */
2829 if (virtual_base)
2830 /* The `this' pointer needs to be adjusted from the declaration to
2831 the nearest virtual base. */
2832 delta = size_diffop_loc (input_location,
2833 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2834 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2835 else if (lost)
2836 /* If the nearest definition is in a lost primary, we don't need an
2837 entry in our vtable. Except possibly in a constructor vtable,
2838 if we happen to get our primary back. In that case, the offset
2839 will be zero, as it will be a primary base. */
2840 delta = size_zero_node;
2841 else
2842 /* The `this' pointer needs to be adjusted from pointing to
2843 BINFO to pointing at the base where the final overrider
2844 appears. */
2845 delta = size_diffop_loc (input_location,
2846 fold_convert (ssizetype,
2847 BINFO_OFFSET (TREE_VALUE (overrider))),
2848 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2850 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2852 if (virtual_base)
2853 BV_VCALL_INDEX (*virtuals)
2854 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2855 else
2856 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2858 BV_LOST_PRIMARY (*virtuals) = lost;
2861 /* Called from modify_all_vtables via dfs_walk. */
2863 static tree
2864 dfs_modify_vtables (tree binfo, void* data)
2866 tree t = (tree) data;
2867 tree virtuals;
2868 tree old_virtuals;
2869 unsigned ix;
2871 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2872 /* A base without a vtable needs no modification, and its bases
2873 are uninteresting. */
2874 return dfs_skip_bases;
2876 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2877 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2878 /* Don't do the primary vtable, if it's new. */
2879 return NULL_TREE;
2881 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2882 /* There's no need to modify the vtable for a non-virtual primary
2883 base; we're not going to use that vtable anyhow. We do still
2884 need to do this for virtual primary bases, as they could become
2885 non-primary in a construction vtable. */
2886 return NULL_TREE;
2888 make_new_vtable (t, binfo);
2890 /* Now, go through each of the virtual functions in the virtual
2891 function table for BINFO. Find the final overrider, and update
2892 the BINFO_VIRTUALS list appropriately. */
2893 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2894 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2895 virtuals;
2896 ix++, virtuals = TREE_CHAIN (virtuals),
2897 old_virtuals = TREE_CHAIN (old_virtuals))
2898 update_vtable_entry_for_fn (t,
2899 binfo,
2900 BV_FN (old_virtuals),
2901 &virtuals, ix);
2903 return NULL_TREE;
2906 /* Update all of the primary and secondary vtables for T. Create new
2907 vtables as required, and initialize their RTTI information. Each
2908 of the functions in VIRTUALS is declared in T and may override a
2909 virtual function from a base class; find and modify the appropriate
2910 entries to point to the overriding functions. Returns a list, in
2911 declaration order, of the virtual functions that are declared in T,
2912 but do not appear in the primary base class vtable, and which
2913 should therefore be appended to the end of the vtable for T. */
2915 static tree
2916 modify_all_vtables (tree t, tree virtuals)
2918 tree binfo = TYPE_BINFO (t);
2919 tree *fnsp;
2921 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2922 if (TYPE_CONTAINS_VPTR_P (t))
2923 get_vtable_decl (t, false);
2925 /* Update all of the vtables. */
2926 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2928 /* Add virtual functions not already in our primary vtable. These
2929 will be both those introduced by this class, and those overridden
2930 from secondary bases. It does not include virtuals merely
2931 inherited from secondary bases. */
2932 for (fnsp = &virtuals; *fnsp; )
2934 tree fn = TREE_VALUE (*fnsp);
2936 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2937 || DECL_VINDEX (fn) == error_mark_node)
2939 /* We don't need to adjust the `this' pointer when
2940 calling this function. */
2941 BV_DELTA (*fnsp) = integer_zero_node;
2942 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2944 /* This is a function not already in our vtable. Keep it. */
2945 fnsp = &TREE_CHAIN (*fnsp);
2947 else
2948 /* We've already got an entry for this function. Skip it. */
2949 *fnsp = TREE_CHAIN (*fnsp);
2952 return virtuals;
2955 /* Get the base virtual function declarations in T that have the
2956 indicated NAME. */
2958 static void
2959 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2961 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2962 int i;
2964 /* Find virtual functions in T with the indicated NAME. */
2965 i = lookup_fnfields_1 (t, name);
2966 bool found_decls = false;
2967 if (i != -1)
2968 for (ovl_iterator iter ((*CLASSTYPE_METHOD_VEC (t))[i]); iter; ++iter)
2970 tree method = *iter;
2972 if (TREE_CODE (method) == FUNCTION_DECL
2973 && DECL_VINDEX (method))
2975 base_fndecls->safe_push (method);
2976 found_decls = true;
2980 if (found_decls)
2981 return;
2983 for (i = 0; i < n_baseclasses; i++)
2985 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2986 get_basefndecls (name, basetype, base_fndecls);
2990 /* If this declaration supersedes the declaration of
2991 a method declared virtual in the base class, then
2992 mark this field as being virtual as well. */
2994 void
2995 check_for_override (tree decl, tree ctype)
2997 bool overrides_found = false;
2998 if (TREE_CODE (decl) == TEMPLATE_DECL)
2999 /* In [temp.mem] we have:
3001 A specialization of a member function template does not
3002 override a virtual function from a base class. */
3003 return;
3004 if ((DECL_DESTRUCTOR_P (decl)
3005 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
3006 || DECL_CONV_FN_P (decl))
3007 && look_for_overrides (ctype, decl)
3008 && !DECL_STATIC_FUNCTION_P (decl))
3009 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
3010 the error_mark_node so that we know it is an overriding
3011 function. */
3013 DECL_VINDEX (decl) = decl;
3014 overrides_found = true;
3015 if (warn_override && !DECL_OVERRIDE_P (decl)
3016 && !DECL_DESTRUCTOR_P (decl))
3017 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
3018 "%qD can be marked override", decl);
3021 if (DECL_VIRTUAL_P (decl))
3023 if (!DECL_VINDEX (decl))
3024 DECL_VINDEX (decl) = error_mark_node;
3025 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3026 if (DECL_DESTRUCTOR_P (decl))
3027 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
3029 else if (DECL_FINAL_P (decl))
3030 error ("%q+#D marked %<final%>, but is not virtual", decl);
3031 if (DECL_OVERRIDE_P (decl) && !overrides_found)
3032 error ("%q+#D marked %<override%>, but does not override", decl);
3035 /* Warn about hidden virtual functions that are not overridden in t.
3036 We know that constructors and destructors don't apply. */
3038 static void
3039 warn_hidden (tree t)
3041 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t);
3042 tree fns;
3043 size_t i;
3045 /* We go through each separately named virtual function. */
3046 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
3047 vec_safe_iterate (method_vec, i, &fns);
3048 ++i)
3050 tree fndecl;
3051 tree base_binfo;
3052 tree binfo;
3053 int j;
3055 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
3056 have the same name. Figure out what name that is. */
3057 tree name = OVL_NAME (fns);
3058 /* There are no possibly hidden functions yet. */
3059 auto_vec<tree, 20> base_fndecls;
3060 /* Iterate through all of the base classes looking for possibly
3061 hidden functions. */
3062 for (binfo = TYPE_BINFO (t), j = 0;
3063 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
3065 tree basetype = BINFO_TYPE (base_binfo);
3066 get_basefndecls (name, basetype, &base_fndecls);
3069 /* If there are no functions to hide, continue. */
3070 if (base_fndecls.is_empty ())
3071 continue;
3073 /* Remove any overridden functions. */
3074 for (ovl_iterator iter (fns); iter; ++iter)
3076 fndecl = *iter;
3077 if (TREE_CODE (fndecl) == FUNCTION_DECL
3078 && DECL_VINDEX (fndecl))
3080 /* If the method from the base class has the same
3081 signature as the method from the derived class, it
3082 has been overridden. */
3083 for (size_t k = 0; k < base_fndecls.length (); k++)
3084 if (base_fndecls[k]
3085 && same_signature_p (fndecl, base_fndecls[k]))
3086 base_fndecls[k] = NULL_TREE;
3090 /* Now give a warning for all base functions without overriders,
3091 as they are hidden. */
3092 size_t k;
3093 tree base_fndecl;
3094 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl)
3095 if (base_fndecl)
3097 /* Here we know it is a hider, and no overrider exists. */
3098 warning_at (location_of (base_fndecl),
3099 OPT_Woverloaded_virtual,
3100 "%qD was hidden", base_fndecl);
3101 warning_at (location_of (fns),
3102 OPT_Woverloaded_virtual, " by %qD", fns);
3107 /* Recursive helper for finish_struct_anon. */
3109 static void
3110 finish_struct_anon_r (tree field, bool complain)
3112 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
3113 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3114 for (; elt; elt = DECL_CHAIN (elt))
3116 /* We're generally only interested in entities the user
3117 declared, but we also find nested classes by noticing
3118 the TYPE_DECL that we create implicitly. You're
3119 allowed to put one anonymous union inside another,
3120 though, so we explicitly tolerate that. We use
3121 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
3122 we also allow unnamed types used for defining fields. */
3123 if (DECL_ARTIFICIAL (elt)
3124 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
3125 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
3126 continue;
3128 if (TREE_CODE (elt) != FIELD_DECL)
3130 /* We already complained about static data members in
3131 finish_static_data_member_decl. */
3132 if (complain && !VAR_P (elt))
3134 if (is_union)
3135 permerror (DECL_SOURCE_LOCATION (elt),
3136 "%q#D invalid; an anonymous union can "
3137 "only have non-static data members", elt);
3138 else
3139 permerror (DECL_SOURCE_LOCATION (elt),
3140 "%q#D invalid; an anonymous struct can "
3141 "only have non-static data members", elt);
3143 continue;
3146 if (complain)
3148 if (TREE_PRIVATE (elt))
3150 if (is_union)
3151 permerror (DECL_SOURCE_LOCATION (elt),
3152 "private member %q#D in anonymous union", elt);
3153 else
3154 permerror (DECL_SOURCE_LOCATION (elt),
3155 "private member %q#D in anonymous struct", elt);
3157 else if (TREE_PROTECTED (elt))
3159 if (is_union)
3160 permerror (DECL_SOURCE_LOCATION (elt),
3161 "protected member %q#D in anonymous union", elt);
3162 else
3163 permerror (DECL_SOURCE_LOCATION (elt),
3164 "protected member %q#D in anonymous struct", elt);
3168 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3169 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3171 /* Recurse into the anonymous aggregates to handle correctly
3172 access control (c++/24926):
3174 class A {
3175 union {
3176 union {
3177 int i;
3182 int j=A().i; */
3183 if (DECL_NAME (elt) == NULL_TREE
3184 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
3185 finish_struct_anon_r (elt, /*complain=*/false);
3189 /* Check for things that are invalid. There are probably plenty of other
3190 things we should check for also. */
3192 static void
3193 finish_struct_anon (tree t)
3195 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3197 if (TREE_STATIC (field))
3198 continue;
3199 if (TREE_CODE (field) != FIELD_DECL)
3200 continue;
3202 if (DECL_NAME (field) == NULL_TREE
3203 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3204 finish_struct_anon_r (field, /*complain=*/true);
3208 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
3209 will be used later during class template instantiation.
3210 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
3211 a non-static member data (FIELD_DECL), a member function
3212 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
3213 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
3214 When FRIEND_P is nonzero, T is either a friend class
3215 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
3216 (FUNCTION_DECL, TEMPLATE_DECL). */
3218 void
3219 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
3221 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
3222 if (CLASSTYPE_TEMPLATE_INFO (type))
3223 CLASSTYPE_DECL_LIST (type)
3224 = tree_cons (friend_p ? NULL_TREE : type,
3225 t, CLASSTYPE_DECL_LIST (type));
3228 /* This function is called from declare_virt_assop_and_dtor via
3229 dfs_walk_all.
3231 DATA is a type that direcly or indirectly inherits the base
3232 represented by BINFO. If BINFO contains a virtual assignment [copy
3233 assignment or move assigment] operator or a virtual constructor,
3234 declare that function in DATA if it hasn't been already declared. */
3236 static tree
3237 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3239 tree bv, fn, t = (tree)data;
3240 tree opname = cp_assignment_operator_id (NOP_EXPR);
3242 gcc_assert (t && CLASS_TYPE_P (t));
3243 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3245 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3246 /* A base without a vtable needs no modification, and its bases
3247 are uninteresting. */
3248 return dfs_skip_bases;
3250 if (BINFO_PRIMARY_P (binfo))
3251 /* If this is a primary base, then we have already looked at the
3252 virtual functions of its vtable. */
3253 return NULL_TREE;
3255 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3257 fn = BV_FN (bv);
3259 if (DECL_NAME (fn) == opname)
3261 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3262 lazily_declare_fn (sfk_copy_assignment, t);
3263 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3264 lazily_declare_fn (sfk_move_assignment, t);
3266 else if (DECL_DESTRUCTOR_P (fn)
3267 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3268 lazily_declare_fn (sfk_destructor, t);
3271 return NULL_TREE;
3274 /* If the class type T has a direct or indirect base that contains a
3275 virtual assignment operator or a virtual destructor, declare that
3276 function in T if it hasn't been already declared. */
3278 static void
3279 declare_virt_assop_and_dtor (tree t)
3281 if (!(TYPE_POLYMORPHIC_P (t)
3282 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3283 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3284 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3285 return;
3287 dfs_walk_all (TYPE_BINFO (t),
3288 dfs_declare_virt_assop_and_dtor,
3289 NULL, t);
3292 /* Declare the inheriting constructor for class T inherited from base
3293 constructor CTOR with the parameter array PARMS of size NPARMS. */
3295 static void
3296 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3298 /* We don't declare an inheriting ctor that would be a default,
3299 copy or move ctor for derived or base. */
3300 if (nparms == 0)
3301 return;
3302 if (nparms == 1
3303 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3305 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3306 if (parm == t || parm == DECL_CONTEXT (ctor))
3307 return;
3310 tree parmlist = void_list_node;
3311 for (int i = nparms - 1; i >= 0; i--)
3312 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3313 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3314 t, false, ctor, parmlist);
3315 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3316 if (add_method (t, fn, false))
3318 DECL_CHAIN (fn) = TYPE_METHODS (t);
3319 TYPE_METHODS (t) = fn;
3323 /* Declare all the inheriting constructors for class T inherited from base
3324 constructor CTOR. */
3326 static void
3327 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3329 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3331 if (flag_new_inheriting_ctors)
3333 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3334 t, /*const*/false, ctor, parms);
3335 add_method (t, ctor, using_decl != NULL_TREE);
3336 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3337 return;
3340 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3341 int i = 0;
3342 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3344 if (TREE_PURPOSE (parms))
3345 one_inheriting_sig (t, ctor, new_parms, i);
3346 new_parms[i++] = TREE_VALUE (parms);
3348 one_inheriting_sig (t, ctor, new_parms, i);
3349 if (parms == NULL_TREE)
3351 if (warning (OPT_Winherited_variadic_ctor,
3352 "the ellipsis in %qD is not inherited", ctor))
3353 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3357 /* Create default constructors, assignment operators, and so forth for
3358 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3359 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3360 the class cannot have a default constructor, copy constructor
3361 taking a const reference argument, or an assignment operator taking
3362 a const reference, respectively. */
3364 static void
3365 add_implicitly_declared_members (tree t, tree* access_decls,
3366 int cant_have_const_cctor,
3367 int cant_have_const_assignment)
3369 bool move_ok = false;
3371 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t)
3372 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3373 && !type_has_move_constructor (t) && !type_has_move_assign (t))
3374 move_ok = true;
3376 /* Destructor. */
3377 if (!CLASSTYPE_DESTRUCTORS (t))
3378 /* In general, we create destructors lazily. */
3379 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3381 /* [class.ctor]
3383 If there is no user-declared constructor for a class, a default
3384 constructor is implicitly declared. */
3385 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3387 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3388 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3389 if (cxx_dialect >= cxx11)
3390 TYPE_HAS_CONSTEXPR_CTOR (t)
3391 /* Don't force the declaration to get a hard answer; if the
3392 definition would have made the class non-literal, it will still be
3393 non-literal because of the base or member in question, and that
3394 gives a better diagnostic. */
3395 = type_maybe_constexpr_default_constructor (t);
3398 /* [class.ctor]
3400 If a class definition does not explicitly declare a copy
3401 constructor, one is declared implicitly. */
3402 if (! TYPE_HAS_COPY_CTOR (t))
3404 TYPE_HAS_COPY_CTOR (t) = 1;
3405 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3406 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3407 if (move_ok)
3408 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3411 /* If there is no assignment operator, one will be created if and
3412 when it is needed. For now, just record whether or not the type
3413 of the parameter to the assignment operator will be a const or
3414 non-const reference. */
3415 if (!TYPE_HAS_COPY_ASSIGN (t))
3417 TYPE_HAS_COPY_ASSIGN (t) = 1;
3418 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3419 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3420 if (move_ok && !LAMBDA_TYPE_P (t))
3421 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3424 /* We can't be lazy about declaring functions that might override
3425 a virtual function from a base class. */
3426 declare_virt_assop_and_dtor (t);
3428 while (*access_decls)
3430 tree using_decl = TREE_VALUE (*access_decls);
3431 tree decl = USING_DECL_DECLS (using_decl);
3432 if (DECL_NAME (using_decl) == ctor_identifier)
3434 /* declare, then remove the decl */
3435 tree ctor_list = decl;
3436 location_t loc = input_location;
3437 input_location = DECL_SOURCE_LOCATION (using_decl);
3438 for (ovl_iterator iter (ctor_list); iter; ++iter)
3439 one_inherited_ctor (*iter, t, using_decl);
3440 *access_decls = TREE_CHAIN (*access_decls);
3441 input_location = loc;
3443 else
3444 access_decls = &TREE_CHAIN (*access_decls);
3448 /* Subroutine of insert_into_classtype_sorted_fields. Recursively
3449 count the number of fields in TYPE, including anonymous union
3450 members. */
3452 static int
3453 count_fields (tree fields)
3455 tree x;
3456 int n_fields = 0;
3457 for (x = fields; x; x = DECL_CHAIN (x))
3459 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3460 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3461 else
3462 n_fields += 1;
3464 return n_fields;
3467 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add
3468 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
3469 elts, starting at offset IDX. */
3471 static int
3472 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
3474 tree x;
3475 for (x = fields; x; x = DECL_CHAIN (x))
3477 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3478 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3479 else
3480 field_vec->elts[idx++] = x;
3482 return idx;
3485 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
3486 starting at offset IDX. */
3488 static int
3489 add_enum_fields_to_record_type (tree enumtype,
3490 struct sorted_fields_type *field_vec,
3491 int idx)
3493 tree values;
3494 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
3495 field_vec->elts[idx++] = TREE_VALUE (values);
3496 return idx;
3499 /* FIELD is a bit-field. We are finishing the processing for its
3500 enclosing type. Issue any appropriate messages and set appropriate
3501 flags. Returns false if an error has been diagnosed. */
3503 static bool
3504 check_bitfield_decl (tree field)
3506 tree type = TREE_TYPE (field);
3507 tree w;
3509 /* Extract the declared width of the bitfield, which has been
3510 temporarily stashed in DECL_INITIAL. */
3511 w = DECL_INITIAL (field);
3512 gcc_assert (w != NULL_TREE);
3513 /* Remove the bit-field width indicator so that the rest of the
3514 compiler does not treat that value as an initializer. */
3515 DECL_INITIAL (field) = NULL_TREE;
3517 /* Detect invalid bit-field type. */
3518 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3520 error ("bit-field %q+#D with non-integral type", field);
3521 w = error_mark_node;
3523 else
3525 location_t loc = input_location;
3526 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3527 STRIP_NOPS (w);
3529 /* detect invalid field size. */
3530 input_location = DECL_SOURCE_LOCATION (field);
3531 w = cxx_constant_value (w);
3532 input_location = loc;
3534 if (TREE_CODE (w) != INTEGER_CST)
3536 error ("bit-field %q+D width not an integer constant", field);
3537 w = error_mark_node;
3539 else if (tree_int_cst_sgn (w) < 0)
3541 error ("negative width in bit-field %q+D", field);
3542 w = error_mark_node;
3544 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3546 error ("zero width for bit-field %q+D", field);
3547 w = error_mark_node;
3549 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3550 && TREE_CODE (type) != BOOLEAN_TYPE
3551 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3552 || ((TREE_CODE (type) == ENUMERAL_TYPE
3553 || TREE_CODE (type) == BOOLEAN_TYPE)
3554 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3555 warning_at (DECL_SOURCE_LOCATION (field), 0,
3556 "width of %qD exceeds its type", field);
3557 else if (TREE_CODE (type) == ENUMERAL_TYPE
3558 && (0 > (compare_tree_int
3559 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3560 warning_at (DECL_SOURCE_LOCATION (field), 0,
3561 "%qD is too small to hold all values of %q#T",
3562 field, type);
3565 if (w != error_mark_node)
3567 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3568 DECL_BIT_FIELD (field) = 1;
3569 return true;
3571 else
3573 /* Non-bit-fields are aligned for their type. */
3574 DECL_BIT_FIELD (field) = 0;
3575 CLEAR_DECL_C_BIT_FIELD (field);
3576 return false;
3580 /* FIELD is a non bit-field. We are finishing the processing for its
3581 enclosing type T. Issue any appropriate messages and set appropriate
3582 flags. */
3584 static bool
3585 check_field_decl (tree field,
3586 tree t,
3587 int* cant_have_const_ctor,
3588 int* no_const_asn_ref)
3590 tree type = strip_array_types (TREE_TYPE (field));
3591 bool any_default_members = false;
3593 /* In C++98 an anonymous union cannot contain any fields which would change
3594 the settings of CANT_HAVE_CONST_CTOR and friends. */
3595 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3597 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3598 structs. So, we recurse through their fields here. */
3599 else if (ANON_AGGR_TYPE_P (type))
3601 for (tree fields = TYPE_FIELDS (type); fields;
3602 fields = DECL_CHAIN (fields))
3603 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3604 any_default_members |= check_field_decl (fields, t,
3605 cant_have_const_ctor,
3606 no_const_asn_ref);
3608 /* Check members with class type for constructors, destructors,
3609 etc. */
3610 else if (CLASS_TYPE_P (type))
3612 /* Never let anything with uninheritable virtuals
3613 make it through without complaint. */
3614 abstract_virtuals_error (field, type);
3616 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3618 static bool warned;
3619 int oldcount = errorcount;
3620 if (TYPE_NEEDS_CONSTRUCTING (type))
3621 error ("member %q+#D with constructor not allowed in union",
3622 field);
3623 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3624 error ("member %q+#D with destructor not allowed in union", field);
3625 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3626 error ("member %q+#D with copy assignment operator not allowed in union",
3627 field);
3628 if (!warned && errorcount > oldcount)
3630 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3631 "only available with -std=c++11 or -std=gnu++11");
3632 warned = true;
3635 else
3637 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3638 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3639 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3640 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3641 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3642 || !TYPE_HAS_COPY_ASSIGN (type));
3643 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3644 || !TYPE_HAS_COPY_CTOR (type));
3645 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3646 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3647 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3648 || TYPE_HAS_COMPLEX_DFLT (type));
3651 if (TYPE_HAS_COPY_CTOR (type)
3652 && !TYPE_HAS_CONST_COPY_CTOR (type))
3653 *cant_have_const_ctor = 1;
3655 if (TYPE_HAS_COPY_ASSIGN (type)
3656 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3657 *no_const_asn_ref = 1;
3660 check_abi_tags (t, field);
3662 if (DECL_INITIAL (field) != NULL_TREE)
3663 /* `build_class_init_list' does not recognize
3664 non-FIELD_DECLs. */
3665 any_default_members = true;
3667 return any_default_members;
3670 /* Check the data members (both static and non-static), class-scoped
3671 typedefs, etc., appearing in the declaration of T. Issue
3672 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3673 declaration order) of access declarations; each TREE_VALUE in this
3674 list is a USING_DECL.
3676 In addition, set the following flags:
3678 EMPTY_P
3679 The class is empty, i.e., contains no non-static data members.
3681 CANT_HAVE_CONST_CTOR_P
3682 This class cannot have an implicitly generated copy constructor
3683 taking a const reference.
3685 CANT_HAVE_CONST_ASN_REF
3686 This class cannot have an implicitly generated assignment
3687 operator taking a const reference.
3689 All of these flags should be initialized before calling this
3690 function.
3692 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3693 fields can be added by adding to this chain. */
3695 static void
3696 check_field_decls (tree t, tree *access_decls,
3697 int *cant_have_const_ctor_p,
3698 int *no_const_asn_ref_p)
3700 tree *field;
3701 tree *next;
3702 bool has_pointers;
3703 bool any_default_members;
3704 int cant_pack = 0;
3705 int field_access = -1;
3707 /* Assume there are no access declarations. */
3708 *access_decls = NULL_TREE;
3709 /* Assume this class has no pointer members. */
3710 has_pointers = false;
3711 /* Assume none of the members of this class have default
3712 initializations. */
3713 any_default_members = false;
3715 for (field = &TYPE_FIELDS (t); *field; field = next)
3717 tree x = *field;
3718 tree type = TREE_TYPE (x);
3719 int this_field_access;
3721 next = &DECL_CHAIN (x);
3723 if (TREE_CODE (x) == USING_DECL)
3725 /* Save the access declarations for our caller. */
3726 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3727 continue;
3730 if (TREE_CODE (x) == TYPE_DECL
3731 || TREE_CODE (x) == TEMPLATE_DECL)
3732 continue;
3734 /* If we've gotten this far, it's a data member, possibly static,
3735 or an enumerator. */
3736 if (TREE_CODE (x) != CONST_DECL)
3737 DECL_CONTEXT (x) = t;
3739 /* When this goes into scope, it will be a non-local reference. */
3740 DECL_NONLOCAL (x) = 1;
3742 if (TREE_CODE (t) == UNION_TYPE)
3744 /* [class.union] (C++98)
3746 If a union contains a static data member, or a member of
3747 reference type, the program is ill-formed.
3749 In C++11 [class.union] says:
3750 If a union contains a non-static data member of reference type
3751 the program is ill-formed. */
3752 if (VAR_P (x) && cxx_dialect < cxx11)
3754 error ("in C++98 %q+D may not be static because it is "
3755 "a member of a union", x);
3756 continue;
3758 if (TREE_CODE (type) == REFERENCE_TYPE
3759 && TREE_CODE (x) == FIELD_DECL)
3761 error ("non-static data member %q+D in a union may not "
3762 "have reference type %qT", x, type);
3763 continue;
3767 /* Perform error checking that did not get done in
3768 grokdeclarator. */
3769 if (TREE_CODE (type) == FUNCTION_TYPE)
3771 error ("field %q+D invalidly declared function type", x);
3772 type = build_pointer_type (type);
3773 TREE_TYPE (x) = type;
3775 else if (TREE_CODE (type) == METHOD_TYPE)
3777 error ("field %q+D invalidly declared method type", x);
3778 type = build_pointer_type (type);
3779 TREE_TYPE (x) = type;
3782 if (type == error_mark_node)
3783 continue;
3785 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3786 continue;
3788 /* Now it can only be a FIELD_DECL. */
3790 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3791 CLASSTYPE_NON_AGGREGATE (t) = 1;
3793 /* If at least one non-static data member is non-literal, the whole
3794 class becomes non-literal. Per Core/1453, volatile non-static
3795 data members and base classes are also not allowed.
3796 Note: if the type is incomplete we will complain later on. */
3797 if (COMPLETE_TYPE_P (type)
3798 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3799 CLASSTYPE_LITERAL_P (t) = false;
3801 /* A standard-layout class is a class that:
3803 has the same access control (Clause 11) for all non-static data members,
3804 ... */
3805 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3806 if (field_access == -1)
3807 field_access = this_field_access;
3808 else if (this_field_access != field_access)
3809 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3811 /* If this is of reference type, check if it needs an init. */
3812 if (TREE_CODE (type) == REFERENCE_TYPE)
3814 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3815 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3816 if (DECL_INITIAL (x) == NULL_TREE)
3817 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3818 if (cxx_dialect < cxx11)
3820 /* ARM $12.6.2: [A member initializer list] (or, for an
3821 aggregate, initialization by a brace-enclosed list) is the
3822 only way to initialize nonstatic const and reference
3823 members. */
3824 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3825 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3829 type = strip_array_types (type);
3831 if (TYPE_PACKED (t))
3833 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3835 warning_at
3836 (DECL_SOURCE_LOCATION (x), 0,
3837 "ignoring packed attribute because of unpacked non-POD field %q#D",
3839 cant_pack = 1;
3841 else if (DECL_C_BIT_FIELD (x)
3842 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3843 DECL_PACKED (x) = 1;
3846 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3847 /* We don't treat zero-width bitfields as making a class
3848 non-empty. */
3850 else
3852 /* The class is non-empty. */
3853 CLASSTYPE_EMPTY_P (t) = 0;
3854 /* The class is not even nearly empty. */
3855 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3856 /* If one of the data members contains an empty class,
3857 so does T. */
3858 if (CLASS_TYPE_P (type)
3859 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3860 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3863 /* This is used by -Weffc++ (see below). Warn only for pointers
3864 to members which might hold dynamic memory. So do not warn
3865 for pointers to functions or pointers to members. */
3866 if (TYPE_PTR_P (type)
3867 && !TYPE_PTRFN_P (type))
3868 has_pointers = true;
3870 if (CLASS_TYPE_P (type))
3872 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3873 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3874 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3875 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3878 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3879 CLASSTYPE_HAS_MUTABLE (t) = 1;
3881 if (DECL_MUTABLE_P (x))
3883 if (CP_TYPE_CONST_P (type))
3885 error ("member %q+D cannot be declared both %<const%> "
3886 "and %<mutable%>", x);
3887 continue;
3889 if (TREE_CODE (type) == REFERENCE_TYPE)
3891 error ("member %q+D cannot be declared as a %<mutable%> "
3892 "reference", x);
3893 continue;
3897 if (! layout_pod_type_p (type))
3898 /* DR 148 now allows pointers to members (which are POD themselves),
3899 to be allowed in POD structs. */
3900 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3902 if (!std_layout_type_p (type))
3903 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3905 if (! zero_init_p (type))
3906 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3908 /* We set DECL_C_BIT_FIELD in grokbitfield.
3909 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3910 if ((! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
3911 && check_field_decl (x, t,
3912 cant_have_const_ctor_p,
3913 no_const_asn_ref_p))
3915 if (any_default_members
3916 && TREE_CODE (t) == UNION_TYPE)
3917 error ("multiple fields in union %qT initialized", t);
3918 any_default_members = true;
3921 /* Now that we've removed bit-field widths from DECL_INITIAL,
3922 anything left in DECL_INITIAL is an NSDMI that makes the class
3923 non-aggregate in C++11. */
3924 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3925 CLASSTYPE_NON_AGGREGATE (t) = true;
3927 /* If any field is const, the structure type is pseudo-const. */
3928 if (CP_TYPE_CONST_P (type))
3930 C_TYPE_FIELDS_READONLY (t) = 1;
3931 if (DECL_INITIAL (x) == NULL_TREE)
3932 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3933 if (cxx_dialect < cxx11)
3935 /* ARM $12.6.2: [A member initializer list] (or, for an
3936 aggregate, initialization by a brace-enclosed list) is the
3937 only way to initialize nonstatic const and reference
3938 members. */
3939 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3940 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3943 /* A field that is pseudo-const makes the structure likewise. */
3944 else if (CLASS_TYPE_P (type))
3946 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3947 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3948 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3949 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3952 /* Core issue 80: A nonstatic data member is required to have a
3953 different name from the class iff the class has a
3954 user-declared constructor. */
3955 if (constructor_name_p (DECL_NAME (x), t)
3956 && TYPE_HAS_USER_CONSTRUCTOR (t))
3957 permerror (DECL_SOURCE_LOCATION (x),
3958 "field %q#D with same name as class", x);
3961 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3962 it should also define a copy constructor and an assignment operator to
3963 implement the correct copy semantic (deep vs shallow, etc.). As it is
3964 not feasible to check whether the constructors do allocate dynamic memory
3965 and store it within members, we approximate the warning like this:
3967 -- Warn only if there are members which are pointers
3968 -- Warn only if there is a non-trivial constructor (otherwise,
3969 there cannot be memory allocated).
3970 -- Warn only if there is a non-trivial destructor. We assume that the
3971 user at least implemented the cleanup correctly, and a destructor
3972 is needed to free dynamic memory.
3974 This seems enough for practical purposes. */
3975 if (warn_ecpp
3976 && has_pointers
3977 && TYPE_HAS_USER_CONSTRUCTOR (t)
3978 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3979 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3981 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3983 if (! TYPE_HAS_COPY_CTOR (t))
3985 warning (OPT_Weffc__,
3986 " but does not override %<%T(const %T&)%>", t, t);
3987 if (!TYPE_HAS_COPY_ASSIGN (t))
3988 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3990 else if (! TYPE_HAS_COPY_ASSIGN (t))
3991 warning (OPT_Weffc__,
3992 " but does not override %<operator=(const %T&)%>", t);
3995 /* Non-static data member initializers make the default constructor
3996 non-trivial. */
3997 if (any_default_members)
3999 TYPE_NEEDS_CONSTRUCTING (t) = true;
4000 TYPE_HAS_COMPLEX_DFLT (t) = true;
4003 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
4004 if (cant_pack)
4005 TYPE_PACKED (t) = 0;
4007 /* Check anonymous struct/anonymous union fields. */
4008 finish_struct_anon (t);
4010 /* We've built up the list of access declarations in reverse order.
4011 Fix that now. */
4012 *access_decls = nreverse (*access_decls);
4015 /* If TYPE is an empty class type, records its OFFSET in the table of
4016 OFFSETS. */
4018 static int
4019 record_subobject_offset (tree type, tree offset, splay_tree offsets)
4021 splay_tree_node n;
4023 if (!is_empty_class (type))
4024 return 0;
4026 /* Record the location of this empty object in OFFSETS. */
4027 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4028 if (!n)
4029 n = splay_tree_insert (offsets,
4030 (splay_tree_key) offset,
4031 (splay_tree_value) NULL_TREE);
4032 n->value = ((splay_tree_value)
4033 tree_cons (NULL_TREE,
4034 type,
4035 (tree) n->value));
4037 return 0;
4040 /* Returns nonzero if TYPE is an empty class type and there is
4041 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
4043 static int
4044 check_subobject_offset (tree type, tree offset, splay_tree offsets)
4046 splay_tree_node n;
4047 tree t;
4049 if (!is_empty_class (type))
4050 return 0;
4052 /* Record the location of this empty object in OFFSETS. */
4053 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
4054 if (!n)
4055 return 0;
4057 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
4058 if (same_type_p (TREE_VALUE (t), type))
4059 return 1;
4061 return 0;
4064 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
4065 F for every subobject, passing it the type, offset, and table of
4066 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
4067 be traversed.
4069 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
4070 than MAX_OFFSET will not be walked.
4072 If F returns a nonzero value, the traversal ceases, and that value
4073 is returned. Otherwise, returns zero. */
4075 static int
4076 walk_subobject_offsets (tree type,
4077 subobject_offset_fn f,
4078 tree offset,
4079 splay_tree offsets,
4080 tree max_offset,
4081 int vbases_p)
4083 int r = 0;
4084 tree type_binfo = NULL_TREE;
4086 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
4087 stop. */
4088 if (max_offset && tree_int_cst_lt (max_offset, offset))
4089 return 0;
4091 if (type == error_mark_node)
4092 return 0;
4094 if (!TYPE_P (type))
4096 type_binfo = type;
4097 type = BINFO_TYPE (type);
4100 if (CLASS_TYPE_P (type))
4102 tree field;
4103 tree binfo;
4104 int i;
4106 /* Avoid recursing into objects that are not interesting. */
4107 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
4108 return 0;
4110 /* Record the location of TYPE. */
4111 r = (*f) (type, offset, offsets);
4112 if (r)
4113 return r;
4115 /* Iterate through the direct base classes of TYPE. */
4116 if (!type_binfo)
4117 type_binfo = TYPE_BINFO (type);
4118 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
4120 tree binfo_offset;
4122 if (BINFO_VIRTUAL_P (binfo))
4123 continue;
4125 tree orig_binfo;
4126 /* We cannot rely on BINFO_OFFSET being set for the base
4127 class yet, but the offsets for direct non-virtual
4128 bases can be calculated by going back to the TYPE. */
4129 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
4130 binfo_offset = size_binop (PLUS_EXPR,
4131 offset,
4132 BINFO_OFFSET (orig_binfo));
4134 r = walk_subobject_offsets (binfo,
4136 binfo_offset,
4137 offsets,
4138 max_offset,
4139 /*vbases_p=*/0);
4140 if (r)
4141 return r;
4144 if (CLASSTYPE_VBASECLASSES (type))
4146 unsigned ix;
4147 vec<tree, va_gc> *vbases;
4149 /* Iterate through the virtual base classes of TYPE. In G++
4150 3.2, we included virtual bases in the direct base class
4151 loop above, which results in incorrect results; the
4152 correct offsets for virtual bases are only known when
4153 working with the most derived type. */
4154 if (vbases_p)
4155 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
4156 vec_safe_iterate (vbases, ix, &binfo); ix++)
4158 r = walk_subobject_offsets (binfo,
4160 size_binop (PLUS_EXPR,
4161 offset,
4162 BINFO_OFFSET (binfo)),
4163 offsets,
4164 max_offset,
4165 /*vbases_p=*/0);
4166 if (r)
4167 return r;
4169 else
4171 /* We still have to walk the primary base, if it is
4172 virtual. (If it is non-virtual, then it was walked
4173 above.) */
4174 tree vbase = get_primary_binfo (type_binfo);
4176 if (vbase && BINFO_VIRTUAL_P (vbase)
4177 && BINFO_PRIMARY_P (vbase)
4178 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
4180 r = (walk_subobject_offsets
4181 (vbase, f, offset,
4182 offsets, max_offset, /*vbases_p=*/0));
4183 if (r)
4184 return r;
4189 /* Iterate through the fields of TYPE. */
4190 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4191 if (TREE_CODE (field) == FIELD_DECL
4192 && TREE_TYPE (field) != error_mark_node
4193 && !DECL_ARTIFICIAL (field))
4195 tree field_offset;
4197 field_offset = byte_position (field);
4199 r = walk_subobject_offsets (TREE_TYPE (field),
4201 size_binop (PLUS_EXPR,
4202 offset,
4203 field_offset),
4204 offsets,
4205 max_offset,
4206 /*vbases_p=*/1);
4207 if (r)
4208 return r;
4211 else if (TREE_CODE (type) == ARRAY_TYPE)
4213 tree element_type = strip_array_types (type);
4214 tree domain = TYPE_DOMAIN (type);
4215 tree index;
4217 /* Avoid recursing into objects that are not interesting. */
4218 if (!CLASS_TYPE_P (element_type)
4219 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
4220 || !domain
4221 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
4222 return 0;
4224 /* Step through each of the elements in the array. */
4225 for (index = size_zero_node;
4226 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
4227 index = size_binop (PLUS_EXPR, index, size_one_node))
4229 r = walk_subobject_offsets (TREE_TYPE (type),
4231 offset,
4232 offsets,
4233 max_offset,
4234 /*vbases_p=*/1);
4235 if (r)
4236 return r;
4237 offset = size_binop (PLUS_EXPR, offset,
4238 TYPE_SIZE_UNIT (TREE_TYPE (type)));
4239 /* If this new OFFSET is bigger than the MAX_OFFSET, then
4240 there's no point in iterating through the remaining
4241 elements of the array. */
4242 if (max_offset && tree_int_cst_lt (max_offset, offset))
4243 break;
4247 return 0;
4250 /* Record all of the empty subobjects of TYPE (either a type or a
4251 binfo). If IS_DATA_MEMBER is true, then a non-static data member
4252 is being placed at OFFSET; otherwise, it is a base class that is
4253 being placed at OFFSET. */
4255 static void
4256 record_subobject_offsets (tree type,
4257 tree offset,
4258 splay_tree offsets,
4259 bool is_data_member)
4261 tree max_offset;
4262 /* If recording subobjects for a non-static data member or a
4263 non-empty base class , we do not need to record offsets beyond
4264 the size of the biggest empty class. Additional data members
4265 will go at the end of the class. Additional base classes will go
4266 either at offset zero (if empty, in which case they cannot
4267 overlap with offsets past the size of the biggest empty class) or
4268 at the end of the class.
4270 However, if we are placing an empty base class, then we must record
4271 all offsets, as either the empty class is at offset zero (where
4272 other empty classes might later be placed) or at the end of the
4273 class (where other objects might then be placed, so other empty
4274 subobjects might later overlap). */
4275 if (is_data_member
4276 || !is_empty_class (BINFO_TYPE (type)))
4277 max_offset = sizeof_biggest_empty_class;
4278 else
4279 max_offset = NULL_TREE;
4280 walk_subobject_offsets (type, record_subobject_offset, offset,
4281 offsets, max_offset, is_data_member);
4284 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4285 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4286 virtual bases of TYPE are examined. */
4288 static int
4289 layout_conflict_p (tree type,
4290 tree offset,
4291 splay_tree offsets,
4292 int vbases_p)
4294 splay_tree_node max_node;
4296 /* Get the node in OFFSETS that indicates the maximum offset where
4297 an empty subobject is located. */
4298 max_node = splay_tree_max (offsets);
4299 /* If there aren't any empty subobjects, then there's no point in
4300 performing this check. */
4301 if (!max_node)
4302 return 0;
4304 return walk_subobject_offsets (type, check_subobject_offset, offset,
4305 offsets, (tree) (max_node->key),
4306 vbases_p);
4309 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4310 non-static data member of the type indicated by RLI. BINFO is the
4311 binfo corresponding to the base subobject, OFFSETS maps offsets to
4312 types already located at those offsets. This function determines
4313 the position of the DECL. */
4315 static void
4316 layout_nonempty_base_or_field (record_layout_info rli,
4317 tree decl,
4318 tree binfo,
4319 splay_tree offsets)
4321 tree offset = NULL_TREE;
4322 bool field_p;
4323 tree type;
4325 if (binfo)
4327 /* For the purposes of determining layout conflicts, we want to
4328 use the class type of BINFO; TREE_TYPE (DECL) will be the
4329 CLASSTYPE_AS_BASE version, which does not contain entries for
4330 zero-sized bases. */
4331 type = TREE_TYPE (binfo);
4332 field_p = false;
4334 else
4336 type = TREE_TYPE (decl);
4337 field_p = true;
4340 /* Try to place the field. It may take more than one try if we have
4341 a hard time placing the field without putting two objects of the
4342 same type at the same address. */
4343 while (1)
4345 struct record_layout_info_s old_rli = *rli;
4347 /* Place this field. */
4348 place_field (rli, decl);
4349 offset = byte_position (decl);
4351 /* We have to check to see whether or not there is already
4352 something of the same type at the offset we're about to use.
4353 For example, consider:
4355 struct S {};
4356 struct T : public S { int i; };
4357 struct U : public S, public T {};
4359 Here, we put S at offset zero in U. Then, we can't put T at
4360 offset zero -- its S component would be at the same address
4361 as the S we already allocated. So, we have to skip ahead.
4362 Since all data members, including those whose type is an
4363 empty class, have nonzero size, any overlap can happen only
4364 with a direct or indirect base-class -- it can't happen with
4365 a data member. */
4366 /* In a union, overlap is permitted; all members are placed at
4367 offset zero. */
4368 if (TREE_CODE (rli->t) == UNION_TYPE)
4369 break;
4370 if (layout_conflict_p (field_p ? type : binfo, offset,
4371 offsets, field_p))
4373 /* Strip off the size allocated to this field. That puts us
4374 at the first place we could have put the field with
4375 proper alignment. */
4376 *rli = old_rli;
4378 /* Bump up by the alignment required for the type. */
4379 rli->bitpos
4380 = size_binop (PLUS_EXPR, rli->bitpos,
4381 bitsize_int (binfo
4382 ? CLASSTYPE_ALIGN (type)
4383 : TYPE_ALIGN (type)));
4384 normalize_rli (rli);
4386 else if (TREE_CODE (type) == NULLPTR_TYPE
4387 && warn_abi && abi_version_crosses (9))
4389 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4390 the offset wasn't aligned like a pointer when we started to
4391 layout this field, that affects its position. */
4392 tree pos = rli_size_unit_so_far (&old_rli);
4393 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4395 if (abi_version_at_least (9))
4396 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4397 "alignment of %qD increased in -fabi-version=9 "
4398 "(GCC 5.2)", decl);
4399 else
4400 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4401 "of %qD will increase in -fabi-version=9", decl);
4403 break;
4405 else
4406 /* There was no conflict. We're done laying out this field. */
4407 break;
4410 /* Now that we know where it will be placed, update its
4411 BINFO_OFFSET. */
4412 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4413 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4414 this point because their BINFO_OFFSET is copied from another
4415 hierarchy. Therefore, we may not need to add the entire
4416 OFFSET. */
4417 propagate_binfo_offsets (binfo,
4418 size_diffop_loc (input_location,
4419 fold_convert (ssizetype, offset),
4420 fold_convert (ssizetype,
4421 BINFO_OFFSET (binfo))));
4424 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4426 static int
4427 empty_base_at_nonzero_offset_p (tree type,
4428 tree offset,
4429 splay_tree /*offsets*/)
4431 return is_empty_class (type) && !integer_zerop (offset);
4434 /* Layout the empty base BINFO. EOC indicates the byte currently just
4435 past the end of the class, and should be correctly aligned for a
4436 class of the type indicated by BINFO; OFFSETS gives the offsets of
4437 the empty bases allocated so far. T is the most derived
4438 type. Return nonzero iff we added it at the end. */
4440 static bool
4441 layout_empty_base (record_layout_info rli, tree binfo,
4442 tree eoc, splay_tree offsets)
4444 tree alignment;
4445 tree basetype = BINFO_TYPE (binfo);
4446 bool atend = false;
4448 /* This routine should only be used for empty classes. */
4449 gcc_assert (is_empty_class (basetype));
4450 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4452 if (!integer_zerop (BINFO_OFFSET (binfo)))
4453 propagate_binfo_offsets
4454 (binfo, size_diffop_loc (input_location,
4455 size_zero_node, BINFO_OFFSET (binfo)));
4457 /* This is an empty base class. We first try to put it at offset
4458 zero. */
4459 if (layout_conflict_p (binfo,
4460 BINFO_OFFSET (binfo),
4461 offsets,
4462 /*vbases_p=*/0))
4464 /* That didn't work. Now, we move forward from the next
4465 available spot in the class. */
4466 atend = true;
4467 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4468 while (1)
4470 if (!layout_conflict_p (binfo,
4471 BINFO_OFFSET (binfo),
4472 offsets,
4473 /*vbases_p=*/0))
4474 /* We finally found a spot where there's no overlap. */
4475 break;
4477 /* There's overlap here, too. Bump along to the next spot. */
4478 propagate_binfo_offsets (binfo, alignment);
4482 if (CLASSTYPE_USER_ALIGN (basetype))
4484 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4485 if (warn_packed)
4486 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4487 TYPE_USER_ALIGN (rli->t) = 1;
4490 return atend;
4493 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4494 fields at NEXT_FIELD, and return it. */
4496 static tree
4497 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4499 /* Create the FIELD_DECL. */
4500 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4501 tree decl = build_decl (input_location,
4502 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4503 DECL_ARTIFICIAL (decl) = 1;
4504 DECL_IGNORED_P (decl) = 1;
4505 DECL_FIELD_CONTEXT (decl) = t;
4506 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4507 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4508 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4509 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4510 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4511 DECL_FIELD_IS_BASE (decl) = 1;
4513 /* Add the new FIELD_DECL to the list of fields for T. */
4514 DECL_CHAIN (decl) = *next_field;
4515 *next_field = decl;
4516 next_field = &DECL_CHAIN (decl);
4518 return decl;
4521 /* Layout the base given by BINFO in the class indicated by RLI.
4522 *BASE_ALIGN is a running maximum of the alignments of
4523 any base class. OFFSETS gives the location of empty base
4524 subobjects. T is the most derived type. Return nonzero if the new
4525 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4526 *NEXT_FIELD, unless BINFO is for an empty base class.
4528 Returns the location at which the next field should be inserted. */
4530 static tree *
4531 build_base_field (record_layout_info rli, tree binfo,
4532 splay_tree offsets, tree *next_field)
4534 tree t = rli->t;
4535 tree basetype = BINFO_TYPE (binfo);
4537 if (!COMPLETE_TYPE_P (basetype))
4538 /* This error is now reported in xref_tag, thus giving better
4539 location information. */
4540 return next_field;
4542 /* Place the base class. */
4543 if (!is_empty_class (basetype))
4545 tree decl;
4547 /* The containing class is non-empty because it has a non-empty
4548 base class. */
4549 CLASSTYPE_EMPTY_P (t) = 0;
4551 /* Create the FIELD_DECL. */
4552 decl = build_base_field_1 (t, basetype, next_field);
4554 /* Try to place the field. It may take more than one try if we
4555 have a hard time placing the field without putting two
4556 objects of the same type at the same address. */
4557 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4559 else
4561 tree eoc;
4562 bool atend;
4564 /* On some platforms (ARM), even empty classes will not be
4565 byte-aligned. */
4566 eoc = round_up_loc (input_location,
4567 rli_size_unit_so_far (rli),
4568 CLASSTYPE_ALIGN_UNIT (basetype));
4569 atend = layout_empty_base (rli, binfo, eoc, offsets);
4570 /* A nearly-empty class "has no proper base class that is empty,
4571 not morally virtual, and at an offset other than zero." */
4572 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4574 if (atend)
4575 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4576 /* The check above (used in G++ 3.2) is insufficient because
4577 an empty class placed at offset zero might itself have an
4578 empty base at a nonzero offset. */
4579 else if (walk_subobject_offsets (basetype,
4580 empty_base_at_nonzero_offset_p,
4581 size_zero_node,
4582 /*offsets=*/NULL,
4583 /*max_offset=*/NULL_TREE,
4584 /*vbases_p=*/true))
4585 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4588 /* We used to not create a FIELD_DECL for empty base classes because of
4589 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4590 be a problem anymore. We need them to handle initialization of C++17
4591 aggregate bases. */
4592 if (cxx_dialect >= cxx1z && !BINFO_VIRTUAL_P (binfo))
4594 tree decl = build_base_field_1 (t, basetype, next_field);
4595 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4596 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4597 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4600 /* An empty virtual base causes a class to be non-empty
4601 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4602 here because that was already done when the virtual table
4603 pointer was created. */
4606 /* Record the offsets of BINFO and its base subobjects. */
4607 record_subobject_offsets (binfo,
4608 BINFO_OFFSET (binfo),
4609 offsets,
4610 /*is_data_member=*/false);
4612 return next_field;
4615 /* Layout all of the non-virtual base classes. Record empty
4616 subobjects in OFFSETS. T is the most derived type. Return nonzero
4617 if the type cannot be nearly empty. The fields created
4618 corresponding to the base classes will be inserted at
4619 *NEXT_FIELD. */
4621 static void
4622 build_base_fields (record_layout_info rli,
4623 splay_tree offsets, tree *next_field)
4625 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4626 subobjects. */
4627 tree t = rli->t;
4628 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4629 int i;
4631 /* The primary base class is always allocated first. */
4632 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4633 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4634 offsets, next_field);
4636 /* Now allocate the rest of the bases. */
4637 for (i = 0; i < n_baseclasses; ++i)
4639 tree base_binfo;
4641 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4643 /* The primary base was already allocated above, so we don't
4644 need to allocate it again here. */
4645 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4646 continue;
4648 /* Virtual bases are added at the end (a primary virtual base
4649 will have already been added). */
4650 if (BINFO_VIRTUAL_P (base_binfo))
4651 continue;
4653 next_field = build_base_field (rli, base_binfo,
4654 offsets, next_field);
4658 /* Go through the TYPE_METHODS of T issuing any appropriate
4659 diagnostics, figuring out which methods override which other
4660 methods, and so forth. */
4662 static void
4663 check_methods (tree t)
4665 tree x;
4667 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x))
4669 check_for_override (x, t);
4670 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4671 error ("initializer specified for non-virtual method %q+D", x);
4672 /* The name of the field is the original field name
4673 Save this in auxiliary field for later overloading. */
4674 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4676 TYPE_POLYMORPHIC_P (t) = 1;
4677 if (DECL_PURE_VIRTUAL_P (x))
4678 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4680 /* All user-provided destructors are non-trivial.
4681 Constructors and assignment ops are handled in
4682 grok_special_member_properties. */
4683 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4684 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4685 if (!DECL_VIRTUAL_P (x)
4686 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x)))
4687 error_at (DECL_SOURCE_LOCATION (x),
4688 "%<transaction_safe_dynamic%> may only be specified for "
4689 "a virtual function");
4693 /* FN is a constructor or destructor. Clone the declaration to create
4694 a specialized in-charge or not-in-charge version, as indicated by
4695 NAME. */
4697 static tree
4698 build_clone (tree fn, tree name)
4700 tree parms;
4701 tree clone;
4703 /* Copy the function. */
4704 clone = copy_decl (fn);
4705 /* Reset the function name. */
4706 DECL_NAME (clone) = name;
4707 /* Remember where this function came from. */
4708 DECL_ABSTRACT_ORIGIN (clone) = fn;
4709 /* Make it easy to find the CLONE given the FN. */
4710 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4711 DECL_CHAIN (fn) = clone;
4713 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4714 if (TREE_CODE (clone) == TEMPLATE_DECL)
4716 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4717 DECL_TEMPLATE_RESULT (clone) = result;
4718 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4719 DECL_TI_TEMPLATE (result) = clone;
4720 TREE_TYPE (clone) = TREE_TYPE (result);
4721 return clone;
4723 else
4725 // Clone constraints.
4726 if (flag_concepts)
4727 if (tree ci = get_constraints (fn))
4728 set_constraints (clone, copy_node (ci));
4732 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4733 DECL_CLONED_FUNCTION (clone) = fn;
4734 /* There's no pending inline data for this function. */
4735 DECL_PENDING_INLINE_INFO (clone) = NULL;
4736 DECL_PENDING_INLINE_P (clone) = 0;
4738 /* The base-class destructor is not virtual. */
4739 if (name == base_dtor_identifier)
4741 DECL_VIRTUAL_P (clone) = 0;
4742 if (TREE_CODE (clone) != TEMPLATE_DECL)
4743 DECL_VINDEX (clone) = NULL_TREE;
4746 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4747 if (ctor_omit_inherited_parms_p)
4748 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4750 /* If there was an in-charge parameter, drop it from the function
4751 type. */
4752 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4754 tree basetype;
4755 tree parmtypes;
4756 tree exceptions;
4758 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4759 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4760 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4761 /* Skip the `this' parameter. */
4762 parmtypes = TREE_CHAIN (parmtypes);
4763 /* Skip the in-charge parameter. */
4764 parmtypes = TREE_CHAIN (parmtypes);
4765 /* And the VTT parm, in a complete [cd]tor. */
4766 if (DECL_HAS_VTT_PARM_P (fn)
4767 && ! DECL_NEEDS_VTT_PARM_P (clone))
4768 parmtypes = TREE_CHAIN (parmtypes);
4769 if (ctor_omit_inherited_parms_p)
4771 /* If we're omitting inherited parms, that just leaves the VTT. */
4772 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4773 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4775 TREE_TYPE (clone)
4776 = build_method_type_directly (basetype,
4777 TREE_TYPE (TREE_TYPE (clone)),
4778 parmtypes);
4779 if (exceptions)
4780 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4781 exceptions);
4782 TREE_TYPE (clone)
4783 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4784 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4787 /* Copy the function parameters. */
4788 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4789 /* Remove the in-charge parameter. */
4790 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4792 DECL_CHAIN (DECL_ARGUMENTS (clone))
4793 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4794 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4796 /* And the VTT parm, in a complete [cd]tor. */
4797 if (DECL_HAS_VTT_PARM_P (fn))
4799 if (DECL_NEEDS_VTT_PARM_P (clone))
4800 DECL_HAS_VTT_PARM_P (clone) = 1;
4801 else
4803 DECL_CHAIN (DECL_ARGUMENTS (clone))
4804 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4805 DECL_HAS_VTT_PARM_P (clone) = 0;
4809 /* A base constructor inheriting from a virtual base doesn't get the
4810 arguments. */
4811 if (ctor_omit_inherited_parms_p)
4812 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4814 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4816 DECL_CONTEXT (parms) = clone;
4817 cxx_dup_lang_specific_decl (parms);
4820 /* Create the RTL for this function. */
4821 SET_DECL_RTL (clone, NULL);
4822 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4824 return clone;
4827 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4828 not invoke this function directly.
4830 For a non-thunk function, returns the address of the slot for storing
4831 the function it is a clone of. Otherwise returns NULL_TREE.
4833 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4834 cloned_function is unset. This is to support the separate
4835 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4836 on a template makes sense, but not the former. */
4838 tree *
4839 decl_cloned_function_p (const_tree decl, bool just_testing)
4841 tree *ptr;
4842 if (just_testing)
4843 decl = STRIP_TEMPLATE (decl);
4845 if (TREE_CODE (decl) != FUNCTION_DECL
4846 || !DECL_LANG_SPECIFIC (decl)
4847 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4849 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4850 if (!just_testing)
4851 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4852 else
4853 #endif
4854 return NULL;
4857 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4858 if (just_testing && *ptr == NULL_TREE)
4859 return NULL;
4860 else
4861 return ptr;
4864 /* Produce declarations for all appropriate clones of FN. If
4865 UPDATE_METHODS is true, the clones are added to the
4866 CLASSTYPE_METHOD_VEC. */
4868 void
4869 clone_function_decl (tree fn, bool update_methods)
4871 tree clone;
4873 /* Avoid inappropriate cloning. */
4874 if (DECL_CHAIN (fn)
4875 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4876 return;
4878 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4880 /* For each constructor, we need two variants: an in-charge version
4881 and a not-in-charge version. */
4882 clone = build_clone (fn, complete_ctor_identifier);
4883 if (update_methods)
4884 add_method (DECL_CONTEXT (clone), clone, false);
4885 clone = build_clone (fn, base_ctor_identifier);
4886 if (update_methods)
4887 add_method (DECL_CONTEXT (clone), clone, false);
4889 else
4891 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4893 /* For each destructor, we need three variants: an in-charge
4894 version, a not-in-charge version, and an in-charge deleting
4895 version. We clone the deleting version first because that
4896 means it will go second on the TYPE_METHODS list -- and that
4897 corresponds to the correct layout order in the virtual
4898 function table.
4900 For a non-virtual destructor, we do not build a deleting
4901 destructor. */
4902 if (DECL_VIRTUAL_P (fn))
4904 clone = build_clone (fn, deleting_dtor_identifier);
4905 if (update_methods)
4906 add_method (DECL_CONTEXT (clone), clone, false);
4908 clone = build_clone (fn, complete_dtor_identifier);
4909 if (update_methods)
4910 add_method (DECL_CONTEXT (clone), clone, false);
4911 clone = build_clone (fn, base_dtor_identifier);
4912 if (update_methods)
4913 add_method (DECL_CONTEXT (clone), clone, false);
4916 /* Note that this is an abstract function that is never emitted. */
4917 DECL_ABSTRACT_P (fn) = true;
4920 /* DECL is an in charge constructor, which is being defined. This will
4921 have had an in class declaration, from whence clones were
4922 declared. An out-of-class definition can specify additional default
4923 arguments. As it is the clones that are involved in overload
4924 resolution, we must propagate the information from the DECL to its
4925 clones. */
4927 void
4928 adjust_clone_args (tree decl)
4930 tree clone;
4932 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4933 clone = DECL_CHAIN (clone))
4935 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4936 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4937 tree decl_parms, clone_parms;
4939 clone_parms = orig_clone_parms;
4941 /* Skip the 'this' parameter. */
4942 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4943 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4945 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4946 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4947 if (DECL_HAS_VTT_PARM_P (decl))
4948 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4950 clone_parms = orig_clone_parms;
4951 if (DECL_HAS_VTT_PARM_P (clone))
4952 clone_parms = TREE_CHAIN (clone_parms);
4954 for (decl_parms = orig_decl_parms; decl_parms;
4955 decl_parms = TREE_CHAIN (decl_parms),
4956 clone_parms = TREE_CHAIN (clone_parms))
4958 if (clone_parms == void_list_node)
4960 gcc_assert (decl_parms == clone_parms
4961 || ctor_omit_inherited_parms (clone));
4962 break;
4965 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4966 TREE_TYPE (clone_parms)));
4968 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4970 /* A default parameter has been added. Adjust the
4971 clone's parameters. */
4972 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4973 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4974 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4975 tree type;
4977 clone_parms = orig_decl_parms;
4979 if (DECL_HAS_VTT_PARM_P (clone))
4981 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4982 TREE_VALUE (orig_clone_parms),
4983 clone_parms);
4984 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4986 type = build_method_type_directly (basetype,
4987 TREE_TYPE (TREE_TYPE (clone)),
4988 clone_parms);
4989 if (exceptions)
4990 type = build_exception_variant (type, exceptions);
4991 if (attrs)
4992 type = cp_build_type_attribute_variant (type, attrs);
4993 TREE_TYPE (clone) = type;
4995 clone_parms = NULL_TREE;
4996 break;
4999 gcc_assert (!clone_parms || clone_parms == void_list_node);
5003 /* For each of the constructors and destructors in T, create an
5004 in-charge and not-in-charge variant. */
5006 static void
5007 clone_constructors_and_destructors (tree t)
5009 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
5010 out now. */
5011 if (!CLASSTYPE_METHOD_VEC (t))
5012 return;
5014 /* While constructors can be via a using declaration, at this point
5015 we no longer need to know that. */
5016 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5017 clone_function_decl (*iter, /*update_methods=*/true);
5018 for (ovl_iterator iter (CLASSTYPE_DESTRUCTORS (t)); iter; ++iter)
5019 clone_function_decl (*iter, /*update_methods=*/true);
5022 /* Deduce noexcept for a destructor DTOR. */
5024 void
5025 deduce_noexcept_on_destructor (tree dtor)
5027 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
5028 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
5029 noexcept_deferred_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))
7191 vec_safe_push (keyed_classes, t);
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"