PR tree-optimization/81303
[official-gcc.git] / gcc / cp / class.c
blob73529a94a52377c74c1c06229aeff738f2aa4883
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 maybe_warn_about_overly_private_class (tree);
133 static void add_implicitly_declared_members (tree, tree*, int, int);
134 static tree fixed_type_or_null (tree, int *, int *);
135 static tree build_simple_base_path (tree expr, tree binfo);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static void build_vtbl_initializer (tree, tree, tree, tree, int *,
138 vec<constructor_elt, va_gc> **);
139 static bool check_bitfield_decl (tree);
140 static bool check_field_decl (tree, tree, int *, int *);
141 static void check_field_decls (tree, tree *, int *, int *);
142 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
143 static void build_base_fields (record_layout_info, splay_tree, tree *);
144 static void check_methods (tree);
145 static void remove_zero_width_bit_fields (tree);
146 static bool accessible_nvdtor_p (tree);
148 /* Used by find_flexarrays and related functions. */
149 struct flexmems_t;
150 static void diagnose_flexarrays (tree, const flexmems_t *);
151 static void find_flexarrays (tree, flexmems_t *, bool = false,
152 tree = NULL_TREE, tree = NULL_TREE);
153 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false);
154 static void check_bases (tree, int *, int *);
155 static void check_bases_and_members (tree);
156 static tree create_vtable_ptr (tree, tree *);
157 static void include_empty_classes (record_layout_info);
158 static void layout_class_type (tree, tree *);
159 static void propagate_binfo_offsets (tree, tree);
160 static void layout_virtual_bases (record_layout_info, splay_tree);
161 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
163 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
164 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
165 static void add_vcall_offset (tree, tree, vtbl_init_data *);
166 static void layout_vtable_decl (tree, int);
167 static tree dfs_find_final_overrider_pre (tree, void *);
168 static tree dfs_find_final_overrider_post (tree, void *);
169 static tree find_final_overrider (tree, tree, tree);
170 static int make_new_vtable (tree, tree);
171 static tree get_primary_binfo (tree);
172 static int maybe_indent_hierarchy (FILE *, int, int);
173 static tree dump_class_hierarchy_r (FILE *, dump_flags_t, tree, tree, int);
174 static void dump_class_hierarchy (tree);
175 static void dump_class_hierarchy_1 (FILE *, dump_flags_t, tree);
176 static void dump_array (FILE *, tree);
177 static void dump_vtable (tree, tree, tree);
178 static void dump_vtt (tree, tree);
179 static void dump_thunk (FILE *, int, tree);
180 static tree build_vtable (tree, tree, tree);
181 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *);
182 static void layout_nonempty_base_or_field (record_layout_info,
183 tree, tree, splay_tree);
184 static tree end_of_class (tree, int);
185 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
186 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree,
187 vec<constructor_elt, va_gc> **);
188 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree,
189 vec<constructor_elt, va_gc> **);
190 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
191 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
192 static void clone_constructors_and_destructors (tree);
193 static tree build_clone (tree, tree);
194 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
195 static void build_ctor_vtbl_group (tree, tree);
196 static void build_vtt (tree);
197 static tree binfo_ctor_vtable (tree);
198 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **,
199 tree *);
200 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
201 static tree dfs_fixup_binfo_vtbls (tree, void *);
202 static int record_subobject_offset (tree, tree, splay_tree);
203 static int check_subobject_offset (tree, tree, splay_tree);
204 static int walk_subobject_offsets (tree, subobject_offset_fn,
205 tree, splay_tree, tree, int);
206 static void record_subobject_offsets (tree, tree, splay_tree, bool);
207 static int layout_conflict_p (tree, tree, splay_tree, int);
208 static int splay_tree_compare_integer_csts (splay_tree_key k1,
209 splay_tree_key k2);
210 static void warn_about_ambiguous_bases (tree);
211 static bool type_requires_array_cookie (tree);
212 static bool base_derived_from (tree, tree);
213 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
214 static tree end_of_base (tree);
215 static tree get_vcall_index (tree, tree);
216 static bool type_maybe_constexpr_default_constructor (tree);
218 /* Variables shared between class.c and call.c. */
220 int n_vtables = 0;
221 int n_vtable_entries = 0;
222 int n_vtable_searches = 0;
223 int n_vtable_elems = 0;
224 int n_convert_harshness = 0;
225 int n_compute_conversion_costs = 0;
226 int n_inner_fields_searched = 0;
228 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
229 'structor is in charge of 'structing virtual bases, or FALSE_STMT
230 otherwise. */
232 tree
233 build_if_in_charge (tree true_stmt, tree false_stmt)
235 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
236 tree cmp = build2 (NE_EXPR, boolean_type_node,
237 current_in_charge_parm, integer_zero_node);
238 tree type = unlowered_expr_type (true_stmt);
239 if (VOID_TYPE_P (type))
240 type = unlowered_expr_type (false_stmt);
241 tree cond = build3 (COND_EXPR, type,
242 cmp, true_stmt, false_stmt);
243 return cond;
246 /* Convert to or from a base subobject. EXPR is an expression of type
247 `A' or `A*', an expression of type `B' or `B*' is returned. To
248 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
249 the B base instance within A. To convert base A to derived B, CODE
250 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
251 In this latter case, A must not be a morally virtual base of B.
252 NONNULL is true if EXPR is known to be non-NULL (this is only
253 needed when EXPR is of pointer type). CV qualifiers are preserved
254 from EXPR. */
256 tree
257 build_base_path (enum tree_code code,
258 tree expr,
259 tree binfo,
260 int nonnull,
261 tsubst_flags_t complain)
263 tree v_binfo = NULL_TREE;
264 tree d_binfo = NULL_TREE;
265 tree probe;
266 tree offset;
267 tree target_type;
268 tree null_test = NULL;
269 tree ptr_target_type;
270 int fixed_type_p;
271 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
272 bool has_empty = false;
273 bool virtual_access;
274 bool rvalue = false;
276 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
277 return error_mark_node;
279 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
281 d_binfo = probe;
282 if (is_empty_class (BINFO_TYPE (probe)))
283 has_empty = true;
284 if (!v_binfo && BINFO_VIRTUAL_P (probe))
285 v_binfo = probe;
288 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
289 if (want_pointer)
290 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
292 if (code == PLUS_EXPR
293 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
295 /* This can happen when adjust_result_of_qualified_name_lookup can't
296 find a unique base binfo in a call to a member function. We
297 couldn't give the diagnostic then since we might have been calling
298 a static member function, so we do it now. In other cases, eg.
299 during error recovery (c++/71979), we may not have a base at all. */
300 if (complain & tf_error)
302 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
303 ba_unique, NULL, complain);
304 gcc_assert (base == error_mark_node || !base);
306 return error_mark_node;
309 gcc_assert ((code == MINUS_EXPR
310 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
311 || code == PLUS_EXPR);
313 if (binfo == d_binfo)
314 /* Nothing to do. */
315 return expr;
317 if (code == MINUS_EXPR && v_binfo)
319 if (complain & tf_error)
321 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
323 if (want_pointer)
324 error ("cannot convert from pointer to base class %qT to "
325 "pointer to derived class %qT because the base is "
326 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
327 else
328 error ("cannot convert from base class %qT to derived "
329 "class %qT because the base is virtual",
330 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
332 else
334 if (want_pointer)
335 error ("cannot convert from pointer to base class %qT to "
336 "pointer to derived class %qT via virtual base %qT",
337 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
338 BINFO_TYPE (v_binfo));
339 else
340 error ("cannot convert from base class %qT to derived "
341 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
342 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
345 return error_mark_node;
348 if (!want_pointer)
350 rvalue = !lvalue_p (expr);
351 /* This must happen before the call to save_expr. */
352 expr = cp_build_addr_expr (expr, complain);
354 else
355 expr = mark_rvalue_use (expr);
357 offset = BINFO_OFFSET (binfo);
358 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
359 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
360 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
361 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
362 expression returned matches the input. */
363 target_type = cp_build_qualified_type
364 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
365 ptr_target_type = build_pointer_type (target_type);
367 /* Do we need to look in the vtable for the real offset? */
368 virtual_access = (v_binfo && fixed_type_p <= 0);
370 /* Don't bother with the calculations inside sizeof; they'll ICE if the
371 source type is incomplete and the pointer value doesn't matter. In a
372 template (even in instantiate_non_dependent_expr), we don't have vtables
373 set up properly yet, and the value doesn't matter there either; we're
374 just interested in the result of overload resolution. */
375 if (cp_unevaluated_operand != 0
376 || processing_template_decl
377 || in_template_function ())
379 expr = build_nop (ptr_target_type, expr);
380 goto indout;
383 /* If we're in an NSDMI, we don't have the full constructor context yet
384 that we need for converting to a virtual base, so just build a stub
385 CONVERT_EXPR and expand it later in bot_replace. */
386 if (virtual_access && fixed_type_p < 0
387 && current_scope () != current_function_decl)
389 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
390 CONVERT_EXPR_VBASE_PATH (expr) = true;
391 goto indout;
394 /* Do we need to check for a null pointer? */
395 if (want_pointer && !nonnull)
397 /* If we know the conversion will not actually change the value
398 of EXPR, then we can avoid testing the expression for NULL.
399 We have to avoid generating a COMPONENT_REF for a base class
400 field, because other parts of the compiler know that such
401 expressions are always non-NULL. */
402 if (!virtual_access && integer_zerop (offset))
403 return build_nop (ptr_target_type, expr);
404 null_test = error_mark_node;
407 /* Protect against multiple evaluation if necessary. */
408 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
409 expr = save_expr (expr);
411 /* Now that we've saved expr, build the real null test. */
412 if (null_test)
414 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
415 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
416 expr, zero);
417 /* This is a compiler generated comparison, don't emit
418 e.g. -Wnonnull-compare warning for it. */
419 TREE_NO_WARNING (null_test) = 1;
422 /* If this is a simple base reference, express it as a COMPONENT_REF. */
423 if (code == PLUS_EXPR && !virtual_access
424 /* We don't build base fields for empty bases, and they aren't very
425 interesting to the optimizers anyway. */
426 && !has_empty)
428 expr = cp_build_fold_indirect_ref (expr);
429 expr = build_simple_base_path (expr, binfo);
430 if (rvalue)
431 expr = move (expr);
432 if (want_pointer)
433 expr = build_address (expr);
434 target_type = TREE_TYPE (expr);
435 goto out;
438 if (virtual_access)
440 /* Going via virtual base V_BINFO. We need the static offset
441 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
442 V_BINFO. That offset is an entry in D_BINFO's vtable. */
443 tree v_offset;
445 if (fixed_type_p < 0 && in_base_initializer)
447 /* In a base member initializer, we cannot rely on the
448 vtable being set up. We have to indirect via the
449 vtt_parm. */
450 tree t;
452 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
453 t = build_pointer_type (t);
454 v_offset = fold_convert (t, current_vtt_parm);
455 v_offset = cp_build_fold_indirect_ref (v_offset);
457 else
459 tree t = expr;
460 if (sanitize_flags_p (SANITIZE_VPTR)
461 && fixed_type_p == 0)
463 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
464 probe, expr);
465 if (t == NULL_TREE)
466 t = expr;
468 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
469 TREE_TYPE (TREE_TYPE (expr)));
472 if (v_offset == error_mark_node)
473 return error_mark_node;
475 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
476 v_offset = build1 (NOP_EXPR,
477 build_pointer_type (ptrdiff_type_node),
478 v_offset);
479 v_offset = cp_build_fold_indirect_ref (v_offset);
480 TREE_CONSTANT (v_offset) = 1;
482 offset = convert_to_integer (ptrdiff_type_node,
483 size_diffop_loc (input_location, offset,
484 BINFO_OFFSET (v_binfo)));
486 if (!integer_zerop (offset))
487 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
489 if (fixed_type_p < 0)
490 /* Negative fixed_type_p means this is a constructor or destructor;
491 virtual base layout is fixed in in-charge [cd]tors, but not in
492 base [cd]tors. */
493 offset = build_if_in_charge
494 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
495 v_offset);
496 else
497 offset = v_offset;
500 if (want_pointer)
501 target_type = ptr_target_type;
503 expr = build1 (NOP_EXPR, ptr_target_type, expr);
505 if (!integer_zerop (offset))
507 offset = fold_convert (sizetype, offset);
508 if (code == MINUS_EXPR)
509 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
510 expr = fold_build_pointer_plus (expr, offset);
512 else
513 null_test = NULL;
515 indout:
516 if (!want_pointer)
518 expr = cp_build_fold_indirect_ref (expr);
519 if (rvalue)
520 expr = move (expr);
523 out:
524 if (null_test)
525 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
526 build_zero_cst (target_type));
528 return expr;
531 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
532 Perform a derived-to-base conversion by recursively building up a
533 sequence of COMPONENT_REFs to the appropriate base fields. */
535 static tree
536 build_simple_base_path (tree expr, tree binfo)
538 tree type = BINFO_TYPE (binfo);
539 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
540 tree field;
542 if (d_binfo == NULL_TREE)
544 tree temp;
546 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
548 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
549 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
550 an lvalue in the front end; only _DECLs and _REFs are lvalues
551 in the back end. */
552 temp = unary_complex_lvalue (ADDR_EXPR, expr);
553 if (temp)
554 expr = cp_build_fold_indirect_ref (temp);
556 return expr;
559 /* Recurse. */
560 expr = build_simple_base_path (expr, d_binfo);
562 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
563 field; field = DECL_CHAIN (field))
564 /* Is this the base field created by build_base_field? */
565 if (TREE_CODE (field) == FIELD_DECL
566 && DECL_FIELD_IS_BASE (field)
567 && TREE_TYPE (field) == type
568 /* If we're looking for a field in the most-derived class,
569 also check the field offset; we can have two base fields
570 of the same type if one is an indirect virtual base and one
571 is a direct non-virtual base. */
572 && (BINFO_INHERITANCE_CHAIN (d_binfo)
573 || tree_int_cst_equal (byte_position (field),
574 BINFO_OFFSET (binfo))))
576 /* We don't use build_class_member_access_expr here, as that
577 has unnecessary checks, and more importantly results in
578 recursive calls to dfs_walk_once. */
579 int type_quals = cp_type_quals (TREE_TYPE (expr));
581 expr = build3 (COMPONENT_REF,
582 cp_build_qualified_type (type, type_quals),
583 expr, field, NULL_TREE);
584 /* Mark the expression const or volatile, as appropriate.
585 Even though we've dealt with the type above, we still have
586 to mark the expression itself. */
587 if (type_quals & TYPE_QUAL_CONST)
588 TREE_READONLY (expr) = 1;
589 if (type_quals & TYPE_QUAL_VOLATILE)
590 TREE_THIS_VOLATILE (expr) = 1;
592 return expr;
595 /* Didn't find the base field?!? */
596 gcc_unreachable ();
599 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
600 type is a class type or a pointer to a class type. In the former
601 case, TYPE is also a class type; in the latter it is another
602 pointer type. If CHECK_ACCESS is true, an error message is emitted
603 if TYPE is inaccessible. If OBJECT has pointer type, the value is
604 assumed to be non-NULL. */
606 tree
607 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
608 tsubst_flags_t complain)
610 tree binfo;
611 tree object_type;
613 if (TYPE_PTR_P (TREE_TYPE (object)))
615 object_type = TREE_TYPE (TREE_TYPE (object));
616 type = TREE_TYPE (type);
618 else
619 object_type = TREE_TYPE (object);
621 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
622 NULL, complain);
623 if (!binfo || binfo == error_mark_node)
624 return error_mark_node;
626 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
629 /* EXPR is an expression with unqualified class type. BASE is a base
630 binfo of that class type. Returns EXPR, converted to the BASE
631 type. This function assumes that EXPR is the most derived class;
632 therefore virtual bases can be found at their static offsets. */
634 tree
635 convert_to_base_statically (tree expr, tree base)
637 tree expr_type;
639 expr_type = TREE_TYPE (expr);
640 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
642 /* If this is a non-empty base, use a COMPONENT_REF. */
643 if (!is_empty_class (BINFO_TYPE (base)))
644 return build_simple_base_path (expr, base);
646 /* We use fold_build2 and fold_convert below to simplify the trees
647 provided to the optimizers. It is not safe to call these functions
648 when processing a template because they do not handle C++-specific
649 trees. */
650 gcc_assert (!processing_template_decl);
651 expr = cp_build_addr_expr (expr, tf_warning_or_error);
652 if (!integer_zerop (BINFO_OFFSET (base)))
653 expr = fold_build_pointer_plus_loc (input_location,
654 expr, BINFO_OFFSET (base));
655 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
656 expr = build_fold_indirect_ref_loc (input_location, expr);
659 return expr;
663 tree
664 build_vfield_ref (tree datum, tree type)
666 tree vfield, vcontext;
668 if (datum == error_mark_node
669 /* Can happen in case of duplicate base types (c++/59082). */
670 || !TYPE_VFIELD (type))
671 return error_mark_node;
673 /* First, convert to the requested type. */
674 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
675 datum = convert_to_base (datum, type, /*check_access=*/false,
676 /*nonnull=*/true, tf_warning_or_error);
678 /* Second, the requested type may not be the owner of its own vptr.
679 If not, convert to the base class that owns it. We cannot use
680 convert_to_base here, because VCONTEXT may appear more than once
681 in the inheritance hierarchy of TYPE, and thus direct conversion
682 between the types may be ambiguous. Following the path back up
683 one step at a time via primary bases avoids the problem. */
684 vfield = TYPE_VFIELD (type);
685 vcontext = DECL_CONTEXT (vfield);
686 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
688 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
689 type = TREE_TYPE (datum);
692 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
695 /* Given an object INSTANCE, return an expression which yields the
696 vtable element corresponding to INDEX. There are many special
697 cases for INSTANCE which we take care of here, mainly to avoid
698 creating extra tree nodes when we don't have to. */
700 static tree
701 build_vtbl_ref_1 (tree instance, tree idx)
703 tree aref;
704 tree vtbl = NULL_TREE;
706 /* Try to figure out what a reference refers to, and
707 access its virtual function table directly. */
709 int cdtorp = 0;
710 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
712 tree basetype = non_reference (TREE_TYPE (instance));
714 if (fixed_type && !cdtorp)
716 tree binfo = lookup_base (fixed_type, basetype,
717 ba_unique, NULL, tf_none);
718 if (binfo && binfo != error_mark_node)
719 vtbl = unshare_expr (BINFO_VTABLE (binfo));
722 if (!vtbl)
723 vtbl = build_vfield_ref (instance, basetype);
725 aref = build_array_ref (input_location, vtbl, idx);
726 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
728 return aref;
731 tree
732 build_vtbl_ref (tree instance, tree idx)
734 tree aref = build_vtbl_ref_1 (instance, idx);
736 return aref;
739 /* Given a stable object pointer INSTANCE_PTR, return an expression which
740 yields a function pointer corresponding to vtable element INDEX. */
742 tree
743 build_vfn_ref (tree instance_ptr, tree idx)
745 tree aref;
747 aref = build_vtbl_ref_1 (cp_build_fold_indirect_ref (instance_ptr),
748 idx);
750 /* When using function descriptors, the address of the
751 vtable entry is treated as a function pointer. */
752 if (TARGET_VTABLE_USES_DESCRIPTORS)
753 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
754 cp_build_addr_expr (aref, tf_warning_or_error));
756 /* Remember this as a method reference, for later devirtualization. */
757 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
759 return aref;
762 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
763 for the given TYPE. */
765 static tree
766 get_vtable_name (tree type)
768 return mangle_vtbl_for_type (type);
771 /* DECL is an entity associated with TYPE, like a virtual table or an
772 implicitly generated constructor. Determine whether or not DECL
773 should have external or internal linkage at the object file
774 level. This routine does not deal with COMDAT linkage and other
775 similar complexities; it simply sets TREE_PUBLIC if it possible for
776 entities in other translation units to contain copies of DECL, in
777 the abstract. */
779 void
780 set_linkage_according_to_type (tree /*type*/, tree decl)
782 TREE_PUBLIC (decl) = 1;
783 determine_visibility (decl);
786 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
787 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
788 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
790 static tree
791 build_vtable (tree class_type, tree name, tree vtable_type)
793 tree decl;
795 decl = build_lang_decl (VAR_DECL, name, vtable_type);
796 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
797 now to avoid confusion in mangle_decl. */
798 SET_DECL_ASSEMBLER_NAME (decl, name);
799 DECL_CONTEXT (decl) = class_type;
800 DECL_ARTIFICIAL (decl) = 1;
801 TREE_STATIC (decl) = 1;
802 TREE_READONLY (decl) = 1;
803 DECL_VIRTUAL_P (decl) = 1;
804 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
805 DECL_USER_ALIGN (decl) = true;
806 DECL_VTABLE_OR_VTT_P (decl) = 1;
807 set_linkage_according_to_type (class_type, decl);
808 /* The vtable has not been defined -- yet. */
809 DECL_EXTERNAL (decl) = 1;
810 DECL_NOT_REALLY_EXTERN (decl) = 1;
812 /* Mark the VAR_DECL node representing the vtable itself as a
813 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
814 is rather important that such things be ignored because any
815 effort to actually generate DWARF for them will run into
816 trouble when/if we encounter code like:
818 #pragma interface
819 struct S { virtual void member (); };
821 because the artificial declaration of the vtable itself (as
822 manufactured by the g++ front end) will say that the vtable is
823 a static member of `S' but only *after* the debug output for
824 the definition of `S' has already been output. This causes
825 grief because the DWARF entry for the definition of the vtable
826 will try to refer back to an earlier *declaration* of the
827 vtable as a static member of `S' and there won't be one. We
828 might be able to arrange to have the "vtable static member"
829 attached to the member list for `S' before the debug info for
830 `S' get written (which would solve the problem) but that would
831 require more intrusive changes to the g++ front end. */
832 DECL_IGNORED_P (decl) = 1;
834 return decl;
837 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
838 or even complete. If this does not exist, create it. If COMPLETE is
839 nonzero, then complete the definition of it -- that will render it
840 impossible to actually build the vtable, but is useful to get at those
841 which are known to exist in the runtime. */
843 tree
844 get_vtable_decl (tree type, int complete)
846 tree decl;
848 if (CLASSTYPE_VTABLES (type))
849 return CLASSTYPE_VTABLES (type);
851 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
852 CLASSTYPE_VTABLES (type) = decl;
854 if (complete)
856 DECL_EXTERNAL (decl) = 1;
857 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
860 return decl;
863 /* Build the primary virtual function table for TYPE. If BINFO is
864 non-NULL, build the vtable starting with the initial approximation
865 that it is the same as the one which is the head of the association
866 list. Returns a nonzero value if a new vtable is actually
867 created. */
869 static int
870 build_primary_vtable (tree binfo, tree type)
872 tree decl;
873 tree virtuals;
875 decl = get_vtable_decl (type, /*complete=*/0);
877 if (binfo)
879 if (BINFO_NEW_VTABLE_MARKED (binfo))
880 /* We have already created a vtable for this base, so there's
881 no need to do it again. */
882 return 0;
884 virtuals = copy_list (BINFO_VIRTUALS (binfo));
885 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
886 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
887 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
889 else
891 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
892 virtuals = NULL_TREE;
895 if (GATHER_STATISTICS)
897 n_vtables += 1;
898 n_vtable_elems += list_length (virtuals);
901 /* Initialize the association list for this type, based
902 on our first approximation. */
903 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
904 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
905 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
906 return 1;
909 /* Give BINFO a new virtual function table which is initialized
910 with a skeleton-copy of its original initialization. The only
911 entry that changes is the `delta' entry, so we can really
912 share a lot of structure.
914 FOR_TYPE is the most derived type which caused this table to
915 be needed.
917 Returns nonzero if we haven't met BINFO before.
919 The order in which vtables are built (by calling this function) for
920 an object must remain the same, otherwise a binary incompatibility
921 can result. */
923 static int
924 build_secondary_vtable (tree binfo)
926 if (BINFO_NEW_VTABLE_MARKED (binfo))
927 /* We already created a vtable for this base. There's no need to
928 do it again. */
929 return 0;
931 /* Remember that we've created a vtable for this BINFO, so that we
932 don't try to do so again. */
933 SET_BINFO_NEW_VTABLE_MARKED (binfo);
935 /* Make fresh virtual list, so we can smash it later. */
936 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
938 /* Secondary vtables are laid out as part of the same structure as
939 the primary vtable. */
940 BINFO_VTABLE (binfo) = NULL_TREE;
941 return 1;
944 /* Create a new vtable for BINFO which is the hierarchy dominated by
945 T. Return nonzero if we actually created a new vtable. */
947 static int
948 make_new_vtable (tree t, tree binfo)
950 if (binfo == TYPE_BINFO (t))
951 /* In this case, it is *type*'s vtable we are modifying. We start
952 with the approximation that its vtable is that of the
953 immediate base class. */
954 return build_primary_vtable (binfo, t);
955 else
956 /* This is our very own copy of `basetype' to play with. Later,
957 we will fill in all the virtual functions that override the
958 virtual functions in these base classes which are not defined
959 by the current type. */
960 return build_secondary_vtable (binfo);
963 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
964 (which is in the hierarchy dominated by T) list FNDECL as its
965 BV_FN. DELTA is the required constant adjustment from the `this'
966 pointer where the vtable entry appears to the `this' required when
967 the function is actually called. */
969 static void
970 modify_vtable_entry (tree t,
971 tree binfo,
972 tree fndecl,
973 tree delta,
974 tree *virtuals)
976 tree v;
978 v = *virtuals;
980 if (fndecl != BV_FN (v)
981 || !tree_int_cst_equal (delta, BV_DELTA (v)))
983 /* We need a new vtable for BINFO. */
984 if (make_new_vtable (t, binfo))
986 /* If we really did make a new vtable, we also made a copy
987 of the BINFO_VIRTUALS list. Now, we have to find the
988 corresponding entry in that list. */
989 *virtuals = BINFO_VIRTUALS (binfo);
990 while (BV_FN (*virtuals) != BV_FN (v))
991 *virtuals = TREE_CHAIN (*virtuals);
992 v = *virtuals;
995 BV_DELTA (v) = delta;
996 BV_VCALL_INDEX (v) = NULL_TREE;
997 BV_FN (v) = fndecl;
1002 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
1003 METHOD is being injected via a using_decl. Returns true if the
1004 method could be added to the method vec. */
1006 bool
1007 add_method (tree type, tree method, bool via_using)
1009 if (method == error_mark_node)
1010 return false;
1012 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1013 grok_special_member_properties (method);
1015 tree *slot = get_member_slot (type, DECL_NAME (method));
1016 tree current_fns = *slot;
1018 gcc_assert (!DECL_EXTERN_C_P (method));
1020 /* Check to see if we've already got this method. */
1021 for (ovl_iterator iter (current_fns); iter; ++iter)
1023 tree fn = *iter;
1024 tree fn_type;
1025 tree method_type;
1026 tree parms1;
1027 tree parms2;
1029 if (TREE_CODE (fn) != TREE_CODE (method))
1030 continue;
1032 /* Two using-declarations can coexist, we'll complain about ambiguity in
1033 overload resolution. */
1034 if (via_using && iter.using_p ()
1035 /* Except handle inherited constructors specially. */
1036 && ! DECL_CONSTRUCTOR_P (fn))
1037 continue;
1039 /* [over.load] Member function declarations with the
1040 same name and the same parameter types cannot be
1041 overloaded if any of them is a static member
1042 function declaration.
1044 [over.load] Member function declarations with the same name and
1045 the same parameter-type-list as well as member function template
1046 declarations with the same name, the same parameter-type-list, and
1047 the same template parameter lists cannot be overloaded if any of
1048 them, but not all, have a ref-qualifier.
1050 [namespace.udecl] When a using-declaration brings names
1051 from a base class into a derived class scope, member
1052 functions in the derived class override and/or hide member
1053 functions with the same name and parameter types in a base
1054 class (rather than conflicting). */
1055 fn_type = TREE_TYPE (fn);
1056 method_type = TREE_TYPE (method);
1057 parms1 = TYPE_ARG_TYPES (fn_type);
1058 parms2 = TYPE_ARG_TYPES (method_type);
1060 /* Compare the quals on the 'this' parm. Don't compare
1061 the whole types, as used functions are treated as
1062 coming from the using class in overload resolution. */
1063 if (! DECL_STATIC_FUNCTION_P (fn)
1064 && ! DECL_STATIC_FUNCTION_P (method)
1065 /* Either both or neither need to be ref-qualified for
1066 differing quals to allow overloading. */
1067 && (FUNCTION_REF_QUALIFIED (fn_type)
1068 == FUNCTION_REF_QUALIFIED (method_type))
1069 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1070 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1071 continue;
1073 /* For templates, the return type and template parameters
1074 must be identical. */
1075 if (TREE_CODE (fn) == TEMPLATE_DECL
1076 && (!same_type_p (TREE_TYPE (fn_type),
1077 TREE_TYPE (method_type))
1078 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1079 DECL_TEMPLATE_PARMS (method))))
1080 continue;
1082 if (! DECL_STATIC_FUNCTION_P (fn))
1083 parms1 = TREE_CHAIN (parms1);
1084 if (! DECL_STATIC_FUNCTION_P (method))
1085 parms2 = TREE_CHAIN (parms2);
1087 /* Bring back parameters omitted from an inherited ctor. */
1088 if (ctor_omit_inherited_parms (fn))
1089 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1090 if (ctor_omit_inherited_parms (method))
1091 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1093 if (compparms (parms1, parms2)
1094 && (!DECL_CONV_FN_P (fn)
1095 || same_type_p (TREE_TYPE (fn_type),
1096 TREE_TYPE (method_type)))
1097 && equivalently_constrained (fn, method))
1099 /* If these are versions of the same function, process and
1100 move on. */
1101 if (TREE_CODE (fn) == FUNCTION_DECL
1102 && maybe_version_functions (method, fn))
1103 continue;
1105 if (DECL_INHERITED_CTOR (method))
1107 if (DECL_INHERITED_CTOR (fn))
1109 tree basem = DECL_INHERITED_CTOR_BASE (method);
1110 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1111 if (flag_new_inheriting_ctors)
1113 if (basem == basef)
1115 /* Inheriting the same constructor along different
1116 paths, combine them. */
1117 SET_DECL_INHERITED_CTOR
1118 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1119 DECL_INHERITED_CTOR (fn)));
1120 /* And discard the new one. */
1121 return false;
1123 else
1124 /* Inherited ctors can coexist until overload
1125 resolution. */
1126 continue;
1128 error_at (DECL_SOURCE_LOCATION (method),
1129 "%q#D conflicts with version inherited from %qT",
1130 method, basef);
1131 inform (DECL_SOURCE_LOCATION (fn),
1132 "version inherited from %qT declared here",
1133 basef);
1135 /* Otherwise defer to the other function. */
1136 return false;
1139 if (via_using)
1140 /* Defer to the local function. */
1141 return false;
1142 else if (flag_new_inheriting_ctors
1143 && DECL_INHERITED_CTOR (fn))
1145 /* Remove the inherited constructor. */
1146 current_fns = iter.remove_node (current_fns);
1147 continue;
1149 else
1151 error_at (DECL_SOURCE_LOCATION (method),
1152 "%q#D cannot be overloaded with %q#D", method, fn);
1153 inform (DECL_SOURCE_LOCATION (fn),
1154 "previous declaration %q#D", fn);
1155 return false;
1160 /* A class should never have more than one destructor. */
1161 gcc_assert (!current_fns || !DECL_DESTRUCTOR_P (method));
1163 current_fns = ovl_insert (method, current_fns, via_using);
1165 if (!DECL_CONV_FN_P (method) && !COMPLETE_TYPE_P (type))
1166 push_class_level_binding (DECL_NAME (method), current_fns);
1168 *slot = current_fns;
1170 return true;
1173 /* Subroutines of finish_struct. */
1175 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1176 legit, otherwise return 0. */
1178 static int
1179 alter_access (tree t, tree fdecl, tree access)
1181 tree elem;
1183 retrofit_lang_decl (fdecl);
1185 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
1187 elem = purpose_member (t, DECL_ACCESS (fdecl));
1188 if (elem)
1190 if (TREE_VALUE (elem) != access)
1192 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1193 error ("conflicting access specifications for method"
1194 " %q+D, ignored", TREE_TYPE (fdecl));
1195 else
1196 error ("conflicting access specifications for field %qE, ignored",
1197 DECL_NAME (fdecl));
1199 else
1201 /* They're changing the access to the same thing they changed
1202 it to before. That's OK. */
1206 else
1208 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl,
1209 tf_warning_or_error);
1210 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1211 return 1;
1213 return 0;
1216 /* Return the access node for DECL's access in its enclosing class. */
1218 tree
1219 declared_access (tree decl)
1221 return (TREE_PRIVATE (decl) ? access_private_node
1222 : TREE_PROTECTED (decl) ? access_protected_node
1223 : access_public_node);
1226 /* Process the USING_DECL, which is a member of T. */
1228 static void
1229 handle_using_decl (tree using_decl, tree t)
1231 tree decl = USING_DECL_DECLS (using_decl);
1232 tree name = DECL_NAME (using_decl);
1233 tree access = declared_access (using_decl);
1234 tree flist = NULL_TREE;
1235 tree old_value;
1237 gcc_assert (!processing_template_decl && decl);
1239 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false,
1240 tf_warning_or_error);
1241 if (old_value)
1243 old_value = OVL_FIRST (old_value);
1245 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1246 /* OK */;
1247 else
1248 old_value = NULL_TREE;
1251 cp_emit_debug_info_for_using (decl, t);
1253 if (is_overloaded_fn (decl))
1254 flist = decl;
1256 if (! old_value)
1258 else if (is_overloaded_fn (old_value))
1260 if (flist)
1261 /* It's OK to use functions from a base when there are functions with
1262 the same name already present in the current class. */;
1263 else
1265 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1266 "because of local method %q#D with same name",
1267 using_decl, t, old_value);
1268 inform (DECL_SOURCE_LOCATION (old_value),
1269 "local method %q#D declared here", old_value);
1270 return;
1273 else if (!DECL_ARTIFICIAL (old_value))
1275 error_at (DECL_SOURCE_LOCATION (using_decl), "%qD invalid in %q#T "
1276 "because of local member %q#D with same name",
1277 using_decl, t, old_value);
1278 inform (DECL_SOURCE_LOCATION (old_value),
1279 "local member %q#D declared here", old_value);
1280 return;
1283 /* Make type T see field decl FDECL with access ACCESS. */
1284 if (flist)
1285 for (ovl_iterator iter (flist); iter; ++iter)
1287 add_method (t, *iter, true);
1288 alter_access (t, *iter, access);
1290 else
1291 alter_access (t, decl, access);
1294 /* Data structure for find_abi_tags_r, below. */
1296 struct abi_tag_data
1298 tree t; // The type that we're checking for missing tags.
1299 tree subob; // The subobject of T that we're getting tags from.
1300 tree tags; // error_mark_node for diagnostics, or a list of missing tags.
1303 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP
1304 in the context of P. TAG can be either an identifier (the DECL_NAME of
1305 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */
1307 static void
1308 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p)
1310 if (!IDENTIFIER_MARKED (id))
1312 if (p->tags != error_mark_node)
1314 /* We're collecting tags from template arguments or from
1315 the type of a variable or function return type. */
1316 p->tags = tree_cons (NULL_TREE, tag, p->tags);
1318 /* Don't inherit this tag multiple times. */
1319 IDENTIFIER_MARKED (id) = true;
1321 if (TYPE_P (p->t))
1323 /* Tags inherited from type template arguments are only used
1324 to avoid warnings. */
1325 ABI_TAG_IMPLICIT (p->tags) = true;
1326 return;
1328 /* For functions and variables we want to warn, too. */
1331 /* Otherwise we're diagnosing missing tags. */
1332 if (TREE_CODE (p->t) == FUNCTION_DECL)
1334 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1335 "that %qT (used in its return type) has",
1336 p->t, tag, *tp))
1337 inform (location_of (*tp), "%qT declared here", *tp);
1339 else if (VAR_P (p->t))
1341 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag "
1342 "that %qT (used in its type) has", p->t, tag, *tp))
1343 inform (location_of (*tp), "%qT declared here", *tp);
1345 else if (TYPE_P (p->subob))
1347 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1348 "that base %qT has", p->t, tag, p->subob))
1349 inform (location_of (p->subob), "%qT declared here",
1350 p->subob);
1352 else
1354 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag "
1355 "that %qT (used in the type of %qD) has",
1356 p->t, tag, *tp, p->subob))
1358 inform (location_of (p->subob), "%qD declared here",
1359 p->subob);
1360 inform (location_of (*tp), "%qT declared here", *tp);
1366 /* Find all the ABI tags in the attribute list ATTR and either call
1367 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1369 static void
1370 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val)
1372 if (!attr)
1373 return;
1374 for (; (attr = lookup_attribute ("abi_tag", attr));
1375 attr = TREE_CHAIN (attr))
1376 for (tree list = TREE_VALUE (attr); list;
1377 list = TREE_CHAIN (list))
1379 tree tag = TREE_VALUE (list);
1380 tree id = get_identifier (TREE_STRING_POINTER (tag));
1381 if (tp)
1382 check_tag (tag, id, tp, p);
1383 else
1384 IDENTIFIER_MARKED (id) = val;
1388 /* Find all the ABI tags on T and its enclosing scopes and either call
1389 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */
1391 static void
1392 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val)
1394 while (t != global_namespace)
1396 tree attr;
1397 if (TYPE_P (t))
1399 attr = TYPE_ATTRIBUTES (t);
1400 t = CP_TYPE_CONTEXT (t);
1402 else
1404 attr = DECL_ATTRIBUTES (t);
1405 t = CP_DECL_CONTEXT (t);
1407 mark_or_check_attr_tags (attr, tp, p, val);
1411 /* walk_tree callback for check_abi_tags: if the type at *TP involves any
1412 types with ABI tags, add the corresponding identifiers to the VEC in
1413 *DATA and set IDENTIFIER_MARKED. */
1415 static tree
1416 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1418 if (!OVERLOAD_TYPE_P (*tp))
1419 return NULL_TREE;
1421 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1422 anyway, but let's make sure of it. */
1423 *walk_subtrees = false;
1425 abi_tag_data *p = static_cast<struct abi_tag_data*>(data);
1427 mark_or_check_tags (*tp, tp, p, false);
1429 return NULL_TREE;
1432 /* walk_tree callback for mark_abi_tags: if *TP is a class, set
1433 IDENTIFIER_MARKED on its ABI tags. */
1435 static tree
1436 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data)
1438 if (!OVERLOAD_TYPE_P (*tp))
1439 return NULL_TREE;
1441 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE
1442 anyway, but let's make sure of it. */
1443 *walk_subtrees = false;
1445 bool *valp = static_cast<bool*>(data);
1447 mark_or_check_tags (*tp, NULL, NULL, *valp);
1449 return NULL_TREE;
1452 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing
1453 scopes. */
1455 static void
1456 mark_abi_tags (tree t, bool val)
1458 mark_or_check_tags (t, NULL, NULL, val);
1459 if (DECL_P (t))
1461 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t)
1462 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
1464 /* Template arguments are part of the signature. */
1465 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t));
1466 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1468 tree arg = TREE_VEC_ELT (level, j);
1469 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val);
1472 if (TREE_CODE (t) == FUNCTION_DECL)
1473 /* A function's parameter types are part of the signature, so
1474 we don't need to inherit any tags that are also in them. */
1475 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg;
1476 arg = TREE_CHAIN (arg))
1477 cp_walk_tree_without_duplicates (&TREE_VALUE (arg),
1478 mark_abi_tags_r, &val);
1482 /* Check that T has all the ABI tags that subobject SUBOB has, or
1483 warn if not. If T is a (variable or function) declaration, also
1484 return any missing tags, and add them to T if JUST_CHECKING is false. */
1486 static tree
1487 check_abi_tags (tree t, tree subob, bool just_checking = false)
1489 bool inherit = DECL_P (t);
1491 if (!inherit && !warn_abi_tag)
1492 return NULL_TREE;
1494 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t;
1495 if (!TREE_PUBLIC (decl))
1496 /* No need to worry about things local to this TU. */
1497 return NULL_TREE;
1499 mark_abi_tags (t, true);
1501 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob);
1502 struct abi_tag_data data = { t, subob, error_mark_node };
1503 if (inherit)
1504 data.tags = NULL_TREE;
1506 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data);
1508 if (!(inherit && data.tags))
1509 /* We don't need to do anything with data.tags. */;
1510 else if (just_checking)
1511 for (tree t = data.tags; t; t = TREE_CHAIN (t))
1513 tree id = get_identifier (TREE_STRING_POINTER (TREE_VALUE (t)));
1514 IDENTIFIER_MARKED (id) = false;
1516 else
1518 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t));
1519 if (attr)
1520 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1521 else
1522 DECL_ATTRIBUTES (t)
1523 = tree_cons (get_identifier ("abi_tag"), data.tags,
1524 DECL_ATTRIBUTES (t));
1527 mark_abi_tags (t, false);
1529 return data.tags;
1532 /* Check that DECL has all the ABI tags that are used in parts of its type
1533 that are not reflected in its mangled name. */
1535 void
1536 check_abi_tags (tree decl)
1538 if (VAR_P (decl))
1539 check_abi_tags (decl, TREE_TYPE (decl));
1540 else if (TREE_CODE (decl) == FUNCTION_DECL
1541 && !DECL_CONV_FN_P (decl)
1542 && !mangle_return_type_p (decl))
1543 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1546 /* Return any ABI tags that are used in parts of the type of DECL
1547 that are not reflected in its mangled name. This function is only
1548 used in backward-compatible mangling for ABI <11. */
1550 tree
1551 missing_abi_tags (tree decl)
1553 if (VAR_P (decl))
1554 return check_abi_tags (decl, TREE_TYPE (decl), true);
1555 else if (TREE_CODE (decl) == FUNCTION_DECL
1556 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1557 that we can use this function for setting need_abi_warning
1558 regardless of the current flag_abi_version. */
1559 && !mangle_return_type_p (decl))
1560 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1561 else
1562 return NULL_TREE;
1565 void
1566 inherit_targ_abi_tags (tree t)
1568 if (!CLASS_TYPE_P (t)
1569 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1570 return;
1572 mark_abi_tags (t, true);
1574 tree args = CLASSTYPE_TI_ARGS (t);
1575 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1576 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1578 tree level = TMPL_ARGS_LEVEL (args, i+1);
1579 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1581 tree arg = TREE_VEC_ELT (level, j);
1582 data.subob = arg;
1583 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1587 // If we found some tags on our template arguments, add them to our
1588 // abi_tag attribute.
1589 if (data.tags)
1591 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1592 if (attr)
1593 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1594 else
1595 TYPE_ATTRIBUTES (t)
1596 = tree_cons (get_identifier ("abi_tag"), data.tags,
1597 TYPE_ATTRIBUTES (t));
1600 mark_abi_tags (t, false);
1603 /* Return true, iff class T has a non-virtual destructor that is
1604 accessible from outside the class heirarchy (i.e. is public, or
1605 there's a suitable friend. */
1607 static bool
1608 accessible_nvdtor_p (tree t)
1610 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1612 /* An implicitly declared destructor is always public. And,
1613 if it were virtual, we would have created it by now. */
1614 if (!dtor)
1615 return true;
1617 if (DECL_VINDEX (dtor))
1618 return false; /* Virtual */
1620 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1621 return true; /* Public */
1623 if (CLASSTYPE_FRIEND_CLASSES (t)
1624 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1625 return true; /* Has friends */
1627 return false;
1630 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1631 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1632 properties of the bases. */
1634 static void
1635 check_bases (tree t,
1636 int* cant_have_const_ctor_p,
1637 int* no_const_asn_ref_p)
1639 int i;
1640 bool seen_non_virtual_nearly_empty_base_p = 0;
1641 int seen_tm_mask = 0;
1642 tree base_binfo;
1643 tree binfo;
1644 tree field = NULL_TREE;
1646 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1647 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1648 if (TREE_CODE (field) == FIELD_DECL)
1649 break;
1651 for (binfo = TYPE_BINFO (t), i = 0;
1652 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1654 tree basetype = TREE_TYPE (base_binfo);
1656 gcc_assert (COMPLETE_TYPE_P (basetype));
1658 if (CLASSTYPE_FINAL (basetype))
1659 error ("cannot derive from %<final%> base %qT in derived type %qT",
1660 basetype, t);
1662 /* If any base class is non-literal, so is the derived class. */
1663 if (!CLASSTYPE_LITERAL_P (basetype))
1664 CLASSTYPE_LITERAL_P (t) = false;
1666 /* If the base class doesn't have copy constructors or
1667 assignment operators that take const references, then the
1668 derived class cannot have such a member automatically
1669 generated. */
1670 if (TYPE_HAS_COPY_CTOR (basetype)
1671 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1672 *cant_have_const_ctor_p = 1;
1673 if (TYPE_HAS_COPY_ASSIGN (basetype)
1674 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1675 *no_const_asn_ref_p = 1;
1677 if (BINFO_VIRTUAL_P (base_binfo))
1678 /* A virtual base does not effect nearly emptiness. */
1680 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1682 if (seen_non_virtual_nearly_empty_base_p)
1683 /* And if there is more than one nearly empty base, then the
1684 derived class is not nearly empty either. */
1685 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1686 else
1687 /* Remember we've seen one. */
1688 seen_non_virtual_nearly_empty_base_p = 1;
1690 else if (!is_empty_class (basetype))
1691 /* If the base class is not empty or nearly empty, then this
1692 class cannot be nearly empty. */
1693 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1695 /* A lot of properties from the bases also apply to the derived
1696 class. */
1697 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1698 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1699 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1700 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1701 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1702 || !TYPE_HAS_COPY_ASSIGN (basetype));
1703 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1704 || !TYPE_HAS_COPY_CTOR (basetype));
1705 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1706 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1707 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1708 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1709 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1710 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1711 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1712 || TYPE_HAS_COMPLEX_DFLT (basetype));
1713 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1714 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1715 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1716 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1717 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1718 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1719 if (TYPE_HAS_MUTABLE_P (basetype))
1720 CLASSTYPE_HAS_MUTABLE (t) = 1;
1722 /* A standard-layout class is a class that:
1724 * has no non-standard-layout base classes, */
1725 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1726 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1728 tree basefield;
1729 /* ...has no base classes of the same type as the first non-static
1730 data member... */
1731 if (field && DECL_CONTEXT (field) == t
1732 && (same_type_ignoring_top_level_qualifiers_p
1733 (TREE_TYPE (field), basetype)))
1734 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1735 else
1736 /* ...either has no non-static data members in the most-derived
1737 class and at most one base class with non-static data
1738 members, or has no base classes with non-static data
1739 members */
1740 for (basefield = TYPE_FIELDS (basetype); basefield;
1741 basefield = DECL_CHAIN (basefield))
1742 if (TREE_CODE (basefield) == FIELD_DECL
1743 && !(DECL_FIELD_IS_BASE (basefield)
1744 && integer_zerop (DECL_SIZE (basefield))))
1746 if (field)
1747 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1748 else
1749 field = basefield;
1750 break;
1754 /* Don't bother collecting tm attributes if transactional memory
1755 support is not enabled. */
1756 if (flag_tm)
1758 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1759 if (tm_attr)
1760 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1763 check_abi_tags (t, basetype);
1766 /* If one of the base classes had TM attributes, and the current class
1767 doesn't define its own, then the current class inherits one. */
1768 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1770 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1771 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1775 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1776 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1777 that have had a nearly-empty virtual primary base stolen by some
1778 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1779 T. */
1781 static void
1782 determine_primary_bases (tree t)
1784 unsigned i;
1785 tree primary = NULL_TREE;
1786 tree type_binfo = TYPE_BINFO (t);
1787 tree base_binfo;
1789 /* Determine the primary bases of our bases. */
1790 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1791 base_binfo = TREE_CHAIN (base_binfo))
1793 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1795 /* See if we're the non-virtual primary of our inheritance
1796 chain. */
1797 if (!BINFO_VIRTUAL_P (base_binfo))
1799 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1800 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1802 if (parent_primary
1803 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1804 BINFO_TYPE (parent_primary)))
1805 /* We are the primary binfo. */
1806 BINFO_PRIMARY_P (base_binfo) = 1;
1808 /* Determine if we have a virtual primary base, and mark it so.
1810 if (primary && BINFO_VIRTUAL_P (primary))
1812 tree this_primary = copied_binfo (primary, base_binfo);
1814 if (BINFO_PRIMARY_P (this_primary))
1815 /* Someone already claimed this base. */
1816 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1817 else
1819 tree delta;
1821 BINFO_PRIMARY_P (this_primary) = 1;
1822 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1824 /* A virtual binfo might have been copied from within
1825 another hierarchy. As we're about to use it as a
1826 primary base, make sure the offsets match. */
1827 delta = size_diffop_loc (input_location,
1828 fold_convert (ssizetype,
1829 BINFO_OFFSET (base_binfo)),
1830 fold_convert (ssizetype,
1831 BINFO_OFFSET (this_primary)));
1833 propagate_binfo_offsets (this_primary, delta);
1838 /* First look for a dynamic direct non-virtual base. */
1839 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1841 tree basetype = BINFO_TYPE (base_binfo);
1843 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1845 primary = base_binfo;
1846 goto found;
1850 /* A "nearly-empty" virtual base class can be the primary base
1851 class, if no non-virtual polymorphic base can be found. Look for
1852 a nearly-empty virtual dynamic base that is not already a primary
1853 base of something in the hierarchy. If there is no such base,
1854 just pick the first nearly-empty virtual base. */
1856 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1857 base_binfo = TREE_CHAIN (base_binfo))
1858 if (BINFO_VIRTUAL_P (base_binfo)
1859 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1861 if (!BINFO_PRIMARY_P (base_binfo))
1863 /* Found one that is not primary. */
1864 primary = base_binfo;
1865 goto found;
1867 else if (!primary)
1868 /* Remember the first candidate. */
1869 primary = base_binfo;
1872 found:
1873 /* If we've got a primary base, use it. */
1874 if (primary)
1876 tree basetype = BINFO_TYPE (primary);
1878 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1879 if (BINFO_PRIMARY_P (primary))
1880 /* We are stealing a primary base. */
1881 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1882 BINFO_PRIMARY_P (primary) = 1;
1883 if (BINFO_VIRTUAL_P (primary))
1885 tree delta;
1887 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1888 /* A virtual binfo might have been copied from within
1889 another hierarchy. As we're about to use it as a primary
1890 base, make sure the offsets match. */
1891 delta = size_diffop_loc (input_location, ssize_int (0),
1892 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1894 propagate_binfo_offsets (primary, delta);
1897 primary = TYPE_BINFO (basetype);
1899 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1900 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1901 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1905 /* Update the variant types of T. */
1907 void
1908 fixup_type_variants (tree t)
1910 tree variants;
1912 if (!t)
1913 return;
1915 for (variants = TYPE_NEXT_VARIANT (t);
1916 variants;
1917 variants = TYPE_NEXT_VARIANT (variants))
1919 /* These fields are in the _TYPE part of the node, not in
1920 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1921 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1922 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1923 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1924 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1926 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1928 TYPE_BINFO (variants) = TYPE_BINFO (t);
1930 /* Copy whatever these are holding today. */
1931 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1932 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1936 /* KLASS is a class that we're applying may_alias to after the body is
1937 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1938 canonical type(s) will be implicitly updated. */
1940 static void
1941 fixup_may_alias (tree klass)
1943 tree t, v;
1945 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1946 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1947 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1948 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1949 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1950 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1953 /* Early variant fixups: we apply attributes at the beginning of the class
1954 definition, and we need to fix up any variants that have already been
1955 made via elaborated-type-specifier so that check_qualified_type works. */
1957 void
1958 fixup_attribute_variants (tree t)
1960 tree variants;
1962 if (!t)
1963 return;
1965 tree attrs = TYPE_ATTRIBUTES (t);
1966 unsigned align = TYPE_ALIGN (t);
1967 bool user_align = TYPE_USER_ALIGN (t);
1968 bool may_alias = lookup_attribute ("may_alias", attrs);
1970 if (may_alias)
1971 fixup_may_alias (t);
1973 for (variants = TYPE_NEXT_VARIANT (t);
1974 variants;
1975 variants = TYPE_NEXT_VARIANT (variants))
1977 /* These are the two fields that check_qualified_type looks at and
1978 are affected by attributes. */
1979 TYPE_ATTRIBUTES (variants) = attrs;
1980 unsigned valign = align;
1981 if (TYPE_USER_ALIGN (variants))
1982 valign = MAX (valign, TYPE_ALIGN (variants));
1983 else
1984 TYPE_USER_ALIGN (variants) = user_align;
1985 SET_TYPE_ALIGN (variants, valign);
1986 if (may_alias)
1987 fixup_may_alias (variants);
1991 /* Set memoizing fields and bits of T (and its variants) for later
1992 use. */
1994 static void
1995 finish_struct_bits (tree t)
1997 /* Fix up variants (if any). */
1998 fixup_type_variants (t);
2000 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
2001 /* For a class w/o baseclasses, 'finish_struct' has set
2002 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2003 Similarly for a class whose base classes do not have vtables.
2004 When neither of these is true, we might have removed abstract
2005 virtuals (by providing a definition), added some (by declaring
2006 new ones), or redeclared ones from a base class. We need to
2007 recalculate what's really an abstract virtual at this point (by
2008 looking in the vtables). */
2009 get_pure_virtuals (t);
2011 /* If this type has a copy constructor or a destructor, force its
2012 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2013 nonzero. This will cause it to be passed by invisible reference
2014 and prevent it from being returned in a register. */
2015 if (type_has_nontrivial_copy_init (t)
2016 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2018 tree variants;
2019 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2020 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2022 SET_TYPE_MODE (variants, BLKmode);
2023 TREE_ADDRESSABLE (variants) = 1;
2028 /* Issue warnings about T having private constructors, but no friends,
2029 and so forth.
2031 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2032 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2033 non-private static member functions. */
2035 static void
2036 maybe_warn_about_overly_private_class (tree t)
2038 int has_member_fn = 0;
2039 int has_nonprivate_method = 0;
2041 if (!warn_ctor_dtor_privacy
2042 /* If the class has friends, those entities might create and
2043 access instances, so we should not warn. */
2044 || (CLASSTYPE_FRIEND_CLASSES (t)
2045 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2046 /* We will have warned when the template was declared; there's
2047 no need to warn on every instantiation. */
2048 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2049 /* There's no reason to even consider warning about this
2050 class. */
2051 return;
2053 /* We only issue one warning, if more than one applies, because
2054 otherwise, on code like:
2056 class A {
2057 // Oops - forgot `public:'
2058 A();
2059 A(const A&);
2060 ~A();
2063 we warn several times about essentially the same problem. */
2065 /* Check to see if all (non-constructor, non-destructor) member
2066 functions are private. (Since there are no friends or
2067 non-private statics, we can't ever call any of the private member
2068 functions.) */
2069 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2070 if (!DECL_DECLARES_FUNCTION_P (fn))
2071 /* Not a function. */;
2072 else if (DECL_ARTIFICIAL (fn))
2073 /* We're not interested in compiler-generated methods; they don't
2074 provide any way to call private members. */;
2075 else if (!TREE_PRIVATE (fn))
2077 if (DECL_STATIC_FUNCTION_P (fn))
2078 /* A non-private static member function is just like a
2079 friend; it can create and invoke private member
2080 functions, and be accessed without a class
2081 instance. */
2082 return;
2084 has_nonprivate_method = 1;
2085 /* Keep searching for a static member function. */
2087 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2088 has_member_fn = 1;
2090 if (!has_nonprivate_method && has_member_fn)
2092 /* There are no non-private methods, and there's at least one
2093 private member function that isn't a constructor or
2094 destructor. (If all the private members are
2095 constructors/destructors we want to use the code below that
2096 issues error messages specifically referring to
2097 constructors/destructors.) */
2098 unsigned i;
2099 tree binfo = TYPE_BINFO (t);
2101 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2102 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2104 has_nonprivate_method = 1;
2105 break;
2107 if (!has_nonprivate_method)
2109 warning (OPT_Wctor_dtor_privacy,
2110 "all member functions in class %qT are private", t);
2111 return;
2115 /* Even if some of the member functions are non-private, the class
2116 won't be useful for much if all the constructors or destructors
2117 are private: such an object can never be created or destroyed. */
2118 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2119 if (TREE_PRIVATE (dtor))
2121 warning (OPT_Wctor_dtor_privacy,
2122 "%q#T only defines a private destructor and has no friends",
2124 return;
2127 /* Warn about classes that have private constructors and no friends. */
2128 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2129 /* Implicitly generated constructors are always public. */
2130 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2132 bool nonprivate_ctor = false;
2133 tree copy_or_move = NULL_TREE;
2135 /* If a non-template class does not define a copy
2136 constructor, one is defined for it, enabling it to avoid
2137 this warning. For a template class, this does not
2138 happen, and so we would normally get a warning on:
2140 template <class T> class C { private: C(); };
2142 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2143 complete non-template or fully instantiated classes have this
2144 flag set. */
2145 if (!TYPE_HAS_COPY_CTOR (t))
2146 nonprivate_ctor = true;
2147 else
2148 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2149 !nonprivate_ctor && iter; ++iter)
2150 if (TREE_PRIVATE (*iter))
2151 continue;
2152 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2153 /* Ideally, we wouldn't count any constructor that takes
2154 an argument of the class type as a parameter, because
2155 such things cannot be used to construct an instance of
2156 the class unless you already have one. */
2157 copy_or_move = *iter;
2158 else
2159 nonprivate_ctor = true;
2161 if (!nonprivate_ctor)
2163 warning (OPT_Wctor_dtor_privacy,
2164 "%q#T only defines private constructors and has no friends",
2166 if (copy_or_move)
2167 inform (DECL_SOURCE_LOCATION (copy_or_move),
2168 "%q#D is public, but requires an existing %q#T object",
2169 copy_or_move, t);
2170 return;
2175 /* Make BINFO's vtable have N entries, including RTTI entries,
2176 vbase and vcall offsets, etc. Set its type and call the back end
2177 to lay it out. */
2179 static void
2180 layout_vtable_decl (tree binfo, int n)
2182 tree atype;
2183 tree vtable;
2185 atype = build_array_of_n_type (vtable_entry_type, n);
2186 layout_type (atype);
2188 /* We may have to grow the vtable. */
2189 vtable = get_vtbl_decl_for_binfo (binfo);
2190 if (!same_type_p (TREE_TYPE (vtable), atype))
2192 TREE_TYPE (vtable) = atype;
2193 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2194 layout_decl (vtable, 0);
2198 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2199 have the same signature. */
2202 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2204 /* One destructor overrides another if they are the same kind of
2205 destructor. */
2206 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2207 && special_function_p (base_fndecl) == special_function_p (fndecl))
2208 return 1;
2209 /* But a non-destructor never overrides a destructor, nor vice
2210 versa, nor do different kinds of destructors override
2211 one-another. For example, a complete object destructor does not
2212 override a deleting destructor. */
2213 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2214 return 0;
2216 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2217 || (DECL_CONV_FN_P (fndecl)
2218 && DECL_CONV_FN_P (base_fndecl)
2219 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2220 DECL_CONV_FN_TYPE (base_fndecl))))
2222 tree fntype = TREE_TYPE (fndecl);
2223 tree base_fntype = TREE_TYPE (base_fndecl);
2224 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2225 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2226 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2227 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2228 return 1;
2230 return 0;
2233 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2234 subobject. */
2236 static bool
2237 base_derived_from (tree derived, tree base)
2239 tree probe;
2241 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2243 if (probe == derived)
2244 return true;
2245 else if (BINFO_VIRTUAL_P (probe))
2246 /* If we meet a virtual base, we can't follow the inheritance
2247 any more. See if the complete type of DERIVED contains
2248 such a virtual base. */
2249 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2250 != NULL_TREE);
2252 return false;
2255 struct find_final_overrider_data {
2256 /* The function for which we are trying to find a final overrider. */
2257 tree fn;
2258 /* The base class in which the function was declared. */
2259 tree declaring_base;
2260 /* The candidate overriders. */
2261 tree candidates;
2262 /* Path to most derived. */
2263 vec<tree> path;
2266 /* Add the overrider along the current path to FFOD->CANDIDATES.
2267 Returns true if an overrider was found; false otherwise. */
2269 static bool
2270 dfs_find_final_overrider_1 (tree binfo,
2271 find_final_overrider_data *ffod,
2272 unsigned depth)
2274 tree method;
2276 /* If BINFO is not the most derived type, try a more derived class.
2277 A definition there will overrider a definition here. */
2278 if (depth)
2280 depth--;
2281 if (dfs_find_final_overrider_1
2282 (ffod->path[depth], ffod, depth))
2283 return true;
2286 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2287 if (method)
2289 tree *candidate = &ffod->candidates;
2291 /* Remove any candidates overridden by this new function. */
2292 while (*candidate)
2294 /* If *CANDIDATE overrides METHOD, then METHOD
2295 cannot override anything else on the list. */
2296 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2297 return true;
2298 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2299 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2300 *candidate = TREE_CHAIN (*candidate);
2301 else
2302 candidate = &TREE_CHAIN (*candidate);
2305 /* Add the new function. */
2306 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2307 return true;
2310 return false;
2313 /* Called from find_final_overrider via dfs_walk. */
2315 static tree
2316 dfs_find_final_overrider_pre (tree binfo, void *data)
2318 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2320 if (binfo == ffod->declaring_base)
2321 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2322 ffod->path.safe_push (binfo);
2324 return NULL_TREE;
2327 static tree
2328 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2330 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2331 ffod->path.pop ();
2333 return NULL_TREE;
2336 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2337 FN and whose TREE_VALUE is the binfo for the base where the
2338 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2339 DERIVED) is the base object in which FN is declared. */
2341 static tree
2342 find_final_overrider (tree derived, tree binfo, tree fn)
2344 find_final_overrider_data ffod;
2346 /* Getting this right is a little tricky. This is valid:
2348 struct S { virtual void f (); };
2349 struct T { virtual void f (); };
2350 struct U : public S, public T { };
2352 even though calling `f' in `U' is ambiguous. But,
2354 struct R { virtual void f(); };
2355 struct S : virtual public R { virtual void f (); };
2356 struct T : virtual public R { virtual void f (); };
2357 struct U : public S, public T { };
2359 is not -- there's no way to decide whether to put `S::f' or
2360 `T::f' in the vtable for `R'.
2362 The solution is to look at all paths to BINFO. If we find
2363 different overriders along any two, then there is a problem. */
2364 if (DECL_THUNK_P (fn))
2365 fn = THUNK_TARGET (fn);
2367 /* Determine the depth of the hierarchy. */
2368 ffod.fn = fn;
2369 ffod.declaring_base = binfo;
2370 ffod.candidates = NULL_TREE;
2371 ffod.path.create (30);
2373 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2374 dfs_find_final_overrider_post, &ffod);
2376 ffod.path.release ();
2378 /* If there was no winner, issue an error message. */
2379 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2380 return error_mark_node;
2382 return ffod.candidates;
2385 /* Return the index of the vcall offset for FN when TYPE is used as a
2386 virtual base. */
2388 static tree
2389 get_vcall_index (tree fn, tree type)
2391 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2392 tree_pair_p p;
2393 unsigned ix;
2395 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2396 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2397 || same_signature_p (fn, p->purpose))
2398 return p->value;
2400 /* There should always be an appropriate index. */
2401 gcc_unreachable ();
2404 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2405 dominated by T. FN is the old function; VIRTUALS points to the
2406 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2407 of that entry in the list. */
2409 static void
2410 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2411 unsigned ix)
2413 tree b;
2414 tree overrider;
2415 tree delta;
2416 tree virtual_base;
2417 tree first_defn;
2418 tree overrider_fn, overrider_target;
2419 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2420 tree over_return, base_return;
2421 bool lost = false;
2423 /* Find the nearest primary base (possibly binfo itself) which defines
2424 this function; this is the class the caller will convert to when
2425 calling FN through BINFO. */
2426 for (b = binfo; ; b = get_primary_binfo (b))
2428 gcc_assert (b);
2429 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2430 break;
2432 /* The nearest definition is from a lost primary. */
2433 if (BINFO_LOST_PRIMARY_P (b))
2434 lost = true;
2436 first_defn = b;
2438 /* Find the final overrider. */
2439 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2440 if (overrider == error_mark_node)
2442 error ("no unique final overrider for %qD in %qT", target_fn, t);
2443 return;
2445 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2447 /* Check for adjusting covariant return types. */
2448 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2449 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2451 if (POINTER_TYPE_P (over_return)
2452 && TREE_CODE (over_return) == TREE_CODE (base_return)
2453 && CLASS_TYPE_P (TREE_TYPE (over_return))
2454 && CLASS_TYPE_P (TREE_TYPE (base_return))
2455 /* If the overrider is invalid, don't even try. */
2456 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2458 /* If FN is a covariant thunk, we must figure out the adjustment
2459 to the final base FN was converting to. As OVERRIDER_TARGET might
2460 also be converting to the return type of FN, we have to
2461 combine the two conversions here. */
2462 tree fixed_offset, virtual_offset;
2464 over_return = TREE_TYPE (over_return);
2465 base_return = TREE_TYPE (base_return);
2467 if (DECL_THUNK_P (fn))
2469 gcc_assert (DECL_RESULT_THUNK_P (fn));
2470 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2471 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2473 else
2474 fixed_offset = virtual_offset = NULL_TREE;
2476 if (virtual_offset)
2477 /* Find the equivalent binfo within the return type of the
2478 overriding function. We will want the vbase offset from
2479 there. */
2480 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2481 over_return);
2482 else if (!same_type_ignoring_top_level_qualifiers_p
2483 (over_return, base_return))
2485 /* There was no existing virtual thunk (which takes
2486 precedence). So find the binfo of the base function's
2487 return type within the overriding function's return type.
2488 Fortunately we know the covariancy is valid (it
2489 has already been checked), so we can just iterate along
2490 the binfos, which have been chained in inheritance graph
2491 order. Of course it is lame that we have to repeat the
2492 search here anyway -- we should really be caching pieces
2493 of the vtable and avoiding this repeated work. */
2494 tree thunk_binfo, base_binfo;
2496 /* Find the base binfo within the overriding function's
2497 return type. We will always find a thunk_binfo, except
2498 when the covariancy is invalid (which we will have
2499 already diagnosed). */
2500 for (base_binfo = TYPE_BINFO (base_return),
2501 thunk_binfo = TYPE_BINFO (over_return);
2502 thunk_binfo;
2503 thunk_binfo = TREE_CHAIN (thunk_binfo))
2504 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2505 BINFO_TYPE (base_binfo)))
2506 break;
2508 /* See if virtual inheritance is involved. */
2509 for (virtual_offset = thunk_binfo;
2510 virtual_offset;
2511 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2512 if (BINFO_VIRTUAL_P (virtual_offset))
2513 break;
2515 if (virtual_offset
2516 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2518 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2520 if (virtual_offset)
2522 /* We convert via virtual base. Adjust the fixed
2523 offset to be from there. */
2524 offset =
2525 size_diffop (offset,
2526 fold_convert (ssizetype,
2527 BINFO_OFFSET (virtual_offset)));
2529 if (fixed_offset)
2530 /* There was an existing fixed offset, this must be
2531 from the base just converted to, and the base the
2532 FN was thunking to. */
2533 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2534 else
2535 fixed_offset = offset;
2539 if (fixed_offset || virtual_offset)
2540 /* Replace the overriding function with a covariant thunk. We
2541 will emit the overriding function in its own slot as
2542 well. */
2543 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2544 fixed_offset, virtual_offset);
2546 else
2547 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2548 !DECL_THUNK_P (fn));
2550 /* If we need a covariant thunk, then we may need to adjust first_defn.
2551 The ABI specifies that the thunks emitted with a function are
2552 determined by which bases the function overrides, so we need to be
2553 sure that we're using a thunk for some overridden base; even if we
2554 know that the necessary this adjustment is zero, there may not be an
2555 appropriate zero-this-adjustment thunk for us to use since thunks for
2556 overriding virtual bases always use the vcall offset.
2558 Furthermore, just choosing any base that overrides this function isn't
2559 quite right, as this slot won't be used for calls through a type that
2560 puts a covariant thunk here. Calling the function through such a type
2561 will use a different slot, and that slot is the one that determines
2562 the thunk emitted for that base.
2564 So, keep looking until we find the base that we're really overriding
2565 in this slot: the nearest primary base that doesn't use a covariant
2566 thunk in this slot. */
2567 if (overrider_target != overrider_fn)
2569 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2570 /* We already know that the overrider needs a covariant thunk. */
2571 b = get_primary_binfo (b);
2572 for (; ; b = get_primary_binfo (b))
2574 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2575 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2576 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2577 break;
2578 if (BINFO_LOST_PRIMARY_P (b))
2579 lost = true;
2581 first_defn = b;
2584 /* Assume that we will produce a thunk that convert all the way to
2585 the final overrider, and not to an intermediate virtual base. */
2586 virtual_base = NULL_TREE;
2588 /* See if we can convert to an intermediate virtual base first, and then
2589 use the vcall offset located there to finish the conversion. */
2590 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2592 /* If we find the final overrider, then we can stop
2593 walking. */
2594 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2595 BINFO_TYPE (TREE_VALUE (overrider))))
2596 break;
2598 /* If we find a virtual base, and we haven't yet found the
2599 overrider, then there is a virtual base between the
2600 declaring base (first_defn) and the final overrider. */
2601 if (BINFO_VIRTUAL_P (b))
2603 virtual_base = b;
2604 break;
2608 /* Compute the constant adjustment to the `this' pointer. The
2609 `this' pointer, when this function is called, will point at BINFO
2610 (or one of its primary bases, which are at the same offset). */
2611 if (virtual_base)
2612 /* The `this' pointer needs to be adjusted from the declaration to
2613 the nearest virtual base. */
2614 delta = size_diffop_loc (input_location,
2615 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2616 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2617 else if (lost)
2618 /* If the nearest definition is in a lost primary, we don't need an
2619 entry in our vtable. Except possibly in a constructor vtable,
2620 if we happen to get our primary back. In that case, the offset
2621 will be zero, as it will be a primary base. */
2622 delta = size_zero_node;
2623 else
2624 /* The `this' pointer needs to be adjusted from pointing to
2625 BINFO to pointing at the base where the final overrider
2626 appears. */
2627 delta = size_diffop_loc (input_location,
2628 fold_convert (ssizetype,
2629 BINFO_OFFSET (TREE_VALUE (overrider))),
2630 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2632 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2634 if (virtual_base)
2635 BV_VCALL_INDEX (*virtuals)
2636 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2637 else
2638 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2640 BV_LOST_PRIMARY (*virtuals) = lost;
2643 /* Called from modify_all_vtables via dfs_walk. */
2645 static tree
2646 dfs_modify_vtables (tree binfo, void* data)
2648 tree t = (tree) data;
2649 tree virtuals;
2650 tree old_virtuals;
2651 unsigned ix;
2653 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2654 /* A base without a vtable needs no modification, and its bases
2655 are uninteresting. */
2656 return dfs_skip_bases;
2658 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2659 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2660 /* Don't do the primary vtable, if it's new. */
2661 return NULL_TREE;
2663 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2664 /* There's no need to modify the vtable for a non-virtual primary
2665 base; we're not going to use that vtable anyhow. We do still
2666 need to do this for virtual primary bases, as they could become
2667 non-primary in a construction vtable. */
2668 return NULL_TREE;
2670 make_new_vtable (t, binfo);
2672 /* Now, go through each of the virtual functions in the virtual
2673 function table for BINFO. Find the final overrider, and update
2674 the BINFO_VIRTUALS list appropriately. */
2675 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2676 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2677 virtuals;
2678 ix++, virtuals = TREE_CHAIN (virtuals),
2679 old_virtuals = TREE_CHAIN (old_virtuals))
2680 update_vtable_entry_for_fn (t,
2681 binfo,
2682 BV_FN (old_virtuals),
2683 &virtuals, ix);
2685 return NULL_TREE;
2688 /* Update all of the primary and secondary vtables for T. Create new
2689 vtables as required, and initialize their RTTI information. Each
2690 of the functions in VIRTUALS is declared in T and may override a
2691 virtual function from a base class; find and modify the appropriate
2692 entries to point to the overriding functions. Returns a list, in
2693 declaration order, of the virtual functions that are declared in T,
2694 but do not appear in the primary base class vtable, and which
2695 should therefore be appended to the end of the vtable for T. */
2697 static tree
2698 modify_all_vtables (tree t, tree virtuals)
2700 tree binfo = TYPE_BINFO (t);
2701 tree *fnsp;
2703 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2704 if (TYPE_CONTAINS_VPTR_P (t))
2705 get_vtable_decl (t, false);
2707 /* Update all of the vtables. */
2708 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2710 /* Add virtual functions not already in our primary vtable. These
2711 will be both those introduced by this class, and those overridden
2712 from secondary bases. It does not include virtuals merely
2713 inherited from secondary bases. */
2714 for (fnsp = &virtuals; *fnsp; )
2716 tree fn = TREE_VALUE (*fnsp);
2718 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2719 || DECL_VINDEX (fn) == error_mark_node)
2721 /* We don't need to adjust the `this' pointer when
2722 calling this function. */
2723 BV_DELTA (*fnsp) = integer_zero_node;
2724 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2726 /* This is a function not already in our vtable. Keep it. */
2727 fnsp = &TREE_CHAIN (*fnsp);
2729 else
2730 /* We've already got an entry for this function. Skip it. */
2731 *fnsp = TREE_CHAIN (*fnsp);
2734 return virtuals;
2737 /* Get the base virtual function declarations in T that have the
2738 indicated NAME. */
2740 static void
2741 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2743 bool found_decls = false;
2745 /* Find virtual functions in T with the indicated NAME. */
2746 for (ovl_iterator iter (get_class_binding (t, name)); iter; ++iter)
2748 tree method = *iter;
2750 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2752 base_fndecls->safe_push (method);
2753 found_decls = true;
2757 if (found_decls)
2758 return;
2760 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2761 for (int i = 0; i < n_baseclasses; i++)
2763 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2764 get_basefndecls (name, basetype, base_fndecls);
2768 /* If this declaration supersedes the declaration of
2769 a method declared virtual in the base class, then
2770 mark this field as being virtual as well. */
2772 void
2773 check_for_override (tree decl, tree ctype)
2775 bool overrides_found = false;
2776 if (TREE_CODE (decl) == TEMPLATE_DECL)
2777 /* In [temp.mem] we have:
2779 A specialization of a member function template does not
2780 override a virtual function from a base class. */
2781 return;
2782 if ((DECL_DESTRUCTOR_P (decl)
2783 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2784 || DECL_CONV_FN_P (decl))
2785 && look_for_overrides (ctype, decl)
2786 && !DECL_STATIC_FUNCTION_P (decl))
2787 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2788 the error_mark_node so that we know it is an overriding
2789 function. */
2791 DECL_VINDEX (decl) = decl;
2792 overrides_found = true;
2793 if (warn_override && !DECL_OVERRIDE_P (decl)
2794 && !DECL_DESTRUCTOR_P (decl))
2795 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2796 "%qD can be marked override", decl);
2799 if (DECL_VIRTUAL_P (decl))
2801 if (!DECL_VINDEX (decl))
2802 DECL_VINDEX (decl) = error_mark_node;
2803 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2804 if (DECL_DESTRUCTOR_P (decl))
2805 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2807 else if (DECL_FINAL_P (decl))
2808 error ("%q+#D marked %<final%>, but is not virtual", decl);
2809 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2810 error ("%q+#D marked %<override%>, but does not override", decl);
2813 /* Warn about hidden virtual functions that are not overridden in t.
2814 We know that constructors and destructors don't apply. */
2816 static void
2817 warn_hidden (tree t)
2819 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2820 for (unsigned ix = member_vec->length (); ix--;)
2822 tree fns = (*member_vec)[ix];
2824 if (!OVL_P (fns))
2825 continue;
2827 tree name = OVL_NAME (fns);
2828 auto_vec<tree, 20> base_fndecls;
2829 tree base_binfo;
2830 tree binfo;
2831 unsigned j;
2833 /* Iterate through all of the base classes looking for possibly
2834 hidden functions. */
2835 for (binfo = TYPE_BINFO (t), j = 0;
2836 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2838 tree basetype = BINFO_TYPE (base_binfo);
2839 get_basefndecls (name, basetype, &base_fndecls);
2842 /* If there are no functions to hide, continue. */
2843 if (base_fndecls.is_empty ())
2844 continue;
2846 /* Remove any overridden functions. */
2847 for (ovl_iterator iter (fns); iter; ++iter)
2849 tree fndecl = *iter;
2850 if (TREE_CODE (fndecl) == FUNCTION_DECL
2851 && DECL_VINDEX (fndecl))
2853 /* If the method from the base class has the same
2854 signature as the method from the derived class, it
2855 has been overridden. */
2856 for (size_t k = 0; k < base_fndecls.length (); k++)
2857 if (base_fndecls[k]
2858 && same_signature_p (fndecl, base_fndecls[k]))
2859 base_fndecls[k] = NULL_TREE;
2863 /* Now give a warning for all base functions without overriders,
2864 as they are hidden. */
2865 tree base_fndecl;
2866 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
2867 if (base_fndecl)
2869 /* Here we know it is a hider, and no overrider exists. */
2870 warning_at (location_of (base_fndecl),
2871 OPT_Woverloaded_virtual,
2872 "%qD was hidden", base_fndecl);
2873 warning_at (location_of (fns),
2874 OPT_Woverloaded_virtual, " by %qD", fns);
2879 /* Recursive helper for finish_struct_anon. */
2881 static void
2882 finish_struct_anon_r (tree field, bool complain)
2884 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
2885 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2886 for (; elt; elt = DECL_CHAIN (elt))
2888 /* We're generally only interested in entities the user
2889 declared, but we also find nested classes by noticing
2890 the TYPE_DECL that we create implicitly. You're
2891 allowed to put one anonymous union inside another,
2892 though, so we explicitly tolerate that. We use
2893 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2894 we also allow unnamed types used for defining fields. */
2895 if (DECL_ARTIFICIAL (elt)
2896 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2897 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2898 continue;
2900 if (TREE_CODE (elt) != FIELD_DECL)
2902 /* We already complained about static data members in
2903 finish_static_data_member_decl. */
2904 if (complain && !VAR_P (elt))
2906 if (is_union)
2907 permerror (DECL_SOURCE_LOCATION (elt),
2908 "%q#D invalid; an anonymous union can "
2909 "only have non-static data members", elt);
2910 else
2911 permerror (DECL_SOURCE_LOCATION (elt),
2912 "%q#D invalid; an anonymous struct can "
2913 "only have non-static data members", elt);
2915 continue;
2918 if (complain)
2920 if (TREE_PRIVATE (elt))
2922 if (is_union)
2923 permerror (DECL_SOURCE_LOCATION (elt),
2924 "private member %q#D in anonymous union", elt);
2925 else
2926 permerror (DECL_SOURCE_LOCATION (elt),
2927 "private member %q#D in anonymous struct", elt);
2929 else if (TREE_PROTECTED (elt))
2931 if (is_union)
2932 permerror (DECL_SOURCE_LOCATION (elt),
2933 "protected member %q#D in anonymous union", elt);
2934 else
2935 permerror (DECL_SOURCE_LOCATION (elt),
2936 "protected member %q#D in anonymous struct", elt);
2940 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2941 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2943 /* Recurse into the anonymous aggregates to handle correctly
2944 access control (c++/24926):
2946 class A {
2947 union {
2948 union {
2949 int i;
2954 int j=A().i; */
2955 if (DECL_NAME (elt) == NULL_TREE
2956 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2957 finish_struct_anon_r (elt, /*complain=*/false);
2961 /* Check for things that are invalid. There are probably plenty of other
2962 things we should check for also. */
2964 static void
2965 finish_struct_anon (tree t)
2967 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2969 if (TREE_STATIC (field))
2970 continue;
2971 if (TREE_CODE (field) != FIELD_DECL)
2972 continue;
2974 if (DECL_NAME (field) == NULL_TREE
2975 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2976 finish_struct_anon_r (field, /*complain=*/true);
2980 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2981 will be used later during class template instantiation.
2982 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2983 a non-static member data (FIELD_DECL), a member function
2984 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2985 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2986 When FRIEND_P is nonzero, T is either a friend class
2987 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2988 (FUNCTION_DECL, TEMPLATE_DECL). */
2990 void
2991 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2993 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2994 if (CLASSTYPE_TEMPLATE_INFO (type))
2995 CLASSTYPE_DECL_LIST (type)
2996 = tree_cons (friend_p ? NULL_TREE : type,
2997 t, CLASSTYPE_DECL_LIST (type));
3000 /* This function is called from declare_virt_assop_and_dtor via
3001 dfs_walk_all.
3003 DATA is a type that direcly or indirectly inherits the base
3004 represented by BINFO. If BINFO contains a virtual assignment [copy
3005 assignment or move assigment] operator or a virtual constructor,
3006 declare that function in DATA if it hasn't been already declared. */
3008 static tree
3009 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
3011 tree bv, fn, t = (tree)data;
3012 tree opname = assign_op_identifier;
3014 gcc_assert (t && CLASS_TYPE_P (t));
3015 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3017 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3018 /* A base without a vtable needs no modification, and its bases
3019 are uninteresting. */
3020 return dfs_skip_bases;
3022 if (BINFO_PRIMARY_P (binfo))
3023 /* If this is a primary base, then we have already looked at the
3024 virtual functions of its vtable. */
3025 return NULL_TREE;
3027 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3029 fn = BV_FN (bv);
3031 if (DECL_NAME (fn) == opname)
3033 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3034 lazily_declare_fn (sfk_copy_assignment, t);
3035 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3036 lazily_declare_fn (sfk_move_assignment, t);
3038 else if (DECL_DESTRUCTOR_P (fn)
3039 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3040 lazily_declare_fn (sfk_destructor, t);
3043 return NULL_TREE;
3046 /* If the class type T has a direct or indirect base that contains a
3047 virtual assignment operator or a virtual destructor, declare that
3048 function in T if it hasn't been already declared. */
3050 static void
3051 declare_virt_assop_and_dtor (tree t)
3053 if (!(TYPE_POLYMORPHIC_P (t)
3054 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3055 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3056 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3057 return;
3059 dfs_walk_all (TYPE_BINFO (t),
3060 dfs_declare_virt_assop_and_dtor,
3061 NULL, t);
3064 /* Declare the inheriting constructor for class T inherited from base
3065 constructor CTOR with the parameter array PARMS of size NPARMS. */
3067 static void
3068 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3070 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3072 /* We don't declare an inheriting ctor that would be a default,
3073 copy or move ctor for derived or base. */
3074 if (nparms == 0)
3075 return;
3076 if (nparms == 1
3077 && TREE_CODE (parms[0]) == REFERENCE_TYPE)
3079 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3080 if (parm == t || parm == DECL_CONTEXT (ctor))
3081 return;
3084 tree parmlist = void_list_node;
3085 for (int i = nparms - 1; i >= 0; i--)
3086 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3087 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3088 t, false, ctor, parmlist);
3090 if (add_method (t, fn, false))
3092 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3093 TYPE_FIELDS (t) = fn;
3097 /* Declare all the inheriting constructors for class T inherited from base
3098 constructor CTOR. */
3100 static void
3101 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3103 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3105 if (flag_new_inheriting_ctors)
3107 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3108 t, /*const*/false, ctor, parms);
3109 add_method (t, ctor, using_decl != NULL_TREE);
3110 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3111 return;
3114 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3115 int i = 0;
3116 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3118 if (TREE_PURPOSE (parms))
3119 one_inheriting_sig (t, ctor, new_parms, i);
3120 new_parms[i++] = TREE_VALUE (parms);
3122 one_inheriting_sig (t, ctor, new_parms, i);
3123 if (parms == NULL_TREE)
3125 if (warning (OPT_Winherited_variadic_ctor,
3126 "the ellipsis in %qD is not inherited", ctor))
3127 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3131 /* Create default constructors, assignment operators, and so forth for
3132 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3133 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3134 the class cannot have a default constructor, copy constructor
3135 taking a const reference argument, or an assignment operator taking
3136 a const reference, respectively. */
3138 static void
3139 add_implicitly_declared_members (tree t, tree* access_decls,
3140 int cant_have_const_cctor,
3141 int cant_have_const_assignment)
3143 /* Destructor. */
3144 if (!CLASSTYPE_DESTRUCTOR (t))
3145 /* In general, we create destructors lazily. */
3146 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3148 bool move_ok = false;
3149 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3150 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3151 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3152 move_ok = true;
3154 /* [class.ctor]
3156 If there is no user-declared constructor for a class, a default
3157 constructor is implicitly declared. */
3158 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3160 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3161 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3162 if (cxx_dialect >= cxx11)
3163 TYPE_HAS_CONSTEXPR_CTOR (t)
3164 /* Don't force the declaration to get a hard answer; if the
3165 definition would have made the class non-literal, it will still be
3166 non-literal because of the base or member in question, and that
3167 gives a better diagnostic. */
3168 = type_maybe_constexpr_default_constructor (t);
3171 /* [class.ctor]
3173 If a class definition does not explicitly declare a copy
3174 constructor, one is declared implicitly. */
3175 if (! TYPE_HAS_COPY_CTOR (t))
3177 TYPE_HAS_COPY_CTOR (t) = 1;
3178 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3179 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3180 if (move_ok)
3181 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3184 /* If there is no assignment operator, one will be created if and
3185 when it is needed. For now, just record whether or not the type
3186 of the parameter to the assignment operator will be a const or
3187 non-const reference. */
3188 if (!TYPE_HAS_COPY_ASSIGN (t))
3190 TYPE_HAS_COPY_ASSIGN (t) = 1;
3191 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3192 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3193 if (move_ok && !LAMBDA_TYPE_P (t))
3194 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3197 /* We can't be lazy about declaring functions that might override
3198 a virtual function from a base class. */
3199 declare_virt_assop_and_dtor (t);
3201 while (*access_decls)
3203 tree using_decl = TREE_VALUE (*access_decls);
3204 tree decl = USING_DECL_DECLS (using_decl);
3205 if (DECL_NAME (using_decl) == ctor_identifier)
3207 /* declare, then remove the decl */
3208 tree ctor_list = decl;
3209 location_t loc = input_location;
3210 input_location = DECL_SOURCE_LOCATION (using_decl);
3211 for (ovl_iterator iter (ctor_list); iter; ++iter)
3212 one_inherited_ctor (*iter, t, using_decl);
3213 *access_decls = TREE_CHAIN (*access_decls);
3214 input_location = loc;
3216 else
3217 access_decls = &TREE_CHAIN (*access_decls);
3221 /* FIELD is a bit-field. We are finishing the processing for its
3222 enclosing type. Issue any appropriate messages and set appropriate
3223 flags. Returns false if an error has been diagnosed. */
3225 static bool
3226 check_bitfield_decl (tree field)
3228 tree type = TREE_TYPE (field);
3229 tree w;
3231 /* Extract the declared width of the bitfield, which has been
3232 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3233 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3234 gcc_assert (w != NULL_TREE);
3235 /* Remove the bit-field width indicator so that the rest of the
3236 compiler does not treat that value as a qualifier. */
3237 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3239 /* Detect invalid bit-field type. */
3240 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3242 error ("bit-field %q+#D with non-integral type", field);
3243 w = error_mark_node;
3245 else
3247 location_t loc = input_location;
3248 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3249 STRIP_NOPS (w);
3251 /* detect invalid field size. */
3252 input_location = DECL_SOURCE_LOCATION (field);
3253 w = cxx_constant_value (w);
3254 input_location = loc;
3256 if (TREE_CODE (w) != INTEGER_CST)
3258 error ("bit-field %q+D width not an integer constant", field);
3259 w = error_mark_node;
3261 else if (tree_int_cst_sgn (w) < 0)
3263 error ("negative width in bit-field %q+D", field);
3264 w = error_mark_node;
3266 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3268 error ("zero width for bit-field %q+D", field);
3269 w = error_mark_node;
3271 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3272 && TREE_CODE (type) != BOOLEAN_TYPE
3273 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3274 || ((TREE_CODE (type) == ENUMERAL_TYPE
3275 || TREE_CODE (type) == BOOLEAN_TYPE)
3276 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3277 warning_at (DECL_SOURCE_LOCATION (field), 0,
3278 "width of %qD exceeds its type", field);
3279 else if (TREE_CODE (type) == ENUMERAL_TYPE
3280 && (0 > (compare_tree_int
3281 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type))))))
3282 warning_at (DECL_SOURCE_LOCATION (field), 0,
3283 "%qD is too small to hold all values of %q#T",
3284 field, type);
3287 if (w != error_mark_node)
3289 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3290 DECL_BIT_FIELD (field) = 1;
3291 return true;
3293 else
3295 /* Non-bit-fields are aligned for their type. */
3296 DECL_BIT_FIELD (field) = 0;
3297 CLEAR_DECL_C_BIT_FIELD (field);
3298 return false;
3302 /* FIELD is a non bit-field. We are finishing the processing for its
3303 enclosing type T. Issue any appropriate messages and set appropriate
3304 flags. */
3306 static bool
3307 check_field_decl (tree field,
3308 tree t,
3309 int* cant_have_const_ctor,
3310 int* no_const_asn_ref)
3312 tree type = strip_array_types (TREE_TYPE (field));
3313 bool any_default_members = false;
3315 /* In C++98 an anonymous union cannot contain any fields which would change
3316 the settings of CANT_HAVE_CONST_CTOR and friends. */
3317 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3319 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3320 structs. So, we recurse through their fields here. */
3321 else if (ANON_AGGR_TYPE_P (type))
3323 for (tree fields = TYPE_FIELDS (type); fields;
3324 fields = DECL_CHAIN (fields))
3325 if (TREE_CODE (fields) == FIELD_DECL)
3326 any_default_members |= check_field_decl (fields, t,
3327 cant_have_const_ctor,
3328 no_const_asn_ref);
3330 /* Check members with class type for constructors, destructors,
3331 etc. */
3332 else if (CLASS_TYPE_P (type))
3334 /* Never let anything with uninheritable virtuals
3335 make it through without complaint. */
3336 abstract_virtuals_error (field, type);
3338 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3340 static bool warned;
3341 int oldcount = errorcount;
3342 if (TYPE_NEEDS_CONSTRUCTING (type))
3343 error ("member %q+#D with constructor not allowed in union",
3344 field);
3345 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3346 error ("member %q+#D with destructor not allowed in union", field);
3347 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3348 error ("member %q+#D with copy assignment operator not allowed in union",
3349 field);
3350 if (!warned && errorcount > oldcount)
3352 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3353 "only available with -std=c++11 or -std=gnu++11");
3354 warned = true;
3357 else
3359 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3360 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3361 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3362 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3363 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3364 || !TYPE_HAS_COPY_ASSIGN (type));
3365 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3366 || !TYPE_HAS_COPY_CTOR (type));
3367 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3368 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3369 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3370 || TYPE_HAS_COMPLEX_DFLT (type));
3373 if (TYPE_HAS_COPY_CTOR (type)
3374 && !TYPE_HAS_CONST_COPY_CTOR (type))
3375 *cant_have_const_ctor = 1;
3377 if (TYPE_HAS_COPY_ASSIGN (type)
3378 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3379 *no_const_asn_ref = 1;
3382 check_abi_tags (t, field);
3384 if (DECL_INITIAL (field) != NULL_TREE)
3385 /* `build_class_init_list' does not recognize
3386 non-FIELD_DECLs. */
3387 any_default_members = true;
3389 return any_default_members;
3392 /* Check the data members (both static and non-static), class-scoped
3393 typedefs, etc., appearing in the declaration of T. Issue
3394 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3395 declaration order) of access declarations; each TREE_VALUE in this
3396 list is a USING_DECL.
3398 In addition, set the following flags:
3400 EMPTY_P
3401 The class is empty, i.e., contains no non-static data members.
3403 CANT_HAVE_CONST_CTOR_P
3404 This class cannot have an implicitly generated copy constructor
3405 taking a const reference.
3407 CANT_HAVE_CONST_ASN_REF
3408 This class cannot have an implicitly generated assignment
3409 operator taking a const reference.
3411 All of these flags should be initialized before calling this
3412 function.
3414 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3415 fields can be added by adding to this chain. */
3417 static void
3418 check_field_decls (tree t, tree *access_decls,
3419 int *cant_have_const_ctor_p,
3420 int *no_const_asn_ref_p)
3422 tree *field;
3423 tree *next;
3424 bool has_pointers;
3425 bool any_default_members;
3426 int cant_pack = 0;
3427 int field_access = -1;
3429 /* Assume there are no access declarations. */
3430 *access_decls = NULL_TREE;
3431 /* Assume this class has no pointer members. */
3432 has_pointers = false;
3433 /* Assume none of the members of this class have default
3434 initializations. */
3435 any_default_members = false;
3437 for (field = &TYPE_FIELDS (t); *field; field = next)
3439 tree x = *field;
3440 tree type = TREE_TYPE (x);
3441 int this_field_access;
3443 next = &DECL_CHAIN (x);
3445 if (TREE_CODE (x) == USING_DECL)
3447 /* Save the access declarations for our caller. */
3448 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3449 continue;
3452 if (TREE_CODE (x) == TYPE_DECL
3453 || TREE_CODE (x) == TEMPLATE_DECL)
3454 continue;
3456 if (TREE_CODE (x) == FUNCTION_DECL)
3457 /* FIXME: We should fold in the checking from check_methods. */
3458 continue;
3460 /* If we've gotten this far, it's a data member, possibly static,
3461 or an enumerator. */
3462 if (TREE_CODE (x) != CONST_DECL)
3463 DECL_CONTEXT (x) = t;
3465 /* When this goes into scope, it will be a non-local reference. */
3466 DECL_NONLOCAL (x) = 1;
3468 if (TREE_CODE (t) == UNION_TYPE)
3470 /* [class.union] (C++98)
3472 If a union contains a static data member, or a member of
3473 reference type, the program is ill-formed.
3475 In C++11 [class.union] says:
3476 If a union contains a non-static data member of reference type
3477 the program is ill-formed. */
3478 if (VAR_P (x) && cxx_dialect < cxx11)
3480 error ("in C++98 %q+D may not be static because it is "
3481 "a member of a union", x);
3482 continue;
3484 if (TREE_CODE (type) == REFERENCE_TYPE
3485 && TREE_CODE (x) == FIELD_DECL)
3487 error ("non-static data member %q+D in a union may not "
3488 "have reference type %qT", x, type);
3489 continue;
3493 /* Perform error checking that did not get done in
3494 grokdeclarator. */
3495 if (TREE_CODE (type) == FUNCTION_TYPE)
3497 error ("field %q+D invalidly declared function type", x);
3498 type = build_pointer_type (type);
3499 TREE_TYPE (x) = type;
3501 else if (TREE_CODE (type) == METHOD_TYPE)
3503 error ("field %q+D invalidly declared method type", x);
3504 type = build_pointer_type (type);
3505 TREE_TYPE (x) = type;
3508 if (type == error_mark_node)
3509 continue;
3511 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3512 continue;
3514 /* Now it can only be a FIELD_DECL. */
3516 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3517 CLASSTYPE_NON_AGGREGATE (t) = 1;
3519 /* If at least one non-static data member is non-literal, the whole
3520 class becomes non-literal. Per Core/1453, volatile non-static
3521 data members and base classes are also not allowed.
3522 Note: if the type is incomplete we will complain later on. */
3523 if (COMPLETE_TYPE_P (type)
3524 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3525 CLASSTYPE_LITERAL_P (t) = false;
3527 /* A standard-layout class is a class that:
3529 has the same access control (Clause 11) for all non-static data members,
3530 ... */
3531 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3532 if (field_access == -1)
3533 field_access = this_field_access;
3534 else if (this_field_access != field_access)
3535 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3537 /* If this is of reference type, check if it needs an init. */
3538 if (TREE_CODE (type) == REFERENCE_TYPE)
3540 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3541 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3542 if (DECL_INITIAL (x) == NULL_TREE)
3543 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3544 if (cxx_dialect < cxx11)
3546 /* ARM $12.6.2: [A member initializer list] (or, for an
3547 aggregate, initialization by a brace-enclosed list) is the
3548 only way to initialize nonstatic const and reference
3549 members. */
3550 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3551 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3555 type = strip_array_types (type);
3557 if (TYPE_PACKED (t))
3559 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3561 warning_at
3562 (DECL_SOURCE_LOCATION (x), 0,
3563 "ignoring packed attribute because of unpacked non-POD field %q#D",
3565 cant_pack = 1;
3567 else if (DECL_C_BIT_FIELD (x)
3568 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3569 DECL_PACKED (x) = 1;
3572 if (DECL_C_BIT_FIELD (x)
3573 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
3574 /* We don't treat zero-width bitfields as making a class
3575 non-empty. */
3577 else
3579 /* The class is non-empty. */
3580 CLASSTYPE_EMPTY_P (t) = 0;
3581 /* The class is not even nearly empty. */
3582 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3583 /* If one of the data members contains an empty class,
3584 so does T. */
3585 if (CLASS_TYPE_P (type)
3586 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3587 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3590 /* This is used by -Weffc++ (see below). Warn only for pointers
3591 to members which might hold dynamic memory. So do not warn
3592 for pointers to functions or pointers to members. */
3593 if (TYPE_PTR_P (type)
3594 && !TYPE_PTRFN_P (type))
3595 has_pointers = true;
3597 if (CLASS_TYPE_P (type))
3599 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3600 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3601 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3602 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3605 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3606 CLASSTYPE_HAS_MUTABLE (t) = 1;
3608 if (DECL_MUTABLE_P (x))
3610 if (CP_TYPE_CONST_P (type))
3612 error ("member %q+D cannot be declared both %<const%> "
3613 "and %<mutable%>", x);
3614 continue;
3616 if (TREE_CODE (type) == REFERENCE_TYPE)
3618 error ("member %q+D cannot be declared as a %<mutable%> "
3619 "reference", x);
3620 continue;
3624 if (! layout_pod_type_p (type))
3625 /* DR 148 now allows pointers to members (which are POD themselves),
3626 to be allowed in POD structs. */
3627 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3629 if (!std_layout_type_p (type))
3630 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3632 if (! zero_init_p (type))
3633 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3635 /* We set DECL_C_BIT_FIELD in grokbitfield.
3636 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3637 if (DECL_C_BIT_FIELD (x))
3638 check_bitfield_decl (x);
3640 if (check_field_decl (x, t, cant_have_const_ctor_p, no_const_asn_ref_p))
3642 if (any_default_members
3643 && TREE_CODE (t) == UNION_TYPE)
3644 error ("multiple fields in union %qT initialized", t);
3645 any_default_members = true;
3648 /* Now that we've removed bit-field widths from DECL_INITIAL,
3649 anything left in DECL_INITIAL is an NSDMI that makes the class
3650 non-aggregate in C++11. */
3651 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3652 CLASSTYPE_NON_AGGREGATE (t) = true;
3654 /* If any field is const, the structure type is pseudo-const. */
3655 if (CP_TYPE_CONST_P (type))
3657 C_TYPE_FIELDS_READONLY (t) = 1;
3658 if (DECL_INITIAL (x) == NULL_TREE)
3659 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3660 if (cxx_dialect < cxx11)
3662 /* ARM $12.6.2: [A member initializer list] (or, for an
3663 aggregate, initialization by a brace-enclosed list) is the
3664 only way to initialize nonstatic const and reference
3665 members. */
3666 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3667 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3670 /* A field that is pseudo-const makes the structure likewise. */
3671 else if (CLASS_TYPE_P (type))
3673 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3674 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3675 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3676 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3679 /* Core issue 80: A nonstatic data member is required to have a
3680 different name from the class iff the class has a
3681 user-declared constructor. */
3682 if (constructor_name_p (DECL_NAME (x), t)
3683 && TYPE_HAS_USER_CONSTRUCTOR (t))
3684 permerror (DECL_SOURCE_LOCATION (x),
3685 "field %q#D with same name as class", x);
3688 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3689 it should also define a copy constructor and an assignment operator to
3690 implement the correct copy semantic (deep vs shallow, etc.). As it is
3691 not feasible to check whether the constructors do allocate dynamic memory
3692 and store it within members, we approximate the warning like this:
3694 -- Warn only if there are members which are pointers
3695 -- Warn only if there is a non-trivial constructor (otherwise,
3696 there cannot be memory allocated).
3697 -- Warn only if there is a non-trivial destructor. We assume that the
3698 user at least implemented the cleanup correctly, and a destructor
3699 is needed to free dynamic memory.
3701 This seems enough for practical purposes. */
3702 if (warn_ecpp
3703 && has_pointers
3704 && TYPE_HAS_USER_CONSTRUCTOR (t)
3705 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3706 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3708 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3710 if (! TYPE_HAS_COPY_CTOR (t))
3712 warning (OPT_Weffc__,
3713 " but does not override %<%T(const %T&)%>", t, t);
3714 if (!TYPE_HAS_COPY_ASSIGN (t))
3715 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3717 else if (! TYPE_HAS_COPY_ASSIGN (t))
3718 warning (OPT_Weffc__,
3719 " but does not override %<operator=(const %T&)%>", t);
3722 /* Non-static data member initializers make the default constructor
3723 non-trivial. */
3724 if (any_default_members)
3726 TYPE_NEEDS_CONSTRUCTING (t) = true;
3727 TYPE_HAS_COMPLEX_DFLT (t) = true;
3730 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3731 if (cant_pack)
3732 TYPE_PACKED (t) = 0;
3734 /* Check anonymous struct/anonymous union fields. */
3735 finish_struct_anon (t);
3737 /* We've built up the list of access declarations in reverse order.
3738 Fix that now. */
3739 *access_decls = nreverse (*access_decls);
3742 /* If TYPE is an empty class type, records its OFFSET in the table of
3743 OFFSETS. */
3745 static int
3746 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3748 splay_tree_node n;
3750 if (!is_empty_class (type))
3751 return 0;
3753 /* Record the location of this empty object in OFFSETS. */
3754 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3755 if (!n)
3756 n = splay_tree_insert (offsets,
3757 (splay_tree_key) offset,
3758 (splay_tree_value) NULL_TREE);
3759 n->value = ((splay_tree_value)
3760 tree_cons (NULL_TREE,
3761 type,
3762 (tree) n->value));
3764 return 0;
3767 /* Returns nonzero if TYPE is an empty class type and there is
3768 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3770 static int
3771 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3773 splay_tree_node n;
3774 tree t;
3776 if (!is_empty_class (type))
3777 return 0;
3779 /* Record the location of this empty object in OFFSETS. */
3780 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3781 if (!n)
3782 return 0;
3784 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3785 if (same_type_p (TREE_VALUE (t), type))
3786 return 1;
3788 return 0;
3791 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3792 F for every subobject, passing it the type, offset, and table of
3793 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3794 be traversed.
3796 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3797 than MAX_OFFSET will not be walked.
3799 If F returns a nonzero value, the traversal ceases, and that value
3800 is returned. Otherwise, returns zero. */
3802 static int
3803 walk_subobject_offsets (tree type,
3804 subobject_offset_fn f,
3805 tree offset,
3806 splay_tree offsets,
3807 tree max_offset,
3808 int vbases_p)
3810 int r = 0;
3811 tree type_binfo = NULL_TREE;
3813 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3814 stop. */
3815 if (max_offset && tree_int_cst_lt (max_offset, offset))
3816 return 0;
3818 if (type == error_mark_node)
3819 return 0;
3821 if (!TYPE_P (type))
3823 type_binfo = type;
3824 type = BINFO_TYPE (type);
3827 if (CLASS_TYPE_P (type))
3829 tree field;
3830 tree binfo;
3831 int i;
3833 /* Avoid recursing into objects that are not interesting. */
3834 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3835 return 0;
3837 /* Record the location of TYPE. */
3838 r = (*f) (type, offset, offsets);
3839 if (r)
3840 return r;
3842 /* Iterate through the direct base classes of TYPE. */
3843 if (!type_binfo)
3844 type_binfo = TYPE_BINFO (type);
3845 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3847 tree binfo_offset;
3849 if (BINFO_VIRTUAL_P (binfo))
3850 continue;
3852 tree orig_binfo;
3853 /* We cannot rely on BINFO_OFFSET being set for the base
3854 class yet, but the offsets for direct non-virtual
3855 bases can be calculated by going back to the TYPE. */
3856 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3857 binfo_offset = size_binop (PLUS_EXPR,
3858 offset,
3859 BINFO_OFFSET (orig_binfo));
3861 r = walk_subobject_offsets (binfo,
3863 binfo_offset,
3864 offsets,
3865 max_offset,
3866 /*vbases_p=*/0);
3867 if (r)
3868 return r;
3871 if (CLASSTYPE_VBASECLASSES (type))
3873 unsigned ix;
3874 vec<tree, va_gc> *vbases;
3876 /* Iterate through the virtual base classes of TYPE. In G++
3877 3.2, we included virtual bases in the direct base class
3878 loop above, which results in incorrect results; the
3879 correct offsets for virtual bases are only known when
3880 working with the most derived type. */
3881 if (vbases_p)
3882 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3883 vec_safe_iterate (vbases, ix, &binfo); ix++)
3885 r = walk_subobject_offsets (binfo,
3887 size_binop (PLUS_EXPR,
3888 offset,
3889 BINFO_OFFSET (binfo)),
3890 offsets,
3891 max_offset,
3892 /*vbases_p=*/0);
3893 if (r)
3894 return r;
3896 else
3898 /* We still have to walk the primary base, if it is
3899 virtual. (If it is non-virtual, then it was walked
3900 above.) */
3901 tree vbase = get_primary_binfo (type_binfo);
3903 if (vbase && BINFO_VIRTUAL_P (vbase)
3904 && BINFO_PRIMARY_P (vbase)
3905 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3907 r = (walk_subobject_offsets
3908 (vbase, f, offset,
3909 offsets, max_offset, /*vbases_p=*/0));
3910 if (r)
3911 return r;
3916 /* Iterate through the fields of TYPE. */
3917 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3918 if (TREE_CODE (field) == FIELD_DECL
3919 && TREE_TYPE (field) != error_mark_node
3920 && !DECL_ARTIFICIAL (field))
3922 tree field_offset;
3924 field_offset = byte_position (field);
3926 r = walk_subobject_offsets (TREE_TYPE (field),
3928 size_binop (PLUS_EXPR,
3929 offset,
3930 field_offset),
3931 offsets,
3932 max_offset,
3933 /*vbases_p=*/1);
3934 if (r)
3935 return r;
3938 else if (TREE_CODE (type) == ARRAY_TYPE)
3940 tree element_type = strip_array_types (type);
3941 tree domain = TYPE_DOMAIN (type);
3942 tree index;
3944 /* Avoid recursing into objects that are not interesting. */
3945 if (!CLASS_TYPE_P (element_type)
3946 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
3947 || !domain
3948 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
3949 return 0;
3951 /* Step through each of the elements in the array. */
3952 for (index = size_zero_node;
3953 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
3954 index = size_binop (PLUS_EXPR, index, size_one_node))
3956 r = walk_subobject_offsets (TREE_TYPE (type),
3958 offset,
3959 offsets,
3960 max_offset,
3961 /*vbases_p=*/1);
3962 if (r)
3963 return r;
3964 offset = size_binop (PLUS_EXPR, offset,
3965 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3966 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3967 there's no point in iterating through the remaining
3968 elements of the array. */
3969 if (max_offset && tree_int_cst_lt (max_offset, offset))
3970 break;
3974 return 0;
3977 /* Record all of the empty subobjects of TYPE (either a type or a
3978 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3979 is being placed at OFFSET; otherwise, it is a base class that is
3980 being placed at OFFSET. */
3982 static void
3983 record_subobject_offsets (tree type,
3984 tree offset,
3985 splay_tree offsets,
3986 bool is_data_member)
3988 tree max_offset;
3989 /* If recording subobjects for a non-static data member or a
3990 non-empty base class , we do not need to record offsets beyond
3991 the size of the biggest empty class. Additional data members
3992 will go at the end of the class. Additional base classes will go
3993 either at offset zero (if empty, in which case they cannot
3994 overlap with offsets past the size of the biggest empty class) or
3995 at the end of the class.
3997 However, if we are placing an empty base class, then we must record
3998 all offsets, as either the empty class is at offset zero (where
3999 other empty classes might later be placed) or at the end of the
4000 class (where other objects might then be placed, so other empty
4001 subobjects might later overlap). */
4002 if (is_data_member
4003 || !is_empty_class (BINFO_TYPE (type)))
4004 max_offset = sizeof_biggest_empty_class;
4005 else
4006 max_offset = NULL_TREE;
4007 walk_subobject_offsets (type, record_subobject_offset, offset,
4008 offsets, max_offset, is_data_member);
4011 /* Returns nonzero if any of the empty subobjects of TYPE (located at
4012 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4013 virtual bases of TYPE are examined. */
4015 static int
4016 layout_conflict_p (tree type,
4017 tree offset,
4018 splay_tree offsets,
4019 int vbases_p)
4021 splay_tree_node max_node;
4023 /* Get the node in OFFSETS that indicates the maximum offset where
4024 an empty subobject is located. */
4025 max_node = splay_tree_max (offsets);
4026 /* If there aren't any empty subobjects, then there's no point in
4027 performing this check. */
4028 if (!max_node)
4029 return 0;
4031 return walk_subobject_offsets (type, check_subobject_offset, offset,
4032 offsets, (tree) (max_node->key),
4033 vbases_p);
4036 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4037 non-static data member of the type indicated by RLI. BINFO is the
4038 binfo corresponding to the base subobject, OFFSETS maps offsets to
4039 types already located at those offsets. This function determines
4040 the position of the DECL. */
4042 static void
4043 layout_nonempty_base_or_field (record_layout_info rli,
4044 tree decl,
4045 tree binfo,
4046 splay_tree offsets)
4048 tree offset = NULL_TREE;
4049 bool field_p;
4050 tree type;
4052 if (binfo)
4054 /* For the purposes of determining layout conflicts, we want to
4055 use the class type of BINFO; TREE_TYPE (DECL) will be the
4056 CLASSTYPE_AS_BASE version, which does not contain entries for
4057 zero-sized bases. */
4058 type = TREE_TYPE (binfo);
4059 field_p = false;
4061 else
4063 type = TREE_TYPE (decl);
4064 field_p = true;
4067 /* Try to place the field. It may take more than one try if we have
4068 a hard time placing the field without putting two objects of the
4069 same type at the same address. */
4070 while (1)
4072 struct record_layout_info_s old_rli = *rli;
4074 /* Place this field. */
4075 place_field (rli, decl);
4076 offset = byte_position (decl);
4078 /* We have to check to see whether or not there is already
4079 something of the same type at the offset we're about to use.
4080 For example, consider:
4082 struct S {};
4083 struct T : public S { int i; };
4084 struct U : public S, public T {};
4086 Here, we put S at offset zero in U. Then, we can't put T at
4087 offset zero -- its S component would be at the same address
4088 as the S we already allocated. So, we have to skip ahead.
4089 Since all data members, including those whose type is an
4090 empty class, have nonzero size, any overlap can happen only
4091 with a direct or indirect base-class -- it can't happen with
4092 a data member. */
4093 /* In a union, overlap is permitted; all members are placed at
4094 offset zero. */
4095 if (TREE_CODE (rli->t) == UNION_TYPE)
4096 break;
4097 if (layout_conflict_p (field_p ? type : binfo, offset,
4098 offsets, field_p))
4100 /* Strip off the size allocated to this field. That puts us
4101 at the first place we could have put the field with
4102 proper alignment. */
4103 *rli = old_rli;
4105 /* Bump up by the alignment required for the type. */
4106 rli->bitpos
4107 = size_binop (PLUS_EXPR, rli->bitpos,
4108 bitsize_int (binfo
4109 ? CLASSTYPE_ALIGN (type)
4110 : TYPE_ALIGN (type)));
4111 normalize_rli (rli);
4113 else if (TREE_CODE (type) == NULLPTR_TYPE
4114 && warn_abi && abi_version_crosses (9))
4116 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4117 the offset wasn't aligned like a pointer when we started to
4118 layout this field, that affects its position. */
4119 tree pos = rli_size_unit_so_far (&old_rli);
4120 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4122 if (abi_version_at_least (9))
4123 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4124 "alignment of %qD increased in -fabi-version=9 "
4125 "(GCC 5.2)", decl);
4126 else
4127 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4128 "of %qD will increase in -fabi-version=9", decl);
4130 break;
4132 else
4133 /* There was no conflict. We're done laying out this field. */
4134 break;
4137 /* Now that we know where it will be placed, update its
4138 BINFO_OFFSET. */
4139 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4140 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4141 this point because their BINFO_OFFSET is copied from another
4142 hierarchy. Therefore, we may not need to add the entire
4143 OFFSET. */
4144 propagate_binfo_offsets (binfo,
4145 size_diffop_loc (input_location,
4146 fold_convert (ssizetype, offset),
4147 fold_convert (ssizetype,
4148 BINFO_OFFSET (binfo))));
4151 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4153 static int
4154 empty_base_at_nonzero_offset_p (tree type,
4155 tree offset,
4156 splay_tree /*offsets*/)
4158 return is_empty_class (type) && !integer_zerop (offset);
4161 /* Layout the empty base BINFO. EOC indicates the byte currently just
4162 past the end of the class, and should be correctly aligned for a
4163 class of the type indicated by BINFO; OFFSETS gives the offsets of
4164 the empty bases allocated so far. T is the most derived
4165 type. Return nonzero iff we added it at the end. */
4167 static bool
4168 layout_empty_base (record_layout_info rli, tree binfo,
4169 tree eoc, splay_tree offsets)
4171 tree alignment;
4172 tree basetype = BINFO_TYPE (binfo);
4173 bool atend = false;
4175 /* This routine should only be used for empty classes. */
4176 gcc_assert (is_empty_class (basetype));
4177 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4179 if (!integer_zerop (BINFO_OFFSET (binfo)))
4180 propagate_binfo_offsets
4181 (binfo, size_diffop_loc (input_location,
4182 size_zero_node, BINFO_OFFSET (binfo)));
4184 /* This is an empty base class. We first try to put it at offset
4185 zero. */
4186 if (layout_conflict_p (binfo,
4187 BINFO_OFFSET (binfo),
4188 offsets,
4189 /*vbases_p=*/0))
4191 /* That didn't work. Now, we move forward from the next
4192 available spot in the class. */
4193 atend = true;
4194 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4195 while (1)
4197 if (!layout_conflict_p (binfo,
4198 BINFO_OFFSET (binfo),
4199 offsets,
4200 /*vbases_p=*/0))
4201 /* We finally found a spot where there's no overlap. */
4202 break;
4204 /* There's overlap here, too. Bump along to the next spot. */
4205 propagate_binfo_offsets (binfo, alignment);
4209 if (CLASSTYPE_USER_ALIGN (basetype))
4211 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4212 if (warn_packed)
4213 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4214 TYPE_USER_ALIGN (rli->t) = 1;
4217 return atend;
4220 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4221 fields at NEXT_FIELD, and return it. */
4223 static tree
4224 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4226 /* Create the FIELD_DECL. */
4227 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4228 tree decl = build_decl (input_location,
4229 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4230 DECL_ARTIFICIAL (decl) = 1;
4231 DECL_IGNORED_P (decl) = 1;
4232 DECL_FIELD_CONTEXT (decl) = t;
4233 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4234 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4235 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4236 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4237 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4238 DECL_FIELD_IS_BASE (decl) = 1;
4240 /* Add the new FIELD_DECL to the list of fields for T. */
4241 DECL_CHAIN (decl) = *next_field;
4242 *next_field = decl;
4243 next_field = &DECL_CHAIN (decl);
4245 return decl;
4248 /* Layout the base given by BINFO in the class indicated by RLI.
4249 *BASE_ALIGN is a running maximum of the alignments of
4250 any base class. OFFSETS gives the location of empty base
4251 subobjects. T is the most derived type. Return nonzero if the new
4252 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4253 *NEXT_FIELD, unless BINFO is for an empty base class.
4255 Returns the location at which the next field should be inserted. */
4257 static tree *
4258 build_base_field (record_layout_info rli, tree binfo,
4259 splay_tree offsets, tree *next_field)
4261 tree t = rli->t;
4262 tree basetype = BINFO_TYPE (binfo);
4264 if (!COMPLETE_TYPE_P (basetype))
4265 /* This error is now reported in xref_tag, thus giving better
4266 location information. */
4267 return next_field;
4269 /* Place the base class. */
4270 if (!is_empty_class (basetype))
4272 tree decl;
4274 /* The containing class is non-empty because it has a non-empty
4275 base class. */
4276 CLASSTYPE_EMPTY_P (t) = 0;
4278 /* Create the FIELD_DECL. */
4279 decl = build_base_field_1 (t, basetype, next_field);
4281 /* Try to place the field. It may take more than one try if we
4282 have a hard time placing the field without putting two
4283 objects of the same type at the same address. */
4284 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4286 else
4288 tree eoc;
4289 bool atend;
4291 /* On some platforms (ARM), even empty classes will not be
4292 byte-aligned. */
4293 eoc = round_up_loc (input_location,
4294 rli_size_unit_so_far (rli),
4295 CLASSTYPE_ALIGN_UNIT (basetype));
4296 atend = layout_empty_base (rli, binfo, eoc, offsets);
4297 /* A nearly-empty class "has no proper base class that is empty,
4298 not morally virtual, and at an offset other than zero." */
4299 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4301 if (atend)
4302 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4303 /* The check above (used in G++ 3.2) is insufficient because
4304 an empty class placed at offset zero might itself have an
4305 empty base at a nonzero offset. */
4306 else if (walk_subobject_offsets (basetype,
4307 empty_base_at_nonzero_offset_p,
4308 size_zero_node,
4309 /*offsets=*/NULL,
4310 /*max_offset=*/NULL_TREE,
4311 /*vbases_p=*/true))
4312 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4315 /* We used to not create a FIELD_DECL for empty base classes because of
4316 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4317 be a problem anymore. We need them to handle initialization of C++17
4318 aggregate bases. */
4319 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4321 tree decl = build_base_field_1 (t, basetype, next_field);
4322 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4323 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4324 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4327 /* An empty virtual base causes a class to be non-empty
4328 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4329 here because that was already done when the virtual table
4330 pointer was created. */
4333 /* Record the offsets of BINFO and its base subobjects. */
4334 record_subobject_offsets (binfo,
4335 BINFO_OFFSET (binfo),
4336 offsets,
4337 /*is_data_member=*/false);
4339 return next_field;
4342 /* Layout all of the non-virtual base classes. Record empty
4343 subobjects in OFFSETS. T is the most derived type. Return nonzero
4344 if the type cannot be nearly empty. The fields created
4345 corresponding to the base classes will be inserted at
4346 *NEXT_FIELD. */
4348 static void
4349 build_base_fields (record_layout_info rli,
4350 splay_tree offsets, tree *next_field)
4352 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4353 subobjects. */
4354 tree t = rli->t;
4355 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4356 int i;
4358 /* The primary base class is always allocated first. */
4359 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4360 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4361 offsets, next_field);
4363 /* Now allocate the rest of the bases. */
4364 for (i = 0; i < n_baseclasses; ++i)
4366 tree base_binfo;
4368 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4370 /* The primary base was already allocated above, so we don't
4371 need to allocate it again here. */
4372 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4373 continue;
4375 /* Virtual bases are added at the end (a primary virtual base
4376 will have already been added). */
4377 if (BINFO_VIRTUAL_P (base_binfo))
4378 continue;
4380 next_field = build_base_field (rli, base_binfo,
4381 offsets, next_field);
4385 /* Go through the TYPE_FIELDS of T issuing any appropriate
4386 diagnostics, figuring out which methods override which other
4387 methods, and so forth. */
4389 static void
4390 check_methods (tree t)
4392 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4393 if (DECL_DECLARES_FUNCTION_P (x))
4395 check_for_override (x, t);
4397 if (DECL_PURE_VIRTUAL_P (x)
4398 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4399 error ("initializer specified for non-virtual method %q+D", x);
4400 /* The name of the field is the original field name
4401 Save this in auxiliary field for later overloading. */
4402 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4404 TYPE_POLYMORPHIC_P (t) = 1;
4405 if (DECL_PURE_VIRTUAL_P (x))
4406 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4409 /* All user-provided destructors are non-trivial.
4410 Constructors and assignment ops are handled in
4411 grok_special_member_properties. */
4412 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4413 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4414 if (!DECL_VIRTUAL_P (x)
4415 && lookup_attribute ("transaction_safe_dynamic",
4416 DECL_ATTRIBUTES (x)))
4417 error_at (DECL_SOURCE_LOCATION (x),
4418 "%<transaction_safe_dynamic%> may only be specified for "
4419 "a virtual function");
4423 /* FN is a constructor or destructor. Clone the declaration to create
4424 a specialized in-charge or not-in-charge version, as indicated by
4425 NAME. */
4427 static tree
4428 build_clone (tree fn, tree name)
4430 tree parms;
4431 tree clone;
4433 /* Copy the function. */
4434 clone = copy_decl (fn);
4435 /* Reset the function name. */
4436 DECL_NAME (clone) = name;
4437 /* Remember where this function came from. */
4438 DECL_ABSTRACT_ORIGIN (clone) = fn;
4439 /* Make it easy to find the CLONE given the FN. */
4440 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4441 DECL_CHAIN (fn) = clone;
4443 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4444 if (TREE_CODE (clone) == TEMPLATE_DECL)
4446 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4447 DECL_TEMPLATE_RESULT (clone) = result;
4448 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4449 DECL_TI_TEMPLATE (result) = clone;
4450 TREE_TYPE (clone) = TREE_TYPE (result);
4451 return clone;
4453 else
4455 // Clone constraints.
4456 if (flag_concepts)
4457 if (tree ci = get_constraints (fn))
4458 set_constraints (clone, copy_node (ci));
4462 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4463 DECL_CLONED_FUNCTION (clone) = fn;
4464 /* There's no pending inline data for this function. */
4465 DECL_PENDING_INLINE_INFO (clone) = NULL;
4466 DECL_PENDING_INLINE_P (clone) = 0;
4468 /* The base-class destructor is not virtual. */
4469 if (name == base_dtor_identifier)
4471 DECL_VIRTUAL_P (clone) = 0;
4472 if (TREE_CODE (clone) != TEMPLATE_DECL)
4473 DECL_VINDEX (clone) = NULL_TREE;
4476 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4477 if (ctor_omit_inherited_parms_p)
4478 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4480 /* If there was an in-charge parameter, drop it from the function
4481 type. */
4482 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4484 tree basetype;
4485 tree parmtypes;
4486 tree exceptions;
4488 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4489 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4490 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4491 /* Skip the `this' parameter. */
4492 parmtypes = TREE_CHAIN (parmtypes);
4493 /* Skip the in-charge parameter. */
4494 parmtypes = TREE_CHAIN (parmtypes);
4495 /* And the VTT parm, in a complete [cd]tor. */
4496 if (DECL_HAS_VTT_PARM_P (fn)
4497 && ! DECL_NEEDS_VTT_PARM_P (clone))
4498 parmtypes = TREE_CHAIN (parmtypes);
4499 if (ctor_omit_inherited_parms_p)
4501 /* If we're omitting inherited parms, that just leaves the VTT. */
4502 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4503 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4505 TREE_TYPE (clone)
4506 = build_method_type_directly (basetype,
4507 TREE_TYPE (TREE_TYPE (clone)),
4508 parmtypes);
4509 if (exceptions)
4510 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4511 exceptions);
4512 TREE_TYPE (clone)
4513 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4514 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4517 /* Copy the function parameters. */
4518 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4519 /* Remove the in-charge parameter. */
4520 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4522 DECL_CHAIN (DECL_ARGUMENTS (clone))
4523 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4524 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4526 /* And the VTT parm, in a complete [cd]tor. */
4527 if (DECL_HAS_VTT_PARM_P (fn))
4529 if (DECL_NEEDS_VTT_PARM_P (clone))
4530 DECL_HAS_VTT_PARM_P (clone) = 1;
4531 else
4533 DECL_CHAIN (DECL_ARGUMENTS (clone))
4534 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4535 DECL_HAS_VTT_PARM_P (clone) = 0;
4539 /* A base constructor inheriting from a virtual base doesn't get the
4540 arguments. */
4541 if (ctor_omit_inherited_parms_p)
4542 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4544 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4546 DECL_CONTEXT (parms) = clone;
4547 cxx_dup_lang_specific_decl (parms);
4550 /* Create the RTL for this function. */
4551 SET_DECL_RTL (clone, NULL);
4552 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4554 return clone;
4557 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4558 not invoke this function directly.
4560 For a non-thunk function, returns the address of the slot for storing
4561 the function it is a clone of. Otherwise returns NULL_TREE.
4563 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4564 cloned_function is unset. This is to support the separate
4565 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4566 on a template makes sense, but not the former. */
4568 tree *
4569 decl_cloned_function_p (const_tree decl, bool just_testing)
4571 tree *ptr;
4572 if (just_testing)
4573 decl = STRIP_TEMPLATE (decl);
4575 if (TREE_CODE (decl) != FUNCTION_DECL
4576 || !DECL_LANG_SPECIFIC (decl)
4577 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4579 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4580 if (!just_testing)
4581 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4582 else
4583 #endif
4584 return NULL;
4587 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4588 if (just_testing && *ptr == NULL_TREE)
4589 return NULL;
4590 else
4591 return ptr;
4594 /* Produce declarations for all appropriate clones of FN. If
4595 UPDATE_METHODS is true, the clones are added to the
4596 CLASSTYPE_MEMBER_VEC. */
4598 void
4599 clone_function_decl (tree fn, bool update_methods)
4601 tree clone;
4603 /* Avoid inappropriate cloning. */
4604 if (DECL_CHAIN (fn)
4605 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4606 return;
4608 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4610 /* For each constructor, we need two variants: an in-charge version
4611 and a not-in-charge version. */
4612 clone = build_clone (fn, complete_ctor_identifier);
4613 if (update_methods)
4614 add_method (DECL_CONTEXT (clone), clone, false);
4615 clone = build_clone (fn, base_ctor_identifier);
4616 if (update_methods)
4617 add_method (DECL_CONTEXT (clone), clone, false);
4619 else
4621 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4623 /* For each destructor, we need three variants: an in-charge
4624 version, a not-in-charge version, and an in-charge deleting
4625 version. We clone the deleting version first because that
4626 means it will go second on the TYPE_FIELDS list -- and that
4627 corresponds to the correct layout order in the virtual
4628 function table.
4630 For a non-virtual destructor, we do not build a deleting
4631 destructor. */
4632 if (DECL_VIRTUAL_P (fn))
4634 clone = build_clone (fn, deleting_dtor_identifier);
4635 if (update_methods)
4636 add_method (DECL_CONTEXT (clone), clone, false);
4638 clone = build_clone (fn, complete_dtor_identifier);
4639 if (update_methods)
4640 add_method (DECL_CONTEXT (clone), clone, false);
4641 clone = build_clone (fn, base_dtor_identifier);
4642 if (update_methods)
4643 add_method (DECL_CONTEXT (clone), clone, false);
4646 /* Note that this is an abstract function that is never emitted. */
4647 DECL_ABSTRACT_P (fn) = true;
4650 /* DECL is an in charge constructor, which is being defined. This will
4651 have had an in class declaration, from whence clones were
4652 declared. An out-of-class definition can specify additional default
4653 arguments. As it is the clones that are involved in overload
4654 resolution, we must propagate the information from the DECL to its
4655 clones. */
4657 void
4658 adjust_clone_args (tree decl)
4660 tree clone;
4662 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4663 clone = DECL_CHAIN (clone))
4665 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4666 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4667 tree decl_parms, clone_parms;
4669 clone_parms = orig_clone_parms;
4671 /* Skip the 'this' parameter. */
4672 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4673 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4675 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4676 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4677 if (DECL_HAS_VTT_PARM_P (decl))
4678 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4680 clone_parms = orig_clone_parms;
4681 if (DECL_HAS_VTT_PARM_P (clone))
4682 clone_parms = TREE_CHAIN (clone_parms);
4684 for (decl_parms = orig_decl_parms; decl_parms;
4685 decl_parms = TREE_CHAIN (decl_parms),
4686 clone_parms = TREE_CHAIN (clone_parms))
4688 if (clone_parms == void_list_node)
4690 gcc_assert (decl_parms == clone_parms
4691 || ctor_omit_inherited_parms (clone));
4692 break;
4695 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4696 TREE_TYPE (clone_parms)));
4698 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4700 /* A default parameter has been added. Adjust the
4701 clone's parameters. */
4702 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4703 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone));
4704 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4705 tree type;
4707 clone_parms = orig_decl_parms;
4709 if (DECL_HAS_VTT_PARM_P (clone))
4711 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4712 TREE_VALUE (orig_clone_parms),
4713 clone_parms);
4714 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4716 type = build_method_type_directly (basetype,
4717 TREE_TYPE (TREE_TYPE (clone)),
4718 clone_parms);
4719 if (exceptions)
4720 type = build_exception_variant (type, exceptions);
4721 if (attrs)
4722 type = cp_build_type_attribute_variant (type, attrs);
4723 TREE_TYPE (clone) = type;
4725 clone_parms = NULL_TREE;
4726 break;
4729 gcc_assert (!clone_parms || clone_parms == void_list_node);
4733 /* For each of the constructors and destructors in T, create an
4734 in-charge and not-in-charge variant. */
4736 static void
4737 clone_constructors_and_destructors (tree t)
4739 /* While constructors can be via a using declaration, at this point
4740 we no longer need to know that. */
4741 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4742 clone_function_decl (*iter, /*update_methods=*/true);
4744 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4745 clone_function_decl (dtor, /*update_methods=*/true);
4748 /* Deduce noexcept for a destructor DTOR. */
4750 void
4751 deduce_noexcept_on_destructor (tree dtor)
4753 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4754 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
4755 noexcept_deferred_spec);
4758 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4759 of TYPE for virtual functions which FNDECL overrides. Return a
4760 mask of the tm attributes found therein. */
4762 static int
4763 look_for_tm_attr_overrides (tree type, tree fndecl)
4765 tree binfo = TYPE_BINFO (type);
4766 tree base_binfo;
4767 int ix, found = 0;
4769 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4771 tree o, basetype = BINFO_TYPE (base_binfo);
4773 if (!TYPE_POLYMORPHIC_P (basetype))
4774 continue;
4776 o = look_for_overrides_here (basetype, fndecl);
4777 if (o)
4779 if (lookup_attribute ("transaction_safe_dynamic",
4780 DECL_ATTRIBUTES (o)))
4781 /* transaction_safe_dynamic is not inherited. */;
4782 else
4783 found |= tm_attr_to_mask (find_tm_attribute
4784 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4786 else
4787 found |= look_for_tm_attr_overrides (basetype, fndecl);
4790 return found;
4793 /* Subroutine of set_method_tm_attributes. Handle the checks and
4794 inheritance for one virtual method FNDECL. */
4796 static void
4797 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4799 tree tm_attr;
4800 int found, have;
4802 found = look_for_tm_attr_overrides (type, fndecl);
4804 /* If FNDECL doesn't actually override anything (i.e. T is the
4805 class that first declares FNDECL virtual), then we're done. */
4806 if (found == 0)
4807 return;
4809 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4810 have = tm_attr_to_mask (tm_attr);
4812 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4813 tm_pure must match exactly, otherwise no weakening of
4814 tm_safe > tm_callable > nothing. */
4815 /* ??? The tm_pure attribute didn't make the transition to the
4816 multivendor language spec. */
4817 if (have == TM_ATTR_PURE)
4819 if (found != TM_ATTR_PURE)
4821 found &= -found;
4822 goto err_override;
4825 /* If the overridden function is tm_pure, then FNDECL must be. */
4826 else if (found == TM_ATTR_PURE && tm_attr)
4827 goto err_override;
4828 /* Look for base class combinations that cannot be satisfied. */
4829 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4831 found &= ~TM_ATTR_PURE;
4832 found &= -found;
4833 error_at (DECL_SOURCE_LOCATION (fndecl),
4834 "method overrides both %<transaction_pure%> and %qE methods",
4835 tm_mask_to_attr (found));
4837 /* If FNDECL did not declare an attribute, then inherit the most
4838 restrictive one. */
4839 else if (tm_attr == NULL)
4841 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
4843 /* Otherwise validate that we're not weaker than a function
4844 that is being overridden. */
4845 else
4847 found &= -found;
4848 if (found <= TM_ATTR_CALLABLE && have > found)
4849 goto err_override;
4851 return;
4853 err_override:
4854 error_at (DECL_SOURCE_LOCATION (fndecl),
4855 "method declared %qE overriding %qE method",
4856 tm_attr, tm_mask_to_attr (found));
4859 /* For each of the methods in T, propagate a class-level tm attribute. */
4861 static void
4862 set_method_tm_attributes (tree t)
4864 tree class_tm_attr, fndecl;
4866 /* Don't bother collecting tm attributes if transactional memory
4867 support is not enabled. */
4868 if (!flag_tm)
4869 return;
4871 /* Process virtual methods first, as they inherit directly from the
4872 base virtual function and also require validation of new attributes. */
4873 if (TYPE_CONTAINS_VPTR_P (t))
4875 tree vchain;
4876 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4877 vchain = TREE_CHAIN (vchain))
4879 fndecl = BV_FN (vchain);
4880 if (DECL_THUNK_P (fndecl))
4881 fndecl = THUNK_TARGET (fndecl);
4882 set_one_vmethod_tm_attributes (t, fndecl);
4886 /* If the class doesn't have an attribute, nothing more to do. */
4887 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4888 if (class_tm_attr == NULL)
4889 return;
4891 /* Any method that does not yet have a tm attribute inherits
4892 the one from the class. */
4893 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
4894 if (DECL_DECLARES_FUNCTION_P (fndecl)
4895 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4896 apply_tm_attr (fndecl, class_tm_attr);
4899 /* Returns true if FN is a default constructor. */
4901 bool
4902 default_ctor_p (tree fn)
4904 return (DECL_CONSTRUCTOR_P (fn)
4905 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
4908 /* Returns true iff class T has a user-defined constructor that can be called
4909 with more than zero arguments. */
4911 bool
4912 type_has_user_nondefault_constructor (tree t)
4914 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4915 return false;
4917 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4919 tree fn = *iter;
4920 if (!DECL_ARTIFICIAL (fn)
4921 && (TREE_CODE (fn) == TEMPLATE_DECL
4922 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4923 != NULL_TREE)))
4924 return true;
4927 return false;
4930 /* Returns the defaulted constructor if T has one. Otherwise, returns
4931 NULL_TREE. */
4933 tree
4934 in_class_defaulted_default_constructor (tree t)
4936 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4937 return NULL_TREE;
4939 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4941 tree fn = *iter;
4943 if (DECL_DEFAULTED_IN_CLASS_P (fn)
4944 && default_ctor_p (fn))
4945 return fn;
4948 return NULL_TREE;
4951 /* Returns true iff FN is a user-provided function, i.e. user-declared
4952 and not defaulted at its first declaration. */
4954 bool
4955 user_provided_p (tree fn)
4957 if (TREE_CODE (fn) == TEMPLATE_DECL)
4958 return true;
4959 else
4960 return (!DECL_ARTIFICIAL (fn)
4961 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4962 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4965 /* Returns true iff class T has a user-provided constructor. */
4967 bool
4968 type_has_user_provided_constructor (tree t)
4970 if (!CLASS_TYPE_P (t))
4971 return false;
4973 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4974 return false;
4976 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4977 if (user_provided_p (*iter))
4978 return true;
4980 return false;
4983 /* Returns true iff class T has a user-provided or explicit constructor. */
4985 bool
4986 type_has_user_provided_or_explicit_constructor (tree t)
4988 if (!CLASS_TYPE_P (t))
4989 return false;
4991 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4992 return false;
4994 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4996 tree fn = *iter;
4997 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
4998 return true;
5001 return false;
5004 /* Returns true iff class T has a non-user-provided (i.e. implicitly
5005 declared or explicitly defaulted in the class body) default
5006 constructor. */
5008 bool
5009 type_has_non_user_provided_default_constructor (tree t)
5011 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
5012 return false;
5013 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5014 return true;
5016 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5018 tree fn = *iter;
5019 if (TREE_CODE (fn) == FUNCTION_DECL
5020 && default_ctor_p (fn)
5021 && !user_provided_p (fn))
5022 return true;
5025 return false;
5028 /* TYPE is being used as a virtual base, and has a non-trivial move
5029 assignment. Return true if this is due to there being a user-provided
5030 move assignment in TYPE or one of its subobjects; if there isn't, then
5031 multiple move assignment can't cause any harm. */
5033 bool
5034 vbase_has_user_provided_move_assign (tree type)
5036 /* Does the type itself have a user-provided move assignment operator? */
5037 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5038 for (ovl_iterator iter (get_class_binding_direct
5039 (type, assign_op_identifier));
5040 iter; ++iter)
5041 if (!DECL_ARTIFICIAL (*iter) && move_fn_p (*iter))
5042 return true;
5044 /* Do any of its bases? */
5045 tree binfo = TYPE_BINFO (type);
5046 tree base_binfo;
5047 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5048 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5049 return true;
5051 /* Or non-static data members? */
5052 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5054 if (TREE_CODE (field) == FIELD_DECL
5055 && CLASS_TYPE_P (TREE_TYPE (field))
5056 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5057 return true;
5060 /* Seems not. */
5061 return false;
5064 /* If default-initialization leaves part of TYPE uninitialized, returns
5065 a DECL for the field or TYPE itself (DR 253). */
5067 tree
5068 default_init_uninitialized_part (tree type)
5070 tree t, r, binfo;
5071 int i;
5073 type = strip_array_types (type);
5074 if (!CLASS_TYPE_P (type))
5075 return type;
5076 if (!type_has_non_user_provided_default_constructor (type))
5077 return NULL_TREE;
5078 for (binfo = TYPE_BINFO (type), i = 0;
5079 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5081 r = default_init_uninitialized_part (BINFO_TYPE (t));
5082 if (r)
5083 return r;
5085 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5086 if (TREE_CODE (t) == FIELD_DECL
5087 && !DECL_ARTIFICIAL (t)
5088 && !DECL_INITIAL (t))
5090 r = default_init_uninitialized_part (TREE_TYPE (t));
5091 if (r)
5092 return DECL_P (r) ? r : t;
5095 return NULL_TREE;
5098 /* Returns true iff for class T, a trivial synthesized default constructor
5099 would be constexpr. */
5101 bool
5102 trivial_default_constructor_is_constexpr (tree t)
5104 /* A defaulted trivial default constructor is constexpr
5105 if there is nothing to initialize. */
5106 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5107 return is_really_empty_class (t);
5110 /* Returns true iff class T has a constexpr default constructor. */
5112 bool
5113 type_has_constexpr_default_constructor (tree t)
5115 tree fns;
5117 if (!CLASS_TYPE_P (t))
5119 /* The caller should have stripped an enclosing array. */
5120 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5121 return false;
5123 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5125 if (!TYPE_HAS_COMPLEX_DFLT (t))
5126 return trivial_default_constructor_is_constexpr (t);
5127 /* Non-trivial, we need to check subobject constructors. */
5128 lazily_declare_fn (sfk_constructor, t);
5130 fns = locate_ctor (t);
5131 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5134 /* Returns true iff class T has a constexpr default constructor or has an
5135 implicitly declared default constructor that we can't tell if it's constexpr
5136 without forcing a lazy declaration (which might cause undesired
5137 instantiations). */
5139 bool
5140 type_maybe_constexpr_default_constructor (tree t)
5142 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5143 && TYPE_HAS_COMPLEX_DFLT (t))
5144 /* Assume it's constexpr. */
5145 return true;
5146 return type_has_constexpr_default_constructor (t);
5149 /* Returns true iff class TYPE has a virtual destructor. */
5151 bool
5152 type_has_virtual_destructor (tree type)
5154 tree dtor;
5156 if (!CLASS_TYPE_P (type))
5157 return false;
5159 gcc_assert (COMPLETE_TYPE_P (type));
5160 dtor = CLASSTYPE_DESTRUCTOR (type);
5161 return (dtor && DECL_VIRTUAL_P (dtor));
5164 /* Returns true iff T, a class, has a move-assignment or
5165 move-constructor. Does not lazily declare either.
5166 If USER_P is false, any move function will do. If it is true, the
5167 move function must be user-declared.
5169 Note that user-declared here is different from "user-provided",
5170 which doesn't include functions that are defaulted in the
5171 class. */
5173 bool
5174 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5176 gcc_assert (user_p
5177 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5178 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5180 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5181 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5182 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5183 return true;
5185 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5186 for (ovl_iterator iter (get_class_binding_direct
5187 (t, assign_op_identifier));
5188 iter; ++iter)
5189 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5190 return true;
5192 return false;
5195 /* Nonzero if we need to build up a constructor call when initializing an
5196 object of this class, either because it has a user-declared constructor
5197 or because it doesn't have a default constructor (so we need to give an
5198 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5199 what you care about is whether or not an object can be produced by a
5200 constructor (e.g. so we don't set TREE_READONLY on const variables of
5201 such type); use this function when what you care about is whether or not
5202 to try to call a constructor to create an object. The latter case is
5203 the former plus some cases of constructors that cannot be called. */
5205 bool
5206 type_build_ctor_call (tree t)
5208 tree inner;
5209 if (TYPE_NEEDS_CONSTRUCTING (t))
5210 return true;
5211 inner = strip_array_types (t);
5212 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5213 return false;
5214 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5215 return true;
5216 if (cxx_dialect < cxx11)
5217 return false;
5218 /* A user-declared constructor might be private, and a constructor might
5219 be trivial but deleted. */
5220 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5221 iter; ++iter)
5223 tree fn = *iter;
5224 if (!DECL_ARTIFICIAL (fn)
5225 || DECL_DELETED_FN (fn))
5226 return true;
5228 return false;
5231 /* Like type_build_ctor_call, but for destructors. */
5233 bool
5234 type_build_dtor_call (tree t)
5236 tree inner;
5237 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5238 return true;
5239 inner = strip_array_types (t);
5240 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5241 || !COMPLETE_TYPE_P (inner))
5242 return false;
5243 if (cxx_dialect < cxx11)
5244 return false;
5245 /* A user-declared destructor might be private, and a destructor might
5246 be trivial but deleted. */
5247 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5248 iter; ++iter)
5250 tree fn = *iter;
5251 if (!DECL_ARTIFICIAL (fn)
5252 || DECL_DELETED_FN (fn))
5253 return true;
5255 return false;
5258 /* Remove all zero-width bit-fields from T. */
5260 static void
5261 remove_zero_width_bit_fields (tree t)
5263 tree *fieldsp;
5265 fieldsp = &TYPE_FIELDS (t);
5266 while (*fieldsp)
5268 if (TREE_CODE (*fieldsp) == FIELD_DECL
5269 && DECL_C_BIT_FIELD (*fieldsp)
5270 /* We should not be confused by the fact that grokbitfield
5271 temporarily sets the width of the bit field into
5272 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5273 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5274 to that width. */
5275 && (DECL_SIZE (*fieldsp) == NULL_TREE
5276 || integer_zerop (DECL_SIZE (*fieldsp))))
5277 *fieldsp = DECL_CHAIN (*fieldsp);
5278 else
5279 fieldsp = &DECL_CHAIN (*fieldsp);
5283 /* Returns TRUE iff we need a cookie when dynamically allocating an
5284 array whose elements have the indicated class TYPE. */
5286 static bool
5287 type_requires_array_cookie (tree type)
5289 tree fns;
5290 bool has_two_argument_delete_p = false;
5292 gcc_assert (CLASS_TYPE_P (type));
5294 /* If there's a non-trivial destructor, we need a cookie. In order
5295 to iterate through the array calling the destructor for each
5296 element, we'll have to know how many elements there are. */
5297 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5298 return true;
5300 /* If the usual deallocation function is a two-argument whose second
5301 argument is of type `size_t', then we have to pass the size of
5302 the array to the deallocation function, so we will need to store
5303 a cookie. */
5304 fns = lookup_fnfields (TYPE_BINFO (type),
5305 ovl_op_identifier (false, VEC_DELETE_EXPR),
5306 /*protect=*/0);
5307 /* If there are no `operator []' members, or the lookup is
5308 ambiguous, then we don't need a cookie. */
5309 if (!fns || fns == error_mark_node)
5310 return false;
5311 /* Loop through all of the functions. */
5312 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5314 tree fn = *iter;
5316 /* See if this function is a one-argument delete function. If
5317 it is, then it will be the usual deallocation function. */
5318 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5319 if (second_parm == void_list_node)
5320 return false;
5321 /* Do not consider this function if its second argument is an
5322 ellipsis. */
5323 if (!second_parm)
5324 continue;
5325 /* Otherwise, if we have a two-argument function and the second
5326 argument is `size_t', it will be the usual deallocation
5327 function -- unless there is one-argument function, too. */
5328 if (TREE_CHAIN (second_parm) == void_list_node
5329 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5330 has_two_argument_delete_p = true;
5333 return has_two_argument_delete_p;
5336 /* Finish computing the `literal type' property of class type T.
5338 At this point, we have already processed base classes and
5339 non-static data members. We need to check whether the copy
5340 constructor is trivial, the destructor is trivial, and there
5341 is a trivial default constructor or at least one constexpr
5342 constructor other than the copy constructor. */
5344 static void
5345 finalize_literal_type_property (tree t)
5347 tree fn;
5349 if (cxx_dialect < cxx11
5350 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5351 CLASSTYPE_LITERAL_P (t) = false;
5352 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5353 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5354 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5355 && CLASSTYPE_NON_AGGREGATE (t)
5356 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5357 CLASSTYPE_LITERAL_P (t) = false;
5359 /* C++14 DR 1684 removed this restriction. */
5360 if (cxx_dialect < cxx14
5361 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5362 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5363 if (TREE_CODE (fn) == FUNCTION_DECL
5364 && DECL_DECLARED_CONSTEXPR_P (fn)
5365 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5366 && !DECL_CONSTRUCTOR_P (fn))
5368 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5369 if (!DECL_GENERATED_P (fn)
5370 && pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5371 "enclosing class of %<constexpr%> non-static member "
5372 "function %q+#D is not a literal type", fn))
5373 explain_non_literal_class (t);
5377 /* T is a non-literal type used in a context which requires a constant
5378 expression. Explain why it isn't literal. */
5380 void
5381 explain_non_literal_class (tree t)
5383 static hash_set<tree> *diagnosed;
5385 if (!CLASS_TYPE_P (t))
5386 return;
5387 t = TYPE_MAIN_VARIANT (t);
5389 if (diagnosed == NULL)
5390 diagnosed = new hash_set<tree>;
5391 if (diagnosed->add (t))
5392 /* Already explained. */
5393 return;
5395 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5396 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5397 inform (UNKNOWN_LOCATION,
5398 " %qT is a closure type, which is only literal in "
5399 "C++17 and later", t);
5400 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5401 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5402 else if (CLASSTYPE_NON_AGGREGATE (t)
5403 && !TYPE_HAS_TRIVIAL_DFLT (t)
5404 && !LAMBDA_TYPE_P (t)
5405 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5407 inform (UNKNOWN_LOCATION,
5408 " %q+T is not an aggregate, does not have a trivial "
5409 "default constructor, and has no %<constexpr%> constructor that "
5410 "is not a copy or move constructor", t);
5411 if (type_has_non_user_provided_default_constructor (t))
5412 /* Note that we can't simply call locate_ctor because when the
5413 constructor is deleted it just returns NULL_TREE. */
5414 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5416 tree fn = *iter;
5417 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5419 parms = skip_artificial_parms_for (fn, parms);
5421 if (sufficient_parms_p (parms))
5423 if (DECL_DELETED_FN (fn))
5424 maybe_explain_implicit_delete (fn);
5425 else
5426 explain_invalid_constexpr_fn (fn);
5427 break;
5431 else
5433 tree binfo, base_binfo, field; int i;
5434 for (binfo = TYPE_BINFO (t), i = 0;
5435 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5437 tree basetype = TREE_TYPE (base_binfo);
5438 if (!CLASSTYPE_LITERAL_P (basetype))
5440 inform (UNKNOWN_LOCATION,
5441 " base class %qT of %q+T is non-literal",
5442 basetype, t);
5443 explain_non_literal_class (basetype);
5444 return;
5447 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5449 tree ftype;
5450 if (TREE_CODE (field) != FIELD_DECL)
5451 continue;
5452 ftype = TREE_TYPE (field);
5453 if (!literal_type_p (ftype))
5455 inform (DECL_SOURCE_LOCATION (field),
5456 " non-static data member %qD has non-literal type",
5457 field);
5458 if (CLASS_TYPE_P (ftype))
5459 explain_non_literal_class (ftype);
5461 if (CP_TYPE_VOLATILE_P (ftype))
5462 inform (DECL_SOURCE_LOCATION (field),
5463 " non-static data member %qD has volatile type", field);
5468 /* Check the validity of the bases and members declared in T. Add any
5469 implicitly-generated functions (like copy-constructors and
5470 assignment operators). Compute various flag bits (like
5471 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5472 level: i.e., independently of the ABI in use. */
5474 static void
5475 check_bases_and_members (tree t)
5477 /* Nonzero if the implicitly generated copy constructor should take
5478 a non-const reference argument. */
5479 int cant_have_const_ctor;
5480 /* Nonzero if the implicitly generated assignment operator
5481 should take a non-const reference argument. */
5482 int no_const_asn_ref;
5483 tree access_decls;
5484 bool saved_complex_asn_ref;
5485 bool saved_nontrivial_dtor;
5486 tree fn;
5488 /* By default, we use const reference arguments and generate default
5489 constructors. */
5490 cant_have_const_ctor = 0;
5491 no_const_asn_ref = 0;
5493 /* Check all the base-classes and set FMEM members to point to arrays
5494 of potential interest. */
5495 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5497 /* Deduce noexcept on destructor. This needs to happen after we've set
5498 triviality flags appropriately for our bases. */
5499 if (cxx_dialect >= cxx11)
5500 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5501 deduce_noexcept_on_destructor (dtor);
5503 /* Check all the method declarations. */
5504 check_methods (t);
5506 /* Save the initial values of these flags which only indicate whether
5507 or not the class has user-provided functions. As we analyze the
5508 bases and members we can set these flags for other reasons. */
5509 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5510 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5512 /* Check all the data member declarations. We cannot call
5513 check_field_decls until we have called check_bases check_methods,
5514 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5515 being set appropriately. */
5516 check_field_decls (t, &access_decls,
5517 &cant_have_const_ctor,
5518 &no_const_asn_ref);
5520 /* A nearly-empty class has to be vptr-containing; a nearly empty
5521 class contains just a vptr. */
5522 if (!TYPE_CONTAINS_VPTR_P (t))
5523 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5525 /* Do some bookkeeping that will guide the generation of implicitly
5526 declared member functions. */
5527 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5528 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5529 /* We need to call a constructor for this class if it has a
5530 user-provided constructor, or if the default constructor is going
5531 to initialize the vptr. (This is not an if-and-only-if;
5532 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5533 themselves need constructing.) */
5534 TYPE_NEEDS_CONSTRUCTING (t)
5535 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5536 /* [dcl.init.aggr]
5538 An aggregate is an array or a class with no user-provided
5539 constructors ... and no virtual functions.
5541 Again, other conditions for being an aggregate are checked
5542 elsewhere. */
5543 CLASSTYPE_NON_AGGREGATE (t)
5544 |= (type_has_user_provided_or_explicit_constructor (t)
5545 || TYPE_POLYMORPHIC_P (t));
5546 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5547 retain the old definition internally for ABI reasons. */
5548 CLASSTYPE_NON_LAYOUT_POD_P (t)
5549 |= (CLASSTYPE_NON_AGGREGATE (t)
5550 || saved_nontrivial_dtor || saved_complex_asn_ref);
5551 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5552 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5553 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5554 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5556 /* If the only explicitly declared default constructor is user-provided,
5557 set TYPE_HAS_COMPLEX_DFLT. */
5558 if (!TYPE_HAS_COMPLEX_DFLT (t)
5559 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5560 && !type_has_non_user_provided_default_constructor (t))
5561 TYPE_HAS_COMPLEX_DFLT (t) = true;
5563 /* Warn if a public base of a polymorphic type has an accessible
5564 non-virtual destructor. It is only now that we know the class is
5565 polymorphic. Although a polymorphic base will have a already
5566 been diagnosed during its definition, we warn on use too. */
5567 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5569 tree binfo = TYPE_BINFO (t);
5570 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5571 tree base_binfo;
5572 unsigned i;
5574 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5576 tree basetype = TREE_TYPE (base_binfo);
5578 if ((*accesses)[i] == access_public_node
5579 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5580 && accessible_nvdtor_p (basetype))
5581 warning (OPT_Wnon_virtual_dtor,
5582 "base class %q#T has accessible non-virtual destructor",
5583 basetype);
5587 /* If the class has no user-declared constructor, but does have
5588 non-static const or reference data members that can never be
5589 initialized, issue a warning. */
5590 if (warn_uninitialized
5591 /* Classes with user-declared constructors are presumed to
5592 initialize these members. */
5593 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5594 /* Aggregates can be initialized with brace-enclosed
5595 initializers. */
5596 && CLASSTYPE_NON_AGGREGATE (t))
5598 tree field;
5600 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5602 tree type;
5604 if (TREE_CODE (field) != FIELD_DECL
5605 || DECL_INITIAL (field) != NULL_TREE)
5606 continue;
5608 type = TREE_TYPE (field);
5609 if (TREE_CODE (type) == REFERENCE_TYPE)
5610 warning_at (DECL_SOURCE_LOCATION (field),
5611 OPT_Wuninitialized, "non-static reference %q#D "
5612 "in class without a constructor", field);
5613 else if (CP_TYPE_CONST_P (type)
5614 && (!CLASS_TYPE_P (type)
5615 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5616 warning_at (DECL_SOURCE_LOCATION (field),
5617 OPT_Wuninitialized, "non-static const member %q#D "
5618 "in class without a constructor", field);
5622 /* Synthesize any needed methods. */
5623 add_implicitly_declared_members (t, &access_decls,
5624 cant_have_const_ctor,
5625 no_const_asn_ref);
5627 /* Check defaulted declarations here so we have cant_have_const_ctor
5628 and don't need to worry about clones. */
5629 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5630 if (DECL_DECLARES_FUNCTION_P (fn)
5631 && !DECL_ARTIFICIAL (fn)
5632 && DECL_DEFAULTED_IN_CLASS_P (fn))
5634 int copy = copy_fn_p (fn);
5635 if (copy > 0)
5637 bool imp_const_p
5638 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5639 : !no_const_asn_ref);
5640 bool fn_const_p = (copy == 2);
5642 if (fn_const_p && !imp_const_p)
5643 /* If the function is defaulted outside the class, we just
5644 give the synthesis error. */
5645 error ("%q+D declared to take const reference, but implicit "
5646 "declaration would take non-const", fn);
5648 defaulted_late_check (fn);
5651 if (LAMBDA_TYPE_P (t))
5653 /* "This class type is not an aggregate." */
5654 CLASSTYPE_NON_AGGREGATE (t) = 1;
5657 /* Compute the 'literal type' property before we
5658 do anything with non-static member functions. */
5659 finalize_literal_type_property (t);
5661 /* Create the in-charge and not-in-charge variants of constructors
5662 and destructors. */
5663 clone_constructors_and_destructors (t);
5665 /* Process the using-declarations. */
5666 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5667 handle_using_decl (TREE_VALUE (access_decls), t);
5669 /* Figure out whether or not we will need a cookie when dynamically
5670 allocating an array of this type. */
5671 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5672 = type_requires_array_cookie (t);
5675 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5676 accordingly. If a new vfield was created (because T doesn't have a
5677 primary base class), then the newly created field is returned. It
5678 is not added to the TYPE_FIELDS list; it is the caller's
5679 responsibility to do that. Accumulate declared virtual functions
5680 on VIRTUALS_P. */
5682 static tree
5683 create_vtable_ptr (tree t, tree* virtuals_p)
5685 tree fn;
5687 /* Collect the virtual functions declared in T. */
5688 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5689 if (TREE_CODE (fn) == FUNCTION_DECL
5690 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5691 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5693 tree new_virtual = make_node (TREE_LIST);
5695 BV_FN (new_virtual) = fn;
5696 BV_DELTA (new_virtual) = integer_zero_node;
5697 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5699 TREE_CHAIN (new_virtual) = *virtuals_p;
5700 *virtuals_p = new_virtual;
5703 /* If we couldn't find an appropriate base class, create a new field
5704 here. Even if there weren't any new virtual functions, we might need a
5705 new virtual function table if we're supposed to include vptrs in
5706 all classes that need them. */
5707 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5709 /* We build this decl with vtbl_ptr_type_node, which is a
5710 `vtable_entry_type*'. It might seem more precise to use
5711 `vtable_entry_type (*)[N]' where N is the number of virtual
5712 functions. However, that would require the vtable pointer in
5713 base classes to have a different type than the vtable pointer
5714 in derived classes. We could make that happen, but that
5715 still wouldn't solve all the problems. In particular, the
5716 type-based alias analysis code would decide that assignments
5717 to the base class vtable pointer can't alias assignments to
5718 the derived class vtable pointer, since they have different
5719 types. Thus, in a derived class destructor, where the base
5720 class constructor was inlined, we could generate bad code for
5721 setting up the vtable pointer.
5723 Therefore, we use one type for all vtable pointers. We still
5724 use a type-correct type; it's just doesn't indicate the array
5725 bounds. That's better than using `void*' or some such; it's
5726 cleaner, and it let's the alias analysis code know that these
5727 stores cannot alias stores to void*! */
5728 tree field;
5730 field = build_decl (input_location,
5731 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5732 DECL_VIRTUAL_P (field) = 1;
5733 DECL_ARTIFICIAL (field) = 1;
5734 DECL_FIELD_CONTEXT (field) = t;
5735 DECL_FCONTEXT (field) = t;
5736 if (TYPE_PACKED (t))
5737 DECL_PACKED (field) = 1;
5739 TYPE_VFIELD (t) = field;
5741 /* This class is non-empty. */
5742 CLASSTYPE_EMPTY_P (t) = 0;
5744 return field;
5747 return NULL_TREE;
5750 /* Add OFFSET to all base types of BINFO which is a base in the
5751 hierarchy dominated by T.
5753 OFFSET, which is a type offset, is number of bytes. */
5755 static void
5756 propagate_binfo_offsets (tree binfo, tree offset)
5758 int i;
5759 tree primary_binfo;
5760 tree base_binfo;
5762 /* Update BINFO's offset. */
5763 BINFO_OFFSET (binfo)
5764 = fold_convert (sizetype,
5765 size_binop (PLUS_EXPR,
5766 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5767 offset));
5769 /* Find the primary base class. */
5770 primary_binfo = get_primary_binfo (binfo);
5772 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5773 propagate_binfo_offsets (primary_binfo, offset);
5775 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5776 downwards. */
5777 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5779 /* Don't do the primary base twice. */
5780 if (base_binfo == primary_binfo)
5781 continue;
5783 if (BINFO_VIRTUAL_P (base_binfo))
5784 continue;
5786 propagate_binfo_offsets (base_binfo, offset);
5790 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5791 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5792 empty subobjects of T. */
5794 static void
5795 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5797 tree vbase;
5798 tree t = rli->t;
5799 tree *next_field;
5801 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5802 return;
5804 /* Find the last field. The artificial fields created for virtual
5805 bases will go after the last extant field to date. */
5806 next_field = &TYPE_FIELDS (t);
5807 while (*next_field)
5808 next_field = &DECL_CHAIN (*next_field);
5810 /* Go through the virtual bases, allocating space for each virtual
5811 base that is not already a primary base class. These are
5812 allocated in inheritance graph order. */
5813 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5815 if (!BINFO_VIRTUAL_P (vbase))
5816 continue;
5818 if (!BINFO_PRIMARY_P (vbase))
5820 /* This virtual base is not a primary base of any class in the
5821 hierarchy, so we have to add space for it. */
5822 next_field = build_base_field (rli, vbase,
5823 offsets, next_field);
5828 /* Returns the offset of the byte just past the end of the base class
5829 BINFO. */
5831 static tree
5832 end_of_base (tree binfo)
5834 tree size;
5836 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5837 size = TYPE_SIZE_UNIT (char_type_node);
5838 else if (is_empty_class (BINFO_TYPE (binfo)))
5839 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5840 allocate some space for it. It cannot have virtual bases, so
5841 TYPE_SIZE_UNIT is fine. */
5842 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5843 else
5844 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5846 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5849 /* Returns the offset of the byte just past the end of the base class
5850 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5851 only non-virtual bases are included. */
5853 static tree
5854 end_of_class (tree t, int include_virtuals_p)
5856 tree result = size_zero_node;
5857 vec<tree, va_gc> *vbases;
5858 tree binfo;
5859 tree base_binfo;
5860 tree offset;
5861 int i;
5863 for (binfo = TYPE_BINFO (t), i = 0;
5864 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5866 if (!include_virtuals_p
5867 && BINFO_VIRTUAL_P (base_binfo)
5868 && (!BINFO_PRIMARY_P (base_binfo)
5869 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5870 continue;
5872 offset = end_of_base (base_binfo);
5873 if (tree_int_cst_lt (result, offset))
5874 result = offset;
5877 if (include_virtuals_p)
5878 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5879 vec_safe_iterate (vbases, i, &base_binfo); i++)
5881 offset = end_of_base (base_binfo);
5882 if (tree_int_cst_lt (result, offset))
5883 result = offset;
5886 return result;
5889 /* Warn about bases of T that are inaccessible because they are
5890 ambiguous. For example:
5892 struct S {};
5893 struct T : public S {};
5894 struct U : public S, public T {};
5896 Here, `(S*) new U' is not allowed because there are two `S'
5897 subobjects of U. */
5899 static void
5900 warn_about_ambiguous_bases (tree t)
5902 int i;
5903 vec<tree, va_gc> *vbases;
5904 tree basetype;
5905 tree binfo;
5906 tree base_binfo;
5908 /* If there are no repeated bases, nothing can be ambiguous. */
5909 if (!CLASSTYPE_REPEATED_BASE_P (t))
5910 return;
5912 /* Check direct bases. */
5913 for (binfo = TYPE_BINFO (t), i = 0;
5914 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5916 basetype = BINFO_TYPE (base_binfo);
5918 if (!uniquely_derived_from_p (basetype, t))
5919 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5920 basetype, t);
5923 /* Check for ambiguous virtual bases. */
5924 if (extra_warnings)
5925 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5926 vec_safe_iterate (vbases, i, &binfo); i++)
5928 basetype = BINFO_TYPE (binfo);
5930 if (!uniquely_derived_from_p (basetype, t))
5931 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5932 "to ambiguity", basetype, t);
5936 /* Compare two INTEGER_CSTs K1 and K2. */
5938 static int
5939 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5941 return tree_int_cst_compare ((tree) k1, (tree) k2);
5944 /* Increase the size indicated in RLI to account for empty classes
5945 that are "off the end" of the class. */
5947 static void
5948 include_empty_classes (record_layout_info rli)
5950 tree eoc;
5951 tree rli_size;
5953 /* It might be the case that we grew the class to allocate a
5954 zero-sized base class. That won't be reflected in RLI, yet,
5955 because we are willing to overlay multiple bases at the same
5956 offset. However, now we need to make sure that RLI is big enough
5957 to reflect the entire class. */
5958 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5959 rli_size = rli_size_unit_so_far (rli);
5960 if (TREE_CODE (rli_size) == INTEGER_CST
5961 && tree_int_cst_lt (rli_size, eoc))
5963 /* The size should have been rounded to a whole byte. */
5964 gcc_assert (tree_int_cst_equal
5965 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
5966 rli->bitpos
5967 = size_binop (PLUS_EXPR,
5968 rli->bitpos,
5969 size_binop (MULT_EXPR,
5970 fold_convert (bitsizetype,
5971 size_binop (MINUS_EXPR,
5972 eoc, rli_size)),
5973 bitsize_int (BITS_PER_UNIT)));
5974 normalize_rli (rli);
5978 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
5979 BINFO_OFFSETs for all of the base-classes. Position the vtable
5980 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
5982 static void
5983 layout_class_type (tree t, tree *virtuals_p)
5985 tree non_static_data_members;
5986 tree field;
5987 tree vptr;
5988 record_layout_info rli;
5989 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
5990 types that appear at that offset. */
5991 splay_tree empty_base_offsets;
5992 /* True if the last field laid out was a bit-field. */
5993 bool last_field_was_bitfield = false;
5994 /* The location at which the next field should be inserted. */
5995 tree *next_field;
5997 /* Keep track of the first non-static data member. */
5998 non_static_data_members = TYPE_FIELDS (t);
6000 /* Start laying out the record. */
6001 rli = start_record_layout (t);
6003 /* Mark all the primary bases in the hierarchy. */
6004 determine_primary_bases (t);
6006 /* Create a pointer to our virtual function table. */
6007 vptr = create_vtable_ptr (t, virtuals_p);
6009 /* The vptr is always the first thing in the class. */
6010 if (vptr)
6012 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6013 TYPE_FIELDS (t) = vptr;
6014 next_field = &DECL_CHAIN (vptr);
6015 place_field (rli, vptr);
6017 else
6018 next_field = &TYPE_FIELDS (t);
6020 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6021 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6022 NULL, NULL);
6023 build_base_fields (rli, empty_base_offsets, next_field);
6025 /* Layout the non-static data members. */
6026 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6028 tree type;
6029 tree padding;
6031 /* We still pass things that aren't non-static data members to
6032 the back end, in case it wants to do something with them. */
6033 if (TREE_CODE (field) != FIELD_DECL)
6035 place_field (rli, field);
6036 /* If the static data member has incomplete type, keep track
6037 of it so that it can be completed later. (The handling
6038 of pending statics in finish_record_layout is
6039 insufficient; consider:
6041 struct S1;
6042 struct S2 { static S1 s1; };
6044 At this point, finish_record_layout will be called, but
6045 S1 is still incomplete.) */
6046 if (VAR_P (field))
6048 maybe_register_incomplete_var (field);
6049 /* The visibility of static data members is determined
6050 at their point of declaration, not their point of
6051 definition. */
6052 determine_visibility (field);
6054 continue;
6057 type = TREE_TYPE (field);
6058 if (type == error_mark_node)
6059 continue;
6061 padding = NULL_TREE;
6063 /* If this field is a bit-field whose width is greater than its
6064 type, then there are some special rules for allocating
6065 it. */
6066 if (DECL_C_BIT_FIELD (field)
6067 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6069 bool was_unnamed_p = false;
6070 /* We must allocate the bits as if suitably aligned for the
6071 longest integer type that fits in this many bits. Then,
6072 we are supposed to use the left over bits as additional
6073 padding. */
6075 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6076 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6077 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6078 limit = DECL_SIZE (field);
6080 tree integer_type = integer_types[itk_char];
6081 for (unsigned itk = itk_char; itk != itk_none; itk++)
6082 if (tree next = integer_types[itk])
6084 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6085 /* Too big, so our current guess is what we want. */
6086 break;
6087 /* Not bigger than limit, ok */
6088 integer_type = next;
6091 /* Figure out how much additional padding is required. */
6092 if (TREE_CODE (t) == UNION_TYPE)
6093 /* In a union, the padding field must have the full width
6094 of the bit-field; all fields start at offset zero. */
6095 padding = DECL_SIZE (field);
6096 else
6097 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6098 TYPE_SIZE (integer_type));
6100 if (integer_zerop (padding))
6101 padding = NULL_TREE;
6103 /* An unnamed bitfield does not normally affect the
6104 alignment of the containing class on a target where
6105 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6106 make any exceptions for unnamed bitfields when the
6107 bitfields are longer than their types. Therefore, we
6108 temporarily give the field a name. */
6109 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6111 was_unnamed_p = true;
6112 DECL_NAME (field) = make_anon_name ();
6115 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6116 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6117 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6118 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6119 empty_base_offsets);
6120 if (was_unnamed_p)
6121 DECL_NAME (field) = NULL_TREE;
6122 /* Now that layout has been performed, set the size of the
6123 field to the size of its declared type; the rest of the
6124 field is effectively invisible. */
6125 DECL_SIZE (field) = TYPE_SIZE (type);
6126 /* We must also reset the DECL_MODE of the field. */
6127 SET_DECL_MODE (field, TYPE_MODE (type));
6129 else
6130 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6131 empty_base_offsets);
6133 /* Remember the location of any empty classes in FIELD. */
6134 record_subobject_offsets (TREE_TYPE (field),
6135 byte_position(field),
6136 empty_base_offsets,
6137 /*is_data_member=*/true);
6139 /* If a bit-field does not immediately follow another bit-field,
6140 and yet it starts in the middle of a byte, we have failed to
6141 comply with the ABI. */
6142 if (warn_abi
6143 && DECL_C_BIT_FIELD (field)
6144 /* The TREE_NO_WARNING flag gets set by Objective-C when
6145 laying out an Objective-C class. The ObjC ABI differs
6146 from the C++ ABI, and so we do not want a warning
6147 here. */
6148 && !TREE_NO_WARNING (field)
6149 && !last_field_was_bitfield
6150 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6151 DECL_FIELD_BIT_OFFSET (field),
6152 bitsize_unit_node)))
6153 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6154 "offset of %qD is not ABI-compliant and may "
6155 "change in a future version of GCC", field);
6157 /* The middle end uses the type of expressions to determine the
6158 possible range of expression values. In order to optimize
6159 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6160 must be made aware of the width of "i", via its type.
6162 Because C++ does not have integer types of arbitrary width,
6163 we must (for the purposes of the front end) convert from the
6164 type assigned here to the declared type of the bitfield
6165 whenever a bitfield expression is used as an rvalue.
6166 Similarly, when assigning a value to a bitfield, the value
6167 must be converted to the type given the bitfield here. */
6168 if (DECL_C_BIT_FIELD (field))
6170 unsigned HOST_WIDE_INT width;
6171 tree ftype = TREE_TYPE (field);
6172 width = tree_to_uhwi (DECL_SIZE (field));
6173 if (width != TYPE_PRECISION (ftype))
6175 TREE_TYPE (field)
6176 = c_build_bitfield_integer_type (width,
6177 TYPE_UNSIGNED (ftype));
6178 TREE_TYPE (field)
6179 = cp_build_qualified_type (TREE_TYPE (field),
6180 cp_type_quals (ftype));
6184 /* If we needed additional padding after this field, add it
6185 now. */
6186 if (padding)
6188 tree padding_field;
6190 padding_field = build_decl (input_location,
6191 FIELD_DECL,
6192 NULL_TREE,
6193 char_type_node);
6194 DECL_BIT_FIELD (padding_field) = 1;
6195 DECL_SIZE (padding_field) = padding;
6196 DECL_CONTEXT (padding_field) = t;
6197 DECL_ARTIFICIAL (padding_field) = 1;
6198 DECL_IGNORED_P (padding_field) = 1;
6199 DECL_PADDING_P (padding_field) = 1;
6200 layout_nonempty_base_or_field (rli, padding_field,
6201 NULL_TREE,
6202 empty_base_offsets);
6205 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6208 if (!integer_zerop (rli->bitpos))
6210 /* Make sure that we are on a byte boundary so that the size of
6211 the class without virtual bases will always be a round number
6212 of bytes. */
6213 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6214 normalize_rli (rli);
6217 /* Delete all zero-width bit-fields from the list of fields. Now
6218 that the type is laid out they are no longer important. */
6219 remove_zero_width_bit_fields (t);
6221 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6223 /* T needs a different layout as a base (eliding virtual bases
6224 or whatever). Create that version. */
6225 tree base_t = make_node (TREE_CODE (t));
6227 /* If the ABI version is not at least two, and the last
6228 field was a bit-field, RLI may not be on a byte
6229 boundary. In particular, rli_size_unit_so_far might
6230 indicate the last complete byte, while rli_size_so_far
6231 indicates the total number of bits used. Therefore,
6232 rli_size_so_far, rather than rli_size_unit_so_far, is
6233 used to compute TYPE_SIZE_UNIT. */
6234 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6235 TYPE_SIZE_UNIT (base_t)
6236 = size_binop (MAX_EXPR,
6237 fold_convert (sizetype,
6238 size_binop (CEIL_DIV_EXPR,
6239 rli_size_so_far (rli),
6240 bitsize_int (BITS_PER_UNIT))),
6241 eoc);
6242 TYPE_SIZE (base_t)
6243 = size_binop (MAX_EXPR,
6244 rli_size_so_far (rli),
6245 size_binop (MULT_EXPR,
6246 fold_convert (bitsizetype, eoc),
6247 bitsize_int (BITS_PER_UNIT)));
6248 SET_TYPE_ALIGN (base_t, rli->record_align);
6249 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6251 /* Copy the non-static data members of T. This will include its
6252 direct non-virtual bases & vtable. */
6253 next_field = &TYPE_FIELDS (base_t);
6254 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6255 if (TREE_CODE (field) == FIELD_DECL)
6257 *next_field = copy_node (field);
6258 DECL_CONTEXT (*next_field) = base_t;
6259 next_field = &DECL_CHAIN (*next_field);
6261 *next_field = NULL_TREE;
6263 /* We use the base type for trivial assignments, and hence it
6264 needs a mode. */
6265 compute_record_mode (base_t);
6267 TYPE_CONTEXT (base_t) = t;
6269 /* Record the base version of the type. */
6270 CLASSTYPE_AS_BASE (t) = base_t;
6272 else
6273 CLASSTYPE_AS_BASE (t) = t;
6275 /* Every empty class contains an empty class. */
6276 if (CLASSTYPE_EMPTY_P (t))
6277 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6279 /* Set the TYPE_DECL for this type to contain the right
6280 value for DECL_OFFSET, so that we can use it as part
6281 of a COMPONENT_REF for multiple inheritance. */
6282 layout_decl (TYPE_MAIN_DECL (t), 0);
6284 /* Now fix up any virtual base class types that we left lying
6285 around. We must get these done before we try to lay out the
6286 virtual function table. As a side-effect, this will remove the
6287 base subobject fields. */
6288 layout_virtual_bases (rli, empty_base_offsets);
6290 /* Make sure that empty classes are reflected in RLI at this
6291 point. */
6292 include_empty_classes (rli);
6294 /* Make sure not to create any structures with zero size. */
6295 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6296 place_field (rli,
6297 build_decl (input_location,
6298 FIELD_DECL, NULL_TREE, char_type_node));
6300 /* If this is a non-POD, declaring it packed makes a difference to how it
6301 can be used as a field; don't let finalize_record_size undo it. */
6302 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6303 rli->packed_maybe_necessary = true;
6305 /* Let the back end lay out the type. */
6306 finish_record_layout (rli, /*free_p=*/true);
6308 if (TYPE_SIZE_UNIT (t)
6309 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6310 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6311 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6312 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6314 /* Warn about bases that can't be talked about due to ambiguity. */
6315 warn_about_ambiguous_bases (t);
6317 /* Now that we're done with layout, give the base fields the real types. */
6318 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6319 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6320 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6322 /* Clean up. */
6323 splay_tree_delete (empty_base_offsets);
6325 if (CLASSTYPE_EMPTY_P (t)
6326 && tree_int_cst_lt (sizeof_biggest_empty_class,
6327 TYPE_SIZE_UNIT (t)))
6328 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6331 /* Determine the "key method" for the class type indicated by TYPE,
6332 and set CLASSTYPE_KEY_METHOD accordingly. */
6334 void
6335 determine_key_method (tree type)
6337 tree method;
6339 if (processing_template_decl
6340 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6341 || CLASSTYPE_INTERFACE_KNOWN (type))
6342 return;
6344 /* The key method is the first non-pure virtual function that is not
6345 inline at the point of class definition. On some targets the
6346 key function may not be inline; those targets should not call
6347 this function until the end of the translation unit. */
6348 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6349 if (TREE_CODE (method) == FUNCTION_DECL
6350 && DECL_VINDEX (method) != NULL_TREE
6351 && ! DECL_DECLARED_INLINE_P (method)
6352 && ! DECL_PURE_VIRTUAL_P (method))
6354 CLASSTYPE_KEY_METHOD (type) = method;
6355 break;
6358 return;
6361 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6362 class data member of non-zero size, otherwise false. */
6364 static inline bool
6365 field_nonempty_p (const_tree fld)
6367 if (TREE_CODE (fld) == ERROR_MARK)
6368 return false;
6370 tree type = TREE_TYPE (fld);
6371 if (TREE_CODE (fld) == FIELD_DECL
6372 && TREE_CODE (type) != ERROR_MARK
6373 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6375 return TYPE_SIZE (type)
6376 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6377 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6380 return false;
6383 /* Used by find_flexarrays and related functions. */
6385 struct flexmems_t
6387 /* The first flexible array member or non-zero array member found
6388 in the order of layout. */
6389 tree array;
6390 /* First non-static non-empty data member in the class or its bases. */
6391 tree first;
6392 /* The first non-static non-empty data member following either
6393 the flexible array member, if found, or the zero-length array member
6394 otherwise. AFTER[1] refers to the first such data member of a union
6395 of which the struct containing the flexible array member or zero-length
6396 array is a member, or NULL when no such union exists. This element is
6397 only used during searching, not for diagnosing problems. AFTER[0]
6398 refers to the first such data member that is not a member of such
6399 a union. */
6400 tree after[2];
6402 /* Refers to a struct (not union) in which the struct of which the flexible
6403 array is member is defined. Used to diagnose strictly (according to C)
6404 invalid uses of the latter structs. */
6405 tree enclosing;
6408 /* Find either the first flexible array member or the first zero-length
6409 array, in that order of preference, among members of class T (but not
6410 its base classes), and set members of FMEM accordingly.
6411 BASE_P is true if T is a base class of another class.
6412 PUN is set to the outermost union in which the flexible array member
6413 (or zero-length array) is defined if one such union exists, otherwise
6414 to NULL.
6415 Similarly, PSTR is set to a data member of the outermost struct of
6416 which the flexible array is a member if one such struct exists,
6417 otherwise to NULL. */
6419 static void
6420 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6421 tree pun /* = NULL_TREE */,
6422 tree pstr /* = NULL_TREE */)
6424 /* Set the "pointer" to the outermost enclosing union if not set
6425 yet and maintain it for the remainder of the recursion. */
6426 if (!pun && TREE_CODE (t) == UNION_TYPE)
6427 pun = t;
6429 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6431 if (fld == error_mark_node)
6432 return;
6434 /* Is FLD a typedef for an anonymous struct? */
6436 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6437 handled elsewhere so that errors like the following are detected
6438 as well:
6439 typedef struct { int i, a[], j; } S; // bug c++/72753
6440 S s [2]; // bug c++/68489
6442 if (TREE_CODE (fld) == TYPE_DECL
6443 && DECL_IMPLICIT_TYPEDEF_P (fld)
6444 && CLASS_TYPE_P (TREE_TYPE (fld))
6445 && anon_aggrname_p (DECL_NAME (fld)))
6447 /* Check the nested unnamed type referenced via a typedef
6448 independently of FMEM (since it's not a data member of
6449 the enclosing class). */
6450 check_flexarrays (TREE_TYPE (fld));
6451 continue;
6454 /* Skip anything that's GCC-generated or not a (non-static) data
6455 member. */
6456 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6457 continue;
6459 /* Type of the member. */
6460 tree fldtype = TREE_TYPE (fld);
6461 if (fldtype == error_mark_node)
6462 return;
6464 /* Determine the type of the array element or object referenced
6465 by the member so that it can be checked for flexible array
6466 members if it hasn't been yet. */
6467 tree eltype = fldtype;
6468 while (TREE_CODE (eltype) == ARRAY_TYPE
6469 || TREE_CODE (eltype) == POINTER_TYPE
6470 || TREE_CODE (eltype) == REFERENCE_TYPE)
6471 eltype = TREE_TYPE (eltype);
6473 if (RECORD_OR_UNION_TYPE_P (eltype))
6475 if (fmem->array && !fmem->after[bool (pun)])
6477 /* Once the member after the flexible array has been found
6478 we're done. */
6479 fmem->after[bool (pun)] = fld;
6480 break;
6483 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6485 /* Descend into the non-static member struct or union and try
6486 to find a flexible array member or zero-length array among
6487 its members. This is only necessary for anonymous types
6488 and types in whose context the current type T has not been
6489 defined (the latter must not be checked again because they
6490 are already in the process of being checked by one of the
6491 recursive calls). */
6493 tree first = fmem->first;
6494 tree array = fmem->array;
6496 /* If this member isn't anonymous and a prior non-flexible array
6497 member has been seen in one of the enclosing structs, clear
6498 the FIRST member since it doesn't contribute to the flexible
6499 array struct's members. */
6500 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6501 fmem->first = NULL_TREE;
6503 find_flexarrays (eltype, fmem, false, pun,
6504 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6506 if (fmem->array != array)
6507 continue;
6509 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6511 /* Restore the FIRST member reset above if no flexible
6512 array member has been found in this member's struct. */
6513 fmem->first = first;
6516 /* If the member struct contains the first flexible array
6517 member, or if this member is a base class, continue to
6518 the next member and avoid setting the FMEM->NEXT pointer
6519 to point to it. */
6520 if (base_p)
6521 continue;
6525 if (field_nonempty_p (fld))
6527 /* Remember the first non-static data member. */
6528 if (!fmem->first)
6529 fmem->first = fld;
6531 /* Remember the first non-static data member after the flexible
6532 array member, if one has been found, or the zero-length array
6533 if it has been found. */
6534 if (fmem->array && !fmem->after[bool (pun)])
6535 fmem->after[bool (pun)] = fld;
6538 /* Skip non-arrays. */
6539 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6540 continue;
6542 /* Determine the upper bound of the array if it has one. */
6543 if (TYPE_DOMAIN (fldtype))
6545 if (fmem->array)
6547 /* Make a record of the zero-length array if either one
6548 such field or a flexible array member has been seen to
6549 handle the pathological and unlikely case of multiple
6550 such members. */
6551 if (!fmem->after[bool (pun)])
6552 fmem->after[bool (pun)] = fld;
6554 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6556 /* Remember the first zero-length array unless a flexible array
6557 member has already been seen. */
6558 fmem->array = fld;
6559 fmem->enclosing = pstr;
6562 else
6564 /* Flexible array members have no upper bound. */
6565 if (fmem->array)
6567 /* Replace the zero-length array if it's been stored and
6568 reset the after pointer. */
6569 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6571 fmem->after[bool (pun)] = NULL_TREE;
6572 fmem->array = fld;
6573 fmem->enclosing = pstr;
6576 else
6578 fmem->array = fld;
6579 fmem->enclosing = pstr;
6585 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6586 a flexible array member (or the zero-length array extension). */
6588 static void
6589 diagnose_invalid_flexarray (const flexmems_t *fmem)
6591 if (fmem->array && fmem->enclosing
6592 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6593 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6594 ? G_("invalid use of %q#T with a zero-size array "
6595 "in %q#D")
6596 : G_("invalid use of %q#T with a flexible array member "
6597 "in %q#T"),
6598 DECL_CONTEXT (fmem->array),
6599 DECL_CONTEXT (fmem->enclosing)))
6600 inform (DECL_SOURCE_LOCATION (fmem->array),
6601 "array member %q#D declared here", fmem->array);
6604 /* Issue diagnostics for invalid flexible array members or zero-length
6605 arrays that are not the last elements of the containing class or its
6606 base classes or that are its sole members. */
6608 static void
6609 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6611 if (!fmem->array)
6612 return;
6614 if (fmem->first && !fmem->after[0])
6616 diagnose_invalid_flexarray (fmem);
6617 return;
6620 /* Has a diagnostic been issued? */
6621 bool diagd = false;
6623 const char *msg = 0;
6625 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6627 if (fmem->after[0])
6628 msg = G_("zero-size array member %qD not at end of %q#T");
6629 else if (!fmem->first)
6630 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6632 if (msg)
6634 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6636 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6638 inform (location_of (t), "in the definition of %q#T", t);
6639 diagd = true;
6643 else
6645 if (fmem->after[0])
6646 msg = G_("flexible array member %qD not at end of %q#T");
6647 else if (!fmem->first)
6648 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6650 if (msg)
6652 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6653 diagd = true;
6655 error_at (loc, msg, fmem->array, t);
6657 /* In the unlikely event that the member following the flexible
6658 array member is declared in a different class, or the member
6659 overlaps another member of a common union, point to it.
6660 Otherwise it should be obvious. */
6661 if (fmem->after[0]
6662 && ((DECL_CONTEXT (fmem->after[0])
6663 != DECL_CONTEXT (fmem->array))))
6665 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6666 "next member %q#D declared here",
6667 fmem->after[0]);
6668 inform (location_of (t), "in the definition of %q#T", t);
6673 if (!diagd && fmem->array && fmem->enclosing)
6674 diagnose_invalid_flexarray (fmem);
6678 /* Recursively check to make sure that any flexible array or zero-length
6679 array members of class T or its bases are valid (i.e., not the sole
6680 non-static data member of T and, if one exists, that it is the last
6681 non-static data member of T and its base classes. FMEM is expected
6682 to be initially null and is used internally by recursive calls to
6683 the function. Issue the appropriate diagnostics for the array member
6684 that fails the checks. */
6686 static void
6687 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
6688 bool base_p /* = false */)
6690 /* Initialize the result of a search for flexible array and zero-length
6691 array members. Avoid doing any work if the most interesting FMEM data
6692 have already been populated. */
6693 flexmems_t flexmems = flexmems_t ();
6694 if (!fmem)
6695 fmem = &flexmems;
6696 else if (fmem->array && fmem->first && fmem->after[0])
6697 return;
6699 tree fam = fmem->array;
6701 /* Recursively check the primary base class first. */
6702 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6704 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6705 check_flexarrays (basetype, fmem, true);
6708 /* Recursively check the base classes. */
6709 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
6710 for (int i = 0; i < nbases; ++i)
6712 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6714 /* The primary base class was already checked above. */
6715 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6716 continue;
6718 /* Virtual base classes are at the end. */
6719 if (BINFO_VIRTUAL_P (base_binfo))
6720 continue;
6722 /* Check the base class. */
6723 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
6726 if (fmem == &flexmems)
6728 /* Check virtual base classes only once per derived class.
6729 I.e., this check is not performed recursively for base
6730 classes. */
6731 int i;
6732 tree base_binfo;
6733 vec<tree, va_gc> *vbases;
6734 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6735 vec_safe_iterate (vbases, i, &base_binfo); i++)
6737 /* Check the virtual base class. */
6738 tree basetype = TREE_TYPE (base_binfo);
6740 check_flexarrays (basetype, fmem, /*base_p=*/true);
6744 /* Is the type unnamed (and therefore a member of it potentially
6745 an anonymous struct or union)? */
6746 bool maybe_anon_p = TYPE_UNNAMED_P (t);
6748 /* Search the members of the current (possibly derived) class, skipping
6749 unnamed structs and unions since those could be anonymous. */
6750 if (fmem != &flexmems || !maybe_anon_p)
6751 find_flexarrays (t, fmem, base_p || fam != fmem->array);
6753 if (fmem == &flexmems && !maybe_anon_p)
6755 /* Issue diagnostics for invalid flexible and zero-length array
6756 members found in base classes or among the members of the current
6757 class. Ignore anonymous structs and unions whose members are
6758 considered to be members of the enclosing class and thus will
6759 be diagnosed when checking it. */
6760 diagnose_flexarrays (t, fmem);
6764 /* Perform processing required when the definition of T (a class type)
6765 is complete. Diagnose invalid definitions of flexible array members
6766 and zero-size arrays. */
6768 void
6769 finish_struct_1 (tree t)
6771 tree x;
6772 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6773 tree virtuals = NULL_TREE;
6775 if (COMPLETE_TYPE_P (t))
6777 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6778 error ("redefinition of %q#T", t);
6779 popclass ();
6780 return;
6783 /* If this type was previously laid out as a forward reference,
6784 make sure we lay it out again. */
6785 TYPE_SIZE (t) = NULL_TREE;
6786 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6788 /* Make assumptions about the class; we'll reset the flags if
6789 necessary. */
6790 CLASSTYPE_EMPTY_P (t) = 1;
6791 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6792 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6793 CLASSTYPE_LITERAL_P (t) = true;
6795 /* Do end-of-class semantic processing: checking the validity of the
6796 bases and members and add implicitly generated methods. */
6797 check_bases_and_members (t);
6799 /* Find the key method. */
6800 if (TYPE_CONTAINS_VPTR_P (t))
6802 /* The Itanium C++ ABI permits the key method to be chosen when
6803 the class is defined -- even though the key method so
6804 selected may later turn out to be an inline function. On
6805 some systems (such as ARM Symbian OS) the key method cannot
6806 be determined until the end of the translation unit. On such
6807 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6808 will cause the class to be added to KEYED_CLASSES. Then, in
6809 finish_file we will determine the key method. */
6810 if (targetm.cxx.key_method_may_be_inline ())
6811 determine_key_method (t);
6813 /* If a polymorphic class has no key method, we may emit the vtable
6814 in every translation unit where the class definition appears. If
6815 we're devirtualizing, we can look into the vtable even if we
6816 aren't emitting it. */
6817 if (!CLASSTYPE_KEY_METHOD (t))
6818 vec_safe_push (keyed_classes, t);
6821 /* Layout the class itself. */
6822 layout_class_type (t, &virtuals);
6823 /* COMPLETE_TYPE_P is now true. */
6825 set_class_bindings (t);
6827 /* With the layout complete, check for flexible array members and
6828 zero-length arrays that might overlap other members in the final
6829 layout. */
6830 check_flexarrays (t);
6832 virtuals = modify_all_vtables (t, nreverse (virtuals));
6834 /* If necessary, create the primary vtable for this class. */
6835 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6837 /* We must enter these virtuals into the table. */
6838 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6839 build_primary_vtable (NULL_TREE, t);
6840 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6841 /* Here we know enough to change the type of our virtual
6842 function table, but we will wait until later this function. */
6843 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6845 /* If we're warning about ABI tags, check the types of the new
6846 virtual functions. */
6847 if (warn_abi_tag)
6848 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6849 check_abi_tags (t, TREE_VALUE (v));
6852 if (TYPE_CONTAINS_VPTR_P (t))
6854 int vindex;
6855 tree fn;
6857 if (BINFO_VTABLE (TYPE_BINFO (t)))
6858 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6859 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6860 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6862 /* Add entries for virtual functions introduced by this class. */
6863 BINFO_VIRTUALS (TYPE_BINFO (t))
6864 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6866 /* Set DECL_VINDEX for all functions declared in this class. */
6867 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6869 fn = TREE_CHAIN (fn),
6870 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6871 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6873 tree fndecl = BV_FN (fn);
6875 if (DECL_THUNK_P (fndecl))
6876 /* A thunk. We should never be calling this entry directly
6877 from this vtable -- we'd use the entry for the non
6878 thunk base function. */
6879 DECL_VINDEX (fndecl) = NULL_TREE;
6880 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6881 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6885 finish_struct_bits (t);
6887 set_method_tm_attributes (t);
6888 if (flag_openmp || flag_openmp_simd)
6889 finish_omp_declare_simd_methods (t);
6891 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
6892 for any static member objects of the type we're working on. */
6893 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6894 if (DECL_DECLARES_FUNCTION_P (x))
6895 DECL_IN_AGGR_P (x) = false;
6896 else if (VAR_P (x) && TREE_STATIC (x)
6897 && TREE_TYPE (x) != error_mark_node
6898 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6899 SET_DECL_MODE (x, TYPE_MODE (t));
6901 /* Complain if one of the field types requires lower visibility. */
6902 constrain_class_visibility (t);
6904 /* Make the rtl for any new vtables we have created, and unmark
6905 the base types we marked. */
6906 finish_vtbls (t);
6908 /* Build the VTT for T. */
6909 build_vtt (t);
6911 if (warn_nonvdtor
6912 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6913 && !CLASSTYPE_FINAL (t))
6914 warning (OPT_Wnon_virtual_dtor,
6915 "%q#T has virtual functions and accessible"
6916 " non-virtual destructor", t);
6918 complete_vars (t);
6920 if (warn_overloaded_virtual)
6921 warn_hidden (t);
6923 /* Class layout, assignment of virtual table slots, etc., is now
6924 complete. Give the back end a chance to tweak the visibility of
6925 the class or perform any other required target modifications. */
6926 targetm.cxx.adjust_class_at_definition (t);
6928 maybe_suppress_debug_info (t);
6930 if (flag_vtable_verify)
6931 vtv_save_class_info (t);
6933 dump_class_hierarchy (t);
6935 /* Finish debugging output for this type. */
6936 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6938 if (TYPE_TRANSPARENT_AGGR (t))
6940 tree field = first_field (t);
6941 if (field == NULL_TREE || error_operand_p (field))
6943 error ("type transparent %q#T does not have any fields", t);
6944 TYPE_TRANSPARENT_AGGR (t) = 0;
6946 else if (DECL_ARTIFICIAL (field))
6948 if (DECL_FIELD_IS_BASE (field))
6949 error ("type transparent class %qT has base classes", t);
6950 else
6952 gcc_checking_assert (DECL_VIRTUAL_P (field));
6953 error ("type transparent class %qT has virtual functions", t);
6955 TYPE_TRANSPARENT_AGGR (t) = 0;
6957 else if (TYPE_MODE (t) != DECL_MODE (field))
6959 error ("type transparent %q#T cannot be made transparent because "
6960 "the type of the first field has a different ABI from the "
6961 "class overall", t);
6962 TYPE_TRANSPARENT_AGGR (t) = 0;
6967 /* When T was built up, the member declarations were added in reverse
6968 order. Rearrange them to declaration order. */
6970 void
6971 unreverse_member_declarations (tree t)
6973 tree next;
6974 tree prev;
6975 tree x;
6977 /* The following lists are all in reverse order. Put them in
6978 declaration order now. */
6979 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
6981 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
6982 order, so we can't just use nreverse. Due to stat_hack
6983 chicanery in finish_member_declaration. */
6984 prev = NULL_TREE;
6985 for (x = TYPE_FIELDS (t);
6986 x && TREE_CODE (x) != TYPE_DECL;
6987 x = next)
6989 next = DECL_CHAIN (x);
6990 DECL_CHAIN (x) = prev;
6991 prev = x;
6994 if (prev)
6996 DECL_CHAIN (TYPE_FIELDS (t)) = x;
6997 TYPE_FIELDS (t) = prev;
7001 tree
7002 finish_struct (tree t, tree attributes)
7004 location_t saved_loc = input_location;
7006 /* Now that we've got all the field declarations, reverse everything
7007 as necessary. */
7008 unreverse_member_declarations (t);
7010 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7011 fixup_attribute_variants (t);
7013 /* Nadger the current location so that diagnostics point to the start of
7014 the struct, not the end. */
7015 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7017 if (processing_template_decl)
7019 tree x;
7021 /* We need to add the target functions of USING_DECLS, so that
7022 they can be found when the using declaration is not
7023 instantiated yet. */
7024 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7025 if (TREE_CODE (x) == USING_DECL)
7027 tree fn = strip_using_decl (x);
7028 if (OVL_P (fn))
7029 for (lkp_iterator iter (fn); iter; ++iter)
7030 add_method (t, *iter, true);
7032 else if (DECL_DECLARES_FUNCTION_P (x))
7033 DECL_IN_AGGR_P (x) = false;
7035 TYPE_SIZE (t) = bitsize_zero_node;
7036 TYPE_SIZE_UNIT (t) = size_zero_node;
7037 /* COMPLETE_TYPE_P is now true. */
7039 set_class_bindings (t);
7041 /* We need to emit an error message if this type was used as a parameter
7042 and it is an abstract type, even if it is a template. We construct
7043 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7044 account and we call complete_vars with this type, which will check
7045 the PARM_DECLS. Note that while the type is being defined,
7046 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7047 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7048 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7049 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7050 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7051 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7052 complete_vars (t);
7054 /* Remember current #pragma pack value. */
7055 TYPE_PRECISION (t) = maximum_field_alignment;
7057 /* Fix up any variants we've already built. */
7058 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7060 TYPE_SIZE (x) = TYPE_SIZE (t);
7061 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7062 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7065 else
7066 finish_struct_1 (t);
7067 /* COMPLETE_TYPE_P is now true. */
7069 maybe_warn_about_overly_private_class (t);
7071 if (is_std_init_list (t))
7073 /* People keep complaining that the compiler crashes on an invalid
7074 definition of initializer_list, so I guess we should explicitly
7075 reject it. What the compiler internals care about is that it's a
7076 template and has a pointer field followed by an integer field. */
7077 bool ok = false;
7078 if (processing_template_decl)
7080 tree f = next_initializable_field (TYPE_FIELDS (t));
7081 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE)
7083 f = next_initializable_field (DECL_CHAIN (f));
7084 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7085 ok = true;
7088 if (!ok)
7089 fatal_error (input_location,
7090 "definition of std::initializer_list does not match "
7091 "#include <initializer_list>");
7094 input_location = saved_loc;
7096 TYPE_BEING_DEFINED (t) = 0;
7098 if (current_class_type)
7099 popclass ();
7100 else
7101 error ("trying to finish struct, but kicked out due to previous parse errors");
7103 if (processing_template_decl && at_function_scope_p ()
7104 /* Lambdas are defined by the LAMBDA_EXPR. */
7105 && !LAMBDA_TYPE_P (t))
7106 add_stmt (build_min (TAG_DEFN, t));
7108 return t;
7111 /* Hash table to avoid endless recursion when handling references. */
7112 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7114 /* Return the dynamic type of INSTANCE, if known.
7115 Used to determine whether the virtual function table is needed
7116 or not.
7118 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7119 of our knowledge of its type. *NONNULL should be initialized
7120 before this function is called. */
7122 static tree
7123 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7125 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7127 switch (TREE_CODE (instance))
7129 case INDIRECT_REF:
7130 if (POINTER_TYPE_P (TREE_TYPE (instance)))
7131 return NULL_TREE;
7132 else
7133 return RECUR (TREE_OPERAND (instance, 0));
7135 case CALL_EXPR:
7136 /* This is a call to a constructor, hence it's never zero. */
7137 if (TREE_HAS_CONSTRUCTOR (instance))
7139 if (nonnull)
7140 *nonnull = 1;
7141 return TREE_TYPE (instance);
7143 return NULL_TREE;
7145 case SAVE_EXPR:
7146 /* This is a call to a constructor, hence it's never zero. */
7147 if (TREE_HAS_CONSTRUCTOR (instance))
7149 if (nonnull)
7150 *nonnull = 1;
7151 return TREE_TYPE (instance);
7153 return RECUR (TREE_OPERAND (instance, 0));
7155 case POINTER_PLUS_EXPR:
7156 case PLUS_EXPR:
7157 case MINUS_EXPR:
7158 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7159 return RECUR (TREE_OPERAND (instance, 0));
7160 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7161 /* Propagate nonnull. */
7162 return RECUR (TREE_OPERAND (instance, 0));
7164 return NULL_TREE;
7166 CASE_CONVERT:
7167 return RECUR (TREE_OPERAND (instance, 0));
7169 case ADDR_EXPR:
7170 instance = TREE_OPERAND (instance, 0);
7171 if (nonnull)
7173 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7174 with a real object -- given &p->f, p can still be null. */
7175 tree t = get_base_address (instance);
7176 /* ??? Probably should check DECL_WEAK here. */
7177 if (t && DECL_P (t))
7178 *nonnull = 1;
7180 return RECUR (instance);
7182 case COMPONENT_REF:
7183 /* If this component is really a base class reference, then the field
7184 itself isn't definitive. */
7185 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7186 return RECUR (TREE_OPERAND (instance, 0));
7187 return RECUR (TREE_OPERAND (instance, 1));
7189 case VAR_DECL:
7190 case FIELD_DECL:
7191 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7192 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7194 if (nonnull)
7195 *nonnull = 1;
7196 return TREE_TYPE (TREE_TYPE (instance));
7198 /* fall through. */
7199 case TARGET_EXPR:
7200 case PARM_DECL:
7201 case RESULT_DECL:
7202 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7204 if (nonnull)
7205 *nonnull = 1;
7206 return TREE_TYPE (instance);
7208 else if (instance == current_class_ptr)
7210 if (nonnull)
7211 *nonnull = 1;
7213 /* if we're in a ctor or dtor, we know our type. If
7214 current_class_ptr is set but we aren't in a function, we're in
7215 an NSDMI (and therefore a constructor). */
7216 if (current_scope () != current_function_decl
7217 || (DECL_LANG_SPECIFIC (current_function_decl)
7218 && (DECL_CONSTRUCTOR_P (current_function_decl)
7219 || DECL_DESTRUCTOR_P (current_function_decl))))
7221 if (cdtorp)
7222 *cdtorp = 1;
7223 return TREE_TYPE (TREE_TYPE (instance));
7226 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
7228 /* We only need one hash table because it is always left empty. */
7229 if (!fixed_type_or_null_ref_ht)
7230 fixed_type_or_null_ref_ht
7231 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7233 /* Reference variables should be references to objects. */
7234 if (nonnull)
7235 *nonnull = 1;
7237 /* Enter the INSTANCE in a table to prevent recursion; a
7238 variable's initializer may refer to the variable
7239 itself. */
7240 if (VAR_P (instance)
7241 && DECL_INITIAL (instance)
7242 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7243 && !fixed_type_or_null_ref_ht->find (instance))
7245 tree type;
7246 tree_node **slot;
7248 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7249 *slot = instance;
7250 type = RECUR (DECL_INITIAL (instance));
7251 fixed_type_or_null_ref_ht->remove_elt (instance);
7253 return type;
7256 return NULL_TREE;
7258 default:
7259 return NULL_TREE;
7261 #undef RECUR
7264 /* Return nonzero if the dynamic type of INSTANCE is known, and
7265 equivalent to the static type. We also handle the case where
7266 INSTANCE is really a pointer. Return negative if this is a
7267 ctor/dtor. There the dynamic type is known, but this might not be
7268 the most derived base of the original object, and hence virtual
7269 bases may not be laid out according to this type.
7271 Used to determine whether the virtual function table is needed
7272 or not.
7274 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7275 of our knowledge of its type. *NONNULL should be initialized
7276 before this function is called. */
7279 resolves_to_fixed_type_p (tree instance, int* nonnull)
7281 tree t = TREE_TYPE (instance);
7282 int cdtorp = 0;
7283 tree fixed;
7285 /* processing_template_decl can be false in a template if we're in
7286 instantiate_non_dependent_expr, but we still want to suppress
7287 this check. */
7288 if (in_template_function ())
7290 /* In a template we only care about the type of the result. */
7291 if (nonnull)
7292 *nonnull = true;
7293 return true;
7296 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7297 if (fixed == NULL_TREE)
7298 return 0;
7299 if (POINTER_TYPE_P (t))
7300 t = TREE_TYPE (t);
7301 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7302 return 0;
7303 return cdtorp ? -1 : 1;
7307 void
7308 init_class_processing (void)
7310 current_class_depth = 0;
7311 current_class_stack_size = 10;
7312 current_class_stack
7313 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7314 vec_alloc (local_classes, 8);
7315 sizeof_biggest_empty_class = size_zero_node;
7317 ridpointers[(int) RID_PUBLIC] = access_public_node;
7318 ridpointers[(int) RID_PRIVATE] = access_private_node;
7319 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7322 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7324 static void
7325 restore_class_cache (void)
7327 tree type;
7329 /* We are re-entering the same class we just left, so we don't
7330 have to search the whole inheritance matrix to find all the
7331 decls to bind again. Instead, we install the cached
7332 class_shadowed list and walk through it binding names. */
7333 push_binding_level (previous_class_level);
7334 class_binding_level = previous_class_level;
7335 /* Restore IDENTIFIER_TYPE_VALUE. */
7336 for (type = class_binding_level->type_shadowed;
7337 type;
7338 type = TREE_CHAIN (type))
7339 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7342 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7343 appropriate for TYPE.
7345 So that we may avoid calls to lookup_name, we cache the _TYPE
7346 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7348 For multiple inheritance, we perform a two-pass depth-first search
7349 of the type lattice. */
7351 void
7352 pushclass (tree type)
7354 class_stack_node_t csn;
7356 type = TYPE_MAIN_VARIANT (type);
7358 /* Make sure there is enough room for the new entry on the stack. */
7359 if (current_class_depth + 1 >= current_class_stack_size)
7361 current_class_stack_size *= 2;
7362 current_class_stack
7363 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7364 current_class_stack_size);
7367 /* Insert a new entry on the class stack. */
7368 csn = current_class_stack + current_class_depth;
7369 csn->name = current_class_name;
7370 csn->type = current_class_type;
7371 csn->access = current_access_specifier;
7372 csn->names_used = 0;
7373 csn->hidden = 0;
7374 current_class_depth++;
7376 /* Now set up the new type. */
7377 current_class_name = TYPE_NAME (type);
7378 if (TREE_CODE (current_class_name) == TYPE_DECL)
7379 current_class_name = DECL_NAME (current_class_name);
7380 current_class_type = type;
7382 /* By default, things in classes are private, while things in
7383 structures or unions are public. */
7384 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7385 ? access_private_node
7386 : access_public_node);
7388 if (previous_class_level
7389 && type != previous_class_level->this_entity
7390 && current_class_depth == 1)
7392 /* Forcibly remove any old class remnants. */
7393 invalidate_class_lookup_cache ();
7396 if (!previous_class_level
7397 || type != previous_class_level->this_entity
7398 || current_class_depth > 1)
7399 pushlevel_class ();
7400 else
7401 restore_class_cache ();
7404 /* When we exit a toplevel class scope, we save its binding level so
7405 that we can restore it quickly. Here, we've entered some other
7406 class, so we must invalidate our cache. */
7408 void
7409 invalidate_class_lookup_cache (void)
7411 previous_class_level = NULL;
7414 /* Get out of the current class scope. If we were in a class scope
7415 previously, that is the one popped to. */
7417 void
7418 popclass (void)
7420 poplevel_class ();
7422 current_class_depth--;
7423 current_class_name = current_class_stack[current_class_depth].name;
7424 current_class_type = current_class_stack[current_class_depth].type;
7425 current_access_specifier = current_class_stack[current_class_depth].access;
7426 if (current_class_stack[current_class_depth].names_used)
7427 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7430 /* Mark the top of the class stack as hidden. */
7432 void
7433 push_class_stack (void)
7435 if (current_class_depth)
7436 ++current_class_stack[current_class_depth - 1].hidden;
7439 /* Mark the top of the class stack as un-hidden. */
7441 void
7442 pop_class_stack (void)
7444 if (current_class_depth)
7445 --current_class_stack[current_class_depth - 1].hidden;
7448 /* Returns 1 if the class type currently being defined is either T or
7449 a nested type of T. Returns the type from the current_class_stack,
7450 which might be equivalent to but not equal to T in case of
7451 constrained partial specializations. */
7453 tree
7454 currently_open_class (tree t)
7456 int i;
7458 if (!CLASS_TYPE_P (t))
7459 return NULL_TREE;
7461 t = TYPE_MAIN_VARIANT (t);
7463 /* We start looking from 1 because entry 0 is from global scope,
7464 and has no type. */
7465 for (i = current_class_depth; i > 0; --i)
7467 tree c;
7468 if (i == current_class_depth)
7469 c = current_class_type;
7470 else
7472 if (current_class_stack[i].hidden)
7473 break;
7474 c = current_class_stack[i].type;
7476 if (!c)
7477 continue;
7478 if (same_type_p (c, t))
7479 return c;
7481 return NULL_TREE;
7484 /* If either current_class_type or one of its enclosing classes are derived
7485 from T, return the appropriate type. Used to determine how we found
7486 something via unqualified lookup. */
7488 tree
7489 currently_open_derived_class (tree t)
7491 int i;
7493 /* The bases of a dependent type are unknown. */
7494 if (dependent_type_p (t))
7495 return NULL_TREE;
7497 if (!current_class_type)
7498 return NULL_TREE;
7500 if (DERIVED_FROM_P (t, current_class_type))
7501 return current_class_type;
7503 for (i = current_class_depth - 1; i > 0; --i)
7505 if (current_class_stack[i].hidden)
7506 break;
7507 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7508 return current_class_stack[i].type;
7511 return NULL_TREE;
7514 /* Return the outermost enclosing class type that is still open, or
7515 NULL_TREE. */
7517 tree
7518 outermost_open_class (void)
7520 if (!current_class_type)
7521 return NULL_TREE;
7522 tree r = NULL_TREE;
7523 if (TYPE_BEING_DEFINED (current_class_type))
7524 r = current_class_type;
7525 for (int i = current_class_depth - 1; i > 0; --i)
7527 if (current_class_stack[i].hidden)
7528 break;
7529 tree t = current_class_stack[i].type;
7530 if (!TYPE_BEING_DEFINED (t))
7531 break;
7532 r = t;
7534 return r;
7537 /* Returns the innermost class type which is not a lambda closure type. */
7539 tree
7540 current_nonlambda_class_type (void)
7542 tree type = current_class_type;
7543 while (type && LAMBDA_TYPE_P (type))
7544 type = decl_type_context (TYPE_NAME (type));
7545 return type;
7548 /* When entering a class scope, all enclosing class scopes' names with
7549 static meaning (static variables, static functions, types and
7550 enumerators) have to be visible. This recursive function calls
7551 pushclass for all enclosing class contexts until global or a local
7552 scope is reached. TYPE is the enclosed class. */
7554 void
7555 push_nested_class (tree type)
7557 /* A namespace might be passed in error cases, like A::B:C. */
7558 if (type == NULL_TREE
7559 || !CLASS_TYPE_P (type))
7560 return;
7562 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7564 pushclass (type);
7567 /* Undoes a push_nested_class call. */
7569 void
7570 pop_nested_class (void)
7572 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7574 popclass ();
7575 if (context && CLASS_TYPE_P (context))
7576 pop_nested_class ();
7579 /* Returns the number of extern "LANG" blocks we are nested within. */
7582 current_lang_depth (void)
7584 return vec_safe_length (current_lang_base);
7587 /* Set global variables CURRENT_LANG_NAME to appropriate value
7588 so that behavior of name-mangling machinery is correct. */
7590 void
7591 push_lang_context (tree name)
7593 vec_safe_push (current_lang_base, current_lang_name);
7595 if (name == lang_name_cplusplus)
7596 current_lang_name = name;
7597 else if (name == lang_name_c)
7598 current_lang_name = name;
7599 else
7600 error ("language string %<\"%E\"%> not recognized", name);
7603 /* Get out of the current language scope. */
7605 void
7606 pop_lang_context (void)
7608 current_lang_name = current_lang_base->pop ();
7611 /* Type instantiation routines. */
7613 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7614 matches the TARGET_TYPE. If there is no satisfactory match, return
7615 error_mark_node, and issue an error & warning messages under
7616 control of FLAGS. Permit pointers to member function if FLAGS
7617 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7618 a template-id, and EXPLICIT_TARGS are the explicitly provided
7619 template arguments.
7621 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7622 is the base path used to reference those member functions. If
7623 the address is resolved to a member function, access checks will be
7624 performed and errors issued if appropriate. */
7626 static tree
7627 resolve_address_of_overloaded_function (tree target_type,
7628 tree overload,
7629 tsubst_flags_t complain,
7630 bool template_only,
7631 tree explicit_targs,
7632 tree access_path)
7634 /* Here's what the standard says:
7636 [over.over]
7638 If the name is a function template, template argument deduction
7639 is done, and if the argument deduction succeeds, the deduced
7640 arguments are used to generate a single template function, which
7641 is added to the set of overloaded functions considered.
7643 Non-member functions and static member functions match targets of
7644 type "pointer-to-function" or "reference-to-function." Nonstatic
7645 member functions match targets of type "pointer-to-member
7646 function;" the function type of the pointer to member is used to
7647 select the member function from the set of overloaded member
7648 functions. If a nonstatic member function is selected, the
7649 reference to the overloaded function name is required to have the
7650 form of a pointer to member as described in 5.3.1.
7652 If more than one function is selected, any template functions in
7653 the set are eliminated if the set also contains a non-template
7654 function, and any given template function is eliminated if the
7655 set contains a second template function that is more specialized
7656 than the first according to the partial ordering rules 14.5.5.2.
7657 After such eliminations, if any, there shall remain exactly one
7658 selected function. */
7660 int is_ptrmem = 0;
7661 /* We store the matches in a TREE_LIST rooted here. The functions
7662 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7663 interoperability with most_specialized_instantiation. */
7664 tree matches = NULL_TREE;
7665 tree fn;
7666 tree target_fn_type;
7668 /* By the time we get here, we should be seeing only real
7669 pointer-to-member types, not the internal POINTER_TYPE to
7670 METHOD_TYPE representation. */
7671 gcc_assert (!TYPE_PTR_P (target_type)
7672 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7674 gcc_assert (is_overloaded_fn (overload));
7676 /* Check that the TARGET_TYPE is reasonable. */
7677 if (TYPE_PTRFN_P (target_type)
7678 || TYPE_REFFN_P (target_type))
7679 /* This is OK. */;
7680 else if (TYPE_PTRMEMFUNC_P (target_type))
7681 /* This is OK, too. */
7682 is_ptrmem = 1;
7683 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7684 /* This is OK, too. This comes from a conversion to reference
7685 type. */
7686 target_type = build_reference_type (target_type);
7687 else
7689 if (complain & tf_error)
7690 error ("cannot resolve overloaded function %qD based on"
7691 " conversion to type %qT",
7692 OVL_NAME (overload), target_type);
7693 return error_mark_node;
7696 /* Non-member functions and static member functions match targets of type
7697 "pointer-to-function" or "reference-to-function." Nonstatic member
7698 functions match targets of type "pointer-to-member-function;" the
7699 function type of the pointer to member is used to select the member
7700 function from the set of overloaded member functions.
7702 So figure out the FUNCTION_TYPE that we want to match against. */
7703 target_fn_type = static_fn_type (target_type);
7705 /* If we can find a non-template function that matches, we can just
7706 use it. There's no point in generating template instantiations
7707 if we're just going to throw them out anyhow. But, of course, we
7708 can only do this when we don't *need* a template function. */
7709 if (!template_only)
7710 for (lkp_iterator iter (overload); iter; ++iter)
7712 tree fn = *iter;
7714 if (TREE_CODE (fn) == TEMPLATE_DECL)
7715 /* We're not looking for templates just yet. */
7716 continue;
7718 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
7719 /* We're looking for a non-static member, and this isn't
7720 one, or vice versa. */
7721 continue;
7723 /* In C++17 we need the noexcept-qualifier to compare types. */
7724 if (flag_noexcept_type
7725 && !maybe_instantiate_noexcept (fn, complain))
7726 continue;
7728 /* See if there's a match. */
7729 tree fntype = static_fn_type (fn);
7730 if (same_type_p (target_fn_type, fntype)
7731 || fnptr_conv_p (target_fn_type, fntype))
7732 matches = tree_cons (fn, NULL_TREE, matches);
7735 /* Now, if we've already got a match (or matches), there's no need
7736 to proceed to the template functions. But, if we don't have a
7737 match we need to look at them, too. */
7738 if (!matches)
7740 tree target_arg_types;
7741 tree target_ret_type;
7742 tree *args;
7743 unsigned int nargs, ia;
7744 tree arg;
7746 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7747 target_ret_type = TREE_TYPE (target_fn_type);
7749 nargs = list_length (target_arg_types);
7750 args = XALLOCAVEC (tree, nargs);
7751 for (arg = target_arg_types, ia = 0;
7752 arg != NULL_TREE && arg != void_list_node;
7753 arg = TREE_CHAIN (arg), ++ia)
7754 args[ia] = TREE_VALUE (arg);
7755 nargs = ia;
7757 for (lkp_iterator iter (overload); iter; ++iter)
7759 tree fn = *iter;
7760 tree instantiation;
7761 tree targs;
7763 if (TREE_CODE (fn) != TEMPLATE_DECL)
7764 /* We're only looking for templates. */
7765 continue;
7767 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7768 != is_ptrmem)
7769 /* We're not looking for a non-static member, and this is
7770 one, or vice versa. */
7771 continue;
7773 tree ret = target_ret_type;
7775 /* If the template has a deduced return type, don't expose it to
7776 template argument deduction. */
7777 if (undeduced_auto_decl (fn))
7778 ret = NULL_TREE;
7780 /* Try to do argument deduction. */
7781 targs = make_tree_vec (DECL_NTPARMS (fn));
7782 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7783 nargs, ret,
7784 DEDUCE_EXACT, LOOKUP_NORMAL,
7785 false, false);
7786 if (instantiation == error_mark_node)
7787 /* Instantiation failed. */
7788 continue;
7790 /* Constraints must be satisfied. This is done before
7791 return type deduction since that instantiates the
7792 function. */
7793 if (flag_concepts && !constraints_satisfied_p (instantiation))
7794 continue;
7796 /* And now force instantiation to do return type deduction. */
7797 if (undeduced_auto_decl (instantiation))
7799 ++function_depth;
7800 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7801 --function_depth;
7803 require_deduced_type (instantiation);
7806 /* In C++17 we need the noexcept-qualifier to compare types. */
7807 if (flag_noexcept_type)
7808 maybe_instantiate_noexcept (instantiation, complain);
7810 /* See if there's a match. */
7811 tree fntype = static_fn_type (instantiation);
7812 if (same_type_p (target_fn_type, fntype)
7813 || fnptr_conv_p (target_fn_type, fntype))
7814 matches = tree_cons (instantiation, fn, matches);
7817 /* Now, remove all but the most specialized of the matches. */
7818 if (matches)
7820 tree match = most_specialized_instantiation (matches);
7822 if (match != error_mark_node)
7823 matches = tree_cons (TREE_PURPOSE (match),
7824 NULL_TREE,
7825 NULL_TREE);
7829 /* Now we should have exactly one function in MATCHES. */
7830 if (matches == NULL_TREE)
7832 /* There were *no* matches. */
7833 if (complain & tf_error)
7835 error ("no matches converting function %qD to type %q#T",
7836 OVL_NAME (overload), target_type);
7838 print_candidates (overload);
7840 return error_mark_node;
7842 else if (TREE_CHAIN (matches))
7844 /* There were too many matches. First check if they're all
7845 the same function. */
7846 tree match = NULL_TREE;
7848 fn = TREE_PURPOSE (matches);
7850 /* For multi-versioned functions, more than one match is just fine and
7851 decls_match will return false as they are different. */
7852 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7853 if (!decls_match (fn, TREE_PURPOSE (match))
7854 && !targetm.target_option.function_versions
7855 (fn, TREE_PURPOSE (match)))
7856 break;
7858 if (match)
7860 if (complain & tf_error)
7862 error ("converting overloaded function %qD to type %q#T is ambiguous",
7863 OVL_NAME (overload), target_type);
7865 /* Since print_candidates expects the functions in the
7866 TREE_VALUE slot, we flip them here. */
7867 for (match = matches; match; match = TREE_CHAIN (match))
7868 TREE_VALUE (match) = TREE_PURPOSE (match);
7870 print_candidates (matches);
7873 return error_mark_node;
7877 /* Good, exactly one match. Now, convert it to the correct type. */
7878 fn = TREE_PURPOSE (matches);
7880 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7881 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7883 static int explained;
7885 if (!(complain & tf_error))
7886 return error_mark_node;
7888 permerror (input_location, "assuming pointer to member %qD", fn);
7889 if (!explained)
7891 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
7892 explained = 1;
7896 /* If a pointer to a function that is multi-versioned is requested, the
7897 pointer to the dispatcher function is returned instead. This works
7898 well because indirectly calling the function will dispatch the right
7899 function version at run-time. */
7900 if (DECL_FUNCTION_VERSIONED (fn))
7902 fn = get_function_version_dispatcher (fn);
7903 if (fn == NULL)
7904 return error_mark_node;
7905 /* Mark all the versions corresponding to the dispatcher as used. */
7906 if (!(complain & tf_conv))
7907 mark_versions_used (fn);
7910 /* If we're doing overload resolution purely for the purpose of
7911 determining conversion sequences, we should not consider the
7912 function used. If this conversion sequence is selected, the
7913 function will be marked as used at this point. */
7914 if (!(complain & tf_conv))
7916 /* Make =delete work with SFINAE. */
7917 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7918 return error_mark_node;
7919 if (!mark_used (fn, complain) && !(complain & tf_error))
7920 return error_mark_node;
7923 /* We could not check access to member functions when this
7924 expression was originally created since we did not know at that
7925 time to which function the expression referred. */
7926 if (DECL_FUNCTION_MEMBER_P (fn))
7928 gcc_assert (access_path);
7929 perform_or_defer_access_check (access_path, fn, fn, complain);
7932 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7933 return cp_build_addr_expr (fn, complain);
7934 else
7936 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7937 will mark the function as addressed, but here we must do it
7938 explicitly. */
7939 cxx_mark_addressable (fn);
7941 return fn;
7945 /* This function will instantiate the type of the expression given in
7946 RHS to match the type of LHSTYPE. If errors exist, then return
7947 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7948 we complain on errors. If we are not complaining, never modify rhs,
7949 as overload resolution wants to try many possible instantiations, in
7950 the hope that at least one will work.
7952 For non-recursive calls, LHSTYPE should be a function, pointer to
7953 function, or a pointer to member function. */
7955 tree
7956 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7958 tsubst_flags_t complain_in = complain;
7959 tree access_path = NULL_TREE;
7961 complain &= ~tf_ptrmem_ok;
7963 if (lhstype == unknown_type_node)
7965 if (complain & tf_error)
7966 error ("not enough type information");
7967 return error_mark_node;
7970 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
7972 tree fntype = non_reference (lhstype);
7973 if (same_type_p (fntype, TREE_TYPE (rhs)))
7974 return rhs;
7975 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
7976 return rhs;
7977 if (flag_ms_extensions
7978 && TYPE_PTRMEMFUNC_P (fntype)
7979 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
7980 /* Microsoft allows `A::f' to be resolved to a
7981 pointer-to-member. */
7983 else
7985 if (complain & tf_error)
7986 error ("cannot convert %qE from type %qT to type %qT",
7987 rhs, TREE_TYPE (rhs), fntype);
7988 return error_mark_node;
7992 if (BASELINK_P (rhs))
7994 access_path = BASELINK_ACCESS_BINFO (rhs);
7995 rhs = BASELINK_FUNCTIONS (rhs);
7998 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
7999 deduce any type information. */
8000 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8002 if (complain & tf_error)
8003 error ("not enough type information");
8004 return error_mark_node;
8007 /* If we instantiate a template, and it is a A ?: C expression
8008 with omitted B, look through the SAVE_EXPR. */
8009 if (TREE_CODE (rhs) == SAVE_EXPR)
8010 rhs = TREE_OPERAND (rhs, 0);
8012 /* There are only a few kinds of expressions that may have a type
8013 dependent on overload resolution. */
8014 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8015 || TREE_CODE (rhs) == COMPONENT_REF
8016 || is_overloaded_fn (rhs)
8017 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8019 /* This should really only be used when attempting to distinguish
8020 what sort of a pointer to function we have. For now, any
8021 arithmetic operation which is not supported on pointers
8022 is rejected as an error. */
8024 switch (TREE_CODE (rhs))
8026 case COMPONENT_REF:
8028 tree member = TREE_OPERAND (rhs, 1);
8030 member = instantiate_type (lhstype, member, complain);
8031 if (member != error_mark_node
8032 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8033 /* Do not lose object's side effects. */
8034 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8035 TREE_OPERAND (rhs, 0), member);
8036 return member;
8039 case OFFSET_REF:
8040 rhs = TREE_OPERAND (rhs, 1);
8041 if (BASELINK_P (rhs))
8042 return instantiate_type (lhstype, rhs, complain_in);
8044 /* This can happen if we are forming a pointer-to-member for a
8045 member template. */
8046 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8048 /* Fall through. */
8050 case TEMPLATE_ID_EXPR:
8052 tree fns = TREE_OPERAND (rhs, 0);
8053 tree args = TREE_OPERAND (rhs, 1);
8055 return
8056 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8057 /*template_only=*/true,
8058 args, access_path);
8061 case OVERLOAD:
8062 case FUNCTION_DECL:
8063 return
8064 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8065 /*template_only=*/false,
8066 /*explicit_targs=*/NULL_TREE,
8067 access_path);
8069 case ADDR_EXPR:
8071 if (PTRMEM_OK_P (rhs))
8072 complain |= tf_ptrmem_ok;
8074 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8077 case ERROR_MARK:
8078 return error_mark_node;
8080 default:
8081 gcc_unreachable ();
8083 return error_mark_node;
8086 /* Return the name of the virtual function pointer field
8087 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8088 this may have to look back through base types to find the
8089 ultimate field name. (For single inheritance, these could
8090 all be the same name. Who knows for multiple inheritance). */
8092 static tree
8093 get_vfield_name (tree type)
8095 tree binfo, base_binfo;
8097 for (binfo = TYPE_BINFO (type);
8098 BINFO_N_BASE_BINFOS (binfo);
8099 binfo = base_binfo)
8101 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8103 if (BINFO_VIRTUAL_P (base_binfo)
8104 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8105 break;
8108 type = BINFO_TYPE (binfo);
8109 tree ctor_name = constructor_name (type);
8110 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8111 + IDENTIFIER_LENGTH (ctor_name) + 2);
8112 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8113 return get_identifier (buf);
8116 void
8117 print_class_statistics (void)
8119 if (! GATHER_STATISTICS)
8120 return;
8122 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
8123 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
8124 if (n_vtables)
8126 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
8127 n_vtables, n_vtable_searches);
8128 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
8129 n_vtable_entries, n_vtable_elems);
8133 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8134 according to [class]:
8135 The class-name is also inserted
8136 into the scope of the class itself. For purposes of access checking,
8137 the inserted class name is treated as if it were a public member name. */
8139 void
8140 build_self_reference (void)
8142 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8143 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8145 DECL_NONLOCAL (value) = 1;
8146 DECL_CONTEXT (value) = current_class_type;
8147 DECL_ARTIFICIAL (value) = 1;
8148 SET_DECL_SELF_REFERENCE_P (value);
8149 set_underlying_type (value);
8151 if (processing_template_decl)
8152 value = push_template_decl (value);
8154 tree saved_cas = current_access_specifier;
8155 current_access_specifier = access_public_node;
8156 finish_member_declaration (value);
8157 current_access_specifier = saved_cas;
8160 /* Returns 1 if TYPE contains only padding bytes. */
8163 is_empty_class (tree type)
8165 if (type == error_mark_node)
8166 return 0;
8168 if (! CLASS_TYPE_P (type))
8169 return 0;
8171 return CLASSTYPE_EMPTY_P (type);
8174 /* Returns true if TYPE contains no actual data, just various
8175 possible combinations of empty classes and possibly a vptr. */
8177 bool
8178 is_really_empty_class (tree type)
8180 if (CLASS_TYPE_P (type))
8182 tree field;
8183 tree binfo;
8184 tree base_binfo;
8185 int i;
8187 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8188 out, but we'd like to be able to check this before then. */
8189 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8190 return true;
8192 for (binfo = TYPE_BINFO (type), i = 0;
8193 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8194 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8195 return false;
8196 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8197 if (TREE_CODE (field) == FIELD_DECL
8198 && !DECL_ARTIFICIAL (field)
8199 /* An unnamed bit-field is not a data member. */
8200 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field))
8201 && !is_really_empty_class (TREE_TYPE (field)))
8202 return false;
8203 return true;
8205 else if (TREE_CODE (type) == ARRAY_TYPE)
8206 return (integer_zerop (array_type_nelts_top (type))
8207 || is_really_empty_class (TREE_TYPE (type)));
8208 return false;
8211 /* Note that NAME was looked up while the current class was being
8212 defined and that the result of that lookup was DECL. */
8214 void
8215 maybe_note_name_used_in_class (tree name, tree decl)
8217 splay_tree names_used;
8219 /* If we're not defining a class, there's nothing to do. */
8220 if (!(innermost_scope_kind() == sk_class
8221 && TYPE_BEING_DEFINED (current_class_type)
8222 && !LAMBDA_TYPE_P (current_class_type)))
8223 return;
8225 /* If there's already a binding for this NAME, then we don't have
8226 anything to worry about. */
8227 if (lookup_member (current_class_type, name,
8228 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8229 return;
8231 if (!current_class_stack[current_class_depth - 1].names_used)
8232 current_class_stack[current_class_depth - 1].names_used
8233 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8234 names_used = current_class_stack[current_class_depth - 1].names_used;
8236 splay_tree_insert (names_used,
8237 (splay_tree_key) name,
8238 (splay_tree_value) decl);
8241 /* Note that NAME was declared (as DECL) in the current class. Check
8242 to see that the declaration is valid. */
8244 void
8245 note_name_declared_in_class (tree name, tree decl)
8247 splay_tree names_used;
8248 splay_tree_node n;
8250 /* Look to see if we ever used this name. */
8251 names_used
8252 = current_class_stack[current_class_depth - 1].names_used;
8253 if (!names_used)
8254 return;
8255 /* The C language allows members to be declared with a type of the same
8256 name, and the C++ standard says this diagnostic is not required. So
8257 allow it in extern "C" blocks unless predantic is specified.
8258 Allow it in all cases if -ms-extensions is specified. */
8259 if ((!pedantic && current_lang_name == lang_name_c)
8260 || flag_ms_extensions)
8261 return;
8262 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8263 if (n)
8265 /* [basic.scope.class]
8267 A name N used in a class S shall refer to the same declaration
8268 in its context and when re-evaluated in the completed scope of
8269 S. */
8270 permerror (input_location, "declaration of %q#D", decl);
8271 permerror (location_of ((tree) n->value),
8272 "changes meaning of %qD from %q#D",
8273 OVL_NAME (decl), (tree) n->value);
8277 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8278 Secondary vtables are merged with primary vtables; this function
8279 will return the VAR_DECL for the primary vtable. */
8281 tree
8282 get_vtbl_decl_for_binfo (tree binfo)
8284 tree decl;
8286 decl = BINFO_VTABLE (binfo);
8287 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8289 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8290 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8292 if (decl)
8293 gcc_assert (VAR_P (decl));
8294 return decl;
8298 /* Returns the binfo for the primary base of BINFO. If the resulting
8299 BINFO is a virtual base, and it is inherited elsewhere in the
8300 hierarchy, then the returned binfo might not be the primary base of
8301 BINFO in the complete object. Check BINFO_PRIMARY_P or
8302 BINFO_LOST_PRIMARY_P to be sure. */
8304 static tree
8305 get_primary_binfo (tree binfo)
8307 tree primary_base;
8309 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8310 if (!primary_base)
8311 return NULL_TREE;
8313 return copied_binfo (primary_base, binfo);
8316 /* As above, but iterate until we reach the binfo that actually provides the
8317 vptr for BINFO. */
8319 static tree
8320 most_primary_binfo (tree binfo)
8322 tree b = binfo;
8323 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8324 && !BINFO_LOST_PRIMARY_P (b))
8326 tree primary_base = get_primary_binfo (b);
8327 gcc_assert (BINFO_PRIMARY_P (primary_base)
8328 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8329 b = primary_base;
8331 return b;
8334 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8335 type. Note that the virtual inheritance might be above or below BINFO in
8336 the hierarchy. */
8338 bool
8339 vptr_via_virtual_p (tree binfo)
8341 if (TYPE_P (binfo))
8342 binfo = TYPE_BINFO (binfo);
8343 tree primary = most_primary_binfo (binfo);
8344 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8345 a morally virtual base. */
8346 tree virt = binfo_via_virtual (primary, NULL_TREE);
8347 return virt != NULL_TREE;
8350 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8352 static int
8353 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8355 if (!indented_p)
8356 fprintf (stream, "%*s", indent, "");
8357 return 1;
8360 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8361 INDENT should be zero when called from the top level; it is
8362 incremented recursively. IGO indicates the next expected BINFO in
8363 inheritance graph ordering. */
8365 static tree
8366 dump_class_hierarchy_r (FILE *stream,
8367 dump_flags_t flags,
8368 tree binfo,
8369 tree igo,
8370 int indent)
8372 int indented = 0;
8373 tree base_binfo;
8374 int i;
8376 indented = maybe_indent_hierarchy (stream, indent, 0);
8377 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8378 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8379 (HOST_WIDE_INT) (uintptr_t) binfo);
8380 if (binfo != igo)
8382 fprintf (stream, "alternative-path\n");
8383 return igo;
8385 igo = TREE_CHAIN (binfo);
8387 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8388 tree_to_shwi (BINFO_OFFSET (binfo)));
8389 if (is_empty_class (BINFO_TYPE (binfo)))
8390 fprintf (stream, " empty");
8391 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8392 fprintf (stream, " nearly-empty");
8393 if (BINFO_VIRTUAL_P (binfo))
8394 fprintf (stream, " virtual");
8395 fprintf (stream, "\n");
8397 indented = 0;
8398 if (BINFO_PRIMARY_P (binfo))
8400 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8401 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8402 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8403 TFF_PLAIN_IDENTIFIER),
8404 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8406 if (BINFO_LOST_PRIMARY_P (binfo))
8408 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8409 fprintf (stream, " lost-primary");
8411 if (indented)
8412 fprintf (stream, "\n");
8414 if (!(flags & TDF_SLIM))
8416 int indented = 0;
8418 if (BINFO_SUBVTT_INDEX (binfo))
8420 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8421 fprintf (stream, " subvttidx=%s",
8422 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8423 TFF_PLAIN_IDENTIFIER));
8425 if (BINFO_VPTR_INDEX (binfo))
8427 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8428 fprintf (stream, " vptridx=%s",
8429 expr_as_string (BINFO_VPTR_INDEX (binfo),
8430 TFF_PLAIN_IDENTIFIER));
8432 if (BINFO_VPTR_FIELD (binfo))
8434 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8435 fprintf (stream, " vbaseoffset=%s",
8436 expr_as_string (BINFO_VPTR_FIELD (binfo),
8437 TFF_PLAIN_IDENTIFIER));
8439 if (BINFO_VTABLE (binfo))
8441 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8442 fprintf (stream, " vptr=%s",
8443 expr_as_string (BINFO_VTABLE (binfo),
8444 TFF_PLAIN_IDENTIFIER));
8447 if (indented)
8448 fprintf (stream, "\n");
8451 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8452 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8454 return igo;
8457 /* Dump the BINFO hierarchy for T. */
8459 static void
8460 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8462 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8463 fprintf (stream, " size=%lu align=%lu\n",
8464 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8465 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8466 fprintf (stream, " base size=%lu base align=%lu\n",
8467 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8468 / BITS_PER_UNIT),
8469 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8470 / BITS_PER_UNIT));
8471 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8472 fprintf (stream, "\n");
8475 /* Debug interface to hierarchy dumping. */
8477 void
8478 debug_class (tree t)
8480 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8483 static void
8484 dump_class_hierarchy (tree t)
8486 dump_flags_t flags;
8487 if (FILE *stream = dump_begin (class_dump_id, &flags))
8489 dump_class_hierarchy_1 (stream, flags, t);
8490 dump_end (class_dump_id, stream);
8494 static void
8495 dump_array (FILE * stream, tree decl)
8497 tree value;
8498 unsigned HOST_WIDE_INT ix;
8499 HOST_WIDE_INT elt;
8500 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8502 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8503 / BITS_PER_UNIT);
8504 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8505 fprintf (stream, " %s entries",
8506 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8507 TFF_PLAIN_IDENTIFIER));
8508 fprintf (stream, "\n");
8510 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8511 ix, value)
8512 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8513 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8516 static void
8517 dump_vtable (tree t, tree binfo, tree vtable)
8519 dump_flags_t flags;
8520 FILE *stream = dump_begin (class_dump_id, &flags);
8522 if (!stream)
8523 return;
8525 if (!(flags & TDF_SLIM))
8527 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8529 fprintf (stream, "%s for %s",
8530 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8531 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8532 if (ctor_vtbl_p)
8534 if (!BINFO_VIRTUAL_P (binfo))
8535 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8536 (HOST_WIDE_INT) (uintptr_t) binfo);
8537 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8539 fprintf (stream, "\n");
8540 dump_array (stream, vtable);
8541 fprintf (stream, "\n");
8544 dump_end (class_dump_id, stream);
8547 static void
8548 dump_vtt (tree t, tree vtt)
8550 dump_flags_t flags;
8551 FILE *stream = dump_begin (class_dump_id, &flags);
8553 if (!stream)
8554 return;
8556 if (!(flags & TDF_SLIM))
8558 fprintf (stream, "VTT for %s\n",
8559 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8560 dump_array (stream, vtt);
8561 fprintf (stream, "\n");
8564 dump_end (class_dump_id, stream);
8567 /* Dump a function or thunk and its thunkees. */
8569 static void
8570 dump_thunk (FILE *stream, int indent, tree thunk)
8572 static const char spaces[] = " ";
8573 tree name = DECL_NAME (thunk);
8574 tree thunks;
8576 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8577 (void *)thunk,
8578 !DECL_THUNK_P (thunk) ? "function"
8579 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8580 name ? IDENTIFIER_POINTER (name) : "<unset>");
8581 if (DECL_THUNK_P (thunk))
8583 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8584 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8586 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8587 if (!virtual_adjust)
8588 /*NOP*/;
8589 else if (DECL_THIS_THUNK_P (thunk))
8590 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8591 tree_to_shwi (virtual_adjust));
8592 else
8593 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8594 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8595 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8596 if (THUNK_ALIAS (thunk))
8597 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8599 fprintf (stream, "\n");
8600 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8601 dump_thunk (stream, indent + 2, thunks);
8604 /* Dump the thunks for FN. */
8606 void
8607 debug_thunks (tree fn)
8609 dump_thunk (stderr, 0, fn);
8612 /* Virtual function table initialization. */
8614 /* Create all the necessary vtables for T and its base classes. */
8616 static void
8617 finish_vtbls (tree t)
8619 tree vbase;
8620 vec<constructor_elt, va_gc> *v = NULL;
8621 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8623 /* We lay out the primary and secondary vtables in one contiguous
8624 vtable. The primary vtable is first, followed by the non-virtual
8625 secondary vtables in inheritance graph order. */
8626 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8627 vtable, t, &v);
8629 /* Then come the virtual bases, also in inheritance graph order. */
8630 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8632 if (!BINFO_VIRTUAL_P (vbase))
8633 continue;
8634 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8637 if (BINFO_VTABLE (TYPE_BINFO (t)))
8638 initialize_vtable (TYPE_BINFO (t), v);
8641 /* Initialize the vtable for BINFO with the INITS. */
8643 static void
8644 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8646 tree decl;
8648 layout_vtable_decl (binfo, vec_safe_length (inits));
8649 decl = get_vtbl_decl_for_binfo (binfo);
8650 initialize_artificial_var (decl, inits);
8651 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8654 /* Build the VTT (virtual table table) for T.
8655 A class requires a VTT if it has virtual bases.
8657 This holds
8658 1 - primary virtual pointer for complete object T
8659 2 - secondary VTTs for each direct non-virtual base of T which requires a
8661 3 - secondary virtual pointers for each direct or indirect base of T which
8662 has virtual bases or is reachable via a virtual path from T.
8663 4 - secondary VTTs for each direct or indirect virtual base of T.
8665 Secondary VTTs look like complete object VTTs without part 4. */
8667 static void
8668 build_vtt (tree t)
8670 tree type;
8671 tree vtt;
8672 tree index;
8673 vec<constructor_elt, va_gc> *inits;
8675 /* Build up the initializers for the VTT. */
8676 inits = NULL;
8677 index = size_zero_node;
8678 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8680 /* If we didn't need a VTT, we're done. */
8681 if (!inits)
8682 return;
8684 /* Figure out the type of the VTT. */
8685 type = build_array_of_n_type (const_ptr_type_node,
8686 inits->length ());
8688 /* Now, build the VTT object itself. */
8689 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8690 initialize_artificial_var (vtt, inits);
8691 /* Add the VTT to the vtables list. */
8692 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8693 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8695 dump_vtt (t, vtt);
8698 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8699 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8700 and CHAIN the vtable pointer for this binfo after construction is
8701 complete. VALUE can also be another BINFO, in which case we recurse. */
8703 static tree
8704 binfo_ctor_vtable (tree binfo)
8706 tree vt;
8708 while (1)
8710 vt = BINFO_VTABLE (binfo);
8711 if (TREE_CODE (vt) == TREE_LIST)
8712 vt = TREE_VALUE (vt);
8713 if (TREE_CODE (vt) == TREE_BINFO)
8714 binfo = vt;
8715 else
8716 break;
8719 return vt;
8722 /* Data for secondary VTT initialization. */
8723 struct secondary_vptr_vtt_init_data
8725 /* Is this the primary VTT? */
8726 bool top_level_p;
8728 /* Current index into the VTT. */
8729 tree index;
8731 /* Vector of initializers built up. */
8732 vec<constructor_elt, va_gc> *inits;
8734 /* The type being constructed by this secondary VTT. */
8735 tree type_being_constructed;
8738 /* Recursively build the VTT-initializer for BINFO (which is in the
8739 hierarchy dominated by T). INITS points to the end of the initializer
8740 list to date. INDEX is the VTT index where the next element will be
8741 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8742 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8743 for virtual bases of T. When it is not so, we build the constructor
8744 vtables for the BINFO-in-T variant. */
8746 static void
8747 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8748 tree *index)
8750 int i;
8751 tree b;
8752 tree init;
8753 secondary_vptr_vtt_init_data data;
8754 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8756 /* We only need VTTs for subobjects with virtual bases. */
8757 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8758 return;
8760 /* We need to use a construction vtable if this is not the primary
8761 VTT. */
8762 if (!top_level_p)
8764 build_ctor_vtbl_group (binfo, t);
8766 /* Record the offset in the VTT where this sub-VTT can be found. */
8767 BINFO_SUBVTT_INDEX (binfo) = *index;
8770 /* Add the address of the primary vtable for the complete object. */
8771 init = binfo_ctor_vtable (binfo);
8772 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8773 if (top_level_p)
8775 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8776 BINFO_VPTR_INDEX (binfo) = *index;
8778 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8780 /* Recursively add the secondary VTTs for non-virtual bases. */
8781 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8782 if (!BINFO_VIRTUAL_P (b))
8783 build_vtt_inits (b, t, inits, index);
8785 /* Add secondary virtual pointers for all subobjects of BINFO with
8786 either virtual bases or reachable along a virtual path, except
8787 subobjects that are non-virtual primary bases. */
8788 data.top_level_p = top_level_p;
8789 data.index = *index;
8790 data.inits = *inits;
8791 data.type_being_constructed = BINFO_TYPE (binfo);
8793 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8795 *index = data.index;
8797 /* data.inits might have grown as we added secondary virtual pointers.
8798 Make sure our caller knows about the new vector. */
8799 *inits = data.inits;
8801 if (top_level_p)
8802 /* Add the secondary VTTs for virtual bases in inheritance graph
8803 order. */
8804 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8806 if (!BINFO_VIRTUAL_P (b))
8807 continue;
8809 build_vtt_inits (b, t, inits, index);
8811 else
8812 /* Remove the ctor vtables we created. */
8813 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8816 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8817 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8819 static tree
8820 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8822 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8824 /* We don't care about bases that don't have vtables. */
8825 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8826 return dfs_skip_bases;
8828 /* We're only interested in proper subobjects of the type being
8829 constructed. */
8830 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8831 return NULL_TREE;
8833 /* We're only interested in bases with virtual bases or reachable
8834 via a virtual path from the type being constructed. */
8835 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8836 || binfo_via_virtual (binfo, data->type_being_constructed)))
8837 return dfs_skip_bases;
8839 /* We're not interested in non-virtual primary bases. */
8840 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8841 return NULL_TREE;
8843 /* Record the index where this secondary vptr can be found. */
8844 if (data->top_level_p)
8846 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8847 BINFO_VPTR_INDEX (binfo) = data->index;
8849 if (BINFO_VIRTUAL_P (binfo))
8851 /* It's a primary virtual base, and this is not a
8852 construction vtable. Find the base this is primary of in
8853 the inheritance graph, and use that base's vtable
8854 now. */
8855 while (BINFO_PRIMARY_P (binfo))
8856 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8860 /* Add the initializer for the secondary vptr itself. */
8861 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8863 /* Advance the vtt index. */
8864 data->index = size_binop (PLUS_EXPR, data->index,
8865 TYPE_SIZE_UNIT (ptr_type_node));
8867 return NULL_TREE;
8870 /* Called from build_vtt_inits via dfs_walk. After building
8871 constructor vtables and generating the sub-vtt from them, we need
8872 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8873 binfo of the base whose sub vtt was generated. */
8875 static tree
8876 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8878 tree vtable = BINFO_VTABLE (binfo);
8880 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8881 /* If this class has no vtable, none of its bases do. */
8882 return dfs_skip_bases;
8884 if (!vtable)
8885 /* This might be a primary base, so have no vtable in this
8886 hierarchy. */
8887 return NULL_TREE;
8889 /* If we scribbled the construction vtable vptr into BINFO, clear it
8890 out now. */
8891 if (TREE_CODE (vtable) == TREE_LIST
8892 && (TREE_PURPOSE (vtable) == (tree) data))
8893 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8895 return NULL_TREE;
8898 /* Build the construction vtable group for BINFO which is in the
8899 hierarchy dominated by T. */
8901 static void
8902 build_ctor_vtbl_group (tree binfo, tree t)
8904 tree type;
8905 tree vtbl;
8906 tree id;
8907 tree vbase;
8908 vec<constructor_elt, va_gc> *v;
8910 /* See if we've already created this construction vtable group. */
8911 id = mangle_ctor_vtbl_for_type (t, binfo);
8912 if (get_global_binding (id))
8913 return;
8915 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8916 /* Build a version of VTBL (with the wrong type) for use in
8917 constructing the addresses of secondary vtables in the
8918 construction vtable group. */
8919 vtbl = build_vtable (t, id, ptr_type_node);
8920 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8921 /* Don't export construction vtables from shared libraries. Even on
8922 targets that don't support hidden visibility, this tells
8923 can_refer_decl_in_current_unit_p not to assume that it's safe to
8924 access from a different compilation unit (bz 54314). */
8925 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8926 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8928 v = NULL;
8929 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8930 binfo, vtbl, t, &v);
8932 /* Add the vtables for each of our virtual bases using the vbase in T
8933 binfo. */
8934 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8935 vbase;
8936 vbase = TREE_CHAIN (vbase))
8938 tree b;
8940 if (!BINFO_VIRTUAL_P (vbase))
8941 continue;
8942 b = copied_binfo (vbase, binfo);
8944 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8947 /* Figure out the type of the construction vtable. */
8948 type = build_array_of_n_type (vtable_entry_type, v->length ());
8949 layout_type (type);
8950 TREE_TYPE (vtbl) = type;
8951 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8952 layout_decl (vtbl, 0);
8954 /* Initialize the construction vtable. */
8955 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8956 initialize_artificial_var (vtbl, v);
8957 dump_vtable (t, binfo, vtbl);
8960 /* Add the vtbl initializers for BINFO (and its bases other than
8961 non-virtual primaries) to the list of INITS. BINFO is in the
8962 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8963 the constructor the vtbl inits should be accumulated for. (If this
8964 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8965 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8966 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8967 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8968 but are not necessarily the same in terms of layout. */
8970 static void
8971 accumulate_vtbl_inits (tree binfo,
8972 tree orig_binfo,
8973 tree rtti_binfo,
8974 tree vtbl,
8975 tree t,
8976 vec<constructor_elt, va_gc> **inits)
8978 int i;
8979 tree base_binfo;
8980 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
8982 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
8984 /* If it doesn't have a vptr, we don't do anything. */
8985 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8986 return;
8988 /* If we're building a construction vtable, we're not interested in
8989 subobjects that don't require construction vtables. */
8990 if (ctor_vtbl_p
8991 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8992 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
8993 return;
8995 /* Build the initializers for the BINFO-in-T vtable. */
8996 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
8998 /* Walk the BINFO and its bases. We walk in preorder so that as we
8999 initialize each vtable we can figure out at what offset the
9000 secondary vtable lies from the primary vtable. We can't use
9001 dfs_walk here because we need to iterate through bases of BINFO
9002 and RTTI_BINFO simultaneously. */
9003 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9005 /* Skip virtual bases. */
9006 if (BINFO_VIRTUAL_P (base_binfo))
9007 continue;
9008 accumulate_vtbl_inits (base_binfo,
9009 BINFO_BASE_BINFO (orig_binfo, i),
9010 rtti_binfo, vtbl, t,
9011 inits);
9015 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9016 BINFO vtable to L. */
9018 static void
9019 dfs_accumulate_vtbl_inits (tree binfo,
9020 tree orig_binfo,
9021 tree rtti_binfo,
9022 tree orig_vtbl,
9023 tree t,
9024 vec<constructor_elt, va_gc> **l)
9026 tree vtbl = NULL_TREE;
9027 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9028 int n_inits;
9030 if (ctor_vtbl_p
9031 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9033 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9034 primary virtual base. If it is not the same primary in
9035 the hierarchy of T, we'll need to generate a ctor vtable
9036 for it, to place at its location in T. If it is the same
9037 primary, we still need a VTT entry for the vtable, but it
9038 should point to the ctor vtable for the base it is a
9039 primary for within the sub-hierarchy of RTTI_BINFO.
9041 There are three possible cases:
9043 1) We are in the same place.
9044 2) We are a primary base within a lost primary virtual base of
9045 RTTI_BINFO.
9046 3) We are primary to something not a base of RTTI_BINFO. */
9048 tree b;
9049 tree last = NULL_TREE;
9051 /* First, look through the bases we are primary to for RTTI_BINFO
9052 or a virtual base. */
9053 b = binfo;
9054 while (BINFO_PRIMARY_P (b))
9056 b = BINFO_INHERITANCE_CHAIN (b);
9057 last = b;
9058 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9059 goto found;
9061 /* If we run out of primary links, keep looking down our
9062 inheritance chain; we might be an indirect primary. */
9063 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9064 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9065 break;
9066 found:
9068 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9069 base B and it is a base of RTTI_BINFO, this is case 2. In
9070 either case, we share our vtable with LAST, i.e. the
9071 derived-most base within B of which we are a primary. */
9072 if (b == rtti_binfo
9073 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9074 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9075 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9076 binfo_ctor_vtable after everything's been set up. */
9077 vtbl = last;
9079 /* Otherwise, this is case 3 and we get our own. */
9081 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9082 return;
9084 n_inits = vec_safe_length (*l);
9086 if (!vtbl)
9088 tree index;
9089 int non_fn_entries;
9091 /* Add the initializer for this vtable. */
9092 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9093 &non_fn_entries, l);
9095 /* Figure out the position to which the VPTR should point. */
9096 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9097 index = size_binop (MULT_EXPR,
9098 TYPE_SIZE_UNIT (vtable_entry_type),
9099 size_int (non_fn_entries + n_inits));
9100 vtbl = fold_build_pointer_plus (vtbl, index);
9103 if (ctor_vtbl_p)
9104 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9105 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9106 straighten this out. */
9107 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9108 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9109 /* Throw away any unneeded intializers. */
9110 (*l)->truncate (n_inits);
9111 else
9112 /* For an ordinary vtable, set BINFO_VTABLE. */
9113 BINFO_VTABLE (binfo) = vtbl;
9116 static GTY(()) tree abort_fndecl_addr;
9117 static GTY(()) tree dvirt_fn;
9119 /* Construct the initializer for BINFO's virtual function table. BINFO
9120 is part of the hierarchy dominated by T. If we're building a
9121 construction vtable, the ORIG_BINFO is the binfo we should use to
9122 find the actual function pointers to put in the vtable - but they
9123 can be overridden on the path to most-derived in the graph that
9124 ORIG_BINFO belongs. Otherwise,
9125 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9126 BINFO that should be indicated by the RTTI information in the
9127 vtable; it will be a base class of T, rather than T itself, if we
9128 are building a construction vtable.
9130 The value returned is a TREE_LIST suitable for wrapping in a
9131 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9132 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9133 number of non-function entries in the vtable.
9135 It might seem that this function should never be called with a
9136 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9137 base is always subsumed by a derived class vtable. However, when
9138 we are building construction vtables, we do build vtables for
9139 primary bases; we need these while the primary base is being
9140 constructed. */
9142 static void
9143 build_vtbl_initializer (tree binfo,
9144 tree orig_binfo,
9145 tree t,
9146 tree rtti_binfo,
9147 int* non_fn_entries_p,
9148 vec<constructor_elt, va_gc> **inits)
9150 tree v;
9151 vtbl_init_data vid;
9152 unsigned ix, jx;
9153 tree vbinfo;
9154 vec<tree, va_gc> *vbases;
9155 constructor_elt *e;
9157 /* Initialize VID. */
9158 memset (&vid, 0, sizeof (vid));
9159 vid.binfo = binfo;
9160 vid.derived = t;
9161 vid.rtti_binfo = rtti_binfo;
9162 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9163 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9164 vid.generate_vcall_entries = true;
9165 /* The first vbase or vcall offset is at index -3 in the vtable. */
9166 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9168 /* Add entries to the vtable for RTTI. */
9169 build_rtti_vtbl_entries (binfo, &vid);
9171 /* Create an array for keeping track of the functions we've
9172 processed. When we see multiple functions with the same
9173 signature, we share the vcall offsets. */
9174 vec_alloc (vid.fns, 32);
9175 /* Add the vcall and vbase offset entries. */
9176 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9178 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9179 build_vbase_offset_vtbl_entries. */
9180 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9181 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9182 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9184 /* If the target requires padding between data entries, add that now. */
9185 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9187 int n_entries = vec_safe_length (vid.inits);
9189 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9191 /* Move data entries into their new positions and add padding
9192 after the new positions. Iterate backwards so we don't
9193 overwrite entries that we would need to process later. */
9194 for (ix = n_entries - 1;
9195 vid.inits->iterate (ix, &e);
9196 ix--)
9198 int j;
9199 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9200 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9202 (*vid.inits)[new_position] = *e;
9204 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9206 constructor_elt *f = &(*vid.inits)[new_position - j];
9207 f->index = NULL_TREE;
9208 f->value = build1 (NOP_EXPR, vtable_entry_type,
9209 null_pointer_node);
9214 if (non_fn_entries_p)
9215 *non_fn_entries_p = vec_safe_length (vid.inits);
9217 /* The initializers for virtual functions were built up in reverse
9218 order. Straighten them out and add them to the running list in one
9219 step. */
9220 jx = vec_safe_length (*inits);
9221 vec_safe_grow (*inits, jx + vid.inits->length ());
9223 for (ix = vid.inits->length () - 1;
9224 vid.inits->iterate (ix, &e);
9225 ix--, jx++)
9226 (**inits)[jx] = *e;
9228 /* Go through all the ordinary virtual functions, building up
9229 initializers. */
9230 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9232 tree delta;
9233 tree vcall_index;
9234 tree fn, fn_original;
9235 tree init = NULL_TREE;
9237 fn = BV_FN (v);
9238 fn_original = fn;
9239 if (DECL_THUNK_P (fn))
9241 if (!DECL_NAME (fn))
9242 finish_thunk (fn);
9243 if (THUNK_ALIAS (fn))
9245 fn = THUNK_ALIAS (fn);
9246 BV_FN (v) = fn;
9248 fn_original = THUNK_TARGET (fn);
9251 /* If the only definition of this function signature along our
9252 primary base chain is from a lost primary, this vtable slot will
9253 never be used, so just zero it out. This is important to avoid
9254 requiring extra thunks which cannot be generated with the function.
9256 We first check this in update_vtable_entry_for_fn, so we handle
9257 restored primary bases properly; we also need to do it here so we
9258 zero out unused slots in ctor vtables, rather than filling them
9259 with erroneous values (though harmless, apart from relocation
9260 costs). */
9261 if (BV_LOST_PRIMARY (v))
9262 init = size_zero_node;
9264 if (! init)
9266 /* Pull the offset for `this', and the function to call, out of
9267 the list. */
9268 delta = BV_DELTA (v);
9269 vcall_index = BV_VCALL_INDEX (v);
9271 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9272 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9274 /* You can't call an abstract virtual function; it's abstract.
9275 So, we replace these functions with __pure_virtual. */
9276 if (DECL_PURE_VIRTUAL_P (fn_original))
9278 fn = abort_fndecl;
9279 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9281 if (abort_fndecl_addr == NULL)
9282 abort_fndecl_addr
9283 = fold_convert (vfunc_ptr_type_node,
9284 build_fold_addr_expr (fn));
9285 init = abort_fndecl_addr;
9288 /* Likewise for deleted virtuals. */
9289 else if (DECL_DELETED_FN (fn_original))
9291 if (!dvirt_fn)
9293 tree name = get_identifier ("__cxa_deleted_virtual");
9294 dvirt_fn = get_global_binding (name);
9295 if (!dvirt_fn)
9296 dvirt_fn = push_library_fn
9297 (name,
9298 build_function_type_list (void_type_node, NULL_TREE),
9299 NULL_TREE, ECF_NORETURN | ECF_COLD);
9301 fn = dvirt_fn;
9302 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9303 init = fold_convert (vfunc_ptr_type_node,
9304 build_fold_addr_expr (fn));
9306 else
9308 if (!integer_zerop (delta) || vcall_index)
9310 fn = make_thunk (fn, /*this_adjusting=*/1,
9311 delta, vcall_index);
9312 if (!DECL_NAME (fn))
9313 finish_thunk (fn);
9315 /* Take the address of the function, considering it to be of an
9316 appropriate generic type. */
9317 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9318 init = fold_convert (vfunc_ptr_type_node,
9319 build_fold_addr_expr (fn));
9320 /* Don't refer to a virtual destructor from a constructor
9321 vtable or a vtable for an abstract class, since destroying
9322 an object under construction is undefined behavior and we
9323 don't want it to be considered a candidate for speculative
9324 devirtualization. But do create the thunk for ABI
9325 compliance. */
9326 if (DECL_DESTRUCTOR_P (fn_original)
9327 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9328 || orig_binfo != binfo))
9329 init = size_zero_node;
9333 /* And add it to the chain of initializers. */
9334 if (TARGET_VTABLE_USES_DESCRIPTORS)
9336 int i;
9337 if (init == size_zero_node)
9338 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9339 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9340 else
9341 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9343 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9344 fn, build_int_cst (NULL_TREE, i));
9345 TREE_CONSTANT (fdesc) = 1;
9347 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9350 else
9351 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9355 /* Adds to vid->inits the initializers for the vbase and vcall
9356 offsets in BINFO, which is in the hierarchy dominated by T. */
9358 static void
9359 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9361 tree b;
9363 /* If this is a derived class, we must first create entries
9364 corresponding to the primary base class. */
9365 b = get_primary_binfo (binfo);
9366 if (b)
9367 build_vcall_and_vbase_vtbl_entries (b, vid);
9369 /* Add the vbase entries for this base. */
9370 build_vbase_offset_vtbl_entries (binfo, vid);
9371 /* Add the vcall entries for this base. */
9372 build_vcall_offset_vtbl_entries (binfo, vid);
9375 /* Returns the initializers for the vbase offset entries in the vtable
9376 for BINFO (which is part of the class hierarchy dominated by T), in
9377 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9378 where the next vbase offset will go. */
9380 static void
9381 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9383 tree vbase;
9384 tree t;
9385 tree non_primary_binfo;
9387 /* If there are no virtual baseclasses, then there is nothing to
9388 do. */
9389 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9390 return;
9392 t = vid->derived;
9394 /* We might be a primary base class. Go up the inheritance hierarchy
9395 until we find the most derived class of which we are a primary base:
9396 it is the offset of that which we need to use. */
9397 non_primary_binfo = binfo;
9398 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9400 tree b;
9402 /* If we have reached a virtual base, then it must be a primary
9403 base (possibly multi-level) of vid->binfo, or we wouldn't
9404 have called build_vcall_and_vbase_vtbl_entries for it. But it
9405 might be a lost primary, so just skip down to vid->binfo. */
9406 if (BINFO_VIRTUAL_P (non_primary_binfo))
9408 non_primary_binfo = vid->binfo;
9409 break;
9412 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9413 if (get_primary_binfo (b) != non_primary_binfo)
9414 break;
9415 non_primary_binfo = b;
9418 /* Go through the virtual bases, adding the offsets. */
9419 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9420 vbase;
9421 vbase = TREE_CHAIN (vbase))
9423 tree b;
9424 tree delta;
9426 if (!BINFO_VIRTUAL_P (vbase))
9427 continue;
9429 /* Find the instance of this virtual base in the complete
9430 object. */
9431 b = copied_binfo (vbase, binfo);
9433 /* If we've already got an offset for this virtual base, we
9434 don't need another one. */
9435 if (BINFO_VTABLE_PATH_MARKED (b))
9436 continue;
9437 BINFO_VTABLE_PATH_MARKED (b) = 1;
9439 /* Figure out where we can find this vbase offset. */
9440 delta = size_binop (MULT_EXPR,
9441 vid->index,
9442 fold_convert (ssizetype,
9443 TYPE_SIZE_UNIT (vtable_entry_type)));
9444 if (vid->primary_vtbl_p)
9445 BINFO_VPTR_FIELD (b) = delta;
9447 if (binfo != TYPE_BINFO (t))
9448 /* The vbase offset had better be the same. */
9449 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9451 /* The next vbase will come at a more negative offset. */
9452 vid->index = size_binop (MINUS_EXPR, vid->index,
9453 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9455 /* The initializer is the delta from BINFO to this virtual base.
9456 The vbase offsets go in reverse inheritance-graph order, and
9457 we are walking in inheritance graph order so these end up in
9458 the right order. */
9459 delta = size_diffop_loc (input_location,
9460 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9462 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9463 fold_build1_loc (input_location, NOP_EXPR,
9464 vtable_entry_type, delta));
9468 /* Adds the initializers for the vcall offset entries in the vtable
9469 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9470 to VID->INITS. */
9472 static void
9473 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9475 /* We only need these entries if this base is a virtual base. We
9476 compute the indices -- but do not add to the vtable -- when
9477 building the main vtable for a class. */
9478 if (binfo == TYPE_BINFO (vid->derived)
9479 || (BINFO_VIRTUAL_P (binfo)
9480 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9481 correspond to VID->DERIVED), we are building a primary
9482 construction virtual table. Since this is a primary
9483 virtual table, we do not need the vcall offsets for
9484 BINFO. */
9485 && binfo != vid->rtti_binfo))
9487 /* We need a vcall offset for each of the virtual functions in this
9488 vtable. For example:
9490 class A { virtual void f (); };
9491 class B1 : virtual public A { virtual void f (); };
9492 class B2 : virtual public A { virtual void f (); };
9493 class C: public B1, public B2 { virtual void f (); };
9495 A C object has a primary base of B1, which has a primary base of A. A
9496 C also has a secondary base of B2, which no longer has a primary base
9497 of A. So the B2-in-C construction vtable needs a secondary vtable for
9498 A, which will adjust the A* to a B2* to call f. We have no way of
9499 knowing what (or even whether) this offset will be when we define B2,
9500 so we store this "vcall offset" in the A sub-vtable and look it up in
9501 a "virtual thunk" for B2::f.
9503 We need entries for all the functions in our primary vtable and
9504 in our non-virtual bases' secondary vtables. */
9505 vid->vbase = binfo;
9506 /* If we are just computing the vcall indices -- but do not need
9507 the actual entries -- not that. */
9508 if (!BINFO_VIRTUAL_P (binfo))
9509 vid->generate_vcall_entries = false;
9510 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9511 add_vcall_offset_vtbl_entries_r (binfo, vid);
9515 /* Build vcall offsets, starting with those for BINFO. */
9517 static void
9518 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9520 int i;
9521 tree primary_binfo;
9522 tree base_binfo;
9524 /* Don't walk into virtual bases -- except, of course, for the
9525 virtual base for which we are building vcall offsets. Any
9526 primary virtual base will have already had its offsets generated
9527 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9528 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9529 return;
9531 /* If BINFO has a primary base, process it first. */
9532 primary_binfo = get_primary_binfo (binfo);
9533 if (primary_binfo)
9534 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9536 /* Add BINFO itself to the list. */
9537 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9539 /* Scan the non-primary bases of BINFO. */
9540 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9541 if (base_binfo != primary_binfo)
9542 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9545 /* Called from build_vcall_offset_vtbl_entries_r. */
9547 static void
9548 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9550 /* Make entries for the rest of the virtuals. */
9551 tree orig_fn;
9553 /* The ABI requires that the methods be processed in declaration
9554 order. */
9555 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9556 orig_fn;
9557 orig_fn = DECL_CHAIN (orig_fn))
9558 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9559 add_vcall_offset (orig_fn, binfo, vid);
9562 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9564 static void
9565 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9567 size_t i;
9568 tree vcall_offset;
9569 tree derived_entry;
9571 /* If there is already an entry for a function with the same
9572 signature as FN, then we do not need a second vcall offset.
9573 Check the list of functions already present in the derived
9574 class vtable. */
9575 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9577 if (same_signature_p (derived_entry, orig_fn)
9578 /* We only use one vcall offset for virtual destructors,
9579 even though there are two virtual table entries. */
9580 || (DECL_DESTRUCTOR_P (derived_entry)
9581 && DECL_DESTRUCTOR_P (orig_fn)))
9582 return;
9585 /* If we are building these vcall offsets as part of building
9586 the vtable for the most derived class, remember the vcall
9587 offset. */
9588 if (vid->binfo == TYPE_BINFO (vid->derived))
9590 tree_pair_s elt = {orig_fn, vid->index};
9591 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9594 /* The next vcall offset will be found at a more negative
9595 offset. */
9596 vid->index = size_binop (MINUS_EXPR, vid->index,
9597 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9599 /* Keep track of this function. */
9600 vec_safe_push (vid->fns, orig_fn);
9602 if (vid->generate_vcall_entries)
9604 tree base;
9605 tree fn;
9607 /* Find the overriding function. */
9608 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9609 if (fn == error_mark_node)
9610 vcall_offset = build_zero_cst (vtable_entry_type);
9611 else
9613 base = TREE_VALUE (fn);
9615 /* The vbase we're working on is a primary base of
9616 vid->binfo. But it might be a lost primary, so its
9617 BINFO_OFFSET might be wrong, so we just use the
9618 BINFO_OFFSET from vid->binfo. */
9619 vcall_offset = size_diffop_loc (input_location,
9620 BINFO_OFFSET (base),
9621 BINFO_OFFSET (vid->binfo));
9622 vcall_offset = fold_build1_loc (input_location,
9623 NOP_EXPR, vtable_entry_type,
9624 vcall_offset);
9626 /* Add the initializer to the vtable. */
9627 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9631 /* Return vtbl initializers for the RTTI entries corresponding to the
9632 BINFO's vtable. The RTTI entries should indicate the object given
9633 by VID->rtti_binfo. */
9635 static void
9636 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9638 tree b;
9639 tree t;
9640 tree offset;
9641 tree decl;
9642 tree init;
9644 t = BINFO_TYPE (vid->rtti_binfo);
9646 /* To find the complete object, we will first convert to our most
9647 primary base, and then add the offset in the vtbl to that value. */
9648 b = most_primary_binfo (binfo);
9649 offset = size_diffop_loc (input_location,
9650 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9652 /* The second entry is the address of the typeinfo object. */
9653 if (flag_rtti)
9654 decl = build_address (get_tinfo_decl (t));
9655 else
9656 decl = integer_zero_node;
9658 /* Convert the declaration to a type that can be stored in the
9659 vtable. */
9660 init = build_nop (vfunc_ptr_type_node, decl);
9661 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9663 /* Add the offset-to-top entry. It comes earlier in the vtable than
9664 the typeinfo entry. Convert the offset to look like a
9665 function pointer, so that we can put it in the vtable. */
9666 init = build_nop (vfunc_ptr_type_node, offset);
9667 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9670 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9671 accessibility. */
9673 bool
9674 uniquely_derived_from_p (tree parent, tree type)
9676 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9677 return base && base != error_mark_node;
9680 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9682 bool
9683 publicly_uniquely_derived_p (tree parent, tree type)
9685 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9686 NULL, tf_none);
9687 return base && base != error_mark_node;
9690 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9691 class between them, if any. */
9693 tree
9694 common_enclosing_class (tree ctx1, tree ctx2)
9696 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9697 return NULL_TREE;
9698 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9699 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9700 if (ctx1 == ctx2)
9701 return ctx1;
9702 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9703 TYPE_MARKED_P (t) = true;
9704 tree found = NULL_TREE;
9705 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9706 if (TYPE_MARKED_P (t))
9708 found = t;
9709 break;
9711 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9712 TYPE_MARKED_P (t) = false;
9713 return found;
9716 #include "gt-cp-class.h"