PR c++/86728 - C variadic generic lambda.
[official-gcc.git] / gcc / cp / class.c
blob7b10b20ede5eac19ed85b22ac27e874b4a871b55
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987-2018 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 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the
219 'structor is in charge of 'structing virtual bases, or FALSE_STMT
220 otherwise. */
222 tree
223 build_if_in_charge (tree true_stmt, tree false_stmt)
225 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl));
226 tree cmp = build2 (NE_EXPR, boolean_type_node,
227 current_in_charge_parm, integer_zero_node);
228 tree type = unlowered_expr_type (true_stmt);
229 if (VOID_TYPE_P (type))
230 type = unlowered_expr_type (false_stmt);
231 tree cond = build3 (COND_EXPR, type,
232 cmp, true_stmt, false_stmt);
233 return cond;
236 /* Convert to or from a base subobject. EXPR is an expression of type
237 `A' or `A*', an expression of type `B' or `B*' is returned. To
238 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
239 the B base instance within A. To convert base A to derived B, CODE
240 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
241 In this latter case, A must not be a morally virtual base of B.
242 NONNULL is true if EXPR is known to be non-NULL (this is only
243 needed when EXPR is of pointer type). CV qualifiers are preserved
244 from EXPR. */
246 tree
247 build_base_path (enum tree_code code,
248 tree expr,
249 tree binfo,
250 int nonnull,
251 tsubst_flags_t complain)
253 tree v_binfo = NULL_TREE;
254 tree d_binfo = NULL_TREE;
255 tree probe;
256 tree offset;
257 tree target_type;
258 tree null_test = NULL;
259 tree ptr_target_type;
260 int fixed_type_p;
261 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr));
262 bool has_empty = false;
263 bool virtual_access;
264 bool rvalue = false;
266 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
267 return error_mark_node;
269 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
271 d_binfo = probe;
272 if (is_empty_class (BINFO_TYPE (probe)))
273 has_empty = true;
274 if (!v_binfo && BINFO_VIRTUAL_P (probe))
275 v_binfo = probe;
278 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
279 if (want_pointer)
280 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
281 if (dependent_type_p (probe))
282 if (tree open = currently_open_class (probe))
283 probe = open;
285 if (code == PLUS_EXPR
286 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))
288 /* This can happen when adjust_result_of_qualified_name_lookup can't
289 find a unique base binfo in a call to a member function. We
290 couldn't give the diagnostic then since we might have been calling
291 a static member function, so we do it now. In other cases, eg.
292 during error recovery (c++/71979), we may not have a base at all. */
293 if (complain & tf_error)
295 tree base = lookup_base (probe, BINFO_TYPE (d_binfo),
296 ba_unique, NULL, complain);
297 gcc_assert (base == error_mark_node || !base);
299 return error_mark_node;
302 gcc_assert ((code == MINUS_EXPR
303 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
304 || code == PLUS_EXPR);
306 if (binfo == d_binfo)
307 /* Nothing to do. */
308 return expr;
310 if (code == MINUS_EXPR && v_binfo)
312 if (complain & tf_error)
314 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo)))
316 if (want_pointer)
317 error ("cannot convert from pointer to base class %qT to "
318 "pointer to derived class %qT because the base is "
319 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
320 else
321 error ("cannot convert from base class %qT to derived "
322 "class %qT because the base is virtual",
323 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo));
325 else
327 if (want_pointer)
328 error ("cannot convert from pointer to base class %qT to "
329 "pointer to derived class %qT via virtual base %qT",
330 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo),
331 BINFO_TYPE (v_binfo));
332 else
333 error ("cannot convert from base class %qT to derived "
334 "class %qT via virtual base %qT", BINFO_TYPE (binfo),
335 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
338 return error_mark_node;
341 if (!want_pointer)
343 rvalue = !lvalue_p (expr);
344 /* This must happen before the call to save_expr. */
345 expr = cp_build_addr_expr (expr, complain);
347 else
348 expr = mark_rvalue_use (expr);
350 offset = BINFO_OFFSET (binfo);
351 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
352 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
353 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always
354 cv-unqualified. Extract the cv-qualifiers from EXPR so that the
355 expression returned matches the input. */
356 target_type = cp_build_qualified_type
357 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
358 ptr_target_type = build_pointer_type (target_type);
360 /* Do we need to look in the vtable for the real offset? */
361 virtual_access = (v_binfo && fixed_type_p <= 0);
363 /* Don't bother with the calculations inside sizeof; they'll ICE if the
364 source type is incomplete and the pointer value doesn't matter. In a
365 template (even in instantiate_non_dependent_expr), we don't have vtables
366 set up properly yet, and the value doesn't matter there either; we're
367 just interested in the result of overload resolution. */
368 if (cp_unevaluated_operand != 0
369 || processing_template_decl
370 || in_template_function ())
372 expr = build_nop (ptr_target_type, expr);
373 goto indout;
376 if (!COMPLETE_TYPE_P (probe))
378 if (complain & tf_error)
379 error ("cannot convert from %qT to base class %qT because %qT is "
380 "incomplete", BINFO_TYPE (d_binfo), BINFO_TYPE (binfo),
381 BINFO_TYPE (d_binfo));
382 return error_mark_node;
385 /* If we're in an NSDMI, we don't have the full constructor context yet
386 that we need for converting to a virtual base, so just build a stub
387 CONVERT_EXPR and expand it later in bot_replace. */
388 if (virtual_access && fixed_type_p < 0
389 && current_scope () != current_function_decl)
391 expr = build1 (CONVERT_EXPR, ptr_target_type, expr);
392 CONVERT_EXPR_VBASE_PATH (expr) = true;
393 goto indout;
396 /* Do we need to check for a null pointer? */
397 if (want_pointer && !nonnull)
399 /* If we know the conversion will not actually change the value
400 of EXPR, then we can avoid testing the expression for NULL.
401 We have to avoid generating a COMPONENT_REF for a base class
402 field, because other parts of the compiler know that such
403 expressions are always non-NULL. */
404 if (!virtual_access && integer_zerop (offset))
405 return build_nop (ptr_target_type, expr);
406 null_test = error_mark_node;
409 /* Protect against multiple evaluation if necessary. */
410 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
411 expr = save_expr (expr);
413 /* Now that we've saved expr, build the real null test. */
414 if (null_test)
416 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain);
417 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node,
418 expr, zero);
419 /* This is a compiler generated comparison, don't emit
420 e.g. -Wnonnull-compare warning for it. */
421 TREE_NO_WARNING (null_test) = 1;
424 /* If this is a simple base reference, express it as a COMPONENT_REF. */
425 if (code == PLUS_EXPR && !virtual_access
426 /* We don't build base fields for empty bases, and they aren't very
427 interesting to the optimizers anyway. */
428 && !has_empty)
430 expr = cp_build_fold_indirect_ref (expr);
431 expr = build_simple_base_path (expr, binfo);
432 if (rvalue && lvalue_p (expr))
433 expr = move (expr);
434 if (want_pointer)
435 expr = build_address (expr);
436 target_type = TREE_TYPE (expr);
437 goto out;
440 if (virtual_access)
442 /* Going via virtual base V_BINFO. We need the static offset
443 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
444 V_BINFO. That offset is an entry in D_BINFO's vtable. */
445 tree v_offset;
447 if (fixed_type_p < 0 && in_base_initializer)
449 /* In a base member initializer, we cannot rely on the
450 vtable being set up. We have to indirect via the
451 vtt_parm. */
452 tree t;
454 t = TREE_TYPE (TYPE_VFIELD (current_class_type));
455 t = build_pointer_type (t);
456 v_offset = fold_convert (t, current_vtt_parm);
457 v_offset = cp_build_fold_indirect_ref (v_offset);
459 else
461 tree t = expr;
462 if (sanitize_flags_p (SANITIZE_VPTR)
463 && fixed_type_p == 0)
465 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location,
466 probe, expr);
467 if (t == NULL_TREE)
468 t = expr;
470 v_offset = build_vfield_ref (cp_build_fold_indirect_ref (t),
471 TREE_TYPE (TREE_TYPE (expr)));
474 if (v_offset == error_mark_node)
475 return error_mark_node;
477 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo));
478 v_offset = build1 (NOP_EXPR,
479 build_pointer_type (ptrdiff_type_node),
480 v_offset);
481 v_offset = cp_build_fold_indirect_ref (v_offset);
482 TREE_CONSTANT (v_offset) = 1;
484 offset = convert_to_integer (ptrdiff_type_node,
485 size_diffop_loc (input_location, offset,
486 BINFO_OFFSET (v_binfo)));
488 if (!integer_zerop (offset))
489 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset);
491 if (fixed_type_p < 0)
492 /* Negative fixed_type_p means this is a constructor or destructor;
493 virtual base layout is fixed in in-charge [cd]tors, but not in
494 base [cd]tors. */
495 offset = build_if_in_charge
496 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)),
497 v_offset);
498 else
499 offset = v_offset;
502 if (want_pointer)
503 target_type = ptr_target_type;
505 expr = build1 (NOP_EXPR, ptr_target_type, expr);
507 if (!integer_zerop (offset))
509 offset = fold_convert (sizetype, offset);
510 if (code == MINUS_EXPR)
511 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset);
512 expr = fold_build_pointer_plus (expr, offset);
514 else
515 null_test = NULL;
517 indout:
518 if (!want_pointer)
520 expr = cp_build_fold_indirect_ref (expr);
521 if (rvalue)
522 expr = move (expr);
525 out:
526 if (null_test)
527 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr,
528 build_zero_cst (target_type));
530 return expr;
533 /* Subroutine of build_base_path; EXPR and BINFO are as in that function.
534 Perform a derived-to-base conversion by recursively building up a
535 sequence of COMPONENT_REFs to the appropriate base fields. */
537 static tree
538 build_simple_base_path (tree expr, tree binfo)
540 tree type = BINFO_TYPE (binfo);
541 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
542 tree field;
544 if (d_binfo == NULL_TREE)
546 tree temp;
548 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
550 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
551 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only
552 an lvalue in the front end; only _DECLs and _REFs are lvalues
553 in the back end. */
554 temp = unary_complex_lvalue (ADDR_EXPR, expr);
555 if (temp)
556 expr = cp_build_fold_indirect_ref (temp);
558 return expr;
561 /* Recurse. */
562 expr = build_simple_base_path (expr, d_binfo);
564 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
565 field; field = DECL_CHAIN (field))
566 /* Is this the base field created by build_base_field? */
567 if (TREE_CODE (field) == FIELD_DECL
568 && DECL_FIELD_IS_BASE (field)
569 && TREE_TYPE (field) == type
570 /* If we're looking for a field in the most-derived class,
571 also check the field offset; we can have two base fields
572 of the same type if one is an indirect virtual base and one
573 is a direct non-virtual base. */
574 && (BINFO_INHERITANCE_CHAIN (d_binfo)
575 || tree_int_cst_equal (byte_position (field),
576 BINFO_OFFSET (binfo))))
578 /* We don't use build_class_member_access_expr here, as that
579 has unnecessary checks, and more importantly results in
580 recursive calls to dfs_walk_once. */
581 int type_quals = cp_type_quals (TREE_TYPE (expr));
583 expr = build3 (COMPONENT_REF,
584 cp_build_qualified_type (type, type_quals),
585 expr, field, NULL_TREE);
586 /* Mark the expression const or volatile, as appropriate.
587 Even though we've dealt with the type above, we still have
588 to mark the expression itself. */
589 if (type_quals & TYPE_QUAL_CONST)
590 TREE_READONLY (expr) = 1;
591 if (type_quals & TYPE_QUAL_VOLATILE)
592 TREE_THIS_VOLATILE (expr) = 1;
594 return expr;
597 /* Didn't find the base field?!? */
598 gcc_unreachable ();
601 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose
602 type is a class type or a pointer to a class type. In the former
603 case, TYPE is also a class type; in the latter it is another
604 pointer type. If CHECK_ACCESS is true, an error message is emitted
605 if TYPE is inaccessible. If OBJECT has pointer type, the value is
606 assumed to be non-NULL. */
608 tree
609 convert_to_base (tree object, tree type, bool check_access, bool nonnull,
610 tsubst_flags_t complain)
612 tree binfo;
613 tree object_type;
615 if (TYPE_PTR_P (TREE_TYPE (object)))
617 object_type = TREE_TYPE (TREE_TYPE (object));
618 type = TREE_TYPE (type);
620 else
621 object_type = TREE_TYPE (object);
623 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique,
624 NULL, complain);
625 if (!binfo || binfo == error_mark_node)
626 return error_mark_node;
628 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain);
631 /* EXPR is an expression with unqualified class type. BASE is a base
632 binfo of that class type. Returns EXPR, converted to the BASE
633 type. This function assumes that EXPR is the most derived class;
634 therefore virtual bases can be found at their static offsets. */
636 tree
637 convert_to_base_statically (tree expr, tree base)
639 tree expr_type;
641 expr_type = TREE_TYPE (expr);
642 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
644 /* If this is a non-empty base, use a COMPONENT_REF. */
645 if (!is_empty_class (BINFO_TYPE (base)))
646 return build_simple_base_path (expr, base);
648 /* We use fold_build2 and fold_convert below to simplify the trees
649 provided to the optimizers. It is not safe to call these functions
650 when processing a template because they do not handle C++-specific
651 trees. */
652 gcc_assert (!processing_template_decl);
653 expr = cp_build_addr_expr (expr, tf_warning_or_error);
654 if (!integer_zerop (BINFO_OFFSET (base)))
655 expr = fold_build_pointer_plus_loc (input_location,
656 expr, BINFO_OFFSET (base));
657 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
658 expr = build_fold_indirect_ref_loc (input_location, expr);
661 return expr;
665 tree
666 build_vfield_ref (tree datum, tree type)
668 tree vfield, vcontext;
670 if (datum == error_mark_node
671 /* Can happen in case of duplicate base types (c++/59082). */
672 || !TYPE_VFIELD (type))
673 return error_mark_node;
675 /* First, convert to the requested type. */
676 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
677 datum = convert_to_base (datum, type, /*check_access=*/false,
678 /*nonnull=*/true, tf_warning_or_error);
680 /* Second, the requested type may not be the owner of its own vptr.
681 If not, convert to the base class that owns it. We cannot use
682 convert_to_base here, because VCONTEXT may appear more than once
683 in the inheritance hierarchy of TYPE, and thus direct conversion
684 between the types may be ambiguous. Following the path back up
685 one step at a time via primary bases avoids the problem. */
686 vfield = TYPE_VFIELD (type);
687 vcontext = DECL_CONTEXT (vfield);
688 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
690 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
691 type = TREE_TYPE (datum);
694 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
697 /* Given an object INSTANCE, return an expression which yields the
698 vtable element corresponding to INDEX. There are many special
699 cases for INSTANCE which we take care of here, mainly to avoid
700 creating extra tree nodes when we don't have to. */
702 static tree
703 build_vtbl_ref_1 (tree instance, tree idx)
705 tree aref;
706 tree vtbl = NULL_TREE;
708 /* Try to figure out what a reference refers to, and
709 access its virtual function table directly. */
711 int cdtorp = 0;
712 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
714 tree basetype = non_reference (TREE_TYPE (instance));
716 if (fixed_type && !cdtorp)
718 tree binfo = lookup_base (fixed_type, basetype,
719 ba_unique, NULL, tf_none);
720 if (binfo && binfo != error_mark_node)
721 vtbl = unshare_expr (BINFO_VTABLE (binfo));
724 if (!vtbl)
725 vtbl = build_vfield_ref (instance, basetype);
727 aref = build_array_ref (input_location, vtbl, idx);
728 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
730 return aref;
733 tree
734 build_vtbl_ref (tree instance, tree idx)
736 tree aref = build_vtbl_ref_1 (instance, idx);
738 return aref;
741 /* Given a stable object pointer INSTANCE_PTR, return an expression which
742 yields a function pointer corresponding to vtable element INDEX. */
744 tree
745 build_vfn_ref (tree instance_ptr, tree idx)
747 tree aref;
749 aref = build_vtbl_ref_1 (cp_build_fold_indirect_ref (instance_ptr),
750 idx);
752 /* When using function descriptors, the address of the
753 vtable entry is treated as a function pointer. */
754 if (TARGET_VTABLE_USES_DESCRIPTORS)
755 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
756 cp_build_addr_expr (aref, tf_warning_or_error));
758 /* Remember this as a method reference, for later devirtualization. */
759 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
761 return aref;
764 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
765 for the given TYPE. */
767 static tree
768 get_vtable_name (tree type)
770 return mangle_vtbl_for_type (type);
773 /* DECL is an entity associated with TYPE, like a virtual table or an
774 implicitly generated constructor. Determine whether or not DECL
775 should have external or internal linkage at the object file
776 level. This routine does not deal with COMDAT linkage and other
777 similar complexities; it simply sets TREE_PUBLIC if it possible for
778 entities in other translation units to contain copies of DECL, in
779 the abstract. */
781 void
782 set_linkage_according_to_type (tree /*type*/, tree decl)
784 TREE_PUBLIC (decl) = 1;
785 determine_visibility (decl);
788 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
789 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
790 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
792 static tree
793 build_vtable (tree class_type, tree name, tree vtable_type)
795 tree decl;
797 decl = build_lang_decl (VAR_DECL, name, vtable_type);
798 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
799 now to avoid confusion in mangle_decl. */
800 SET_DECL_ASSEMBLER_NAME (decl, name);
801 DECL_CONTEXT (decl) = class_type;
802 DECL_ARTIFICIAL (decl) = 1;
803 TREE_STATIC (decl) = 1;
804 TREE_READONLY (decl) = 1;
805 DECL_VIRTUAL_P (decl) = 1;
806 SET_DECL_ALIGN (decl, TARGET_VTABLE_ENTRY_ALIGN);
807 DECL_USER_ALIGN (decl) = true;
808 DECL_VTABLE_OR_VTT_P (decl) = 1;
809 set_linkage_according_to_type (class_type, decl);
810 /* The vtable has not been defined -- yet. */
811 DECL_EXTERNAL (decl) = 1;
812 DECL_NOT_REALLY_EXTERN (decl) = 1;
814 /* Mark the VAR_DECL node representing the vtable itself as a
815 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It
816 is rather important that such things be ignored because any
817 effort to actually generate DWARF for them will run into
818 trouble when/if we encounter code like:
820 #pragma interface
821 struct S { virtual void member (); };
823 because the artificial declaration of the vtable itself (as
824 manufactured by the g++ front end) will say that the vtable is
825 a static member of `S' but only *after* the debug output for
826 the definition of `S' has already been output. This causes
827 grief because the DWARF entry for the definition of the vtable
828 will try to refer back to an earlier *declaration* of the
829 vtable as a static member of `S' and there won't be one. We
830 might be able to arrange to have the "vtable static member"
831 attached to the member list for `S' before the debug info for
832 `S' get written (which would solve the problem) but that would
833 require more intrusive changes to the g++ front end. */
834 DECL_IGNORED_P (decl) = 1;
836 return decl;
839 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
840 or even complete. If this does not exist, create it. If COMPLETE is
841 nonzero, then complete the definition of it -- that will render it
842 impossible to actually build the vtable, but is useful to get at those
843 which are known to exist in the runtime. */
845 tree
846 get_vtable_decl (tree type, int complete)
848 tree decl;
850 if (CLASSTYPE_VTABLES (type))
851 return CLASSTYPE_VTABLES (type);
853 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
854 CLASSTYPE_VTABLES (type) = decl;
856 if (complete)
858 DECL_EXTERNAL (decl) = 1;
859 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
862 return decl;
865 /* Build the primary virtual function table for TYPE. If BINFO is
866 non-NULL, build the vtable starting with the initial approximation
867 that it is the same as the one which is the head of the association
868 list. Returns a nonzero value if a new vtable is actually
869 created. */
871 static int
872 build_primary_vtable (tree binfo, tree type)
874 tree decl;
875 tree virtuals;
877 decl = get_vtable_decl (type, /*complete=*/0);
879 if (binfo)
881 if (BINFO_NEW_VTABLE_MARKED (binfo))
882 /* We have already created a vtable for this base, so there's
883 no need to do it again. */
884 return 0;
886 virtuals = copy_list (BINFO_VIRTUALS (binfo));
887 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
888 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
889 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
891 else
893 gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
894 virtuals = NULL_TREE;
897 /* Initialize the association list for this type, based
898 on our first approximation. */
899 BINFO_VTABLE (TYPE_BINFO (type)) = decl;
900 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
901 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
902 return 1;
905 /* Give BINFO a new virtual function table which is initialized
906 with a skeleton-copy of its original initialization. The only
907 entry that changes is the `delta' entry, so we can really
908 share a lot of structure.
910 FOR_TYPE is the most derived type which caused this table to
911 be needed.
913 Returns nonzero if we haven't met BINFO before.
915 The order in which vtables are built (by calling this function) for
916 an object must remain the same, otherwise a binary incompatibility
917 can result. */
919 static int
920 build_secondary_vtable (tree binfo)
922 if (BINFO_NEW_VTABLE_MARKED (binfo))
923 /* We already created a vtable for this base. There's no need to
924 do it again. */
925 return 0;
927 /* Remember that we've created a vtable for this BINFO, so that we
928 don't try to do so again. */
929 SET_BINFO_NEW_VTABLE_MARKED (binfo);
931 /* Make fresh virtual list, so we can smash it later. */
932 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
934 /* Secondary vtables are laid out as part of the same structure as
935 the primary vtable. */
936 BINFO_VTABLE (binfo) = NULL_TREE;
937 return 1;
940 /* Create a new vtable for BINFO which is the hierarchy dominated by
941 T. Return nonzero if we actually created a new vtable. */
943 static int
944 make_new_vtable (tree t, tree binfo)
946 if (binfo == TYPE_BINFO (t))
947 /* In this case, it is *type*'s vtable we are modifying. We start
948 with the approximation that its vtable is that of the
949 immediate base class. */
950 return build_primary_vtable (binfo, t);
951 else
952 /* This is our very own copy of `basetype' to play with. Later,
953 we will fill in all the virtual functions that override the
954 virtual functions in these base classes which are not defined
955 by the current type. */
956 return build_secondary_vtable (binfo);
959 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
960 (which is in the hierarchy dominated by T) list FNDECL as its
961 BV_FN. DELTA is the required constant adjustment from the `this'
962 pointer where the vtable entry appears to the `this' required when
963 the function is actually called. */
965 static void
966 modify_vtable_entry (tree t,
967 tree binfo,
968 tree fndecl,
969 tree delta,
970 tree *virtuals)
972 tree v;
974 v = *virtuals;
976 if (fndecl != BV_FN (v)
977 || !tree_int_cst_equal (delta, BV_DELTA (v)))
979 /* We need a new vtable for BINFO. */
980 if (make_new_vtable (t, binfo))
982 /* If we really did make a new vtable, we also made a copy
983 of the BINFO_VIRTUALS list. Now, we have to find the
984 corresponding entry in that list. */
985 *virtuals = BINFO_VIRTUALS (binfo);
986 while (BV_FN (*virtuals) != BV_FN (v))
987 *virtuals = TREE_CHAIN (*virtuals);
988 v = *virtuals;
991 BV_DELTA (v) = delta;
992 BV_VCALL_INDEX (v) = NULL_TREE;
993 BV_FN (v) = fndecl;
998 /* Add method METHOD to class TYPE. If VIA_USING indicates whether
999 METHOD is being injected via a using_decl. Returns true if the
1000 method could be added to the method vec. */
1002 bool
1003 add_method (tree type, tree method, bool via_using)
1005 if (method == error_mark_node)
1006 return false;
1008 gcc_assert (!DECL_EXTERN_C_P (method));
1010 tree *slot = find_member_slot (type, DECL_NAME (method));
1011 tree current_fns = slot ? *slot : NULL_TREE;
1013 /* Check to see if we've already got this method. */
1014 for (ovl_iterator iter (current_fns); iter; ++iter)
1016 tree fn = *iter;
1017 tree fn_type;
1018 tree method_type;
1019 tree parms1;
1020 tree parms2;
1022 if (TREE_CODE (fn) != TREE_CODE (method))
1023 continue;
1025 /* Two using-declarations can coexist, we'll complain about ambiguity in
1026 overload resolution. */
1027 if (via_using && iter.using_p ()
1028 /* Except handle inherited constructors specially. */
1029 && ! DECL_CONSTRUCTOR_P (fn))
1030 continue;
1032 /* [over.load] Member function declarations with the
1033 same name and the same parameter types cannot be
1034 overloaded if any of them is a static member
1035 function declaration.
1037 [over.load] Member function declarations with the same name and
1038 the same parameter-type-list as well as member function template
1039 declarations with the same name, the same parameter-type-list, and
1040 the same template parameter lists cannot be overloaded if any of
1041 them, but not all, have a ref-qualifier.
1043 [namespace.udecl] When a using-declaration brings names
1044 from a base class into a derived class scope, member
1045 functions in the derived class override and/or hide member
1046 functions with the same name and parameter types in a base
1047 class (rather than conflicting). */
1048 fn_type = TREE_TYPE (fn);
1049 method_type = TREE_TYPE (method);
1050 parms1 = TYPE_ARG_TYPES (fn_type);
1051 parms2 = TYPE_ARG_TYPES (method_type);
1053 /* Compare the quals on the 'this' parm. Don't compare
1054 the whole types, as used functions are treated as
1055 coming from the using class in overload resolution. */
1056 if (! DECL_STATIC_FUNCTION_P (fn)
1057 && ! DECL_STATIC_FUNCTION_P (method)
1058 /* Either both or neither need to be ref-qualified for
1059 differing quals to allow overloading. */
1060 && (FUNCTION_REF_QUALIFIED (fn_type)
1061 == FUNCTION_REF_QUALIFIED (method_type))
1062 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type)
1063 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type)))
1064 continue;
1066 /* For templates, the return type and template parameters
1067 must be identical. */
1068 if (TREE_CODE (fn) == TEMPLATE_DECL
1069 && (!same_type_p (TREE_TYPE (fn_type),
1070 TREE_TYPE (method_type))
1071 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
1072 DECL_TEMPLATE_PARMS (method))))
1073 continue;
1075 if (! DECL_STATIC_FUNCTION_P (fn))
1076 parms1 = TREE_CHAIN (parms1);
1077 if (! DECL_STATIC_FUNCTION_P (method))
1078 parms2 = TREE_CHAIN (parms2);
1080 /* Bring back parameters omitted from an inherited ctor. */
1081 if (ctor_omit_inherited_parms (fn))
1082 parms1 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
1083 if (ctor_omit_inherited_parms (method))
1084 parms2 = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (method));
1086 if (compparms (parms1, parms2)
1087 && (!DECL_CONV_FN_P (fn)
1088 || same_type_p (TREE_TYPE (fn_type),
1089 TREE_TYPE (method_type)))
1090 && equivalently_constrained (fn, method))
1092 /* If these are versions of the same function, process and
1093 move on. */
1094 if (TREE_CODE (fn) == FUNCTION_DECL
1095 && maybe_version_functions (method, fn, true))
1096 continue;
1098 if (DECL_INHERITED_CTOR (method))
1100 if (DECL_INHERITED_CTOR (fn))
1102 tree basem = DECL_INHERITED_CTOR_BASE (method);
1103 tree basef = DECL_INHERITED_CTOR_BASE (fn);
1104 if (flag_new_inheriting_ctors)
1106 if (basem == basef)
1108 /* Inheriting the same constructor along different
1109 paths, combine them. */
1110 SET_DECL_INHERITED_CTOR
1111 (fn, ovl_make (DECL_INHERITED_CTOR (method),
1112 DECL_INHERITED_CTOR (fn)));
1113 /* And discard the new one. */
1114 return false;
1116 else
1117 /* Inherited ctors can coexist until overload
1118 resolution. */
1119 continue;
1121 error_at (DECL_SOURCE_LOCATION (method),
1122 "%q#D conflicts with version inherited from %qT",
1123 method, basef);
1124 inform (DECL_SOURCE_LOCATION (fn),
1125 "version inherited from %qT declared here",
1126 basef);
1128 /* Otherwise defer to the other function. */
1129 return false;
1132 if (via_using)
1133 /* Defer to the local function. */
1134 return false;
1135 else if (flag_new_inheriting_ctors
1136 && DECL_INHERITED_CTOR (fn))
1138 /* Remove the inherited constructor. */
1139 current_fns = iter.remove_node (current_fns);
1140 continue;
1142 else
1144 error_at (DECL_SOURCE_LOCATION (method),
1145 "%q#D cannot be overloaded with %q#D", method, fn);
1146 inform (DECL_SOURCE_LOCATION (fn),
1147 "previous declaration %q#D", fn);
1148 return false;
1153 /* A class should never have more than one destructor. */
1154 gcc_assert (!current_fns || !DECL_DESTRUCTOR_P (method));
1156 current_fns = ovl_insert (method, current_fns, via_using);
1158 if (!COMPLETE_TYPE_P (type) && !DECL_CONV_FN_P (method)
1159 && !push_class_level_binding (DECL_NAME (method), current_fns))
1160 return false;
1162 if (!slot)
1163 slot = add_member_slot (type, DECL_NAME (method));
1165 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */
1166 grok_special_member_properties (method);
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 (abi_tag_identifier, data.tags, DECL_ATTRIBUTES (t));
1526 mark_abi_tags (t, false);
1528 return data.tags;
1531 /* Check that DECL has all the ABI tags that are used in parts of its type
1532 that are not reflected in its mangled name. */
1534 void
1535 check_abi_tags (tree decl)
1537 if (VAR_P (decl))
1538 check_abi_tags (decl, TREE_TYPE (decl));
1539 else if (TREE_CODE (decl) == FUNCTION_DECL
1540 && !DECL_CONV_FN_P (decl)
1541 && !mangle_return_type_p (decl))
1542 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)));
1545 /* Return any ABI tags that are used in parts of the type of DECL
1546 that are not reflected in its mangled name. This function is only
1547 used in backward-compatible mangling for ABI <11. */
1549 tree
1550 missing_abi_tags (tree decl)
1552 if (VAR_P (decl))
1553 return check_abi_tags (decl, TREE_TYPE (decl), true);
1554 else if (TREE_CODE (decl) == FUNCTION_DECL
1555 /* Don't check DECL_CONV_FN_P here like we do in check_abi_tags, so
1556 that we can use this function for setting need_abi_warning
1557 regardless of the current flag_abi_version. */
1558 && !mangle_return_type_p (decl))
1559 return check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl)), true);
1560 else
1561 return NULL_TREE;
1564 void
1565 inherit_targ_abi_tags (tree t)
1567 if (!CLASS_TYPE_P (t)
1568 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE)
1569 return;
1571 mark_abi_tags (t, true);
1573 tree args = CLASSTYPE_TI_ARGS (t);
1574 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE };
1575 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
1577 tree level = TMPL_ARGS_LEVEL (args, i+1);
1578 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j)
1580 tree arg = TREE_VEC_ELT (level, j);
1581 data.subob = arg;
1582 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data);
1586 // If we found some tags on our template arguments, add them to our
1587 // abi_tag attribute.
1588 if (data.tags)
1590 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t));
1591 if (attr)
1592 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr));
1593 else
1594 TYPE_ATTRIBUTES (t)
1595 = tree_cons (abi_tag_identifier, data.tags, TYPE_ATTRIBUTES (t));
1598 mark_abi_tags (t, false);
1601 /* Return true, iff class T has a non-virtual destructor that is
1602 accessible from outside the class heirarchy (i.e. is public, or
1603 there's a suitable friend. */
1605 static bool
1606 accessible_nvdtor_p (tree t)
1608 tree dtor = CLASSTYPE_DESTRUCTOR (t);
1610 /* An implicitly declared destructor is always public. And,
1611 if it were virtual, we would have created it by now. */
1612 if (!dtor)
1613 return true;
1615 if (DECL_VINDEX (dtor))
1616 return false; /* Virtual */
1618 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
1619 return true; /* Public */
1621 if (CLASSTYPE_FRIEND_CLASSES (t)
1622 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1623 return true; /* Has friends */
1625 return false;
1628 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
1629 and NO_CONST_ASN_REF_P. Also set flag bits in T based on
1630 properties of the bases. */
1632 static void
1633 check_bases (tree t,
1634 int* cant_have_const_ctor_p,
1635 int* no_const_asn_ref_p)
1637 int i;
1638 bool seen_non_virtual_nearly_empty_base_p = 0;
1639 int seen_tm_mask = 0;
1640 tree base_binfo;
1641 tree binfo;
1642 tree field = NULL_TREE;
1644 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1645 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
1646 if (TREE_CODE (field) == FIELD_DECL)
1647 break;
1649 for (binfo = TYPE_BINFO (t), i = 0;
1650 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
1652 tree basetype = TREE_TYPE (base_binfo);
1654 gcc_assert (COMPLETE_TYPE_P (basetype));
1656 if (CLASSTYPE_FINAL (basetype))
1657 error ("cannot derive from %<final%> base %qT in derived type %qT",
1658 basetype, t);
1660 /* If any base class is non-literal, so is the derived class. */
1661 if (!CLASSTYPE_LITERAL_P (basetype))
1662 CLASSTYPE_LITERAL_P (t) = false;
1664 /* If the base class doesn't have copy constructors or
1665 assignment operators that take const references, then the
1666 derived class cannot have such a member automatically
1667 generated. */
1668 if (TYPE_HAS_COPY_CTOR (basetype)
1669 && ! TYPE_HAS_CONST_COPY_CTOR (basetype))
1670 *cant_have_const_ctor_p = 1;
1671 if (TYPE_HAS_COPY_ASSIGN (basetype)
1672 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype))
1673 *no_const_asn_ref_p = 1;
1675 if (BINFO_VIRTUAL_P (base_binfo))
1676 /* A virtual base does not effect nearly emptiness. */
1678 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1680 if (seen_non_virtual_nearly_empty_base_p)
1681 /* And if there is more than one nearly empty base, then the
1682 derived class is not nearly empty either. */
1683 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1684 else
1685 /* Remember we've seen one. */
1686 seen_non_virtual_nearly_empty_base_p = 1;
1688 else if (!is_empty_class (basetype))
1689 /* If the base class is not empty or nearly empty, then this
1690 class cannot be nearly empty. */
1691 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1693 /* A lot of properties from the bases also apply to the derived
1694 class. */
1695 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1696 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1697 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1698 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
1699 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype)
1700 || !TYPE_HAS_COPY_ASSIGN (basetype));
1701 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype)
1702 || !TYPE_HAS_COPY_CTOR (basetype));
1703 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
1704 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype);
1705 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype);
1706 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1707 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1708 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1709 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
1710 || TYPE_HAS_COMPLEX_DFLT (basetype));
1711 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT
1712 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
1713 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype));
1714 SET_CLASSTYPE_REF_FIELDS_NEED_INIT
1715 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t)
1716 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype));
1717 if (TYPE_HAS_MUTABLE_P (basetype))
1718 CLASSTYPE_HAS_MUTABLE (t) = 1;
1720 /* A standard-layout class is a class that:
1722 * has no non-standard-layout base classes, */
1723 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype);
1724 if (!CLASSTYPE_NON_STD_LAYOUT (t))
1726 tree basefield;
1727 /* ...has no base classes of the same type as the first non-static
1728 data member... */
1729 if (field && DECL_CONTEXT (field) == t
1730 && (same_type_ignoring_top_level_qualifiers_p
1731 (TREE_TYPE (field), basetype)))
1732 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1733 else
1734 /* ...either has no non-static data members in the most-derived
1735 class and at most one base class with non-static data
1736 members, or has no base classes with non-static data
1737 members */
1738 for (basefield = TYPE_FIELDS (basetype); basefield;
1739 basefield = DECL_CHAIN (basefield))
1740 if (TREE_CODE (basefield) == FIELD_DECL
1741 && !(DECL_FIELD_IS_BASE (basefield)
1742 && integer_zerop (DECL_SIZE (basefield))))
1744 if (field)
1745 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
1746 else
1747 field = basefield;
1748 break;
1752 /* Don't bother collecting tm attributes if transactional memory
1753 support is not enabled. */
1754 if (flag_tm)
1756 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype));
1757 if (tm_attr)
1758 seen_tm_mask |= tm_attr_to_mask (tm_attr);
1761 check_abi_tags (t, basetype);
1764 /* If one of the base classes had TM attributes, and the current class
1765 doesn't define its own, then the current class inherits one. */
1766 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t)))
1768 tree tm_attr = tm_mask_to_attr (least_bit_hwi (seen_tm_mask));
1769 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t));
1773 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for
1774 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those
1775 that have had a nearly-empty virtual primary base stolen by some
1776 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for
1777 T. */
1779 static void
1780 determine_primary_bases (tree t)
1782 unsigned i;
1783 tree primary = NULL_TREE;
1784 tree type_binfo = TYPE_BINFO (t);
1785 tree base_binfo;
1787 /* Determine the primary bases of our bases. */
1788 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1789 base_binfo = TREE_CHAIN (base_binfo))
1791 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
1793 /* See if we're the non-virtual primary of our inheritance
1794 chain. */
1795 if (!BINFO_VIRTUAL_P (base_binfo))
1797 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
1798 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
1800 if (parent_primary
1801 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
1802 BINFO_TYPE (parent_primary)))
1803 /* We are the primary binfo. */
1804 BINFO_PRIMARY_P (base_binfo) = 1;
1806 /* Determine if we have a virtual primary base, and mark it so.
1808 if (primary && BINFO_VIRTUAL_P (primary))
1810 tree this_primary = copied_binfo (primary, base_binfo);
1812 if (BINFO_PRIMARY_P (this_primary))
1813 /* Someone already claimed this base. */
1814 BINFO_LOST_PRIMARY_P (base_binfo) = 1;
1815 else
1817 tree delta;
1819 BINFO_PRIMARY_P (this_primary) = 1;
1820 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
1822 /* A virtual binfo might have been copied from within
1823 another hierarchy. As we're about to use it as a
1824 primary base, make sure the offsets match. */
1825 delta = size_diffop_loc (input_location,
1826 fold_convert (ssizetype,
1827 BINFO_OFFSET (base_binfo)),
1828 fold_convert (ssizetype,
1829 BINFO_OFFSET (this_primary)));
1831 propagate_binfo_offsets (this_primary, delta);
1836 /* First look for a dynamic direct non-virtual base. */
1837 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
1839 tree basetype = BINFO_TYPE (base_binfo);
1841 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
1843 primary = base_binfo;
1844 goto found;
1848 /* A "nearly-empty" virtual base class can be the primary base
1849 class, if no non-virtual polymorphic base can be found. Look for
1850 a nearly-empty virtual dynamic base that is not already a primary
1851 base of something in the hierarchy. If there is no such base,
1852 just pick the first nearly-empty virtual base. */
1854 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
1855 base_binfo = TREE_CHAIN (base_binfo))
1856 if (BINFO_VIRTUAL_P (base_binfo)
1857 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
1859 if (!BINFO_PRIMARY_P (base_binfo))
1861 /* Found one that is not primary. */
1862 primary = base_binfo;
1863 goto found;
1865 else if (!primary)
1866 /* Remember the first candidate. */
1867 primary = base_binfo;
1870 found:
1871 /* If we've got a primary base, use it. */
1872 if (primary)
1874 tree basetype = BINFO_TYPE (primary);
1876 CLASSTYPE_PRIMARY_BINFO (t) = primary;
1877 if (BINFO_PRIMARY_P (primary))
1878 /* We are stealing a primary base. */
1879 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
1880 BINFO_PRIMARY_P (primary) = 1;
1881 if (BINFO_VIRTUAL_P (primary))
1883 tree delta;
1885 BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
1886 /* A virtual binfo might have been copied from within
1887 another hierarchy. As we're about to use it as a primary
1888 base, make sure the offsets match. */
1889 delta = size_diffop_loc (input_location, ssize_int (0),
1890 fold_convert (ssizetype, BINFO_OFFSET (primary)));
1892 propagate_binfo_offsets (primary, delta);
1895 primary = TYPE_BINFO (basetype);
1897 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1898 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
1899 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
1903 /* Update the variant types of T. */
1905 void
1906 fixup_type_variants (tree t)
1908 tree variants;
1910 if (!t)
1911 return;
1913 for (variants = TYPE_NEXT_VARIANT (t);
1914 variants;
1915 variants = TYPE_NEXT_VARIANT (variants))
1917 /* These fields are in the _TYPE part of the node, not in
1918 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1919 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
1920 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1921 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1922 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1924 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1926 TYPE_BINFO (variants) = TYPE_BINFO (t);
1928 /* Copy whatever these are holding today. */
1929 TYPE_VFIELD (variants) = TYPE_VFIELD (t);
1930 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1934 /* KLASS is a class that we're applying may_alias to after the body is
1935 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The
1936 canonical type(s) will be implicitly updated. */
1938 static void
1939 fixup_may_alias (tree klass)
1941 tree t, v;
1943 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t))
1944 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1945 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1946 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t))
1947 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1948 TYPE_REF_CAN_ALIAS_ALL (v) = true;
1951 /* Early variant fixups: we apply attributes at the beginning of the class
1952 definition, and we need to fix up any variants that have already been
1953 made via elaborated-type-specifier so that check_qualified_type works. */
1955 void
1956 fixup_attribute_variants (tree t)
1958 tree variants;
1960 if (!t)
1961 return;
1963 tree attrs = TYPE_ATTRIBUTES (t);
1964 unsigned align = TYPE_ALIGN (t);
1965 bool user_align = TYPE_USER_ALIGN (t);
1966 bool may_alias = lookup_attribute ("may_alias", attrs);
1968 if (may_alias)
1969 fixup_may_alias (t);
1971 for (variants = TYPE_NEXT_VARIANT (t);
1972 variants;
1973 variants = TYPE_NEXT_VARIANT (variants))
1975 /* These are the two fields that check_qualified_type looks at and
1976 are affected by attributes. */
1977 TYPE_ATTRIBUTES (variants) = attrs;
1978 unsigned valign = align;
1979 if (TYPE_USER_ALIGN (variants))
1980 valign = MAX (valign, TYPE_ALIGN (variants));
1981 else
1982 TYPE_USER_ALIGN (variants) = user_align;
1983 SET_TYPE_ALIGN (variants, valign);
1984 if (may_alias)
1985 fixup_may_alias (variants);
1989 /* Set memoizing fields and bits of T (and its variants) for later
1990 use. */
1992 static void
1993 finish_struct_bits (tree t)
1995 /* Fix up variants (if any). */
1996 fixup_type_variants (t);
1998 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
1999 /* For a class w/o baseclasses, 'finish_struct' has set
2000 CLASSTYPE_PURE_VIRTUALS correctly (by definition).
2001 Similarly for a class whose base classes do not have vtables.
2002 When neither of these is true, we might have removed abstract
2003 virtuals (by providing a definition), added some (by declaring
2004 new ones), or redeclared ones from a base class. We need to
2005 recalculate what's really an abstract virtual at this point (by
2006 looking in the vtables). */
2007 get_pure_virtuals (t);
2009 /* If this type has a copy constructor or a destructor, force its
2010 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be
2011 nonzero. This will cause it to be passed by invisible reference
2012 and prevent it from being returned in a register. */
2013 if (type_has_nontrivial_copy_init (t)
2014 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
2016 tree variants;
2017 SET_DECL_MODE (TYPE_MAIN_DECL (t), BLKmode);
2018 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2020 SET_TYPE_MODE (variants, BLKmode);
2021 TREE_ADDRESSABLE (variants) = 1;
2026 /* Issue warnings about T having private constructors, but no friends,
2027 and so forth.
2029 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2030 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2031 non-private static member functions. */
2033 static void
2034 maybe_warn_about_overly_private_class (tree t)
2036 int has_member_fn = 0;
2037 int has_nonprivate_method = 0;
2038 bool nonprivate_ctor = false;
2040 if (!warn_ctor_dtor_privacy
2041 /* If the class has friends, those entities might create and
2042 access instances, so we should not warn. */
2043 || (CLASSTYPE_FRIEND_CLASSES (t)
2044 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2045 /* We will have warned when the template was declared; there's
2046 no need to warn on every instantiation. */
2047 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2048 /* There's no reason to even consider warning about this
2049 class. */
2050 return;
2052 /* We only issue one warning, if more than one applies, because
2053 otherwise, on code like:
2055 class A {
2056 // Oops - forgot `public:'
2057 A();
2058 A(const A&);
2059 ~A();
2062 we warn several times about essentially the same problem. */
2064 /* Check to see if all (non-constructor, non-destructor) member
2065 functions are private. (Since there are no friends or
2066 non-private statics, we can't ever call any of the private member
2067 functions.) */
2068 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2069 if (TREE_CODE (fn) == USING_DECL
2070 && DECL_NAME (fn) == ctor_identifier
2071 && !TREE_PRIVATE (fn))
2072 nonprivate_ctor = true;
2073 else if (!DECL_DECLARES_FUNCTION_P (fn))
2074 /* Not a function. */;
2075 else if (DECL_ARTIFICIAL (fn))
2076 /* We're not interested in compiler-generated methods; they don't
2077 provide any way to call private members. */;
2078 else if (!TREE_PRIVATE (fn))
2080 if (DECL_STATIC_FUNCTION_P (fn))
2081 /* A non-private static member function is just like a
2082 friend; it can create and invoke private member
2083 functions, and be accessed without a class
2084 instance. */
2085 return;
2087 has_nonprivate_method = 1;
2088 /* Keep searching for a static member function. */
2090 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2091 has_member_fn = 1;
2093 if (!has_nonprivate_method && has_member_fn)
2095 /* There are no non-private methods, and there's at least one
2096 private member function that isn't a constructor or
2097 destructor. (If all the private members are
2098 constructors/destructors we want to use the code below that
2099 issues error messages specifically referring to
2100 constructors/destructors.) */
2101 unsigned i;
2102 tree binfo = TYPE_BINFO (t);
2104 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
2105 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
2107 has_nonprivate_method = 1;
2108 break;
2110 if (!has_nonprivate_method)
2112 warning (OPT_Wctor_dtor_privacy,
2113 "all member functions in class %qT are private", t);
2114 return;
2118 /* Even if some of the member functions are non-private, the class
2119 won't be useful for much if all the constructors or destructors
2120 are private: such an object can never be created or destroyed. */
2121 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
2122 if (TREE_PRIVATE (dtor))
2124 warning (OPT_Wctor_dtor_privacy,
2125 "%q#T only defines a private destructor and has no friends",
2127 return;
2130 /* Warn about classes that have private constructors and no friends. */
2131 if (TYPE_HAS_USER_CONSTRUCTOR (t)
2132 /* Implicitly generated constructors are always public. */
2133 && !CLASSTYPE_LAZY_DEFAULT_CTOR (t))
2135 tree copy_or_move = NULL_TREE;
2137 /* If a non-template class does not define a copy
2138 constructor, one is defined for it, enabling it to avoid
2139 this warning. For a template class, this does not
2140 happen, and so we would normally get a warning on:
2142 template <class T> class C { private: C(); };
2144 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All
2145 complete non-template or fully instantiated classes have this
2146 flag set. */
2147 if (!TYPE_HAS_COPY_CTOR (t))
2148 nonprivate_ctor = true;
2149 else
2150 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t));
2151 !nonprivate_ctor && iter; ++iter)
2152 if (TREE_PRIVATE (*iter))
2153 continue;
2154 else if (copy_fn_p (*iter) || move_fn_p (*iter))
2155 /* Ideally, we wouldn't count any constructor that takes
2156 an argument of the class type as a parameter, because
2157 such things cannot be used to construct an instance of
2158 the class unless you already have one. */
2159 copy_or_move = *iter;
2160 else
2161 nonprivate_ctor = true;
2163 if (!nonprivate_ctor)
2165 warning (OPT_Wctor_dtor_privacy,
2166 "%q#T only defines private constructors and has no friends",
2168 if (copy_or_move)
2169 inform (DECL_SOURCE_LOCATION (copy_or_move),
2170 "%q#D is public, but requires an existing %q#T object",
2171 copy_or_move, t);
2172 return;
2177 /* Make BINFO's vtable have N entries, including RTTI entries,
2178 vbase and vcall offsets, etc. Set its type and call the back end
2179 to lay it out. */
2181 static void
2182 layout_vtable_decl (tree binfo, int n)
2184 tree atype;
2185 tree vtable;
2187 atype = build_array_of_n_type (vtable_entry_type, n);
2188 layout_type (atype);
2190 /* We may have to grow the vtable. */
2191 vtable = get_vtbl_decl_for_binfo (binfo);
2192 if (!same_type_p (TREE_TYPE (vtable), atype))
2194 TREE_TYPE (vtable) = atype;
2195 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2196 layout_decl (vtable, 0);
2200 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2201 have the same signature. */
2204 same_signature_p (const_tree fndecl, const_tree base_fndecl)
2206 /* One destructor overrides another if they are the same kind of
2207 destructor. */
2208 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2209 && special_function_p (base_fndecl) == special_function_p (fndecl))
2210 return 1;
2211 /* But a non-destructor never overrides a destructor, nor vice
2212 versa, nor do different kinds of destructors override
2213 one-another. For example, a complete object destructor does not
2214 override a deleting destructor. */
2215 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2216 return 0;
2218 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
2219 || (DECL_CONV_FN_P (fndecl)
2220 && DECL_CONV_FN_P (base_fndecl)
2221 && same_type_p (DECL_CONV_FN_TYPE (fndecl),
2222 DECL_CONV_FN_TYPE (base_fndecl))))
2224 tree fntype = TREE_TYPE (fndecl);
2225 tree base_fntype = TREE_TYPE (base_fndecl);
2226 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype)
2227 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype)
2228 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl),
2229 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl)))
2230 return 1;
2232 return 0;
2235 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2236 subobject. */
2238 static bool
2239 base_derived_from (tree derived, tree base)
2241 tree probe;
2243 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2245 if (probe == derived)
2246 return true;
2247 else if (BINFO_VIRTUAL_P (probe))
2248 /* If we meet a virtual base, we can't follow the inheritance
2249 any more. See if the complete type of DERIVED contains
2250 such a virtual base. */
2251 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
2252 != NULL_TREE);
2254 return false;
2257 struct find_final_overrider_data {
2258 /* The function for which we are trying to find a final overrider. */
2259 tree fn;
2260 /* The base class in which the function was declared. */
2261 tree declaring_base;
2262 /* The candidate overriders. */
2263 tree candidates;
2264 /* Path to most derived. */
2265 vec<tree> path;
2268 /* Add the overrider along the current path to FFOD->CANDIDATES.
2269 Returns true if an overrider was found; false otherwise. */
2271 static bool
2272 dfs_find_final_overrider_1 (tree binfo,
2273 find_final_overrider_data *ffod,
2274 unsigned depth)
2276 tree method;
2278 /* If BINFO is not the most derived type, try a more derived class.
2279 A definition there will overrider a definition here. */
2280 if (depth)
2282 depth--;
2283 if (dfs_find_final_overrider_1
2284 (ffod->path[depth], ffod, depth))
2285 return true;
2288 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
2289 if (method)
2291 tree *candidate = &ffod->candidates;
2293 /* Remove any candidates overridden by this new function. */
2294 while (*candidate)
2296 /* If *CANDIDATE overrides METHOD, then METHOD
2297 cannot override anything else on the list. */
2298 if (base_derived_from (TREE_VALUE (*candidate), binfo))
2299 return true;
2300 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2301 if (base_derived_from (binfo, TREE_VALUE (*candidate)))
2302 *candidate = TREE_CHAIN (*candidate);
2303 else
2304 candidate = &TREE_CHAIN (*candidate);
2307 /* Add the new function. */
2308 ffod->candidates = tree_cons (method, binfo, ffod->candidates);
2309 return true;
2312 return false;
2315 /* Called from find_final_overrider via dfs_walk. */
2317 static tree
2318 dfs_find_final_overrider_pre (tree binfo, void *data)
2320 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2322 if (binfo == ffod->declaring_base)
2323 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ());
2324 ffod->path.safe_push (binfo);
2326 return NULL_TREE;
2329 static tree
2330 dfs_find_final_overrider_post (tree /*binfo*/, void *data)
2332 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2333 ffod->path.pop ();
2335 return NULL_TREE;
2338 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2339 FN and whose TREE_VALUE is the binfo for the base where the
2340 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2341 DERIVED) is the base object in which FN is declared. */
2343 static tree
2344 find_final_overrider (tree derived, tree binfo, tree fn)
2346 find_final_overrider_data ffod;
2348 /* Getting this right is a little tricky. This is valid:
2350 struct S { virtual void f (); };
2351 struct T { virtual void f (); };
2352 struct U : public S, public T { };
2354 even though calling `f' in `U' is ambiguous. But,
2356 struct R { virtual void f(); };
2357 struct S : virtual public R { virtual void f (); };
2358 struct T : virtual public R { virtual void f (); };
2359 struct U : public S, public T { };
2361 is not -- there's no way to decide whether to put `S::f' or
2362 `T::f' in the vtable for `R'.
2364 The solution is to look at all paths to BINFO. If we find
2365 different overriders along any two, then there is a problem. */
2366 if (DECL_THUNK_P (fn))
2367 fn = THUNK_TARGET (fn);
2369 /* Determine the depth of the hierarchy. */
2370 ffod.fn = fn;
2371 ffod.declaring_base = binfo;
2372 ffod.candidates = NULL_TREE;
2373 ffod.path.create (30);
2375 dfs_walk_all (derived, dfs_find_final_overrider_pre,
2376 dfs_find_final_overrider_post, &ffod);
2378 ffod.path.release ();
2380 /* If there was no winner, issue an error message. */
2381 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2382 return error_mark_node;
2384 return ffod.candidates;
2387 /* Return the index of the vcall offset for FN when TYPE is used as a
2388 virtual base. */
2390 static tree
2391 get_vcall_index (tree fn, tree type)
2393 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type);
2394 tree_pair_p p;
2395 unsigned ix;
2397 FOR_EACH_VEC_SAFE_ELT (indices, ix, p)
2398 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
2399 || same_signature_p (fn, p->purpose))
2400 return p->value;
2402 /* There should always be an appropriate index. */
2403 gcc_unreachable ();
2406 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2407 dominated by T. FN is the old function; VIRTUALS points to the
2408 corresponding position in the new BINFO_VIRTUALS list. IX is the index
2409 of that entry in the list. */
2411 static void
2412 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2413 unsigned ix)
2415 tree b;
2416 tree overrider;
2417 tree delta;
2418 tree virtual_base;
2419 tree first_defn;
2420 tree overrider_fn, overrider_target;
2421 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2422 tree over_return, base_return;
2423 bool lost = false;
2425 /* Find the nearest primary base (possibly binfo itself) which defines
2426 this function; this is the class the caller will convert to when
2427 calling FN through BINFO. */
2428 for (b = binfo; ; b = get_primary_binfo (b))
2430 gcc_assert (b);
2431 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2432 break;
2434 /* The nearest definition is from a lost primary. */
2435 if (BINFO_LOST_PRIMARY_P (b))
2436 lost = true;
2438 first_defn = b;
2440 /* Find the final overrider. */
2441 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2442 if (overrider == error_mark_node)
2444 error ("no unique final overrider for %qD in %qT", target_fn, t);
2445 return;
2447 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2449 /* Check for adjusting covariant return types. */
2450 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2451 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2453 if (INDIRECT_TYPE_P (over_return)
2454 && TREE_CODE (over_return) == TREE_CODE (base_return)
2455 && CLASS_TYPE_P (TREE_TYPE (over_return))
2456 && CLASS_TYPE_P (TREE_TYPE (base_return))
2457 /* If the overrider is invalid, don't even try. */
2458 && !DECL_INVALID_OVERRIDER_P (overrider_target))
2460 /* If FN is a covariant thunk, we must figure out the adjustment
2461 to the final base FN was converting to. As OVERRIDER_TARGET might
2462 also be converting to the return type of FN, we have to
2463 combine the two conversions here. */
2464 tree fixed_offset, virtual_offset;
2466 over_return = TREE_TYPE (over_return);
2467 base_return = TREE_TYPE (base_return);
2469 if (DECL_THUNK_P (fn))
2471 gcc_assert (DECL_RESULT_THUNK_P (fn));
2472 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2473 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2475 else
2476 fixed_offset = virtual_offset = NULL_TREE;
2478 if (virtual_offset)
2479 /* Find the equivalent binfo within the return type of the
2480 overriding function. We will want the vbase offset from
2481 there. */
2482 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2483 over_return);
2484 else if (!same_type_ignoring_top_level_qualifiers_p
2485 (over_return, base_return))
2487 /* There was no existing virtual thunk (which takes
2488 precedence). So find the binfo of the base function's
2489 return type within the overriding function's return type.
2490 Fortunately we know the covariancy is valid (it
2491 has already been checked), so we can just iterate along
2492 the binfos, which have been chained in inheritance graph
2493 order. Of course it is lame that we have to repeat the
2494 search here anyway -- we should really be caching pieces
2495 of the vtable and avoiding this repeated work. */
2496 tree thunk_binfo = NULL_TREE;
2497 tree base_binfo = TYPE_BINFO (base_return);
2499 /* Find the base binfo within the overriding function's
2500 return type. We will always find a thunk_binfo, except
2501 when the covariancy is invalid (which we will have
2502 already diagnosed). */
2503 if (base_binfo)
2504 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo;
2505 thunk_binfo = TREE_CHAIN (thunk_binfo))
2506 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
2507 BINFO_TYPE (base_binfo)))
2508 break;
2509 gcc_assert (thunk_binfo || errorcount);
2511 /* See if virtual inheritance is involved. */
2512 for (virtual_offset = thunk_binfo;
2513 virtual_offset;
2514 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
2515 if (BINFO_VIRTUAL_P (virtual_offset))
2516 break;
2518 if (virtual_offset
2519 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
2521 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo));
2523 if (virtual_offset)
2525 /* We convert via virtual base. Adjust the fixed
2526 offset to be from there. */
2527 offset =
2528 size_diffop (offset,
2529 fold_convert (ssizetype,
2530 BINFO_OFFSET (virtual_offset)));
2532 if (fixed_offset)
2533 /* There was an existing fixed offset, this must be
2534 from the base just converted to, and the base the
2535 FN was thunking to. */
2536 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2537 else
2538 fixed_offset = offset;
2542 if (fixed_offset || virtual_offset)
2543 /* Replace the overriding function with a covariant thunk. We
2544 will emit the overriding function in its own slot as
2545 well. */
2546 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2547 fixed_offset, virtual_offset);
2549 else
2550 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
2551 !DECL_THUNK_P (fn));
2553 /* If we need a covariant thunk, then we may need to adjust first_defn.
2554 The ABI specifies that the thunks emitted with a function are
2555 determined by which bases the function overrides, so we need to be
2556 sure that we're using a thunk for some overridden base; even if we
2557 know that the necessary this adjustment is zero, there may not be an
2558 appropriate zero-this-adjustment thunk for us to use since thunks for
2559 overriding virtual bases always use the vcall offset.
2561 Furthermore, just choosing any base that overrides this function isn't
2562 quite right, as this slot won't be used for calls through a type that
2563 puts a covariant thunk here. Calling the function through such a type
2564 will use a different slot, and that slot is the one that determines
2565 the thunk emitted for that base.
2567 So, keep looking until we find the base that we're really overriding
2568 in this slot: the nearest primary base that doesn't use a covariant
2569 thunk in this slot. */
2570 if (overrider_target != overrider_fn)
2572 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target))
2573 /* We already know that the overrider needs a covariant thunk. */
2574 b = get_primary_binfo (b);
2575 for (; ; b = get_primary_binfo (b))
2577 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b));
2578 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo));
2579 if (!DECL_THUNK_P (TREE_VALUE (bv)))
2580 break;
2581 if (BINFO_LOST_PRIMARY_P (b))
2582 lost = true;
2584 first_defn = b;
2587 /* Assume that we will produce a thunk that convert all the way to
2588 the final overrider, and not to an intermediate virtual base. */
2589 virtual_base = NULL_TREE;
2591 /* See if we can convert to an intermediate virtual base first, and then
2592 use the vcall offset located there to finish the conversion. */
2593 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2595 /* If we find the final overrider, then we can stop
2596 walking. */
2597 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b),
2598 BINFO_TYPE (TREE_VALUE (overrider))))
2599 break;
2601 /* If we find a virtual base, and we haven't yet found the
2602 overrider, then there is a virtual base between the
2603 declaring base (first_defn) and the final overrider. */
2604 if (BINFO_VIRTUAL_P (b))
2606 virtual_base = b;
2607 break;
2611 /* Compute the constant adjustment to the `this' pointer. The
2612 `this' pointer, when this function is called, will point at BINFO
2613 (or one of its primary bases, which are at the same offset). */
2614 if (virtual_base)
2615 /* The `this' pointer needs to be adjusted from the declaration to
2616 the nearest virtual base. */
2617 delta = size_diffop_loc (input_location,
2618 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)),
2619 fold_convert (ssizetype, BINFO_OFFSET (first_defn)));
2620 else if (lost)
2621 /* If the nearest definition is in a lost primary, we don't need an
2622 entry in our vtable. Except possibly in a constructor vtable,
2623 if we happen to get our primary back. In that case, the offset
2624 will be zero, as it will be a primary base. */
2625 delta = size_zero_node;
2626 else
2627 /* The `this' pointer needs to be adjusted from pointing to
2628 BINFO to pointing at the base where the final overrider
2629 appears. */
2630 delta = size_diffop_loc (input_location,
2631 fold_convert (ssizetype,
2632 BINFO_OFFSET (TREE_VALUE (overrider))),
2633 fold_convert (ssizetype, BINFO_OFFSET (binfo)));
2635 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2637 if (virtual_base)
2638 BV_VCALL_INDEX (*virtuals)
2639 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2640 else
2641 BV_VCALL_INDEX (*virtuals) = NULL_TREE;
2643 BV_LOST_PRIMARY (*virtuals) = lost;
2646 /* Called from modify_all_vtables via dfs_walk. */
2648 static tree
2649 dfs_modify_vtables (tree binfo, void* data)
2651 tree t = (tree) data;
2652 tree virtuals;
2653 tree old_virtuals;
2654 unsigned ix;
2656 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
2657 /* A base without a vtable needs no modification, and its bases
2658 are uninteresting. */
2659 return dfs_skip_bases;
2661 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
2662 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2663 /* Don't do the primary vtable, if it's new. */
2664 return NULL_TREE;
2666 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
2667 /* There's no need to modify the vtable for a non-virtual primary
2668 base; we're not going to use that vtable anyhow. We do still
2669 need to do this for virtual primary bases, as they could become
2670 non-primary in a construction vtable. */
2671 return NULL_TREE;
2673 make_new_vtable (t, binfo);
2675 /* Now, go through each of the virtual functions in the virtual
2676 function table for BINFO. Find the final overrider, and update
2677 the BINFO_VIRTUALS list appropriately. */
2678 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2679 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2680 virtuals;
2681 ix++, virtuals = TREE_CHAIN (virtuals),
2682 old_virtuals = TREE_CHAIN (old_virtuals))
2683 update_vtable_entry_for_fn (t,
2684 binfo,
2685 BV_FN (old_virtuals),
2686 &virtuals, ix);
2688 return NULL_TREE;
2691 /* Update all of the primary and secondary vtables for T. Create new
2692 vtables as required, and initialize their RTTI information. Each
2693 of the functions in VIRTUALS is declared in T and may override a
2694 virtual function from a base class; find and modify the appropriate
2695 entries to point to the overriding functions. Returns a list, in
2696 declaration order, of the virtual functions that are declared in T,
2697 but do not appear in the primary base class vtable, and which
2698 should therefore be appended to the end of the vtable for T. */
2700 static tree
2701 modify_all_vtables (tree t, tree virtuals)
2703 tree binfo = TYPE_BINFO (t);
2704 tree *fnsp;
2706 /* Mangle the vtable name before entering dfs_walk (c++/51884). */
2707 if (TYPE_CONTAINS_VPTR_P (t))
2708 get_vtable_decl (t, false);
2710 /* Update all of the vtables. */
2711 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
2713 /* Add virtual functions not already in our primary vtable. These
2714 will be both those introduced by this class, and those overridden
2715 from secondary bases. It does not include virtuals merely
2716 inherited from secondary bases. */
2717 for (fnsp = &virtuals; *fnsp; )
2719 tree fn = TREE_VALUE (*fnsp);
2721 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2722 || DECL_VINDEX (fn) == error_mark_node)
2724 /* We don't need to adjust the `this' pointer when
2725 calling this function. */
2726 BV_DELTA (*fnsp) = integer_zero_node;
2727 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2729 /* This is a function not already in our vtable. Keep it. */
2730 fnsp = &TREE_CHAIN (*fnsp);
2732 else
2733 /* We've already got an entry for this function. Skip it. */
2734 *fnsp = TREE_CHAIN (*fnsp);
2737 return virtuals;
2740 /* Get the base virtual function declarations in T that have the
2741 indicated NAME. */
2743 static void
2744 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls)
2746 bool found_decls = false;
2748 /* Find virtual functions in T with the indicated NAME. */
2749 for (ovl_iterator iter (get_class_binding (t, name)); iter; ++iter)
2751 tree method = *iter;
2753 if (TREE_CODE (method) == FUNCTION_DECL && DECL_VINDEX (method))
2755 base_fndecls->safe_push (method);
2756 found_decls = true;
2760 if (found_decls)
2761 return;
2763 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
2764 for (int i = 0; i < n_baseclasses; i++)
2766 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
2767 get_basefndecls (name, basetype, base_fndecls);
2771 /* If this declaration supersedes the declaration of
2772 a method declared virtual in the base class, then
2773 mark this field as being virtual as well. */
2775 void
2776 check_for_override (tree decl, tree ctype)
2778 bool overrides_found = false;
2779 if (TREE_CODE (decl) == TEMPLATE_DECL)
2780 /* In [temp.mem] we have:
2782 A specialization of a member function template does not
2783 override a virtual function from a base class. */
2784 return;
2785 if ((DECL_DESTRUCTOR_P (decl)
2786 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
2787 || DECL_CONV_FN_P (decl))
2788 && look_for_overrides (ctype, decl)
2789 && !DECL_STATIC_FUNCTION_P (decl))
2790 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2791 the error_mark_node so that we know it is an overriding
2792 function. */
2794 DECL_VINDEX (decl) = decl;
2795 overrides_found = true;
2796 if (warn_override && !DECL_OVERRIDE_P (decl)
2797 && !DECL_DESTRUCTOR_P (decl))
2798 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
2799 "%qD can be marked override", decl);
2802 if (DECL_VIRTUAL_P (decl))
2804 if (!DECL_VINDEX (decl))
2805 DECL_VINDEX (decl) = error_mark_node;
2806 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2807 if (DECL_DESTRUCTOR_P (decl))
2808 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
2810 else if (DECL_FINAL_P (decl))
2811 error ("%q+#D marked %<final%>, but is not virtual", decl);
2812 if (DECL_OVERRIDE_P (decl) && !overrides_found)
2813 error ("%q+#D marked %<override%>, but does not override", decl);
2816 /* Warn about hidden virtual functions that are not overridden in t.
2817 We know that constructors and destructors don't apply. */
2819 static void
2820 warn_hidden (tree t)
2822 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (t))
2823 for (unsigned ix = member_vec->length (); ix--;)
2825 tree fns = (*member_vec)[ix];
2827 if (!OVL_P (fns))
2828 continue;
2830 tree name = OVL_NAME (fns);
2831 auto_vec<tree, 20> base_fndecls;
2832 tree base_binfo;
2833 tree binfo;
2834 unsigned j;
2836 /* Iterate through all of the base classes looking for possibly
2837 hidden functions. */
2838 for (binfo = TYPE_BINFO (t), j = 0;
2839 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
2841 tree basetype = BINFO_TYPE (base_binfo);
2842 get_basefndecls (name, basetype, &base_fndecls);
2845 /* If there are no functions to hide, continue. */
2846 if (base_fndecls.is_empty ())
2847 continue;
2849 /* Remove any overridden functions. */
2850 for (ovl_iterator iter (fns); iter; ++iter)
2852 tree fndecl = *iter;
2853 if (TREE_CODE (fndecl) == FUNCTION_DECL
2854 && DECL_VINDEX (fndecl))
2856 /* If the method from the base class has the same
2857 signature as the method from the derived class, it
2858 has been overridden. */
2859 for (size_t k = 0; k < base_fndecls.length (); k++)
2860 if (base_fndecls[k]
2861 && same_signature_p (fndecl, base_fndecls[k]))
2862 base_fndecls[k] = NULL_TREE;
2866 /* Now give a warning for all base functions without overriders,
2867 as they are hidden. */
2868 tree base_fndecl;
2869 FOR_EACH_VEC_ELT (base_fndecls, j, base_fndecl)
2870 if (base_fndecl)
2872 /* Here we know it is a hider, and no overrider exists. */
2873 warning_at (location_of (base_fndecl),
2874 OPT_Woverloaded_virtual,
2875 "%qD was hidden", base_fndecl);
2876 warning_at (location_of (fns),
2877 OPT_Woverloaded_virtual, " by %qD", fns);
2882 /* Recursive helper for finish_struct_anon. */
2884 static void
2885 finish_struct_anon_r (tree field, bool complain)
2887 for (tree elt = TYPE_FIELDS (TREE_TYPE (field)); elt; elt = DECL_CHAIN (elt))
2889 /* We're generally only interested in entities the user
2890 declared, but we also find nested classes by noticing
2891 the TYPE_DECL that we create implicitly. You're
2892 allowed to put one anonymous union inside another,
2893 though, so we explicitly tolerate that. We use
2894 TYPE_UNNAMED_P rather than ANON_AGGR_TYPE_P so that
2895 we also allow unnamed types used for defining fields. */
2896 if (DECL_ARTIFICIAL (elt)
2897 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2898 || TYPE_UNNAMED_P (TREE_TYPE (elt))))
2899 continue;
2901 if (complain
2902 && (TREE_CODE (elt) != FIELD_DECL
2903 || (TREE_PRIVATE (elt) || TREE_PROTECTED (elt))))
2905 /* We already complained about static data members in
2906 finish_static_data_member_decl. */
2907 if (!VAR_P (elt)
2908 && permerror (DECL_SOURCE_LOCATION (elt),
2909 TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
2910 ? "%q#D invalid; an anonymous union may "
2911 "only have public non-static data members"
2912 : "%q#D invalid; an anonymous struct may "
2913 "only have public non-static data members", elt))
2915 static bool hint;
2916 if (flag_permissive && !hint)
2918 hint = true;
2919 inform (DECL_SOURCE_LOCATION (elt),
2920 "this flexibility is deprecated and will be removed");
2925 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2926 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2928 /* Recurse into the anonymous aggregates to correctly handle
2929 access control (c++/24926):
2931 class A {
2932 union {
2933 union {
2934 int i;
2939 int j=A().i; */
2940 if (DECL_NAME (elt) == NULL_TREE
2941 && ANON_AGGR_TYPE_P (TREE_TYPE (elt)))
2942 finish_struct_anon_r (elt, /*complain=*/false);
2946 /* Check for things that are invalid. There are probably plenty of other
2947 things we should check for also. */
2949 static void
2950 finish_struct_anon (tree t)
2952 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
2954 if (TREE_STATIC (field))
2955 continue;
2956 if (TREE_CODE (field) != FIELD_DECL)
2957 continue;
2959 if (DECL_NAME (field) == NULL_TREE
2960 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2961 finish_struct_anon_r (field, /*complain=*/true);
2965 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2966 will be used later during class template instantiation.
2967 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2968 a non-static member data (FIELD_DECL), a member function
2969 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2970 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2971 When FRIEND_P is nonzero, T is either a friend class
2972 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2973 (FUNCTION_DECL, TEMPLATE_DECL). */
2975 void
2976 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2978 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2979 if (CLASSTYPE_TEMPLATE_INFO (type))
2980 CLASSTYPE_DECL_LIST (type)
2981 = tree_cons (friend_p ? NULL_TREE : type,
2982 t, CLASSTYPE_DECL_LIST (type));
2985 /* This function is called from declare_virt_assop_and_dtor via
2986 dfs_walk_all.
2988 DATA is a type that direcly or indirectly inherits the base
2989 represented by BINFO. If BINFO contains a virtual assignment [copy
2990 assignment or move assigment] operator or a virtual constructor,
2991 declare that function in DATA if it hasn't been already declared. */
2993 static tree
2994 dfs_declare_virt_assop_and_dtor (tree binfo, void *data)
2996 tree bv, fn, t = (tree)data;
2997 tree opname = assign_op_identifier;
2999 gcc_assert (t && CLASS_TYPE_P (t));
3000 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO);
3002 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
3003 /* A base without a vtable needs no modification, and its bases
3004 are uninteresting. */
3005 return dfs_skip_bases;
3007 if (BINFO_PRIMARY_P (binfo))
3008 /* If this is a primary base, then we have already looked at the
3009 virtual functions of its vtable. */
3010 return NULL_TREE;
3012 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv))
3014 fn = BV_FN (bv);
3016 if (DECL_NAME (fn) == opname)
3018 if (CLASSTYPE_LAZY_COPY_ASSIGN (t))
3019 lazily_declare_fn (sfk_copy_assignment, t);
3020 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t))
3021 lazily_declare_fn (sfk_move_assignment, t);
3023 else if (DECL_DESTRUCTOR_P (fn)
3024 && CLASSTYPE_LAZY_DESTRUCTOR (t))
3025 lazily_declare_fn (sfk_destructor, t);
3028 return NULL_TREE;
3031 /* If the class type T has a direct or indirect base that contains a
3032 virtual assignment operator or a virtual destructor, declare that
3033 function in T if it hasn't been already declared. */
3035 static void
3036 declare_virt_assop_and_dtor (tree t)
3038 if (!(TYPE_POLYMORPHIC_P (t)
3039 && (CLASSTYPE_LAZY_COPY_ASSIGN (t)
3040 || CLASSTYPE_LAZY_MOVE_ASSIGN (t)
3041 || CLASSTYPE_LAZY_DESTRUCTOR (t))))
3042 return;
3044 dfs_walk_all (TYPE_BINFO (t),
3045 dfs_declare_virt_assop_and_dtor,
3046 NULL, t);
3049 /* Declare the inheriting constructor for class T inherited from base
3050 constructor CTOR with the parameter array PARMS of size NPARMS. */
3052 static void
3053 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms)
3055 gcc_assert (TYPE_MAIN_VARIANT (t) == t);
3057 /* We don't declare an inheriting ctor that would be a default,
3058 copy or move ctor for derived or base. */
3059 if (nparms == 0)
3060 return;
3061 if (nparms == 1
3062 && TYPE_REF_P (parms[0]))
3064 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0]));
3065 if (parm == t || parm == DECL_CONTEXT (ctor))
3066 return;
3069 tree parmlist = void_list_node;
3070 for (int i = nparms - 1; i >= 0; i--)
3071 parmlist = tree_cons (NULL_TREE, parms[i], parmlist);
3072 tree fn = implicitly_declare_fn (sfk_inheriting_constructor,
3073 t, false, ctor, parmlist);
3075 if (add_method (t, fn, false))
3077 DECL_CHAIN (fn) = TYPE_FIELDS (t);
3078 TYPE_FIELDS (t) = fn;
3082 /* Declare all the inheriting constructors for class T inherited from base
3083 constructor CTOR. */
3085 static void
3086 one_inherited_ctor (tree ctor, tree t, tree using_decl)
3088 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor);
3090 if (flag_new_inheriting_ctors)
3092 ctor = implicitly_declare_fn (sfk_inheriting_constructor,
3093 t, /*const*/false, ctor, parms);
3094 add_method (t, ctor, using_decl != NULL_TREE);
3095 TYPE_HAS_USER_CONSTRUCTOR (t) = true;
3096 return;
3099 tree *new_parms = XALLOCAVEC (tree, list_length (parms));
3100 int i = 0;
3101 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms))
3103 if (TREE_PURPOSE (parms))
3104 one_inheriting_sig (t, ctor, new_parms, i);
3105 new_parms[i++] = TREE_VALUE (parms);
3107 one_inheriting_sig (t, ctor, new_parms, i);
3108 if (parms == NULL_TREE)
3110 if (warning (OPT_Winherited_variadic_ctor,
3111 "the ellipsis in %qD is not inherited", ctor))
3112 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor);
3116 /* Create default constructors, assignment operators, and so forth for
3117 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR,
3118 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
3119 the class cannot have a default constructor, copy constructor
3120 taking a const reference argument, or an assignment operator taking
3121 a const reference, respectively. */
3123 static void
3124 add_implicitly_declared_members (tree t, tree* access_decls,
3125 int cant_have_const_cctor,
3126 int cant_have_const_assignment)
3128 /* Destructor. */
3129 if (!CLASSTYPE_DESTRUCTOR (t))
3130 /* In general, we create destructors lazily. */
3131 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
3133 bool move_ok = false;
3134 if (cxx_dialect >= cxx11 && CLASSTYPE_LAZY_DESTRUCTOR (t)
3135 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t)
3136 && !classtype_has_move_assign_or_move_ctor_p (t, false))
3137 move_ok = true;
3139 /* [class.ctor]
3141 If there is no user-declared constructor for a class, a default
3142 constructor is implicitly declared. */
3143 if (! TYPE_HAS_USER_CONSTRUCTOR (t))
3145 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
3146 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
3147 if (cxx_dialect >= cxx11)
3148 TYPE_HAS_CONSTEXPR_CTOR (t)
3149 /* Don't force the declaration to get a hard answer; if the
3150 definition would have made the class non-literal, it will still be
3151 non-literal because of the base or member in question, and that
3152 gives a better diagnostic. */
3153 = type_maybe_constexpr_default_constructor (t);
3156 /* [class.ctor]
3158 If a class definition does not explicitly declare a copy
3159 constructor, one is declared implicitly. */
3160 if (! TYPE_HAS_COPY_CTOR (t))
3162 TYPE_HAS_COPY_CTOR (t) = 1;
3163 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor;
3164 CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
3165 if (move_ok)
3166 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1;
3169 /* If there is no assignment operator, one will be created if and
3170 when it is needed. For now, just record whether or not the type
3171 of the parameter to the assignment operator will be a const or
3172 non-const reference. */
3173 if (!TYPE_HAS_COPY_ASSIGN (t))
3175 TYPE_HAS_COPY_ASSIGN (t) = 1;
3176 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment;
3177 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1;
3178 if (move_ok && !LAMBDA_TYPE_P (t))
3179 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1;
3182 /* We can't be lazy about declaring functions that might override
3183 a virtual function from a base class. */
3184 declare_virt_assop_and_dtor (t);
3186 while (*access_decls)
3188 tree using_decl = TREE_VALUE (*access_decls);
3189 tree decl = USING_DECL_DECLS (using_decl);
3190 if (DECL_NAME (using_decl) == ctor_identifier)
3192 /* declare, then remove the decl */
3193 tree ctor_list = decl;
3194 location_t loc = input_location;
3195 input_location = DECL_SOURCE_LOCATION (using_decl);
3196 for (ovl_iterator iter (ctor_list); iter; ++iter)
3197 one_inherited_ctor (*iter, t, using_decl);
3198 *access_decls = TREE_CHAIN (*access_decls);
3199 input_location = loc;
3201 else
3202 access_decls = &TREE_CHAIN (*access_decls);
3206 /* FIELD is a bit-field. We are finishing the processing for its
3207 enclosing type. Issue any appropriate messages and set appropriate
3208 flags. Returns false if an error has been diagnosed. */
3210 static bool
3211 check_bitfield_decl (tree field)
3213 tree type = TREE_TYPE (field);
3214 tree w;
3216 /* Extract the declared width of the bitfield, which has been
3217 temporarily stashed in DECL_BIT_FIELD_REPRESENTATIVE by grokbitfield. */
3218 w = DECL_BIT_FIELD_REPRESENTATIVE (field);
3219 gcc_assert (w != NULL_TREE);
3220 /* Remove the bit-field width indicator so that the rest of the
3221 compiler does not treat that value as a qualifier. */
3222 DECL_BIT_FIELD_REPRESENTATIVE (field) = NULL_TREE;
3224 /* Detect invalid bit-field type. */
3225 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3227 error ("bit-field %q+#D with non-integral type", field);
3228 w = error_mark_node;
3230 else
3232 location_t loc = input_location;
3233 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3234 STRIP_NOPS (w);
3236 /* detect invalid field size. */
3237 input_location = DECL_SOURCE_LOCATION (field);
3238 w = cxx_constant_value (w);
3239 input_location = loc;
3241 if (TREE_CODE (w) != INTEGER_CST)
3243 error ("bit-field %q+D width not an integer constant", field);
3244 w = error_mark_node;
3246 else if (tree_int_cst_sgn (w) < 0)
3248 error ("negative width in bit-field %q+D", field);
3249 w = error_mark_node;
3251 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3253 error ("zero width for bit-field %q+D", field);
3254 w = error_mark_node;
3256 else if ((TREE_CODE (type) != ENUMERAL_TYPE
3257 && TREE_CODE (type) != BOOLEAN_TYPE
3258 && compare_tree_int (w, TYPE_PRECISION (type)) > 0)
3259 || ((TREE_CODE (type) == ENUMERAL_TYPE
3260 || TREE_CODE (type) == BOOLEAN_TYPE)
3261 && tree_int_cst_lt (TYPE_SIZE (type), w)))
3262 warning_at (DECL_SOURCE_LOCATION (field), 0,
3263 "width of %qD exceeds its type", field);
3264 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3266 int prec = TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type));
3267 if (compare_tree_int (w, prec) < 0)
3268 warning_at (DECL_SOURCE_LOCATION (field), 0,
3269 "%qD is too small to hold all values of %q#T",
3270 field, type);
3274 if (w != error_mark_node)
3276 DECL_SIZE (field) = fold_convert (bitsizetype, w);
3277 DECL_BIT_FIELD (field) = 1;
3278 return true;
3280 else
3282 /* Non-bit-fields are aligned for their type. */
3283 DECL_BIT_FIELD (field) = 0;
3284 CLEAR_DECL_C_BIT_FIELD (field);
3285 return false;
3289 /* FIELD is a non bit-field. We are finishing the processing for its
3290 enclosing type T. Issue any appropriate messages and set appropriate
3291 flags. */
3293 static bool
3294 check_field_decl (tree field,
3295 tree t,
3296 int* cant_have_const_ctor,
3297 int* no_const_asn_ref)
3299 tree type = strip_array_types (TREE_TYPE (field));
3300 bool any_default_members = false;
3302 /* In C++98 an anonymous union cannot contain any fields which would change
3303 the settings of CANT_HAVE_CONST_CTOR and friends. */
3304 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11)
3306 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous
3307 structs. So, we recurse through their fields here. */
3308 else if (ANON_AGGR_TYPE_P (type))
3310 for (tree fields = TYPE_FIELDS (type); fields;
3311 fields = DECL_CHAIN (fields))
3312 if (TREE_CODE (fields) == FIELD_DECL)
3313 any_default_members |= check_field_decl (fields, t,
3314 cant_have_const_ctor,
3315 no_const_asn_ref);
3317 /* Check members with class type for constructors, destructors,
3318 etc. */
3319 else if (CLASS_TYPE_P (type))
3321 /* Never let anything with uninheritable virtuals
3322 make it through without complaint. */
3323 abstract_virtuals_error (field, type);
3325 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11)
3327 static bool warned;
3328 int oldcount = errorcount;
3329 if (TYPE_NEEDS_CONSTRUCTING (type))
3330 error ("member %q+#D with constructor not allowed in union",
3331 field);
3332 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3333 error ("member %q+#D with destructor not allowed in union", field);
3334 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
3335 error ("member %q+#D with copy assignment operator not allowed in union",
3336 field);
3337 if (!warned && errorcount > oldcount)
3339 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions "
3340 "only available with -std=c++11 or -std=gnu++11");
3341 warned = true;
3344 else
3346 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3347 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3348 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3349 TYPE_HAS_COMPLEX_COPY_ASSIGN (t)
3350 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)
3351 || !TYPE_HAS_COPY_ASSIGN (type));
3352 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type)
3353 || !TYPE_HAS_COPY_CTOR (type));
3354 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type);
3355 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type);
3356 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type)
3357 || TYPE_HAS_COMPLEX_DFLT (type));
3360 if (TYPE_HAS_COPY_CTOR (type)
3361 && !TYPE_HAS_CONST_COPY_CTOR (type))
3362 *cant_have_const_ctor = 1;
3364 if (TYPE_HAS_COPY_ASSIGN (type)
3365 && !TYPE_HAS_CONST_COPY_ASSIGN (type))
3366 *no_const_asn_ref = 1;
3369 check_abi_tags (t, field);
3371 if (DECL_INITIAL (field) != NULL_TREE)
3372 /* `build_class_init_list' does not recognize
3373 non-FIELD_DECLs. */
3374 any_default_members = true;
3376 return any_default_members;
3379 /* Check the data members (both static and non-static), class-scoped
3380 typedefs, etc., appearing in the declaration of T. Issue
3381 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3382 declaration order) of access declarations; each TREE_VALUE in this
3383 list is a USING_DECL.
3385 In addition, set the following flags:
3387 EMPTY_P
3388 The class is empty, i.e., contains no non-static data members.
3390 CANT_HAVE_CONST_CTOR_P
3391 This class cannot have an implicitly generated copy constructor
3392 taking a const reference.
3394 CANT_HAVE_CONST_ASN_REF
3395 This class cannot have an implicitly generated assignment
3396 operator taking a const reference.
3398 All of these flags should be initialized before calling this
3399 function.
3401 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3402 fields can be added by adding to this chain. */
3404 static void
3405 check_field_decls (tree t, tree *access_decls,
3406 int *cant_have_const_ctor_p,
3407 int *no_const_asn_ref_p)
3409 tree *field;
3410 tree *next;
3411 bool has_pointers;
3412 bool any_default_members;
3413 int cant_pack = 0;
3414 int field_access = -1;
3416 /* Assume there are no access declarations. */
3417 *access_decls = NULL_TREE;
3418 /* Assume this class has no pointer members. */
3419 has_pointers = false;
3420 /* Assume none of the members of this class have default
3421 initializations. */
3422 any_default_members = false;
3424 for (field = &TYPE_FIELDS (t); *field; field = next)
3426 tree x = *field;
3427 tree type = TREE_TYPE (x);
3428 int this_field_access;
3430 next = &DECL_CHAIN (x);
3432 if (TREE_CODE (x) == USING_DECL)
3434 /* Save the access declarations for our caller. */
3435 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3436 continue;
3439 if (TREE_CODE (x) == TYPE_DECL
3440 || TREE_CODE (x) == TEMPLATE_DECL)
3441 continue;
3443 if (TREE_CODE (x) == FUNCTION_DECL)
3444 /* FIXME: We should fold in the checking from check_methods. */
3445 continue;
3447 /* If we've gotten this far, it's a data member, possibly static,
3448 or an enumerator. */
3449 if (TREE_CODE (x) != CONST_DECL)
3450 DECL_CONTEXT (x) = t;
3452 /* When this goes into scope, it will be a non-local reference. */
3453 DECL_NONLOCAL (x) = 1;
3455 if (TREE_CODE (t) == UNION_TYPE)
3457 /* [class.union] (C++98)
3459 If a union contains a static data member, or a member of
3460 reference type, the program is ill-formed.
3462 In C++11 [class.union] says:
3463 If a union contains a non-static data member of reference type
3464 the program is ill-formed. */
3465 if (VAR_P (x) && cxx_dialect < cxx11)
3467 error ("in C++98 %q+D may not be static because it is "
3468 "a member of a union", x);
3469 continue;
3471 if (TYPE_REF_P (type)
3472 && TREE_CODE (x) == FIELD_DECL)
3474 error ("non-static data member %q+D in a union may not "
3475 "have reference type %qT", x, type);
3476 continue;
3480 /* Perform error checking that did not get done in
3481 grokdeclarator. */
3482 if (TREE_CODE (type) == FUNCTION_TYPE)
3484 error ("field %q+D invalidly declared function type", x);
3485 type = build_pointer_type (type);
3486 TREE_TYPE (x) = type;
3488 else if (TREE_CODE (type) == METHOD_TYPE)
3490 error ("field %q+D invalidly declared method type", x);
3491 type = build_pointer_type (type);
3492 TREE_TYPE (x) = type;
3495 if (type == error_mark_node)
3496 continue;
3498 if (TREE_CODE (x) == CONST_DECL || VAR_P (x))
3499 continue;
3501 /* Now it can only be a FIELD_DECL. */
3503 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3504 CLASSTYPE_NON_AGGREGATE (t) = 1;
3506 /* If at least one non-static data member is non-literal, the whole
3507 class becomes non-literal. Per Core/1453, volatile non-static
3508 data members and base classes are also not allowed.
3509 Note: if the type is incomplete we will complain later on. */
3510 if (COMPLETE_TYPE_P (type)
3511 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type)))
3512 CLASSTYPE_LITERAL_P (t) = false;
3514 /* A standard-layout class is a class that:
3516 has the same access control (Clause 11) for all non-static data members,
3517 ... */
3518 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0;
3519 if (field_access == -1)
3520 field_access = this_field_access;
3521 else if (this_field_access != field_access)
3522 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3524 /* If this is of reference type, check if it needs an init. */
3525 if (TYPE_REF_P (type))
3527 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3528 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3529 if (DECL_INITIAL (x) == NULL_TREE)
3530 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3531 if (cxx_dialect < cxx11)
3533 /* ARM $12.6.2: [A member initializer list] (or, for an
3534 aggregate, initialization by a brace-enclosed list) is the
3535 only way to initialize nonstatic const and reference
3536 members. */
3537 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3538 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3542 type = strip_array_types (type);
3544 if (TYPE_PACKED (t))
3546 if (!layout_pod_type_p (type) && !TYPE_PACKED (type))
3548 warning_at
3549 (DECL_SOURCE_LOCATION (x), 0,
3550 "ignoring packed attribute because of unpacked non-POD field %q#D",
3552 cant_pack = 1;
3554 else if (DECL_C_BIT_FIELD (x)
3555 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
3556 DECL_PACKED (x) = 1;
3559 if (DECL_C_BIT_FIELD (x)
3560 && integer_zerop (DECL_BIT_FIELD_REPRESENTATIVE (x)))
3561 /* We don't treat zero-width bitfields as making a class
3562 non-empty. */
3564 else
3566 /* The class is non-empty. */
3567 CLASSTYPE_EMPTY_P (t) = 0;
3568 /* The class is not even nearly empty. */
3569 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3570 /* If one of the data members contains an empty class,
3571 so does T. */
3572 if (CLASS_TYPE_P (type)
3573 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3574 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3577 /* This is used by -Weffc++ (see below). Warn only for pointers
3578 to members which might hold dynamic memory. So do not warn
3579 for pointers to functions or pointers to members. */
3580 if (TYPE_PTR_P (type)
3581 && !TYPE_PTRFN_P (type))
3582 has_pointers = true;
3584 if (CLASS_TYPE_P (type))
3586 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
3587 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3588 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
3589 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3592 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3593 CLASSTYPE_HAS_MUTABLE (t) = 1;
3595 if (DECL_MUTABLE_P (x))
3597 if (CP_TYPE_CONST_P (type))
3599 error ("member %q+D cannot be declared both %<const%> "
3600 "and %<mutable%>", x);
3601 continue;
3603 if (TYPE_REF_P (type))
3605 error ("member %q+D cannot be declared as a %<mutable%> "
3606 "reference", x);
3607 continue;
3611 if (! layout_pod_type_p (type))
3612 /* DR 148 now allows pointers to members (which are POD themselves),
3613 to be allowed in POD structs. */
3614 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1;
3616 if (!std_layout_type_p (type))
3617 CLASSTYPE_NON_STD_LAYOUT (t) = 1;
3619 if (! zero_init_p (type))
3620 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3622 /* We set DECL_C_BIT_FIELD in grokbitfield.
3623 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3624 if (DECL_C_BIT_FIELD (x))
3625 check_bitfield_decl (x);
3627 if (check_field_decl (x, t, cant_have_const_ctor_p, no_const_asn_ref_p))
3629 if (any_default_members
3630 && TREE_CODE (t) == UNION_TYPE)
3631 error ("multiple fields in union %qT initialized", t);
3632 any_default_members = true;
3635 /* Now that we've removed bit-field widths from DECL_INITIAL,
3636 anything left in DECL_INITIAL is an NSDMI that makes the class
3637 non-aggregate in C++11. */
3638 if (DECL_INITIAL (x) && cxx_dialect < cxx14)
3639 CLASSTYPE_NON_AGGREGATE (t) = true;
3641 /* If any field is const, the structure type is pseudo-const. */
3642 if (CP_TYPE_CONST_P (type))
3644 C_TYPE_FIELDS_READONLY (t) = 1;
3645 if (DECL_INITIAL (x) == NULL_TREE)
3646 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3647 if (cxx_dialect < cxx11)
3649 /* ARM $12.6.2: [A member initializer list] (or, for an
3650 aggregate, initialization by a brace-enclosed list) is the
3651 only way to initialize nonstatic const and reference
3652 members. */
3653 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1;
3654 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1;
3657 /* A field that is pseudo-const makes the structure likewise. */
3658 else if (CLASS_TYPE_P (type))
3660 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3661 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3662 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3663 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3666 /* Core issue 80: A nonstatic data member is required to have a
3667 different name from the class iff the class has a
3668 user-declared constructor. */
3669 if (constructor_name_p (DECL_NAME (x), t)
3670 && TYPE_HAS_USER_CONSTRUCTOR (t))
3671 permerror (DECL_SOURCE_LOCATION (x),
3672 "field %q#D with same name as class", x);
3675 /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
3676 it should also define a copy constructor and an assignment operator to
3677 implement the correct copy semantic (deep vs shallow, etc.). As it is
3678 not feasible to check whether the constructors do allocate dynamic memory
3679 and store it within members, we approximate the warning like this:
3681 -- Warn only if there are members which are pointers
3682 -- Warn only if there is a non-trivial constructor (otherwise,
3683 there cannot be memory allocated).
3684 -- Warn only if there is a non-trivial destructor. We assume that the
3685 user at least implemented the cleanup correctly, and a destructor
3686 is needed to free dynamic memory.
3688 This seems enough for practical purposes. */
3689 if (warn_ecpp
3690 && has_pointers
3691 && TYPE_HAS_USER_CONSTRUCTOR (t)
3692 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3693 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t)))
3695 warning (OPT_Weffc__, "%q#T has pointer data members", t);
3697 if (! TYPE_HAS_COPY_CTOR (t))
3699 warning (OPT_Weffc__,
3700 " but does not override %<%T(const %T&)%>", t, t);
3701 if (!TYPE_HAS_COPY_ASSIGN (t))
3702 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t);
3704 else if (! TYPE_HAS_COPY_ASSIGN (t))
3705 warning (OPT_Weffc__,
3706 " but does not override %<operator=(const %T&)%>", t);
3709 /* Non-static data member initializers make the default constructor
3710 non-trivial. */
3711 if (any_default_members)
3713 TYPE_NEEDS_CONSTRUCTING (t) = true;
3714 TYPE_HAS_COMPLEX_DFLT (t) = true;
3717 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */
3718 if (cant_pack)
3719 TYPE_PACKED (t) = 0;
3721 /* Check anonymous struct/anonymous union fields. */
3722 finish_struct_anon (t);
3724 /* We've built up the list of access declarations in reverse order.
3725 Fix that now. */
3726 *access_decls = nreverse (*access_decls);
3729 /* If TYPE is an empty class type, records its OFFSET in the table of
3730 OFFSETS. */
3732 static int
3733 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3735 splay_tree_node n;
3737 if (!is_empty_class (type))
3738 return 0;
3740 /* Record the location of this empty object in OFFSETS. */
3741 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3742 if (!n)
3743 n = splay_tree_insert (offsets,
3744 (splay_tree_key) offset,
3745 (splay_tree_value) NULL_TREE);
3746 n->value = ((splay_tree_value)
3747 tree_cons (NULL_TREE,
3748 type,
3749 (tree) n->value));
3751 return 0;
3754 /* Returns nonzero if TYPE is an empty class type and there is
3755 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3757 static int
3758 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3760 splay_tree_node n;
3761 tree t;
3763 if (!is_empty_class (type))
3764 return 0;
3766 /* Record the location of this empty object in OFFSETS. */
3767 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3768 if (!n)
3769 return 0;
3771 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3772 if (same_type_p (TREE_VALUE (t), type))
3773 return 1;
3775 return 0;
3778 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3779 F for every subobject, passing it the type, offset, and table of
3780 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3781 be traversed.
3783 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3784 than MAX_OFFSET will not be walked.
3786 If F returns a nonzero value, the traversal ceases, and that value
3787 is returned. Otherwise, returns zero. */
3789 static int
3790 walk_subobject_offsets (tree type,
3791 subobject_offset_fn f,
3792 tree offset,
3793 splay_tree offsets,
3794 tree max_offset,
3795 int vbases_p)
3797 int r = 0;
3798 tree type_binfo = NULL_TREE;
3800 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3801 stop. */
3802 if (max_offset && tree_int_cst_lt (max_offset, offset))
3803 return 0;
3805 if (type == error_mark_node)
3806 return 0;
3808 if (!TYPE_P (type))
3810 type_binfo = type;
3811 type = BINFO_TYPE (type);
3814 if (CLASS_TYPE_P (type))
3816 tree field;
3817 tree binfo;
3818 int i;
3820 /* Avoid recursing into objects that are not interesting. */
3821 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3822 return 0;
3824 /* Record the location of TYPE. */
3825 r = (*f) (type, offset, offsets);
3826 if (r)
3827 return r;
3829 /* Iterate through the direct base classes of TYPE. */
3830 if (!type_binfo)
3831 type_binfo = TYPE_BINFO (type);
3832 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
3834 tree binfo_offset;
3836 if (BINFO_VIRTUAL_P (binfo))
3837 continue;
3839 tree orig_binfo;
3840 /* We cannot rely on BINFO_OFFSET being set for the base
3841 class yet, but the offsets for direct non-virtual
3842 bases can be calculated by going back to the TYPE. */
3843 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
3844 binfo_offset = size_binop (PLUS_EXPR,
3845 offset,
3846 BINFO_OFFSET (orig_binfo));
3848 r = walk_subobject_offsets (binfo,
3850 binfo_offset,
3851 offsets,
3852 max_offset,
3853 /*vbases_p=*/0);
3854 if (r)
3855 return r;
3858 if (CLASSTYPE_VBASECLASSES (type))
3860 unsigned ix;
3861 vec<tree, va_gc> *vbases;
3863 /* Iterate through the virtual base classes of TYPE. In G++
3864 3.2, we included virtual bases in the direct base class
3865 loop above, which results in incorrect results; the
3866 correct offsets for virtual bases are only known when
3867 working with the most derived type. */
3868 if (vbases_p)
3869 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
3870 vec_safe_iterate (vbases, ix, &binfo); ix++)
3872 r = walk_subobject_offsets (binfo,
3874 size_binop (PLUS_EXPR,
3875 offset,
3876 BINFO_OFFSET (binfo)),
3877 offsets,
3878 max_offset,
3879 /*vbases_p=*/0);
3880 if (r)
3881 return r;
3883 else
3885 /* We still have to walk the primary base, if it is
3886 virtual. (If it is non-virtual, then it was walked
3887 above.) */
3888 tree vbase = get_primary_binfo (type_binfo);
3890 if (vbase && BINFO_VIRTUAL_P (vbase)
3891 && BINFO_PRIMARY_P (vbase)
3892 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
3894 r = (walk_subobject_offsets
3895 (vbase, f, offset,
3896 offsets, max_offset, /*vbases_p=*/0));
3897 if (r)
3898 return r;
3903 /* Iterate through the fields of TYPE. */
3904 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
3905 if (TREE_CODE (field) == FIELD_DECL
3906 && TREE_TYPE (field) != error_mark_node
3907 && !DECL_ARTIFICIAL (field))
3909 tree field_offset;
3911 field_offset = byte_position (field);
3913 r = walk_subobject_offsets (TREE_TYPE (field),
3915 size_binop (PLUS_EXPR,
3916 offset,
3917 field_offset),
3918 offsets,
3919 max_offset,
3920 /*vbases_p=*/1);
3921 if (r)
3922 return r;
3925 else if (TREE_CODE (type) == ARRAY_TYPE)
3927 tree element_type = strip_array_types (type);
3928 tree domain = TYPE_DOMAIN (type);
3929 tree index;
3931 /* Avoid recursing into objects that are not interesting. */
3932 if (!CLASS_TYPE_P (element_type)
3933 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
3934 || !domain
3935 || integer_minus_onep (TYPE_MAX_VALUE (domain)))
3936 return 0;
3938 /* Step through each of the elements in the array. */
3939 for (index = size_zero_node;
3940 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index);
3941 index = size_binop (PLUS_EXPR, index, size_one_node))
3943 r = walk_subobject_offsets (TREE_TYPE (type),
3945 offset,
3946 offsets,
3947 max_offset,
3948 /*vbases_p=*/1);
3949 if (r)
3950 return r;
3951 offset = size_binop (PLUS_EXPR, offset,
3952 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3953 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3954 there's no point in iterating through the remaining
3955 elements of the array. */
3956 if (max_offset && tree_int_cst_lt (max_offset, offset))
3957 break;
3961 return 0;
3964 /* Record all of the empty subobjects of TYPE (either a type or a
3965 binfo). If IS_DATA_MEMBER is true, then a non-static data member
3966 is being placed at OFFSET; otherwise, it is a base class that is
3967 being placed at OFFSET. */
3969 static void
3970 record_subobject_offsets (tree type,
3971 tree offset,
3972 splay_tree offsets,
3973 bool is_data_member)
3975 tree max_offset;
3976 /* If recording subobjects for a non-static data member or a
3977 non-empty base class , we do not need to record offsets beyond
3978 the size of the biggest empty class. Additional data members
3979 will go at the end of the class. Additional base classes will go
3980 either at offset zero (if empty, in which case they cannot
3981 overlap with offsets past the size of the biggest empty class) or
3982 at the end of the class.
3984 However, if we are placing an empty base class, then we must record
3985 all offsets, as either the empty class is at offset zero (where
3986 other empty classes might later be placed) or at the end of the
3987 class (where other objects might then be placed, so other empty
3988 subobjects might later overlap). */
3989 if (is_data_member
3990 || !is_empty_class (BINFO_TYPE (type)))
3991 max_offset = sizeof_biggest_empty_class;
3992 else
3993 max_offset = NULL_TREE;
3994 walk_subobject_offsets (type, record_subobject_offset, offset,
3995 offsets, max_offset, is_data_member);
3998 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3999 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
4000 virtual bases of TYPE are examined. */
4002 static int
4003 layout_conflict_p (tree type,
4004 tree offset,
4005 splay_tree offsets,
4006 int vbases_p)
4008 splay_tree_node max_node;
4010 /* Get the node in OFFSETS that indicates the maximum offset where
4011 an empty subobject is located. */
4012 max_node = splay_tree_max (offsets);
4013 /* If there aren't any empty subobjects, then there's no point in
4014 performing this check. */
4015 if (!max_node)
4016 return 0;
4018 return walk_subobject_offsets (type, check_subobject_offset, offset,
4019 offsets, (tree) (max_node->key),
4020 vbases_p);
4023 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4024 non-static data member of the type indicated by RLI. BINFO is the
4025 binfo corresponding to the base subobject, OFFSETS maps offsets to
4026 types already located at those offsets. This function determines
4027 the position of the DECL. */
4029 static void
4030 layout_nonempty_base_or_field (record_layout_info rli,
4031 tree decl,
4032 tree binfo,
4033 splay_tree offsets)
4035 tree offset = NULL_TREE;
4036 bool field_p;
4037 tree type;
4039 if (binfo)
4041 /* For the purposes of determining layout conflicts, we want to
4042 use the class type of BINFO; TREE_TYPE (DECL) will be the
4043 CLASSTYPE_AS_BASE version, which does not contain entries for
4044 zero-sized bases. */
4045 type = TREE_TYPE (binfo);
4046 field_p = false;
4048 else
4050 type = TREE_TYPE (decl);
4051 field_p = true;
4054 /* Try to place the field. It may take more than one try if we have
4055 a hard time placing the field without putting two objects of the
4056 same type at the same address. */
4057 while (1)
4059 struct record_layout_info_s old_rli = *rli;
4061 /* Place this field. */
4062 place_field (rli, decl);
4063 offset = byte_position (decl);
4065 /* We have to check to see whether or not there is already
4066 something of the same type at the offset we're about to use.
4067 For example, consider:
4069 struct S {};
4070 struct T : public S { int i; };
4071 struct U : public S, public T {};
4073 Here, we put S at offset zero in U. Then, we can't put T at
4074 offset zero -- its S component would be at the same address
4075 as the S we already allocated. So, we have to skip ahead.
4076 Since all data members, including those whose type is an
4077 empty class, have nonzero size, any overlap can happen only
4078 with a direct or indirect base-class -- it can't happen with
4079 a data member. */
4080 /* In a union, overlap is permitted; all members are placed at
4081 offset zero. */
4082 if (TREE_CODE (rli->t) == UNION_TYPE)
4083 break;
4084 if (layout_conflict_p (field_p ? type : binfo, offset,
4085 offsets, field_p))
4087 /* Strip off the size allocated to this field. That puts us
4088 at the first place we could have put the field with
4089 proper alignment. */
4090 *rli = old_rli;
4092 /* Bump up by the alignment required for the type. */
4093 rli->bitpos
4094 = size_binop (PLUS_EXPR, rli->bitpos,
4095 bitsize_int (binfo
4096 ? CLASSTYPE_ALIGN (type)
4097 : TYPE_ALIGN (type)));
4098 normalize_rli (rli);
4100 else if (TREE_CODE (type) == NULLPTR_TYPE
4101 && warn_abi && abi_version_crosses (9))
4103 /* Before ABI v9, we were giving nullptr_t alignment of 1; if
4104 the offset wasn't aligned like a pointer when we started to
4105 layout this field, that affects its position. */
4106 tree pos = rli_size_unit_so_far (&old_rli);
4107 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0)
4109 if (abi_version_at_least (9))
4110 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi,
4111 "alignment of %qD increased in -fabi-version=9 "
4112 "(GCC 5.2)", decl);
4113 else
4114 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment "
4115 "of %qD will increase in -fabi-version=9", decl);
4117 break;
4119 else
4120 /* There was no conflict. We're done laying out this field. */
4121 break;
4124 /* Now that we know where it will be placed, update its
4125 BINFO_OFFSET. */
4126 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
4127 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
4128 this point because their BINFO_OFFSET is copied from another
4129 hierarchy. Therefore, we may not need to add the entire
4130 OFFSET. */
4131 propagate_binfo_offsets (binfo,
4132 size_diffop_loc (input_location,
4133 fold_convert (ssizetype, offset),
4134 fold_convert (ssizetype,
4135 BINFO_OFFSET (binfo))));
4138 /* Returns true if TYPE is empty and OFFSET is nonzero. */
4140 static int
4141 empty_base_at_nonzero_offset_p (tree type,
4142 tree offset,
4143 splay_tree /*offsets*/)
4145 return is_empty_class (type) && !integer_zerop (offset);
4148 /* Layout the empty base BINFO. EOC indicates the byte currently just
4149 past the end of the class, and should be correctly aligned for a
4150 class of the type indicated by BINFO; OFFSETS gives the offsets of
4151 the empty bases allocated so far. T is the most derived
4152 type. Return nonzero iff we added it at the end. */
4154 static bool
4155 layout_empty_base (record_layout_info rli, tree binfo,
4156 tree eoc, splay_tree offsets)
4158 tree alignment;
4159 tree basetype = BINFO_TYPE (binfo);
4160 bool atend = false;
4162 /* This routine should only be used for empty classes. */
4163 gcc_assert (is_empty_class (basetype));
4164 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
4166 if (!integer_zerop (BINFO_OFFSET (binfo)))
4167 propagate_binfo_offsets
4168 (binfo, size_diffop_loc (input_location,
4169 size_zero_node, BINFO_OFFSET (binfo)));
4171 /* This is an empty base class. We first try to put it at offset
4172 zero. */
4173 if (layout_conflict_p (binfo,
4174 BINFO_OFFSET (binfo),
4175 offsets,
4176 /*vbases_p=*/0))
4178 /* That didn't work. Now, we move forward from the next
4179 available spot in the class. */
4180 atend = true;
4181 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc));
4182 while (1)
4184 if (!layout_conflict_p (binfo,
4185 BINFO_OFFSET (binfo),
4186 offsets,
4187 /*vbases_p=*/0))
4188 /* We finally found a spot where there's no overlap. */
4189 break;
4191 /* There's overlap here, too. Bump along to the next spot. */
4192 propagate_binfo_offsets (binfo, alignment);
4196 if (CLASSTYPE_USER_ALIGN (basetype))
4198 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
4199 if (warn_packed)
4200 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
4201 TYPE_USER_ALIGN (rli->t) = 1;
4204 return atend;
4207 /* Build the FIELD_DECL for BASETYPE as a base of T, add it to the chain of
4208 fields at NEXT_FIELD, and return it. */
4210 static tree
4211 build_base_field_1 (tree t, tree basetype, tree *&next_field)
4213 /* Create the FIELD_DECL. */
4214 gcc_assert (CLASSTYPE_AS_BASE (basetype));
4215 tree decl = build_decl (input_location,
4216 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
4217 DECL_ARTIFICIAL (decl) = 1;
4218 DECL_IGNORED_P (decl) = 1;
4219 DECL_FIELD_CONTEXT (decl) = t;
4220 if (is_empty_class (basetype))
4221 /* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
4222 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
4223 else
4225 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4226 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4228 SET_DECL_ALIGN (decl, CLASSTYPE_ALIGN (basetype));
4229 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
4230 SET_DECL_MODE (decl, TYPE_MODE (basetype));
4231 DECL_FIELD_IS_BASE (decl) = 1;
4233 /* Add the new FIELD_DECL to the list of fields for T. */
4234 DECL_CHAIN (decl) = *next_field;
4235 *next_field = decl;
4236 next_field = &DECL_CHAIN (decl);
4238 return decl;
4241 /* Layout the base given by BINFO in the class indicated by RLI.
4242 *BASE_ALIGN is a running maximum of the alignments of
4243 any base class. OFFSETS gives the location of empty base
4244 subobjects. T is the most derived type. Return nonzero if the new
4245 object cannot be nearly-empty. A new FIELD_DECL is inserted at
4246 *NEXT_FIELD, unless BINFO is for an empty base class.
4248 Returns the location at which the next field should be inserted. */
4250 static tree *
4251 build_base_field (record_layout_info rli, tree binfo,
4252 splay_tree offsets, tree *next_field)
4254 tree t = rli->t;
4255 tree basetype = BINFO_TYPE (binfo);
4257 if (!COMPLETE_TYPE_P (basetype))
4258 /* This error is now reported in xref_tag, thus giving better
4259 location information. */
4260 return next_field;
4262 /* Place the base class. */
4263 if (!is_empty_class (basetype))
4265 tree decl;
4267 /* The containing class is non-empty because it has a non-empty
4268 base class. */
4269 CLASSTYPE_EMPTY_P (t) = 0;
4271 /* Create the FIELD_DECL. */
4272 decl = build_base_field_1 (t, basetype, next_field);
4274 /* Try to place the field. It may take more than one try if we
4275 have a hard time placing the field without putting two
4276 objects of the same type at the same address. */
4277 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
4279 else
4281 tree eoc;
4282 bool atend;
4284 /* On some platforms (ARM), even empty classes will not be
4285 byte-aligned. */
4286 eoc = round_up_loc (input_location,
4287 rli_size_unit_so_far (rli),
4288 CLASSTYPE_ALIGN_UNIT (basetype));
4289 atend = layout_empty_base (rli, binfo, eoc, offsets);
4290 /* A nearly-empty class "has no proper base class that is empty,
4291 not morally virtual, and at an offset other than zero." */
4292 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
4294 if (atend)
4295 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4296 /* The check above (used in G++ 3.2) is insufficient because
4297 an empty class placed at offset zero might itself have an
4298 empty base at a nonzero offset. */
4299 else if (walk_subobject_offsets (basetype,
4300 empty_base_at_nonzero_offset_p,
4301 size_zero_node,
4302 /*offsets=*/NULL,
4303 /*max_offset=*/NULL_TREE,
4304 /*vbases_p=*/true))
4305 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4308 /* We used to not create a FIELD_DECL for empty base classes because of
4309 back end issues with overlapping FIELD_DECLs, but that doesn't seem to
4310 be a problem anymore. We need them to handle initialization of C++17
4311 aggregate bases. */
4312 if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
4314 tree decl = build_base_field_1 (t, basetype, next_field);
4315 DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
4316 DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
4317 SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
4320 /* An empty virtual base causes a class to be non-empty
4321 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
4322 here because that was already done when the virtual table
4323 pointer was created. */
4326 /* Record the offsets of BINFO and its base subobjects. */
4327 record_subobject_offsets (binfo,
4328 BINFO_OFFSET (binfo),
4329 offsets,
4330 /*is_data_member=*/false);
4332 return next_field;
4335 /* Layout all of the non-virtual base classes. Record empty
4336 subobjects in OFFSETS. T is the most derived type. Return nonzero
4337 if the type cannot be nearly empty. The fields created
4338 corresponding to the base classes will be inserted at
4339 *NEXT_FIELD. */
4341 static void
4342 build_base_fields (record_layout_info rli,
4343 splay_tree offsets, tree *next_field)
4345 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4346 subobjects. */
4347 tree t = rli->t;
4348 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
4349 int i;
4351 /* The primary base class is always allocated first. */
4352 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4353 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4354 offsets, next_field);
4356 /* Now allocate the rest of the bases. */
4357 for (i = 0; i < n_baseclasses; ++i)
4359 tree base_binfo;
4361 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
4363 /* The primary base was already allocated above, so we don't
4364 need to allocate it again here. */
4365 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4366 continue;
4368 /* Virtual bases are added at the end (a primary virtual base
4369 will have already been added). */
4370 if (BINFO_VIRTUAL_P (base_binfo))
4371 continue;
4373 next_field = build_base_field (rli, base_binfo,
4374 offsets, next_field);
4378 /* Go through the TYPE_FIELDS of T issuing any appropriate
4379 diagnostics, figuring out which methods override which other
4380 methods, and so forth. */
4382 static void
4383 check_methods (tree t)
4385 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
4386 if (DECL_DECLARES_FUNCTION_P (x))
4388 check_for_override (x, t);
4390 if (DECL_PURE_VIRTUAL_P (x)
4391 && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x)))
4392 error ("initializer specified for non-virtual method %q+D", x);
4393 /* The name of the field is the original field name
4394 Save this in auxiliary field for later overloading. */
4395 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x))
4397 TYPE_POLYMORPHIC_P (t) = 1;
4398 if (DECL_PURE_VIRTUAL_P (x))
4399 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
4402 /* All user-provided destructors are non-trivial.
4403 Constructors and assignment ops are handled in
4404 grok_special_member_properties. */
4405 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x))
4406 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
4407 if (!DECL_VIRTUAL_P (x)
4408 && lookup_attribute ("transaction_safe_dynamic",
4409 DECL_ATTRIBUTES (x)))
4410 error_at (DECL_SOURCE_LOCATION (x),
4411 "%<transaction_safe_dynamic%> may only be specified for "
4412 "a virtual function");
4416 /* FN is a constructor or destructor. Clone the declaration to create
4417 a specialized in-charge or not-in-charge version, as indicated by
4418 NAME. */
4420 static tree
4421 build_clone (tree fn, tree name)
4423 tree parms;
4424 tree clone;
4426 /* Copy the function. */
4427 clone = copy_decl (fn);
4428 /* Reset the function name. */
4429 DECL_NAME (clone) = name;
4430 /* Remember where this function came from. */
4431 DECL_ABSTRACT_ORIGIN (clone) = fn;
4432 /* Make it easy to find the CLONE given the FN. */
4433 DECL_CHAIN (clone) = DECL_CHAIN (fn);
4434 DECL_CHAIN (fn) = clone;
4436 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */
4437 if (TREE_CODE (clone) == TEMPLATE_DECL)
4439 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4440 DECL_TEMPLATE_RESULT (clone) = result;
4441 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4442 DECL_TI_TEMPLATE (result) = clone;
4443 TREE_TYPE (clone) = TREE_TYPE (result);
4444 return clone;
4446 else
4448 // Clone constraints.
4449 if (flag_concepts)
4450 if (tree ci = get_constraints (fn))
4451 set_constraints (clone, copy_node (ci));
4455 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4456 DECL_CLONED_FUNCTION (clone) = fn;
4457 /* There's no pending inline data for this function. */
4458 DECL_PENDING_INLINE_INFO (clone) = NULL;
4459 DECL_PENDING_INLINE_P (clone) = 0;
4461 /* The base-class destructor is not virtual. */
4462 if (name == base_dtor_identifier)
4464 DECL_VIRTUAL_P (clone) = 0;
4465 if (TREE_CODE (clone) != TEMPLATE_DECL)
4466 DECL_VINDEX (clone) = NULL_TREE;
4469 bool ctor_omit_inherited_parms_p = ctor_omit_inherited_parms (clone);
4470 if (ctor_omit_inherited_parms_p)
4471 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (clone));
4473 /* If there was an in-charge parameter, drop it from the function
4474 type. */
4475 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4477 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4478 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4479 /* Skip the `this' parameter. */
4480 parmtypes = TREE_CHAIN (parmtypes);
4481 /* Skip the in-charge parameter. */
4482 parmtypes = TREE_CHAIN (parmtypes);
4483 /* And the VTT parm, in a complete [cd]tor. */
4484 if (DECL_HAS_VTT_PARM_P (fn)
4485 && ! DECL_NEEDS_VTT_PARM_P (clone))
4486 parmtypes = TREE_CHAIN (parmtypes);
4487 if (ctor_omit_inherited_parms_p)
4489 /* If we're omitting inherited parms, that just leaves the VTT. */
4490 gcc_assert (DECL_NEEDS_VTT_PARM_P (clone));
4491 parmtypes = tree_cons (NULL_TREE, vtt_parm_type, void_list_node);
4493 TREE_TYPE (clone)
4494 = build_method_type_directly (basetype,
4495 TREE_TYPE (TREE_TYPE (clone)),
4496 parmtypes);
4497 TREE_TYPE (clone)
4498 = cp_build_type_attribute_variant (TREE_TYPE (clone),
4499 TYPE_ATTRIBUTES (TREE_TYPE (fn)));
4500 TREE_TYPE (clone)
4501 = cxx_copy_lang_qualifiers (TREE_TYPE (clone), TREE_TYPE (fn));
4504 /* Copy the function parameters. */
4505 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4506 /* Remove the in-charge parameter. */
4507 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4509 DECL_CHAIN (DECL_ARGUMENTS (clone))
4510 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4511 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4513 /* And the VTT parm, in a complete [cd]tor. */
4514 if (DECL_HAS_VTT_PARM_P (fn))
4516 if (DECL_NEEDS_VTT_PARM_P (clone))
4517 DECL_HAS_VTT_PARM_P (clone) = 1;
4518 else
4520 DECL_CHAIN (DECL_ARGUMENTS (clone))
4521 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone)));
4522 DECL_HAS_VTT_PARM_P (clone) = 0;
4526 /* A base constructor inheriting from a virtual base doesn't get the
4527 arguments. */
4528 if (ctor_omit_inherited_parms_p)
4529 DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))) = NULL_TREE;
4531 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms))
4533 DECL_CONTEXT (parms) = clone;
4534 cxx_dup_lang_specific_decl (parms);
4537 /* Create the RTL for this function. */
4538 SET_DECL_RTL (clone, NULL);
4539 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
4541 return clone;
4544 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do
4545 not invoke this function directly.
4547 For a non-thunk function, returns the address of the slot for storing
4548 the function it is a clone of. Otherwise returns NULL_TREE.
4550 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if
4551 cloned_function is unset. This is to support the separate
4552 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter
4553 on a template makes sense, but not the former. */
4555 tree *
4556 decl_cloned_function_p (const_tree decl, bool just_testing)
4558 tree *ptr;
4559 if (just_testing)
4560 decl = STRIP_TEMPLATE (decl);
4562 if (TREE_CODE (decl) != FUNCTION_DECL
4563 || !DECL_LANG_SPECIFIC (decl)
4564 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p)
4566 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4567 if (!just_testing)
4568 lang_check_failed (__FILE__, __LINE__, __FUNCTION__);
4569 else
4570 #endif
4571 return NULL;
4574 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function;
4575 if (just_testing && *ptr == NULL_TREE)
4576 return NULL;
4577 else
4578 return ptr;
4581 /* Produce declarations for all appropriate clones of FN. If
4582 UPDATE_METHODS is true, the clones are added to the
4583 CLASSTYPE_MEMBER_VEC. */
4585 void
4586 clone_function_decl (tree fn, bool update_methods)
4588 tree clone;
4590 /* Avoid inappropriate cloning. */
4591 if (DECL_CHAIN (fn)
4592 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn)))
4593 return;
4595 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4597 /* For each constructor, we need two variants: an in-charge version
4598 and a not-in-charge version. */
4599 clone = build_clone (fn, complete_ctor_identifier);
4600 if (update_methods)
4601 add_method (DECL_CONTEXT (clone), clone, false);
4602 clone = build_clone (fn, base_ctor_identifier);
4603 if (update_methods)
4604 add_method (DECL_CONTEXT (clone), clone, false);
4606 else
4608 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
4610 /* For each destructor, we need three variants: an in-charge
4611 version, a not-in-charge version, and an in-charge deleting
4612 version. We clone the deleting version first because that
4613 means it will go second on the TYPE_FIELDS list -- and that
4614 corresponds to the correct layout order in the virtual
4615 function table.
4617 For a non-virtual destructor, we do not build a deleting
4618 destructor. */
4619 if (DECL_VIRTUAL_P (fn))
4621 clone = build_clone (fn, deleting_dtor_identifier);
4622 if (update_methods)
4623 add_method (DECL_CONTEXT (clone), clone, false);
4625 clone = build_clone (fn, complete_dtor_identifier);
4626 if (update_methods)
4627 add_method (DECL_CONTEXT (clone), clone, false);
4628 clone = build_clone (fn, base_dtor_identifier);
4629 if (update_methods)
4630 add_method (DECL_CONTEXT (clone), clone, false);
4633 /* Note that this is an abstract function that is never emitted. */
4634 DECL_ABSTRACT_P (fn) = true;
4637 /* DECL is an in charge constructor, which is being defined. This will
4638 have had an in class declaration, from whence clones were
4639 declared. An out-of-class definition can specify additional default
4640 arguments. As it is the clones that are involved in overload
4641 resolution, we must propagate the information from the DECL to its
4642 clones. */
4644 void
4645 adjust_clone_args (tree decl)
4647 tree clone;
4649 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone);
4650 clone = DECL_CHAIN (clone))
4652 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4653 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4654 tree decl_parms, clone_parms;
4656 clone_parms = orig_clone_parms;
4658 /* Skip the 'this' parameter. */
4659 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4660 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4662 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4663 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4664 if (DECL_HAS_VTT_PARM_P (decl))
4665 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4667 clone_parms = orig_clone_parms;
4668 if (DECL_HAS_VTT_PARM_P (clone))
4669 clone_parms = TREE_CHAIN (clone_parms);
4671 for (decl_parms = orig_decl_parms; decl_parms;
4672 decl_parms = TREE_CHAIN (decl_parms),
4673 clone_parms = TREE_CHAIN (clone_parms))
4675 if (clone_parms == void_list_node)
4677 gcc_assert (decl_parms == clone_parms
4678 || ctor_omit_inherited_parms (clone));
4679 break;
4682 gcc_assert (same_type_p (TREE_TYPE (decl_parms),
4683 TREE_TYPE (clone_parms)));
4685 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4687 /* A default parameter has been added. Adjust the
4688 clone's parameters. */
4689 clone_parms = orig_decl_parms;
4691 if (DECL_HAS_VTT_PARM_P (clone))
4693 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4694 TREE_VALUE (orig_clone_parms),
4695 clone_parms);
4696 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4699 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4700 tree type
4701 = build_method_type_directly (basetype,
4702 TREE_TYPE (TREE_TYPE (clone)),
4703 clone_parms);
4704 if (tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)))
4705 type = cp_build_type_attribute_variant (type, attrs);
4706 type = cxx_copy_lang_qualifiers (type, TREE_TYPE (clone));
4707 TREE_TYPE (clone) = type;
4709 clone_parms = NULL_TREE;
4710 break;
4713 gcc_assert (!clone_parms || clone_parms == void_list_node);
4717 /* For each of the constructors and destructors in T, create an
4718 in-charge and not-in-charge variant. */
4720 static void
4721 clone_constructors_and_destructors (tree t)
4723 /* While constructors can be via a using declaration, at this point
4724 we no longer need to know that. */
4725 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4726 clone_function_decl (*iter, /*update_methods=*/true);
4728 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
4729 clone_function_decl (dtor, /*update_methods=*/true);
4732 /* Deduce noexcept for a destructor DTOR. */
4734 void
4735 deduce_noexcept_on_destructor (tree dtor)
4737 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor)))
4738 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor),
4739 noexcept_deferred_spec);
4742 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes
4743 of TYPE for virtual functions which FNDECL overrides. Return a
4744 mask of the tm attributes found therein. */
4746 static int
4747 look_for_tm_attr_overrides (tree type, tree fndecl)
4749 tree binfo = TYPE_BINFO (type);
4750 tree base_binfo;
4751 int ix, found = 0;
4753 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix)
4755 tree o, basetype = BINFO_TYPE (base_binfo);
4757 if (!TYPE_POLYMORPHIC_P (basetype))
4758 continue;
4760 o = look_for_overrides_here (basetype, fndecl);
4761 if (o)
4763 if (lookup_attribute ("transaction_safe_dynamic",
4764 DECL_ATTRIBUTES (o)))
4765 /* transaction_safe_dynamic is not inherited. */;
4766 else
4767 found |= tm_attr_to_mask (find_tm_attribute
4768 (TYPE_ATTRIBUTES (TREE_TYPE (o))));
4770 else
4771 found |= look_for_tm_attr_overrides (basetype, fndecl);
4774 return found;
4777 /* Subroutine of set_method_tm_attributes. Handle the checks and
4778 inheritance for one virtual method FNDECL. */
4780 static void
4781 set_one_vmethod_tm_attributes (tree type, tree fndecl)
4783 tree tm_attr;
4784 int found, have;
4786 found = look_for_tm_attr_overrides (type, fndecl);
4788 /* If FNDECL doesn't actually override anything (i.e. T is the
4789 class that first declares FNDECL virtual), then we're done. */
4790 if (found == 0)
4791 return;
4793 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
4794 have = tm_attr_to_mask (tm_attr);
4796 /* Intel STM Language Extension 3.0, Section 4.2 table 4:
4797 tm_pure must match exactly, otherwise no weakening of
4798 tm_safe > tm_callable > nothing. */
4799 /* ??? The tm_pure attribute didn't make the transition to the
4800 multivendor language spec. */
4801 if (have == TM_ATTR_PURE)
4803 if (found != TM_ATTR_PURE)
4805 found &= -found;
4806 goto err_override;
4809 /* If the overridden function is tm_pure, then FNDECL must be. */
4810 else if (found == TM_ATTR_PURE && tm_attr)
4811 goto err_override;
4812 /* Look for base class combinations that cannot be satisfied. */
4813 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE))
4815 found &= ~TM_ATTR_PURE;
4816 found &= -found;
4817 error_at (DECL_SOURCE_LOCATION (fndecl),
4818 "method overrides both %<transaction_pure%> and %qE methods",
4819 tm_mask_to_attr (found));
4821 /* If FNDECL did not declare an attribute, then inherit the most
4822 restrictive one. */
4823 else if (tm_attr == NULL)
4825 apply_tm_attr (fndecl, tm_mask_to_attr (least_bit_hwi (found)));
4827 /* Otherwise validate that we're not weaker than a function
4828 that is being overridden. */
4829 else
4831 found &= -found;
4832 if (found <= TM_ATTR_CALLABLE && have > found)
4833 goto err_override;
4835 return;
4837 err_override:
4838 error_at (DECL_SOURCE_LOCATION (fndecl),
4839 "method declared %qE overriding %qE method",
4840 tm_attr, tm_mask_to_attr (found));
4843 /* For each of the methods in T, propagate a class-level tm attribute. */
4845 static void
4846 set_method_tm_attributes (tree t)
4848 tree class_tm_attr, fndecl;
4850 /* Don't bother collecting tm attributes if transactional memory
4851 support is not enabled. */
4852 if (!flag_tm)
4853 return;
4855 /* Process virtual methods first, as they inherit directly from the
4856 base virtual function and also require validation of new attributes. */
4857 if (TYPE_CONTAINS_VPTR_P (t))
4859 tree vchain;
4860 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain;
4861 vchain = TREE_CHAIN (vchain))
4863 fndecl = BV_FN (vchain);
4864 if (DECL_THUNK_P (fndecl))
4865 fndecl = THUNK_TARGET (fndecl);
4866 set_one_vmethod_tm_attributes (t, fndecl);
4870 /* If the class doesn't have an attribute, nothing more to do. */
4871 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t));
4872 if (class_tm_attr == NULL)
4873 return;
4875 /* Any method that does not yet have a tm attribute inherits
4876 the one from the class. */
4877 for (fndecl = TYPE_FIELDS (t); fndecl; fndecl = DECL_CHAIN (fndecl))
4878 if (DECL_DECLARES_FUNCTION_P (fndecl)
4879 && !find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
4880 apply_tm_attr (fndecl, class_tm_attr);
4883 /* Returns true if FN is a default constructor. */
4885 bool
4886 default_ctor_p (tree fn)
4888 return (DECL_CONSTRUCTOR_P (fn)
4889 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
4892 /* Returns true iff class T has a user-provided constructor that can be called
4893 with more than zero arguments. */
4895 bool
4896 type_has_user_nondefault_constructor (tree t)
4898 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4899 return false;
4901 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4903 tree fn = *iter;
4904 if (user_provided_p (fn)
4905 && (TREE_CODE (fn) == TEMPLATE_DECL
4906 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
4907 != NULL_TREE)))
4908 return true;
4911 return false;
4914 /* Returns the defaulted constructor if T has one. Otherwise, returns
4915 NULL_TREE. */
4917 tree
4918 in_class_defaulted_default_constructor (tree t)
4920 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4921 return NULL_TREE;
4923 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4925 tree fn = *iter;
4927 if (DECL_DEFAULTED_IN_CLASS_P (fn)
4928 && default_ctor_p (fn))
4929 return fn;
4932 return NULL_TREE;
4935 /* Returns true iff FN is a user-provided function, i.e. user-declared
4936 and not defaulted at its first declaration. */
4938 bool
4939 user_provided_p (tree fn)
4941 if (TREE_CODE (fn) == TEMPLATE_DECL)
4942 return true;
4943 else
4944 return (!DECL_ARTIFICIAL (fn)
4945 && !(DECL_INITIALIZED_IN_CLASS_P (fn)
4946 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn))));
4949 /* Returns true iff class T has a user-provided constructor. */
4951 bool
4952 type_has_user_provided_constructor (tree t)
4954 if (!CLASS_TYPE_P (t))
4955 return false;
4957 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4958 return false;
4960 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4961 if (user_provided_p (*iter))
4962 return true;
4964 return false;
4967 /* Returns true iff class T has a user-provided or explicit constructor. */
4969 bool
4970 type_has_user_provided_or_explicit_constructor (tree t)
4972 if (!CLASS_TYPE_P (t))
4973 return false;
4975 if (!TYPE_HAS_USER_CONSTRUCTOR (t))
4976 return false;
4978 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4980 tree fn = *iter;
4981 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn))
4982 return true;
4985 return false;
4988 /* Returns true iff class T has a non-user-provided (i.e. implicitly
4989 declared or explicitly defaulted in the class body) default
4990 constructor. */
4992 bool
4993 type_has_non_user_provided_default_constructor (tree t)
4995 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t))
4996 return false;
4997 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
4998 return true;
5000 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5002 tree fn = *iter;
5003 if (TREE_CODE (fn) == FUNCTION_DECL
5004 && default_ctor_p (fn)
5005 && !user_provided_p (fn))
5006 return true;
5009 return false;
5012 /* TYPE is being used as a virtual base, and has a non-trivial move
5013 assignment. Return true if this is due to there being a user-provided
5014 move assignment in TYPE or one of its subobjects; if there isn't, then
5015 multiple move assignment can't cause any harm. */
5017 bool
5018 vbase_has_user_provided_move_assign (tree type)
5020 /* Does the type itself have a user-provided move assignment operator? */
5021 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (type))
5022 for (ovl_iterator iter (get_class_binding_direct
5023 (type, assign_op_identifier));
5024 iter; ++iter)
5025 if (user_provided_p (*iter) && move_fn_p (*iter))
5026 return true;
5028 /* Do any of its bases? */
5029 tree binfo = TYPE_BINFO (type);
5030 tree base_binfo;
5031 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5032 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
5033 return true;
5035 /* Or non-static data members? */
5036 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5038 if (TREE_CODE (field) == FIELD_DECL
5039 && CLASS_TYPE_P (TREE_TYPE (field))
5040 && vbase_has_user_provided_move_assign (TREE_TYPE (field)))
5041 return true;
5044 /* Seems not. */
5045 return false;
5048 /* If default-initialization leaves part of TYPE uninitialized, returns
5049 a DECL for the field or TYPE itself (DR 253). */
5051 tree
5052 default_init_uninitialized_part (tree type)
5054 tree t, r, binfo;
5055 int i;
5057 type = strip_array_types (type);
5058 if (!CLASS_TYPE_P (type))
5059 return type;
5060 if (!type_has_non_user_provided_default_constructor (type))
5061 return NULL_TREE;
5062 for (binfo = TYPE_BINFO (type), i = 0;
5063 BINFO_BASE_ITERATE (binfo, i, t); ++i)
5065 r = default_init_uninitialized_part (BINFO_TYPE (t));
5066 if (r)
5067 return r;
5069 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
5070 if (TREE_CODE (t) == FIELD_DECL
5071 && !DECL_ARTIFICIAL (t)
5072 && !DECL_INITIAL (t))
5074 r = default_init_uninitialized_part (TREE_TYPE (t));
5075 if (r)
5076 return DECL_P (r) ? r : t;
5079 return NULL_TREE;
5082 /* Returns true iff for class T, a trivial synthesized default constructor
5083 would be constexpr. */
5085 bool
5086 trivial_default_constructor_is_constexpr (tree t)
5088 /* A defaulted trivial default constructor is constexpr
5089 if there is nothing to initialize. */
5090 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t));
5091 return is_really_empty_class (t);
5094 /* Returns true iff class T has a constexpr default constructor. */
5096 bool
5097 type_has_constexpr_default_constructor (tree t)
5099 tree fns;
5101 if (!CLASS_TYPE_P (t))
5103 /* The caller should have stripped an enclosing array. */
5104 gcc_assert (TREE_CODE (t) != ARRAY_TYPE);
5105 return false;
5107 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t))
5109 if (!TYPE_HAS_COMPLEX_DFLT (t))
5110 return trivial_default_constructor_is_constexpr (t);
5111 /* Non-trivial, we need to check subobject constructors. */
5112 lazily_declare_fn (sfk_constructor, t);
5114 fns = locate_ctor (t);
5115 return (fns && DECL_DECLARED_CONSTEXPR_P (fns));
5118 /* Returns true iff class T has a constexpr default constructor or has an
5119 implicitly declared default constructor that we can't tell if it's constexpr
5120 without forcing a lazy declaration (which might cause undesired
5121 instantiations). */
5123 bool
5124 type_maybe_constexpr_default_constructor (tree t)
5126 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t)
5127 && TYPE_HAS_COMPLEX_DFLT (t))
5128 /* Assume it's constexpr. */
5129 return true;
5130 return type_has_constexpr_default_constructor (t);
5133 /* Returns true iff class TYPE has a virtual destructor. */
5135 bool
5136 type_has_virtual_destructor (tree type)
5138 tree dtor;
5140 if (!CLASS_TYPE_P (type))
5141 return false;
5143 gcc_assert (COMPLETE_TYPE_P (type));
5144 dtor = CLASSTYPE_DESTRUCTOR (type);
5145 return (dtor && DECL_VIRTUAL_P (dtor));
5148 /* Returns true iff T, a class, has a move-assignment or
5149 move-constructor. Does not lazily declare either.
5150 If USER_P is false, any move function will do. If it is true, the
5151 move function must be user-declared.
5153 Note that user-declared here is different from "user-provided",
5154 which doesn't include functions that are defaulted in the
5155 class. */
5157 bool
5158 classtype_has_move_assign_or_move_ctor_p (tree t, bool user_p)
5160 gcc_assert (user_p
5161 || (!CLASSTYPE_LAZY_MOVE_CTOR (t)
5162 && !CLASSTYPE_LAZY_MOVE_ASSIGN (t)));
5164 if (!CLASSTYPE_LAZY_MOVE_CTOR (t))
5165 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5166 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5167 return true;
5169 if (!CLASSTYPE_LAZY_MOVE_ASSIGN (t))
5170 for (ovl_iterator iter (get_class_binding_direct
5171 (t, assign_op_identifier));
5172 iter; ++iter)
5173 if ((!user_p || !DECL_ARTIFICIAL (*iter)) && move_fn_p (*iter))
5174 return true;
5176 return false;
5179 /* True iff T has a move constructor that is not deleted. */
5181 bool
5182 classtype_has_non_deleted_move_ctor (tree t)
5184 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
5185 lazily_declare_fn (sfk_move_constructor, t);
5186 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5187 if (move_fn_p (*iter) && !DECL_DELETED_FN (*iter))
5188 return true;
5189 return false;
5192 /* If T, a class, has a user-provided copy constructor, copy assignment
5193 operator, or destructor, returns that function. Otherwise, null. */
5195 tree
5196 classtype_has_user_copy_or_dtor (tree t)
5198 if (!CLASSTYPE_LAZY_COPY_CTOR (t))
5199 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5201 tree fn = *iter;
5202 if (user_provided_p (fn) && copy_fn_p (fn))
5203 return fn;
5206 if (!CLASSTYPE_LAZY_COPY_ASSIGN (t))
5207 for (ovl_iterator iter (get_class_binding_direct
5208 (t, assign_op_identifier));
5209 iter; ++iter)
5211 tree fn = *iter;
5212 if (user_provided_p (fn) && copy_fn_p (fn))
5213 return fn;
5216 if (!CLASSTYPE_LAZY_DESTRUCTOR (t))
5218 tree fn = CLASSTYPE_DESTRUCTOR (t);
5219 if (user_provided_p (fn))
5220 return fn;
5223 return NULL_TREE;
5226 /* Nonzero if we need to build up a constructor call when initializing an
5227 object of this class, either because it has a user-declared constructor
5228 or because it doesn't have a default constructor (so we need to give an
5229 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when
5230 what you care about is whether or not an object can be produced by a
5231 constructor (e.g. so we don't set TREE_READONLY on const variables of
5232 such type); use this function when what you care about is whether or not
5233 to try to call a constructor to create an object. The latter case is
5234 the former plus some cases of constructors that cannot be called. */
5236 bool
5237 type_build_ctor_call (tree t)
5239 tree inner;
5240 if (TYPE_NEEDS_CONSTRUCTING (t))
5241 return true;
5242 inner = strip_array_types (t);
5243 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner))
5244 return false;
5245 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner))
5246 return true;
5247 if (cxx_dialect < cxx11)
5248 return false;
5249 /* A user-declared constructor might be private, and a constructor might
5250 be trivial but deleted. */
5251 for (ovl_iterator iter (get_class_binding (inner, complete_ctor_identifier));
5252 iter; ++iter)
5254 tree fn = *iter;
5255 if (!DECL_ARTIFICIAL (fn)
5256 || TREE_DEPRECATED (fn)
5257 || DECL_DELETED_FN (fn))
5258 return true;
5260 return false;
5263 /* Like type_build_ctor_call, but for destructors. */
5265 bool
5266 type_build_dtor_call (tree t)
5268 tree inner;
5269 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5270 return true;
5271 inner = strip_array_types (t);
5272 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)
5273 || !COMPLETE_TYPE_P (inner))
5274 return false;
5275 if (cxx_dialect < cxx11)
5276 return false;
5277 /* A user-declared destructor might be private, and a destructor might
5278 be trivial but deleted. */
5279 for (ovl_iterator iter (get_class_binding (inner, complete_dtor_identifier));
5280 iter; ++iter)
5282 tree fn = *iter;
5283 if (!DECL_ARTIFICIAL (fn)
5284 || TREE_DEPRECATED (fn)
5285 || DECL_DELETED_FN (fn))
5286 return true;
5288 return false;
5291 /* Remove all zero-width bit-fields from T. */
5293 static void
5294 remove_zero_width_bit_fields (tree t)
5296 tree *fieldsp;
5298 fieldsp = &TYPE_FIELDS (t);
5299 while (*fieldsp)
5301 if (TREE_CODE (*fieldsp) == FIELD_DECL
5302 && DECL_C_BIT_FIELD (*fieldsp)
5303 /* We should not be confused by the fact that grokbitfield
5304 temporarily sets the width of the bit field into
5305 DECL_BIT_FIELD_REPRESENTATIVE (*fieldsp).
5306 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp)
5307 to that width. */
5308 && (DECL_SIZE (*fieldsp) == NULL_TREE
5309 || integer_zerop (DECL_SIZE (*fieldsp))))
5310 *fieldsp = DECL_CHAIN (*fieldsp);
5311 else
5312 fieldsp = &DECL_CHAIN (*fieldsp);
5316 /* Returns TRUE iff we need a cookie when dynamically allocating an
5317 array whose elements have the indicated class TYPE. */
5319 static bool
5320 type_requires_array_cookie (tree type)
5322 tree fns;
5323 bool has_two_argument_delete_p = false;
5325 gcc_assert (CLASS_TYPE_P (type));
5327 /* If there's a non-trivial destructor, we need a cookie. In order
5328 to iterate through the array calling the destructor for each
5329 element, we'll have to know how many elements there are. */
5330 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5331 return true;
5333 /* If the usual deallocation function is a two-argument whose second
5334 argument is of type `size_t', then we have to pass the size of
5335 the array to the deallocation function, so we will need to store
5336 a cookie. */
5337 fns = lookup_fnfields (TYPE_BINFO (type),
5338 ovl_op_identifier (false, VEC_DELETE_EXPR),
5339 /*protect=*/0);
5340 /* If there are no `operator []' members, or the lookup is
5341 ambiguous, then we don't need a cookie. */
5342 if (!fns || fns == error_mark_node)
5343 return false;
5344 /* Loop through all of the functions. */
5345 for (lkp_iterator iter (BASELINK_FUNCTIONS (fns)); iter; ++iter)
5347 tree fn = *iter;
5349 /* See if this function is a one-argument delete function. If
5350 it is, then it will be the usual deallocation function. */
5351 tree second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
5352 if (second_parm == void_list_node)
5353 return false;
5354 /* Do not consider this function if its second argument is an
5355 ellipsis. */
5356 if (!second_parm)
5357 continue;
5358 /* Otherwise, if we have a two-argument function and the second
5359 argument is `size_t', it will be the usual deallocation
5360 function -- unless there is one-argument function, too. */
5361 if (TREE_CHAIN (second_parm) == void_list_node
5362 && same_type_p (TREE_VALUE (second_parm), size_type_node))
5363 has_two_argument_delete_p = true;
5366 return has_two_argument_delete_p;
5369 /* Finish computing the `literal type' property of class type T.
5371 At this point, we have already processed base classes and
5372 non-static data members. We need to check whether the copy
5373 constructor is trivial, the destructor is trivial, and there
5374 is a trivial default constructor or at least one constexpr
5375 constructor other than the copy constructor. */
5377 static void
5378 finalize_literal_type_property (tree t)
5380 tree fn;
5382 if (cxx_dialect < cxx11
5383 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5384 CLASSTYPE_LITERAL_P (t) = false;
5385 else if (CLASSTYPE_LITERAL_P (t) && LAMBDA_TYPE_P (t))
5386 CLASSTYPE_LITERAL_P (t) = (cxx_dialect >= cxx17);
5387 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t)
5388 && CLASSTYPE_NON_AGGREGATE (t)
5389 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5390 CLASSTYPE_LITERAL_P (t) = false;
5392 /* C++14 DR 1684 removed this restriction. */
5393 if (cxx_dialect < cxx14
5394 && !CLASSTYPE_LITERAL_P (t) && !LAMBDA_TYPE_P (t))
5395 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5396 if (TREE_CODE (fn) == FUNCTION_DECL
5397 && DECL_DECLARED_CONSTEXPR_P (fn)
5398 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
5399 && !DECL_CONSTRUCTOR_P (fn))
5401 DECL_DECLARED_CONSTEXPR_P (fn) = false;
5402 if (!DECL_GENERATED_P (fn)
5403 && pedwarn (DECL_SOURCE_LOCATION (fn), OPT_Wpedantic,
5404 "enclosing class of %<constexpr%> non-static member "
5405 "function %q+#D is not a literal type", fn))
5406 explain_non_literal_class (t);
5410 /* T is a non-literal type used in a context which requires a constant
5411 expression. Explain why it isn't literal. */
5413 void
5414 explain_non_literal_class (tree t)
5416 static hash_set<tree> *diagnosed;
5418 if (!CLASS_TYPE_P (t))
5419 return;
5420 t = TYPE_MAIN_VARIANT (t);
5422 if (diagnosed == NULL)
5423 diagnosed = new hash_set<tree>;
5424 if (diagnosed->add (t))
5425 /* Already explained. */
5426 return;
5428 inform (UNKNOWN_LOCATION, "%q+T is not literal because:", t);
5429 if (cxx_dialect < cxx17 && LAMBDA_TYPE_P (t))
5430 inform (UNKNOWN_LOCATION,
5431 " %qT is a closure type, which is only literal in "
5432 "C++17 and later", t);
5433 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
5434 inform (UNKNOWN_LOCATION, " %q+T has a non-trivial destructor", t);
5435 else if (CLASSTYPE_NON_AGGREGATE (t)
5436 && !TYPE_HAS_TRIVIAL_DFLT (t)
5437 && !LAMBDA_TYPE_P (t)
5438 && !TYPE_HAS_CONSTEXPR_CTOR (t))
5440 inform (UNKNOWN_LOCATION,
5441 " %q+T is not an aggregate, does not have a trivial "
5442 "default constructor, and has no %<constexpr%> constructor that "
5443 "is not a copy or move constructor", t);
5444 if (type_has_non_user_provided_default_constructor (t))
5445 /* Note that we can't simply call locate_ctor because when the
5446 constructor is deleted it just returns NULL_TREE. */
5447 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
5449 tree fn = *iter;
5450 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
5452 parms = skip_artificial_parms_for (fn, parms);
5454 if (sufficient_parms_p (parms))
5456 if (DECL_DELETED_FN (fn))
5457 maybe_explain_implicit_delete (fn);
5458 else
5459 explain_invalid_constexpr_fn (fn);
5460 break;
5464 else
5466 tree binfo, base_binfo, field; int i;
5467 for (binfo = TYPE_BINFO (t), i = 0;
5468 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5470 tree basetype = TREE_TYPE (base_binfo);
5471 if (!CLASSTYPE_LITERAL_P (basetype))
5473 inform (UNKNOWN_LOCATION,
5474 " base class %qT of %q+T is non-literal",
5475 basetype, t);
5476 explain_non_literal_class (basetype);
5477 return;
5480 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5482 tree ftype;
5483 if (TREE_CODE (field) != FIELD_DECL)
5484 continue;
5485 ftype = TREE_TYPE (field);
5486 if (!literal_type_p (ftype))
5488 inform (DECL_SOURCE_LOCATION (field),
5489 " non-static data member %qD has non-literal type",
5490 field);
5491 if (CLASS_TYPE_P (ftype))
5492 explain_non_literal_class (ftype);
5494 if (CP_TYPE_VOLATILE_P (ftype))
5495 inform (DECL_SOURCE_LOCATION (field),
5496 " non-static data member %qD has volatile type", field);
5501 /* Check the validity of the bases and members declared in T. Add any
5502 implicitly-generated functions (like copy-constructors and
5503 assignment operators). Compute various flag bits (like
5504 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++
5505 level: i.e., independently of the ABI in use. */
5507 static void
5508 check_bases_and_members (tree t)
5510 /* Nonzero if the implicitly generated copy constructor should take
5511 a non-const reference argument. */
5512 int cant_have_const_ctor;
5513 /* Nonzero if the implicitly generated assignment operator
5514 should take a non-const reference argument. */
5515 int no_const_asn_ref;
5516 tree access_decls;
5517 bool saved_complex_asn_ref;
5518 bool saved_nontrivial_dtor;
5519 tree fn;
5521 /* By default, we use const reference arguments and generate default
5522 constructors. */
5523 cant_have_const_ctor = 0;
5524 no_const_asn_ref = 0;
5526 /* Check all the base-classes and set FMEM members to point to arrays
5527 of potential interest. */
5528 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref);
5530 /* Deduce noexcept on destructor. This needs to happen after we've set
5531 triviality flags appropriately for our bases. */
5532 if (cxx_dialect >= cxx11)
5533 if (tree dtor = CLASSTYPE_DESTRUCTOR (t))
5534 deduce_noexcept_on_destructor (dtor);
5536 /* Check all the method declarations. */
5537 check_methods (t);
5539 /* Save the initial values of these flags which only indicate whether
5540 or not the class has user-provided functions. As we analyze the
5541 bases and members we can set these flags for other reasons. */
5542 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t);
5543 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
5545 /* Check all the data member declarations. We cannot call
5546 check_field_decls until we have called check_bases check_methods,
5547 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5548 being set appropriately. */
5549 check_field_decls (t, &access_decls,
5550 &cant_have_const_ctor,
5551 &no_const_asn_ref);
5553 /* A nearly-empty class has to be vptr-containing; a nearly empty
5554 class contains just a vptr. */
5555 if (!TYPE_CONTAINS_VPTR_P (t))
5556 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
5558 /* Do some bookkeeping that will guide the generation of implicitly
5559 declared member functions. */
5560 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5561 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t);
5562 /* We need to call a constructor for this class if it has a
5563 user-provided constructor, or if the default constructor is going
5564 to initialize the vptr. (This is not an if-and-only-if;
5565 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
5566 themselves need constructing.) */
5567 TYPE_NEEDS_CONSTRUCTING (t)
5568 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
5569 /* [dcl.init.aggr]
5571 An aggregate is an array or a class with no user-provided
5572 constructors ... and no virtual functions.
5574 Again, other conditions for being an aggregate are checked
5575 elsewhere. */
5576 CLASSTYPE_NON_AGGREGATE (t)
5577 |= ((cxx_dialect < cxx2a
5578 ? type_has_user_provided_or_explicit_constructor (t)
5579 : TYPE_HAS_USER_CONSTRUCTOR (t))
5580 || TYPE_POLYMORPHIC_P (t));
5581 /* This is the C++98/03 definition of POD; it changed in C++0x, but we
5582 retain the old definition internally for ABI reasons. */
5583 CLASSTYPE_NON_LAYOUT_POD_P (t)
5584 |= (CLASSTYPE_NON_AGGREGATE (t)
5585 || saved_nontrivial_dtor || saved_complex_asn_ref);
5586 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t);
5587 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5588 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t);
5589 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
5591 /* If the only explicitly declared default constructor is user-provided,
5592 set TYPE_HAS_COMPLEX_DFLT. */
5593 if (!TYPE_HAS_COMPLEX_DFLT (t)
5594 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
5595 && !type_has_non_user_provided_default_constructor (t))
5596 TYPE_HAS_COMPLEX_DFLT (t) = true;
5598 /* Warn if a public base of a polymorphic type has an accessible
5599 non-virtual destructor. It is only now that we know the class is
5600 polymorphic. Although a polymorphic base will have a already
5601 been diagnosed during its definition, we warn on use too. */
5602 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor)
5604 tree binfo = TYPE_BINFO (t);
5605 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
5606 tree base_binfo;
5607 unsigned i;
5609 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5611 tree basetype = TREE_TYPE (base_binfo);
5613 if ((*accesses)[i] == access_public_node
5614 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp)
5615 && accessible_nvdtor_p (basetype))
5616 warning (OPT_Wnon_virtual_dtor,
5617 "base class %q#T has accessible non-virtual destructor",
5618 basetype);
5622 /* If the class has no user-declared constructor, but does have
5623 non-static const or reference data members that can never be
5624 initialized, issue a warning. */
5625 if (warn_uninitialized
5626 /* Classes with user-declared constructors are presumed to
5627 initialize these members. */
5628 && !TYPE_HAS_USER_CONSTRUCTOR (t)
5629 /* Aggregates can be initialized with brace-enclosed
5630 initializers. */
5631 && CLASSTYPE_NON_AGGREGATE (t))
5633 tree field;
5635 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
5637 tree type;
5639 if (TREE_CODE (field) != FIELD_DECL
5640 || DECL_INITIAL (field) != NULL_TREE)
5641 continue;
5643 type = TREE_TYPE (field);
5644 if (TYPE_REF_P (type))
5645 warning_at (DECL_SOURCE_LOCATION (field),
5646 OPT_Wuninitialized, "non-static reference %q#D "
5647 "in class without a constructor", field);
5648 else if (CP_TYPE_CONST_P (type)
5649 && (!CLASS_TYPE_P (type)
5650 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
5651 warning_at (DECL_SOURCE_LOCATION (field),
5652 OPT_Wuninitialized, "non-static const member %q#D "
5653 "in class without a constructor", field);
5657 /* Synthesize any needed methods. */
5658 add_implicitly_declared_members (t, &access_decls,
5659 cant_have_const_ctor,
5660 no_const_asn_ref);
5662 /* Check defaulted declarations here so we have cant_have_const_ctor
5663 and don't need to worry about clones. */
5664 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5665 if (DECL_DECLARES_FUNCTION_P (fn)
5666 && !DECL_ARTIFICIAL (fn)
5667 && DECL_DEFAULTED_IN_CLASS_P (fn))
5669 int copy = copy_fn_p (fn);
5670 if (copy > 0)
5672 bool imp_const_p
5673 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor
5674 : !no_const_asn_ref);
5675 bool fn_const_p = (copy == 2);
5677 if (fn_const_p && !imp_const_p)
5678 /* If the function is defaulted outside the class, we just
5679 give the synthesis error. Core Issue #1331 says this is
5680 no longer ill-formed, it is defined as deleted instead. */
5681 DECL_DELETED_FN (fn) = true;
5683 defaulted_late_check (fn);
5686 if (LAMBDA_TYPE_P (t))
5688 /* "This class type is not an aggregate." */
5689 CLASSTYPE_NON_AGGREGATE (t) = 1;
5692 /* Compute the 'literal type' property before we
5693 do anything with non-static member functions. */
5694 finalize_literal_type_property (t);
5696 /* Create the in-charge and not-in-charge variants of constructors
5697 and destructors. */
5698 clone_constructors_and_destructors (t);
5700 /* Process the using-declarations. */
5701 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
5702 handle_using_decl (TREE_VALUE (access_decls), t);
5704 /* Figure out whether or not we will need a cookie when dynamically
5705 allocating an array of this type. */
5706 LANG_TYPE_CLASS_CHECK (t)->vec_new_uses_cookie
5707 = type_requires_array_cookie (t);
5710 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
5711 accordingly. If a new vfield was created (because T doesn't have a
5712 primary base class), then the newly created field is returned. It
5713 is not added to the TYPE_FIELDS list; it is the caller's
5714 responsibility to do that. Accumulate declared virtual functions
5715 on VIRTUALS_P. */
5717 static tree
5718 create_vtable_ptr (tree t, tree* virtuals_p)
5720 tree fn;
5722 /* Collect the virtual functions declared in T. */
5723 for (fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
5724 if (TREE_CODE (fn) == FUNCTION_DECL
5725 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
5726 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
5728 tree new_virtual = make_node (TREE_LIST);
5730 BV_FN (new_virtual) = fn;
5731 BV_DELTA (new_virtual) = integer_zero_node;
5732 BV_VCALL_INDEX (new_virtual) = NULL_TREE;
5734 TREE_CHAIN (new_virtual) = *virtuals_p;
5735 *virtuals_p = new_virtual;
5738 /* If we couldn't find an appropriate base class, create a new field
5739 here. Even if there weren't any new virtual functions, we might need a
5740 new virtual function table if we're supposed to include vptrs in
5741 all classes that need them. */
5742 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
5744 /* We build this decl with vtbl_ptr_type_node, which is a
5745 `vtable_entry_type*'. It might seem more precise to use
5746 `vtable_entry_type (*)[N]' where N is the number of virtual
5747 functions. However, that would require the vtable pointer in
5748 base classes to have a different type than the vtable pointer
5749 in derived classes. We could make that happen, but that
5750 still wouldn't solve all the problems. In particular, the
5751 type-based alias analysis code would decide that assignments
5752 to the base class vtable pointer can't alias assignments to
5753 the derived class vtable pointer, since they have different
5754 types. Thus, in a derived class destructor, where the base
5755 class constructor was inlined, we could generate bad code for
5756 setting up the vtable pointer.
5758 Therefore, we use one type for all vtable pointers. We still
5759 use a type-correct type; it's just doesn't indicate the array
5760 bounds. That's better than using `void*' or some such; it's
5761 cleaner, and it let's the alias analysis code know that these
5762 stores cannot alias stores to void*! */
5763 tree field;
5765 field = build_decl (input_location,
5766 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
5767 DECL_VIRTUAL_P (field) = 1;
5768 DECL_ARTIFICIAL (field) = 1;
5769 DECL_FIELD_CONTEXT (field) = t;
5770 DECL_FCONTEXT (field) = t;
5771 if (TYPE_PACKED (t))
5772 DECL_PACKED (field) = 1;
5774 TYPE_VFIELD (t) = field;
5776 /* This class is non-empty. */
5777 CLASSTYPE_EMPTY_P (t) = 0;
5779 return field;
5782 return NULL_TREE;
5785 /* Add OFFSET to all base types of BINFO which is a base in the
5786 hierarchy dominated by T.
5788 OFFSET, which is a type offset, is number of bytes. */
5790 static void
5791 propagate_binfo_offsets (tree binfo, tree offset)
5793 int i;
5794 tree primary_binfo;
5795 tree base_binfo;
5797 /* Update BINFO's offset. */
5798 BINFO_OFFSET (binfo)
5799 = fold_convert (sizetype,
5800 size_binop (PLUS_EXPR,
5801 fold_convert (ssizetype, BINFO_OFFSET (binfo)),
5802 offset));
5804 /* Find the primary base class. */
5805 primary_binfo = get_primary_binfo (binfo);
5807 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
5808 propagate_binfo_offsets (primary_binfo, offset);
5810 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
5811 downwards. */
5812 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5814 /* Don't do the primary base twice. */
5815 if (base_binfo == primary_binfo)
5816 continue;
5818 if (BINFO_VIRTUAL_P (base_binfo))
5819 continue;
5821 propagate_binfo_offsets (base_binfo, offset);
5825 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
5826 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
5827 empty subobjects of T. */
5829 static void
5830 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
5832 tree vbase;
5833 tree t = rli->t;
5834 tree *next_field;
5836 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
5837 return;
5839 /* Find the last field. The artificial fields created for virtual
5840 bases will go after the last extant field to date. */
5841 next_field = &TYPE_FIELDS (t);
5842 while (*next_field)
5843 next_field = &DECL_CHAIN (*next_field);
5845 /* Go through the virtual bases, allocating space for each virtual
5846 base that is not already a primary base class. These are
5847 allocated in inheritance graph order. */
5848 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
5850 if (!BINFO_VIRTUAL_P (vbase))
5851 continue;
5853 if (!BINFO_PRIMARY_P (vbase))
5855 /* This virtual base is not a primary base of any class in the
5856 hierarchy, so we have to add space for it. */
5857 next_field = build_base_field (rli, vbase,
5858 offsets, next_field);
5863 /* Returns the offset of the byte just past the end of the base class
5864 BINFO. */
5866 static tree
5867 end_of_base (tree binfo)
5869 tree size;
5871 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
5872 size = TYPE_SIZE_UNIT (char_type_node);
5873 else if (is_empty_class (BINFO_TYPE (binfo)))
5874 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
5875 allocate some space for it. It cannot have virtual bases, so
5876 TYPE_SIZE_UNIT is fine. */
5877 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5878 else
5879 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
5881 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
5884 /* Returns the offset of the byte just past the end of the base class
5885 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
5886 only non-virtual bases are included. */
5888 static tree
5889 end_of_class (tree t, int include_virtuals_p)
5891 tree result = size_zero_node;
5892 vec<tree, va_gc> *vbases;
5893 tree binfo;
5894 tree base_binfo;
5895 tree offset;
5896 int i;
5898 for (binfo = TYPE_BINFO (t), i = 0;
5899 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5901 if (!include_virtuals_p
5902 && BINFO_VIRTUAL_P (base_binfo)
5903 && (!BINFO_PRIMARY_P (base_binfo)
5904 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
5905 continue;
5907 offset = end_of_base (base_binfo);
5908 if (tree_int_cst_lt (result, offset))
5909 result = offset;
5912 if (include_virtuals_p)
5913 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5914 vec_safe_iterate (vbases, i, &base_binfo); i++)
5916 offset = end_of_base (base_binfo);
5917 if (tree_int_cst_lt (result, offset))
5918 result = offset;
5921 return result;
5924 /* Warn about bases of T that are inaccessible because they are
5925 ambiguous. For example:
5927 struct S {};
5928 struct T : public S {};
5929 struct U : public S, public T {};
5931 Here, `(S*) new U' is not allowed because there are two `S'
5932 subobjects of U. */
5934 static void
5935 warn_about_ambiguous_bases (tree t)
5937 int i;
5938 vec<tree, va_gc> *vbases;
5939 tree basetype;
5940 tree binfo;
5941 tree base_binfo;
5943 /* If there are no repeated bases, nothing can be ambiguous. */
5944 if (!CLASSTYPE_REPEATED_BASE_P (t))
5945 return;
5947 /* Check direct bases. */
5948 for (binfo = TYPE_BINFO (t), i = 0;
5949 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
5951 basetype = BINFO_TYPE (base_binfo);
5953 if (!uniquely_derived_from_p (basetype, t))
5954 warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
5955 basetype, t);
5958 /* Check for ambiguous virtual bases. */
5959 if (extra_warnings)
5960 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
5961 vec_safe_iterate (vbases, i, &binfo); i++)
5963 basetype = BINFO_TYPE (binfo);
5965 if (!uniquely_derived_from_p (basetype, t))
5966 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due "
5967 "to ambiguity", basetype, t);
5971 /* Compare two INTEGER_CSTs K1 and K2. */
5973 static int
5974 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
5976 return tree_int_cst_compare ((tree) k1, (tree) k2);
5979 /* Increase the size indicated in RLI to account for empty classes
5980 that are "off the end" of the class. */
5982 static void
5983 include_empty_classes (record_layout_info rli)
5985 tree eoc;
5986 tree rli_size;
5988 /* It might be the case that we grew the class to allocate a
5989 zero-sized base class. That won't be reflected in RLI, yet,
5990 because we are willing to overlay multiple bases at the same
5991 offset. However, now we need to make sure that RLI is big enough
5992 to reflect the entire class. */
5993 eoc = end_of_class (rli->t, CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
5994 rli_size = rli_size_unit_so_far (rli);
5995 if (TREE_CODE (rli_size) == INTEGER_CST
5996 && tree_int_cst_lt (rli_size, eoc))
5998 /* The size should have been rounded to a whole byte. */
5999 gcc_assert (tree_int_cst_equal
6000 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
6001 rli->bitpos
6002 = size_binop (PLUS_EXPR,
6003 rli->bitpos,
6004 size_binop (MULT_EXPR,
6005 fold_convert (bitsizetype,
6006 size_binop (MINUS_EXPR,
6007 eoc, rli_size)),
6008 bitsize_int (BITS_PER_UNIT)));
6009 normalize_rli (rli);
6013 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
6014 BINFO_OFFSETs for all of the base-classes. Position the vtable
6015 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
6017 static void
6018 layout_class_type (tree t, tree *virtuals_p)
6020 tree non_static_data_members;
6021 tree field;
6022 tree vptr;
6023 record_layout_info rli;
6024 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
6025 types that appear at that offset. */
6026 splay_tree empty_base_offsets;
6027 /* True if the last field laid out was a bit-field. */
6028 bool last_field_was_bitfield = false;
6029 /* The location at which the next field should be inserted. */
6030 tree *next_field;
6032 /* Keep track of the first non-static data member. */
6033 non_static_data_members = TYPE_FIELDS (t);
6035 /* Start laying out the record. */
6036 rli = start_record_layout (t);
6038 /* Mark all the primary bases in the hierarchy. */
6039 determine_primary_bases (t);
6041 /* Create a pointer to our virtual function table. */
6042 vptr = create_vtable_ptr (t, virtuals_p);
6044 /* The vptr is always the first thing in the class. */
6045 if (vptr)
6047 DECL_CHAIN (vptr) = TYPE_FIELDS (t);
6048 TYPE_FIELDS (t) = vptr;
6049 next_field = &DECL_CHAIN (vptr);
6050 place_field (rli, vptr);
6052 else
6053 next_field = &TYPE_FIELDS (t);
6055 /* Build FIELD_DECLs for all of the non-virtual base-types. */
6056 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
6057 NULL, NULL);
6058 build_base_fields (rli, empty_base_offsets, next_field);
6060 /* Layout the non-static data members. */
6061 for (field = non_static_data_members; field; field = DECL_CHAIN (field))
6063 tree type;
6064 tree padding;
6066 /* We still pass things that aren't non-static data members to
6067 the back end, in case it wants to do something with them. */
6068 if (TREE_CODE (field) != FIELD_DECL)
6070 place_field (rli, field);
6071 /* If the static data member has incomplete type, keep track
6072 of it so that it can be completed later. (The handling
6073 of pending statics in finish_record_layout is
6074 insufficient; consider:
6076 struct S1;
6077 struct S2 { static S1 s1; };
6079 At this point, finish_record_layout will be called, but
6080 S1 is still incomplete.) */
6081 if (VAR_P (field))
6083 maybe_register_incomplete_var (field);
6084 /* The visibility of static data members is determined
6085 at their point of declaration, not their point of
6086 definition. */
6087 determine_visibility (field);
6089 continue;
6092 type = TREE_TYPE (field);
6093 if (type == error_mark_node)
6094 continue;
6096 padding = NULL_TREE;
6098 /* If this field is a bit-field whose width is greater than its
6099 type, then there are some special rules for allocating
6100 it. */
6101 if (DECL_C_BIT_FIELD (field)
6102 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field)))
6104 bool was_unnamed_p = false;
6105 /* We must allocate the bits as if suitably aligned for the
6106 longest integer type that fits in this many bits. Then,
6107 we are supposed to use the left over bits as additional
6108 padding. */
6110 /* Do not pick a type bigger than MAX_FIXED_MODE_SIZE. */
6111 tree limit = size_int (MAX_FIXED_MODE_SIZE);
6112 if (tree_int_cst_lt (DECL_SIZE (field), limit))
6113 limit = DECL_SIZE (field);
6115 tree integer_type = integer_types[itk_char];
6116 for (unsigned itk = itk_char; itk != itk_none; itk++)
6117 if (tree next = integer_types[itk])
6119 if (tree_int_cst_lt (limit, TYPE_SIZE (next)))
6120 /* Too big, so our current guess is what we want. */
6121 break;
6122 /* Not bigger than limit, ok */
6123 integer_type = next;
6126 /* Figure out how much additional padding is required. */
6127 if (TREE_CODE (t) == UNION_TYPE)
6128 /* In a union, the padding field must have the full width
6129 of the bit-field; all fields start at offset zero. */
6130 padding = DECL_SIZE (field);
6131 else
6132 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
6133 TYPE_SIZE (integer_type));
6135 if (integer_zerop (padding))
6136 padding = NULL_TREE;
6138 /* An unnamed bitfield does not normally affect the
6139 alignment of the containing class on a target where
6140 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
6141 make any exceptions for unnamed bitfields when the
6142 bitfields are longer than their types. Therefore, we
6143 temporarily give the field a name. */
6144 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
6146 was_unnamed_p = true;
6147 DECL_NAME (field) = make_anon_name ();
6150 DECL_SIZE (field) = TYPE_SIZE (integer_type);
6151 SET_DECL_ALIGN (field, TYPE_ALIGN (integer_type));
6152 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
6153 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6154 empty_base_offsets);
6155 if (was_unnamed_p)
6156 DECL_NAME (field) = NULL_TREE;
6157 /* Now that layout has been performed, set the size of the
6158 field to the size of its declared type; the rest of the
6159 field is effectively invisible. */
6160 DECL_SIZE (field) = TYPE_SIZE (type);
6161 /* We must also reset the DECL_MODE of the field. */
6162 SET_DECL_MODE (field, TYPE_MODE (type));
6164 else
6165 layout_nonempty_base_or_field (rli, field, NULL_TREE,
6166 empty_base_offsets);
6168 /* Remember the location of any empty classes in FIELD. */
6169 record_subobject_offsets (TREE_TYPE (field),
6170 byte_position(field),
6171 empty_base_offsets,
6172 /*is_data_member=*/true);
6174 /* If a bit-field does not immediately follow another bit-field,
6175 and yet it starts in the middle of a byte, we have failed to
6176 comply with the ABI. */
6177 if (warn_abi
6178 && DECL_C_BIT_FIELD (field)
6179 /* The TREE_NO_WARNING flag gets set by Objective-C when
6180 laying out an Objective-C class. The ObjC ABI differs
6181 from the C++ ABI, and so we do not want a warning
6182 here. */
6183 && !TREE_NO_WARNING (field)
6184 && !last_field_was_bitfield
6185 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
6186 DECL_FIELD_BIT_OFFSET (field),
6187 bitsize_unit_node)))
6188 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi,
6189 "offset of %qD is not ABI-compliant and may "
6190 "change in a future version of GCC", field);
6192 /* The middle end uses the type of expressions to determine the
6193 possible range of expression values. In order to optimize
6194 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
6195 must be made aware of the width of "i", via its type.
6197 Because C++ does not have integer types of arbitrary width,
6198 we must (for the purposes of the front end) convert from the
6199 type assigned here to the declared type of the bitfield
6200 whenever a bitfield expression is used as an rvalue.
6201 Similarly, when assigning a value to a bitfield, the value
6202 must be converted to the type given the bitfield here. */
6203 if (DECL_C_BIT_FIELD (field))
6205 unsigned HOST_WIDE_INT width;
6206 tree ftype = TREE_TYPE (field);
6207 width = tree_to_uhwi (DECL_SIZE (field));
6208 if (width != TYPE_PRECISION (ftype))
6210 TREE_TYPE (field)
6211 = c_build_bitfield_integer_type (width,
6212 TYPE_UNSIGNED (ftype));
6213 TREE_TYPE (field)
6214 = cp_build_qualified_type (TREE_TYPE (field),
6215 cp_type_quals (ftype));
6219 /* If we needed additional padding after this field, add it
6220 now. */
6221 if (padding)
6223 tree padding_field;
6225 padding_field = build_decl (input_location,
6226 FIELD_DECL,
6227 NULL_TREE,
6228 char_type_node);
6229 DECL_BIT_FIELD (padding_field) = 1;
6230 DECL_SIZE (padding_field) = padding;
6231 DECL_CONTEXT (padding_field) = t;
6232 DECL_ARTIFICIAL (padding_field) = 1;
6233 DECL_IGNORED_P (padding_field) = 1;
6234 DECL_PADDING_P (padding_field) = 1;
6235 layout_nonempty_base_or_field (rli, padding_field,
6236 NULL_TREE,
6237 empty_base_offsets);
6240 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
6243 if (!integer_zerop (rli->bitpos))
6245 /* Make sure that we are on a byte boundary so that the size of
6246 the class without virtual bases will always be a round number
6247 of bytes. */
6248 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT);
6249 normalize_rli (rli);
6252 /* Delete all zero-width bit-fields from the list of fields. Now
6253 that the type is laid out they are no longer important. */
6254 remove_zero_width_bit_fields (t);
6256 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t))
6258 /* T needs a different layout as a base (eliding virtual bases
6259 or whatever). Create that version. */
6260 tree base_t = make_node (TREE_CODE (t));
6262 /* If the ABI version is not at least two, and the last
6263 field was a bit-field, RLI may not be on a byte
6264 boundary. In particular, rli_size_unit_so_far might
6265 indicate the last complete byte, while rli_size_so_far
6266 indicates the total number of bits used. Therefore,
6267 rli_size_so_far, rather than rli_size_unit_so_far, is
6268 used to compute TYPE_SIZE_UNIT. */
6269 tree eoc = end_of_class (t, /*include_virtuals_p=*/0);
6270 TYPE_SIZE_UNIT (base_t)
6271 = size_binop (MAX_EXPR,
6272 fold_convert (sizetype,
6273 size_binop (CEIL_DIV_EXPR,
6274 rli_size_so_far (rli),
6275 bitsize_int (BITS_PER_UNIT))),
6276 eoc);
6277 TYPE_SIZE (base_t)
6278 = size_binop (MAX_EXPR,
6279 rli_size_so_far (rli),
6280 size_binop (MULT_EXPR,
6281 fold_convert (bitsizetype, eoc),
6282 bitsize_int (BITS_PER_UNIT)));
6283 SET_TYPE_ALIGN (base_t, rli->record_align);
6284 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
6285 TYPE_TYPELESS_STORAGE (base_t) = TYPE_TYPELESS_STORAGE (t);
6287 /* Copy the non-static data members of T. This will include its
6288 direct non-virtual bases & vtable. */
6289 next_field = &TYPE_FIELDS (base_t);
6290 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6291 if (TREE_CODE (field) == FIELD_DECL)
6293 *next_field = copy_node (field);
6294 DECL_CONTEXT (*next_field) = base_t;
6295 next_field = &DECL_CHAIN (*next_field);
6297 *next_field = NULL_TREE;
6299 /* We use the base type for trivial assignments, and hence it
6300 needs a mode. */
6301 compute_record_mode (base_t);
6303 TYPE_CONTEXT (base_t) = t;
6305 /* Record the base version of the type. */
6306 CLASSTYPE_AS_BASE (t) = base_t;
6308 else
6309 CLASSTYPE_AS_BASE (t) = t;
6311 /* Every empty class contains an empty class. */
6312 if (CLASSTYPE_EMPTY_P (t))
6313 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
6315 /* Set the TYPE_DECL for this type to contain the right
6316 value for DECL_OFFSET, so that we can use it as part
6317 of a COMPONENT_REF for multiple inheritance. */
6318 layout_decl (TYPE_MAIN_DECL (t), 0);
6320 /* Now fix up any virtual base class types that we left lying
6321 around. We must get these done before we try to lay out the
6322 virtual function table. As a side-effect, this will remove the
6323 base subobject fields. */
6324 layout_virtual_bases (rli, empty_base_offsets);
6326 /* Make sure that empty classes are reflected in RLI at this
6327 point. */
6328 include_empty_classes (rli);
6330 /* Make sure not to create any structures with zero size. */
6331 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
6332 place_field (rli,
6333 build_decl (input_location,
6334 FIELD_DECL, NULL_TREE, char_type_node));
6336 /* If this is a non-POD, declaring it packed makes a difference to how it
6337 can be used as a field; don't let finalize_record_size undo it. */
6338 if (TYPE_PACKED (t) && !layout_pod_type_p (t))
6339 rli->packed_maybe_necessary = true;
6341 /* Let the back end lay out the type. */
6342 finish_record_layout (rli, /*free_p=*/true);
6344 if (TYPE_SIZE_UNIT (t)
6345 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
6346 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
6347 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
6348 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t));
6350 /* Warn about bases that can't be talked about due to ambiguity. */
6351 warn_about_ambiguous_bases (t);
6353 /* Now that we're done with layout, give the base fields the real types. */
6354 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
6355 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
6356 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
6358 /* Clean up. */
6359 splay_tree_delete (empty_base_offsets);
6361 if (CLASSTYPE_EMPTY_P (t)
6362 && tree_int_cst_lt (sizeof_biggest_empty_class,
6363 TYPE_SIZE_UNIT (t)))
6364 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
6367 /* Determine the "key method" for the class type indicated by TYPE,
6368 and set CLASSTYPE_KEY_METHOD accordingly. */
6370 void
6371 determine_key_method (tree type)
6373 tree method;
6375 if (processing_template_decl
6376 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
6377 || CLASSTYPE_INTERFACE_KNOWN (type))
6378 return;
6380 /* The key method is the first non-pure virtual function that is not
6381 inline at the point of class definition. On some targets the
6382 key function may not be inline; those targets should not call
6383 this function until the end of the translation unit. */
6384 for (method = TYPE_FIELDS (type); method; method = DECL_CHAIN (method))
6385 if (TREE_CODE (method) == FUNCTION_DECL
6386 && DECL_VINDEX (method) != NULL_TREE
6387 && ! DECL_DECLARED_INLINE_P (method)
6388 && ! DECL_PURE_VIRTUAL_P (method))
6390 CLASSTYPE_KEY_METHOD (type) = method;
6391 break;
6394 return;
6397 /* Helper of find_flexarrays. Return true when FLD refers to a non-static
6398 class data member of non-zero size, otherwise false. */
6400 static inline bool
6401 field_nonempty_p (const_tree fld)
6403 if (TREE_CODE (fld) == ERROR_MARK)
6404 return false;
6406 tree type = TREE_TYPE (fld);
6407 if (TREE_CODE (fld) == FIELD_DECL
6408 && TREE_CODE (type) != ERROR_MARK
6409 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type)))
6411 return TYPE_SIZE (type)
6412 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6413 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));
6416 return false;
6419 /* Used by find_flexarrays and related functions. */
6421 struct flexmems_t
6423 /* The first flexible array member or non-zero array member found
6424 in the order of layout. */
6425 tree array;
6426 /* First non-static non-empty data member in the class or its bases. */
6427 tree first;
6428 /* The first non-static non-empty data member following either
6429 the flexible array member, if found, or the zero-length array member
6430 otherwise. AFTER[1] refers to the first such data member of a union
6431 of which the struct containing the flexible array member or zero-length
6432 array is a member, or NULL when no such union exists. This element is
6433 only used during searching, not for diagnosing problems. AFTER[0]
6434 refers to the first such data member that is not a member of such
6435 a union. */
6436 tree after[2];
6438 /* Refers to a struct (not union) in which the struct of which the flexible
6439 array is member is defined. Used to diagnose strictly (according to C)
6440 invalid uses of the latter structs. */
6441 tree enclosing;
6444 /* Find either the first flexible array member or the first zero-length
6445 array, in that order of preference, among members of class T (but not
6446 its base classes), and set members of FMEM accordingly.
6447 BASE_P is true if T is a base class of another class.
6448 PUN is set to the outermost union in which the flexible array member
6449 (or zero-length array) is defined if one such union exists, otherwise
6450 to NULL.
6451 Similarly, PSTR is set to a data member of the outermost struct of
6452 which the flexible array is a member if one such struct exists,
6453 otherwise to NULL. */
6455 static void
6456 find_flexarrays (tree t, flexmems_t *fmem, bool base_p,
6457 tree pun /* = NULL_TREE */,
6458 tree pstr /* = NULL_TREE */)
6460 /* Set the "pointer" to the outermost enclosing union if not set
6461 yet and maintain it for the remainder of the recursion. */
6462 if (!pun && TREE_CODE (t) == UNION_TYPE)
6463 pun = t;
6465 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
6467 if (fld == error_mark_node)
6468 return;
6470 /* Is FLD a typedef for an anonymous struct? */
6472 /* FIXME: Note that typedefs (as well as arrays) need to be fully
6473 handled elsewhere so that errors like the following are detected
6474 as well:
6475 typedef struct { int i, a[], j; } S; // bug c++/72753
6476 S s [2]; // bug c++/68489
6478 if (TREE_CODE (fld) == TYPE_DECL
6479 && DECL_IMPLICIT_TYPEDEF_P (fld)
6480 && CLASS_TYPE_P (TREE_TYPE (fld))
6481 && anon_aggrname_p (DECL_NAME (fld)))
6483 /* Check the nested unnamed type referenced via a typedef
6484 independently of FMEM (since it's not a data member of
6485 the enclosing class). */
6486 check_flexarrays (TREE_TYPE (fld));
6487 continue;
6490 /* Skip anything that's GCC-generated or not a (non-static) data
6491 member. */
6492 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL)
6493 continue;
6495 /* Type of the member. */
6496 tree fldtype = TREE_TYPE (fld);
6497 if (fldtype == error_mark_node)
6498 return;
6500 /* Determine the type of the array element or object referenced
6501 by the member so that it can be checked for flexible array
6502 members if it hasn't been yet. */
6503 tree eltype = fldtype;
6504 while (TREE_CODE (eltype) == ARRAY_TYPE
6505 || INDIRECT_TYPE_P (eltype))
6506 eltype = TREE_TYPE (eltype);
6508 if (RECORD_OR_UNION_TYPE_P (eltype))
6510 if (fmem->array && !fmem->after[bool (pun)])
6512 /* Once the member after the flexible array has been found
6513 we're done. */
6514 fmem->after[bool (pun)] = fld;
6515 break;
6518 if (eltype == fldtype || TYPE_UNNAMED_P (eltype))
6520 /* Descend into the non-static member struct or union and try
6521 to find a flexible array member or zero-length array among
6522 its members. This is only necessary for anonymous types
6523 and types in whose context the current type T has not been
6524 defined (the latter must not be checked again because they
6525 are already in the process of being checked by one of the
6526 recursive calls). */
6528 tree first = fmem->first;
6529 tree array = fmem->array;
6531 /* If this member isn't anonymous and a prior non-flexible array
6532 member has been seen in one of the enclosing structs, clear
6533 the FIRST member since it doesn't contribute to the flexible
6534 array struct's members. */
6535 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6536 fmem->first = NULL_TREE;
6538 find_flexarrays (eltype, fmem, false, pun,
6539 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr);
6541 if (fmem->array != array)
6542 continue;
6544 if (first && !array && !ANON_AGGR_TYPE_P (eltype))
6546 /* Restore the FIRST member reset above if no flexible
6547 array member has been found in this member's struct. */
6548 fmem->first = first;
6551 /* If the member struct contains the first flexible array
6552 member, or if this member is a base class, continue to
6553 the next member and avoid setting the FMEM->NEXT pointer
6554 to point to it. */
6555 if (base_p)
6556 continue;
6560 if (field_nonempty_p (fld))
6562 /* Remember the first non-static data member. */
6563 if (!fmem->first)
6564 fmem->first = fld;
6566 /* Remember the first non-static data member after the flexible
6567 array member, if one has been found, or the zero-length array
6568 if it has been found. */
6569 if (fmem->array && !fmem->after[bool (pun)])
6570 fmem->after[bool (pun)] = fld;
6573 /* Skip non-arrays. */
6574 if (TREE_CODE (fldtype) != ARRAY_TYPE)
6575 continue;
6577 /* Determine the upper bound of the array if it has one. */
6578 if (TYPE_DOMAIN (fldtype))
6580 if (fmem->array)
6582 /* Make a record of the zero-length array if either one
6583 such field or a flexible array member has been seen to
6584 handle the pathological and unlikely case of multiple
6585 such members. */
6586 if (!fmem->after[bool (pun)])
6587 fmem->after[bool (pun)] = fld;
6589 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype))))
6591 /* Remember the first zero-length array unless a flexible array
6592 member has already been seen. */
6593 fmem->array = fld;
6594 fmem->enclosing = pstr;
6597 else
6599 /* Flexible array members have no upper bound. */
6600 if (fmem->array)
6602 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6604 /* Replace the zero-length array if it's been stored and
6605 reset the after pointer. */
6606 fmem->after[bool (pun)] = NULL_TREE;
6607 fmem->array = fld;
6608 fmem->enclosing = pstr;
6610 else if (!fmem->after[bool (pun)])
6611 /* Make a record of another flexible array member. */
6612 fmem->after[bool (pun)] = fld;
6614 else
6616 fmem->array = fld;
6617 fmem->enclosing = pstr;
6623 /* Diagnose a strictly (by the C standard) invalid use of a struct with
6624 a flexible array member (or the zero-length array extension). */
6626 static void
6627 diagnose_invalid_flexarray (const flexmems_t *fmem)
6629 if (fmem->array && fmem->enclosing
6630 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic,
6631 TYPE_DOMAIN (TREE_TYPE (fmem->array))
6632 ? G_("invalid use of %q#T with a zero-size array "
6633 "in %q#D")
6634 : G_("invalid use of %q#T with a flexible array member "
6635 "in %q#T"),
6636 DECL_CONTEXT (fmem->array),
6637 DECL_CONTEXT (fmem->enclosing)))
6638 inform (DECL_SOURCE_LOCATION (fmem->array),
6639 "array member %q#D declared here", fmem->array);
6642 /* Issue diagnostics for invalid flexible array members or zero-length
6643 arrays that are not the last elements of the containing class or its
6644 base classes or that are its sole members. */
6646 static void
6647 diagnose_flexarrays (tree t, const flexmems_t *fmem)
6649 if (!fmem->array)
6650 return;
6652 if (fmem->first && !fmem->after[0])
6654 diagnose_invalid_flexarray (fmem);
6655 return;
6658 /* Has a diagnostic been issued? */
6659 bool diagd = false;
6661 const char *msg = 0;
6663 if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
6665 if (fmem->after[0])
6666 msg = G_("zero-size array member %qD not at end of %q#T");
6667 else if (!fmem->first)
6668 msg = G_("zero-size array member %qD in an otherwise empty %q#T");
6670 if (msg)
6672 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6674 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t))
6676 inform (location_of (t), "in the definition of %q#T", t);
6677 diagd = true;
6681 else
6683 if (fmem->after[0])
6684 msg = G_("flexible array member %qD not at end of %q#T");
6685 else if (!fmem->first)
6686 msg = G_("flexible array member %qD in an otherwise empty %q#T");
6688 if (msg)
6690 location_t loc = DECL_SOURCE_LOCATION (fmem->array);
6691 diagd = true;
6693 error_at (loc, msg, fmem->array, t);
6695 /* In the unlikely event that the member following the flexible
6696 array member is declared in a different class, or the member
6697 overlaps another member of a common union, point to it.
6698 Otherwise it should be obvious. */
6699 if (fmem->after[0]
6700 && ((DECL_CONTEXT (fmem->after[0])
6701 != DECL_CONTEXT (fmem->array))))
6703 inform (DECL_SOURCE_LOCATION (fmem->after[0]),
6704 "next member %q#D declared here",
6705 fmem->after[0]);
6706 inform (location_of (t), "in the definition of %q#T", t);
6711 if (!diagd && fmem->array && fmem->enclosing)
6712 diagnose_invalid_flexarray (fmem);
6716 /* Recursively check to make sure that any flexible array or zero-length
6717 array members of class T or its bases are valid (i.e., not the sole
6718 non-static data member of T and, if one exists, that it is the last
6719 non-static data member of T and its base classes. FMEM is expected
6720 to be initially null and is used internally by recursive calls to
6721 the function. Issue the appropriate diagnostics for the array member
6722 that fails the checks. */
6724 static void
6725 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */,
6726 bool base_p /* = false */)
6728 /* Initialize the result of a search for flexible array and zero-length
6729 array members. Avoid doing any work if the most interesting FMEM data
6730 have already been populated. */
6731 flexmems_t flexmems = flexmems_t ();
6732 if (!fmem)
6733 fmem = &flexmems;
6734 else if (fmem->array && fmem->first && fmem->after[0])
6735 return;
6737 tree fam = fmem->array;
6739 /* Recursively check the primary base class first. */
6740 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6742 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
6743 check_flexarrays (basetype, fmem, true);
6746 /* Recursively check the base classes. */
6747 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0;
6748 for (int i = 0; i < nbases; ++i)
6750 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
6752 /* The primary base class was already checked above. */
6753 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
6754 continue;
6756 /* Virtual base classes are at the end. */
6757 if (BINFO_VIRTUAL_P (base_binfo))
6758 continue;
6760 /* Check the base class. */
6761 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true);
6764 if (fmem == &flexmems)
6766 /* Check virtual base classes only once per derived class.
6767 I.e., this check is not performed recursively for base
6768 classes. */
6769 int i;
6770 tree base_binfo;
6771 vec<tree, va_gc> *vbases;
6772 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
6773 vec_safe_iterate (vbases, i, &base_binfo); i++)
6775 /* Check the virtual base class. */
6776 tree basetype = TREE_TYPE (base_binfo);
6778 check_flexarrays (basetype, fmem, /*base_p=*/true);
6782 /* Is the type unnamed (and therefore a member of it potentially
6783 an anonymous struct or union)? */
6784 bool maybe_anon_p = TYPE_UNNAMED_P (t);
6786 /* Search the members of the current (possibly derived) class, skipping
6787 unnamed structs and unions since those could be anonymous. */
6788 if (fmem != &flexmems || !maybe_anon_p)
6789 find_flexarrays (t, fmem, base_p || fam != fmem->array);
6791 if (fmem == &flexmems && !maybe_anon_p)
6793 /* Issue diagnostics for invalid flexible and zero-length array
6794 members found in base classes or among the members of the current
6795 class. Ignore anonymous structs and unions whose members are
6796 considered to be members of the enclosing class and thus will
6797 be diagnosed when checking it. */
6798 diagnose_flexarrays (t, fmem);
6802 /* Perform processing required when the definition of T (a class type)
6803 is complete. Diagnose invalid definitions of flexible array members
6804 and zero-size arrays. */
6806 void
6807 finish_struct_1 (tree t)
6809 tree x;
6810 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
6811 tree virtuals = NULL_TREE;
6813 if (COMPLETE_TYPE_P (t))
6815 gcc_assert (MAYBE_CLASS_TYPE_P (t));
6816 error ("redefinition of %q#T", t);
6817 popclass ();
6818 return;
6821 /* If this type was previously laid out as a forward reference,
6822 make sure we lay it out again. */
6823 TYPE_SIZE (t) = NULL_TREE;
6824 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
6826 /* Make assumptions about the class; we'll reset the flags if
6827 necessary. */
6828 CLASSTYPE_EMPTY_P (t) = 1;
6829 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
6830 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
6831 CLASSTYPE_LITERAL_P (t) = true;
6833 /* Do end-of-class semantic processing: checking the validity of the
6834 bases and members and add implicitly generated methods. */
6835 check_bases_and_members (t);
6837 /* Find the key method. */
6838 if (TYPE_CONTAINS_VPTR_P (t))
6840 /* The Itanium C++ ABI permits the key method to be chosen when
6841 the class is defined -- even though the key method so
6842 selected may later turn out to be an inline function. On
6843 some systems (such as ARM Symbian OS) the key method cannot
6844 be determined until the end of the translation unit. On such
6845 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
6846 will cause the class to be added to KEYED_CLASSES. Then, in
6847 finish_file we will determine the key method. */
6848 if (targetm.cxx.key_method_may_be_inline ())
6849 determine_key_method (t);
6851 /* If a polymorphic class has no key method, we may emit the vtable
6852 in every translation unit where the class definition appears. If
6853 we're devirtualizing, we can look into the vtable even if we
6854 aren't emitting it. */
6855 if (!CLASSTYPE_KEY_METHOD (t))
6856 vec_safe_push (keyed_classes, t);
6859 /* Layout the class itself. */
6860 layout_class_type (t, &virtuals);
6861 /* COMPLETE_TYPE_P is now true. */
6863 set_class_bindings (t);
6865 /* With the layout complete, check for flexible array members and
6866 zero-length arrays that might overlap other members in the final
6867 layout. */
6868 check_flexarrays (t);
6870 virtuals = modify_all_vtables (t, nreverse (virtuals));
6872 /* If necessary, create the primary vtable for this class. */
6873 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
6875 /* We must enter these virtuals into the table. */
6876 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6877 build_primary_vtable (NULL_TREE, t);
6878 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
6879 /* Here we know enough to change the type of our virtual
6880 function table, but we will wait until later this function. */
6881 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
6883 /* If we're warning about ABI tags, check the types of the new
6884 virtual functions. */
6885 if (warn_abi_tag)
6886 for (tree v = virtuals; v; v = TREE_CHAIN (v))
6887 check_abi_tags (t, TREE_VALUE (v));
6890 if (TYPE_CONTAINS_VPTR_P (t))
6892 int vindex;
6893 tree fn;
6895 if (BINFO_VTABLE (TYPE_BINFO (t)))
6896 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
6897 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
6898 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
6900 /* Add entries for virtual functions introduced by this class. */
6901 BINFO_VIRTUALS (TYPE_BINFO (t))
6902 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
6904 /* Set DECL_VINDEX for all functions declared in this class. */
6905 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
6907 fn = TREE_CHAIN (fn),
6908 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
6909 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
6911 tree fndecl = BV_FN (fn);
6913 if (DECL_THUNK_P (fndecl))
6914 /* A thunk. We should never be calling this entry directly
6915 from this vtable -- we'd use the entry for the non
6916 thunk base function. */
6917 DECL_VINDEX (fndecl) = NULL_TREE;
6918 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
6919 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
6923 finish_struct_bits (t);
6925 set_method_tm_attributes (t);
6926 if (flag_openmp || flag_openmp_simd)
6927 finish_omp_declare_simd_methods (t);
6929 /* Clear DECL_IN_AGGR_P for all member functions. Complete the rtl
6930 for any static member objects of the type we're working on. */
6931 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6932 if (DECL_DECLARES_FUNCTION_P (x))
6933 DECL_IN_AGGR_P (x) = false;
6934 else if (VAR_P (x) && TREE_STATIC (x)
6935 && TREE_TYPE (x) != error_mark_node
6936 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
6937 SET_DECL_MODE (x, TYPE_MODE (t));
6939 /* Complain if one of the field types requires lower visibility. */
6940 constrain_class_visibility (t);
6942 /* Make the rtl for any new vtables we have created, and unmark
6943 the base types we marked. */
6944 finish_vtbls (t);
6946 /* Build the VTT for T. */
6947 build_vtt (t);
6949 if (warn_nonvdtor
6950 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t)
6951 && !CLASSTYPE_FINAL (t))
6952 warning (OPT_Wnon_virtual_dtor,
6953 "%q#T has virtual functions and accessible"
6954 " non-virtual destructor", t);
6956 complete_vars (t);
6958 if (warn_overloaded_virtual)
6959 warn_hidden (t);
6961 /* Class layout, assignment of virtual table slots, etc., is now
6962 complete. Give the back end a chance to tweak the visibility of
6963 the class or perform any other required target modifications. */
6964 targetm.cxx.adjust_class_at_definition (t);
6966 maybe_suppress_debug_info (t);
6968 if (flag_vtable_verify)
6969 vtv_save_class_info (t);
6971 dump_class_hierarchy (t);
6973 /* Finish debugging output for this type. */
6974 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
6976 if (TYPE_TRANSPARENT_AGGR (t))
6978 tree field = first_field (t);
6979 if (field == NULL_TREE || error_operand_p (field))
6981 error ("type transparent %q#T does not have any fields", t);
6982 TYPE_TRANSPARENT_AGGR (t) = 0;
6984 else if (DECL_ARTIFICIAL (field))
6986 if (DECL_FIELD_IS_BASE (field))
6987 error ("type transparent class %qT has base classes", t);
6988 else
6990 gcc_checking_assert (DECL_VIRTUAL_P (field));
6991 error ("type transparent class %qT has virtual functions", t);
6993 TYPE_TRANSPARENT_AGGR (t) = 0;
6995 else if (TYPE_MODE (t) != DECL_MODE (field))
6997 error ("type transparent %q#T cannot be made transparent because "
6998 "the type of the first field has a different ABI from the "
6999 "class overall", t);
7000 TYPE_TRANSPARENT_AGGR (t) = 0;
7005 /* When T was built up, the member declarations were added in reverse
7006 order. Rearrange them to declaration order. */
7008 void
7009 unreverse_member_declarations (tree t)
7011 tree next;
7012 tree prev;
7013 tree x;
7015 /* The following lists are all in reverse order. Put them in
7016 declaration order now. */
7017 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
7019 /* For the TYPE_FIELDS, only the non TYPE_DECLs are in reverse
7020 order, so we can't just use nreverse. Due to stat_hack
7021 chicanery in finish_member_declaration. */
7022 prev = NULL_TREE;
7023 for (x = TYPE_FIELDS (t);
7024 x && TREE_CODE (x) != TYPE_DECL;
7025 x = next)
7027 next = DECL_CHAIN (x);
7028 DECL_CHAIN (x) = prev;
7029 prev = x;
7032 if (prev)
7034 DECL_CHAIN (TYPE_FIELDS (t)) = x;
7035 TYPE_FIELDS (t) = prev;
7039 tree
7040 finish_struct (tree t, tree attributes)
7042 location_t saved_loc = input_location;
7044 /* Now that we've got all the field declarations, reverse everything
7045 as necessary. */
7046 unreverse_member_declarations (t);
7048 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7049 fixup_attribute_variants (t);
7051 /* Nadger the current location so that diagnostics point to the start of
7052 the struct, not the end. */
7053 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
7055 if (processing_template_decl)
7057 tree x;
7059 /* We need to add the target functions of USING_DECLS, so that
7060 they can be found when the using declaration is not
7061 instantiated yet. */
7062 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7063 if (TREE_CODE (x) == USING_DECL)
7065 tree fn = strip_using_decl (x);
7066 if (OVL_P (fn))
7067 for (lkp_iterator iter (fn); iter; ++iter)
7068 add_method (t, *iter, true);
7070 else if (DECL_DECLARES_FUNCTION_P (x))
7071 DECL_IN_AGGR_P (x) = false;
7073 TYPE_SIZE (t) = bitsize_zero_node;
7074 TYPE_SIZE_UNIT (t) = size_zero_node;
7075 /* COMPLETE_TYPE_P is now true. */
7077 set_class_bindings (t);
7079 /* We need to emit an error message if this type was used as a parameter
7080 and it is an abstract type, even if it is a template. We construct
7081 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
7082 account and we call complete_vars with this type, which will check
7083 the PARM_DECLS. Note that while the type is being defined,
7084 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
7085 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */
7086 CLASSTYPE_PURE_VIRTUALS (t) = NULL;
7087 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
7088 if (TREE_CODE (x) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (x))
7089 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x);
7090 complete_vars (t);
7092 /* Remember current #pragma pack value. */
7093 TYPE_PRECISION (t) = maximum_field_alignment;
7095 /* Fix up any variants we've already built. */
7096 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7098 TYPE_SIZE (x) = TYPE_SIZE (t);
7099 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t);
7100 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7103 else
7104 finish_struct_1 (t);
7105 /* COMPLETE_TYPE_P is now true. */
7107 maybe_warn_about_overly_private_class (t);
7109 if (is_std_init_list (t))
7111 /* People keep complaining that the compiler crashes on an invalid
7112 definition of initializer_list, so I guess we should explicitly
7113 reject it. What the compiler internals care about is that it's a
7114 template and has a pointer field followed by size_type field. */
7115 bool ok = false;
7116 if (processing_template_decl)
7118 tree f = next_initializable_field (TYPE_FIELDS (t));
7119 if (f && TYPE_PTR_P (TREE_TYPE (f)))
7121 f = next_initializable_field (DECL_CHAIN (f));
7122 if (f && same_type_p (TREE_TYPE (f), size_type_node))
7123 ok = true;
7126 if (!ok)
7127 fatal_error (input_location, "definition of %qD does not match "
7128 "%<#include <initializer_list>%>", TYPE_NAME (t));
7131 input_location = saved_loc;
7133 TYPE_BEING_DEFINED (t) = 0;
7135 if (current_class_type)
7136 popclass ();
7137 else
7138 error ("trying to finish struct, but kicked out due to previous parse errors");
7140 if (processing_template_decl && at_function_scope_p ()
7141 /* Lambdas are defined by the LAMBDA_EXPR. */
7142 && !LAMBDA_TYPE_P (t))
7143 add_stmt (build_min (TAG_DEFN, t));
7145 return t;
7148 /* Hash table to avoid endless recursion when handling references. */
7149 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht;
7151 /* Return the dynamic type of INSTANCE, if known.
7152 Used to determine whether the virtual function table is needed
7153 or not.
7155 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7156 of our knowledge of its type. *NONNULL should be initialized
7157 before this function is called. */
7159 static tree
7160 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
7162 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
7164 switch (TREE_CODE (instance))
7166 case INDIRECT_REF:
7167 if (INDIRECT_TYPE_P (TREE_TYPE (instance)))
7168 return NULL_TREE;
7169 else
7170 return RECUR (TREE_OPERAND (instance, 0));
7172 case CALL_EXPR:
7173 /* This is a call to a constructor, hence it's never zero. */
7174 if (CALL_EXPR_FN (instance)
7175 && TREE_HAS_CONSTRUCTOR (instance))
7177 if (nonnull)
7178 *nonnull = 1;
7179 return TREE_TYPE (instance);
7181 return NULL_TREE;
7183 case SAVE_EXPR:
7184 /* This is a call to a constructor, hence it's never zero. */
7185 if (TREE_HAS_CONSTRUCTOR (instance))
7187 if (nonnull)
7188 *nonnull = 1;
7189 return TREE_TYPE (instance);
7191 return RECUR (TREE_OPERAND (instance, 0));
7193 case POINTER_PLUS_EXPR:
7194 case PLUS_EXPR:
7195 case MINUS_EXPR:
7196 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
7197 return RECUR (TREE_OPERAND (instance, 0));
7198 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
7199 /* Propagate nonnull. */
7200 return RECUR (TREE_OPERAND (instance, 0));
7202 return NULL_TREE;
7204 CASE_CONVERT:
7205 return RECUR (TREE_OPERAND (instance, 0));
7207 case ADDR_EXPR:
7208 instance = TREE_OPERAND (instance, 0);
7209 if (nonnull)
7211 /* Just because we see an ADDR_EXPR doesn't mean we're dealing
7212 with a real object -- given &p->f, p can still be null. */
7213 tree t = get_base_address (instance);
7214 /* ??? Probably should check DECL_WEAK here. */
7215 if (t && DECL_P (t))
7216 *nonnull = 1;
7218 return RECUR (instance);
7220 case COMPONENT_REF:
7221 /* If this component is really a base class reference, then the field
7222 itself isn't definitive. */
7223 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
7224 return RECUR (TREE_OPERAND (instance, 0));
7225 return RECUR (TREE_OPERAND (instance, 1));
7227 case VAR_DECL:
7228 case FIELD_DECL:
7229 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
7230 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
7232 if (nonnull)
7233 *nonnull = 1;
7234 return TREE_TYPE (TREE_TYPE (instance));
7236 /* fall through. */
7237 case TARGET_EXPR:
7238 case PARM_DECL:
7239 case RESULT_DECL:
7240 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
7242 if (nonnull)
7243 *nonnull = 1;
7244 return TREE_TYPE (instance);
7246 else if (instance == current_class_ptr)
7248 if (nonnull)
7249 *nonnull = 1;
7251 /* if we're in a ctor or dtor, we know our type. If
7252 current_class_ptr is set but we aren't in a function, we're in
7253 an NSDMI (and therefore a constructor). */
7254 if (current_scope () != current_function_decl
7255 || (DECL_LANG_SPECIFIC (current_function_decl)
7256 && (DECL_CONSTRUCTOR_P (current_function_decl)
7257 || DECL_DESTRUCTOR_P (current_function_decl))))
7259 if (cdtorp)
7260 *cdtorp = 1;
7261 return TREE_TYPE (TREE_TYPE (instance));
7264 else if (TYPE_REF_P (TREE_TYPE (instance)))
7266 /* We only need one hash table because it is always left empty. */
7267 if (!fixed_type_or_null_ref_ht)
7268 fixed_type_or_null_ref_ht
7269 = new hash_table<nofree_ptr_hash<tree_node> > (37);
7271 /* Reference variables should be references to objects. */
7272 if (nonnull)
7273 *nonnull = 1;
7275 /* Enter the INSTANCE in a table to prevent recursion; a
7276 variable's initializer may refer to the variable
7277 itself. */
7278 if (VAR_P (instance)
7279 && DECL_INITIAL (instance)
7280 && !type_dependent_expression_p_push (DECL_INITIAL (instance))
7281 && !fixed_type_or_null_ref_ht->find (instance))
7283 tree type;
7284 tree_node **slot;
7286 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT);
7287 *slot = instance;
7288 type = RECUR (DECL_INITIAL (instance));
7289 fixed_type_or_null_ref_ht->remove_elt (instance);
7291 return type;
7294 return NULL_TREE;
7296 default:
7297 return NULL_TREE;
7299 #undef RECUR
7302 /* Return nonzero if the dynamic type of INSTANCE is known, and
7303 equivalent to the static type. We also handle the case where
7304 INSTANCE is really a pointer. Return negative if this is a
7305 ctor/dtor. There the dynamic type is known, but this might not be
7306 the most derived base of the original object, and hence virtual
7307 bases may not be laid out according to this type.
7309 Used to determine whether the virtual function table is needed
7310 or not.
7312 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
7313 of our knowledge of its type. *NONNULL should be initialized
7314 before this function is called. */
7317 resolves_to_fixed_type_p (tree instance, int* nonnull)
7319 tree t = TREE_TYPE (instance);
7320 int cdtorp = 0;
7321 tree fixed;
7323 /* processing_template_decl can be false in a template if we're in
7324 instantiate_non_dependent_expr, but we still want to suppress
7325 this check. */
7326 if (in_template_function ())
7328 /* In a template we only care about the type of the result. */
7329 if (nonnull)
7330 *nonnull = true;
7331 return true;
7334 fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
7335 if (fixed == NULL_TREE)
7336 return 0;
7337 if (INDIRECT_TYPE_P (t))
7338 t = TREE_TYPE (t);
7339 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
7340 return 0;
7341 return cdtorp ? -1 : 1;
7345 void
7346 init_class_processing (void)
7348 current_class_depth = 0;
7349 current_class_stack_size = 10;
7350 current_class_stack
7351 = XNEWVEC (struct class_stack_node, current_class_stack_size);
7352 vec_alloc (local_classes, 8);
7353 sizeof_biggest_empty_class = size_zero_node;
7355 ridpointers[(int) RID_PUBLIC] = access_public_node;
7356 ridpointers[(int) RID_PRIVATE] = access_private_node;
7357 ridpointers[(int) RID_PROTECTED] = access_protected_node;
7360 /* Restore the cached PREVIOUS_CLASS_LEVEL. */
7362 static void
7363 restore_class_cache (void)
7365 tree type;
7367 /* We are re-entering the same class we just left, so we don't
7368 have to search the whole inheritance matrix to find all the
7369 decls to bind again. Instead, we install the cached
7370 class_shadowed list and walk through it binding names. */
7371 push_binding_level (previous_class_level);
7372 class_binding_level = previous_class_level;
7373 /* Restore IDENTIFIER_TYPE_VALUE. */
7374 for (type = class_binding_level->type_shadowed;
7375 type;
7376 type = TREE_CHAIN (type))
7377 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
7380 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
7381 appropriate for TYPE.
7383 So that we may avoid calls to lookup_name, we cache the _TYPE
7384 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
7386 For multiple inheritance, we perform a two-pass depth-first search
7387 of the type lattice. */
7389 void
7390 pushclass (tree type)
7392 class_stack_node_t csn;
7394 type = TYPE_MAIN_VARIANT (type);
7396 /* Make sure there is enough room for the new entry on the stack. */
7397 if (current_class_depth + 1 >= current_class_stack_size)
7399 current_class_stack_size *= 2;
7400 current_class_stack
7401 = XRESIZEVEC (struct class_stack_node, current_class_stack,
7402 current_class_stack_size);
7405 /* Insert a new entry on the class stack. */
7406 csn = current_class_stack + current_class_depth;
7407 csn->name = current_class_name;
7408 csn->type = current_class_type;
7409 csn->access = current_access_specifier;
7410 csn->names_used = 0;
7411 csn->hidden = 0;
7412 current_class_depth++;
7414 /* Now set up the new type. */
7415 current_class_name = TYPE_NAME (type);
7416 if (TREE_CODE (current_class_name) == TYPE_DECL)
7417 current_class_name = DECL_NAME (current_class_name);
7418 current_class_type = type;
7420 /* By default, things in classes are private, while things in
7421 structures or unions are public. */
7422 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
7423 ? access_private_node
7424 : access_public_node);
7426 if (previous_class_level
7427 && type != previous_class_level->this_entity
7428 && current_class_depth == 1)
7430 /* Forcibly remove any old class remnants. */
7431 invalidate_class_lookup_cache ();
7434 if (!previous_class_level
7435 || type != previous_class_level->this_entity
7436 || current_class_depth > 1)
7437 pushlevel_class ();
7438 else
7439 restore_class_cache ();
7442 /* When we exit a toplevel class scope, we save its binding level so
7443 that we can restore it quickly. Here, we've entered some other
7444 class, so we must invalidate our cache. */
7446 void
7447 invalidate_class_lookup_cache (void)
7449 previous_class_level = NULL;
7452 /* Get out of the current class scope. If we were in a class scope
7453 previously, that is the one popped to. */
7455 void
7456 popclass (void)
7458 poplevel_class ();
7460 current_class_depth--;
7461 current_class_name = current_class_stack[current_class_depth].name;
7462 current_class_type = current_class_stack[current_class_depth].type;
7463 current_access_specifier = current_class_stack[current_class_depth].access;
7464 if (current_class_stack[current_class_depth].names_used)
7465 splay_tree_delete (current_class_stack[current_class_depth].names_used);
7468 /* Mark the top of the class stack as hidden. */
7470 void
7471 push_class_stack (void)
7473 if (current_class_depth)
7474 ++current_class_stack[current_class_depth - 1].hidden;
7477 /* Mark the top of the class stack as un-hidden. */
7479 void
7480 pop_class_stack (void)
7482 if (current_class_depth)
7483 --current_class_stack[current_class_depth - 1].hidden;
7486 /* If the class type currently being defined is either T or
7487 a nested type of T, returns the type from the current_class_stack,
7488 which might be equivalent to but not equal to T in case of
7489 constrained partial specializations. */
7491 tree
7492 currently_open_class (tree t)
7494 int i;
7496 if (!CLASS_TYPE_P (t))
7497 return NULL_TREE;
7499 t = TYPE_MAIN_VARIANT (t);
7501 /* We start looking from 1 because entry 0 is from global scope,
7502 and has no type. */
7503 for (i = current_class_depth; i > 0; --i)
7505 tree c;
7506 if (i == current_class_depth)
7507 c = current_class_type;
7508 else
7510 if (current_class_stack[i].hidden)
7511 break;
7512 c = current_class_stack[i].type;
7514 if (!c)
7515 continue;
7516 if (same_type_p (c, t))
7517 return c;
7519 return NULL_TREE;
7522 /* If either current_class_type or one of its enclosing classes are derived
7523 from T, return the appropriate type. Used to determine how we found
7524 something via unqualified lookup. */
7526 tree
7527 currently_open_derived_class (tree t)
7529 int i;
7531 /* The bases of a dependent type are unknown. */
7532 if (dependent_type_p (t))
7533 return NULL_TREE;
7535 if (!current_class_type)
7536 return NULL_TREE;
7538 if (DERIVED_FROM_P (t, current_class_type))
7539 return current_class_type;
7541 for (i = current_class_depth - 1; i > 0; --i)
7543 if (current_class_stack[i].hidden)
7544 break;
7545 if (DERIVED_FROM_P (t, current_class_stack[i].type))
7546 return current_class_stack[i].type;
7549 return NULL_TREE;
7552 /* Return the outermost enclosing class type that is still open, or
7553 NULL_TREE. */
7555 tree
7556 outermost_open_class (void)
7558 if (!current_class_type)
7559 return NULL_TREE;
7560 tree r = NULL_TREE;
7561 if (TYPE_BEING_DEFINED (current_class_type))
7562 r = current_class_type;
7563 for (int i = current_class_depth - 1; i > 0; --i)
7565 if (current_class_stack[i].hidden)
7566 break;
7567 tree t = current_class_stack[i].type;
7568 if (!TYPE_BEING_DEFINED (t))
7569 break;
7570 r = t;
7572 return r;
7575 /* Returns the innermost class type which is not a lambda closure type. */
7577 tree
7578 current_nonlambda_class_type (void)
7580 tree type = current_class_type;
7581 while (type && LAMBDA_TYPE_P (type))
7582 type = decl_type_context (TYPE_NAME (type));
7583 return type;
7586 /* When entering a class scope, all enclosing class scopes' names with
7587 static meaning (static variables, static functions, types and
7588 enumerators) have to be visible. This recursive function calls
7589 pushclass for all enclosing class contexts until global or a local
7590 scope is reached. TYPE is the enclosed class. */
7592 void
7593 push_nested_class (tree type)
7595 /* A namespace might be passed in error cases, like A::B:C. */
7596 if (type == NULL_TREE
7597 || !CLASS_TYPE_P (type))
7598 return;
7600 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
7602 pushclass (type);
7605 /* Undoes a push_nested_class call. */
7607 void
7608 pop_nested_class (void)
7610 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
7612 popclass ();
7613 if (context && CLASS_TYPE_P (context))
7614 pop_nested_class ();
7617 /* Returns the number of extern "LANG" blocks we are nested within. */
7620 current_lang_depth (void)
7622 return vec_safe_length (current_lang_base);
7625 /* Set global variables CURRENT_LANG_NAME to appropriate value
7626 so that behavior of name-mangling machinery is correct. */
7628 void
7629 push_lang_context (tree name)
7631 vec_safe_push (current_lang_base, current_lang_name);
7633 if (name == lang_name_cplusplus)
7634 current_lang_name = name;
7635 else if (name == lang_name_c)
7636 current_lang_name = name;
7637 else
7638 error ("language string %<\"%E\"%> not recognized", name);
7641 /* Get out of the current language scope. */
7643 void
7644 pop_lang_context (void)
7646 current_lang_name = current_lang_base->pop ();
7649 /* Type instantiation routines. */
7651 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
7652 matches the TARGET_TYPE. If there is no satisfactory match, return
7653 error_mark_node, and issue an error & warning messages under
7654 control of FLAGS. Permit pointers to member function if FLAGS
7655 permits. If TEMPLATE_ONLY, the name of the overloaded function was
7656 a template-id, and EXPLICIT_TARGS are the explicitly provided
7657 template arguments.
7659 If OVERLOAD is for one or more member functions, then ACCESS_PATH
7660 is the base path used to reference those member functions. If
7661 the address is resolved to a member function, access checks will be
7662 performed and errors issued if appropriate. */
7664 static tree
7665 resolve_address_of_overloaded_function (tree target_type,
7666 tree overload,
7667 tsubst_flags_t complain,
7668 bool template_only,
7669 tree explicit_targs,
7670 tree access_path)
7672 /* Here's what the standard says:
7674 [over.over]
7676 If the name is a function template, template argument deduction
7677 is done, and if the argument deduction succeeds, the deduced
7678 arguments are used to generate a single template function, which
7679 is added to the set of overloaded functions considered.
7681 Non-member functions and static member functions match targets of
7682 type "pointer-to-function" or "reference-to-function." Nonstatic
7683 member functions match targets of type "pointer-to-member
7684 function;" the function type of the pointer to member is used to
7685 select the member function from the set of overloaded member
7686 functions. If a nonstatic member function is selected, the
7687 reference to the overloaded function name is required to have the
7688 form of a pointer to member as described in 5.3.1.
7690 If more than one function is selected, any template functions in
7691 the set are eliminated if the set also contains a non-template
7692 function, and any given template function is eliminated if the
7693 set contains a second template function that is more specialized
7694 than the first according to the partial ordering rules 14.5.5.2.
7695 After such eliminations, if any, there shall remain exactly one
7696 selected function. */
7698 int is_ptrmem = 0;
7699 /* We store the matches in a TREE_LIST rooted here. The functions
7700 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
7701 interoperability with most_specialized_instantiation. */
7702 tree matches = NULL_TREE;
7703 tree fn;
7704 tree target_fn_type;
7706 /* By the time we get here, we should be seeing only real
7707 pointer-to-member types, not the internal POINTER_TYPE to
7708 METHOD_TYPE representation. */
7709 gcc_assert (!TYPE_PTR_P (target_type)
7710 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
7712 gcc_assert (is_overloaded_fn (overload));
7714 /* Check that the TARGET_TYPE is reasonable. */
7715 if (TYPE_PTRFN_P (target_type)
7716 || TYPE_REFFN_P (target_type))
7717 /* This is OK. */;
7718 else if (TYPE_PTRMEMFUNC_P (target_type))
7719 /* This is OK, too. */
7720 is_ptrmem = 1;
7721 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
7722 /* This is OK, too. This comes from a conversion to reference
7723 type. */
7724 target_type = build_reference_type (target_type);
7725 else
7727 if (complain & tf_error)
7728 error ("cannot resolve overloaded function %qD based on"
7729 " conversion to type %qT",
7730 OVL_NAME (overload), target_type);
7731 return error_mark_node;
7734 /* Non-member functions and static member functions match targets of type
7735 "pointer-to-function" or "reference-to-function." Nonstatic member
7736 functions match targets of type "pointer-to-member-function;" the
7737 function type of the pointer to member is used to select the member
7738 function from the set of overloaded member functions.
7740 So figure out the FUNCTION_TYPE that we want to match against. */
7741 target_fn_type = static_fn_type (target_type);
7743 /* If we can find a non-template function that matches, we can just
7744 use it. There's no point in generating template instantiations
7745 if we're just going to throw them out anyhow. But, of course, we
7746 can only do this when we don't *need* a template function. */
7747 if (!template_only)
7748 for (lkp_iterator iter (overload); iter; ++iter)
7750 tree fn = *iter;
7752 if (TREE_CODE (fn) == TEMPLATE_DECL)
7753 /* We're not looking for templates just yet. */
7754 continue;
7756 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) != is_ptrmem)
7757 /* We're looking for a non-static member, and this isn't
7758 one, or vice versa. */
7759 continue;
7761 /* In C++17 we need the noexcept-qualifier to compare types. */
7762 if (flag_noexcept_type
7763 && !maybe_instantiate_noexcept (fn, complain))
7764 continue;
7766 /* See if there's a match. */
7767 tree fntype = static_fn_type (fn);
7768 if (same_type_p (target_fn_type, fntype)
7769 || fnptr_conv_p (target_fn_type, fntype))
7770 matches = tree_cons (fn, NULL_TREE, matches);
7773 /* Now, if we've already got a match (or matches), there's no need
7774 to proceed to the template functions. But, if we don't have a
7775 match we need to look at them, too. */
7776 if (!matches)
7778 tree target_arg_types;
7779 tree target_ret_type;
7780 tree *args;
7781 unsigned int nargs, ia;
7782 tree arg;
7784 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
7785 target_ret_type = TREE_TYPE (target_fn_type);
7787 nargs = list_length (target_arg_types);
7788 args = XALLOCAVEC (tree, nargs);
7789 for (arg = target_arg_types, ia = 0;
7790 arg != NULL_TREE && arg != void_list_node;
7791 arg = TREE_CHAIN (arg), ++ia)
7792 args[ia] = TREE_VALUE (arg);
7793 nargs = ia;
7795 for (lkp_iterator iter (overload); iter; ++iter)
7797 tree fn = *iter;
7798 tree instantiation;
7799 tree targs;
7801 if (TREE_CODE (fn) != TEMPLATE_DECL)
7802 /* We're only looking for templates. */
7803 continue;
7805 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
7806 != is_ptrmem)
7807 /* We're not looking for a non-static member, and this is
7808 one, or vice versa. */
7809 continue;
7811 tree ret = target_ret_type;
7813 /* If the template has a deduced return type, don't expose it to
7814 template argument deduction. */
7815 if (undeduced_auto_decl (fn))
7816 ret = NULL_TREE;
7818 /* Try to do argument deduction. */
7819 targs = make_tree_vec (DECL_NTPARMS (fn));
7820 instantiation = fn_type_unification (fn, explicit_targs, targs, args,
7821 nargs, ret,
7822 DEDUCE_EXACT, LOOKUP_NORMAL,
7823 NULL, false, false);
7824 if (instantiation == error_mark_node)
7825 /* Instantiation failed. */
7826 continue;
7828 /* Constraints must be satisfied. This is done before
7829 return type deduction since that instantiates the
7830 function. */
7831 if (flag_concepts && !constraints_satisfied_p (instantiation))
7832 continue;
7834 /* And now force instantiation to do return type deduction. */
7835 if (undeduced_auto_decl (instantiation))
7837 ++function_depth;
7838 instantiate_decl (instantiation, /*defer*/false, /*class*/false);
7839 --function_depth;
7841 require_deduced_type (instantiation);
7844 /* In C++17 we need the noexcept-qualifier to compare types. */
7845 if (flag_noexcept_type)
7846 maybe_instantiate_noexcept (instantiation, complain);
7848 /* See if there's a match. */
7849 tree fntype = static_fn_type (instantiation);
7850 if (same_type_p (target_fn_type, fntype)
7851 || fnptr_conv_p (target_fn_type, fntype))
7852 matches = tree_cons (instantiation, fn, matches);
7855 /* Now, remove all but the most specialized of the matches. */
7856 if (matches)
7858 tree match = most_specialized_instantiation (matches);
7860 if (match != error_mark_node)
7861 matches = tree_cons (TREE_PURPOSE (match),
7862 NULL_TREE,
7863 NULL_TREE);
7867 /* Now we should have exactly one function in MATCHES. */
7868 if (matches == NULL_TREE)
7870 /* There were *no* matches. */
7871 if (complain & tf_error)
7873 error ("no matches converting function %qD to type %q#T",
7874 OVL_NAME (overload), target_type);
7876 print_candidates (overload);
7878 return error_mark_node;
7880 else if (TREE_CHAIN (matches))
7882 /* There were too many matches. First check if they're all
7883 the same function. */
7884 tree match = NULL_TREE;
7886 fn = TREE_PURPOSE (matches);
7888 /* For multi-versioned functions, more than one match is just fine and
7889 decls_match will return false as they are different. */
7890 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
7891 if (!decls_match (fn, TREE_PURPOSE (match))
7892 && !targetm.target_option.function_versions
7893 (fn, TREE_PURPOSE (match)))
7894 break;
7896 if (match)
7898 if (complain & tf_error)
7900 error ("converting overloaded function %qD to type %q#T is ambiguous",
7901 OVL_NAME (overload), target_type);
7903 /* Since print_candidates expects the functions in the
7904 TREE_VALUE slot, we flip them here. */
7905 for (match = matches; match; match = TREE_CHAIN (match))
7906 TREE_VALUE (match) = TREE_PURPOSE (match);
7908 print_candidates (matches);
7911 return error_mark_node;
7915 /* Good, exactly one match. Now, convert it to the correct type. */
7916 fn = TREE_PURPOSE (matches);
7918 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
7919 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions)
7921 static int explained;
7923 if (!(complain & tf_error))
7924 return error_mark_node;
7926 if (permerror (input_location, "assuming pointer to member %qD", fn)
7927 && !explained)
7929 inform (input_location, "(a pointer to member can only be "
7930 "formed with %<&%E%>)", fn);
7931 explained = 1;
7935 /* If a pointer to a function that is multi-versioned is requested, the
7936 pointer to the dispatcher function is returned instead. This works
7937 well because indirectly calling the function will dispatch the right
7938 function version at run-time. */
7939 if (DECL_FUNCTION_VERSIONED (fn))
7941 fn = get_function_version_dispatcher (fn);
7942 if (fn == NULL)
7943 return error_mark_node;
7944 /* Mark all the versions corresponding to the dispatcher as used. */
7945 if (!(complain & tf_conv))
7946 mark_versions_used (fn);
7949 /* If we're doing overload resolution purely for the purpose of
7950 determining conversion sequences, we should not consider the
7951 function used. If this conversion sequence is selected, the
7952 function will be marked as used at this point. */
7953 if (!(complain & tf_conv))
7955 /* Make =delete work with SFINAE. */
7956 if (DECL_DELETED_FN (fn) && !(complain & tf_error))
7957 return error_mark_node;
7958 if (!mark_used (fn, complain) && !(complain & tf_error))
7959 return error_mark_node;
7962 /* We could not check access to member functions when this
7963 expression was originally created since we did not know at that
7964 time to which function the expression referred. */
7965 if (DECL_FUNCTION_MEMBER_P (fn))
7967 gcc_assert (access_path);
7968 perform_or_defer_access_check (access_path, fn, fn, complain);
7971 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
7972 return cp_build_addr_expr (fn, complain);
7973 else
7975 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op
7976 will mark the function as addressed, but here we must do it
7977 explicitly. */
7978 cxx_mark_addressable (fn);
7980 return fn;
7984 /* This function will instantiate the type of the expression given in
7985 RHS to match the type of LHSTYPE. If errors exist, then return
7986 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then
7987 we complain on errors. If we are not complaining, never modify rhs,
7988 as overload resolution wants to try many possible instantiations, in
7989 the hope that at least one will work.
7991 For non-recursive calls, LHSTYPE should be a function, pointer to
7992 function, or a pointer to member function. */
7994 tree
7995 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain)
7997 tsubst_flags_t complain_in = complain;
7998 tree access_path = NULL_TREE;
8000 complain &= ~tf_ptrmem_ok;
8002 if (lhstype == unknown_type_node)
8004 if (complain & tf_error)
8005 error ("not enough type information");
8006 return error_mark_node;
8009 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
8011 tree fntype = non_reference (lhstype);
8012 if (same_type_p (fntype, TREE_TYPE (rhs)))
8013 return rhs;
8014 if (fnptr_conv_p (fntype, TREE_TYPE (rhs)))
8015 return rhs;
8016 if (flag_ms_extensions
8017 && TYPE_PTRMEMFUNC_P (fntype)
8018 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
8019 /* Microsoft allows `A::f' to be resolved to a
8020 pointer-to-member. */
8022 else
8024 if (complain & tf_error)
8025 error ("cannot convert %qE from type %qT to type %qT",
8026 rhs, TREE_TYPE (rhs), fntype);
8027 return error_mark_node;
8031 /* If we instantiate a template, and it is a A ?: C expression
8032 with omitted B, look through the SAVE_EXPR. */
8033 if (TREE_CODE (rhs) == SAVE_EXPR)
8034 rhs = TREE_OPERAND (rhs, 0);
8036 if (BASELINK_P (rhs))
8038 access_path = BASELINK_ACCESS_BINFO (rhs);
8039 rhs = BASELINK_FUNCTIONS (rhs);
8042 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
8043 deduce any type information. */
8044 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
8046 if (complain & tf_error)
8047 error ("not enough type information");
8048 return error_mark_node;
8051 /* There are only a few kinds of expressions that may have a type
8052 dependent on overload resolution. */
8053 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
8054 || TREE_CODE (rhs) == COMPONENT_REF
8055 || is_overloaded_fn (rhs)
8056 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL));
8058 /* This should really only be used when attempting to distinguish
8059 what sort of a pointer to function we have. For now, any
8060 arithmetic operation which is not supported on pointers
8061 is rejected as an error. */
8063 switch (TREE_CODE (rhs))
8065 case COMPONENT_REF:
8067 tree member = TREE_OPERAND (rhs, 1);
8069 member = instantiate_type (lhstype, member, complain);
8070 if (member != error_mark_node
8071 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
8072 /* Do not lose object's side effects. */
8073 return build2 (COMPOUND_EXPR, TREE_TYPE (member),
8074 TREE_OPERAND (rhs, 0), member);
8075 return member;
8078 case OFFSET_REF:
8079 rhs = TREE_OPERAND (rhs, 1);
8080 if (BASELINK_P (rhs))
8081 return instantiate_type (lhstype, rhs, complain_in);
8083 /* This can happen if we are forming a pointer-to-member for a
8084 member template. */
8085 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
8087 /* Fall through. */
8089 case TEMPLATE_ID_EXPR:
8091 tree fns = TREE_OPERAND (rhs, 0);
8092 tree args = TREE_OPERAND (rhs, 1);
8094 return
8095 resolve_address_of_overloaded_function (lhstype, fns, complain_in,
8096 /*template_only=*/true,
8097 args, access_path);
8100 case OVERLOAD:
8101 case FUNCTION_DECL:
8102 return
8103 resolve_address_of_overloaded_function (lhstype, rhs, complain_in,
8104 /*template_only=*/false,
8105 /*explicit_targs=*/NULL_TREE,
8106 access_path);
8108 case ADDR_EXPR:
8110 if (PTRMEM_OK_P (rhs))
8111 complain |= tf_ptrmem_ok;
8113 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
8116 case ERROR_MARK:
8117 return error_mark_node;
8119 default:
8120 gcc_unreachable ();
8122 return error_mark_node;
8125 /* Return the name of the virtual function pointer field
8126 (as an IDENTIFIER_NODE) for the given TYPE. Note that
8127 this may have to look back through base types to find the
8128 ultimate field name. (For single inheritance, these could
8129 all be the same name. Who knows for multiple inheritance). */
8131 static tree
8132 get_vfield_name (tree type)
8134 tree binfo, base_binfo;
8136 for (binfo = TYPE_BINFO (type);
8137 BINFO_N_BASE_BINFOS (binfo);
8138 binfo = base_binfo)
8140 base_binfo = BINFO_BASE_BINFO (binfo, 0);
8142 if (BINFO_VIRTUAL_P (base_binfo)
8143 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
8144 break;
8147 type = BINFO_TYPE (binfo);
8148 tree ctor_name = constructor_name (type);
8149 char *buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
8150 + IDENTIFIER_LENGTH (ctor_name) + 2);
8151 sprintf (buf, VFIELD_NAME_FORMAT, IDENTIFIER_POINTER (ctor_name));
8152 return get_identifier (buf);
8155 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
8156 according to [class]:
8157 The class-name is also inserted
8158 into the scope of the class itself. For purposes of access checking,
8159 the inserted class name is treated as if it were a public member name. */
8161 void
8162 build_self_reference (void)
8164 tree name = DECL_NAME (TYPE_NAME (current_class_type));
8165 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
8167 DECL_NONLOCAL (value) = 1;
8168 DECL_CONTEXT (value) = current_class_type;
8169 DECL_ARTIFICIAL (value) = 1;
8170 SET_DECL_SELF_REFERENCE_P (value);
8171 set_underlying_type (value);
8173 if (processing_template_decl)
8174 value = push_template_decl (value);
8176 tree saved_cas = current_access_specifier;
8177 current_access_specifier = access_public_node;
8178 finish_member_declaration (value);
8179 current_access_specifier = saved_cas;
8182 /* Returns 1 if TYPE contains only padding bytes. */
8185 is_empty_class (tree type)
8187 if (type == error_mark_node)
8188 return 0;
8190 if (! CLASS_TYPE_P (type))
8191 return 0;
8193 return CLASSTYPE_EMPTY_P (type);
8196 /* Returns true if TYPE contains no actual data, just various
8197 possible combinations of empty classes and possibly a vptr. */
8199 bool
8200 is_really_empty_class (tree type)
8202 if (CLASS_TYPE_P (type))
8204 tree field;
8205 tree binfo;
8206 tree base_binfo;
8207 int i;
8209 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid
8210 out, but we'd like to be able to check this before then. */
8211 if (COMPLETE_TYPE_P (type) && is_empty_class (type))
8212 return true;
8214 for (binfo = TYPE_BINFO (type), i = 0;
8215 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
8216 if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
8217 return false;
8218 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8219 if (TREE_CODE (field) == FIELD_DECL
8220 && !DECL_ARTIFICIAL (field)
8221 /* An unnamed bit-field is not a data member. */
8222 && !DECL_UNNAMED_BIT_FIELD (field)
8223 && !is_really_empty_class (TREE_TYPE (field)))
8224 return false;
8225 return true;
8227 else if (TREE_CODE (type) == ARRAY_TYPE)
8228 return (integer_zerop (array_type_nelts_top (type))
8229 || is_really_empty_class (TREE_TYPE (type)));
8230 return false;
8233 /* Note that NAME was looked up while the current class was being
8234 defined and that the result of that lookup was DECL. */
8236 void
8237 maybe_note_name_used_in_class (tree name, tree decl)
8239 splay_tree names_used;
8241 /* If we're not defining a class, there's nothing to do. */
8242 if (!(innermost_scope_kind() == sk_class
8243 && TYPE_BEING_DEFINED (current_class_type)
8244 && !LAMBDA_TYPE_P (current_class_type)))
8245 return;
8247 /* If there's already a binding for this NAME, then we don't have
8248 anything to worry about. */
8249 if (lookup_member (current_class_type, name,
8250 /*protect=*/0, /*want_type=*/false, tf_warning_or_error))
8251 return;
8253 if (!current_class_stack[current_class_depth - 1].names_used)
8254 current_class_stack[current_class_depth - 1].names_used
8255 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
8256 names_used = current_class_stack[current_class_depth - 1].names_used;
8258 splay_tree_insert (names_used,
8259 (splay_tree_key) name,
8260 (splay_tree_value) decl);
8263 /* Note that NAME was declared (as DECL) in the current class. Check
8264 to see that the declaration is valid. */
8266 void
8267 note_name_declared_in_class (tree name, tree decl)
8269 splay_tree names_used;
8270 splay_tree_node n;
8272 /* Look to see if we ever used this name. */
8273 names_used
8274 = current_class_stack[current_class_depth - 1].names_used;
8275 if (!names_used)
8276 return;
8277 /* The C language allows members to be declared with a type of the same
8278 name, and the C++ standard says this diagnostic is not required. So
8279 allow it in extern "C" blocks unless predantic is specified.
8280 Allow it in all cases if -ms-extensions is specified. */
8281 if ((!pedantic && current_lang_name == lang_name_c)
8282 || flag_ms_extensions)
8283 return;
8284 n = splay_tree_lookup (names_used, (splay_tree_key) name);
8285 if (n)
8287 /* [basic.scope.class]
8289 A name N used in a class S shall refer to the same declaration
8290 in its context and when re-evaluated in the completed scope of
8291 S. */
8292 if (permerror (location_of (decl),
8293 "declaration of %q#D changes meaning of %qD",
8294 decl, OVL_NAME (decl)))
8295 inform (location_of ((tree) n->value),
8296 "%qD declared here as %q#D",
8297 OVL_NAME (decl), (tree) n->value);
8301 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
8302 Secondary vtables are merged with primary vtables; this function
8303 will return the VAR_DECL for the primary vtable. */
8305 tree
8306 get_vtbl_decl_for_binfo (tree binfo)
8308 tree decl;
8310 decl = BINFO_VTABLE (binfo);
8311 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
8313 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
8314 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
8316 if (decl)
8317 gcc_assert (VAR_P (decl));
8318 return decl;
8322 /* Returns the binfo for the primary base of BINFO. If the resulting
8323 BINFO is a virtual base, and it is inherited elsewhere in the
8324 hierarchy, then the returned binfo might not be the primary base of
8325 BINFO in the complete object. Check BINFO_PRIMARY_P or
8326 BINFO_LOST_PRIMARY_P to be sure. */
8328 static tree
8329 get_primary_binfo (tree binfo)
8331 tree primary_base;
8333 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
8334 if (!primary_base)
8335 return NULL_TREE;
8337 return copied_binfo (primary_base, binfo);
8340 /* As above, but iterate until we reach the binfo that actually provides the
8341 vptr for BINFO. */
8343 static tree
8344 most_primary_binfo (tree binfo)
8346 tree b = binfo;
8347 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8348 && !BINFO_LOST_PRIMARY_P (b))
8350 tree primary_base = get_primary_binfo (b);
8351 gcc_assert (BINFO_PRIMARY_P (primary_base)
8352 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
8353 b = primary_base;
8355 return b;
8358 /* Returns true if BINFO gets its vptr from a virtual base of the most derived
8359 type. Note that the virtual inheritance might be above or below BINFO in
8360 the hierarchy. */
8362 bool
8363 vptr_via_virtual_p (tree binfo)
8365 if (TYPE_P (binfo))
8366 binfo = TYPE_BINFO (binfo);
8367 tree primary = most_primary_binfo (binfo);
8368 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is
8369 a morally virtual base. */
8370 tree virt = binfo_via_virtual (primary, NULL_TREE);
8371 return virt != NULL_TREE;
8374 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
8376 static int
8377 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
8379 if (!indented_p)
8380 fprintf (stream, "%*s", indent, "");
8381 return 1;
8384 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
8385 INDENT should be zero when called from the top level; it is
8386 incremented recursively. IGO indicates the next expected BINFO in
8387 inheritance graph ordering. */
8389 static tree
8390 dump_class_hierarchy_r (FILE *stream,
8391 dump_flags_t flags,
8392 tree binfo,
8393 tree igo,
8394 int indent)
8396 int indented = 0;
8397 tree base_binfo;
8398 int i;
8400 indented = maybe_indent_hierarchy (stream, indent, 0);
8401 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ",
8402 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
8403 (HOST_WIDE_INT) (uintptr_t) binfo);
8404 if (binfo != igo)
8406 fprintf (stream, "alternative-path\n");
8407 return igo;
8409 igo = TREE_CHAIN (binfo);
8411 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
8412 tree_to_shwi (BINFO_OFFSET (binfo)));
8413 if (is_empty_class (BINFO_TYPE (binfo)))
8414 fprintf (stream, " empty");
8415 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
8416 fprintf (stream, " nearly-empty");
8417 if (BINFO_VIRTUAL_P (binfo))
8418 fprintf (stream, " virtual");
8419 fprintf (stream, "\n");
8421 indented = 0;
8422 if (BINFO_PRIMARY_P (binfo))
8424 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8425 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")",
8426 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
8427 TFF_PLAIN_IDENTIFIER),
8428 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo));
8430 if (BINFO_LOST_PRIMARY_P (binfo))
8432 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8433 fprintf (stream, " lost-primary");
8435 if (indented)
8436 fprintf (stream, "\n");
8438 if (!(flags & TDF_SLIM))
8440 int indented = 0;
8442 if (BINFO_SUBVTT_INDEX (binfo))
8444 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8445 fprintf (stream, " subvttidx=%s",
8446 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
8447 TFF_PLAIN_IDENTIFIER));
8449 if (BINFO_VPTR_INDEX (binfo))
8451 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8452 fprintf (stream, " vptridx=%s",
8453 expr_as_string (BINFO_VPTR_INDEX (binfo),
8454 TFF_PLAIN_IDENTIFIER));
8456 if (BINFO_VPTR_FIELD (binfo))
8458 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8459 fprintf (stream, " vbaseoffset=%s",
8460 expr_as_string (BINFO_VPTR_FIELD (binfo),
8461 TFF_PLAIN_IDENTIFIER));
8463 if (BINFO_VTABLE (binfo))
8465 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
8466 fprintf (stream, " vptr=%s",
8467 expr_as_string (BINFO_VTABLE (binfo),
8468 TFF_PLAIN_IDENTIFIER));
8471 if (indented)
8472 fprintf (stream, "\n");
8475 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
8476 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
8478 return igo;
8481 /* Dump the BINFO hierarchy for T. */
8483 static void
8484 dump_class_hierarchy_1 (FILE *stream, dump_flags_t flags, tree t)
8486 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8487 fprintf (stream, " size=%lu align=%lu\n",
8488 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT),
8489 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
8490 fprintf (stream, " base size=%lu base align=%lu\n",
8491 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t)))
8492 / BITS_PER_UNIT),
8493 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
8494 / BITS_PER_UNIT));
8495 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
8496 fprintf (stream, "\n");
8499 /* Debug interface to hierarchy dumping. */
8501 void
8502 debug_class (tree t)
8504 dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
8507 static void
8508 dump_class_hierarchy (tree t)
8510 dump_flags_t flags;
8511 if (FILE *stream = dump_begin (class_dump_id, &flags))
8513 dump_class_hierarchy_1 (stream, flags, t);
8514 dump_end (class_dump_id, stream);
8518 static void
8519 dump_array (FILE * stream, tree decl)
8521 tree value;
8522 unsigned HOST_WIDE_INT ix;
8523 HOST_WIDE_INT elt;
8524 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
8526 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))))
8527 / BITS_PER_UNIT);
8528 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
8529 fprintf (stream, " %s entries",
8530 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
8531 TFF_PLAIN_IDENTIFIER));
8532 fprintf (stream, "\n");
8534 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
8535 ix, value)
8536 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
8537 expr_as_string (value, TFF_PLAIN_IDENTIFIER));
8540 static void
8541 dump_vtable (tree t, tree binfo, tree vtable)
8543 dump_flags_t flags;
8544 FILE *stream = dump_begin (class_dump_id, &flags);
8546 if (!stream)
8547 return;
8549 if (!(flags & TDF_SLIM))
8551 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
8553 fprintf (stream, "%s for %s",
8554 ctor_vtbl_p ? "Construction vtable" : "Vtable",
8555 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
8556 if (ctor_vtbl_p)
8558 if (!BINFO_VIRTUAL_P (binfo))
8559 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)",
8560 (HOST_WIDE_INT) (uintptr_t) binfo);
8561 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
8563 fprintf (stream, "\n");
8564 dump_array (stream, vtable);
8565 fprintf (stream, "\n");
8568 dump_end (class_dump_id, stream);
8571 static void
8572 dump_vtt (tree t, tree vtt)
8574 dump_flags_t flags;
8575 FILE *stream = dump_begin (class_dump_id, &flags);
8577 if (!stream)
8578 return;
8580 if (!(flags & TDF_SLIM))
8582 fprintf (stream, "VTT for %s\n",
8583 type_as_string (t, TFF_PLAIN_IDENTIFIER));
8584 dump_array (stream, vtt);
8585 fprintf (stream, "\n");
8588 dump_end (class_dump_id, stream);
8591 /* Dump a function or thunk and its thunkees. */
8593 static void
8594 dump_thunk (FILE *stream, int indent, tree thunk)
8596 static const char spaces[] = " ";
8597 tree name = DECL_NAME (thunk);
8598 tree thunks;
8600 fprintf (stream, "%.*s%p %s %s", indent, spaces,
8601 (void *)thunk,
8602 !DECL_THUNK_P (thunk) ? "function"
8603 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
8604 name ? IDENTIFIER_POINTER (name) : "<unset>");
8605 if (DECL_THUNK_P (thunk))
8607 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
8608 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
8610 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
8611 if (!virtual_adjust)
8612 /*NOP*/;
8613 else if (DECL_THIS_THUNK_P (thunk))
8614 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC,
8615 tree_to_shwi (virtual_adjust));
8616 else
8617 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
8618 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)),
8619 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
8620 if (THUNK_ALIAS (thunk))
8621 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
8623 fprintf (stream, "\n");
8624 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
8625 dump_thunk (stream, indent + 2, thunks);
8628 /* Dump the thunks for FN. */
8630 void
8631 debug_thunks (tree fn)
8633 dump_thunk (stderr, 0, fn);
8636 /* Virtual function table initialization. */
8638 /* Create all the necessary vtables for T and its base classes. */
8640 static void
8641 finish_vtbls (tree t)
8643 tree vbase;
8644 vec<constructor_elt, va_gc> *v = NULL;
8645 tree vtable = BINFO_VTABLE (TYPE_BINFO (t));
8647 /* We lay out the primary and secondary vtables in one contiguous
8648 vtable. The primary vtable is first, followed by the non-virtual
8649 secondary vtables in inheritance graph order. */
8650 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t),
8651 vtable, t, &v);
8653 /* Then come the virtual bases, also in inheritance graph order. */
8654 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
8656 if (!BINFO_VIRTUAL_P (vbase))
8657 continue;
8658 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v);
8661 if (BINFO_VTABLE (TYPE_BINFO (t)))
8662 initialize_vtable (TYPE_BINFO (t), v);
8665 /* Initialize the vtable for BINFO with the INITS. */
8667 static void
8668 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits)
8670 tree decl;
8672 layout_vtable_decl (binfo, vec_safe_length (inits));
8673 decl = get_vtbl_decl_for_binfo (binfo);
8674 initialize_artificial_var (decl, inits);
8675 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
8678 /* Build the VTT (virtual table table) for T.
8679 A class requires a VTT if it has virtual bases.
8681 This holds
8682 1 - primary virtual pointer for complete object T
8683 2 - secondary VTTs for each direct non-virtual base of T which requires a
8685 3 - secondary virtual pointers for each direct or indirect base of T which
8686 has virtual bases or is reachable via a virtual path from T.
8687 4 - secondary VTTs for each direct or indirect virtual base of T.
8689 Secondary VTTs look like complete object VTTs without part 4. */
8691 static void
8692 build_vtt (tree t)
8694 tree type;
8695 tree vtt;
8696 tree index;
8697 vec<constructor_elt, va_gc> *inits;
8699 /* Build up the initializers for the VTT. */
8700 inits = NULL;
8701 index = size_zero_node;
8702 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
8704 /* If we didn't need a VTT, we're done. */
8705 if (!inits)
8706 return;
8708 /* Figure out the type of the VTT. */
8709 type = build_array_of_n_type (const_ptr_type_node,
8710 inits->length ());
8712 /* Now, build the VTT object itself. */
8713 vtt = build_vtable (t, mangle_vtt_for_type (t), type);
8714 initialize_artificial_var (vtt, inits);
8715 /* Add the VTT to the vtables list. */
8716 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t));
8717 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
8719 dump_vtt (t, vtt);
8722 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
8723 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
8724 and CHAIN the vtable pointer for this binfo after construction is
8725 complete. VALUE can also be another BINFO, in which case we recurse. */
8727 static tree
8728 binfo_ctor_vtable (tree binfo)
8730 tree vt;
8732 while (1)
8734 vt = BINFO_VTABLE (binfo);
8735 if (TREE_CODE (vt) == TREE_LIST)
8736 vt = TREE_VALUE (vt);
8737 if (TREE_CODE (vt) == TREE_BINFO)
8738 binfo = vt;
8739 else
8740 break;
8743 return vt;
8746 /* Data for secondary VTT initialization. */
8747 struct secondary_vptr_vtt_init_data
8749 /* Is this the primary VTT? */
8750 bool top_level_p;
8752 /* Current index into the VTT. */
8753 tree index;
8755 /* Vector of initializers built up. */
8756 vec<constructor_elt, va_gc> *inits;
8758 /* The type being constructed by this secondary VTT. */
8759 tree type_being_constructed;
8762 /* Recursively build the VTT-initializer for BINFO (which is in the
8763 hierarchy dominated by T). INITS points to the end of the initializer
8764 list to date. INDEX is the VTT index where the next element will be
8765 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
8766 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
8767 for virtual bases of T. When it is not so, we build the constructor
8768 vtables for the BINFO-in-T variant. */
8770 static void
8771 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits,
8772 tree *index)
8774 int i;
8775 tree b;
8776 tree init;
8777 secondary_vptr_vtt_init_data data;
8778 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
8780 /* We only need VTTs for subobjects with virtual bases. */
8781 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
8782 return;
8784 /* We need to use a construction vtable if this is not the primary
8785 VTT. */
8786 if (!top_level_p)
8788 build_ctor_vtbl_group (binfo, t);
8790 /* Record the offset in the VTT where this sub-VTT can be found. */
8791 BINFO_SUBVTT_INDEX (binfo) = *index;
8794 /* Add the address of the primary vtable for the complete object. */
8795 init = binfo_ctor_vtable (binfo);
8796 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
8797 if (top_level_p)
8799 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8800 BINFO_VPTR_INDEX (binfo) = *index;
8802 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
8804 /* Recursively add the secondary VTTs for non-virtual bases. */
8805 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
8806 if (!BINFO_VIRTUAL_P (b))
8807 build_vtt_inits (b, t, inits, index);
8809 /* Add secondary virtual pointers for all subobjects of BINFO with
8810 either virtual bases or reachable along a virtual path, except
8811 subobjects that are non-virtual primary bases. */
8812 data.top_level_p = top_level_p;
8813 data.index = *index;
8814 data.inits = *inits;
8815 data.type_being_constructed = BINFO_TYPE (binfo);
8817 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
8819 *index = data.index;
8821 /* data.inits might have grown as we added secondary virtual pointers.
8822 Make sure our caller knows about the new vector. */
8823 *inits = data.inits;
8825 if (top_level_p)
8826 /* Add the secondary VTTs for virtual bases in inheritance graph
8827 order. */
8828 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
8830 if (!BINFO_VIRTUAL_P (b))
8831 continue;
8833 build_vtt_inits (b, t, inits, index);
8835 else
8836 /* Remove the ctor vtables we created. */
8837 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
8840 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base
8841 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */
8843 static tree
8844 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
8846 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
8848 /* We don't care about bases that don't have vtables. */
8849 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
8850 return dfs_skip_bases;
8852 /* We're only interested in proper subobjects of the type being
8853 constructed. */
8854 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
8855 return NULL_TREE;
8857 /* We're only interested in bases with virtual bases or reachable
8858 via a virtual path from the type being constructed. */
8859 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
8860 || binfo_via_virtual (binfo, data->type_being_constructed)))
8861 return dfs_skip_bases;
8863 /* We're not interested in non-virtual primary bases. */
8864 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
8865 return NULL_TREE;
8867 /* Record the index where this secondary vptr can be found. */
8868 if (data->top_level_p)
8870 gcc_assert (!BINFO_VPTR_INDEX (binfo));
8871 BINFO_VPTR_INDEX (binfo) = data->index;
8873 if (BINFO_VIRTUAL_P (binfo))
8875 /* It's a primary virtual base, and this is not a
8876 construction vtable. Find the base this is primary of in
8877 the inheritance graph, and use that base's vtable
8878 now. */
8879 while (BINFO_PRIMARY_P (binfo))
8880 binfo = BINFO_INHERITANCE_CHAIN (binfo);
8884 /* Add the initializer for the secondary vptr itself. */
8885 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo));
8887 /* Advance the vtt index. */
8888 data->index = size_binop (PLUS_EXPR, data->index,
8889 TYPE_SIZE_UNIT (ptr_type_node));
8891 return NULL_TREE;
8894 /* Called from build_vtt_inits via dfs_walk. After building
8895 constructor vtables and generating the sub-vtt from them, we need
8896 to restore the BINFO_VTABLES that were scribbled on. DATA is the
8897 binfo of the base whose sub vtt was generated. */
8899 static tree
8900 dfs_fixup_binfo_vtbls (tree binfo, void* data)
8902 tree vtable = BINFO_VTABLE (binfo);
8904 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
8905 /* If this class has no vtable, none of its bases do. */
8906 return dfs_skip_bases;
8908 if (!vtable)
8909 /* This might be a primary base, so have no vtable in this
8910 hierarchy. */
8911 return NULL_TREE;
8913 /* If we scribbled the construction vtable vptr into BINFO, clear it
8914 out now. */
8915 if (TREE_CODE (vtable) == TREE_LIST
8916 && (TREE_PURPOSE (vtable) == (tree) data))
8917 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
8919 return NULL_TREE;
8922 /* Build the construction vtable group for BINFO which is in the
8923 hierarchy dominated by T. */
8925 static void
8926 build_ctor_vtbl_group (tree binfo, tree t)
8928 tree type;
8929 tree vtbl;
8930 tree id;
8931 tree vbase;
8932 vec<constructor_elt, va_gc> *v;
8934 /* See if we've already created this construction vtable group. */
8935 id = mangle_ctor_vtbl_for_type (t, binfo);
8936 if (get_global_binding (id))
8937 return;
8939 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
8940 /* Build a version of VTBL (with the wrong type) for use in
8941 constructing the addresses of secondary vtables in the
8942 construction vtable group. */
8943 vtbl = build_vtable (t, id, ptr_type_node);
8944 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
8945 /* Don't export construction vtables from shared libraries. Even on
8946 targets that don't support hidden visibility, this tells
8947 can_refer_decl_in_current_unit_p not to assume that it's safe to
8948 access from a different compilation unit (bz 54314). */
8949 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN;
8950 DECL_VISIBILITY_SPECIFIED (vtbl) = true;
8952 v = NULL;
8953 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
8954 binfo, vtbl, t, &v);
8956 /* Add the vtables for each of our virtual bases using the vbase in T
8957 binfo. */
8958 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
8959 vbase;
8960 vbase = TREE_CHAIN (vbase))
8962 tree b;
8964 if (!BINFO_VIRTUAL_P (vbase))
8965 continue;
8966 b = copied_binfo (vbase, binfo);
8968 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v);
8971 /* Figure out the type of the construction vtable. */
8972 type = build_array_of_n_type (vtable_entry_type, v->length ());
8973 layout_type (type);
8974 TREE_TYPE (vtbl) = type;
8975 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
8976 layout_decl (vtbl, 0);
8978 /* Initialize the construction vtable. */
8979 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
8980 initialize_artificial_var (vtbl, v);
8981 dump_vtable (t, binfo, vtbl);
8984 /* Add the vtbl initializers for BINFO (and its bases other than
8985 non-virtual primaries) to the list of INITS. BINFO is in the
8986 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
8987 the constructor the vtbl inits should be accumulated for. (If this
8988 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
8989 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
8990 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
8991 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
8992 but are not necessarily the same in terms of layout. */
8994 static void
8995 accumulate_vtbl_inits (tree binfo,
8996 tree orig_binfo,
8997 tree rtti_binfo,
8998 tree vtbl,
8999 tree t,
9000 vec<constructor_elt, va_gc> **inits)
9002 int i;
9003 tree base_binfo;
9004 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9006 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
9008 /* If it doesn't have a vptr, we don't do anything. */
9009 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
9010 return;
9012 /* If we're building a construction vtable, we're not interested in
9013 subobjects that don't require construction vtables. */
9014 if (ctor_vtbl_p
9015 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
9016 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
9017 return;
9019 /* Build the initializers for the BINFO-in-T vtable. */
9020 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits);
9022 /* Walk the BINFO and its bases. We walk in preorder so that as we
9023 initialize each vtable we can figure out at what offset the
9024 secondary vtable lies from the primary vtable. We can't use
9025 dfs_walk here because we need to iterate through bases of BINFO
9026 and RTTI_BINFO simultaneously. */
9027 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9029 /* Skip virtual bases. */
9030 if (BINFO_VIRTUAL_P (base_binfo))
9031 continue;
9032 accumulate_vtbl_inits (base_binfo,
9033 BINFO_BASE_BINFO (orig_binfo, i),
9034 rtti_binfo, vtbl, t,
9035 inits);
9039 /* Called from accumulate_vtbl_inits. Adds the initializers for the
9040 BINFO vtable to L. */
9042 static void
9043 dfs_accumulate_vtbl_inits (tree binfo,
9044 tree orig_binfo,
9045 tree rtti_binfo,
9046 tree orig_vtbl,
9047 tree t,
9048 vec<constructor_elt, va_gc> **l)
9050 tree vtbl = NULL_TREE;
9051 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9052 int n_inits;
9054 if (ctor_vtbl_p
9055 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
9057 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
9058 primary virtual base. If it is not the same primary in
9059 the hierarchy of T, we'll need to generate a ctor vtable
9060 for it, to place at its location in T. If it is the same
9061 primary, we still need a VTT entry for the vtable, but it
9062 should point to the ctor vtable for the base it is a
9063 primary for within the sub-hierarchy of RTTI_BINFO.
9065 There are three possible cases:
9067 1) We are in the same place.
9068 2) We are a primary base within a lost primary virtual base of
9069 RTTI_BINFO.
9070 3) We are primary to something not a base of RTTI_BINFO. */
9072 tree b;
9073 tree last = NULL_TREE;
9075 /* First, look through the bases we are primary to for RTTI_BINFO
9076 or a virtual base. */
9077 b = binfo;
9078 while (BINFO_PRIMARY_P (b))
9080 b = BINFO_INHERITANCE_CHAIN (b);
9081 last = b;
9082 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9083 goto found;
9085 /* If we run out of primary links, keep looking down our
9086 inheritance chain; we might be an indirect primary. */
9087 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
9088 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
9089 break;
9090 found:
9092 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
9093 base B and it is a base of RTTI_BINFO, this is case 2. In
9094 either case, we share our vtable with LAST, i.e. the
9095 derived-most base within B of which we are a primary. */
9096 if (b == rtti_binfo
9097 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo))))
9098 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
9099 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
9100 binfo_ctor_vtable after everything's been set up. */
9101 vtbl = last;
9103 /* Otherwise, this is case 3 and we get our own. */
9105 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
9106 return;
9108 n_inits = vec_safe_length (*l);
9110 if (!vtbl)
9112 tree index;
9113 int non_fn_entries;
9115 /* Add the initializer for this vtable. */
9116 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
9117 &non_fn_entries, l);
9119 /* Figure out the position to which the VPTR should point. */
9120 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl);
9121 index = size_binop (MULT_EXPR,
9122 TYPE_SIZE_UNIT (vtable_entry_type),
9123 size_int (non_fn_entries + n_inits));
9124 vtbl = fold_build_pointer_plus (vtbl, index);
9127 if (ctor_vtbl_p)
9128 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
9129 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
9130 straighten this out. */
9131 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
9132 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
9133 /* Throw away any unneeded intializers. */
9134 (*l)->truncate (n_inits);
9135 else
9136 /* For an ordinary vtable, set BINFO_VTABLE. */
9137 BINFO_VTABLE (binfo) = vtbl;
9140 static GTY(()) tree abort_fndecl_addr;
9141 static GTY(()) tree dvirt_fn;
9143 /* Construct the initializer for BINFO's virtual function table. BINFO
9144 is part of the hierarchy dominated by T. If we're building a
9145 construction vtable, the ORIG_BINFO is the binfo we should use to
9146 find the actual function pointers to put in the vtable - but they
9147 can be overridden on the path to most-derived in the graph that
9148 ORIG_BINFO belongs. Otherwise,
9149 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
9150 BINFO that should be indicated by the RTTI information in the
9151 vtable; it will be a base class of T, rather than T itself, if we
9152 are building a construction vtable.
9154 The value returned is a TREE_LIST suitable for wrapping in a
9155 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
9156 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
9157 number of non-function entries in the vtable.
9159 It might seem that this function should never be called with a
9160 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
9161 base is always subsumed by a derived class vtable. However, when
9162 we are building construction vtables, we do build vtables for
9163 primary bases; we need these while the primary base is being
9164 constructed. */
9166 static void
9167 build_vtbl_initializer (tree binfo,
9168 tree orig_binfo,
9169 tree t,
9170 tree rtti_binfo,
9171 int* non_fn_entries_p,
9172 vec<constructor_elt, va_gc> **inits)
9174 tree v;
9175 vtbl_init_data vid;
9176 unsigned ix, jx;
9177 tree vbinfo;
9178 vec<tree, va_gc> *vbases;
9179 constructor_elt *e;
9181 /* Initialize VID. */
9182 memset (&vid, 0, sizeof (vid));
9183 vid.binfo = binfo;
9184 vid.derived = t;
9185 vid.rtti_binfo = rtti_binfo;
9186 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
9187 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
9188 vid.generate_vcall_entries = true;
9189 /* The first vbase or vcall offset is at index -3 in the vtable. */
9190 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
9192 /* Add entries to the vtable for RTTI. */
9193 build_rtti_vtbl_entries (binfo, &vid);
9195 /* Create an array for keeping track of the functions we've
9196 processed. When we see multiple functions with the same
9197 signature, we share the vcall offsets. */
9198 vec_alloc (vid.fns, 32);
9199 /* Add the vcall and vbase offset entries. */
9200 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
9202 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
9203 build_vbase_offset_vtbl_entries. */
9204 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
9205 vec_safe_iterate (vbases, ix, &vbinfo); ix++)
9206 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
9208 /* If the target requires padding between data entries, add that now. */
9209 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
9211 int n_entries = vec_safe_length (vid.inits);
9213 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries);
9215 /* Move data entries into their new positions and add padding
9216 after the new positions. Iterate backwards so we don't
9217 overwrite entries that we would need to process later. */
9218 for (ix = n_entries - 1;
9219 vid.inits->iterate (ix, &e);
9220 ix--)
9222 int j;
9223 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix
9224 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1));
9226 (*vid.inits)[new_position] = *e;
9228 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j)
9230 constructor_elt *f = &(*vid.inits)[new_position - j];
9231 f->index = NULL_TREE;
9232 f->value = build1 (NOP_EXPR, vtable_entry_type,
9233 null_pointer_node);
9238 if (non_fn_entries_p)
9239 *non_fn_entries_p = vec_safe_length (vid.inits);
9241 /* The initializers for virtual functions were built up in reverse
9242 order. Straighten them out and add them to the running list in one
9243 step. */
9244 jx = vec_safe_length (*inits);
9245 vec_safe_grow (*inits, jx + vid.inits->length ());
9247 for (ix = vid.inits->length () - 1;
9248 vid.inits->iterate (ix, &e);
9249 ix--, jx++)
9250 (**inits)[jx] = *e;
9252 /* Go through all the ordinary virtual functions, building up
9253 initializers. */
9254 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
9256 tree delta;
9257 tree vcall_index;
9258 tree fn, fn_original;
9259 tree init = NULL_TREE;
9261 fn = BV_FN (v);
9262 fn_original = fn;
9263 if (DECL_THUNK_P (fn))
9265 if (!DECL_NAME (fn))
9266 finish_thunk (fn);
9267 if (THUNK_ALIAS (fn))
9269 fn = THUNK_ALIAS (fn);
9270 BV_FN (v) = fn;
9272 fn_original = THUNK_TARGET (fn);
9275 /* If the only definition of this function signature along our
9276 primary base chain is from a lost primary, this vtable slot will
9277 never be used, so just zero it out. This is important to avoid
9278 requiring extra thunks which cannot be generated with the function.
9280 We first check this in update_vtable_entry_for_fn, so we handle
9281 restored primary bases properly; we also need to do it here so we
9282 zero out unused slots in ctor vtables, rather than filling them
9283 with erroneous values (though harmless, apart from relocation
9284 costs). */
9285 if (BV_LOST_PRIMARY (v))
9286 init = size_zero_node;
9288 if (! init)
9290 /* Pull the offset for `this', and the function to call, out of
9291 the list. */
9292 delta = BV_DELTA (v);
9293 vcall_index = BV_VCALL_INDEX (v);
9295 gcc_assert (TREE_CODE (delta) == INTEGER_CST);
9296 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9298 /* You can't call an abstract virtual function; it's abstract.
9299 So, we replace these functions with __pure_virtual. */
9300 if (DECL_PURE_VIRTUAL_P (fn_original))
9302 fn = abort_fndecl;
9303 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9305 if (abort_fndecl_addr == NULL)
9306 abort_fndecl_addr
9307 = fold_convert (vfunc_ptr_type_node,
9308 build_fold_addr_expr (fn));
9309 init = abort_fndecl_addr;
9312 /* Likewise for deleted virtuals. */
9313 else if (DECL_DELETED_FN (fn_original))
9315 if (!dvirt_fn)
9317 tree name = get_identifier ("__cxa_deleted_virtual");
9318 dvirt_fn = get_global_binding (name);
9319 if (!dvirt_fn)
9320 dvirt_fn = push_library_fn
9321 (name,
9322 build_function_type_list (void_type_node, NULL_TREE),
9323 NULL_TREE, ECF_NORETURN | ECF_COLD);
9325 fn = dvirt_fn;
9326 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9327 init = fold_convert (vfunc_ptr_type_node,
9328 build_fold_addr_expr (fn));
9330 else
9332 if (!integer_zerop (delta) || vcall_index)
9334 fn = make_thunk (fn, /*this_adjusting=*/1,
9335 delta, vcall_index);
9336 if (!DECL_NAME (fn))
9337 finish_thunk (fn);
9339 /* Take the address of the function, considering it to be of an
9340 appropriate generic type. */
9341 if (!TARGET_VTABLE_USES_DESCRIPTORS)
9342 init = fold_convert (vfunc_ptr_type_node,
9343 build_fold_addr_expr (fn));
9344 /* Don't refer to a virtual destructor from a constructor
9345 vtable or a vtable for an abstract class, since destroying
9346 an object under construction is undefined behavior and we
9347 don't want it to be considered a candidate for speculative
9348 devirtualization. But do create the thunk for ABI
9349 compliance. */
9350 if (DECL_DESTRUCTOR_P (fn_original)
9351 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original))
9352 || orig_binfo != binfo))
9353 init = size_zero_node;
9357 /* And add it to the chain of initializers. */
9358 if (TARGET_VTABLE_USES_DESCRIPTORS)
9360 int i;
9361 if (init == size_zero_node)
9362 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9363 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9364 else
9365 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
9367 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
9368 fn, build_int_cst (NULL_TREE, i));
9369 TREE_CONSTANT (fdesc) = 1;
9371 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc);
9374 else
9375 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init);
9379 /* Adds to vid->inits the initializers for the vbase and vcall
9380 offsets in BINFO, which is in the hierarchy dominated by T. */
9382 static void
9383 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
9385 tree b;
9387 /* If this is a derived class, we must first create entries
9388 corresponding to the primary base class. */
9389 b = get_primary_binfo (binfo);
9390 if (b)
9391 build_vcall_and_vbase_vtbl_entries (b, vid);
9393 /* Add the vbase entries for this base. */
9394 build_vbase_offset_vtbl_entries (binfo, vid);
9395 /* Add the vcall entries for this base. */
9396 build_vcall_offset_vtbl_entries (binfo, vid);
9399 /* Returns the initializers for the vbase offset entries in the vtable
9400 for BINFO (which is part of the class hierarchy dominated by T), in
9401 reverse order. VBASE_OFFSET_INDEX gives the vtable index
9402 where the next vbase offset will go. */
9404 static void
9405 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9407 tree vbase;
9408 tree t;
9409 tree non_primary_binfo;
9411 /* If there are no virtual baseclasses, then there is nothing to
9412 do. */
9413 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
9414 return;
9416 t = vid->derived;
9418 /* We might be a primary base class. Go up the inheritance hierarchy
9419 until we find the most derived class of which we are a primary base:
9420 it is the offset of that which we need to use. */
9421 non_primary_binfo = binfo;
9422 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
9424 tree b;
9426 /* If we have reached a virtual base, then it must be a primary
9427 base (possibly multi-level) of vid->binfo, or we wouldn't
9428 have called build_vcall_and_vbase_vtbl_entries for it. But it
9429 might be a lost primary, so just skip down to vid->binfo. */
9430 if (BINFO_VIRTUAL_P (non_primary_binfo))
9432 non_primary_binfo = vid->binfo;
9433 break;
9436 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
9437 if (get_primary_binfo (b) != non_primary_binfo)
9438 break;
9439 non_primary_binfo = b;
9442 /* Go through the virtual bases, adding the offsets. */
9443 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
9444 vbase;
9445 vbase = TREE_CHAIN (vbase))
9447 tree b;
9448 tree delta;
9450 if (!BINFO_VIRTUAL_P (vbase))
9451 continue;
9453 /* Find the instance of this virtual base in the complete
9454 object. */
9455 b = copied_binfo (vbase, binfo);
9457 /* If we've already got an offset for this virtual base, we
9458 don't need another one. */
9459 if (BINFO_VTABLE_PATH_MARKED (b))
9460 continue;
9461 BINFO_VTABLE_PATH_MARKED (b) = 1;
9463 /* Figure out where we can find this vbase offset. */
9464 delta = size_binop (MULT_EXPR,
9465 vid->index,
9466 fold_convert (ssizetype,
9467 TYPE_SIZE_UNIT (vtable_entry_type)));
9468 if (vid->primary_vtbl_p)
9469 BINFO_VPTR_FIELD (b) = delta;
9471 if (binfo != TYPE_BINFO (t))
9472 /* The vbase offset had better be the same. */
9473 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
9475 /* The next vbase will come at a more negative offset. */
9476 vid->index = size_binop (MINUS_EXPR, vid->index,
9477 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9479 /* The initializer is the delta from BINFO to this virtual base.
9480 The vbase offsets go in reverse inheritance-graph order, and
9481 we are walking in inheritance graph order so these end up in
9482 the right order. */
9483 delta = size_diffop_loc (input_location,
9484 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
9486 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE,
9487 fold_build1_loc (input_location, NOP_EXPR,
9488 vtable_entry_type, delta));
9492 /* Adds the initializers for the vcall offset entries in the vtable
9493 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
9494 to VID->INITS. */
9496 static void
9497 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
9499 /* We only need these entries if this base is a virtual base. We
9500 compute the indices -- but do not add to the vtable -- when
9501 building the main vtable for a class. */
9502 if (binfo == TYPE_BINFO (vid->derived)
9503 || (BINFO_VIRTUAL_P (binfo)
9504 /* If BINFO is RTTI_BINFO, then (since BINFO does not
9505 correspond to VID->DERIVED), we are building a primary
9506 construction virtual table. Since this is a primary
9507 virtual table, we do not need the vcall offsets for
9508 BINFO. */
9509 && binfo != vid->rtti_binfo))
9511 /* We need a vcall offset for each of the virtual functions in this
9512 vtable. For example:
9514 class A { virtual void f (); };
9515 class B1 : virtual public A { virtual void f (); };
9516 class B2 : virtual public A { virtual void f (); };
9517 class C: public B1, public B2 { virtual void f (); };
9519 A C object has a primary base of B1, which has a primary base of A. A
9520 C also has a secondary base of B2, which no longer has a primary base
9521 of A. So the B2-in-C construction vtable needs a secondary vtable for
9522 A, which will adjust the A* to a B2* to call f. We have no way of
9523 knowing what (or even whether) this offset will be when we define B2,
9524 so we store this "vcall offset" in the A sub-vtable and look it up in
9525 a "virtual thunk" for B2::f.
9527 We need entries for all the functions in our primary vtable and
9528 in our non-virtual bases' secondary vtables. */
9529 vid->vbase = binfo;
9530 /* If we are just computing the vcall indices -- but do not need
9531 the actual entries -- not that. */
9532 if (!BINFO_VIRTUAL_P (binfo))
9533 vid->generate_vcall_entries = false;
9534 /* Now, walk through the non-virtual bases, adding vcall offsets. */
9535 add_vcall_offset_vtbl_entries_r (binfo, vid);
9539 /* Build vcall offsets, starting with those for BINFO. */
9541 static void
9542 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
9544 int i;
9545 tree primary_binfo;
9546 tree base_binfo;
9548 /* Don't walk into virtual bases -- except, of course, for the
9549 virtual base for which we are building vcall offsets. Any
9550 primary virtual base will have already had its offsets generated
9551 through the recursion in build_vcall_and_vbase_vtbl_entries. */
9552 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
9553 return;
9555 /* If BINFO has a primary base, process it first. */
9556 primary_binfo = get_primary_binfo (binfo);
9557 if (primary_binfo)
9558 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
9560 /* Add BINFO itself to the list. */
9561 add_vcall_offset_vtbl_entries_1 (binfo, vid);
9563 /* Scan the non-primary bases of BINFO. */
9564 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
9565 if (base_binfo != primary_binfo)
9566 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
9569 /* Called from build_vcall_offset_vtbl_entries_r. */
9571 static void
9572 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
9574 /* Make entries for the rest of the virtuals. */
9575 tree orig_fn;
9577 /* The ABI requires that the methods be processed in declaration
9578 order. */
9579 for (orig_fn = TYPE_FIELDS (BINFO_TYPE (binfo));
9580 orig_fn;
9581 orig_fn = DECL_CHAIN (orig_fn))
9582 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn))
9583 add_vcall_offset (orig_fn, binfo, vid);
9586 /* Add a vcall offset entry for ORIG_FN to the vtable. */
9588 static void
9589 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
9591 size_t i;
9592 tree vcall_offset;
9593 tree derived_entry;
9595 /* If there is already an entry for a function with the same
9596 signature as FN, then we do not need a second vcall offset.
9597 Check the list of functions already present in the derived
9598 class vtable. */
9599 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry)
9601 if (same_signature_p (derived_entry, orig_fn)
9602 /* We only use one vcall offset for virtual destructors,
9603 even though there are two virtual table entries. */
9604 || (DECL_DESTRUCTOR_P (derived_entry)
9605 && DECL_DESTRUCTOR_P (orig_fn)))
9606 return;
9609 /* If we are building these vcall offsets as part of building
9610 the vtable for the most derived class, remember the vcall
9611 offset. */
9612 if (vid->binfo == TYPE_BINFO (vid->derived))
9614 tree_pair_s elt = {orig_fn, vid->index};
9615 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt);
9618 /* The next vcall offset will be found at a more negative
9619 offset. */
9620 vid->index = size_binop (MINUS_EXPR, vid->index,
9621 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
9623 /* Keep track of this function. */
9624 vec_safe_push (vid->fns, orig_fn);
9626 if (vid->generate_vcall_entries)
9628 tree base;
9629 tree fn;
9631 /* Find the overriding function. */
9632 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
9633 if (fn == error_mark_node)
9634 vcall_offset = build_zero_cst (vtable_entry_type);
9635 else
9637 base = TREE_VALUE (fn);
9639 /* The vbase we're working on is a primary base of
9640 vid->binfo. But it might be a lost primary, so its
9641 BINFO_OFFSET might be wrong, so we just use the
9642 BINFO_OFFSET from vid->binfo. */
9643 vcall_offset = size_diffop_loc (input_location,
9644 BINFO_OFFSET (base),
9645 BINFO_OFFSET (vid->binfo));
9646 vcall_offset = fold_build1_loc (input_location,
9647 NOP_EXPR, vtable_entry_type,
9648 vcall_offset);
9650 /* Add the initializer to the vtable. */
9651 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset);
9655 /* Return vtbl initializers for the RTTI entries corresponding to the
9656 BINFO's vtable. The RTTI entries should indicate the object given
9657 by VID->rtti_binfo. */
9659 static void
9660 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
9662 tree b;
9663 tree t;
9664 tree offset;
9665 tree decl;
9666 tree init;
9668 t = BINFO_TYPE (vid->rtti_binfo);
9670 /* To find the complete object, we will first convert to our most
9671 primary base, and then add the offset in the vtbl to that value. */
9672 b = most_primary_binfo (binfo);
9673 offset = size_diffop_loc (input_location,
9674 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
9676 /* The second entry is the address of the typeinfo object. */
9677 if (flag_rtti)
9678 decl = build_address (get_tinfo_decl (t));
9679 else
9680 decl = integer_zero_node;
9682 /* Convert the declaration to a type that can be stored in the
9683 vtable. */
9684 init = build_nop (vfunc_ptr_type_node, decl);
9685 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9687 /* Add the offset-to-top entry. It comes earlier in the vtable than
9688 the typeinfo entry. Convert the offset to look like a
9689 function pointer, so that we can put it in the vtable. */
9690 init = build_nop (vfunc_ptr_type_node, offset);
9691 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init);
9694 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores
9695 accessibility. */
9697 bool
9698 uniquely_derived_from_p (tree parent, tree type)
9700 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none);
9701 return base && base != error_mark_node;
9704 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */
9706 bool
9707 publicly_uniquely_derived_p (tree parent, tree type)
9709 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check,
9710 NULL, tf_none);
9711 return base && base != error_mark_node;
9714 /* CTX1 and CTX2 are declaration contexts. Return the innermost common
9715 class between them, if any. */
9717 tree
9718 common_enclosing_class (tree ctx1, tree ctx2)
9720 if (!TYPE_P (ctx1) || !TYPE_P (ctx2))
9721 return NULL_TREE;
9722 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1)
9723 && ctx2 == TYPE_MAIN_VARIANT (ctx2));
9724 if (ctx1 == ctx2)
9725 return ctx1;
9726 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9727 TYPE_MARKED_P (t) = true;
9728 tree found = NULL_TREE;
9729 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t))
9730 if (TYPE_MARKED_P (t))
9732 found = t;
9733 break;
9735 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t))
9736 TYPE_MARKED_P (t) = false;
9737 return found;
9740 #include "gt-cp-class.h"